@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
package/src/ui/dialogs.js CHANGED
@@ -1,249 +1,249 @@
1
- // [文件] gbdocx·Dialogs —— 通用模态 + 页面设置 / 页边距 / 分栏 对话框
2
- import { HF_MODES, PAGE_NUMBER_ALIGNS, PAGE_NUMBER_FORMATS, PAGE_SIZES, defaultPageConf, defaultPageNumbers, defaultWatermark } from '../kernel/docx/constants.js';
3
-
4
- /** 通用模态:build(bodyEl) 填充内容;onConfirm() 返回 false 可阻止关闭 */
5
- export function openModal({ title, build, onConfirm, confirmText = '确定', width = 460 }) {
6
- const overlay = document.createElement('div');
7
- overlay.className = 'gbd-modal-overlay';
8
- const box = document.createElement('div');
9
- box.className = 'gbd-modal';
10
- box.style.width = `${width}px`;
11
- box.innerHTML = `
12
- <div class="gbd-modal__head"><span class="gbd-modal__title"></span><button type="button" class="gbd-modal__close">✕</button></div>
13
- <div class="gbd-modal__body"></div>
14
- <div class="gbd-modal__foot">
15
- <button type="button" class="gbd-btn" data-act="cancel">取消</button>
16
- <button type="button" class="gbd-btn gbd-btn--primary" data-act="ok"></button>
17
- </div>`;
18
- box.querySelector('.gbd-modal__title').textContent = title;
19
- box.querySelector('[data-act="ok"]').textContent = confirmText;
20
- const bodyEl = box.querySelector('.gbd-modal__body');
21
- const close = () => {
22
- document.removeEventListener('keydown', onKey, true);
23
- overlay.parentElement?.remove();
24
- };
25
- const onKey = (e) => {
26
- if (e.key === 'Escape') {
27
- e.stopPropagation();
28
- close();
29
- }
30
- };
31
- const confirm = () => {
32
- if (onConfirm && onConfirm() === false) return;
33
- close();
34
- };
35
- box.querySelector('[data-act="cancel"]').addEventListener('click', close);
36
- box.querySelector('.gbd-modal__close').addEventListener('click', close);
37
- box.querySelector('[data-act="ok"]').addEventListener('click', confirm);
38
- overlay.addEventListener('mousedown', (e) => {
39
- if (e.target === overlay) close();
40
- });
41
- document.addEventListener('keydown', onKey, true);
42
- build?.(bodyEl, close);
43
- overlay.appendChild(box);
44
- // 样式作用域:模态挂 body,但仍须落在 .word-scope 子树里
45
- const scope = document.createElement('div');
46
- scope.className = 'word-scope';
47
- scope.appendChild(overlay);
48
- document.body.appendChild(scope);
49
- return { close };
50
- }
51
-
52
- function field(labelText, control) {
53
- const row = document.createElement('label');
54
- row.className = 'gbd-field';
55
- const span = document.createElement('span');
56
- span.className = 'gbd-field__label';
57
- span.textContent = labelText;
58
- row.append(span, control);
59
- return row;
60
- }
61
-
62
- function selectEl(options, value) {
63
- const sel = document.createElement('select');
64
- sel.className = 'gbd-input';
65
- for (const o of options) {
66
- const opt = document.createElement('option');
67
- opt.value = o.value;
68
- opt.textContent = o.label;
69
- sel.appendChild(opt);
70
- }
71
- sel.value = value ?? '';
72
- return sel;
73
- }
74
-
75
- function numberEl(value, { min, max, step = 1 } = {}) {
76
- const input = document.createElement('input');
77
- input.type = 'number';
78
- input.className = 'gbd-input';
79
- if (min !== undefined) input.min = min;
80
- if (max !== undefined) input.max = max;
81
- input.step = step;
82
- input.value = value;
83
- return input;
84
- }
85
-
86
- /** 页面设置对话框(纸张/方向/分栏/页码/水印/页眉页脚),确认回调收到 {page, pageNumbers, watermark, header, footer} */
87
- export function openPageSetupDialog(current, onConfirm) {
88
- const page = { ...defaultPageConf(), ...(current.page || {}) };
89
- const pn = { ...defaultPageNumbers(), ...(current.pageNumbers || {}) };
90
- const wm = { ...defaultWatermark(), ...(current.watermark || {}) };
91
- const margins = { ...page.margins };
92
- let out = null;
93
- openModal({
94
- title: '页面设置',
95
- width: 520,
96
- build: (body) => {
97
- const grid = document.createElement('div');
98
- grid.className = 'gbd-field-grid';
99
- const sizeSel = selectEl(Object.keys(PAGE_SIZES).map((k) => ({ value: k, label: k })), page.size);
100
- const orientSel = selectEl([{ value: 'portrait', label: '纵向' }, { value: 'landscape', label: '横向' }], page.orientation);
101
- const colsSel = numberEl(page.columns || 1, { min: 1, max: 3 });
102
- grid.append(field('纸张', sizeSel), field('方向', orientSel), field('分栏', colsSel));
103
- body.appendChild(grid);
104
-
105
- const marginGrid = document.createElement('div');
106
- marginGrid.className = 'gbd-field-grid';
107
- const mTop = numberEl(margins.top, { min: 0, step: 0.1 });
108
- const mBottom = numberEl(margins.bottom, { min: 0, step: 0.1 });
109
- const mLeft = numberEl(margins.left, { min: 0, step: 0.1 });
110
- const mRight = numberEl(margins.right, { min: 0, step: 0.1 });
111
- marginGrid.append(field('上边距 mm', mTop), field('下边距 mm', mBottom), field('左边距 mm', mLeft), field('右边距 mm', mRight));
112
- body.appendChild(marginGrid);
113
-
114
- const section = document.createElement('div');
115
- section.className = 'gbd-field-section';
116
- section.textContent = '页码';
117
- body.appendChild(section);
118
- const pnGrid = document.createElement('div');
119
- pnGrid.className = 'gbd-field-grid';
120
- const pnEnabled = document.createElement('input');
121
- pnEnabled.type = 'checkbox';
122
- pnEnabled.checked = pn.enabled;
123
- const pnAlign = selectEl(PAGE_NUMBER_ALIGNS, pn.align);
124
- const pnFormat = selectEl(PAGE_NUMBER_FORMATS, pn.format);
125
- const pnStart = numberEl(pn.start, { min: 1 });
126
- pnGrid.append(field('显示页码', pnEnabled), field('位置', pnAlign), field('格式', pnFormat), field('起始页码', pnStart));
127
- body.appendChild(pnGrid);
128
-
129
- const wmSection = document.createElement('div');
130
- wmSection.className = 'gbd-field-section';
131
- wmSection.textContent = '水印';
132
- body.appendChild(wmSection);
133
- const wmGrid = document.createElement('div');
134
- wmGrid.className = 'gbd-field-grid';
135
- const wmText = document.createElement('input');
136
- wmText.type = 'text';
137
- wmText.className = 'gbd-input';
138
- wmText.value = wm.data || '';
139
- wmText.placeholder = '留空即无水印';
140
- const wmColor = document.createElement('input');
141
- wmColor.type = 'color';
142
- wmColor.className = 'gbd-input';
143
- wmColor.value = /^#[0-9a-f]{6}$/i.test(wm.color || '') ? wm.color : '#AEB5C0';
144
- const wmSize = numberEl(wm.size, { min: 20, max: 400 });
145
- const wmOpacity = numberEl(wm.opacity, { min: 0, max: 1, step: 0.05 });
146
- wmGrid.append(field('水印文字', wmText), field('颜色', wmColor), field('字号 pt', wmSize), field('透明度', wmOpacity));
147
- body.appendChild(wmGrid);
148
-
149
- const hfSection = document.createElement('div');
150
- hfSection.className = 'gbd-field-section';
151
- hfSection.textContent = '页眉页脚';
152
- body.appendChild(hfSection);
153
- const hfGrid = document.createElement('div');
154
- hfGrid.className = 'gbd-field-grid';
155
- const hfMode = selectEl(HF_MODES, current.hfMode || 'all');
156
- const plainHf = (html) => (html || '').replace(/<[^>]*>/g, '');
157
- const textInput = (value) => {
158
- const input = document.createElement('input');
159
- input.type = 'text';
160
- input.className = 'gbd-input';
161
- input.value = value;
162
- return input;
163
- };
164
- const hfHeader = textInput(plainHf(current.header));
165
- const hfFooter = textInput(plainHf(current.footer));
166
- hfGrid.append(field('重复模式', hfMode), field('页眉文字', hfHeader), field('页脚文字', hfFooter));
167
- body.appendChild(hfGrid);
168
-
169
- const firstGrid = document.createElement('div');
170
- firstGrid.className = 'gbd-field-grid';
171
- const hfHeaderFirst = textInput(plainHf(current.headerFirst));
172
- const hfFooterFirst = textInput(plainHf(current.footerFirst));
173
- firstGrid.append(field('首页页眉', hfHeaderFirst), field('首页页脚', hfFooterFirst));
174
- body.appendChild(firstGrid);
175
- const syncFirst = () => {
176
- const show = hfMode.value === 'except-first' || hfMode.value === 'odd-even-first';
177
- firstGrid.style.display = show ? '' : 'none';
178
- };
179
- hfMode.addEventListener('change', syncFirst);
180
- syncFirst();
181
-
182
- // 文本框只承载纯文本:值没改就保留原有富文本 HTML,改过的才用新文字覆盖
183
- const picked = (input, original) => (input.value === plainHf(original) ? original : input.value);
184
-
185
- out = () => ({
186
- page: {
187
- ...page,
188
- size: sizeSel.value,
189
- orientation: orientSel.value,
190
- columns: Math.max(1, Math.min(3, Number(colsSel.value) || 1)),
191
- margins: { top: Number(mTop.value), bottom: Number(mBottom.value), left: Number(mLeft.value), right: Number(mRight.value) },
192
- },
193
- pageNumbers: { enabled: pnEnabled.checked, align: pnAlign.value, format: pnFormat.value, start: Number(pnStart.value) || 1 },
194
- watermark: { data: wmText.value, color: wmColor.value, size: Number(wmSize.value) || 120, opacity: Number(wmOpacity.value) || 0.3 },
195
- hfMode: hfMode.value,
196
- header: picked(hfHeader, current.header),
197
- footer: picked(hfFooter, current.footer),
198
- headerFirst: picked(hfHeaderFirst, current.headerFirst),
199
- footerFirst: picked(hfFooterFirst, current.footerFirst),
200
- });
201
- },
202
- onConfirm: () => onConfirm(out()),
203
- });
204
- }
205
-
206
- /** 页边距对话框 */
207
- export function openMarginDialog(current, onConfirm) {
208
- const margins = { ...defaultPageConf().margins, ...(current || {}) };
209
- let out = null;
210
- openModal({
211
- title: '页边距',
212
- width: 380,
213
- build: (body) => {
214
- const grid = document.createElement('div');
215
- grid.className = 'gbd-field-grid';
216
- const top = numberEl(margins.top, { min: 0, step: 0.1 });
217
- const bottom = numberEl(margins.bottom, { min: 0, step: 0.1 });
218
- const left = numberEl(margins.left, { min: 0, step: 0.1 });
219
- const right = numberEl(margins.right, { min: 0, step: 0.1 });
220
- grid.append(field('上 mm', top), field('下 mm', bottom), field('左 mm', left), field('右 mm', right));
221
- body.appendChild(grid);
222
- out = () => ({ top: Number(top.value), bottom: Number(bottom.value), left: Number(left.value), right: Number(right.value) });
223
- },
224
- onConfirm: () => onConfirm(out()),
225
- });
226
- }
227
-
228
- /** 分栏对话框 */
229
- export function openColumnDialog(current, onConfirm) {
230
- const page = { ...defaultPageConf(), ...(current || {}) };
231
- let out = null;
232
- openModal({
233
- title: '分栏',
234
- width: 360,
235
- build: (body) => {
236
- const grid = document.createElement('div');
237
- grid.className = 'gbd-field-grid';
238
- const cols = numberEl(page.columns || 1, { min: 1, max: 3 });
239
- const gap = numberEl(page.colGap || 8, { min: 0, step: 0.5 });
240
- const sepOn = document.createElement('input');
241
- sepOn.type = 'checkbox';
242
- sepOn.checked = !!page.colSeparator;
243
- grid.append(field('栏数', cols), field('栏间距 mm', gap), field('分隔线', sepOn));
244
- body.appendChild(grid);
245
- out = () => ({ columns: Math.max(1, Math.min(3, Number(cols.value) || 1)), colGap: Number(gap.value), colSeparator: sepOn.checked });
246
- },
247
- onConfirm: () => onConfirm(out()),
248
- });
249
- }
1
+ // [文件] gbdocx·Dialogs —— 通用模态 + 页面设置 / 页边距 / 分栏 对话框
2
+ import { HF_MODES, PAGE_NUMBER_ALIGNS, PAGE_NUMBER_FORMATS, PAGE_SIZES, defaultPageConf, defaultPageNumbers, defaultWatermark } from '../kernel/docx/constants.js';
3
+
4
+ /** 通用模态:build(bodyEl) 填充内容;onConfirm() 返回 false 可阻止关闭 */
5
+ export function openModal({ title, build, onConfirm, confirmText = '确定', width = 460 }) {
6
+ const overlay = document.createElement('div');
7
+ overlay.className = 'gbd-modal-overlay';
8
+ const box = document.createElement('div');
9
+ box.className = 'gbd-modal';
10
+ box.style.width = `${width}px`;
11
+ box.innerHTML = `
12
+ <div class="gbd-modal__head"><span class="gbd-modal__title"></span><button type="button" class="gbd-modal__close">✕</button></div>
13
+ <div class="gbd-modal__body"></div>
14
+ <div class="gbd-modal__foot">
15
+ <button type="button" class="gbd-btn" data-act="cancel">取消</button>
16
+ <button type="button" class="gbd-btn gbd-btn--primary" data-act="ok"></button>
17
+ </div>`;
18
+ box.querySelector('.gbd-modal__title').textContent = title;
19
+ box.querySelector('[data-act="ok"]').textContent = confirmText;
20
+ const bodyEl = box.querySelector('.gbd-modal__body');
21
+ const close = () => {
22
+ document.removeEventListener('keydown', onKey, true);
23
+ overlay.parentElement?.remove();
24
+ };
25
+ const onKey = (e) => {
26
+ if (e.key === 'Escape') {
27
+ e.stopPropagation();
28
+ close();
29
+ }
30
+ };
31
+ const confirm = () => {
32
+ if (onConfirm && onConfirm() === false) return;
33
+ close();
34
+ };
35
+ box.querySelector('[data-act="cancel"]').addEventListener('click', close);
36
+ box.querySelector('.gbd-modal__close').addEventListener('click', close);
37
+ box.querySelector('[data-act="ok"]').addEventListener('click', confirm);
38
+ overlay.addEventListener('mousedown', (e) => {
39
+ if (e.target === overlay) close();
40
+ });
41
+ document.addEventListener('keydown', onKey, true);
42
+ build?.(bodyEl, close);
43
+ overlay.appendChild(box);
44
+ // 样式作用域:模态挂 body,但仍须落在 .word-scope 子树里
45
+ const scope = document.createElement('div');
46
+ scope.className = 'word-scope';
47
+ scope.appendChild(overlay);
48
+ document.body.appendChild(scope);
49
+ return { close };
50
+ }
51
+
52
+ function field(labelText, control) {
53
+ const row = document.createElement('label');
54
+ row.className = 'gbd-field';
55
+ const span = document.createElement('span');
56
+ span.className = 'gbd-field__label';
57
+ span.textContent = labelText;
58
+ row.append(span, control);
59
+ return row;
60
+ }
61
+
62
+ function selectEl(options, value) {
63
+ const sel = document.createElement('select');
64
+ sel.className = 'gbd-input';
65
+ for (const o of options) {
66
+ const opt = document.createElement('option');
67
+ opt.value = o.value;
68
+ opt.textContent = o.label;
69
+ sel.appendChild(opt);
70
+ }
71
+ sel.value = value ?? '';
72
+ return sel;
73
+ }
74
+
75
+ function numberEl(value, { min, max, step = 1 } = {}) {
76
+ const input = document.createElement('input');
77
+ input.type = 'number';
78
+ input.className = 'gbd-input';
79
+ if (min !== undefined) input.min = min;
80
+ if (max !== undefined) input.max = max;
81
+ input.step = step;
82
+ input.value = value;
83
+ return input;
84
+ }
85
+
86
+ /** 页面设置对话框(纸张/方向/分栏/页码/水印/页眉页脚),确认回调收到 {page, pageNumbers, watermark, header, footer} */
87
+ export function openPageSetupDialog(current, onConfirm) {
88
+ const page = { ...defaultPageConf(), ...(current.page || {}) };
89
+ const pn = { ...defaultPageNumbers(), ...(current.pageNumbers || {}) };
90
+ const wm = { ...defaultWatermark(), ...(current.watermark || {}) };
91
+ const margins = { ...page.margins };
92
+ let out = null;
93
+ openModal({
94
+ title: '页面设置',
95
+ width: 520,
96
+ build: (body) => {
97
+ const grid = document.createElement('div');
98
+ grid.className = 'gbd-field-grid';
99
+ const sizeSel = selectEl(Object.keys(PAGE_SIZES).map((k) => ({ value: k, label: k })), page.size);
100
+ const orientSel = selectEl([{ value: 'portrait', label: '纵向' }, { value: 'landscape', label: '横向' }], page.orientation);
101
+ const colsSel = numberEl(page.columns || 1, { min: 1, max: 3 });
102
+ grid.append(field('纸张', sizeSel), field('方向', orientSel), field('分栏', colsSel));
103
+ body.appendChild(grid);
104
+
105
+ const marginGrid = document.createElement('div');
106
+ marginGrid.className = 'gbd-field-grid';
107
+ const mTop = numberEl(margins.top, { min: 0, step: 0.1 });
108
+ const mBottom = numberEl(margins.bottom, { min: 0, step: 0.1 });
109
+ const mLeft = numberEl(margins.left, { min: 0, step: 0.1 });
110
+ const mRight = numberEl(margins.right, { min: 0, step: 0.1 });
111
+ marginGrid.append(field('上边距 mm', mTop), field('下边距 mm', mBottom), field('左边距 mm', mLeft), field('右边距 mm', mRight));
112
+ body.appendChild(marginGrid);
113
+
114
+ const section = document.createElement('div');
115
+ section.className = 'gbd-field-section';
116
+ section.textContent = '页码';
117
+ body.appendChild(section);
118
+ const pnGrid = document.createElement('div');
119
+ pnGrid.className = 'gbd-field-grid';
120
+ const pnEnabled = document.createElement('input');
121
+ pnEnabled.type = 'checkbox';
122
+ pnEnabled.checked = pn.enabled;
123
+ const pnAlign = selectEl(PAGE_NUMBER_ALIGNS, pn.align);
124
+ const pnFormat = selectEl(PAGE_NUMBER_FORMATS, pn.format);
125
+ const pnStart = numberEl(pn.start, { min: 1 });
126
+ pnGrid.append(field('显示页码', pnEnabled), field('位置', pnAlign), field('格式', pnFormat), field('起始页码', pnStart));
127
+ body.appendChild(pnGrid);
128
+
129
+ const wmSection = document.createElement('div');
130
+ wmSection.className = 'gbd-field-section';
131
+ wmSection.textContent = '水印';
132
+ body.appendChild(wmSection);
133
+ const wmGrid = document.createElement('div');
134
+ wmGrid.className = 'gbd-field-grid';
135
+ const wmText = document.createElement('input');
136
+ wmText.type = 'text';
137
+ wmText.className = 'gbd-input';
138
+ wmText.value = wm.data || '';
139
+ wmText.placeholder = '留空即无水印';
140
+ const wmColor = document.createElement('input');
141
+ wmColor.type = 'color';
142
+ wmColor.className = 'gbd-input';
143
+ wmColor.value = /^#[0-9a-f]{6}$/i.test(wm.color || '') ? wm.color : '#AEB5C0';
144
+ const wmSize = numberEl(wm.size, { min: 20, max: 400 });
145
+ const wmOpacity = numberEl(wm.opacity, { min: 0, max: 1, step: 0.05 });
146
+ wmGrid.append(field('水印文字', wmText), field('颜色', wmColor), field('字号 pt', wmSize), field('透明度', wmOpacity));
147
+ body.appendChild(wmGrid);
148
+
149
+ const hfSection = document.createElement('div');
150
+ hfSection.className = 'gbd-field-section';
151
+ hfSection.textContent = '页眉页脚';
152
+ body.appendChild(hfSection);
153
+ const hfGrid = document.createElement('div');
154
+ hfGrid.className = 'gbd-field-grid';
155
+ const hfMode = selectEl(HF_MODES, current.hfMode || 'all');
156
+ const plainHf = (html) => (html || '').replace(/<[^>]*>/g, '');
157
+ const textInput = (value) => {
158
+ const input = document.createElement('input');
159
+ input.type = 'text';
160
+ input.className = 'gbd-input';
161
+ input.value = value;
162
+ return input;
163
+ };
164
+ const hfHeader = textInput(plainHf(current.header));
165
+ const hfFooter = textInput(plainHf(current.footer));
166
+ hfGrid.append(field('重复模式', hfMode), field('页眉文字', hfHeader), field('页脚文字', hfFooter));
167
+ body.appendChild(hfGrid);
168
+
169
+ const firstGrid = document.createElement('div');
170
+ firstGrid.className = 'gbd-field-grid';
171
+ const hfHeaderFirst = textInput(plainHf(current.headerFirst));
172
+ const hfFooterFirst = textInput(plainHf(current.footerFirst));
173
+ firstGrid.append(field('首页页眉', hfHeaderFirst), field('首页页脚', hfFooterFirst));
174
+ body.appendChild(firstGrid);
175
+ const syncFirst = () => {
176
+ const show = hfMode.value === 'except-first' || hfMode.value === 'odd-even-first';
177
+ firstGrid.style.display = show ? '' : 'none';
178
+ };
179
+ hfMode.addEventListener('change', syncFirst);
180
+ syncFirst();
181
+
182
+ // 文本框只承载纯文本:值没改就保留原有富文本 HTML,改过的才用新文字覆盖
183
+ const picked = (input, original) => (input.value === plainHf(original) ? original : input.value);
184
+
185
+ out = () => ({
186
+ page: {
187
+ ...page,
188
+ size: sizeSel.value,
189
+ orientation: orientSel.value,
190
+ columns: Math.max(1, Math.min(3, Number(colsSel.value) || 1)),
191
+ margins: { top: Number(mTop.value), bottom: Number(mBottom.value), left: Number(mLeft.value), right: Number(mRight.value) },
192
+ },
193
+ pageNumbers: { enabled: pnEnabled.checked, align: pnAlign.value, format: pnFormat.value, start: Number(pnStart.value) || 1 },
194
+ watermark: { data: wmText.value, color: wmColor.value, size: Number(wmSize.value) || 120, opacity: Number(wmOpacity.value) || 0.3 },
195
+ hfMode: hfMode.value,
196
+ header: picked(hfHeader, current.header),
197
+ footer: picked(hfFooter, current.footer),
198
+ headerFirst: picked(hfHeaderFirst, current.headerFirst),
199
+ footerFirst: picked(hfFooterFirst, current.footerFirst),
200
+ });
201
+ },
202
+ onConfirm: () => onConfirm(out()),
203
+ });
204
+ }
205
+
206
+ /** 页边距对话框 */
207
+ export function openMarginDialog(current, onConfirm) {
208
+ const margins = { ...defaultPageConf().margins, ...(current || {}) };
209
+ let out = null;
210
+ openModal({
211
+ title: '页边距',
212
+ width: 380,
213
+ build: (body) => {
214
+ const grid = document.createElement('div');
215
+ grid.className = 'gbd-field-grid';
216
+ const top = numberEl(margins.top, { min: 0, step: 0.1 });
217
+ const bottom = numberEl(margins.bottom, { min: 0, step: 0.1 });
218
+ const left = numberEl(margins.left, { min: 0, step: 0.1 });
219
+ const right = numberEl(margins.right, { min: 0, step: 0.1 });
220
+ grid.append(field('上 mm', top), field('下 mm', bottom), field('左 mm', left), field('右 mm', right));
221
+ body.appendChild(grid);
222
+ out = () => ({ top: Number(top.value), bottom: Number(bottom.value), left: Number(left.value), right: Number(right.value) });
223
+ },
224
+ onConfirm: () => onConfirm(out()),
225
+ });
226
+ }
227
+
228
+ /** 分栏对话框 */
229
+ export function openColumnDialog(current, onConfirm) {
230
+ const page = { ...defaultPageConf(), ...(current || {}) };
231
+ let out = null;
232
+ openModal({
233
+ title: '分栏',
234
+ width: 360,
235
+ build: (body) => {
236
+ const grid = document.createElement('div');
237
+ grid.className = 'gbd-field-grid';
238
+ const cols = numberEl(page.columns || 1, { min: 1, max: 3 });
239
+ const gap = numberEl(page.colGap || 8, { min: 0, step: 0.5 });
240
+ const sepOn = document.createElement('input');
241
+ sepOn.type = 'checkbox';
242
+ sepOn.checked = !!page.colSeparator;
243
+ grid.append(field('栏数', cols), field('栏间距 mm', gap), field('分隔线', sepOn));
244
+ body.appendChild(grid);
245
+ out = () => ({ columns: Math.max(1, Math.min(3, Number(cols.value) || 1)), colGap: Number(gap.value), colSeparator: sepOn.checked });
246
+ },
247
+ onConfirm: () => onConfirm(out()),
248
+ });
249
+ }