Skip to content

Commit 2fafe07

Browse files
author
nwind
committed
增加上传图片功能
1 parent 3ebea43 commit 2fafe07

11 files changed

+244
-46
lines changed

dist/kityminder.editor.css

+41
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/kityminder.editor.css.map

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/kityminder.editor.js

+67-34
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/*!
22
* ====================================================
3-
* kityminder-editor - v1.0.51 - 2016-01-22
3+
* kityminder-editor - v1.0.51 - 2016-05-09
44
* https://github.com/fex-team/kityminder-editor
55
* GitHub: https://github.com/fex-team/kityminder-editor
66
* Copyright (c) 2016 ; Licensed
@@ -427,7 +427,7 @@ _p[7] = {
427427
/**
428428
* @fileOverview
429429
*
430-
* 用于拖拽节点是屏蔽键盘事件
430+
* 用于拖拽节点时屏蔽键盘事件
431431
*
432432
* @author: techird
433433
* @copyright: Baidu FEX, 2014
@@ -454,7 +454,6 @@ _p[8] = {
454454
});
455455
}
456456
var downX, downY;
457-
var editorRect;
458457
var MOUSE_HAS_DOWN = 0;
459458
var MOUSE_HAS_UP = 1;
460459
var flag = MOUSE_HAS_UP;
@@ -463,6 +462,7 @@ _p[8] = {
463462
var frame;
464463
function move(direction, speed) {
465464
if (!direction) {
465+
freeHorizen = freeVirtical = false;
466466
frame && kity.releaseFrame(frame);
467467
frame = null;
468468
return;
@@ -513,42 +513,40 @@ _p[8] = {
513513
downY = e.originEvent.clientY;
514514
maxX = minder.getPaper().container.clientWidth;
515515
maxY = minder.getPaper().container.clientHeight;
516-
editorRect = minder.getPaper().container.getBoundingClientRect();
517516
});
518517
minder.on("mousemove", function(e) {
519-
if (flag == MOUSE_HAS_DOWN && minder.getSelectedNode() && (Math.abs(downX - e.originEvent.clientX) > 10 || Math.abs(downY - e.originEvent.clientY) > 10)) {
520-
if (fsm.state() === "drag") {
521-
osx = e.originEvent.clientX;
522-
osy = e.originEvent.clientY - editorRect.top;
523-
if (osx < 10) {
524-
move("right", 10 - osx);
525-
} else if (osx > maxX - 10) {
526-
move("left", 10 + osx - maxX);
527-
} else {
528-
freeHorizen = true;
529-
}
530-
if (osy < 10) {
531-
move("bottom", osy);
532-
} else if (osy > maxY - 10) {
533-
move("top", 10 + osy - maxY);
534-
} else {
535-
freeVirtical = true;
536-
}
537-
if (freeHorizen && freeVirtical) {
538-
freeHorizen = freeVirtical = false;
539-
move(false);
540-
}
518+
if (fsm.state() === "drag" && flag == MOUSE_HAS_DOWN && minder.getSelectedNode() && (Math.abs(downX - e.originEvent.clientX) > 10 || Math.abs(downY - e.originEvent.clientY) > 10)) {
519+
osx = e.originEvent.offsetX;
520+
osy = e.originEvent.offsetY;
521+
if (osx < 10) {
522+
move("right", 10 - osx);
523+
} else if (osx > maxX - 10) {
524+
move("left", 10 + osx - maxX);
541525
} else {
542-
if (fsm.state() == "hotbox") {
543-
hotbox.active(Hotbox.STATE_IDLE);
544-
}
545-
return fsm.jump("drag", "user-drag");
526+
freeHorizen = true;
527+
}
528+
if (osy < 10) {
529+
move("bottom", osy);
530+
} else if (osy > maxY - 10) {
531+
move("top", 10 + osy - maxY);
532+
} else {
533+
freeVirtical = true;
534+
}
535+
if (freeHorizen && freeVirtical) {
536+
move(false);
546537
}
547538
}
539+
if (fsm.state() != "drag" && flag == MOUSE_HAS_DOWN && minder.getSelectedNode() && (Math.abs(downX - e.originEvent.clientX) > 10 || Math.abs(downY - e.originEvent.clientY) > 10)) {
540+
if (fsm.state() == "hotbox") {
541+
hotbox.active(Hotbox.STATE_IDLE);
542+
}
543+
return fsm.jump("drag", "user-drag");
544+
}
548545
});
549546
document.body.onmouseup = function(e) {
550547
flag = MOUSE_HAS_UP;
551548
if (fsm.state() == "drag") {
549+
move(false);
552550
return fsm.jump("normal", "drag-finish");
553551
}
554552
};
@@ -1171,6 +1169,12 @@ _p[12] = {
11711169
var node = minder.getSelectedNode();
11721170
textNodes = commitInputText(textNodes);
11731171
commitInputNode(node, textNodes);
1172+
if (node.type == "root") {
1173+
var rootText = minder.getRoot().getText();
1174+
minder.fire("initChangeRoot", {
1175+
text: rootText
1176+
});
1177+
}
11741178
}
11751179
function exitInputMode() {
11761180
receiverElement.classList.remove("input");
@@ -2202,7 +2206,7 @@ angular.module('kityminderEditor').run(['$templateCache', function($templateCach
22022206

22032207

22042208
$templateCache.put('ui/dialog/image/image.tpl.html',
2205-
"<div class=\"modal-header\"><h3 class=\"modal-title\">图片</h3></div><div class=\"modal-body\"><tabset><tab heading=\"图片搜索\"><form class=\"form-inline\"><div class=\"form-group\"><label for=\"search-keyword\">关键词:</label><input type=\"text\" class=\"form-control\" ng-model=\"data.searchKeyword2\" id=\"search-keyword\" placeholder=\"请输入搜索的关键词\"></div><button class=\"btn btn-primary\" ng-click=\"searchImage()\">百度一下</button></form><div class=\"search-result\" id=\"search-result\"><ul><li ng-repeat=\"image in list\" id=\"{{ 'img-item' + $index }}\" ng-class=\"{'selected' : isSelected}\" ng-click=\"selectImage($event)\"><img id=\"{{ 'img-' + $index }}\" ng-src=\"{{ image.src || '' }}\" alt=\"{{ image.title }}\" onerror=\"this.parentNode.removeChild(this)\"> <span>{{ image.title }}</span></li></ul></div></tab><tab heading=\"插入图片\" active=\"true\"><form><div class=\"form-group\" ng-class=\"{true: 'has-success', false: 'has-error'}[urlPassed]\"><label for=\"image-url\">链接地址:</label><input type=\"text\" class=\"form-control\" ng-model=\"data.url\" ng-blur=\"urlPassed = data.R_URL.test(data.url)\" ng-focus=\"this.value = data.url\" ng-keydown=\"shortCut($event)\" id=\"image-url\" placeholder=\"必填:以 http(s):// 开头\"></div><div class=\"form-group\" ng-class=\"{'has-success' : titlePassed}\"><label for=\"image-title\">提示文本:</label><input type=\"text\" class=\"form-control\" ng-model=\"data.title\" ng-blur=\"titlePassed = true\" id=\"image-title\" placeholder=\"选填:鼠标在图片上悬停时提示的文本\"></div><div class=\"form-group\"><label for=\"image-preview\">图片预览:</label><img class=\"image-preview\" id=\"image-preview\" ng-src=\"{{ data.url }}\" alt=\"{{ data.title }}\"></div></form></tab></tabset></div><div class=\"modal-footer\"><button class=\"btn btn-primary\" ng-click=\"ok()\">确定</button> <button class=\"btn btn-warning\" ng-click=\"cancel()\">取消</button></div>"
2209+
"<div class=\"modal-header\"><h3 class=\"modal-title\">图片</h3></div><div class=\"modal-body\"><tabset><tab heading=\"图片搜索\"><form class=\"form-inline\"><div class=\"form-group\"><label for=\"search-keyword\">关键词:</label><input type=\"text\" class=\"form-control\" ng-model=\"data.searchKeyword2\" id=\"search-keyword\" placeholder=\"请输入搜索的关键词\"></div><button class=\"btn btn-primary\" ng-click=\"searchImage()\">百度一下</button></form><div class=\"search-result\" id=\"search-result\"><ul><li ng-repeat=\"image in list\" id=\"{{ 'img-item' + $index }}\" ng-class=\"{'selected' : isSelected}\" ng-click=\"selectImage($event)\"><img id=\"{{ 'img-' + $index }}\" ng-src=\"{{ image.src || '' }}\" alt=\"{{ image.title }}\" onerror=\"this.parentNode.removeChild(this)\"> <span>{{ image.title }}</span></li></ul></div></tab><tab heading=\"插入图片地址\"><form><div class=\"form-group\" ng-class=\"{true: 'has-success', false: 'has-error'}[urlPassed]\"><label for=\"image-url\">链接地址:</label><input type=\"text\" class=\"form-control\" ng-model=\"data.url\" ng-blur=\"urlPassed = data.R_URL.test(data.url)\" ng-focus=\"this.value = data.url\" ng-keydown=\"shortCut($event)\" id=\"image-url\" placeholder=\"必填:以 http(s):// 开头\"></div><div class=\"form-group\" ng-class=\"{'has-success' : titlePassed}\"><label for=\"image-title\">提示文本:</label><input type=\"text\" class=\"form-control\" ng-model=\"data.title\" ng-blur=\"titlePassed = true\" id=\"image-title\" placeholder=\"选填:鼠标在图片上悬停时提示的文本\"></div><div class=\"form-group\"><label for=\"image-preview\">图片预览:</label><img class=\"image-preview\" id=\"image-preview\" ng-src=\"{{ data.url }}\" alt=\"{{ data.title }}\"></div></form></tab><tab heading=\"上传图片\" active=\"true\"><form><div class=\"form-group\"><input type=\"file\" name=\"upload-image\" id=\"upload-image\" class=\"upload-image\" onchange=\"angular.element(this).scope().uploadImage()\"><label for=\"upload-image\"><svg xmlns=\"http://www.w3.org/2000/svg\" width=\"20\" height=\"17\" viewbox=\"0 0 20 17\"><path d=\"M10 0l-5.2 4.9h3.3v5.1h3.8v-5.1h3.3l-5.2-4.9zm9.3 11.5l-3.2-2.1h-2l3.4 2.6h-3.5c-.1 0-.2.1-.2.1l-.8 2.3h-6l-.8-2.2c-.1-.1-.1-.2-.2-.2h-3.6l3.4-2.6h-2l-3.2 2.1c-.4.3-.7 1-.6 1.5l.6 3.1c.1.5.7.9 1.2.9h16.3c.6 0 1.1-.4 1.3-.9l.6-3.1c.1-.5-.2-1.2-.7-1.5z\"></svg> <span>选择文件&hellip;</span></label></div><div class=\"form-group\" ng-class=\"{'has-success' : titlePassed}\"><label for=\"image-title\">提示文本:</label><input type=\"text\" class=\"form-control\" ng-model=\"data.title\" ng-blur=\"titlePassed = true\" id=\"image-title\" placeholder=\"选填:鼠标在图片上悬停时提示的文本\"></div><div class=\"form-group\"><label for=\"image-preview\">图片预览:</label><img class=\"image-preview\" id=\"image-preview\" ng-src=\"{{ data.url }}\" alt=\"{{ data.title }}\"></div></form></tab></tabset></div><div class=\"modal-footer\"><button class=\"btn btn-primary\" ng-click=\"ok()\">确定</button> <button class=\"btn btn-warning\" ng-click=\"cancel()\">取消</button></div>"
22062210
);
22072211

22082212
}]);
@@ -3114,6 +3118,17 @@ angular.module('kityminderEditor')
31143118
$scope.data.title = targetImg.attr('alt');
31153119
};
31163120

3121+
// 自动上传图片,后端需要直接返回图片 URL
3122+
$scope.uploadImage = function() {
3123+
var fileInput = $('#upload-image');
3124+
if (/^.*\.(jpg|JPG|jpeg|JPEG|gif|GIF|png|PNG)$/.test(fileInput.val())) {
3125+
var file = fileInput[0].files[0];
3126+
uploadFile($scope, file);
3127+
} else {
3128+
alert("后缀只能是 jpg、gif 及 png");
3129+
}
3130+
}
3131+
31173132
$scope.shortCut = function(e) {
31183133
e.stopPropagation();
31193134

@@ -3134,8 +3149,11 @@ angular.module('kityminderEditor')
31343149
$scope.urlPassed = false;
31353150

31363151
var $imageUrl = $('#image-url');
3137-
$imageUrl.focus();
3138-
$imageUrl[0].setSelectionRange(0, $scope.data.url.length);
3152+
if ($imageUrl) {
3153+
$imageUrl.focus();
3154+
$imageUrl[0].setSelectionRange(0, $scope.data.url.length);
3155+
}
3156+
31393157
}
31403158

31413159
editor.receiver.selectAll();
@@ -3146,13 +3164,28 @@ angular.module('kityminderEditor')
31463164
editor.receiver.selectAll();
31473165
};
31483166

3149-
function getImageData(){
3167+
function getImageData(file) {
31503168
var key = $scope.data.searchKeyword2;
31513169
var currentTime = new Date();
31523170
var url = 'http://image.baidu.com/search/acjson?tn=resultjson_com&ipn=rj&ct=201326592&fp=result&queryWord='+ key +'&cl=2&lm=-1&ie=utf-8&oe=utf-8&st=-1&ic=0&word='+ key +'&face=0&istype=2&nc=1&pn=60&rn=60&gsm=3c&'+ currentTime.getTime() +'=&callback=JSON_CALLBACK';
31533171

31543172
return $http.jsonp(url);
31553173
}
3174+
3175+
function uploadFile($scope, file) {
3176+
var url = '/upload.php';
3177+
var xhr = new XMLHttpRequest();
3178+
var fd = new FormData();
3179+
xhr.open("POST", url, true);
3180+
xhr.onreadystatechange = function() {
3181+
if (xhr.readyState == 4 && xhr.status == 200) {
3182+
$scope.data.url = xhr.responseText;
3183+
}
3184+
};
3185+
fd.append("upload_file", file);
3186+
xhr.send(fd);
3187+
3188+
}
31563189
}]);
31573190
angular.module('kityminderEditor')
31583191
.directive('appendNode', ['commandBinder', function(commandBinder) {

dist/kityminder.editor.min.css

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/kityminder.editor.min.js

+4-4
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

less/editor.less

+5-1
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ div.minder-editor-container {
105105
}
106106

107107
@import (less) "_vars.less";
108+
@import (less) "uploadImage.less";
108109
@import (less) "topTab/topTab.less";
109110
@import (less) "topTab/idea/undoRedo.less";
110111
@import (less) "topTab/idea/appendNode.less";
@@ -128,4 +129,7 @@ div.minder-editor-container {
128129
@import (less) "topTab/view/search.less";
129130
@import (less) "topTab/searchBox.less";
130131
@import (less) "_tool_group.less";
131-
@import (less) "_navigator.less";
132+
@import (less) "_navigator.less";
133+
134+
135+

less/uploadImage.less

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
.upload-image {
2+
width: 0.1px;
3+
height: 0.1px;
4+
opacity: 0;
5+
overflow: hidden;
6+
position: absolute;
7+
z-index: -1;
8+
}
9+
10+
.upload-image + label {
11+
max-width: 80%;
12+
font-size: 1.25rem;
13+
font-weight: 700;
14+
text-overflow: ellipsis;
15+
white-space: nowrap;
16+
cursor: pointer;
17+
display: inline-block;
18+
overflow: hidden;
19+
padding: 0.625rem 1.25rem;
20+
}
21+
22+
.upload-image:focus + label,
23+
.upload-image.has-focus + label {
24+
outline: 1px dotted #000;
25+
outline: -webkit-focus-ring-color auto 5px;
26+
}
27+
28+
.upload-image + label svg {
29+
width: 1em;
30+
height: 1em;
31+
vertical-align: middle;
32+
fill: currentColor;
33+
margin-top: -0.25em;
34+
margin-right: 0.25em;
35+
}
36+
37+
.upload-image + label {
38+
color: #fff;
39+
background-color: #00b7ee;
40+
}
41+
42+
.upload-image:focus + label,
43+
.upload-image.has-focus + label,
44+
.upload-image + label:hover {
45+
background-color: #00a2d4;
46+
}

ui/dialog/image/image.ctrl.js

+35-3
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,20 @@ angular.module('kityminderEditor')
5151
$scope.data.title = targetImg.attr('alt');
5252
};
5353

54+
// 自动上传图片,后端需要直接返回图片 URL
55+
$scope.uploadImage = function() {
56+
var fileInput = $('#upload-image');
57+
if (!fileInput.val()) {
58+
return;
59+
}
60+
if (/^.*\.(jpg|JPG|jpeg|JPEG|gif|GIF|png|PNG)$/.test(fileInput.val())) {
61+
var file = fileInput[0].files[0];
62+
uploadFile($scope, file);
63+
} else {
64+
alert("后缀只能是 jpg、gif 及 png");
65+
}
66+
}
67+
5468
$scope.shortCut = function(e) {
5569
e.stopPropagation();
5670

@@ -71,8 +85,11 @@ angular.module('kityminderEditor')
7185
$scope.urlPassed = false;
7286

7387
var $imageUrl = $('#image-url');
74-
$imageUrl.focus();
75-
$imageUrl[0].setSelectionRange(0, $scope.data.url.length);
88+
if ($imageUrl) {
89+
$imageUrl.focus();
90+
$imageUrl[0].setSelectionRange(0, $scope.data.url.length);
91+
}
92+
7693
}
7794

7895
editor.receiver.selectAll();
@@ -83,11 +100,26 @@ angular.module('kityminderEditor')
83100
editor.receiver.selectAll();
84101
};
85102

86-
function getImageData(){
103+
function getImageData() {
87104
var key = $scope.data.searchKeyword2;
88105
var currentTime = new Date();
89106
var url = 'http://image.baidu.com/search/acjson?tn=resultjson_com&ipn=rj&ct=201326592&fp=result&queryWord='+ key +'&cl=2&lm=-1&ie=utf-8&oe=utf-8&st=-1&ic=0&word='+ key +'&face=0&istype=2&nc=1&pn=60&rn=60&gsm=3c&'+ currentTime.getTime() +'=&callback=JSON_CALLBACK';
90107

91108
return $http.jsonp(url);
92109
}
110+
111+
function uploadFile($scope, file) {
112+
var url = '/upload.php';
113+
var xhr = new XMLHttpRequest();
114+
var fd = new FormData();
115+
xhr.open("POST", url, true);
116+
xhr.onreadystatechange = function() {
117+
if (xhr.readyState == 4 && xhr.status == 200) {
118+
$scope.data.url = xhr.responseText;
119+
}
120+
};
121+
fd.append("upload_file", file);
122+
xhr.send(fd);
123+
124+
}
93125
}]);

ui/dialog/image/image.tpl.html

+18-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ <h3 class="modal-title">图片</h3>
2020
</ul>
2121
</div>
2222
</tab>
23-
<tab heading="插入图片" active="true">
23+
<tab heading="插入图片地址">
2424
<form>
2525
<div class="form-group" ng-class="{true: 'has-success', false: 'has-error'}[urlPassed]">
2626
<label for="image-url">链接地址:</label>
@@ -43,6 +43,23 @@ <h3 class="modal-title">图片</h3>
4343
</div>
4444
</form>
4545
</tab>
46+
<tab heading="上传图片" active="true">
47+
<form>
48+
<div class="form-group">
49+
<!-- 这里不能用 ng-change -->
50+
<input type="file" name="upload-image" id="upload-image" class="upload-image" onchange="angular.element(this).scope().uploadImage()"/>
51+
<label for="upload-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="17" viewBox="0 0 20 17"><path d="M10 0l-5.2 4.9h3.3v5.1h3.8v-5.1h3.3l-5.2-4.9zm9.3 11.5l-3.2-2.1h-2l3.4 2.6h-3.5c-.1 0-.2.1-.2.1l-.8 2.3h-6l-.8-2.2c-.1-.1-.1-.2-.2-.2h-3.6l3.4-2.6h-2l-3.2 2.1c-.4.3-.7 1-.6 1.5l.6 3.1c.1.5.7.9 1.2.9h16.3c.6 0 1.1-.4 1.3-.9l.6-3.1c.1-.5-.2-1.2-.7-1.5z"/></svg> <span>选择文件&hellip;</span></label>
52+
</div>
53+
<div class="form-group" ng-class="{'has-success' : titlePassed}">
54+
<label for="image-title">提示文本:</label>
55+
<input type="text" class="form-control" ng-model="data.title" ng-blur="titlePassed = true" id="image-title" placeholder="选填:鼠标在图片上悬停时提示的文本">
56+
</div>
57+
<div class="form-group">
58+
<label for="image-preview">图片预览:</label>
59+
<img class="image-preview" id="image-preview" ng-src="{{ data.url }}" alt="{{ data.title }}"/>
60+
</div>
61+
</form>
62+
</tab>
4663
</tabset>
4764

4865
</div>

0 commit comments

Comments
 (0)