@a200888213/gbdocx 0.6.2 → 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/word/file.js +191 -191
- package/src/app/word/menus.js +201 -201
- package/src/app/word/view.js +159 -159
- package/src/app/word-app.js +487 -487
- package/src/kernel/docx/catalog.js +41 -41
- package/src/kernel/docx/menus.js +348 -348
- package/src/kernel/docx/wordShortcuts.js +37 -37
- package/src/kernel/editor/engine/editor.js +213 -213
- package/src/kernel/editor/engine/search.js +113 -113
- package/src/kernel/editor/pasteClean.js +293 -293
- package/src/kernel/notify.js +54 -54
- package/src/kernel/util/clipboard.js +89 -89
- package/src/kernel/util/safeValue.js +25 -25
- package/src/surface/editor-surface.js +5 -2
- package/src/ui/context-menu.js +4 -2
- package/src/ui/dialogs.js +249 -249
- package/src/ui/icons.js +245 -137
- package/src/ui/popup.js +79 -79
- package/styles/app.css +970 -970
- package/styles/word-panel.css +410 -410
package/src/app/word-app.js
CHANGED
|
@@ -1,487 +1,487 @@
|
|
|
1
|
-
// [文件] gbdocx·WordApp —— docx 编辑器应用装配(几何/分页/批注/脚注/目录/页眉页脚带/菜单/快捷键)
|
|
2
|
-
//
|
|
3
|
-
// [设计] 本文件只留「装配 + 骨架 + 生命周期 + 自检」;其余职责拆到 ./word/*:
|
|
4
|
-
// layout(几何与分页)/ doc(文档状态)/ file(文件动作)/ edit(编辑动作)/ catalog(目录)/
|
|
5
|
-
// comments(批注)/ notes(脚注尾注)/ hf(页眉页脚带)/ view(视图与对话框)/ menus(右键菜单与快捷键)。
|
|
6
|
-
// 各模块导出方法组,由文件末尾 Object.assign 装配到 WordApp.prototype —— 同一个 this、同一套调用,
|
|
7
|
-
// 外部拿到的 WordApp 形状与拆分前完全一致。
|
|
8
|
-
// [坑] 装配必须在实例化之前完成:Object.assign 在模块求值时执行,而实例只能在 import 之后 new。
|
|
9
|
-
|
|
10
|
-
import { EditorSurface } from '../surface/editor-surface.js';
|
|
11
|
-
import { bindCtrlWheelZoom, createReadOnlyGuard } from 'guabiutils';
|
|
12
|
-
import { createOfficeHead } from 'gbofficeui/head';
|
|
13
|
-
import { parseDocx } from '../kernel/docx/codec/docx.js';
|
|
14
|
-
import { buildDocxVerified } from '../kernel/docx/codec/roundtrip.js';
|
|
15
|
-
import { runShortcut } from '../kernel/docx/wordShortcuts.js';
|
|
16
|
-
import { buildMenus } from '../kernel/docx/menus.js';
|
|
17
|
-
import { HF_MODES, defaultPageConf, defaultPageNumbers, defaultWatermark } from '../kernel/docx/constants.js';
|
|
18
|
-
import { notify } from '../kernel/notify.js';
|
|
19
|
-
import { useKeyboard, KEY_PRIORITY } from '../kernel/keyboard.js';
|
|
20
|
-
import { buildMenuBar } from '../ui/menubar.js';
|
|
21
|
-
import { closeAll } from '../ui/popup.js';
|
|
22
|
-
import { buildRibbon } from '../ui/ribbon.js';
|
|
23
|
-
import { icon } from '../ui/icons.js';
|
|
24
|
-
import { StatusBar } from '../ui/statusbar.js';
|
|
25
|
-
import { CatalogPanel, CommentsPanel } from '../ui/panels.js';
|
|
26
|
-
import { PromptPopover } from '../ui/prompt.js';
|
|
27
|
-
import { FindBar } from '../ui/find-bar.js';
|
|
28
|
-
import { buildSpecialChars } from '../ui/special-chars.js';
|
|
29
|
-
import { openColumnDialog, openMarginDialog } from '../ui/dialogs.js';
|
|
30
|
-
import { renderSheets } from './word-sheets.js';
|
|
31
|
-
import { loadPrefs, savePrefs } from './word/shared.js';
|
|
32
|
-
import { layoutMethods } from './word/layout.js';
|
|
33
|
-
import { docMethods } from './word/doc.js';
|
|
34
|
-
import { fileMethods } from './word/file.js';
|
|
35
|
-
import { editMethods } from './word/edit.js';
|
|
36
|
-
import { catalogMethods } from './word/catalog.js';
|
|
37
|
-
import { commentsMethods } from './word/comments.js';
|
|
38
|
-
import { notesMethods } from './word/notes.js';
|
|
39
|
-
import { hfMethods } from './word/hf.js';
|
|
40
|
-
import { viewMethods } from './word/view.js';
|
|
41
|
-
import { menusMethods } from './word/menus.js';
|
|
42
|
-
|
|
43
|
-
export class WordApp {
|
|
44
|
-
/**
|
|
45
|
-
* @param {HTMLElement} mount 已挂在文档里的挂载点
|
|
46
|
-
* @param {object} [options] embed=嵌入宿主;io=文件动作;onDirty/isActive=脏态与快捷键闸门;
|
|
47
|
-
* readOnly=只读(禁写且锁编辑控件);hideMenuBar=隐藏顶部菜单栏那一行
|
|
48
|
-
*/
|
|
49
|
-
constructor(mount, options = {}) {
|
|
50
|
-
this.mount = mount;
|
|
51
|
-
this.embed = !!options.embed;
|
|
52
|
-
this.readOnly = !!options.readOnly;
|
|
53
|
-
this.menuBarVisible = !options.hideMenuBar;
|
|
54
|
-
this.io = options.io || null;
|
|
55
|
-
this.onDirty = typeof options.onDirty === 'function' ? options.onDirty : null;
|
|
56
|
-
this.isActive = typeof options.isActive === 'function' ? options.isActive : null;
|
|
57
|
-
this.doc = { name: '新建文档', handle: null };
|
|
58
|
-
this.state = {
|
|
59
|
-
sections: [],
|
|
60
|
-
page: defaultPageConf(),
|
|
61
|
-
pageNumbers: defaultPageNumbers(),
|
|
62
|
-
watermark: defaultWatermark(),
|
|
63
|
-
hfMode: HF_MODES[0].value,
|
|
64
|
-
header: '',
|
|
65
|
-
footer: '',
|
|
66
|
-
headerEven: '',
|
|
67
|
-
footerEven: '',
|
|
68
|
-
headerFirst: '',
|
|
69
|
-
footerFirst: '',
|
|
70
|
-
footnotes: [],
|
|
71
|
-
endnotes: [],
|
|
72
|
-
props: null,
|
|
73
|
-
defaultTabStop: 720,
|
|
74
|
-
compressPunctuation: false,
|
|
75
|
-
carry: [],
|
|
76
|
-
losses: [],
|
|
77
|
-
};
|
|
78
|
-
this.comments = [];
|
|
79
|
-
this.catalog = [];
|
|
80
|
-
/** 本次载入的「保存必丢」清单是否已由用户确认(自动保存不得绕过确认静默丢内容) */
|
|
81
|
-
this.lossAck = false;
|
|
82
|
-
this.lossAutoWarned = false;
|
|
83
|
-
this.footnotePages = new Map();
|
|
84
|
-
this.fnHeights = [];
|
|
85
|
-
this.pageCount = 1;
|
|
86
|
-
this.visiblePage = 1;
|
|
87
|
-
this.editMode = 'edit';
|
|
88
|
-
this.preview = false;
|
|
89
|
-
this.previewRestore = null;
|
|
90
|
-
this.pageFullscreen = false;
|
|
91
|
-
this.dirty = false;
|
|
92
|
-
this.saving = false;
|
|
93
|
-
this.suggestionCount = 0;
|
|
94
|
-
this.activeCommentKey = '';
|
|
95
|
-
this.bandEditSlot = '';
|
|
96
|
-
this.prefs = loadPrefs();
|
|
97
|
-
this.zoom = this.prefs.zoom || 100;
|
|
98
|
-
this.flags = this.prefs.flags || { catalog: false, commentsPanel: false };
|
|
99
|
-
this.lastEmittedHtml = '';
|
|
100
|
-
this.pageTimer = 0;
|
|
101
|
-
this.pageRaf = 0;
|
|
102
|
-
// [坑] 编辑器内核在 mount 时即抛一次 update(构造期),此时状态栏/工具栏尚未装配:
|
|
103
|
-
// 事件接线在构造完成前一律静默,避免读到未定义的宿主成员
|
|
104
|
-
this.ready = false;
|
|
105
|
-
this.build();
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
/* ===================== 几何读取 ===================== */
|
|
109
|
-
|
|
110
|
-
build() {
|
|
111
|
-
// [设计] 所有样式收在 .word-scope 下(本包 CSS 已按该前缀作用域化),宿主落地时不污染同页其它编辑器
|
|
112
|
-
const scope = document.createElement('div');
|
|
113
|
-
// 应用作用域:需要撑满宿主(.word-app 的 height:100% 以它为基准);浮层的 .word-scope 不挂这个类
|
|
114
|
-
scope.className = 'word-scope word-scope--app';
|
|
115
|
-
const root = document.createElement('div');
|
|
116
|
-
root.className = 'word-app word-skin';
|
|
117
|
-
if (this.embed) root.classList.add('gbd-is-embed');
|
|
118
|
-
root.innerHTML = `
|
|
119
|
-
<div class="word-head-host"></div>
|
|
120
|
-
<div class="gbd-menubar-host"></div>
|
|
121
|
-
<div class="gbd-toolbar-host"></div>
|
|
122
|
-
<div class="word-main">
|
|
123
|
-
<div class="word-editor"><div class="word-editor-mount"></div></div>
|
|
124
|
-
</div>
|
|
125
|
-
<div class="gbd-statusbar-host"></div>`;
|
|
126
|
-
scope.appendChild(root);
|
|
127
|
-
this.mount.appendChild(scope);
|
|
128
|
-
this.scopeEl = scope;
|
|
129
|
-
this.root = root;
|
|
130
|
-
// 文档头骨架来自 gbofficeui(标题 / 重命名 / 保存 / 下载 / 预览 / 全屏 三家同一套)
|
|
131
|
-
const dirtyEl = document.createElement('span');
|
|
132
|
-
dirtyEl.className = 'word-head__dirty';
|
|
133
|
-
dirtyEl.hidden = true;
|
|
134
|
-
dirtyEl.textContent = '●';
|
|
135
|
-
this.head = createOfficeHead({
|
|
136
|
-
className: 'word-head',
|
|
137
|
-
icon,
|
|
138
|
-
title: this.doc.name,
|
|
139
|
-
autoSave: this.autosaveOn(),
|
|
140
|
-
extra: [dirtyEl],
|
|
141
|
-
rename: this.canRename() ? { onClick: (event) => this.renameDoc(event) } : null,
|
|
142
|
-
download: this.canDownload() ? { onClick: () => this.downloadDoc() } : null,
|
|
143
|
-
onSave: () => {
|
|
144
|
-
const hostSave = this.embed ? this.io?.save : null;
|
|
145
|
-
return hostSave ? hostSave() : this.save();
|
|
146
|
-
},
|
|
147
|
-
onPreview: () => this.setPreview(!this.preview),
|
|
148
|
-
onFullscreen: () => this.toggleFullscreen(),
|
|
149
|
-
});
|
|
150
|
-
root.querySelector('.word-head-host').replaceWith(this.head.el);
|
|
151
|
-
this.menubarHost = root.querySelector('.gbd-menubar-host');
|
|
152
|
-
this.headEl = this.head.el;
|
|
153
|
-
this.toolbarHost = root.querySelector('.gbd-toolbar-host');
|
|
154
|
-
this.headActionsEl = this.head.actionsEl;
|
|
155
|
-
// 预览按钮不随只读被 inert:它要能在预览档里继续点「退出预览」
|
|
156
|
-
this.guard = createReadOnlyGuard(root, { editZones: () => [this.headActionsEl, this.toolbarHost] });
|
|
157
|
-
this.editorWrap = root.querySelector('.word-editor');
|
|
158
|
-
this.titleEl = this.head.titleEl;
|
|
159
|
-
this.pathEl = this.head.subEl;
|
|
160
|
-
this.statusEl = this.head.statusEl;
|
|
161
|
-
this.dirtyEl = dirtyEl;
|
|
162
|
-
this.autosaveEl = this.head.autosaveEl;
|
|
163
|
-
this.previewBtn = this.head.previewBtn;
|
|
164
|
-
this.fullscreenBtn = this.head.fullscreenBtn;
|
|
165
|
-
|
|
166
|
-
this.catalogPanel = new CatalogPanel((key) => this.locateCatalog(key));
|
|
167
|
-
this.commentsPanel = new CommentsPanel({
|
|
168
|
-
locate: (key) => this.locateComment(key),
|
|
169
|
-
toggle: (key) => this.toggleComment(key),
|
|
170
|
-
remove: (key) => this.removeComment(key),
|
|
171
|
-
});
|
|
172
|
-
|
|
173
|
-
this.surface = new EditorSurface({
|
|
174
|
-
mount: root.querySelector('.word-editor-mount'),
|
|
175
|
-
placeholder: '开始输入正文…',
|
|
176
|
-
editable: true,
|
|
177
|
-
handlers: {
|
|
178
|
-
update: () => { if (this.ready) this.onDocUpdate(); },
|
|
179
|
-
'open-find': () => this.findBar.open(),
|
|
180
|
-
'request-link': () => this.insertLink(),
|
|
181
|
-
},
|
|
182
|
-
backdrop: (host) => renderSheets(host, this.sheetsCtx()),
|
|
183
|
-
});
|
|
184
|
-
this.editorState = this.editor.state();
|
|
185
|
-
|
|
186
|
-
// 选项卡栏(WPS 风格):tab 行 + 各页分组工具面板,完全接管原图标工具栏
|
|
187
|
-
this.ribbon = buildRibbon({
|
|
188
|
-
getState: () => ({ ...this.editorState, ...this.menuCtx().state }),
|
|
189
|
-
getCtx: () => this.menuCtx(),
|
|
190
|
-
isDisabled: () => this.editMode === 'readonly',
|
|
191
|
-
run: (name, ...args) => this.run(name, ...args),
|
|
192
|
-
actions: {
|
|
193
|
-
insertTable: (e) => this.openTableSpec(e),
|
|
194
|
-
insertImage: () => this.pickImage(),
|
|
195
|
-
insertLink: () => this.insertLink(),
|
|
196
|
-
},
|
|
197
|
-
find: (replace) => this.findBar.open(!!replace),
|
|
198
|
-
openMargin: () => this.openMarginDialog(),
|
|
199
|
-
openColumn: () => this.openColumnDialog(),
|
|
200
|
-
});
|
|
201
|
-
this.toolbarHost.appendChild(this.ribbon);
|
|
202
|
-
|
|
203
|
-
this.statusBar = new StatusBar({
|
|
204
|
-
onMode: (m) => this.setEditMode(m),
|
|
205
|
-
onZoomIn: () => this.setZoom(this.zoom + 10),
|
|
206
|
-
onZoomOut: () => this.setZoom(this.zoom - 10),
|
|
207
|
-
onZoomReset: () => this.setZoom(100),
|
|
208
|
-
onOpenMargin: () => this.openMarginDialog(),
|
|
209
|
-
onOpenColumn: () => this.openColumnDialog(),
|
|
210
|
-
});
|
|
211
|
-
root.querySelector('.gbd-statusbar-host').appendChild(this.statusBar.el);
|
|
212
|
-
|
|
213
|
-
this.findBar = new FindBar(() => this.editor);
|
|
214
|
-
root.querySelector('.word-editor').appendChild(this.findBar.el);
|
|
215
|
-
|
|
216
|
-
this.prompt = new PromptPopover();
|
|
217
|
-
this.prompt.onClosed = () => this.onPromptClosed();
|
|
218
|
-
// 重命名气泡另起一个实例:批注/脚注气泡的「取消回滚」语义与它无关
|
|
219
|
-
this.renamePrompt = new PromptPopover();
|
|
220
|
-
|
|
221
|
-
this.imageInput = document.createElement('input');
|
|
222
|
-
this.imageInput.type = 'file';
|
|
223
|
-
this.imageInput.accept = 'image/*';
|
|
224
|
-
this.imageInput.multiple = true;
|
|
225
|
-
this.imageInput.hidden = true;
|
|
226
|
-
this.imageInput.addEventListener('change', () => this.onImagePicked());
|
|
227
|
-
root.appendChild(this.imageInput);
|
|
228
|
-
|
|
229
|
-
this.importInput = document.createElement('input');
|
|
230
|
-
this.importInput.type = 'file';
|
|
231
|
-
this.importInput.accept = '.docx';
|
|
232
|
-
this.importInput.hidden = true;
|
|
233
|
-
this.importInput.addEventListener('change', () => this.onImportFile());
|
|
234
|
-
root.appendChild(this.importInput);
|
|
235
|
-
|
|
236
|
-
this.charsPop = document.createElement('div');
|
|
237
|
-
this.charsPop.className = 'word-chars-pop';
|
|
238
|
-
this.charsPop.hidden = true;
|
|
239
|
-
this.charsPop.appendChild(buildSpecialChars((ch) => this.run('insertText', ch)));
|
|
240
|
-
root.querySelector('.word-editor').appendChild(this.charsPop);
|
|
241
|
-
|
|
242
|
-
this.bindHead();
|
|
243
|
-
this.buildChrome();
|
|
244
|
-
this.bindEvents();
|
|
245
|
-
this.applyZoom();
|
|
246
|
-
this.ready = true;
|
|
247
|
-
this.applyMode();
|
|
248
|
-
}
|
|
249
|
-
|
|
250
|
-
get editor() {
|
|
251
|
-
return this.surface.editor;
|
|
252
|
-
}
|
|
253
|
-
|
|
254
|
-
/** 自动保存开关初值:嵌入模式读宿主,独立模式读本地偏好 */
|
|
255
|
-
autosaveOn() {
|
|
256
|
-
const io = this.io || {};
|
|
257
|
-
return typeof io.autosave === 'function' ? !!io.autosave() : !!this.prefs.autosave;
|
|
258
|
-
}
|
|
259
|
-
|
|
260
|
-
/** 文档头「重命名」是否可用:嵌入模式由宿主提供,独立模式走包内实现 */
|
|
261
|
-
canRename() {
|
|
262
|
-
return !this.embed || typeof this.io?.rename === 'function';
|
|
263
|
-
}
|
|
264
|
-
|
|
265
|
-
/** 文档头「下载」是否可用:嵌入模式由宿主提供,独立模式走包内实现 */
|
|
266
|
-
canDownload() {
|
|
267
|
-
return !this.embed || typeof this.io?.download === 'function';
|
|
268
|
-
}
|
|
269
|
-
|
|
270
|
-
bindHead() {
|
|
271
|
-
// 保存 / 预览 / 全屏的点击已由共享文档头(gbofficeui)接管,这里只挂文档头之外的交互
|
|
272
|
-
this.onFullscreenEsc = (event) => {
|
|
273
|
-
if (event.key !== 'Escape' || !this.pageFullscreen) return;
|
|
274
|
-
// 菜单/弹窗开着时先交给它们自己关,别一按 Esc 就退出全屏
|
|
275
|
-
if (document.querySelector('.gbd-menu, .gbd-modal-mask, .gbd-prompt, .gbrt-modal')) return;
|
|
276
|
-
event.stopPropagation();
|
|
277
|
-
this.setPageFullscreen(false);
|
|
278
|
-
};
|
|
279
|
-
this.autosaveEl.addEventListener('change', () => {
|
|
280
|
-
const on = this.autosaveEl.checked;
|
|
281
|
-
if (this.embed && this.io?.setAutosave) return this.io.setAutosave(on);
|
|
282
|
-
this.prefs.autosave = on;
|
|
283
|
-
savePrefs(this.prefs);
|
|
284
|
-
});
|
|
285
|
-
}
|
|
286
|
-
|
|
287
|
-
/** 菜单栏(条目结构来自 docx/menus.js 的 buildMenus);只读档锁掉纯编辑类菜单 */
|
|
288
|
-
buildChrome() {
|
|
289
|
-
const menus = buildMenus(this.menuCtx());
|
|
290
|
-
this.menubarHost.replaceChildren(buildMenuBar(menus, this.readOnly ? ['edit', 'insert', 'table', 'format'] : []));
|
|
291
|
-
}
|
|
292
|
-
|
|
293
|
-
/** 构造参数落 DOM:装配完成后才调用,此时工具栏/状态栏/查找栏都已就位 */
|
|
294
|
-
applyMode() {
|
|
295
|
-
this.setMenuBarVisible(this.menuBarVisible);
|
|
296
|
-
if (this.readOnly) this.setReadOnly(true);
|
|
297
|
-
}
|
|
298
|
-
|
|
299
|
-
/** 显示/隐藏顶部菜单栏那一行;文件头、正文与状态栏不受影响 */
|
|
300
|
-
setMenuBarVisible(on) {
|
|
301
|
-
this.menuBarVisible = !!on;
|
|
302
|
-
this.menubarHost.style.display = this.menuBarVisible ? '' : 'none';
|
|
303
|
-
}
|
|
304
|
-
|
|
305
|
-
/** 只读开关:内核档位、DOM 守卫、编辑控件与菜单可用性同一处切换 */
|
|
306
|
-
setReadOnly(on) {
|
|
307
|
-
this.readOnly = !!on;
|
|
308
|
-
this.guard.setReadOnly(this.readOnly);
|
|
309
|
-
this.setEditMode(this.readOnly ? 'readonly' : 'edit');
|
|
310
|
-
this.statusBar.setReadOnly(this.readOnly);
|
|
311
|
-
this.findBar.setReadOnly(this.readOnly);
|
|
312
|
-
this.buildChrome();
|
|
313
|
-
}
|
|
314
|
-
|
|
315
|
-
bindEvents() {
|
|
316
|
-
// 编辑器状态刷新 → 工具栏/状态栏
|
|
317
|
-
const refresh = () => {
|
|
318
|
-
this.editorState = this.editor.state();
|
|
319
|
-
this.ribbon.refresh();
|
|
320
|
-
this.statusBar.setCaret(this.editorState.line || 0, this.editorState.column || 0);
|
|
321
|
-
};
|
|
322
|
-
this.editor.on('state', refresh);
|
|
323
|
-
this.editor.on('selection', refresh);
|
|
324
|
-
|
|
325
|
-
// 快捷键(Ctrl/⌘ 组合走 wordShortcuts;Esc 关特殊字符浮盘)
|
|
326
|
-
this.unbindKeys = useKeyboard({
|
|
327
|
-
keydown: (e) => {
|
|
328
|
-
// 嵌入模式:非激活页签不消费任何快捷键(多页签同页共存时键盘归属只看 active)
|
|
329
|
-
if (this.isActive && !this.isActive()) return false;
|
|
330
|
-
if (!e.ctrlKey && !e.metaKey) {
|
|
331
|
-
if (e.key === 'Escape' && this.preview) {
|
|
332
|
-
this.setPreview(false);
|
|
333
|
-
return true;
|
|
334
|
-
}
|
|
335
|
-
if (e.key === 'Escape' && !this.charsPop.hidden) {
|
|
336
|
-
this.charsPop.hidden = true;
|
|
337
|
-
return true;
|
|
338
|
-
}
|
|
339
|
-
return false;
|
|
340
|
-
}
|
|
341
|
-
return runShortcut(e, this.shortcutCtx());
|
|
342
|
-
},
|
|
343
|
-
}, { priority: KEY_PRIORITY.WINDOW });
|
|
344
|
-
|
|
345
|
-
// 滚动读数(可见页)
|
|
346
|
-
const scroll = this.surface.scrollEl;
|
|
347
|
-
scroll.addEventListener('scroll', () => this.refreshScroll(), { passive: true });
|
|
348
|
-
|
|
349
|
-
// Ctrl/⌘+滚轮:以指针为锚缩放(编辑器用 CSS zoom,滚动量是内容本地像素)
|
|
350
|
-
this.unbindWheel = bindCtrlWheelZoom({
|
|
351
|
-
target: scroll,
|
|
352
|
-
scrollEl: scroll,
|
|
353
|
-
scrollMode: 'local',
|
|
354
|
-
getScale: () => this.zoom / 100,
|
|
355
|
-
zoomStep: (direction) => {
|
|
356
|
-
const before = this.zoom;
|
|
357
|
-
this.setZoom(before + direction * 10);
|
|
358
|
-
return this.zoom !== before;
|
|
359
|
-
},
|
|
360
|
-
});
|
|
361
|
-
|
|
362
|
-
// 编辑器右键菜单
|
|
363
|
-
this.surface.contentEl.addEventListener('contextmenu', (e) => this.onContextMenu(e));
|
|
364
|
-
|
|
365
|
-
// 图片异步加载 / 窗口宽度变化都会改正文高度 → 重排
|
|
366
|
-
this.layoutObs = new ResizeObserver(() => this.schedulePaginate());
|
|
367
|
-
this.layoutObs.observe(this.editor.root);
|
|
368
|
-
this._onWinResize = () => this.schedulePaginate();
|
|
369
|
-
window.addEventListener('resize', this._onWinResize);
|
|
370
|
-
|
|
371
|
-
// 批注锚点 hover / 点击
|
|
372
|
-
this.surface.contentEl.addEventListener('mouseover', (e) => this.onAnchorOver(e));
|
|
373
|
-
this.surface.contentEl.addEventListener('mouseout', (e) => this.onAnchorOut(e));
|
|
374
|
-
this.surface.contentEl.addEventListener('click', (e) => this.onAnchorClick(e));
|
|
375
|
-
}
|
|
376
|
-
|
|
377
|
-
/* ===================== 纸面 ctx(供 renderSheets) ===================== */
|
|
378
|
-
|
|
379
|
-
onImportFile = async () => {
|
|
380
|
-
const file = this.importInput.files?.[0];
|
|
381
|
-
this.importInput.value = '';
|
|
382
|
-
if (!file) return;
|
|
383
|
-
if (!/\.docx$/i.test(file.name)) return notify.warning('只支持 .docx(旧版 .doc 请先用 Word 另存为 .docx)');
|
|
384
|
-
if (this.dirty && !this.confirmDiscardSync('导入会替换当前编辑内容,未保存的修改将丢失。仍然导入?')) return;
|
|
385
|
-
try {
|
|
386
|
-
const bytes = new Uint8Array(await file.arrayBuffer());
|
|
387
|
-
await this.applyDoc(bytes.length ? await parseDocx(bytes) : null);
|
|
388
|
-
this.setDirty(true);
|
|
389
|
-
notify.success(`已导入 ${file.name}——保存将写入当前文档`);
|
|
390
|
-
} catch (err) {
|
|
391
|
-
notify.error(`导入失败:${err.message || err}`);
|
|
392
|
-
}
|
|
393
|
-
};
|
|
394
|
-
|
|
395
|
-
/* ===================== 编辑动作 ===================== */
|
|
396
|
-
|
|
397
|
-
async selfTest() {
|
|
398
|
-
// 覆盖全部「模型承诺过」的往返口径:批注/脚注/尾注锚点、首页不同页眉、奇偶页、表格与标题
|
|
399
|
-
const html = '<h1>标题一</h1><p>正文 <strong>加粗</strong> 与 <em>斜体</em>'
|
|
400
|
-
+ ' <span class="doc-comment" data-cmt="c1">批注锚</span>'
|
|
401
|
-
+ '<sup class="doc-footnote" data-fn="fn1">1</sup><sup class="doc-endnote" data-fn="en1">1</sup></p>'
|
|
402
|
-
+ '<table><tbody><tr><td><p>A</p></td><td><p>B</p></td></tr></tbody></table>';
|
|
403
|
-
const model = {
|
|
404
|
-
html,
|
|
405
|
-
sections: [{
|
|
406
|
-
type: '',
|
|
407
|
-
page: this.state.page,
|
|
408
|
-
hfMode: 'except-first',
|
|
409
|
-
header: '页眉',
|
|
410
|
-
footer: '',
|
|
411
|
-
headerFirst: '首页页眉',
|
|
412
|
-
footerFirst: '',
|
|
413
|
-
pageNumbers: { enabled: true, align: 'center', format: 'arabic', start: 2 },
|
|
414
|
-
watermark: { data: '', color: '#AEB5C0', size: 120, opacity: 0.3 },
|
|
415
|
-
}],
|
|
416
|
-
comments: [{ key: 'c1', text: '批注文本', author: '我', date: '2025-01-01T00:00:00' }],
|
|
417
|
-
footnotes: [{ id: 'fn1', text: '脚注文本' }],
|
|
418
|
-
endnotes: [{ id: 'en1', text: '尾注文本' }],
|
|
419
|
-
props: null,
|
|
420
|
-
defaultTabStop: 720,
|
|
421
|
-
compressPunctuation: false,
|
|
422
|
-
carry: [],
|
|
423
|
-
};
|
|
424
|
-
const bytes = await buildDocxVerified(model);
|
|
425
|
-
const back = await parseDocx(bytes);
|
|
426
|
-
return {
|
|
427
|
-
bytes: bytes.length,
|
|
428
|
-
backHtmlLength: (back.html || '').length,
|
|
429
|
-
hasTitle: /标题一/.test(back.html || ''),
|
|
430
|
-
hasBold: /<strong|<b\b/i.test(back.html || ''),
|
|
431
|
-
hasTable: /<table/i.test(back.html || ''),
|
|
432
|
-
hasCommentAnchor: /data-cmt="0"/.test(back.html || ''),
|
|
433
|
-
hasFootnoteAnchor: /data-fn="fn1"/.test(back.html || ''),
|
|
434
|
-
hasEndnoteAnchor: /data-fn="en1"/.test(back.html || ''),
|
|
435
|
-
sections: (back.sections || []).length,
|
|
436
|
-
comments: (back.comments || []).length,
|
|
437
|
-
footnotes: (back.footnotes || []).length,
|
|
438
|
-
endnotes: (back.endnotes || []).length,
|
|
439
|
-
hfMode: (back.sections || [])[0]?.hfMode || '',
|
|
440
|
-
header: (back.sections || [])[0]?.header || '',
|
|
441
|
-
headerFirst: (back.sections || [])[0]?.headerFirst || '',
|
|
442
|
-
};
|
|
443
|
-
}
|
|
444
|
-
|
|
445
|
-
/* ===================== 启动 ===================== */
|
|
446
|
-
|
|
447
|
-
async start() {
|
|
448
|
-
this.applyView();
|
|
449
|
-
this.renderHead();
|
|
450
|
-
if (this.embed) return;
|
|
451
|
-
await this.applyDoc(null);
|
|
452
|
-
notify.info(this.readOnly ? 'gbdocx 已就绪:只读预览' : 'gbdocx 已就绪:可直接编辑,或用「打开」载入 .docx');
|
|
453
|
-
}
|
|
454
|
-
|
|
455
|
-
destroy() {
|
|
456
|
-
// 全屏中的 .word-scope 挂着 body:先归还原位,再随挂载点一起移除
|
|
457
|
-
if (this.pageFullscreen) this.setPageFullscreen(false);
|
|
458
|
-
this.unbindKeys?.();
|
|
459
|
-
this.unbindWheel?.();
|
|
460
|
-
this.layoutObs?.disconnect();
|
|
461
|
-
window.removeEventListener('resize', this._onWinResize);
|
|
462
|
-
clearTimeout(this.pageTimer);
|
|
463
|
-
clearTimeout(this._autoTimer);
|
|
464
|
-
clearTimeout(this._peekTimer);
|
|
465
|
-
cancelAnimationFrame(this.pageRaf);
|
|
466
|
-
// 菜单/浮层挂 body:宿主卸载后不收,会留下悬空浮层与 document 监听
|
|
467
|
-
closeAll();
|
|
468
|
-
document.removeEventListener('keydown', this.onFullscreenEsc, true);
|
|
469
|
-
this.guard.destroy();
|
|
470
|
-
this.surface.destroy();
|
|
471
|
-
this.scopeEl?.remove();
|
|
472
|
-
}
|
|
473
|
-
}
|
|
474
|
-
|
|
475
|
-
Object.assign(
|
|
476
|
-
WordApp.prototype,
|
|
477
|
-
layoutMethods,
|
|
478
|
-
docMethods,
|
|
479
|
-
fileMethods,
|
|
480
|
-
editMethods,
|
|
481
|
-
catalogMethods,
|
|
482
|
-
commentsMethods,
|
|
483
|
-
notesMethods,
|
|
484
|
-
hfMethods,
|
|
485
|
-
viewMethods,
|
|
486
|
-
menusMethods
|
|
487
|
-
);
|
|
1
|
+
// [文件] gbdocx·WordApp —— docx 编辑器应用装配(几何/分页/批注/脚注/目录/页眉页脚带/菜单/快捷键)
|
|
2
|
+
//
|
|
3
|
+
// [设计] 本文件只留「装配 + 骨架 + 生命周期 + 自检」;其余职责拆到 ./word/*:
|
|
4
|
+
// layout(几何与分页)/ doc(文档状态)/ file(文件动作)/ edit(编辑动作)/ catalog(目录)/
|
|
5
|
+
// comments(批注)/ notes(脚注尾注)/ hf(页眉页脚带)/ view(视图与对话框)/ menus(右键菜单与快捷键)。
|
|
6
|
+
// 各模块导出方法组,由文件末尾 Object.assign 装配到 WordApp.prototype —— 同一个 this、同一套调用,
|
|
7
|
+
// 外部拿到的 WordApp 形状与拆分前完全一致。
|
|
8
|
+
// [坑] 装配必须在实例化之前完成:Object.assign 在模块求值时执行,而实例只能在 import 之后 new。
|
|
9
|
+
|
|
10
|
+
import { EditorSurface } from '../surface/editor-surface.js';
|
|
11
|
+
import { bindCtrlWheelZoom, createReadOnlyGuard } from 'guabiutils';
|
|
12
|
+
import { createOfficeHead } from 'gbofficeui/head';
|
|
13
|
+
import { parseDocx } from '../kernel/docx/codec/docx.js';
|
|
14
|
+
import { buildDocxVerified } from '../kernel/docx/codec/roundtrip.js';
|
|
15
|
+
import { runShortcut } from '../kernel/docx/wordShortcuts.js';
|
|
16
|
+
import { buildMenus } from '../kernel/docx/menus.js';
|
|
17
|
+
import { HF_MODES, defaultPageConf, defaultPageNumbers, defaultWatermark } from '../kernel/docx/constants.js';
|
|
18
|
+
import { notify } from '../kernel/notify.js';
|
|
19
|
+
import { useKeyboard, KEY_PRIORITY } from '../kernel/keyboard.js';
|
|
20
|
+
import { buildMenuBar } from '../ui/menubar.js';
|
|
21
|
+
import { closeAll } from '../ui/popup.js';
|
|
22
|
+
import { buildRibbon } from '../ui/ribbon.js';
|
|
23
|
+
import { icon } from '../ui/icons.js';
|
|
24
|
+
import { StatusBar } from '../ui/statusbar.js';
|
|
25
|
+
import { CatalogPanel, CommentsPanel } from '../ui/panels.js';
|
|
26
|
+
import { PromptPopover } from '../ui/prompt.js';
|
|
27
|
+
import { FindBar } from '../ui/find-bar.js';
|
|
28
|
+
import { buildSpecialChars } from '../ui/special-chars.js';
|
|
29
|
+
import { openColumnDialog, openMarginDialog } from '../ui/dialogs.js';
|
|
30
|
+
import { renderSheets } from './word-sheets.js';
|
|
31
|
+
import { loadPrefs, savePrefs } from './word/shared.js';
|
|
32
|
+
import { layoutMethods } from './word/layout.js';
|
|
33
|
+
import { docMethods } from './word/doc.js';
|
|
34
|
+
import { fileMethods } from './word/file.js';
|
|
35
|
+
import { editMethods } from './word/edit.js';
|
|
36
|
+
import { catalogMethods } from './word/catalog.js';
|
|
37
|
+
import { commentsMethods } from './word/comments.js';
|
|
38
|
+
import { notesMethods } from './word/notes.js';
|
|
39
|
+
import { hfMethods } from './word/hf.js';
|
|
40
|
+
import { viewMethods } from './word/view.js';
|
|
41
|
+
import { menusMethods } from './word/menus.js';
|
|
42
|
+
|
|
43
|
+
export class WordApp {
|
|
44
|
+
/**
|
|
45
|
+
* @param {HTMLElement} mount 已挂在文档里的挂载点
|
|
46
|
+
* @param {object} [options] embed=嵌入宿主;io=文件动作;onDirty/isActive=脏态与快捷键闸门;
|
|
47
|
+
* readOnly=只读(禁写且锁编辑控件);hideMenuBar=隐藏顶部菜单栏那一行
|
|
48
|
+
*/
|
|
49
|
+
constructor(mount, options = {}) {
|
|
50
|
+
this.mount = mount;
|
|
51
|
+
this.embed = !!options.embed;
|
|
52
|
+
this.readOnly = !!options.readOnly;
|
|
53
|
+
this.menuBarVisible = !options.hideMenuBar;
|
|
54
|
+
this.io = options.io || null;
|
|
55
|
+
this.onDirty = typeof options.onDirty === 'function' ? options.onDirty : null;
|
|
56
|
+
this.isActive = typeof options.isActive === 'function' ? options.isActive : null;
|
|
57
|
+
this.doc = { name: '新建文档', handle: null };
|
|
58
|
+
this.state = {
|
|
59
|
+
sections: [],
|
|
60
|
+
page: defaultPageConf(),
|
|
61
|
+
pageNumbers: defaultPageNumbers(),
|
|
62
|
+
watermark: defaultWatermark(),
|
|
63
|
+
hfMode: HF_MODES[0].value,
|
|
64
|
+
header: '',
|
|
65
|
+
footer: '',
|
|
66
|
+
headerEven: '',
|
|
67
|
+
footerEven: '',
|
|
68
|
+
headerFirst: '',
|
|
69
|
+
footerFirst: '',
|
|
70
|
+
footnotes: [],
|
|
71
|
+
endnotes: [],
|
|
72
|
+
props: null,
|
|
73
|
+
defaultTabStop: 720,
|
|
74
|
+
compressPunctuation: false,
|
|
75
|
+
carry: [],
|
|
76
|
+
losses: [],
|
|
77
|
+
};
|
|
78
|
+
this.comments = [];
|
|
79
|
+
this.catalog = [];
|
|
80
|
+
/** 本次载入的「保存必丢」清单是否已由用户确认(自动保存不得绕过确认静默丢内容) */
|
|
81
|
+
this.lossAck = false;
|
|
82
|
+
this.lossAutoWarned = false;
|
|
83
|
+
this.footnotePages = new Map();
|
|
84
|
+
this.fnHeights = [];
|
|
85
|
+
this.pageCount = 1;
|
|
86
|
+
this.visiblePage = 1;
|
|
87
|
+
this.editMode = 'edit';
|
|
88
|
+
this.preview = false;
|
|
89
|
+
this.previewRestore = null;
|
|
90
|
+
this.pageFullscreen = false;
|
|
91
|
+
this.dirty = false;
|
|
92
|
+
this.saving = false;
|
|
93
|
+
this.suggestionCount = 0;
|
|
94
|
+
this.activeCommentKey = '';
|
|
95
|
+
this.bandEditSlot = '';
|
|
96
|
+
this.prefs = loadPrefs();
|
|
97
|
+
this.zoom = this.prefs.zoom || 100;
|
|
98
|
+
this.flags = this.prefs.flags || { catalog: false, commentsPanel: false };
|
|
99
|
+
this.lastEmittedHtml = '';
|
|
100
|
+
this.pageTimer = 0;
|
|
101
|
+
this.pageRaf = 0;
|
|
102
|
+
// [坑] 编辑器内核在 mount 时即抛一次 update(构造期),此时状态栏/工具栏尚未装配:
|
|
103
|
+
// 事件接线在构造完成前一律静默,避免读到未定义的宿主成员
|
|
104
|
+
this.ready = false;
|
|
105
|
+
this.build();
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
/* ===================== 几何读取 ===================== */
|
|
109
|
+
|
|
110
|
+
build() {
|
|
111
|
+
// [设计] 所有样式收在 .word-scope 下(本包 CSS 已按该前缀作用域化),宿主落地时不污染同页其它编辑器
|
|
112
|
+
const scope = document.createElement('div');
|
|
113
|
+
// 应用作用域:需要撑满宿主(.word-app 的 height:100% 以它为基准);浮层的 .word-scope 不挂这个类
|
|
114
|
+
scope.className = 'word-scope word-scope--app';
|
|
115
|
+
const root = document.createElement('div');
|
|
116
|
+
root.className = 'word-app word-skin';
|
|
117
|
+
if (this.embed) root.classList.add('gbd-is-embed');
|
|
118
|
+
root.innerHTML = `
|
|
119
|
+
<div class="word-head-host"></div>
|
|
120
|
+
<div class="gbd-menubar-host"></div>
|
|
121
|
+
<div class="gbd-toolbar-host"></div>
|
|
122
|
+
<div class="word-main">
|
|
123
|
+
<div class="word-editor"><div class="word-editor-mount"></div></div>
|
|
124
|
+
</div>
|
|
125
|
+
<div class="gbd-statusbar-host"></div>`;
|
|
126
|
+
scope.appendChild(root);
|
|
127
|
+
this.mount.appendChild(scope);
|
|
128
|
+
this.scopeEl = scope;
|
|
129
|
+
this.root = root;
|
|
130
|
+
// 文档头骨架来自 gbofficeui(标题 / 重命名 / 保存 / 下载 / 预览 / 全屏 三家同一套)
|
|
131
|
+
const dirtyEl = document.createElement('span');
|
|
132
|
+
dirtyEl.className = 'word-head__dirty';
|
|
133
|
+
dirtyEl.hidden = true;
|
|
134
|
+
dirtyEl.textContent = '●';
|
|
135
|
+
this.head = createOfficeHead({
|
|
136
|
+
className: 'word-head',
|
|
137
|
+
icon,
|
|
138
|
+
title: this.doc.name,
|
|
139
|
+
autoSave: this.autosaveOn(),
|
|
140
|
+
extra: [dirtyEl],
|
|
141
|
+
rename: this.canRename() ? { onClick: (event) => this.renameDoc(event) } : null,
|
|
142
|
+
download: this.canDownload() ? { onClick: () => this.downloadDoc() } : null,
|
|
143
|
+
onSave: () => {
|
|
144
|
+
const hostSave = this.embed ? this.io?.save : null;
|
|
145
|
+
return hostSave ? hostSave() : this.save();
|
|
146
|
+
},
|
|
147
|
+
onPreview: () => this.setPreview(!this.preview),
|
|
148
|
+
onFullscreen: () => this.toggleFullscreen(),
|
|
149
|
+
});
|
|
150
|
+
root.querySelector('.word-head-host').replaceWith(this.head.el);
|
|
151
|
+
this.menubarHost = root.querySelector('.gbd-menubar-host');
|
|
152
|
+
this.headEl = this.head.el;
|
|
153
|
+
this.toolbarHost = root.querySelector('.gbd-toolbar-host');
|
|
154
|
+
this.headActionsEl = this.head.actionsEl;
|
|
155
|
+
// 预览按钮不随只读被 inert:它要能在预览档里继续点「退出预览」
|
|
156
|
+
this.guard = createReadOnlyGuard(root, { editZones: () => [this.headActionsEl, this.toolbarHost] });
|
|
157
|
+
this.editorWrap = root.querySelector('.word-editor');
|
|
158
|
+
this.titleEl = this.head.titleEl;
|
|
159
|
+
this.pathEl = this.head.subEl;
|
|
160
|
+
this.statusEl = this.head.statusEl;
|
|
161
|
+
this.dirtyEl = dirtyEl;
|
|
162
|
+
this.autosaveEl = this.head.autosaveEl;
|
|
163
|
+
this.previewBtn = this.head.previewBtn;
|
|
164
|
+
this.fullscreenBtn = this.head.fullscreenBtn;
|
|
165
|
+
|
|
166
|
+
this.catalogPanel = new CatalogPanel((key) => this.locateCatalog(key));
|
|
167
|
+
this.commentsPanel = new CommentsPanel({
|
|
168
|
+
locate: (key) => this.locateComment(key),
|
|
169
|
+
toggle: (key) => this.toggleComment(key),
|
|
170
|
+
remove: (key) => this.removeComment(key),
|
|
171
|
+
});
|
|
172
|
+
|
|
173
|
+
this.surface = new EditorSurface({
|
|
174
|
+
mount: root.querySelector('.word-editor-mount'),
|
|
175
|
+
placeholder: '开始输入正文…',
|
|
176
|
+
editable: true,
|
|
177
|
+
handlers: {
|
|
178
|
+
update: () => { if (this.ready) this.onDocUpdate(); },
|
|
179
|
+
'open-find': () => this.findBar.open(),
|
|
180
|
+
'request-link': () => this.insertLink(),
|
|
181
|
+
},
|
|
182
|
+
backdrop: (host) => renderSheets(host, this.sheetsCtx()),
|
|
183
|
+
});
|
|
184
|
+
this.editorState = this.editor.state();
|
|
185
|
+
|
|
186
|
+
// 选项卡栏(WPS 风格):tab 行 + 各页分组工具面板,完全接管原图标工具栏
|
|
187
|
+
this.ribbon = buildRibbon({
|
|
188
|
+
getState: () => ({ ...this.editorState, ...this.menuCtx().state }),
|
|
189
|
+
getCtx: () => this.menuCtx(),
|
|
190
|
+
isDisabled: () => this.editMode === 'readonly',
|
|
191
|
+
run: (name, ...args) => this.run(name, ...args),
|
|
192
|
+
actions: {
|
|
193
|
+
insertTable: (e) => this.openTableSpec(e),
|
|
194
|
+
insertImage: () => this.pickImage(),
|
|
195
|
+
insertLink: () => this.insertLink(),
|
|
196
|
+
},
|
|
197
|
+
find: (replace) => this.findBar.open(!!replace),
|
|
198
|
+
openMargin: () => this.openMarginDialog(),
|
|
199
|
+
openColumn: () => this.openColumnDialog(),
|
|
200
|
+
});
|
|
201
|
+
this.toolbarHost.appendChild(this.ribbon);
|
|
202
|
+
|
|
203
|
+
this.statusBar = new StatusBar({
|
|
204
|
+
onMode: (m) => this.setEditMode(m),
|
|
205
|
+
onZoomIn: () => this.setZoom(this.zoom + 10),
|
|
206
|
+
onZoomOut: () => this.setZoom(this.zoom - 10),
|
|
207
|
+
onZoomReset: () => this.setZoom(100),
|
|
208
|
+
onOpenMargin: () => this.openMarginDialog(),
|
|
209
|
+
onOpenColumn: () => this.openColumnDialog(),
|
|
210
|
+
});
|
|
211
|
+
root.querySelector('.gbd-statusbar-host').appendChild(this.statusBar.el);
|
|
212
|
+
|
|
213
|
+
this.findBar = new FindBar(() => this.editor);
|
|
214
|
+
root.querySelector('.word-editor').appendChild(this.findBar.el);
|
|
215
|
+
|
|
216
|
+
this.prompt = new PromptPopover();
|
|
217
|
+
this.prompt.onClosed = () => this.onPromptClosed();
|
|
218
|
+
// 重命名气泡另起一个实例:批注/脚注气泡的「取消回滚」语义与它无关
|
|
219
|
+
this.renamePrompt = new PromptPopover();
|
|
220
|
+
|
|
221
|
+
this.imageInput = document.createElement('input');
|
|
222
|
+
this.imageInput.type = 'file';
|
|
223
|
+
this.imageInput.accept = 'image/*';
|
|
224
|
+
this.imageInput.multiple = true;
|
|
225
|
+
this.imageInput.hidden = true;
|
|
226
|
+
this.imageInput.addEventListener('change', () => this.onImagePicked());
|
|
227
|
+
root.appendChild(this.imageInput);
|
|
228
|
+
|
|
229
|
+
this.importInput = document.createElement('input');
|
|
230
|
+
this.importInput.type = 'file';
|
|
231
|
+
this.importInput.accept = '.docx';
|
|
232
|
+
this.importInput.hidden = true;
|
|
233
|
+
this.importInput.addEventListener('change', () => this.onImportFile());
|
|
234
|
+
root.appendChild(this.importInput);
|
|
235
|
+
|
|
236
|
+
this.charsPop = document.createElement('div');
|
|
237
|
+
this.charsPop.className = 'word-chars-pop';
|
|
238
|
+
this.charsPop.hidden = true;
|
|
239
|
+
this.charsPop.appendChild(buildSpecialChars((ch) => this.run('insertText', ch)));
|
|
240
|
+
root.querySelector('.word-editor').appendChild(this.charsPop);
|
|
241
|
+
|
|
242
|
+
this.bindHead();
|
|
243
|
+
this.buildChrome();
|
|
244
|
+
this.bindEvents();
|
|
245
|
+
this.applyZoom();
|
|
246
|
+
this.ready = true;
|
|
247
|
+
this.applyMode();
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
get editor() {
|
|
251
|
+
return this.surface.editor;
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
/** 自动保存开关初值:嵌入模式读宿主,独立模式读本地偏好 */
|
|
255
|
+
autosaveOn() {
|
|
256
|
+
const io = this.io || {};
|
|
257
|
+
return typeof io.autosave === 'function' ? !!io.autosave() : !!this.prefs.autosave;
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
/** 文档头「重命名」是否可用:嵌入模式由宿主提供,独立模式走包内实现 */
|
|
261
|
+
canRename() {
|
|
262
|
+
return !this.embed || typeof this.io?.rename === 'function';
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
/** 文档头「下载」是否可用:嵌入模式由宿主提供,独立模式走包内实现 */
|
|
266
|
+
canDownload() {
|
|
267
|
+
return !this.embed || typeof this.io?.download === 'function';
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
bindHead() {
|
|
271
|
+
// 保存 / 预览 / 全屏的点击已由共享文档头(gbofficeui)接管,这里只挂文档头之外的交互
|
|
272
|
+
this.onFullscreenEsc = (event) => {
|
|
273
|
+
if (event.key !== 'Escape' || !this.pageFullscreen) return;
|
|
274
|
+
// 菜单/弹窗开着时先交给它们自己关,别一按 Esc 就退出全屏
|
|
275
|
+
if (document.querySelector('.gbd-menu, .gbd-modal-mask, .gbd-prompt, .gbrt-modal')) return;
|
|
276
|
+
event.stopPropagation();
|
|
277
|
+
this.setPageFullscreen(false);
|
|
278
|
+
};
|
|
279
|
+
this.autosaveEl.addEventListener('change', () => {
|
|
280
|
+
const on = this.autosaveEl.checked;
|
|
281
|
+
if (this.embed && this.io?.setAutosave) return this.io.setAutosave(on);
|
|
282
|
+
this.prefs.autosave = on;
|
|
283
|
+
savePrefs(this.prefs);
|
|
284
|
+
});
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
/** 菜单栏(条目结构来自 docx/menus.js 的 buildMenus);只读档锁掉纯编辑类菜单 */
|
|
288
|
+
buildChrome() {
|
|
289
|
+
const menus = buildMenus(this.menuCtx());
|
|
290
|
+
this.menubarHost.replaceChildren(buildMenuBar(menus, this.readOnly ? ['edit', 'insert', 'table', 'format'] : []));
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
/** 构造参数落 DOM:装配完成后才调用,此时工具栏/状态栏/查找栏都已就位 */
|
|
294
|
+
applyMode() {
|
|
295
|
+
this.setMenuBarVisible(this.menuBarVisible);
|
|
296
|
+
if (this.readOnly) this.setReadOnly(true);
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
/** 显示/隐藏顶部菜单栏那一行;文件头、正文与状态栏不受影响 */
|
|
300
|
+
setMenuBarVisible(on) {
|
|
301
|
+
this.menuBarVisible = !!on;
|
|
302
|
+
this.menubarHost.style.display = this.menuBarVisible ? '' : 'none';
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
/** 只读开关:内核档位、DOM 守卫、编辑控件与菜单可用性同一处切换 */
|
|
306
|
+
setReadOnly(on) {
|
|
307
|
+
this.readOnly = !!on;
|
|
308
|
+
this.guard.setReadOnly(this.readOnly);
|
|
309
|
+
this.setEditMode(this.readOnly ? 'readonly' : 'edit');
|
|
310
|
+
this.statusBar.setReadOnly(this.readOnly);
|
|
311
|
+
this.findBar.setReadOnly(this.readOnly);
|
|
312
|
+
this.buildChrome();
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
bindEvents() {
|
|
316
|
+
// 编辑器状态刷新 → 工具栏/状态栏
|
|
317
|
+
const refresh = () => {
|
|
318
|
+
this.editorState = this.editor.state();
|
|
319
|
+
this.ribbon.refresh();
|
|
320
|
+
this.statusBar.setCaret(this.editorState.line || 0, this.editorState.column || 0);
|
|
321
|
+
};
|
|
322
|
+
this.editor.on('state', refresh);
|
|
323
|
+
this.editor.on('selection', refresh);
|
|
324
|
+
|
|
325
|
+
// 快捷键(Ctrl/⌘ 组合走 wordShortcuts;Esc 关特殊字符浮盘)
|
|
326
|
+
this.unbindKeys = useKeyboard({
|
|
327
|
+
keydown: (e) => {
|
|
328
|
+
// 嵌入模式:非激活页签不消费任何快捷键(多页签同页共存时键盘归属只看 active)
|
|
329
|
+
if (this.isActive && !this.isActive()) return false;
|
|
330
|
+
if (!e.ctrlKey && !e.metaKey) {
|
|
331
|
+
if (e.key === 'Escape' && this.preview) {
|
|
332
|
+
this.setPreview(false);
|
|
333
|
+
return true;
|
|
334
|
+
}
|
|
335
|
+
if (e.key === 'Escape' && !this.charsPop.hidden) {
|
|
336
|
+
this.charsPop.hidden = true;
|
|
337
|
+
return true;
|
|
338
|
+
}
|
|
339
|
+
return false;
|
|
340
|
+
}
|
|
341
|
+
return runShortcut(e, this.shortcutCtx());
|
|
342
|
+
},
|
|
343
|
+
}, { priority: KEY_PRIORITY.WINDOW });
|
|
344
|
+
|
|
345
|
+
// 滚动读数(可见页)
|
|
346
|
+
const scroll = this.surface.scrollEl;
|
|
347
|
+
scroll.addEventListener('scroll', () => this.refreshScroll(), { passive: true });
|
|
348
|
+
|
|
349
|
+
// Ctrl/⌘+滚轮:以指针为锚缩放(编辑器用 CSS zoom,滚动量是内容本地像素)
|
|
350
|
+
this.unbindWheel = bindCtrlWheelZoom({
|
|
351
|
+
target: scroll,
|
|
352
|
+
scrollEl: scroll,
|
|
353
|
+
scrollMode: 'local',
|
|
354
|
+
getScale: () => this.zoom / 100,
|
|
355
|
+
zoomStep: (direction) => {
|
|
356
|
+
const before = this.zoom;
|
|
357
|
+
this.setZoom(before + direction * 10);
|
|
358
|
+
return this.zoom !== before;
|
|
359
|
+
},
|
|
360
|
+
});
|
|
361
|
+
|
|
362
|
+
// 编辑器右键菜单
|
|
363
|
+
this.surface.contentEl.addEventListener('contextmenu', (e) => this.onContextMenu(e));
|
|
364
|
+
|
|
365
|
+
// 图片异步加载 / 窗口宽度变化都会改正文高度 → 重排
|
|
366
|
+
this.layoutObs = new ResizeObserver(() => this.schedulePaginate());
|
|
367
|
+
this.layoutObs.observe(this.editor.root);
|
|
368
|
+
this._onWinResize = () => this.schedulePaginate();
|
|
369
|
+
window.addEventListener('resize', this._onWinResize);
|
|
370
|
+
|
|
371
|
+
// 批注锚点 hover / 点击
|
|
372
|
+
this.surface.contentEl.addEventListener('mouseover', (e) => this.onAnchorOver(e));
|
|
373
|
+
this.surface.contentEl.addEventListener('mouseout', (e) => this.onAnchorOut(e));
|
|
374
|
+
this.surface.contentEl.addEventListener('click', (e) => this.onAnchorClick(e));
|
|
375
|
+
}
|
|
376
|
+
|
|
377
|
+
/* ===================== 纸面 ctx(供 renderSheets) ===================== */
|
|
378
|
+
|
|
379
|
+
onImportFile = async () => {
|
|
380
|
+
const file = this.importInput.files?.[0];
|
|
381
|
+
this.importInput.value = '';
|
|
382
|
+
if (!file) return;
|
|
383
|
+
if (!/\.docx$/i.test(file.name)) return notify.warning('只支持 .docx(旧版 .doc 请先用 Word 另存为 .docx)');
|
|
384
|
+
if (this.dirty && !this.confirmDiscardSync('导入会替换当前编辑内容,未保存的修改将丢失。仍然导入?')) return;
|
|
385
|
+
try {
|
|
386
|
+
const bytes = new Uint8Array(await file.arrayBuffer());
|
|
387
|
+
await this.applyDoc(bytes.length ? await parseDocx(bytes) : null);
|
|
388
|
+
this.setDirty(true);
|
|
389
|
+
notify.success(`已导入 ${file.name}——保存将写入当前文档`);
|
|
390
|
+
} catch (err) {
|
|
391
|
+
notify.error(`导入失败:${err.message || err}`);
|
|
392
|
+
}
|
|
393
|
+
};
|
|
394
|
+
|
|
395
|
+
/* ===================== 编辑动作 ===================== */
|
|
396
|
+
|
|
397
|
+
async selfTest() {
|
|
398
|
+
// 覆盖全部「模型承诺过」的往返口径:批注/脚注/尾注锚点、首页不同页眉、奇偶页、表格与标题
|
|
399
|
+
const html = '<h1>标题一</h1><p>正文 <strong>加粗</strong> 与 <em>斜体</em>'
|
|
400
|
+
+ ' <span class="doc-comment" data-cmt="c1">批注锚</span>'
|
|
401
|
+
+ '<sup class="doc-footnote" data-fn="fn1">1</sup><sup class="doc-endnote" data-fn="en1">1</sup></p>'
|
|
402
|
+
+ '<table><tbody><tr><td><p>A</p></td><td><p>B</p></td></tr></tbody></table>';
|
|
403
|
+
const model = {
|
|
404
|
+
html,
|
|
405
|
+
sections: [{
|
|
406
|
+
type: '',
|
|
407
|
+
page: this.state.page,
|
|
408
|
+
hfMode: 'except-first',
|
|
409
|
+
header: '页眉',
|
|
410
|
+
footer: '',
|
|
411
|
+
headerFirst: '首页页眉',
|
|
412
|
+
footerFirst: '',
|
|
413
|
+
pageNumbers: { enabled: true, align: 'center', format: 'arabic', start: 2 },
|
|
414
|
+
watermark: { data: '', color: '#AEB5C0', size: 120, opacity: 0.3 },
|
|
415
|
+
}],
|
|
416
|
+
comments: [{ key: 'c1', text: '批注文本', author: '我', date: '2025-01-01T00:00:00' }],
|
|
417
|
+
footnotes: [{ id: 'fn1', text: '脚注文本' }],
|
|
418
|
+
endnotes: [{ id: 'en1', text: '尾注文本' }],
|
|
419
|
+
props: null,
|
|
420
|
+
defaultTabStop: 720,
|
|
421
|
+
compressPunctuation: false,
|
|
422
|
+
carry: [],
|
|
423
|
+
};
|
|
424
|
+
const bytes = await buildDocxVerified(model);
|
|
425
|
+
const back = await parseDocx(bytes);
|
|
426
|
+
return {
|
|
427
|
+
bytes: bytes.length,
|
|
428
|
+
backHtmlLength: (back.html || '').length,
|
|
429
|
+
hasTitle: /标题一/.test(back.html || ''),
|
|
430
|
+
hasBold: /<strong|<b\b/i.test(back.html || ''),
|
|
431
|
+
hasTable: /<table/i.test(back.html || ''),
|
|
432
|
+
hasCommentAnchor: /data-cmt="0"/.test(back.html || ''),
|
|
433
|
+
hasFootnoteAnchor: /data-fn="fn1"/.test(back.html || ''),
|
|
434
|
+
hasEndnoteAnchor: /data-fn="en1"/.test(back.html || ''),
|
|
435
|
+
sections: (back.sections || []).length,
|
|
436
|
+
comments: (back.comments || []).length,
|
|
437
|
+
footnotes: (back.footnotes || []).length,
|
|
438
|
+
endnotes: (back.endnotes || []).length,
|
|
439
|
+
hfMode: (back.sections || [])[0]?.hfMode || '',
|
|
440
|
+
header: (back.sections || [])[0]?.header || '',
|
|
441
|
+
headerFirst: (back.sections || [])[0]?.headerFirst || '',
|
|
442
|
+
};
|
|
443
|
+
}
|
|
444
|
+
|
|
445
|
+
/* ===================== 启动 ===================== */
|
|
446
|
+
|
|
447
|
+
async start() {
|
|
448
|
+
this.applyView();
|
|
449
|
+
this.renderHead();
|
|
450
|
+
if (this.embed) return;
|
|
451
|
+
await this.applyDoc(null);
|
|
452
|
+
notify.info(this.readOnly ? 'gbdocx 已就绪:只读预览' : 'gbdocx 已就绪:可直接编辑,或用「打开」载入 .docx');
|
|
453
|
+
}
|
|
454
|
+
|
|
455
|
+
destroy() {
|
|
456
|
+
// 全屏中的 .word-scope 挂着 body:先归还原位,再随挂载点一起移除
|
|
457
|
+
if (this.pageFullscreen) this.setPageFullscreen(false);
|
|
458
|
+
this.unbindKeys?.();
|
|
459
|
+
this.unbindWheel?.();
|
|
460
|
+
this.layoutObs?.disconnect();
|
|
461
|
+
window.removeEventListener('resize', this._onWinResize);
|
|
462
|
+
clearTimeout(this.pageTimer);
|
|
463
|
+
clearTimeout(this._autoTimer);
|
|
464
|
+
clearTimeout(this._peekTimer);
|
|
465
|
+
cancelAnimationFrame(this.pageRaf);
|
|
466
|
+
// 菜单/浮层挂 body:宿主卸载后不收,会留下悬空浮层与 document 监听
|
|
467
|
+
closeAll();
|
|
468
|
+
document.removeEventListener('keydown', this.onFullscreenEsc, true);
|
|
469
|
+
this.guard.destroy();
|
|
470
|
+
this.surface.destroy();
|
|
471
|
+
this.scopeEl?.remove();
|
|
472
|
+
}
|
|
473
|
+
}
|
|
474
|
+
|
|
475
|
+
Object.assign(
|
|
476
|
+
WordApp.prototype,
|
|
477
|
+
layoutMethods,
|
|
478
|
+
docMethods,
|
|
479
|
+
fileMethods,
|
|
480
|
+
editMethods,
|
|
481
|
+
catalogMethods,
|
|
482
|
+
commentsMethods,
|
|
483
|
+
notesMethods,
|
|
484
|
+
hfMethods,
|
|
485
|
+
viewMethods,
|
|
486
|
+
menusMethods
|
|
487
|
+
);
|