@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,112 +1,112 @@
1
- // [文件] gbdocx·word·comments —— 批注 —— 插入/定位/开关/删除与悬停预览
2
-
3
- // [设计] 方法以独立函数导出,由 word-app.js 末尾 Object.assign 装配到 WordApp.prototype:
4
- // 仍是同一个 this(方法调用时绑定),调用方形状与拆分前完全一致。
5
-
6
- import { genId } from 'guabiutils';
7
- import { commentTime } from '../../kernel/docx/comments.js';
8
- import { DOC_CLASS, COMMENT_ATTR } from '../../kernel/editor/engine/schema.js';
9
- import { notify } from '../../kernel/notify.js';
10
- import { openPopup } from '../../ui/popup.js';
11
- function insertComment() {
12
- if (this.editMode === 'readonly') return;
13
- if (!this.editor?.state().hasSelection) return notify.warning('请先选择要批注的文字');
14
- if (this._pendingCommentKey) {
15
- this.run('removeComment', this._pendingCommentKey);
16
- this._pendingCommentKey = '';
17
- }
18
- const key = genId('c');
19
- if (!this.run('setComment', key)) return notify.warning('请先选择要批注的文字');
20
- this._pendingCommentKey = key;
21
- const anchorEl = this.editor.root.querySelector(`.${DOC_CLASS.COMMENT}[${COMMENT_ATTR}="${key}"]`);
22
- this.prompt.openAt(anchorEl, {
23
- title: '批注内容',
24
- placeholder: '写下你的意见…',
25
- maxlength: 300,
26
- submit: (text) => {
27
- this._pendingCommentKey = '';
28
- this.comments = [...this.comments, { key, text, author: '我', date: new Date().toISOString().slice(0, 19), resolved: false }];
29
- this.activeCommentKey = key;
30
- this.setView('commentsPanel', true);
31
- this.renderPanels();
32
- this.onDocUpdate();
33
- return true;
34
- },
35
- });
36
- }
37
-
38
- function onPromptClosed() {
39
- const key = this._pendingCommentKey;
40
- this._pendingCommentKey = '';
41
- if (key) this.run('removeComment', key);
42
- this.rollbackPendingNote();
43
- }
44
-
45
- function locateComment(key) {
46
- this.activeCommentKey = key;
47
- if (this.commentsPanel) {
48
- this.commentsPanel.activeKey = key;
49
- this.commentsPanel.render();
50
- }
51
- this.editor.root.querySelector(`.${DOC_CLASS.COMMENT}[${COMMENT_ATTR}="${key}"]`)?.scrollIntoView({ block: 'center', behavior: 'smooth' });
52
- }
53
-
54
- function toggleComment(key) {
55
- this.comments = this.comments.map((c) => (c.key === key ? { ...c, resolved: !c.resolved } : c));
56
- this.renderPanels();
57
- this.onDocUpdate();
58
- }
59
-
60
- function removeComment(key) {
61
- this.run('removeComment', key);
62
- this.comments = this.comments.filter((c) => c.key !== key);
63
- if (this.activeCommentKey === key) this.activeCommentKey = '';
64
- this.renderPanels();
65
- this.onDocUpdate();
66
- }
67
-
68
- function onAnchorOver(e) {
69
- const target = e.target?.closest?.(`.${DOC_CLASS.COMMENT}`);
70
- if (!target) return;
71
- const key = target.getAttribute(COMMENT_ATTR);
72
- if (key === this._peekKey && this._peekHandle) return;
73
- const item = this.comments.find((c) => c.key === key);
74
- if (!item) return;
75
- clearTimeout(this._peekTimer);
76
- this.closePeek();
77
- const card = document.createElement('div');
78
- card.className = 'wd-cmt-peek';
79
- card.innerHTML = `<div class="wd-cmt-peek-meta"></div><div class="wd-cmt-peek-text"></div>`;
80
- card.querySelector('.wd-cmt-peek-meta').textContent = `${item.author || '匿名'} · ${commentTime(item.date)}${item.resolved ? ' · 已解决' : ''}`;
81
- card.querySelector('.wd-cmt-peek-text').textContent = item.text;
82
- card.addEventListener('mouseenter', () => clearTimeout(this._peekTimer));
83
- card.addEventListener('mouseleave', () => this.scheduleClosePeek());
84
- this._peekHandle = openPopup({ anchor: target.getBoundingClientRect(), content: card, placement: 'top-start', className: 'gbd-popup--peek' });
85
- this._peekKey = key;
86
- }
87
-
88
- function onAnchorOut(e) {
89
- if (e.relatedTarget?.closest?.(`.${DOC_CLASS.COMMENT}`)) return;
90
- this.scheduleClosePeek();
91
- }
92
-
93
- function scheduleClosePeek() {
94
- clearTimeout(this._peekTimer);
95
- this._peekTimer = setTimeout(() => this.closePeek(), 200);
96
- }
97
-
98
- function closePeek() {
99
- this._peekHandle?.close();
100
- this._peekHandle = null;
101
- this._peekKey = '';
102
- }
103
-
104
- function onAnchorClick(e) {
105
- const key = e.target?.closest?.(`.${DOC_CLASS.COMMENT}`)?.getAttribute(COMMENT_ATTR);
106
- if (!key) return;
107
- if (window.getSelection() && !window.getSelection().isCollapsed) return;
108
- this.setView('commentsPanel', true);
109
- this.locateComment(key);
110
- }
111
-
112
- export const commentsMethods = { insertComment, onPromptClosed, locateComment, toggleComment, removeComment, onAnchorOver, onAnchorOut, scheduleClosePeek, closePeek, onAnchorClick };
1
+ // [文件] gbdocx·word·comments —— 批注 —— 插入/定位/开关/删除与悬停预览
2
+
3
+ // [设计] 方法以独立函数导出,由 word-app.js 末尾 Object.assign 装配到 WordApp.prototype:
4
+ // 仍是同一个 this(方法调用时绑定),调用方形状与拆分前完全一致。
5
+
6
+ import { genId } from 'guabiutils';
7
+ import { commentTime } from '../../kernel/docx/comments.js';
8
+ import { DOC_CLASS, COMMENT_ATTR } from '../../kernel/editor/engine/schema.js';
9
+ import { notify } from '../../kernel/notify.js';
10
+ import { openPopup } from '../../ui/popup.js';
11
+ function insertComment() {
12
+ if (this.editMode === 'readonly') return;
13
+ if (!this.editor?.state().hasSelection) return notify.warning('请先选择要批注的文字');
14
+ if (this._pendingCommentKey) {
15
+ this.run('removeComment', this._pendingCommentKey);
16
+ this._pendingCommentKey = '';
17
+ }
18
+ const key = genId('c');
19
+ if (!this.run('setComment', key)) return notify.warning('请先选择要批注的文字');
20
+ this._pendingCommentKey = key;
21
+ const anchorEl = this.editor.root.querySelector(`.${DOC_CLASS.COMMENT}[${COMMENT_ATTR}="${key}"]`);
22
+ this.prompt.openAt(anchorEl, {
23
+ title: '批注内容',
24
+ placeholder: '写下你的意见…',
25
+ maxlength: 300,
26
+ submit: (text) => {
27
+ this._pendingCommentKey = '';
28
+ this.comments = [...this.comments, { key, text, author: '我', date: new Date().toISOString().slice(0, 19), resolved: false }];
29
+ this.activeCommentKey = key;
30
+ this.setView('commentsPanel', true);
31
+ this.renderPanels();
32
+ this.onDocUpdate();
33
+ return true;
34
+ },
35
+ });
36
+ }
37
+
38
+ function onPromptClosed() {
39
+ const key = this._pendingCommentKey;
40
+ this._pendingCommentKey = '';
41
+ if (key) this.run('removeComment', key);
42
+ this.rollbackPendingNote();
43
+ }
44
+
45
+ function locateComment(key) {
46
+ this.activeCommentKey = key;
47
+ if (this.commentsPanel) {
48
+ this.commentsPanel.activeKey = key;
49
+ this.commentsPanel.render();
50
+ }
51
+ this.editor.root.querySelector(`.${DOC_CLASS.COMMENT}[${COMMENT_ATTR}="${key}"]`)?.scrollIntoView({ block: 'center', behavior: 'smooth' });
52
+ }
53
+
54
+ function toggleComment(key) {
55
+ this.comments = this.comments.map((c) => (c.key === key ? { ...c, resolved: !c.resolved } : c));
56
+ this.renderPanels();
57
+ this.onDocUpdate();
58
+ }
59
+
60
+ function removeComment(key) {
61
+ this.run('removeComment', key);
62
+ this.comments = this.comments.filter((c) => c.key !== key);
63
+ if (this.activeCommentKey === key) this.activeCommentKey = '';
64
+ this.renderPanels();
65
+ this.onDocUpdate();
66
+ }
67
+
68
+ function onAnchorOver(e) {
69
+ const target = e.target?.closest?.(`.${DOC_CLASS.COMMENT}`);
70
+ if (!target) return;
71
+ const key = target.getAttribute(COMMENT_ATTR);
72
+ if (key === this._peekKey && this._peekHandle) return;
73
+ const item = this.comments.find((c) => c.key === key);
74
+ if (!item) return;
75
+ clearTimeout(this._peekTimer);
76
+ this.closePeek();
77
+ const card = document.createElement('div');
78
+ card.className = 'wd-cmt-peek';
79
+ card.innerHTML = `<div class="wd-cmt-peek-meta"></div><div class="wd-cmt-peek-text"></div>`;
80
+ card.querySelector('.wd-cmt-peek-meta').textContent = `${item.author || '匿名'} · ${commentTime(item.date)}${item.resolved ? ' · 已解决' : ''}`;
81
+ card.querySelector('.wd-cmt-peek-text').textContent = item.text;
82
+ card.addEventListener('mouseenter', () => clearTimeout(this._peekTimer));
83
+ card.addEventListener('mouseleave', () => this.scheduleClosePeek());
84
+ this._peekHandle = openPopup({ anchor: target.getBoundingClientRect(), content: card, placement: 'top-start', className: 'gbd-popup--peek' });
85
+ this._peekKey = key;
86
+ }
87
+
88
+ function onAnchorOut(e) {
89
+ if (e.relatedTarget?.closest?.(`.${DOC_CLASS.COMMENT}`)) return;
90
+ this.scheduleClosePeek();
91
+ }
92
+
93
+ function scheduleClosePeek() {
94
+ clearTimeout(this._peekTimer);
95
+ this._peekTimer = setTimeout(() => this.closePeek(), 200);
96
+ }
97
+
98
+ function closePeek() {
99
+ this._peekHandle?.close();
100
+ this._peekHandle = null;
101
+ this._peekKey = '';
102
+ }
103
+
104
+ function onAnchorClick(e) {
105
+ const key = e.target?.closest?.(`.${DOC_CLASS.COMMENT}`)?.getAttribute(COMMENT_ATTR);
106
+ if (!key) return;
107
+ if (window.getSelection() && !window.getSelection().isCollapsed) return;
108
+ this.setView('commentsPanel', true);
109
+ this.locateComment(key);
110
+ }
111
+
112
+ export const commentsMethods = { insertComment, onPromptClosed, locateComment, toggleComment, removeComment, onAnchorOver, onAnchorOut, scheduleClosePeek, closePeek, onAnchorClick };
@@ -1,113 +1,113 @@
1
- // [文件] gbdocx·word·doc —— 文档状态 —— 应用/读取当前文档、脏标记与自动保存
2
-
3
- // [设计] 方法以独立函数导出,由 word-app.js 末尾 Object.assign 装配到 WordApp.prototype:
4
- // 仍是同一个 this(方法调用时绑定),调用方形状与拆分前完全一致。
5
-
6
- import { cleanHfHtml } from '../../kernel/docx/codec/helpers.js';
7
- import { resetPagination } from '../../kernel/docx/pagination.js';
8
- import { HF_MODES, defaultPageConf, defaultPageNumbers, defaultWatermark } from '../../kernel/docx/constants.js';
9
- import { DOC_CLASS } from '../../kernel/editor/engine/schema.js';
10
- import { countRevisions } from '../../kernel/editor/engine/revision.js';
11
- function onDocUpdate() {
12
- this.setDirty(true);
13
- // 嵌入模式:自动保存归宿主(宿主自己排期与写盘),包内不按文件句柄自动保存
14
- if (!this.embed && this.prefs.autosave) this.scheduleAutosave();
15
- const root = this.editor?.root;
16
- this.statusBar.setWords((root?.textContent || '').replace(/\s+/g, '').length);
17
- this.suggestionCount = countRevisions(root);
18
- this.statusBar.setSuggestions(this.suggestionCount);
19
- if (this.flags.catalog) this.refreshCatalog();
20
- this.schedulePaginate();
21
- }
22
-
23
- function setDirty(on) {
24
- this.dirty = on;
25
- if (this.dirtyEl) this.dirtyEl.hidden = !on;
26
- this.statusBar?.setDirty(on, this.saving);
27
- // 嵌入模式:脏态回调宿主(页签脏标与自动保存排期由宿主决定)
28
- this.onDirty?.(on);
29
- }
30
-
31
- function scheduleAutosave() {
32
- clearTimeout(this._autoTimer);
33
- this._autoTimer = setTimeout(() => {
34
- if (this.dirty && this.doc.handle) this.save(true);
35
- }, 2000);
36
- }
37
-
38
- function applyDoc(model) {
39
- const s = this.state;
40
- const list = Array.isArray(model?.sections) ? model.sections : [];
41
- s.sections = list;
42
- const base = list[list.length - 1] || {};
43
- s.page = { ...defaultPageConf(), ...(base.page || {}) };
44
- s.hfMode = base.hfMode || HF_MODES[0].value;
45
- s.header = cleanHfHtml(base.header || '');
46
- s.footer = cleanHfHtml(base.footer || '');
47
- s.headerEven = cleanHfHtml(base.headerEven || '');
48
- s.footerEven = cleanHfHtml(base.footerEven || '');
49
- s.headerFirst = cleanHfHtml(base.headerFirst || '');
50
- s.footerFirst = cleanHfHtml(base.footerFirst || '');
51
- s.pageNumbers = { ...defaultPageNumbers(), ...(base.pageNumbers || {}) };
52
- s.watermark = { ...defaultWatermark(), ...(base.watermark || {}) };
53
- s.footnotes = model?.footnotes || [];
54
- s.endnotes = model?.endnotes || [];
55
- s.props = model?.props || null;
56
- s.defaultTabStop = model?.defaultTabStop > 0 ? model.defaultTabStop : 720;
57
- // 标点挤压是排版设置:存回去时得带着,否则保存一次版式就回退(每行少一个字、整篇多一页)
58
- s.compressPunctuation = model?.compressPunctuation === true;
59
- s.carry = model?.carry || [];
60
- s.losses = model?.losses || [];
61
- this.lossAck = false;
62
- this.lossAutoWarned = false;
63
- this.comments = model?.comments || [];
64
- this.activeCommentKey = '';
65
- this.surface.setHTML(model?.html?.trim() ? model.html : '<p><br></p>');
66
- this.renumberFootnotes();
67
- const ed = this.editor;
68
- if (ed?.root) ed.quietly(() => resetPagination(ed.root));
69
- this.setEditMode(this.editMode);
70
- this.setDirty(false);
71
- this.refreshStats();
72
- this.paginate();
73
- this.renderPanels();
74
- this.renderHead();
75
- }
76
-
77
- function currentDoc() {
78
- const s = this.state;
79
- const base = {
80
- type: s.sections[s.sections.length - 1]?.type || '',
81
- page: s.page,
82
- header: s.header,
83
- footer: s.footer,
84
- headerEven: s.headerEven,
85
- footerEven: s.footerEven,
86
- headerFirst: s.headerFirst,
87
- footerFirst: s.footerFirst,
88
- hfMode: s.hfMode,
89
- pageNumbers: s.pageNumbers,
90
- watermark: s.watermark,
91
- };
92
- const list = s.sections.slice(0, -1);
93
- list.push(base);
94
- return {
95
- html: this.surface.flush() ?? '',
96
- sections: list,
97
- comments: this.comments,
98
- footnotes: s.footnotes,
99
- endnotes: s.endnotes,
100
- props: s.props,
101
- defaultTabStop: s.defaultTabStop,
102
- compressPunctuation: s.compressPunctuation === true,
103
- carry: s.carry,
104
- };
105
- }
106
-
107
- function hasContent(raw) {
108
- const probe = document.createElement('div');
109
- probe.innerHTML = raw || '';
110
- return Boolean(probe.textContent.trim() || probe.querySelector(`img,table,hr,.${DOC_CLASS.PAGEBREAK}`));
111
- }
112
-
113
- export const docMethods = { onDocUpdate, setDirty, scheduleAutosave, applyDoc, currentDoc, hasContent };
1
+ // [文件] gbdocx·word·doc —— 文档状态 —— 应用/读取当前文档、脏标记与自动保存
2
+
3
+ // [设计] 方法以独立函数导出,由 word-app.js 末尾 Object.assign 装配到 WordApp.prototype:
4
+ // 仍是同一个 this(方法调用时绑定),调用方形状与拆分前完全一致。
5
+
6
+ import { cleanHfHtml } from '../../kernel/docx/codec/helpers.js';
7
+ import { resetPagination } from '../../kernel/docx/pagination.js';
8
+ import { HF_MODES, defaultPageConf, defaultPageNumbers, defaultWatermark } from '../../kernel/docx/constants.js';
9
+ import { DOC_CLASS } from '../../kernel/editor/engine/schema.js';
10
+ import { countRevisions } from '../../kernel/editor/engine/revision.js';
11
+ function onDocUpdate() {
12
+ this.setDirty(true);
13
+ // 嵌入模式:自动保存归宿主(宿主自己排期与写盘),包内不按文件句柄自动保存
14
+ if (!this.embed && this.prefs.autosave) this.scheduleAutosave();
15
+ const root = this.editor?.root;
16
+ this.statusBar.setWords((root?.textContent || '').replace(/\s+/g, '').length);
17
+ this.suggestionCount = countRevisions(root);
18
+ this.statusBar.setSuggestions(this.suggestionCount);
19
+ if (this.flags.catalog) this.refreshCatalog();
20
+ this.schedulePaginate();
21
+ }
22
+
23
+ function setDirty(on) {
24
+ this.dirty = on;
25
+ if (this.dirtyEl) this.dirtyEl.hidden = !on;
26
+ this.statusBar?.setDirty(on, this.saving);
27
+ // 嵌入模式:脏态回调宿主(页签脏标与自动保存排期由宿主决定)
28
+ this.onDirty?.(on);
29
+ }
30
+
31
+ function scheduleAutosave() {
32
+ clearTimeout(this._autoTimer);
33
+ this._autoTimer = setTimeout(() => {
34
+ if (this.dirty && this.doc.handle) this.save(true);
35
+ }, 2000);
36
+ }
37
+
38
+ function applyDoc(model) {
39
+ const s = this.state;
40
+ const list = Array.isArray(model?.sections) ? model.sections : [];
41
+ s.sections = list;
42
+ const base = list[list.length - 1] || {};
43
+ s.page = { ...defaultPageConf(), ...(base.page || {}) };
44
+ s.hfMode = base.hfMode || HF_MODES[0].value;
45
+ s.header = cleanHfHtml(base.header || '');
46
+ s.footer = cleanHfHtml(base.footer || '');
47
+ s.headerEven = cleanHfHtml(base.headerEven || '');
48
+ s.footerEven = cleanHfHtml(base.footerEven || '');
49
+ s.headerFirst = cleanHfHtml(base.headerFirst || '');
50
+ s.footerFirst = cleanHfHtml(base.footerFirst || '');
51
+ s.pageNumbers = { ...defaultPageNumbers(), ...(base.pageNumbers || {}) };
52
+ s.watermark = { ...defaultWatermark(), ...(base.watermark || {}) };
53
+ s.footnotes = model?.footnotes || [];
54
+ s.endnotes = model?.endnotes || [];
55
+ s.props = model?.props || null;
56
+ s.defaultTabStop = model?.defaultTabStop > 0 ? model.defaultTabStop : 720;
57
+ // 标点挤压是排版设置:存回去时得带着,否则保存一次版式就回退(每行少一个字、整篇多一页)
58
+ s.compressPunctuation = model?.compressPunctuation === true;
59
+ s.carry = model?.carry || [];
60
+ s.losses = model?.losses || [];
61
+ this.lossAck = false;
62
+ this.lossAutoWarned = false;
63
+ this.comments = model?.comments || [];
64
+ this.activeCommentKey = '';
65
+ this.surface.setHTML(model?.html?.trim() ? model.html : '<p><br></p>');
66
+ this.renumberFootnotes();
67
+ const ed = this.editor;
68
+ if (ed?.root) ed.quietly(() => resetPagination(ed.root));
69
+ this.setEditMode(this.editMode);
70
+ this.setDirty(false);
71
+ this.refreshStats();
72
+ this.paginate();
73
+ this.renderPanels();
74
+ this.renderHead();
75
+ }
76
+
77
+ function currentDoc() {
78
+ const s = this.state;
79
+ const base = {
80
+ type: s.sections[s.sections.length - 1]?.type || '',
81
+ page: s.page,
82
+ header: s.header,
83
+ footer: s.footer,
84
+ headerEven: s.headerEven,
85
+ footerEven: s.footerEven,
86
+ headerFirst: s.headerFirst,
87
+ footerFirst: s.footerFirst,
88
+ hfMode: s.hfMode,
89
+ pageNumbers: s.pageNumbers,
90
+ watermark: s.watermark,
91
+ };
92
+ const list = s.sections.slice(0, -1);
93
+ list.push(base);
94
+ return {
95
+ html: this.surface.flush() ?? '',
96
+ sections: list,
97
+ comments: this.comments,
98
+ footnotes: s.footnotes,
99
+ endnotes: s.endnotes,
100
+ props: s.props,
101
+ defaultTabStop: s.defaultTabStop,
102
+ compressPunctuation: s.compressPunctuation === true,
103
+ carry: s.carry,
104
+ };
105
+ }
106
+
107
+ function hasContent(raw) {
108
+ const probe = document.createElement('div');
109
+ probe.innerHTML = raw || '';
110
+ return Boolean(probe.textContent.trim() || probe.querySelector(`img,table,hr,.${DOC_CLASS.PAGEBREAK}`));
111
+ }
112
+
113
+ export const docMethods = { onDocUpdate, setDirty, scheduleAutosave, applyDoc, currentDoc, hasContent };
@@ -1,72 +1,72 @@
1
- // [文件] gbdocx·word·edit —— 编辑动作 —— 命令执行、编辑模式、统计、图片/链接/表格插入
2
-
3
- // [设计] 方法以独立函数导出,由 word-app.js 末尾 Object.assign 装配到 WordApp.prototype:
4
- // 仍是同一个 this(方法调用时绑定),调用方形状与拆分前完全一致。
5
-
6
- import { countRevisions } from '../../kernel/editor/engine/revision.js';
7
- import { notify } from '../../kernel/notify.js';
8
- import { openMenu } from '../../ui/context-menu.js';
9
- function run(name, ...args) {
10
- const ed = this.editor;
11
- if (!ed || ed.destroyed || this.editMode === 'readonly') return false;
12
- ed.focus();
13
- return ed.exec(name, ...args);
14
- }
15
-
16
- function setEditMode(mode) {
17
- this.editMode = mode;
18
- this.surface.setEditable(mode !== 'readonly');
19
- this.surface.setSuggesting(mode === 'suggest');
20
- this.statusBar.setMode(mode);
21
- this.ribbon?.refresh();
22
- }
23
-
24
- function refreshStats() {
25
- const root = this.editor?.root;
26
- this.statusBar.setWords((root?.textContent || '').replace(/\s+/g, '').length);
27
- this.suggestionCount = countRevisions(root);
28
- this.statusBar.setSuggestions(this.suggestionCount);
29
- if (this.flags.catalog) this.refreshCatalog();
30
- }
31
-
32
- function pickImage() {
33
- if (this.editMode === 'readonly') return;
34
- this.imageInput.click();
35
- }
36
-
37
- function onImagePicked() {
38
- const files = Array.from(this.imageInput.files || []);
39
- this.imageInput.value = '';
40
- if (!files.length) return;
41
- this.editor?.focus();
42
- this.surface.insertImages(files);
43
- }
44
-
45
- function insertLink() {
46
- if (this.editMode === 'readonly') return;
47
- if (!this.editorState.hasSelection) return notify.warning('请先选择要加链接的文字');
48
- this.editor.focus();
49
- this.prompt.openAt(this.titleEl, {
50
- title: '链接地址',
51
- placeholder: 'https://…(留空取消链接)',
52
- maxlength: 500,
53
- required: false,
54
- submit: (value) => {
55
- if (this.run('setLink', value)) return true;
56
- notify.warning('链接地址仅支持 http/https/mailto/tel/ftp 协议');
57
- return false;
58
- },
59
- });
60
- }
61
-
62
- function openTableSpec(e) {
63
- if (this.editMode === 'readonly') return;
64
- const anchor = e?.currentTarget || e?.target || this.headEl;
65
- openMenu({
66
- anchor,
67
- items: [2, 3, 4, 5].map((n) => ({ label: `${n} × ${n} 表格`, onClick: () => this.run('insertTable', { rows: n, cols: n, withHeaderRow: true }) })),
68
- placement: 'bottom-start',
69
- });
70
- }
71
-
72
- export const editMethods = { run, setEditMode, refreshStats, pickImage, onImagePicked, insertLink, openTableSpec };
1
+ // [文件] gbdocx·word·edit —— 编辑动作 —— 命令执行、编辑模式、统计、图片/链接/表格插入
2
+
3
+ // [设计] 方法以独立函数导出,由 word-app.js 末尾 Object.assign 装配到 WordApp.prototype:
4
+ // 仍是同一个 this(方法调用时绑定),调用方形状与拆分前完全一致。
5
+
6
+ import { countRevisions } from '../../kernel/editor/engine/revision.js';
7
+ import { notify } from '../../kernel/notify.js';
8
+ import { openMenu } from '../../ui/context-menu.js';
9
+ function run(name, ...args) {
10
+ const ed = this.editor;
11
+ if (!ed || ed.destroyed || this.editMode === 'readonly') return false;
12
+ ed.focus();
13
+ return ed.exec(name, ...args);
14
+ }
15
+
16
+ function setEditMode(mode) {
17
+ this.editMode = mode;
18
+ this.surface.setEditable(mode !== 'readonly');
19
+ this.surface.setSuggesting(mode === 'suggest');
20
+ this.statusBar.setMode(mode);
21
+ this.ribbon?.refresh();
22
+ }
23
+
24
+ function refreshStats() {
25
+ const root = this.editor?.root;
26
+ this.statusBar.setWords((root?.textContent || '').replace(/\s+/g, '').length);
27
+ this.suggestionCount = countRevisions(root);
28
+ this.statusBar.setSuggestions(this.suggestionCount);
29
+ if (this.flags.catalog) this.refreshCatalog();
30
+ }
31
+
32
+ function pickImage() {
33
+ if (this.editMode === 'readonly') return;
34
+ this.imageInput.click();
35
+ }
36
+
37
+ function onImagePicked() {
38
+ const files = Array.from(this.imageInput.files || []);
39
+ this.imageInput.value = '';
40
+ if (!files.length) return;
41
+ this.editor?.focus();
42
+ this.surface.insertImages(files);
43
+ }
44
+
45
+ function insertLink() {
46
+ if (this.editMode === 'readonly') return;
47
+ if (!this.editorState.hasSelection) return notify.warning('请先选择要加链接的文字');
48
+ this.editor.focus();
49
+ this.prompt.openAt(this.titleEl, {
50
+ title: '链接地址',
51
+ placeholder: 'https://…(留空取消链接)',
52
+ maxlength: 500,
53
+ required: false,
54
+ submit: (value) => {
55
+ if (this.run('setLink', value)) return true;
56
+ notify.warning('链接地址仅支持 http/https/mailto/tel/ftp 协议');
57
+ return false;
58
+ },
59
+ });
60
+ }
61
+
62
+ function openTableSpec(e) {
63
+ if (this.editMode === 'readonly') return;
64
+ const anchor = e?.currentTarget || e?.target || this.headEl;
65
+ openMenu({
66
+ anchor,
67
+ items: [2, 3, 4, 5].map((n) => ({ label: `${n} × ${n} 表格`, onClick: () => this.run('insertTable', { rows: n, cols: n, withHeaderRow: true }) })),
68
+ placement: 'bottom-start',
69
+ });
70
+ }
71
+
72
+ export const editMethods = { run, setEditMode, refreshStats, pickImage, onImagePicked, insertLink, openTableSpec };