@a200888213/gbdocx 0.0.0-stage → 0.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (96) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +206 -2
  3. package/index.html +25 -0
  4. package/package.json +48 -6
  5. package/serve.mjs +22 -0
  6. package/src/app/file-io.js +122 -0
  7. package/src/app/main.js +9 -0
  8. package/src/app/word/catalog.js +52 -0
  9. package/src/app/word/comments.js +112 -0
  10. package/src/app/word/doc.js +113 -0
  11. package/src/app/word/edit.js +72 -0
  12. package/src/app/word/file.js +162 -0
  13. package/src/app/word/hf.js +76 -0
  14. package/src/app/word/layout.js +155 -0
  15. package/src/app/word/menus.js +175 -0
  16. package/src/app/word/notes.js +110 -0
  17. package/src/app/word/shared.js +22 -0
  18. package/src/app/word/view.js +117 -0
  19. package/src/app/word-app.js +459 -0
  20. package/src/app/word-sheets.js +101 -0
  21. package/src/kernel/docx/catalog.js +41 -0
  22. package/src/kernel/docx/codec/blocks.js +123 -0
  23. package/src/kernel/docx/codec/carry.js +129 -0
  24. package/src/kernel/docx/codec/comments.js +25 -0
  25. package/src/kernel/docx/codec/docx.js +198 -0
  26. package/src/kernel/docx/codec/docxStyles.js +177 -0
  27. package/src/kernel/docx/codec/fonts.js +195 -0
  28. package/src/kernel/docx/codec/helpers.js +161 -0
  29. package/src/kernel/docx/codec/hfHtml.js +152 -0
  30. package/src/kernel/docx/codec/html.js +739 -0
  31. package/src/kernel/docx/codec/losses.js +420 -0
  32. package/src/kernel/docx/codec/markdown.js +170 -0
  33. package/src/kernel/docx/codec/parts.js +249 -0
  34. package/src/kernel/docx/codec/read.js +603 -0
  35. package/src/kernel/docx/codec/render.js +1156 -0
  36. package/src/kernel/docx/codec/roundtrip.js +236 -0
  37. package/src/kernel/docx/codec/write.js +833 -0
  38. package/src/kernel/docx/comments.js +14 -0
  39. package/src/kernel/docx/constants.js +63 -0
  40. package/src/kernel/docx/menus.js +211 -0
  41. package/src/kernel/docx/pageGeometry.js +123 -0
  42. package/src/kernel/docx/pagination.js +481 -0
  43. package/src/kernel/docx/watermark.js +45 -0
  44. package/src/kernel/docx/wordShortcuts.js +37 -0
  45. package/src/kernel/editor/constants.js +8 -0
  46. package/src/kernel/editor/engine/blocks.js +594 -0
  47. package/src/kernel/editor/engine/blocksEnter.js +109 -0
  48. package/src/kernel/editor/engine/blocksInsert.js +254 -0
  49. package/src/kernel/editor/engine/blocksStructure.js +226 -0
  50. package/src/kernel/editor/engine/commands.js +181 -0
  51. package/src/kernel/editor/engine/dom.js +646 -0
  52. package/src/kernel/editor/engine/editor.js +213 -0
  53. package/src/kernel/editor/engine/history.js +149 -0
  54. package/src/kernel/editor/engine/inline.js +656 -0
  55. package/src/kernel/editor/engine/input.js +554 -0
  56. package/src/kernel/editor/engine/interaction.js +137 -0
  57. package/src/kernel/editor/engine/pointer.js +39 -0
  58. package/src/kernel/editor/engine/revision.js +138 -0
  59. package/src/kernel/editor/engine/schema.js +417 -0
  60. package/src/kernel/editor/engine/search.js +113 -0
  61. package/src/kernel/editor/engine/selection.js +119 -0
  62. package/src/kernel/editor/engine/state.js +118 -0
  63. package/src/kernel/editor/engine/tableClip.js +154 -0
  64. package/src/kernel/editor/engine/tableGrid.js +55 -0
  65. package/src/kernel/editor/engine/tableInteraction.js +388 -0
  66. package/src/kernel/editor/engine/tableMenu.js +250 -0
  67. package/src/kernel/editor/engine/tableOps.js +174 -0
  68. package/src/kernel/editor/engine/tables.js +732 -0
  69. package/src/kernel/editor/overlayGeometry.js +20 -0
  70. package/src/kernel/editor/pasteClean.js +293 -0
  71. package/src/kernel/editor/utils.js +49 -0
  72. package/src/kernel/keyboard.js +114 -0
  73. package/src/kernel/notify.js +54 -0
  74. package/src/kernel/textStyles.js +84 -0
  75. package/src/kernel/util/clipboard.js +89 -0
  76. package/src/kernel/util/imageFile.js +91 -0
  77. package/src/kernel/util/safeValue.js +25 -0
  78. package/src/kernel/util/shortcutKey.js +33 -0
  79. package/src/surface/editor-surface.js +600 -0
  80. package/src/ui/color-panel.js +69 -0
  81. package/src/ui/context-menu.js +130 -0
  82. package/src/ui/dialogs.js +249 -0
  83. package/src/ui/find-bar.js +151 -0
  84. package/src/ui/icons.js +134 -0
  85. package/src/ui/menubar.js +28 -0
  86. package/src/ui/panels.js +115 -0
  87. package/src/ui/popup.js +79 -0
  88. package/src/ui/prompt.js +89 -0
  89. package/src/ui/ribbon.js +485 -0
  90. package/src/ui/special-chars.js +39 -0
  91. package/src/ui/statusbar.js +81 -0
  92. package/src/ui/toolbar.js +32 -0
  93. package/styles/app.css +944 -0
  94. package/styles/editor-content.css +440 -0
  95. package/styles/tokens.css +154 -0
  96. package/styles/word-panel.css +410 -0
@@ -0,0 +1,130 @@
1
+ // [文件] gbdocx·ContextMenu —— 右键/下拉菜单渲染(条目口径:label/disabled/checked/icon/colorDot/shortcut/divided/children/input/onClick)
2
+ import { openPopup } from './popup.js';
3
+
4
+ const CHECK = '<svg viewBox="0 0 24 24" width="14" height="14" fill="none" stroke="currentColor" stroke-width="2.4" stroke-linecap="round" stroke-linejoin="round"><path d="M5 12l5 5L20 7"/></svg>';
5
+ const SUB = '<svg viewBox="0 0 24 24" width="12" height="12" fill="none" stroke="currentColor" stroke-width="2.2" stroke-linecap="round" stroke-linejoin="round"><path d="M9 6l6 6-6 6"/></svg>';
6
+
7
+ /**
8
+ * 打开菜单
9
+ * @param {object} opts
10
+ * @param {HTMLElement|DOMRect} opts.anchor
11
+ * @param {Array<object>} opts.items 条目表
12
+ * @param {string} [opts.placement]
13
+ * @param {() => void} [opts.onClose] 关闭回调(父菜单据此同步按钮态)
14
+ */
15
+ export function openMenu({ anchor, items, placement = 'bottom-start', onClose }) {
16
+ const body = buildList(items);
17
+ return openPopup({
18
+ anchor,
19
+ content: body,
20
+ placement,
21
+ className: 'gbd-menu',
22
+ // 子菜单是独立浮层:既不能算作「点在外面」把父菜单关掉,父菜单关闭时也要把它一起收
23
+ isInside: (target) => Boolean(target?.closest?.('.gbd-menu--sub')),
24
+ onClose: () => {
25
+ body.__closeSub?.();
26
+ onClose?.();
27
+ },
28
+ });
29
+ }
30
+
31
+ /** 构建条目列表 DOM(子菜单在 hover 时另开浮层) */
32
+ function buildList(items) {
33
+ const ul = document.createElement('div');
34
+ ul.className = 'gbd-menu__list';
35
+ let subHandle = null;
36
+ const closeSub = () => {
37
+ subHandle?.close();
38
+ subHandle = null;
39
+ };
40
+ // 父级浮层关闭时由 openMenu 回调这里,把子菜单一起收走
41
+ ul.__closeSub = closeSub;
42
+ for (const item of items || []) {
43
+ // divided 有两种用法:裸分隔标记(只有 divided)与「本项之前画分隔线」(真实条目带 label)。
44
+ // 后者不能 continue,否则每个分组的首项(如表格菜单的「全选 / 删除表格」)都会被吞成一条分隔线。
45
+ if (item.divided) {
46
+ const sep = document.createElement('div');
47
+ sep.className = 'gbd-menu__sep';
48
+ ul.appendChild(sep);
49
+ if (!item.label) continue;
50
+ }
51
+ const row = document.createElement('div');
52
+ row.className = 'gbd-menu__item';
53
+ if (item.disabled) row.classList.add('gbd-is-disabled');
54
+ if (item.danger) row.classList.add('gbd-is-danger');
55
+
56
+ const lead = document.createElement('span');
57
+ lead.className = 'gbd-menu__lead';
58
+ if (item.checked) lead.innerHTML = CHECK;
59
+ else if (item.colorDot) {
60
+ lead.innerHTML = `<i class="gbd-menu__dot" style="background:${item.colorDot}"></i>`;
61
+ }
62
+ row.appendChild(lead);
63
+
64
+ const label = document.createElement('span');
65
+ label.className = 'gbd-menu__label';
66
+ label.textContent = item.label;
67
+ row.appendChild(label);
68
+
69
+ let inputEl = null;
70
+ if (item.input && !item.disabled) {
71
+ const box = document.createElement('span');
72
+ box.className = 'gbd-menu__input';
73
+ inputEl = document.createElement('input');
74
+ inputEl.type = 'number';
75
+ inputEl.min = item.input.min;
76
+ inputEl.max = item.input.max;
77
+ inputEl.value = item.input.defaultValue;
78
+ inputEl.addEventListener('mousedown', (e) => e.stopPropagation());
79
+ box.appendChild(inputEl);
80
+ if (item.input.unit) {
81
+ const unit = document.createElement('span');
82
+ unit.className = 'gbd-menu__unit';
83
+ unit.textContent = item.input.unit;
84
+ box.appendChild(unit);
85
+ }
86
+ row.appendChild(box);
87
+ } else if (item.shortcut) {
88
+ const sc = document.createElement('span');
89
+ sc.className = 'gbd-menu__shortcut';
90
+ sc.textContent = item.shortcut;
91
+ row.appendChild(sc);
92
+ }
93
+
94
+ if (item.children) {
95
+ const arrow = document.createElement('span');
96
+ arrow.className = 'gbd-menu__arrow';
97
+ arrow.innerHTML = SUB;
98
+ row.appendChild(arrow);
99
+ let openTimer = 0;
100
+ row.addEventListener('mouseenter', () => {
101
+ clearTimeout(openTimer);
102
+ openTimer = setTimeout(() => {
103
+ closeSub();
104
+ if (item.disabled) return;
105
+ const subBody = buildList(item.children);
106
+ subHandle = openPopup({
107
+ anchor: row.getBoundingClientRect(),
108
+ content: subBody,
109
+ placement: 'right-start',
110
+ className: 'gbd-menu gbd-menu--sub',
111
+ // 子级浮层:不能走 closeAll 把父菜单一起关掉
112
+ nested: true,
113
+ onClose: () => subBody.__closeSub?.(),
114
+ });
115
+ }, 60);
116
+ });
117
+ row.addEventListener('mouseleave', () => clearTimeout(openTimer));
118
+ } else {
119
+ row.addEventListener('click', (e) => {
120
+ if (item.disabled) return;
121
+ const value = inputEl ? Number(inputEl.value) : undefined;
122
+ item.onClick?.(e, value);
123
+ // 关闭整串菜单(含父级)
124
+ document.dispatchEvent(new CustomEvent('gbdocx-menu-close'));
125
+ });
126
+ }
127
+ ul.appendChild(row);
128
+ }
129
+ return ul;
130
+ }
@@ -0,0 +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
+ }
@@ -0,0 +1,151 @@
1
+ // [文件] gbdocx·FindBar —— 查找与替换浮条(两行:查找行常显、替换行默认收起;走内核 search 的 CSS Highlight 高亮,正文零改写)
2
+ export class FindBar {
3
+ /** @param {() => object|null} getEditor 内核实例读取器 */
4
+ constructor(getEditor) {
5
+ this.getEditor = getEditor;
6
+ this.replaceOpen = false;
7
+ this.readOnly = false;
8
+ this.el = document.createElement('div');
9
+ this.el.className = 'gbd-findbar';
10
+ this.el.innerHTML = `
11
+ <div class="gbd-findbar__row">
12
+ <input class="gbd-findbar__input" type="text" placeholder="查找…" />
13
+ <span class="gbd-findbar__count">0/0</span>
14
+ <button type="button" class="gbd-iconbtn" data-act="prev" title="上一个">↑</button>
15
+ <button type="button" class="gbd-iconbtn" data-act="next" title="下一个">↓</button>
16
+ <label class="gbd-findbar__case"><input type="checkbox" data-act="case" />区分大小写</label>
17
+ <button type="button" class="gbd-iconbtn" data-act="toggleReplace" title="展开替换"></button>
18
+ <button type="button" class="gbd-iconbtn" data-act="close" title="关闭">✕</button>
19
+ </div>
20
+ <div class="gbd-findbar__row" data-k="replace" hidden>
21
+ <input class="gbd-findbar__replace" type="text" placeholder="替换为…" />
22
+ <button type="button" class="gbd-btn" data-act="replace">替换</button>
23
+ <button type="button" class="gbd-btn" data-act="replaceAll">全部替换</button>
24
+ </div>`;
25
+ this.el.hidden = true;
26
+ this.input = this.el.querySelector('.gbd-findbar__input');
27
+ this.replaceInput = this.el.querySelector('.gbd-findbar__replace');
28
+ this.replaceRow = this.el.querySelector('[data-k="replace"]');
29
+ this.countEl = this.el.querySelector('.gbd-findbar__count');
30
+ this.caseEl = this.el.querySelector('[data-act="case"]');
31
+ this.toggleReplaceEl = this.el.querySelector('[data-act="toggleReplace"]');
32
+ this.bind();
33
+ this.syncReplace();
34
+ }
35
+
36
+ bind() {
37
+ const on = (act, fn) => this.el.querySelector(`[data-act="${act}"]`).addEventListener('click', (e) => {
38
+ e.preventDefault();
39
+ fn();
40
+ });
41
+ this.input.addEventListener('input', () => this.refresh());
42
+ this.caseEl.addEventListener('change', () => this.refresh());
43
+ this.input.addEventListener('keydown', (e) => {
44
+ if (e.key === 'Enter') {
45
+ e.preventDefault();
46
+ e.shiftKey ? this.prev() : this.next();
47
+ } else if (e.key === 'Escape') {
48
+ e.preventDefault();
49
+ this.close();
50
+ }
51
+ });
52
+ this.replaceInput.addEventListener('keydown', (e) => {
53
+ if (e.key === 'Enter') {
54
+ e.preventDefault();
55
+ this.replaceCurrent();
56
+ } else if (e.key === 'Escape') {
57
+ e.preventDefault();
58
+ this.close();
59
+ }
60
+ });
61
+ on('prev', () => this.prev());
62
+ on('next', () => this.next());
63
+ on('replace', () => this.replaceCurrent());
64
+ on('replaceAll', () => this.replaceAll());
65
+ on('close', () => this.close());
66
+ on('toggleReplace', () => this.setReplaceOpen(!this.replaceOpen));
67
+ }
68
+
69
+ get search() {
70
+ return this.getEditor()?.search || null;
71
+ }
72
+
73
+ /** 替换行展开/收起(展开时聚焦替换输入) */
74
+ setReplaceOpen(on) {
75
+ this.replaceOpen = !!on;
76
+ this.syncReplace();
77
+ if (this.replaceOpen) requestAnimationFrame(() => this.replaceInput.focus());
78
+ }
79
+
80
+ syncReplace() {
81
+ this.replaceRow.hidden = !this.replaceOpen;
82
+ this.toggleReplaceEl.textContent = this.replaceOpen ? '▴' : '▾';
83
+ this.toggleReplaceEl.title = this.replaceOpen ? '收起替换' : '展开替换';
84
+ }
85
+
86
+ refresh() {
87
+ const s = this.search;
88
+ if (!s) return;
89
+ s.setKeyword(this.input.value, { caseSensitive: this.caseEl.checked });
90
+ this.renderCount();
91
+ }
92
+
93
+ renderCount() {
94
+ const s = this.search;
95
+ if (!s) return;
96
+ const snap = s.snapshot();
97
+ this.countEl.textContent = `${snap.total ? snap.activeIndex + 1 : 0}/${snap.total}`;
98
+ }
99
+
100
+ next() {
101
+ this.search?.next();
102
+ this.renderCount();
103
+ }
104
+
105
+ prev() {
106
+ this.search?.prev();
107
+ this.renderCount();
108
+ }
109
+
110
+ replaceCurrent() {
111
+ if (this.readOnly) return;
112
+ const ed = this.getEditor();
113
+ if (!ed) return;
114
+ ed.run(() => ed.search.replaceCurrent(this.replaceInput.value));
115
+ this.renderCount();
116
+ }
117
+
118
+ replaceAll() {
119
+ if (this.readOnly) return;
120
+ const ed = this.getEditor();
121
+ if (!ed) return;
122
+ ed.run(() => ed.search.replaceAll(this.replaceInput.value));
123
+ this.renderCount();
124
+ }
125
+
126
+ /** 只读档只留查找,替换行的入口与执行一并锁掉 */
127
+ setReadOnly(on) {
128
+ this.readOnly = !!on;
129
+ this.toggleReplaceEl.disabled = this.readOnly;
130
+ if (this.readOnly) this.setReplaceOpen(false);
131
+ }
132
+
133
+ /** 打开:replace=true(Ctrl+H / 菜单「查找和替换」)时展开替换行,否则只显示查找行 */
134
+ open(replace = false) {
135
+ this.el.hidden = false;
136
+ this.setReplaceOpen(replace && !this.readOnly);
137
+ requestAnimationFrame(() => {
138
+ this.input.focus();
139
+ this.input.select();
140
+ });
141
+ this.refresh();
142
+ }
143
+
144
+ close() {
145
+ if (this.el.hidden) return;
146
+ this.el.hidden = true;
147
+ this.replaceOpen = false;
148
+ this.syncReplace();
149
+ this.search?.clear();
150
+ }
151
+ }