@a200888213/gbdocx 0.6.1 → 0.6.3
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/LICENSE +21 -21
- package/README.md +215 -215
- package/index.html +31 -30
- package/package.json +49 -48
- package/serve.mjs +27 -23
- 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 +191 -191
- package/src/app/word/hf.js +76 -76
- package/src/app/word/layout.js +155 -155
- package/src/app/word/menus.js +201 -201
- package/src/app/word/notes.js +110 -110
- package/src/app/word/shared.js +22 -22
- package/src/app/word/view.js +159 -159
- package/src/app/word-app.js +487 -487
- package/src/app/word-sheets.js +101 -101
- package/src/kernel/docx/catalog.js +41 -41
- 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/menus.js +348 -348
- 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/docx/wordShortcuts.js +37 -37
- 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/editor.js +213 -213
- 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/search.js +113 -113
- 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/pasteClean.js +293 -293
- package/src/kernel/editor/utils.js +48 -48
- package/src/kernel/keyboard.js +114 -114
- package/src/kernel/notify.js +54 -54
- package/src/kernel/textStyles.js +84 -84
- package/src/kernel/util/clipboard.js +89 -89
- package/src/kernel/util/imageFile.js +91 -91
- package/src/kernel/util/safeValue.js +25 -25
- package/src/kernel/util/shortcutKey.js +33 -33
- package/src/surface/editor-surface.js +603 -600
- package/src/ui/color-panel.js +69 -69
- package/src/ui/context-menu.js +135 -133
- package/src/ui/dialogs.js +249 -249
- package/src/ui/find-bar.js +151 -151
- package/src/ui/icons.js +245 -137
- package/src/ui/menubar.js +28 -28
- package/src/ui/panels.js +115 -115
- package/src/ui/popup.js +79 -79
- 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/app.css +970 -970
- package/styles/editor-content.css +440 -440
- package/styles/tokens.css +154 -154
- package/styles/word-panel.css +410 -410
|
@@ -1,113 +1,113 @@
|
|
|
1
|
-
// [文件] web·editor-kernel·engine·search —— 查找替换:命中采集 + CSS Custom Highlight 高亮(零正文改写)
|
|
2
|
-
// [设计] 高亮绝不能往正文里塞 `<mark>`:内核以 DOM 为文档模型,外来节点会被读进正文并随序列化落盘。
|
|
3
|
-
// CSS.highlights 只登记 Range,样式靠 ::highlight() 伪元素画,编辑面 DOM 一个字节都不动。
|
|
4
|
-
// [坑] Custom Highlight API 需 Chrome 105+ / Safari 17.2+ / Firefox 125+;不支持时只失去高亮,
|
|
5
|
-
// 计数、跳转、替换仍全部可用(区间一样由 Range 驱动)。
|
|
6
|
-
// [设计] 匹配只在单个文本块内,不跨块跨单元格:跨块命中无法用一个连续 Range 表达,
|
|
7
|
-
// 且「一句话被段落切断」的匹配对用户没有意义。
|
|
8
|
-
|
|
9
|
-
import { allTextBlocks, blockText, rangeInBlock } from './dom.js';
|
|
10
|
-
|
|
11
|
-
const ALL = 'richtext-search';
|
|
12
|
-
const CURRENT = 'richtext-search-current';
|
|
13
|
-
|
|
14
|
-
const supported = () => typeof Highlight !== 'undefined' && typeof CSS !== 'undefined' && CSS.highlights;
|
|
15
|
-
|
|
16
|
-
export function createSearch(root) {
|
|
17
|
-
let keyword = '';
|
|
18
|
-
let caseSensitive = false;
|
|
19
|
-
let activeIndex = -1;
|
|
20
|
-
let ranges = [];
|
|
21
|
-
|
|
22
|
-
const collect = () => {
|
|
23
|
-
ranges = [];
|
|
24
|
-
if (!keyword) return;
|
|
25
|
-
const needle = caseSensitive ? keyword : keyword.toLowerCase();
|
|
26
|
-
for (const block of allTextBlocks(root)) {
|
|
27
|
-
const text = blockText(block);
|
|
28
|
-
const haystack = caseSensitive ? text : text.toLowerCase();
|
|
29
|
-
let index = haystack.indexOf(needle);
|
|
30
|
-
while (index !== -1) {
|
|
31
|
-
ranges.push(rangeInBlock(block, index, index + needle.length));
|
|
32
|
-
index = haystack.indexOf(needle, index + needle.length);
|
|
33
|
-
}
|
|
34
|
-
}
|
|
35
|
-
activeIndex = ranges.length ? Math.min(Math.max(activeIndex, 0), ranges.length - 1) : -1;
|
|
36
|
-
};
|
|
37
|
-
|
|
38
|
-
const paint = () => {
|
|
39
|
-
if (!supported()) return;
|
|
40
|
-
CSS.highlights.delete(ALL);
|
|
41
|
-
CSS.highlights.delete(CURRENT);
|
|
42
|
-
if (!ranges.length) return;
|
|
43
|
-
CSS.highlights.set(ALL, new Highlight(...ranges));
|
|
44
|
-
if (activeIndex >= 0 && ranges[activeIndex]) CSS.highlights.set(CURRENT, new Highlight(ranges[activeIndex]));
|
|
45
|
-
};
|
|
46
|
-
|
|
47
|
-
/** 正文变化后重算命中(编辑器 update 时调用) */
|
|
48
|
-
const refresh = () => {
|
|
49
|
-
collect();
|
|
50
|
-
paint();
|
|
51
|
-
};
|
|
52
|
-
|
|
53
|
-
const setKeyword = (value, options = {}) => {
|
|
54
|
-
keyword = String(value ?? '');
|
|
55
|
-
if (options.caseSensitive !== undefined) caseSensitive = Boolean(options.caseSensitive);
|
|
56
|
-
activeIndex = keyword ? 0 : -1;
|
|
57
|
-
refresh();
|
|
58
|
-
return snapshot();
|
|
59
|
-
};
|
|
60
|
-
|
|
61
|
-
const clear = () => {
|
|
62
|
-
keyword = '';
|
|
63
|
-
activeIndex = -1;
|
|
64
|
-
ranges = [];
|
|
65
|
-
if (supported()) {
|
|
66
|
-
CSS.highlights.delete(ALL);
|
|
67
|
-
CSS.highlights.delete(CURRENT);
|
|
68
|
-
}
|
|
69
|
-
};
|
|
70
|
-
|
|
71
|
-
const step = (dir) => {
|
|
72
|
-
if (!ranges.length) return false;
|
|
73
|
-
activeIndex = (activeIndex + dir + ranges.length) % ranges.length;
|
|
74
|
-
paint();
|
|
75
|
-
scrollToActive();
|
|
76
|
-
return true;
|
|
77
|
-
};
|
|
78
|
-
|
|
79
|
-
const snapshot = () => ({ keyword, total: ranges.length, activeIndex });
|
|
80
|
-
|
|
81
|
-
const replaceAt = (index, text) => {
|
|
82
|
-
const range = ranges[index];
|
|
83
|
-
if (!range) return false;
|
|
84
|
-
const node = document.createTextNode(String(text ?? ''));
|
|
85
|
-
range.deleteContents();
|
|
86
|
-
range.insertNode(node);
|
|
87
|
-
return true;
|
|
88
|
-
};
|
|
89
|
-
|
|
90
|
-
/** 替换当前命中并保持停在同一序号(下一条自动落在原位置之后) */
|
|
91
|
-
const replaceCurrent = (text) => {
|
|
92
|
-
if (!replaceAt(activeIndex, text)) return false;
|
|
93
|
-
refresh();
|
|
94
|
-
return true;
|
|
95
|
-
};
|
|
96
|
-
|
|
97
|
-
/** 全部替换:倒序改文,前面的偏移不受后面改动影响 */
|
|
98
|
-
const replaceAll = (text) => {
|
|
99
|
-
if (!ranges.length) return false;
|
|
100
|
-
for (let i = ranges.length - 1; i >= 0; i -= 1) replaceAt(i, text);
|
|
101
|
-
refresh();
|
|
102
|
-
return true;
|
|
103
|
-
};
|
|
104
|
-
|
|
105
|
-
function scrollToActive() {
|
|
106
|
-
const range = ranges[activeIndex];
|
|
107
|
-
if (!range) return;
|
|
108
|
-
const node = range.startContainer.nodeType === Node.TEXT_NODE ? range.startContainer.parentElement : range.startContainer;
|
|
109
|
-
node?.scrollIntoView?.({ block: 'center', behavior: 'smooth' });
|
|
110
|
-
}
|
|
111
|
-
|
|
112
|
-
return { refresh, setKeyword, clear, snapshot, next: () => step(1), prev: () => step(-1), replaceCurrent, replaceAll, scrollToActive };
|
|
113
|
-
}
|
|
1
|
+
// [文件] web·editor-kernel·engine·search —— 查找替换:命中采集 + CSS Custom Highlight 高亮(零正文改写)
|
|
2
|
+
// [设计] 高亮绝不能往正文里塞 `<mark>`:内核以 DOM 为文档模型,外来节点会被读进正文并随序列化落盘。
|
|
3
|
+
// CSS.highlights 只登记 Range,样式靠 ::highlight() 伪元素画,编辑面 DOM 一个字节都不动。
|
|
4
|
+
// [坑] Custom Highlight API 需 Chrome 105+ / Safari 17.2+ / Firefox 125+;不支持时只失去高亮,
|
|
5
|
+
// 计数、跳转、替换仍全部可用(区间一样由 Range 驱动)。
|
|
6
|
+
// [设计] 匹配只在单个文本块内,不跨块跨单元格:跨块命中无法用一个连续 Range 表达,
|
|
7
|
+
// 且「一句话被段落切断」的匹配对用户没有意义。
|
|
8
|
+
|
|
9
|
+
import { allTextBlocks, blockText, rangeInBlock } from './dom.js';
|
|
10
|
+
|
|
11
|
+
const ALL = 'richtext-search';
|
|
12
|
+
const CURRENT = 'richtext-search-current';
|
|
13
|
+
|
|
14
|
+
const supported = () => typeof Highlight !== 'undefined' && typeof CSS !== 'undefined' && CSS.highlights;
|
|
15
|
+
|
|
16
|
+
export function createSearch(root) {
|
|
17
|
+
let keyword = '';
|
|
18
|
+
let caseSensitive = false;
|
|
19
|
+
let activeIndex = -1;
|
|
20
|
+
let ranges = [];
|
|
21
|
+
|
|
22
|
+
const collect = () => {
|
|
23
|
+
ranges = [];
|
|
24
|
+
if (!keyword) return;
|
|
25
|
+
const needle = caseSensitive ? keyword : keyword.toLowerCase();
|
|
26
|
+
for (const block of allTextBlocks(root)) {
|
|
27
|
+
const text = blockText(block);
|
|
28
|
+
const haystack = caseSensitive ? text : text.toLowerCase();
|
|
29
|
+
let index = haystack.indexOf(needle);
|
|
30
|
+
while (index !== -1) {
|
|
31
|
+
ranges.push(rangeInBlock(block, index, index + needle.length));
|
|
32
|
+
index = haystack.indexOf(needle, index + needle.length);
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
activeIndex = ranges.length ? Math.min(Math.max(activeIndex, 0), ranges.length - 1) : -1;
|
|
36
|
+
};
|
|
37
|
+
|
|
38
|
+
const paint = () => {
|
|
39
|
+
if (!supported()) return;
|
|
40
|
+
CSS.highlights.delete(ALL);
|
|
41
|
+
CSS.highlights.delete(CURRENT);
|
|
42
|
+
if (!ranges.length) return;
|
|
43
|
+
CSS.highlights.set(ALL, new Highlight(...ranges));
|
|
44
|
+
if (activeIndex >= 0 && ranges[activeIndex]) CSS.highlights.set(CURRENT, new Highlight(ranges[activeIndex]));
|
|
45
|
+
};
|
|
46
|
+
|
|
47
|
+
/** 正文变化后重算命中(编辑器 update 时调用) */
|
|
48
|
+
const refresh = () => {
|
|
49
|
+
collect();
|
|
50
|
+
paint();
|
|
51
|
+
};
|
|
52
|
+
|
|
53
|
+
const setKeyword = (value, options = {}) => {
|
|
54
|
+
keyword = String(value ?? '');
|
|
55
|
+
if (options.caseSensitive !== undefined) caseSensitive = Boolean(options.caseSensitive);
|
|
56
|
+
activeIndex = keyword ? 0 : -1;
|
|
57
|
+
refresh();
|
|
58
|
+
return snapshot();
|
|
59
|
+
};
|
|
60
|
+
|
|
61
|
+
const clear = () => {
|
|
62
|
+
keyword = '';
|
|
63
|
+
activeIndex = -1;
|
|
64
|
+
ranges = [];
|
|
65
|
+
if (supported()) {
|
|
66
|
+
CSS.highlights.delete(ALL);
|
|
67
|
+
CSS.highlights.delete(CURRENT);
|
|
68
|
+
}
|
|
69
|
+
};
|
|
70
|
+
|
|
71
|
+
const step = (dir) => {
|
|
72
|
+
if (!ranges.length) return false;
|
|
73
|
+
activeIndex = (activeIndex + dir + ranges.length) % ranges.length;
|
|
74
|
+
paint();
|
|
75
|
+
scrollToActive();
|
|
76
|
+
return true;
|
|
77
|
+
};
|
|
78
|
+
|
|
79
|
+
const snapshot = () => ({ keyword, total: ranges.length, activeIndex });
|
|
80
|
+
|
|
81
|
+
const replaceAt = (index, text) => {
|
|
82
|
+
const range = ranges[index];
|
|
83
|
+
if (!range) return false;
|
|
84
|
+
const node = document.createTextNode(String(text ?? ''));
|
|
85
|
+
range.deleteContents();
|
|
86
|
+
range.insertNode(node);
|
|
87
|
+
return true;
|
|
88
|
+
};
|
|
89
|
+
|
|
90
|
+
/** 替换当前命中并保持停在同一序号(下一条自动落在原位置之后) */
|
|
91
|
+
const replaceCurrent = (text) => {
|
|
92
|
+
if (!replaceAt(activeIndex, text)) return false;
|
|
93
|
+
refresh();
|
|
94
|
+
return true;
|
|
95
|
+
};
|
|
96
|
+
|
|
97
|
+
/** 全部替换:倒序改文,前面的偏移不受后面改动影响 */
|
|
98
|
+
const replaceAll = (text) => {
|
|
99
|
+
if (!ranges.length) return false;
|
|
100
|
+
for (let i = ranges.length - 1; i >= 0; i -= 1) replaceAt(i, text);
|
|
101
|
+
refresh();
|
|
102
|
+
return true;
|
|
103
|
+
};
|
|
104
|
+
|
|
105
|
+
function scrollToActive() {
|
|
106
|
+
const range = ranges[activeIndex];
|
|
107
|
+
if (!range) return;
|
|
108
|
+
const node = range.startContainer.nodeType === Node.TEXT_NODE ? range.startContainer.parentElement : range.startContainer;
|
|
109
|
+
node?.scrollIntoView?.({ block: 'center', behavior: 'smooth' });
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
return { refresh, setKeyword, clear, snapshot, next: () => step(1), prev: () => step(-1), replaceCurrent, replaceAll, scrollToActive };
|
|
113
|
+
}
|
|
@@ -1,119 +1,119 @@
|
|
|
1
|
-
// [文件] web·editor-kernel·engine·selection —— 内核内容与选区:序列化 / 程序化装载 / 选区捕获与复原(混入 EditorCore.prototype)
|
|
2
|
-
// [设计] 选区快照分两份:节点引用(startNode 等)只服务「本次命令的选区接续」,路径(start/end.path)供历史快照跨 DOM 重建,
|
|
3
|
-
// 历史不带 node,避免攥住脱离文档的节点。
|
|
4
|
-
import * as D from './dom.js';
|
|
5
|
-
import { normalizeBody, serializeBody } from './schema.js';
|
|
6
|
-
|
|
7
|
-
/** 内容与选区方法(装配到 EditorCore.prototype;this 为内核实例) */
|
|
8
|
-
export const selectionMethods = {
|
|
9
|
-
getHTML() {
|
|
10
|
-
return serializeBody(this.root);
|
|
11
|
-
},
|
|
12
|
-
|
|
13
|
-
/** 程序化装载(切档 / 外部写入):重置历史与选区 */
|
|
14
|
-
setHTML(html) {
|
|
15
|
-
const root = this.root;
|
|
16
|
-
this.applying = true;
|
|
17
|
-
root.innerHTML = html || '';
|
|
18
|
-
normalizeBody(root, { editable: true });
|
|
19
|
-
this.pending = null;
|
|
20
|
-
this.cellRect = null;
|
|
21
|
-
// 换文档后右键命中格/拖拽起点的旧元素都脱离 DOM,留着会让下一次表格动作命中失效
|
|
22
|
-
this.contextCell = null;
|
|
23
|
-
this.cellDragFrom = null;
|
|
24
|
-
this.clearAtom();
|
|
25
|
-
this.undoStack = [];
|
|
26
|
-
this.redoStack = [];
|
|
27
|
-
this.baseline = this.snapshot();
|
|
28
|
-
setTimeout(() => {
|
|
29
|
-
this.applying = false;
|
|
30
|
-
}, 0);
|
|
31
|
-
this.search.refresh();
|
|
32
|
-
this.syncState();
|
|
33
|
-
},
|
|
34
|
-
|
|
35
|
-
/**
|
|
36
|
-
* 按快照路径复原选区
|
|
37
|
-
* [坑] 块命令会整块换标签或包进列表,原光标所在的文本节点随之脱离文档;不补这一步,
|
|
38
|
-
* 用户点「标题 / 列表」之后光标就丢了,接着打字会落到编辑器外
|
|
39
|
-
*/
|
|
40
|
-
applyCaptured(sel) {
|
|
41
|
-
if (!sel?.start?.path) return;
|
|
42
|
-
const start = this.pointBlock(sel.startNode) || this.pathBlock(sel.start.path);
|
|
43
|
-
if (!start) return;
|
|
44
|
-
const end = this.pointBlock(sel.endNode) || this.pathBlock(sel.end?.path || []) || start;
|
|
45
|
-
D.selectRange(
|
|
46
|
-
this.root,
|
|
47
|
-
{ block: start, offset: this.offsetIn(start, sel.startNode, sel.startOffset, sel.start.offset) },
|
|
48
|
-
{ block: end, offset: this.offsetIn(end, sel.endNode, sel.endOffset, sel.end?.offset ?? sel.start.offset) }
|
|
49
|
-
);
|
|
50
|
-
},
|
|
51
|
-
|
|
52
|
-
/** 元素还在原位就用它,否则退回按路径找 */
|
|
53
|
-
pointBlock(node) {
|
|
54
|
-
if (!node || !this.root.contains(node)) return null;
|
|
55
|
-
return D.blockOf(node, this.root) || this.textBlockAt(node.nodeType === Node.ELEMENT_NODE ? node : node.parentElement);
|
|
56
|
-
},
|
|
57
|
-
|
|
58
|
-
pathBlock(path) {
|
|
59
|
-
return this.textBlockAt(D.resolvePath(this.root, path));
|
|
60
|
-
},
|
|
61
|
-
|
|
62
|
-
offsetIn(block, node, nodeOffset, fallback) {
|
|
63
|
-
if (node && this.root.contains(node) && (block === node || block.contains(node))) return D.charOffset(block, node, nodeOffset);
|
|
64
|
-
return fallback ?? 0;
|
|
65
|
-
},
|
|
66
|
-
|
|
67
|
-
/** 路径落点可能已是容器(包进列表 / 引用后),要下沉到真正的文本块 */
|
|
68
|
-
textBlockAt(el) {
|
|
69
|
-
if (!el || !this.root.contains(el)) return null;
|
|
70
|
-
return D.isTextBlock(el, this.root) ? el : D.firstTextBlockIn(el, this.root);
|
|
71
|
-
},
|
|
72
|
-
|
|
73
|
-
isFocused() {
|
|
74
|
-
return document.activeElement === this.root || this.root.contains(document.activeElement);
|
|
75
|
-
},
|
|
76
|
-
|
|
77
|
-
focus() {
|
|
78
|
-
this.root.focus({ preventScroll: true });
|
|
79
|
-
if (!D.rangeOf(this.root) && this.savedRange) this.applyCaptured(this.savedRange);
|
|
80
|
-
},
|
|
81
|
-
|
|
82
|
-
selectionRanges() {
|
|
83
|
-
return D.blocksInRange(this.root);
|
|
84
|
-
},
|
|
85
|
-
|
|
86
|
-
/**
|
|
87
|
-
* 程序化改动后把选区接回原位
|
|
88
|
-
* [坑] 两种塌法都要接:① 整块换标签后浏览器把选区挪到父节点(丢文本块);
|
|
89
|
-
* ② 行内重建后选区被压成折叠光标(再点一次同一个标记会因为「选区没了」而取消不掉)
|
|
90
|
-
*/
|
|
91
|
-
keepSelection(beforeSel) {
|
|
92
|
-
if (!beforeSel?.start) return;
|
|
93
|
-
const live = D.rangeOf(this.root);
|
|
94
|
-
const liveBlock = live && D.blockOf(live.startContainer, this.root);
|
|
95
|
-
if (!liveBlock) {
|
|
96
|
-
this.applyCaptured(beforeSel);
|
|
97
|
-
return;
|
|
98
|
-
}
|
|
99
|
-
const wasRange = beforeSel.end && (beforeSel.end.path.join() !== beforeSel.start.path.join() || beforeSel.end.offset !== beforeSel.start.offset);
|
|
100
|
-
if (wasRange && live.collapsed) this.applyCaptured(beforeSel);
|
|
101
|
-
},
|
|
102
|
-
|
|
103
|
-
/** 当前选区(含折叠光标);工具栏抢走焦点后据此复原 */
|
|
104
|
-
captureSelection() {
|
|
105
|
-
const range = D.rangeOf(this.root);
|
|
106
|
-
if (!range) return null;
|
|
107
|
-
const start = blockPoint(this.root, range.startContainer, range.startOffset);
|
|
108
|
-
const end = blockPoint(this.root, range.endContainer, range.endOffset);
|
|
109
|
-
if (!start) return null;
|
|
110
|
-
// 节点引用只服务本次命令的选区接续(历史快照另存,不带 node,避免攥住脱离文档的节点)
|
|
111
|
-
return { start, end, startNode: range.startContainer, startOffset: range.startOffset, endNode: range.endContainer, endOffset: range.endOffset };
|
|
112
|
-
},
|
|
113
|
-
};
|
|
114
|
-
|
|
115
|
-
function blockPoint(root, node, offset) {
|
|
116
|
-
const block = D.blockOf(node, root);
|
|
117
|
-
if (!block) return null;
|
|
118
|
-
return { path: D.pathOf(block, root), offset: D.charOffset(block, node, offset) };
|
|
119
|
-
}
|
|
1
|
+
// [文件] web·editor-kernel·engine·selection —— 内核内容与选区:序列化 / 程序化装载 / 选区捕获与复原(混入 EditorCore.prototype)
|
|
2
|
+
// [设计] 选区快照分两份:节点引用(startNode 等)只服务「本次命令的选区接续」,路径(start/end.path)供历史快照跨 DOM 重建,
|
|
3
|
+
// 历史不带 node,避免攥住脱离文档的节点。
|
|
4
|
+
import * as D from './dom.js';
|
|
5
|
+
import { normalizeBody, serializeBody } from './schema.js';
|
|
6
|
+
|
|
7
|
+
/** 内容与选区方法(装配到 EditorCore.prototype;this 为内核实例) */
|
|
8
|
+
export const selectionMethods = {
|
|
9
|
+
getHTML() {
|
|
10
|
+
return serializeBody(this.root);
|
|
11
|
+
},
|
|
12
|
+
|
|
13
|
+
/** 程序化装载(切档 / 外部写入):重置历史与选区 */
|
|
14
|
+
setHTML(html) {
|
|
15
|
+
const root = this.root;
|
|
16
|
+
this.applying = true;
|
|
17
|
+
root.innerHTML = html || '';
|
|
18
|
+
normalizeBody(root, { editable: true });
|
|
19
|
+
this.pending = null;
|
|
20
|
+
this.cellRect = null;
|
|
21
|
+
// 换文档后右键命中格/拖拽起点的旧元素都脱离 DOM,留着会让下一次表格动作命中失效
|
|
22
|
+
this.contextCell = null;
|
|
23
|
+
this.cellDragFrom = null;
|
|
24
|
+
this.clearAtom();
|
|
25
|
+
this.undoStack = [];
|
|
26
|
+
this.redoStack = [];
|
|
27
|
+
this.baseline = this.snapshot();
|
|
28
|
+
setTimeout(() => {
|
|
29
|
+
this.applying = false;
|
|
30
|
+
}, 0);
|
|
31
|
+
this.search.refresh();
|
|
32
|
+
this.syncState();
|
|
33
|
+
},
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* 按快照路径复原选区
|
|
37
|
+
* [坑] 块命令会整块换标签或包进列表,原光标所在的文本节点随之脱离文档;不补这一步,
|
|
38
|
+
* 用户点「标题 / 列表」之后光标就丢了,接着打字会落到编辑器外
|
|
39
|
+
*/
|
|
40
|
+
applyCaptured(sel) {
|
|
41
|
+
if (!sel?.start?.path) return;
|
|
42
|
+
const start = this.pointBlock(sel.startNode) || this.pathBlock(sel.start.path);
|
|
43
|
+
if (!start) return;
|
|
44
|
+
const end = this.pointBlock(sel.endNode) || this.pathBlock(sel.end?.path || []) || start;
|
|
45
|
+
D.selectRange(
|
|
46
|
+
this.root,
|
|
47
|
+
{ block: start, offset: this.offsetIn(start, sel.startNode, sel.startOffset, sel.start.offset) },
|
|
48
|
+
{ block: end, offset: this.offsetIn(end, sel.endNode, sel.endOffset, sel.end?.offset ?? sel.start.offset) }
|
|
49
|
+
);
|
|
50
|
+
},
|
|
51
|
+
|
|
52
|
+
/** 元素还在原位就用它,否则退回按路径找 */
|
|
53
|
+
pointBlock(node) {
|
|
54
|
+
if (!node || !this.root.contains(node)) return null;
|
|
55
|
+
return D.blockOf(node, this.root) || this.textBlockAt(node.nodeType === Node.ELEMENT_NODE ? node : node.parentElement);
|
|
56
|
+
},
|
|
57
|
+
|
|
58
|
+
pathBlock(path) {
|
|
59
|
+
return this.textBlockAt(D.resolvePath(this.root, path));
|
|
60
|
+
},
|
|
61
|
+
|
|
62
|
+
offsetIn(block, node, nodeOffset, fallback) {
|
|
63
|
+
if (node && this.root.contains(node) && (block === node || block.contains(node))) return D.charOffset(block, node, nodeOffset);
|
|
64
|
+
return fallback ?? 0;
|
|
65
|
+
},
|
|
66
|
+
|
|
67
|
+
/** 路径落点可能已是容器(包进列表 / 引用后),要下沉到真正的文本块 */
|
|
68
|
+
textBlockAt(el) {
|
|
69
|
+
if (!el || !this.root.contains(el)) return null;
|
|
70
|
+
return D.isTextBlock(el, this.root) ? el : D.firstTextBlockIn(el, this.root);
|
|
71
|
+
},
|
|
72
|
+
|
|
73
|
+
isFocused() {
|
|
74
|
+
return document.activeElement === this.root || this.root.contains(document.activeElement);
|
|
75
|
+
},
|
|
76
|
+
|
|
77
|
+
focus() {
|
|
78
|
+
this.root.focus({ preventScroll: true });
|
|
79
|
+
if (!D.rangeOf(this.root) && this.savedRange) this.applyCaptured(this.savedRange);
|
|
80
|
+
},
|
|
81
|
+
|
|
82
|
+
selectionRanges() {
|
|
83
|
+
return D.blocksInRange(this.root);
|
|
84
|
+
},
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* 程序化改动后把选区接回原位
|
|
88
|
+
* [坑] 两种塌法都要接:① 整块换标签后浏览器把选区挪到父节点(丢文本块);
|
|
89
|
+
* ② 行内重建后选区被压成折叠光标(再点一次同一个标记会因为「选区没了」而取消不掉)
|
|
90
|
+
*/
|
|
91
|
+
keepSelection(beforeSel) {
|
|
92
|
+
if (!beforeSel?.start) return;
|
|
93
|
+
const live = D.rangeOf(this.root);
|
|
94
|
+
const liveBlock = live && D.blockOf(live.startContainer, this.root);
|
|
95
|
+
if (!liveBlock) {
|
|
96
|
+
this.applyCaptured(beforeSel);
|
|
97
|
+
return;
|
|
98
|
+
}
|
|
99
|
+
const wasRange = beforeSel.end && (beforeSel.end.path.join() !== beforeSel.start.path.join() || beforeSel.end.offset !== beforeSel.start.offset);
|
|
100
|
+
if (wasRange && live.collapsed) this.applyCaptured(beforeSel);
|
|
101
|
+
},
|
|
102
|
+
|
|
103
|
+
/** 当前选区(含折叠光标);工具栏抢走焦点后据此复原 */
|
|
104
|
+
captureSelection() {
|
|
105
|
+
const range = D.rangeOf(this.root);
|
|
106
|
+
if (!range) return null;
|
|
107
|
+
const start = blockPoint(this.root, range.startContainer, range.startOffset);
|
|
108
|
+
const end = blockPoint(this.root, range.endContainer, range.endOffset);
|
|
109
|
+
if (!start) return null;
|
|
110
|
+
// 节点引用只服务本次命令的选区接续(历史快照另存,不带 node,避免攥住脱离文档的节点)
|
|
111
|
+
return { start, end, startNode: range.startContainer, startOffset: range.startOffset, endNode: range.endContainer, endOffset: range.endOffset };
|
|
112
|
+
},
|
|
113
|
+
};
|
|
114
|
+
|
|
115
|
+
function blockPoint(root, node, offset) {
|
|
116
|
+
const block = D.blockOf(node, root);
|
|
117
|
+
if (!block) return null;
|
|
118
|
+
return { path: D.pathOf(block, root), offset: D.charOffset(block, node, offset) };
|
|
119
|
+
}
|