@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,174 @@
1
+ // [文件] web·editor-kernel·engine·tableOps —— 表格上下文 / 动作 / 格间剪贴板:菜单三态结构与执行体都靠它拿数据(混入 EditorCore.prototype)
2
+ // [设计] 表格动作的 DOM 改动即命令本体,历史与上报由 exec 统一负责;剪贴板读写归 core/utils/clipboard【强制通道】,内核不认识系统剪贴板。
3
+ import { copyRich, copyText, readRich, pasteText } from '../../util/clipboard.js';
4
+ import { notify } from '../../notify.js';
5
+ import { cleanPastedHtml } from '../pasteClean.js';
6
+ import * as D from './dom.js';
7
+ import * as T from './tables.js';
8
+ import * as C from './tableClip.js';
9
+
10
+ /** 表格上下文 / 动作 / 剪贴板方法(装配到 EditorCore.prototype;this 为内核实例) */
11
+ export const tableOpsMethods = {
12
+ cellAt(x, y) {
13
+ const cell = T.cellAtCoords(this.root, x, y);
14
+ this.contextCell = cell;
15
+ return cell;
16
+ },
17
+
18
+ /** 右键落在表格里 */
19
+ isInsideTable(node) {
20
+ return !!node && !!D.tableOf(node, this.root);
21
+ },
22
+
23
+ /** 表格动作上下文:框选区域优先,其次右键命中的单元格,最后光标 */
24
+ tableContext() {
25
+ const rect = this.cellRect?.rect || null;
26
+ // 右键缓存只在元素仍属于当前文档时有效:换文档(打开/新建/导入)后旧引用会脱离 DOM,
27
+ // 继续把它传给内核会让所有表格动作因「取不到上下文」静默失败
28
+ const cell = this.contextCell && this.contextCell.isConnected && this.root.contains(this.contextCell) ? this.contextCell : null;
29
+ return T.tableContext(this.root, cell, rect);
30
+ },
31
+
32
+ canTableAction(action) {
33
+ return T.canTableAction(this.tableContext(), action);
34
+ },
35
+
36
+ isTableActionActive(action) {
37
+ return T.isTableActionActive(this.tableContext(), action);
38
+ },
39
+
40
+ /**
41
+ * 表格动作:DOM 改动即命令本体,历史与上报由 exec 统一负责
42
+ * @param {string} action ACTIONS 键(菜单描述符见 tableMenu.js 的 tableMenuModel)
43
+ * @param {{rows?:number, cols?:number, value?:string}} [payload] 数量 / 带值动作的值
44
+ */
45
+ runTableAction(action, payload) {
46
+ // 「全选」不是网格变换,而是把单格上下文升级为整表框选:交给持久化选区的方法,
47
+ // 内核动作表里的 selectAll 只改了临时 ctx,菜单关闭后选区就丢了
48
+ if (action === 'selectAll') return this.selectAllCells();
49
+ const ctx = this.tableContext();
50
+ if (!ctx) return false;
51
+ const changed = T.runTableAction(ctx, action, payload);
52
+ // 行列增删 / 合并会挪动网格:按新网格重画框选区(几何落点由宿主浮层承担)
53
+ if (changed) this.repaintCellSelection();
54
+ return changed;
55
+ },
56
+
57
+ /**
58
+ * 动作后按当前网格重画框选区(行列增删与合并会让旧矩形落到错位的格子上)
59
+ * @returns {{r1:number,c1:number,r2:number,c2:number}|null} 实际落地的区域(无选区 / 表已脱文档为 null)
60
+ * [设计] 动作会改动网格尺寸,重画必须按裁剪后的 rect 记账:差一格的旧 rect 会让覆盖层画到选框外面去
61
+ */
62
+ repaintCellSelection() {
63
+ const sel = this.cellRect;
64
+ if (!sel?.table?.isConnected) return null;
65
+ const grid = T.toGrid(sel.table);
66
+ const rect = {
67
+ r1: Math.min(sel.rect.r1, Math.max(0, grid.height - 1)),
68
+ c1: Math.min(sel.rect.c1, Math.max(0, grid.width - 1)),
69
+ r2: Math.min(sel.rect.r2, Math.max(0, grid.height - 1)),
70
+ c2: Math.min(sel.rect.c2, Math.max(0, grid.width - 1)),
71
+ };
72
+ this.cellRect = { table: sel.table, rect };
73
+ // [坑] 表头切换会把命中格换成另一种标签、行列增删会让旧引用移位:不把它挪回现格,
74
+ // 紧接着的第二个菜单动作会因为「取不到表格上下文」整条静默失效(表现为点第二下没反应)
75
+ const anchor = grid.matrix[rect.r1]?.[rect.c1];
76
+ if (anchor?.el?.isConnected) this.contextCell = anchor.el;
77
+ this.notifyCellSelection();
78
+ return rect;
79
+ },
80
+
81
+ /** 全选表格单元格(菜单「选择整个表格」):转成框选态,后续合并/删除按整表走 */
82
+ selectAllCells() {
83
+ const ctx = this.tableContext();
84
+ return ctx ? this.selectTable(ctx.table) : false;
85
+ },
86
+
87
+ /**
88
+ * 表格菜单的统一执行入口(消费方只按描述符的 op 派发,不认识动作表)
89
+ * @param {'table'|'clip'} op 描述符的 op
90
+ * @param {string} key 动作键
91
+ * @param {{rows?:number, cols?:number, value?:string}} [payload]
92
+ * @returns {boolean|Promise<boolean>} 表格动作同步返回;剪贴板动作返回 Promise(内部已兜住全部失败路径)
93
+ */
94
+ runTableOp(op, key, payload) {
95
+ if (op !== 'clip') return this.exec('tableAction', key, payload);
96
+ if (key === 'cut') return this.cutTable();
97
+ if (key === 'paste' || key === 'pasteKeepFormat' || key === 'pasteText') return this.pasteTable(key);
98
+ return this.copyTable(key);
99
+ },
100
+
101
+ /** 命中格 / 框选区的复制数据(TSV 文本 + `<table>` HTML);不在表格内返回 null */
102
+ tableClipData() {
103
+ const ctx = this.tableContext();
104
+ return ctx ? C.copySelection(ctx) : null;
105
+ },
106
+
107
+ /**
108
+ * 复制(kind:copy=格内文字选区优先、否则整片区域 / copyRich=区域富文本 / copyText=区域纯文本)
109
+ * [设计] 与思源同义:有文字选区时「复制」交回浏览器按原生选区复制;没有才按单元格区域走,
110
+ * 否则用户在格内选半句话点「复制」会把整格结构也带出去
111
+ * @returns {Promise<boolean>}
112
+ */
113
+ async copyTable(kind = 'copy') {
114
+ if (kind === 'copy') {
115
+ const range = D.rangeOf(this.root);
116
+ // execCommand 在无焦点 / 被策略拦下时会抛,退回区域复制即可,不能把异常漏成未处理拒绝
117
+ if (range && !range.collapsed) {
118
+ try {
119
+ if (document.execCommand?.('copy')) return true;
120
+ } catch {
121
+ /* 退化到下面的区域复制 */
122
+ }
123
+ }
124
+ }
125
+ const data = this.tableClipData();
126
+ if (!data) return false;
127
+ return kind === 'copyText' ? copyText(data.text) : copyRich(data.text, data.html);
128
+ },
129
+
130
+ /** 剪切 = 复制 + 清空选区(清空走 exec 提交一次可撤销历史) */
131
+ async cutTable() {
132
+ if (!(await this.copyTable('copy'))) return false;
133
+ return this.exec('tableAction', 'clearCell');
134
+ },
135
+
136
+ /**
137
+ * 粘贴(kind:paste=清洗来源格式 / pasteKeepFormat=保留源格式 / pasteText=只取纯文本)
138
+ * [设计] 剪贴板读取被拒(非安全上下文 / 无权限)是用户可感知的失败,提示只此一处,
139
+ * 禁各消费方再包一层 try/catch
140
+ * @returns {Promise<boolean>}
141
+ */
142
+ async pasteTable(kind = 'paste') {
143
+ if (!this.tableContext()) return false;
144
+ let payload;
145
+ try {
146
+ payload = kind === 'pasteText' ? { text: await pasteText() } : await readRich();
147
+ } catch (error) {
148
+ notify.error(error?.message || '无法读取剪贴板');
149
+ return false;
150
+ }
151
+ return this.pasteTableData(payload, kind);
152
+ },
153
+
154
+ /** 用一段已到手的剪贴板数据粘进表格(Ctrl+V 通路:数据来自 paste 事件,不碰系统剪贴板) */
155
+ pasteTableData(payload, kind = 'paste') {
156
+ const ctx = this.tableContext();
157
+ if (!ctx) return false;
158
+ const html = kind === 'pasteKeepFormat' ? payload?.html : cleanPastedHtml(payload?.html || '');
159
+ const rows = C.clipboardRows({ text: payload?.text || '', html: html || '' });
160
+ if (!rows?.length) return false;
161
+ const changed = this.run(() => C.pasteCells(ctx, rows));
162
+ if (changed) this.repaintCellSelection();
163
+ return changed;
164
+ },
165
+
166
+ /**
167
+ * 框选态落地:把「选区变了」抛给宿主(宿主浮层按 cellSelectionBox() 重画一层矩形)
168
+ * [设计] 内核不再给格子挂任何类名/装饰:框选高亮是宿主浮层里的一层矩形,正文 DOM 一个字节都不动;
169
+ * 与列宽提示线(column-hover)同一套上报口径,宿主收到后按内容坐标重画。
170
+ */
171
+ notifyCellSelection() {
172
+ this.emit('cell-selection');
173
+ },
174
+ };