@a200888213/gbdocx 0.0.0-stage → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +205 -2
- package/index.html +24 -0
- package/package.json +47 -6
- package/serve.mjs +20 -0
- package/src/app/file-io.js +122 -0
- package/src/app/main.js +9 -0
- package/src/app/word/catalog.js +52 -0
- package/src/app/word/comments.js +112 -0
- package/src/app/word/doc.js +113 -0
- package/src/app/word/edit.js +72 -0
- package/src/app/word/file.js +162 -0
- package/src/app/word/hf.js +76 -0
- package/src/app/word/layout.js +152 -0
- package/src/app/word/menus.js +175 -0
- package/src/app/word/notes.js +110 -0
- package/src/app/word/shared.js +22 -0
- package/src/app/word/view.js +88 -0
- package/src/app/word-app.js +440 -0
- package/src/app/word-sheets.js +101 -0
- package/src/kernel/docx/catalog.js +41 -0
- package/src/kernel/docx/codec/blocks.js +123 -0
- package/src/kernel/docx/codec/carry.js +129 -0
- package/src/kernel/docx/codec/comments.js +25 -0
- package/src/kernel/docx/codec/docx.js +198 -0
- package/src/kernel/docx/codec/docxStyles.js +177 -0
- package/src/kernel/docx/codec/fonts.js +195 -0
- package/src/kernel/docx/codec/helpers.js +161 -0
- package/src/kernel/docx/codec/hfHtml.js +152 -0
- package/src/kernel/docx/codec/html.js +739 -0
- package/src/kernel/docx/codec/losses.js +420 -0
- package/src/kernel/docx/codec/markdown.js +170 -0
- package/src/kernel/docx/codec/parts.js +249 -0
- package/src/kernel/docx/codec/read.js +603 -0
- package/src/kernel/docx/codec/render.js +1156 -0
- package/src/kernel/docx/codec/roundtrip.js +236 -0
- package/src/kernel/docx/codec/write.js +833 -0
- package/src/kernel/docx/comments.js +14 -0
- package/src/kernel/docx/constants.js +63 -0
- package/src/kernel/docx/menus.js +211 -0
- package/src/kernel/docx/pageGeometry.js +121 -0
- package/src/kernel/docx/pagination.js +481 -0
- package/src/kernel/docx/watermark.js +45 -0
- package/src/kernel/docx/wordShortcuts.js +37 -0
- package/src/kernel/editor/constants.js +8 -0
- package/src/kernel/editor/engine/blocks.js +594 -0
- package/src/kernel/editor/engine/blocksEnter.js +109 -0
- package/src/kernel/editor/engine/blocksInsert.js +254 -0
- package/src/kernel/editor/engine/blocksStructure.js +208 -0
- package/src/kernel/editor/engine/commands.js +180 -0
- package/src/kernel/editor/engine/dom.js +622 -0
- package/src/kernel/editor/engine/editor.js +213 -0
- package/src/kernel/editor/engine/history.js +149 -0
- package/src/kernel/editor/engine/inline.js +656 -0
- package/src/kernel/editor/engine/input.js +554 -0
- package/src/kernel/editor/engine/interaction.js +137 -0
- package/src/kernel/editor/engine/pointer.js +39 -0
- package/src/kernel/editor/engine/revision.js +138 -0
- package/src/kernel/editor/engine/schema.js +417 -0
- package/src/kernel/editor/engine/search.js +113 -0
- package/src/kernel/editor/engine/selection.js +119 -0
- package/src/kernel/editor/engine/state.js +118 -0
- package/src/kernel/editor/engine/tableClip.js +154 -0
- package/src/kernel/editor/engine/tableGrid.js +55 -0
- package/src/kernel/editor/engine/tableInteraction.js +371 -0
- package/src/kernel/editor/engine/tableMenu.js +250 -0
- package/src/kernel/editor/engine/tableOps.js +178 -0
- package/src/kernel/editor/engine/tables.js +732 -0
- package/src/kernel/editor/overlayGeometry.js +20 -0
- package/src/kernel/editor/pasteClean.js +293 -0
- package/src/kernel/editor/utils.js +49 -0
- package/src/kernel/keyboard.js +114 -0
- package/src/kernel/notify.js +54 -0
- package/src/kernel/textStyles.js +84 -0
- package/src/kernel/util/clipboard.js +89 -0
- package/src/kernel/util/imageFile.js +91 -0
- package/src/kernel/util/safeValue.js +25 -0
- package/src/kernel/util/shortcutKey.js +33 -0
- package/src/surface/editor-surface.js +469 -0
- package/src/ui/color-panel.js +69 -0
- package/src/ui/context-menu.js +130 -0
- package/src/ui/dialogs.js +249 -0
- package/src/ui/find-bar.js +151 -0
- package/src/ui/icons.js +36 -0
- package/src/ui/menubar.js +28 -0
- package/src/ui/panels.js +115 -0
- package/src/ui/popup.js +79 -0
- package/src/ui/prompt.js +89 -0
- package/src/ui/special-chars.js +39 -0
- package/src/ui/statusbar.js +81 -0
- package/src/ui/toolbar.js +199 -0
- package/styles/app.css +944 -0
- package/styles/editor-content.css +440 -0
- package/styles/tokens.css +154 -0
- package/styles/word-panel.css +450 -0
|
@@ -0,0 +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
|
+
);
|
|
@@ -0,0 +1,149 @@
|
|
|
1
|
+
// [文件] web·editor-kernel·engine·history —— 内核历史栈与编辑态:一次改写 = 一次历史提交 + 一次变更上报(混入 EditorCore.prototype)
|
|
2
|
+
// [设计] 历史以「整篇 innerHTML 快照」为记账单位,配合 typing 合并窗口把连续输入合成一条撤销记录。
|
|
3
|
+
import { normalizeBody } from './schema.js';
|
|
4
|
+
|
|
5
|
+
/** 历史栈深度上限 */
|
|
6
|
+
const HISTORY_LIMIT = 120;
|
|
7
|
+
|
|
8
|
+
/** 连续输入合入同一条历史记录的空闲窗口 */
|
|
9
|
+
const TYPING_MERGE_MS = 600;
|
|
10
|
+
|
|
11
|
+
/** 历史与编辑态方法(装配到 EditorCore.prototype;this 为内核实例) */
|
|
12
|
+
export const historyMethods = {
|
|
13
|
+
snapshot() {
|
|
14
|
+
return { html: this.root.innerHTML, sel: this.captureSelection() };
|
|
15
|
+
},
|
|
16
|
+
|
|
17
|
+
pushHistory() {
|
|
18
|
+
if (!this.baseline) return;
|
|
19
|
+
const last = this.undoStack[this.undoStack.length - 1];
|
|
20
|
+
// [坑] 同状态重复入栈会让「撤销」看起来没反应(弹出来的是改动后的快照)
|
|
21
|
+
if (last && last.html === this.baseline.html) return;
|
|
22
|
+
this.undoStack.push(this.baseline);
|
|
23
|
+
if (this.undoStack.length > HISTORY_LIMIT) this.undoStack.shift();
|
|
24
|
+
this.redoStack = [];
|
|
25
|
+
},
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* 执行一次 DOM 改写:入历史 + 上报变更
|
|
29
|
+
* @param {Function} mutate 直接操作 DOM 的回调
|
|
30
|
+
* @param {{merge?:boolean, coalesce?:boolean}} options merge=true 合入当前输入会话(输入规则不另起一条撤销记录);
|
|
31
|
+
* coalesce=true 合入调用方自己开好的一步(拖列宽这类逐帧动作,整次手势只留一条撤销记录)
|
|
32
|
+
*/
|
|
33
|
+
run(mutate, options = {}) {
|
|
34
|
+
if (this.destroyed) return false;
|
|
35
|
+
if (!options.coalesce && !(options.merge && this.typing)) this.pushHistory();
|
|
36
|
+
const beforeSel = this.captureSelection();
|
|
37
|
+
this.applying = true;
|
|
38
|
+
this.typing = false;
|
|
39
|
+
clearTimeout(this.idleTimer);
|
|
40
|
+
try {
|
|
41
|
+
mutate();
|
|
42
|
+
} finally {
|
|
43
|
+
Promise.resolve().then(() => {
|
|
44
|
+
this.applying = false;
|
|
45
|
+
});
|
|
46
|
+
}
|
|
47
|
+
this.keepSelection(beforeSel);
|
|
48
|
+
this.baseline = this.snapshot();
|
|
49
|
+
this.afterChange();
|
|
50
|
+
return true;
|
|
51
|
+
},
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* 静默改写:运行期布局(Word 分页的占位 / 栏容器 / 拆块)专用——改写照跑,
|
|
55
|
+
* 但观察者记账窗口整个关掉:不入历史、不标脏、不上报 update
|
|
56
|
+
*/
|
|
57
|
+
quietly(mutate) {
|
|
58
|
+
if (this.destroyed) return;
|
|
59
|
+
this.applying = true;
|
|
60
|
+
try {
|
|
61
|
+
mutate();
|
|
62
|
+
} finally {
|
|
63
|
+
// [坑] 与 run() 同一口径:观察者回调是微任务,同步解锁会让分页产物被判成正文变更
|
|
64
|
+
Promise.resolve().then(() => {
|
|
65
|
+
this.applying = false;
|
|
66
|
+
});
|
|
67
|
+
}
|
|
68
|
+
},
|
|
69
|
+
|
|
70
|
+
/** 建议修改模式开关(开启后键入/删除被拦成修订标记;Word 应用的状态栏与菜单驱动) */
|
|
71
|
+
setSuggesting(on) {
|
|
72
|
+
this.suggesting = Boolean(on);
|
|
73
|
+
this.syncState();
|
|
74
|
+
return true;
|
|
75
|
+
},
|
|
76
|
+
|
|
77
|
+
/** 当前是否可写(只读档由 setEditable(false) 落到 contenteditable;工具栏/浮层可用性统一问它) */
|
|
78
|
+
isEditable() {
|
|
79
|
+
return this.root.getAttribute('contenteditable') !== 'false';
|
|
80
|
+
},
|
|
81
|
+
|
|
82
|
+
/** 只读开关(切 contenteditable;按钮可用性由宿主按自身档位决定) */
|
|
83
|
+
setEditable(on) {
|
|
84
|
+
this.root.setAttribute('contenteditable', on ? 'true' : 'false');
|
|
85
|
+
this.syncState();
|
|
86
|
+
return true;
|
|
87
|
+
},
|
|
88
|
+
|
|
89
|
+
onDomMutated() {
|
|
90
|
+
if (this.applying || this.destroyed) return;
|
|
91
|
+
if (!this.typing) {
|
|
92
|
+
this.pushHistory();
|
|
93
|
+
this.typing = true;
|
|
94
|
+
}
|
|
95
|
+
clearTimeout(this.idleTimer);
|
|
96
|
+
this.idleTimer = setTimeout(() => {
|
|
97
|
+
this.typing = false;
|
|
98
|
+
this.baseline = this.snapshot();
|
|
99
|
+
this.afterChange();
|
|
100
|
+
}, TYPING_MERGE_MS);
|
|
101
|
+
this.emit('update');
|
|
102
|
+
this.syncState();
|
|
103
|
+
},
|
|
104
|
+
|
|
105
|
+
undo() {
|
|
106
|
+
if (!this.undoStack.length) return false;
|
|
107
|
+
if (this.typing) {
|
|
108
|
+
this.typing = false;
|
|
109
|
+
clearTimeout(this.idleTimer);
|
|
110
|
+
}
|
|
111
|
+
this.redoStack.push(this.snapshot());
|
|
112
|
+
this.restore(this.undoStack.pop());
|
|
113
|
+
return true;
|
|
114
|
+
},
|
|
115
|
+
|
|
116
|
+
redo() {
|
|
117
|
+
if (!this.redoStack.length) return false;
|
|
118
|
+
this.undoStack.push(this.snapshot());
|
|
119
|
+
this.restore(this.redoStack.pop());
|
|
120
|
+
return true;
|
|
121
|
+
},
|
|
122
|
+
|
|
123
|
+
restore(entry) {
|
|
124
|
+
this.applying = true;
|
|
125
|
+
this.root.innerHTML = entry.html;
|
|
126
|
+
normalizeBody(this.root, { editable: true });
|
|
127
|
+
setTimeout(() => {
|
|
128
|
+
this.applying = false;
|
|
129
|
+
}, 0);
|
|
130
|
+
this.baseline = { ...entry };
|
|
131
|
+
this.pending = null;
|
|
132
|
+
// [坑] 整篇换 DOM 后,表格框选与右键命中的旧元素引用全部脱离文档:不清掉的话,
|
|
133
|
+
// 撤销/重做之后紧接着的表格菜单动作会因取不到上下文而静默失效(又是「点了没反应」)
|
|
134
|
+
this.cellRect = null;
|
|
135
|
+
this.contextCell = null;
|
|
136
|
+
this.cellDragFrom = null;
|
|
137
|
+
this.selectedAtom = null;
|
|
138
|
+
this.afterChange();
|
|
139
|
+
const sel = entry.sel;
|
|
140
|
+
this.applyCaptured(sel);
|
|
141
|
+
},
|
|
142
|
+
|
|
143
|
+
afterChange() {
|
|
144
|
+
this.search.refresh();
|
|
145
|
+
this.emit('update');
|
|
146
|
+
this.emit('transaction');
|
|
147
|
+
this.syncState();
|
|
148
|
+
},
|
|
149
|
+
};
|