@kerebron/extension-tables 0.4.28 → 0.4.30
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/esm/ExtensionTables.js +1 -0
- package/esm/ExtensionTables.js.map +1 -0
- package/esm/NodeTable.js +1 -0
- package/esm/NodeTable.js.map +1 -0
- package/esm/NodeTableCell.js +1 -0
- package/esm/NodeTableCell.js.map +1 -0
- package/esm/NodeTableHeader.js +1 -0
- package/esm/NodeTableHeader.js.map +1 -0
- package/esm/NodeTableRow.js +1 -0
- package/esm/NodeTableRow.js.map +1 -0
- package/esm/_dnt.shims.js +1 -0
- package/esm/_dnt.shims.js.map +1 -0
- package/esm/utilities/CellSelection.js +1 -0
- package/esm/utilities/CellSelection.js.map +1 -0
- package/esm/utilities/TableMap.js +1 -0
- package/esm/utilities/TableMap.js.map +1 -0
- package/esm/utilities/TableView.js +1 -0
- package/esm/utilities/TableView.js.map +1 -0
- package/esm/utilities/columnResizing.js +1 -0
- package/esm/utilities/columnResizing.js.map +1 -0
- package/esm/utilities/commands.js +1 -0
- package/esm/utilities/commands.js.map +1 -0
- package/esm/utilities/copypaste.js +1 -0
- package/esm/utilities/copypaste.js.map +1 -0
- package/esm/utilities/createCell.js +1 -0
- package/esm/utilities/createCell.js.map +1 -0
- package/esm/utilities/createTable.js +1 -0
- package/esm/utilities/createTable.js.map +1 -0
- package/esm/utilities/fixTables.js +1 -0
- package/esm/utilities/fixTables.js.map +1 -0
- package/esm/utilities/getTableNodeTypes.js +1 -0
- package/esm/utilities/getTableNodeTypes.js.map +1 -0
- package/esm/utilities/input.js +1 -0
- package/esm/utilities/input.js.map +1 -0
- package/esm/utilities/tableEditing.js +1 -0
- package/esm/utilities/tableEditing.js.map +1 -0
- package/esm/utilities/tableNodeTypes.js +1 -0
- package/esm/utilities/tableNodeTypes.js.map +1 -0
- package/esm/utilities/util.js +1 -0
- package/esm/utilities/util.js.map +1 -0
- package/package.json +6 -2
- package/src/ExtensionTables.ts +16 -0
- package/src/NodeTable.ts +139 -0
- package/src/NodeTableCell.ts +70 -0
- package/src/NodeTableHeader.ts +49 -0
- package/src/NodeTableRow.ts +41 -0
- package/src/_dnt.shims.ts +60 -0
- package/src/utilities/CellSelection.ts +477 -0
- package/src/utilities/TableMap.ts +392 -0
- package/src/utilities/TableView.ts +102 -0
- package/src/utilities/columnResizing.ts +437 -0
- package/src/utilities/commands.ts +896 -0
- package/src/utilities/copypaste.ts +394 -0
- package/src/utilities/createCell.ts +12 -0
- package/src/utilities/createTable.ts +53 -0
- package/src/utilities/fixTables.ts +156 -0
- package/src/utilities/getTableNodeTypes.ts +21 -0
- package/src/utilities/input.ts +299 -0
- package/src/utilities/tableEditing.ts +90 -0
- package/src/utilities/tableNodeTypes.ts +32 -0
- package/src/utilities/util.ts +204 -0
- package/assets/tables.css +0 -85
|
@@ -0,0 +1,299 @@
|
|
|
1
|
+
// This file defines a number of helpers for wiring up user input to
|
|
2
|
+
// table-related functionality.
|
|
3
|
+
|
|
4
|
+
import { Fragment, ResolvedPos, Slice } from 'prosemirror-model';
|
|
5
|
+
import {
|
|
6
|
+
EditorState,
|
|
7
|
+
Selection,
|
|
8
|
+
TextSelection,
|
|
9
|
+
Transaction,
|
|
10
|
+
} from 'prosemirror-state';
|
|
11
|
+
|
|
12
|
+
import { EditorView } from 'prosemirror-view';
|
|
13
|
+
|
|
14
|
+
import { keydownHandler } from '@kerebron/editor/plugins/keymap';
|
|
15
|
+
import type { Command } from '@kerebron/editor/commands';
|
|
16
|
+
|
|
17
|
+
import { CellSelection } from './CellSelection.js';
|
|
18
|
+
import { deleteCellSelection } from './commands.js';
|
|
19
|
+
import { clipCells, fitSlice, insertCells, pastedCells } from './copypaste.js';
|
|
20
|
+
import { tableNodeTypes } from './tableNodeTypes.js';
|
|
21
|
+
import { TableMap } from './TableMap.js';
|
|
22
|
+
import {
|
|
23
|
+
cellAround,
|
|
24
|
+
inSameTable,
|
|
25
|
+
isInTable,
|
|
26
|
+
nextCell,
|
|
27
|
+
selectionCell,
|
|
28
|
+
tableEditingKey,
|
|
29
|
+
} from './util.js';
|
|
30
|
+
|
|
31
|
+
type Axis = 'horiz' | 'vert';
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* @public
|
|
35
|
+
*/
|
|
36
|
+
export type Direction = -1 | 1;
|
|
37
|
+
|
|
38
|
+
export const handleKeyDown = keydownHandler({
|
|
39
|
+
ArrowLeft: arrow('horiz', -1),
|
|
40
|
+
ArrowRight: arrow('horiz', 1),
|
|
41
|
+
// ArrowUp: arrow('vert', -1),
|
|
42
|
+
// ArrowDown: arrow('vert', 1),
|
|
43
|
+
|
|
44
|
+
'Shift-ArrowLeft': shiftArrow('horiz', -1),
|
|
45
|
+
'Shift-ArrowRight': shiftArrow('horiz', 1),
|
|
46
|
+
'Shift-ArrowUp': shiftArrow('vert', -1),
|
|
47
|
+
'Shift-ArrowDown': shiftArrow('vert', 1),
|
|
48
|
+
|
|
49
|
+
Backspace: deleteCellSelection,
|
|
50
|
+
'Mod-Backspace': deleteCellSelection,
|
|
51
|
+
Delete: deleteCellSelection,
|
|
52
|
+
'Mod-Delete': deleteCellSelection,
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
function maybeSetSelection(
|
|
56
|
+
state: EditorState,
|
|
57
|
+
dispatch: undefined | ((tr: Transaction) => void),
|
|
58
|
+
selection: Selection,
|
|
59
|
+
): boolean {
|
|
60
|
+
if (selection.eq(state.selection)) return false;
|
|
61
|
+
if (dispatch) dispatch(state.tr.setSelection(selection).scrollIntoView());
|
|
62
|
+
return true;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* @internal
|
|
67
|
+
*/
|
|
68
|
+
export function arrow(axis: Axis, dir: Direction): Command {
|
|
69
|
+
return (state, dispatch, view) => {
|
|
70
|
+
if (!view) return false;
|
|
71
|
+
const sel = state.selection;
|
|
72
|
+
if (sel instanceof CellSelection) {
|
|
73
|
+
return maybeSetSelection(
|
|
74
|
+
state,
|
|
75
|
+
dispatch,
|
|
76
|
+
Selection.near(sel.$headCell, dir),
|
|
77
|
+
);
|
|
78
|
+
}
|
|
79
|
+
if (axis != 'horiz' && !sel.empty) return false;
|
|
80
|
+
const end = atEndOfCell(view, axis, dir);
|
|
81
|
+
if (end == null) return false;
|
|
82
|
+
if (axis == 'horiz') {
|
|
83
|
+
return maybeSetSelection(
|
|
84
|
+
state,
|
|
85
|
+
dispatch,
|
|
86
|
+
Selection.near(state.doc.resolve(sel.head + dir), dir),
|
|
87
|
+
);
|
|
88
|
+
} else {
|
|
89
|
+
const $cell = state.doc.resolve(end);
|
|
90
|
+
const $next = nextCell($cell, axis, dir);
|
|
91
|
+
let newSel;
|
|
92
|
+
if ($next) newSel = Selection.near($next, 1);
|
|
93
|
+
else if (dir < 0) {
|
|
94
|
+
newSel = Selection.near(state.doc.resolve($cell.before(-1)), -1);
|
|
95
|
+
} else newSel = Selection.near(state.doc.resolve($cell.after(-1)), 1);
|
|
96
|
+
return maybeSetSelection(state, dispatch, newSel);
|
|
97
|
+
}
|
|
98
|
+
};
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
function shiftArrow(axis: Axis, dir: Direction): Command {
|
|
102
|
+
return (state, dispatch, view) => {
|
|
103
|
+
if (!view) return false;
|
|
104
|
+
const sel = state.selection;
|
|
105
|
+
let cellSel: CellSelection;
|
|
106
|
+
if (sel instanceof CellSelection) {
|
|
107
|
+
cellSel = sel;
|
|
108
|
+
} else {
|
|
109
|
+
const end = atEndOfCell(view, axis, dir);
|
|
110
|
+
if (end == null) return false;
|
|
111
|
+
cellSel = new CellSelection(state.doc.resolve(end));
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
const $head = nextCell(cellSel.$headCell, axis, dir);
|
|
115
|
+
if (!$head) return false;
|
|
116
|
+
return maybeSetSelection(
|
|
117
|
+
state,
|
|
118
|
+
dispatch,
|
|
119
|
+
new CellSelection(cellSel.$anchorCell, $head),
|
|
120
|
+
);
|
|
121
|
+
};
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
export function handleTripleClick(view: EditorView, pos: number): boolean {
|
|
125
|
+
const doc = view.state.doc,
|
|
126
|
+
$cell = cellAround(doc.resolve(pos));
|
|
127
|
+
if (!$cell) return false;
|
|
128
|
+
view.dispatch(view.state.tr.setSelection(new CellSelection($cell)));
|
|
129
|
+
return true;
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
/**
|
|
133
|
+
* @public
|
|
134
|
+
*/
|
|
135
|
+
export function handlePaste(
|
|
136
|
+
view: EditorView,
|
|
137
|
+
_: ClipboardEvent,
|
|
138
|
+
slice: Slice,
|
|
139
|
+
): boolean {
|
|
140
|
+
if (!isInTable(view.state)) return false;
|
|
141
|
+
let cells = pastedCells(slice);
|
|
142
|
+
const sel = view.state.selection;
|
|
143
|
+
if (sel instanceof CellSelection) {
|
|
144
|
+
if (!cells) {
|
|
145
|
+
cells = {
|
|
146
|
+
width: 1,
|
|
147
|
+
height: 1,
|
|
148
|
+
rows: [
|
|
149
|
+
Fragment.from(
|
|
150
|
+
fitSlice(tableNodeTypes(view.state.schema).cell, slice),
|
|
151
|
+
),
|
|
152
|
+
],
|
|
153
|
+
};
|
|
154
|
+
}
|
|
155
|
+
const table = sel.$anchorCell.node(-1);
|
|
156
|
+
const start = sel.$anchorCell.start(-1);
|
|
157
|
+
const rect = TableMap.get(table).rectBetween(
|
|
158
|
+
sel.$anchorCell.pos - start,
|
|
159
|
+
sel.$headCell.pos - start,
|
|
160
|
+
);
|
|
161
|
+
cells = clipCells(cells, rect.right - rect.left, rect.bottom - rect.top);
|
|
162
|
+
insertCells(view.state, view.dispatch, start, rect, cells);
|
|
163
|
+
return true;
|
|
164
|
+
} else if (cells) {
|
|
165
|
+
const $cell = selectionCell(view.state);
|
|
166
|
+
const start = $cell.start(-1);
|
|
167
|
+
insertCells(
|
|
168
|
+
view.state,
|
|
169
|
+
view.dispatch,
|
|
170
|
+
start,
|
|
171
|
+
TableMap.get($cell.node(-1)).findCell($cell.pos - start),
|
|
172
|
+
cells,
|
|
173
|
+
);
|
|
174
|
+
return true;
|
|
175
|
+
} else {
|
|
176
|
+
return false;
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
export function handleMouseDown(
|
|
181
|
+
view: EditorView,
|
|
182
|
+
startEvent: MouseEvent,
|
|
183
|
+
): void {
|
|
184
|
+
if (startEvent.ctrlKey || startEvent.metaKey) return;
|
|
185
|
+
|
|
186
|
+
const startDOMCell = domInCell(view, startEvent.target as Node);
|
|
187
|
+
let $anchor;
|
|
188
|
+
if (startEvent.shiftKey && view.state.selection instanceof CellSelection) {
|
|
189
|
+
// Adding to an existing cell selection
|
|
190
|
+
setCellSelection(view.state.selection.$anchorCell, startEvent);
|
|
191
|
+
startEvent.preventDefault();
|
|
192
|
+
} else if (
|
|
193
|
+
startEvent.shiftKey &&
|
|
194
|
+
startDOMCell &&
|
|
195
|
+
($anchor = cellAround(view.state.selection.$anchor)) != null &&
|
|
196
|
+
cellUnderMouse(view, startEvent)?.pos != $anchor.pos
|
|
197
|
+
) {
|
|
198
|
+
// Adding to a selection that starts in another cell (causing a
|
|
199
|
+
// cell selection to be created).
|
|
200
|
+
setCellSelection($anchor, startEvent);
|
|
201
|
+
startEvent.preventDefault();
|
|
202
|
+
} else if (!startDOMCell) {
|
|
203
|
+
// Not in a cell, let the default behavior happen.
|
|
204
|
+
return;
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
// Create and dispatch a cell selection between the given anchor and
|
|
208
|
+
// the position under the mouse.
|
|
209
|
+
function setCellSelection($anchor: ResolvedPos, event: MouseEvent): void {
|
|
210
|
+
let $head = cellUnderMouse(view, event);
|
|
211
|
+
const starting = tableEditingKey.getState(view.state) == null;
|
|
212
|
+
if (!$head || !inSameTable($anchor, $head)) {
|
|
213
|
+
if (starting) $head = $anchor;
|
|
214
|
+
else return;
|
|
215
|
+
}
|
|
216
|
+
const selection = new CellSelection($anchor, $head);
|
|
217
|
+
if (starting || !view.state.selection.eq(selection)) {
|
|
218
|
+
const tr = view.state.tr.setSelection(selection);
|
|
219
|
+
if (starting) tr.setMeta(tableEditingKey, $anchor.pos);
|
|
220
|
+
view.dispatch(tr);
|
|
221
|
+
}
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
// Stop listening to mouse motion events.
|
|
225
|
+
function stop(): void {
|
|
226
|
+
view.root.removeEventListener('mouseup', stop);
|
|
227
|
+
view.root.removeEventListener('dragstart', stop);
|
|
228
|
+
view.root.removeEventListener('mousemove', move);
|
|
229
|
+
if (tableEditingKey.getState(view.state) != null) {
|
|
230
|
+
view.dispatch(view.state.tr.setMeta(tableEditingKey, -1));
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
function move(_event: Event): void {
|
|
235
|
+
const event = _event as MouseEvent;
|
|
236
|
+
const anchor = tableEditingKey.getState(view.state);
|
|
237
|
+
let $anchor;
|
|
238
|
+
if (anchor != null) {
|
|
239
|
+
// Continuing an existing cross-cell selection
|
|
240
|
+
$anchor = view.state.doc.resolve(anchor);
|
|
241
|
+
} else if (domInCell(view, event.target as Node) != startDOMCell) {
|
|
242
|
+
// Moving out of the initial cell -- start a new cell selection
|
|
243
|
+
$anchor = cellUnderMouse(view, startEvent);
|
|
244
|
+
if (!$anchor) return stop();
|
|
245
|
+
}
|
|
246
|
+
if ($anchor) setCellSelection($anchor, event);
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
view.root.addEventListener('mouseup', stop);
|
|
250
|
+
view.root.addEventListener('dragstart', stop);
|
|
251
|
+
view.root.addEventListener('mousemove', move);
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
// Check whether the cursor is at the end of a cell (so that further
|
|
255
|
+
// motion would move out of the cell)
|
|
256
|
+
function atEndOfCell(view: EditorView, axis: Axis, dir: number): null | number {
|
|
257
|
+
if (!(view.state.selection instanceof TextSelection)) return null;
|
|
258
|
+
|
|
259
|
+
const { $head } = view.state.selection;
|
|
260
|
+
for (let d = $head.depth - 1; d >= 0; d--) {
|
|
261
|
+
const parent = $head.node(d),
|
|
262
|
+
index = dir < 0 ? $head.index(d) : $head.indexAfter(d);
|
|
263
|
+
if (index != (dir < 0 ? 0 : parent.childCount)) return null;
|
|
264
|
+
if (
|
|
265
|
+
parent.type.spec.tableRole == 'cell' ||
|
|
266
|
+
parent.type.spec.tableRole == 'header_cell'
|
|
267
|
+
) {
|
|
268
|
+
const cellPos = $head.before(d);
|
|
269
|
+
const dirStr: 'up' | 'down' | 'left' | 'right' = axis == 'vert'
|
|
270
|
+
? (dir > 0 ? 'down' : 'up')
|
|
271
|
+
: dir > 0
|
|
272
|
+
? 'right'
|
|
273
|
+
: 'left';
|
|
274
|
+
return view.endOfTextblock(dirStr) ? cellPos : null;
|
|
275
|
+
}
|
|
276
|
+
}
|
|
277
|
+
return null;
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
function domInCell(view: EditorView, dom: Node | null): Node | null {
|
|
281
|
+
for (; dom && dom != view.dom; dom = dom.parentNode) {
|
|
282
|
+
if (dom.nodeName == 'TD' || dom.nodeName == 'TH') {
|
|
283
|
+
return dom;
|
|
284
|
+
}
|
|
285
|
+
}
|
|
286
|
+
return null;
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
function cellUnderMouse(
|
|
290
|
+
view: EditorView,
|
|
291
|
+
event: MouseEvent,
|
|
292
|
+
): ResolvedPos | null {
|
|
293
|
+
const mousePos = view.posAtCoords({
|
|
294
|
+
left: event.clientX,
|
|
295
|
+
top: event.clientY,
|
|
296
|
+
});
|
|
297
|
+
if (!mousePos) return null;
|
|
298
|
+
return mousePos ? cellAround(view.state.doc.resolve(mousePos.pos)) : null;
|
|
299
|
+
}
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
// This file defines a plugin that handles the drawing of cell
|
|
2
|
+
// selections and the basic user interactions for creating and working
|
|
3
|
+
// with such selections. It also makes sure that, after each
|
|
4
|
+
// transaction, the shapes of tables are normalized to be rectangular
|
|
5
|
+
// and not contain overlapping cells.
|
|
6
|
+
|
|
7
|
+
import { Plugin } from 'prosemirror-state';
|
|
8
|
+
|
|
9
|
+
import { drawCellSelection, normalizeSelection } from './CellSelection.js';
|
|
10
|
+
import { fixTables, fixTablesKey } from './fixTables.js';
|
|
11
|
+
import {
|
|
12
|
+
handleKeyDown,
|
|
13
|
+
handleMouseDown,
|
|
14
|
+
handlePaste,
|
|
15
|
+
handleTripleClick,
|
|
16
|
+
} from './input.js';
|
|
17
|
+
import { tableEditingKey } from './util.js';
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* @public
|
|
21
|
+
*/
|
|
22
|
+
export type TableEditingOptions = {
|
|
23
|
+
allowTableNodeSelection?: boolean;
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* Creates a [plugin](http://prosemirror.net/docs/ref/#state.Plugin)
|
|
28
|
+
* that, when added to an editor, enables cell-selection, handles
|
|
29
|
+
* cell-based copy/paste, and makes sure tables stay well-formed (each
|
|
30
|
+
* row has the same width, and cells don't overlap).
|
|
31
|
+
*
|
|
32
|
+
* You should probably put this plugin near the end of your array of
|
|
33
|
+
* plugins, since it handles mouse and arrow key events in tables
|
|
34
|
+
* rather broadly, and other plugins, like the gap cursor or the
|
|
35
|
+
* column-width dragging plugin, might want to get a turn first to
|
|
36
|
+
* perform more specific behavior.
|
|
37
|
+
*
|
|
38
|
+
* @public
|
|
39
|
+
*/
|
|
40
|
+
export function tableEditing({
|
|
41
|
+
allowTableNodeSelection = false,
|
|
42
|
+
}: TableEditingOptions = {}): Plugin {
|
|
43
|
+
return new Plugin({
|
|
44
|
+
key: tableEditingKey,
|
|
45
|
+
|
|
46
|
+
// This piece of state is used to remember when a mouse-drag
|
|
47
|
+
// cell-selection is happening, so that it can continue even as
|
|
48
|
+
// transactions (which might move its anchor cell) come in.
|
|
49
|
+
state: {
|
|
50
|
+
init() {
|
|
51
|
+
return null;
|
|
52
|
+
},
|
|
53
|
+
apply(tr, cur) {
|
|
54
|
+
const set = tr.getMeta(tableEditingKey);
|
|
55
|
+
if (set != null) return set == -1 ? null : set;
|
|
56
|
+
if (cur == null || !tr.docChanged) return cur;
|
|
57
|
+
const { deleted, pos } = tr.mapping.mapResult(cur);
|
|
58
|
+
return deleted ? null : pos;
|
|
59
|
+
},
|
|
60
|
+
},
|
|
61
|
+
|
|
62
|
+
props: {
|
|
63
|
+
decorations: drawCellSelection,
|
|
64
|
+
|
|
65
|
+
handleDOMEvents: {
|
|
66
|
+
mousedown: handleMouseDown,
|
|
67
|
+
},
|
|
68
|
+
|
|
69
|
+
createSelectionBetween(view) {
|
|
70
|
+
return tableEditingKey.getState(view.state) != null
|
|
71
|
+
? view.state.selection
|
|
72
|
+
: null;
|
|
73
|
+
},
|
|
74
|
+
|
|
75
|
+
handleTripleClick,
|
|
76
|
+
|
|
77
|
+
handleKeyDown,
|
|
78
|
+
|
|
79
|
+
handlePaste,
|
|
80
|
+
},
|
|
81
|
+
|
|
82
|
+
appendTransaction(_, oldState, state) {
|
|
83
|
+
return normalizeSelection(
|
|
84
|
+
state,
|
|
85
|
+
fixTables(state, oldState),
|
|
86
|
+
allowTableNodeSelection,
|
|
87
|
+
);
|
|
88
|
+
},
|
|
89
|
+
});
|
|
90
|
+
}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
// Helper for creating a schema that supports tables.
|
|
2
|
+
|
|
3
|
+
import { NodeSpec, NodeType, Schema } from 'prosemirror-model';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* @public
|
|
7
|
+
*/
|
|
8
|
+
export type TableNodes = Record<
|
|
9
|
+
'table' | 'table_row' | 'table_cell' | 'table_header',
|
|
10
|
+
NodeSpec
|
|
11
|
+
>;
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* @public
|
|
15
|
+
*/
|
|
16
|
+
export type TableRole = 'table' | 'row' | 'cell' | 'header_cell';
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* @public
|
|
20
|
+
*/
|
|
21
|
+
export function tableNodeTypes(schema: Schema): Record<TableRole, NodeType> {
|
|
22
|
+
let result = schema.cached.tableNodeTypes;
|
|
23
|
+
if (!result) {
|
|
24
|
+
result = schema.cached.tableNodeTypes = {};
|
|
25
|
+
for (const name in schema.nodes) {
|
|
26
|
+
const type = schema.nodes[name],
|
|
27
|
+
role = type.spec.tableRole;
|
|
28
|
+
if (role) result[role] = type;
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
return result;
|
|
32
|
+
}
|
|
@@ -0,0 +1,204 @@
|
|
|
1
|
+
// Various helper function for working with tables
|
|
2
|
+
|
|
3
|
+
import { EditorState, NodeSelection, PluginKey } from 'prosemirror-state';
|
|
4
|
+
|
|
5
|
+
import { Attrs, Node, ResolvedPos } from 'prosemirror-model';
|
|
6
|
+
import { CellSelection } from './CellSelection.js';
|
|
7
|
+
import { tableNodeTypes } from './tableNodeTypes.js';
|
|
8
|
+
import { Rect, TableMap } from './TableMap.js';
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* @public
|
|
12
|
+
*/
|
|
13
|
+
export type MutableAttrs = Record<string, unknown>;
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* @public
|
|
17
|
+
*/
|
|
18
|
+
export interface CellAttrs {
|
|
19
|
+
colspan: number;
|
|
20
|
+
rowspan: number;
|
|
21
|
+
colwidth: number[] | null;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* @public
|
|
26
|
+
*/
|
|
27
|
+
export const tableEditingKey = new PluginKey<number>('selectingCells');
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* @public
|
|
31
|
+
*/
|
|
32
|
+
export function cellAround($pos: ResolvedPos): ResolvedPos | null {
|
|
33
|
+
for (let d = $pos.depth - 1; d > 0; d--) {
|
|
34
|
+
if ($pos.node(d).type.spec.tableRole == 'row') {
|
|
35
|
+
return $pos.node(0).resolve($pos.before(d + 1));
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
return null;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
export function cellWrapping($pos: ResolvedPos): null | Node {
|
|
42
|
+
for (let d = $pos.depth; d > 0; d--) {
|
|
43
|
+
// Sometimes the cell can be in the same depth.
|
|
44
|
+
const role = $pos.node(d).type.spec.tableRole;
|
|
45
|
+
if (role === 'cell' || role === 'header_cell') return $pos.node(d);
|
|
46
|
+
}
|
|
47
|
+
return null;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* @public
|
|
52
|
+
*/
|
|
53
|
+
export function isInTable(state: EditorState): boolean {
|
|
54
|
+
const $head = state.selection.$head;
|
|
55
|
+
for (let d = $head.depth; d > 0; d--) {
|
|
56
|
+
if ($head.node(d).type.spec.tableRole == 'row') return true;
|
|
57
|
+
}
|
|
58
|
+
return false;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* @internal
|
|
63
|
+
*/
|
|
64
|
+
export function selectionCell(state: EditorState): ResolvedPos {
|
|
65
|
+
const sel = state.selection as CellSelection | NodeSelection;
|
|
66
|
+
if ('$anchorCell' in sel && sel.$anchorCell) {
|
|
67
|
+
return sel.$anchorCell.pos > sel.$headCell.pos
|
|
68
|
+
? sel.$anchorCell
|
|
69
|
+
: sel.$headCell;
|
|
70
|
+
} else if (
|
|
71
|
+
'node' in sel &&
|
|
72
|
+
sel.node &&
|
|
73
|
+
sel.node.type.spec.tableRole == 'cell'
|
|
74
|
+
) {
|
|
75
|
+
return sel.$anchor;
|
|
76
|
+
}
|
|
77
|
+
const $cell = cellAround(sel.$head) || cellNear(sel.$head);
|
|
78
|
+
if ($cell) {
|
|
79
|
+
return $cell;
|
|
80
|
+
}
|
|
81
|
+
throw new RangeError(`No cell found around position ${sel.head}`);
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
/**
|
|
85
|
+
* @public
|
|
86
|
+
*/
|
|
87
|
+
export function cellNear($pos: ResolvedPos): ResolvedPos | undefined {
|
|
88
|
+
for (
|
|
89
|
+
let after = $pos.nodeAfter, pos = $pos.pos;
|
|
90
|
+
after;
|
|
91
|
+
after = after.firstChild, pos++
|
|
92
|
+
) {
|
|
93
|
+
const role = after.type.spec.tableRole;
|
|
94
|
+
if (role == 'cell' || role == 'header_cell') return $pos.doc.resolve(pos);
|
|
95
|
+
}
|
|
96
|
+
for (
|
|
97
|
+
let before = $pos.nodeBefore, pos = $pos.pos;
|
|
98
|
+
before;
|
|
99
|
+
before = before.lastChild, pos--
|
|
100
|
+
) {
|
|
101
|
+
const role = before.type.spec.tableRole;
|
|
102
|
+
if (role == 'cell' || role == 'header_cell') {
|
|
103
|
+
return $pos.doc.resolve(pos - before.nodeSize);
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
/**
|
|
109
|
+
* @public
|
|
110
|
+
*/
|
|
111
|
+
export function pointsAtCell($pos: ResolvedPos): boolean {
|
|
112
|
+
return $pos.parent.type.spec.tableRole == 'row' && !!$pos.nodeAfter;
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
/**
|
|
116
|
+
* @public
|
|
117
|
+
*/
|
|
118
|
+
export function moveCellForward($pos: ResolvedPos): ResolvedPos {
|
|
119
|
+
return $pos.node(0).resolve($pos.pos + $pos.nodeAfter!.nodeSize);
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
/**
|
|
123
|
+
* @internal
|
|
124
|
+
*/
|
|
125
|
+
export function inSameTable($cellA: ResolvedPos, $cellB: ResolvedPos): boolean {
|
|
126
|
+
return (
|
|
127
|
+
$cellA.depth == $cellB.depth &&
|
|
128
|
+
$cellA.pos >= $cellB.start(-1) &&
|
|
129
|
+
$cellA.pos <= $cellB.end(-1)
|
|
130
|
+
);
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
/**
|
|
134
|
+
* @public
|
|
135
|
+
*/
|
|
136
|
+
export function findCell($pos: ResolvedPos): Rect {
|
|
137
|
+
return TableMap.get($pos.node(-1)).findCell($pos.pos - $pos.start(-1));
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
/**
|
|
141
|
+
* @public
|
|
142
|
+
*/
|
|
143
|
+
export function colCount($pos: ResolvedPos): number {
|
|
144
|
+
return TableMap.get($pos.node(-1)).colCount($pos.pos - $pos.start(-1));
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
/**
|
|
148
|
+
* @public
|
|
149
|
+
*/
|
|
150
|
+
export function nextCell(
|
|
151
|
+
$pos: ResolvedPos,
|
|
152
|
+
axis: 'horiz' | 'vert',
|
|
153
|
+
dir: number,
|
|
154
|
+
): ResolvedPos | null {
|
|
155
|
+
const table = $pos.node(-1);
|
|
156
|
+
const map = TableMap.get(table);
|
|
157
|
+
const tableStart = $pos.start(-1);
|
|
158
|
+
|
|
159
|
+
const moved = map.nextCell($pos.pos - tableStart, axis, dir);
|
|
160
|
+
return moved == null ? null : $pos.node(0).resolve(tableStart + moved);
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
/**
|
|
164
|
+
* @public
|
|
165
|
+
*/
|
|
166
|
+
export function removeColSpan(attrs: CellAttrs, pos: number, n = 1): CellAttrs {
|
|
167
|
+
const result: CellAttrs = { ...attrs, colspan: attrs.colspan - n };
|
|
168
|
+
|
|
169
|
+
if (result.colwidth) {
|
|
170
|
+
result.colwidth = result.colwidth.slice();
|
|
171
|
+
result.colwidth.splice(pos, n);
|
|
172
|
+
if (!result.colwidth.some((w) => w > 0)) result.colwidth = null;
|
|
173
|
+
}
|
|
174
|
+
return result;
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
/**
|
|
178
|
+
* @public
|
|
179
|
+
*/
|
|
180
|
+
export function addColSpan(attrs: CellAttrs, pos: number, n = 1): Attrs {
|
|
181
|
+
const result = { ...attrs, colspan: attrs.colspan + n };
|
|
182
|
+
if (result.colwidth) {
|
|
183
|
+
result.colwidth = result.colwidth.slice();
|
|
184
|
+
for (let i = 0; i < n; i++) result.colwidth.splice(pos, 0, 0);
|
|
185
|
+
}
|
|
186
|
+
return result;
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
/**
|
|
190
|
+
* @public
|
|
191
|
+
*/
|
|
192
|
+
export function columnIsHeader(
|
|
193
|
+
map: TableMap,
|
|
194
|
+
table: Node,
|
|
195
|
+
col: number,
|
|
196
|
+
): boolean {
|
|
197
|
+
const headerCell = tableNodeTypes(table.type.schema).header_cell;
|
|
198
|
+
for (let row = 0; row < map.height; row++) {
|
|
199
|
+
if (table.nodeAt(map.map[col + row * map.width])!.type != headerCell) {
|
|
200
|
+
return false;
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
return true;
|
|
204
|
+
}
|
package/assets/tables.css
DELETED
|
@@ -1,85 +0,0 @@
|
|
|
1
|
-
.kb-editor .tableWrapper {
|
|
2
|
-
overflow-x: auto;
|
|
3
|
-
}
|
|
4
|
-
.kb-editor table {
|
|
5
|
-
border-collapse: collapse;
|
|
6
|
-
table-layout: fixed;
|
|
7
|
-
width: 100%;
|
|
8
|
-
overflow: hidden;
|
|
9
|
-
}
|
|
10
|
-
.kb-editor td,
|
|
11
|
-
.kb-editor th {
|
|
12
|
-
vertical-align: top;
|
|
13
|
-
box-sizing: border-box;
|
|
14
|
-
position: relative;
|
|
15
|
-
border: 1px solid var(--kb-color-border);
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
.kb-editor td:not([data-colwidth]):not(.column-resize-dragging),
|
|
19
|
-
.kb-editor th:not([data-colwidth]):not(.column-resize-dragging) {
|
|
20
|
-
/* if there's no explicit width set and the column is not being resized, set a default width */
|
|
21
|
-
min-width: var(--default-cell-min-width);
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
.kb-editor .column-resize-handle {
|
|
25
|
-
position: absolute;
|
|
26
|
-
right: -2px;
|
|
27
|
-
top: 0;
|
|
28
|
-
bottom: 0;
|
|
29
|
-
width: 4px;
|
|
30
|
-
z-index: 20;
|
|
31
|
-
background-color: #adf;
|
|
32
|
-
pointer-events: none;
|
|
33
|
-
}
|
|
34
|
-
.kb-editor.resize-cursor {
|
|
35
|
-
cursor: ew-resize;
|
|
36
|
-
cursor: col-resize;
|
|
37
|
-
}
|
|
38
|
-
/* Give selected cells a blue overlay */
|
|
39
|
-
.kb-editor .selectedCell:after {
|
|
40
|
-
z-index: 2;
|
|
41
|
-
position: absolute;
|
|
42
|
-
content: '';
|
|
43
|
-
left: 0;
|
|
44
|
-
right: 0;
|
|
45
|
-
top: 0;
|
|
46
|
-
bottom: 0;
|
|
47
|
-
background: rgba(200, 200, 255, 0.4);
|
|
48
|
-
pointer-events: none;
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
/* Touch-optimized table editing */
|
|
52
|
-
@media (max-width: 767px) {
|
|
53
|
-
.kb-editor table {
|
|
54
|
-
font-size: var(--kb-text-sm);
|
|
55
|
-
border-collapse: collapse;
|
|
56
|
-
width: 100%;
|
|
57
|
-
overflow-x: auto;
|
|
58
|
-
display: block;
|
|
59
|
-
white-space: nowrap;
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
.kb-editor th,
|
|
63
|
-
.kb-editor td {
|
|
64
|
-
min-width: 120px;
|
|
65
|
-
padding: var(--kb-space-md);
|
|
66
|
-
border: 1px solid var(--kb-color-border);
|
|
67
|
-
vertical-align: top;
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
/* Hide complex table controls on mobile */
|
|
71
|
-
.tableWrapper .column-resize-handle {
|
|
72
|
-
display: none !important;
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
.kb-editor .tableWrapper {
|
|
76
|
-
overflow-x: auto;
|
|
77
|
-
-webkit-overflow-scrolling: touch;
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
/* Table selection visual feedback */
|
|
81
|
-
.kb-editor .selectedCell {
|
|
82
|
-
background: rgba(var(--kb-color-primary-rgb), 0.1);
|
|
83
|
-
border: 2px solid var(--kb-color-primary);
|
|
84
|
-
}
|
|
85
|
-
}
|