@kerebron/extension-tables 0.0.1
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 +23 -0
- package/README.md +34 -0
- package/assets/tables.css +48 -0
- package/esm/ExtensionTables.d.ts +10 -0
- package/esm/ExtensionTables.d.ts.map +1 -0
- package/esm/ExtensionTables.js +27 -0
- package/esm/NodeTable.d.ts +24 -0
- package/esm/NodeTable.d.ts.map +1 -0
- package/esm/NodeTable.js +115 -0
- package/esm/NodeTableCell.d.ts +16 -0
- package/esm/NodeTableCell.d.ts.map +1 -0
- package/esm/NodeTableCell.js +77 -0
- package/esm/NodeTableHeader.d.ts +16 -0
- package/esm/NodeTableHeader.d.ts.map +1 -0
- package/esm/NodeTableHeader.js +75 -0
- package/esm/NodeTableRow.d.ts +16 -0
- package/esm/NodeTableRow.d.ts.map +1 -0
- package/esm/NodeTableRow.js +47 -0
- package/esm/_dnt.shims.d.ts +6 -0
- package/esm/_dnt.shims.d.ts.map +1 -0
- package/esm/_dnt.shims.js +61 -0
- package/esm/package.json +3 -0
- package/esm/utilities/CellSelection.d.ts +53 -0
- package/esm/utilities/CellSelection.d.ts.map +1 -0
- package/esm/utilities/CellSelection.js +382 -0
- package/esm/utilities/TableMap.d.ts +92 -0
- package/esm/utilities/TableMap.d.ts.map +1 -0
- package/esm/utilities/TableMap.js +335 -0
- package/esm/utilities/TableView.d.ts +21 -0
- package/esm/utilities/TableView.d.ts.map +1 -0
- package/esm/utilities/TableView.js +108 -0
- package/esm/utilities/columnResizing.d.ts +50 -0
- package/esm/utilities/columnResizing.d.ts.map +1 -0
- package/esm/utilities/columnResizing.js +307 -0
- package/esm/utilities/commands.d.ts +166 -0
- package/esm/utilities/commands.d.ts.map +1 -0
- package/esm/utilities/commands.js +702 -0
- package/esm/utilities/copypaste.d.ts +35 -0
- package/esm/utilities/copypaste.d.ts.map +1 -0
- package/esm/utilities/copypaste.js +283 -0
- package/esm/utilities/createCell.d.ts +3 -0
- package/esm/utilities/createCell.d.ts.map +1 -0
- package/esm/utilities/createCell.js +6 -0
- package/esm/utilities/createTable.d.ts +3 -0
- package/esm/utilities/createTable.d.ts.map +1 -0
- package/esm/utilities/createTable.js +30 -0
- package/esm/utilities/fixTables.d.ts +18 -0
- package/esm/utilities/fixTables.d.ts.map +1 -0
- package/esm/utilities/fixTables.js +146 -0
- package/esm/utilities/getTableNodeTypes.d.ts +5 -0
- package/esm/utilities/getTableNodeTypes.d.ts.map +1 -0
- package/esm/utilities/getTableNodeTypes.js +14 -0
- package/esm/utilities/input.d.ts +21 -0
- package/esm/utilities/input.d.ts.map +1 -0
- package/esm/utilities/input.js +241 -0
- package/esm/utilities/tableEditing.d.ts +23 -0
- package/esm/utilities/tableEditing.d.ts.map +1 -0
- package/esm/utilities/tableEditing.js +63 -0
- package/esm/utilities/tableNodeTypes.d.ts +14 -0
- package/esm/utilities/tableNodeTypes.d.ts.map +1 -0
- package/esm/utilities/tableNodeTypes.js +16 -0
- package/esm/utilities/util.d.ts +73 -0
- package/esm/utilities/util.d.ts.map +1 -0
- package/esm/utilities/util.js +155 -0
- package/package.json +30 -0
|
@@ -0,0 +1,241 @@
|
|
|
1
|
+
// This file defines a number of helpers for wiring up user input to
|
|
2
|
+
// table-related functionality.
|
|
3
|
+
import { keydownHandler } from 'prosemirror-keymap';
|
|
4
|
+
import { Fragment } from 'prosemirror-model';
|
|
5
|
+
import { Selection, TextSelection, } from 'prosemirror-state';
|
|
6
|
+
import { CellSelection } from './CellSelection.js';
|
|
7
|
+
import { deleteCellSelection } from './commands.js';
|
|
8
|
+
import { clipCells, fitSlice, insertCells, pastedCells } from './copypaste.js';
|
|
9
|
+
import { tableNodeTypes } from './tableNodeTypes.js';
|
|
10
|
+
import { TableMap } from './TableMap.js';
|
|
11
|
+
import { cellAround, inSameTable, isInTable, nextCell, selectionCell, tableEditingKey, } from './util.js';
|
|
12
|
+
export const handleKeyDown = keydownHandler({
|
|
13
|
+
ArrowLeft: arrow('horiz', -1),
|
|
14
|
+
ArrowRight: arrow('horiz', 1),
|
|
15
|
+
// ArrowUp: arrow('vert', -1),
|
|
16
|
+
// ArrowDown: arrow('vert', 1),
|
|
17
|
+
'Shift-ArrowLeft': shiftArrow('horiz', -1),
|
|
18
|
+
'Shift-ArrowRight': shiftArrow('horiz', 1),
|
|
19
|
+
'Shift-ArrowUp': shiftArrow('vert', -1),
|
|
20
|
+
'Shift-ArrowDown': shiftArrow('vert', 1),
|
|
21
|
+
Backspace: deleteCellSelection,
|
|
22
|
+
'Mod-Backspace': deleteCellSelection,
|
|
23
|
+
Delete: deleteCellSelection,
|
|
24
|
+
'Mod-Delete': deleteCellSelection,
|
|
25
|
+
});
|
|
26
|
+
function maybeSetSelection(state, dispatch, selection) {
|
|
27
|
+
if (selection.eq(state.selection))
|
|
28
|
+
return false;
|
|
29
|
+
if (dispatch)
|
|
30
|
+
dispatch(state.tr.setSelection(selection).scrollIntoView());
|
|
31
|
+
return true;
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* @internal
|
|
35
|
+
*/
|
|
36
|
+
export function arrow(axis, dir) {
|
|
37
|
+
return (state, dispatch, view) => {
|
|
38
|
+
if (!view)
|
|
39
|
+
return false;
|
|
40
|
+
const sel = state.selection;
|
|
41
|
+
if (sel instanceof CellSelection) {
|
|
42
|
+
return maybeSetSelection(state, dispatch, Selection.near(sel.$headCell, dir));
|
|
43
|
+
}
|
|
44
|
+
if (axis != 'horiz' && !sel.empty)
|
|
45
|
+
return false;
|
|
46
|
+
const end = atEndOfCell(view, axis, dir);
|
|
47
|
+
if (end == null)
|
|
48
|
+
return false;
|
|
49
|
+
if (axis == 'horiz') {
|
|
50
|
+
return maybeSetSelection(state, dispatch, Selection.near(state.doc.resolve(sel.head + dir), dir));
|
|
51
|
+
}
|
|
52
|
+
else {
|
|
53
|
+
const $cell = state.doc.resolve(end);
|
|
54
|
+
const $next = nextCell($cell, axis, dir);
|
|
55
|
+
let newSel;
|
|
56
|
+
if ($next)
|
|
57
|
+
newSel = Selection.near($next, 1);
|
|
58
|
+
else if (dir < 0) {
|
|
59
|
+
newSel = Selection.near(state.doc.resolve($cell.before(-1)), -1);
|
|
60
|
+
}
|
|
61
|
+
else
|
|
62
|
+
newSel = Selection.near(state.doc.resolve($cell.after(-1)), 1);
|
|
63
|
+
return maybeSetSelection(state, dispatch, newSel);
|
|
64
|
+
}
|
|
65
|
+
};
|
|
66
|
+
}
|
|
67
|
+
function shiftArrow(axis, dir) {
|
|
68
|
+
return (state, dispatch, view) => {
|
|
69
|
+
if (!view)
|
|
70
|
+
return false;
|
|
71
|
+
const sel = state.selection;
|
|
72
|
+
let cellSel;
|
|
73
|
+
if (sel instanceof CellSelection) {
|
|
74
|
+
cellSel = sel;
|
|
75
|
+
}
|
|
76
|
+
else {
|
|
77
|
+
const end = atEndOfCell(view, axis, dir);
|
|
78
|
+
if (end == null)
|
|
79
|
+
return false;
|
|
80
|
+
cellSel = new CellSelection(state.doc.resolve(end));
|
|
81
|
+
}
|
|
82
|
+
const $head = nextCell(cellSel.$headCell, axis, dir);
|
|
83
|
+
if (!$head)
|
|
84
|
+
return false;
|
|
85
|
+
return maybeSetSelection(state, dispatch, new CellSelection(cellSel.$anchorCell, $head));
|
|
86
|
+
};
|
|
87
|
+
}
|
|
88
|
+
export function handleTripleClick(view, pos) {
|
|
89
|
+
const doc = view.state.doc, $cell = cellAround(doc.resolve(pos));
|
|
90
|
+
if (!$cell)
|
|
91
|
+
return false;
|
|
92
|
+
view.dispatch(view.state.tr.setSelection(new CellSelection($cell)));
|
|
93
|
+
return true;
|
|
94
|
+
}
|
|
95
|
+
/**
|
|
96
|
+
* @public
|
|
97
|
+
*/
|
|
98
|
+
export function handlePaste(view, _, slice) {
|
|
99
|
+
if (!isInTable(view.state))
|
|
100
|
+
return false;
|
|
101
|
+
let cells = pastedCells(slice);
|
|
102
|
+
const sel = view.state.selection;
|
|
103
|
+
if (sel instanceof CellSelection) {
|
|
104
|
+
if (!cells) {
|
|
105
|
+
cells = {
|
|
106
|
+
width: 1,
|
|
107
|
+
height: 1,
|
|
108
|
+
rows: [
|
|
109
|
+
Fragment.from(fitSlice(tableNodeTypes(view.state.schema).cell, slice)),
|
|
110
|
+
],
|
|
111
|
+
};
|
|
112
|
+
}
|
|
113
|
+
const table = sel.$anchorCell.node(-1);
|
|
114
|
+
const start = sel.$anchorCell.start(-1);
|
|
115
|
+
const rect = TableMap.get(table).rectBetween(sel.$anchorCell.pos - start, sel.$headCell.pos - start);
|
|
116
|
+
cells = clipCells(cells, rect.right - rect.left, rect.bottom - rect.top);
|
|
117
|
+
insertCells(view.state, view.dispatch, start, rect, cells);
|
|
118
|
+
return true;
|
|
119
|
+
}
|
|
120
|
+
else if (cells) {
|
|
121
|
+
const $cell = selectionCell(view.state);
|
|
122
|
+
const start = $cell.start(-1);
|
|
123
|
+
insertCells(view.state, view.dispatch, start, TableMap.get($cell.node(-1)).findCell($cell.pos - start), cells);
|
|
124
|
+
return true;
|
|
125
|
+
}
|
|
126
|
+
else {
|
|
127
|
+
return false;
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
export function handleMouseDown(view, startEvent) {
|
|
131
|
+
if (startEvent.ctrlKey || startEvent.metaKey)
|
|
132
|
+
return;
|
|
133
|
+
const startDOMCell = domInCell(view, startEvent.target);
|
|
134
|
+
let $anchor;
|
|
135
|
+
if (startEvent.shiftKey && view.state.selection instanceof CellSelection) {
|
|
136
|
+
// Adding to an existing cell selection
|
|
137
|
+
setCellSelection(view.state.selection.$anchorCell, startEvent);
|
|
138
|
+
startEvent.preventDefault();
|
|
139
|
+
}
|
|
140
|
+
else if (startEvent.shiftKey &&
|
|
141
|
+
startDOMCell &&
|
|
142
|
+
($anchor = cellAround(view.state.selection.$anchor)) != null &&
|
|
143
|
+
cellUnderMouse(view, startEvent)?.pos != $anchor.pos) {
|
|
144
|
+
// Adding to a selection that starts in another cell (causing a
|
|
145
|
+
// cell selection to be created).
|
|
146
|
+
setCellSelection($anchor, startEvent);
|
|
147
|
+
startEvent.preventDefault();
|
|
148
|
+
}
|
|
149
|
+
else if (!startDOMCell) {
|
|
150
|
+
// Not in a cell, let the default behavior happen.
|
|
151
|
+
return;
|
|
152
|
+
}
|
|
153
|
+
// Create and dispatch a cell selection between the given anchor and
|
|
154
|
+
// the position under the mouse.
|
|
155
|
+
function setCellSelection($anchor, event) {
|
|
156
|
+
let $head = cellUnderMouse(view, event);
|
|
157
|
+
const starting = tableEditingKey.getState(view.state) == null;
|
|
158
|
+
if (!$head || !inSameTable($anchor, $head)) {
|
|
159
|
+
if (starting)
|
|
160
|
+
$head = $anchor;
|
|
161
|
+
else
|
|
162
|
+
return;
|
|
163
|
+
}
|
|
164
|
+
const selection = new CellSelection($anchor, $head);
|
|
165
|
+
if (starting || !view.state.selection.eq(selection)) {
|
|
166
|
+
const tr = view.state.tr.setSelection(selection);
|
|
167
|
+
if (starting)
|
|
168
|
+
tr.setMeta(tableEditingKey, $anchor.pos);
|
|
169
|
+
view.dispatch(tr);
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
// Stop listening to mouse motion events.
|
|
173
|
+
function stop() {
|
|
174
|
+
view.root.removeEventListener('mouseup', stop);
|
|
175
|
+
view.root.removeEventListener('dragstart', stop);
|
|
176
|
+
view.root.removeEventListener('mousemove', move);
|
|
177
|
+
if (tableEditingKey.getState(view.state) != null) {
|
|
178
|
+
view.dispatch(view.state.tr.setMeta(tableEditingKey, -1));
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
function move(_event) {
|
|
182
|
+
const event = _event;
|
|
183
|
+
const anchor = tableEditingKey.getState(view.state);
|
|
184
|
+
let $anchor;
|
|
185
|
+
if (anchor != null) {
|
|
186
|
+
// Continuing an existing cross-cell selection
|
|
187
|
+
$anchor = view.state.doc.resolve(anchor);
|
|
188
|
+
}
|
|
189
|
+
else if (domInCell(view, event.target) != startDOMCell) {
|
|
190
|
+
// Moving out of the initial cell -- start a new cell selection
|
|
191
|
+
$anchor = cellUnderMouse(view, startEvent);
|
|
192
|
+
if (!$anchor)
|
|
193
|
+
return stop();
|
|
194
|
+
}
|
|
195
|
+
if ($anchor)
|
|
196
|
+
setCellSelection($anchor, event);
|
|
197
|
+
}
|
|
198
|
+
view.root.addEventListener('mouseup', stop);
|
|
199
|
+
view.root.addEventListener('dragstart', stop);
|
|
200
|
+
view.root.addEventListener('mousemove', move);
|
|
201
|
+
}
|
|
202
|
+
// Check whether the cursor is at the end of a cell (so that further
|
|
203
|
+
// motion would move out of the cell)
|
|
204
|
+
function atEndOfCell(view, axis, dir) {
|
|
205
|
+
if (!(view.state.selection instanceof TextSelection))
|
|
206
|
+
return null;
|
|
207
|
+
const { $head } = view.state.selection;
|
|
208
|
+
for (let d = $head.depth - 1; d >= 0; d--) {
|
|
209
|
+
const parent = $head.node(d), index = dir < 0 ? $head.index(d) : $head.indexAfter(d);
|
|
210
|
+
if (index != (dir < 0 ? 0 : parent.childCount))
|
|
211
|
+
return null;
|
|
212
|
+
if (parent.type.spec.tableRole == 'cell' ||
|
|
213
|
+
parent.type.spec.tableRole == 'header_cell') {
|
|
214
|
+
const cellPos = $head.before(d);
|
|
215
|
+
const dirStr = axis == 'vert'
|
|
216
|
+
? (dir > 0 ? 'down' : 'up')
|
|
217
|
+
: dir > 0
|
|
218
|
+
? 'right'
|
|
219
|
+
: 'left';
|
|
220
|
+
return view.endOfTextblock(dirStr) ? cellPos : null;
|
|
221
|
+
}
|
|
222
|
+
}
|
|
223
|
+
return null;
|
|
224
|
+
}
|
|
225
|
+
function domInCell(view, dom) {
|
|
226
|
+
for (; dom && dom != view.dom; dom = dom.parentNode) {
|
|
227
|
+
if (dom.nodeName == 'TD' || dom.nodeName == 'TH') {
|
|
228
|
+
return dom;
|
|
229
|
+
}
|
|
230
|
+
}
|
|
231
|
+
return null;
|
|
232
|
+
}
|
|
233
|
+
function cellUnderMouse(view, event) {
|
|
234
|
+
const mousePos = view.posAtCoords({
|
|
235
|
+
left: event.clientX,
|
|
236
|
+
top: event.clientY,
|
|
237
|
+
});
|
|
238
|
+
if (!mousePos)
|
|
239
|
+
return null;
|
|
240
|
+
return mousePos ? cellAround(view.state.doc.resolve(mousePos.pos)) : null;
|
|
241
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { Plugin } from 'prosemirror-state';
|
|
2
|
+
/**
|
|
3
|
+
* @public
|
|
4
|
+
*/
|
|
5
|
+
export type TableEditingOptions = {
|
|
6
|
+
allowTableNodeSelection?: boolean;
|
|
7
|
+
};
|
|
8
|
+
/**
|
|
9
|
+
* Creates a [plugin](http://prosemirror.net/docs/ref/#state.Plugin)
|
|
10
|
+
* that, when added to an editor, enables cell-selection, handles
|
|
11
|
+
* cell-based copy/paste, and makes sure tables stay well-formed (each
|
|
12
|
+
* row has the same width, and cells don't overlap).
|
|
13
|
+
*
|
|
14
|
+
* You should probably put this plugin near the end of your array of
|
|
15
|
+
* plugins, since it handles mouse and arrow key events in tables
|
|
16
|
+
* rather broadly, and other plugins, like the gap cursor or the
|
|
17
|
+
* column-width dragging plugin, might want to get a turn first to
|
|
18
|
+
* perform more specific behavior.
|
|
19
|
+
*
|
|
20
|
+
* @public
|
|
21
|
+
*/
|
|
22
|
+
export declare function tableEditing({ allowTableNodeSelection, }?: TableEditingOptions): Plugin;
|
|
23
|
+
//# sourceMappingURL=tableEditing.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"tableEditing.d.ts","sourceRoot":"","sources":["../../src/utilities/tableEditing.ts"],"names":[],"mappings":"AAMA,OAAO,EAAE,MAAM,EAAE,MAAM,mBAAmB,CAAC;AAY3C;;GAEG;AACH,MAAM,MAAM,mBAAmB,GAAG;IAChC,uBAAuB,CAAC,EAAE,OAAO,CAAC;CACnC,CAAC;AAEF;;;;;;;;;;;;;GAaG;AACH,wBAAgB,YAAY,CAAC,EAC3B,uBAA+B,GAChC,GAAE,mBAAwB,GAAG,MAAM,CAgDnC"}
|
|
@@ -0,0 +1,63 @@
|
|
|
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
|
+
import { Plugin } from 'prosemirror-state';
|
|
7
|
+
import { drawCellSelection, normalizeSelection } from './CellSelection.js';
|
|
8
|
+
import { fixTables } from './fixTables.js';
|
|
9
|
+
import { handleKeyDown, handleMouseDown, handlePaste, handleTripleClick, } from './input.js';
|
|
10
|
+
import { tableEditingKey } from './util.js';
|
|
11
|
+
/**
|
|
12
|
+
* Creates a [plugin](http://prosemirror.net/docs/ref/#state.Plugin)
|
|
13
|
+
* that, when added to an editor, enables cell-selection, handles
|
|
14
|
+
* cell-based copy/paste, and makes sure tables stay well-formed (each
|
|
15
|
+
* row has the same width, and cells don't overlap).
|
|
16
|
+
*
|
|
17
|
+
* You should probably put this plugin near the end of your array of
|
|
18
|
+
* plugins, since it handles mouse and arrow key events in tables
|
|
19
|
+
* rather broadly, and other plugins, like the gap cursor or the
|
|
20
|
+
* column-width dragging plugin, might want to get a turn first to
|
|
21
|
+
* perform more specific behavior.
|
|
22
|
+
*
|
|
23
|
+
* @public
|
|
24
|
+
*/
|
|
25
|
+
export function tableEditing({ allowTableNodeSelection = false, } = {}) {
|
|
26
|
+
return new Plugin({
|
|
27
|
+
key: tableEditingKey,
|
|
28
|
+
// This piece of state is used to remember when a mouse-drag
|
|
29
|
+
// cell-selection is happening, so that it can continue even as
|
|
30
|
+
// transactions (which might move its anchor cell) come in.
|
|
31
|
+
state: {
|
|
32
|
+
init() {
|
|
33
|
+
return null;
|
|
34
|
+
},
|
|
35
|
+
apply(tr, cur) {
|
|
36
|
+
const set = tr.getMeta(tableEditingKey);
|
|
37
|
+
if (set != null)
|
|
38
|
+
return set == -1 ? null : set;
|
|
39
|
+
if (cur == null || !tr.docChanged)
|
|
40
|
+
return cur;
|
|
41
|
+
const { deleted, pos } = tr.mapping.mapResult(cur);
|
|
42
|
+
return deleted ? null : pos;
|
|
43
|
+
},
|
|
44
|
+
},
|
|
45
|
+
props: {
|
|
46
|
+
decorations: drawCellSelection,
|
|
47
|
+
handleDOMEvents: {
|
|
48
|
+
mousedown: handleMouseDown,
|
|
49
|
+
},
|
|
50
|
+
createSelectionBetween(view) {
|
|
51
|
+
return tableEditingKey.getState(view.state) != null
|
|
52
|
+
? view.state.selection
|
|
53
|
+
: null;
|
|
54
|
+
},
|
|
55
|
+
handleTripleClick,
|
|
56
|
+
handleKeyDown,
|
|
57
|
+
handlePaste,
|
|
58
|
+
},
|
|
59
|
+
appendTransaction(_, oldState, state) {
|
|
60
|
+
return normalizeSelection(state, fixTables(state, oldState), allowTableNodeSelection);
|
|
61
|
+
},
|
|
62
|
+
});
|
|
63
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { NodeSpec, NodeType, Schema } from 'prosemirror-model';
|
|
2
|
+
/**
|
|
3
|
+
* @public
|
|
4
|
+
*/
|
|
5
|
+
export type TableNodes = Record<'table' | 'table_row' | 'table_cell' | 'table_header', NodeSpec>;
|
|
6
|
+
/**
|
|
7
|
+
* @public
|
|
8
|
+
*/
|
|
9
|
+
export type TableRole = 'table' | 'row' | 'cell' | 'header_cell';
|
|
10
|
+
/**
|
|
11
|
+
* @public
|
|
12
|
+
*/
|
|
13
|
+
export declare function tableNodeTypes(schema: Schema): Record<TableRole, NodeType>;
|
|
14
|
+
//# sourceMappingURL=tableNodeTypes.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"tableNodeTypes.d.ts","sourceRoot":"","sources":["../../src/utilities/tableNodeTypes.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,QAAQ,EAAE,QAAQ,EAAE,MAAM,EAAE,MAAM,mBAAmB,CAAC;AAE/D;;GAEG;AACH,MAAM,MAAM,UAAU,GAAG,MAAM,CAC7B,OAAO,GAAG,WAAW,GAAG,YAAY,GAAG,cAAc,EACrD,QAAQ,CACT,CAAC;AAEF;;GAEG;AACH,MAAM,MAAM,SAAS,GAAG,OAAO,GAAG,KAAK,GAAG,MAAM,GAAG,aAAa,CAAC;AAEjE;;GAEG;AACH,wBAAgB,cAAc,CAAC,MAAM,EAAE,MAAM,GAAG,MAAM,CAAC,SAAS,EAAE,QAAQ,CAAC,CAW1E"}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
// Helper for creating a schema that supports tables.
|
|
2
|
+
/**
|
|
3
|
+
* @public
|
|
4
|
+
*/
|
|
5
|
+
export function tableNodeTypes(schema) {
|
|
6
|
+
let result = schema.cached.tableNodeTypes;
|
|
7
|
+
if (!result) {
|
|
8
|
+
result = schema.cached.tableNodeTypes = {};
|
|
9
|
+
for (const name in schema.nodes) {
|
|
10
|
+
const type = schema.nodes[name], role = type.spec.tableRole;
|
|
11
|
+
if (role)
|
|
12
|
+
result[role] = type;
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
return result;
|
|
16
|
+
}
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
import { EditorState } from 'prosemirror-state';
|
|
2
|
+
import { Attrs, Node, ResolvedPos } from 'prosemirror-model';
|
|
3
|
+
import { Rect, TableMap } from './TableMap.js';
|
|
4
|
+
/**
|
|
5
|
+
* @public
|
|
6
|
+
*/
|
|
7
|
+
export type MutableAttrs = Record<string, unknown>;
|
|
8
|
+
/**
|
|
9
|
+
* @public
|
|
10
|
+
*/
|
|
11
|
+
export interface CellAttrs {
|
|
12
|
+
colspan: number;
|
|
13
|
+
rowspan: number;
|
|
14
|
+
colwidth: number[] | null;
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* @public
|
|
18
|
+
*/
|
|
19
|
+
export declare const tableEditingKey: any;
|
|
20
|
+
/**
|
|
21
|
+
* @public
|
|
22
|
+
*/
|
|
23
|
+
export declare function cellAround($pos: ResolvedPos): ResolvedPos | null;
|
|
24
|
+
export declare function cellWrapping($pos: ResolvedPos): null | Node;
|
|
25
|
+
/**
|
|
26
|
+
* @public
|
|
27
|
+
*/
|
|
28
|
+
export declare function isInTable(state: EditorState): boolean;
|
|
29
|
+
/**
|
|
30
|
+
* @internal
|
|
31
|
+
*/
|
|
32
|
+
export declare function selectionCell(state: EditorState): ResolvedPos;
|
|
33
|
+
/**
|
|
34
|
+
* @public
|
|
35
|
+
*/
|
|
36
|
+
export declare function cellNear($pos: ResolvedPos): ResolvedPos | undefined;
|
|
37
|
+
/**
|
|
38
|
+
* @public
|
|
39
|
+
*/
|
|
40
|
+
export declare function pointsAtCell($pos: ResolvedPos): boolean;
|
|
41
|
+
/**
|
|
42
|
+
* @public
|
|
43
|
+
*/
|
|
44
|
+
export declare function moveCellForward($pos: ResolvedPos): ResolvedPos;
|
|
45
|
+
/**
|
|
46
|
+
* @internal
|
|
47
|
+
*/
|
|
48
|
+
export declare function inSameTable($cellA: ResolvedPos, $cellB: ResolvedPos): boolean;
|
|
49
|
+
/**
|
|
50
|
+
* @public
|
|
51
|
+
*/
|
|
52
|
+
export declare function findCell($pos: ResolvedPos): Rect;
|
|
53
|
+
/**
|
|
54
|
+
* @public
|
|
55
|
+
*/
|
|
56
|
+
export declare function colCount($pos: ResolvedPos): number;
|
|
57
|
+
/**
|
|
58
|
+
* @public
|
|
59
|
+
*/
|
|
60
|
+
export declare function nextCell($pos: ResolvedPos, axis: 'horiz' | 'vert', dir: number): ResolvedPos | null;
|
|
61
|
+
/**
|
|
62
|
+
* @public
|
|
63
|
+
*/
|
|
64
|
+
export declare function removeColSpan(attrs: CellAttrs, pos: number, n?: number): CellAttrs;
|
|
65
|
+
/**
|
|
66
|
+
* @public
|
|
67
|
+
*/
|
|
68
|
+
export declare function addColSpan(attrs: CellAttrs, pos: number, n?: number): Attrs;
|
|
69
|
+
/**
|
|
70
|
+
* @public
|
|
71
|
+
*/
|
|
72
|
+
export declare function columnIsHeader(map: TableMap, table: Node, col: number): boolean;
|
|
73
|
+
//# sourceMappingURL=util.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"util.d.ts","sourceRoot":"","sources":["../../src/utilities/util.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,WAAW,EAA4B,MAAM,mBAAmB,CAAC;AAE1E,OAAO,EAAE,KAAK,EAAE,IAAI,EAAE,WAAW,EAAE,MAAM,mBAAmB,CAAC;AAG7D,OAAO,EAAE,IAAI,EAAE,QAAQ,EAAE,MAAM,eAAe,CAAC;AAE/C;;GAEG;AACH,MAAM,MAAM,YAAY,GAAG,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;AAEnD;;GAEG;AACH,MAAM,WAAW,SAAS;IACxB,OAAO,EAAE,MAAM,CAAC;IAChB,OAAO,EAAE,MAAM,CAAC;IAChB,QAAQ,EAAE,MAAM,EAAE,GAAG,IAAI,CAAC;CAC3B;AAED;;GAEG;AACH,eAAO,MAAM,eAAe,KAA0C,CAAC;AAEvE;;GAEG;AACH,wBAAgB,UAAU,CAAC,IAAI,EAAE,WAAW,GAAG,WAAW,GAAG,IAAI,CAOhE;AAED,wBAAgB,YAAY,CAAC,IAAI,EAAE,WAAW,GAAG,IAAI,GAAG,IAAI,CAO3D;AAED;;GAEG;AACH,wBAAgB,SAAS,CAAC,KAAK,EAAE,WAAW,GAAG,OAAO,CAMrD;AAED;;GAEG;AACH,wBAAgB,aAAa,CAAC,KAAK,EAAE,WAAW,GAAG,WAAW,CAkB7D;AAED;;GAEG;AACH,wBAAgB,QAAQ,CAAC,IAAI,EAAE,WAAW,GAAG,WAAW,GAAG,SAAS,CAmBnE;AAED;;GAEG;AACH,wBAAgB,YAAY,CAAC,IAAI,EAAE,WAAW,GAAG,OAAO,CAEvD;AAED;;GAEG;AACH,wBAAgB,eAAe,CAAC,IAAI,EAAE,WAAW,GAAG,WAAW,CAE9D;AAED;;GAEG;AACH,wBAAgB,WAAW,CAAC,MAAM,EAAE,WAAW,EAAE,MAAM,EAAE,WAAW,GAAG,OAAO,CAM7E;AAED;;GAEG;AACH,wBAAgB,QAAQ,CAAC,IAAI,EAAE,WAAW,GAAG,IAAI,CAEhD;AAED;;GAEG;AACH,wBAAgB,QAAQ,CAAC,IAAI,EAAE,WAAW,GAAG,MAAM,CAElD;AAED;;GAEG;AACH,wBAAgB,QAAQ,CACtB,IAAI,EAAE,WAAW,EACjB,IAAI,EAAE,OAAO,GAAG,MAAM,EACtB,GAAG,EAAE,MAAM,GACV,WAAW,GAAG,IAAI,CAOpB;AAED;;GAEG;AACH,wBAAgB,aAAa,CAAC,KAAK,EAAE,SAAS,EAAE,GAAG,EAAE,MAAM,EAAE,CAAC,SAAI,GAAG,SAAS,CAS7E;AAED;;GAEG;AACH,wBAAgB,UAAU,CAAC,KAAK,EAAE,SAAS,EAAE,GAAG,EAAE,MAAM,EAAE,CAAC,SAAI,GAAG,KAAK,CAOtE;AAED;;GAEG;AACH,wBAAgB,cAAc,CAC5B,GAAG,EAAE,QAAQ,EACb,KAAK,EAAE,IAAI,EACX,GAAG,EAAE,MAAM,GACV,OAAO,CAQT"}
|
|
@@ -0,0 +1,155 @@
|
|
|
1
|
+
// Various helper function for working with tables
|
|
2
|
+
import { PluginKey } from 'prosemirror-state';
|
|
3
|
+
import { tableNodeTypes } from './tableNodeTypes.js';
|
|
4
|
+
import { TableMap } from './TableMap.js';
|
|
5
|
+
/**
|
|
6
|
+
* @public
|
|
7
|
+
*/
|
|
8
|
+
export const tableEditingKey = new PluginKey('selectingCells');
|
|
9
|
+
/**
|
|
10
|
+
* @public
|
|
11
|
+
*/
|
|
12
|
+
export function cellAround($pos) {
|
|
13
|
+
for (let d = $pos.depth - 1; d > 0; d--) {
|
|
14
|
+
if ($pos.node(d).type.spec.tableRole == 'row') {
|
|
15
|
+
return $pos.node(0).resolve($pos.before(d + 1));
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
return null;
|
|
19
|
+
}
|
|
20
|
+
export function cellWrapping($pos) {
|
|
21
|
+
for (let d = $pos.depth; d > 0; d--) {
|
|
22
|
+
// Sometimes the cell can be in the same depth.
|
|
23
|
+
const role = $pos.node(d).type.spec.tableRole;
|
|
24
|
+
if (role === 'cell' || role === 'header_cell')
|
|
25
|
+
return $pos.node(d);
|
|
26
|
+
}
|
|
27
|
+
return null;
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* @public
|
|
31
|
+
*/
|
|
32
|
+
export function isInTable(state) {
|
|
33
|
+
const $head = state.selection.$head;
|
|
34
|
+
for (let d = $head.depth; d > 0; d--) {
|
|
35
|
+
if ($head.node(d).type.spec.tableRole == 'row')
|
|
36
|
+
return true;
|
|
37
|
+
}
|
|
38
|
+
return false;
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* @internal
|
|
42
|
+
*/
|
|
43
|
+
export function selectionCell(state) {
|
|
44
|
+
const sel = state.selection;
|
|
45
|
+
if ('$anchorCell' in sel && sel.$anchorCell) {
|
|
46
|
+
return sel.$anchorCell.pos > sel.$headCell.pos
|
|
47
|
+
? sel.$anchorCell
|
|
48
|
+
: sel.$headCell;
|
|
49
|
+
}
|
|
50
|
+
else if ('node' in sel &&
|
|
51
|
+
sel.node &&
|
|
52
|
+
sel.node.type.spec.tableRole == 'cell') {
|
|
53
|
+
return sel.$anchor;
|
|
54
|
+
}
|
|
55
|
+
const $cell = cellAround(sel.$head) || cellNear(sel.$head);
|
|
56
|
+
if ($cell) {
|
|
57
|
+
return $cell;
|
|
58
|
+
}
|
|
59
|
+
throw new RangeError(`No cell found around position ${sel.head}`);
|
|
60
|
+
}
|
|
61
|
+
/**
|
|
62
|
+
* @public
|
|
63
|
+
*/
|
|
64
|
+
export function cellNear($pos) {
|
|
65
|
+
for (let after = $pos.nodeAfter, pos = $pos.pos; after; after = after.firstChild, pos++) {
|
|
66
|
+
const role = after.type.spec.tableRole;
|
|
67
|
+
if (role == 'cell' || role == 'header_cell')
|
|
68
|
+
return $pos.doc.resolve(pos);
|
|
69
|
+
}
|
|
70
|
+
for (let before = $pos.nodeBefore, pos = $pos.pos; before; before = before.lastChild, pos--) {
|
|
71
|
+
const role = before.type.spec.tableRole;
|
|
72
|
+
if (role == 'cell' || role == 'header_cell') {
|
|
73
|
+
return $pos.doc.resolve(pos - before.nodeSize);
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
/**
|
|
78
|
+
* @public
|
|
79
|
+
*/
|
|
80
|
+
export function pointsAtCell($pos) {
|
|
81
|
+
return $pos.parent.type.spec.tableRole == 'row' && !!$pos.nodeAfter;
|
|
82
|
+
}
|
|
83
|
+
/**
|
|
84
|
+
* @public
|
|
85
|
+
*/
|
|
86
|
+
export function moveCellForward($pos) {
|
|
87
|
+
return $pos.node(0).resolve($pos.pos + $pos.nodeAfter.nodeSize);
|
|
88
|
+
}
|
|
89
|
+
/**
|
|
90
|
+
* @internal
|
|
91
|
+
*/
|
|
92
|
+
export function inSameTable($cellA, $cellB) {
|
|
93
|
+
return ($cellA.depth == $cellB.depth &&
|
|
94
|
+
$cellA.pos >= $cellB.start(-1) &&
|
|
95
|
+
$cellA.pos <= $cellB.end(-1));
|
|
96
|
+
}
|
|
97
|
+
/**
|
|
98
|
+
* @public
|
|
99
|
+
*/
|
|
100
|
+
export function findCell($pos) {
|
|
101
|
+
return TableMap.get($pos.node(-1)).findCell($pos.pos - $pos.start(-1));
|
|
102
|
+
}
|
|
103
|
+
/**
|
|
104
|
+
* @public
|
|
105
|
+
*/
|
|
106
|
+
export function colCount($pos) {
|
|
107
|
+
return TableMap.get($pos.node(-1)).colCount($pos.pos - $pos.start(-1));
|
|
108
|
+
}
|
|
109
|
+
/**
|
|
110
|
+
* @public
|
|
111
|
+
*/
|
|
112
|
+
export function nextCell($pos, axis, dir) {
|
|
113
|
+
const table = $pos.node(-1);
|
|
114
|
+
const map = TableMap.get(table);
|
|
115
|
+
const tableStart = $pos.start(-1);
|
|
116
|
+
const moved = map.nextCell($pos.pos - tableStart, axis, dir);
|
|
117
|
+
return moved == null ? null : $pos.node(0).resolve(tableStart + moved);
|
|
118
|
+
}
|
|
119
|
+
/**
|
|
120
|
+
* @public
|
|
121
|
+
*/
|
|
122
|
+
export function removeColSpan(attrs, pos, n = 1) {
|
|
123
|
+
const result = { ...attrs, colspan: attrs.colspan - n };
|
|
124
|
+
if (result.colwidth) {
|
|
125
|
+
result.colwidth = result.colwidth.slice();
|
|
126
|
+
result.colwidth.splice(pos, n);
|
|
127
|
+
if (!result.colwidth.some((w) => w > 0))
|
|
128
|
+
result.colwidth = null;
|
|
129
|
+
}
|
|
130
|
+
return result;
|
|
131
|
+
}
|
|
132
|
+
/**
|
|
133
|
+
* @public
|
|
134
|
+
*/
|
|
135
|
+
export function addColSpan(attrs, pos, n = 1) {
|
|
136
|
+
const result = { ...attrs, colspan: attrs.colspan + n };
|
|
137
|
+
if (result.colwidth) {
|
|
138
|
+
result.colwidth = result.colwidth.slice();
|
|
139
|
+
for (let i = 0; i < n; i++)
|
|
140
|
+
result.colwidth.splice(pos, 0, 0);
|
|
141
|
+
}
|
|
142
|
+
return result;
|
|
143
|
+
}
|
|
144
|
+
/**
|
|
145
|
+
* @public
|
|
146
|
+
*/
|
|
147
|
+
export function columnIsHeader(map, table, col) {
|
|
148
|
+
const headerCell = tableNodeTypes(table.type.schema).header_cell;
|
|
149
|
+
for (let row = 0; row < map.height; row++) {
|
|
150
|
+
if (table.nodeAt(map.map[col + row * map.width]).type != headerCell) {
|
|
151
|
+
return false;
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
return true;
|
|
155
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@kerebron/extension-tables",
|
|
3
|
+
"version": "0.0.1",
|
|
4
|
+
"license": "MIT",
|
|
5
|
+
"module": "./esm/ExtensionTables.js",
|
|
6
|
+
"exports": {
|
|
7
|
+
".": {
|
|
8
|
+
"import": "./esm/ExtensionTables.js"
|
|
9
|
+
},
|
|
10
|
+
"./NodeTable": {
|
|
11
|
+
"import": "./esm/NodeTable.js"
|
|
12
|
+
},
|
|
13
|
+
"./NodeTableRow": {
|
|
14
|
+
"import": "./esm/NodeTableRow.js"
|
|
15
|
+
},
|
|
16
|
+
"./NodeTableHeader": {
|
|
17
|
+
"import": "./esm/NodeTableHeader.js"
|
|
18
|
+
},
|
|
19
|
+
"./NodeTableCell": {
|
|
20
|
+
"import": "./esm/NodeTableCell.js"
|
|
21
|
+
}
|
|
22
|
+
},
|
|
23
|
+
"dependencies": {
|
|
24
|
+
"@deno/shim-deno": "~0.18.0"
|
|
25
|
+
},
|
|
26
|
+
"devDependencies": {
|
|
27
|
+
"@types/node": "^20.9.0"
|
|
28
|
+
},
|
|
29
|
+
"_generatedBy": "dnt@dev"
|
|
30
|
+
}
|