@a200888213/gbdocx 0.6.1 → 0.6.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (95) hide show
  1. package/LICENSE +21 -21
  2. package/README.md +215 -215
  3. package/index.html +31 -30
  4. package/package.json +49 -48
  5. package/serve.mjs +27 -23
  6. package/src/app/file-io.js +122 -122
  7. package/src/app/main.js +9 -9
  8. package/src/app/word/catalog.js +52 -52
  9. package/src/app/word/comments.js +112 -112
  10. package/src/app/word/doc.js +113 -113
  11. package/src/app/word/edit.js +72 -72
  12. package/src/app/word/file.js +191 -191
  13. package/src/app/word/hf.js +76 -76
  14. package/src/app/word/layout.js +155 -155
  15. package/src/app/word/menus.js +201 -201
  16. package/src/app/word/notes.js +110 -110
  17. package/src/app/word/shared.js +22 -22
  18. package/src/app/word/view.js +159 -159
  19. package/src/app/word-app.js +487 -487
  20. package/src/app/word-sheets.js +101 -101
  21. package/src/kernel/docx/catalog.js +41 -41
  22. package/src/kernel/docx/codec/blocks.js +123 -123
  23. package/src/kernel/docx/codec/carry.js +129 -129
  24. package/src/kernel/docx/codec/comments.js +25 -25
  25. package/src/kernel/docx/codec/docx.js +198 -198
  26. package/src/kernel/docx/codec/docxStyles.js +177 -177
  27. package/src/kernel/docx/codec/fonts.js +195 -195
  28. package/src/kernel/docx/codec/helpers.js +161 -161
  29. package/src/kernel/docx/codec/hfHtml.js +152 -152
  30. package/src/kernel/docx/codec/html.js +739 -739
  31. package/src/kernel/docx/codec/losses.js +420 -420
  32. package/src/kernel/docx/codec/markdown.js +170 -170
  33. package/src/kernel/docx/codec/parts.js +249 -249
  34. package/src/kernel/docx/codec/read.js +603 -603
  35. package/src/kernel/docx/codec/render.js +1156 -1156
  36. package/src/kernel/docx/codec/roundtrip.js +236 -236
  37. package/src/kernel/docx/codec/write.js +833 -833
  38. package/src/kernel/docx/comments.js +14 -14
  39. package/src/kernel/docx/menus.js +348 -348
  40. package/src/kernel/docx/pageGeometry.js +123 -123
  41. package/src/kernel/docx/pagination.js +481 -481
  42. package/src/kernel/docx/watermark.js +45 -45
  43. package/src/kernel/docx/wordShortcuts.js +37 -37
  44. package/src/kernel/editor/constants.js +8 -8
  45. package/src/kernel/editor/engine/blocks.js +594 -594
  46. package/src/kernel/editor/engine/blocksEnter.js +109 -109
  47. package/src/kernel/editor/engine/blocksInsert.js +254 -254
  48. package/src/kernel/editor/engine/blocksStructure.js +226 -226
  49. package/src/kernel/editor/engine/commands.js +181 -181
  50. package/src/kernel/editor/engine/dom.js +646 -646
  51. package/src/kernel/editor/engine/editor.js +213 -213
  52. package/src/kernel/editor/engine/history.js +149 -149
  53. package/src/kernel/editor/engine/inline.js +656 -656
  54. package/src/kernel/editor/engine/input.js +554 -554
  55. package/src/kernel/editor/engine/interaction.js +137 -137
  56. package/src/kernel/editor/engine/pointer.js +39 -39
  57. package/src/kernel/editor/engine/revision.js +138 -138
  58. package/src/kernel/editor/engine/schema.js +417 -417
  59. package/src/kernel/editor/engine/search.js +113 -113
  60. package/src/kernel/editor/engine/selection.js +119 -119
  61. package/src/kernel/editor/engine/state.js +118 -118
  62. package/src/kernel/editor/engine/tableClip.js +154 -154
  63. package/src/kernel/editor/engine/tableGrid.js +55 -55
  64. package/src/kernel/editor/engine/tableInteraction.js +388 -388
  65. package/src/kernel/editor/engine/tableMenu.js +250 -250
  66. package/src/kernel/editor/engine/tableOps.js +174 -174
  67. package/src/kernel/editor/engine/tables.js +732 -732
  68. package/src/kernel/editor/overlayGeometry.js +20 -20
  69. package/src/kernel/editor/pasteClean.js +293 -293
  70. package/src/kernel/editor/utils.js +48 -48
  71. package/src/kernel/keyboard.js +114 -114
  72. package/src/kernel/notify.js +54 -54
  73. package/src/kernel/textStyles.js +84 -84
  74. package/src/kernel/util/clipboard.js +89 -89
  75. package/src/kernel/util/imageFile.js +91 -91
  76. package/src/kernel/util/safeValue.js +25 -25
  77. package/src/kernel/util/shortcutKey.js +33 -33
  78. package/src/surface/editor-surface.js +603 -600
  79. package/src/ui/color-panel.js +69 -69
  80. package/src/ui/context-menu.js +135 -133
  81. package/src/ui/dialogs.js +249 -249
  82. package/src/ui/find-bar.js +151 -151
  83. package/src/ui/icons.js +245 -137
  84. package/src/ui/menubar.js +28 -28
  85. package/src/ui/panels.js +115 -115
  86. package/src/ui/popup.js +79 -79
  87. package/src/ui/prompt.js +89 -89
  88. package/src/ui/ribbon.js +485 -485
  89. package/src/ui/special-chars.js +39 -39
  90. package/src/ui/statusbar.js +81 -81
  91. package/src/ui/toolbar.js +32 -32
  92. package/styles/app.css +970 -970
  93. package/styles/editor-content.css +440 -440
  94. package/styles/tokens.css +154 -154
  95. package/styles/word-panel.css +410 -410
@@ -1,201 +1,201 @@
1
- // [文件] gbdocx·word·menus —— 右键菜单与快捷键上下文
2
-
3
- // [设计] 方法以独立函数导出,由 word-app.js 末尾 Object.assign 装配到 WordApp.prototype:
4
- // 仍是同一个 this(方法调用时绑定),调用方形状与拆分前完全一致。
5
-
6
- import { textMenuItems, tableMenuItems, revisionMenuItems } from '../../kernel/docx/menus.js';
7
- import { DOC_CLASS } from '../../kernel/editor/engine/schema.js';
8
- import { tableMenuModel } from '../../kernel/editor/engine/tableMenu.js';
9
- import { notify } from '../../kernel/notify.js';
10
- import { openMenu } from '../../ui/context-menu.js';
11
-
12
- import { savePrefs } from './shared.js';
13
- function onContextMenu(e) {
14
- if (!e.target.closest?.('.rich-text-content')) return;
15
- e.preventDefault();
16
- const ed = this.editor;
17
- const cell = ed.cellAt(e.clientX, e.clientY);
18
- const rev = e.target.closest?.(`.${DOC_CLASS.INS}, .${DOC_CLASS.DEL}`);
19
- const items = rev
20
- ? revisionMenuItems(rev, (el, accept) => { if (this.run('resolveRevisionAt', el, accept)) this.onDocUpdate(); })
21
- : cell
22
- ? tableMenuItems({ inTable: true }, {
23
- model: () => tableMenuModel(ed.tableContext() ?? null),
24
- state: () => ed.state() ?? null,
25
- can: (key) => ed.canTableAction(key) ?? false,
26
- active: (key) => ed.isTableActionActive(key) ?? false,
27
- exec: (command, ...args) => this.run(command, ...args),
28
- action: (key, payload, op) => this.tableAction(key, payload, op),
29
- })
30
- : textMenuItems(this.menuCtx());
31
- openMenu({ anchor: { left: e.clientX, top: e.clientY, right: e.clientX, bottom: e.clientY }, items, placement: 'bottom-start' });
32
- }
33
-
34
- function tableAction(action, payload, op = 'table') {
35
- if (op === 'clip') return this.editor?.runTableOp('clip', action, payload);
36
- return this.run('tableAction', action, payload);
37
- }
38
-
39
- function menuCtx() {
40
- const app = this;
41
- const io = this.io || {};
42
- // [坑] state 必须是活值:菜单结构只在启动时构建一次,普通快照会让勾选态/可用性永远停在首帧。
43
- // 这里用 getter 按打开那一刻的编辑器状态现读(readonly 属性在菜单求值时才访问)
44
- const ctx = {
45
- state: {
46
- get autosave() {
47
- return app.embed && typeof io.autosave === 'function' ? io.autosave() : !!app.prefs.autosave;
48
- },
49
- get suggesting() { return app.editMode === 'suggest'; },
50
- get blockType() { return app.editorState?.blockType; },
51
- get align() { return app.editorState?.textAlign; },
52
- get lineHeight() { return app.editorState?.lineHeight; },
53
- get firstLineIndent() { return app.editorState?.firstLineIndent; },
54
- get bold() { return app.editorState?.bold; },
55
- get italic() { return app.editorState?.italic; },
56
- get underline() { return app.editorState?.underline; },
57
- get strike() { return app.editorState?.strike; },
58
- get superscript() { return app.editorState?.superscript; },
59
- get subscript() { return app.editorState?.subscript; },
60
- get fontSize() { return app.editorState?.fontSize; },
61
- get fontFamily() { return app.editorState?.fontFamily; },
62
- get color() { return app.editorState?.color; },
63
- get backgroundColor() { return app.editorState?.backgroundColor; },
64
- get bulletList() { return app.editorState?.bulletList; },
65
- get orderedList() { return app.editorState?.orderedList; },
66
- get taskList() { return app.editorState?.taskList; },
67
- get inTable() { return app.editorState?.inTable; },
68
- get hasSelection() { return app.editorState?.hasSelection; },
69
- get columns() { return app.state.page.columns || 1; },
70
- get pageNumbers() { return app.state.pageNumbers; },
71
- get hfMode() { return app.state.hfMode; },
72
- get catalog() { return app.flags.catalog; },
73
- get commentsPanel() { return app.flags.commentsPanel; },
74
- },
75
- file: {
76
- // 嵌入模式:文件动作全部交给宿主 io(打开新页签/写回存储/另存为弹窗),缺省才走包内自带的本地文件实现
77
- newDoc: io.newDoc || (() => this.newDoc()),
78
- open: io.open || (() => this.openFile()),
79
- recentList: () => [],
80
- openRecent: () => {},
81
- save: io.save || (() => this.save()),
82
- saveAs: io.saveAs || (() => this.saveAs()),
83
- setAutosave: io.setAutosave || ((on) => { this.prefs.autosave = on; savePrefs(this.prefs); this.autosaveEl.checked = on; }),
84
- pageSetup: () => this.openPageSetupDialog(),
85
- importDoc: io.importDoc || (() => this.importInput.click()),
86
- exportCopy: io.exportCopy || (() => this.exportDocx()),
87
- exportMarkdown: io.exportMarkdown || (() => this.exportMarkdown()),
88
- print: io.print || (() => this.print()),
89
- },
90
- edit: {
91
- undo: () => this.run('undo'),
92
- redo: () => this.run('redo'),
93
- canUndo: () => Boolean(this.editorState.canUndo),
94
- canRedo: () => Boolean(this.editorState.canRedo),
95
- cut: () => { this.editor.focus(); document.execCommand('cut'); },
96
- copy: () => { this.editor.focus(); document.execCommand('copy'); },
97
- paste: () => notify.info('请用 Ctrl+V 粘贴(浏览器不允许脚本触发粘贴)'),
98
- find: (replace) => this.findBar.open(!!replace),
99
- selectAll: () => this.editor?.exec('selectAll'),
100
- setSuggesting: (on) => {
101
- this.setEditMode(on ? 'suggest' : 'edit');
102
- notify.info(on ? '已开启「建议修改」:输入会成为插入建议,删除会成为删除建议' : '已关闭「建议修改」');
103
- },
104
- suggestionCount: () => this.suggestionCount,
105
- acceptAll: () => {
106
- const n = this.suggestionCount;
107
- if (n && this.run('resolveRevisions', true)) notify.success(`已接受 ${n} 条建议`);
108
- this.refreshStats();
109
- },
110
- rejectAll: () => {
111
- const n = this.suggestionCount;
112
- if (n && this.run('resolveRevisions', false)) notify.success(`已拒绝 ${n} 条建议`);
113
- this.refreshStats();
114
- },
115
- },
116
- insert: {
117
- image: () => this.pickImage(),
118
- table: (e) => this.openTableSpec(e),
119
- link: () => this.insertLink(),
120
- comment: () => this.insertComment(),
121
- footnote: () => this.insertFootnote(),
122
- toc: () => this.insertToc(),
123
- refreshToc: () => this.refreshToc(),
124
- setPageNumber: (align) => {
125
- this.state.pageNumbers = { ...this.state.pageNumbers, enabled: true, align };
126
- this.onDocUpdate();
127
- },
128
- pageNumberSetup: () => this.openPageSetupDialog(),
129
- watermark: () => this.openPageSetupDialog(),
130
- pageBreak: () => this.run('insertPageBreak'),
131
- hr: () => this.run('setHorizontalRule'),
132
- specialChars: () => { this.charsPop.hidden = !this.charsPop.hidden; },
133
- },
134
- table: {
135
- action: (key, payload, op) => this.tableAction(key, payload, op),
136
- model: () => tableMenuModel(this.editor?.tableContext() ?? null),
137
- state: () => this.editor?.state() ?? null,
138
- can: (key) => this.editor?.canTableAction(key) ?? false,
139
- active: (key) => this.editor?.isTableActionActive(key) ?? false,
140
- exec: (command, ...args) => this.run(command, ...args),
141
- },
142
- format: {
143
- setLayout: (cols) => { this.state.page = { ...this.state.page, columns: cols }; this.onDocUpdate(); this.paginate(); },
144
- paragraphStyle: (kind) => this.run('convertBlock', kind),
145
- align: (v) => this.run('setBlockAlign', v),
146
- setLineHeight: (v) => this.run('setBlockLineHeight', v),
147
- firstLine: () => this.run('toggleFirstLineIndent'),
148
- bold: () => this.run('toggleBold'),
149
- italic: () => this.run('toggleItalic'),
150
- underline: () => this.run('toggleUnderline'),
151
- strike: () => this.run('toggleStrike'),
152
- superscript: () => this.run('toggleSuperscript'),
153
- subscript: () => this.run('toggleSubscript'),
154
- setFontFamily: (v) => this.run('setFontFamily', v),
155
- setFontSize: (v) => this.run('setFontSize', v),
156
- setColor: (v) => this.run('setColor', v),
157
- unsetColor: () => this.run('unsetColor'),
158
- setHighlight: (v) => this.run('setBackgroundColor', v),
159
- unsetHighlight: () => this.run('unsetBackgroundColor'),
160
- indent: (command) => this.run(command),
161
- setHfMode: (mode) => { this.state.hfMode = mode; this.onDocUpdate(); },
162
- list: (command) => this.run(command),
163
- clearFormat: () => this.run('clearFormat'),
164
- },
165
- view: {
166
- zoomIn: () => this.setZoom(this.zoom + 10),
167
- zoomOut: () => this.setZoom(this.zoom - 10),
168
- zoomReset: () => this.setZoom(100),
169
- setView: (key, on) => this.setView(key, on),
170
- },
171
- };
172
- // [设计] 只读档在这里统一短路写动作:菜单栏、右键菜单、快捷键共用这一份 ctx,
173
- // 不必逐条给菜单项挂 disabled;查询函数(model/state/can…)必须留着,否则菜单结构会读不到数据。
174
- if (app.readOnly) {
175
- const allow = {
176
- file: ['recentList', 'openRecent', 'exportCopy', 'exportMarkdown', 'print'],
177
- edit: ['canUndo', 'canRedo', 'copy', 'find', 'selectAll', 'suggestionCount'],
178
- insert: [],
179
- table: ['model', 'state', 'can', 'active'],
180
- format: [],
181
- };
182
- for (const [group, keys] of Object.entries(allow)) {
183
- for (const [key, fn] of Object.entries(ctx[group])) {
184
- if (typeof fn === 'function' && !keys.includes(key)) ctx[group][key] = () => false;
185
- }
186
- }
187
- }
188
- return ctx;
189
- }
190
-
191
- function shortcutCtx() {
192
- const file = this.menuCtx().file;
193
- return {
194
- file,
195
- insert: { pageBreak: () => this.run('insertPageBreak'), link: () => this.insertLink(), comment: () => this.insertComment(), list: (ordered) => this.run(ordered ? 'toggleOrderedList' : 'toggleBulletList') },
196
- edit: { find: () => this.findBar.open(false), findReplace: () => this.findBar.open(true) },
197
- view: { zoomIn: () => this.setZoom(this.zoom + 10), zoomOut: () => this.setZoom(this.zoom - 10), zoomReset: () => this.setZoom(100) },
198
- };
199
- }
200
-
201
- export const menusMethods = { onContextMenu, tableAction, menuCtx, shortcutCtx };
1
+ // [文件] gbdocx·word·menus —— 右键菜单与快捷键上下文
2
+
3
+ // [设计] 方法以独立函数导出,由 word-app.js 末尾 Object.assign 装配到 WordApp.prototype:
4
+ // 仍是同一个 this(方法调用时绑定),调用方形状与拆分前完全一致。
5
+
6
+ import { textMenuItems, tableMenuItems, revisionMenuItems } from '../../kernel/docx/menus.js';
7
+ import { DOC_CLASS } from '../../kernel/editor/engine/schema.js';
8
+ import { tableMenuModel } from '../../kernel/editor/engine/tableMenu.js';
9
+ import { notify } from '../../kernel/notify.js';
10
+ import { openMenu } from '../../ui/context-menu.js';
11
+
12
+ import { savePrefs } from './shared.js';
13
+ function onContextMenu(e) {
14
+ if (!e.target.closest?.('.rich-text-content')) return;
15
+ e.preventDefault();
16
+ const ed = this.editor;
17
+ const cell = ed.cellAt(e.clientX, e.clientY);
18
+ const rev = e.target.closest?.(`.${DOC_CLASS.INS}, .${DOC_CLASS.DEL}`);
19
+ const items = rev
20
+ ? revisionMenuItems(rev, (el, accept) => { if (this.run('resolveRevisionAt', el, accept)) this.onDocUpdate(); })
21
+ : cell
22
+ ? tableMenuItems({ inTable: true }, {
23
+ model: () => tableMenuModel(ed.tableContext() ?? null),
24
+ state: () => ed.state() ?? null,
25
+ can: (key) => ed.canTableAction(key) ?? false,
26
+ active: (key) => ed.isTableActionActive(key) ?? false,
27
+ exec: (command, ...args) => this.run(command, ...args),
28
+ action: (key, payload, op) => this.tableAction(key, payload, op),
29
+ })
30
+ : textMenuItems(this.menuCtx());
31
+ openMenu({ anchor: { left: e.clientX, top: e.clientY, right: e.clientX, bottom: e.clientY }, items, placement: 'bottom-start' });
32
+ }
33
+
34
+ function tableAction(action, payload, op = 'table') {
35
+ if (op === 'clip') return this.editor?.runTableOp('clip', action, payload);
36
+ return this.run('tableAction', action, payload);
37
+ }
38
+
39
+ function menuCtx() {
40
+ const app = this;
41
+ const io = this.io || {};
42
+ // [坑] state 必须是活值:菜单结构只在启动时构建一次,普通快照会让勾选态/可用性永远停在首帧。
43
+ // 这里用 getter 按打开那一刻的编辑器状态现读(readonly 属性在菜单求值时才访问)
44
+ const ctx = {
45
+ state: {
46
+ get autosave() {
47
+ return app.embed && typeof io.autosave === 'function' ? io.autosave() : !!app.prefs.autosave;
48
+ },
49
+ get suggesting() { return app.editMode === 'suggest'; },
50
+ get blockType() { return app.editorState?.blockType; },
51
+ get align() { return app.editorState?.textAlign; },
52
+ get lineHeight() { return app.editorState?.lineHeight; },
53
+ get firstLineIndent() { return app.editorState?.firstLineIndent; },
54
+ get bold() { return app.editorState?.bold; },
55
+ get italic() { return app.editorState?.italic; },
56
+ get underline() { return app.editorState?.underline; },
57
+ get strike() { return app.editorState?.strike; },
58
+ get superscript() { return app.editorState?.superscript; },
59
+ get subscript() { return app.editorState?.subscript; },
60
+ get fontSize() { return app.editorState?.fontSize; },
61
+ get fontFamily() { return app.editorState?.fontFamily; },
62
+ get color() { return app.editorState?.color; },
63
+ get backgroundColor() { return app.editorState?.backgroundColor; },
64
+ get bulletList() { return app.editorState?.bulletList; },
65
+ get orderedList() { return app.editorState?.orderedList; },
66
+ get taskList() { return app.editorState?.taskList; },
67
+ get inTable() { return app.editorState?.inTable; },
68
+ get hasSelection() { return app.editorState?.hasSelection; },
69
+ get columns() { return app.state.page.columns || 1; },
70
+ get pageNumbers() { return app.state.pageNumbers; },
71
+ get hfMode() { return app.state.hfMode; },
72
+ get catalog() { return app.flags.catalog; },
73
+ get commentsPanel() { return app.flags.commentsPanel; },
74
+ },
75
+ file: {
76
+ // 嵌入模式:文件动作全部交给宿主 io(打开新页签/写回存储/另存为弹窗),缺省才走包内自带的本地文件实现
77
+ newDoc: io.newDoc || (() => this.newDoc()),
78
+ open: io.open || (() => this.openFile()),
79
+ recentList: () => [],
80
+ openRecent: () => {},
81
+ save: io.save || (() => this.save()),
82
+ saveAs: io.saveAs || (() => this.saveAs()),
83
+ setAutosave: io.setAutosave || ((on) => { this.prefs.autosave = on; savePrefs(this.prefs); this.autosaveEl.checked = on; }),
84
+ pageSetup: () => this.openPageSetupDialog(),
85
+ importDoc: io.importDoc || (() => this.importInput.click()),
86
+ exportCopy: io.exportCopy || (() => this.exportDocx()),
87
+ exportMarkdown: io.exportMarkdown || (() => this.exportMarkdown()),
88
+ print: io.print || (() => this.print()),
89
+ },
90
+ edit: {
91
+ undo: () => this.run('undo'),
92
+ redo: () => this.run('redo'),
93
+ canUndo: () => Boolean(this.editorState.canUndo),
94
+ canRedo: () => Boolean(this.editorState.canRedo),
95
+ cut: () => { this.editor.focus(); document.execCommand('cut'); },
96
+ copy: () => { this.editor.focus(); document.execCommand('copy'); },
97
+ paste: () => notify.info('请用 Ctrl+V 粘贴(浏览器不允许脚本触发粘贴)'),
98
+ find: (replace) => this.findBar.open(!!replace),
99
+ selectAll: () => this.editor?.exec('selectAll'),
100
+ setSuggesting: (on) => {
101
+ this.setEditMode(on ? 'suggest' : 'edit');
102
+ notify.info(on ? '已开启「建议修改」:输入会成为插入建议,删除会成为删除建议' : '已关闭「建议修改」');
103
+ },
104
+ suggestionCount: () => this.suggestionCount,
105
+ acceptAll: () => {
106
+ const n = this.suggestionCount;
107
+ if (n && this.run('resolveRevisions', true)) notify.success(`已接受 ${n} 条建议`);
108
+ this.refreshStats();
109
+ },
110
+ rejectAll: () => {
111
+ const n = this.suggestionCount;
112
+ if (n && this.run('resolveRevisions', false)) notify.success(`已拒绝 ${n} 条建议`);
113
+ this.refreshStats();
114
+ },
115
+ },
116
+ insert: {
117
+ image: () => this.pickImage(),
118
+ table: (e) => this.openTableSpec(e),
119
+ link: () => this.insertLink(),
120
+ comment: () => this.insertComment(),
121
+ footnote: () => this.insertFootnote(),
122
+ toc: () => this.insertToc(),
123
+ refreshToc: () => this.refreshToc(),
124
+ setPageNumber: (align) => {
125
+ this.state.pageNumbers = { ...this.state.pageNumbers, enabled: true, align };
126
+ this.onDocUpdate();
127
+ },
128
+ pageNumberSetup: () => this.openPageSetupDialog(),
129
+ watermark: () => this.openPageSetupDialog(),
130
+ pageBreak: () => this.run('insertPageBreak'),
131
+ hr: () => this.run('setHorizontalRule'),
132
+ specialChars: () => { this.charsPop.hidden = !this.charsPop.hidden; },
133
+ },
134
+ table: {
135
+ action: (key, payload, op) => this.tableAction(key, payload, op),
136
+ model: () => tableMenuModel(this.editor?.tableContext() ?? null),
137
+ state: () => this.editor?.state() ?? null,
138
+ can: (key) => this.editor?.canTableAction(key) ?? false,
139
+ active: (key) => this.editor?.isTableActionActive(key) ?? false,
140
+ exec: (command, ...args) => this.run(command, ...args),
141
+ },
142
+ format: {
143
+ setLayout: (cols) => { this.state.page = { ...this.state.page, columns: cols }; this.onDocUpdate(); this.paginate(); },
144
+ paragraphStyle: (kind) => this.run('convertBlock', kind),
145
+ align: (v) => this.run('setBlockAlign', v),
146
+ setLineHeight: (v) => this.run('setBlockLineHeight', v),
147
+ firstLine: () => this.run('toggleFirstLineIndent'),
148
+ bold: () => this.run('toggleBold'),
149
+ italic: () => this.run('toggleItalic'),
150
+ underline: () => this.run('toggleUnderline'),
151
+ strike: () => this.run('toggleStrike'),
152
+ superscript: () => this.run('toggleSuperscript'),
153
+ subscript: () => this.run('toggleSubscript'),
154
+ setFontFamily: (v) => this.run('setFontFamily', v),
155
+ setFontSize: (v) => this.run('setFontSize', v),
156
+ setColor: (v) => this.run('setColor', v),
157
+ unsetColor: () => this.run('unsetColor'),
158
+ setHighlight: (v) => this.run('setBackgroundColor', v),
159
+ unsetHighlight: () => this.run('unsetBackgroundColor'),
160
+ indent: (command) => this.run(command),
161
+ setHfMode: (mode) => { this.state.hfMode = mode; this.onDocUpdate(); },
162
+ list: (command) => this.run(command),
163
+ clearFormat: () => this.run('clearFormat'),
164
+ },
165
+ view: {
166
+ zoomIn: () => this.setZoom(this.zoom + 10),
167
+ zoomOut: () => this.setZoom(this.zoom - 10),
168
+ zoomReset: () => this.setZoom(100),
169
+ setView: (key, on) => this.setView(key, on),
170
+ },
171
+ };
172
+ // [设计] 只读档在这里统一短路写动作:菜单栏、右键菜单、快捷键共用这一份 ctx,
173
+ // 不必逐条给菜单项挂 disabled;查询函数(model/state/can…)必须留着,否则菜单结构会读不到数据。
174
+ if (app.readOnly) {
175
+ const allow = {
176
+ file: ['recentList', 'openRecent', 'exportCopy', 'exportMarkdown', 'print'],
177
+ edit: ['canUndo', 'canRedo', 'copy', 'find', 'selectAll', 'suggestionCount'],
178
+ insert: [],
179
+ table: ['model', 'state', 'can', 'active'],
180
+ format: [],
181
+ };
182
+ for (const [group, keys] of Object.entries(allow)) {
183
+ for (const [key, fn] of Object.entries(ctx[group])) {
184
+ if (typeof fn === 'function' && !keys.includes(key)) ctx[group][key] = () => false;
185
+ }
186
+ }
187
+ }
188
+ return ctx;
189
+ }
190
+
191
+ function shortcutCtx() {
192
+ const file = this.menuCtx().file;
193
+ return {
194
+ file,
195
+ insert: { pageBreak: () => this.run('insertPageBreak'), link: () => this.insertLink(), comment: () => this.insertComment(), list: (ordered) => this.run(ordered ? 'toggleOrderedList' : 'toggleBulletList') },
196
+ edit: { find: () => this.findBar.open(false), findReplace: () => this.findBar.open(true) },
197
+ view: { zoomIn: () => this.setZoom(this.zoom + 10), zoomOut: () => this.setZoom(this.zoom - 10), zoomReset: () => this.setZoom(100) },
198
+ };
199
+ }
200
+
201
+ export const menusMethods = { onContextMenu, tableAction, menuCtx, shortcutCtx };
@@ -1,110 +1,110 @@
1
- // [文件] gbdocx·word·notes —— 脚注与尾注 —— 页码刷新、重新编号、插入与编辑
2
-
3
- // [设计] 方法以独立函数导出,由 word-app.js 末尾 Object.assign 装配到 WordApp.prototype:
4
- // 仍是同一个 this(方法调用时绑定),调用方形状与拆分前完全一致。
5
-
6
- import { genId } from 'guabiutils';
7
- import { pageOfElement } from '../../kernel/docx/pagination.js';
8
- import { DOC_CLASS, NOTE_ATTR } from '../../kernel/editor/engine/schema.js';
9
- import { notify } from '../../kernel/notify.js';
10
- function refreshFootnotePages() {
11
- const root = this.editor?.root;
12
- if (!root || this.state.footnotes.length === 0) {
13
- this.footnotePages = new Map();
14
- return;
15
- }
16
- const byPage = new Map();
17
- for (const sup of root.querySelectorAll(`sup.${DOC_CLASS.FOOTNOTE}`)) {
18
- const id = sup.getAttribute(NOTE_ATTR);
19
- const item = this.state.footnotes.find((n) => n.id === id);
20
- if (!item) continue;
21
- const p = pageOfElement(root, sup);
22
- if (!byPage.has(p)) byPage.set(p, []);
23
- byPage.get(p).push({ id: item.id, text: item.text, ord: sup.textContent || '' });
24
- }
25
- this.footnotePages = byPage;
26
- }
27
-
28
- function renumberFootnotes() {
29
- const sups = this.editor?.root?.querySelectorAll(`sup.${DOC_CLASS.FOOTNOTE}`);
30
- if (!sups) return;
31
- Array.from(sups).forEach((sup, i) => { sup.textContent = String(i + 1); });
32
- }
33
-
34
- function insertFootnote() {
35
- const ed = this.editor;
36
- if (!ed || this.editMode === 'readonly') return;
37
- ed.focus();
38
- const id = genId('fn');
39
- const ord = String((ed.root?.querySelectorAll(`sup.${DOC_CLASS.FOOTNOTE}`).length || 0) + 1);
40
- if (!this.run('insertNote', { id, ord })) return notify.warning('请先把光标放进正文');
41
- this._pendingNoteId = id;
42
- const anchor = ed.root?.querySelector(`sup.${DOC_CLASS.FOOTNOTE}[${NOTE_ATTR}="${id}"]`);
43
- this.prompt.openAt(anchor, {
44
- title: '脚注内容',
45
- placeholder: '写下脚注…',
46
- maxlength: 500,
47
- submit: (text) => {
48
- this._pendingNoteId = '';
49
- if (text.trim() === '') {
50
- this.run('removeNote', id);
51
- this.renumberFootnotes();
52
- return true;
53
- }
54
- this.state.footnotes = [...this.state.footnotes, { id, text }];
55
- this.renumberFootnotes();
56
- this.onDocUpdate();
57
- return true;
58
- },
59
- });
60
- }
61
-
62
- function editFootnote(fn) {
63
- const anchor = this.editor.root?.querySelector(`sup.${DOC_CLASS.FOOTNOTE}[${NOTE_ATTR}="${fn.id}"]`);
64
- this.prompt.openAt(anchor, {
65
- title: '脚注内容',
66
- placeholder: '留空提交 = 删除该脚注',
67
- maxlength: 500,
68
- value: fn.text,
69
- submit: (text) => {
70
- if (text.trim() === '') {
71
- this.state.footnotes = this.state.footnotes.filter((n) => n.id !== fn.id);
72
- this.run('removeNote', fn.id);
73
- this.renumberFootnotes();
74
- } else {
75
- this.state.footnotes = this.state.footnotes.map((n) => (n.id === fn.id ? { ...n, text } : n));
76
- }
77
- this.onDocUpdate();
78
- return true;
79
- },
80
- });
81
- }
82
-
83
- function editEndnote(en) {
84
- const anchor = this.editor.root?.querySelector(`sup.${DOC_CLASS.ENDNOTE}[${NOTE_ATTR}="${en.id}"]`);
85
- this.prompt.openAt(anchor, {
86
- title: '尾注内容',
87
- placeholder: '留空提交 = 删除该尾注',
88
- maxlength: 500,
89
- value: en.text,
90
- submit: (text) => {
91
- if (text.trim() === '') {
92
- this.state.endnotes = this.state.endnotes.filter((n) => n.id !== en.id);
93
- this.run('removeNote', en.id);
94
- } else {
95
- this.state.endnotes = this.state.endnotes.map((n) => (n.id === en.id ? { ...n, text } : n));
96
- }
97
- this.onDocUpdate();
98
- return true;
99
- },
100
- });
101
- }
102
-
103
- function rollbackPendingNote() {
104
- if (!this._pendingNoteId) return;
105
- const id = this._pendingNoteId;
106
- this._pendingNoteId = '';
107
- this.run('removeNote', id);
108
- }
109
-
110
- export const notesMethods = { refreshFootnotePages, renumberFootnotes, insertFootnote, editFootnote, editEndnote, rollbackPendingNote };
1
+ // [文件] gbdocx·word·notes —— 脚注与尾注 —— 页码刷新、重新编号、插入与编辑
2
+
3
+ // [设计] 方法以独立函数导出,由 word-app.js 末尾 Object.assign 装配到 WordApp.prototype:
4
+ // 仍是同一个 this(方法调用时绑定),调用方形状与拆分前完全一致。
5
+
6
+ import { genId } from 'guabiutils';
7
+ import { pageOfElement } from '../../kernel/docx/pagination.js';
8
+ import { DOC_CLASS, NOTE_ATTR } from '../../kernel/editor/engine/schema.js';
9
+ import { notify } from '../../kernel/notify.js';
10
+ function refreshFootnotePages() {
11
+ const root = this.editor?.root;
12
+ if (!root || this.state.footnotes.length === 0) {
13
+ this.footnotePages = new Map();
14
+ return;
15
+ }
16
+ const byPage = new Map();
17
+ for (const sup of root.querySelectorAll(`sup.${DOC_CLASS.FOOTNOTE}`)) {
18
+ const id = sup.getAttribute(NOTE_ATTR);
19
+ const item = this.state.footnotes.find((n) => n.id === id);
20
+ if (!item) continue;
21
+ const p = pageOfElement(root, sup);
22
+ if (!byPage.has(p)) byPage.set(p, []);
23
+ byPage.get(p).push({ id: item.id, text: item.text, ord: sup.textContent || '' });
24
+ }
25
+ this.footnotePages = byPage;
26
+ }
27
+
28
+ function renumberFootnotes() {
29
+ const sups = this.editor?.root?.querySelectorAll(`sup.${DOC_CLASS.FOOTNOTE}`);
30
+ if (!sups) return;
31
+ Array.from(sups).forEach((sup, i) => { sup.textContent = String(i + 1); });
32
+ }
33
+
34
+ function insertFootnote() {
35
+ const ed = this.editor;
36
+ if (!ed || this.editMode === 'readonly') return;
37
+ ed.focus();
38
+ const id = genId('fn');
39
+ const ord = String((ed.root?.querySelectorAll(`sup.${DOC_CLASS.FOOTNOTE}`).length || 0) + 1);
40
+ if (!this.run('insertNote', { id, ord })) return notify.warning('请先把光标放进正文');
41
+ this._pendingNoteId = id;
42
+ const anchor = ed.root?.querySelector(`sup.${DOC_CLASS.FOOTNOTE}[${NOTE_ATTR}="${id}"]`);
43
+ this.prompt.openAt(anchor, {
44
+ title: '脚注内容',
45
+ placeholder: '写下脚注…',
46
+ maxlength: 500,
47
+ submit: (text) => {
48
+ this._pendingNoteId = '';
49
+ if (text.trim() === '') {
50
+ this.run('removeNote', id);
51
+ this.renumberFootnotes();
52
+ return true;
53
+ }
54
+ this.state.footnotes = [...this.state.footnotes, { id, text }];
55
+ this.renumberFootnotes();
56
+ this.onDocUpdate();
57
+ return true;
58
+ },
59
+ });
60
+ }
61
+
62
+ function editFootnote(fn) {
63
+ const anchor = this.editor.root?.querySelector(`sup.${DOC_CLASS.FOOTNOTE}[${NOTE_ATTR}="${fn.id}"]`);
64
+ this.prompt.openAt(anchor, {
65
+ title: '脚注内容',
66
+ placeholder: '留空提交 = 删除该脚注',
67
+ maxlength: 500,
68
+ value: fn.text,
69
+ submit: (text) => {
70
+ if (text.trim() === '') {
71
+ this.state.footnotes = this.state.footnotes.filter((n) => n.id !== fn.id);
72
+ this.run('removeNote', fn.id);
73
+ this.renumberFootnotes();
74
+ } else {
75
+ this.state.footnotes = this.state.footnotes.map((n) => (n.id === fn.id ? { ...n, text } : n));
76
+ }
77
+ this.onDocUpdate();
78
+ return true;
79
+ },
80
+ });
81
+ }
82
+
83
+ function editEndnote(en) {
84
+ const anchor = this.editor.root?.querySelector(`sup.${DOC_CLASS.ENDNOTE}[${NOTE_ATTR}="${en.id}"]`);
85
+ this.prompt.openAt(anchor, {
86
+ title: '尾注内容',
87
+ placeholder: '留空提交 = 删除该尾注',
88
+ maxlength: 500,
89
+ value: en.text,
90
+ submit: (text) => {
91
+ if (text.trim() === '') {
92
+ this.state.endnotes = this.state.endnotes.filter((n) => n.id !== en.id);
93
+ this.run('removeNote', en.id);
94
+ } else {
95
+ this.state.endnotes = this.state.endnotes.map((n) => (n.id === en.id ? { ...n, text } : n));
96
+ }
97
+ this.onDocUpdate();
98
+ return true;
99
+ },
100
+ });
101
+ }
102
+
103
+ function rollbackPendingNote() {
104
+ if (!this._pendingNoteId) return;
105
+ const id = this._pendingNoteId;
106
+ this._pendingNoteId = '';
107
+ this.run('removeNote', id);
108
+ }
109
+
110
+ export const notesMethods = { refreshFootnotePages, renumberFootnotes, insertFootnote, editFootnote, editEndnote, rollbackPendingNote };