@a200888213/gbdocx 0.6.1 → 0.6.2
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/package.json +1 -1
- package/src/app/file-io.js +122 -122
- package/src/app/main.js +9 -9
- package/src/app/word/catalog.js +52 -52
- package/src/app/word/comments.js +112 -112
- package/src/app/word/doc.js +113 -113
- package/src/app/word/edit.js +72 -72
- package/src/app/word/hf.js +76 -76
- package/src/app/word/layout.js +155 -155
- package/src/app/word/notes.js +110 -110
- package/src/app/word/shared.js +22 -22
- package/src/app/word-sheets.js +101 -101
- package/src/kernel/docx/codec/blocks.js +123 -123
- package/src/kernel/docx/codec/carry.js +129 -129
- package/src/kernel/docx/codec/comments.js +25 -25
- package/src/kernel/docx/codec/docx.js +198 -198
- package/src/kernel/docx/codec/docxStyles.js +177 -177
- package/src/kernel/docx/codec/fonts.js +195 -195
- package/src/kernel/docx/codec/helpers.js +161 -161
- package/src/kernel/docx/codec/hfHtml.js +152 -152
- package/src/kernel/docx/codec/html.js +739 -739
- package/src/kernel/docx/codec/losses.js +420 -420
- package/src/kernel/docx/codec/markdown.js +170 -170
- package/src/kernel/docx/codec/parts.js +249 -249
- package/src/kernel/docx/codec/read.js +603 -603
- package/src/kernel/docx/codec/render.js +1156 -1156
- package/src/kernel/docx/codec/roundtrip.js +236 -236
- package/src/kernel/docx/codec/write.js +833 -833
- package/src/kernel/docx/comments.js +14 -14
- package/src/kernel/docx/pageGeometry.js +123 -123
- package/src/kernel/docx/pagination.js +481 -481
- package/src/kernel/docx/watermark.js +45 -45
- package/src/kernel/editor/constants.js +8 -8
- package/src/kernel/editor/engine/blocks.js +594 -594
- package/src/kernel/editor/engine/blocksEnter.js +109 -109
- package/src/kernel/editor/engine/blocksInsert.js +254 -254
- package/src/kernel/editor/engine/blocksStructure.js +226 -226
- package/src/kernel/editor/engine/commands.js +181 -181
- package/src/kernel/editor/engine/dom.js +646 -646
- package/src/kernel/editor/engine/history.js +149 -149
- package/src/kernel/editor/engine/inline.js +656 -656
- package/src/kernel/editor/engine/input.js +554 -554
- package/src/kernel/editor/engine/interaction.js +137 -137
- package/src/kernel/editor/engine/pointer.js +39 -39
- package/src/kernel/editor/engine/revision.js +138 -138
- package/src/kernel/editor/engine/schema.js +417 -417
- package/src/kernel/editor/engine/selection.js +119 -119
- package/src/kernel/editor/engine/state.js +118 -118
- package/src/kernel/editor/engine/tableClip.js +154 -154
- package/src/kernel/editor/engine/tableGrid.js +55 -55
- package/src/kernel/editor/engine/tableInteraction.js +388 -388
- package/src/kernel/editor/engine/tableMenu.js +250 -250
- package/src/kernel/editor/engine/tableOps.js +174 -174
- package/src/kernel/editor/engine/tables.js +732 -732
- package/src/kernel/editor/overlayGeometry.js +20 -20
- package/src/kernel/editor/utils.js +48 -48
- package/src/kernel/keyboard.js +114 -114
- package/src/kernel/textStyles.js +84 -84
- package/src/kernel/util/imageFile.js +91 -91
- package/src/kernel/util/shortcutKey.js +33 -33
- package/src/surface/editor-surface.js +600 -600
- package/src/ui/color-panel.js +69 -69
- package/src/ui/context-menu.js +133 -133
- package/src/ui/find-bar.js +151 -151
- package/src/ui/menubar.js +28 -28
- package/src/ui/panels.js +115 -115
- package/src/ui/prompt.js +89 -89
- package/src/ui/ribbon.js +485 -485
- package/src/ui/special-chars.js +39 -39
- package/src/ui/statusbar.js +81 -81
- package/src/ui/toolbar.js +32 -32
- package/styles/editor-content.css +440 -440
- package/styles/tokens.css +154 -154
- package/styles/word-panel.css +1 -1
|
@@ -1,388 +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
|
-
};
|
|
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
|
+
};
|