@a200888213/gbdocx 0.0.0-stage → 0.3.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 (95) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +205 -2
  3. package/index.html +24 -0
  4. package/package.json +47 -6
  5. package/serve.mjs +20 -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 +152 -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 +88 -0
  19. package/src/app/word-app.js +440 -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 +121 -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 +208 -0
  50. package/src/kernel/editor/engine/commands.js +180 -0
  51. package/src/kernel/editor/engine/dom.js +622 -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 +371 -0
  66. package/src/kernel/editor/engine/tableMenu.js +250 -0
  67. package/src/kernel/editor/engine/tableOps.js +178 -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 +469 -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 +36 -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/special-chars.js +39 -0
  90. package/src/ui/statusbar.js +81 -0
  91. package/src/ui/toolbar.js +199 -0
  92. package/styles/app.css +944 -0
  93. package/styles/editor-content.css +440 -0
  94. package/styles/tokens.css +154 -0
  95. package/styles/word-panel.css +450 -0
@@ -0,0 +1,469 @@
1
+ // [文件] gbdocx·EditorSurface —— 富文本内核宿主(移植自 guabi EditorSurface.vue):挂载内核 / 防抖上报 / 表格浮件层 / 框选 / 图片缩放与放大 / 留白落点
2
+ // 说明:Vue 槽位改回调装配(backdrop 由宿主填充),其余行为与 guabi 版逐条对齐。
3
+
4
+ import { EditorCore } from '../kernel/editor/engine/editor.js';
5
+ import { overlayOrigin, PROBE_WIDTH } from '../kernel/editor/overlayGeometry.js';
6
+
7
+ /** 内容上报防抖(ms):按键只置脏,序列化交给拖尾防抖;失焦与卸载立即落地 */
8
+ const EMIT_DEBOUNCE = 250;
9
+ /** 桌面式框选升级阈值(px) */
10
+ const MARQUEE_THRESHOLD = 4;
11
+ const MIN_IMAGE_WIDTH = 60;
12
+ const MIN_IMAGE_HEIGHT = 40;
13
+ const RESIZE_DIRECTIONS = ['top-left', 'top-right', 'bottom-left', 'bottom-right'];
14
+ const ZOOM_ICON = '<svg viewBox="0 0 24 24" width="14" height="14" fill="none" stroke="currentColor" stroke-width="2"><circle cx="11" cy="11" r="7"/><path d="M21 21l-4.3-4.3M11 8v6M8 11h6"/></svg>';
15
+
16
+ export class EditorSurface {
17
+ /**
18
+ * @param {object} opts
19
+ * @param {HTMLElement} opts.mount 挂载容器
20
+ * @param {string} [opts.modelValue] 初始 HTML
21
+ * @param {string} [opts.placeholder]
22
+ * @param {boolean} [opts.editable=true]
23
+ * @param {object} [opts.handlers] 额外内核事件处理器(与内置 update/blur/atom 合并)
24
+ * @param {(host: HTMLElement) => void} [opts.backdrop] 背景层渲染(Word 纸面层填充);返回后由宿主 refreshBackdrop 触发
25
+ */
26
+ constructor(opts) {
27
+ this.opts = opts;
28
+ this.handlers = opts.handlers || {};
29
+ this.editable = opts.editable !== false;
30
+ this.backdropRender = opts.backdrop || null;
31
+ this.lastEmittedHtml = '';
32
+ this.emitTimer = 0;
33
+ this.emitPending = false;
34
+ this.build();
35
+ this.createEditor(opts.modelValue || '');
36
+ }
37
+
38
+ /* ===================== DOM 装配 ===================== */
39
+
40
+ build() {
41
+ const root = document.createElement('div');
42
+ root.className = 'rich-edit-surface gbd-is-fill';
43
+ root.innerHTML = `
44
+ <div class="re-scroll dt-scrollbar-thin">
45
+ <div class="re-backdrop-host"></div>
46
+ <span class="re-overlay-probe" aria-hidden="true"></span>
47
+ <div class="re-content rich-text-content"></div>
48
+ <div class="re-table-layer">
49
+ <span class="re-table-select" hidden></span>
50
+ <div class="re-table-handles" hidden>
51
+ <button type="button" class="re-table-handle gbd-is-column" aria-label="在右侧插入一列">+</button>
52
+ <button type="button" class="re-table-handle gbd-is-row" aria-label="在下方插入一行">+</button>
53
+ </div>
54
+ <span class="re-col-line" hidden></span>
55
+ </div>
56
+ <div class="re-marquee" hidden></div>
57
+ <div class="re-resize-box" hidden>
58
+ <button type="button" class="re-zoom-btn" title="放大预览" aria-label="放大预览这张图片">${ZOOM_ICON}</button>
59
+ ${RESIZE_DIRECTIONS.map((d) => `<span class="re-resize-handle gbd-is-${d}" data-dir="${d}"></span>`).join('')}
60
+ </div>
61
+ </div>
62
+ <div class="re-image-viewer" hidden>
63
+ <img alt="" />
64
+ </div>`;
65
+ this.el = root;
66
+ this.scrollEl = root.querySelector('.re-scroll');
67
+ this.backdropHost = root.querySelector('.re-backdrop-host');
68
+ this.probeEl = root.querySelector('.re-overlay-probe');
69
+ this.probeEl.style.width = `${PROBE_WIDTH}px`;
70
+ this.contentEl = root.querySelector('.re-content');
71
+ this.tableLayer = root.querySelector('.re-table-layer');
72
+ this.selectBoxEl = root.querySelector('.re-table-select');
73
+ this.handlesEl = root.querySelector('.re-table-handles');
74
+ this.colLineEl = root.querySelector('.re-col-line');
75
+ this.marqueeEl = root.querySelector('.re-marquee');
76
+ this.resizeBoxEl = root.querySelector('.re-resize-box');
77
+ this.viewerEl = root.querySelector('.re-image-viewer');
78
+ this.opts.mount.appendChild(root);
79
+ this.bindSurface();
80
+ this.lastEmittedHtml = '';
81
+ }
82
+
83
+ /* ===================== 内核装配 ===================== */
84
+
85
+ createEditor(modelValue) {
86
+ const extra = this.handlers;
87
+ const ed = new EditorCore({
88
+ root: this.contentEl,
89
+ placeholder: this.opts.placeholder || '',
90
+ handlers: {
91
+ ...extra,
92
+ update: () => {
93
+ this.scheduleEmit();
94
+ this.scheduleOverlayUpdate();
95
+ extra.update?.();
96
+ },
97
+ blur: () => {
98
+ this.flushEmit();
99
+ extra.blur?.();
100
+ },
101
+ atom: (el) => {
102
+ this.selectedImage = el && el.tagName === 'IMG' ? el : null;
103
+ this.scheduleOverlayUpdate();
104
+ extra.atom?.(el);
105
+ },
106
+ },
107
+ });
108
+ ed.mount(modelValue);
109
+ ed.setEditable(this.editable);
110
+ this.editor = ed;
111
+ this.lastEmittedHtml = ed.getHTML();
112
+ ed.on('column-hover', () => this.refreshColLine());
113
+ ed.on('cell-selection', () => this.refreshSelectBox());
114
+ }
115
+
116
+ /* ===================== 上报(防抖) ===================== */
117
+
118
+ flushEmit() {
119
+ if (this.emitTimer) {
120
+ clearTimeout(this.emitTimer);
121
+ this.emitTimer = 0;
122
+ }
123
+ if (!this.emitPending) return;
124
+ this.emitPending = false;
125
+ const ed = this.editor;
126
+ if (!ed || ed.destroyed) return;
127
+ this.lastEmittedHtml = ed.getHTML();
128
+ }
129
+
130
+ scheduleEmit() {
131
+ this.emitPending = true;
132
+ if (this.emitTimer) return;
133
+ this.emitTimer = setTimeout(() => this.flushEmit(), EMIT_DEBOUNCE);
134
+ }
135
+
136
+ /** 落地并现读当前 HTML(外部提交前必须走这里,v-model 落后真实输入 ≤250ms) */
137
+ flush() {
138
+ this.flushEmit();
139
+ return this.editor && !this.editor.destroyed ? this.editor.getHTML() : null;
140
+ }
141
+
142
+ /** 外部装载(换文档):直接 setHTML 并重置基线 */
143
+ setHTML(html) {
144
+ const ed = this.editor;
145
+ if (!ed || ed.destroyed) return;
146
+ if (html === ed.getHTML()) return;
147
+ if (this.emitTimer) {
148
+ clearTimeout(this.emitTimer);
149
+ this.emitTimer = 0;
150
+ }
151
+ this.emitPending = false;
152
+ ed.setHTML(html || '');
153
+ this.lastEmittedHtml = ed.getHTML();
154
+ }
155
+
156
+ /* ===================== 编辑性 / 建议模式 ===================== */
157
+
158
+ setEditable(on) {
159
+ this.editable = !!on;
160
+ this.editor?.setEditable(this.editable);
161
+ if (!on) this.hideTableHandles();
162
+ }
163
+
164
+ setSuggesting(on) {
165
+ this.editor?.setSuggesting(!!on);
166
+ }
167
+
168
+ insertImages(files) {
169
+ return this.editor?.insertImageFiles(files);
170
+ }
171
+
172
+ /** 背景层(Word 纸面)重绘入口:分页/页数变化后调用 */
173
+ refreshBackdrop() {
174
+ if (this.backdropRender) this.backdropRender(this.backdropHost);
175
+ }
176
+
177
+ focus() {
178
+ this.editor?.focus();
179
+ }
180
+
181
+ destroy() {
182
+ this.flushEmit();
183
+ this._resizeCleanup?.();
184
+ this.editor?.destroy();
185
+ this.editor = null;
186
+ if (this.resizeRaf) cancelAnimationFrame(this.resizeRaf);
187
+ document.removeEventListener('mousemove', this._mqMove);
188
+ document.removeEventListener('mouseup', this._mqUp);
189
+ this.el.remove();
190
+ }
191
+
192
+ /* ===================== 浮件坐标更新 ===================== */
193
+
194
+ scheduleOverlayUpdate() {
195
+ if (this.resizeRaf) return;
196
+ this.resizeRaf = requestAnimationFrame(() => {
197
+ this.resizeRaf = 0;
198
+ this.updateResizeBox();
199
+ if (this.hoveredTable) this.refreshTableHandles();
200
+ this.refreshColLine();
201
+ this.refreshSelectBox();
202
+ });
203
+ }
204
+
205
+ /** 图片缩放浮层(选中图片时显示) */
206
+ updateResizeBox() {
207
+ const img = this.selectedImage;
208
+ if (!img || !img.isConnected || !this.editable) {
209
+ this.resizeBoxEl.hidden = true;
210
+ return;
211
+ }
212
+ const base = overlayOrigin(this.probeEl);
213
+ const rect = img.getBoundingClientRect();
214
+ const box = {
215
+ left: (rect.left - base.x) / base.z,
216
+ top: (rect.top - base.y) / base.z,
217
+ width: rect.width / base.z,
218
+ height: rect.height / base.z,
219
+ };
220
+ this.resizeBoxEl.hidden = false;
221
+ Object.assign(this.resizeBoxEl.style, {
222
+ left: `${box.left}px`,
223
+ top: `${box.top}px`,
224
+ width: `${box.width}px`,
225
+ height: `${box.height}px`,
226
+ });
227
+ }
228
+
229
+ refreshTableHandles() {
230
+ const table = this.hoveredTable;
231
+ if (!table || !table.isConnected) {
232
+ this.handlesEl.hidden = true;
233
+ return;
234
+ }
235
+ const base = overlayOrigin(this.probeEl);
236
+ const rect = table.getBoundingClientRect();
237
+ const left = (rect.left - base.x) / base.z;
238
+ const top = (rect.top - base.y) / base.z;
239
+ const width = rect.width / base.z;
240
+ const height = rect.height / base.z;
241
+ const col = this.handlesEl.querySelector('.gbd-is-column');
242
+ const row = this.handlesEl.querySelector('.gbd-is-row');
243
+ Object.assign(col.style, { left: `${Math.round(left + width)}px`, top: `${Math.round(top)}px`, height: `${Math.round(height)}px` });
244
+ Object.assign(row.style, { left: `${Math.round(left)}px`, top: `${Math.round(top + height)}px`, width: `${Math.round(width)}px` });
245
+ this.handlesEl.hidden = false;
246
+ }
247
+
248
+ hideTableHandles() {
249
+ this.hoveredTable = null;
250
+ this.handlesEl.hidden = true;
251
+ }
252
+
253
+ refreshColLine() {
254
+ const ed = this.editor;
255
+ const info = ed && !ed.destroyed ? ed.columnBoundaryBox() : null;
256
+ if (!info) {
257
+ this.colLineEl.hidden = true;
258
+ return;
259
+ }
260
+ const base = overlayOrigin(this.probeEl);
261
+ this.colLineEl.hidden = false;
262
+ Object.assign(this.colLineEl.style, {
263
+ left: `${Math.round((info.boundaryX - base.x) / base.z)}px`,
264
+ top: `${Math.round((info.top - base.y) / base.z)}px`,
265
+ height: `${Math.round(info.height / base.z)}px`,
266
+ });
267
+ }
268
+
269
+ refreshSelectBox() {
270
+ const ed = this.editor;
271
+ const geo = ed && !ed.destroyed ? ed.cellSelectionBox() : null;
272
+ if (!geo) {
273
+ this.selectBoxEl.hidden = true;
274
+ return;
275
+ }
276
+ const base = overlayOrigin(this.probeEl);
277
+ this.selectBoxEl.hidden = false;
278
+ Object.assign(this.selectBoxEl.style, {
279
+ left: `${Math.round((geo.left - base.x) / base.z)}px`,
280
+ top: `${Math.round((geo.top - base.y) / base.z)}px`,
281
+ width: `${Math.round(geo.width / base.z)}px`,
282
+ height: `${Math.round(geo.height / base.z)}px`,
283
+ });
284
+ }
285
+
286
+ /* ===================== 鼠标交互 ===================== */
287
+
288
+ bindSurface() {
289
+ const s = this.scrollEl;
290
+ s.addEventListener('scroll', () => this.scheduleOverlayUpdate());
291
+ s.addEventListener('mousemove', (e) => this.onSurfaceMouseMove(e));
292
+ s.addEventListener('mouseleave', () => this.hideTableHandles());
293
+ s.addEventListener('mousedown', () => this.onSurfaceMouseDownCapture(), true);
294
+ s.addEventListener('mousedown', (e) => this.onBlankMouseDown(e));
295
+ s.addEventListener('click', (e) => this.onContentClick(e));
296
+ s.addEventListener('dblclick', (e) => this.onContentDblClick(e));
297
+ this.resizeBoxEl.querySelector('.re-zoom-btn').addEventListener('mousedown', (e) => e.preventDefault());
298
+ this.resizeBoxEl.querySelector('.re-zoom-btn').addEventListener('click', () => this.zoomSelected());
299
+ this.resizeBoxEl.querySelectorAll('.re-resize-handle').forEach((h) => {
300
+ h.addEventListener('mousedown', (e) => this.startResize(h.dataset.dir, e));
301
+ });
302
+ this.handlesEl.querySelector('.gbd-is-column').addEventListener('mousedown', (e) => this.startEdgeGesture('column', e));
303
+ this.handlesEl.querySelector('.gbd-is-row').addEventListener('mousedown', (e) => this.startEdgeGesture('row', e));
304
+ this.viewerEl.addEventListener('click', () => this.closePreview());
305
+ }
306
+
307
+ onSurfaceMouseMove(event) {
308
+ if (!this.editable) return;
309
+ const target = event.target;
310
+ if (!(target instanceof Element)) return;
311
+ if (target.closest('.re-table-handles')) return;
312
+ const table = target.closest('table');
313
+ if (!table || !this.contentEl.contains(table)) {
314
+ if (this.hoveredTable) this.hideTableHandles();
315
+ return;
316
+ }
317
+ this.hoveredTable = table;
318
+ this.refreshTableHandles();
319
+ }
320
+
321
+ startEdgeGesture(axis, event) {
322
+ const ed = this.editor;
323
+ const table = this.hoveredTable;
324
+ if (!ed || !table || !table.isConnected) return;
325
+ event.preventDefault();
326
+ ed.root.focus({ preventScroll: true });
327
+ ed.beginEdgeGesture(event, table, axis);
328
+ }
329
+
330
+ onBlankMouseDown(event) {
331
+ const ed = this.editor;
332
+ const root = this.contentEl;
333
+ if (event.button !== 0 || !ed || !this.editable) return;
334
+ if (event.target.closest?.('.re-table-handles, .re-resize-handle, .re-zoom-btn, .re-table-select, .re-col-line')) return;
335
+ if (event.target !== root && root.contains(event.target)) return;
336
+ event.preventDefault();
337
+ this.mqStart = { x: event.clientX, y: event.clientY };
338
+ this.mqDragged = false;
339
+ this._mqMove = (e) => this.onMarqueeMove(e);
340
+ this._mqUp = () => this.onMarqueeUp();
341
+ document.addEventListener('mousemove', this._mqMove);
342
+ document.addEventListener('mouseup', this._mqUp);
343
+ }
344
+
345
+ onMarqueeMove(event) {
346
+ if (!this.mqStart) return;
347
+ if (!this.mqDragged && Math.abs(event.clientX - this.mqStart.x) + Math.abs(event.clientY - this.mqStart.y) < MARQUEE_THRESHOLD) return;
348
+ const ed = this.editor;
349
+ if (!ed || ed.destroyed) return;
350
+ this.mqDragged = true;
351
+ ed.blockMarqueeActive = true;
352
+ ed.clearBlockSelection();
353
+ const client = rectBetween(this.mqStart, { x: event.clientX, y: event.clientY });
354
+ ed.marqueeSelect(client);
355
+ this.marqueeEl.hidden = false;
356
+ Object.assign(this.marqueeEl.style, toLocalRect(client, this.probeEl));
357
+ }
358
+
359
+ onMarqueeUp() {
360
+ document.removeEventListener('mousemove', this._mqMove);
361
+ document.removeEventListener('mouseup', this._mqUp);
362
+ const ed = this.editor;
363
+ if (!this.mqDragged && ed && !ed.destroyed && this.mqStart) ed.focusAt(this.mqStart.x, this.mqStart.y);
364
+ this.marqueeEl.hidden = true;
365
+ this.mqStart = null;
366
+ this.mqDragged = false;
367
+ }
368
+
369
+ onSurfaceMouseDownCapture() {
370
+ const ed = this.editor;
371
+ if (ed && ed.blockMarqueeActive) {
372
+ ed.blockMarqueeActive = false;
373
+ ed.clearBlockSelection();
374
+ }
375
+ }
376
+
377
+ /* ===================== 图片放大 ===================== */
378
+
379
+ onContentClick(event) {
380
+ if (this.editable) return;
381
+ const img = event.target.closest?.('img');
382
+ if (img) this.openPreview(img);
383
+ }
384
+
385
+ onContentDblClick(event) {
386
+ if (!this.editable) return;
387
+ const img = event.target.closest?.('img');
388
+ if (!img) return;
389
+ event.preventDefault();
390
+ this.openPreview(img);
391
+ }
392
+
393
+ zoomSelected() {
394
+ if (this.selectedImage) this.openPreview(this.selectedImage);
395
+ }
396
+
397
+ openPreview(img) {
398
+ const viewer = this.viewerEl;
399
+ const target = viewer.querySelector('img');
400
+ target.src = img.src;
401
+ viewer.hidden = false;
402
+ }
403
+
404
+ closePreview() {
405
+ this.viewerEl.hidden = true;
406
+ if (this.editable) requestAnimationFrame(() => this.editor?.focus());
407
+ }
408
+
409
+ startResize(direction, event) {
410
+ const img = this.selectedImage;
411
+ const ed = this.editor;
412
+ if (!img || !ed) return;
413
+ event.preventDefault();
414
+ const rect = img.getBoundingClientRect();
415
+ const ratio = rect.width / Math.max(1, rect.height);
416
+ const scale = overlayOrigin(this.probeEl).z;
417
+ const start = { x: event.clientX, width: rect.width };
418
+ const horizontal = direction.endsWith('left') ? -1 : 1;
419
+ let begun = false;
420
+ const onMove = (moveEvent) => {
421
+ if (!beginEditable()) return;
422
+ const width = Math.max(MIN_IMAGE_WIDTH, Math.round((start.width + (moveEvent.clientX - start.x) * horizontal) / scale));
423
+ const height = Math.max(MIN_IMAGE_HEIGHT, Math.round(width / ratio));
424
+ // [坑] 逐帧 run 会灌满历史栈:拖拽只在首次移动时开一步,后续帧合入同一条撤销记录
425
+ ed.run(() => {
426
+ img.setAttribute('width', String(width));
427
+ img.setAttribute('height', String(height));
428
+ }, { coalesce: begun });
429
+ begun = true;
430
+ this.scheduleOverlayUpdate();
431
+ };
432
+ const beginEditable = () => {
433
+ if (!img.isConnected || ed.destroyed) {
434
+ onUp();
435
+ return false;
436
+ }
437
+ return true;
438
+ };
439
+ const onUp = () => {
440
+ this._resizeCleanup = null;
441
+ document.removeEventListener('mousemove', onMove);
442
+ document.removeEventListener('mouseup', onUp);
443
+ };
444
+ this._resizeCleanup = onUp;
445
+ document.addEventListener('mousemove', onMove);
446
+ document.addEventListener('mouseup', onUp);
447
+ }
448
+ }
449
+
450
+ function rectBetween(a, b) {
451
+ return {
452
+ left: Math.min(a.x, b.x),
453
+ top: Math.min(a.y, b.y),
454
+ right: Math.max(a.x, b.x),
455
+ bottom: Math.max(a.y, b.y),
456
+ width: Math.abs(a.x - b.x),
457
+ height: Math.abs(a.y - b.y),
458
+ };
459
+ }
460
+
461
+ function toLocalRect(client, probe) {
462
+ const base = overlayOrigin(probe);
463
+ return {
464
+ left: `${(client.left - base.x) / base.z}px`,
465
+ top: `${(client.top - base.y) / base.z}px`,
466
+ width: `${client.width / base.z}px`,
467
+ height: `${client.height / base.z}px`,
468
+ };
469
+ }
@@ -0,0 +1,69 @@
1
+ // [文件] gbdocx·ColorPanel —— 文字颜色/底色色板(预设格里 + 系统色轮 + Hex + 清除)
2
+ import { openPopup } from './popup.js';
3
+
4
+ /** 打开色板;onPick(color) 回调(清除传空串) */
5
+ export function openColorPanel({ anchor, colors, value, title, columns = 10, onPick }) {
6
+ const box = document.createElement('div');
7
+ box.className = 'gbd-color';
8
+ const head = document.createElement('div');
9
+ head.className = 'gbd-color__title';
10
+ head.textContent = title || '颜色';
11
+ box.appendChild(head);
12
+
13
+ const grid = document.createElement('div');
14
+ grid.className = 'gbd-color__grid';
15
+ grid.style.gridTemplateColumns = `repeat(${columns}, 18px)`;
16
+ for (const color of colors) {
17
+ const sw = document.createElement('button');
18
+ sw.type = 'button';
19
+ sw.className = 'gbd-color__swatch';
20
+ sw.style.background = color;
21
+ if (value && value.toLowerCase() === color.toLowerCase()) sw.classList.add('gbd-is-active');
22
+ sw.title = color;
23
+ sw.addEventListener('click', () => {
24
+ handle.close();
25
+ onPick(color);
26
+ });
27
+ grid.appendChild(sw);
28
+ }
29
+ box.appendChild(grid);
30
+
31
+ const custom = document.createElement('div');
32
+ custom.className = 'gbd-color__custom';
33
+ const wheel = document.createElement('input');
34
+ wheel.type = 'color';
35
+ wheel.value = /^#[0-9a-f]{6}$/i.test(value || '') ? value : '#000000';
36
+ const hex = document.createElement('input');
37
+ hex.type = 'text';
38
+ hex.className = 'gbd-color__hex';
39
+ hex.value = value || '';
40
+ hex.placeholder = '#rrggbb';
41
+ wheel.addEventListener('input', () => { hex.value = wheel.value; });
42
+ custom.append(wheel, hex);
43
+ box.appendChild(custom);
44
+
45
+ const actions = document.createElement('div');
46
+ actions.className = 'gbd-color__actions';
47
+ const apply = document.createElement('button');
48
+ apply.type = 'button';
49
+ apply.className = 'gbd-btn gbd-btn--primary';
50
+ apply.textContent = '应用';
51
+ const clear = document.createElement('button');
52
+ clear.type = 'button';
53
+ clear.className = 'gbd-btn';
54
+ clear.textContent = '清除';
55
+ apply.addEventListener('click', () => {
56
+ const v = (hex.value || '').trim();
57
+ handle.close();
58
+ onPick(/^#?[0-9a-f]{6}$/i.test(v) ? (v.startsWith('#') ? v : `#${v}`) : '');
59
+ });
60
+ clear.addEventListener('click', () => {
61
+ handle.close();
62
+ onPick('');
63
+ });
64
+ actions.append(apply, clear);
65
+ box.appendChild(actions);
66
+
67
+ const handle = openPopup({ anchor, content: box, placement: 'bottom-start', className: 'gbd-popup--color' });
68
+ return handle;
69
+ }
@@ -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
+ }