@a200888213/gbdocx 0.6.1 → 0.6.2

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 (74) hide show
  1. package/package.json +1 -1
  2. package/src/app/file-io.js +122 -122
  3. package/src/app/main.js +9 -9
  4. package/src/app/word/catalog.js +52 -52
  5. package/src/app/word/comments.js +112 -112
  6. package/src/app/word/doc.js +113 -113
  7. package/src/app/word/edit.js +72 -72
  8. package/src/app/word/hf.js +76 -76
  9. package/src/app/word/layout.js +155 -155
  10. package/src/app/word/notes.js +110 -110
  11. package/src/app/word/shared.js +22 -22
  12. package/src/app/word-sheets.js +101 -101
  13. package/src/kernel/docx/codec/blocks.js +123 -123
  14. package/src/kernel/docx/codec/carry.js +129 -129
  15. package/src/kernel/docx/codec/comments.js +25 -25
  16. package/src/kernel/docx/codec/docx.js +198 -198
  17. package/src/kernel/docx/codec/docxStyles.js +177 -177
  18. package/src/kernel/docx/codec/fonts.js +195 -195
  19. package/src/kernel/docx/codec/helpers.js +161 -161
  20. package/src/kernel/docx/codec/hfHtml.js +152 -152
  21. package/src/kernel/docx/codec/html.js +739 -739
  22. package/src/kernel/docx/codec/losses.js +420 -420
  23. package/src/kernel/docx/codec/markdown.js +170 -170
  24. package/src/kernel/docx/codec/parts.js +249 -249
  25. package/src/kernel/docx/codec/read.js +603 -603
  26. package/src/kernel/docx/codec/render.js +1156 -1156
  27. package/src/kernel/docx/codec/roundtrip.js +236 -236
  28. package/src/kernel/docx/codec/write.js +833 -833
  29. package/src/kernel/docx/comments.js +14 -14
  30. package/src/kernel/docx/pageGeometry.js +123 -123
  31. package/src/kernel/docx/pagination.js +481 -481
  32. package/src/kernel/docx/watermark.js +45 -45
  33. package/src/kernel/editor/constants.js +8 -8
  34. package/src/kernel/editor/engine/blocks.js +594 -594
  35. package/src/kernel/editor/engine/blocksEnter.js +109 -109
  36. package/src/kernel/editor/engine/blocksInsert.js +254 -254
  37. package/src/kernel/editor/engine/blocksStructure.js +226 -226
  38. package/src/kernel/editor/engine/commands.js +181 -181
  39. package/src/kernel/editor/engine/dom.js +646 -646
  40. package/src/kernel/editor/engine/history.js +149 -149
  41. package/src/kernel/editor/engine/inline.js +656 -656
  42. package/src/kernel/editor/engine/input.js +554 -554
  43. package/src/kernel/editor/engine/interaction.js +137 -137
  44. package/src/kernel/editor/engine/pointer.js +39 -39
  45. package/src/kernel/editor/engine/revision.js +138 -138
  46. package/src/kernel/editor/engine/schema.js +417 -417
  47. package/src/kernel/editor/engine/selection.js +119 -119
  48. package/src/kernel/editor/engine/state.js +118 -118
  49. package/src/kernel/editor/engine/tableClip.js +154 -154
  50. package/src/kernel/editor/engine/tableGrid.js +55 -55
  51. package/src/kernel/editor/engine/tableInteraction.js +388 -388
  52. package/src/kernel/editor/engine/tableMenu.js +250 -250
  53. package/src/kernel/editor/engine/tableOps.js +174 -174
  54. package/src/kernel/editor/engine/tables.js +732 -732
  55. package/src/kernel/editor/overlayGeometry.js +20 -20
  56. package/src/kernel/editor/utils.js +48 -48
  57. package/src/kernel/keyboard.js +114 -114
  58. package/src/kernel/textStyles.js +84 -84
  59. package/src/kernel/util/imageFile.js +91 -91
  60. package/src/kernel/util/shortcutKey.js +33 -33
  61. package/src/surface/editor-surface.js +600 -600
  62. package/src/ui/color-panel.js +69 -69
  63. package/src/ui/context-menu.js +133 -133
  64. package/src/ui/find-bar.js +151 -151
  65. package/src/ui/menubar.js +28 -28
  66. package/src/ui/panels.js +115 -115
  67. package/src/ui/prompt.js +89 -89
  68. package/src/ui/ribbon.js +485 -485
  69. package/src/ui/special-chars.js +39 -39
  70. package/src/ui/statusbar.js +81 -81
  71. package/src/ui/toolbar.js +32 -32
  72. package/styles/editor-content.css +440 -440
  73. package/styles/tokens.css +154 -154
  74. package/styles/word-panel.css +1 -1
@@ -1,155 +1,155 @@
1
- // [文件] gbdocx·word·layout —— 页面几何与分页 —— 缩放、纸张层、页眉页脚带测量、滚动读数
2
-
3
- // [设计] 方法以独立函数导出,由 word-app.js 末尾 Object.assign 装配到 WordApp.prototype:
4
- // 仍是同一个 this(方法调用时绑定),调用方形状与拆分前完全一致。
5
-
6
- import { pageGeometry, layoutOf, GUTTER, SHEET_TOP_GAP, fromSheetTop, pageNumberStyle, bandStyle, sheetStyle } from '../../kernel/docx/pageGeometry.js';
7
- import { repaginate } from '../../kernel/docx/pagination.js';
8
- import { savePrefs } from './shared.js';
9
- function geo() { return pageGeometry(this.state.page); }
10
-
11
- function layout() { return layoutOf(this.geo()); }
12
-
13
- function sheetsCtx() {
14
- return {
15
- geo: () => this.geo(),
16
- layout: () => this.layout(),
17
- pageCount: () => this.pageCount,
18
- pageNumbers: () => this.state.pageNumbers,
19
- watermark: () => this.state.watermark,
20
- endnotes: () => this.state.endnotes,
21
- fnHeights: () => this.fnHeights,
22
- bandEdit: () => this.bandEditSlot,
23
- pageNumberText: (p) => this.pageNumberText(p),
24
- fnPageOf: (p) => this.footnotePages.get(p) || [],
25
- sheetStyleOf: (p) => sheetStyle(p, this.layout()),
26
- hfBandStyle: (p, which) => bandStyle(p, which, this.geo(), this.layout(), { head: 0, foot: 0 }),
27
- pageNumStyle: (p) => pageNumberStyle(p, this.geo(), this.layout(), this.state.pageNumbers.align),
28
- fnBandStyle: (p) => {
29
- const L = this.layout();
30
- const h = this.fnHeights[p - 1] || 0;
31
- return { top: fromSheetTop(p, L.padTop + L.availOf() - h, L), left: `${L.padLeft}px`, right: `${L.padRight}px` };
32
- },
33
- enBandStyle: () => {
34
- const L = this.layout();
35
- const p = this.pageCount;
36
- const h = this.fnHeights[p - 1] || 0;
37
- return { top: fromSheetTop(p, L.padTop + L.availOf() - h, L), left: `${L.padLeft}px`, right: `${L.padRight}px`, transform: 'translateY(-100%)' };
38
- },
39
- bandSlot: (which, p) => this.bandSlot(which, p),
40
- hfDisplayHtml: (which, p) => this.hfDisplayHtml(which, p),
41
- showHf: (p, which) => this.showHf(p, which),
42
- showPageNumber: (p) => this.showPageNumber(p),
43
- footerHasPageField: (p) => /data-fld="PAGE"/i.test(this.hfTextAt('footer', p)),
44
- onBandDblClick: (slot, el) => this.startEditBand(slot, el),
45
- onBandCommit: () => this.commitBand(),
46
- onFootnoteDblClick: (fn) => this.editFootnote(fn),
47
- onEndnoteDblClick: (en) => this.editEndnote(en),
48
- zoom: () => this.zoom,
49
- };
50
- }
51
-
52
- function applyEditorGeometry() {
53
- const geo = this.geo();
54
- const layout = this.layout();
55
- this.editorWrap.style.zoom = String(this.zoom / 100);
56
- this.editorWrap.style.setProperty('--gbd-wd-sheet-top', `${SHEET_TOP_GAP}px`);
57
- const style = {
58
- '--gbd-re-content-w': `${geo.w}mm`,
59
- '--gbd-re-content-m': '0 auto',
60
- '--gbd-re-content-pad': `${layout.padTop}px ${layout.padRight}px ${layout.padBottom}px ${layout.padLeft}px`,
61
- '--gbd-re-content-min-h': `${this.pageCount * layout.pageH + Math.max(0, this.pageCount - 1) * GUTTER}px`,
62
- };
63
- for (const [k, v] of Object.entries(style)) this.editorWrap.style.setProperty(k, v);
64
- }
65
-
66
- function applyZoom() {
67
- this.statusBar.setZoom(this.zoom);
68
- this.applyEditorGeometry();
69
- this.schedulePaginate();
70
- }
71
-
72
- function setZoom(z) {
73
- this.zoom = Math.max(40, Math.min(300, Math.round(z)));
74
- this.prefs.zoom = this.zoom;
75
- savePrefs(this.prefs);
76
- this.applyZoom();
77
- }
78
-
79
- function paginate() {
80
- const ed = this.editor;
81
- const root = ed?.root;
82
- if (!root || ed.composing) return;
83
- const L = this.layout();
84
- const opts = {
85
- cellsPerPage: L.cols,
86
- gapOf: L.padTop + L.padBottom,
87
- gutter: GUTTER,
88
- padTop: L.padTop,
89
- colW: L.colW,
90
- colGap: L.colGap,
91
- colSeparator: this.geo().colSeparator,
92
- padLeft: L.padLeft,
93
- padRight: L.padRight,
94
- scale: this.zoom / 100,
95
- };
96
- ed.quietly(() => {
97
- this.pageCount = repaginate(root, { ...opts, cellH: L.availOf() });
98
- });
99
- this.refreshFootnotePages();
100
- this.renderBackdrop();
101
- if (this.state.footnotes.length === 0) {
102
- this.fnHeights = [];
103
- return;
104
- }
105
- requestAnimationFrame(() => {
106
- const hs = this.measureFnBands();
107
- this.fnHeights = hs;
108
- if (!hs.some((h) => h > 0)) {
109
- this.renderBackdrop();
110
- return;
111
- }
112
- ed.quietly(() => {
113
- this.pageCount = repaginate(root, { ...opts, cellH: (p) => Math.max(60, L.availOf() - (hs[p - 1] || 0)) });
114
- });
115
- this.refreshFootnotePages();
116
- this.renderBackdrop();
117
- });
118
- }
119
-
120
- function schedulePaginate() {
121
- clearTimeout(this.pageTimer);
122
- cancelAnimationFrame(this.pageRaf);
123
- this.pageTimer = setTimeout(() => {
124
- this.pageRaf = requestAnimationFrame(() => this.paginate());
125
- }, 120);
126
- }
127
-
128
- function renderBackdrop() {
129
- this.surface.refreshBackdrop();
130
- this.applyEditorGeometry();
131
- this.refreshScroll();
132
- this.statusBar.setPage(this.visiblePage, this.pageCount);
133
- }
134
-
135
- function measureFnBands() {
136
- const scale = this.zoom / 100 || 1;
137
- const out = [];
138
- for (const el of this.root.querySelectorAll('.word-sheet-fn[data-page]')) {
139
- const p = Number(el.dataset.page);
140
- if (p > 0) out[p - 1] = el.getBoundingClientRect().height / scale;
141
- }
142
- return out;
143
- }
144
-
145
- function refreshScroll() {
146
- const pageH = this.layout().pageH;
147
- if (!pageH) return;
148
- const step = pageH + GUTTER;
149
- // 第一页不从滚动原点起:扣掉纸层顶部留白再折算页序,否则页数会提前一页翻
150
- const offset = this.surface.scrollEl.scrollTop - SHEET_TOP_GAP;
151
- this.visiblePage = Math.max(1, Math.min(this.pageCount, Math.floor(Math.max(0, offset) / step) + 1));
152
- this.statusBar.setPage(this.visiblePage, this.pageCount);
153
- }
154
-
155
- export const layoutMethods = { geo, layout, sheetsCtx, applyEditorGeometry, applyZoom, setZoom, paginate, schedulePaginate, renderBackdrop, measureFnBands, refreshScroll };
1
+ // [文件] gbdocx·word·layout —— 页面几何与分页 —— 缩放、纸张层、页眉页脚带测量、滚动读数
2
+
3
+ // [设计] 方法以独立函数导出,由 word-app.js 末尾 Object.assign 装配到 WordApp.prototype:
4
+ // 仍是同一个 this(方法调用时绑定),调用方形状与拆分前完全一致。
5
+
6
+ import { pageGeometry, layoutOf, GUTTER, SHEET_TOP_GAP, fromSheetTop, pageNumberStyle, bandStyle, sheetStyle } from '../../kernel/docx/pageGeometry.js';
7
+ import { repaginate } from '../../kernel/docx/pagination.js';
8
+ import { savePrefs } from './shared.js';
9
+ function geo() { return pageGeometry(this.state.page); }
10
+
11
+ function layout() { return layoutOf(this.geo()); }
12
+
13
+ function sheetsCtx() {
14
+ return {
15
+ geo: () => this.geo(),
16
+ layout: () => this.layout(),
17
+ pageCount: () => this.pageCount,
18
+ pageNumbers: () => this.state.pageNumbers,
19
+ watermark: () => this.state.watermark,
20
+ endnotes: () => this.state.endnotes,
21
+ fnHeights: () => this.fnHeights,
22
+ bandEdit: () => this.bandEditSlot,
23
+ pageNumberText: (p) => this.pageNumberText(p),
24
+ fnPageOf: (p) => this.footnotePages.get(p) || [],
25
+ sheetStyleOf: (p) => sheetStyle(p, this.layout()),
26
+ hfBandStyle: (p, which) => bandStyle(p, which, this.geo(), this.layout(), { head: 0, foot: 0 }),
27
+ pageNumStyle: (p) => pageNumberStyle(p, this.geo(), this.layout(), this.state.pageNumbers.align),
28
+ fnBandStyle: (p) => {
29
+ const L = this.layout();
30
+ const h = this.fnHeights[p - 1] || 0;
31
+ return { top: fromSheetTop(p, L.padTop + L.availOf() - h, L), left: `${L.padLeft}px`, right: `${L.padRight}px` };
32
+ },
33
+ enBandStyle: () => {
34
+ const L = this.layout();
35
+ const p = this.pageCount;
36
+ const h = this.fnHeights[p - 1] || 0;
37
+ return { top: fromSheetTop(p, L.padTop + L.availOf() - h, L), left: `${L.padLeft}px`, right: `${L.padRight}px`, transform: 'translateY(-100%)' };
38
+ },
39
+ bandSlot: (which, p) => this.bandSlot(which, p),
40
+ hfDisplayHtml: (which, p) => this.hfDisplayHtml(which, p),
41
+ showHf: (p, which) => this.showHf(p, which),
42
+ showPageNumber: (p) => this.showPageNumber(p),
43
+ footerHasPageField: (p) => /data-fld="PAGE"/i.test(this.hfTextAt('footer', p)),
44
+ onBandDblClick: (slot, el) => this.startEditBand(slot, el),
45
+ onBandCommit: () => this.commitBand(),
46
+ onFootnoteDblClick: (fn) => this.editFootnote(fn),
47
+ onEndnoteDblClick: (en) => this.editEndnote(en),
48
+ zoom: () => this.zoom,
49
+ };
50
+ }
51
+
52
+ function applyEditorGeometry() {
53
+ const geo = this.geo();
54
+ const layout = this.layout();
55
+ this.editorWrap.style.zoom = String(this.zoom / 100);
56
+ this.editorWrap.style.setProperty('--gbd-wd-sheet-top', `${SHEET_TOP_GAP}px`);
57
+ const style = {
58
+ '--gbd-re-content-w': `${geo.w}mm`,
59
+ '--gbd-re-content-m': '0 auto',
60
+ '--gbd-re-content-pad': `${layout.padTop}px ${layout.padRight}px ${layout.padBottom}px ${layout.padLeft}px`,
61
+ '--gbd-re-content-min-h': `${this.pageCount * layout.pageH + Math.max(0, this.pageCount - 1) * GUTTER}px`,
62
+ };
63
+ for (const [k, v] of Object.entries(style)) this.editorWrap.style.setProperty(k, v);
64
+ }
65
+
66
+ function applyZoom() {
67
+ this.statusBar.setZoom(this.zoom);
68
+ this.applyEditorGeometry();
69
+ this.schedulePaginate();
70
+ }
71
+
72
+ function setZoom(z) {
73
+ this.zoom = Math.max(40, Math.min(300, Math.round(z)));
74
+ this.prefs.zoom = this.zoom;
75
+ savePrefs(this.prefs);
76
+ this.applyZoom();
77
+ }
78
+
79
+ function paginate() {
80
+ const ed = this.editor;
81
+ const root = ed?.root;
82
+ if (!root || ed.composing) return;
83
+ const L = this.layout();
84
+ const opts = {
85
+ cellsPerPage: L.cols,
86
+ gapOf: L.padTop + L.padBottom,
87
+ gutter: GUTTER,
88
+ padTop: L.padTop,
89
+ colW: L.colW,
90
+ colGap: L.colGap,
91
+ colSeparator: this.geo().colSeparator,
92
+ padLeft: L.padLeft,
93
+ padRight: L.padRight,
94
+ scale: this.zoom / 100,
95
+ };
96
+ ed.quietly(() => {
97
+ this.pageCount = repaginate(root, { ...opts, cellH: L.availOf() });
98
+ });
99
+ this.refreshFootnotePages();
100
+ this.renderBackdrop();
101
+ if (this.state.footnotes.length === 0) {
102
+ this.fnHeights = [];
103
+ return;
104
+ }
105
+ requestAnimationFrame(() => {
106
+ const hs = this.measureFnBands();
107
+ this.fnHeights = hs;
108
+ if (!hs.some((h) => h > 0)) {
109
+ this.renderBackdrop();
110
+ return;
111
+ }
112
+ ed.quietly(() => {
113
+ this.pageCount = repaginate(root, { ...opts, cellH: (p) => Math.max(60, L.availOf() - (hs[p - 1] || 0)) });
114
+ });
115
+ this.refreshFootnotePages();
116
+ this.renderBackdrop();
117
+ });
118
+ }
119
+
120
+ function schedulePaginate() {
121
+ clearTimeout(this.pageTimer);
122
+ cancelAnimationFrame(this.pageRaf);
123
+ this.pageTimer = setTimeout(() => {
124
+ this.pageRaf = requestAnimationFrame(() => this.paginate());
125
+ }, 120);
126
+ }
127
+
128
+ function renderBackdrop() {
129
+ this.surface.refreshBackdrop();
130
+ this.applyEditorGeometry();
131
+ this.refreshScroll();
132
+ this.statusBar.setPage(this.visiblePage, this.pageCount);
133
+ }
134
+
135
+ function measureFnBands() {
136
+ const scale = this.zoom / 100 || 1;
137
+ const out = [];
138
+ for (const el of this.root.querySelectorAll('.word-sheet-fn[data-page]')) {
139
+ const p = Number(el.dataset.page);
140
+ if (p > 0) out[p - 1] = el.getBoundingClientRect().height / scale;
141
+ }
142
+ return out;
143
+ }
144
+
145
+ function refreshScroll() {
146
+ const pageH = this.layout().pageH;
147
+ if (!pageH) return;
148
+ const step = pageH + GUTTER;
149
+ // 第一页不从滚动原点起:扣掉纸层顶部留白再折算页序,否则页数会提前一页翻
150
+ const offset = this.surface.scrollEl.scrollTop - SHEET_TOP_GAP;
151
+ this.visiblePage = Math.max(1, Math.min(this.pageCount, Math.floor(Math.max(0, offset) / step) + 1));
152
+ this.statusBar.setPage(this.visiblePage, this.pageCount);
153
+ }
154
+
155
+ export const layoutMethods = { geo, layout, sheetsCtx, applyEditorGeometry, applyZoom, setZoom, paginate, schedulePaginate, renderBackdrop, measureFnBands, refreshScroll };
@@ -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 };
@@ -1,22 +1,22 @@
1
- // [文件] gbdocx·word·shared —— 应用级常量与偏好读写(无 this,装配前后都是模块级函数)
2
-
3
- export const MD_MIME = 'text/markdown;charset=utf-8';
4
-
5
- /** 本机偏好存储键(缩放/自动保存/视图开关等,按环境共用一份) */
6
- const LS_KEY = 'gbdocx-word-prefs';
7
-
8
- export function loadPrefs() {
9
- try {
10
- return JSON.parse(localStorage.getItem(LS_KEY) || '{}') || {};
11
- } catch {
12
- return {};
13
- }
14
- }
15
-
16
- export function savePrefs(prefs) {
17
- try {
18
- localStorage.setItem(LS_KEY, JSON.stringify(prefs));
19
- } catch {
20
- /* 忽略存储失败 */
21
- }
22
- }
1
+ // [文件] gbdocx·word·shared —— 应用级常量与偏好读写(无 this,装配前后都是模块级函数)
2
+
3
+ export const MD_MIME = 'text/markdown;charset=utf-8';
4
+
5
+ /** 本机偏好存储键(缩放/自动保存/视图开关等,按环境共用一份) */
6
+ const LS_KEY = 'gbdocx-word-prefs';
7
+
8
+ export function loadPrefs() {
9
+ try {
10
+ return JSON.parse(localStorage.getItem(LS_KEY) || '{}') || {};
11
+ } catch {
12
+ return {};
13
+ }
14
+ }
15
+
16
+ export function savePrefs(prefs) {
17
+ try {
18
+ localStorage.setItem(LS_KEY, JSON.stringify(prefs));
19
+ } catch {
20
+ /* 忽略存储失败 */
21
+ }
22
+ }