@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,440 @@
|
|
|
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 { parseDocx } from '../kernel/docx/codec/docx.js';
|
|
13
|
+
import { buildDocxVerified } from '../kernel/docx/codec/roundtrip.js';
|
|
14
|
+
import { runShortcut } from '../kernel/docx/wordShortcuts.js';
|
|
15
|
+
import { buildMenus } from '../kernel/docx/menus.js';
|
|
16
|
+
import { HF_MODES, defaultPageConf, defaultPageNumbers, defaultWatermark } from '../kernel/docx/constants.js';
|
|
17
|
+
import { notify } from '../kernel/notify.js';
|
|
18
|
+
import { useKeyboard, KEY_PRIORITY } from '../kernel/keyboard.js';
|
|
19
|
+
import { buildMenuBar } from '../ui/menubar.js';
|
|
20
|
+
import { closeAll } from '../ui/popup.js';
|
|
21
|
+
import { buildToolbar } from '../ui/toolbar.js';
|
|
22
|
+
import { StatusBar } from '../ui/statusbar.js';
|
|
23
|
+
import { CatalogPanel, CommentsPanel } from '../ui/panels.js';
|
|
24
|
+
import { PromptPopover } from '../ui/prompt.js';
|
|
25
|
+
import { FindBar } from '../ui/find-bar.js';
|
|
26
|
+
import { buildSpecialChars } from '../ui/special-chars.js';
|
|
27
|
+
import { openColumnDialog, openMarginDialog } from '../ui/dialogs.js';
|
|
28
|
+
import { renderSheets } from './word-sheets.js';
|
|
29
|
+
import { loadPrefs, savePrefs } from './word/shared.js';
|
|
30
|
+
import { layoutMethods } from './word/layout.js';
|
|
31
|
+
import { docMethods } from './word/doc.js';
|
|
32
|
+
import { fileMethods } from './word/file.js';
|
|
33
|
+
import { editMethods } from './word/edit.js';
|
|
34
|
+
import { catalogMethods } from './word/catalog.js';
|
|
35
|
+
import { commentsMethods } from './word/comments.js';
|
|
36
|
+
import { notesMethods } from './word/notes.js';
|
|
37
|
+
import { hfMethods } from './word/hf.js';
|
|
38
|
+
import { viewMethods } from './word/view.js';
|
|
39
|
+
import { menusMethods } from './word/menus.js';
|
|
40
|
+
|
|
41
|
+
export class WordApp {
|
|
42
|
+
/**
|
|
43
|
+
* @param {HTMLElement} mount 已挂在文档里的挂载点
|
|
44
|
+
* @param {object} [options] embed=嵌入宿主;io=文件动作;onDirty/isActive=脏态与快捷键闸门;
|
|
45
|
+
* readOnly=只读(禁写且锁编辑控件);hideMenuBar=隐藏顶部菜单栏那一行
|
|
46
|
+
*/
|
|
47
|
+
constructor(mount, options = {}) {
|
|
48
|
+
this.mount = mount;
|
|
49
|
+
this.embed = !!options.embed;
|
|
50
|
+
this.readOnly = !!options.readOnly;
|
|
51
|
+
this.menuBarVisible = !options.hideMenuBar;
|
|
52
|
+
this.io = options.io || null;
|
|
53
|
+
this.onDirty = typeof options.onDirty === 'function' ? options.onDirty : null;
|
|
54
|
+
this.isActive = typeof options.isActive === 'function' ? options.isActive : null;
|
|
55
|
+
this.doc = { name: '新建文档', handle: null };
|
|
56
|
+
this.state = {
|
|
57
|
+
sections: [],
|
|
58
|
+
page: defaultPageConf(),
|
|
59
|
+
pageNumbers: defaultPageNumbers(),
|
|
60
|
+
watermark: defaultWatermark(),
|
|
61
|
+
hfMode: HF_MODES[0].value,
|
|
62
|
+
header: '',
|
|
63
|
+
footer: '',
|
|
64
|
+
headerEven: '',
|
|
65
|
+
footerEven: '',
|
|
66
|
+
headerFirst: '',
|
|
67
|
+
footerFirst: '',
|
|
68
|
+
footnotes: [],
|
|
69
|
+
endnotes: [],
|
|
70
|
+
props: null,
|
|
71
|
+
defaultTabStop: 720,
|
|
72
|
+
compressPunctuation: false,
|
|
73
|
+
carry: [],
|
|
74
|
+
losses: [],
|
|
75
|
+
};
|
|
76
|
+
this.comments = [];
|
|
77
|
+
this.catalog = [];
|
|
78
|
+
/** 本次载入的「保存必丢」清单是否已由用户确认(自动保存不得绕过确认静默丢内容) */
|
|
79
|
+
this.lossAck = false;
|
|
80
|
+
this.lossAutoWarned = false;
|
|
81
|
+
this.footnotePages = new Map();
|
|
82
|
+
this.fnHeights = [];
|
|
83
|
+
this.pageCount = 1;
|
|
84
|
+
this.visiblePage = 1;
|
|
85
|
+
this.editMode = 'edit';
|
|
86
|
+
this.dirty = false;
|
|
87
|
+
this.saving = false;
|
|
88
|
+
this.suggestionCount = 0;
|
|
89
|
+
this.activeCommentKey = '';
|
|
90
|
+
this.bandEditSlot = '';
|
|
91
|
+
this.prefs = loadPrefs();
|
|
92
|
+
this.zoom = this.prefs.zoom || 100;
|
|
93
|
+
this.flags = this.prefs.flags || { catalog: false, commentsPanel: false };
|
|
94
|
+
this.lastEmittedHtml = '';
|
|
95
|
+
this.pageTimer = 0;
|
|
96
|
+
this.pageRaf = 0;
|
|
97
|
+
// [坑] 编辑器内核在 mount 时即抛一次 update(构造期),此时状态栏/工具栏尚未装配:
|
|
98
|
+
// 事件接线在构造完成前一律静默,避免读到未定义的宿主成员
|
|
99
|
+
this.ready = false;
|
|
100
|
+
this.build();
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
/* ===================== 几何读取 ===================== */
|
|
104
|
+
|
|
105
|
+
build() {
|
|
106
|
+
// [设计] 所有样式收在 .word-scope 下(本包 CSS 已按该前缀作用域化),宿主落地时不污染同页其它编辑器
|
|
107
|
+
const scope = document.createElement('div');
|
|
108
|
+
// 应用作用域:需要撑满宿主(.word-app 的 height:100% 以它为基准);浮层的 .word-scope 不挂这个类
|
|
109
|
+
scope.className = 'word-scope word-scope--app';
|
|
110
|
+
const root = document.createElement('div');
|
|
111
|
+
root.className = 'word-app word-skin';
|
|
112
|
+
if (this.embed) root.classList.add('gbd-is-embed');
|
|
113
|
+
root.innerHTML = `
|
|
114
|
+
<div class="gbd-menubar-host"></div>
|
|
115
|
+
<div class="word-head">
|
|
116
|
+
<span class="word-head__title"></span>
|
|
117
|
+
<span class="word-head__path"></span>
|
|
118
|
+
<span class="word-head__dirty" hidden>●</span>
|
|
119
|
+
<span class="word-head__spacer"></span>
|
|
120
|
+
<label class="word-head__auto"><input type="checkbox" class="word-head__autosave" />自动保存</label>
|
|
121
|
+
<button type="button" class="gbd-btn" data-act="open">打开</button>
|
|
122
|
+
<button type="button" class="gbd-btn" data-act="save">保存</button>
|
|
123
|
+
<button type="button" class="gbd-btn" data-act="export">导出副本</button>
|
|
124
|
+
</div>
|
|
125
|
+
<div class="gbd-toolbar-host"></div>
|
|
126
|
+
<div class="word-main">
|
|
127
|
+
<div class="word-editor"><div class="word-editor-mount"></div></div>
|
|
128
|
+
</div>
|
|
129
|
+
<div class="gbd-statusbar-host"></div>`;
|
|
130
|
+
scope.appendChild(root);
|
|
131
|
+
this.mount.appendChild(scope);
|
|
132
|
+
this.scopeEl = scope;
|
|
133
|
+
this.root = root;
|
|
134
|
+
this.menubarHost = root.querySelector('.gbd-menubar-host');
|
|
135
|
+
this.headEl = root.querySelector('.word-head');
|
|
136
|
+
this.toolbarHost = root.querySelector('.gbd-toolbar-host');
|
|
137
|
+
this.guard = createReadOnlyGuard(root, { editZones: () => [this.headEl, this.toolbarHost] });
|
|
138
|
+
this.editorWrap = root.querySelector('.word-editor');
|
|
139
|
+
this.titleEl = root.querySelector('.word-head__title');
|
|
140
|
+
this.pathEl = root.querySelector('.word-head__path');
|
|
141
|
+
this.dirtyEl = root.querySelector('.word-head__dirty');
|
|
142
|
+
this.autosaveEl = root.querySelector('.word-head__autosave');
|
|
143
|
+
|
|
144
|
+
this.catalogPanel = new CatalogPanel((key) => this.locateCatalog(key));
|
|
145
|
+
this.commentsPanel = new CommentsPanel({
|
|
146
|
+
locate: (key) => this.locateComment(key),
|
|
147
|
+
toggle: (key) => this.toggleComment(key),
|
|
148
|
+
remove: (key) => this.removeComment(key),
|
|
149
|
+
});
|
|
150
|
+
|
|
151
|
+
this.surface = new EditorSurface({
|
|
152
|
+
mount: root.querySelector('.word-editor-mount'),
|
|
153
|
+
placeholder: '开始输入正文…',
|
|
154
|
+
editable: true,
|
|
155
|
+
handlers: {
|
|
156
|
+
update: () => { if (this.ready) this.onDocUpdate(); },
|
|
157
|
+
'open-find': () => this.findBar.open(),
|
|
158
|
+
'request-link': () => this.insertLink(),
|
|
159
|
+
},
|
|
160
|
+
backdrop: (host) => renderSheets(host, this.sheetsCtx()),
|
|
161
|
+
});
|
|
162
|
+
this.editorState = this.editor.state();
|
|
163
|
+
|
|
164
|
+
this.toolbar = buildToolbar({
|
|
165
|
+
getEditor: () => this.editor,
|
|
166
|
+
getState: () => this.editorState,
|
|
167
|
+
isDisabled: () => this.editMode === 'readonly',
|
|
168
|
+
actions: {
|
|
169
|
+
insertTable: (e) => this.openTableSpec(e),
|
|
170
|
+
insertImage: () => this.pickImage(),
|
|
171
|
+
insertLink: () => this.insertLink(),
|
|
172
|
+
},
|
|
173
|
+
});
|
|
174
|
+
root.querySelector('.gbd-toolbar-host').appendChild(this.toolbar);
|
|
175
|
+
|
|
176
|
+
this.statusBar = new StatusBar({
|
|
177
|
+
onMode: (m) => this.setEditMode(m),
|
|
178
|
+
onZoomIn: () => this.setZoom(this.zoom + 10),
|
|
179
|
+
onZoomOut: () => this.setZoom(this.zoom - 10),
|
|
180
|
+
onZoomReset: () => this.setZoom(100),
|
|
181
|
+
onOpenMargin: () => this.openMarginDialog(),
|
|
182
|
+
onOpenColumn: () => this.openColumnDialog(),
|
|
183
|
+
});
|
|
184
|
+
root.querySelector('.gbd-statusbar-host').appendChild(this.statusBar.el);
|
|
185
|
+
|
|
186
|
+
this.findBar = new FindBar(() => this.editor);
|
|
187
|
+
root.querySelector('.word-editor').appendChild(this.findBar.el);
|
|
188
|
+
|
|
189
|
+
this.prompt = new PromptPopover();
|
|
190
|
+
this.prompt.onClosed = () => this.onPromptClosed();
|
|
191
|
+
|
|
192
|
+
this.imageInput = document.createElement('input');
|
|
193
|
+
this.imageInput.type = 'file';
|
|
194
|
+
this.imageInput.accept = 'image/*';
|
|
195
|
+
this.imageInput.multiple = true;
|
|
196
|
+
this.imageInput.hidden = true;
|
|
197
|
+
this.imageInput.addEventListener('change', () => this.onImagePicked());
|
|
198
|
+
root.appendChild(this.imageInput);
|
|
199
|
+
|
|
200
|
+
this.importInput = document.createElement('input');
|
|
201
|
+
this.importInput.type = 'file';
|
|
202
|
+
this.importInput.accept = '.docx';
|
|
203
|
+
this.importInput.hidden = true;
|
|
204
|
+
this.importInput.addEventListener('change', () => this.onImportFile());
|
|
205
|
+
root.appendChild(this.importInput);
|
|
206
|
+
|
|
207
|
+
this.charsPop = document.createElement('div');
|
|
208
|
+
this.charsPop.className = 'word-chars-pop';
|
|
209
|
+
this.charsPop.hidden = true;
|
|
210
|
+
this.charsPop.appendChild(buildSpecialChars((ch) => this.run('insertText', ch)));
|
|
211
|
+
root.querySelector('.word-editor').appendChild(this.charsPop);
|
|
212
|
+
|
|
213
|
+
this.bindHead();
|
|
214
|
+
this.buildChrome();
|
|
215
|
+
this.bindEvents();
|
|
216
|
+
this.applyZoom();
|
|
217
|
+
this.ready = true;
|
|
218
|
+
this.applyMode();
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
get editor() {
|
|
222
|
+
return this.surface.editor;
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
bindHead() {
|
|
226
|
+
const io = this.io || {};
|
|
227
|
+
const autosaveOn = typeof io.autosave === 'function' ? io.autosave() : !!this.prefs.autosave;
|
|
228
|
+
this.autosaveEl.checked = autosaveOn;
|
|
229
|
+
// 嵌入模式:文件头保留(标题/路径/脏标/保存钮照旧),但动作交给宿主 io
|
|
230
|
+
const bind = (act, ioKey, fallback) => {
|
|
231
|
+
this.root.querySelector(`[data-act="${act}"]`).addEventListener('click', () => {
|
|
232
|
+
const hostAction = this.embed ? this.io?.[ioKey] : null;
|
|
233
|
+
return hostAction ? hostAction() : fallback.call(this);
|
|
234
|
+
});
|
|
235
|
+
};
|
|
236
|
+
bind('open', 'open', this.openFile);
|
|
237
|
+
bind('save', 'save', this.save);
|
|
238
|
+
bind('export', 'exportCopy', this.exportDocx);
|
|
239
|
+
this.autosaveEl.addEventListener('change', () => {
|
|
240
|
+
const on = this.autosaveEl.checked;
|
|
241
|
+
if (this.embed && this.io?.setAutosave) return this.io.setAutosave(on);
|
|
242
|
+
this.prefs.autosave = on;
|
|
243
|
+
savePrefs(this.prefs);
|
|
244
|
+
});
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
/** 菜单栏(条目结构来自 docx/menus.js 的 buildMenus);只读档锁掉纯编辑类菜单 */
|
|
248
|
+
buildChrome() {
|
|
249
|
+
const menus = buildMenus(this.menuCtx());
|
|
250
|
+
this.menubarHost.replaceChildren(buildMenuBar(menus, this.readOnly ? ['edit', 'insert', 'table', 'format'] : []));
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
/** 构造参数落 DOM:装配完成后才调用,此时工具栏/状态栏/查找栏都已就位 */
|
|
254
|
+
applyMode() {
|
|
255
|
+
this.setMenuBarVisible(this.menuBarVisible);
|
|
256
|
+
if (this.readOnly) this.setReadOnly(true);
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
/** 显示/隐藏顶部菜单栏那一行;文件头、正文与状态栏不受影响 */
|
|
260
|
+
setMenuBarVisible(on) {
|
|
261
|
+
this.menuBarVisible = !!on;
|
|
262
|
+
this.menubarHost.style.display = this.menuBarVisible ? '' : 'none';
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
/** 只读开关:内核档位、DOM 守卫、编辑控件与菜单可用性同一处切换 */
|
|
266
|
+
setReadOnly(on) {
|
|
267
|
+
this.readOnly = !!on;
|
|
268
|
+
this.guard.setReadOnly(this.readOnly);
|
|
269
|
+
this.setEditMode(this.readOnly ? 'readonly' : 'edit');
|
|
270
|
+
this.statusBar.setReadOnly(this.readOnly);
|
|
271
|
+
this.findBar.setReadOnly(this.readOnly);
|
|
272
|
+
this.buildChrome();
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
bindEvents() {
|
|
276
|
+
// 编辑器状态刷新 → 工具栏/状态栏
|
|
277
|
+
const refresh = () => {
|
|
278
|
+
this.editorState = this.editor.state();
|
|
279
|
+
this.toolbar.refresh();
|
|
280
|
+
this.statusBar.setCaret(this.editorState.line || 0, this.editorState.column || 0);
|
|
281
|
+
};
|
|
282
|
+
this.editor.on('state', refresh);
|
|
283
|
+
this.editor.on('selection', refresh);
|
|
284
|
+
|
|
285
|
+
// 快捷键(Ctrl/⌘ 组合走 wordShortcuts;Esc 关特殊字符浮盘)
|
|
286
|
+
this.unbindKeys = useKeyboard({
|
|
287
|
+
keydown: (e) => {
|
|
288
|
+
// 嵌入模式:非激活页签不消费任何快捷键(多页签同页共存时键盘归属只看 active)
|
|
289
|
+
if (this.isActive && !this.isActive()) return false;
|
|
290
|
+
if (!e.ctrlKey && !e.metaKey) {
|
|
291
|
+
if (e.key === 'Escape' && !this.charsPop.hidden) {
|
|
292
|
+
this.charsPop.hidden = true;
|
|
293
|
+
return true;
|
|
294
|
+
}
|
|
295
|
+
return false;
|
|
296
|
+
}
|
|
297
|
+
return runShortcut(e, this.shortcutCtx());
|
|
298
|
+
},
|
|
299
|
+
}, { priority: KEY_PRIORITY.WINDOW });
|
|
300
|
+
|
|
301
|
+
// 滚动读数(可见页)
|
|
302
|
+
const scroll = this.surface.scrollEl;
|
|
303
|
+
scroll.addEventListener('scroll', () => this.refreshScroll(), { passive: true });
|
|
304
|
+
|
|
305
|
+
// Ctrl/⌘+滚轮:以指针为锚缩放(编辑器用 CSS zoom,滚动量是内容本地像素)
|
|
306
|
+
this.unbindWheel = bindCtrlWheelZoom({
|
|
307
|
+
target: scroll,
|
|
308
|
+
scrollEl: scroll,
|
|
309
|
+
scrollMode: 'local',
|
|
310
|
+
getScale: () => this.zoom / 100,
|
|
311
|
+
zoomStep: (direction) => {
|
|
312
|
+
const before = this.zoom;
|
|
313
|
+
this.setZoom(before + direction * 10);
|
|
314
|
+
return this.zoom !== before;
|
|
315
|
+
},
|
|
316
|
+
});
|
|
317
|
+
|
|
318
|
+
// 编辑器右键菜单
|
|
319
|
+
this.surface.contentEl.addEventListener('contextmenu', (e) => this.onContextMenu(e));
|
|
320
|
+
|
|
321
|
+
// 图片异步加载 / 窗口宽度变化都会改正文高度 → 重排
|
|
322
|
+
this.layoutObs = new ResizeObserver(() => this.schedulePaginate());
|
|
323
|
+
this.layoutObs.observe(this.editor.root);
|
|
324
|
+
this._onWinResize = () => this.schedulePaginate();
|
|
325
|
+
window.addEventListener('resize', this._onWinResize);
|
|
326
|
+
|
|
327
|
+
// 批注锚点 hover / 点击
|
|
328
|
+
this.surface.contentEl.addEventListener('mouseover', (e) => this.onAnchorOver(e));
|
|
329
|
+
this.surface.contentEl.addEventListener('mouseout', (e) => this.onAnchorOut(e));
|
|
330
|
+
this.surface.contentEl.addEventListener('click', (e) => this.onAnchorClick(e));
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
/* ===================== 纸面 ctx(供 renderSheets) ===================== */
|
|
334
|
+
|
|
335
|
+
onImportFile = async () => {
|
|
336
|
+
const file = this.importInput.files?.[0];
|
|
337
|
+
this.importInput.value = '';
|
|
338
|
+
if (!file) return;
|
|
339
|
+
if (!/\.docx$/i.test(file.name)) return notify.warning('只支持 .docx(旧版 .doc 请先用 Word 另存为 .docx)');
|
|
340
|
+
if (this.dirty && !this.confirmDiscardSync('导入会替换当前编辑内容,未保存的修改将丢失。仍然导入?')) return;
|
|
341
|
+
try {
|
|
342
|
+
const bytes = new Uint8Array(await file.arrayBuffer());
|
|
343
|
+
await this.applyDoc(bytes.length ? await parseDocx(bytes) : null);
|
|
344
|
+
this.setDirty(true);
|
|
345
|
+
notify.success(`已导入 ${file.name}——保存将写入当前文档`);
|
|
346
|
+
} catch (err) {
|
|
347
|
+
notify.error(`导入失败:${err.message || err}`);
|
|
348
|
+
}
|
|
349
|
+
};
|
|
350
|
+
|
|
351
|
+
/* ===================== 编辑动作 ===================== */
|
|
352
|
+
|
|
353
|
+
async selfTest() {
|
|
354
|
+
// 覆盖全部「模型承诺过」的往返口径:批注/脚注/尾注锚点、首页不同页眉、奇偶页、表格与标题
|
|
355
|
+
const html = '<h1>标题一</h1><p>正文 <strong>加粗</strong> 与 <em>斜体</em>'
|
|
356
|
+
+ ' <span class="doc-comment" data-cmt="c1">批注锚</span>'
|
|
357
|
+
+ '<sup class="doc-footnote" data-fn="fn1">1</sup><sup class="doc-endnote" data-fn="en1">1</sup></p>'
|
|
358
|
+
+ '<table><tbody><tr><td><p>A</p></td><td><p>B</p></td></tr></tbody></table>';
|
|
359
|
+
const model = {
|
|
360
|
+
html,
|
|
361
|
+
sections: [{
|
|
362
|
+
type: '',
|
|
363
|
+
page: this.state.page,
|
|
364
|
+
hfMode: 'except-first',
|
|
365
|
+
header: '页眉',
|
|
366
|
+
footer: '',
|
|
367
|
+
headerFirst: '首页页眉',
|
|
368
|
+
footerFirst: '',
|
|
369
|
+
pageNumbers: { enabled: true, align: 'center', format: 'arabic', start: 2 },
|
|
370
|
+
watermark: { data: '', color: '#AEB5C0', size: 120, opacity: 0.3 },
|
|
371
|
+
}],
|
|
372
|
+
comments: [{ key: 'c1', text: '批注文本', author: '我', date: '2025-01-01T00:00:00' }],
|
|
373
|
+
footnotes: [{ id: 'fn1', text: '脚注文本' }],
|
|
374
|
+
endnotes: [{ id: 'en1', text: '尾注文本' }],
|
|
375
|
+
props: null,
|
|
376
|
+
defaultTabStop: 720,
|
|
377
|
+
compressPunctuation: false,
|
|
378
|
+
carry: [],
|
|
379
|
+
};
|
|
380
|
+
const bytes = await buildDocxVerified(model);
|
|
381
|
+
const back = await parseDocx(bytes);
|
|
382
|
+
return {
|
|
383
|
+
bytes: bytes.length,
|
|
384
|
+
backHtmlLength: (back.html || '').length,
|
|
385
|
+
hasTitle: /标题一/.test(back.html || ''),
|
|
386
|
+
hasBold: /<strong|<b\b/i.test(back.html || ''),
|
|
387
|
+
hasTable: /<table/i.test(back.html || ''),
|
|
388
|
+
hasCommentAnchor: /data-cmt="0"/.test(back.html || ''),
|
|
389
|
+
hasFootnoteAnchor: /data-fn="fn1"/.test(back.html || ''),
|
|
390
|
+
hasEndnoteAnchor: /data-fn="en1"/.test(back.html || ''),
|
|
391
|
+
sections: (back.sections || []).length,
|
|
392
|
+
comments: (back.comments || []).length,
|
|
393
|
+
footnotes: (back.footnotes || []).length,
|
|
394
|
+
endnotes: (back.endnotes || []).length,
|
|
395
|
+
hfMode: (back.sections || [])[0]?.hfMode || '',
|
|
396
|
+
header: (back.sections || [])[0]?.header || '',
|
|
397
|
+
headerFirst: (back.sections || [])[0]?.headerFirst || '',
|
|
398
|
+
};
|
|
399
|
+
}
|
|
400
|
+
|
|
401
|
+
/* ===================== 启动 ===================== */
|
|
402
|
+
|
|
403
|
+
async start() {
|
|
404
|
+
this.applyView();
|
|
405
|
+
this.renderHead();
|
|
406
|
+
if (this.embed) return;
|
|
407
|
+
await this.applyDoc(null);
|
|
408
|
+
notify.info(this.readOnly ? 'gbdocx 已就绪:只读预览' : 'gbdocx 已就绪:可直接编辑,或用「打开」载入 .docx');
|
|
409
|
+
}
|
|
410
|
+
|
|
411
|
+
destroy() {
|
|
412
|
+
this.unbindKeys?.();
|
|
413
|
+
this.unbindWheel?.();
|
|
414
|
+
this.layoutObs?.disconnect();
|
|
415
|
+
window.removeEventListener('resize', this._onWinResize);
|
|
416
|
+
clearTimeout(this.pageTimer);
|
|
417
|
+
clearTimeout(this._autoTimer);
|
|
418
|
+
clearTimeout(this._peekTimer);
|
|
419
|
+
cancelAnimationFrame(this.pageRaf);
|
|
420
|
+
// 菜单/浮层挂 body:宿主卸载后不收,会留下悬空浮层与 document 监听
|
|
421
|
+
closeAll();
|
|
422
|
+
this.guard.destroy();
|
|
423
|
+
this.surface.destroy();
|
|
424
|
+
this.scopeEl?.remove();
|
|
425
|
+
}
|
|
426
|
+
}
|
|
427
|
+
|
|
428
|
+
Object.assign(
|
|
429
|
+
WordApp.prototype,
|
|
430
|
+
layoutMethods,
|
|
431
|
+
docMethods,
|
|
432
|
+
fileMethods,
|
|
433
|
+
editMethods,
|
|
434
|
+
catalogMethods,
|
|
435
|
+
commentsMethods,
|
|
436
|
+
notesMethods,
|
|
437
|
+
hfMethods,
|
|
438
|
+
viewMethods,
|
|
439
|
+
menusMethods
|
|
440
|
+
);
|
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
// [文件] gbdocx·纸面层渲染 —— 分页纸面 + 页眉页脚带 + 页码浮层 + 脚注/尾注带 + 水印(纯渲染,几何来自 pageGeometry)
|
|
2
|
+
import { GUTTER, bandStyle, fromSheetTop, pageNumberStyle, sheetStyle, stackStyle } from '../kernel/docx/pageGeometry.js';
|
|
3
|
+
import { watermarkStyle } from '../kernel/docx/watermark.js';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* 渲染纸面层
|
|
7
|
+
* @param {HTMLElement} host 背景层容器(.re-backdrop-host)
|
|
8
|
+
* @param {object} ctx
|
|
9
|
+
* geo(), layout(), pageCount(), page(), pageNumbers(), watermark(), zoom()
|
|
10
|
+
* hfMode(), hfTextAt(which,p), showHf(p,which), showPageNumber(p), hfDisplayHtml(which,p), footerHasPageField(p)
|
|
11
|
+
* pageNumberText(p), fnPageOf(p), fnHeights(), bandEdit(), sheetStyleOf(p), hfBandStyle(p,which), pageNumStyle(p), fnBandStyle(p), enBandStyle()
|
|
12
|
+
* onBandDblClick(slot, el), onFootnoteDblClick(fn), onEndnoteDblClick(en)
|
|
13
|
+
*/
|
|
14
|
+
export function renderSheets(host, ctx) {
|
|
15
|
+
const geo = ctx.geo();
|
|
16
|
+
const layout = ctx.layout();
|
|
17
|
+
const pageCount = ctx.pageCount();
|
|
18
|
+
const wmStyle = watermarkStyle(ctx.watermark(), geo);
|
|
19
|
+
|
|
20
|
+
host.replaceChildren();
|
|
21
|
+
const sheets = document.createElement('div');
|
|
22
|
+
sheets.className = 'word-sheets';
|
|
23
|
+
Object.assign(sheets.style, stackStyle(pageCount, geo, layout));
|
|
24
|
+
Object.assign(sheets.style, { left: '50%', transform: 'translateX(-50%)' });
|
|
25
|
+
|
|
26
|
+
for (let p = 1; p <= pageCount; p += 1) {
|
|
27
|
+
const sheet = document.createElement('div');
|
|
28
|
+
sheet.className = 'word-sheet';
|
|
29
|
+
Object.assign(sheet.style, ctx.sheetStyleOf(p));
|
|
30
|
+
if (wmStyle) Object.assign(sheet.style, wmStyle);
|
|
31
|
+
sheets.appendChild(sheet);
|
|
32
|
+
|
|
33
|
+
for (const which of ['header', 'footer']) {
|
|
34
|
+
if (!ctx.showHf(p, which)) continue;
|
|
35
|
+
const slot = ctx.bandSlot(which, p);
|
|
36
|
+
const editing = ctx.bandEdit() === slot;
|
|
37
|
+
const band = document.createElement('div');
|
|
38
|
+
band.className = `word-sheet-hf word-sheet-hf--${which === 'header' ? 'head' : 'foot'}`;
|
|
39
|
+
if (editing) band.classList.add('gbd-is-editing');
|
|
40
|
+
Object.assign(band.style, ctx.hfBandStyle(p, which));
|
|
41
|
+
band.contentEditable = editing ? 'true' : 'false';
|
|
42
|
+
band.innerHTML = ctx.hfDisplayHtml(which, p);
|
|
43
|
+
band.addEventListener('dblclick', () => ctx.onBandDblClick(slot, band));
|
|
44
|
+
band.addEventListener('blur', () => ctx.onBandCommit());
|
|
45
|
+
band.addEventListener('keydown', (e) => {
|
|
46
|
+
if (e.key === 'Escape') {
|
|
47
|
+
e.preventDefault();
|
|
48
|
+
ctx.onBandCommit();
|
|
49
|
+
}
|
|
50
|
+
});
|
|
51
|
+
sheets.appendChild(band);
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
if (ctx.showPageNumber(p) && !ctx.footerHasPageField(p)) {
|
|
55
|
+
const num = document.createElement('div');
|
|
56
|
+
num.className = 'word-sheet-num';
|
|
57
|
+
Object.assign(num.style, ctx.pageNumStyle(p));
|
|
58
|
+
num.textContent = ctx.pageNumberText(p);
|
|
59
|
+
sheets.appendChild(num);
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
const fns = ctx.fnPageOf(p);
|
|
63
|
+
if (fns.length) {
|
|
64
|
+
const band = document.createElement('div');
|
|
65
|
+
band.className = 'word-sheet-fn';
|
|
66
|
+
band.dataset.page = String(p);
|
|
67
|
+
Object.assign(band.style, ctx.fnBandStyle(p));
|
|
68
|
+
for (const fn of fns) {
|
|
69
|
+
const item = document.createElement('div');
|
|
70
|
+
item.className = 'word-sheet-fn-item';
|
|
71
|
+
const sup = document.createElement('sup');
|
|
72
|
+
sup.className = 'word-sheet-fn-ord';
|
|
73
|
+
sup.textContent = fn.ord || '';
|
|
74
|
+
item.append(sup, document.createTextNode(fn.text));
|
|
75
|
+
item.addEventListener('dblclick', () => ctx.onFootnoteDblClick(fn));
|
|
76
|
+
band.appendChild(item);
|
|
77
|
+
}
|
|
78
|
+
sheets.appendChild(band);
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
const endnotes = ctx.endnotes();
|
|
83
|
+
if (endnotes.length) {
|
|
84
|
+
const band = document.createElement('div');
|
|
85
|
+
band.className = 'word-sheet-fn word-sheet-en';
|
|
86
|
+
Object.assign(band.style, ctx.enBandStyle());
|
|
87
|
+
endnotes.forEach((en, i) => {
|
|
88
|
+
const item = document.createElement('div');
|
|
89
|
+
item.className = 'word-sheet-fn-item';
|
|
90
|
+
const sup = document.createElement('sup');
|
|
91
|
+
sup.className = 'word-sheet-fn-ord';
|
|
92
|
+
sup.textContent = String(i + 1);
|
|
93
|
+
item.append(sup, document.createTextNode(en.text));
|
|
94
|
+
item.addEventListener('dblclick', () => ctx.onEndnoteDblClick(en));
|
|
95
|
+
band.appendChild(item);
|
|
96
|
+
});
|
|
97
|
+
sheets.appendChild(band);
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
host.appendChild(sheets);
|
|
101
|
+
}
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
// [文件] web·office·docx·目录 —— 正文标题树抽取与点击定位(列表项带元素引用,缩进由 level 表达,面板只消费这份扁平列表)
|
|
2
|
+
import { DOC_CLASS } from '../editor/engine/schema.js';
|
|
3
|
+
import { CATALOG_HIT } from '../editor/engine/dom.js';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* 抽目录:正文根内的标题按文档顺序扁平化
|
|
7
|
+
* @param {HTMLElement} root 编辑面根
|
|
8
|
+
* @returns {Array<{key:string, text:string, level:number, el:HTMLElement}>}
|
|
9
|
+
*/
|
|
10
|
+
export const collectCatalog = (root) => {
|
|
11
|
+
if (!root) return [];
|
|
12
|
+
const out = [];
|
|
13
|
+
for (const el of Array.from(root.querySelectorAll('h1,h2,h3,h4,h5,h6'))) {
|
|
14
|
+
const text = (el.textContent || '').trim();
|
|
15
|
+
if (!text) continue;
|
|
16
|
+
const level = el.classList.contains(DOC_CLASS.TITLE) ? 1 : Number(el.tagName[1]);
|
|
17
|
+
out.push({ key: `t${out.length + 1}`, text, level, el });
|
|
18
|
+
}
|
|
19
|
+
return out;
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
/** 目录项定位:滚进视口并闪一下(与批注锚点定位同口径,禁各自写一套滚动) */
|
|
23
|
+
const scrollToCatalogItem = (el) => {
|
|
24
|
+
if (!el) return;
|
|
25
|
+
el.scrollIntoView({ block: 'center', behavior: 'smooth' });
|
|
26
|
+
el.classList.add(CATALOG_HIT);
|
|
27
|
+
setTimeout(() => el.classList.remove(CATALOG_HIT), 1200);
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
/** 目录接线:重建目录(原位替换,Vue 才能追踪) */
|
|
31
|
+
export const bindCatalog = (list, root) => {
|
|
32
|
+
list.value = collectCatalog(root);
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
/** 定位某一项,返回命中的键(未命中返回 null) */
|
|
36
|
+
export const gotoCatalogItem = (list, key) => {
|
|
37
|
+
const item = (list.value || []).find((c) => c.key === key);
|
|
38
|
+
if (!item) return null;
|
|
39
|
+
scrollToCatalogItem(item.el);
|
|
40
|
+
return key;
|
|
41
|
+
};
|