-
Notifications
You must be signed in to change notification settings - Fork 178
Expand file tree
/
Copy pathinput.ts
More file actions
305 lines (282 loc) · 9.47 KB
/
input.ts
File metadata and controls
305 lines (282 loc) · 9.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
// This file defines a number of helpers for wiring up user input to
// table-related functionality.
import { keydownHandler } from 'prosemirror-keymap';
import type { ResolvedPos, Slice } from 'prosemirror-model';
import { Fragment } from 'prosemirror-model';
import type { Command, EditorState, Transaction } from 'prosemirror-state';
import { Selection, TextSelection } from 'prosemirror-state';
import type { EditorView } from 'prosemirror-view';
import { CellSelection } from './cellselection';
import { deleteCellSelection } from './commands';
import { clipCells, fitSlice, insertCells, pastedCells } from './copypaste';
import { tableNodeTypes } from './schema';
import { TableMap } from './tablemap';
import {
cellAround,
inSameTable,
isInTable,
nextCell,
selectionCell,
tableEditingKey,
} from './util';
type Axis = 'horiz' | 'vert';
/**
* @public
*/
export type Direction = -1 | 1;
export const handleKeyDown = keydownHandler({
ArrowLeft: arrow('horiz', -1),
ArrowRight: arrow('horiz', 1),
ArrowUp: arrow('vert', -1),
ArrowDown: arrow('vert', 1),
'Shift-ArrowLeft': shiftArrow('horiz', -1),
'Shift-ArrowRight': shiftArrow('horiz', 1),
'Shift-ArrowUp': shiftArrow('vert', -1),
'Shift-ArrowDown': shiftArrow('vert', 1),
Backspace: deleteCellSelection,
'Mod-Backspace': deleteCellSelection,
Delete: deleteCellSelection,
'Mod-Delete': deleteCellSelection,
});
function maybeSetSelection(
state: EditorState,
dispatch: undefined | ((tr: Transaction) => void),
selection: Selection,
): boolean {
if (selection.eq(state.selection)) return false;
if (dispatch) dispatch(state.tr.setSelection(selection).scrollIntoView());
return true;
}
/**
* @internal
*/
export function arrow(axis: Axis, dir: Direction): Command {
return (state, dispatch, view) => {
if (!view) return false;
const sel = state.selection;
if (sel instanceof CellSelection) {
return maybeSetSelection(
state,
dispatch,
Selection.near(sel.$headCell, dir),
);
}
if (axis != 'horiz' && !sel.empty) return false;
const end = atEndOfCell(view, axis, dir);
if (end == null) return false;
if (axis == 'horiz') {
return maybeSetSelection(
state,
dispatch,
Selection.near(state.doc.resolve(sel.head + dir), dir),
);
} else {
const $cell = state.doc.resolve(end);
const $next = nextCell($cell, axis, dir);
let newSel;
if ($next) newSel = Selection.near($next, 1);
else if (dir < 0)
newSel = Selection.near(state.doc.resolve($cell.before(-1)), -1);
else newSel = Selection.near(state.doc.resolve($cell.after(-1)), 1);
return maybeSetSelection(state, dispatch, newSel);
}
};
}
function shiftArrow(axis: Axis, dir: Direction): Command {
return (state, dispatch, view) => {
if (!view) return false;
const sel = state.selection;
let cellSel: CellSelection;
if (sel instanceof CellSelection) {
cellSel = sel;
} else {
const end = atEndOfCell(view, axis, dir);
if (end == null) return false;
cellSel = new CellSelection(state.doc.resolve(end));
}
const $head = nextCell(cellSel.$headCell, axis, dir);
if (!$head) return false;
return maybeSetSelection(
state,
dispatch,
new CellSelection(cellSel.$anchorCell, $head),
);
};
}
export function handleTripleClick(view: EditorView, pos: number): boolean {
const doc = view.state.doc,
$cell = cellAround(doc.resolve(pos));
if (!$cell) return false;
view.dispatch(view.state.tr.setSelection(new CellSelection($cell)));
return true;
}
/**
* @public
*/
export function handlePaste(
view: EditorView,
_: ClipboardEvent,
slice: Slice,
): boolean {
if (!isInTable(view.state)) return false;
let cells = pastedCells(slice);
const sel = view.state.selection;
if (sel instanceof CellSelection) {
if (!cells)
cells = {
width: 1,
height: 1,
rows: [
Fragment.from(
fitSlice(tableNodeTypes(view.state.schema).cell, slice),
),
],
};
const table = sel.$anchorCell.node(-1);
const start = sel.$anchorCell.start(-1);
const rect = TableMap.get(table).rectBetween(
sel.$anchorCell.pos - start,
sel.$headCell.pos - start,
);
cells = clipCells(cells, rect.right - rect.left, rect.bottom - rect.top);
insertCells(view.state, view.dispatch, start, rect, cells);
return true;
} else if (cells) {
const $cell = selectionCell(view.state);
const start = $cell.start(-1);
insertCells(
view.state,
view.dispatch,
start,
TableMap.get($cell.node(-1)).findCell($cell.pos - start),
cells,
);
return true;
} else {
return false;
}
}
export function handleMouseDown(
view: EditorView,
startEvent: MouseEvent,
): void {
// Only handle mouse down events for the main button (usually the left button).
// This ensures that the cell selection won't be triggered when trying to open
// the context menu.
if (startEvent.button != 0) return;
if (startEvent.ctrlKey || startEvent.metaKey) return;
const startDOMCell = domInCell(view, startEvent.target as Node);
let $anchor;
if (startEvent.shiftKey && view.state.selection instanceof CellSelection) {
// Adding to an existing cell selection
setCellSelection(view.state.selection.$anchorCell, startEvent);
startEvent.preventDefault();
} else if (
startEvent.shiftKey &&
startDOMCell &&
($anchor = cellAround(view.state.selection.$anchor)) != null &&
cellUnderMouse(view, startEvent)?.pos != $anchor.pos
) {
// Adding to a selection that starts in another cell (causing a
// cell selection to be created).
setCellSelection($anchor, startEvent);
startEvent.preventDefault();
} else if (!startDOMCell) {
// Not in a cell, let the default behavior happen.
return;
}
// Create and dispatch a cell selection between the given anchor and
// the position under the mouse.
function setCellSelection($anchor: ResolvedPos, event: MouseEvent): void {
let $head = cellUnderMouse(view, event);
const starting = tableEditingKey.getState(view.state) == null;
if (!$head || !inSameTable($anchor, $head)) {
if (starting) $head = $anchor;
else return;
}
const selection = new CellSelection($anchor, $head);
if (starting || !view.state.selection.eq(selection)) {
const tr = view.state.tr.setSelection(selection);
if (starting) tr.setMeta(tableEditingKey, $anchor.pos);
view.dispatch(tr);
}
}
// Stop listening to mouse motion events.
function stop(): void {
view.root.removeEventListener('mouseup', stop);
view.root.removeEventListener('dragstart', stop);
view.root.removeEventListener('mousemove', move);
if (tableEditingKey.getState(view.state) != null)
view.dispatch(view.state.tr.setMeta(tableEditingKey, -1));
}
function move(_event: Event): void {
const event = _event as MouseEvent;
const anchor = tableEditingKey.getState(view.state);
let $anchor;
if (anchor != null) {
// Continuing an existing cross-cell selection
$anchor = view.state.doc.resolve(anchor);
} else if (domInCell(view, event.target as Node) != startDOMCell) {
// Moving out of the initial cell -- start a new cell selection
$anchor = cellUnderMouse(view, startEvent);
if (!$anchor) return stop();
// Clear browser's native text selection when starting cross-cell selection
// This prevents text from being selected when first dragging across cells
const root = view.root as Document;
if (root.getSelection) {
const domSel = root.getSelection();
if (domSel) domSel.removeAllRanges();
}
}
if ($anchor) setCellSelection($anchor, event);
}
view.root.addEventListener('mouseup', stop);
view.root.addEventListener('dragstart', stop);
view.root.addEventListener('mousemove', move);
}
// Check whether the cursor is at the end of a cell (so that further
// motion would move out of the cell)
function atEndOfCell(view: EditorView, axis: Axis, dir: number): null | number {
if (!(view.state.selection instanceof TextSelection)) return null;
const { $head } = view.state.selection;
for (let d = $head.depth - 1; d >= 0; d--) {
const parent = $head.node(d),
index = dir < 0 ? $head.index(d) : $head.indexAfter(d);
if (index != (dir < 0 ? 0 : parent.childCount)) return null;
if (
parent.type.spec.tableRole == 'cell' ||
parent.type.spec.tableRole == 'header_cell'
) {
const cellPos = $head.before(d);
const dirStr: 'up' | 'down' | 'left' | 'right' =
axis == 'vert' ? (dir > 0 ? 'down' : 'up') : dir > 0 ? 'right' : 'left';
return view.endOfTextblock(dirStr) ? cellPos : null;
}
}
return null;
}
function domInCell(view: EditorView, dom: Node | null): Node | null {
for (; dom && dom != view.dom; dom = dom.parentNode) {
if (dom.nodeName == 'TD' || dom.nodeName == 'TH') {
return dom;
}
}
return null;
}
function cellUnderMouse(
view: EditorView,
event: MouseEvent,
): ResolvedPos | null {
const mousePos = view.posAtCoords({
left: event.clientX,
top: event.clientY,
});
if (!mousePos) return null;
// Prefer `inside` position for better accuracy with merged cells (rowspan/colspan),
// but fall back to `pos` if `inside` doesn't resolve to a valid cell
let { inside, pos } = mousePos;
return (
(inside >= 0 && cellAround(view.state.doc.resolve(inside))) ||
cellAround(view.state.doc.resolve(pos))
);
}