@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.
- package/LICENSE +21 -0
- package/README.md +206 -2
- package/index.html +25 -0
- package/package.json +48 -6
- package/serve.mjs +22 -0
- package/src/app/file-io.js +122 -0
- package/src/app/main.js +9 -0
- package/src/app/word/catalog.js +52 -0
- package/src/app/word/comments.js +112 -0
- package/src/app/word/doc.js +113 -0
- package/src/app/word/edit.js +72 -0
- package/src/app/word/file.js +162 -0
- package/src/app/word/hf.js +76 -0
- package/src/app/word/layout.js +155 -0
- package/src/app/word/menus.js +175 -0
- package/src/app/word/notes.js +110 -0
- package/src/app/word/shared.js +22 -0
- package/src/app/word/view.js +117 -0
- package/src/app/word-app.js +459 -0
- package/src/app/word-sheets.js +101 -0
- package/src/kernel/docx/catalog.js +41 -0
- package/src/kernel/docx/codec/blocks.js +123 -0
- package/src/kernel/docx/codec/carry.js +129 -0
- package/src/kernel/docx/codec/comments.js +25 -0
- package/src/kernel/docx/codec/docx.js +198 -0
- package/src/kernel/docx/codec/docxStyles.js +177 -0
- package/src/kernel/docx/codec/fonts.js +195 -0
- package/src/kernel/docx/codec/helpers.js +161 -0
- package/src/kernel/docx/codec/hfHtml.js +152 -0
- package/src/kernel/docx/codec/html.js +739 -0
- package/src/kernel/docx/codec/losses.js +420 -0
- package/src/kernel/docx/codec/markdown.js +170 -0
- package/src/kernel/docx/codec/parts.js +249 -0
- package/src/kernel/docx/codec/read.js +603 -0
- package/src/kernel/docx/codec/render.js +1156 -0
- package/src/kernel/docx/codec/roundtrip.js +236 -0
- package/src/kernel/docx/codec/write.js +833 -0
- package/src/kernel/docx/comments.js +14 -0
- package/src/kernel/docx/constants.js +63 -0
- package/src/kernel/docx/menus.js +211 -0
- package/src/kernel/docx/pageGeometry.js +123 -0
- package/src/kernel/docx/pagination.js +481 -0
- package/src/kernel/docx/watermark.js +45 -0
- package/src/kernel/docx/wordShortcuts.js +37 -0
- package/src/kernel/editor/constants.js +8 -0
- package/src/kernel/editor/engine/blocks.js +594 -0
- package/src/kernel/editor/engine/blocksEnter.js +109 -0
- package/src/kernel/editor/engine/blocksInsert.js +254 -0
- package/src/kernel/editor/engine/blocksStructure.js +226 -0
- package/src/kernel/editor/engine/commands.js +181 -0
- package/src/kernel/editor/engine/dom.js +646 -0
- package/src/kernel/editor/engine/editor.js +213 -0
- package/src/kernel/editor/engine/history.js +149 -0
- package/src/kernel/editor/engine/inline.js +656 -0
- package/src/kernel/editor/engine/input.js +554 -0
- package/src/kernel/editor/engine/interaction.js +137 -0
- package/src/kernel/editor/engine/pointer.js +39 -0
- package/src/kernel/editor/engine/revision.js +138 -0
- package/src/kernel/editor/engine/schema.js +417 -0
- package/src/kernel/editor/engine/search.js +113 -0
- package/src/kernel/editor/engine/selection.js +119 -0
- package/src/kernel/editor/engine/state.js +118 -0
- package/src/kernel/editor/engine/tableClip.js +154 -0
- package/src/kernel/editor/engine/tableGrid.js +55 -0
- package/src/kernel/editor/engine/tableInteraction.js +388 -0
- package/src/kernel/editor/engine/tableMenu.js +250 -0
- package/src/kernel/editor/engine/tableOps.js +174 -0
- package/src/kernel/editor/engine/tables.js +732 -0
- package/src/kernel/editor/overlayGeometry.js +20 -0
- package/src/kernel/editor/pasteClean.js +293 -0
- package/src/kernel/editor/utils.js +49 -0
- package/src/kernel/keyboard.js +114 -0
- package/src/kernel/notify.js +54 -0
- package/src/kernel/textStyles.js +84 -0
- package/src/kernel/util/clipboard.js +89 -0
- package/src/kernel/util/imageFile.js +91 -0
- package/src/kernel/util/safeValue.js +25 -0
- package/src/kernel/util/shortcutKey.js +33 -0
- package/src/surface/editor-surface.js +600 -0
- package/src/ui/color-panel.js +69 -0
- package/src/ui/context-menu.js +130 -0
- package/src/ui/dialogs.js +249 -0
- package/src/ui/find-bar.js +151 -0
- package/src/ui/icons.js +134 -0
- package/src/ui/menubar.js +28 -0
- package/src/ui/panels.js +115 -0
- package/src/ui/popup.js +79 -0
- package/src/ui/prompt.js +89 -0
- package/src/ui/ribbon.js +485 -0
- package/src/ui/special-chars.js +39 -0
- package/src/ui/statusbar.js +81 -0
- package/src/ui/toolbar.js +32 -0
- package/styles/app.css +944 -0
- package/styles/editor-content.css +440 -0
- package/styles/tokens.css +154 -0
- package/styles/word-panel.css +410 -0
|
@@ -0,0 +1,388 @@
|
|
|
1
|
+
// [文件] web·editor-kernel·engine·tableInteraction —— 表格框选 / 列宽拖拽 / 列分隔线悬停(混入 EditorCore.prototype)
|
|
2
|
+
// [设计] 选中高亮不落正文 DOM:框选只是「逻辑网格矩形」,几何由 cellSelectionBox 抛给宿主浮层;
|
|
3
|
+
// 列宽提示线同理只抛边界几何,正文一个字节都不动。
|
|
4
|
+
// [约束] 带 `on*` 前缀的成员挂编辑面事件,由 editor.js 构造时统一 bind(this)。
|
|
5
|
+
import * as D from './dom.js';
|
|
6
|
+
import * as T from './tables.js';
|
|
7
|
+
|
|
8
|
+
/** 列宽拖拽的命中半宽(px):判据在此,可视命中带在 content.scss 的格右 ::before(width: 6px),两处必须同值 */
|
|
9
|
+
const COL_RESIZE_HIT = 6;
|
|
10
|
+
|
|
11
|
+
/** 框选期间给表挂的运行期类(定义在 dom.js,与 content.scss 同名规则、与 RUNTIME_CLASSES 同源) */
|
|
12
|
+
const CELL_DRAGGING = D.CELL_DRAGGING;
|
|
13
|
+
|
|
14
|
+
/** 需在构造时 bind(this) 的事件处理器名(挂编辑面监听,必须引用稳定) */
|
|
15
|
+
export const TABLE_INTERACTION_HANDLERS = ['onMouseDown', 'onHoverColumn', 'onLeaveColumn'];
|
|
16
|
+
|
|
17
|
+
/** 表格框选与列宽方法(装配到 EditorCore.prototype;this 为内核实例) */
|
|
18
|
+
export const tableInteractionMethods = {
|
|
19
|
+
/**
|
|
20
|
+
* 整表转成单元格框选(移动手柄单击 / 菜单「选择整个表格」共用)
|
|
21
|
+
* @param {HTMLTableElement} table 目标表
|
|
22
|
+
* @returns {boolean} 是否落地
|
|
23
|
+
*/
|
|
24
|
+
selectTable(table) {
|
|
25
|
+
if (!table?.isConnected || !this.root.contains(table)) return false;
|
|
26
|
+
const grid = this.cachedGrid(table);
|
|
27
|
+
if (!grid.width || !grid.height) return false;
|
|
28
|
+
this.cellRect = { table, rect: { r1: 0, c1: 0, r2: grid.height - 1, c2: grid.width - 1 } };
|
|
29
|
+
// 紧接着的表格菜单动作按命中格取上下文:锚到左上格,整表操作才不会静默失效
|
|
30
|
+
this.contextCell = grid.matrix[0]?.[0]?.el || null;
|
|
31
|
+
this.notifyCellSelection();
|
|
32
|
+
this.syncState();
|
|
33
|
+
return true;
|
|
34
|
+
},
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* 拖列边设列宽:落点在「格右边界 ± COL_RESIZE_HIT/2」的命中带内即接管
|
|
38
|
+
* 中间列的右边界 = 与右邻列的共享线(两列守恒转移,表宽不变);最后一列的右边界 = 表右边界
|
|
39
|
+
* (只改末列宽度,表宽随末列同步增减、其余列不动)
|
|
40
|
+
* [坑] 判据宽度与 content.scss 的格右 ::before(width: 6px)必须同值:那边是可视命中带、
|
|
41
|
+
* 这边是可拖范围,改一处不改另一处就会出现「看得见拖不动 / 拖得动看不见」
|
|
42
|
+
* @returns {boolean} 是否已接管本次 mousedown
|
|
43
|
+
*/
|
|
44
|
+
beginColumnResize(event, cell, table) {
|
|
45
|
+
const rect = cell.getBoundingClientRect();
|
|
46
|
+
// 命中带以格右界为中轴:从「右界 - 半宽」起算到格右界都算命中(左半在格内),
|
|
47
|
+
// 越过右界探到右邻格的那半边由右邻自己的同一条判据接管,两格判定结果一致、不会互相打架
|
|
48
|
+
if (event.clientX < rect.right - COL_RESIZE_HIT / 2) return false;
|
|
49
|
+
const grid = T.toGrid(table);
|
|
50
|
+
const cellInfo = T.cellIndex(grid, cell);
|
|
51
|
+
if (!cellInfo) return false;
|
|
52
|
+
const last = cellInfo.c >= grid.width - 1;
|
|
53
|
+
this.root.classList.add('resize-cursor');
|
|
54
|
+
// 拖拽期锁住提示线所在的列:整段手势里 updateHoverColumn 让位,免得提示线跳到别列
|
|
55
|
+
this.colResizing = true;
|
|
56
|
+
this.hoverColumn = { table, colIndex: cellInfo.c };
|
|
57
|
+
this.emit('column-hover');
|
|
58
|
+
this.pushHistory(); // 拖前的状态:一次撤销回到起拖前
|
|
59
|
+
const startX = event.clientX;
|
|
60
|
+
let applied = 0;
|
|
61
|
+
// 末列拖拽的基准起手一次捕获(见 beginRightEdgeDrag):整段手势只在这份基准上按**累计**位移算,
|
|
62
|
+
// 不把 colgroup 的量化百分比读回来当基准(慢拖会越拖越偏)
|
|
63
|
+
const edgeDrag = last ? T.beginRightEdgeDrag(table, cellInfo.c) : null;
|
|
64
|
+
const onMove = (moveEvent) => {
|
|
65
|
+
// 掐掉原生选区蔓延:拖列宽时鼠标横向划过整张表,Chrome 会把途经的格子文字全刷成蓝块
|
|
66
|
+
// (用户截图里「整列文字都被选中」就是这个)。与框选同口径,逐帧就地折叠
|
|
67
|
+
D.collapseRange();
|
|
68
|
+
const delta = moveEvent.clientX - startX;
|
|
69
|
+
if (delta === applied) return;
|
|
70
|
+
const step = delta - applied;
|
|
71
|
+
applied = delta;
|
|
72
|
+
this.run(
|
|
73
|
+
() => (edgeDrag ? T.dragTableRightEdge(table, edgeDrag, delta) : T.dragColumnEdge(table, cellInfo.c, step)),
|
|
74
|
+
{ coalesce: true },
|
|
75
|
+
);
|
|
76
|
+
this.emit('column-hover'); // 列宽变了:提示线实时贴回新的边界
|
|
77
|
+
};
|
|
78
|
+
const onUp = () => {
|
|
79
|
+
this.dragCleanups.delete(stop);
|
|
80
|
+
document.removeEventListener('mousemove', onMove);
|
|
81
|
+
document.removeEventListener('mouseup', onUp);
|
|
82
|
+
this.root.classList.remove('resize-cursor');
|
|
83
|
+
this.colResizing = false;
|
|
84
|
+
this.clearHoverColumn();
|
|
85
|
+
};
|
|
86
|
+
// destroy() 走同一根收尾:卸监听 + 摘高亮,避免拖到一半卸载留下挂着的 document 监听
|
|
87
|
+
const stop = () => onUp();
|
|
88
|
+
this.dragCleanups.add(stop);
|
|
89
|
+
document.addEventListener('mousemove', onMove);
|
|
90
|
+
document.addEventListener('mouseup', onUp);
|
|
91
|
+
event.preventDefault();
|
|
92
|
+
return true;
|
|
93
|
+
},
|
|
94
|
+
|
|
95
|
+
/**
|
|
96
|
+
* 表边缘把手手势(思源 Image 2/3:「点击在右侧插入一列 / 拖拽插入或删除多列」)
|
|
97
|
+
* [设计] 右沿改列、下沿改行共用一套:起手即 +1(单击不拖也拿到一格),拖拽则按位移折算个数,
|
|
98
|
+
* 增减都作用在表的末尾,所以往回拖删掉的正是刚才加出来的那些 —— 与思源 tooltip 同义。
|
|
99
|
+
* [坑] 目标数必须钳在 [1, 起始数 + INSERT_LIMIT]:下限防把最后一行/列删掉(删空表等于删表),
|
|
100
|
+
* 上限与菜单数字框同源(INSERT_LIMIT),免手抖把文档打爆。
|
|
101
|
+
* @param {MouseEvent} event 把手上的 mousedown
|
|
102
|
+
* @param {Element} table 目标表
|
|
103
|
+
* @param {'column'|'row'} axis
|
|
104
|
+
* @returns {boolean} 是否已接管
|
|
105
|
+
*/
|
|
106
|
+
beginEdgeGesture(event, table, axis) {
|
|
107
|
+
const isCol = axis === 'column';
|
|
108
|
+
const grid0 = T.toGrid(table);
|
|
109
|
+
const base = isCol ? grid0.width : grid0.height;
|
|
110
|
+
if (!base) return false;
|
|
111
|
+
this.pushHistory();
|
|
112
|
+
const start = isCol ? event.clientX : event.clientY;
|
|
113
|
+
const unit = this._edgeUnit(grid0, isCol ? 'c' : 'r');
|
|
114
|
+
const max = base + T.INSERT_LIMIT;
|
|
115
|
+
|
|
116
|
+
/** 把表调到目标行/列数(一次手势内 coalesce,全程一条撤销记录) */
|
|
117
|
+
const applyCount = (want) => {
|
|
118
|
+
const g = T.toGrid(table);
|
|
119
|
+
const cur = isCol ? g.width : g.height;
|
|
120
|
+
// 下限 1:删到只剩一行/列就停手,绝不能走 deleteTable 那条路
|
|
121
|
+
const delta = Math.min(Math.max(want, 1), max) - cur;
|
|
122
|
+
if (!delta) return;
|
|
123
|
+
const growing = delta > 0;
|
|
124
|
+
const ctx = isCol
|
|
125
|
+
? {
|
|
126
|
+
table,
|
|
127
|
+
grid: g,
|
|
128
|
+
cell: g.matrix[0]?.[cur - 1]?.el || null,
|
|
129
|
+
rowIndex: -1,
|
|
130
|
+
colIndex: cur - 1,
|
|
131
|
+
rect: growing
|
|
132
|
+
? { r1: 0, c1: cur - 1, r2: g.height - 1, c2: cur - 1 }
|
|
133
|
+
: { r1: 0, c1: cur + delta, r2: g.height - 1, c2: cur - 1 },
|
|
134
|
+
}
|
|
135
|
+
: {
|
|
136
|
+
table,
|
|
137
|
+
grid: g,
|
|
138
|
+
cell: g.matrix[cur - 1]?.[0]?.el || null,
|
|
139
|
+
rowIndex: cur - 1,
|
|
140
|
+
colIndex: -1,
|
|
141
|
+
rect: growing
|
|
142
|
+
? { r1: cur - 1, c1: 0, r2: cur - 1, c2: g.width - 1 }
|
|
143
|
+
: { r1: cur + delta, c1: 0, r2: cur - 1, c2: g.width - 1 },
|
|
144
|
+
};
|
|
145
|
+
const action = growing ? (isCol ? 'insertColumnAfter' : 'insertRowAfter') : isCol ? 'deleteColumn' : 'deleteRow';
|
|
146
|
+
this.run(() => T.runTableAction(ctx, action, { rows: Math.abs(delta), cols: Math.abs(delta) }), { coalesce: true });
|
|
147
|
+
};
|
|
148
|
+
|
|
149
|
+
applyCount(base + 1);
|
|
150
|
+
const onMove = (moveEvent) => {
|
|
151
|
+
// 掐掉原生选区蔓延:横向拖过整张表时浏览器会把途经的格子文字全刷成蓝块
|
|
152
|
+
D.collapseRange();
|
|
153
|
+
const offset = (isCol ? moveEvent.clientX : moveEvent.clientY) - start;
|
|
154
|
+
applyCount(base + 1 + Math.round(offset / unit));
|
|
155
|
+
};
|
|
156
|
+
const onUp = () => {
|
|
157
|
+
this.dragCleanups.delete(stop);
|
|
158
|
+
document.removeEventListener('mousemove', onMove);
|
|
159
|
+
document.removeEventListener('mouseup', onUp);
|
|
160
|
+
};
|
|
161
|
+
// destroy() 走同一根收尾:拖到一半被卸载也要把 document 监听摘干净
|
|
162
|
+
const stop = () => onUp();
|
|
163
|
+
this.dragCleanups.add(stop);
|
|
164
|
+
document.addEventListener('mousemove', onMove);
|
|
165
|
+
document.addEventListener('mouseup', onUp);
|
|
166
|
+
event.preventDefault();
|
|
167
|
+
return true;
|
|
168
|
+
},
|
|
169
|
+
|
|
170
|
+
/** 边缘手势的位移单位:末列宽 / 末行高(取不到时给个可用兜底,免除以 0 得 Infinity) */
|
|
171
|
+
_edgeUnit(grid, axis) {
|
|
172
|
+
const last = axis === 'c' ? grid.width - 1 : grid.height - 1;
|
|
173
|
+
const cell = grid.cells.find((item) => (axis === 'c' ? item.c === last : item.r === last));
|
|
174
|
+
const box = cell?.el.getBoundingClientRect();
|
|
175
|
+
return Math.max(axis === 'c' ? 40 : 24, (axis === 'c' ? box?.width : box?.height) || 0);
|
|
176
|
+
},
|
|
177
|
+
|
|
178
|
+
/**
|
|
179
|
+
* 标记当前悬停的列边界(供宿主浮层画列宽提示线)
|
|
180
|
+
* [设计] 不再给整列挂高亮类:提示线只是贴在边界上的一根竖线,几何由 columnBoundaryBox 现算,
|
|
181
|
+
* 正文 DOM 一个字节都不动;同一列反复进出不重发事件,避免每帧刷宿主
|
|
182
|
+
*/
|
|
183
|
+
setHoverColumn(table, colIndex) {
|
|
184
|
+
const prev = this.hoverColumn;
|
|
185
|
+
if (prev?.table === table && prev.colIndex === colIndex) return;
|
|
186
|
+
this.hoverColumn = { table, colIndex };
|
|
187
|
+
this.emit('column-hover');
|
|
188
|
+
},
|
|
189
|
+
|
|
190
|
+
clearHoverColumn() {
|
|
191
|
+
if (!this.hoverColumn) return;
|
|
192
|
+
this.hoverColumn = null;
|
|
193
|
+
this.emit('column-hover');
|
|
194
|
+
},
|
|
195
|
+
|
|
196
|
+
/**
|
|
197
|
+
* 列宽提示线的可视几何(viewport 坐标):贴在该逻辑列右边界、贯穿整表高度
|
|
198
|
+
* [设计] 内核只抛几何,提示线是宿主浮层里的真元素(与插入把手同层);宿主按滚动容器换算成内容坐标
|
|
199
|
+
* @returns {{boundaryX:number, top:number, height:number}|null}
|
|
200
|
+
*/
|
|
201
|
+
columnBoundaryBox() {
|
|
202
|
+
const hover = this.hoverColumn;
|
|
203
|
+
if (!hover?.table?.isConnected) return null;
|
|
204
|
+
const cell = this.cachedGrid(hover.table).matrix[0]?.[hover.colIndex];
|
|
205
|
+
if (!cell) return null;
|
|
206
|
+
const box = cell.el.getBoundingClientRect();
|
|
207
|
+
const tableRect = hover.table.getBoundingClientRect();
|
|
208
|
+
return { boundaryX: box.right, top: tableRect.top, height: tableRect.height };
|
|
209
|
+
},
|
|
210
|
+
|
|
211
|
+
/**
|
|
212
|
+
* 框选覆盖层的可视几何(viewport 坐标);无选区返回 null
|
|
213
|
+
* [设计] 覆盖层不挂在正文 DOM 上:正文一个字节都不动,宿主浮层按此几何画一层矩形
|
|
214
|
+
* @returns {{left:number, top:number, right:number, bottom:number, width:number, height:number}|null}
|
|
215
|
+
*/
|
|
216
|
+
cellSelectionBox() {
|
|
217
|
+
const sel = this.cellRect;
|
|
218
|
+
if (!sel?.table?.isConnected) return null;
|
|
219
|
+
return T.selectionBox(sel.table, sel.rect);
|
|
220
|
+
},
|
|
221
|
+
|
|
222
|
+
/**
|
|
223
|
+
* 取「表 → 逻辑网格」的短期缓存
|
|
224
|
+
* [设计] 悬停热路径每帧都要 grid(判列序、算跨列合并),但一帧内可能连发多次 mousemove,
|
|
225
|
+
* 全量扫描大表(几百格)吃不消;同一张表在同一帧内复用同一份结果,
|
|
226
|
+
* 表格结构变更(增删行列/合并)由 _gridCache 的 key 不含结构版本兜不住,
|
|
227
|
+
* 故缓存只在「同一事件循环 tick 内」有效(用 rAF 清空),跨帧必然重算
|
|
228
|
+
*/
|
|
229
|
+
cachedGrid(table) {
|
|
230
|
+
const cache = this._gridCache;
|
|
231
|
+
if (cache && cache.table === table) return cache.grid;
|
|
232
|
+
const grid = T.toGrid(table);
|
|
233
|
+
this._gridCache = { table, grid };
|
|
234
|
+
if (!this._gridCachePending) {
|
|
235
|
+
this._gridCachePending = true;
|
|
236
|
+
requestAnimationFrame(() => {
|
|
237
|
+
this._gridCachePending = false;
|
|
238
|
+
this._gridCache = null;
|
|
239
|
+
});
|
|
240
|
+
}
|
|
241
|
+
return grid;
|
|
242
|
+
},
|
|
243
|
+
|
|
244
|
+
/**
|
|
245
|
+
* 悬停在表格上时判断「是否贴着某条列分隔线」,是则让宿主浮层亮出列宽提示线
|
|
246
|
+
* [设计] 用 root 上的 mousemove 代理而非逐格 mouseover:格子数量大时逐格监听内存吃不消,
|
|
247
|
+
* 且鼠标快速掠过分隔线会漏掉 mouseenter/mouseleave 的配对
|
|
248
|
+
* [坑] 命中判据必须与 beginColumnResize 逐字同源(同一个 COL_RESIZE_HIT、同一条 right 基准),
|
|
249
|
+
* 否则会出现「提示线亮了却拖不动 / 能拖却不亮」
|
|
250
|
+
*/
|
|
251
|
+
updateHoverColumn(event) {
|
|
252
|
+
if (this.destroyed) return;
|
|
253
|
+
// 列宽拖拽期由 beginColumnResize 锁住当前列,悬停检测让位,免得提示线跳到别列
|
|
254
|
+
if (this.colResizing) return;
|
|
255
|
+
const cell = D.cellOf(event.target, this.root);
|
|
256
|
+
const table = cell && D.tableOf(cell, this.root);
|
|
257
|
+
if (!cell || !table) {
|
|
258
|
+
this.clearHoverColumn();
|
|
259
|
+
return;
|
|
260
|
+
}
|
|
261
|
+
const rect = cell.getBoundingClientRect();
|
|
262
|
+
if (event.clientX < rect.right - COL_RESIZE_HIT / 2) {
|
|
263
|
+
this.clearHoverColumn();
|
|
264
|
+
return;
|
|
265
|
+
}
|
|
266
|
+
const grid = this.cachedGrid(table);
|
|
267
|
+
const info = T.cellIndex(grid, cell);
|
|
268
|
+
if (!info) {
|
|
269
|
+
this.clearHoverColumn();
|
|
270
|
+
return;
|
|
271
|
+
}
|
|
272
|
+
this.setHoverColumn(table, info.c);
|
|
273
|
+
},
|
|
274
|
+
|
|
275
|
+
onHoverColumn(event) {
|
|
276
|
+
this.updateHoverColumn(event);
|
|
277
|
+
},
|
|
278
|
+
|
|
279
|
+
onLeaveColumn() {
|
|
280
|
+
this.clearHoverColumn();
|
|
281
|
+
},
|
|
282
|
+
|
|
283
|
+
onMouseDown(event) {
|
|
284
|
+
// [坑] 非左键必须原样返回:表格圈选后右键弹菜单是主路径,右键 mousedown 若走下面的
|
|
285
|
+
// 清圈选/起拖逻辑,cellRect 会被清掉,菜单里的合并/删除因取不到框选区域全部静默失效
|
|
286
|
+
if (event.button !== 0) return;
|
|
287
|
+
const cell = D.cellOf(event.target, this.root);
|
|
288
|
+
if (!cell) {
|
|
289
|
+
this.clearHoverColumn();
|
|
290
|
+
if (this.cellRect) this.clearCellSelection();
|
|
291
|
+
return;
|
|
292
|
+
}
|
|
293
|
+
const table = D.tableOf(cell, this.root);
|
|
294
|
+
if (!table) return;
|
|
295
|
+
if (this.beginColumnResize(event, cell, table)) return;
|
|
296
|
+
this.clearHoverColumn(); // 进入框选:悬停提示线让位,免得和选区外框抢视觉
|
|
297
|
+
|
|
298
|
+
if (!event.shiftKey) this.clearCellSelection();
|
|
299
|
+
this.cellDragFrom = cell;
|
|
300
|
+
// [设计] 不在 mousedown 就 preventDefault:那会让表内文字**永远选不中**
|
|
301
|
+
// (浏览器全程拿不到原生拖选手势,长按/划线选词全部失效)。
|
|
302
|
+
// 改为「意图延迟判定」——按下先完全交给浏览器做原生文字选择,只有当拖拽真的跨出起始格时
|
|
303
|
+
// 才升级为单元格框选(见下面 onMove 的 promoteToCellDrag)。格内拖动永远是选文字。
|
|
304
|
+
const dragFrom = cell;
|
|
305
|
+
// ---- 框选升级(惰性:首次跨格才生效)----
|
|
306
|
+
let promoted = false; // 是否已升级为单元格框选(升级后逐帧折叠原生选区)
|
|
307
|
+
let pending = null;
|
|
308
|
+
let raf = 0;
|
|
309
|
+
// [坑] 升级后光靠逐帧 collapseRange 是「追着擦」:Chrome 按自己的拖拽锚点每帧重算选区,
|
|
310
|
+
// 擦不干净就会在框选上叠一条条蓝块,还会刷出 selectionchange 风暴。
|
|
311
|
+
// 这里补一道 selectstart 拦截(捕获阶段):升级后再有原生选区要起就地掐掉。
|
|
312
|
+
// mousedown 时就挂上、待命不生效,故格内纯选文字时完全不干预原生行为。
|
|
313
|
+
const onSelectStart = (selectEvent) => {
|
|
314
|
+
if (promoted) selectEvent.preventDefault();
|
|
315
|
+
};
|
|
316
|
+
document.addEventListener('selectstart', onSelectStart, true);
|
|
317
|
+
const promoteToCellDrag = () => {
|
|
318
|
+
if (promoted) return;
|
|
319
|
+
promoted = true;
|
|
320
|
+
// 第二道防线(见 content.scss 同名规则):升级后才锁住格内文字
|
|
321
|
+
table.classList.add(CELL_DRAGGING);
|
|
322
|
+
// [坑] 升级瞬间浏览器已经按自己的拖拽锚点铺了一小段原生选区(跨格那一帧),
|
|
323
|
+
// 必须就地折叠掉,否则它会跟着鼠标一路蔓延成一条条蓝块叠在框选上
|
|
324
|
+
D.collapseRange();
|
|
325
|
+
this.root.focus({ preventScroll: true });
|
|
326
|
+
};
|
|
327
|
+
const flush = () => {
|
|
328
|
+
raf = 0;
|
|
329
|
+
const over = pending;
|
|
330
|
+
pending = null;
|
|
331
|
+
if (!over || !over.isConnected) return;
|
|
332
|
+
const grid = T.toGrid(table);
|
|
333
|
+
const rect = T.rectBetween(grid, dragFrom, over);
|
|
334
|
+
if (!rect) return;
|
|
335
|
+
// [设计] 重选同区域直接跳过:既省一次全量重画,也避免给 observer 造无谓的 attribute 变更
|
|
336
|
+
const cur = this.cellRect?.rect;
|
|
337
|
+
if (this.cellRect?.table === table && cur && cur.r1 === rect.r1 && cur.c1 === rect.c1 && cur.r2 === rect.r2 && cur.c2 === rect.c2) return;
|
|
338
|
+
this.cellRect = { table, rect };
|
|
339
|
+
// [设计] 选区一变把几何抛给宿主浮层重画(不再给格子挂类名/装饰)
|
|
340
|
+
this.notifyCellSelection();
|
|
341
|
+
};
|
|
342
|
+
const onMove = (moveEvent) => {
|
|
343
|
+
const over = D.cellOf(moveEvent.target, this.root);
|
|
344
|
+
// 还在起始格内:原生文字选择照常进行,不插手
|
|
345
|
+
if (!over || over === dragFrom) return;
|
|
346
|
+
// 跨出起始格 → 立即升级为单元格框选,并折叠掉刚被浏览器铺开的那段原生选区
|
|
347
|
+
promoteToCellDrag();
|
|
348
|
+
D.collapseRange();
|
|
349
|
+
pending = over;
|
|
350
|
+
if (!raf) raf = requestAnimationFrame(flush);
|
|
351
|
+
};
|
|
352
|
+
const onUp = (fromDestroy = false) => {
|
|
353
|
+
this.dragCleanups.delete(stop);
|
|
354
|
+
document.removeEventListener('mousemove', onMove);
|
|
355
|
+
document.removeEventListener('mouseup', finish);
|
|
356
|
+
// [坑] 必须摘:selectstart 挂在 document 上、捕获阶段,不摘会一直留着
|
|
357
|
+
// (每次表格按下都加一个,松手不清 → 监听器泄漏且后续手势被旧闭包干扰)
|
|
358
|
+
document.removeEventListener('selectstart', onSelectStart, true);
|
|
359
|
+
if (raf) cancelAnimationFrame(raf);
|
|
360
|
+
if (promoted) table.classList.remove(CELL_DRAGGING);
|
|
361
|
+
this.cellDragFrom = null;
|
|
362
|
+
// destroy() 触发的收尾只做卸载,不再回写选区/状态:此时 root 已不可编辑、syncState 无意义
|
|
363
|
+
if (fromDestroy) return;
|
|
364
|
+
// 未升级 = 用户只是在选文字:原生选区原样保留,一个字都不动
|
|
365
|
+
if (!promoted) {
|
|
366
|
+
this.syncState();
|
|
367
|
+
return;
|
|
368
|
+
}
|
|
369
|
+
// 松手把选区收敛到框选锚点:拖拽期被折叠的 caret 停在按下点,不收口的话下一次打字会插到框选之前
|
|
370
|
+
if (this.cellRect && dragFrom.isConnected) D.setCaret(dragFrom, 0);
|
|
371
|
+
this.syncState();
|
|
372
|
+
};
|
|
373
|
+
// destroy() 走同一根收尾:拖到一半被卸载时也要把 document 监听摘干净并复位拖拽类
|
|
374
|
+
const stop = () => onUp(true);
|
|
375
|
+
// [坑] mouseup 监听不能直接挂 onUp:事件对象会占据 fromDestroy 参数(恒真),
|
|
376
|
+
// 正常松手也走「卸载收尾」分支,光标收敛与状态回写全部失效
|
|
377
|
+
const finish = () => onUp(false);
|
|
378
|
+
this.dragCleanups.add(stop);
|
|
379
|
+
document.addEventListener('mousemove', onMove);
|
|
380
|
+
document.addEventListener('mouseup', finish);
|
|
381
|
+
},
|
|
382
|
+
|
|
383
|
+
clearCellSelection() {
|
|
384
|
+
if (!this.cellRect) return;
|
|
385
|
+
this.cellRect = null;
|
|
386
|
+
this.notifyCellSelection();
|
|
387
|
+
},
|
|
388
|
+
};
|
|
@@ -0,0 +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
|
+
}
|