@a200888213/gbdocx 0.5.1 → 0.6.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 (80) hide show
  1. package/README.md +215 -207
  2. package/index.html +3 -2
  3. package/package.json +48 -48
  4. package/serve.mjs +3 -2
  5. package/src/app/file-io.js +122 -122
  6. package/src/app/main.js +9 -9
  7. package/src/app/word/catalog.js +52 -52
  8. package/src/app/word/comments.js +112 -112
  9. package/src/app/word/doc.js +113 -113
  10. package/src/app/word/edit.js +72 -72
  11. package/src/app/word/file.js +30 -1
  12. package/src/app/word/hf.js +76 -76
  13. package/src/app/word/layout.js +155 -155
  14. package/src/app/word/notes.js +110 -110
  15. package/src/app/word/shared.js +22 -22
  16. package/src/app/word/view.js +22 -6
  17. package/src/app/word-app.js +486 -467
  18. package/src/app/word-sheets.js +101 -101
  19. package/src/kernel/docx/codec/blocks.js +123 -123
  20. package/src/kernel/docx/codec/carry.js +129 -129
  21. package/src/kernel/docx/codec/comments.js +25 -25
  22. package/src/kernel/docx/codec/docx.js +198 -198
  23. package/src/kernel/docx/codec/docxStyles.js +177 -177
  24. package/src/kernel/docx/codec/fonts.js +195 -195
  25. package/src/kernel/docx/codec/helpers.js +161 -161
  26. package/src/kernel/docx/codec/hfHtml.js +152 -152
  27. package/src/kernel/docx/codec/html.js +739 -739
  28. package/src/kernel/docx/codec/losses.js +420 -420
  29. package/src/kernel/docx/codec/markdown.js +170 -170
  30. package/src/kernel/docx/codec/parts.js +249 -249
  31. package/src/kernel/docx/codec/read.js +603 -603
  32. package/src/kernel/docx/codec/render.js +1156 -1156
  33. package/src/kernel/docx/codec/roundtrip.js +236 -236
  34. package/src/kernel/docx/codec/write.js +833 -833
  35. package/src/kernel/docx/comments.js +14 -14
  36. package/src/kernel/docx/pageGeometry.js +123 -123
  37. package/src/kernel/docx/pagination.js +481 -481
  38. package/src/kernel/docx/watermark.js +45 -45
  39. package/src/kernel/editor/constants.js +8 -8
  40. package/src/kernel/editor/engine/blocks.js +594 -594
  41. package/src/kernel/editor/engine/blocksEnter.js +109 -109
  42. package/src/kernel/editor/engine/blocksInsert.js +254 -254
  43. package/src/kernel/editor/engine/blocksStructure.js +226 -226
  44. package/src/kernel/editor/engine/commands.js +181 -181
  45. package/src/kernel/editor/engine/dom.js +646 -646
  46. package/src/kernel/editor/engine/history.js +149 -149
  47. package/src/kernel/editor/engine/inline.js +656 -656
  48. package/src/kernel/editor/engine/input.js +554 -554
  49. package/src/kernel/editor/engine/interaction.js +137 -137
  50. package/src/kernel/editor/engine/pointer.js +39 -39
  51. package/src/kernel/editor/engine/revision.js +138 -138
  52. package/src/kernel/editor/engine/schema.js +417 -417
  53. package/src/kernel/editor/engine/selection.js +119 -119
  54. package/src/kernel/editor/engine/state.js +118 -118
  55. package/src/kernel/editor/engine/tableClip.js +154 -154
  56. package/src/kernel/editor/engine/tableGrid.js +55 -55
  57. package/src/kernel/editor/engine/tableInteraction.js +388 -388
  58. package/src/kernel/editor/engine/tableMenu.js +250 -250
  59. package/src/kernel/editor/engine/tableOps.js +174 -174
  60. package/src/kernel/editor/engine/tables.js +732 -732
  61. package/src/kernel/editor/overlayGeometry.js +20 -20
  62. package/src/kernel/editor/utils.js +48 -48
  63. package/src/kernel/keyboard.js +114 -114
  64. package/src/kernel/textStyles.js +84 -84
  65. package/src/kernel/util/imageFile.js +91 -91
  66. package/src/kernel/util/shortcutKey.js +33 -33
  67. package/src/surface/editor-surface.js +600 -600
  68. package/src/ui/color-panel.js +69 -69
  69. package/src/ui/context-menu.js +133 -133
  70. package/src/ui/find-bar.js +151 -151
  71. package/src/ui/icons.js +137 -135
  72. package/src/ui/menubar.js +28 -28
  73. package/src/ui/panels.js +115 -115
  74. package/src/ui/prompt.js +89 -89
  75. package/src/ui/ribbon.js +485 -485
  76. package/src/ui/special-chars.js +39 -39
  77. package/src/ui/statusbar.js +81 -81
  78. package/src/ui/toolbar.js +32 -32
  79. package/styles/editor-content.css +440 -440
  80. package/styles/tokens.css +154 -154
@@ -1,250 +1,250 @@
1
- // [文件] web·editor-kernel·engine·tableMenu —— 表格右键菜单的结构与文案(唯一数据源)+ 描述符→菜单条目映射
2
- // [通用:能力] tableMenuModel / tableMenuItems —— 富文本表格菜单与 Word 表格菜单 / 菜单栏共用同一套结构,
3
- // 禁各写一份条目表(三处只差「谁来执行」:富文本直接问内核,Word 经注入的 io)。
4
- // [设计] 描述符只写结构(op/group/title/icon/shortcut/input/danger/toggle),不写执行:op='table' 走内核表格动作(tables.js 的 ACTIONS 键)、
5
- // 'clip' 走剪贴板动作、'inline' 走向内命令、'menu' 是纯子面板容器。
6
- // group 递增处插分隔线(与 ContextMenu 的 divided 口径一致,树的哪一层都适用)。
7
- // [设计] 三态结构照思源「表格框选菜单」与「内容区右键菜单」两张截图及对应源码分支:
8
- // ① 框选态(选区跨多格)=表格框选菜单:复制族+剪切+粘贴+清空 / 字体效果·背景颜色 / 对齐方式 / 删除行列 / 合并单元格;
9
- // 无插入组、无「更多」、无全选、无删除表格。
10
- // ② 单格·有正文=内容区右键的「有文字内容」分支:复制族+剪切+删除 → 粘贴族 → 全选 → 内联样式组 → 插入 → 删除行列 → 更多 → 删除表格。
11
- // ③ 单格·无正文=同上的「无正文」分支:粘贴族 → 全选 → 插入 → 删除行列 → 更多 → 删除表格。
12
- // [坑] 分态判据必须由传入的表格上下文实算(有无框选 + 命中/选区有无正文),不得按调用入口硬编:
13
- // 空表里若照搬复制组会出现一整组点不动的死项;框选态若照搬插入组/更多则与用户截图不符。
14
-
15
- import { TEXT_COLORS } from '../../textStyles.js';
16
- import { INSERT_LIMIT, isCellBlank } from './tables.js';
17
-
18
- /** 单元格底纹预设(docx 安全:6-hex;写入 td 内联 background-color,docx 双向已支持 shade) */
19
- export const TABLE_SHADE_PRESETS = [
20
- { title: '无填充', value: '' },
21
- { title: '浅灰', value: '#f2f3f5' },
22
- { title: '浅蓝', value: '#dce8ff' },
23
- { title: '浅绿', value: '#d8f0d4' },
24
- { title: '浅黄', value: '#fff3c4' },
25
- { title: '浅红', value: '#ffd6d6' },
26
- ];
27
-
28
- /** 行内标记命令 → 编辑器状态快照字段(勾选位取值,与工具栏同一份 state) */
29
- const INLINE_STATE = {
30
- toggleBold: 'bold',
31
- toggleItalic: 'italic',
32
- toggleUnderline: 'underline',
33
- toggleStrike: 'strike',
34
- toggleCode: 'code',
35
- toggleSuperscript: 'superscript',
36
- toggleSubscript: 'subscript',
37
- };
38
-
39
- /** 「对齐方式」子面板(水平 + 垂直合为一组) */
40
- const TABLE_ALIGN_CHILDREN = [
41
- { op: 'table', key: 'alignLeft', title: '居左', group: 0 },
42
- { op: 'table', key: 'alignCenter', title: '居中', group: 0 },
43
- { op: 'table', key: 'alignRight', title: '居右', group: 0 },
44
- { op: 'table', key: 'alignDefault', title: '使用默认水平对齐', group: 0 },
45
- { divided: true, group: 1 },
46
- { op: 'table', key: 'valignTop', title: '顶部对齐', group: 2 },
47
- { op: 'table', key: 'valignMiddle', title: '垂直居中', group: 2 },
48
- { op: 'table', key: 'valignBottom', title: '底部对齐', group: 2 },
49
- { op: 'table', key: 'valignDefault', title: '使用默认垂直对齐', group: 2 },
50
- ];
51
-
52
- /** 「字体效果」子面板:行内标记开关 + 文字颜色(作用于格内文字选区,命令与工具栏同一套) */
53
- const TABLE_FONT_CHILDREN = [
54
- { op: 'inline', command: 'toggleBold', title: '加粗', toggle: true, icon: 'svg:rte-bold', group: 0 },
55
- { op: 'inline', command: 'toggleItalic', title: '斜体', toggle: true, icon: 'svg:rte-italic', group: 0 },
56
- { op: 'inline', command: 'toggleUnderline', title: '下划线', toggle: true, icon: 'svg:rte-underline', group: 0 },
57
- { op: 'inline', command: 'toggleStrike', title: '删除线', toggle: true, icon: 'svg:rte-strike', group: 0 },
58
- { op: 'inline', command: 'toggleCode', title: '行内代码', toggle: true, icon: 'svg:rte-code', group: 0 },
59
- { op: 'inline', command: 'toggleSuperscript', title: '上标', toggle: true, icon: 'svg:rte-superscript', group: 1 },
60
- { op: 'inline', command: 'toggleSubscript', title: '下标', toggle: true, icon: 'svg:rte-subscript', group: 1 },
61
- { divided: true, group: 2 },
62
- { op: 'inline', command: 'unsetColor', title: '默认颜色', group: 2 },
63
- // [设计] 调色板取 core/constants/textStyles 的唯一一张表,不在表格域另建第二份
64
- ...TEXT_COLORS.map((color) => ({ op: 'inline', command: 'setColor', args: [color], title: color, colorDot: color, group: 2 })),
65
- ];
66
-
67
- /** 「背景颜色」子面板:单元格底纹 */
68
- const TABLE_SHADE_CHILDREN = TABLE_SHADE_PRESETS.map((preset, index) => ({
69
- op: 'table',
70
- key: 'cellShade',
71
- args: [preset.value],
72
- title: preset.title,
73
- colorDot: preset.value || undefined,
74
- group: index,
75
- }));
76
-
77
- /** 「更多」子面板(思源 otherMenus + other2Menus:列宽 / 表头 / 转置 / 行列移动 / 合并拆分) */
78
- const TABLE_MORE_CHILDREN = [
79
- { op: 'table', key: 'resetColumnWidth', title: '使用默认列宽', group: 0, icon: 'svg:rte-resetWidth' },
80
- { op: 'table', key: 'pinHeader', title: '固定表头', group: 1, toggle: true, icon: 'svg:rte-pinHead' },
81
- { op: 'table', key: 'toggleHeaderRow', title: '标题行', group: 1, toggle: true, icon: 'svg:rte-table' },
82
- { op: 'table', key: 'toggleHeaderColumn', title: '标题列', group: 1, toggle: true, icon: 'svg:rte-column' },
83
- { op: 'table', key: 'transpose', title: '行列转置', group: 2, icon: 'svg:rte-transpose' },
84
- { op: 'table', key: 'moveRowUp', title: '向上移', group: 3, icon: 'ArrowUp' },
85
- { op: 'table', key: 'moveRowDown', title: '向下移', group: 3, icon: 'ArrowDown' },
86
- { op: 'table', key: 'moveColumnLeft', title: '向左移', group: 3, icon: 'ArrowLeft' },
87
- { op: 'table', key: 'moveColumnRight', title: '向右移', group: 3, icon: 'ArrowRight' },
88
- // [设计] 合并/拆分只在「更多」里出现(对应思源把 cancelMerged 收进 otherMenus):单格态没有框选,
89
- // 顶层挂一个合并项多半是禁用死项,收进「更多」既保留「拆分已合并格」能力又不占主面。
90
- { op: 'table', key: 'mergeOrSplit', title: '合并 / 拆分单元格', group: 4, icon: 'svg:rte-mergeCells' },
91
- ];
92
-
93
- /** 「字体效果 / 背景颜色」子面板(思源 tableControl 里同属一组) */
94
- const fontItem = (group) => ({ op: 'menu', title: '字体效果', group, children: TABLE_FONT_CHILDREN, icon: 'svg:rte-colorIcon' });
95
- const bgItem = (group) => ({ op: 'menu', title: '背景颜色', group, children: TABLE_SHADE_CHILDREN, icon: 'svg:rte-bgIcon' });
96
-
97
- /** 「对齐方式」子面板(思源 tableControl 里单独一组) */
98
- const alignItem = (group) => ({ op: 'menu', title: '对齐方式', group, children: TABLE_ALIGN_CHILDREN, icon: 'svg:rte-alignLeft' });
99
-
100
- /** 「更多」子面板容器 */
101
- const moreItem = (group) => ({ op: 'menu', title: '更多', group, children: TABLE_MORE_CHILDREN, icon: 'MoreFilled' });
102
-
103
- /** 粘贴族(思源 contentMenu 的 paste / pasteAndKeepSourceFormat / pasteAsPlainText) */
104
- const pasteItems = (group) => [
105
- { op: 'clip', key: 'paste', title: '粘贴', shortcut: 'Ctrl+V', group, icon: 'svg:rte-copy' },
106
- { op: 'clip', key: 'pasteKeepFormat', title: '粘贴并保留源格式', group },
107
- { op: 'clip', key: 'pasteText', title: '粘贴为纯文本', shortcut: 'Ctrl+Shift+V', group },
108
- ];
109
-
110
- /** 全选整表(单格态:转成框选态) */
111
- const selectAllItem = (group) => ({ op: 'table', key: 'selectAll', title: '全选', shortcut: 'Ctrl+A', group, icon: 'svg:rte-table' });
112
-
113
- /** 插入行列(行内数字框;照思源 tableMenu 的 insertMenus) */
114
- const insertItems = (group) => [
115
- { op: 'table', key: 'insertRowBefore', title: '在上方插入', unit: '行', input: 'rows', group, icon: 'svg:rte-rowAbove' },
116
- { op: 'table', key: 'insertRowAfter', title: '在下方插入', unit: '行', input: 'rows', group, icon: 'svg:rte-rowBelow' },
117
- { op: 'table', key: 'insertColumnBefore', title: '在左侧插入', unit: '列', input: 'cols', group, icon: 'svg:rte-colLeft' },
118
- { op: 'table', key: 'insertColumnAfter', title: '在右侧插入', unit: '列', input: 'cols', group, icon: 'svg:rte-colRight' },
119
- ];
120
-
121
- /** 删除行 / 删除列(照思源 tableMenu 的 removeMenus) */
122
- const removeRowColItems = (group) => [
123
- { op: 'table', key: 'deleteRow', title: '删除行', shortcut: 'Ctrl+-', group, icon: 'Delete' },
124
- { op: 'table', key: 'deleteColumn', title: '删除列', shortcut: 'Ctrl+Shift+-', group, icon: 'Delete' },
125
- ];
126
-
127
- /** 删除表格(单格态主面末项;思源内容区菜单无此项,故仅单格态保留以维持删表入口) */
128
- const deleteTableItem = (group) => ({ op: 'table', key: 'deleteTable', title: '删除表格', group, danger: true, icon: 'DeleteFilled' });
129
-
130
- /**
131
- * 表格右键菜单描述符(思源三态结构,唯一数据源)
132
- * @param {object|null} ctx tableContext 产物(含 table / grid / cell / rect)
133
- * @returns {Array<object>} 描述符数组;无上下文返回空数组
134
- */
135
- export function tableMenuModel(ctx) {
136
- if (!ctx) return [];
137
- // ① 框选态:选区跨多格(思源表格框选菜单)
138
- if (hasBoxSelection(ctx.rect)) return boxSelectionMenu();
139
- // ② / ③ 单格态:按命中格有无正文二分(思源内容区右键的两条分支)
140
- return cellHasBody(ctx) ? filledCellMenu() : emptyCellMenu();
141
- }
142
-
143
- /** 框选判据:区域跨行或跨列(思源 tableControl 的 selection.mode='cell' 且 isRectangle()) */
144
- function hasBoxSelection(rect) {
145
- return Boolean(rect) && (rect.r1 !== rect.r2 || rect.c1 !== rect.c2);
146
- }
147
-
148
- /** 单格·无正文(思源 contentMenu 无正文分支:粘贴族 + 全选 + 插入 + 删除 + 更多 + 删除表格) */
149
- function emptyCellMenu() {
150
- return [
151
- ...pasteItems(0),
152
- selectAllItem(1),
153
- ...insertItems(2),
154
- ...removeRowColItems(3),
155
- moreItem(4),
156
- deleteTableItem(5),
157
- ];
158
- }
159
-
160
- /** 单格·有正文(思源 contentMenu 有文字内容分支:复制族+剪切+删除 + 粘贴族 + 全选 + 样式组 + 插入 + 删除 + 更多 + 删除表格) */
161
- function filledCellMenu() {
162
- return [
163
- copyItem(0),
164
- copyRichItem(0),
165
- copyTextItem(0),
166
- { op: 'clip', key: 'cut', title: '剪切', shortcut: 'Ctrl+X', group: 0 },
167
- { op: 'table', key: 'clearCell', title: '删除', group: 0, icon: 'svg:rte-clear' },
168
- ...pasteItems(1),
169
- selectAllItem(2),
170
- fontItem(3),
171
- bgItem(3),
172
- alignItem(4),
173
- ...insertItems(5),
174
- ...removeRowColItems(6),
175
- moreItem(7),
176
- deleteTableItem(8),
177
- ];
178
- }
179
-
180
- /** 框选态(思源 tableControl openMenu cell 分支:复制族+剪切+粘贴+清空 / 样式组 / 对齐 / 删除行列 / 合并单元格) */
181
- function boxSelectionMenu() {
182
- return [
183
- copyItem(0),
184
- copyRichItem(0),
185
- copyTextItem(0),
186
- { op: 'clip', key: 'cut', title: '剪切', shortcut: 'Ctrl+X', group: 0 },
187
- { op: 'clip', key: 'paste', title: '粘贴', shortcut: 'Ctrl+V', group: 0 },
188
- { op: 'table', key: 'clearCell', title: '清空', group: 0, danger: true, icon: 'svg:rte-clear' },
189
- fontItem(1),
190
- bgItem(1),
191
- alignItem(2),
192
- { op: 'table', key: 'deleteRow', title: '删除行', group: 3, icon: 'Delete' },
193
- { op: 'table', key: 'deleteColumn', title: '删除列', group: 3, icon: 'Delete' },
194
- { op: 'table', key: 'mergeOrSplit', title: '合并单元格', group: 4, icon: 'svg:rte-mergeCells' },
195
- ];
196
- }
197
-
198
- const copyItem = (group) => ({ op: 'clip', key: 'copy', title: '复制', shortcut: 'Ctrl+C', group, icon: 'svg:rte-copy' });
199
- const copyRichItem = (group) => ({ op: 'clip', key: 'copyRich', title: '复制富文本/HTML', group });
200
- const copyTextItem = (group) => ({ op: 'clip', key: 'copyText', title: '复制纯文本', group });
201
-
202
- /** 选区 / 命中格里是否有正文:多格框选一律按「有」(用户已显式圈了内容),单格按空格判 */
203
- function cellHasBody(ctx) {
204
- const { rect, grid } = ctx;
205
- if (rect.r1 !== rect.r2 || rect.c1 !== rect.c2) return true;
206
- const cell = ctx.cell || grid.matrix[rect.r1]?.[rect.c1]?.el;
207
- return Boolean(cell) && !isCellBlank(cell);
208
- }
209
-
210
- /**
211
- * 描述符 → ContextMenu 条目(唯一映射,三处消费方共用)
212
- * @param {Array} list 描述符(tableMenuModel 产物,或它的 children)
213
- * @param {{state?:object, can?:Function, active?:Function, run?:Function}} io
214
- * state=编辑器状态快照(行内勾选位);can(item)/active(item)=可用性与勾选;
215
- * run(item, entry, value)=执行体,value 为行内数字框的取值(无输入项时 undefined)
216
- * @returns {Array<object>} ContextMenu 的 items
217
- * [设计] 执行体一律挂在 entry.onClick(e, value),三处消费方(富文本右键 / Word 右键 / Word 菜单栏)因此
218
- * 都不需要 select 回调通道:菜单栏浮层(AppMenuBar)与右键浮层拿到的是同一份可运行的条目。
219
- */
220
- export function tableMenuItems(list, io = {}) {
221
- const { state = null, can = () => true, active = () => false, run = () => {} } = io;
222
- const target = [];
223
- let group = null;
224
- for (const item of list) {
225
- // 裸分隔标记(无 title):只出分隔线
226
- if (!item.title) {
227
- target.push({ divided: true });
228
- continue;
229
- }
230
- const entry = { label: item.title, danger: item.danger || undefined, disabled: !can(item) };
231
- if (item.icon) entry.icon = item.icon;
232
- if (item.colorDot) entry.colorDot = item.colorDot;
233
- if (item.shortcut) entry.shortcut = item.shortcut;
234
- if (group !== null && item.group !== group) entry.divided = true;
235
- group = item.group;
236
- if (item.children) {
237
- entry.children = tableMenuItems(item.children, io);
238
- } else {
239
- if (item.input) {
240
- // 行内输入项:图标 + 文案 + 内联数字框 + 单位字;执行体统一经 onClick,数字框的值作第二参传回
241
- entry.input = { min: 1, max: INSERT_LIMIT, defaultValue: 1, unit: item.unit || '' };
242
- } else if (item.toggle) {
243
- entry.checked = item.command ? Boolean(state?.[INLINE_STATE[item.command]]) : active(item);
244
- }
245
- entry.onClick = (e, value) => run(item, entry, value);
246
- }
247
- target.push(entry);
248
- }
249
- return target;
250
- }
1
+ // [文件] web·editor-kernel·engine·tableMenu —— 表格右键菜单的结构与文案(唯一数据源)+ 描述符→菜单条目映射
2
+ // [通用:能力] tableMenuModel / tableMenuItems —— 富文本表格菜单与 Word 表格菜单 / 菜单栏共用同一套结构,
3
+ // 禁各写一份条目表(三处只差「谁来执行」:富文本直接问内核,Word 经注入的 io)。
4
+ // [设计] 描述符只写结构(op/group/title/icon/shortcut/input/danger/toggle),不写执行:op='table' 走内核表格动作(tables.js 的 ACTIONS 键)、
5
+ // 'clip' 走剪贴板动作、'inline' 走向内命令、'menu' 是纯子面板容器。
6
+ // group 递增处插分隔线(与 ContextMenu 的 divided 口径一致,树的哪一层都适用)。
7
+ // [设计] 三态结构照思源「表格框选菜单」与「内容区右键菜单」两张截图及对应源码分支:
8
+ // ① 框选态(选区跨多格)=表格框选菜单:复制族+剪切+粘贴+清空 / 字体效果·背景颜色 / 对齐方式 / 删除行列 / 合并单元格;
9
+ // 无插入组、无「更多」、无全选、无删除表格。
10
+ // ② 单格·有正文=内容区右键的「有文字内容」分支:复制族+剪切+删除 → 粘贴族 → 全选 → 内联样式组 → 插入 → 删除行列 → 更多 → 删除表格。
11
+ // ③ 单格·无正文=同上的「无正文」分支:粘贴族 → 全选 → 插入 → 删除行列 → 更多 → 删除表格。
12
+ // [坑] 分态判据必须由传入的表格上下文实算(有无框选 + 命中/选区有无正文),不得按调用入口硬编:
13
+ // 空表里若照搬复制组会出现一整组点不动的死项;框选态若照搬插入组/更多则与用户截图不符。
14
+
15
+ import { TEXT_COLORS } from '../../textStyles.js';
16
+ import { INSERT_LIMIT, isCellBlank } from './tables.js';
17
+
18
+ /** 单元格底纹预设(docx 安全:6-hex;写入 td 内联 background-color,docx 双向已支持 shade) */
19
+ export const TABLE_SHADE_PRESETS = [
20
+ { title: '无填充', value: '' },
21
+ { title: '浅灰', value: '#f2f3f5' },
22
+ { title: '浅蓝', value: '#dce8ff' },
23
+ { title: '浅绿', value: '#d8f0d4' },
24
+ { title: '浅黄', value: '#fff3c4' },
25
+ { title: '浅红', value: '#ffd6d6' },
26
+ ];
27
+
28
+ /** 行内标记命令 → 编辑器状态快照字段(勾选位取值,与工具栏同一份 state) */
29
+ const INLINE_STATE = {
30
+ toggleBold: 'bold',
31
+ toggleItalic: 'italic',
32
+ toggleUnderline: 'underline',
33
+ toggleStrike: 'strike',
34
+ toggleCode: 'code',
35
+ toggleSuperscript: 'superscript',
36
+ toggleSubscript: 'subscript',
37
+ };
38
+
39
+ /** 「对齐方式」子面板(水平 + 垂直合为一组) */
40
+ const TABLE_ALIGN_CHILDREN = [
41
+ { op: 'table', key: 'alignLeft', title: '居左', group: 0 },
42
+ { op: 'table', key: 'alignCenter', title: '居中', group: 0 },
43
+ { op: 'table', key: 'alignRight', title: '居右', group: 0 },
44
+ { op: 'table', key: 'alignDefault', title: '使用默认水平对齐', group: 0 },
45
+ { divided: true, group: 1 },
46
+ { op: 'table', key: 'valignTop', title: '顶部对齐', group: 2 },
47
+ { op: 'table', key: 'valignMiddle', title: '垂直居中', group: 2 },
48
+ { op: 'table', key: 'valignBottom', title: '底部对齐', group: 2 },
49
+ { op: 'table', key: 'valignDefault', title: '使用默认垂直对齐', group: 2 },
50
+ ];
51
+
52
+ /** 「字体效果」子面板:行内标记开关 + 文字颜色(作用于格内文字选区,命令与工具栏同一套) */
53
+ const TABLE_FONT_CHILDREN = [
54
+ { op: 'inline', command: 'toggleBold', title: '加粗', toggle: true, icon: 'svg:rte-bold', group: 0 },
55
+ { op: 'inline', command: 'toggleItalic', title: '斜体', toggle: true, icon: 'svg:rte-italic', group: 0 },
56
+ { op: 'inline', command: 'toggleUnderline', title: '下划线', toggle: true, icon: 'svg:rte-underline', group: 0 },
57
+ { op: 'inline', command: 'toggleStrike', title: '删除线', toggle: true, icon: 'svg:rte-strike', group: 0 },
58
+ { op: 'inline', command: 'toggleCode', title: '行内代码', toggle: true, icon: 'svg:rte-code', group: 0 },
59
+ { op: 'inline', command: 'toggleSuperscript', title: '上标', toggle: true, icon: 'svg:rte-superscript', group: 1 },
60
+ { op: 'inline', command: 'toggleSubscript', title: '下标', toggle: true, icon: 'svg:rte-subscript', group: 1 },
61
+ { divided: true, group: 2 },
62
+ { op: 'inline', command: 'unsetColor', title: '默认颜色', group: 2 },
63
+ // [设计] 调色板取 core/constants/textStyles 的唯一一张表,不在表格域另建第二份
64
+ ...TEXT_COLORS.map((color) => ({ op: 'inline', command: 'setColor', args: [color], title: color, colorDot: color, group: 2 })),
65
+ ];
66
+
67
+ /** 「背景颜色」子面板:单元格底纹 */
68
+ const TABLE_SHADE_CHILDREN = TABLE_SHADE_PRESETS.map((preset, index) => ({
69
+ op: 'table',
70
+ key: 'cellShade',
71
+ args: [preset.value],
72
+ title: preset.title,
73
+ colorDot: preset.value || undefined,
74
+ group: index,
75
+ }));
76
+
77
+ /** 「更多」子面板(思源 otherMenus + other2Menus:列宽 / 表头 / 转置 / 行列移动 / 合并拆分) */
78
+ const TABLE_MORE_CHILDREN = [
79
+ { op: 'table', key: 'resetColumnWidth', title: '使用默认列宽', group: 0, icon: 'svg:rte-resetWidth' },
80
+ { op: 'table', key: 'pinHeader', title: '固定表头', group: 1, toggle: true, icon: 'svg:rte-pinHead' },
81
+ { op: 'table', key: 'toggleHeaderRow', title: '标题行', group: 1, toggle: true, icon: 'svg:rte-table' },
82
+ { op: 'table', key: 'toggleHeaderColumn', title: '标题列', group: 1, toggle: true, icon: 'svg:rte-column' },
83
+ { op: 'table', key: 'transpose', title: '行列转置', group: 2, icon: 'svg:rte-transpose' },
84
+ { op: 'table', key: 'moveRowUp', title: '向上移', group: 3, icon: 'ArrowUp' },
85
+ { op: 'table', key: 'moveRowDown', title: '向下移', group: 3, icon: 'ArrowDown' },
86
+ { op: 'table', key: 'moveColumnLeft', title: '向左移', group: 3, icon: 'ArrowLeft' },
87
+ { op: 'table', key: 'moveColumnRight', title: '向右移', group: 3, icon: 'ArrowRight' },
88
+ // [设计] 合并/拆分只在「更多」里出现(对应思源把 cancelMerged 收进 otherMenus):单格态没有框选,
89
+ // 顶层挂一个合并项多半是禁用死项,收进「更多」既保留「拆分已合并格」能力又不占主面。
90
+ { op: 'table', key: 'mergeOrSplit', title: '合并 / 拆分单元格', group: 4, icon: 'svg:rte-mergeCells' },
91
+ ];
92
+
93
+ /** 「字体效果 / 背景颜色」子面板(思源 tableControl 里同属一组) */
94
+ const fontItem = (group) => ({ op: 'menu', title: '字体效果', group, children: TABLE_FONT_CHILDREN, icon: 'svg:rte-colorIcon' });
95
+ const bgItem = (group) => ({ op: 'menu', title: '背景颜色', group, children: TABLE_SHADE_CHILDREN, icon: 'svg:rte-bgIcon' });
96
+
97
+ /** 「对齐方式」子面板(思源 tableControl 里单独一组) */
98
+ const alignItem = (group) => ({ op: 'menu', title: '对齐方式', group, children: TABLE_ALIGN_CHILDREN, icon: 'svg:rte-alignLeft' });
99
+
100
+ /** 「更多」子面板容器 */
101
+ const moreItem = (group) => ({ op: 'menu', title: '更多', group, children: TABLE_MORE_CHILDREN, icon: 'MoreFilled' });
102
+
103
+ /** 粘贴族(思源 contentMenu 的 paste / pasteAndKeepSourceFormat / pasteAsPlainText) */
104
+ const pasteItems = (group) => [
105
+ { op: 'clip', key: 'paste', title: '粘贴', shortcut: 'Ctrl+V', group, icon: 'svg:rte-copy' },
106
+ { op: 'clip', key: 'pasteKeepFormat', title: '粘贴并保留源格式', group },
107
+ { op: 'clip', key: 'pasteText', title: '粘贴为纯文本', shortcut: 'Ctrl+Shift+V', group },
108
+ ];
109
+
110
+ /** 全选整表(单格态:转成框选态) */
111
+ const selectAllItem = (group) => ({ op: 'table', key: 'selectAll', title: '全选', shortcut: 'Ctrl+A', group, icon: 'svg:rte-table' });
112
+
113
+ /** 插入行列(行内数字框;照思源 tableMenu 的 insertMenus) */
114
+ const insertItems = (group) => [
115
+ { op: 'table', key: 'insertRowBefore', title: '在上方插入', unit: '行', input: 'rows', group, icon: 'svg:rte-rowAbove' },
116
+ { op: 'table', key: 'insertRowAfter', title: '在下方插入', unit: '行', input: 'rows', group, icon: 'svg:rte-rowBelow' },
117
+ { op: 'table', key: 'insertColumnBefore', title: '在左侧插入', unit: '列', input: 'cols', group, icon: 'svg:rte-colLeft' },
118
+ { op: 'table', key: 'insertColumnAfter', title: '在右侧插入', unit: '列', input: 'cols', group, icon: 'svg:rte-colRight' },
119
+ ];
120
+
121
+ /** 删除行 / 删除列(照思源 tableMenu 的 removeMenus) */
122
+ const removeRowColItems = (group) => [
123
+ { op: 'table', key: 'deleteRow', title: '删除行', shortcut: 'Ctrl+-', group, icon: 'Delete' },
124
+ { op: 'table', key: 'deleteColumn', title: '删除列', shortcut: 'Ctrl+Shift+-', group, icon: 'Delete' },
125
+ ];
126
+
127
+ /** 删除表格(单格态主面末项;思源内容区菜单无此项,故仅单格态保留以维持删表入口) */
128
+ const deleteTableItem = (group) => ({ op: 'table', key: 'deleteTable', title: '删除表格', group, danger: true, icon: 'DeleteFilled' });
129
+
130
+ /**
131
+ * 表格右键菜单描述符(思源三态结构,唯一数据源)
132
+ * @param {object|null} ctx tableContext 产物(含 table / grid / cell / rect)
133
+ * @returns {Array<object>} 描述符数组;无上下文返回空数组
134
+ */
135
+ export function tableMenuModel(ctx) {
136
+ if (!ctx) return [];
137
+ // ① 框选态:选区跨多格(思源表格框选菜单)
138
+ if (hasBoxSelection(ctx.rect)) return boxSelectionMenu();
139
+ // ② / ③ 单格态:按命中格有无正文二分(思源内容区右键的两条分支)
140
+ return cellHasBody(ctx) ? filledCellMenu() : emptyCellMenu();
141
+ }
142
+
143
+ /** 框选判据:区域跨行或跨列(思源 tableControl 的 selection.mode='cell' 且 isRectangle()) */
144
+ function hasBoxSelection(rect) {
145
+ return Boolean(rect) && (rect.r1 !== rect.r2 || rect.c1 !== rect.c2);
146
+ }
147
+
148
+ /** 单格·无正文(思源 contentMenu 无正文分支:粘贴族 + 全选 + 插入 + 删除 + 更多 + 删除表格) */
149
+ function emptyCellMenu() {
150
+ return [
151
+ ...pasteItems(0),
152
+ selectAllItem(1),
153
+ ...insertItems(2),
154
+ ...removeRowColItems(3),
155
+ moreItem(4),
156
+ deleteTableItem(5),
157
+ ];
158
+ }
159
+
160
+ /** 单格·有正文(思源 contentMenu 有文字内容分支:复制族+剪切+删除 + 粘贴族 + 全选 + 样式组 + 插入 + 删除 + 更多 + 删除表格) */
161
+ function filledCellMenu() {
162
+ return [
163
+ copyItem(0),
164
+ copyRichItem(0),
165
+ copyTextItem(0),
166
+ { op: 'clip', key: 'cut', title: '剪切', shortcut: 'Ctrl+X', group: 0 },
167
+ { op: 'table', key: 'clearCell', title: '删除', group: 0, icon: 'svg:rte-clear' },
168
+ ...pasteItems(1),
169
+ selectAllItem(2),
170
+ fontItem(3),
171
+ bgItem(3),
172
+ alignItem(4),
173
+ ...insertItems(5),
174
+ ...removeRowColItems(6),
175
+ moreItem(7),
176
+ deleteTableItem(8),
177
+ ];
178
+ }
179
+
180
+ /** 框选态(思源 tableControl openMenu cell 分支:复制族+剪切+粘贴+清空 / 样式组 / 对齐 / 删除行列 / 合并单元格) */
181
+ function boxSelectionMenu() {
182
+ return [
183
+ copyItem(0),
184
+ copyRichItem(0),
185
+ copyTextItem(0),
186
+ { op: 'clip', key: 'cut', title: '剪切', shortcut: 'Ctrl+X', group: 0 },
187
+ { op: 'clip', key: 'paste', title: '粘贴', shortcut: 'Ctrl+V', group: 0 },
188
+ { op: 'table', key: 'clearCell', title: '清空', group: 0, danger: true, icon: 'svg:rte-clear' },
189
+ fontItem(1),
190
+ bgItem(1),
191
+ alignItem(2),
192
+ { op: 'table', key: 'deleteRow', title: '删除行', group: 3, icon: 'Delete' },
193
+ { op: 'table', key: 'deleteColumn', title: '删除列', group: 3, icon: 'Delete' },
194
+ { op: 'table', key: 'mergeOrSplit', title: '合并单元格', group: 4, icon: 'svg:rte-mergeCells' },
195
+ ];
196
+ }
197
+
198
+ const copyItem = (group) => ({ op: 'clip', key: 'copy', title: '复制', shortcut: 'Ctrl+C', group, icon: 'svg:rte-copy' });
199
+ const copyRichItem = (group) => ({ op: 'clip', key: 'copyRich', title: '复制富文本/HTML', group });
200
+ const copyTextItem = (group) => ({ op: 'clip', key: 'copyText', title: '复制纯文本', group });
201
+
202
+ /** 选区 / 命中格里是否有正文:多格框选一律按「有」(用户已显式圈了内容),单格按空格判 */
203
+ function cellHasBody(ctx) {
204
+ const { rect, grid } = ctx;
205
+ if (rect.r1 !== rect.r2 || rect.c1 !== rect.c2) return true;
206
+ const cell = ctx.cell || grid.matrix[rect.r1]?.[rect.c1]?.el;
207
+ return Boolean(cell) && !isCellBlank(cell);
208
+ }
209
+
210
+ /**
211
+ * 描述符 → ContextMenu 条目(唯一映射,三处消费方共用)
212
+ * @param {Array} list 描述符(tableMenuModel 产物,或它的 children)
213
+ * @param {{state?:object, can?:Function, active?:Function, run?:Function}} io
214
+ * state=编辑器状态快照(行内勾选位);can(item)/active(item)=可用性与勾选;
215
+ * run(item, entry, value)=执行体,value 为行内数字框的取值(无输入项时 undefined)
216
+ * @returns {Array<object>} ContextMenu 的 items
217
+ * [设计] 执行体一律挂在 entry.onClick(e, value),三处消费方(富文本右键 / Word 右键 / Word 菜单栏)因此
218
+ * 都不需要 select 回调通道:菜单栏浮层(AppMenuBar)与右键浮层拿到的是同一份可运行的条目。
219
+ */
220
+ export function tableMenuItems(list, io = {}) {
221
+ const { state = null, can = () => true, active = () => false, run = () => {} } = io;
222
+ const target = [];
223
+ let group = null;
224
+ for (const item of list) {
225
+ // 裸分隔标记(无 title):只出分隔线
226
+ if (!item.title) {
227
+ target.push({ divided: true });
228
+ continue;
229
+ }
230
+ const entry = { label: item.title, danger: item.danger || undefined, disabled: !can(item) };
231
+ if (item.icon) entry.icon = item.icon;
232
+ if (item.colorDot) entry.colorDot = item.colorDot;
233
+ if (item.shortcut) entry.shortcut = item.shortcut;
234
+ if (group !== null && item.group !== group) entry.divided = true;
235
+ group = item.group;
236
+ if (item.children) {
237
+ entry.children = tableMenuItems(item.children, io);
238
+ } else {
239
+ if (item.input) {
240
+ // 行内输入项:图标 + 文案 + 内联数字框 + 单位字;执行体统一经 onClick,数字框的值作第二参传回
241
+ entry.input = { min: 1, max: INSERT_LIMIT, defaultValue: 1, unit: item.unit || '' };
242
+ } else if (item.toggle) {
243
+ entry.checked = item.command ? Boolean(state?.[INLINE_STATE[item.command]]) : active(item);
244
+ }
245
+ entry.onClick = (e, value) => run(item, entry, value);
246
+ }
247
+ target.push(entry);
248
+ }
249
+ return target;
250
+ }