dragon-editor 3.4.5 → 3.5.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +9 -15
- package/dist/module.d.mts +5 -0
- package/dist/module.json +8 -1
- package/dist/module.mjs +6 -4
- package/dist/runtime/components/DragonEditor.vue +252 -734
- package/dist/runtime/components/DragonEditorViewer.vue +59 -45
- package/dist/runtime/scss/editor.css +83 -34
- package/dist/runtime/scss/viewer.css +31 -4
- package/dist/runtime/type.d.ts +89 -42
- package/dist/runtime/utils/event/block.d.ts +0 -0
- package/dist/runtime/utils/event/block.js +78 -0
- package/dist/runtime/utils/event/cursor.d.ts +0 -0
- package/dist/runtime/utils/{cursor.mjs → event/cursor.js} +4 -16
- package/dist/runtime/utils/event/data.d.ts +0 -0
- package/dist/runtime/utils/event/data.js +342 -0
- package/dist/runtime/utils/event/index.d.ts +8 -0
- package/dist/runtime/utils/event/index.js +8 -0
- package/dist/runtime/utils/event/keyboard.d.ts +0 -0
- package/dist/runtime/utils/event/keyboard.js +1368 -0
- package/dist/runtime/utils/event/mouse.d.ts +0 -0
- package/dist/runtime/utils/event/mouse.js +70 -0
- package/dist/runtime/utils/event/scroll.d.ts +0 -0
- package/dist/runtime/utils/event/scroll.js +29 -0
- package/dist/runtime/utils/event/touch.d.ts +0 -0
- package/dist/runtime/utils/event/touch.js +10 -0
- package/dist/runtime/utils/event/window.d.ts +0 -0
- package/dist/runtime/utils/event/window.js +32 -0
- package/dist/runtime/utils/layout/block.d.ts +0 -0
- package/dist/runtime/utils/layout/block.js +105 -0
- package/dist/runtime/utils/layout/body.d.ts +0 -0
- package/dist/runtime/utils/layout/body.js +22 -0
- package/dist/runtime/utils/layout/controlbar.d.ts +0 -0
- package/dist/runtime/utils/layout/controlbar.js +99 -0
- package/dist/runtime/utils/layout/icon.d.ts +0 -0
- package/dist/runtime/utils/layout/icon.js +156 -0
- package/dist/runtime/utils/layout/index.d.ts +5 -0
- package/dist/runtime/utils/layout/index.js +5 -0
- package/dist/runtime/utils/layout/menuBar.d.ts +0 -0
- package/dist/runtime/utils/layout/menuBar.js +358 -0
- package/dist/runtime/utils/node/block.d.ts +0 -0
- package/dist/runtime/utils/node/block.js +235 -0
- package/dist/runtime/utils/{element.d.ts → node/element.d.ts} +1 -0
- package/dist/runtime/utils/{element.mjs → node/element.js} +11 -0
- package/dist/runtime/utils/node/index.d.ts +2 -0
- package/dist/runtime/utils/node/index.js +2 -0
- package/dist/runtime/utils/style/anchor.d.ts +0 -0
- package/dist/runtime/utils/style/anchor.js +240 -0
- package/dist/runtime/utils/style/decoration.d.ts +0 -0
- package/dist/runtime/utils/style/decoration.js +378 -0
- package/dist/runtime/utils/style/index.d.ts +2 -0
- package/dist/runtime/utils/style/index.js +2 -0
- package/dist/types.d.mts +7 -0
- package/dist/types.d.ts +3 -3
- package/package.json +15 -16
- package/dist/runtime/store.d.ts +0 -11
- package/dist/runtime/store.mjs +0 -51
- package/dist/runtime/utils/block.d.ts +0 -13
- package/dist/runtime/utils/block.mjs +0 -144
- package/dist/runtime/utils/content.d.ts +0 -2
- package/dist/runtime/utils/content.mjs +0 -19
- package/dist/runtime/utils/controlBar.d.ts +0 -9
- package/dist/runtime/utils/controlBar.mjs +0 -172
- package/dist/runtime/utils/convertor.d.ts +0 -3
- package/dist/runtime/utils/convertor.mjs +0 -138
- package/dist/runtime/utils/cursor.d.ts +0 -6
- package/dist/runtime/utils/keyboardEvent.d.ts +0 -10
- package/dist/runtime/utils/keyboardEvent.mjs +0 -979
- package/dist/runtime/utils/style.d.ts +0 -5
- package/dist/runtime/utils/style.mjs +0 -617
- /package/dist/runtime/{plugin.mjs → plugin.js} +0 -0
|
@@ -0,0 +1,1368 @@
|
|
|
1
|
+
import { _updateModelData, _updateCursorData, _setCursor, _sortingCursorDataOnElement, _generateId } from "./index.js";
|
|
2
|
+
import { _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 (store.value.eventStatus.preComposing === false && event.isComposing === false) {
|
|
11
|
+
__enterEvent(event, store);
|
|
12
|
+
} else {
|
|
13
|
+
if (store.value.eventStatus.keyboardEnterCount % 2 === 0) {
|
|
14
|
+
__enterEvent(event, store);
|
|
15
|
+
} else {
|
|
16
|
+
event.preventDefault();
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
store.value.eventStatus.keyboardEnterCount += 1;
|
|
20
|
+
break;
|
|
21
|
+
case "Backspace":
|
|
22
|
+
__backspaceEvent(event, store);
|
|
23
|
+
break;
|
|
24
|
+
case "Delete":
|
|
25
|
+
__deleteEvent(event, store);
|
|
26
|
+
break;
|
|
27
|
+
case "Tab":
|
|
28
|
+
__tabEvent(event, store);
|
|
29
|
+
break;
|
|
30
|
+
case " ":
|
|
31
|
+
__spaceEvent(event, store);
|
|
32
|
+
break;
|
|
33
|
+
case "ArrowUp":
|
|
34
|
+
_moveToBlockEvent(event, store, "up");
|
|
35
|
+
break;
|
|
36
|
+
case "ArrowDown":
|
|
37
|
+
_moveToBlockEvent(event, store, "down");
|
|
38
|
+
break;
|
|
39
|
+
case "`":
|
|
40
|
+
__backtickEvent(event, store);
|
|
41
|
+
break;
|
|
42
|
+
}
|
|
43
|
+
_hotKeyEvent(event, store);
|
|
44
|
+
store.value.eventStatus.preComposing = event.isComposing;
|
|
45
|
+
}
|
|
46
|
+
export async function _contentPasteEvent(event, store) {
|
|
47
|
+
event.preventDefault();
|
|
48
|
+
const text = await navigator.clipboard.readText();
|
|
49
|
+
const $block = event.target.closest(".de-block");
|
|
50
|
+
if ($block !== null) {
|
|
51
|
+
if (text === "") {
|
|
52
|
+
const clipboardItems = await navigator.clipboard.read();
|
|
53
|
+
const imageItem = clipboardItems[0].types.find((type) => type.startsWith("image/"));
|
|
54
|
+
if (imageItem !== void 0) {
|
|
55
|
+
const blob = await clipboardItems[0].getType(imageItem);
|
|
56
|
+
const file = new File([blob], `${_generateId()}.${imageItem.split("/")[1]}`);
|
|
57
|
+
store.value.emit("uploadImageEvent", file);
|
|
58
|
+
}
|
|
59
|
+
} else {
|
|
60
|
+
const selection = window.getSelection();
|
|
61
|
+
const textNode = document.createTextNode(text);
|
|
62
|
+
selection.deleteFromDocument();
|
|
63
|
+
selection.getRangeAt(0).insertNode(textNode);
|
|
64
|
+
_setCursor(textNode, textNode.length);
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
function __enterEvent(event, store) {
|
|
69
|
+
if (event.shiftKey === true) {
|
|
70
|
+
switch (store.value.controlStatus.currentBlockType) {
|
|
71
|
+
case "image":
|
|
72
|
+
event.preventDefault();
|
|
73
|
+
break;
|
|
74
|
+
case "code":
|
|
75
|
+
__codeBlockShiftEnterEvent(event, store);
|
|
76
|
+
break;
|
|
77
|
+
case "ol":
|
|
78
|
+
case "ul":
|
|
79
|
+
__listBlockShiftEnterEvent(event, store);
|
|
80
|
+
break;
|
|
81
|
+
default:
|
|
82
|
+
__defaultBlockShiftEnterEvent(event, store);
|
|
83
|
+
}
|
|
84
|
+
} else {
|
|
85
|
+
switch (store.value.controlStatus.currentBlockType) {
|
|
86
|
+
case "image":
|
|
87
|
+
event.preventDefault();
|
|
88
|
+
if (store.value.controlStatus.$currentBlock !== null) {
|
|
89
|
+
const $block = store.value.controlStatus.$currentBlock;
|
|
90
|
+
const $newTextBlock = _createTextBlock(_getDefaultBlockData("text"));
|
|
91
|
+
$block.insertAdjacentElement("afterend", $newTextBlock);
|
|
92
|
+
$newTextBlock.focus();
|
|
93
|
+
}
|
|
94
|
+
break;
|
|
95
|
+
case "code":
|
|
96
|
+
break;
|
|
97
|
+
case "ol":
|
|
98
|
+
case "ul":
|
|
99
|
+
__listBlockEnterEvent(event, store);
|
|
100
|
+
break;
|
|
101
|
+
default:
|
|
102
|
+
__defaultBlockEnterEvent(event, store);
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
function __defaultBlockEnterEvent(event, store) {
|
|
107
|
+
event.preventDefault();
|
|
108
|
+
if (store.value.cursorData !== null && store.value.controlStatus.$currentBlock !== null) {
|
|
109
|
+
const cursorData = store.value.cursorData;
|
|
110
|
+
const $block = store.value.controlStatus.$currentBlock;
|
|
111
|
+
if (store.value.cursorData.type === "Caret") {
|
|
112
|
+
if ($block.textContent === "") {
|
|
113
|
+
if ($block.hasChildNodes() === false) {
|
|
114
|
+
const $newTextBlock = _createTextBlock(_getDefaultBlockData("text"));
|
|
115
|
+
$block.insertAdjacentElement("afterend", $newTextBlock);
|
|
116
|
+
$newTextBlock.focus();
|
|
117
|
+
} else {
|
|
118
|
+
const brList = $block.querySelectorAll("br");
|
|
119
|
+
if (brList.length === 1) {
|
|
120
|
+
const $newTextBlock = _createTextBlock(_getDefaultBlockData("text"));
|
|
121
|
+
$block.insertAdjacentElement("afterend", $newTextBlock);
|
|
122
|
+
$newTextBlock.focus();
|
|
123
|
+
} else {
|
|
124
|
+
const $nextTextBlock = _createTextBlock(_getDefaultBlockData("text"));
|
|
125
|
+
let preStructure = [];
|
|
126
|
+
let nextStructure = [];
|
|
127
|
+
brList.forEach((_, i) => {
|
|
128
|
+
const $br = document.createElement("br");
|
|
129
|
+
if (cursorData.startOffset < i) {
|
|
130
|
+
preStructure.push($br);
|
|
131
|
+
} else {
|
|
132
|
+
nextStructure.push($br);
|
|
133
|
+
}
|
|
134
|
+
});
|
|
135
|
+
$block.replaceChildren(...preStructure);
|
|
136
|
+
$block.insertAdjacentElement("afterend", $nextTextBlock);
|
|
137
|
+
if (nextStructure.length === 0) {
|
|
138
|
+
$nextTextBlock.focus();
|
|
139
|
+
} else {
|
|
140
|
+
if (nextStructure.length === 1) {
|
|
141
|
+
nextStructure.push(document.createElement("br"));
|
|
142
|
+
}
|
|
143
|
+
$nextTextBlock.replaceChildren(...nextStructure);
|
|
144
|
+
_setCursor(nextStructure[0], 0);
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
} else {
|
|
149
|
+
const childNodeList = Array.from($block.childNodes);
|
|
150
|
+
const preStructure = [];
|
|
151
|
+
const nextStructure = [];
|
|
152
|
+
const $targetNode = _getParentElementIfNodeIsText(store.value.cursorData.startNode, $block);
|
|
153
|
+
let nodeIdx = -1;
|
|
154
|
+
for (let i = 0; childNodeList.length > i; i += 1) {
|
|
155
|
+
if (childNodeList[i] === $targetNode) {
|
|
156
|
+
nodeIdx = i;
|
|
157
|
+
break;
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
childNodeList.forEach((node, i) => {
|
|
161
|
+
if (nodeIdx < i) {
|
|
162
|
+
nextStructure.push(node);
|
|
163
|
+
} else if (nodeIdx > i) {
|
|
164
|
+
preStructure.push(node);
|
|
165
|
+
} else if (nodeIdx === i) {
|
|
166
|
+
if (node.constructor.name === "Text") {
|
|
167
|
+
const text = node.textContent;
|
|
168
|
+
const preText = document.createTextNode(text.slice(0, cursorData.startOffset));
|
|
169
|
+
const nextText = document.createTextNode(text.slice(cursorData.startOffset));
|
|
170
|
+
preStructure.push(preText);
|
|
171
|
+
nextStructure.push(nextText);
|
|
172
|
+
} else {
|
|
173
|
+
const originalClassList = Array.from(node.classList);
|
|
174
|
+
const text = node.textContent;
|
|
175
|
+
const preSpan = document.createElement("span");
|
|
176
|
+
const nextSpan = document.createElement("span");
|
|
177
|
+
const nextText = text.slice(cursorData.startOffset);
|
|
178
|
+
preSpan.classList.add(...originalClassList);
|
|
179
|
+
preSpan.textContent = text.slice(0, cursorData.startOffset);
|
|
180
|
+
preStructure.push(preSpan);
|
|
181
|
+
if (nextText !== "") {
|
|
182
|
+
nextSpan.classList.add(...originalClassList);
|
|
183
|
+
nextSpan.textContent = nextText;
|
|
184
|
+
nextStructure.push(nextSpan);
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
});
|
|
189
|
+
const $nextTextBlock = _createTextBlock(_getDefaultBlockData("text"));
|
|
190
|
+
$block.insertAdjacentElement("afterend", $nextTextBlock);
|
|
191
|
+
$block.replaceChildren(...preStructure);
|
|
192
|
+
$nextTextBlock.replaceChildren(...nextStructure);
|
|
193
|
+
if (nextStructure.length === 0) {
|
|
194
|
+
$nextTextBlock.focus();
|
|
195
|
+
} else {
|
|
196
|
+
_setCursor($nextTextBlock.childNodes[0], 0);
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
} else {
|
|
200
|
+
const childNodeList = $block.childNodes;
|
|
201
|
+
const srotingCursorData = _sortingCursorDataOnElement(cursorData, $block);
|
|
202
|
+
const preStructure = [];
|
|
203
|
+
const nextStructure = [];
|
|
204
|
+
if (srotingCursorData.startNodeIdx === srotingCursorData.endNodeIdx) {
|
|
205
|
+
childNodeList.forEach((node, i) => {
|
|
206
|
+
if (srotingCursorData.startNodeIdx > i) {
|
|
207
|
+
preStructure.push(node);
|
|
208
|
+
} else if (srotingCursorData.endNodeIdx < i) {
|
|
209
|
+
nextStructure.push(node);
|
|
210
|
+
} else if (srotingCursorData.startNodeIdx === i) {
|
|
211
|
+
if (node.constructor.name === "Text") {
|
|
212
|
+
const preText = node.textContent.slice(0, srotingCursorData.startOffset);
|
|
213
|
+
const nextText = node.textContent.slice(srotingCursorData.endOffset);
|
|
214
|
+
if (preText !== "") {
|
|
215
|
+
const textNode = document.createTextNode(preText);
|
|
216
|
+
preStructure.push(textNode);
|
|
217
|
+
}
|
|
218
|
+
if (nextText !== "") {
|
|
219
|
+
const textNode = document.createTextNode(nextText);
|
|
220
|
+
nextStructure.push(textNode);
|
|
221
|
+
}
|
|
222
|
+
} else {
|
|
223
|
+
const originalClassList = Array.from(node.classList);
|
|
224
|
+
const preText = node.textContent.slice(0, srotingCursorData.startOffset);
|
|
225
|
+
const nextText = node.textContent.slice(srotingCursorData.endOffset);
|
|
226
|
+
if (preText !== "") {
|
|
227
|
+
const $span = document.createElement("span");
|
|
228
|
+
$span.classList.add(...originalClassList);
|
|
229
|
+
$span.textContent = preText;
|
|
230
|
+
preStructure.push($span);
|
|
231
|
+
}
|
|
232
|
+
if (nextText !== "") {
|
|
233
|
+
const $span = document.createElement("span");
|
|
234
|
+
$span.classList.add(...originalClassList);
|
|
235
|
+
$span.textContent = nextText;
|
|
236
|
+
nextStructure.push($span);
|
|
237
|
+
}
|
|
238
|
+
}
|
|
239
|
+
}
|
|
240
|
+
});
|
|
241
|
+
} else {
|
|
242
|
+
childNodeList.forEach((node, i) => {
|
|
243
|
+
if (srotingCursorData.startNodeIdx > i) {
|
|
244
|
+
preStructure.push(node);
|
|
245
|
+
} else if (srotingCursorData.startNodeIdx === i) {
|
|
246
|
+
if (node.constructor.name === "Text") {
|
|
247
|
+
const text = node.textContent.slice(0, srotingCursorData.startOffset);
|
|
248
|
+
if (text !== "") {
|
|
249
|
+
const textNode = document.createTextNode(text);
|
|
250
|
+
preStructure.push(textNode);
|
|
251
|
+
}
|
|
252
|
+
} else {
|
|
253
|
+
const originalClassList = Array.from(node.classList);
|
|
254
|
+
const text = node.textContent.slice(0, srotingCursorData.startOffset);
|
|
255
|
+
if (text !== "") {
|
|
256
|
+
const $span = document.createElement("span");
|
|
257
|
+
$span.classList.add(...originalClassList);
|
|
258
|
+
$span.textContent = text;
|
|
259
|
+
preStructure.push($span);
|
|
260
|
+
}
|
|
261
|
+
}
|
|
262
|
+
}
|
|
263
|
+
if (srotingCursorData.endNodeIdx < i) {
|
|
264
|
+
nextStructure.push(node);
|
|
265
|
+
} else if (srotingCursorData.endNodeIdx === i) {
|
|
266
|
+
if (node.constructor.name === "Text") {
|
|
267
|
+
const text = node.textContent.slice(srotingCursorData.endOffset);
|
|
268
|
+
if (text !== "") {
|
|
269
|
+
const textNode = document.createTextNode(text);
|
|
270
|
+
nextStructure.push(textNode);
|
|
271
|
+
}
|
|
272
|
+
} else {
|
|
273
|
+
const originalClassList = Array.from(node.classList);
|
|
274
|
+
const text = node.textContent.slice(srotingCursorData.endOffset);
|
|
275
|
+
const $span = document.createElement("span");
|
|
276
|
+
if (text !== "") {
|
|
277
|
+
$span.classList.add(...originalClassList);
|
|
278
|
+
$span.textContent = text;
|
|
279
|
+
nextStructure.push($span);
|
|
280
|
+
}
|
|
281
|
+
}
|
|
282
|
+
}
|
|
283
|
+
});
|
|
284
|
+
const $nextBlock = _createTextBlock(_getDefaultBlockData("text"));
|
|
285
|
+
$block.insertAdjacentElement("afterend", $nextBlock);
|
|
286
|
+
$block.replaceChildren(...preStructure);
|
|
287
|
+
$nextBlock.replaceChildren(...nextStructure);
|
|
288
|
+
if (nextStructure.length === 0) {
|
|
289
|
+
$nextBlock.focus();
|
|
290
|
+
} else {
|
|
291
|
+
_setCursor($nextBlock.childNodes[0], 0);
|
|
292
|
+
}
|
|
293
|
+
}
|
|
294
|
+
}
|
|
295
|
+
} else {
|
|
296
|
+
console.error("[Dragon Editor] : Something wrong.");
|
|
297
|
+
}
|
|
298
|
+
}
|
|
299
|
+
function __defaultBlockShiftEnterEvent(event, store) {
|
|
300
|
+
event.preventDefault();
|
|
301
|
+
if (store.value.cursorData !== null && store.value.controlStatus.$currentBlock !== null) {
|
|
302
|
+
const cursorData = store.value.cursorData;
|
|
303
|
+
const $block = store.value.controlStatus.$currentBlock;
|
|
304
|
+
if (cursorData.type === "Caret") {
|
|
305
|
+
if ($block.textContent === "") {
|
|
306
|
+
if ($block.hasChildNodes() === false) {
|
|
307
|
+
$block.insertAdjacentHTML("beforeend", "<br><br>");
|
|
308
|
+
_setCursor($block.childNodes[1], 0);
|
|
309
|
+
} else {
|
|
310
|
+
const $br = document.createElement("br");
|
|
311
|
+
$block.insertAdjacentElement("beforeend", $br);
|
|
312
|
+
_setCursor($br, 0);
|
|
313
|
+
}
|
|
314
|
+
} else {
|
|
315
|
+
const childList = $block.childNodes;
|
|
316
|
+
let targetIdx = -1;
|
|
317
|
+
let structure = "";
|
|
318
|
+
let $target = cursorData.startNode;
|
|
319
|
+
if ($target.constructor.name === "Text") {
|
|
320
|
+
if ($target.parentNode !== $block) {
|
|
321
|
+
$target = $target.parentNode;
|
|
322
|
+
}
|
|
323
|
+
}
|
|
324
|
+
if ($block === $target) {
|
|
325
|
+
$target = $block.childNodes[cursorData.startOffset];
|
|
326
|
+
}
|
|
327
|
+
for (let i = 0; childList.length > i; i += 1) {
|
|
328
|
+
if (childList[i] === $target) {
|
|
329
|
+
targetIdx = i;
|
|
330
|
+
break;
|
|
331
|
+
}
|
|
332
|
+
}
|
|
333
|
+
let currentIdx = targetIdx;
|
|
334
|
+
childList.forEach((child, i) => {
|
|
335
|
+
if (i === targetIdx) {
|
|
336
|
+
const constructorName = child.constructor.name;
|
|
337
|
+
if (constructorName === "Text") {
|
|
338
|
+
structure += child.textContent.slice(0, cursorData.startOffset) + "<br>" + child.textContent.slice(cursorData.endOffset);
|
|
339
|
+
if (child.nextSibling === null) {
|
|
340
|
+
if (child.textContent.slice(cursorData.endOffset) === "") {
|
|
341
|
+
structure += `<br>`;
|
|
342
|
+
}
|
|
343
|
+
if (child.textContent.slice(0, cursorData.startOffset) === "") {
|
|
344
|
+
currentIdx -= 1;
|
|
345
|
+
}
|
|
346
|
+
} else {
|
|
347
|
+
if (child.textContent.slice(cursorData.endOffset) !== "" && child.textContent.slice(0, cursorData.startOffset) === "") {
|
|
348
|
+
currentIdx -= 1;
|
|
349
|
+
}
|
|
350
|
+
}
|
|
351
|
+
currentIdx += 1;
|
|
352
|
+
} else {
|
|
353
|
+
if (constructorName === "HTMLBRElement") {
|
|
354
|
+
structure += `<br><br>`;
|
|
355
|
+
} else {
|
|
356
|
+
structure += `<span class="${Array.from(child.classList).join(" ")}">${child.textContent.slice(0, cursorData.startOffset)}</span>`;
|
|
357
|
+
structure += `<br>`;
|
|
358
|
+
structure += `<span class="${Array.from(child.classList).join(" ")}">${child.textContent.slice(cursorData.startOffset)}</span>`;
|
|
359
|
+
currentIdx += 1;
|
|
360
|
+
}
|
|
361
|
+
}
|
|
362
|
+
} else {
|
|
363
|
+
if (child.constructor.name === "Text") {
|
|
364
|
+
structure += child.textContent;
|
|
365
|
+
} else {
|
|
366
|
+
structure += child.outerHTML;
|
|
367
|
+
}
|
|
368
|
+
}
|
|
369
|
+
});
|
|
370
|
+
$block.innerHTML = structure;
|
|
371
|
+
if ($block.childNodes[currentIdx + 1] === void 0) {
|
|
372
|
+
$block.insertAdjacentHTML("beforeend", "<br>");
|
|
373
|
+
}
|
|
374
|
+
_setCursor($block.childNodes[currentIdx + 1], 0);
|
|
375
|
+
}
|
|
376
|
+
} else {
|
|
377
|
+
const childNodeList = $block.childNodes;
|
|
378
|
+
const newCursorData = _sortingCursorDataOnElement(cursorData, $block);
|
|
379
|
+
let structure = "";
|
|
380
|
+
childNodeList.forEach((node, i) => {
|
|
381
|
+
if (newCursorData.startNodeIdx > i) {
|
|
382
|
+
if (node.constructor.name === "Text") {
|
|
383
|
+
structure += node.textContent;
|
|
384
|
+
} else {
|
|
385
|
+
structure += node.outerHTML;
|
|
386
|
+
}
|
|
387
|
+
} else if (newCursorData.startNodeIdx === i) {
|
|
388
|
+
if (node.constructor.name === "Text") {
|
|
389
|
+
structure += node.textContent.slice(0, newCursorData.startOffset);
|
|
390
|
+
structure += `<br>`;
|
|
391
|
+
} else {
|
|
392
|
+
if (node.tagName === "BR") {
|
|
393
|
+
structure += `<br>`;
|
|
394
|
+
} else {
|
|
395
|
+
const originalClassList = Array.from(node.classList);
|
|
396
|
+
const text = node.textContent;
|
|
397
|
+
structure += `<span class="${originalClassList.join(" ")}">${text.slice(0, newCursorData.startOffset)}</span><br>`;
|
|
398
|
+
}
|
|
399
|
+
}
|
|
400
|
+
if (childNodeList.length === i) {
|
|
401
|
+
structure += `<br>`;
|
|
402
|
+
}
|
|
403
|
+
}
|
|
404
|
+
if (newCursorData.endNodeIdx < i) {
|
|
405
|
+
if (node.constructor.name === "Text") {
|
|
406
|
+
structure += node.textContent;
|
|
407
|
+
} else {
|
|
408
|
+
structure += node.outerHTML;
|
|
409
|
+
}
|
|
410
|
+
} else if (newCursorData.endNodeIdx === i) {
|
|
411
|
+
if (node.constructor.name === "Text") {
|
|
412
|
+
structure += node.textContent.slice(newCursorData.endOffset);
|
|
413
|
+
} else {
|
|
414
|
+
if (node.tagName === "BR") {
|
|
415
|
+
structure += `<br>`;
|
|
416
|
+
} else {
|
|
417
|
+
const originalClassList = Array.from(node.classList);
|
|
418
|
+
const text = node.textContent;
|
|
419
|
+
structure += `<span class="${originalClassList.join(" ")}">${text.slice(newCursorData.endOffset)}</span><br>`;
|
|
420
|
+
}
|
|
421
|
+
}
|
|
422
|
+
}
|
|
423
|
+
});
|
|
424
|
+
$block.innerHTML = structure;
|
|
425
|
+
_setCursor($block.childNodes[newCursorData.startNodeIdx + 2], 0);
|
|
426
|
+
}
|
|
427
|
+
} else {
|
|
428
|
+
console.error("[Dragon Editor] : Something wrong.");
|
|
429
|
+
}
|
|
430
|
+
}
|
|
431
|
+
function __listBlockEnterEvent(event, store) {
|
|
432
|
+
event.preventDefault();
|
|
433
|
+
if (store.value.controlStatus.$currentBlock !== null && store.value.cursorData !== null) {
|
|
434
|
+
const cursorData = store.value.cursorData;
|
|
435
|
+
const $listBlock = store.value.controlStatus.$currentBlock;
|
|
436
|
+
const $editableElement = _findContentEditableElement(event.target);
|
|
437
|
+
const liList = $listBlock.querySelectorAll(".de-item");
|
|
438
|
+
if ($editableElement !== null) {
|
|
439
|
+
let liIdx = -1;
|
|
440
|
+
for (let i = 0; liList.length > i; i += 1) {
|
|
441
|
+
if (liList[i] === $editableElement) {
|
|
442
|
+
liIdx = i;
|
|
443
|
+
break;
|
|
444
|
+
}
|
|
445
|
+
}
|
|
446
|
+
if (cursorData.type === "Caret") {
|
|
447
|
+
if ($editableElement.textContent === "") {
|
|
448
|
+
if ($editableElement.hasChildNodes() === false) {
|
|
449
|
+
if (liList.length - 1 === liIdx) {
|
|
450
|
+
$changeToTextBlock($listBlock, liList.length, $editableElement);
|
|
451
|
+
} else {
|
|
452
|
+
const $liBlock = _createListItemBlock();
|
|
453
|
+
$editableElement.insertAdjacentElement("afterend", $liBlock);
|
|
454
|
+
$liBlock.focus();
|
|
455
|
+
}
|
|
456
|
+
} else {
|
|
457
|
+
const brList = $editableElement.querySelectorAll("br");
|
|
458
|
+
if (brList.length === 1) {
|
|
459
|
+
} else {
|
|
460
|
+
}
|
|
461
|
+
}
|
|
462
|
+
} else {
|
|
463
|
+
const childNodeList = $editableElement.childNodes;
|
|
464
|
+
const targetNode = _getParentElementIfNodeIsText(cursorData.startNode, $editableElement);
|
|
465
|
+
const preStructure = [];
|
|
466
|
+
const nextStructure = [];
|
|
467
|
+
let nodeIdx = -1;
|
|
468
|
+
for (let i = 0; childNodeList.length > i; i += 1) {
|
|
469
|
+
if (childNodeList[i] === targetNode) {
|
|
470
|
+
nodeIdx = i;
|
|
471
|
+
break;
|
|
472
|
+
}
|
|
473
|
+
}
|
|
474
|
+
childNodeList.forEach((node, i) => {
|
|
475
|
+
if (nodeIdx < i) {
|
|
476
|
+
nextStructure.push(node);
|
|
477
|
+
} else if (nodeIdx > i) {
|
|
478
|
+
preStructure.push(node);
|
|
479
|
+
} else if (nodeIdx === i) {
|
|
480
|
+
if (node.constructor.name === "Text") {
|
|
481
|
+
const preText = node.textContent.slice(0, cursorData.startOffset);
|
|
482
|
+
const nextText = node.textContent.slice(cursorData.endOffset);
|
|
483
|
+
if (preText !== "") {
|
|
484
|
+
preStructure.push(document.createTextNode(preText));
|
|
485
|
+
}
|
|
486
|
+
if (nextText !== "") {
|
|
487
|
+
nextStructure.push(document.createTextNode(nextText));
|
|
488
|
+
}
|
|
489
|
+
} else {
|
|
490
|
+
const originalClassList = Array.from(node.classList);
|
|
491
|
+
const preText = node.textContent.slice(0, cursorData.startOffset);
|
|
492
|
+
const nextText = node.textContent.slice(cursorData.endOffset);
|
|
493
|
+
if (preText !== "") {
|
|
494
|
+
const $span = document.createElement("span");
|
|
495
|
+
$span.textContent = preText;
|
|
496
|
+
$span.classList.add(...originalClassList);
|
|
497
|
+
preStructure.push($span);
|
|
498
|
+
}
|
|
499
|
+
if (nextText !== "") {
|
|
500
|
+
const $span = document.createElement("span");
|
|
501
|
+
$span.textContent = nextText;
|
|
502
|
+
$span.classList.add(...originalClassList);
|
|
503
|
+
nextStructure.push($span);
|
|
504
|
+
}
|
|
505
|
+
}
|
|
506
|
+
}
|
|
507
|
+
});
|
|
508
|
+
const $liBlock = _createListItemBlock();
|
|
509
|
+
$editableElement.insertAdjacentElement("afterend", $liBlock);
|
|
510
|
+
$editableElement.replaceChildren(...preStructure);
|
|
511
|
+
$liBlock.replaceChildren(...nextStructure);
|
|
512
|
+
if (nextStructure.length === 0) {
|
|
513
|
+
$liBlock.focus();
|
|
514
|
+
} else {
|
|
515
|
+
_setCursor($liBlock.childNodes[0], 0);
|
|
516
|
+
}
|
|
517
|
+
}
|
|
518
|
+
} else {
|
|
519
|
+
const childNodeList = $editableElement.childNodes;
|
|
520
|
+
const newCursorData = _sortingCursorDataOnElement(cursorData, $editableElement);
|
|
521
|
+
const preStructure = [];
|
|
522
|
+
const nextStructure = [];
|
|
523
|
+
childNodeList.forEach((node, i) => {
|
|
524
|
+
if (newCursorData.startNodeIdx > i) {
|
|
525
|
+
preStructure.push(node);
|
|
526
|
+
} else if (newCursorData.startNodeIdx === i) {
|
|
527
|
+
if (node.constructor.name === "Text") {
|
|
528
|
+
const text = node.textContent.slice(0, newCursorData.startOffset);
|
|
529
|
+
if (text !== "") {
|
|
530
|
+
const $textNode = document.createTextNode(text);
|
|
531
|
+
preStructure.push($textNode);
|
|
532
|
+
}
|
|
533
|
+
} else {
|
|
534
|
+
const originalClassList = Array.from(node.classList);
|
|
535
|
+
const text = node.textContent.slice(0, newCursorData.startOffset);
|
|
536
|
+
if (text !== "") {
|
|
537
|
+
const $span = document.createElement("span");
|
|
538
|
+
$span.classList.add(...originalClassList);
|
|
539
|
+
$span.textContent = text;
|
|
540
|
+
preStructure.push($span);
|
|
541
|
+
}
|
|
542
|
+
}
|
|
543
|
+
}
|
|
544
|
+
if (newCursorData.endNodeIdx < i) {
|
|
545
|
+
nextStructure.push(node);
|
|
546
|
+
} else if (newCursorData.endNodeIdx === i) {
|
|
547
|
+
if (node.constructor.name === "Text") {
|
|
548
|
+
const text = node.textContent.slice(newCursorData.endOffset);
|
|
549
|
+
if (text !== "") {
|
|
550
|
+
const $textNode = document.createTextNode(text);
|
|
551
|
+
nextStructure.push($textNode);
|
|
552
|
+
}
|
|
553
|
+
} else {
|
|
554
|
+
const originalClassList = Array.from(node.classList);
|
|
555
|
+
const text = node.textContent.slice(newCursorData.endOffset);
|
|
556
|
+
if (text !== "") {
|
|
557
|
+
const $span = document.createElement("span");
|
|
558
|
+
$span.classList.add(...originalClassList);
|
|
559
|
+
$span.textContent = text;
|
|
560
|
+
nextStructure.push($span);
|
|
561
|
+
}
|
|
562
|
+
}
|
|
563
|
+
}
|
|
564
|
+
});
|
|
565
|
+
const $liBlock = _createListItemBlock();
|
|
566
|
+
$editableElement.insertAdjacentElement("afterend", $liBlock);
|
|
567
|
+
$editableElement.replaceChildren(...preStructure);
|
|
568
|
+
if (nextStructure.length === 0) {
|
|
569
|
+
$liBlock.focus();
|
|
570
|
+
} else {
|
|
571
|
+
$liBlock.replaceChildren(...nextStructure);
|
|
572
|
+
_setCursor(nextStructure[0], 0);
|
|
573
|
+
}
|
|
574
|
+
}
|
|
575
|
+
}
|
|
576
|
+
}
|
|
577
|
+
function $changeToTextBlock($listBlock, licount, $editableElement) {
|
|
578
|
+
const $textBlock = _createTextBlock(_getDefaultBlockData("text"));
|
|
579
|
+
$listBlock.insertAdjacentElement("afterend", $textBlock);
|
|
580
|
+
if (licount === 1) {
|
|
581
|
+
$listBlock.remove();
|
|
582
|
+
} else {
|
|
583
|
+
$editableElement.remove();
|
|
584
|
+
}
|
|
585
|
+
$textBlock.focus();
|
|
586
|
+
}
|
|
587
|
+
}
|
|
588
|
+
function __listBlockShiftEnterEvent(event, store) {
|
|
589
|
+
event.preventDefault();
|
|
590
|
+
if (store.value.cursorData !== null && store.value.controlStatus.$currentBlock !== null) {
|
|
591
|
+
const cursorData = store.value.cursorData;
|
|
592
|
+
const $editableElement = _findContentEditableElement(event.target);
|
|
593
|
+
if ($editableElement !== null) {
|
|
594
|
+
if (cursorData.type === "Caret") {
|
|
595
|
+
if ($editableElement.textContent === "") {
|
|
596
|
+
if ($editableElement.hasChildNodes() === false) {
|
|
597
|
+
$editableElement.insertAdjacentHTML("beforeend", "<br><br>");
|
|
598
|
+
_setCursor($editableElement.childNodes[1], 0);
|
|
599
|
+
} else {
|
|
600
|
+
const $br = document.createElement("br");
|
|
601
|
+
$editableElement.insertAdjacentElement("beforeend", $br);
|
|
602
|
+
_setCursor($br, 0);
|
|
603
|
+
}
|
|
604
|
+
} else {
|
|
605
|
+
const childList = $editableElement.childNodes;
|
|
606
|
+
let targetIdx = -1;
|
|
607
|
+
let structure = "";
|
|
608
|
+
let $target = cursorData.startNode;
|
|
609
|
+
if ($target.constructor.name === "Text") {
|
|
610
|
+
if ($target.parentNode !== $editableElement) {
|
|
611
|
+
$target = $target.parentNode;
|
|
612
|
+
}
|
|
613
|
+
}
|
|
614
|
+
if ($editableElement === $target) {
|
|
615
|
+
$target = $editableElement.childNodes[cursorData.startOffset];
|
|
616
|
+
}
|
|
617
|
+
for (let i = 0; childList.length > i; i += 1) {
|
|
618
|
+
if (childList[i] === $target) {
|
|
619
|
+
targetIdx = i;
|
|
620
|
+
break;
|
|
621
|
+
}
|
|
622
|
+
}
|
|
623
|
+
let currentIdx = targetIdx;
|
|
624
|
+
childList.forEach((child, i) => {
|
|
625
|
+
if (i === targetIdx) {
|
|
626
|
+
const constructorName = child.constructor.name;
|
|
627
|
+
if (constructorName === "Text") {
|
|
628
|
+
structure += child.textContent.slice(0, cursorData.startOffset) + "<br>" + child.textContent.slice(cursorData.endOffset);
|
|
629
|
+
if (child.nextSibling === null) {
|
|
630
|
+
if (child.textContent.slice(cursorData.endOffset) === "") {
|
|
631
|
+
structure += `<br>`;
|
|
632
|
+
}
|
|
633
|
+
if (child.textContent.slice(0, cursorData.startOffset) === "") {
|
|
634
|
+
currentIdx -= 1;
|
|
635
|
+
}
|
|
636
|
+
} else {
|
|
637
|
+
if (child.textContent.slice(cursorData.endOffset) !== "" && child.textContent.slice(0, cursorData.startOffset) === "") {
|
|
638
|
+
currentIdx -= 1;
|
|
639
|
+
}
|
|
640
|
+
}
|
|
641
|
+
currentIdx += 1;
|
|
642
|
+
} else {
|
|
643
|
+
if (constructorName === "HTMLBRElement") {
|
|
644
|
+
structure += `<br><br>`;
|
|
645
|
+
} else {
|
|
646
|
+
structure += `<span class="${Array.from(child.classList).join(" ")}">${child.textContent.slice(0, cursorData.startOffset)}</span>`;
|
|
647
|
+
structure += `<br>`;
|
|
648
|
+
structure += `<span class="${Array.from(child.classList).join(" ")}">${child.textContent.slice(cursorData.startOffset)}</span>`;
|
|
649
|
+
currentIdx += 1;
|
|
650
|
+
}
|
|
651
|
+
}
|
|
652
|
+
} else {
|
|
653
|
+
if (child.constructor.name === "Text") {
|
|
654
|
+
structure += child.textContent;
|
|
655
|
+
} else {
|
|
656
|
+
structure += child.outerHTML;
|
|
657
|
+
}
|
|
658
|
+
}
|
|
659
|
+
});
|
|
660
|
+
$editableElement.innerHTML = structure;
|
|
661
|
+
_setCursor($editableElement.childNodes[currentIdx + 1], 0);
|
|
662
|
+
}
|
|
663
|
+
} else {
|
|
664
|
+
const childNodeList = $editableElement.childNodes;
|
|
665
|
+
const newCursorData = _sortingCursorDataOnElement(cursorData, $editableElement);
|
|
666
|
+
let structure = "";
|
|
667
|
+
childNodeList.forEach((node, i) => {
|
|
668
|
+
if (newCursorData.startNodeIdx > i) {
|
|
669
|
+
if (node.constructor.name === "Text") {
|
|
670
|
+
structure += node.textContent;
|
|
671
|
+
} else {
|
|
672
|
+
structure += node.outerHTML;
|
|
673
|
+
}
|
|
674
|
+
} else if (newCursorData.startNodeIdx === i) {
|
|
675
|
+
if (node.constructor.name === "Text") {
|
|
676
|
+
structure += node.textContent.slice(0, newCursorData.startOffset);
|
|
677
|
+
structure += `<br>`;
|
|
678
|
+
} else {
|
|
679
|
+
if (node.tagName === "BR") {
|
|
680
|
+
structure += `<br>`;
|
|
681
|
+
} else {
|
|
682
|
+
const originalClassList = Array.from(node.classList);
|
|
683
|
+
const text = node.textContent;
|
|
684
|
+
structure += `<span class="${originalClassList.join(" ")}">${text.slice(0, newCursorData.startOffset)}</span><br>`;
|
|
685
|
+
}
|
|
686
|
+
}
|
|
687
|
+
if (childNodeList.length === i) {
|
|
688
|
+
structure += `<br>`;
|
|
689
|
+
}
|
|
690
|
+
}
|
|
691
|
+
if (newCursorData.endNodeIdx < i) {
|
|
692
|
+
if (node.constructor.name === "Text") {
|
|
693
|
+
structure += node.textContent;
|
|
694
|
+
} else {
|
|
695
|
+
structure += node.outerHTML;
|
|
696
|
+
}
|
|
697
|
+
} else if (newCursorData.endNodeIdx === i) {
|
|
698
|
+
if (node.constructor.name === "Text") {
|
|
699
|
+
structure += node.textContent.slice(newCursorData.endOffset);
|
|
700
|
+
} else {
|
|
701
|
+
if (node.tagName === "BR") {
|
|
702
|
+
structure += `<br>`;
|
|
703
|
+
} else {
|
|
704
|
+
const originalClassList = Array.from(node.classList);
|
|
705
|
+
const text = node.textContent;
|
|
706
|
+
structure += `<span class="${originalClassList.join(" ")}">${text.slice(newCursorData.endOffset)}</span><br>`;
|
|
707
|
+
}
|
|
708
|
+
}
|
|
709
|
+
}
|
|
710
|
+
});
|
|
711
|
+
$editableElement.innerHTML = structure;
|
|
712
|
+
_setCursor($editableElement.childNodes[newCursorData.startNodeIdx + 2], 0);
|
|
713
|
+
}
|
|
714
|
+
}
|
|
715
|
+
} else {
|
|
716
|
+
console.error("[Dragon Editor] : Something wrong.");
|
|
717
|
+
}
|
|
718
|
+
}
|
|
719
|
+
function __codeBlockShiftEnterEvent(event, store) {
|
|
720
|
+
event.preventDefault();
|
|
721
|
+
if (store.value.controlStatus.$currentBlock !== null) {
|
|
722
|
+
const $block = store.value.controlStatus.$currentBlock;
|
|
723
|
+
const $newTextBlock = _createTextBlock(_getDefaultBlockData("text"));
|
|
724
|
+
$block.insertAdjacentElement("afterend", $newTextBlock);
|
|
725
|
+
$newTextBlock.focus();
|
|
726
|
+
}
|
|
727
|
+
}
|
|
728
|
+
function __backspaceEvent(event, store) {
|
|
729
|
+
switch (store.value.controlStatus.currentBlockType) {
|
|
730
|
+
case "image":
|
|
731
|
+
case "code":
|
|
732
|
+
break;
|
|
733
|
+
case "ol":
|
|
734
|
+
case "ul":
|
|
735
|
+
___listBlockBackspaceEvent(event, store);
|
|
736
|
+
break;
|
|
737
|
+
default:
|
|
738
|
+
___defaultBlockBackspaceEvent(event, store);
|
|
739
|
+
}
|
|
740
|
+
}
|
|
741
|
+
function ___defaultBlockBackspaceEvent(event, store) {
|
|
742
|
+
if (store.value.cursorData !== null && store.value.controlStatus.$currentBlock !== null && store.value.$body !== null) {
|
|
743
|
+
const cursorData = store.value.cursorData;
|
|
744
|
+
const childList = store.value.$body.querySelectorAll(".de-block");
|
|
745
|
+
const $block = store.value.controlStatus.$currentBlock;
|
|
746
|
+
let $target = cursorData.startNode;
|
|
747
|
+
let elementIdx = -1;
|
|
748
|
+
for (let i = 0; childList.length > i; i += 1) {
|
|
749
|
+
if (childList[i] === $block) {
|
|
750
|
+
elementIdx = i;
|
|
751
|
+
break;
|
|
752
|
+
}
|
|
753
|
+
}
|
|
754
|
+
if ($target.constructor.name === "Text") {
|
|
755
|
+
$target = $target.parentNode;
|
|
756
|
+
}
|
|
757
|
+
if (elementIdx === 0) {
|
|
758
|
+
if (cursorData.startOffset === 0 && $target === $block) {
|
|
759
|
+
if ($target.textContent === "") {
|
|
760
|
+
$block.insertAdjacentElement("afterend", _createTextBlock(_getDefaultBlockData("text")));
|
|
761
|
+
_setCursor($block.nextElementSibling, 0);
|
|
762
|
+
$block.remove();
|
|
763
|
+
} else {
|
|
764
|
+
event.preventDefault();
|
|
765
|
+
if (store.value.controlStatus.currentBlockType !== "text") {
|
|
766
|
+
const $newBlock = _createTextBlock({ type: "text", classList: [], textContent: $block.textContent ?? "" });
|
|
767
|
+
$block.insertAdjacentElement("afterend", $newBlock);
|
|
768
|
+
_setCursor($newBlock, 0);
|
|
769
|
+
$block.remove();
|
|
770
|
+
}
|
|
771
|
+
}
|
|
772
|
+
}
|
|
773
|
+
} else {
|
|
774
|
+
if ($block.textContent === "") {
|
|
775
|
+
event.preventDefault();
|
|
776
|
+
let hasChild = false;
|
|
777
|
+
let childHTML = "";
|
|
778
|
+
if ($block.hasChildNodes() === true) {
|
|
779
|
+
const $brList = $block.querySelectorAll("br");
|
|
780
|
+
if ($brList.length > 1) {
|
|
781
|
+
hasChild = true;
|
|
782
|
+
childHTML = $block.innerHTML;
|
|
783
|
+
}
|
|
784
|
+
}
|
|
785
|
+
____backspackeLogic(hasChild, childHTML, $block, $block);
|
|
786
|
+
} else {
|
|
787
|
+
if (cursorData.type === "Caret" && cursorData.startOffset === 0 && ($block.childNodes[0] === cursorData.startNode || $block.childNodes[0] === $target)) {
|
|
788
|
+
event.preventDefault();
|
|
789
|
+
let childHTML = $block.innerHTML;
|
|
790
|
+
____backspackeLogic(true, childHTML, $block, $block);
|
|
791
|
+
}
|
|
792
|
+
}
|
|
793
|
+
}
|
|
794
|
+
____nodeBackspaceEvent(event, cursorData, $block, $target);
|
|
795
|
+
_updateCursorData(store);
|
|
796
|
+
}
|
|
797
|
+
}
|
|
798
|
+
function ___listBlockBackspaceEvent(event, store) {
|
|
799
|
+
if (store.value.cursorData !== null && store.value.controlStatus.$currentBlock !== null && store.value.$body !== null) {
|
|
800
|
+
const cursorData = store.value.cursorData;
|
|
801
|
+
const $listBlock = store.value.controlStatus.$currentBlock;
|
|
802
|
+
const $targetItem = _findContentEditableElement(cursorData.startNode);
|
|
803
|
+
const $liList = $listBlock.querySelectorAll(".de-item");
|
|
804
|
+
let $target = cursorData.startNode;
|
|
805
|
+
let liIdx = -1;
|
|
806
|
+
if ($target.constructor.name === "Text") {
|
|
807
|
+
$target = $target.parentNode;
|
|
808
|
+
}
|
|
809
|
+
for (let i = 0; $liList.length > i; i += 1) {
|
|
810
|
+
if ($liList[i] === $targetItem) {
|
|
811
|
+
liIdx = i;
|
|
812
|
+
break;
|
|
813
|
+
}
|
|
814
|
+
}
|
|
815
|
+
if ($liList.length === 1) {
|
|
816
|
+
if ($targetItem.textContent === "") {
|
|
817
|
+
event.preventDefault();
|
|
818
|
+
let hasChild = false;
|
|
819
|
+
let childHTML = "";
|
|
820
|
+
if ($targetItem.hasChildNodes() === true) {
|
|
821
|
+
const $brList = $targetItem.querySelectorAll("br");
|
|
822
|
+
if ($brList.length > 1) {
|
|
823
|
+
hasChild = true;
|
|
824
|
+
childHTML = $targetItem.innerHTML;
|
|
825
|
+
}
|
|
826
|
+
}
|
|
827
|
+
____backspackeLogic(hasChild, childHTML, $listBlock, $targetItem);
|
|
828
|
+
$listBlock.remove();
|
|
829
|
+
} else {
|
|
830
|
+
if (cursorData.type === "Caret" && cursorData.startOffset === 0 && ($targetItem.childNodes[0] === cursorData.startNode || $targetItem.childNodes[0] === $target)) {
|
|
831
|
+
event.preventDefault();
|
|
832
|
+
let childHTML = $targetItem.innerHTML;
|
|
833
|
+
____backspackeLogic(true, childHTML, $listBlock, $targetItem);
|
|
834
|
+
$listBlock.remove();
|
|
835
|
+
}
|
|
836
|
+
}
|
|
837
|
+
} else {
|
|
838
|
+
if (liIdx === 0) {
|
|
839
|
+
if ($targetItem.textContent === "") {
|
|
840
|
+
event.preventDefault();
|
|
841
|
+
let hasChild = false;
|
|
842
|
+
let childHTML = "";
|
|
843
|
+
if ($targetItem.hasChildNodes() === true) {
|
|
844
|
+
const $brList = $targetItem.querySelectorAll("br");
|
|
845
|
+
if ($brList.length > 1) {
|
|
846
|
+
hasChild = true;
|
|
847
|
+
childHTML = $targetItem.innerHTML;
|
|
848
|
+
}
|
|
849
|
+
}
|
|
850
|
+
____backspackeLogic(hasChild, childHTML, $listBlock, $targetItem);
|
|
851
|
+
} else {
|
|
852
|
+
if (cursorData.type === "Caret" && cursorData.startOffset === 0 && ($targetItem.childNodes[0] === cursorData.startNode || $targetItem.childNodes[0] === $target)) {
|
|
853
|
+
event.preventDefault();
|
|
854
|
+
let childHTML = $targetItem.innerHTML;
|
|
855
|
+
____backspackeLogic(true, childHTML, $listBlock, $targetItem);
|
|
856
|
+
}
|
|
857
|
+
}
|
|
858
|
+
} else {
|
|
859
|
+
if ($targetItem.textContent === "") {
|
|
860
|
+
event.preventDefault();
|
|
861
|
+
const $preLi = $targetItem.previousElementSibling;
|
|
862
|
+
const hasChild = $preLi.hasChildNodes();
|
|
863
|
+
let childHTML = "";
|
|
864
|
+
if ($targetItem.hasChildNodes() === true) {
|
|
865
|
+
const $brList = $targetItem.querySelectorAll("br");
|
|
866
|
+
if ($brList.length > 1) {
|
|
867
|
+
childHTML = $targetItem.innerHTML;
|
|
868
|
+
}
|
|
869
|
+
}
|
|
870
|
+
if ($preLi.hasChildNodes() === true) {
|
|
871
|
+
const textBlockChildList = $preLi.childNodes;
|
|
872
|
+
const textBlockTargetChild = textBlockChildList[textBlockChildList.length - 1];
|
|
873
|
+
if (hasChild === true) {
|
|
874
|
+
$preLi.insertAdjacentHTML("beforeend", childHTML);
|
|
875
|
+
}
|
|
876
|
+
_setCursor(textBlockTargetChild, textBlockTargetChild.textContent.length);
|
|
877
|
+
} else {
|
|
878
|
+
if (hasChild === true) {
|
|
879
|
+
$preLi.insertAdjacentHTML("beforeend", childHTML);
|
|
880
|
+
}
|
|
881
|
+
_setCursor($preLi, 0);
|
|
882
|
+
}
|
|
883
|
+
$targetItem.remove();
|
|
884
|
+
} else {
|
|
885
|
+
if (cursorData.type === "Caret" && cursorData.startOffset === 0 && ($targetItem.childNodes[0] === cursorData.startNode || $targetItem.childNodes[0] === $target)) {
|
|
886
|
+
event.preventDefault();
|
|
887
|
+
const $preLi = $targetItem.previousElementSibling;
|
|
888
|
+
const hasChild = $preLi.hasChildNodes();
|
|
889
|
+
let childHTML = $targetItem.innerHTML;
|
|
890
|
+
if ($preLi.hasChildNodes() === true) {
|
|
891
|
+
const textBlockChildList = $preLi.childNodes;
|
|
892
|
+
const textBlockTargetChild = textBlockChildList[textBlockChildList.length - 1];
|
|
893
|
+
if (hasChild === true) {
|
|
894
|
+
$preLi.insertAdjacentHTML("beforeend", childHTML);
|
|
895
|
+
}
|
|
896
|
+
_setCursor(textBlockTargetChild, textBlockTargetChild.textContent.length);
|
|
897
|
+
} else {
|
|
898
|
+
if (hasChild === true) {
|
|
899
|
+
$preLi.insertAdjacentHTML("beforeend", childHTML);
|
|
900
|
+
}
|
|
901
|
+
_setCursor($preLi, 0);
|
|
902
|
+
}
|
|
903
|
+
$targetItem.remove();
|
|
904
|
+
}
|
|
905
|
+
}
|
|
906
|
+
}
|
|
907
|
+
}
|
|
908
|
+
}
|
|
909
|
+
}
|
|
910
|
+
function ____backspackeLogic(hasChild, childHTML, $block, $targetItem) {
|
|
911
|
+
const $preBlock = $block.previousElementSibling;
|
|
912
|
+
if ($preBlock !== null) {
|
|
913
|
+
const { type: preBlockType } = _getCurrentBlock($preBlock);
|
|
914
|
+
let preDelete = false;
|
|
915
|
+
let $targetBlock = $preBlock;
|
|
916
|
+
switch (preBlockType) {
|
|
917
|
+
case "custom":
|
|
918
|
+
case "image":
|
|
919
|
+
case "code":
|
|
920
|
+
preDelete = true;
|
|
921
|
+
break;
|
|
922
|
+
case "ul":
|
|
923
|
+
case "ol":
|
|
924
|
+
const $liList = $preBlock.querySelectorAll(".de-item");
|
|
925
|
+
$targetBlock = $liList[$liList.length - 1];
|
|
926
|
+
break;
|
|
927
|
+
default:
|
|
928
|
+
$targetBlock = $preBlock;
|
|
929
|
+
}
|
|
930
|
+
if (preDelete === false) {
|
|
931
|
+
if ($targetBlock.hasChildNodes() === true) {
|
|
932
|
+
const textBlockChildList = $targetBlock.childNodes;
|
|
933
|
+
const textBlockTargetChild = textBlockChildList[textBlockChildList.length - 1];
|
|
934
|
+
if (hasChild === true) {
|
|
935
|
+
$targetBlock.insertAdjacentHTML("beforeend", childHTML);
|
|
936
|
+
}
|
|
937
|
+
_setCursor(textBlockTargetChild, textBlockTargetChild.textContent.length);
|
|
938
|
+
} else {
|
|
939
|
+
if (hasChild === true) {
|
|
940
|
+
$targetBlock.insertAdjacentHTML("beforeend", childHTML);
|
|
941
|
+
}
|
|
942
|
+
_setCursor($targetBlock, 0);
|
|
943
|
+
}
|
|
944
|
+
$targetItem.remove();
|
|
945
|
+
} else {
|
|
946
|
+
$preBlock.remove();
|
|
947
|
+
}
|
|
948
|
+
} else {
|
|
949
|
+
const newTextBlockData = _getDefaultBlockData("text");
|
|
950
|
+
newTextBlockData.textContent = childHTML;
|
|
951
|
+
const $newTextBlock = _createTextBlock(newTextBlockData);
|
|
952
|
+
$block.insertAdjacentElement("afterend", $newTextBlock);
|
|
953
|
+
$newTextBlock.focus();
|
|
954
|
+
$block.remove();
|
|
955
|
+
}
|
|
956
|
+
}
|
|
957
|
+
function ____nodeBackspaceEvent(event, cursorData, $block, $target) {
|
|
958
|
+
if (cursorData.startOffset === 1 && $target !== $block) {
|
|
959
|
+
if ($target.textContent.length === 1) {
|
|
960
|
+
event.preventDefault();
|
|
961
|
+
const preNode = $target.previousSibling;
|
|
962
|
+
if (preNode !== null) {
|
|
963
|
+
$target.remove();
|
|
964
|
+
_setCursor(preNode, preNode.textContent.length);
|
|
965
|
+
} else {
|
|
966
|
+
if ($block.childNodes[1] === void 0) {
|
|
967
|
+
$block.innerHTML = "";
|
|
968
|
+
_setCursor($block, 0);
|
|
969
|
+
} else {
|
|
970
|
+
_setCursor($block.childNodes[1], 0);
|
|
971
|
+
$target.remove();
|
|
972
|
+
}
|
|
973
|
+
}
|
|
974
|
+
}
|
|
975
|
+
}
|
|
976
|
+
}
|
|
977
|
+
function __tabEvent(event, store) {
|
|
978
|
+
event.preventDefault();
|
|
979
|
+
switch (store.value.controlStatus.currentBlockType) {
|
|
980
|
+
case "code":
|
|
981
|
+
case "custom":
|
|
982
|
+
case "image":
|
|
983
|
+
break;
|
|
984
|
+
default:
|
|
985
|
+
if (event.shiftKey === true) {
|
|
986
|
+
_setIndent(store, "minus");
|
|
987
|
+
} else {
|
|
988
|
+
_setIndent(store, "plus");
|
|
989
|
+
}
|
|
990
|
+
}
|
|
991
|
+
}
|
|
992
|
+
export function _setIndent(store, type) {
|
|
993
|
+
if (store.value.controlStatus.$currentBlock !== null) {
|
|
994
|
+
const $block = store.value.controlStatus.$currentBlock;
|
|
995
|
+
let value = $block.dataset["depth"] === void 0 ? 0 : parseInt($block.dataset["depth"]);
|
|
996
|
+
if (type === "minus") {
|
|
997
|
+
if (value !== 0) {
|
|
998
|
+
value -= 1;
|
|
999
|
+
}
|
|
1000
|
+
} else {
|
|
1001
|
+
if (value < 5) {
|
|
1002
|
+
value += 1;
|
|
1003
|
+
}
|
|
1004
|
+
}
|
|
1005
|
+
if (value === 0) {
|
|
1006
|
+
delete $block.dataset["depth"];
|
|
1007
|
+
} else {
|
|
1008
|
+
$block.dataset["depth"] = String(value);
|
|
1009
|
+
}
|
|
1010
|
+
_updateModelData(store);
|
|
1011
|
+
}
|
|
1012
|
+
}
|
|
1013
|
+
function __deleteEvent(event, store) {
|
|
1014
|
+
switch (store.value.controlStatus.currentBlockType) {
|
|
1015
|
+
case "image":
|
|
1016
|
+
case "code":
|
|
1017
|
+
break;
|
|
1018
|
+
case "ol":
|
|
1019
|
+
case "ul":
|
|
1020
|
+
___listBlockDeleteEvent(event, store);
|
|
1021
|
+
break;
|
|
1022
|
+
default:
|
|
1023
|
+
___defaultBlockDeleteEvent(event, store);
|
|
1024
|
+
}
|
|
1025
|
+
}
|
|
1026
|
+
function ___defaultBlockDeleteEvent(event, store) {
|
|
1027
|
+
if (store.value.cursorData !== null && store.value.controlStatus.$currentBlock !== null && store.value.$body !== null) {
|
|
1028
|
+
const cursorData = store.value.cursorData;
|
|
1029
|
+
const $block = store.value.controlStatus.$currentBlock;
|
|
1030
|
+
let $target = cursorData.startNode;
|
|
1031
|
+
if ($target.constructor.name === "Text") {
|
|
1032
|
+
$target = $target.parentNode;
|
|
1033
|
+
}
|
|
1034
|
+
if ($block.textContent === "") {
|
|
1035
|
+
event.preventDefault();
|
|
1036
|
+
let hasChild = false;
|
|
1037
|
+
if ($block.hasChildNodes() === true) {
|
|
1038
|
+
const $brList = $block.querySelectorAll("br");
|
|
1039
|
+
if ($brList.length === 1) {
|
|
1040
|
+
$block.innerHTML = "";
|
|
1041
|
+
} else {
|
|
1042
|
+
hasChild = true;
|
|
1043
|
+
}
|
|
1044
|
+
}
|
|
1045
|
+
____deleteLogic($block, $block);
|
|
1046
|
+
} else {
|
|
1047
|
+
const lastChild = $block.childNodes[$block.childNodes.length - 1];
|
|
1048
|
+
if (cursorData.type === "Caret" && lastChild !== void 0 && lastChild.textContent && cursorData.startOffset === lastChild.textContent.length && (lastChild === cursorData.startNode || lastChild === $target)) {
|
|
1049
|
+
event.preventDefault();
|
|
1050
|
+
____deleteLogic($block, $block);
|
|
1051
|
+
}
|
|
1052
|
+
}
|
|
1053
|
+
}
|
|
1054
|
+
}
|
|
1055
|
+
function ___listBlockDeleteEvent(event, store) {
|
|
1056
|
+
if (store.value.cursorData !== null && store.value.controlStatus.$currentBlock !== null && store.value.$body !== null) {
|
|
1057
|
+
const cursorData = store.value.cursorData;
|
|
1058
|
+
const $listBlock = store.value.controlStatus.$currentBlock;
|
|
1059
|
+
const $targetItem = _findContentEditableElement(cursorData.startNode);
|
|
1060
|
+
const $liList = $listBlock.querySelectorAll(".de-item");
|
|
1061
|
+
let $target = cursorData.startNode;
|
|
1062
|
+
let liIdx = -1;
|
|
1063
|
+
if ($target.constructor.name === "Text") {
|
|
1064
|
+
$target = $target.parentNode;
|
|
1065
|
+
}
|
|
1066
|
+
for (let i = 0; $liList.length > i; i += 1) {
|
|
1067
|
+
if ($liList[i] === $targetItem) {
|
|
1068
|
+
liIdx = i;
|
|
1069
|
+
break;
|
|
1070
|
+
}
|
|
1071
|
+
}
|
|
1072
|
+
if ($liList.length === 1) {
|
|
1073
|
+
if ($targetItem.textContent === "") {
|
|
1074
|
+
event.preventDefault();
|
|
1075
|
+
____deleteLogic($targetItem, $listBlock);
|
|
1076
|
+
} else {
|
|
1077
|
+
const lastChild = $targetItem.childNodes[$targetItem.childNodes.length - 1];
|
|
1078
|
+
if (cursorData.type === "Caret" && lastChild !== void 0 && lastChild.textContent && cursorData.startOffset === lastChild.textContent.length && (lastChild === cursorData.startNode || lastChild === $target)) {
|
|
1079
|
+
event.preventDefault();
|
|
1080
|
+
____deleteLogic($targetItem, $listBlock);
|
|
1081
|
+
}
|
|
1082
|
+
}
|
|
1083
|
+
} else {
|
|
1084
|
+
const $nextLi = $targetItem.nextElementSibling;
|
|
1085
|
+
if ($nextLi === null) {
|
|
1086
|
+
if ($targetItem.textContent === "") {
|
|
1087
|
+
event.preventDefault();
|
|
1088
|
+
____deleteLogic($targetItem, $listBlock);
|
|
1089
|
+
} else {
|
|
1090
|
+
const lastChild = $targetItem.childNodes[$targetItem.childNodes.length - 1];
|
|
1091
|
+
if (cursorData.type === "Caret" && lastChild !== void 0 && lastChild.textContent && cursorData.startOffset === lastChild.textContent.length && (lastChild === cursorData.startNode || lastChild === $target)) {
|
|
1092
|
+
event.preventDefault();
|
|
1093
|
+
____deleteLogic($targetItem, $listBlock);
|
|
1094
|
+
}
|
|
1095
|
+
}
|
|
1096
|
+
} else {
|
|
1097
|
+
if ($targetItem.textContent === "") {
|
|
1098
|
+
event.preventDefault();
|
|
1099
|
+
____deleteLogic($targetItem, $targetItem);
|
|
1100
|
+
} else {
|
|
1101
|
+
const lastChild = $targetItem.childNodes[$targetItem.childNodes.length - 1];
|
|
1102
|
+
if (cursorData.type === "Caret" && lastChild !== void 0 && lastChild.textContent && cursorData.startOffset === lastChild.textContent.length && (lastChild === cursorData.startNode || lastChild === $target)) {
|
|
1103
|
+
event.preventDefault();
|
|
1104
|
+
____deleteLogic($targetItem, $targetItem);
|
|
1105
|
+
}
|
|
1106
|
+
}
|
|
1107
|
+
}
|
|
1108
|
+
}
|
|
1109
|
+
}
|
|
1110
|
+
}
|
|
1111
|
+
function ____deleteLogic($targetElement, $block) {
|
|
1112
|
+
let $nextBlock = $block.nextElementSibling;
|
|
1113
|
+
if ($nextBlock !== null) {
|
|
1114
|
+
const { type: nextBlockType } = _getCurrentBlock($nextBlock);
|
|
1115
|
+
let nextDelete = false;
|
|
1116
|
+
switch (nextBlockType) {
|
|
1117
|
+
case "custom":
|
|
1118
|
+
case "image":
|
|
1119
|
+
case "code":
|
|
1120
|
+
nextDelete = true;
|
|
1121
|
+
break;
|
|
1122
|
+
case "ul":
|
|
1123
|
+
case "ol":
|
|
1124
|
+
if ($block.tagName !== "LI") {
|
|
1125
|
+
const $liList = $nextBlock.querySelectorAll(".de-item");
|
|
1126
|
+
$nextBlock = $liList[0];
|
|
1127
|
+
}
|
|
1128
|
+
break;
|
|
1129
|
+
}
|
|
1130
|
+
if (nextDelete === false) {
|
|
1131
|
+
if ($nextBlock.hasChildNodes() === true) {
|
|
1132
|
+
const childHTML = $nextBlock.innerHTML;
|
|
1133
|
+
if ($targetElement.hasChildNodes() === true) {
|
|
1134
|
+
$targetElement.insertAdjacentHTML("beforeend", childHTML);
|
|
1135
|
+
} else {
|
|
1136
|
+
$targetElement.innerHTML = childHTML;
|
|
1137
|
+
}
|
|
1138
|
+
}
|
|
1139
|
+
$nextBlock.remove();
|
|
1140
|
+
} else {
|
|
1141
|
+
$nextBlock.remove();
|
|
1142
|
+
}
|
|
1143
|
+
}
|
|
1144
|
+
}
|
|
1145
|
+
function __spaceEvent(event, store) {
|
|
1146
|
+
if (store.value.cursorData !== null && store.value.controlStatus.$currentBlock !== null && store.value.$body !== null) {
|
|
1147
|
+
const cursorData = store.value.cursorData;
|
|
1148
|
+
const $block = store.value.controlStatus.$currentBlock;
|
|
1149
|
+
const $targetItem = _findContentEditableElement(cursorData.startNode);
|
|
1150
|
+
if (store.value.controlStatus.currentBlockType === "text" && $targetItem !== null && $targetItem.textContent !== "") {
|
|
1151
|
+
switch ($targetItem.textContent) {
|
|
1152
|
+
case "#":
|
|
1153
|
+
event.preventDefault();
|
|
1154
|
+
const $newHeading1Block = _createHeadingBlock(_getDefaultBlockData("heading1"));
|
|
1155
|
+
$block.insertAdjacentElement("afterend", $newHeading1Block);
|
|
1156
|
+
$newHeading1Block.focus();
|
|
1157
|
+
$block.remove();
|
|
1158
|
+
break;
|
|
1159
|
+
case "##":
|
|
1160
|
+
event.preventDefault();
|
|
1161
|
+
const $newHeading2Block = _createHeadingBlock(_getDefaultBlockData("heading2"));
|
|
1162
|
+
$block.insertAdjacentElement("afterend", $newHeading2Block);
|
|
1163
|
+
$newHeading2Block.focus();
|
|
1164
|
+
$block.remove();
|
|
1165
|
+
break;
|
|
1166
|
+
case "###":
|
|
1167
|
+
event.preventDefault();
|
|
1168
|
+
const $newHeading3Block = _createHeadingBlock(_getDefaultBlockData("heading3"));
|
|
1169
|
+
$block.insertAdjacentElement("afterend", $newHeading3Block);
|
|
1170
|
+
$newHeading3Block.focus();
|
|
1171
|
+
$block.remove();
|
|
1172
|
+
break;
|
|
1173
|
+
case "-":
|
|
1174
|
+
event.preventDefault();
|
|
1175
|
+
const $newUlBlock = _createListBlock(_getDefaultBlockData("ul"));
|
|
1176
|
+
$block.insertAdjacentElement("afterend", $newUlBlock);
|
|
1177
|
+
$newUlBlock.children[0].focus();
|
|
1178
|
+
$block.remove();
|
|
1179
|
+
break;
|
|
1180
|
+
case "1.":
|
|
1181
|
+
event.preventDefault();
|
|
1182
|
+
const $newOlBlock = _createListBlock(_getDefaultBlockData("ol"));
|
|
1183
|
+
$block.insertAdjacentElement("afterend", $newOlBlock);
|
|
1184
|
+
$newOlBlock.children[0].focus();
|
|
1185
|
+
$block.remove();
|
|
1186
|
+
break;
|
|
1187
|
+
}
|
|
1188
|
+
}
|
|
1189
|
+
}
|
|
1190
|
+
}
|
|
1191
|
+
function _moveToBlockEvent(event, store, keyType) {
|
|
1192
|
+
if (store.value.cursorData !== null && store.value.controlStatus.$currentBlock !== null) {
|
|
1193
|
+
const cursorData = store.value.cursorData;
|
|
1194
|
+
const $editableElement = _findContentEditableElement(cursorData.startNode);
|
|
1195
|
+
const $block = store.value.controlStatus.$currentBlock;
|
|
1196
|
+
if ($editableElement !== null && $block !== null) {
|
|
1197
|
+
const $brList = $editableElement.querySelectorAll("br");
|
|
1198
|
+
let $targetElement = null;
|
|
1199
|
+
let suitable = false;
|
|
1200
|
+
if ($brList.length !== 0) {
|
|
1201
|
+
if (keyType === "up") {
|
|
1202
|
+
suitable = __isCursorFirstLine(cursorData.startNode, $brList[0]);
|
|
1203
|
+
} else {
|
|
1204
|
+
suitable = __isCursorLastLine(cursorData.startNode, $brList[$brList.length - 1]);
|
|
1205
|
+
}
|
|
1206
|
+
} else {
|
|
1207
|
+
suitable = true;
|
|
1208
|
+
}
|
|
1209
|
+
if (suitable === true) {
|
|
1210
|
+
switch (store.value.controlStatus.currentBlockType) {
|
|
1211
|
+
case "code":
|
|
1212
|
+
break;
|
|
1213
|
+
case "ol":
|
|
1214
|
+
case "ul":
|
|
1215
|
+
if (keyType === "up") {
|
|
1216
|
+
$targetElement = $editableElement.previousElementSibling;
|
|
1217
|
+
if ($targetElement === null) {
|
|
1218
|
+
$targetElement = $block.previousElementSibling;
|
|
1219
|
+
}
|
|
1220
|
+
} else {
|
|
1221
|
+
$targetElement = $editableElement.nextElementSibling;
|
|
1222
|
+
if ($targetElement === null) {
|
|
1223
|
+
$targetElement = $block.nextElementSibling;
|
|
1224
|
+
}
|
|
1225
|
+
}
|
|
1226
|
+
break;
|
|
1227
|
+
default:
|
|
1228
|
+
if (keyType === "up") {
|
|
1229
|
+
$targetElement = $block.previousElementSibling;
|
|
1230
|
+
} else {
|
|
1231
|
+
$targetElement = $block.nextElementSibling;
|
|
1232
|
+
}
|
|
1233
|
+
}
|
|
1234
|
+
if ($targetElement !== null) {
|
|
1235
|
+
const { type, $element } = _getCurrentBlock($targetElement);
|
|
1236
|
+
switch (type) {
|
|
1237
|
+
case "image":
|
|
1238
|
+
if ($element !== null) {
|
|
1239
|
+
const $caption = $element.querySelector(".de-caption");
|
|
1240
|
+
if ($caption !== null) {
|
|
1241
|
+
$targetElement = $caption;
|
|
1242
|
+
}
|
|
1243
|
+
}
|
|
1244
|
+
break;
|
|
1245
|
+
case "ol":
|
|
1246
|
+
case "ul":
|
|
1247
|
+
if ($targetElement.tagName !== "LI" && $element !== null) {
|
|
1248
|
+
const $childList = $element.querySelectorAll(".de-item");
|
|
1249
|
+
if (keyType === "up") {
|
|
1250
|
+
$targetElement = $childList[$childList.length - 1];
|
|
1251
|
+
} else {
|
|
1252
|
+
$targetElement = $childList[0];
|
|
1253
|
+
}
|
|
1254
|
+
}
|
|
1255
|
+
break;
|
|
1256
|
+
}
|
|
1257
|
+
_setCursor($targetElement, 0);
|
|
1258
|
+
}
|
|
1259
|
+
}
|
|
1260
|
+
}
|
|
1261
|
+
}
|
|
1262
|
+
}
|
|
1263
|
+
function __isCursorFirstLine(node, $br) {
|
|
1264
|
+
let suitable = false;
|
|
1265
|
+
let $previousNode = $br.previousSibling;
|
|
1266
|
+
if ($previousNode !== null) {
|
|
1267
|
+
if ($previousNode === node) {
|
|
1268
|
+
suitable = true;
|
|
1269
|
+
} else {
|
|
1270
|
+
suitable = __isCursorFirstLine(node, $previousNode);
|
|
1271
|
+
}
|
|
1272
|
+
}
|
|
1273
|
+
return suitable;
|
|
1274
|
+
}
|
|
1275
|
+
function __isCursorLastLine(node, $br) {
|
|
1276
|
+
let suitable = false;
|
|
1277
|
+
let $nextNode = $br.nextSibling;
|
|
1278
|
+
if ($nextNode !== null) {
|
|
1279
|
+
if ($nextNode === node) {
|
|
1280
|
+
suitable = true;
|
|
1281
|
+
} else {
|
|
1282
|
+
suitable = __isCursorLastLine(node, $nextNode);
|
|
1283
|
+
}
|
|
1284
|
+
}
|
|
1285
|
+
return suitable;
|
|
1286
|
+
}
|
|
1287
|
+
export function _hotKeyEvent(event, store) {
|
|
1288
|
+
const isControlKeyActive = event.ctrlKey || event.metaKey;
|
|
1289
|
+
if (isControlKeyActive === true) {
|
|
1290
|
+
switch (event.key) {
|
|
1291
|
+
case "b":
|
|
1292
|
+
event.preventDefault();
|
|
1293
|
+
_setDecoration("de-bold", store);
|
|
1294
|
+
break;
|
|
1295
|
+
case "i":
|
|
1296
|
+
event.preventDefault();
|
|
1297
|
+
_setDecoration("de-italic", store);
|
|
1298
|
+
break;
|
|
1299
|
+
case "u":
|
|
1300
|
+
event.preventDefault();
|
|
1301
|
+
_setDecoration("de-underline", store);
|
|
1302
|
+
break;
|
|
1303
|
+
case "s":
|
|
1304
|
+
if (event.shiftKey === true) {
|
|
1305
|
+
event.preventDefault();
|
|
1306
|
+
_setDecoration("de-strikethrough", store);
|
|
1307
|
+
}
|
|
1308
|
+
break;
|
|
1309
|
+
case "c":
|
|
1310
|
+
if (event.shiftKey === true) {
|
|
1311
|
+
event.preventDefault();
|
|
1312
|
+
_setDecoration("de-code", store);
|
|
1313
|
+
}
|
|
1314
|
+
break;
|
|
1315
|
+
}
|
|
1316
|
+
}
|
|
1317
|
+
}
|
|
1318
|
+
function __backtickEvent(event, store) {
|
|
1319
|
+
if (store.value.controlStatus.$currentBlock !== null && store.value.controlStatus.currentBlockType === "text") {
|
|
1320
|
+
const $block = store.value.controlStatus.$currentBlock;
|
|
1321
|
+
if ($block.textContent === "``") {
|
|
1322
|
+
event.preventDefault();
|
|
1323
|
+
const $newBlock = _createCodeBlock(_getDefaultBlockData("code"), store);
|
|
1324
|
+
$block.insertAdjacentElement("afterend", $newBlock);
|
|
1325
|
+
$block.remove();
|
|
1326
|
+
const $code = $newBlock.querySelector(".de-code-content");
|
|
1327
|
+
if ($code !== null) {
|
|
1328
|
+
_setCursor($code, 0);
|
|
1329
|
+
}
|
|
1330
|
+
}
|
|
1331
|
+
}
|
|
1332
|
+
}
|
|
1333
|
+
let contentKeyupEvent;
|
|
1334
|
+
export function _contentKeyupEvent(event, store) {
|
|
1335
|
+
_updateCurrentBlock(event, store);
|
|
1336
|
+
_updateCursorData(store);
|
|
1337
|
+
__checkBlock(store);
|
|
1338
|
+
store.value.eventStatus.keyboardEnterCount = 0;
|
|
1339
|
+
clearTimeout(contentKeyupEvent);
|
|
1340
|
+
contentKeyupEvent = setTimeout(() => {
|
|
1341
|
+
_updateModelData(store);
|
|
1342
|
+
}, 250);
|
|
1343
|
+
}
|
|
1344
|
+
function __checkBlock(store) {
|
|
1345
|
+
if (store.value.$body !== null) {
|
|
1346
|
+
const blockList = store.value.$body.querySelectorAll(".de-block");
|
|
1347
|
+
blockList.forEach(($block) => {
|
|
1348
|
+
const { type } = _getCurrentBlock($block);
|
|
1349
|
+
switch (type) {
|
|
1350
|
+
case "ol":
|
|
1351
|
+
case "ul":
|
|
1352
|
+
if ($block.hasChildNodes() === false) {
|
|
1353
|
+
$block.remove();
|
|
1354
|
+
}
|
|
1355
|
+
break;
|
|
1356
|
+
case "text":
|
|
1357
|
+
case "heading":
|
|
1358
|
+
if ($block.textContent === "" && $block.hasChildNodes() === true) {
|
|
1359
|
+
const textNodeType = $block.childNodes[0].constructor.name;
|
|
1360
|
+
if (textNodeType === "HTMLBRElement") {
|
|
1361
|
+
$block.innerHTML = "";
|
|
1362
|
+
}
|
|
1363
|
+
}
|
|
1364
|
+
break;
|
|
1365
|
+
}
|
|
1366
|
+
});
|
|
1367
|
+
}
|
|
1368
|
+
}
|