@a200888213/gbdocx 0.5.2 → 0.6.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 +215 -207
- package/index.html +3 -2
- package/package.json +48 -48
- package/serve.mjs +3 -2
- package/src/app/file-io.js +122 -122
- package/src/app/main.js +9 -9
- package/src/app/word/catalog.js +52 -52
- package/src/app/word/comments.js +112 -112
- package/src/app/word/doc.js +113 -113
- package/src/app/word/edit.js +72 -72
- package/src/app/word/file.js +30 -1
- package/src/app/word/hf.js +76 -76
- package/src/app/word/layout.js +155 -155
- package/src/app/word/notes.js +110 -110
- package/src/app/word/shared.js +22 -22
- package/src/app/word/view.js +9 -2
- package/src/app/word-app.js +487 -469
- package/src/app/word-sheets.js +101 -101
- package/src/kernel/docx/codec/blocks.js +123 -123
- package/src/kernel/docx/codec/carry.js +129 -129
- package/src/kernel/docx/codec/comments.js +25 -25
- package/src/kernel/docx/codec/docx.js +198 -198
- package/src/kernel/docx/codec/docxStyles.js +177 -177
- package/src/kernel/docx/codec/fonts.js +195 -195
- package/src/kernel/docx/codec/helpers.js +161 -161
- package/src/kernel/docx/codec/hfHtml.js +152 -152
- package/src/kernel/docx/codec/html.js +739 -739
- package/src/kernel/docx/codec/losses.js +420 -420
- package/src/kernel/docx/codec/markdown.js +170 -170
- package/src/kernel/docx/codec/parts.js +249 -249
- package/src/kernel/docx/codec/read.js +603 -603
- package/src/kernel/docx/codec/render.js +1156 -1156
- package/src/kernel/docx/codec/roundtrip.js +236 -236
- package/src/kernel/docx/codec/write.js +833 -833
- package/src/kernel/docx/comments.js +14 -14
- package/src/kernel/docx/pageGeometry.js +123 -123
- package/src/kernel/docx/pagination.js +481 -481
- package/src/kernel/docx/watermark.js +45 -45
- package/src/kernel/editor/constants.js +8 -8
- package/src/kernel/editor/engine/blocks.js +594 -594
- package/src/kernel/editor/engine/blocksEnter.js +109 -109
- package/src/kernel/editor/engine/blocksInsert.js +254 -254
- package/src/kernel/editor/engine/blocksStructure.js +226 -226
- package/src/kernel/editor/engine/commands.js +181 -181
- package/src/kernel/editor/engine/dom.js +646 -646
- package/src/kernel/editor/engine/history.js +149 -149
- package/src/kernel/editor/engine/inline.js +656 -656
- package/src/kernel/editor/engine/input.js +554 -554
- package/src/kernel/editor/engine/interaction.js +137 -137
- package/src/kernel/editor/engine/pointer.js +39 -39
- package/src/kernel/editor/engine/revision.js +138 -138
- package/src/kernel/editor/engine/schema.js +417 -417
- package/src/kernel/editor/engine/selection.js +119 -119
- package/src/kernel/editor/engine/state.js +118 -118
- package/src/kernel/editor/engine/tableClip.js +154 -154
- package/src/kernel/editor/engine/tableGrid.js +55 -55
- package/src/kernel/editor/engine/tableInteraction.js +388 -388
- package/src/kernel/editor/engine/tableMenu.js +250 -250
- package/src/kernel/editor/engine/tableOps.js +174 -174
- package/src/kernel/editor/engine/tables.js +732 -732
- package/src/kernel/editor/overlayGeometry.js +20 -20
- package/src/kernel/editor/utils.js +48 -48
- package/src/kernel/keyboard.js +114 -114
- package/src/kernel/textStyles.js +84 -84
- package/src/kernel/util/imageFile.js +91 -91
- package/src/kernel/util/shortcutKey.js +33 -33
- package/src/surface/editor-surface.js +600 -600
- package/src/ui/color-panel.js +69 -69
- package/src/ui/context-menu.js +133 -133
- package/src/ui/find-bar.js +151 -151
- package/src/ui/icons.js +137 -135
- package/src/ui/menubar.js +28 -28
- package/src/ui/panels.js +115 -115
- package/src/ui/prompt.js +89 -89
- package/src/ui/ribbon.js +485 -485
- package/src/ui/special-chars.js +39 -39
- package/src/ui/statusbar.js +81 -81
- package/src/ui/toolbar.js +32 -32
- package/styles/editor-content.css +440 -440
- package/styles/tokens.css +154 -154
|
@@ -1,226 +1,226 @@
|
|
|
1
|
-
// [文件] web·editor-kernel·blocks·结构操作 —— 复制/删除/移动/缩进出缩/光标拆块(含块收尾 settleBlock)
|
|
2
|
-
import { blockLength, blockOf, charOffset, domPoint, hasBlockBody, isBlockEmpty, isCell, isCodeContent, isRuntimeBlock, isTaskItem, mergeSplitFamily, rangeOf, moveChildren, setCaret, RUNTIME_SPLIT_ATTR } from './dom.js';
|
|
3
|
-
import { copyBlockStyle, liftOneOutOf, listItemOf, liveTarget, makeCheckbox, noteContainerOf, lastContainerBlock, pruneEmpty, quoteOf, releaseListItem, structureTargets, taskContentHost } from './blocks.js';
|
|
4
|
-
import { emptyParagraph } from './blocksInsert.js';
|
|
5
|
-
import { insertHardBreak, insertNewlineInCode } from './blocksEnter.js';
|
|
6
|
-
|
|
7
|
-
/* ===================== 结构操作(复制 / 删除 / 移动 / 拖拽) ===================== */
|
|
8
|
-
|
|
9
|
-
export function duplicateBlock(root, el) {
|
|
10
|
-
const target = liveTarget(root, el) || structureTargets(root)[0];
|
|
11
|
-
if (!target) return false;
|
|
12
|
-
const clone = target.cloneNode(true);
|
|
13
|
-
clone.querySelectorAll('input').forEach((input) => {
|
|
14
|
-
if (input.checked) input.setAttribute('checked', '');
|
|
15
|
-
else input.removeAttribute('checked');
|
|
16
|
-
});
|
|
17
|
-
target.after(clone);
|
|
18
|
-
return true;
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
export function deleteBlock(root, el) {
|
|
22
|
-
const target = liveTarget(root, el) || structureTargets(root)[0];
|
|
23
|
-
if (!target) return false;
|
|
24
|
-
const parent = target.parentElement;
|
|
25
|
-
// [坑] 只有「根层唯一块」才整篇换成空段落:嵌套在列表/引用里的块删掉时整篇清空是数据事故
|
|
26
|
-
if (parent === root && root.children.length <= 1) {
|
|
27
|
-
const p = emptyParagraph();
|
|
28
|
-
root.replaceChildren(p);
|
|
29
|
-
setCaret(p, 0);
|
|
30
|
-
return true;
|
|
31
|
-
}
|
|
32
|
-
// 落点跳过运行期布局产物(Word 页间缝):光标落进缝里,下次重排会连缝一起被 flatten 删掉
|
|
33
|
-
const siblings = Array.from(parent.children).filter((node) => !isRuntimeBlock(node));
|
|
34
|
-
const index = siblings.indexOf(target);
|
|
35
|
-
const next = siblings[index + 1] || siblings[index - 1];
|
|
36
|
-
target.remove();
|
|
37
|
-
if (parent !== root) pruneEmpty(parent);
|
|
38
|
-
if (!root.children.length) {
|
|
39
|
-
const p = emptyParagraph();
|
|
40
|
-
root.replaceChildren(p);
|
|
41
|
-
setCaret(p, 0);
|
|
42
|
-
return true;
|
|
43
|
-
}
|
|
44
|
-
if (next?.isConnected) {
|
|
45
|
-
const block = blockOf(next, root) || next;
|
|
46
|
-
setCaret(block, 0);
|
|
47
|
-
} else {
|
|
48
|
-
const fallback = root.lastElementChild;
|
|
49
|
-
if (fallback) setCaret(blockOf(fallback, root) || fallback, 0);
|
|
50
|
-
}
|
|
51
|
-
return true;
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
export function moveBlock(root, el, dir) {
|
|
55
|
-
const target = liveTarget(root, el) || structureTargets(root)[0];
|
|
56
|
-
if (!target) return false;
|
|
57
|
-
// 跳过运行期布局产物(Word 分页的页间缝):与缝对调等于没动,重排后位置不变(表现为「点了没反应」)
|
|
58
|
-
const siblings = Array.from(target.parentElement.children).filter((node) => !isRuntimeBlock(node));
|
|
59
|
-
const swap = siblings[siblings.indexOf(target) + dir];
|
|
60
|
-
if (!swap) return false;
|
|
61
|
-
// [坑] 方向不能反:把目标塞到兄弟之前/之后才是「移动」,反过来是空操作
|
|
62
|
-
if (dir < 0) swap.before(target);
|
|
63
|
-
else swap.after(target);
|
|
64
|
-
return true;
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
/** 拖拽落点:把 el 搬到 ref 前 / 后 */
|
|
68
|
-
export function moveBlockTo(el, ref, before) {
|
|
69
|
-
if (!el || !ref || el === ref || el.contains(ref) || ref.contains(el)) return false;
|
|
70
|
-
if (before) ref.before(el);
|
|
71
|
-
else ref.after(el);
|
|
72
|
-
return true;
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
/**
|
|
76
|
-
* 表格拖拽落点:跨页续表先并回首块再搬家
|
|
77
|
-
* [坑] 只搬当前碎片会丢行序:下次重排 flatten 按文档序并回,被搬走的碎片若落到尾块之后,
|
|
78
|
-
* 接续行会被并到错误的表上——必须先整家族并回,用首块整体搬
|
|
79
|
-
* @param {HTMLElement} root 编辑面根
|
|
80
|
-
* @param {HTMLElement} table 命中的表(可能是续表碎片)
|
|
81
|
-
* @param {HTMLElement} ref 落点块
|
|
82
|
-
* @param {boolean} before true=插到 ref 前,false=插到 ref 后
|
|
83
|
-
*/
|
|
84
|
-
export function moveTableTo(root, table, ref, before) {
|
|
85
|
-
if (!table?.isConnected || !ref?.isConnected) return false;
|
|
86
|
-
if (table === ref || table.contains(ref) || ref.contains(table)) return false;
|
|
87
|
-
// 落点还是同一家族的续表碎片:并回后它会消失,等于搬了个空——先拒绝
|
|
88
|
-
const split = table.getAttribute(RUNTIME_SPLIT_ATTR);
|
|
89
|
-
if (split && ref.getAttribute(RUNTIME_SPLIT_ATTR) === split) return false;
|
|
90
|
-
return moveBlockTo(mergeSplitFamily(root, table), ref, before);
|
|
91
|
-
}
|
|
92
|
-
|
|
93
|
-
/* ===================== 列表升降级(Tab / Shift+Tab) ===================== */
|
|
94
|
-
|
|
95
|
-
export function sinkListItem(li) {
|
|
96
|
-
const list = li.parentElement;
|
|
97
|
-
const prev = li.previousElementSibling;
|
|
98
|
-
if (!list || !prev) return false;
|
|
99
|
-
let nested = Array.from(prev.children).find((child) => child.tagName === list.tagName && child.getAttribute('data-type') === list.getAttribute('data-type'));
|
|
100
|
-
if (!nested) {
|
|
101
|
-
nested = document.createElement(list.tagName);
|
|
102
|
-
if (list.hasAttribute('data-type')) nested.setAttribute('data-type', list.getAttribute('data-type'));
|
|
103
|
-
(isTaskItem(prev) ? taskContentHost(prev) : prev).appendChild(nested);
|
|
104
|
-
}
|
|
105
|
-
nested.appendChild(li);
|
|
106
|
-
return true;
|
|
107
|
-
}
|
|
108
|
-
|
|
109
|
-
/** 提升一级(已在顶层返回 false,由调用方改走退出列表) */
|
|
110
|
-
export function liftListItem(li) {
|
|
111
|
-
const list = li.parentElement;
|
|
112
|
-
if (!list || (list.tagName !== 'UL' && list.tagName !== 'OL')) return false;
|
|
113
|
-
const outer = list.parentElement;
|
|
114
|
-
if (outer?.tagName !== 'LI') return false;
|
|
115
|
-
const style = li.getAttribute('style');
|
|
116
|
-
if (isTaskItem(li)) {
|
|
117
|
-
const p = document.createElement('p');
|
|
118
|
-
const content = taskContentHost(li);
|
|
119
|
-
moveChildren(content, p);
|
|
120
|
-
if (style) p.setAttribute('style', style);
|
|
121
|
-
li.remove();
|
|
122
|
-
outer.after(p);
|
|
123
|
-
} else {
|
|
124
|
-
outer.after(li);
|
|
125
|
-
}
|
|
126
|
-
pruneEmpty(list);
|
|
127
|
-
return true;
|
|
128
|
-
}
|
|
129
|
-
|
|
130
|
-
/* ===================== 回车 / 退格 ===================== */
|
|
131
|
-
|
|
132
|
-
/**
|
|
133
|
-
* 在光标处拆块(接管浏览器默认 Enter:Chrome 默认会造出 `<div>`,产物不干净)
|
|
134
|
-
* @param {boolean} shift Shift+Enter 只插换行
|
|
135
|
-
*/
|
|
136
|
-
export function splitAtCaret(root, shift = false) {
|
|
137
|
-
const range = rangeOf(root);
|
|
138
|
-
if (!range || !range.collapsed) return false;
|
|
139
|
-
const block = blockOf(range.startContainer, root);
|
|
140
|
-
if (!block) return false;
|
|
141
|
-
|
|
142
|
-
if (shift) return insertHardBreak(range);
|
|
143
|
-
if (isCodeContent(block)) return insertNewlineInCode(block, range);
|
|
144
|
-
if (isCell(block)) return insertHardBreak(range);
|
|
145
|
-
|
|
146
|
-
const li = listItemOf(block, root);
|
|
147
|
-
// 空条目再回车 = 退出列表(否则永远退不出去)
|
|
148
|
-
if (li && isBlockEmpty(block)) {
|
|
149
|
-
// [坑] releaseListItem 会把 li 换成新段落并删掉旧 li:光标必须落到返回的新段落上
|
|
150
|
-
const p = releaseListItem(li);
|
|
151
|
-
setCaret(p, 0);
|
|
152
|
-
return true;
|
|
153
|
-
}
|
|
154
|
-
const quote = quoteOf(block, root);
|
|
155
|
-
if (quote && isBlockEmpty(block)) {
|
|
156
|
-
liftOneOutOf(quote, block);
|
|
157
|
-
pruneEmpty(quote);
|
|
158
|
-
setCaret(block, 0);
|
|
159
|
-
return true;
|
|
160
|
-
}
|
|
161
|
-
// 高亮块/超级块:空块回车退出容器(与行首退格同口径)
|
|
162
|
-
const nc = noteContainerOf(block, root);
|
|
163
|
-
if (nc && isBlockEmpty(block)) {
|
|
164
|
-
liftOneOutOf(nc, block);
|
|
165
|
-
pruneEmpty(nc);
|
|
166
|
-
setCaret(block, 0);
|
|
167
|
-
return true;
|
|
168
|
-
}
|
|
169
|
-
// 高亮块/超级块末尾回车:退出到容器之后(不在容器内续段);blockquote 不纳入(noteContainerOf 已排除)
|
|
170
|
-
if (nc && block === lastContainerBlock(nc) && charOffset(block, range.startContainer, range.startOffset) >= blockLength(block)) {
|
|
171
|
-
const after = document.createElement('p');
|
|
172
|
-
after.appendChild(document.createElement('br'));
|
|
173
|
-
nc.after(after);
|
|
174
|
-
setCaret(after, 0);
|
|
175
|
-
return true;
|
|
176
|
-
}
|
|
177
|
-
|
|
178
|
-
const tail = document.createRange();
|
|
179
|
-
tail.setStart(range.startContainer, range.startOffset);
|
|
180
|
-
const end = domPoint(block, blockLength(block));
|
|
181
|
-
tail.setEnd(end.node, end.offset);
|
|
182
|
-
const moved = tail.extractContents();
|
|
183
|
-
|
|
184
|
-
if (li) {
|
|
185
|
-
const next = document.createElement('li');
|
|
186
|
-
copyBlockStyle(li, next);
|
|
187
|
-
if (isTaskItem(li)) {
|
|
188
|
-
next.setAttribute('data-checked', 'false');
|
|
189
|
-
const { label } = makeCheckbox();
|
|
190
|
-
const div = document.createElement('div');
|
|
191
|
-
div.appendChild(moved);
|
|
192
|
-
next.append(label, div);
|
|
193
|
-
li.after(next);
|
|
194
|
-
settleBlock(block);
|
|
195
|
-
setCaret(div, 0);
|
|
196
|
-
} else {
|
|
197
|
-
next.appendChild(moved);
|
|
198
|
-
li.after(next);
|
|
199
|
-
settleBlock(block);
|
|
200
|
-
setCaret(next, 0);
|
|
201
|
-
}
|
|
202
|
-
return true;
|
|
203
|
-
}
|
|
204
|
-
|
|
205
|
-
// 标题回车落正文,其余同标签续块(引用内仍是段落并列在同一个引用里)
|
|
206
|
-
const next = document.createElement('p');
|
|
207
|
-
copyBlockStyle(block, next);
|
|
208
|
-
next.appendChild(moved);
|
|
209
|
-
block.after(next);
|
|
210
|
-
settleBlock(block);
|
|
211
|
-
settleBlock(next);
|
|
212
|
-
setCaret(next, 0);
|
|
213
|
-
return true;
|
|
214
|
-
}
|
|
215
|
-
|
|
216
|
-
/** 拆完若块里没正文了要补 br(extractContents 常留下空文本节点,光看 childNodes.length 会漏判) */
|
|
217
|
-
export function settleBlock(block) {
|
|
218
|
-
if (!block.isConnected) return;
|
|
219
|
-
// 拆块时尾巴可能整段取自行内容器之外,留下一个空壳搬进新块(`<strong></strong>丙`):
|
|
220
|
-
// 空壳没有正文也没有原子,留着只会跟着序列化落盘,一律剪掉
|
|
221
|
-
for (const el of Array.from(block.querySelectorAll('strong,b,em,i,u,s,code,a,del,strike,span'))) {
|
|
222
|
-
if (!hasBlockBody(el)) el.remove();
|
|
223
|
-
}
|
|
224
|
-
if (hasBlockBody(block)) return;
|
|
225
|
-
block.replaceChildren(document.createElement('br'));
|
|
226
|
-
}
|
|
1
|
+
// [文件] web·editor-kernel·blocks·结构操作 —— 复制/删除/移动/缩进出缩/光标拆块(含块收尾 settleBlock)
|
|
2
|
+
import { blockLength, blockOf, charOffset, domPoint, hasBlockBody, isBlockEmpty, isCell, isCodeContent, isRuntimeBlock, isTaskItem, mergeSplitFamily, rangeOf, moveChildren, setCaret, RUNTIME_SPLIT_ATTR } from './dom.js';
|
|
3
|
+
import { copyBlockStyle, liftOneOutOf, listItemOf, liveTarget, makeCheckbox, noteContainerOf, lastContainerBlock, pruneEmpty, quoteOf, releaseListItem, structureTargets, taskContentHost } from './blocks.js';
|
|
4
|
+
import { emptyParagraph } from './blocksInsert.js';
|
|
5
|
+
import { insertHardBreak, insertNewlineInCode } from './blocksEnter.js';
|
|
6
|
+
|
|
7
|
+
/* ===================== 结构操作(复制 / 删除 / 移动 / 拖拽) ===================== */
|
|
8
|
+
|
|
9
|
+
export function duplicateBlock(root, el) {
|
|
10
|
+
const target = liveTarget(root, el) || structureTargets(root)[0];
|
|
11
|
+
if (!target) return false;
|
|
12
|
+
const clone = target.cloneNode(true);
|
|
13
|
+
clone.querySelectorAll('input').forEach((input) => {
|
|
14
|
+
if (input.checked) input.setAttribute('checked', '');
|
|
15
|
+
else input.removeAttribute('checked');
|
|
16
|
+
});
|
|
17
|
+
target.after(clone);
|
|
18
|
+
return true;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export function deleteBlock(root, el) {
|
|
22
|
+
const target = liveTarget(root, el) || structureTargets(root)[0];
|
|
23
|
+
if (!target) return false;
|
|
24
|
+
const parent = target.parentElement;
|
|
25
|
+
// [坑] 只有「根层唯一块」才整篇换成空段落:嵌套在列表/引用里的块删掉时整篇清空是数据事故
|
|
26
|
+
if (parent === root && root.children.length <= 1) {
|
|
27
|
+
const p = emptyParagraph();
|
|
28
|
+
root.replaceChildren(p);
|
|
29
|
+
setCaret(p, 0);
|
|
30
|
+
return true;
|
|
31
|
+
}
|
|
32
|
+
// 落点跳过运行期布局产物(Word 页间缝):光标落进缝里,下次重排会连缝一起被 flatten 删掉
|
|
33
|
+
const siblings = Array.from(parent.children).filter((node) => !isRuntimeBlock(node));
|
|
34
|
+
const index = siblings.indexOf(target);
|
|
35
|
+
const next = siblings[index + 1] || siblings[index - 1];
|
|
36
|
+
target.remove();
|
|
37
|
+
if (parent !== root) pruneEmpty(parent);
|
|
38
|
+
if (!root.children.length) {
|
|
39
|
+
const p = emptyParagraph();
|
|
40
|
+
root.replaceChildren(p);
|
|
41
|
+
setCaret(p, 0);
|
|
42
|
+
return true;
|
|
43
|
+
}
|
|
44
|
+
if (next?.isConnected) {
|
|
45
|
+
const block = blockOf(next, root) || next;
|
|
46
|
+
setCaret(block, 0);
|
|
47
|
+
} else {
|
|
48
|
+
const fallback = root.lastElementChild;
|
|
49
|
+
if (fallback) setCaret(blockOf(fallback, root) || fallback, 0);
|
|
50
|
+
}
|
|
51
|
+
return true;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
export function moveBlock(root, el, dir) {
|
|
55
|
+
const target = liveTarget(root, el) || structureTargets(root)[0];
|
|
56
|
+
if (!target) return false;
|
|
57
|
+
// 跳过运行期布局产物(Word 分页的页间缝):与缝对调等于没动,重排后位置不变(表现为「点了没反应」)
|
|
58
|
+
const siblings = Array.from(target.parentElement.children).filter((node) => !isRuntimeBlock(node));
|
|
59
|
+
const swap = siblings[siblings.indexOf(target) + dir];
|
|
60
|
+
if (!swap) return false;
|
|
61
|
+
// [坑] 方向不能反:把目标塞到兄弟之前/之后才是「移动」,反过来是空操作
|
|
62
|
+
if (dir < 0) swap.before(target);
|
|
63
|
+
else swap.after(target);
|
|
64
|
+
return true;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/** 拖拽落点:把 el 搬到 ref 前 / 后 */
|
|
68
|
+
export function moveBlockTo(el, ref, before) {
|
|
69
|
+
if (!el || !ref || el === ref || el.contains(ref) || ref.contains(el)) return false;
|
|
70
|
+
if (before) ref.before(el);
|
|
71
|
+
else ref.after(el);
|
|
72
|
+
return true;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* 表格拖拽落点:跨页续表先并回首块再搬家
|
|
77
|
+
* [坑] 只搬当前碎片会丢行序:下次重排 flatten 按文档序并回,被搬走的碎片若落到尾块之后,
|
|
78
|
+
* 接续行会被并到错误的表上——必须先整家族并回,用首块整体搬
|
|
79
|
+
* @param {HTMLElement} root 编辑面根
|
|
80
|
+
* @param {HTMLElement} table 命中的表(可能是续表碎片)
|
|
81
|
+
* @param {HTMLElement} ref 落点块
|
|
82
|
+
* @param {boolean} before true=插到 ref 前,false=插到 ref 后
|
|
83
|
+
*/
|
|
84
|
+
export function moveTableTo(root, table, ref, before) {
|
|
85
|
+
if (!table?.isConnected || !ref?.isConnected) return false;
|
|
86
|
+
if (table === ref || table.contains(ref) || ref.contains(table)) return false;
|
|
87
|
+
// 落点还是同一家族的续表碎片:并回后它会消失,等于搬了个空——先拒绝
|
|
88
|
+
const split = table.getAttribute(RUNTIME_SPLIT_ATTR);
|
|
89
|
+
if (split && ref.getAttribute(RUNTIME_SPLIT_ATTR) === split) return false;
|
|
90
|
+
return moveBlockTo(mergeSplitFamily(root, table), ref, before);
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
/* ===================== 列表升降级(Tab / Shift+Tab) ===================== */
|
|
94
|
+
|
|
95
|
+
export function sinkListItem(li) {
|
|
96
|
+
const list = li.parentElement;
|
|
97
|
+
const prev = li.previousElementSibling;
|
|
98
|
+
if (!list || !prev) return false;
|
|
99
|
+
let nested = Array.from(prev.children).find((child) => child.tagName === list.tagName && child.getAttribute('data-type') === list.getAttribute('data-type'));
|
|
100
|
+
if (!nested) {
|
|
101
|
+
nested = document.createElement(list.tagName);
|
|
102
|
+
if (list.hasAttribute('data-type')) nested.setAttribute('data-type', list.getAttribute('data-type'));
|
|
103
|
+
(isTaskItem(prev) ? taskContentHost(prev) : prev).appendChild(nested);
|
|
104
|
+
}
|
|
105
|
+
nested.appendChild(li);
|
|
106
|
+
return true;
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
/** 提升一级(已在顶层返回 false,由调用方改走退出列表) */
|
|
110
|
+
export function liftListItem(li) {
|
|
111
|
+
const list = li.parentElement;
|
|
112
|
+
if (!list || (list.tagName !== 'UL' && list.tagName !== 'OL')) return false;
|
|
113
|
+
const outer = list.parentElement;
|
|
114
|
+
if (outer?.tagName !== 'LI') return false;
|
|
115
|
+
const style = li.getAttribute('style');
|
|
116
|
+
if (isTaskItem(li)) {
|
|
117
|
+
const p = document.createElement('p');
|
|
118
|
+
const content = taskContentHost(li);
|
|
119
|
+
moveChildren(content, p);
|
|
120
|
+
if (style) p.setAttribute('style', style);
|
|
121
|
+
li.remove();
|
|
122
|
+
outer.after(p);
|
|
123
|
+
} else {
|
|
124
|
+
outer.after(li);
|
|
125
|
+
}
|
|
126
|
+
pruneEmpty(list);
|
|
127
|
+
return true;
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
/* ===================== 回车 / 退格 ===================== */
|
|
131
|
+
|
|
132
|
+
/**
|
|
133
|
+
* 在光标处拆块(接管浏览器默认 Enter:Chrome 默认会造出 `<div>`,产物不干净)
|
|
134
|
+
* @param {boolean} shift Shift+Enter 只插换行
|
|
135
|
+
*/
|
|
136
|
+
export function splitAtCaret(root, shift = false) {
|
|
137
|
+
const range = rangeOf(root);
|
|
138
|
+
if (!range || !range.collapsed) return false;
|
|
139
|
+
const block = blockOf(range.startContainer, root);
|
|
140
|
+
if (!block) return false;
|
|
141
|
+
|
|
142
|
+
if (shift) return insertHardBreak(range);
|
|
143
|
+
if (isCodeContent(block)) return insertNewlineInCode(block, range);
|
|
144
|
+
if (isCell(block)) return insertHardBreak(range);
|
|
145
|
+
|
|
146
|
+
const li = listItemOf(block, root);
|
|
147
|
+
// 空条目再回车 = 退出列表(否则永远退不出去)
|
|
148
|
+
if (li && isBlockEmpty(block)) {
|
|
149
|
+
// [坑] releaseListItem 会把 li 换成新段落并删掉旧 li:光标必须落到返回的新段落上
|
|
150
|
+
const p = releaseListItem(li);
|
|
151
|
+
setCaret(p, 0);
|
|
152
|
+
return true;
|
|
153
|
+
}
|
|
154
|
+
const quote = quoteOf(block, root);
|
|
155
|
+
if (quote && isBlockEmpty(block)) {
|
|
156
|
+
liftOneOutOf(quote, block);
|
|
157
|
+
pruneEmpty(quote);
|
|
158
|
+
setCaret(block, 0);
|
|
159
|
+
return true;
|
|
160
|
+
}
|
|
161
|
+
// 高亮块/超级块:空块回车退出容器(与行首退格同口径)
|
|
162
|
+
const nc = noteContainerOf(block, root);
|
|
163
|
+
if (nc && isBlockEmpty(block)) {
|
|
164
|
+
liftOneOutOf(nc, block);
|
|
165
|
+
pruneEmpty(nc);
|
|
166
|
+
setCaret(block, 0);
|
|
167
|
+
return true;
|
|
168
|
+
}
|
|
169
|
+
// 高亮块/超级块末尾回车:退出到容器之后(不在容器内续段);blockquote 不纳入(noteContainerOf 已排除)
|
|
170
|
+
if (nc && block === lastContainerBlock(nc) && charOffset(block, range.startContainer, range.startOffset) >= blockLength(block)) {
|
|
171
|
+
const after = document.createElement('p');
|
|
172
|
+
after.appendChild(document.createElement('br'));
|
|
173
|
+
nc.after(after);
|
|
174
|
+
setCaret(after, 0);
|
|
175
|
+
return true;
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
const tail = document.createRange();
|
|
179
|
+
tail.setStart(range.startContainer, range.startOffset);
|
|
180
|
+
const end = domPoint(block, blockLength(block));
|
|
181
|
+
tail.setEnd(end.node, end.offset);
|
|
182
|
+
const moved = tail.extractContents();
|
|
183
|
+
|
|
184
|
+
if (li) {
|
|
185
|
+
const next = document.createElement('li');
|
|
186
|
+
copyBlockStyle(li, next);
|
|
187
|
+
if (isTaskItem(li)) {
|
|
188
|
+
next.setAttribute('data-checked', 'false');
|
|
189
|
+
const { label } = makeCheckbox();
|
|
190
|
+
const div = document.createElement('div');
|
|
191
|
+
div.appendChild(moved);
|
|
192
|
+
next.append(label, div);
|
|
193
|
+
li.after(next);
|
|
194
|
+
settleBlock(block);
|
|
195
|
+
setCaret(div, 0);
|
|
196
|
+
} else {
|
|
197
|
+
next.appendChild(moved);
|
|
198
|
+
li.after(next);
|
|
199
|
+
settleBlock(block);
|
|
200
|
+
setCaret(next, 0);
|
|
201
|
+
}
|
|
202
|
+
return true;
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
// 标题回车落正文,其余同标签续块(引用内仍是段落并列在同一个引用里)
|
|
206
|
+
const next = document.createElement('p');
|
|
207
|
+
copyBlockStyle(block, next);
|
|
208
|
+
next.appendChild(moved);
|
|
209
|
+
block.after(next);
|
|
210
|
+
settleBlock(block);
|
|
211
|
+
settleBlock(next);
|
|
212
|
+
setCaret(next, 0);
|
|
213
|
+
return true;
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
/** 拆完若块里没正文了要补 br(extractContents 常留下空文本节点,光看 childNodes.length 会漏判) */
|
|
217
|
+
export function settleBlock(block) {
|
|
218
|
+
if (!block.isConnected) return;
|
|
219
|
+
// 拆块时尾巴可能整段取自行内容器之外,留下一个空壳搬进新块(`<strong></strong>丙`):
|
|
220
|
+
// 空壳没有正文也没有原子,留着只会跟着序列化落盘,一律剪掉
|
|
221
|
+
for (const el of Array.from(block.querySelectorAll('strong,b,em,i,u,s,code,a,del,strike,span'))) {
|
|
222
|
+
if (!hasBlockBody(el)) el.remove();
|
|
223
|
+
}
|
|
224
|
+
if (hasBlockBody(block)) return;
|
|
225
|
+
block.replaceChildren(document.createElement('br'));
|
|
226
|
+
}
|