-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
editorCommonEvent.ts
984 lines (970 loc) · 45.5 KB
/
editorCommonEvent.ts
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
import {focusBlock, focusByRange} from "./selection";
import {hasClosestBlock, hasClosestByAttribute, hasClosestByClassName} from "./hasClosest";
import {Constants} from "../../constants";
import {paste} from "./paste";
import {cancelSB, genEmptyElement, genSBElement} from "../../block/util";
import {transaction} from "../wysiwyg/transaction";
import {getTopAloneElement} from "../wysiwyg/getBlock";
import {updateListOrder} from "../wysiwyg/list";
import {fetchPost, fetchSyncPost} from "../../util/fetch";
import {onGet} from "./onGet";
/// #if !MOBILE
import {getInstanceById} from "../../layout/util";
import {Tab} from "../../layout/Tab";
import {updatePanelByEditor} from "../../editor/util";
/// #endif
import {Editor} from "../../editor";
import {blockRender} from "../markdown/blockRender";
import {uploadLocalFiles} from "../upload";
import {insertHTML} from "./insertHTML";
import {isBrowser} from "../../util/functions";
import {hideElements} from "../ui/hideElements";
const moveToNew = (protyle: IProtyle, sourceElements: Element[], targetElement: Element, newSourceElement: Element,
isSameDoc: boolean, isBottom: boolean, isCopy: boolean) => {
let topSourceElement;
const targetId = targetElement.getAttribute("data-node-id");
const newSourceId = newSourceElement.getAttribute("data-node-id");
const doOperations: IOperation[] = [];
const undoOperations: IOperation[] = [];
targetElement.insertAdjacentElement(isBottom ? "afterend" : "beforebegin", newSourceElement);
if (isBottom) {
doOperations.push({
action: "insert",
data: newSourceElement.outerHTML,
id: newSourceId,
previousID: targetId,
});
} else {
doOperations.push({
action: "insert",
data: newSourceElement.outerHTML,
id: newSourceId,
nextID: targetId,
});
}
sourceElements.reverse().forEach((item, index) => {
const itemId = item.getAttribute("data-node-id");
if (index === sourceElements.length - 1) {
topSourceElement = getTopAloneElement(item);
if (topSourceElement.isSameNode(item)) {
topSourceElement = undefined;
}
}
const copyId = Lute.NewNodeID();
if (isCopy) {
undoOperations.push({
action: "delete",
id: copyId,
});
} else {
undoOperations.push({
action: "move",
id: itemId,
previousID: item.previousElementSibling?.getAttribute("data-node-id"),
parentID: item.parentElement.getAttribute("data-node-id") || protyle.block.rootID,
});
}
if (!isSameDoc && !isCopy) {
// 打开两个相同的文档
const sameElement = protyle.wysiwyg.element.querySelector(`[data-node-id="${itemId}"]`);
if (sameElement) {
sameElement.remove();
}
}
if (isCopy) {
const copyElement = item.cloneNode(true) as HTMLElement;
copyElement.setAttribute("data-node-id", copyId);
copyElement.querySelectorAll("[data-node-id]").forEach((e) => {
const newId = Lute.NewNodeID();
e.setAttribute("data-node-id", newId);
e.setAttribute("updated", newId.split("-")[0]);
});
newSourceElement.insertAdjacentElement("afterbegin", copyElement);
doOperations.push({
action: "insert",
id: copyId,
data: copyElement.outerHTML,
parentID: newSourceId,
});
} else {
newSourceElement.insertAdjacentElement("afterbegin", item);
doOperations.push({
action: "move",
id: itemId,
parentID: newSourceId,
});
}
});
undoOperations.reverse();
if (newSourceElement.getAttribute("data-subtype") === "o") {
undoOperations.splice(0, 0, {
action: "update",
id: newSourceId,
data: newSourceElement.outerHTML
});
updateListOrder(newSourceElement, 1);
doOperations.push({
action: "update",
id: newSourceId,
data: newSourceElement.outerHTML
});
}
undoOperations.push({
action: "delete",
id: newSourceId,
});
return {
doOperations,
undoOperations,
topSourceElement,
};
};
const moveTo = async (protyle: IProtyle, sourceElements: Element[], targetElement: Element,
isSameDoc: boolean, position: InsertPosition, isCopy: boolean) => {
let topSourceElement;
const doOperations: IOperation[] = [];
const undoOperations: IOperation[] = [];
const foldHeadingIds: { id: string, parentID: string }[] = [];
const targetId = targetElement.getAttribute("data-node-id");
let tempTargetElement = targetElement;
sourceElements.reverse().forEach((item, index) => {
const id = item.getAttribute("data-node-id");
const parentID = item.parentElement.getAttribute("data-node-id") || protyle.block.rootID;
if (index === sourceElements.length - 1) {
topSourceElement = getTopAloneElement(item);
if (topSourceElement.isSameNode(item)) {
topSourceElement = undefined;
} else if (topSourceElement.contains(item) && topSourceElement.contains(targetElement)) {
// * * 1 列表项拖拽到父级列表项下 https://ld246.com/article/1665448570858
topSourceElement = targetElement;
}
}
if (isCopy && item.getAttribute("data-type") === "NodeHeading" && item.getAttribute("fold") === "1") {
item.removeAttribute("fold");
foldHeadingIds.push({id, parentID});
}
let copyId;
let copyElement;
if (isCopy) {
copyId = Lute.NewNodeID();
undoOperations.push({
action: "delete",
id: copyId,
});
} else {
undoOperations.push({
action: "move",
id,
previousID: item.previousElementSibling?.getAttribute("data-node-id"),
parentID,
});
}
if (!isSameDoc && !isCopy) {
// 打开两个相同的文档
const sameElement = protyle.wysiwyg.element.querySelector(`[data-node-id="${id}"]`);
if (sameElement) {
sameElement.remove();
}
}
if (isCopy) {
copyElement = item.cloneNode(true) as HTMLElement;
copyElement.setAttribute("data-node-id", copyId);
copyElement.querySelectorAll("[data-node-id]").forEach((e) => {
const newId = Lute.NewNodeID();
e.setAttribute("data-node-id", newId);
e.setAttribute("updated", newId.split("-")[0]);
});
tempTargetElement.insertAdjacentElement(position, copyElement);
doOperations.push({
action: "insert",
id: copyId,
data: copyElement.outerHTML,
previousID: position === "afterend" ? targetId : copyElement.previousElementSibling?.getAttribute("data-node-id"), // 不能使用常量,移动后会被修改
parentID: copyElement.parentElement?.getAttribute("data-node-id") || protyle.block.parentID || protyle.block.rootID,
});
} else {
tempTargetElement.insertAdjacentElement(position, item);
doOperations.push({
action: "move",
id,
previousID: position === "afterend" ? targetId : item.previousElementSibling?.getAttribute("data-node-id"), // 不能使用常量,移动后会被修改
parentID: item.parentElement?.getAttribute("data-node-id") || protyle.block.parentID || protyle.block.rootID,
});
}
if (position !== "afterend") {
tempTargetElement = isCopy ? copyElement : item;
}
});
undoOperations.reverse();
for (let j = 0; j < foldHeadingIds.length; j++) {
const childrenItem = foldHeadingIds[j];
const headingIds = await fetchSyncPost("/api/block/getHeadingChildrenIDs", {id: childrenItem.id});
headingIds.data.reverse().forEach((headingId: string) => {
undoOperations.push({
action: "move",
id: headingId,
previousID: childrenItem.id,
parentID: childrenItem.parentID,
});
});
undoOperations.push({
action: "foldHeading",
id: childrenItem.id,
data: "remove"
});
doOperations.push({
action: "unfoldHeading",
id: childrenItem.id,
});
}
return {
doOperations,
undoOperations,
topSourceElement,
};
};
const dragSb = async (protyle: IProtyle, sourceElements: Element[], targetElement: Element, isBottom: boolean,
direct: "col" | "row", isCopy: boolean) => {
const isSameDoc = protyle.element.contains(sourceElements[0]);
let newSourceElement: HTMLElement;
if (sourceElements[0].getAttribute("data-type") === "NodeListItem" && targetElement.getAttribute("data-type") !== "NodeListItem") {
newSourceElement = document.createElement("div");
newSourceElement.setAttribute("data-node-id", Lute.NewNodeID());
newSourceElement.setAttribute("data-type", "NodeList");
newSourceElement.setAttribute("data-subtype", sourceElements[0].getAttribute("data-subtype"));
newSourceElement.className = "list";
newSourceElement.insertAdjacentHTML("beforeend", `<div class="protyle-attr" contenteditable="false">${Constants.ZWSP}</div>`);
}
const undoOperations: IOperation[] = [{
action: "move",
id: targetElement.getAttribute("data-node-id"),
previousID: targetElement.previousElementSibling?.getAttribute("data-node-id"),
parentID: targetElement.parentElement?.getAttribute("data-node-id") || protyle.block.parentID || protyle.block.rootID
}];
let topSourceElement: Element;
let oldSourceParentElement = sourceElements[0].parentElement;
const sbElement = genSBElement(direct);
targetElement.parentElement.replaceChild(sbElement, targetElement);
const doOperations: IOperation[] = [{
action: "insert",
data: sbElement.outerHTML,
id: sbElement.getAttribute("data-node-id"),
nextID: sbElement.nextElementSibling?.getAttribute("data-node-id"),
previousID: sbElement.previousElementSibling?.getAttribute("data-node-id"),
parentID: sbElement.parentElement.getAttribute("data-node-id") || protyle.block.parentID || protyle.block.rootID
}];
if (newSourceElement) {
const newSourceId = newSourceElement.getAttribute("data-node-id");
sbElement.insertAdjacentElement("afterbegin", targetElement);
doOperations.push({
action: "move",
id: targetElement.getAttribute("data-node-id"),
parentID: sbElement.getAttribute("data-node-id")
});
if (isBottom) {
targetElement.insertAdjacentElement("afterend", newSourceElement);
doOperations.push({
action: "insert",
data: newSourceElement.outerHTML,
id: newSourceId,
previousID: targetElement.getAttribute("data-node-id"),
});
} else {
targetElement.insertAdjacentElement("beforebegin", newSourceElement);
doOperations.push({
action: "insert",
data: newSourceElement.outerHTML,
id: newSourceId,
nextID: targetElement.getAttribute("data-node-id"),
});
}
sourceElements.reverse().forEach((item, index) => {
if (index === sourceElements.length - 1) {
topSourceElement = getTopAloneElement(item);
if (topSourceElement.isSameNode(item)) {
topSourceElement = undefined;
}
}
const copyId = Lute.NewNodeID();
if (isCopy) {
undoOperations.push({
action: "delete",
id: copyId
});
} else {
undoOperations.push({
action: "move",
id: item.getAttribute("data-node-id"),
previousID: item.previousElementSibling?.getAttribute("data-node-id"),
parentID: item.parentElement.getAttribute("data-node-id") || protyle.block.rootID,
});
}
if (!isSameDoc && !isCopy) {
// 打开两个相同的文档
const sameElement = protyle.wysiwyg.element.querySelector(`[data-node-id="${item.getAttribute("data-node-id")}"]`);
if (sameElement) {
sameElement.remove();
}
}
if (isCopy) {
const copyElement = item.cloneNode(true) as HTMLElement;
copyElement.setAttribute("data-node-id", copyId);
copyElement.querySelectorAll("[data-node-id]").forEach((e) => {
const newId = Lute.NewNodeID();
e.setAttribute("data-node-id", newId);
e.setAttribute("updated", newId.split("-")[0]);
});
newSourceElement.insertAdjacentElement("afterbegin", copyElement);
doOperations.push({
action: "insert",
id: copyId,
data: copyElement.outerHTML,
parentID: newSourceId,
});
} else {
newSourceElement.insertAdjacentElement("afterbegin", item);
doOperations.push({
action: "move",
id: item.getAttribute("data-node-id"),
parentID: newSourceId,
});
}
});
undoOperations.reverse();
undoOperations.push({
action: "delete",
id: newSourceId,
});
} else {
const foldHeadingIds: { id: string, parentID: string }[] = [];
let afterPreviousID;
sourceElements.reverse().forEach((item, index) => {
const id = item.getAttribute("data-node-id");
const parentID = item.parentElement.getAttribute("data-node-id") || protyle.block.rootID;
if (index === sourceElements.length - 1) {
topSourceElement = getTopAloneElement(item);
if (topSourceElement.isSameNode(item)) {
topSourceElement = undefined;
}
}
const copyId = Lute.NewNodeID();
if (index === 0) {
afterPreviousID = isCopy ? copyId : id;
}
if (isCopy && item.getAttribute("data-type") === "NodeHeading" && item.getAttribute("fold") === "1") {
item.removeAttribute("fold");
foldHeadingIds.push({id, parentID});
}
if (isCopy) {
undoOperations.push({
action: "delete",
id: copyId,
});
} else {
undoOperations.push({
action: "move",
id,
previousID: item.previousElementSibling?.getAttribute("data-node-id"),
parentID
});
}
if (!isSameDoc && !isCopy) {
// 打开两个相同的文档
const sameElement = protyle.wysiwyg.element.querySelector(`[data-node-id="${id}"]`);
if (sameElement) {
sameElement.remove();
}
}
if (isCopy) {
const copyElement = item.cloneNode(true) as HTMLElement;
copyElement.setAttribute("data-node-id", copyId);
copyElement.querySelectorAll("[data-node-id]").forEach((e) => {
const newId = Lute.NewNodeID();
e.setAttribute("data-node-id", newId);
e.setAttribute("updated", newId.split("-")[0]);
});
sbElement.insertAdjacentElement("afterbegin", copyElement);
doOperations.push({
action: "insert",
id: copyId,
data: copyElement.outerHTML,
parentID: sbElement.getAttribute("data-node-id"),
});
} else {
sbElement.insertAdjacentElement("afterbegin", item);
doOperations.push({
action: "move",
id,
parentID: sbElement.getAttribute("data-node-id"),
});
}
});
undoOperations.reverse();
for (let j = 0; j < foldHeadingIds.length; j++) {
const childrenItem = foldHeadingIds[j];
const headingIds = await fetchSyncPost("/api/block/getHeadingChildrenIDs", {id: childrenItem.id});
headingIds.data.reverse().forEach((headingId: string) => {
undoOperations.push({
action: "move",
id: headingId,
previousID: childrenItem.id,
parentID: childrenItem.parentID,
});
});
if (j === 0) {
afterPreviousID = headingIds.data[0];
}
undoOperations.push({
action: "foldHeading",
id: childrenItem.id,
data: "remove"
});
doOperations.push({
action: "unfoldHeading",
id: childrenItem.id,
});
}
if (isBottom) {
sbElement.insertAdjacentElement("afterbegin", targetElement);
doOperations.push({
action: "move",
id: targetElement.getAttribute("data-node-id"),
parentID: sbElement.getAttribute("data-node-id")
});
} else {
sbElement.lastElementChild.insertAdjacentElement("beforebegin", targetElement);
doOperations.push({
action: "move",
id: targetElement.getAttribute("data-node-id"),
previousID: afterPreviousID
});
}
}
undoOperations.push({
action: "delete",
id: sbElement.getAttribute("data-node-id"),
});
// https://github.com/siyuan-note/insider/issues/536
if (!isCopy && oldSourceParentElement && oldSourceParentElement.classList.contains("list") &&
oldSourceParentElement.getAttribute("data-subtype") === "o" &&
!oldSourceParentElement.isSameNode(sourceElements[0].parentElement) && oldSourceParentElement.childElementCount > 1) {
Array.from(oldSourceParentElement.children).forEach((item) => {
if (item.classList.contains("protyle-attr")) {
return;
}
// 撤销更新不能位于最后,否则又更新为最新结果 https://github.com/siyuan-note/siyuan/issues/5725
undoOperations.splice(0, 0, {
action: "update",
id: item.getAttribute("data-node-id"),
data: item.outerHTML
});
});
updateListOrder(oldSourceParentElement, 1);
Array.from(oldSourceParentElement.children).forEach((item) => {
if (item.classList.contains("protyle-attr")) {
return;
}
doOperations.push({
action: "update",
id: item.getAttribute("data-node-id"),
data: item.outerHTML
});
});
}
// 删除空元素
if (!isCopy && topSourceElement) {
doOperations.push({
action: "delete",
id: topSourceElement.getAttribute("data-node-id"),
});
undoOperations.splice(0, 0, {
action: "insert",
data: topSourceElement.outerHTML,
id: topSourceElement.getAttribute("data-node-id"),
previousID: topSourceElement.previousElementSibling?.getAttribute("data-node-id"),
parentID: topSourceElement.parentElement?.getAttribute("data-node-id") || protyle.block.parentID || protyle.block.rootID
});
if (!isSameDoc) {
// 打开两个相同的文档
const sameElement = protyle.wysiwyg.element.querySelector(`[data-node-id="${topSourceElement.getAttribute("data-node-id")}"]`);
if (sameElement) {
sameElement.remove();
}
}
oldSourceParentElement = topSourceElement.parentElement;
topSourceElement.remove();
}
if (!isCopy && oldSourceParentElement && oldSourceParentElement.classList.contains("sb") && oldSourceParentElement.childElementCount === 2) {
// 拖拽后,sb 只剩下一个元素
const sbData = cancelSB(protyle, oldSourceParentElement);
doOperations.push(sbData.doOperations[0], sbData.doOperations[1]);
undoOperations.splice(0, 0, sbData.undoOperations[0], sbData.undoOperations[1]);
} else if (!isCopy && oldSourceParentElement && oldSourceParentElement.classList.contains("protyle-wysiwyg") && oldSourceParentElement.innerHTML === "") {
/// #if !MOBILE
// 拖拽后,根文档原内容为空,且不为悬浮窗
const protyleElement = hasClosestByClassName(oldSourceParentElement, "protyle", true);
if (protyleElement && !protyleElement.classList.contains("block__edit")) {
const editor = getInstanceById(protyleElement.getAttribute("data-id")) as Tab;
if (editor && editor.model instanceof Editor && editor.model.editor.protyle.block.id === editor.model.editor.protyle.block.rootID) {
const newId = Lute.NewNodeID();
doOperations.splice(0, 0, {
action: "insert",
id: newId,
data: genEmptyElement(false, false, newId).outerHTML,
parentID: editor.model.editor.protyle.block.parentID
});
undoOperations.splice(0, 0, {
action: "delete",
id: newId,
});
}
}
/// #endif
}
if (isSameDoc || isCopy) {
transaction(protyle, doOperations, undoOperations);
} else {
// 跨文档不支持撤销
transaction(protyle, doOperations);
}
focusBlock(sourceElements[0]);
};
const dragSame = async (protyle: IProtyle, sourceElements: Element[], targetElement: Element, isBottom: boolean, isCopy: boolean) => {
const isSameDoc = protyle.element.contains(sourceElements[0]);
const doOperations: IOperation[] = [];
const undoOperations: IOperation[] = [];
let newSourceElement: HTMLElement;
if (sourceElements[0].getAttribute("data-type") === "NodeListItem" && targetElement.getAttribute("data-type") !== "NodeListItem") {
newSourceElement = document.createElement("div");
newSourceElement.setAttribute("data-node-id", Lute.NewNodeID());
newSourceElement.setAttribute("data-type", "NodeList");
newSourceElement.setAttribute("data-subtype", sourceElements[0].getAttribute("data-subtype"));
newSourceElement.className = "list";
newSourceElement.insertAdjacentHTML("beforeend", `<div class="protyle-attr" contenteditable="false">${Constants.ZWSP}</div>`);
}
let topSourceElement: Element;
let oldSourceParentElement = sourceElements[0].parentElement;
if (isBottom) {
if (newSourceElement) {
const moveToResult = moveToNew(protyle, sourceElements, targetElement, newSourceElement, isSameDoc, isBottom, isCopy);
doOperations.push(...moveToResult.doOperations);
undoOperations.push(...moveToResult.undoOperations);
topSourceElement = moveToResult.topSourceElement;
} else {
const moveToResult = await moveTo(protyle, sourceElements, targetElement, isSameDoc, "afterend", isCopy);
doOperations.push(...moveToResult.doOperations);
undoOperations.push(...moveToResult.undoOperations);
topSourceElement = moveToResult.topSourceElement;
}
} else {
if (newSourceElement) {
const moveToResult = moveToNew(protyle, sourceElements, targetElement, newSourceElement, isSameDoc, isBottom, isCopy);
doOperations.push(...moveToResult.doOperations);
undoOperations.push(...moveToResult.undoOperations);
topSourceElement = moveToResult.topSourceElement;
} else {
const moveToResult = await moveTo(protyle, sourceElements, targetElement, isSameDoc, "beforebegin", isCopy);
doOperations.push(...moveToResult.doOperations);
undoOperations.push(...moveToResult.undoOperations);
topSourceElement = moveToResult.topSourceElement;
}
}
if (targetElement.getAttribute("data-type") === "NodeListItem" && targetElement.getAttribute("data-subtype") === "o") {
// https://github.com/siyuan-note/insider/issues/536
Array.from(targetElement.parentElement.children).forEach((item) => {
if (item.classList.contains("protyle-attr")) {
return;
}
undoOperations.splice(0, 0, {
action: "update",
id: item.getAttribute("data-node-id"),
data: item.outerHTML
});
});
updateListOrder(targetElement.parentElement, 1);
Array.from(targetElement.parentElement.children).forEach((item) => {
if (item.classList.contains("protyle-attr")) {
return;
}
doOperations.push({
action: "update",
id: item.getAttribute("data-node-id"),
data: item.outerHTML
});
});
}
if (!isCopy &&
oldSourceParentElement && oldSourceParentElement.classList.contains("list") &&
oldSourceParentElement.getAttribute("data-subtype") === "o" &&
!oldSourceParentElement.isSameNode(sourceElements[0].parentElement) && oldSourceParentElement.childElementCount > 1) {
Array.from(oldSourceParentElement.children).forEach((item) => {
if (item.classList.contains("protyle-attr")) {
return;
}
if (oldSourceParentElement.contains(targetElement)) {
undoOperations.splice(0, 0, {
action: "update",
id: item.getAttribute("data-node-id"),
data: item.outerHTML
});
} else {
undoOperations.splice(targetElement.parentElement.childElementCount - 1, 0, {
action: "update",
id: item.getAttribute("data-node-id"),
data: item.outerHTML
});
}
});
updateListOrder(oldSourceParentElement, 1);
Array.from(oldSourceParentElement.children).forEach((item) => {
if (item.classList.contains("protyle-attr")) {
return;
}
doOperations.push({
action: "update",
id: item.getAttribute("data-node-id"),
data: item.outerHTML
});
});
}
// 删除空元素
if (!isCopy && topSourceElement) {
doOperations.push({
action: "delete",
id: topSourceElement.getAttribute("data-node-id"),
});
undoOperations.splice(0, 0, {
action: "insert",
data: topSourceElement.outerHTML,
id: topSourceElement.getAttribute("data-node-id"),
previousID: topSourceElement.previousElementSibling?.getAttribute("data-node-id"),
parentID: topSourceElement.parentElement?.getAttribute("data-node-id") || protyle.block.parentID || protyle.block.rootID
});
oldSourceParentElement = topSourceElement.parentElement;
topSourceElement.remove();
if (!isSameDoc) {
// 打开两个相同的文档
const sameElement = protyle.wysiwyg.element.querySelector(`[data-node-id="${topSourceElement.getAttribute("data-node-id")}"]`);
if (sameElement) {
sameElement.remove();
}
}
}
if (!isCopy && oldSourceParentElement && oldSourceParentElement.classList.contains("sb") && oldSourceParentElement.childElementCount === 2) {
// 拖拽后,sb 只剩下一个元素
const sbData = cancelSB(protyle, oldSourceParentElement);
doOperations.push(sbData.doOperations[0], sbData.doOperations[1]);
undoOperations.splice(0, 0, sbData.undoOperations[0], sbData.undoOperations[1]);
} else if (!isCopy && oldSourceParentElement && oldSourceParentElement.classList.contains("protyle-wysiwyg") && oldSourceParentElement.childElementCount === 0) {
/// #if !MOBILE
// 拖拽后,根文档原内容为空,且不为悬浮窗
const protyleElement = hasClosestByClassName(oldSourceParentElement, "protyle", true);
if (protyleElement && !protyleElement.classList.contains("block__edit")) {
const editor = getInstanceById(protyleElement.getAttribute("data-id")) as Tab;
if (editor && editor.model instanceof Editor && editor.model.editor.protyle.block.id === editor.model.editor.protyle.block.rootID) {
const newId = Lute.NewNodeID();
doOperations.splice(0, 0, {
action: "insert",
id: newId,
data: genEmptyElement(false, false, newId).outerHTML,
parentID: editor.model.editor.protyle.block.parentID
});
undoOperations.splice(0, 0, {
action: "delete",
id: newId,
});
}
}
/// #endif
}
if (isSameDoc || isCopy) {
transaction(protyle, doOperations, undoOperations);
} else {
// 跨文档不支持撤销
transaction(protyle, doOperations);
}
focusBlock(sourceElements[0]);
};
export const dropEvent = (protyle: IProtyle, editorElement: HTMLElement) => {
editorElement.addEventListener("dragstart", (event) => {
const target = event.target as HTMLElement;
if (target.tagName === "IMG") {
window.siyuan.dragElement = undefined;
event.preventDefault();
return;
}
if (target.classList && target.classList.contains("protyle-action")) {
if (hasClosestByClassName(target, "protyle-wysiwyg__embed")) {
window.siyuan.dragElement = undefined;
event.preventDefault();
} else {
window.siyuan.dragElement = protyle.wysiwyg.element;
event.dataTransfer.setData(`${Constants.SIYUAN_DROP_GUTTER}NodeListItem${Constants.ZWSP}${target.parentElement.getAttribute("data-subtype")}${Constants.ZWSP}${[target.parentElement.getAttribute("data-node-id")]}`,
protyle.wysiwyg.element.innerHTML);
}
return;
}
// 选中编辑器中的文字进行拖拽
event.dataTransfer.setData(Constants.SIYUAN_DROP_EDITOR, Constants.SIYUAN_DROP_EDITOR);
protyle.element.style.userSelect = "auto";
document.onmousemove = null;
document.onmouseup = null;
});
editorElement.addEventListener("drop", async (event: DragEvent & { target: HTMLElement }) => {
if (protyle.disabled || event.dataTransfer.getData(Constants.SIYUAN_DROP_EDITOR)) {
// 只读模式/编辑器内选中文字拖拽
event.preventDefault();
event.stopPropagation();
return;
}
const targetElement = editorElement.querySelector(".dragover__bottom") || editorElement.querySelector(".dragover__top") || editorElement.querySelector(".dragover__left") || editorElement.querySelector(".dragover__right");
let gutterType = "";
for (const item of event.dataTransfer.items) {
if (item.type.startsWith(Constants.SIYUAN_DROP_GUTTER)) {
gutterType = item.type;
}
}
if (gutterType) {
// gutter 或反链面板拖拽
const sourceElements: Element[] = [];
const gutterTypes = gutterType.replace(Constants.SIYUAN_DROP_GUTTER, "").split(Constants.ZWSP);
const selectedIds = gutterTypes[2].split(",");
if (event.altKey) {
focusByRange(document.caretRangeFromPoint(event.clientX, event.clientY));
let html = "";
for (let i = 0; i < selectedIds.length; i++) {
const response = await fetchSyncPost("/api/block/getRefText", {id: selectedIds[i]});
html += `((${selectedIds[i]} '${response.data}')) `;
}
insertHTML(html, protyle);
} else if (event.shiftKey) {
focusByRange(document.caretRangeFromPoint(event.clientX, event.clientY));
let html = "";
selectedIds.forEach(item => {
html += `{{select * from blocks where id='${item}'}}\n`;
});
insertHTML(protyle.lute.SpinBlockDOM(html), protyle, true);
blockRender(protyle, protyle.wysiwyg.element);
} else if (targetElement) {
let queryClass = "";
selectedIds.forEach(item => {
queryClass += `[data-node-id="${item}"],`;
});
if (window.siyuan.dragElement) {
window.siyuan.dragElement.querySelectorAll(queryClass.substring(0, queryClass.length - 1)).forEach(elementItem => {
if (elementItem.getAttribute("data-type") === "NodeBlockQueryEmbed" ||
!hasClosestByAttribute(elementItem, "data-type", "NodeBlockQueryEmbed")) {
sourceElements.push(elementItem);
}
});
} else { // 跨窗口拖拽
const targetProtyleElement = document.createElement("template");
targetProtyleElement.innerHTML = event.dataTransfer.getData(gutterType);
targetProtyleElement.content.querySelectorAll(queryClass.substring(0, queryClass.length - 1)).forEach(elementItem => {
if (elementItem.getAttribute("data-type") === "NodeBlockQueryEmbed" ||
!hasClosestByAttribute(elementItem, "data-type", "NodeBlockQueryEmbed")) {
sourceElements.push(elementItem);
}
});
}
sourceElements.forEach(item => {
item.classList.remove("protyle-wysiwyg--select", "protyle-wysiwyg--hl");
item.removeAttribute("select-start");
item.removeAttribute("select-end");
// 反链提及有高亮,如果拖拽到正文的话,应移除
item.querySelectorAll('[data-type="search-mark"]').forEach(markItem => {
markItem.outerHTML = markItem.innerHTML;
});
});
hideElements(["gutter"], protyle);
const targetClass = targetElement.className.split(" ");
targetElement.classList.remove("dragover__bottom", "dragover__top", "dragover__left", "dragover__right", "protyle-wysiwyg--select");
if (targetElement.parentElement.getAttribute("data-type") === "NodeSuperBlock" &&
targetElement.parentElement.getAttribute("data-sb-layout") === "col") {
if (targetClass.includes("dragover__left") || targetClass.includes("dragover__right")) {
dragSame(protyle, sourceElements, targetElement, targetClass.includes("dragover__right"), event.ctrlKey);
} else {
dragSb(protyle, sourceElements, targetElement, targetClass.includes("dragover__bottom"), "row", event.ctrlKey);
}
} else {
if (targetClass.includes("dragover__left") || targetClass.includes("dragover__right")) {
dragSb(protyle, sourceElements, targetElement, targetClass.includes("dragover__right"), "col", event.ctrlKey);
} else {
dragSame(protyle, sourceElements, targetElement, targetClass.includes("dragover__bottom"), event.ctrlKey);
}
}
}
} else if (event.dataTransfer.getData(Constants.SIYUAN_DROP_FILE)?.split("-").length > 1
&& targetElement && !protyle.options.backlinkData) {
// 文件树拖拽
const scrollTop = protyle.contentElement.scrollTop;
const ids = event.dataTransfer.getData(Constants.SIYUAN_DROP_FILE).split(",");
for (let i = 0; i < ids.length; i++) {
if (ids[i]) {
const response = await fetchSyncPost("/api/filetree/doc2Heading", {
srcID: ids[i],
after: targetElement.classList.contains("dragover__bottom"),
targetID: targetElement.getAttribute("data-node-id"),
});
}
}
fetchPost("/api/filetree/getDoc", {
id: protyle.block.id,
size: window.siyuan.config.editor.dynamicLoadBlocks,
}, getResponse => {
onGet(getResponse, protyle);
/// #if !MOBILE
// 文档标题互转后,需更新大纲
updatePanelByEditor(protyle, false, false, true);
/// #endif
// 文档标题互转后,编辑区会跳转到开头 https://github.com/siyuan-note/siyuan/issues/2939
setTimeout(() => {
protyle.contentElement.scrollTop = scrollTop;
protyle.scroll.lastScrollTop = scrollTop - 1;
}, Constants.TIMEOUT_BLOCKLOAD);
});
targetElement.classList.remove("dragover__bottom", "dragover__top");
} else if (!window.siyuan.dragElement && (event.dataTransfer.types[0] === "Files" || event.dataTransfer.types.includes("text/html"))) {
// 外部文件拖入编辑器中或者编辑器内选中文字拖拽
focusByRange(document.caretRangeFromPoint(event.clientX, event.clientY));
if (event.dataTransfer.types[0] === "Files" && !isBrowser()) {
const files: string[] = [];
for (let i = 0; i < event.dataTransfer.files.length; i++) {
files.push(event.dataTransfer.files[i].path);
}
uploadLocalFiles(files, protyle, !event.altKey);
} else {
paste(protyle, event);
}
}
if (window.siyuan.dragElement) {
window.siyuan.dragElement.style.opacity = "";
window.siyuan.dragElement = undefined;
}
});
let dragoverElement: Element;
let disabledPosition: string;
editorElement.addEventListener("dragover", (event: DragEvent & { target: HTMLElement }) => {
// 设置了的话 drop 就无法监听 shift/control event.dataTransfer.dropEffect = "move";
if (event.dataTransfer.types.includes("Files") && event.target.classList.contains("protyle-wysiwyg")) {
// 文档底部拖拽文件需 preventDefault,否则无法触发 drop 事件 https://github.com/siyuan-note/siyuan/issues/2665
event.preventDefault();
return;
}
let gutterType = "";
for (const item of event.dataTransfer.items) {
if (item.type.startsWith(Constants.SIYUAN_DROP_GUTTER)) {
gutterType = item.type;
}
}
if (!gutterType && !window.siyuan.dragElement) {
// https://github.com/siyuan-note/siyuan/issues/6436
event.preventDefault();
return;
}
if (event.shiftKey || event.altKey) {
const targetElement = hasClosestBlock(event.target);
if (targetElement) {
targetElement.classList.remove("dragover__top", "protyle-wysiwyg--select", "dragover__bottom", "dragover__left", "dragover__right");
}
event.preventDefault();
return;
}
// 编辑器内文字拖拽或资源文件拖拽或按住 alt/shift 拖拽反链图标进入编辑器时不能运行 event.preventDefault(), 否则无光标; 需放在 !window.siyuan.dragElement 之后
event.preventDefault();
const targetElement = hasClosestBlock(event.target) as Element;
if (!targetElement) {
return;
}
const fileTreeIds = (event.dataTransfer.types.includes(Constants.SIYUAN_DROP_FILE) && window.siyuan.dragElement) ? window.siyuan.dragElement.innerText : "";
if (targetElement && dragoverElement && targetElement.isSameNode(dragoverElement)) {
// 性能优化,目标为同一个元素不再进行校验
const nodeRect = targetElement.getBoundingClientRect();
targetElement.classList.remove("protyle-wysiwyg--select", "dragover__top", "dragover__bottom", "dragover__left", "dragover__right");
if (targetElement.getAttribute("data-type") === "NodeListItem" || fileTreeIds.indexOf("-") > -1) {
if (event.clientY > nodeRect.top + nodeRect.height / 2) {
targetElement.classList.add("dragover__bottom", "protyle-wysiwyg--select");
} else {
targetElement.classList.add("dragover__top", "protyle-wysiwyg--select");
}
return;
}
if (event.clientX < nodeRect.left + 32 && event.clientX > nodeRect.left) {
targetElement.classList.add("dragover__left", "protyle-wysiwyg--select");
} else if (event.clientX > nodeRect.right - 32 && event.clientX < nodeRect.right) {
targetElement.classList.add("dragover__right", "protyle-wysiwyg--select");
} else {
if (event.clientY > nodeRect.top + nodeRect.height / 2 && disabledPosition !== "bottom") {
targetElement.classList.add("dragover__bottom", "protyle-wysiwyg--select");
} else if (disabledPosition !== "top") {
targetElement.classList.add("dragover__top", "protyle-wysiwyg--select");
}
}
return;
}
if (fileTreeIds.indexOf("-") > -1) {
if (fileTreeIds.split(",").includes(protyle.block.rootID)) {
dragoverElement = undefined;
} else {
dragoverElement = targetElement;
}
return;
}
if (gutterType) {
disabledPosition = "";
// gutter 文档内拖拽限制
// 排除自己及子孙
const gutterTypes = gutterType.replace(Constants.SIYUAN_DROP_GUTTER, "").split(Constants.ZWSP);
const isSelf = gutterTypes[2].split(",").find((item: string) => {
if (item && hasClosestByAttribute(targetElement, "data-node-id", item)) {
return true;
}
});
if (isSelf) {
return;
}
if (hasClosestByAttribute(targetElement.parentElement, "data-type", "NodeBlockQueryEmbed")) {
// 不允许托入嵌入块
return;
}
if (gutterTypes[0] === "nodelistitem" &&
gutterTypes[1] !== targetElement.getAttribute("data-subtype") &&
"NodeListItem" === targetElement.getAttribute("data-type")) {
// 排除类型不同的列表项
return;
}
if (gutterTypes[0] !== "nodelistitem" && targetElement.getAttribute("data-type") === "NodeListItem") {
// 非列表项不能拖入列表项周围
return;
}
if (gutterTypes[0] === "nodelistitem" && targetElement.parentElement.classList.contains("li") &&
targetElement.previousElementSibling?.classList.contains("protyle-action")) {
// 列表项不能拖入列表项中第一个元素之上
disabledPosition = "top";
}
if (gutterTypes[0] === "nodelistitem" && targetElement.nextElementSibling?.classList.contains("list")) {
// 列表项不能拖入列表上方块的下面
disabledPosition = "bottom";
}
dragoverElement = targetElement;
}
});
editorElement.addEventListener("dragleave", (event: DragEvent & { target: HTMLElement }) => {
const nodeElement = hasClosestBlock(event.target);
if (nodeElement) {
let gutterType = "";
for (const item of event.dataTransfer.items) {
if (item.type.startsWith(Constants.SIYUAN_DROP_GUTTER)) {
gutterType = item.type;
}
}
if (gutterType.indexOf(nodeElement.getAttribute("data-node-id")) === -1) {
// 选中的元素不应移除,否则拖拽 gutter 经过选中的元素,该元素就会被取消选中
nodeElement.classList.remove("protyle-wysiwyg--select");
nodeElement.removeAttribute("select-start");
nodeElement.removeAttribute("select-end");
}
nodeElement.classList.remove("dragover__top", "dragover__bottom", "dragover__left", "dragover__right");
}
});
};