dragon-editor 3.8.6 → 4.0.0

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 (91) hide show
  1. package/README.md +10 -9
  2. package/dist/module.json +1 -1
  3. package/dist/module.mjs +9 -7
  4. package/dist/runtime/components/Block/Code.d.vue.ts +13 -0
  5. package/dist/runtime/components/Block/Code.vue +193 -0
  6. package/dist/runtime/components/Block/Code.vue.d.ts +13 -0
  7. package/dist/runtime/components/Block/Custom.d.vue.ts +9 -0
  8. package/dist/runtime/components/Block/Custom.vue +28 -0
  9. package/dist/runtime/components/Block/Custom.vue.d.ts +9 -0
  10. package/dist/runtime/components/Block/Divider.d.vue.ts +9 -0
  11. package/dist/runtime/components/Block/Divider.vue +27 -0
  12. package/dist/runtime/components/Block/Divider.vue.d.ts +9 -0
  13. package/dist/runtime/components/Block/Heading.d.vue.ts +13 -0
  14. package/dist/runtime/components/Block/Heading.vue +84 -0
  15. package/dist/runtime/components/Block/Heading.vue.d.ts +13 -0
  16. package/dist/runtime/components/Block/Image.d.vue.ts +13 -0
  17. package/dist/runtime/components/Block/Image.vue +146 -0
  18. package/dist/runtime/components/Block/Image.vue.d.ts +13 -0
  19. package/dist/runtime/components/Block/List.d.vue.ts +13 -0
  20. package/dist/runtime/components/Block/List.vue +170 -0
  21. package/dist/runtime/components/Block/List.vue.d.ts +13 -0
  22. package/dist/runtime/components/Block/Text.d.vue.ts +13 -0
  23. package/dist/runtime/components/Block/Text.vue +84 -0
  24. package/dist/runtime/components/Block/Text.vue.d.ts +13 -0
  25. package/dist/runtime/components/DragonEditor.d.vue.ts +28 -22
  26. package/dist/runtime/components/DragonEditor.vue +75 -1225
  27. package/dist/runtime/components/DragonEditor.vue.d.ts +28 -22
  28. package/dist/runtime/components/DragonEditorViewer.d.vue.ts +13 -5
  29. package/dist/runtime/components/DragonEditorViewer.vue +17 -813
  30. package/dist/runtime/components/DragonEditorViewer.vue.d.ts +13 -5
  31. package/dist/runtime/components/MenuBar.d.vue.ts +3 -0
  32. package/dist/runtime/components/MenuBar.vue +385 -0
  33. package/dist/runtime/components/MenuBar.vue.d.ts +3 -0
  34. package/dist/runtime/scss/editor.css +237 -283
  35. package/dist/runtime/scss/viewer.css +184 -210
  36. package/dist/runtime/store/editor.d.ts +2 -0
  37. package/dist/runtime/store/editor.js +59 -0
  38. package/dist/runtime/type.d.mts +101 -124
  39. package/dist/runtime/utils/data/block.d.ts +20 -0
  40. package/dist/runtime/utils/data/block.js +663 -0
  41. package/dist/runtime/utils/data/cursor.d.ts +9 -0
  42. package/dist/runtime/utils/data/cursor.js +137 -0
  43. package/dist/runtime/utils/data/index.d.ts +5 -0
  44. package/dist/runtime/utils/data/index.js +17 -0
  45. package/dist/runtime/utils/data/node.d.ts +9 -0
  46. package/dist/runtime/utils/data/node.js +101 -0
  47. package/dist/runtime/utils/event/cursor.d.ts +3 -6
  48. package/dist/runtime/utils/event/cursor.js +52 -105
  49. package/dist/runtime/utils/event/index.d.ts +2 -6
  50. package/dist/runtime/utils/event/index.js +48 -6
  51. package/dist/runtime/utils/event/keyboard.d.ts +22 -7
  52. package/dist/runtime/utils/event/keyboard.js +910 -1406
  53. package/dist/runtime/utils/layout/body.d.ts +3 -4
  54. package/dist/runtime/utils/layout/body.js +83 -51
  55. package/dist/runtime/utils/layout/icon.d.ts +1 -2
  56. package/dist/runtime/utils/layout/icon.js +64 -67
  57. package/dist/runtime/utils/layout/index.d.ts +0 -3
  58. package/dist/runtime/utils/layout/index.js +0 -3
  59. package/dist/runtime/utils/node/element.d.ts +4 -6
  60. package/dist/runtime/utils/node/element.js +58 -71
  61. package/dist/runtime/utils/node/index.d.ts +1 -1
  62. package/dist/runtime/utils/node/index.js +1 -1
  63. package/dist/runtime/utils/node/style.d.ts +5 -0
  64. package/dist/runtime/utils/node/style.js +702 -0
  65. package/package.json +31 -17
  66. package/dist/runtime/utils/event/block.d.ts +0 -7
  67. package/dist/runtime/utils/event/block.js +0 -110
  68. package/dist/runtime/utils/event/data.d.ts +0 -8
  69. package/dist/runtime/utils/event/data.js +0 -379
  70. package/dist/runtime/utils/event/mouse.d.ts +0 -11
  71. package/dist/runtime/utils/event/mouse.js +0 -85
  72. package/dist/runtime/utils/event/scroll.d.ts +0 -3
  73. package/dist/runtime/utils/event/scroll.js +0 -29
  74. package/dist/runtime/utils/event/touch.d.ts +0 -5
  75. package/dist/runtime/utils/event/touch.js +0 -10
  76. package/dist/runtime/utils/event/window.d.ts +0 -5
  77. package/dist/runtime/utils/event/window.js +0 -47
  78. package/dist/runtime/utils/layout/block.d.ts +0 -7
  79. package/dist/runtime/utils/layout/block.js +0 -123
  80. package/dist/runtime/utils/layout/controlbar.d.ts +0 -3
  81. package/dist/runtime/utils/layout/controlbar.js +0 -99
  82. package/dist/runtime/utils/layout/menuBar.d.ts +0 -3
  83. package/dist/runtime/utils/layout/menuBar.js +0 -408
  84. package/dist/runtime/utils/node/block.d.ts +0 -19
  85. package/dist/runtime/utils/node/block.js +0 -256
  86. package/dist/runtime/utils/style/anchor.d.ts +0 -5
  87. package/dist/runtime/utils/style/anchor.js +0 -245
  88. package/dist/runtime/utils/style/decoration.d.ts +0 -4
  89. package/dist/runtime/utils/style/decoration.js +0 -378
  90. package/dist/runtime/utils/style/index.d.ts +0 -2
  91. package/dist/runtime/utils/style/index.js +0 -2
@@ -1,1586 +1,1090 @@
1
- import { _updateModelData, _updateCursorData, _setCursor, _sortingCursorDataOnElement, _generateId } from "./index.js";
2
- import { _addBlock, _getCurrentBlock, _createTextBlock, _createHeadingBlock, _createListBlock, _getParentElementIfNodeIsText, _findContentEditableElement, _createListItemBlock, _updateCurrentBlock, _createCodeBlock } from "../node/index.js";
3
- import { _getDefaultBlockData } from "../event/index.js";
4
- import { _setDecoration } from "../style/index.js";
5
- export function _contentKeydownEvent(event, store) {
6
- _updateCurrentBlock(event, store);
7
- _updateCursorData(store);
8
- switch (event.key) {
9
- case "Enter":
10
- if (event.isComposing === false) {
11
- __enterEvent(event, store);
12
- } else {
13
- event.preventDefault();
14
- }
15
- break;
16
- case "Backspace":
17
- __backspaceEvent(event, store);
18
- break;
19
- case "Delete":
20
- __deleteEvent(event, store);
21
- break;
22
- case "Tab":
23
- __tabEvent(event, store);
24
- break;
25
- case " ":
26
- __spaceEvent(event, store);
27
- break;
28
- case "ArrowUp":
29
- _moveToBlockEvent(event, store, "up");
30
- break;
31
- case "ArrowDown":
32
- _moveToBlockEvent(event, store, "down");
33
- break;
34
- case "`":
35
- __backtickEvent(event, store);
36
- break;
37
- }
38
- _hotKeyEvent(event, store);
39
- }
40
- export async function _contentPasteEvent(event, store) {
1
+ import { nextTick } from "#imports";
2
+ import { useEditorStore } from "../../store/editor.js";
3
+ import { _updateCursorData, _setCursorPosition } from "./index.js";
4
+ import { _createTextBlockData, _createHeadingBlockData, _getMultilinePosition, _getBlockType, _getBeforeAndAfterHTMLOfCursor, _createListBlockData, _createListBlockChildData, _isCursorAtLineBoundary, _getEditorbleEndPosition, _getEditorbleCursorPosition, _createDividerBlockData, _convertMarkdownToEditor, _generateId, _createCodeBlockData } from "../data/index.js";
5
+ import { _findEditableElement, _findParentBlock, _setDecoration } from "../node/index.js";
6
+ export async function _sliceAndNewTextBlock(event, data, index) {
7
+ const editorStore = useEditorStore();
41
8
  event.preventDefault();
42
- const text = await navigator.clipboard.readText();
43
- const $block = event.target.closest(".de-block");
44
- if ($block !== null) {
45
- if (text === "") {
46
- const clipboardItems = await navigator.clipboard.read();
47
- const imageItem = clipboardItems[0].types.find((type) => type.startsWith("image/"));
48
- if (imageItem !== void 0) {
49
- const blob = await clipboardItems[0].getType(imageItem);
50
- const file = new File([blob], `${_generateId()}.${imageItem.split("/")[1]}`);
51
- store.value.emit("uploadImageEvent", file);
52
- }
9
+ _updateCursorData();
10
+ if (editorStore.cursorSelection !== null && editorStore.fn.updateEditorData !== null) {
11
+ const newData = JSON.parse(JSON.stringify(editorStore.data));
12
+ const $target = event.currentTarget;
13
+ const { beforeHTML, afterHTML } = _getBeforeAndAfterHTMLOfCursor($target);
14
+ if (beforeHTML === "" && afterHTML !== "") {
15
+ newData.splice(index, 0, _createTextBlockData());
16
+ editorStore.fn.updateEditorData(newData);
17
+ } else if (beforeHTML !== "" && afterHTML === "" || beforeHTML === "" && afterHTML === "") {
18
+ data.textContent = beforeHTML;
19
+ newData.splice(index + 1, 0, _createTextBlockData());
20
+ newData[index] = data;
21
+ editorStore.fn.updateEditorData(newData);
22
+ await nextTick();
23
+ $target.dispatchEvent(new Event("input"));
24
+ await nextTick();
25
+ $target.nextElementSibling.focus();
53
26
  } else {
54
- if (store.value.controlStatus.currentBlockType === "code") {
55
- const selection = window.getSelection();
56
- const textNode = document.createTextNode(text);
57
- selection.deleteFromDocument();
58
- selection.getRangeAt(0).insertNode(textNode);
59
- _setCursor(textNode, textNode.length);
27
+ if (data.type === "text") {
28
+ newData.splice(index + 1, 0, _createTextBlockData(afterHTML));
60
29
  } else {
61
- __pasteToMarkDownFormat(text, store);
30
+ newData.splice(index, 0, _createHeadingBlockData(data.level, beforeHTML));
31
+ newData.splice(index + 1, 1, _createTextBlockData(afterHTML));
62
32
  }
33
+ data.textContent = beforeHTML;
34
+ newData[index] = data;
35
+ editorStore.fn.updateEditorData(newData);
36
+ await nextTick();
37
+ if (editorStore.element.body !== null) {
38
+ const $block = editorStore.element.body.children[index + 1];
39
+ $block.focus();
40
+ $block.dispatchEvent(new Event("input"));
41
+ }
42
+ editorStore.selectedBlockIndex += 1;
43
+ _updateCursorData();
63
44
  }
64
45
  }
65
46
  }
66
- function __pasteToMarkDownFormat(value, store) {
67
- if (store.value.controlStatus.$currentBlock !== null) {
68
- const lineList = value.split("\n");
69
- const blockList = [];
70
- const unorderListReg = new RegExp("^( +)?(\\+|\\*|-)(?= )( )");
71
- const orderListReg = new RegExp("^( +)?(\\d+.)(?= )( )");
72
- const codeBlockReg = new RegExp("^```");
73
- let tempData = null;
74
- let isCodeBlock = false;
75
- if (lineList.length === 1) {
76
- const selection = window.getSelection();
77
- const textNode = document.createTextNode(lineList[0]);
78
- selection.deleteFromDocument();
79
- selection.getRangeAt(0).insertNode(textNode);
80
- _setCursor(textNode, textNode.length);
81
- } else {
82
- let isDelete = false;
83
- let $target = null;
84
- lineList.forEach((text, lineIndex) => {
85
- switch (true) {
86
- case new RegExp("^(---|___|\\*\\*\\*)").test(text):
87
- blockList.push({
88
- type: "divider"
89
- });
90
- break;
91
- case (codeBlockReg.test(text) || isCodeBlock === true):
92
- if (isCodeBlock === false) {
93
- const startLineText = text.split("```");
94
- let codeBlockLang = "text";
95
- if (["text", "csharp", "c", "cpp", "css", "django", "dockerfile", "go", "html", "json", "java", "javascript", "typescript", "kotlin", "lua", "markdown", "nginx", "php", "python", "ruby", "scss", "sql", "shellscript", "swift", "yaml"].includes(startLineText[1]) === true) {
96
- codeBlockLang = startLineText[1];
97
- }
98
- isCodeBlock = true;
99
- tempData = {
100
- type: "code",
101
- filename: "",
102
- theme: "github-light",
103
- language: codeBlockLang,
104
- textContent: ""
105
- };
106
- } else {
107
- if (tempData !== null) {
108
- if (codeBlockReg.test(text) !== true) {
109
- if (tempData.type === "code") {
110
- tempData.textContent += `${text}
111
- `;
112
- }
113
- } else {
114
- blockList.push(tempData);
115
- isCodeBlock = false;
116
- tempData = null;
117
- }
47
+ export async function _imageEnterEvent(event, data, index) {
48
+ const editorStore = useEditorStore();
49
+ event.preventDefault();
50
+ _updateCursorData();
51
+ if (editorStore.element.body !== null && editorStore.cursorSelection !== null && editorStore.fn.updateEditorData !== null) {
52
+ const newData = JSON.parse(JSON.stringify(editorStore.data));
53
+ const $target = event.currentTarget;
54
+ const { beforeHTML, afterHTML } = _getBeforeAndAfterHTMLOfCursor($target);
55
+ data.caption = beforeHTML;
56
+ newData.splice(index + 1, 0, _createTextBlockData(afterHTML));
57
+ newData[index] = data;
58
+ editorStore.selectedBlockIndex += 1;
59
+ editorStore.fn.updateEditorData(newData);
60
+ await nextTick();
61
+ $target.dispatchEvent(new Event("input"));
62
+ editorStore.element.body.children[index + 1].focus();
63
+ _updateCursorData();
64
+ }
65
+ }
66
+ export async function _listBlockEnterEvent(event, data, index, childIndex, setEvent, abortEvent) {
67
+ const editorStore = useEditorStore();
68
+ event.preventDefault();
69
+ _updateCursorData();
70
+ if (editorStore.cursorSelection !== null && editorStore.fn.updateEditorData !== null && editorStore.element.body !== null) {
71
+ const newData = JSON.parse(JSON.stringify(editorStore.data));
72
+ const targetChildrenCount = data.child.length;
73
+ const targetChild = data.child[childIndex];
74
+ const $target = event.currentTarget;
75
+ if (targetChild !== void 0) {
76
+ const { beforeHTML, afterHTML } = _getBeforeAndAfterHTMLOfCursor($target);
77
+ abortEvent();
78
+ if (targetChildrenCount === 1) {
79
+ if (beforeHTML === "" && afterHTML === "") {
80
+ newData.splice(index, 1, _createTextBlockData());
81
+ editorStore.fn.updateEditorData(newData);
82
+ await nextTick();
83
+ editorStore.element.body.children[index].focus();
84
+ } else {
85
+ data.child[childIndex].textContent = beforeHTML;
86
+ data.child.push(_createListBlockChildData(afterHTML, targetChild.depth));
87
+ newData[index] = data;
88
+ editorStore.fn.updateEditorData(newData);
89
+ await nextTick();
90
+ const $parentBlock = editorStore.element.body.children[index];
91
+ if ($parentBlock !== void 0) {
92
+ const $childElement = $parentBlock.children[childIndex + 1];
93
+ if ($childElement !== void 0) {
94
+ const $textArea = $childElement.querySelector(".de-item-text");
95
+ if ($textArea !== null) {
96
+ $textArea.focus();
118
97
  }
119
98
  }
120
- break;
121
- case orderListReg.test(text):
122
- const olSplitText = text.split(new RegExp("\\d+.(?= )"));
123
- const olDepth = Math.floor(olSplitText[0].length / 4);
124
- if (tempData === null) {
125
- tempData = {
126
- type: "list",
127
- style: ["decimal", "lower-alpha", "upper-alpha", "lower-roman", "upper-roman"][olDepth],
128
- depth: olDepth,
129
- element: "ol",
130
- child: []
131
- };
132
- tempData.child.push({
133
- classList: [],
134
- textContent: ___replaceTextData(olSplitText[1].trim())
135
- });
99
+ }
100
+ }
101
+ } else if (targetChildrenCount - 1 === childIndex) {
102
+ if (beforeHTML === "" && afterHTML === "") {
103
+ if (targetChild.depth === void 0) {
104
+ data.child.splice(childIndex, 1);
105
+ newData[index] = data;
106
+ newData.splice(index + 1, 0, _createTextBlockData());
107
+ editorStore.fn.updateEditorData(newData);
108
+ await nextTick();
109
+ editorStore.element.body.children[index + 1].focus();
110
+ } else {
111
+ if (targetChild.depth === 1) {
112
+ delete targetChild.depth;
136
113
  } else {
137
- if (tempData.type === "list") {
138
- const nextLine = lineList[lineIndex + 1];
139
- tempData.child.push({
140
- classList: [],
141
- textContent: ___replaceTextData(olSplitText[1].trim())
142
- });
143
- if (nextLine !== void 0) {
144
- const nextOlSplitText = nextLine.split(new RegExp("\\d+.(?= )"));
145
- const nextOlDepth = Math.floor(nextOlSplitText[0].length / 4);
146
- if (orderListReg.test(nextLine) === false) {
147
- blockList.push(tempData);
148
- tempData = null;
149
- } else {
150
- if (tempData.depth !== nextOlDepth) {
151
- blockList.push(tempData);
152
- tempData = null;
153
- }
154
- }
155
- } else {
156
- blockList.push(tempData);
157
- tempData = null;
114
+ targetChild.depth -= 1;
115
+ }
116
+ data.child[childIndex] = targetChild;
117
+ newData[index] = data;
118
+ editorStore.fn.updateEditorData(newData);
119
+ await nextTick();
120
+ const $parentBlock = editorStore.element.body.children[index];
121
+ if ($parentBlock !== void 0) {
122
+ const $childElement = $parentBlock.querySelectorAll("li")[childIndex];
123
+ if ($childElement !== void 0) {
124
+ const $textArea = $childElement.querySelector(".de-item-text");
125
+ if ($textArea !== null) {
126
+ $textArea.focus();
158
127
  }
159
128
  }
160
129
  }
161
- break;
162
- case unorderListReg.test(text):
163
- const ulSplitText = text.split(new RegExp("\\+|\\*|-"));
164
- const ulDepth = Math.floor(ulSplitText[0].length / 4);
165
- if (tempData === null) {
166
- tempData = {
167
- type: "list",
168
- style: ulDepth % 2 === 0 ? "disc" : "square",
169
- depth: ulDepth,
170
- element: "ul",
171
- child: []
172
- };
173
- tempData.child.push({
174
- classList: [],
175
- textContent: ___replaceTextData(ulSplitText[1].trim())
176
- });
177
- } else {
178
- if (tempData.type === "list") {
179
- const nextLine = lineList[lineIndex + 1];
180
- tempData.child.push({
181
- classList: [],
182
- textContent: ___replaceTextData(ulSplitText[1].trim())
183
- });
184
- if (nextLine !== void 0) {
185
- const nextUlSplitText = nextLine.split(new RegExp("\\+|\\*|-"));
186
- const nextUlDepth = Math.floor(nextUlSplitText[0].length / 4);
187
- if (unorderListReg.test(nextLine) === false) {
188
- blockList.push(tempData);
189
- tempData = null;
190
- } else {
191
- if (tempData.depth !== nextUlDepth) {
192
- blockList.push(tempData);
193
- tempData = null;
194
- }
195
- }
196
- } else {
197
- blockList.push(tempData);
198
- tempData = null;
199
- }
130
+ }
131
+ } else {
132
+ data.child[childIndex].textContent = beforeHTML;
133
+ data.child.push(_createListBlockChildData(afterHTML, targetChild.depth));
134
+ newData[index] = data;
135
+ editorStore.fn.updateEditorData(newData);
136
+ await nextTick();
137
+ const $parentBlock = editorStore.element.body.children[index];
138
+ if ($parentBlock !== void 0) {
139
+ const $childElement = $parentBlock.querySelectorAll("li")[childIndex + 1];
140
+ if ($childElement !== void 0) {
141
+ const $textArea = $childElement.querySelector(".de-item-text");
142
+ if ($textArea !== null) {
143
+ $textArea.focus();
200
144
  }
201
145
  }
202
- break;
203
- case new RegExp("^###(?= )").test(text):
204
- blockList.push({
205
- type: "heading",
206
- level: 3,
207
- id: _generateId(),
208
- classList: [],
209
- textContent: ___replaceTextData(text.substring(4))
210
- });
211
- break;
212
- case new RegExp("^##(?= )").test(text):
213
- blockList.push({
214
- type: "heading",
215
- level: 2,
216
- id: _generateId(),
217
- classList: [],
218
- textContent: ___replaceTextData(text.substring(3))
219
- });
220
- break;
221
- case new RegExp("^#(?= )").test(text):
222
- blockList.push({
223
- type: "heading",
224
- level: 1,
225
- id: _generateId(),
226
- classList: [],
227
- textContent: ___replaceTextData(text.substring(2))
228
- });
229
- break;
230
- default:
231
- blockList.push({
232
- type: "text",
233
- classList: [],
234
- textContent: ___replaceTextData(text)
235
- });
236
- }
237
- });
238
- if (store.value.controlStatus.currentBlockType === "heading" || store.value.controlStatus.currentBlockType === "text") {
239
- if (store.value.controlStatus.$currentBlock !== null) {
240
- if (store.value.controlStatus.$currentBlock.textContent === "") {
241
- isDelete = true;
242
- $target = store.value.controlStatus.$currentBlock;
243
146
  }
244
147
  }
245
- }
246
- blockList.forEach((data) => {
247
- switch (data.type) {
248
- case "heading":
249
- if (data.level === 1) {
250
- _addBlock("heading1", store, data);
148
+ } else {
149
+ if (beforeHTML === "" && afterHTML === "") {
150
+ if (targetChild.depth === void 0) {
151
+ const beforeChildList = data.child.slice(0, childIndex);
152
+ const afterChildList = data.child.slice(childIndex + 1);
153
+ newData.splice(index, 1, _createListBlockData(data.element, beforeChildList));
154
+ newData.splice(index + 1, 0, _createTextBlockData());
155
+ newData.splice(index + 2, 0, _createListBlockData(data.element, afterChildList));
156
+ editorStore.fn.updateEditorData(newData);
157
+ await nextTick();
158
+ const $block = editorStore.element.body.children[index + 1];
159
+ if ($block !== void 0) {
160
+ $block.focus();
161
+ }
162
+ } else {
163
+ if (targetChild.depth === 1) {
164
+ delete targetChild.depth;
165
+ } else {
166
+ targetChild.depth -= 1;
251
167
  }
252
- if (data.level === 2) {
253
- _addBlock("heading2", store, data);
168
+ data.child[childIndex] = targetChild;
169
+ newData[index] = data;
170
+ editorStore.fn.updateEditorData(newData);
171
+ await nextTick();
172
+ const $parentBlock = editorStore.element.body.children[index];
173
+ if ($parentBlock !== void 0) {
174
+ const $childElement = $parentBlock.querySelectorAll("li")[childIndex];
175
+ if ($childElement !== void 0) {
176
+ const $textArea = $childElement.querySelector(".de-item-text");
177
+ if ($textArea !== null) {
178
+ $textArea.focus();
179
+ }
180
+ }
254
181
  }
255
- if (data.level === 3) {
256
- _addBlock("heading3", store, data);
182
+ }
183
+ } else {
184
+ targetChild.textContent = beforeHTML;
185
+ data.child[childIndex] = targetChild;
186
+ data.child.splice(childIndex + 1, 0, _createListBlockChildData(afterHTML, targetChild.depth || 0));
187
+ newData[index] = data;
188
+ editorStore.fn.updateEditorData(newData);
189
+ await nextTick();
190
+ const $parentBlock = editorStore.element.body.children[index];
191
+ if ($parentBlock !== void 0) {
192
+ const $targetChildElement = $parentBlock.querySelectorAll(".de-item-text")[childIndex];
193
+ const $childElement = $parentBlock.querySelectorAll(".de-item-text")[childIndex + 1];
194
+ if ($childElement !== void 0) {
195
+ $childElement.focus();
257
196
  }
258
- break;
259
- case "list":
260
- _addBlock(data.element, store, data);
261
- break;
262
- default:
263
- _addBlock(data.type, store, data);
197
+ }
264
198
  }
265
- });
266
- if (isDelete === true) {
267
- $target?.remove();
268
199
  }
269
200
  }
270
201
  }
271
202
  }
272
- function ___replaceTextData(text) {
273
- return text.replaceAll("<", "&lt;").replaceAll(">", "&gt;").replaceAll(new RegExp("(`)([^`]+)(`)", "g"), `<span class="de-code">$2</span>`).replaceAll(new RegExp("(\\*\\*)([^\\*]+)(?=\\*\\*)(\\*\\*)", "g"), `<span class="de-bold">$2</span>`).replaceAll(new RegExp("(\\_\\_)([^\\_]+)(?=\\_\\_)(\\_\\_)", "g"), `<span class="de-bold">$2</span>`).replaceAll(new RegExp("(\\~\\~)([^\\~]+)(?=\\~\\~)(\\~\\~)", "g"), `<span class="de-strikethrough">$2</span>`).replaceAll(new RegExp("(\\*)([^\\*]+)(?=\\*)(\\*)", "g"), `<span class="de-italic">$2</span>`).replaceAll(new RegExp("(\\_)([^\\_]+)(?=\\_)(\\_)", "g"), `<span class="de-italic">$2</span>`).replaceAll(new RegExp("(\\[)([^\\[\\]]+)(\\])(?=\\()(\\()([^\\(\\)]+)(?=\\))(\\))", "g"), `<a class="de-link" href="$5" target="_blank">$2</a>`);
203
+ export async function _codeBlockShiftEnterEvent(event, index) {
204
+ const editorStore = useEditorStore();
205
+ event.preventDefault();
206
+ _updateCursorData();
207
+ if (editorStore.element.body !== null && editorStore.cursorSelection !== null && editorStore.fn.updateEditorData !== null) {
208
+ const newData = JSON.parse(JSON.stringify(editorStore.data));
209
+ const newTextBlockData = _createTextBlockData();
210
+ newData.splice(index + 1, 0, newTextBlockData);
211
+ editorStore.fn.updateEditorData(newData);
212
+ editorStore.selectedBlockIndex += 1;
213
+ editorStore.selectedBlockId = newTextBlockData.id;
214
+ await nextTick();
215
+ editorStore.element.body.children[index + 1].focus();
216
+ _updateCursorData();
217
+ }
274
218
  }
275
- function __enterEvent(event, store) {
276
- if (event.shiftKey === true) {
277
- switch (store.value.controlStatus.currentBlockType) {
278
- case "image":
279
- event.preventDefault();
280
- break;
281
- case "code":
282
- __codeBlockShiftEnterEvent(event, store);
283
- break;
284
- case "ol":
285
- case "ul":
286
- __listBlockShiftEnterEvent(event, store);
287
- break;
288
- default:
289
- __defaultBlockShiftEnterEvent(event, store);
290
- }
291
- } else {
292
- switch (store.value.controlStatus.currentBlockType) {
293
- case "image":
294
- event.preventDefault();
295
- if (store.value.controlStatus.$currentBlock !== null) {
296
- const $block = store.value.controlStatus.$currentBlock;
297
- const $newTextBlock = _createTextBlock(_getDefaultBlockData("text"));
298
- $block.insertAdjacentElement("afterend", $newTextBlock);
299
- $newTextBlock.focus();
219
+ export async function _blockTabEvent(event, data, index, setEvent, abortEvent) {
220
+ const editorStore = useEditorStore();
221
+ const newData = JSON.parse(JSON.stringify(editorStore.data));
222
+ const $target = event.currentTarget;
223
+ event.preventDefault();
224
+ _updateCursorData();
225
+ if (editorStore.cursorSelection !== null && editorStore.fn.updateEditorData !== null && $target !== null) {
226
+ const offset = _getEditorbleCursorPosition($target);
227
+ if (event.shiftKey === false) {
228
+ if (data.depth === void 0) {
229
+ data.depth = 1;
230
+ } else {
231
+ data.depth += 1;
232
+ }
233
+ if (data.depth > 5) {
234
+ data.depth = 5;
235
+ }
236
+ } else {
237
+ if (data.depth !== void 0) {
238
+ data.depth -= 1;
239
+ if (data.depth <= 0) {
240
+ delete data.depth;
300
241
  }
301
- break;
302
- case "code":
303
- break;
304
- case "ol":
305
- case "ul":
306
- __listBlockEnterEvent(event, store);
307
- break;
308
- default:
309
- __defaultBlockEnterEvent(event, store);
242
+ }
243
+ }
244
+ abortEvent();
245
+ newData.splice(index, 1, data);
246
+ editorStore.fn.updateEditorData(newData);
247
+ await nextTick();
248
+ setEvent();
249
+ const $node = $target.childNodes[offset.nodeIndex];
250
+ if ($node !== void 0) {
251
+ _setCursorPosition($node, offset.offset);
310
252
  }
311
253
  }
312
254
  }
313
- function __defaultBlockEnterEvent(event, store) {
255
+ export async function _listChildTabEvent(event, data, index, childIndex, setEvent, abortEvent) {
256
+ const editorStore = useEditorStore();
257
+ const newData = JSON.parse(JSON.stringify(editorStore.data));
258
+ const targetChild = data.child[childIndex];
259
+ const $target = event.currentTarget;
260
+ let type = "plus";
314
261
  event.preventDefault();
315
- if (store.value.cursorData !== null && store.value.controlStatus.$currentBlock !== null) {
316
- const cursorData = store.value.cursorData;
317
- const $block = store.value.controlStatus.$currentBlock;
318
- if (store.value.cursorData.type === "Caret") {
319
- if ($block.textContent === "") {
320
- if ($block.hasChildNodes() === false) {
321
- const $newTextBlock = _createTextBlock(_getDefaultBlockData("text"));
322
- $block.insertAdjacentElement("afterend", $newTextBlock);
323
- $newTextBlock.focus();
324
- } else {
325
- const brList = $block.querySelectorAll("br");
326
- if (brList.length === 1) {
327
- const $newTextBlock = _createTextBlock(_getDefaultBlockData("text"));
328
- $block.insertAdjacentElement("afterend", $newTextBlock);
329
- $newTextBlock.focus();
330
- } else {
331
- const $nextTextBlock = _createTextBlock(_getDefaultBlockData("text"));
332
- let preStructure = [];
333
- let nextStructure = [];
334
- brList.forEach((_, i) => {
335
- const $br = document.createElement("br");
336
- if (cursorData.startOffset < i) {
337
- preStructure.push($br);
338
- } else {
339
- nextStructure.push($br);
340
- }
341
- });
342
- $block.replaceChildren(...preStructure);
343
- $block.insertAdjacentElement("afterend", $nextTextBlock);
344
- if (nextStructure.length === 0) {
345
- $nextTextBlock.focus();
346
- } else {
347
- if (nextStructure.length === 1) {
348
- nextStructure.push(document.createElement("br"));
349
- }
350
- $nextTextBlock.replaceChildren(...nextStructure);
351
- _setCursor(nextStructure[0], 0);
352
- }
353
- }
354
- }
262
+ _updateCursorData();
263
+ if (data.child.length > 1 && editorStore.cursorSelection !== null && editorStore.fn.updateEditorData !== null && editorStore.element.body !== null && targetChild !== void 0) {
264
+ const offset = _getEditorbleCursorPosition($target);
265
+ if (event.shiftKey === false) {
266
+ const preChildData = data.child[childIndex - 1];
267
+ if (targetChild.depth === void 0) {
268
+ targetChild.depth = 1;
355
269
  } else {
356
- const childNodeList = Array.from($block.childNodes);
357
- const preStructure = [];
358
- const nextStructure = [];
359
- const $targetNode = _getParentElementIfNodeIsText(store.value.cursorData.startNode, $block);
360
- let nodeIdx = -1;
361
- for (let i = 0; childNodeList.length > i; i += 1) {
362
- if (childNodeList[i] === $targetNode) {
363
- nodeIdx = i;
364
- break;
365
- }
366
- }
367
- childNodeList.forEach((node, i) => {
368
- if (nodeIdx < i) {
369
- nextStructure.push(node);
370
- } else if (nodeIdx > i) {
371
- preStructure.push(node);
372
- } else if (nodeIdx === i) {
373
- if (node.constructor.name === "Text") {
374
- const text = node.textContent;
375
- const preText = document.createTextNode(text.slice(0, cursorData.startOffset));
376
- const nextText = document.createTextNode(text.slice(cursorData.startOffset));
377
- preStructure.push(preText);
378
- nextStructure.push(nextText);
379
- } else {
380
- const originalClassList = Array.from(node.classList);
381
- const text = node.textContent;
382
- const preSpan = document.createElement("span");
383
- const nextSpan = document.createElement("span");
384
- const nextText = text.slice(cursorData.startOffset);
385
- preSpan.classList.add(...originalClassList);
386
- preSpan.textContent = text.slice(0, cursorData.startOffset);
387
- preStructure.push(preSpan);
388
- if (nextText !== "") {
389
- nextSpan.classList.add(...originalClassList);
390
- nextSpan.textContent = nextText;
391
- nextStructure.push(nextSpan);
392
- }
393
- }
394
- }
395
- });
396
- const $nextTextBlock = _createTextBlock(_getDefaultBlockData("text"));
397
- $block.insertAdjacentElement("afterend", $nextTextBlock);
398
- $block.replaceChildren(...preStructure);
399
- $nextTextBlock.replaceChildren(...nextStructure);
400
- if (nextStructure.length === 0) {
401
- $nextTextBlock.focus();
402
- } else {
403
- _setCursor($nextTextBlock.childNodes[0], 0);
404
- }
270
+ targetChild.depth += 1;
271
+ }
272
+ if (targetChild.depth > 5) {
273
+ targetChild.depth = 5;
274
+ }
275
+ if (preChildData !== void 0 && targetChild.depth > (preChildData.depth || 0)) {
276
+ targetChild.depth = (preChildData.depth || 0) + 1;
405
277
  }
406
278
  } else {
407
- const childNodeList = $block.childNodes;
408
- const srotingCursorData = _sortingCursorDataOnElement(cursorData, $block);
409
- const preStructure = [];
410
- const nextStructure = [];
411
- if (srotingCursorData.startNodeIdx === srotingCursorData.endNodeIdx) {
412
- childNodeList.forEach((node, i) => {
413
- if (srotingCursorData.startNodeIdx > i) {
414
- preStructure.push(node);
415
- } else if (srotingCursorData.endNodeIdx < i) {
416
- nextStructure.push(node);
417
- } else if (srotingCursorData.startNodeIdx === i) {
418
- if (node.constructor.name === "Text") {
419
- const preText = node.textContent.slice(0, srotingCursorData.startOffset);
420
- const nextText = node.textContent.slice(srotingCursorData.endOffset);
421
- if (preText !== "") {
422
- const textNode = document.createTextNode(preText);
423
- preStructure.push(textNode);
424
- }
425
- if (nextText !== "") {
426
- const textNode = document.createTextNode(nextText);
427
- nextStructure.push(textNode);
428
- }
279
+ type = "minus";
280
+ if (targetChild.depth !== void 0) {
281
+ targetChild.depth -= 1;
282
+ if (targetChild.depth <= 0) {
283
+ delete targetChild.depth;
284
+ }
285
+ }
286
+ }
287
+ data.child[childIndex] = targetChild;
288
+ for (let i = 0; i < data.child.length; i += 1) {
289
+ const child = data.child[i];
290
+ if (i > childIndex && child !== void 0) {
291
+ if ((child.depth || 0) <= (targetChild.depth || 0) - 1 || (targetChild.depth || 0) === 0) {
292
+ break;
293
+ } else {
294
+ if (type === "plus") {
295
+ if (child.depth === void 0) {
296
+ child.depth = 1;
429
297
  } else {
430
- const originalClassList = Array.from(node.classList);
431
- const preText = node.textContent.slice(0, srotingCursorData.startOffset);
432
- const nextText = node.textContent.slice(srotingCursorData.endOffset);
433
- if (preText !== "") {
434
- const $span = document.createElement("span");
435
- $span.classList.add(...originalClassList);
436
- $span.textContent = preText;
437
- preStructure.push($span);
438
- }
439
- if (nextText !== "") {
440
- const $span = document.createElement("span");
441
- $span.classList.add(...originalClassList);
442
- $span.textContent = nextText;
443
- nextStructure.push($span);
444
- }
298
+ child.depth += 1;
445
299
  }
446
- }
447
- });
448
- } else {
449
- childNodeList.forEach((node, i) => {
450
- if (srotingCursorData.startNodeIdx > i) {
451
- preStructure.push(node);
452
- } else if (srotingCursorData.startNodeIdx === i) {
453
- if (node.constructor.name === "Text") {
454
- const text = node.textContent.slice(0, srotingCursorData.startOffset);
455
- if (text !== "") {
456
- const textNode = document.createTextNode(text);
457
- preStructure.push(textNode);
458
- }
459
- } else {
460
- const originalClassList = Array.from(node.classList);
461
- const text = node.textContent.slice(0, srotingCursorData.startOffset);
462
- if (text !== "") {
463
- const $span = document.createElement("span");
464
- $span.classList.add(...originalClassList);
465
- $span.textContent = text;
466
- preStructure.push($span);
467
- }
300
+ if (child.depth > 5) {
301
+ child.depth = 5;
468
302
  }
469
- }
470
- if (srotingCursorData.endNodeIdx < i) {
471
- nextStructure.push(node);
472
- } else if (srotingCursorData.endNodeIdx === i) {
473
- if (node.constructor.name === "Text") {
474
- const text = node.textContent.slice(srotingCursorData.endOffset);
475
- if (text !== "") {
476
- const textNode = document.createTextNode(text);
477
- nextStructure.push(textNode);
478
- }
479
- } else {
480
- const originalClassList = Array.from(node.classList);
481
- const text = node.textContent.slice(srotingCursorData.endOffset);
482
- const $span = document.createElement("span");
483
- if (text !== "") {
484
- $span.classList.add(...originalClassList);
485
- $span.textContent = text;
486
- nextStructure.push($span);
303
+ } else {
304
+ if (child.depth !== void 0) {
305
+ child.depth -= 1;
306
+ if (child.depth <= 0) {
307
+ delete child.depth;
487
308
  }
488
309
  }
489
310
  }
490
- });
491
- const $nextBlock = _createTextBlock(_getDefaultBlockData("text"));
492
- $block.insertAdjacentElement("afterend", $nextBlock);
493
- $block.replaceChildren(...preStructure);
494
- $nextBlock.replaceChildren(...nextStructure);
495
- if (nextStructure.length === 0) {
496
- $nextBlock.focus();
311
+ }
312
+ }
313
+ }
314
+ newData[index] = data;
315
+ abortEvent();
316
+ editorStore.fn.updateEditorData(newData);
317
+ await nextTick();
318
+ setEvent(childIndex, targetChild.id);
319
+ const $parentBlock = editorStore.element.body.children[index];
320
+ if ($parentBlock !== void 0) {
321
+ const $textArea = $parentBlock.querySelectorAll(".de-item-text");
322
+ const $targetTextArea = $textArea[childIndex];
323
+ if ($targetTextArea !== void 0) {
324
+ const $node = $targetTextArea.childNodes[offset.nodeIndex];
325
+ if ($node !== void 0) {
326
+ _setCursorPosition($node, offset.offset);
497
327
  } else {
498
- _setCursor($nextBlock.childNodes[0], 0);
328
+ _setCursorPosition($targetTextArea, 0);
499
329
  }
500
330
  }
501
331
  }
502
- } else {
503
- console.error("[Dragon Editor] : Something wrong.");
504
332
  }
505
333
  }
506
- function __defaultBlockShiftEnterEvent(event, store) {
334
+ export async function _codeBlockTabEvent(event) {
335
+ const editorStore = useEditorStore();
336
+ const $target = event.currentTarget;
507
337
  event.preventDefault();
508
- if (store.value.cursorData !== null && store.value.controlStatus.$currentBlock !== null) {
509
- const cursorData = store.value.cursorData;
510
- const $block = store.value.controlStatus.$currentBlock;
511
- if (cursorData.type === "Caret") {
512
- if ($block.textContent === "") {
513
- if ($block.hasChildNodes() === false) {
514
- $block.insertAdjacentHTML("beforeend", "<br><br>");
515
- _setCursor($block.childNodes[1], 0);
516
- } else {
517
- const $br = document.createElement("br");
518
- $block.insertAdjacentElement("beforeend", $br);
519
- _setCursor($br, 0);
520
- }
521
- } else {
522
- const childList = $block.childNodes;
523
- let targetIdx = -1;
524
- let structure = "";
525
- let $target = cursorData.startNode;
526
- if ($target.constructor.name === "Text") {
527
- if ($target.parentNode !== $block) {
528
- $target = $target.parentNode;
529
- }
338
+ _updateCursorData();
339
+ if (editorStore.cursorSelection !== null && editorStore.cursorSelection.rangeCount > 0 && $target !== null && $target.contains(editorStore.cursorSelection.getRangeAt(0).startContainer)) {
340
+ let traverse = function(node) {
341
+ if (node.nodeType === Node.TEXT_NODE) {
342
+ const len = node.textContent?.length || 0;
343
+ if (startNode === null && selectionOffset + len >= newStartOffset) {
344
+ startNode = node;
345
+ startNodeOffset = newStartOffset - selectionOffset;
530
346
  }
531
- if ($block === $target) {
532
- $target = $block.childNodes[cursorData.startOffset];
347
+ if (endNode === null && selectionOffset + len >= newEndOffset) {
348
+ endNode = node;
349
+ endNodeOffset = newEndOffset - selectionOffset;
533
350
  }
534
- for (let i = 0; childList.length > i; i += 1) {
535
- if (childList[i] === $target) {
536
- targetIdx = i;
351
+ selectionOffset += len;
352
+ } else {
353
+ for (let i = 0; i < node.childNodes.length; i++) {
354
+ traverse(node.childNodes[i]);
355
+ if (startNode !== null && endNode !== null) {
537
356
  break;
538
357
  }
539
358
  }
540
- let currentIdx = targetIdx;
541
- childList.forEach((child, i) => {
542
- if (i === targetIdx) {
543
- const constructorName = child.constructor.name;
544
- if (constructorName === "Text") {
545
- structure += child.textContent.slice(0, cursorData.startOffset) + "<br>" + child.textContent.slice(cursorData.endOffset);
546
- if (child.nextSibling === null) {
547
- if (child.textContent.slice(cursorData.endOffset) === "") {
548
- structure += `<br>`;
549
- }
550
- if (child.textContent.slice(0, cursorData.startOffset) === "") {
551
- currentIdx -= 1;
552
- }
553
- } else {
554
- if (child.textContent.slice(cursorData.endOffset) !== "" && child.textContent.slice(0, cursorData.startOffset) === "") {
555
- currentIdx -= 1;
556
- }
557
- }
558
- currentIdx += 1;
559
- } else {
560
- if (constructorName === "HTMLBRElement") {
561
- structure += `<br><br>`;
562
- } else {
563
- structure += `<span class="${Array.from(child.classList).join(" ")}">${child.textContent.slice(0, cursorData.startOffset)}</span>`;
564
- structure += `<br>`;
565
- structure += `<span class="${Array.from(child.classList).join(" ")}">${child.textContent.slice(cursorData.startOffset)}</span>`;
566
- currentIdx += 1;
567
- }
568
- }
569
- } else {
570
- if (child.constructor.name === "Text") {
571
- structure += child.textContent;
572
- } else {
573
- structure += child.outerHTML;
574
- }
575
- }
576
- });
577
- $block.innerHTML = structure;
578
- if ($block.childNodes[currentIdx + 1] === void 0) {
579
- $block.insertAdjacentHTML("beforeend", "<br>");
359
+ }
360
+ };
361
+ const range = editorStore.cursorSelection.getRangeAt(0);
362
+ const startRange = range.cloneRange();
363
+ startRange.selectNodeContents($target);
364
+ startRange.setEnd(range.startContainer, range.startOffset);
365
+ const startOffsetChar = startRange.toString().length;
366
+ const endRange = range.cloneRange();
367
+ endRange.selectNodeContents($target);
368
+ endRange.setEnd(range.endContainer, range.endOffset);
369
+ const endOffsetChar = endRange.toString().length;
370
+ const fullText = $target.innerText || $target.textContent || "";
371
+ const lines = fullText.split("\n");
372
+ const linesInfo = [];
373
+ let currentOffset = 0;
374
+ lines.forEach((lineText) => {
375
+ const start = currentOffset;
376
+ const end = currentOffset + lineText.length;
377
+ linesInfo.push({ text: lineText, start, end });
378
+ currentOffset = end + 1;
379
+ });
380
+ const selectedLineIndices = [];
381
+ if (startOffsetChar === endOffsetChar) {
382
+ let targetLineIdx = -1;
383
+ for (let i = 0; i < linesInfo.length; i++) {
384
+ const line = linesInfo[i];
385
+ const isLast = i === linesInfo.length - 1;
386
+ if (startOffsetChar >= line.start && (startOffsetChar <= line.end || isLast && startOffsetChar <= line.end + 1)) {
387
+ targetLineIdx = i;
388
+ break;
389
+ }
390
+ if (i < linesInfo.length - 1 && startOffsetChar === linesInfo[i + 1].start - 1) {
391
+ targetLineIdx = i;
392
+ break;
580
393
  }
581
- _setCursor($block.childNodes[currentIdx + 1], 0);
394
+ }
395
+ if (targetLineIdx !== -1) {
396
+ selectedLineIndices.push(targetLineIdx);
582
397
  }
583
398
  } else {
584
- const childNodeList = $block.childNodes;
585
- const newCursorData = _sortingCursorDataOnElement(cursorData, $block);
586
- let structure = "";
587
- childNodeList.forEach((node, i) => {
588
- if (newCursorData.startNodeIdx > i) {
589
- if (node.constructor.name === "Text") {
590
- structure += node.textContent;
591
- } else {
592
- structure += node.outerHTML;
593
- }
594
- } else if (newCursorData.startNodeIdx === i) {
595
- if (node.constructor.name === "Text") {
596
- structure += node.textContent.slice(0, newCursorData.startOffset);
597
- structure += `<br>`;
598
- } else {
599
- if (node.tagName === "BR") {
600
- structure += `<br>`;
601
- } else {
602
- const originalClassList = Array.from(node.classList);
603
- const text = node.textContent;
604
- structure += `<span class="${originalClassList.join(" ")}">${text.slice(0, newCursorData.startOffset)}</span><br>`;
605
- }
606
- }
607
- if (childNodeList.length === i) {
608
- structure += `<br>`;
609
- }
610
- }
611
- if (newCursorData.endNodeIdx < i) {
612
- if (node.constructor.name === "Text") {
613
- structure += node.textContent;
614
- } else {
615
- structure += node.outerHTML;
616
- }
617
- } else if (newCursorData.endNodeIdx === i) {
618
- if (node.constructor.name === "Text") {
619
- structure += node.textContent.slice(newCursorData.endOffset);
620
- } else {
621
- if (node.tagName === "BR") {
622
- structure += `<br>`;
623
- } else {
624
- const originalClassList = Array.from(node.classList);
625
- const text = node.textContent;
626
- structure += `<span class="${originalClassList.join(" ")}">${text.slice(newCursorData.endOffset)}</span><br>`;
627
- }
628
- }
399
+ linesInfo.forEach((line, idx) => {
400
+ const hasOverlap = Math.max(startOffsetChar, line.start) < Math.min(endOffsetChar, line.end + 1);
401
+ if (hasOverlap) {
402
+ selectedLineIndices.push(idx);
629
403
  }
630
404
  });
631
- $block.innerHTML = structure;
632
- _setCursor($block.childNodes[newCursorData.startNodeIdx + 2], 0);
633
405
  }
634
- } else {
635
- console.error("[Dragon Editor] : Something wrong.");
636
- }
637
- }
638
- function __listBlockEnterEvent(event, store) {
639
- event.preventDefault();
640
- if (store.value.controlStatus.$currentBlock !== null && store.value.cursorData !== null) {
641
- const cursorData = store.value.cursorData;
642
- const $listBlock = store.value.controlStatus.$currentBlock;
643
- const $editableElement = _findContentEditableElement(event.target);
644
- const liList = $listBlock.querySelectorAll(".de-item");
645
- if ($editableElement !== null) {
646
- let liIdx = -1;
647
- for (let i = 0; liList.length > i; i += 1) {
648
- if (liList[i] === $editableElement) {
649
- liIdx = i;
650
- break;
651
- }
406
+ let startLineIdx = 0;
407
+ let endLineIdx = 0;
408
+ for (let i = 0; i < linesInfo.length; i++) {
409
+ const line = linesInfo[i];
410
+ const isLast = i === linesInfo.length - 1;
411
+ if (startOffsetChar >= line.start && (startOffsetChar <= line.end || isLast && startOffsetChar <= line.end + 1)) {
412
+ startLineIdx = i;
652
413
  }
653
- if (cursorData.type === "Caret") {
654
- if ($editableElement.textContent === "") {
655
- if ($editableElement.hasChildNodes() === false) {
656
- if (liList.length - 1 === liIdx) {
657
- $changeToTextBlock($listBlock, liList.length, $editableElement);
658
- } else {
659
- const $liBlock = _createListItemBlock();
660
- $editableElement.insertAdjacentElement("afterend", $liBlock);
661
- $liBlock.focus();
662
- }
663
- } else {
664
- const brList = $editableElement.querySelectorAll("br");
665
- if (brList.length === 1) {
666
- } else {
667
- }
668
- }
414
+ if (endOffsetChar >= line.start && (endOffsetChar <= line.end || isLast && endOffsetChar <= line.end + 1)) {
415
+ endLineIdx = i;
416
+ }
417
+ }
418
+ const startRelative = startOffsetChar - linesInfo[startLineIdx].start;
419
+ const endRelative = endOffsetChar - linesInfo[endLineIdx].start;
420
+ const updatedLines = [...lines];
421
+ const lineDiffs = Array(lines.length).fill(0);
422
+ const codeBlockSpaces = editorStore.option.codeBlockSpaces;
423
+ const addedSpacesStr = " ".repeat(codeBlockSpaces);
424
+ linesInfo.forEach((line, idx) => {
425
+ const isSelected = selectedLineIndices.includes(idx);
426
+ if (isSelected) {
427
+ let lineText = line.text;
428
+ let diff = 0;
429
+ if (event.shiftKey === false) {
430
+ lineText = addedSpacesStr + lineText;
431
+ diff = codeBlockSpaces;
669
432
  } else {
670
- const childNodeList = $editableElement.childNodes;
671
- const targetNode = _getParentElementIfNodeIsText(cursorData.startNode, $editableElement);
672
- const preStructure = [];
673
- const nextStructure = [];
674
- let nodeIdx = -1;
675
- for (let i = 0; childNodeList.length > i; i += 1) {
676
- if (childNodeList[i] === targetNode) {
677
- nodeIdx = i;
433
+ let spacesToRemove = 0;
434
+ for (let i = 0; i < codeBlockSpaces; i++) {
435
+ if (lineText[i] === " ") {
436
+ spacesToRemove++;
437
+ } else if (lineText[i] === " ") {
438
+ spacesToRemove = 1;
678
439
  break;
679
- }
680
- }
681
- childNodeList.forEach((node, i) => {
682
- if (nodeIdx < i) {
683
- nextStructure.push(node);
684
- } else if (nodeIdx > i) {
685
- preStructure.push(node);
686
- } else if (nodeIdx === i) {
687
- if (node.constructor.name === "Text") {
688
- const preText = node.textContent.slice(0, cursorData.startOffset);
689
- const nextText = node.textContent.slice(cursorData.endOffset);
690
- if (preText !== "") {
691
- preStructure.push(document.createTextNode(preText));
692
- }
693
- if (nextText !== "") {
694
- nextStructure.push(document.createTextNode(nextText));
695
- }
696
- } else {
697
- const originalClassList = Array.from(node.classList);
698
- const preText = node.textContent.slice(0, cursorData.startOffset);
699
- const nextText = node.textContent.slice(cursorData.endOffset);
700
- if (preText !== "") {
701
- const $span = document.createElement("span");
702
- $span.textContent = preText;
703
- $span.classList.add(...originalClassList);
704
- preStructure.push($span);
705
- }
706
- if (nextText !== "") {
707
- const $span = document.createElement("span");
708
- $span.textContent = nextText;
709
- $span.classList.add(...originalClassList);
710
- nextStructure.push($span);
711
- }
712
- }
713
- }
714
- });
715
- const $liBlock = _createListItemBlock();
716
- $editableElement.insertAdjacentElement("afterend", $liBlock);
717
- $editableElement.replaceChildren(...preStructure);
718
- $liBlock.replaceChildren(...nextStructure);
719
- if (nextStructure.length === 0) {
720
- $liBlock.focus();
721
- } else {
722
- _setCursor($liBlock.childNodes[0], 0);
723
- }
724
- }
725
- } else {
726
- const childNodeList = $editableElement.childNodes;
727
- const newCursorData = _sortingCursorDataOnElement(cursorData, $editableElement);
728
- const preStructure = [];
729
- const nextStructure = [];
730
- childNodeList.forEach((node, i) => {
731
- if (newCursorData.startNodeIdx > i) {
732
- preStructure.push(node);
733
- } else if (newCursorData.startNodeIdx === i) {
734
- if (node.constructor.name === "Text") {
735
- const text = node.textContent.slice(0, newCursorData.startOffset);
736
- if (text !== "") {
737
- const $textNode = document.createTextNode(text);
738
- preStructure.push($textNode);
739
- }
740
440
  } else {
741
- const originalClassList = Array.from(node.classList);
742
- const text = node.textContent.slice(0, newCursorData.startOffset);
743
- if (text !== "") {
744
- const $span = document.createElement("span");
745
- $span.classList.add(...originalClassList);
746
- $span.textContent = text;
747
- preStructure.push($span);
748
- }
441
+ break;
749
442
  }
750
443
  }
751
- if (newCursorData.endNodeIdx < i) {
752
- nextStructure.push(node);
753
- } else if (newCursorData.endNodeIdx === i) {
754
- if (node.constructor.name === "Text") {
755
- const text = node.textContent.slice(newCursorData.endOffset);
756
- if (text !== "") {
757
- const $textNode = document.createTextNode(text);
758
- nextStructure.push($textNode);
759
- }
760
- } else {
761
- const originalClassList = Array.from(node.classList);
762
- const text = node.textContent.slice(newCursorData.endOffset);
763
- if (text !== "") {
764
- const $span = document.createElement("span");
765
- $span.classList.add(...originalClassList);
766
- $span.textContent = text;
767
- nextStructure.push($span);
768
- }
769
- }
444
+ if (spacesToRemove > 0) {
445
+ lineText = lineText.slice(spacesToRemove);
446
+ diff = -spacesToRemove;
770
447
  }
771
- });
772
- const $liBlock = _createListItemBlock();
773
- $editableElement.insertAdjacentElement("afterend", $liBlock);
774
- $editableElement.replaceChildren(...preStructure);
775
- if (nextStructure.length === 0) {
776
- $liBlock.focus();
777
- } else {
778
- $liBlock.replaceChildren(...nextStructure);
779
- _setCursor(nextStructure[0], 0);
780
448
  }
449
+ updatedLines[idx] = lineText;
450
+ lineDiffs[idx] = diff;
451
+ }
452
+ });
453
+ const updatedText = updatedLines.join("\n");
454
+ const newLinesStart = [];
455
+ let curOffset = 0;
456
+ updatedLines.forEach((lineText) => {
457
+ newLinesStart.push(curOffset);
458
+ curOffset += lineText.length + 1;
459
+ });
460
+ let finalStartRelative = startRelative;
461
+ if (selectedLineIndices.includes(startLineIdx)) {
462
+ const diff = lineDiffs[startLineIdx];
463
+ if (diff > 0) {
464
+ finalStartRelative += diff;
465
+ } else {
466
+ finalStartRelative = Math.max(0, finalStartRelative + diff);
781
467
  }
782
468
  }
783
- }
784
- function $changeToTextBlock($listBlock, licount, $editableElement) {
785
- const $textBlock = _createTextBlock(_getDefaultBlockData("text"));
786
- $listBlock.insertAdjacentElement("afterend", $textBlock);
787
- if (licount === 1) {
788
- $listBlock.remove();
789
- } else {
790
- $editableElement.remove();
469
+ const newStartOffset = newLinesStart[startLineIdx] + finalStartRelative;
470
+ let finalEndRelative = endRelative;
471
+ if (selectedLineIndices.includes(endLineIdx)) {
472
+ const diff = lineDiffs[endLineIdx];
473
+ if (diff > 0) {
474
+ finalEndRelative += diff;
475
+ } else {
476
+ finalEndRelative = Math.max(0, finalEndRelative + diff);
477
+ }
478
+ }
479
+ const newEndOffset = newLinesStart[endLineIdx] + finalEndRelative;
480
+ $target.textContent = updatedText;
481
+ $target.dispatchEvent(new Event("input"));
482
+ await nextTick();
483
+ let selectionOffset = 0;
484
+ let startNode = null;
485
+ let startNodeOffset = 0;
486
+ let endNode = null;
487
+ let endNodeOffset = 0;
488
+ traverse($target);
489
+ if (startNode === null) {
490
+ startNode = $target;
491
+ startNodeOffset = 0;
791
492
  }
792
- $textBlock.focus();
493
+ if (endNode === null) {
494
+ endNode = $target;
495
+ endNodeOffset = 0;
496
+ }
497
+ editorStore.cursorSelection.removeAllRanges();
498
+ const newRange = document.createRange();
499
+ newRange.setStart(startNode, startNodeOffset);
500
+ newRange.setEnd(endNode, endNodeOffset);
501
+ editorStore.cursorSelection.addRange(newRange);
793
502
  }
794
503
  }
795
- function __listBlockShiftEnterEvent(event, store) {
796
- event.preventDefault();
797
- if (store.value.cursorData !== null && store.value.controlStatus.$currentBlock !== null) {
798
- const cursorData = store.value.cursorData;
799
- const $editableElement = _findContentEditableElement(event.target);
800
- if ($editableElement !== null) {
801
- if (cursorData.type === "Caret") {
802
- if ($editableElement.textContent === "") {
803
- if ($editableElement.hasChildNodes() === false) {
804
- $editableElement.insertAdjacentHTML("beforeend", "<br><br>");
805
- _setCursor($editableElement.childNodes[1], 0);
806
- } else {
807
- const $br = document.createElement("br");
808
- $editableElement.insertAdjacentElement("beforeend", $br);
809
- _setCursor($br, 0);
504
+ export function _moveBlockDefaultEvent(event, type) {
505
+ const editorStore = useEditorStore();
506
+ _updateCursorData();
507
+ if (editorStore.element.body !== null && editorStore.selectedBlockIndex !== -1) {
508
+ const $block = editorStore.element.body.children[editorStore.selectedBlockIndex];
509
+ const $targetBlock = type === "up" ? $block.previousElementSibling : $block.nextElementSibling;
510
+ const blockType = _getBlockType($block);
511
+ let positionData = _getMultilinePosition($block);
512
+ if (blockType === "image") {
513
+ positionData = _getMultilinePosition($block.querySelector(".de-caption"));
514
+ }
515
+ if ($targetBlock !== null) {
516
+ let logicWork = false;
517
+ if (positionData.lineCount === 1) {
518
+ logicWork = true;
519
+ } else {
520
+ if (type === "up") {
521
+ if (positionData.curruntLine === 1) {
522
+ logicWork = true;
810
523
  }
811
524
  } else {
812
- const childList = $editableElement.childNodes;
813
- let targetIdx = -1;
814
- let structure = "";
815
- let $target = cursorData.startNode;
816
- if ($target.constructor.name === "Text") {
817
- if ($target.parentNode !== $editableElement) {
818
- $target = $target.parentNode;
819
- }
525
+ if (positionData.curruntLine === positionData.lineCount) {
526
+ logicWork = true;
820
527
  }
821
- if ($editableElement === $target) {
822
- $target = $editableElement.childNodes[cursorData.startOffset];
823
- }
824
- for (let i = 0; childList.length > i; i += 1) {
825
- if (childList[i] === $target) {
826
- targetIdx = i;
827
- break;
828
- }
829
- }
830
- let currentIdx = targetIdx;
831
- childList.forEach((child, i) => {
832
- if (i === targetIdx) {
833
- const constructorName = child.constructor.name;
834
- if (constructorName === "Text") {
835
- structure += child.textContent.slice(0, cursorData.startOffset) + "<br>" + child.textContent.slice(cursorData.endOffset);
836
- if (child.nextSibling === null) {
837
- if (child.textContent.slice(cursorData.endOffset) === "") {
838
- structure += `<br>`;
839
- }
840
- if (child.textContent.slice(0, cursorData.startOffset) === "") {
841
- currentIdx -= 1;
842
- }
843
- } else {
844
- if (child.textContent.slice(cursorData.endOffset) !== "" && child.textContent.slice(0, cursorData.startOffset) === "") {
845
- currentIdx -= 1;
846
- }
847
- }
848
- currentIdx += 1;
849
- } else {
850
- if (constructorName === "HTMLBRElement") {
851
- structure += `<br><br>`;
852
- } else {
853
- structure += `<span class="${Array.from(child.classList).join(" ")}">${child.textContent.slice(0, cursorData.startOffset)}</span>`;
854
- structure += `<br>`;
855
- structure += `<span class="${Array.from(child.classList).join(" ")}">${child.textContent.slice(cursorData.startOffset)}</span>`;
856
- currentIdx += 1;
857
- }
858
- }
859
- } else {
860
- if (child.constructor.name === "Text") {
861
- structure += child.textContent;
862
- } else {
863
- structure += child.outerHTML;
864
- }
865
- }
866
- });
867
- $editableElement.innerHTML = structure;
868
- _setCursor($editableElement.childNodes[currentIdx + 1], 0);
869
528
  }
870
- } else {
871
- const childNodeList = $editableElement.childNodes;
872
- const newCursorData = _sortingCursorDataOnElement(cursorData, $editableElement);
873
- let structure = "";
874
- childNodeList.forEach((node, i) => {
875
- if (newCursorData.startNodeIdx > i) {
876
- if (node.constructor.name === "Text") {
877
- structure += node.textContent;
878
- } else {
879
- structure += node.outerHTML;
880
- }
881
- } else if (newCursorData.startNodeIdx === i) {
882
- if (node.constructor.name === "Text") {
883
- structure += node.textContent.slice(0, newCursorData.startOffset);
884
- structure += `<br>`;
885
- } else {
886
- if (node.tagName === "BR") {
887
- structure += `<br>`;
888
- } else {
889
- const originalClassList = Array.from(node.classList);
890
- const text = node.textContent;
891
- structure += `<span class="${originalClassList.join(" ")}">${text.slice(0, newCursorData.startOffset)}</span><br>`;
892
- }
893
- }
894
- if (childNodeList.length === i) {
895
- structure += `<br>`;
896
- }
897
- }
898
- if (newCursorData.endNodeIdx < i) {
899
- if (node.constructor.name === "Text") {
900
- structure += node.textContent;
901
- } else {
902
- structure += node.outerHTML;
903
- }
904
- } else if (newCursorData.endNodeIdx === i) {
905
- if (node.constructor.name === "Text") {
906
- structure += node.textContent.slice(newCursorData.endOffset);
907
- } else {
908
- if (node.tagName === "BR") {
909
- structure += `<br>`;
910
- } else {
911
- const originalClassList = Array.from(node.classList);
912
- const text = node.textContent;
913
- structure += `<span class="${originalClassList.join(" ")}">${text.slice(newCursorData.endOffset)}</span><br>`;
914
- }
915
- }
529
+ }
530
+ if (logicWork === true) {
531
+ event.preventDefault();
532
+ const $editableTarget = _findEditableElement($targetBlock, type);
533
+ if (editorStore.cursorSelection !== null && $editableTarget !== null) {
534
+ const range = document.createRange();
535
+ $editableTarget.focus();
536
+ range.selectNodeContents($editableTarget);
537
+ if (type === "up") {
538
+ range.collapse(false);
539
+ } else {
540
+ range.collapse(true);
916
541
  }
917
- });
918
- $editableElement.innerHTML = structure;
919
- _setCursor($editableElement.childNodes[newCursorData.startNodeIdx + 2], 0);
542
+ editorStore.cursorSelection.removeAllRanges();
543
+ editorStore.cursorSelection.addRange(range);
544
+ _updateCursorData();
545
+ }
920
546
  }
921
547
  }
922
- } else {
923
- console.error("[Dragon Editor] : Something wrong.");
924
- }
925
- }
926
- function __codeBlockShiftEnterEvent(event, store) {
927
- event.preventDefault();
928
- if (store.value.controlStatus.$currentBlock !== null) {
929
- const $block = store.value.controlStatus.$currentBlock;
930
- const $newTextBlock = _createTextBlock(_getDefaultBlockData("text"));
931
- $block.insertAdjacentElement("afterend", $newTextBlock);
932
- $newTextBlock.focus();
933
548
  }
934
549
  }
935
- function __backspaceEvent(event, store) {
936
- switch (store.value.controlStatus.currentBlockType) {
937
- case "image":
938
- case "code":
939
- break;
940
- case "ol":
941
- case "ul":
942
- ___listBlockBackspaceEvent(event, store);
943
- break;
944
- default:
945
- ___defaultBlockBackspaceEvent(event, store);
946
- }
947
- }
948
- function ___defaultBlockBackspaceEvent(event, store) {
949
- if (store.value.cursorData !== null && store.value.controlStatus.$currentBlock !== null && store.value.$body !== null) {
950
- const cursorData = store.value.cursorData;
951
- const childList = store.value.$body.querySelectorAll(".de-block");
952
- const $block = store.value.controlStatus.$currentBlock;
953
- let $target = cursorData.startNode;
954
- let elementIdx = -1;
955
- for (let i = 0; childList.length > i; i += 1) {
956
- if (childList[i] === $block) {
957
- elementIdx = i;
958
- break;
550
+ export function _moveListChildEvent(event, data, index, childIndex, type) {
551
+ const editorStore = useEditorStore();
552
+ _updateCursorData();
553
+ if (editorStore.element.body !== null) {
554
+ const $target = event.currentTarget;
555
+ let positionData = _getMultilinePosition($target);
556
+ let targetType = "child";
557
+ let logicWork = false;
558
+ if (data.child.length === 1) {
559
+ targetType = "block";
560
+ } else {
561
+ if (childIndex === 0 && type === "up") {
562
+ targetType = "block";
563
+ }
564
+ if (childIndex === data.child.length - 1 && type === "down") {
565
+ targetType = "block";
959
566
  }
960
567
  }
961
- if ($target.constructor.name === "Text") {
962
- $target = $target.parentNode;
963
- }
964
- if (elementIdx === 0) {
965
- if (cursorData.type === "Caret") {
966
- if (cursorData.startOffset === 0 && $target === $block) {
967
- if ($target.textContent === "") {
968
- $block.insertAdjacentElement("afterend", _createTextBlock(_getDefaultBlockData("text")));
969
- _setCursor($block.nextElementSibling, 0);
970
- $block.remove();
971
- } else {
972
- event.preventDefault();
973
- if (store.value.controlStatus.currentBlockType !== "text") {
974
- const $newBlock = _createTextBlock({ type: "text", classList: [], textContent: $block.textContent ?? "" });
975
- $block.insertAdjacentElement("afterend", $newBlock);
976
- _setCursor($newBlock, 0);
977
- $block.remove();
978
- }
979
- }
568
+ if (positionData.lineCount === 1) {
569
+ logicWork = true;
570
+ } else {
571
+ if (type === "up") {
572
+ if (positionData.curruntLine === 1) {
573
+ logicWork = true;
980
574
  }
981
575
  } else {
982
- if ($block.textContent !== "") {
983
- if ((cursorData.startOffset === 0 || cursorData.endOffset === 0) && ($block.childNodes[0] === cursorData.startNode || $block.childNodes[0] === $target || $block.childNodes[0] === cursorData.endNode)) {
984
- event.preventDefault();
985
- const $newBlock = _createTextBlock({ type: "text", classList: [], textContent: "" });
986
- $block.insertAdjacentElement("afterend", $newBlock);
987
- _setCursor($newBlock, 0);
988
- $block.remove();
989
- }
576
+ if (positionData.curruntLine === positionData.lineCount) {
577
+ logicWork = true;
990
578
  }
991
579
  }
992
- } else {
993
- if ($block.textContent === "") {
994
- event.preventDefault();
995
- let hasChild = false;
996
- let childHTML = "";
997
- if ($block.hasChildNodes() === true) {
998
- const $brList = $block.querySelectorAll("br");
999
- if ($brList.length > 1) {
1000
- hasChild = true;
1001
- childHTML = $block.innerHTML;
1002
- }
580
+ }
581
+ if (logicWork === true) {
582
+ const $block = editorStore.element.body.children[index];
583
+ let $editableTarget = null;
584
+ event.preventDefault();
585
+ if (targetType === "block") {
586
+ const $targetBlock = type === "up" ? $block.previousElementSibling : $block.nextElementSibling;
587
+ if ($targetBlock !== null) {
588
+ $editableTarget = _findEditableElement($targetBlock, type);
1003
589
  }
1004
- ____backspackeLogic(hasChild, childHTML, $block, $block);
1005
590
  } else {
1006
- if (cursorData.type === "Caret" && cursorData.startOffset === 0 && ($block.childNodes[0] === cursorData.startNode || $block.childNodes[0] === $target)) {
1007
- event.preventDefault();
1008
- let childHTML = $block.innerHTML;
1009
- ____backspackeLogic(true, childHTML, $block, $block);
591
+ const $targetChildElement = $block.querySelectorAll(".de-item-text");
592
+ $editableTarget = type === "up" ? $targetChildElement[childIndex - 1] : $targetChildElement[childIndex + 1];
593
+ }
594
+ if (editorStore.cursorSelection !== null && $editableTarget !== null) {
595
+ const range = document.createRange();
596
+ $editableTarget.focus();
597
+ range.selectNodeContents($editableTarget);
598
+ if (type === "up") {
599
+ range.collapse(false);
600
+ } else {
601
+ range.collapse(true);
1010
602
  }
603
+ editorStore.cursorSelection.removeAllRanges();
604
+ editorStore.cursorSelection.addRange(range);
605
+ _updateCursorData();
1011
606
  }
1012
607
  }
1013
- ____nodeBackspaceEvent(event, cursorData, $block, $target);
1014
- _updateCursorData(store);
1015
608
  }
1016
609
  }
1017
- function ___listBlockBackspaceEvent(event, store) {
1018
- if (store.value.cursorData !== null && store.value.controlStatus.$currentBlock !== null && store.value.$body !== null) {
1019
- const cursorData = store.value.cursorData;
1020
- const $listBlock = store.value.controlStatus.$currentBlock;
1021
- const $targetItem = _findContentEditableElement(cursorData.startNode);
1022
- const $liList = $listBlock.querySelectorAll(".de-item");
1023
- let $target = cursorData.startNode;
1024
- let liIdx = -1;
1025
- if ($target.constructor.name === "Text") {
1026
- $target = $target.parentNode;
1027
- }
1028
- for (let i = 0; $liList.length > i; i += 1) {
1029
- if ($liList[i] === $targetItem) {
1030
- liIdx = i;
1031
- break;
1032
- }
1033
- }
1034
- if ($liList.length === 1) {
1035
- if ($targetItem.textContent === "") {
1036
- event.preventDefault();
1037
- let hasChild = false;
1038
- let childHTML = "";
1039
- if ($targetItem.hasChildNodes() === true) {
1040
- const $brList = $targetItem.querySelectorAll("br");
1041
- if ($brList.length > 1) {
1042
- hasChild = true;
1043
- childHTML = $targetItem.innerHTML;
1044
- }
1045
- }
1046
- ____backspackeLogic(hasChild, childHTML, $listBlock, $targetItem);
1047
- $listBlock.remove();
610
+ export function _moveCodeBlockEvent(event, direction, type, $targetElement) {
611
+ const editorStore = useEditorStore();
612
+ _updateCursorData();
613
+ if (editorStore.cursorSelection !== null) {
614
+ if (type === "filename") {
615
+ event.preventDefault();
616
+ if (direction === "up") {
617
+ _moveBlockDefaultEvent(event, direction);
1048
618
  } else {
1049
- if (cursorData.type === "Caret" && cursorData.startOffset === 0 && ($targetItem.childNodes[0] === cursorData.startNode || $targetItem.childNodes[0] === $target)) {
1050
- event.preventDefault();
1051
- let childHTML = $targetItem.innerHTML;
1052
- ____backspackeLogic(true, childHTML, $listBlock, $targetItem);
1053
- $listBlock.remove();
1054
- }
619
+ const range = document.createRange();
620
+ $targetElement.focus();
621
+ range.selectNodeContents($targetElement);
622
+ range.collapse(true);
623
+ editorStore.cursorSelection.removeAllRanges();
624
+ editorStore.cursorSelection.addRange(range);
625
+ _updateCursorData();
1055
626
  }
1056
627
  } else {
1057
- if (liIdx === 0) {
1058
- if ($targetItem.textContent === "") {
628
+ const $content = event.currentTarget;
629
+ const positionData = _getMultilinePosition($content);
630
+ if (direction === "up") {
631
+ if (positionData.curruntLine === 1) {
1059
632
  event.preventDefault();
1060
- let hasChild = false;
1061
- let childHTML = "";
1062
- if ($targetItem.hasChildNodes() === true) {
1063
- const $brList = $targetItem.querySelectorAll("br");
1064
- if ($brList.length > 1) {
1065
- hasChild = true;
1066
- childHTML = $targetItem.innerHTML;
1067
- }
1068
- }
1069
- ____backspackeLogic(hasChild, childHTML, $listBlock, $targetItem);
1070
- } else {
1071
- if (cursorData.type === "Caret" && cursorData.startOffset === 0 && ($targetItem.childNodes[0] === cursorData.startNode || $targetItem.childNodes[0] === $target)) {
1072
- event.preventDefault();
1073
- let childHTML = $targetItem.innerHTML;
1074
- ____backspackeLogic(true, childHTML, $listBlock, $targetItem);
1075
- }
633
+ const range = document.createRange();
634
+ $targetElement.focus();
635
+ range.selectNodeContents($targetElement);
636
+ range.collapse(false);
637
+ editorStore.cursorSelection.removeAllRanges();
638
+ editorStore.cursorSelection.addRange(range);
639
+ _updateCursorData();
1076
640
  }
1077
641
  } else {
1078
- if ($targetItem.textContent === "") {
1079
- event.preventDefault();
1080
- const $preLi = $targetItem.previousElementSibling;
1081
- const hasChild = $preLi.hasChildNodes();
1082
- let childHTML = "";
1083
- if ($targetItem.hasChildNodes() === true) {
1084
- const $brList = $targetItem.querySelectorAll("br");
1085
- if ($brList.length > 1) {
1086
- childHTML = $targetItem.innerHTML;
1087
- }
1088
- }
1089
- if ($preLi.hasChildNodes() === true) {
1090
- const textBlockChildList = $preLi.childNodes;
1091
- const textBlockTargetChild = textBlockChildList[textBlockChildList.length - 1];
1092
- if (hasChild === true) {
1093
- $preLi.insertAdjacentHTML("beforeend", childHTML);
1094
- }
1095
- _setCursor(textBlockTargetChild, textBlockTargetChild.textContent.length);
1096
- } else {
1097
- if (hasChild === true) {
1098
- $preLi.insertAdjacentHTML("beforeend", childHTML);
1099
- }
1100
- _setCursor($preLi, 0);
1101
- }
1102
- $targetItem.remove();
1103
- } else {
1104
- if (cursorData.type === "Caret" && cursorData.startOffset === 0 && ($targetItem.childNodes[0] === cursorData.startNode || $targetItem.childNodes[0] === $target)) {
1105
- event.preventDefault();
1106
- const $preLi = $targetItem.previousElementSibling;
1107
- const hasChild = $preLi.hasChildNodes();
1108
- let childHTML = $targetItem.innerHTML;
1109
- if ($preLi.hasChildNodes() === true) {
1110
- const textBlockChildList = $preLi.childNodes;
1111
- const textBlockTargetChild = textBlockChildList[textBlockChildList.length - 1];
1112
- if (hasChild === true) {
1113
- $preLi.insertAdjacentHTML("beforeend", childHTML);
1114
- }
1115
- _setCursor(textBlockTargetChild, textBlockTargetChild.textContent.length);
1116
- } else {
1117
- if (hasChild === true) {
1118
- $preLi.insertAdjacentHTML("beforeend", childHTML);
1119
- }
1120
- _setCursor($preLi, 0);
1121
- }
1122
- $targetItem.remove();
1123
- }
642
+ if (positionData.curruntLine === positionData.lineCount) {
643
+ _moveBlockDefaultEvent(event, direction);
1124
644
  }
1125
645
  }
1126
646
  }
1127
647
  }
1128
648
  }
1129
- function ____backspackeLogic(hasChild, childHTML, $block, $targetItem) {
1130
- const $preBlock = $block.previousElementSibling;
1131
- if ($preBlock !== null) {
1132
- const { type: preBlockType } = _getCurrentBlock($preBlock);
1133
- let preDelete = false;
1134
- let $targetBlock = $preBlock;
1135
- switch (preBlockType) {
1136
- case "custom":
1137
- case "image":
1138
- case "code":
1139
- preDelete = true;
1140
- break;
1141
- case "ul":
1142
- case "ol":
1143
- const $liList = $preBlock.querySelectorAll(".de-item");
1144
- $targetBlock = $liList[$liList.length - 1];
1145
- break;
1146
- default:
1147
- $targetBlock = $preBlock;
1148
- }
1149
- if (preDelete === false) {
1150
- if ($targetBlock.hasChildNodes() === true) {
1151
- const textBlockChildList = $targetBlock.childNodes;
1152
- const textBlockTargetChild = textBlockChildList[textBlockChildList.length - 1];
1153
- if (hasChild === true) {
1154
- $targetBlock.insertAdjacentHTML("beforeend", childHTML);
649
+ export async function _defaultBackspaceEvent(event) {
650
+ const editorStore = useEditorStore();
651
+ const $target = event.currentTarget;
652
+ _updateCursorData();
653
+ if ($target !== null && editorStore.element.body !== null && editorStore.fn.updateEditorData !== null) {
654
+ const multilinePosition = _getMultilinePosition($target);
655
+ const cursorPosition = _isCursorAtLineBoundary();
656
+ if (multilinePosition.curruntLine === 1 && cursorPosition.isStart === true) {
657
+ event.preventDefault();
658
+ const newData = JSON.parse(JSON.stringify(editorStore.data));
659
+ const curruntData = newData[editorStore.selectedBlockIndex];
660
+ if (editorStore.selectedBlockIndex === 0) {
661
+ if (curruntData.type === "heading") {
662
+ newData.splice(editorStore.selectedBlockIndex, 1, _createTextBlockData(curruntData.textContent));
663
+ editorStore.fn.updateEditorData(newData);
664
+ await nextTick();
665
+ const $block = editorStore.element.body.children[0];
666
+ $block.focus();
1155
667
  }
1156
- _setCursor(textBlockTargetChild, textBlockTargetChild.textContent.length);
1157
668
  } else {
1158
- if (hasChild === true) {
1159
- $targetBlock.insertAdjacentHTML("beforeend", childHTML);
669
+ const preveiousData = newData[editorStore.selectedBlockIndex - 1];
670
+ const $preveiousBlock = editorStore.element.body.children[editorStore.selectedBlockIndex - 1];
671
+ if (preveiousData.type === "text" || preveiousData.type === "heading") {
672
+ const blockLastOffset = _getEditorbleEndPosition($preveiousBlock);
673
+ preveiousData.textContent += curruntData.textContent;
674
+ newData.splice(editorStore.selectedBlockIndex - 1, 1, preveiousData);
675
+ newData.splice(editorStore.selectedBlockIndex, 1);
676
+ editorStore.fn.updateEditorData(newData);
677
+ await nextTick();
678
+ const $block = editorStore.element.body.children[editorStore.selectedBlockIndex - 1];
679
+ const $blockTargetNode = $block.childNodes[blockLastOffset.nodeIndex];
680
+ if ($blockTargetNode !== void 0) {
681
+ _setCursorPosition($blockTargetNode, blockLastOffset.offset);
682
+ }
683
+ } else if (preveiousData.type === "list") {
684
+ const $listItems = $preveiousBlock.querySelectorAll(".de-item-text");
685
+ const $listLastChild = $listItems[$listItems.length - 1];
686
+ if ($listLastChild !== void 0) {
687
+ const listChildOffset = _getEditorbleEndPosition($listLastChild);
688
+ const listChildData = preveiousData.child[preveiousData.child.length - 1];
689
+ listChildData.textContent += curruntData.textContent;
690
+ preveiousData.child[preveiousData.child.length - 1] = listChildData;
691
+ newData.splice(editorStore.selectedBlockIndex - 1, 1, preveiousData);
692
+ newData.splice(editorStore.selectedBlockIndex, 1);
693
+ editorStore.fn.updateEditorData(newData);
694
+ await nextTick();
695
+ const $node = $listLastChild.childNodes[listChildOffset.nodeIndex];
696
+ if ($node !== void 0) {
697
+ _setCursorPosition($node, listChildOffset.offset);
698
+ }
699
+ }
700
+ } else {
701
+ newData.splice(editorStore.selectedBlockIndex - 1, 1);
702
+ editorStore.fn.updateEditorData(newData);
703
+ editorStore.selectedBlockIndex -= 1;
1160
704
  }
1161
- _setCursor($targetBlock, 0);
1162
705
  }
1163
- $targetItem.remove();
1164
- } else {
1165
- $preBlock.remove();
1166
706
  }
1167
- } else {
1168
- const newTextBlockData = _getDefaultBlockData("text");
1169
- newTextBlockData.textContent = childHTML;
1170
- const $newTextBlock = _createTextBlock(newTextBlockData);
1171
- $block.insertAdjacentElement("afterend", $newTextBlock);
1172
- $newTextBlock.focus();
1173
- $block.remove();
1174
707
  }
1175
708
  }
1176
- function ____nodeBackspaceEvent(event, cursorData, $block, $target) {
1177
- if (cursorData.startOffset === 1 && $target !== $block) {
1178
- if ($target.textContent.length === 1) {
709
+ export async function _listBackspaceEvent(event, childIndex) {
710
+ const editorStore = useEditorStore();
711
+ const $target = event.currentTarget;
712
+ _updateCursorData();
713
+ if ($target !== null && editorStore.element.body !== null && editorStore.fn.updateEditorData !== null) {
714
+ const multilinePosition = _getMultilinePosition($target);
715
+ const cursorPosition = _isCursorAtLineBoundary();
716
+ if (multilinePosition.curruntLine === 1 && cursorPosition.isStart === true) {
1179
717
  event.preventDefault();
1180
- const preNode = $target.previousSibling;
1181
- if (preNode !== null) {
1182
- $target.remove();
1183
- _setCursor(preNode, preNode.textContent.length);
1184
- } else {
1185
- if ($block.childNodes[1] === void 0) {
1186
- $block.innerHTML = "";
1187
- _setCursor($block, 0);
718
+ const newData = JSON.parse(JSON.stringify(editorStore.data));
719
+ const curruntData = newData[editorStore.selectedBlockIndex];
720
+ const curruntListItem = curruntData.child[childIndex];
721
+ const $block = _findParentBlock($target);
722
+ if ($block !== null && curruntListItem !== void 0) {
723
+ if (childIndex === 0) {
724
+ if (curruntData.child.length === 1) {
725
+ newData.splice(editorStore.selectedBlockIndex, 1, _createTextBlockData(curruntListItem.textContent));
726
+ editorStore.fn.updateEditorData(newData);
727
+ await nextTick();
728
+ const $targetBlock = editorStore.element.body.children[editorStore.selectedBlockIndex];
729
+ if ($targetBlock !== void 0) {
730
+ $targetBlock.focus();
731
+ }
732
+ } else {
733
+ curruntData.child.splice(childIndex, 1);
734
+ newData.splice(editorStore.selectedBlockIndex, 1, _createTextBlockData(curruntListItem.textContent), curruntData);
735
+ editorStore.fn.updateEditorData(newData);
736
+ await nextTick();
737
+ const $targetBlock = editorStore.element.body.children[editorStore.selectedBlockIndex];
738
+ if ($targetBlock !== void 0) {
739
+ $targetBlock.focus();
740
+ }
741
+ }
1188
742
  } else {
1189
- _setCursor($block.childNodes[1], 0);
1190
- $target.remove();
743
+ const preveiousListItem = curruntData.child[childIndex - 1];
744
+ const $listItems = $block.querySelectorAll(".de-item-text");
745
+ const $preveiousListBlock = $listItems[childIndex - 1];
746
+ if (preveiousListItem !== void 0 && $preveiousListBlock !== void 0) {
747
+ const listChildEndOffset = _getEditorbleEndPosition($preveiousListBlock);
748
+ preveiousListItem.textContent += curruntListItem.textContent;
749
+ curruntData.child.splice(childIndex, 1);
750
+ newData.splice(editorStore.selectedBlockIndex, 1, curruntData);
751
+ editorStore.fn.updateEditorData(newData);
752
+ await nextTick();
753
+ const $targetNode = $preveiousListBlock.childNodes[listChildEndOffset.nodeIndex];
754
+ if ($targetNode !== void 0) {
755
+ _setCursorPosition($targetNode, listChildEndOffset.offset);
756
+ }
757
+ }
1191
758
  }
1192
759
  }
1193
760
  }
1194
761
  }
1195
762
  }
1196
- function __tabEvent(event, store) {
1197
- event.preventDefault();
1198
- switch (store.value.controlStatus.currentBlockType) {
1199
- case "code":
1200
- case "custom":
1201
- case "image":
1202
- break;
1203
- default:
1204
- if (event.shiftKey === true) {
1205
- _setIndent(store, "minus");
1206
- } else {
1207
- _setIndent(store, "plus");
1208
- }
1209
- }
1210
- }
1211
- export function _setIndent(store, type) {
1212
- if (store.value.controlStatus.$currentBlock !== null) {
1213
- const $block = store.value.controlStatus.$currentBlock;
1214
- let value = $block.dataset["depth"] === void 0 ? 0 : parseInt($block.dataset["depth"]);
1215
- if (type === "minus") {
1216
- if (value !== 0) {
1217
- value -= 1;
1218
- }
1219
- } else {
1220
- if (value < 5) {
1221
- value += 1;
763
+ export async function _defaultDeleteEvent(event, setEvent, abortEvent) {
764
+ const editorStore = useEditorStore();
765
+ const $target = event.currentTarget;
766
+ _updateCursorData();
767
+ if ($target !== null && editorStore.element.body !== null && editorStore.fn.updateEditorData !== null) {
768
+ const multilinePosition = _getMultilinePosition($target);
769
+ const cursorPosition = _isCursorAtLineBoundary();
770
+ if (multilinePosition.curruntLine === multilinePosition.lineCount && cursorPosition.isEnd === true) {
771
+ event.preventDefault();
772
+ const newData = JSON.parse(JSON.stringify(editorStore.data));
773
+ const curruntData = newData[editorStore.selectedBlockIndex];
774
+ if (editorStore.selectedBlockIndex !== newData.length - 1) {
775
+ const nextData = newData[editorStore.selectedBlockIndex + 1];
776
+ const $nextBlock = editorStore.element.body.children[editorStore.selectedBlockIndex + 1];
777
+ const blockLastOffset = _getEditorbleEndPosition($target);
778
+ if (nextData.type === "text" || nextData.type === "heading") {
779
+ curruntData.textContent += nextData.textContent;
780
+ newData.splice(editorStore.selectedBlockIndex, 1, curruntData);
781
+ newData.splice(editorStore.selectedBlockIndex + 1, 1);
782
+ abortEvent();
783
+ editorStore.fn.updateEditorData(newData);
784
+ await nextTick();
785
+ setEvent(editorStore.selectedBlockIndex, curruntData.id);
786
+ const $block = editorStore.element.body.children[editorStore.selectedBlockIndex];
787
+ const $blockTargetNode = $block.childNodes[blockLastOffset.nodeIndex];
788
+ if ($blockTargetNode !== void 0) {
789
+ _setCursorPosition($blockTargetNode, blockLastOffset.offset);
790
+ }
791
+ } else if (nextData.type === "list") {
792
+ const $listItems = $nextBlock.querySelectorAll(".de-item-text");
793
+ const $listLastChild = $listItems[0];
794
+ if ($listLastChild !== void 0) {
795
+ const listChildData = nextData.child[0];
796
+ curruntData.textContent += listChildData.textContent;
797
+ nextData.child.splice(0, 1);
798
+ newData.splice(editorStore.selectedBlockIndex, 1, curruntData);
799
+ newData.splice(editorStore.selectedBlockIndex + 1, 1, nextData);
800
+ abortEvent();
801
+ editorStore.fn.updateEditorData(newData);
802
+ await nextTick();
803
+ setEvent(editorStore.selectedBlockIndex, curruntData.id);
804
+ const $block = editorStore.element.body.children[editorStore.selectedBlockIndex];
805
+ const $blockTargetNode = $block.childNodes[blockLastOffset.nodeIndex];
806
+ if ($blockTargetNode !== void 0) {
807
+ _setCursorPosition($blockTargetNode, blockLastOffset.offset);
808
+ }
809
+ }
810
+ } else {
811
+ newData.splice(editorStore.selectedBlockIndex + 1, 1);
812
+ editorStore.fn.updateEditorData(newData);
813
+ }
1222
814
  }
1223
815
  }
1224
- if (value === 0) {
1225
- delete $block.dataset["depth"];
1226
- } else {
1227
- $block.dataset["depth"] = String(value);
1228
- }
1229
- _updateModelData(store);
1230
816
  }
1231
817
  }
1232
- function __deleteEvent(event, store) {
1233
- switch (store.value.controlStatus.currentBlockType) {
1234
- case "image":
1235
- case "code":
1236
- break;
1237
- case "ol":
1238
- case "ul":
1239
- ___listBlockDeleteEvent(event, store);
1240
- break;
1241
- default:
1242
- ___defaultBlockDeleteEvent(event, store);
1243
- }
1244
- }
1245
- function ___defaultBlockDeleteEvent(event, store) {
1246
- if (store.value.cursorData !== null && store.value.controlStatus.$currentBlock !== null && store.value.$body !== null) {
1247
- const cursorData = store.value.cursorData;
1248
- const $block = store.value.controlStatus.$currentBlock;
1249
- let $target = cursorData.startNode;
1250
- if ($target.constructor.name === "Text") {
1251
- $target = $target.parentNode;
1252
- }
1253
- if ($block.textContent === "") {
818
+ export async function _listDeleteEvent(event, childIndex, setEvent, abortEvent) {
819
+ const editorStore = useEditorStore();
820
+ const $target = event.currentTarget;
821
+ _updateCursorData();
822
+ if ($target !== null && editorStore.element.body !== null && editorStore.fn.updateEditorData !== null) {
823
+ const multilinePosition = _getMultilinePosition($target);
824
+ const cursorPosition = _isCursorAtLineBoundary();
825
+ if (multilinePosition.curruntLine === multilinePosition.lineCount && cursorPosition.isEnd === true) {
1254
826
  event.preventDefault();
1255
- let hasChild = false;
1256
- if ($block.hasChildNodes() === true) {
1257
- const $brList = $block.querySelectorAll("br");
1258
- if ($brList.length === 1) {
1259
- $block.innerHTML = "";
827
+ const newData = JSON.parse(JSON.stringify(editorStore.data));
828
+ const curruntData = newData[editorStore.selectedBlockIndex];
829
+ const curruntListItem = curruntData.child[childIndex];
830
+ const blockLastOffset = _getEditorbleEndPosition($target);
831
+ const $block = _findParentBlock($target);
832
+ if ($block !== null && curruntListItem !== void 0) {
833
+ if (childIndex === curruntData.child.length - 1) {
834
+ const nextData = newData[editorStore.selectedBlockIndex + 1];
835
+ const $nextBlock = editorStore.element.body.children[editorStore.selectedBlockIndex + 1];
836
+ if (nextData.type === "text" || nextData.type === "heading") {
837
+ curruntListItem.textContent += nextData.textContent;
838
+ curruntData.child.splice(childIndex, 1, curruntListItem);
839
+ newData.splice(editorStore.selectedBlockIndex, 1, curruntData);
840
+ newData.splice(editorStore.selectedBlockIndex + 1, 1);
841
+ abortEvent();
842
+ editorStore.fn.updateEditorData(newData);
843
+ await nextTick();
844
+ setEvent(childIndex, curruntListItem.id);
845
+ const $targetNode = $target.childNodes[blockLastOffset.nodeIndex];
846
+ if ($targetNode !== void 0) {
847
+ _setCursorPosition($targetNode, blockLastOffset.offset);
848
+ }
849
+ } else if (nextData.type === "list") {
850
+ const nextChildData = nextData.child[0];
851
+ curruntListItem.textContent += nextChildData.textContent;
852
+ curruntData.child.splice(childIndex, 1, curruntListItem);
853
+ nextData.child.splice(0, 1);
854
+ newData.splice(editorStore.selectedBlockIndex, 1, curruntData);
855
+ newData.splice(editorStore.selectedBlockIndex + 1, 1, nextData);
856
+ abortEvent();
857
+ editorStore.fn.updateEditorData(newData);
858
+ await nextTick();
859
+ setEvent(childIndex, curruntListItem.id);
860
+ const $targetNode = $target.childNodes[blockLastOffset.nodeIndex];
861
+ if ($targetNode !== void 0) {
862
+ _setCursorPosition($targetNode, blockLastOffset.offset);
863
+ }
864
+ } else {
865
+ newData.splice(editorStore.selectedBlockIndex + 1, 1);
866
+ editorStore.fn.updateEditorData(newData);
867
+ }
1260
868
  } else {
1261
- hasChild = true;
869
+ const nextListItem = curruntData.child[childIndex + 1];
870
+ const $listItems = $block.querySelectorAll(".de-item-text");
871
+ const $nextListBlock = $listItems[childIndex + 1];
872
+ if (nextListItem !== void 0 && $nextListBlock !== void 0) {
873
+ curruntListItem.textContent += nextListItem.textContent;
874
+ curruntData.child.splice(childIndex, 1, curruntListItem);
875
+ curruntData.child.splice(childIndex + 1, 1);
876
+ newData.splice(editorStore.selectedBlockIndex, 1, curruntData);
877
+ abortEvent();
878
+ editorStore.fn.updateEditorData(newData);
879
+ await nextTick();
880
+ setEvent(childIndex, curruntListItem.id);
881
+ const $targetNode = $target.childNodes[blockLastOffset.nodeIndex];
882
+ if ($targetNode !== void 0) {
883
+ _setCursorPosition($targetNode, blockLastOffset.offset);
884
+ }
885
+ }
1262
886
  }
1263
887
  }
1264
- ____deleteLogic($block, $block);
1265
- } else {
1266
- const lastChild = $block.childNodes[$block.childNodes.length - 1];
1267
- if (cursorData.type === "Caret" && lastChild !== void 0 && lastChild.textContent && cursorData.startOffset === lastChild.textContent.length && (lastChild === cursorData.startNode || lastChild === $target)) {
1268
- event.preventDefault();
1269
- ____deleteLogic($block, $block);
1270
- }
1271
888
  }
1272
889
  }
1273
890
  }
1274
- function ___listBlockDeleteEvent(event, store) {
1275
- if (store.value.cursorData !== null && store.value.controlStatus.$currentBlock !== null && store.value.$body !== null) {
1276
- const cursorData = store.value.cursorData;
1277
- const $listBlock = store.value.controlStatus.$currentBlock;
1278
- const $targetItem = _findContentEditableElement(cursorData.startNode);
1279
- const $liList = $listBlock.querySelectorAll(".de-item");
1280
- let $target = cursorData.startNode;
1281
- let liIdx = -1;
1282
- if ($target.constructor.name === "Text") {
1283
- $target = $target.parentNode;
1284
- }
1285
- for (let i = 0; $liList.length > i; i += 1) {
1286
- if ($liList[i] === $targetItem) {
1287
- liIdx = i;
1288
- break;
1289
- }
1290
- }
1291
- if ($liList.length === 1) {
1292
- if ($targetItem.textContent === "") {
1293
- event.preventDefault();
1294
- ____deleteLogic($targetItem, $listBlock);
1295
- } else {
1296
- const lastChild = $targetItem.childNodes[$targetItem.childNodes.length - 1];
1297
- if (cursorData.type === "Caret" && lastChild !== void 0 && lastChild.textContent && cursorData.startOffset === lastChild.textContent.length && (lastChild === cursorData.startNode || lastChild === $target)) {
1298
- event.preventDefault();
1299
- ____deleteLogic($targetItem, $listBlock);
891
+ export async function _allDataPasteEvent(event, setEvent, abortEvent) {
892
+ event.preventDefault();
893
+ _updateCursorData();
894
+ const editorStore = useEditorStore();
895
+ const newData = JSON.parse(JSON.stringify(editorStore.data));
896
+ const curruntData = newData[editorStore.selectedBlockIndex];
897
+ const textData = await navigator.clipboard.readText();
898
+ const $target = event.target;
899
+ if (editorStore.fn.updateEditorData !== null && editorStore.cursorSelection !== null) {
900
+ if (textData === "") {
901
+ const clipboardItems = await navigator.clipboard.read();
902
+ if (editorStore.fn.uploadImage !== null) {
903
+ const imageItem = clipboardItems[0].types.find((type) => type.startsWith("image/"));
904
+ if (imageItem !== void 0) {
905
+ const blob = await clipboardItems[0].getType(imageItem);
906
+ const file = new File([blob], `${_generateId()}.${imageItem.split("/")[1]}`);
907
+ editorStore.fn.uploadImage([file]);
1300
908
  }
1301
909
  }
1302
910
  } else {
1303
- const $nextLi = $targetItem.nextElementSibling;
1304
- if ($nextLi === null) {
1305
- if ($targetItem.textContent === "") {
1306
- event.preventDefault();
1307
- ____deleteLogic($targetItem, $listBlock);
1308
- } else {
1309
- const lastChild = $targetItem.childNodes[$targetItem.childNodes.length - 1];
1310
- if (cursorData.type === "Caret" && lastChild !== void 0 && lastChild.textContent && cursorData.startOffset === lastChild.textContent.length && (lastChild === cursorData.startNode || lastChild === $target)) {
1311
- event.preventDefault();
1312
- ____deleteLogic($targetItem, $listBlock);
1313
- }
1314
- }
911
+ const dataLine = textData.split("\n");
912
+ const blockData = await _convertMarkdownToEditor(dataLine);
913
+ const firstBlockData = blockData[0];
914
+ if (dataLine.length === 1 && firstBlockData.type === "text") {
915
+ const textNode = document.createTextNode(firstBlockData.textContent);
916
+ abortEvent();
917
+ editorStore.cursorSelection.deleteFromDocument();
918
+ editorStore.cursorSelection.getRangeAt(0).insertNode(textNode);
919
+ $target.dispatchEvent(new Event("input"));
920
+ setEvent();
921
+ _setCursorPosition(textNode, textNode.length);
1315
922
  } else {
1316
- if ($targetItem.textContent === "") {
1317
- event.preventDefault();
1318
- ____deleteLogic($targetItem, $targetItem);
923
+ if (editorStore.selectedBlockIndex > -1) {
924
+ newData.splice(editorStore.selectedBlockIndex + 1, 0, ...blockData);
1319
925
  } else {
1320
- const lastChild = $targetItem.childNodes[$targetItem.childNodes.length - 1];
1321
- if (cursorData.type === "Caret" && lastChild !== void 0 && lastChild.textContent && cursorData.startOffset === lastChild.textContent.length && (lastChild === cursorData.startNode || lastChild === $target)) {
1322
- event.preventDefault();
1323
- ____deleteLogic($targetItem, $targetItem);
1324
- }
926
+ newData.push(...blockData);
1325
927
  }
928
+ abortEvent();
929
+ editorStore.fn.updateEditorData(newData);
930
+ await nextTick();
1326
931
  }
1327
932
  }
1328
933
  }
1329
934
  }
1330
- function ____deleteLogic($targetElement, $block) {
1331
- let $nextBlock = $block.nextElementSibling;
1332
- if ($nextBlock !== null) {
1333
- const { type: nextBlockType } = _getCurrentBlock($nextBlock);
1334
- let nextDelete = false;
1335
- switch (nextBlockType) {
1336
- case "custom":
1337
- case "image":
1338
- case "code":
1339
- nextDelete = true;
1340
- break;
1341
- case "ul":
1342
- case "ol":
1343
- if ($block.tagName !== "LI") {
1344
- const $liList = $nextBlock.querySelectorAll(".de-item");
1345
- $nextBlock = $liList[0];
1346
- }
1347
- break;
1348
- }
1349
- if (nextDelete === false) {
1350
- if ($nextBlock.hasChildNodes() === true) {
1351
- const childHTML = $nextBlock.innerHTML;
1352
- if ($targetElement.hasChildNodes() === true) {
1353
- $targetElement.insertAdjacentHTML("beforeend", childHTML);
1354
- } else {
1355
- $targetElement.innerHTML = childHTML;
1356
- }
1357
- }
1358
- $nextBlock.remove();
935
+ export async function _normalPasteEvent(event, setEvent, abortEvent) {
936
+ event.preventDefault();
937
+ _updateCursorData();
938
+ const editorStore = useEditorStore();
939
+ const textData = await navigator.clipboard.readText();
940
+ const $target = event.target;
941
+ if (textData !== "" && editorStore.cursorSelection !== null && $target !== null) {
942
+ const textNode = document.createTextNode(textData);
943
+ if ($target.classList.contains("de-item-text") === true) {
944
+ editorStore.cursorSelection.deleteFromDocument();
945
+ editorStore.cursorSelection.getRangeAt(0).insertNode(textNode);
946
+ $target.dispatchEvent(new Event("input"));
947
+ _setCursorPosition(textNode, textNode.length);
1359
948
  } else {
1360
- $nextBlock.remove();
949
+ abortEvent();
950
+ editorStore.cursorSelection.deleteFromDocument();
951
+ editorStore.cursorSelection.getRangeAt(0).insertNode(textNode);
952
+ $target.dispatchEvent(new Event("input"));
953
+ setEvent();
954
+ _setCursorPosition(textNode, textNode.length);
1361
955
  }
1362
956
  }
1363
957
  }
1364
- function __spaceEvent(event, store) {
1365
- if (store.value.cursorData !== null && store.value.controlStatus.$currentBlock !== null && store.value.$body !== null) {
1366
- const cursorData = store.value.cursorData;
1367
- const $block = store.value.controlStatus.$currentBlock;
1368
- const $targetItem = _findContentEditableElement(cursorData.startNode);
1369
- if (store.value.controlStatus.currentBlockType === "text" && $targetItem !== null && $targetItem.textContent !== "") {
1370
- switch ($targetItem.textContent) {
1371
- case "#":
1372
- event.preventDefault();
1373
- const $newHeading1Block = _createHeadingBlock(_getDefaultBlockData("heading1"));
1374
- $block.insertAdjacentElement("afterend", $newHeading1Block);
1375
- $newHeading1Block.focus();
1376
- $block.remove();
1377
- break;
1378
- case "##":
1379
- event.preventDefault();
1380
- const $newHeading2Block = _createHeadingBlock(_getDefaultBlockData("heading2"));
1381
- $block.insertAdjacentElement("afterend", $newHeading2Block);
1382
- $newHeading2Block.focus();
1383
- $block.remove();
1384
- break;
1385
- case "###":
1386
- event.preventDefault();
1387
- const $newHeading3Block = _createHeadingBlock(_getDefaultBlockData("heading3"));
1388
- $block.insertAdjacentElement("afterend", $newHeading3Block);
1389
- $newHeading3Block.focus();
1390
- $block.remove();
1391
- break;
1392
- case "-":
1393
- event.preventDefault();
1394
- const $newUlBlock = _createListBlock(_getDefaultBlockData("ul"));
1395
- $block.insertAdjacentElement("afterend", $newUlBlock);
1396
- $newUlBlock.children[0].focus();
1397
- $block.remove();
1398
- break;
1399
- case "1.":
1400
- event.preventDefault();
1401
- const $newOlBlock = _createListBlock(_getDefaultBlockData("ol"));
1402
- $block.insertAdjacentElement("afterend", $newOlBlock);
1403
- $newOlBlock.children[0].focus();
1404
- $block.remove();
1405
- break;
958
+ export async function _convertTextBlockType(event, data, index) {
959
+ const editorStore = useEditorStore();
960
+ const headingReg = new RegExp("^(#{1,3})");
961
+ const orderedReg = new RegExp("^\\d*\\.");
962
+ const unorderedReg = new RegExp("^-");
963
+ if (editorStore.fn.updateEditorData !== null && editorStore.element.body !== null) {
964
+ const newData = JSON.parse(JSON.stringify(editorStore.data));
965
+ switch (true) {
966
+ case headingReg.test(data.textContent):
967
+ event.preventDefault();
968
+ const match = data.textContent.match(headingReg);
969
+ if (match !== null) {
970
+ const level = match[0].length;
971
+ newData.splice(index, 1, _createHeadingBlockData(level, data.textContent.replace(headingReg, "")));
972
+ }
973
+ break;
974
+ case orderedReg.test(data.textContent):
975
+ event.preventDefault();
976
+ newData.splice(index, 1, _createListBlockData("ol", [_createListBlockChildData(data.textContent.replace(orderedReg, ""))]));
977
+ break;
978
+ case unorderedReg.test(data.textContent):
979
+ event.preventDefault();
980
+ newData.splice(index, 1, _createListBlockData("ul", [_createListBlockChildData(data.textContent.replace(unorderedReg, ""))]));
981
+ break;
982
+ }
983
+ editorStore.fn.updateEditorData(newData);
984
+ await nextTick();
985
+ const $block = editorStore.element.body.children[index];
986
+ if ($block !== void 0) {
987
+ const $target = _findEditableElement($block, "down");
988
+ if ($target !== null) {
989
+ $target.focus();
990
+ $target.dispatchEvent(new Event("input"));
1406
991
  }
1407
992
  }
1408
993
  }
1409
994
  }
1410
- function _moveToBlockEvent(event, store, keyType) {
1411
- if (store.value.cursorData !== null && store.value.controlStatus.$currentBlock !== null) {
1412
- const cursorData = store.value.cursorData;
1413
- const $editableElement = _findContentEditableElement(cursorData.startNode);
1414
- const $block = store.value.controlStatus.$currentBlock;
1415
- if ($editableElement !== null && $block !== null) {
1416
- const $brList = $editableElement.querySelectorAll("br");
1417
- let $targetElement = null;
1418
- let suitable = false;
1419
- if ($brList.length !== 0) {
1420
- if (keyType === "up") {
1421
- suitable = __isCursorFirstLine(cursorData.startNode, $brList[0]);
1422
- } else {
1423
- suitable = __isCursorLastLine(cursorData.startNode, $brList[$brList.length - 1]);
1424
- }
1425
- } else {
1426
- suitable = true;
1427
- }
1428
- if (suitable === true) {
1429
- switch (store.value.controlStatus.currentBlockType) {
1430
- case "code":
1431
- break;
1432
- case "ol":
1433
- case "ul":
1434
- if (keyType === "up") {
1435
- $targetElement = $editableElement.previousElementSibling;
1436
- if ($targetElement === null) {
1437
- $targetElement = $block.previousElementSibling;
1438
- }
1439
- } else {
1440
- $targetElement = $editableElement.nextElementSibling;
1441
- if ($targetElement === null) {
1442
- $targetElement = $block.nextElementSibling;
1443
- }
1444
- }
1445
- break;
1446
- default:
1447
- if (keyType === "up") {
1448
- $targetElement = $block.previousElementSibling;
1449
- } else {
1450
- $targetElement = $block.nextElementSibling;
1451
- }
1452
- }
1453
- if ($targetElement !== null) {
1454
- const { type, $element } = _getCurrentBlock($targetElement);
1455
- switch (type) {
1456
- case "image":
1457
- if ($element !== null) {
1458
- const $caption = $element.querySelector(".de-caption");
1459
- if ($caption !== null) {
1460
- $targetElement = $caption;
1461
- }
1462
- }
1463
- break;
1464
- case "ol":
1465
- case "ul":
1466
- if ($targetElement.tagName !== "LI" && $element !== null) {
1467
- const $childList = $element.querySelectorAll(".de-item");
1468
- if (keyType === "up") {
1469
- $targetElement = $childList[$childList.length - 1];
1470
- } else {
1471
- $targetElement = $childList[0];
1472
- }
1473
- }
1474
- break;
1475
- }
1476
- _setCursor($targetElement, 0);
1477
- }
995
+ export async function _convertTextBlockToCodeBlock(event, data, index) {
996
+ const editorStore = useEditorStore();
997
+ const regexp = new RegExp("^``");
998
+ if (regexp.test(data.textContent) === true && editorStore.fn.updateEditorData !== null && editorStore.element.body !== null) {
999
+ event.preventDefault();
1000
+ const newData = JSON.parse(JSON.stringify(editorStore.data));
1001
+ newData.splice(index, 1, _createCodeBlockData());
1002
+ editorStore.fn.updateEditorData(newData);
1003
+ await nextTick();
1004
+ const $block = editorStore.element.body.children[index];
1005
+ if ($block !== void 0) {
1006
+ const $target = _findEditableElement($block, "down");
1007
+ if ($target !== null) {
1008
+ $target.focus();
1009
+ $target.dispatchEvent(new Event("input"));
1478
1010
  }
1479
1011
  }
1480
1012
  }
1481
1013
  }
1482
- function __isCursorFirstLine(node, $br) {
1483
- let suitable = false;
1484
- let $previousNode = $br.previousSibling;
1485
- if ($previousNode !== null) {
1486
- if ($previousNode === node) {
1487
- suitable = true;
1488
- } else {
1489
- suitable = __isCursorFirstLine(node, $previousNode);
1014
+ export async function _convertTextBlockToDividerBlock(event, data, index) {
1015
+ const editorStore = useEditorStore();
1016
+ const regexp = new RegExp("^--");
1017
+ if (regexp.test(data.textContent) === true && editorStore.fn.updateEditorData !== null && editorStore.element.body !== null) {
1018
+ event.preventDefault();
1019
+ const newData = JSON.parse(JSON.stringify(editorStore.data));
1020
+ data.textContent = "";
1021
+ newData.splice(index, 0, _createDividerBlockData());
1022
+ newData.splice(index + 1, 1, data);
1023
+ editorStore.fn.updateEditorData(newData);
1024
+ await nextTick();
1025
+ const $block = editorStore.element.body.children[index + 1];
1026
+ if ($block !== void 0) {
1027
+ const $target = _findEditableElement($block, "down");
1028
+ if ($target !== null) {
1029
+ $target.innerHTML = "";
1030
+ $target.focus();
1031
+ $target.dispatchEvent(new Event("input"));
1032
+ }
1490
1033
  }
1491
1034
  }
1492
- return suitable;
1493
1035
  }
1494
- function __isCursorLastLine(node, $br) {
1495
- let suitable = false;
1496
- let $nextNode = $br.nextSibling;
1497
- if ($nextNode !== null) {
1498
- if ($nextNode === node) {
1499
- suitable = true;
1500
- } else {
1501
- suitable = __isCursorLastLine(node, $nextNode);
1036
+ export async function _convertHeadingBlockType(event, data, index, setEvent, abortEvent) {
1037
+ const editorStore = useEditorStore();
1038
+ const regexp = new RegExp("^(#{1,3})");
1039
+ if (regexp.test(data.textContent) === true && editorStore.fn.updateEditorData !== null && editorStore.element.body !== null) {
1040
+ event.preventDefault();
1041
+ const newData = JSON.parse(JSON.stringify(editorStore.data));
1042
+ const match = regexp.exec(data.textContent);
1043
+ if (match !== null) {
1044
+ const level = match[0].length;
1045
+ data.level = level;
1046
+ data.textContent = data.textContent.replace(regexp, "");
1047
+ newData.splice(index, 1, data);
1048
+ abortEvent();
1049
+ editorStore.fn.updateEditorData(newData);
1050
+ await nextTick();
1051
+ setEvent();
1052
+ const $block = editorStore.element.body.children[index];
1053
+ if ($block !== void 0) {
1054
+ $block.focus();
1055
+ $block.dispatchEvent(new Event("input"));
1056
+ }
1502
1057
  }
1503
1058
  }
1504
- return suitable;
1505
1059
  }
1506
- export function _hotKeyEvent(event, store) {
1507
- const isControlKeyActive = event.ctrlKey || event.metaKey;
1508
- if (isControlKeyActive === true) {
1060
+ export function _hotKeyEvent(event) {
1061
+ const isControllKeyActive = event.ctrlKey || event.metaKey;
1062
+ if (isControllKeyActive === true) {
1509
1063
  switch (event.key) {
1510
1064
  case "b":
1511
1065
  event.preventDefault();
1512
- _setDecoration("de-bold", store);
1066
+ _setDecoration("de-bold");
1513
1067
  break;
1514
1068
  case "i":
1515
1069
  event.preventDefault();
1516
- _setDecoration("de-italic", store);
1070
+ _setDecoration("de-italic");
1517
1071
  break;
1518
1072
  case "u":
1519
1073
  event.preventDefault();
1520
- _setDecoration("de-underline", store);
1074
+ _setDecoration("de-underline");
1521
1075
  break;
1522
1076
  case "s":
1523
1077
  if (event.shiftKey === true) {
1524
1078
  event.preventDefault();
1525
- _setDecoration("de-strikethrough", store);
1079
+ _setDecoration("de-strikethrough");
1526
1080
  }
1527
1081
  break;
1528
1082
  case "c":
1529
1083
  if (event.shiftKey === true) {
1530
1084
  event.preventDefault();
1531
- _setDecoration("de-code", store);
1085
+ _setDecoration("de-code");
1532
1086
  }
1533
1087
  break;
1534
1088
  }
1535
1089
  }
1536
1090
  }
1537
- function __backtickEvent(event, store) {
1538
- if (store.value.controlStatus.$currentBlock !== null && store.value.controlStatus.currentBlockType === "text") {
1539
- const $block = store.value.controlStatus.$currentBlock;
1540
- if ($block.textContent === "``") {
1541
- event.preventDefault();
1542
- const $newBlock = _createCodeBlock(_getDefaultBlockData("code"), store);
1543
- $block.insertAdjacentElement("afterend", $newBlock);
1544
- $block.remove();
1545
- const $code = $newBlock.querySelector(".de-code-content");
1546
- if ($code !== null) {
1547
- _setCursor($code, 0);
1548
- }
1549
- }
1550
- }
1551
- }
1552
- let contentKeyupEvent;
1553
- export function _contentKeyupEvent(event, store) {
1554
- _updateCurrentBlock(event, store);
1555
- _updateCursorData(store);
1556
- __checkBlock(store);
1557
- clearTimeout(contentKeyupEvent);
1558
- contentKeyupEvent = setTimeout(() => {
1559
- _updateModelData(store);
1560
- }, 250);
1561
- }
1562
- function __checkBlock(store) {
1563
- if (store.value.$body !== null) {
1564
- const blockList = store.value.$body.querySelectorAll(".de-block");
1565
- blockList.forEach(($block) => {
1566
- const { type } = _getCurrentBlock($block);
1567
- switch (type) {
1568
- case "ol":
1569
- case "ul":
1570
- if ($block.hasChildNodes() === false) {
1571
- $block.remove();
1572
- }
1573
- break;
1574
- case "text":
1575
- case "heading":
1576
- if ($block.textContent === "" && $block.hasChildNodes() === true) {
1577
- const textNodeType = $block.childNodes[0].constructor.name;
1578
- if (textNodeType === "HTMLBRElement") {
1579
- $block.innerHTML = "";
1580
- }
1581
- }
1582
- break;
1583
- }
1584
- });
1585
- }
1586
- }