-
-
Notifications
You must be signed in to change notification settings - Fork 95
/
kityminder.editor.js
4550 lines (4109 loc) · 175 KB
/
kityminder.editor.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*!
* ====================================================
* kityminder-editor - v1.0.55 - 2016-05-26
* https://github.com/fex-team/kityminder-editor
* GitHub: https://github.com/fex-team/kityminder-editor
* Copyright (c) 2016 ; Licensed
* ====================================================
*/
(function () {
var _p = {
r: function(index) {
if (_p[index].inited) {
return _p[index].value;
}
if (typeof _p[index].value === "function") {
var module = {
exports: {}
}, returnValue = _p[index].value(null, module.exports, module);
_p[index].inited = true;
_p[index].value = returnValue;
if (returnValue !== undefined) {
return returnValue;
} else {
for (var key in module.exports) {
if (module.exports.hasOwnProperty(key)) {
_p[index].inited = true;
_p[index].value = module.exports;
return module.exports;
}
}
}
} else {
_p[index].inited = true;
return _p[index].value;
}
}
};
//src/editor.js
_p[0] = {
value: function(require, exports, module) {
/**
* 运行时
*/
var runtimes = [];
function assemble(runtime) {
runtimes.push(runtime);
}
function KMEditor(selector) {
this.selector = selector;
for (var i = 0; i < runtimes.length; i++) {
if (typeof runtimes[i] == "function") {
runtimes[i].call(this, this);
}
}
}
KMEditor.assemble = assemble;
assemble(_p.r(7));
assemble(_p.r(9));
assemble(_p.r(14));
assemble(_p.r(18));
assemble(_p.r(11));
assemble(_p.r(12));
assemble(_p.r(5));
assemble(_p.r(6));
assemble(_p.r(8));
assemble(_p.r(15));
assemble(_p.r(10));
assemble(_p.r(13));
assemble(_p.r(16));
assemble(_p.r(17));
return module.exports = KMEditor;
}
};
//src/expose-editor.js
/**
* @fileOverview
*
* 打包暴露
*
* @author: techird
* @copyright: Baidu FEX, 2014
*/
_p[1] = {
value: function(require, exports, module) {
return module.exports = kityminder.Editor = _p.r(0);
}
};
//src/hotbox.js
_p[2] = {
value: function(require, exports, module) {
return module.exports = window.HotBox;
}
};
//src/lang.js
_p[3] = {
value: function(require, exports, module) {}
};
//src/minder.js
_p[4] = {
value: function(require, exports, module) {
return module.exports = window.kityminder.Minder;
}
};
//src/runtime/clipboard-mimetype.js
/**
* @Desc: 新增一个用于处理系统ctrl+c ctrl+v等方式导入导出节点的MIMETYPE处理,如系统不支持clipboardEvent或者是FF则不初始化改class
* @Editor: Naixor
* @Date: 2015.9.21
*/
_p[5] = {
value: function(require, exports, module) {
function MimeType() {
/**
* 私有变量
*/
var SPLITOR = "\ufeff";
var MIMETYPE = {
"application/km": ""
};
var SIGN = {
"\ufeff": "SPLITOR",
"": "application/km"
};
/**
* 用于将一段纯文本封装成符合其数据格式的文本
* @method process private
* @param {MIMETYPE} mimetype 数据格式
* @param {String} text 原始文本
* @return {String} 符合该数据格式下的文本
* @example
* var str = "123";
* str = process('application/km', str); // 返回的内容再经过MimeType判断会读取出其数据格式为application/km
* process('text/plain', str); // 若接受到一个非纯文本信息,则会将其转换为新的数据格式
*/
function process(mimetype, text) {
if (!this.isPureText(text)) {
var _mimetype = this.whichMimeType(text);
if (!_mimetype) {
throw new Error("unknow mimetype!");
}
text = this.getPureText(text);
}
if (mimetype === false) {
return text;
}
return mimetype + SPLITOR + text;
}
/**
* 注册数据类型的标识
* @method registMimeTypeProtocol public
* @param {String} type 数据类型
* @param {String} sign 标识
*/
this.registMimeTypeProtocol = function(type, sign) {
if (sign && SIGN[sign]) {
throw new Error("sing has registed!");
}
if (type && !!MIMETYPE[type]) {
throw new Error("mimetype has registed!");
}
SIGN[sign] = type;
MIMETYPE[type] = sign;
};
/**
* 获取已注册数据类型的协议
* @method getMimeTypeProtocol public
* @param {String} type 数据类型
* @param {String} text|undefiend 文本内容或不传入
* @return {String|Function}
* @example
* text若不传入则直接返回对应数据格式的处理(process)方法
* 若传入文本则直接调用对应的process方法进行处理,此时返回处理后的内容
* var m = new MimeType();
* var kmprocess = m.getMimeTypeProtocol('application/km');
* kmprocess("123") === m.getMimeTypeProtocol('application/km', "123");
*
*/
this.getMimeTypeProtocol = function(type, text) {
var mimetype = MIMETYPE[type] || false;
if (text === undefined) {
return process.bind(this, mimetype);
}
return process(mimetype, text);
};
this.getSpitor = function() {
return SPLITOR;
};
this.getMimeType = function(sign) {
if (sign !== undefined) {
return SIGN[sign] || null;
}
return MIMETYPE;
};
}
MimeType.prototype.isPureText = function(text) {
return !~text.indexOf(this.getSpitor());
};
MimeType.prototype.getPureText = function(text) {
if (this.isPureText(text)) {
return text;
}
return text.split(this.getSpitor())[1];
};
MimeType.prototype.whichMimeType = function(text) {
if (this.isPureText(text)) {
return null;
}
return this.getMimeType(text.split(this.getSpitor())[0]);
};
function MimeTypeRuntime() {
if (this.minder.supportClipboardEvent && !kity.Browser.gecko) {
this.MimeType = new MimeType();
}
}
return module.exports = MimeTypeRuntime;
}
};
//src/runtime/clipboard.js
/**
* @Desc: 处理editor的clipboard事件,只在支持ClipboardEvent并且不是FF的情况下工作
* @Editor: Naixor
* @Date: 2015.9.21
*/
_p[6] = {
value: function(require, exports, module) {
function ClipboardRuntime() {
var minder = this.minder;
var Data = window.kityminder.data;
if (!minder.supportClipboardEvent || kity.Browser.gecko) {
return;
}
var fsm = this.fsm;
var receiver = this.receiver;
var MimeType = this.MimeType;
var kmencode = MimeType.getMimeTypeProtocol("application/km"), decode = Data.getRegisterProtocol("json").decode;
var _selectedNodes = [];
/*
* 增加对多节点赋值粘贴的处理
*/
function encode(nodes) {
var _nodes = [];
for (var i = 0, l = nodes.length; i < l; i++) {
_nodes.push(minder.exportNode(nodes[i]));
}
return kmencode(Data.getRegisterProtocol("json").encode(_nodes));
}
var beforeCopy = function(e) {
if (document.activeElement == receiver.element) {
var clipBoardEvent = e;
var state = fsm.state();
switch (state) {
case "input":
{
break;
}
case "normal":
{
var nodes = [].concat(minder.getSelectedNodes());
if (nodes.length) {
// 这里由于被粘贴复制的节点的id信息也都一样,故做此算法
// 这里有个疑问,使用node.getParent()或者node.parent会离奇导致出现非选中节点被渲染成选中节点,因此使用isAncestorOf,而没有使用自行回溯的方式
if (nodes.length > 1) {
var targetLevel;
nodes.sort(function(a, b) {
return a.getLevel() - b.getLevel();
});
targetLevel = nodes[0].getLevel();
if (targetLevel !== nodes[nodes.length - 1].getLevel()) {
var plevel, pnode, idx = 0, l = nodes.length, pidx = l - 1;
pnode = nodes[pidx];
while (pnode.getLevel() !== targetLevel) {
idx = 0;
while (idx < l && nodes[idx].getLevel() === targetLevel) {
if (nodes[idx].isAncestorOf(pnode)) {
nodes.splice(pidx, 1);
break;
}
idx++;
}
pidx--;
pnode = nodes[pidx];
}
}
}
var str = encode(nodes);
clipBoardEvent.clipboardData.setData("text/plain", str);
}
e.preventDefault();
break;
}
}
}
};
var beforeCut = function(e) {
if (document.activeElement == receiver.element) {
if (minder.getStatus() !== "normal") {
e.preventDefault();
return;
}
var clipBoardEvent = e;
var state = fsm.state();
switch (state) {
case "input":
{
break;
}
case "normal":
{
var nodes = minder.getSelectedNodes();
if (nodes.length) {
clipBoardEvent.clipboardData.setData("text/plain", encode(nodes));
minder.execCommand("removenode");
}
e.preventDefault();
break;
}
}
}
};
var beforePaste = function(e) {
if (document.activeElement == receiver.element) {
if (minder.getStatus() !== "normal") {
e.preventDefault();
return;
}
var clipBoardEvent = e;
var state = fsm.state();
var textData = clipBoardEvent.clipboardData.getData("text/plain");
switch (state) {
case "input":
{
// input状态下如果格式为application/km则不进行paste操作
if (!MimeType.isPureText(textData)) {
e.preventDefault();
return;
}
break;
}
case "normal":
{
/*
* 针对normal状态下通过对选中节点粘贴导入子节点文本进行单独处理
*/
var sNodes = minder.getSelectedNodes();
if (MimeType.whichMimeType(textData) === "application/km") {
var nodes = decode(MimeType.getPureText(textData));
var _node;
sNodes.forEach(function(node) {
// 由于粘贴逻辑中为了排除子节点重新排序导致逆序,因此复制的时候倒过来
for (var i = nodes.length - 1; i >= 0; i--) {
_node = minder.createNode(null, node);
minder.importNode(_node, nodes[i]);
_selectedNodes.push(_node);
node.appendChild(_node);
}
});
minder.select(_selectedNodes, true);
_selectedNodes = [];
minder.refresh();
} else {
sNodes.forEach(function(node) {
minder.Text2Children(node, textData);
});
}
e.preventDefault();
break;
}
}
}
};
/**
* 由editor的receiver统一处理全部事件,包括clipboard事件
* @Editor: Naixor
* @Date: 2015.9.24
*/
document.addEventListener("copy", beforeCopy);
document.addEventListener("cut", beforeCut);
document.addEventListener("paste", beforePaste);
}
return module.exports = ClipboardRuntime;
}
};
//src/runtime/container.js
/**
* @fileOverview
*
* 初始化编辑器的容器
*
* @author: techird
* @copyright: Baidu FEX, 2014
*/
_p[7] = {
value: function(require, exports, module) {
/**
* 最先执行的 Runtime,初始化编辑器容器
*/
function ContainerRuntime() {
var container;
if (typeof this.selector == "string") {
container = document.querySelector(this.selector);
} else {
container = this.selector;
}
if (!container) throw new Error("Invalid selector: " + this.selector);
// 这个类名用于给编辑器添加样式
container.classList.add("km-editor");
// 暴露容器给其他运行时使用
this.container = container;
}
return module.exports = ContainerRuntime;
}
};
//src/runtime/drag.js
/**
* @fileOverview
*
* 用于拖拽节点时屏蔽键盘事件
*
* @author: techird
* @copyright: Baidu FEX, 2014
*/
_p[8] = {
value: function(require, exports, module) {
var Hotbox = _p.r(2);
var Debug = _p.r(19);
var debug = new Debug("drag");
function DragRuntime() {
var fsm = this.fsm;
var minder = this.minder;
var hotbox = this.hotbox;
var receiver = this.receiver;
var receiverElement = receiver.element;
// setup everything to go
setupFsm();
// listen the fsm changes, make action.
function setupFsm() {
// when jumped to drag mode, enter
fsm.when("* -> drag", function() {});
fsm.when("drag -> *", function(exit, enter, reason) {
if (reason == "drag-finish") {}
});
}
var downX, downY;
var MOUSE_HAS_DOWN = 0;
var MOUSE_HAS_UP = 1;
var BOUND_CHECK = 20;
var flag = MOUSE_HAS_UP;
var maxX, maxY, osx, osy, containerY;
var freeHorizen = false, freeVirtical = false;
var frame;
function move(direction, speed) {
if (!direction) {
freeHorizen = freeVirtical = false;
frame && kity.releaseFrame(frame);
frame = null;
return;
}
if (!frame) {
frame = kity.requestFrame(function(direction, speed, minder) {
return function(frame) {
switch (direction) {
case "left":
minder._viewDragger.move({
x: -speed,
y: 0
}, 0);
break;
case "top":
minder._viewDragger.move({
x: 0,
y: -speed
}, 0);
break;
case "right":
minder._viewDragger.move({
x: speed,
y: 0
}, 0);
break;
case "bottom":
minder._viewDragger.move({
x: 0,
y: speed
}, 0);
break;
default:
return;
}
frame.next();
};
}(direction, speed, minder));
}
}
minder.on("mousedown", function(e) {
flag = MOUSE_HAS_DOWN;
var rect = minder.getPaper().container.getBoundingClientRect();
downX = e.originEvent.clientX;
downY = e.originEvent.clientY;
containerY = rect.top;
maxX = rect.width;
maxY = rect.height;
});
minder.on("mousemove", function(e) {
if (fsm.state() === "drag" && flag == MOUSE_HAS_DOWN && minder.getSelectedNode() && (Math.abs(downX - e.originEvent.clientX) > BOUND_CHECK || Math.abs(downY - e.originEvent.clientY) > BOUND_CHECK)) {
osx = e.originEvent.clientX;
osy = e.originEvent.clientY - containerY;
if (osx < BOUND_CHECK) {
move("right", BOUND_CHECK - osx);
} else if (osx > maxX - BOUND_CHECK) {
move("left", BOUND_CHECK + osx - maxX);
} else {
freeHorizen = true;
}
if (osy < BOUND_CHECK) {
move("bottom", osy);
} else if (osy > maxY - BOUND_CHECK) {
move("top", BOUND_CHECK + osy - maxY);
} else {
freeVirtical = true;
}
if (freeHorizen && freeVirtical) {
move(false);
}
}
if (fsm.state() !== "drag" && flag === MOUSE_HAS_DOWN && minder.getSelectedNode() && (Math.abs(downX - e.originEvent.clientX) > BOUND_CHECK || Math.abs(downY - e.originEvent.clientY) > BOUND_CHECK)) {
if (fsm.state() === "hotbox") {
hotbox.active(Hotbox.STATE_IDLE);
}
return fsm.jump("drag", "user-drag");
}
});
window.addEventListener("mouseup", function() {
flag = MOUSE_HAS_UP;
if (fsm.state() === "drag") {
move(false);
return fsm.jump("normal", "drag-finish");
}
}, false);
}
return module.exports = DragRuntime;
}
};
//src/runtime/fsm.js
/**
* @fileOverview
*
* 编辑器状态机
*
* @author: techird
* @copyright: Baidu FEX, 2014
*/
_p[9] = {
value: function(require, exports, module) {
var Debug = _p.r(19);
var debug = new Debug("fsm");
function handlerConditionMatch(condition, when, exit, enter) {
if (condition.when != when) return false;
if (condition.enter != "*" && condition.enter != enter) return false;
if (condition.exit != "*" && condition.exit != exit) return;
return true;
}
function FSM(defaultState) {
var currentState = defaultState;
var BEFORE_ARROW = " - ";
var AFTER_ARROW = " -> ";
var handlers = [];
/**
* 状态跳转
*
* 会通知所有的状态跳转监视器
*
* @param {string} newState 新状态名称
* @param {any} reason 跳转的原因,可以作为参数传递给跳转监视器
*/
this.jump = function(newState, reason) {
if (!reason) throw new Error("Please tell fsm the reason to jump");
var oldState = currentState;
var notify = [ oldState, newState ].concat([].slice.call(arguments, 1));
var i, handler;
// 跳转前
for (i = 0; i < handlers.length; i++) {
handler = handlers[i];
if (handlerConditionMatch(handler.condition, "before", oldState, newState)) {
if (handler.apply(null, notify)) return;
}
}
currentState = newState;
debug.log("[{0}] {1} -> {2}", reason, oldState, newState);
// 跳转后
for (i = 0; i < handlers.length; i++) {
handler = handlers[i];
if (handlerConditionMatch(handler.condition, "after", oldState, newState)) {
handler.apply(null, notify);
}
}
return currentState;
};
/**
* 返回当前状态
* @return {string}
*/
this.state = function() {
return currentState;
};
/**
* 添加状态跳转监视器
*
* @param {string} condition
* 监视的时机
* "* => *" (默认)
*
* @param {Function} handler
* 监视函数,当状态跳转的时候,会接收三个参数
* * from - 跳转前的状态
* * to - 跳转后的状态
* * reason - 跳转的原因
*/
this.when = function(condition, handler) {
if (arguments.length == 1) {
handler = condition;
condition = "* -> *";
}
var when, resolved, exit, enter;
resolved = condition.split(BEFORE_ARROW);
if (resolved.length == 2) {
when = "before";
} else {
resolved = condition.split(AFTER_ARROW);
if (resolved.length == 2) {
when = "after";
}
}
if (!when) throw new Error("Illegal fsm condition: " + condition);
exit = resolved[0];
enter = resolved[1];
handler.condition = {
when: when,
exit: exit,
enter: enter
};
handlers.push(handler);
};
}
function FSMRumtime() {
this.fsm = new FSM("normal");
}
return module.exports = FSMRumtime;
}
};
//src/runtime/history.js
/**
* @fileOverview
*
* 历史管理
*
* @author: techird
* @copyright: Baidu FEX, 2014
*/
_p[10] = {
value: function(require, exports, module) {
var jsonDiff = _p.r(22);
function HistoryRuntime() {
var minder = this.minder;
var hotbox = this.hotbox;
var MAX_HISTORY = 100;
var lastSnap;
var patchLock;
var undoDiffs;
var redoDiffs;
function reset() {
undoDiffs = [];
redoDiffs = [];
lastSnap = minder.exportJson();
}
function makeUndoDiff() {
var headSnap = minder.exportJson();
var diff = jsonDiff(headSnap, lastSnap);
if (diff.length) {
undoDiffs.push(diff);
while (undoDiffs.length > MAX_HISTORY) {
undoDiffs.shift();
}
lastSnap = headSnap;
return true;
}
}
function makeRedoDiff() {
var revertSnap = minder.exportJson();
redoDiffs.push(jsonDiff(revertSnap, lastSnap));
lastSnap = revertSnap;
}
function undo() {
patchLock = true;
var undoDiff = undoDiffs.pop();
if (undoDiff) {
minder.applyPatches(undoDiff);
makeRedoDiff();
}
patchLock = false;
}
function redo() {
patchLock = true;
var redoDiff = redoDiffs.pop();
if (redoDiff) {
minder.applyPatches(redoDiff);
makeUndoDiff();
}
patchLock = false;
}
function changed() {
if (patchLock) return;
if (makeUndoDiff()) redoDiffs = [];
}
function hasUndo() {
return !!undoDiffs.length;
}
function hasRedo() {
return !!redoDiffs.length;
}
function updateSelection(e) {
if (!patchLock) return;
var patch = e.patch;
switch (patch.express) {
case "node.add":
minder.select(patch.node.getChild(patch.index), true);
break;
case "node.remove":
case "data.replace":
case "data.remove":
case "data.add":
minder.select(patch.node, true);
break;
}
}
this.history = {
reset: reset,
undo: undo,
redo: redo,
hasUndo: hasUndo,
hasRedo: hasRedo
};
reset();
minder.on("contentchange", changed);
minder.on("import", reset);
minder.on("patch", updateSelection);
var main = hotbox.state("main");
main.button({
position: "top",
label: "撤销",
key: "Ctrl + Z",
enable: hasUndo,
action: undo,
next: "idle"
});
main.button({
position: "top",
label: "重做",
key: "Ctrl + Y",
enable: hasRedo,
action: redo,
next: "idle"
});
}
window.diff = jsonDiff;
return module.exports = HistoryRuntime;
}
};
//src/runtime/hotbox.js
/**
* @fileOverview
*
* 热盒 Runtime
*
* @author: techird
* @copyright: Baidu FEX, 2014
*/
_p[11] = {
value: function(require, exports, module) {
var Hotbox = _p.r(2);
function HotboxRuntime() {
var fsm = this.fsm;
var minder = this.minder;
var receiver = this.receiver;
var container = this.container;
var hotbox = new Hotbox(container);
hotbox.setParentFSM(fsm);
fsm.when("normal -> hotbox", function(exit, enter, reason) {
var node = minder.getSelectedNode();
var position;
if (node) {
var box = node.getRenderBox();
position = {
x: box.cx,
y: box.cy
};
}
hotbox.active("main", position);
});
fsm.when("normal -> normal", function(exit, enter, reason, e) {
if (reason == "shortcut-handle") {
var handleResult = hotbox.dispatch(e);
if (handleResult) {
e.preventDefault();
} else {
minder.dispatchKeyEvent(e);
}
}
});
fsm.when("modal -> normal", function(exit, enter, reason, e) {
if (reason == "import-text-finish") {
receiver.element.focus();
}
});
this.hotbox = hotbox;
}
return module.exports = HotboxRuntime;
}
};
//src/runtime/input.js
/**
* @fileOverview
*
* 文本输入支持
*
* @author: techird
* @copyright: Baidu FEX, 2014
*/
_p[12] = {
value: function(require, exports, module) {
_p.r(21);
var Debug = _p.r(19);
var debug = new Debug("input");
function InputRuntime() {
var fsm = this.fsm;
var minder = this.minder;
var hotbox = this.hotbox;
var receiver = this.receiver;
var receiverElement = receiver.element;
var isGecko = window.kity.Browser.gecko;
// setup everything to go
setupReciverElement();
setupFsm();
setupHotbox();
// expose editText()
this.editText = editText;
// listen the fsm changes, make action.
function setupFsm() {
// when jumped to input mode, enter
fsm.when("* -> input", enterInputMode);
// when exited, commit or exit depends on the exit reason
fsm.when("input -> *", function(exit, enter, reason) {
switch (reason) {
case "input-cancel":
return exitInputMode();
case "input-commit":
default:
return commitInputResult();
}
});
// lost focus to commit
receiver.onblur(function(e) {
if (fsm.state() == "input") {
fsm.jump("normal", "input-commit");
}
});
minder.on("beforemousedown", function() {
if (fsm.state() == "input") {
fsm.jump("normal", "input-commit");
}
});
minder.on("dblclick", function() {
if (minder.getSelectedNode()) {
editText();
}
});
}
// let the receiver follow the current selected node position
function setupReciverElement() {
if (debug.flaged) {
receiverElement.classList.add("debug");
}
receiverElement.onmousedown = function(e) {
e.stopPropagation();
};
minder.on("layoutallfinish viewchange viewchanged selectionchange", function(e) {
// viewchange event is too frequenced, lazy it
if (e.type == "viewchange" && fsm.state() != "input") return;
updatePosition();
});
updatePosition();
}
// edit entrance in hotbox
function setupHotbox() {
hotbox.state("main").button({
position: "center",
label: "编辑",
key: "F2",
enable: function() {
return minder.queryCommandState("text") != -1;
},
action: editText
});
}
/**
* 增加对字体的鉴别,以保证用户在编辑状态ctrl/cmd + b/i所触发的加粗斜体与显示一致
* @editor Naixor
* @Date 2015-12-2
*/
// edit for the selected node
function editText() {
var node = minder.getSelectedNode();
if (!node) {
return;
}
var textContainer = receiverElement;
receiverElement.innerText = "";
if (node.getData("font-weight") === "bold") {
var b = document.createElement("b");
textContainer.appendChild(b);
textContainer = b;
}
if (node.getData("font-style") === "italic") {
var i = document.createElement("i");
textContainer.appendChild(i);
textContainer = i;
}
textContainer.innerText = minder.queryCommandValue("text");
if (isGecko) {
receiver.fixFFCaretDisappeared();
}
fsm.jump("input", "input-request");
receiver.selectAll();
}
/**
* 增加对字体的鉴别,以保证用户在编辑状态ctrl/cmd + b/i所触发的加粗斜体与显示一致
* @editor Naixor
* @Date 2015-12-2
*/
function enterInputMode() {
var node = minder.getSelectedNode();
if (node) {
var fontSize = node.getData("font-size") || node.getStyle("font-size");
receiverElement.style.fontSize = fontSize + "px";
receiverElement.style.minWidth = 0;
receiverElement.style.minWidth = receiverElement.clientWidth + "px";
receiverElement.style.fontWeight = node.getData("font-weight") || "";
receiverElement.style.fontStyle = node.getData("font-style") || "";
receiverElement.classList.add("input");
receiverElement.focus();
}
}
/**
* 按照文本提交操作处理
* @Desc: 从其他节点复制文字到另一个节点时部分浏览器(chrome)会自动包裹一个span标签,这样试用一下逻辑出来的就不是text节点二是span节点因此导致undefined的情况发生
* @Warning: 下方代码使用[].slice.call来将HTMLDomCollection处理成为Array,ie8及以下会有问题
* @Editor: Naixor
* @Date: 2015.9.16
*/
function commitInputText(textNodes) {
var text = "";
var TAB_CHAR = " ", ENTER_CHAR = "\n", STR_CHECK = /\S/, SPACE_CHAR = " ", // 针对FF,SG,BD,LB,IE等浏览器下SPACE的charCode存在为32和160的情况做处理
SPACE_CHAR_REGEXP = new RegExp("( |" + String.fromCharCode(160) + ")"), BR = document.createElement("br");
var isBold = false, isItalic = false;
for (var str, _divChildNodes, space_l, space_num, tab_num, i = 0, l = textNodes.length; i < l; i++) {
str = textNodes[i];
switch (Object.prototype.toString.call(str)) {
// 正常情况处理
case "[object HTMLBRElement]":
{
text += ENTER_CHAR;
break;
}
case "[object Text]":
{
// SG下会莫名其妙的加上 影响后续判断,干掉!
/**