@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,213 +1,213 @@
|
|
|
1
|
-
// [文件] web·editor-kernel·engine·editor —— 自研富文本内核装配层:EditorCore 类(构造 / 生命周期 / 事件 / 命令派发)+ 各混入装配
|
|
2
|
-
// [约束] 由 src/surface/editor-surface.js 装配(文档应用层的编辑面外壳);业务侧只经编辑面使用内核。
|
|
3
|
-
// DOM 即文档模型:正文结构由 schema.js 在装载时规范化、序列化时剥运行期装饰,
|
|
4
|
-
// 任何外来节点(高亮壳 / 选中类名)都不得留在编辑面里。
|
|
5
|
-
// [设计] 命令一律走 exec(name, ...args):每条命令 = 一次历史提交 + 一次 update 上报,
|
|
6
|
-
// 不做链式事务包装(DOM 改动即时生效,链式只是把一次提交拆成多段,反而要为回滚造额外状态)。
|
|
7
|
-
// [设计] 内核按职责拆成多个混入文件(selection / history / interaction / tableInteraction / state / pointer / tableOps),
|
|
8
|
-
// 本文件只保留类骨架与装配:方法体仍绑在 EditorCore.prototype 上,`this` 语义与单文件版逐字一致。
|
|
9
|
-
// 带 `on*` 的事件处理器在构造时统一 bind(this),保证 addEventListener / removeEventListener 引用稳定。
|
|
10
|
-
import { imageFilesToDataUrls } from '../../util/imageFile.js';
|
|
11
|
-
import { notify } from '../../notify.js';
|
|
12
|
-
import { createSearch } from './search.js';
|
|
13
|
-
import { normalizeBody } from './schema.js';
|
|
14
|
-
import { attachInput } from './input.js';
|
|
15
|
-
import { COMMANDS } from './commands.js';
|
|
16
|
-
import * as D from './dom.js';
|
|
17
|
-
import { selectionMethods } from './selection.js';
|
|
18
|
-
import { historyMethods } from './history.js';
|
|
19
|
-
import { interactionMethods, INTERACTION_HANDLERS } from './interaction.js';
|
|
20
|
-
import { tableInteractionMethods, TABLE_INTERACTION_HANDLERS } from './tableInteraction.js';
|
|
21
|
-
import { stateMethods } from './state.js';
|
|
22
|
-
import { pointerMethods } from './pointer.js';
|
|
23
|
-
import { tableOpsMethods } from './tableOps.js';
|
|
24
|
-
|
|
25
|
-
/** 构造时需 bind(this) 的事件处理器名(挂编辑面/document 监听,引用必须稳定) */
|
|
26
|
-
const BOUND_HANDLERS = [...INTERACTION_HANDLERS, ...TABLE_INTERACTION_HANDLERS];
|
|
27
|
-
|
|
28
|
-
export class EditorCore {
|
|
29
|
-
constructor(options = {}) {
|
|
30
|
-
this.root = options.root;
|
|
31
|
-
this.placeholder = options.placeholder || '';
|
|
32
|
-
this.handlers = options.handlers || {};
|
|
33
|
-
this.undoStack = [];
|
|
34
|
-
this.redoStack = [];
|
|
35
|
-
this.pending = null;
|
|
36
|
-
this.savedRange = null;
|
|
37
|
-
this.cellRect = null;
|
|
38
|
-
this.contextCell = null;
|
|
39
|
-
this.selectedAtom = null;
|
|
40
|
-
this.cellDragFrom = null;
|
|
41
|
-
// 列宽拖拽进行中(拖拽期抑制悬停检测,免得提示线跳到别列)
|
|
42
|
-
this.colResizing = false;
|
|
43
|
-
// 悬停到列分隔线时的目标({table, colIndex}):提示线几何由 columnBoundaryBox 由此现算
|
|
44
|
-
this.hoverColumn = null;
|
|
45
|
-
// 「表 → 逻辑网格」的同帧短期缓存(见 cachedGrid):悬停热路径每帧都要网格,避免反复全量扫描
|
|
46
|
-
this._gridCache = null;
|
|
47
|
-
this._gridCachePending = false;
|
|
48
|
-
// 拖拽期挂在 document 上的收尾函数(列宽拖拽 / 单元格框选各一个):
|
|
49
|
-
// 拖到一半组件被卸载时,destroy() 无这两根指针就摘不掉,监听会一直挂着并拴住整个内核实例
|
|
50
|
-
this.dragCleanups = new Set();
|
|
51
|
-
this.modal = false;
|
|
52
|
-
this.applying = false;
|
|
53
|
-
this.typing = false;
|
|
54
|
-
// 桌面式框选进行中(宿主框选层置位):此期间选区跨块高亮归框选层管,syncBlockSelection 让位
|
|
55
|
-
this.blockMarqueeActive = false;
|
|
56
|
-
// 输入法组合中(宿主据此挂起排版重建:拆装 DOM 会打断候选状态)
|
|
57
|
-
this.composing = false;
|
|
58
|
-
this.destroyed = false;
|
|
59
|
-
// 建议修改模式(Word 应用开关;开启后 input.js 把键入拦成修订标记,见 revision.js)
|
|
60
|
-
this.suggesting = false;
|
|
61
|
-
this.listeners = new Map();
|
|
62
|
-
this.search = createSearch(this.root);
|
|
63
|
-
this.unbindInput = null;
|
|
64
|
-
this.observer = null;
|
|
65
|
-
this.idleTimer = 0;
|
|
66
|
-
// 事件处理器一次性绑定(等价原箭头函数字段的绑定语义:引用稳定、this 恒为内核实例)
|
|
67
|
-
BOUND_HANDLERS.forEach((name) => {
|
|
68
|
-
this[name] = this[name].bind(this);
|
|
69
|
-
});
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
/* ===================== 生命周期 ===================== */
|
|
73
|
-
|
|
74
|
-
mount(html = '') {
|
|
75
|
-
const root = this.root;
|
|
76
|
-
root.setAttribute('contenteditable', 'true');
|
|
77
|
-
root.setAttribute('spellcheck', 'false');
|
|
78
|
-
root.setAttribute('autocorrect', 'off');
|
|
79
|
-
root.dataset.placeholder = this.placeholder;
|
|
80
|
-
root.innerHTML = html || '';
|
|
81
|
-
normalizeBody(root, { editable: true });
|
|
82
|
-
|
|
83
|
-
this.bind();
|
|
84
|
-
this.baseline = this.snapshot();
|
|
85
|
-
this.emit('update');
|
|
86
|
-
this.emit('transaction');
|
|
87
|
-
this.syncState();
|
|
88
|
-
return this;
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
bind() {
|
|
92
|
-
const root = this.root;
|
|
93
|
-
this.observer = new MutationObserver((records) => {
|
|
94
|
-
// 运行期布局元素(Word 分页的页间占位)的增删不是文档内容变更:不入历史、不标脏
|
|
95
|
-
const onlyRuntime = records.every((r) => {
|
|
96
|
-
if (r.target instanceof Element && r.target.matches?.(D.RUNTIME_ELEMENT_SELECTOR)) return true;
|
|
97
|
-
return [...r.addedNodes, ...r.removedNodes].every((n) => n.nodeType === Node.ELEMENT_NODE && n.matches?.(D.RUNTIME_ELEMENT_SELECTOR));
|
|
98
|
-
});
|
|
99
|
-
if (!onlyRuntime) this.onDomMutated();
|
|
100
|
-
});
|
|
101
|
-
this.observer.observe(root, { childList: true, subtree: true, characterData: true });
|
|
102
|
-
|
|
103
|
-
root.addEventListener('focus', this.onFocus);
|
|
104
|
-
root.addEventListener('blur', this.onBlur);
|
|
105
|
-
root.addEventListener('mousedown', this.onMouseDown);
|
|
106
|
-
root.addEventListener('mousemove', this.onHoverColumn);
|
|
107
|
-
root.addEventListener('mouseleave', this.onLeaveColumn);
|
|
108
|
-
root.addEventListener('click', this.onClick);
|
|
109
|
-
root.addEventListener('change', this.onCheckboxChange);
|
|
110
|
-
root.addEventListener('keyup', this.onPointerState);
|
|
111
|
-
root.addEventListener('mouseup', this.onPointerState);
|
|
112
|
-
document.addEventListener('selectionchange', this.onSelectionChange);
|
|
113
|
-
this.unbindInput = attachInput(this);
|
|
114
|
-
}
|
|
115
|
-
|
|
116
|
-
destroy() {
|
|
117
|
-
if (this.destroyed) return;
|
|
118
|
-
this.destroyed = true;
|
|
119
|
-
const root = this.root;
|
|
120
|
-
this.observer?.disconnect();
|
|
121
|
-
this.hoverColumn = null;
|
|
122
|
-
this._gridCache = null;
|
|
123
|
-
this.dragCleanups.forEach((fn) => fn());
|
|
124
|
-
this.dragCleanups.clear();
|
|
125
|
-
clearTimeout(this.idleTimer);
|
|
126
|
-
root.removeEventListener('focus', this.onFocus);
|
|
127
|
-
root.removeEventListener('blur', this.onBlur);
|
|
128
|
-
root.removeEventListener('mousedown', this.onMouseDown);
|
|
129
|
-
root.removeEventListener('mousemove', this.onHoverColumn);
|
|
130
|
-
root.removeEventListener('mouseleave', this.onLeaveColumn);
|
|
131
|
-
root.removeEventListener('click', this.onClick);
|
|
132
|
-
root.removeEventListener('change', this.onCheckboxChange);
|
|
133
|
-
root.removeEventListener('keyup', this.onPointerState);
|
|
134
|
-
root.removeEventListener('mouseup', this.onPointerState);
|
|
135
|
-
document.removeEventListener('selectionchange', this.onSelectionChange);
|
|
136
|
-
this.unbindInput?.();
|
|
137
|
-
this.search.clear();
|
|
138
|
-
// 内核实例被丢弃前把事件订阅一并清空:订阅闭包持有宿主组件作用域,留着就等于把宿主钉在内存里
|
|
139
|
-
this.listeners.clear();
|
|
140
|
-
root.removeAttribute('contenteditable');
|
|
141
|
-
}
|
|
142
|
-
|
|
143
|
-
/* ===================== 事件 ===================== */
|
|
144
|
-
|
|
145
|
-
emit(event, payload) {
|
|
146
|
-
this.handlers[event]?.(payload);
|
|
147
|
-
for (const fn of this.listeners.get(event) || []) fn(payload);
|
|
148
|
-
}
|
|
149
|
-
|
|
150
|
-
on(name, fn) {
|
|
151
|
-
if (!this.listeners.has(name)) this.listeners.set(name, new Set());
|
|
152
|
-
this.listeners.get(name).add(fn);
|
|
153
|
-
}
|
|
154
|
-
|
|
155
|
-
off(name, fn) {
|
|
156
|
-
this.listeners.get(name)?.delete(fn);
|
|
157
|
-
}
|
|
158
|
-
|
|
159
|
-
/* ===================== 命令表 ===================== */
|
|
160
|
-
|
|
161
|
-
exec(name, ...args) {
|
|
162
|
-
const command = COMMANDS[name];
|
|
163
|
-
if (!command) return false;
|
|
164
|
-
if (command.raw) return Boolean(command.run(this, ...args));
|
|
165
|
-
const before = this.root.innerHTML;
|
|
166
|
-
// [坑] 命令未产生 DOM 变化(如折叠光标下的待定格式)时必须把历史整体回滚:
|
|
167
|
-
// 只 pop undoStack 会把「入栈即清空」的 redoStack 永久丢掉,撤销后无法重做
|
|
168
|
-
const undoDepth = this.undoStack.length;
|
|
169
|
-
const redoStack = this.redoStack;
|
|
170
|
-
const baseline = this.baseline;
|
|
171
|
-
let result = false;
|
|
172
|
-
this.run(() => {
|
|
173
|
-
result = command.run(this, ...args);
|
|
174
|
-
});
|
|
175
|
-
if (!result || this.root.innerHTML === before) {
|
|
176
|
-
this.undoStack.length = undoDepth;
|
|
177
|
-
this.redoStack = redoStack;
|
|
178
|
-
this.baseline = baseline;
|
|
179
|
-
}
|
|
180
|
-
return Boolean(result);
|
|
181
|
-
}
|
|
182
|
-
|
|
183
|
-
can(name, ...args) {
|
|
184
|
-
const command = COMMANDS[name];
|
|
185
|
-
if (!command) return false;
|
|
186
|
-
return command.can ? Boolean(command.can(this, ...args)) : true;
|
|
187
|
-
}
|
|
188
|
-
|
|
189
|
-
/* ===================== 图片 ===================== */
|
|
190
|
-
|
|
191
|
-
/** 图片转 base64 后按当前光标处逐张内嵌 */
|
|
192
|
-
async insertImageFiles(files) {
|
|
193
|
-
for (const { file, src } of await imageFilesToDataUrls(files, (m) => notify.error(m))) {
|
|
194
|
-
this.exec('insertImage', { src, alt: file.name || 'image' });
|
|
195
|
-
}
|
|
196
|
-
}
|
|
197
|
-
|
|
198
|
-
isMenuOpen() {
|
|
199
|
-
return this.modal;
|
|
200
|
-
}
|
|
201
|
-
}
|
|
202
|
-
|
|
203
|
-
// 各职责混入装配到原型:方法体与单文件版逐字一致,仅换承载文件(`this` 语义不变)
|
|
204
|
-
Object.assign(
|
|
205
|
-
EditorCore.prototype,
|
|
206
|
-
selectionMethods,
|
|
207
|
-
historyMethods,
|
|
208
|
-
interactionMethods,
|
|
209
|
-
tableInteractionMethods,
|
|
210
|
-
stateMethods,
|
|
211
|
-
pointerMethods,
|
|
212
|
-
tableOpsMethods,
|
|
213
|
-
);
|
|
1
|
+
// [文件] web·editor-kernel·engine·editor —— 自研富文本内核装配层:EditorCore 类(构造 / 生命周期 / 事件 / 命令派发)+ 各混入装配
|
|
2
|
+
// [约束] 由 src/surface/editor-surface.js 装配(文档应用层的编辑面外壳);业务侧只经编辑面使用内核。
|
|
3
|
+
// DOM 即文档模型:正文结构由 schema.js 在装载时规范化、序列化时剥运行期装饰,
|
|
4
|
+
// 任何外来节点(高亮壳 / 选中类名)都不得留在编辑面里。
|
|
5
|
+
// [设计] 命令一律走 exec(name, ...args):每条命令 = 一次历史提交 + 一次 update 上报,
|
|
6
|
+
// 不做链式事务包装(DOM 改动即时生效,链式只是把一次提交拆成多段,反而要为回滚造额外状态)。
|
|
7
|
+
// [设计] 内核按职责拆成多个混入文件(selection / history / interaction / tableInteraction / state / pointer / tableOps),
|
|
8
|
+
// 本文件只保留类骨架与装配:方法体仍绑在 EditorCore.prototype 上,`this` 语义与单文件版逐字一致。
|
|
9
|
+
// 带 `on*` 的事件处理器在构造时统一 bind(this),保证 addEventListener / removeEventListener 引用稳定。
|
|
10
|
+
import { imageFilesToDataUrls } from '../../util/imageFile.js';
|
|
11
|
+
import { notify } from '../../notify.js';
|
|
12
|
+
import { createSearch } from './search.js';
|
|
13
|
+
import { normalizeBody } from './schema.js';
|
|
14
|
+
import { attachInput } from './input.js';
|
|
15
|
+
import { COMMANDS } from './commands.js';
|
|
16
|
+
import * as D from './dom.js';
|
|
17
|
+
import { selectionMethods } from './selection.js';
|
|
18
|
+
import { historyMethods } from './history.js';
|
|
19
|
+
import { interactionMethods, INTERACTION_HANDLERS } from './interaction.js';
|
|
20
|
+
import { tableInteractionMethods, TABLE_INTERACTION_HANDLERS } from './tableInteraction.js';
|
|
21
|
+
import { stateMethods } from './state.js';
|
|
22
|
+
import { pointerMethods } from './pointer.js';
|
|
23
|
+
import { tableOpsMethods } from './tableOps.js';
|
|
24
|
+
|
|
25
|
+
/** 构造时需 bind(this) 的事件处理器名(挂编辑面/document 监听,引用必须稳定) */
|
|
26
|
+
const BOUND_HANDLERS = [...INTERACTION_HANDLERS, ...TABLE_INTERACTION_HANDLERS];
|
|
27
|
+
|
|
28
|
+
export class EditorCore {
|
|
29
|
+
constructor(options = {}) {
|
|
30
|
+
this.root = options.root;
|
|
31
|
+
this.placeholder = options.placeholder || '';
|
|
32
|
+
this.handlers = options.handlers || {};
|
|
33
|
+
this.undoStack = [];
|
|
34
|
+
this.redoStack = [];
|
|
35
|
+
this.pending = null;
|
|
36
|
+
this.savedRange = null;
|
|
37
|
+
this.cellRect = null;
|
|
38
|
+
this.contextCell = null;
|
|
39
|
+
this.selectedAtom = null;
|
|
40
|
+
this.cellDragFrom = null;
|
|
41
|
+
// 列宽拖拽进行中(拖拽期抑制悬停检测,免得提示线跳到别列)
|
|
42
|
+
this.colResizing = false;
|
|
43
|
+
// 悬停到列分隔线时的目标({table, colIndex}):提示线几何由 columnBoundaryBox 由此现算
|
|
44
|
+
this.hoverColumn = null;
|
|
45
|
+
// 「表 → 逻辑网格」的同帧短期缓存(见 cachedGrid):悬停热路径每帧都要网格,避免反复全量扫描
|
|
46
|
+
this._gridCache = null;
|
|
47
|
+
this._gridCachePending = false;
|
|
48
|
+
// 拖拽期挂在 document 上的收尾函数(列宽拖拽 / 单元格框选各一个):
|
|
49
|
+
// 拖到一半组件被卸载时,destroy() 无这两根指针就摘不掉,监听会一直挂着并拴住整个内核实例
|
|
50
|
+
this.dragCleanups = new Set();
|
|
51
|
+
this.modal = false;
|
|
52
|
+
this.applying = false;
|
|
53
|
+
this.typing = false;
|
|
54
|
+
// 桌面式框选进行中(宿主框选层置位):此期间选区跨块高亮归框选层管,syncBlockSelection 让位
|
|
55
|
+
this.blockMarqueeActive = false;
|
|
56
|
+
// 输入法组合中(宿主据此挂起排版重建:拆装 DOM 会打断候选状态)
|
|
57
|
+
this.composing = false;
|
|
58
|
+
this.destroyed = false;
|
|
59
|
+
// 建议修改模式(Word 应用开关;开启后 input.js 把键入拦成修订标记,见 revision.js)
|
|
60
|
+
this.suggesting = false;
|
|
61
|
+
this.listeners = new Map();
|
|
62
|
+
this.search = createSearch(this.root);
|
|
63
|
+
this.unbindInput = null;
|
|
64
|
+
this.observer = null;
|
|
65
|
+
this.idleTimer = 0;
|
|
66
|
+
// 事件处理器一次性绑定(等价原箭头函数字段的绑定语义:引用稳定、this 恒为内核实例)
|
|
67
|
+
BOUND_HANDLERS.forEach((name) => {
|
|
68
|
+
this[name] = this[name].bind(this);
|
|
69
|
+
});
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/* ===================== 生命周期 ===================== */
|
|
73
|
+
|
|
74
|
+
mount(html = '') {
|
|
75
|
+
const root = this.root;
|
|
76
|
+
root.setAttribute('contenteditable', 'true');
|
|
77
|
+
root.setAttribute('spellcheck', 'false');
|
|
78
|
+
root.setAttribute('autocorrect', 'off');
|
|
79
|
+
root.dataset.placeholder = this.placeholder;
|
|
80
|
+
root.innerHTML = html || '';
|
|
81
|
+
normalizeBody(root, { editable: true });
|
|
82
|
+
|
|
83
|
+
this.bind();
|
|
84
|
+
this.baseline = this.snapshot();
|
|
85
|
+
this.emit('update');
|
|
86
|
+
this.emit('transaction');
|
|
87
|
+
this.syncState();
|
|
88
|
+
return this;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
bind() {
|
|
92
|
+
const root = this.root;
|
|
93
|
+
this.observer = new MutationObserver((records) => {
|
|
94
|
+
// 运行期布局元素(Word 分页的页间占位)的增删不是文档内容变更:不入历史、不标脏
|
|
95
|
+
const onlyRuntime = records.every((r) => {
|
|
96
|
+
if (r.target instanceof Element && r.target.matches?.(D.RUNTIME_ELEMENT_SELECTOR)) return true;
|
|
97
|
+
return [...r.addedNodes, ...r.removedNodes].every((n) => n.nodeType === Node.ELEMENT_NODE && n.matches?.(D.RUNTIME_ELEMENT_SELECTOR));
|
|
98
|
+
});
|
|
99
|
+
if (!onlyRuntime) this.onDomMutated();
|
|
100
|
+
});
|
|
101
|
+
this.observer.observe(root, { childList: true, subtree: true, characterData: true });
|
|
102
|
+
|
|
103
|
+
root.addEventListener('focus', this.onFocus);
|
|
104
|
+
root.addEventListener('blur', this.onBlur);
|
|
105
|
+
root.addEventListener('mousedown', this.onMouseDown);
|
|
106
|
+
root.addEventListener('mousemove', this.onHoverColumn);
|
|
107
|
+
root.addEventListener('mouseleave', this.onLeaveColumn);
|
|
108
|
+
root.addEventListener('click', this.onClick);
|
|
109
|
+
root.addEventListener('change', this.onCheckboxChange);
|
|
110
|
+
root.addEventListener('keyup', this.onPointerState);
|
|
111
|
+
root.addEventListener('mouseup', this.onPointerState);
|
|
112
|
+
document.addEventListener('selectionchange', this.onSelectionChange);
|
|
113
|
+
this.unbindInput = attachInput(this);
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
destroy() {
|
|
117
|
+
if (this.destroyed) return;
|
|
118
|
+
this.destroyed = true;
|
|
119
|
+
const root = this.root;
|
|
120
|
+
this.observer?.disconnect();
|
|
121
|
+
this.hoverColumn = null;
|
|
122
|
+
this._gridCache = null;
|
|
123
|
+
this.dragCleanups.forEach((fn) => fn());
|
|
124
|
+
this.dragCleanups.clear();
|
|
125
|
+
clearTimeout(this.idleTimer);
|
|
126
|
+
root.removeEventListener('focus', this.onFocus);
|
|
127
|
+
root.removeEventListener('blur', this.onBlur);
|
|
128
|
+
root.removeEventListener('mousedown', this.onMouseDown);
|
|
129
|
+
root.removeEventListener('mousemove', this.onHoverColumn);
|
|
130
|
+
root.removeEventListener('mouseleave', this.onLeaveColumn);
|
|
131
|
+
root.removeEventListener('click', this.onClick);
|
|
132
|
+
root.removeEventListener('change', this.onCheckboxChange);
|
|
133
|
+
root.removeEventListener('keyup', this.onPointerState);
|
|
134
|
+
root.removeEventListener('mouseup', this.onPointerState);
|
|
135
|
+
document.removeEventListener('selectionchange', this.onSelectionChange);
|
|
136
|
+
this.unbindInput?.();
|
|
137
|
+
this.search.clear();
|
|
138
|
+
// 内核实例被丢弃前把事件订阅一并清空:订阅闭包持有宿主组件作用域,留着就等于把宿主钉在内存里
|
|
139
|
+
this.listeners.clear();
|
|
140
|
+
root.removeAttribute('contenteditable');
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
/* ===================== 事件 ===================== */
|
|
144
|
+
|
|
145
|
+
emit(event, payload) {
|
|
146
|
+
this.handlers[event]?.(payload);
|
|
147
|
+
for (const fn of this.listeners.get(event) || []) fn(payload);
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
on(name, fn) {
|
|
151
|
+
if (!this.listeners.has(name)) this.listeners.set(name, new Set());
|
|
152
|
+
this.listeners.get(name).add(fn);
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
off(name, fn) {
|
|
156
|
+
this.listeners.get(name)?.delete(fn);
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
/* ===================== 命令表 ===================== */
|
|
160
|
+
|
|
161
|
+
exec(name, ...args) {
|
|
162
|
+
const command = COMMANDS[name];
|
|
163
|
+
if (!command) return false;
|
|
164
|
+
if (command.raw) return Boolean(command.run(this, ...args));
|
|
165
|
+
const before = this.root.innerHTML;
|
|
166
|
+
// [坑] 命令未产生 DOM 变化(如折叠光标下的待定格式)时必须把历史整体回滚:
|
|
167
|
+
// 只 pop undoStack 会把「入栈即清空」的 redoStack 永久丢掉,撤销后无法重做
|
|
168
|
+
const undoDepth = this.undoStack.length;
|
|
169
|
+
const redoStack = this.redoStack;
|
|
170
|
+
const baseline = this.baseline;
|
|
171
|
+
let result = false;
|
|
172
|
+
this.run(() => {
|
|
173
|
+
result = command.run(this, ...args);
|
|
174
|
+
});
|
|
175
|
+
if (!result || this.root.innerHTML === before) {
|
|
176
|
+
this.undoStack.length = undoDepth;
|
|
177
|
+
this.redoStack = redoStack;
|
|
178
|
+
this.baseline = baseline;
|
|
179
|
+
}
|
|
180
|
+
return Boolean(result);
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
can(name, ...args) {
|
|
184
|
+
const command = COMMANDS[name];
|
|
185
|
+
if (!command) return false;
|
|
186
|
+
return command.can ? Boolean(command.can(this, ...args)) : true;
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
/* ===================== 图片 ===================== */
|
|
190
|
+
|
|
191
|
+
/** 图片转 base64 后按当前光标处逐张内嵌 */
|
|
192
|
+
async insertImageFiles(files) {
|
|
193
|
+
for (const { file, src } of await imageFilesToDataUrls(files, (m) => notify.error(m))) {
|
|
194
|
+
this.exec('insertImage', { src, alt: file.name || 'image' });
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
isMenuOpen() {
|
|
199
|
+
return this.modal;
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
// 各职责混入装配到原型:方法体与单文件版逐字一致,仅换承载文件(`this` 语义不变)
|
|
204
|
+
Object.assign(
|
|
205
|
+
EditorCore.prototype,
|
|
206
|
+
selectionMethods,
|
|
207
|
+
historyMethods,
|
|
208
|
+
interactionMethods,
|
|
209
|
+
tableInteractionMethods,
|
|
210
|
+
stateMethods,
|
|
211
|
+
pointerMethods,
|
|
212
|
+
tableOpsMethods,
|
|
213
|
+
);
|