dragon-editor 3.4.5 → 3.5.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (70) hide show
  1. package/README.md +9 -15
  2. package/dist/module.d.mts +5 -0
  3. package/dist/module.json +8 -1
  4. package/dist/module.mjs +6 -4
  5. package/dist/runtime/components/DragonEditor.vue +252 -734
  6. package/dist/runtime/components/DragonEditorViewer.vue +59 -45
  7. package/dist/runtime/scss/editor.css +83 -34
  8. package/dist/runtime/scss/viewer.css +31 -4
  9. package/dist/runtime/type.d.ts +89 -42
  10. package/dist/runtime/utils/event/block.d.ts +0 -0
  11. package/dist/runtime/utils/event/block.js +78 -0
  12. package/dist/runtime/utils/event/cursor.d.ts +0 -0
  13. package/dist/runtime/utils/{cursor.mjs → event/cursor.js} +4 -16
  14. package/dist/runtime/utils/event/data.d.ts +0 -0
  15. package/dist/runtime/utils/event/data.js +342 -0
  16. package/dist/runtime/utils/event/index.d.ts +8 -0
  17. package/dist/runtime/utils/event/index.js +8 -0
  18. package/dist/runtime/utils/event/keyboard.d.ts +0 -0
  19. package/dist/runtime/utils/event/keyboard.js +1368 -0
  20. package/dist/runtime/utils/event/mouse.d.ts +0 -0
  21. package/dist/runtime/utils/event/mouse.js +70 -0
  22. package/dist/runtime/utils/event/scroll.d.ts +0 -0
  23. package/dist/runtime/utils/event/scroll.js +29 -0
  24. package/dist/runtime/utils/event/touch.d.ts +0 -0
  25. package/dist/runtime/utils/event/touch.js +10 -0
  26. package/dist/runtime/utils/event/window.d.ts +0 -0
  27. package/dist/runtime/utils/event/window.js +32 -0
  28. package/dist/runtime/utils/layout/block.d.ts +0 -0
  29. package/dist/runtime/utils/layout/block.js +105 -0
  30. package/dist/runtime/utils/layout/body.d.ts +0 -0
  31. package/dist/runtime/utils/layout/body.js +22 -0
  32. package/dist/runtime/utils/layout/controlbar.d.ts +0 -0
  33. package/dist/runtime/utils/layout/controlbar.js +99 -0
  34. package/dist/runtime/utils/layout/icon.d.ts +0 -0
  35. package/dist/runtime/utils/layout/icon.js +156 -0
  36. package/dist/runtime/utils/layout/index.d.ts +5 -0
  37. package/dist/runtime/utils/layout/index.js +5 -0
  38. package/dist/runtime/utils/layout/menuBar.d.ts +0 -0
  39. package/dist/runtime/utils/layout/menuBar.js +358 -0
  40. package/dist/runtime/utils/node/block.d.ts +0 -0
  41. package/dist/runtime/utils/node/block.js +235 -0
  42. package/dist/runtime/utils/{element.d.ts → node/element.d.ts} +1 -0
  43. package/dist/runtime/utils/{element.mjs → node/element.js} +11 -0
  44. package/dist/runtime/utils/node/index.d.ts +2 -0
  45. package/dist/runtime/utils/node/index.js +2 -0
  46. package/dist/runtime/utils/style/anchor.d.ts +0 -0
  47. package/dist/runtime/utils/style/anchor.js +240 -0
  48. package/dist/runtime/utils/style/decoration.d.ts +0 -0
  49. package/dist/runtime/utils/style/decoration.js +378 -0
  50. package/dist/runtime/utils/style/index.d.ts +2 -0
  51. package/dist/runtime/utils/style/index.js +2 -0
  52. package/dist/types.d.mts +7 -0
  53. package/dist/types.d.ts +3 -3
  54. package/package.json +15 -16
  55. package/dist/runtime/store.d.ts +0 -11
  56. package/dist/runtime/store.mjs +0 -51
  57. package/dist/runtime/utils/block.d.ts +0 -13
  58. package/dist/runtime/utils/block.mjs +0 -144
  59. package/dist/runtime/utils/content.d.ts +0 -2
  60. package/dist/runtime/utils/content.mjs +0 -19
  61. package/dist/runtime/utils/controlBar.d.ts +0 -9
  62. package/dist/runtime/utils/controlBar.mjs +0 -172
  63. package/dist/runtime/utils/convertor.d.ts +0 -3
  64. package/dist/runtime/utils/convertor.mjs +0 -138
  65. package/dist/runtime/utils/cursor.d.ts +0 -6
  66. package/dist/runtime/utils/keyboardEvent.d.ts +0 -10
  67. package/dist/runtime/utils/keyboardEvent.mjs +0 -979
  68. package/dist/runtime/utils/style.d.ts +0 -5
  69. package/dist/runtime/utils/style.mjs +0 -617
  70. /package/dist/runtime/{plugin.mjs → plugin.js} +0 -0
@@ -1,979 +0,0 @@
1
- import { _setCursor, _setCursorData, _clenupCursor, _soltingCursorDataOnElement } from "./cursor.mjs";
2
- import { _getParentElementIfNodeIsText, _findContentEditableElement } from "./element.mjs";
3
- import { _getBlockType, _createTextBlock, _createListItemBlock, _generateId } from "./block.mjs";
4
- import { _setNodeStyle } from "./style.mjs";
5
- let keyEventCount = 0;
6
- let keyEventFn;
7
- export function _elementKeyEvent(event, store) {
8
- _setCursorData(store);
9
- const { type: blockType } = _getBlockType(event.target);
10
- switch (event.code) {
11
- case "Enter":
12
- if (blockType !== "code") {
13
- event.preventDefault();
14
- }
15
- if (blockType === "code" && event.shiftKey === true) {
16
- event.preventDefault();
17
- }
18
- if (keyEventCount === 0) {
19
- _clenupCursor(store);
20
- setTimeout(() => {
21
- if (event.shiftKey === true) {
22
- elementShiftEnterEvent(event, store);
23
- } else {
24
- elementEnterEvent(event, store);
25
- }
26
- });
27
- }
28
- keyEventCount += 1;
29
- setTimeout(() => {
30
- keyEventCount = 0;
31
- }, 200);
32
- break;
33
- case "Backspace":
34
- elementBackspaceEvent(event, store);
35
- break;
36
- case "Tab":
37
- event.preventDefault();
38
- if (keyEventCount === 0) {
39
- _clenupCursor(store);
40
- setTimeout(() => {
41
- elementTabEvent(event, store);
42
- });
43
- }
44
- keyEventCount += 1;
45
- setTimeout(() => {
46
- keyEventCount = 0;
47
- }, 200);
48
- break;
49
- case "Space":
50
- break;
51
- case "ArrowUp":
52
- if (keyEventCount === 0) {
53
- moveToBlockEvent(event, store, "up");
54
- }
55
- keyEventCount += 1;
56
- setTimeout(() => {
57
- keyEventCount = 0;
58
- }, 250);
59
- break;
60
- case "ArrowDown":
61
- if (keyEventCount === 0) {
62
- moveToBlockEvent(event, store, "down");
63
- }
64
- keyEventCount += 1;
65
- setTimeout(() => {
66
- keyEventCount = 0;
67
- }, 250);
68
- break;
69
- }
70
- clearTimeout(keyEventFn);
71
- keyEventFn = setTimeout(() => {
72
- _clenupCursor(store);
73
- const $block = _findContentEditableElement(store.cursorData.startNode);
74
- if ($block !== null) {
75
- store.setCurrentBlock($block.closest(".de-block"));
76
- }
77
- }, 250);
78
- }
79
- function elementEnterEvent(event, store) {
80
- const { $element, type } = _getBlockType(event.target);
81
- switch (type) {
82
- case "text":
83
- case "heading":
84
- defaultBlockEnterEvent(store, $element);
85
- break;
86
- case "list":
87
- listBlockEnterEvent(event, store, $element);
88
- break;
89
- case "code":
90
- break;
91
- }
92
- }
93
- function elementShiftEnterEvent(event, store) {
94
- const { $element, type } = _getBlockType(event.target);
95
- switch (type) {
96
- case "text":
97
- case "heading":
98
- defaultBlockShiftEnterEvent(store, $element);
99
- break;
100
- case "list":
101
- listBlockEnterEvent(event, store, $element);
102
- break;
103
- case "code":
104
- codeBlockShiftEnterEvent($element);
105
- break;
106
- }
107
- }
108
- function elementBackspaceEvent(e, store) {
109
- const { $element, type } = _getBlockType(e.target);
110
- switch (type) {
111
- case "text":
112
- case "heading":
113
- defaultBlockBackspaceEvent(e, store, $element);
114
- break;
115
- case "list":
116
- listBlockBackspaceEvent(e, store, $element);
117
- break;
118
- default:
119
- }
120
- }
121
- function elementTabEvent(e, store) {
122
- const { $element, type } = _getBlockType(e.target);
123
- switch (type) {
124
- case "text":
125
- defaultTabEvent(e.shiftKey, $element);
126
- break;
127
- }
128
- }
129
- function defaultBlockEnterEvent(store, $element) {
130
- const $textBlock = $element;
131
- if (store.cursorData.type === "Caret") {
132
- if ($textBlock.textContent === "") {
133
- if ($textBlock.hasChildNodes() === false) {
134
- const $newTextBlock = _createTextBlock();
135
- $textBlock.insertAdjacentElement("afterend", $newTextBlock);
136
- $newTextBlock.focus();
137
- } else {
138
- const brList = $textBlock.querySelectorAll("br");
139
- let preStructure = [];
140
- let nextStructure = [];
141
- brList.forEach((_, i) => {
142
- const $br = document.createElement("br");
143
- if (store.cursorData.startOffset < i) {
144
- preStructure.push($br);
145
- } else {
146
- nextStructure.push($br);
147
- }
148
- });
149
- $textBlock.replaceChildren(...preStructure);
150
- $textBlock.insertAdjacentElement("afterend", _createTextBlock());
151
- const $nextBlock = $textBlock.nextElementSibling;
152
- if (nextStructure.length === 0) {
153
- $nextBlock.focus();
154
- } else {
155
- if (nextStructure.length === 1) {
156
- nextStructure.push(document.createElement("br"));
157
- }
158
- $nextBlock.replaceChildren(...nextStructure);
159
- _setCursor(nextStructure[0], 0);
160
- }
161
- }
162
- } else {
163
- const childNodeList = [...$element.childNodes];
164
- const $targetNode = _getParentElementIfNodeIsText(store.cursorData.startNode, $textBlock);
165
- const preStructure = [];
166
- const nextStructure = [];
167
- let nodeIdx = -1;
168
- for (let i = 0; childNodeList.length > i; i += 1) {
169
- if (childNodeList[i] === $targetNode) {
170
- nodeIdx = i;
171
- break;
172
- }
173
- }
174
- childNodeList.forEach((node, i) => {
175
- if (nodeIdx < i) {
176
- nextStructure.push(node);
177
- } else if (nodeIdx > i) {
178
- preStructure.push(node);
179
- } else if (nodeIdx === i) {
180
- if (node.constructor.name === "Text") {
181
- const text = node.textContent;
182
- const preText = document.createTextNode(text.slice(0, store.cursorData.startOffset));
183
- const nextText = document.createTextNode(text.slice(store.cursorData.startOffset));
184
- preStructure.push(preText);
185
- nextStructure.push(nextText);
186
- } else {
187
- const originalClassList = [...node.classList];
188
- const text = node.textContent;
189
- const preSpan = document.createElement("span");
190
- const nextSpan = document.createElement("span");
191
- const nextText = text.slice(store.cursorData.startOffset);
192
- preSpan.classList.add(...originalClassList);
193
- preSpan.textContent = text.slice(0, store.cursorData.startOffset);
194
- preStructure.push(preSpan);
195
- if (nextText !== "") {
196
- nextSpan.classList.add(...originalClassList);
197
- nextSpan.textContent = nextText;
198
- nextStructure.push(nextSpan);
199
- }
200
- }
201
- }
202
- });
203
- const $nextBlock = _createTextBlock();
204
- $textBlock.insertAdjacentElement("afterend", $nextBlock);
205
- $textBlock.replaceChildren(...preStructure);
206
- $nextBlock.replaceChildren(...nextStructure);
207
- if (nextStructure.length === 0) {
208
- $nextBlock.focus();
209
- } else {
210
- _setCursor($nextBlock.childNodes[0], 0);
211
- }
212
- }
213
- } else {
214
- const childNodeList = $textBlock.childNodes;
215
- const cursorData = _soltingCursorDataOnElement(store.cursorData, $textBlock);
216
- const preStructure = [];
217
- const nextStructure = [];
218
- if (cursorData.startNodeIdx === cursorData.endNodeIdx) {
219
- childNodeList.forEach((node, i) => {
220
- if (cursorData.startNodeIdx > i) {
221
- preStructure.push(node);
222
- } else if (cursorData.endNodeIdx < i) {
223
- nextStructure.push(node);
224
- } else if (cursorData.startNodeIdx === i) {
225
- if (node.constructor.name === "Text") {
226
- const preText = node.textContent.slice(0, cursorData.startOffset);
227
- const nextText = node.textContent.slice(cursorData.endOffset);
228
- if (preText !== "") {
229
- const textNode = document.createTextNode(preText);
230
- preStructure.push(textNode);
231
- }
232
- if (nextText !== "") {
233
- const textNode = document.createTextNode(nextText);
234
- nextStructure.push(textNode);
235
- }
236
- } else {
237
- const originalClassList = [...node.classList];
238
- const preText = node.textContent.slice(0, cursorData.startOffset);
239
- const nextText = node.textContent.slice(cursorData.endOffset);
240
- if (preText !== "") {
241
- const $span = document.createElement("span");
242
- $span.classList.add(...originalClassList);
243
- $span.textContent = preText;
244
- preStructure.push($span);
245
- }
246
- if (nextText !== "") {
247
- const $span = document.createElement("span");
248
- $span.classList.add(...originalClassList);
249
- $span.textContent = nextText;
250
- nextStructure.push($span);
251
- }
252
- }
253
- }
254
- });
255
- } else {
256
- childNodeList.forEach((node, i) => {
257
- if (cursorData.startNodeIdx > i) {
258
- preStructure.push(node);
259
- } else if (cursorData.startNodeIdx === i) {
260
- if (node.constructor.name === "Text") {
261
- const text = node.textContent.slice(0, cursorData.startOffset);
262
- if (text !== "") {
263
- const textNode = document.createTextNode(text);
264
- preStructure.push(textNode);
265
- }
266
- } else {
267
- const originalClassList = [...node.classList];
268
- const text = node.textContent.slice(0, cursorData.startOffset);
269
- if (text !== "") {
270
- const $span = document.createElement("span");
271
- $span.classList.add(...originalClassList);
272
- $span.textContent = text;
273
- preStructure.push($span);
274
- }
275
- }
276
- }
277
- if (cursorData.endNodeIdx < i) {
278
- nextStructure.push(node);
279
- } else if (cursorData.endNodeIdx === i) {
280
- if (node.constructor.name === "Text") {
281
- const text = node.textContent.slice(cursorData.endOffset);
282
- if (text !== "") {
283
- const textNode = document.createTextNode(text);
284
- nextStructure.push(textNode);
285
- }
286
- } else {
287
- const originalClassList = [...node.classList];
288
- const text = node.textContent.slice(cursorData.endOffset);
289
- const $span = document.createElement("span");
290
- if (text !== "") {
291
- $span.classList.add(...originalClassList);
292
- $span.textContent = text;
293
- nextStructure.push($span);
294
- }
295
- }
296
- }
297
- });
298
- }
299
- const $nextBlock = _createTextBlock({ type: "text", classList: [], textContent: "" });
300
- $textBlock.insertAdjacentElement("afterend", $nextBlock);
301
- $textBlock.replaceChildren(...preStructure);
302
- $nextBlock.replaceChildren(...nextStructure);
303
- if (nextStructure.length === 0) {
304
- $nextBlock.focus();
305
- } else {
306
- _setCursor($nextBlock.childNodes[0], 0);
307
- }
308
- }
309
- }
310
- function listBlockEnterEvent(event, store, $element) {
311
- const $listBlock = $element;
312
- const $editableElement = _findContentEditableElement(event.target);
313
- const liList = $listBlock.querySelectorAll(".de-item");
314
- let liIdx = -1;
315
- for (let i = 0; liList.length > i; i += 1) {
316
- if (liList[i] === $editableElement) {
317
- liIdx = i;
318
- break;
319
- }
320
- }
321
- if (store.cursorData.type === "Caret") {
322
- if ($editableElement.textContent === "") {
323
- if (liList.length - 1 === liIdx) {
324
- const $textBlock = _createTextBlock();
325
- $listBlock.insertAdjacentElement("afterend", $textBlock);
326
- if (liList.length === 1) {
327
- $listBlock.remove();
328
- } else {
329
- $editableElement.remove();
330
- }
331
- $textBlock.focus();
332
- } else {
333
- const $liBlock = _createListItemBlock();
334
- $editableElement.insertAdjacentElement("afterend", $liBlock);
335
- $liBlock.focus();
336
- }
337
- } else {
338
- const childNodeList = $editableElement.childNodes;
339
- const targetNode = _getParentElementIfNodeIsText(store.cursorData.startNode, $editableElement);
340
- const preStructure = [];
341
- const nextStructure = [];
342
- let nodeIdx = -1;
343
- for (let i = 0; childNodeList.length > i; i += 1) {
344
- if (childNodeList[i] === targetNode) {
345
- nodeIdx = i;
346
- break;
347
- }
348
- }
349
- childNodeList.forEach((node, i) => {
350
- if (nodeIdx < i) {
351
- nextStructure.push(node);
352
- } else if (nodeIdx > i) {
353
- preStructure.push(node);
354
- } else if (nodeIdx === i) {
355
- if (node.constructor.name === "Text") {
356
- const preText = node.textContent.slice(0, store.cursorData.startOffset);
357
- const nextText = node.textContent.slice(store.cursorData.endOffset);
358
- if (preText !== "") {
359
- preStructure.push(document.createTextNode(preText));
360
- }
361
- if (nextText !== "") {
362
- nextStructure.push(document.createTextNode(nextText));
363
- }
364
- } else {
365
- const originalClassList = [...node.classList];
366
- const preText = node.textContent.slice(0, store.cursorData.startOffset);
367
- const nextText = node.textContent.slice(store.cursorData.endOffset);
368
- if (preText !== "") {
369
- const $span = document.createElement("span");
370
- $span.textContent = preText;
371
- $span.classList.add(...originalClassList);
372
- preStructure.push($span);
373
- }
374
- if (nextText !== "") {
375
- const $span = document.createElement("span");
376
- $span.textContent = nextText;
377
- $span.classList.add(...originalClassList);
378
- nextStructure.push($span);
379
- }
380
- }
381
- }
382
- });
383
- const $liBlock = _createListItemBlock();
384
- $editableElement.insertAdjacentElement("afterend", $liBlock);
385
- $editableElement.replaceChildren(...preStructure);
386
- $liBlock.replaceChildren(...nextStructure);
387
- if (nextStructure.length === 0) {
388
- $liBlock.focus();
389
- } else {
390
- _setCursor($liBlock.childNodes[0], 0);
391
- }
392
- }
393
- } else {
394
- const childNodeList = $editableElement.childNodes;
395
- const cursorData = _soltingCursorDataOnElement(store.cursorData, $editableElement);
396
- const preStructure = [];
397
- const nextStructure = [];
398
- childNodeList.forEach((node, i) => {
399
- if (cursorData.startNodeIdx > i) {
400
- preStructure.push(node);
401
- } else if (cursorData.startNodeIdx === i) {
402
- if (node.constructor.name === "Text") {
403
- const text = node.textContent.slice(0, cursorData.startOffset);
404
- if (text !== "") {
405
- const $textNode = document.createTextNode(text);
406
- preStructure.push($textNode);
407
- }
408
- } else {
409
- const originalClassList = [...node.classList];
410
- const text = node.textContent.slice(0, cursorData.startOffset);
411
- if (text !== "") {
412
- const $span = document.createElement("span");
413
- $span.classList.add(...originalClassList);
414
- $span.textContent = text;
415
- preStructure.push($span);
416
- }
417
- }
418
- }
419
- if (cursorData.endNodeIdx < i) {
420
- nextStructure.push(node);
421
- } else if (cursorData.endNodeIdx === i) {
422
- if (node.constructor.name === "Text") {
423
- const text = node.textContent.slice(cursorData.endOffset);
424
- if (text !== "") {
425
- const $textNode = document.createTextNode(text);
426
- nextStructure.push($textNode);
427
- }
428
- } else {
429
- const originalClassList = [...node.classList];
430
- const text = node.textContent.slice(cursorData.endOffset);
431
- if (text !== "") {
432
- const $span = document.createElement("span");
433
- $span.classList.add(...originalClassList);
434
- $span.textContent = text;
435
- nextStructure.push($span);
436
- }
437
- }
438
- }
439
- });
440
- const $liBlock = _createListItemBlock();
441
- $editableElement.insertAdjacentElement("afterend", $liBlock);
442
- $editableElement.replaceChildren(...preStructure);
443
- if (nextStructure.length === 0) {
444
- $liBlock.focus();
445
- } else {
446
- $liBlock.replaceChildren(...nextStructure);
447
- _setCursor(nextStructure[0], 0);
448
- }
449
- }
450
- }
451
- function defaultBlockShiftEnterEvent(store, $element) {
452
- const $textBlock = $element;
453
- if (store.cursorData.type === "Caret") {
454
- if ($textBlock.textContent === "") {
455
- if ($textBlock.hasChildNodes() === false) {
456
- $textBlock.insertAdjacentHTML("beforeend", "<br><br>");
457
- _setCursor($textBlock.childNodes[1], 0);
458
- } else {
459
- let $target = store.cursorData.startNode;
460
- let childIdx = -1;
461
- if ($textBlock === $target) {
462
- $target = $textBlock.childNodes[store.cursorData.startOffset];
463
- }
464
- for (let i = 0; $textBlock.childNodes.length > i; i += 1) {
465
- if ($textBlock.childNodes[i] === $target) {
466
- childIdx = i;
467
- break;
468
- }
469
- }
470
- $target.insertAdjacentHTML("afterend", "<br>");
471
- _setCursor($textBlock.childNodes[childIdx + 1], 0);
472
- }
473
- } else {
474
- const childList = $textBlock.childNodes;
475
- let targetIdx = -1;
476
- let structure = "";
477
- let $target = store.cursorData.startNode;
478
- if ($target.constructor.name === "Text") {
479
- if ($target.parentNode !== $textBlock) {
480
- $target = $target.parentNode;
481
- }
482
- }
483
- if ($textBlock === $target) {
484
- $target = $textBlock.childNodes[store.cursorData.startOffset];
485
- }
486
- for (let i = 0; childList.length > i; i += 1) {
487
- if (childList[i] === $target) {
488
- targetIdx = i;
489
- break;
490
- }
491
- }
492
- let curruntIdx = targetIdx;
493
- childList.forEach((child, i) => {
494
- if (i === targetIdx) {
495
- const constructorName = child.constructor.name;
496
- if (constructorName === "Text") {
497
- structure += child.textContent.slice(0, store.cursorData.startOffset) + "<br>" + child.textContent.slice(store.cursorData.endOffset);
498
- if (child.nextSibling === null) {
499
- if (child.textContent.slice(store.cursorData.endOffset) === "") {
500
- structure += `<br>`;
501
- }
502
- if (child.textContent.slice(0, store.cursorData.startOffset) === "") {
503
- curruntIdx -= 1;
504
- }
505
- } else {
506
- if (child.textContent.slice(store.cursorData.endOffset) !== "" && child.textContent.slice(0, store.cursorData.startOffset) === "") {
507
- curruntIdx -= 1;
508
- }
509
- }
510
- curruntIdx += 1;
511
- } else {
512
- if (constructorName === "HTMLBRElement") {
513
- structure += `<br><br>`;
514
- } else {
515
- structure += `<span class="${[...child.classList].join(" ")}">${child.textContent.slice(0, store.cursorData.startOffset)}</span>`;
516
- structure += `<br>`;
517
- structure += `<span class="${[...child.classList].join(" ")}">${child.textContent.slice(store.cursorData.startOffset)}</span>`;
518
- curruntIdx += 1;
519
- }
520
- }
521
- } else {
522
- if (child.constructor.name === "Text") {
523
- structure += child.textContent;
524
- } else {
525
- structure += child.outerHTML;
526
- }
527
- }
528
- });
529
- $textBlock.innerHTML = structure;
530
- _setCursor($textBlock.childNodes[curruntIdx + 1], 0);
531
- }
532
- } else {
533
- const childNodeList = $textBlock.childNodes;
534
- let startTargetNode = store.cursorData.startNode;
535
- let startNodeIdx = -1;
536
- let startOffset = store.cursorData.startOffset;
537
- let endTargetNode = store.cursorData.endNode;
538
- let endNodeIdx = -1;
539
- let endOffset = store.cursorData.endOffset;
540
- let structure = "";
541
- if (startTargetNode.constructor.name === "Text") {
542
- if (startTargetNode.parentElement !== $textBlock) {
543
- startTargetNode = startTargetNode.parentElement;
544
- }
545
- }
546
- if (endTargetNode.constructor.name === "Text") {
547
- if (endTargetNode.parentElement !== $textBlock) {
548
- endTargetNode = endTargetNode.parentElement;
549
- }
550
- }
551
- for (let i = 0; childNodeList.length > i; i += 1) {
552
- if (startTargetNode === childNodeList[i]) {
553
- startNodeIdx = i;
554
- }
555
- if (endTargetNode === childNodeList[i]) {
556
- endNodeIdx = i;
557
- }
558
- if (startNodeIdx !== -1 && endNodeIdx !== -1) {
559
- break;
560
- }
561
- }
562
- if (startNodeIdx !== endNodeIdx) {
563
- if (startNodeIdx > endNodeIdx) {
564
- const originalEndNodeIdx = endNodeIdx;
565
- const originalEndOffset = endOffset;
566
- const originalStartNodeIdx = startNodeIdx;
567
- const originalStartOffset = startOffset;
568
- startNodeIdx = originalEndNodeIdx;
569
- startOffset = originalEndOffset;
570
- endNodeIdx = originalStartNodeIdx;
571
- endOffset = originalStartOffset;
572
- }
573
- } else {
574
- if (startOffset > endOffset) {
575
- const originalEndOffset = endOffset;
576
- const originalStartOffset = startOffset;
577
- startOffset = originalEndOffset;
578
- endOffset = originalStartOffset;
579
- }
580
- }
581
- childNodeList.forEach((node, i) => {
582
- if (startNodeIdx > i) {
583
- if (node.constructor.name === "Text") {
584
- structure += node.textContent;
585
- } else {
586
- structure += node.outerHTML;
587
- }
588
- } else if (startNodeIdx === i) {
589
- if (node.constructor.name === "Text") {
590
- structure += node.textContent.slice(0, startOffset);
591
- structure += `<br>`;
592
- } else {
593
- if (node.tagName === "BR") {
594
- structure += `<br>`;
595
- } else {
596
- const originalClassList = [...node.classList];
597
- const text = node.textContent;
598
- structure += `<span class="${originalClassList.join(" ")}">${text.slice(0, startOffset)}</span><br>`;
599
- }
600
- }
601
- if (childNodeList.length === i) {
602
- structure += `<br>`;
603
- }
604
- }
605
- if (endNodeIdx < i) {
606
- if (node.constructor.name === "Text") {
607
- structure += node.textContent;
608
- } else {
609
- structure += node.outerHTML;
610
- }
611
- } else if (endNodeIdx === i) {
612
- if (node.constructor.name === "Text") {
613
- structure += node.textContent.slice(endOffset);
614
- } else {
615
- if (node.tagName === "BR") {
616
- structure += `<br>`;
617
- } else {
618
- const originalClassList = [...node.classList];
619
- const text = node.textContent;
620
- structure += `<span class="${originalClassList.join(" ")}">${text.slice(endOffset)}</span><br>`;
621
- }
622
- }
623
- }
624
- });
625
- $textBlock.innerHTML = structure;
626
- _setCursor($textBlock.childNodes[startNodeIdx + 2], 0);
627
- }
628
- }
629
- function codeBlockShiftEnterEvent($element) {
630
- const $newTextBlock = _createTextBlock();
631
- $element.insertAdjacentElement("afterend", $newTextBlock);
632
- $newTextBlock.focus();
633
- }
634
- function defaultBlockBackspaceEvent(e, store, $element) {
635
- const $textBlock = $element;
636
- const childList = store.$content.querySelectorAll(".de-block");
637
- const $target = _getParentElementIfNodeIsText(store.cursorData.startNode, $textBlock);
638
- let elementIdx = -1;
639
- for (let i = 0; childList.length > i; i += 1) {
640
- if (childList[i] === $element) {
641
- elementIdx = i;
642
- break;
643
- }
644
- }
645
- if (elementIdx === 0) {
646
- if (store.cursorData.startOffset === 0 && $target === $textBlock) {
647
- if ($target.textContent === "") {
648
- $textBlock.insertAdjacentElement("afterend", _createTextBlock());
649
- _setCursor($textBlock.nextElementSibling, 0);
650
- $textBlock.remove();
651
- } else {
652
- e.preventDefault();
653
- if ($textBlock.tagName !== "P") {
654
- $textBlock.insertAdjacentElement(
655
- "afterend",
656
- _createTextBlock({
657
- type: "text",
658
- classList: [],
659
- textContent: $textBlock.textContent ?? ""
660
- })
661
- );
662
- _setCursor($textBlock.nextElementSibling, 0);
663
- $textBlock.remove();
664
- }
665
- }
666
- }
667
- } else {
668
- if ($textBlock.hasChildNodes() === false) {
669
- e.preventDefault();
670
- const $preBlock = $textBlock.previousElementSibling;
671
- const { type: preBlockType } = _getBlockType($preBlock);
672
- $textBlock.remove();
673
- if (preBlockType === "text" || preBlockType === "heading") {
674
- if ($preBlock.hasChildNodes() === true) {
675
- const textBlockChildList = $preBlock.childNodes;
676
- const textBlockTargetChild = textBlockChildList[textBlockChildList.length - 1];
677
- _setCursor(textBlockTargetChild, textBlockTargetChild.textContent.length);
678
- } else {
679
- _setCursor($preBlock, 0);
680
- }
681
- }
682
- } else {
683
- if (store.cursorData.type === "Caret" && store.cursorData.startOffset === 0 && ($textBlock.childNodes[0] === store.cursorData.startNode || $textBlock.childNodes[0] === $target)) {
684
- e.preventDefault();
685
- const $preBlock = $textBlock.previousElementSibling;
686
- const { type: preBlockType } = _getBlockType($preBlock);
687
- if (preBlockType === "text" || preBlockType === "heading") {
688
- if ($preBlock.hasChildNodes() === true) {
689
- const textBlockChildList = $preBlock.childNodes;
690
- const textBlockTargetChildIdx = textBlockChildList.length - 1;
691
- const textBlockTargetCursorIdx = textBlockChildList[textBlockTargetChildIdx].textContent.length;
692
- const thisBlockHTML = $textBlock.innerHTML;
693
- $preBlock.innerHTML = $preBlock.innerHTML + thisBlockHTML;
694
- _setCursor($preBlock.childNodes[textBlockTargetChildIdx], textBlockTargetCursorIdx);
695
- } else {
696
- $preBlock.innerHTML = $textBlock.innerHTML;
697
- _setCursor($preBlock, 0);
698
- }
699
- $textBlock.remove();
700
- }
701
- }
702
- }
703
- }
704
- if (store.cursorData.startOffset === 1 && $target !== $textBlock) {
705
- if ($target.textContent.length === 1) {
706
- e.preventDefault();
707
- const preNode = $target.previousSibling;
708
- if (preNode !== null) {
709
- $target.remove();
710
- _setCursor(preNode, preNode.textContent.length);
711
- } else {
712
- if ($textBlock.childNodes[1] === void 0) {
713
- $textBlock.innerHTML = "";
714
- _setCursor($textBlock, 0);
715
- } else {
716
- _setCursor($textBlock.childNodes[1], 0);
717
- $target.remove();
718
- }
719
- }
720
- }
721
- }
722
- _setCursorData(store);
723
- }
724
- function listBlockBackspaceEvent(e, store, $element) {
725
- const $listBlock = $element;
726
- const $targetItem = _findContentEditableElement(store.cursorData.startNode);
727
- const liList = $listBlock.querySelectorAll(".de-item");
728
- const $target = _getParentElementIfNodeIsText(store.cursorData.startNode, $targetItem);
729
- let liIdx = -1;
730
- for (let i = 0; liList.length > i; i += 1) {
731
- if (liList[i] === $targetItem) {
732
- liIdx = i;
733
- break;
734
- }
735
- }
736
- if (liList.length === 1) {
737
- if ($targetItem.textContent === "") {
738
- e.preventDefault();
739
- const $preBlock = $listBlock.previousElementSibling;
740
- const { type: preBlockType } = _getBlockType($preBlock);
741
- $listBlock.remove();
742
- if (preBlockType === "text" || preBlockType === "heading") {
743
- if ($preBlock.hasChildNodes() === true) {
744
- const textBlockChildList = $preBlock.childNodes;
745
- const textBlockTargetChild = textBlockChildList[textBlockChildList.length - 1];
746
- _setCursor(textBlockTargetChild, textBlockTargetChild.textContent.length);
747
- } else {
748
- _setCursor($preBlock, 0);
749
- }
750
- }
751
- } else {
752
- if (store.cursorData.type === "Caret" && store.cursorData.startOffset === 0 && ($targetItem.childNodes[0] === store.cursorData.startNode || $targetItem.childNodes[0] === $target)) {
753
- e.preventDefault();
754
- const $preBlock = $listBlock.previousElementSibling;
755
- const { type: preBlockType } = _getBlockType($preBlock);
756
- if (preBlockType === "text" || preBlockType === "heading") {
757
- if ($preBlock.hasChildNodes() === true) {
758
- const textBlockChildList = $preBlock.childNodes;
759
- const textBlockTargetChildIdx = textBlockChildList.length - 1;
760
- const textBlockTargetCursorIdx = textBlockChildList[textBlockTargetChildIdx].textContent.length;
761
- const thisBlockHTML = $targetItem.innerHTML;
762
- $preBlock.innerHTML = $preBlock.innerHTML + thisBlockHTML;
763
- _setCursor($preBlock.childNodes[textBlockTargetChildIdx], textBlockTargetCursorIdx);
764
- } else {
765
- $preBlock.innerHTML = $targetItem.innerHTML;
766
- _setCursor($preBlock, 0);
767
- }
768
- $targetItem.remove();
769
- $listBlock.remove();
770
- }
771
- }
772
- }
773
- } else {
774
- if (liIdx === 0) {
775
- if ($targetItem.textContent === "") {
776
- e.preventDefault();
777
- const $preBlock = $listBlock.previousElementSibling;
778
- const { type: preBlockType } = _getBlockType($preBlock);
779
- $targetItem.remove();
780
- if (preBlockType === "text" || preBlockType === "heading") {
781
- if ($preBlock.hasChildNodes() === true) {
782
- const textBlockChildList = $preBlock.childNodes;
783
- const textBlockTargetChild = textBlockChildList[textBlockChildList.length - 1];
784
- _setCursor(textBlockTargetChild, textBlockTargetChild.textContent.length);
785
- } else {
786
- _setCursor($preBlock, 0);
787
- }
788
- }
789
- } else {
790
- if (store.cursorData.type === "Caret" && store.cursorData.startOffset === 0 && ($targetItem.childNodes[0] === store.cursorData.startNode || $targetItem.childNodes[0] === $target)) {
791
- e.preventDefault();
792
- const $preBlock = $listBlock.previousElementSibling;
793
- const { type: preBlockType } = _getBlockType($preBlock);
794
- if (preBlockType === "text" || preBlockType === "heading") {
795
- if ($preBlock.hasChildNodes() === true) {
796
- const textBlockChildList = $preBlock.childNodes;
797
- const textBlockTargetChildIdx = textBlockChildList.length - 1;
798
- const textBlockTargetCursorIdx = textBlockChildList[textBlockTargetChildIdx].textContent.length;
799
- const thisBlockHTML = $targetItem.innerHTML;
800
- $preBlock.innerHTML = $preBlock.innerHTML + thisBlockHTML;
801
- _setCursor($preBlock.childNodes[textBlockTargetChildIdx], textBlockTargetCursorIdx);
802
- } else {
803
- $preBlock.innerHTML = $targetItem.innerHTML;
804
- _setCursor($preBlock, 0);
805
- }
806
- $targetItem.remove();
807
- }
808
- }
809
- }
810
- } else {
811
- if ($targetItem.textContent === "") {
812
- e.preventDefault();
813
- const $preBlock = liList[liIdx - 1];
814
- $targetItem.remove();
815
- if ($preBlock.hasChildNodes() === true) {
816
- const textBlockChildList = $preBlock.childNodes;
817
- const textBlockTargetChild = textBlockChildList[textBlockChildList.length - 1];
818
- _setCursor(textBlockTargetChild, textBlockTargetChild.textContent.length);
819
- } else {
820
- _setCursor($preBlock, 0);
821
- }
822
- } else {
823
- if (store.cursorData.type === "Caret" && store.cursorData.startOffset === 0 && ($targetItem.childNodes[0] === store.cursorData.startNode || $targetItem.childNodes[0] === $target)) {
824
- e.preventDefault();
825
- const $preBlock = liList[liIdx - 1];
826
- if ($preBlock.hasChildNodes() === true) {
827
- const textBlockChildList = $preBlock.childNodes;
828
- const textBlockTargetChildIdx = textBlockChildList.length - 1;
829
- const textBlockTargetCursorIdx = textBlockChildList[textBlockTargetChildIdx].textContent.length;
830
- const thisBlockHTML = $targetItem.innerHTML;
831
- $preBlock.innerHTML = $preBlock.innerHTML + thisBlockHTML;
832
- _setCursor($preBlock.childNodes[textBlockTargetChildIdx], textBlockTargetCursorIdx);
833
- } else {
834
- $preBlock.innerHTML = $targetItem.innerHTML;
835
- _setCursor($preBlock, 0);
836
- }
837
- $targetItem.remove();
838
- }
839
- }
840
- }
841
- }
842
- }
843
- function defaultTabEvent(useShiftKey, $element) {
844
- const $target = $element;
845
- let value = $target.dataset["depth"] === void 0 ? 0 : parseInt($target.dataset["depth"]);
846
- if (useShiftKey === true) {
847
- if (value !== 0) {
848
- value -= 1;
849
- }
850
- } else {
851
- if (value < 5) {
852
- value += 1;
853
- }
854
- }
855
- if (value === 0) {
856
- delete $target.dataset["depth"];
857
- } else {
858
- $target.dataset["depth"] = String(value);
859
- }
860
- }
861
- function moveToBlockEvent(e, store, keyType) {
862
- const $editableElement = _findContentEditableElement(store.cursorData.startNode);
863
- if ($editableElement !== null) {
864
- const { $element, type } = _getBlockType($editableElement);
865
- let $target;
866
- if (store.cursorData.startIdx > store.cursorData.endIdx) {
867
- _setCursor(store.cursorData.startNode, store.cursorData.endIdx);
868
- } else {
869
- _setCursor(store.cursorData.startNode, store.cursorData.startIdx);
870
- }
871
- switch (type) {
872
- case "list":
873
- e.preventDefault();
874
- if (keyType === "up") {
875
- $target = $editableElement.previousElementSibling;
876
- } else {
877
- $target = $editableElement.nextElementSibling;
878
- }
879
- if ($target === null) {
880
- if (keyType === "up") {
881
- $target = $element.previousElementSibling;
882
- } else {
883
- $target = $element.nextElementSibling;
884
- }
885
- }
886
- break;
887
- case "code":
888
- $target = null;
889
- break;
890
- default:
891
- e.preventDefault();
892
- if (keyType === "up") {
893
- $target = $element.previousElementSibling;
894
- } else {
895
- $target = $element.nextElementSibling;
896
- }
897
- }
898
- if ($target !== null) {
899
- _clenupCursor(store);
900
- setTimeout(() => {
901
- if ($target.classList.contains("de-block") == true) {
902
- const { $element: $element2, type: targetType } = _getBlockType($target);
903
- switch (targetType) {
904
- case "list":
905
- const $targetItem = $element2.querySelectorAll(".de-item");
906
- let $item;
907
- if (keyType === "up") {
908
- $item = $targetItem[$targetItem.length - 1];
909
- } else {
910
- $item = $targetItem[0];
911
- }
912
- _setCursor($item, 0);
913
- break;
914
- default:
915
- _setCursor($element2, 0);
916
- }
917
- } else {
918
- _setCursor($target, 0);
919
- }
920
- });
921
- }
922
- }
923
- }
924
- export function _hotKeyEvent(event, store) {
925
- _setCursorData(store);
926
- const isControlKeyActive = event.ctrlKey || event.metaKey;
927
- if (isControlKeyActive === true) {
928
- switch (event.key) {
929
- case "b":
930
- event.preventDefault();
931
- _setNodeStyle("de-bold", store);
932
- break;
933
- case "i":
934
- event.preventDefault();
935
- _setNodeStyle("de-italic", store);
936
- break;
937
- case "u":
938
- event.preventDefault();
939
- _setNodeStyle("de-underline", store);
940
- break;
941
- case "s":
942
- if (event.shiftKey === true) {
943
- event.preventDefault();
944
- _setNodeStyle("de-strikethrough", store);
945
- }
946
- break;
947
- case "c":
948
- if (event.shiftKey === true) {
949
- event.preventDefault();
950
- _setNodeStyle("de-code", store);
951
- }
952
- break;
953
- }
954
- }
955
- }
956
- export function _copyEvent(event, store) {
957
- }
958
- export async function _pasteEvent(event, store, emit) {
959
- event.preventDefault();
960
- const text = await navigator.clipboard.readText();
961
- const $block = event.target.closest(".de-block");
962
- if ($block !== null) {
963
- if (text === "") {
964
- const clipboardItems = await navigator.clipboard.read();
965
- const imageItem = clipboardItems[0].types.find((type) => type.startsWith("image/"));
966
- if (imageItem !== void 0) {
967
- const blob = await clipboardItems[0].getType(imageItem);
968
- const file = new File([blob], `${_generateId()}.${imageItem.split("/")[1]}`);
969
- emit("uploadImageEvent", file);
970
- }
971
- } else {
972
- const selection = window.getSelection();
973
- const textNode = document.createTextNode(text);
974
- selection.deleteFromDocument();
975
- selection.getRangeAt(0).insertNode(textNode);
976
- _setCursor(textNode, textNode.length);
977
- }
978
- }
979
- }