@folijs/core 0.4.0 → 0.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +21 -0
- package/dist/model/schema.d.ts +9 -1
- package/dist/model/schema.d.ts.map +1 -1
- package/dist/model/schema.js +13 -3
- package/dist/model/schema.js.map +1 -1
- package/dist/state/commands.d.ts +8 -1
- package/dist/state/commands.d.ts.map +1 -1
- package/dist/state/commands.js +87 -34
- package/dist/state/commands.js.map +1 -1
- package/dist/state/comments.js +3 -3
- package/dist/state/comments.js.map +1 -1
- package/dist/state/editor.d.ts.map +1 -1
- package/dist/state/editor.js +2 -0
- package/dist/state/editor.js.map +1 -1
- package/dist/state/index.d.ts +4 -1
- package/dist/state/index.d.ts.map +1 -1
- package/dist/state/index.js +4 -1
- package/dist/state/index.js.map +1 -1
- package/dist/state/links.d.ts.map +1 -1
- package/dist/state/links.js +30 -19
- package/dist/state/links.js.map +1 -1
- package/dist/state/pictures.d.ts +51 -0
- package/dist/state/pictures.d.ts.map +1 -0
- package/dist/state/pictures.js +140 -0
- package/dist/state/pictures.js.map +1 -0
- package/dist/state/review.d.ts +1 -1
- package/dist/state/review.d.ts.map +1 -1
- package/dist/state/review.js +92 -13
- package/dist/state/review.js.map +1 -1
- package/dist/state/selection.d.ts +12 -1
- package/dist/state/selection.d.ts.map +1 -1
- package/dist/state/selection.js +12 -0
- package/dist/state/selection.js.map +1 -1
- package/dist/state/tablecommands.d.ts +77 -0
- package/dist/state/tablecommands.d.ts.map +1 -0
- package/dist/state/tablecommands.js +675 -0
- package/dist/state/tablecommands.js.map +1 -0
- package/dist/state/tables.d.ts +110 -0
- package/dist/state/tables.d.ts.map +1 -0
- package/dist/state/tables.js +729 -0
- package/dist/state/tables.js.map +1 -0
- package/dist/state/track.js +55 -6
- package/dist/state/track.js.map +1 -1
- package/dist/state/word.js +1 -1
- package/dist/state/word.js.map +1 -1
- package/dist/state/wordclip.d.ts.map +1 -1
- package/dist/state/wordclip.js +26 -9
- package/dist/state/wordclip.js.map +1 -1
- package/dist/state/wordformat.d.ts +9 -1
- package/dist/state/wordformat.d.ts.map +1 -1
- package/dist/state/wordformat.js +38 -16
- package/dist/state/wordformat.js.map +1 -1
- package/dist/v3/document.d.ts +114 -7
- package/dist/v3/document.d.ts.map +1 -1
- package/dist/v3/document.js +170 -13
- package/dist/v3/document.js.map +1 -1
- package/dist/v3/index.d.ts +2 -2
- package/dist/v3/index.d.ts.map +1 -1
- package/dist/v3/index.js +2 -2
- package/dist/v3/index.js.map +1 -1
- package/dist/v3/migrate.d.ts +2 -2
- package/dist/v3/migrate.d.ts.map +1 -1
- package/dist/v3/resolve.d.ts +50 -4
- package/dist/v3/resolve.d.ts.map +1 -1
- package/dist/v3/resolve.js +205 -15
- package/dist/v3/resolve.js.map +1 -1
- package/dist/v3/schema.d.ts +4 -0
- package/dist/v3/schema.d.ts.map +1 -1
- package/dist/v3/schema.js +166 -6
- package/dist/v3/schema.js.map +1 -1
- package/package.json +1 -1
- package/src/model/schema.ts +18 -2
- package/src/state/commands.ts +80 -28
- package/src/state/comments.ts +3 -3
- package/src/state/editor.ts +2 -0
- package/src/state/index.ts +34 -0
- package/src/state/links.ts +27 -17
- package/src/state/pictures.ts +168 -0
- package/src/state/review.ts +96 -14
- package/src/state/selection.ts +15 -1
- package/src/state/tablecommands.ts +741 -0
- package/src/state/tables.ts +760 -0
- package/src/state/track.ts +63 -6
- package/src/state/word.ts +1 -1
- package/src/state/wordclip.ts +28 -11
- package/src/state/wordformat.ts +35 -13
- package/src/v3/document.ts +285 -14
- package/src/v3/index.ts +14 -0
- package/src/v3/migrate.ts +2 -2
- package/src/v3/resolve.ts +259 -15
- package/src/v3/schema.ts +187 -7
|
@@ -0,0 +1,760 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Editing across a table's edges (10.6, ADR-0006): a range delete that keeps tables valid, as
|
|
3
|
+
* Word does. A selection inside one cell is ordinary text. One that runs from outside a table
|
|
4
|
+
* into it takes the whole rows it touches (the whole table when it touches every row); one
|
|
5
|
+
* across cells of a table empties those cells. Nothing is joined across a table's edge.
|
|
6
|
+
*/
|
|
7
|
+
import { Fragment, type Node, type ResolvedPos, Slice } from "../model/index.js";
|
|
8
|
+
import type { Mappable } from "../transform/stepmap.js";
|
|
9
|
+
import { attempt as attemptTr } from "./attempt.js";
|
|
10
|
+
import { MARK_ATTRS } from "./mark.js";
|
|
11
|
+
import { Selection, type SelectionJSON, TextSelection } from "./selection.js";
|
|
12
|
+
import { type Command, type EditorState, Plugin, type Transaction } from "./state.js";
|
|
13
|
+
|
|
14
|
+
const isTable = (node: Node) => node.type.name === "table";
|
|
15
|
+
|
|
16
|
+
/** The depth of the innermost table cell around `$pos`, or -1. */
|
|
17
|
+
export function cellDepth($pos: ResolvedPos): number {
|
|
18
|
+
for (let d = $pos.depth; d > 0; d--) if ($pos.node(d).type.name === "tableCell") return d;
|
|
19
|
+
return -1;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* The depth of the table around `$pos` just below the depth the two ends share (`shared`), or
|
|
24
|
+
* -1: only that table is cut (a table nested deeper goes with the rows or blocks around it).
|
|
25
|
+
*/
|
|
26
|
+
function tableBelow($pos: ResolvedPos, shared: number): number {
|
|
27
|
+
return $pos.depth > shared && isTable($pos.node(shared + 1)) ? shared + 1 : -1;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* Deletes `from..to` in `tr` keeping tables valid; returns false (and does nothing) when an
|
|
32
|
+
* ordinary delete keeps them valid (no end of the range inside a table it leaves).
|
|
33
|
+
*/
|
|
34
|
+
export function deleteAcrossTables(tr: Transaction, from: number, to: number): boolean {
|
|
35
|
+
const doc = tr.doc;
|
|
36
|
+
const $from = doc.resolve(from);
|
|
37
|
+
const $to = doc.resolve(to);
|
|
38
|
+
const fromCell = cellDepth($from);
|
|
39
|
+
const toCell = cellDepth($to);
|
|
40
|
+
if (fromCell < 0 && toCell < 0) return false;
|
|
41
|
+
if (fromCell >= 0 && toCell >= 0 && $from.before(fromCell) === $to.before(toCell)) return false;
|
|
42
|
+
// Ends in different cells of one table (or of one row): those cells are emptied.
|
|
43
|
+
const shared = $from.sharedDepth(to);
|
|
44
|
+
const common = $from.node(shared).type.name;
|
|
45
|
+
if (common === "table" || common === "tableRow") {
|
|
46
|
+
clearCells(tr, from, to, $from.before(shared + 1), $to.after(shared + 1));
|
|
47
|
+
return true;
|
|
48
|
+
}
|
|
49
|
+
const tableTo = tableBelow($to, shared);
|
|
50
|
+
const tableFrom = tableBelow($from, shared);
|
|
51
|
+
if (tableTo < 0 && tableFrom < 0) return false;
|
|
52
|
+
// From the end backward, so earlier positions stay valid.
|
|
53
|
+
let middleEnd = to;
|
|
54
|
+
if (tableTo >= 0) {
|
|
55
|
+
const table = $to.node(tableTo);
|
|
56
|
+
const start = $to.before(tableTo);
|
|
57
|
+
const row = $to.index(tableTo);
|
|
58
|
+
if (row === table.childCount - 1) tr.delete(start, start + table.nodeSize);
|
|
59
|
+
else {
|
|
60
|
+
let end = start + 1;
|
|
61
|
+
for (let r = 0; r <= row; r++) end += table.child(r).nodeSize;
|
|
62
|
+
tr.delete(start + 1, end);
|
|
63
|
+
}
|
|
64
|
+
middleEnd = start;
|
|
65
|
+
}
|
|
66
|
+
let middleStart = from;
|
|
67
|
+
let fromRows: [number, number] | null = null;
|
|
68
|
+
if (tableFrom >= 0) {
|
|
69
|
+
const table = $from.node(tableFrom);
|
|
70
|
+
const start = $from.before(tableFrom);
|
|
71
|
+
const end = start + table.nodeSize;
|
|
72
|
+
const row = $from.index(tableFrom);
|
|
73
|
+
if (row === 0) fromRows = [start, end];
|
|
74
|
+
else {
|
|
75
|
+
let rowStart = start + 1;
|
|
76
|
+
for (let r = 0; r < row; r++) rowStart += table.child(r).nodeSize;
|
|
77
|
+
fromRows = [rowStart, end - 1];
|
|
78
|
+
}
|
|
79
|
+
middleStart = end;
|
|
80
|
+
}
|
|
81
|
+
if (middleStart < middleEnd) deleteBetween(tr, middleStart, middleEnd);
|
|
82
|
+
if (fromRows) tr.delete(fromRows[0], fromRows[1]);
|
|
83
|
+
return true;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* Deletes `a..b`, where one end may be inside a paragraph and the other at a block boundary
|
|
88
|
+
* (next to a table): the paragraph's part and the whole blocks between, each on its own, so no
|
|
89
|
+
* paragraph is joined across the table.
|
|
90
|
+
*/
|
|
91
|
+
function deleteBetween(tr: Transaction, a: number, b: number): void {
|
|
92
|
+
const $a = tr.doc.resolve(a);
|
|
93
|
+
const $b = tr.doc.resolve(b);
|
|
94
|
+
const ranges: [number, number][] = [];
|
|
95
|
+
let blocksFrom = a;
|
|
96
|
+
let blocksTo = b;
|
|
97
|
+
if ($a.parent.inlineContent) {
|
|
98
|
+
ranges.push([a, $a.end()]);
|
|
99
|
+
blocksFrom = $a.after();
|
|
100
|
+
}
|
|
101
|
+
if ($b.parent.inlineContent) {
|
|
102
|
+
ranges.push([$b.start(), b]);
|
|
103
|
+
blocksTo = $b.before();
|
|
104
|
+
}
|
|
105
|
+
if (blocksFrom < blocksTo) ranges.push([blocksFrom, blocksTo]);
|
|
106
|
+
for (const [s, e] of ranges.sort((x, y) => y[0] - x[0])) if (s < e) tr.delete(s, e);
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
/**
|
|
110
|
+
* Empties the cells of one table (or row) from the one holding `from` to the one holding `to`,
|
|
111
|
+
* in document order (`start..end`: the table's or row's children there). Their text is deleted
|
|
112
|
+
* (paragraphs in a cell joined), so a tracked clear can be rejected; a cell holding other
|
|
113
|
+
* blocks (a nested table) gets one empty paragraph instead.
|
|
114
|
+
*
|
|
115
|
+
* shortcut: the cells in document order, not Word's rectangle (`CellSelection` will give that).
|
|
116
|
+
*/
|
|
117
|
+
function clearCells(tr: Transaction, from: number, to: number, start: number, end: number): void {
|
|
118
|
+
const cells: { pos: number; node: Node }[] = [];
|
|
119
|
+
tr.doc.nodesBetween(start, end, (node, pos) => {
|
|
120
|
+
const within = pos >= start && pos + node.nodeSize <= end;
|
|
121
|
+
// A cell around the table the ends share (it is nested): go on into it.
|
|
122
|
+
if (node.type.name !== "tableCell" || !within) return true;
|
|
123
|
+
// A cell of the table or row the ends share (a nested table's are not entered).
|
|
124
|
+
if (pos < to && pos + node.nodeSize > from) cells.push({ pos, node });
|
|
125
|
+
return false;
|
|
126
|
+
});
|
|
127
|
+
emptyCells(
|
|
128
|
+
tr,
|
|
129
|
+
cells.map((c) => c.pos),
|
|
130
|
+
);
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
/**
|
|
134
|
+
* Empties the cells at positions (positions before them, any order): their text deleted
|
|
135
|
+
* (paragraphs in a cell joined), so a tracked clear can be rejected; a cell holding other blocks
|
|
136
|
+
* (a nested table) gets one empty paragraph instead.
|
|
137
|
+
*/
|
|
138
|
+
export function emptyCells(tr: Transaction, positions: readonly number[]): void {
|
|
139
|
+
for (const pos of [...positions].sort((a, b) => b - a)) {
|
|
140
|
+
const node = tr.doc.nodeAt(pos);
|
|
141
|
+
if (node?.type.name !== "tableCell") continue;
|
|
142
|
+
let paragraphs = true;
|
|
143
|
+
node.forEach((child) => {
|
|
144
|
+
if (child.type.name !== "paragraph") paragraphs = false;
|
|
145
|
+
});
|
|
146
|
+
if (paragraphs) {
|
|
147
|
+
const textStart = pos + 2;
|
|
148
|
+
const textEnd = pos + node.nodeSize - 2;
|
|
149
|
+
if (textStart < textEnd) tr.delete(textStart, textEnd);
|
|
150
|
+
continue;
|
|
151
|
+
}
|
|
152
|
+
const paragraph = node.type.schema.nodes.paragraph?.create();
|
|
153
|
+
if (paragraph)
|
|
154
|
+
tr.replace(pos + 1, pos + node.nodeSize - 1, new Slice(Fragment.from(paragraph), 0, 0));
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
/**
|
|
159
|
+
* Restores Word's rules after a delete: a paragraph between two tables that became adjacent
|
|
160
|
+
* (Word would merge them) and after a table that ends the body, as Word puts one.
|
|
161
|
+
*/
|
|
162
|
+
export function keepTablesApart(tr: Transaction): void {
|
|
163
|
+
const paragraph = tr.doc.type.schema.nodes.paragraph;
|
|
164
|
+
if (!paragraph) return;
|
|
165
|
+
const inserts: number[] = [];
|
|
166
|
+
const visit = (parent: Node, start: number) => {
|
|
167
|
+
let pos = start;
|
|
168
|
+
parent.forEach((child, _offset, index) => {
|
|
169
|
+
if (isTable(child)) {
|
|
170
|
+
const next = parent.maybeChild(index + 1);
|
|
171
|
+
if (next ? isTable(next) : parent.type.name === "doc") inserts.push(pos + child.nodeSize);
|
|
172
|
+
// Cells of this table: their content too.
|
|
173
|
+
let rowPos = pos + 1;
|
|
174
|
+
child.forEach((row) => {
|
|
175
|
+
let cellPos = rowPos + 1;
|
|
176
|
+
row.forEach((cell) => {
|
|
177
|
+
visit(cell, cellPos + 1);
|
|
178
|
+
cellPos += cell.nodeSize;
|
|
179
|
+
});
|
|
180
|
+
rowPos += row.nodeSize;
|
|
181
|
+
});
|
|
182
|
+
}
|
|
183
|
+
pos += child.nodeSize;
|
|
184
|
+
});
|
|
185
|
+
};
|
|
186
|
+
visit(tr.doc, 0);
|
|
187
|
+
for (const at of inserts.reverse()) tr.insert(at, paragraph.create());
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
// ---- Moving between cells (10.6b) --------------------------------------------------------------
|
|
191
|
+
|
|
192
|
+
/** The innermost cell around the selection head: its depth, or -1. */
|
|
193
|
+
function headCell(state: EditorState): { $pos: ResolvedPos; depth: number } | null {
|
|
194
|
+
const $pos = state.selection.$head;
|
|
195
|
+
const depth = cellDepth($pos);
|
|
196
|
+
return depth < 0 ? null : { $pos, depth };
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
/** The positions of the cells of `table` (at `start`) one can go to, row by row (not those a merge covers). */
|
|
200
|
+
function cellsOf(table: Node, start: number): number[] {
|
|
201
|
+
const out: number[] = [];
|
|
202
|
+
let rowPos = start + 1;
|
|
203
|
+
table.forEach((row) => {
|
|
204
|
+
let cellPos = rowPos + 1;
|
|
205
|
+
row.forEach((cell) => {
|
|
206
|
+
if (cell.attrs.vMerge !== "continue") out.push(cellPos);
|
|
207
|
+
cellPos += cell.nodeSize;
|
|
208
|
+
});
|
|
209
|
+
rowPos += row.nodeSize;
|
|
210
|
+
});
|
|
211
|
+
return out;
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
/** Selects a cell's content (Word's Tab does), or puts the caret in an empty one. */
|
|
215
|
+
function selectCell(tr: Transaction, cellPos: number): void {
|
|
216
|
+
const cell = tr.doc.nodeAt(cellPos) as Node;
|
|
217
|
+
const from = Selection.near(tr.doc.resolve(cellPos + 1), 1).from;
|
|
218
|
+
const to = Selection.near(tr.doc.resolve(cellPos + cell.nodeSize - 1), -1).to;
|
|
219
|
+
tr.setSelection(TextSelection.create(tr.doc, from, Math.max(from, to)));
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
/**
|
|
223
|
+
* Tab (`dir` 1) and Shift+Tab (-1) in a table: the next or previous cell, its content selected,
|
|
224
|
+
* as Word does. Tab in the last cell adds a row like the last one (its cells empty, no merge).
|
|
225
|
+
* Does not apply outside a table.
|
|
226
|
+
*/
|
|
227
|
+
export function goToCell(dir: 1 | -1): Command {
|
|
228
|
+
return (state, dispatch) => {
|
|
229
|
+
const found = headCell(state);
|
|
230
|
+
if (!found) return false;
|
|
231
|
+
const { $pos, depth } = found;
|
|
232
|
+
const tableDepth = depth - 2;
|
|
233
|
+
const table = $pos.node(tableDepth);
|
|
234
|
+
const tableStart = $pos.before(tableDepth);
|
|
235
|
+
const cells = cellsOf(table, tableStart);
|
|
236
|
+
const at = cells.indexOf($pos.before(depth));
|
|
237
|
+
const next = cells[at + dir];
|
|
238
|
+
if (next !== undefined) {
|
|
239
|
+
if (dispatch) {
|
|
240
|
+
const tr = state.tr;
|
|
241
|
+
selectCell(tr, next);
|
|
242
|
+
dispatch(tr);
|
|
243
|
+
}
|
|
244
|
+
return true;
|
|
245
|
+
}
|
|
246
|
+
if (dir < 0) return true; // the first cell: Word stays
|
|
247
|
+
// The last cell: a new row after the table's last one.
|
|
248
|
+
const row = emptyRowLike(table.lastChild as Node);
|
|
249
|
+
if (!row) return false;
|
|
250
|
+
const insertAt = tableStart + table.nodeSize - 1;
|
|
251
|
+
return attemptTr(state, dispatch, (tr) => {
|
|
252
|
+
tr.insert(insertAt, row);
|
|
253
|
+
tr.setSelection(TextSelection.create(tr.doc, insertAt + 3));
|
|
254
|
+
});
|
|
255
|
+
};
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
/**
|
|
259
|
+
* A new row like `last`: its properties, not its identity (`w14:paraId`…) or revisions; its
|
|
260
|
+
* cells empty, unmerged, their paragraph with the first one's properties, without what only one
|
|
261
|
+
* paragraph has. Null when the schema has no paragraph.
|
|
262
|
+
*/
|
|
263
|
+
export function emptyRowLike(last: Node): Node | null {
|
|
264
|
+
const paragraph = last.type.schema.nodes.paragraph;
|
|
265
|
+
if (!paragraph) return null;
|
|
266
|
+
const rowAttrs = { ...last.attrs, revision: null, header: null, attributes: null };
|
|
267
|
+
const markOnly = Object.fromEntries(MARK_ATTRS.map((key) => [key, null]));
|
|
268
|
+
const cells: Node[] = [];
|
|
269
|
+
last.forEach((cell) => {
|
|
270
|
+
const first = cell.firstChild;
|
|
271
|
+
const own = first?.type === paragraph ? { ...first.attrs, ...markOnly } : null;
|
|
272
|
+
cells.push(cell.type.create({ ...cell.attrs, vMerge: null }, paragraph.create(own)));
|
|
273
|
+
});
|
|
274
|
+
return last.type.create(rowAttrs, cells);
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
/**
|
|
278
|
+
* Up (`dir` -1) and Down (1) from the first or last line of a cell (the view says when): the
|
|
279
|
+
* cell above or below that covers the same grid column (a merge's first cell), its last or first
|
|
280
|
+
* line; from the table's first or last row, out of the table.
|
|
281
|
+
*/
|
|
282
|
+
export function goToCellVertically(dir: 1 | -1): Command {
|
|
283
|
+
return (state, dispatch) => {
|
|
284
|
+
const found = headCell(state);
|
|
285
|
+
if (!found) return false;
|
|
286
|
+
const { $pos, depth } = found;
|
|
287
|
+
// A selection of a cell's whole content (Tab's) moves as the cell; any other selection is
|
|
288
|
+
// the browser's (a word selected on a line moves by line).
|
|
289
|
+
if (!state.selection.empty) {
|
|
290
|
+
const start = $pos.before(depth);
|
|
291
|
+
const node = $pos.node(depth);
|
|
292
|
+
const first = Selection.near(state.doc.resolve(start + 1), 1).from;
|
|
293
|
+
const last = Selection.near(state.doc.resolve(start + node.nodeSize - 1), -1).to;
|
|
294
|
+
if (state.selection.from > first || state.selection.to < last) return false;
|
|
295
|
+
}
|
|
296
|
+
// Only from the cell's first (last) paragraph.
|
|
297
|
+
const index = $pos.index(depth);
|
|
298
|
+
const cell = $pos.node(depth);
|
|
299
|
+
if (dir < 0 ? index > 0 : index < cell.childCount - 1) return false;
|
|
300
|
+
const rowDepth = depth - 1;
|
|
301
|
+
const tableDepth = depth - 2;
|
|
302
|
+
const table = $pos.node(tableDepth);
|
|
303
|
+
const tableStart = $pos.before(tableDepth);
|
|
304
|
+
const rowIndex = $pos.index(tableDepth);
|
|
305
|
+
const column = columnOf($pos.node(rowDepth), $pos.index(rowDepth));
|
|
306
|
+
let target: number | null = null;
|
|
307
|
+
for (let r = rowIndex + dir; r >= 0 && r < table.childCount; r += dir) {
|
|
308
|
+
const cellPos = cellAtColumn(table, tableStart, r, column);
|
|
309
|
+
if (cellPos === null) continue;
|
|
310
|
+
const node = state.doc.nodeAt(cellPos) as Node;
|
|
311
|
+
// A covered cell is part of the merge above it: going up, the merge's first cell.
|
|
312
|
+
if (node.attrs.vMerge === "continue") continue;
|
|
313
|
+
target = cellPos;
|
|
314
|
+
break;
|
|
315
|
+
}
|
|
316
|
+
// Out of the table: before or after it. Nothing there (the document's start or end, or a
|
|
317
|
+
// nested table at its cell's edge): the browser's move.
|
|
318
|
+
let out: Selection | null = null;
|
|
319
|
+
if (target === null) {
|
|
320
|
+
const pos = dir < 0 ? tableStart : tableStart + table.nodeSize;
|
|
321
|
+
out = Selection.near(state.doc.resolve(pos), dir);
|
|
322
|
+
const end = tableStart + table.nodeSize;
|
|
323
|
+
if (out.from > tableStart && out.from < end) return false;
|
|
324
|
+
const outer = cellDepth(state.doc.resolve(tableStart));
|
|
325
|
+
if (outer >= 0 && out.$head.before(outer) !== state.doc.resolve(tableStart).before(outer))
|
|
326
|
+
return false;
|
|
327
|
+
}
|
|
328
|
+
if (!dispatch) return true;
|
|
329
|
+
const tr = state.tr;
|
|
330
|
+
if (out) tr.setSelection(out);
|
|
331
|
+
else if (target !== null) {
|
|
332
|
+
const node = tr.doc.nodeAt(target) as Node;
|
|
333
|
+
const pos = dir < 0 ? target + node.nodeSize - 1 : target + 1;
|
|
334
|
+
tr.setSelection(Selection.near(tr.doc.resolve(pos), dir < 0 ? -1 : 1));
|
|
335
|
+
}
|
|
336
|
+
dispatch(tr);
|
|
337
|
+
return true;
|
|
338
|
+
};
|
|
339
|
+
}
|
|
340
|
+
|
|
341
|
+
/** The first grid column of a row's cell at `index` (`gridBefore` counted). */
|
|
342
|
+
function columnOf(row: Node, index: number): number {
|
|
343
|
+
let column = (row.attrs.gridBefore as number | null) ?? 0;
|
|
344
|
+
for (let i = 0; i < index; i++) column += (row.child(i).attrs.gridSpan as number | null) ?? 1;
|
|
345
|
+
return column;
|
|
346
|
+
}
|
|
347
|
+
|
|
348
|
+
/** The position of the cell of row `r` covering grid column `column`, if any. */
|
|
349
|
+
function cellAtColumn(table: Node, tableStart: number, r: number, column: number): number | null {
|
|
350
|
+
let rowPos = tableStart + 1;
|
|
351
|
+
for (let i = 0; i < r; i++) rowPos += table.child(i).nodeSize;
|
|
352
|
+
const row = table.child(r);
|
|
353
|
+
let at = (row.attrs.gridBefore as number | null) ?? 0;
|
|
354
|
+
let cellPos = rowPos + 1;
|
|
355
|
+
for (let i = 0; i < row.childCount; i++) {
|
|
356
|
+
const cell = row.child(i);
|
|
357
|
+
const span = (cell.attrs.gridSpan as number | null) ?? 1;
|
|
358
|
+
if (column >= at && column < at + span) return cellPos;
|
|
359
|
+
at += span;
|
|
360
|
+
cellPos += cell.nodeSize;
|
|
361
|
+
}
|
|
362
|
+
return null;
|
|
363
|
+
}
|
|
364
|
+
/** Tab and Shift+Tab between cells: before the lists' Tab (in a cell Word moves, numbered or not). */
|
|
365
|
+
export function tableKeymap(): Plugin {
|
|
366
|
+
return new Plugin({ keymap: { Tab: goToCell(1), "Shift-Tab": goToCell(-1) } });
|
|
367
|
+
}
|
|
368
|
+
|
|
369
|
+
// ---- Cell selections (10.6c) ---------------------------------------------------------------------
|
|
370
|
+
|
|
371
|
+
/** A cell where a table's grid has it: its position and the rows and grid columns it covers. */
|
|
372
|
+
export interface GridCell {
|
|
373
|
+
pos: number;
|
|
374
|
+
row: number;
|
|
375
|
+
rowSpan: number;
|
|
376
|
+
column: number;
|
|
377
|
+
span: number;
|
|
378
|
+
}
|
|
379
|
+
|
|
380
|
+
/** A table's cells on its grid (a merge's first cell spans the rows its merge covers). */
|
|
381
|
+
export function gridOf(table: Node, start: number): GridCell[] {
|
|
382
|
+
const out: GridCell[] = [];
|
|
383
|
+
const open = new Map<number, GridCell>();
|
|
384
|
+
let rowPos = start + 1;
|
|
385
|
+
table.forEach((row, _o, r) => {
|
|
386
|
+
let column = (row.attrs.gridBefore as number | null) ?? 0;
|
|
387
|
+
let cellPos = rowPos + 1;
|
|
388
|
+
row.forEach((cell) => {
|
|
389
|
+
const span = (cell.attrs.gridSpan as number | null) ?? 1;
|
|
390
|
+
const above = open.get(column);
|
|
391
|
+
// A merge continues only right below its last row (a row without that column ends it).
|
|
392
|
+
if (cell.attrs.vMerge === "continue" && above && above.row + above.rowSpan === r)
|
|
393
|
+
above.rowSpan++;
|
|
394
|
+
else {
|
|
395
|
+
const entry = { pos: cellPos, row: r, rowSpan: 1, column, span };
|
|
396
|
+
out.push(entry);
|
|
397
|
+
if (cell.attrs.vMerge === "restart") open.set(column, entry);
|
|
398
|
+
else open.delete(column);
|
|
399
|
+
}
|
|
400
|
+
column += span;
|
|
401
|
+
cellPos += cell.nodeSize;
|
|
402
|
+
});
|
|
403
|
+
rowPos += row.nodeSize;
|
|
404
|
+
});
|
|
405
|
+
return out;
|
|
406
|
+
}
|
|
407
|
+
|
|
408
|
+
/**
|
|
409
|
+
* Cells selected as a rectangle (ADR-0006): `anchor` and `head` are the positions before two
|
|
410
|
+
* cells of one table; the selection is every cell in the rectangle between them, widened until
|
|
411
|
+
* no merged cell sticks out. `from` and `to` are the rectangle's first and last cell's edges.
|
|
412
|
+
*/
|
|
413
|
+
export class CellSelection extends Selection {
|
|
414
|
+
/** The positions of the selected cells, in document order. */
|
|
415
|
+
readonly cells: readonly number[];
|
|
416
|
+
|
|
417
|
+
constructor($anchorCell: ResolvedPos, $headCell: ResolvedPos) {
|
|
418
|
+
super($anchorCell, $headCell);
|
|
419
|
+
this.cells = rectangle($anchorCell, $headCell);
|
|
420
|
+
}
|
|
421
|
+
|
|
422
|
+
/** A cell selection from the cell at `anchor` to the one at `head` (positions before them). */
|
|
423
|
+
static create(doc: Node, anchor: number, head = anchor): CellSelection {
|
|
424
|
+
return new CellSelection(doc.resolve(anchor), doc.resolve(head));
|
|
425
|
+
}
|
|
426
|
+
|
|
427
|
+
/** Whether `pos` is the position before a cell, and `other` before a cell of the same table. */
|
|
428
|
+
static valid(doc: Node, anchor: number, head: number): boolean {
|
|
429
|
+
const a = doc.resolve(anchor);
|
|
430
|
+
const h = doc.resolve(head);
|
|
431
|
+
return (
|
|
432
|
+
a.nodeAfter?.type.name === "tableCell" &&
|
|
433
|
+
h.nodeAfter?.type.name === "tableCell" &&
|
|
434
|
+
a.depth === h.depth &&
|
|
435
|
+
a.before(a.depth - 1) === h.before(h.depth - 1)
|
|
436
|
+
);
|
|
437
|
+
}
|
|
438
|
+
|
|
439
|
+
override get from(): number {
|
|
440
|
+
return this.cells[0] ?? this.anchor;
|
|
441
|
+
}
|
|
442
|
+
|
|
443
|
+
override get to(): number {
|
|
444
|
+
const last = this.cells[this.cells.length - 1] ?? this.head;
|
|
445
|
+
return last + ((this.$head.doc.nodeAt(last)?.nodeSize as number | undefined) ?? 0);
|
|
446
|
+
}
|
|
447
|
+
|
|
448
|
+
override get empty(): boolean {
|
|
449
|
+
return false;
|
|
450
|
+
}
|
|
451
|
+
|
|
452
|
+
/** Each selected cell's content. */
|
|
453
|
+
override get ranges(): readonly { from: number; to: number }[] {
|
|
454
|
+
const doc = this.$head.doc;
|
|
455
|
+
return this.cells.map((pos) => ({
|
|
456
|
+
from: pos + 1,
|
|
457
|
+
to: pos + ((doc.nodeAt(pos)?.nodeSize as number | undefined) ?? 2) - 1,
|
|
458
|
+
}));
|
|
459
|
+
}
|
|
460
|
+
|
|
461
|
+
/** Still these cells when both survive the mapping; a text selection near the head otherwise. */
|
|
462
|
+
map(doc: Node, mapping: Mappable): Selection {
|
|
463
|
+
const anchor = mapping.mapResult(this.anchor, 1);
|
|
464
|
+
const head = mapping.mapResult(this.head, 1);
|
|
465
|
+
if (!anchor.deleted && !head.deleted && CellSelection.valid(doc, anchor.pos, head.pos))
|
|
466
|
+
return CellSelection.create(doc, anchor.pos, head.pos);
|
|
467
|
+
return Selection.near(doc.resolve(Math.min(head.pos, doc.content.size)));
|
|
468
|
+
}
|
|
469
|
+
|
|
470
|
+
toJSON(): SelectionJSON {
|
|
471
|
+
return { type: "cell", anchor: this.anchor, head: this.head };
|
|
472
|
+
}
|
|
473
|
+
}
|
|
474
|
+
|
|
475
|
+
/** The grid cell covering the cell at `pos` of `table` (at `start`): a merge's, for a covered one. */
|
|
476
|
+
export function coveringCell(
|
|
477
|
+
table: Node,
|
|
478
|
+
start: number,
|
|
479
|
+
grid: readonly GridCell[],
|
|
480
|
+
pos: number,
|
|
481
|
+
): GridCell | undefined {
|
|
482
|
+
let rowPos = start + 1;
|
|
483
|
+
for (let r = 0; r < table.childCount; r++) {
|
|
484
|
+
const row = table.child(r);
|
|
485
|
+
let column = (row.attrs.gridBefore as number | null) ?? 0;
|
|
486
|
+
let cellPos = rowPos + 1;
|
|
487
|
+
for (let i = 0; i < row.childCount; i++) {
|
|
488
|
+
const cell = row.child(i);
|
|
489
|
+
if (cellPos === pos) return coveringAt(grid, r, column);
|
|
490
|
+
column += (cell.attrs.gridSpan as number | null) ?? 1;
|
|
491
|
+
cellPos += cell.nodeSize;
|
|
492
|
+
}
|
|
493
|
+
rowPos += row.nodeSize;
|
|
494
|
+
}
|
|
495
|
+
return undefined;
|
|
496
|
+
}
|
|
497
|
+
|
|
498
|
+
/** The grid cell covering `row` and `column`. */
|
|
499
|
+
export const coveringAt = (grid: readonly GridCell[], row: number, column: number) =>
|
|
500
|
+
grid.find(
|
|
501
|
+
(c) =>
|
|
502
|
+
c.row <= row && row < c.row + c.rowSpan && c.column <= column && column < c.column + c.span,
|
|
503
|
+
);
|
|
504
|
+
|
|
505
|
+
/** The cells (positions) of the rectangle between two cells of a table, merges widening it. */
|
|
506
|
+
function rectangle($anchor: ResolvedPos, $head: ResolvedPos): number[] {
|
|
507
|
+
const tableDepth = $anchor.depth - 1;
|
|
508
|
+
const table = $anchor.node(tableDepth);
|
|
509
|
+
const start = $anchor.before(tableDepth);
|
|
510
|
+
const grid = gridOf(table, start);
|
|
511
|
+
const cellAt = (pos: number) =>
|
|
512
|
+
grid.find((c) => c.pos === pos) ?? coveringCell(table, start, grid, pos);
|
|
513
|
+
const a = cellAt($anchor.pos);
|
|
514
|
+
const h = cellAt($head.pos);
|
|
515
|
+
if (!a || !h) return [$anchor.pos];
|
|
516
|
+
let top = Math.min(a.row, h.row);
|
|
517
|
+
let bottom = Math.max(a.row + a.rowSpan, h.row + h.rowSpan) - 1;
|
|
518
|
+
let left = Math.min(a.column, h.column);
|
|
519
|
+
let right = Math.max(a.column + a.span, h.column + h.span) - 1;
|
|
520
|
+
const inside = (c: GridCell) =>
|
|
521
|
+
c.row <= bottom &&
|
|
522
|
+
c.row + c.rowSpan - 1 >= top &&
|
|
523
|
+
c.column <= right &&
|
|
524
|
+
c.column + c.span - 1 >= left;
|
|
525
|
+
for (let changed = true; changed; ) {
|
|
526
|
+
changed = false;
|
|
527
|
+
for (const c of grid) {
|
|
528
|
+
if (!inside(c)) continue;
|
|
529
|
+
const next = [
|
|
530
|
+
Math.min(top, c.row),
|
|
531
|
+
Math.max(bottom, c.row + c.rowSpan - 1),
|
|
532
|
+
Math.min(left, c.column),
|
|
533
|
+
Math.max(right, c.column + c.span - 1),
|
|
534
|
+
];
|
|
535
|
+
if (next[0] !== top || next[1] !== bottom || next[2] !== left || next[3] !== right) {
|
|
536
|
+
[top, bottom, left, right] = next as [number, number, number, number];
|
|
537
|
+
changed = true;
|
|
538
|
+
}
|
|
539
|
+
}
|
|
540
|
+
}
|
|
541
|
+
return grid.filter(inside).map((c) => c.pos);
|
|
542
|
+
}
|
|
543
|
+
|
|
544
|
+
Selection.kinds.set("cell", (doc, json) => {
|
|
545
|
+
const clamp = (pos: number) => Math.max(0, Math.min(pos, doc.content.size));
|
|
546
|
+
const anchor = clamp(json.anchor);
|
|
547
|
+
const head = clamp(json.head);
|
|
548
|
+
return CellSelection.valid(doc, anchor, head)
|
|
549
|
+
? CellSelection.create(doc, anchor, head)
|
|
550
|
+
: Selection.near(doc.resolve(head));
|
|
551
|
+
});
|
|
552
|
+
/**
|
|
553
|
+
* Shift+arrows across cells: a cell selection's head moves to the cell beside it (`axis`
|
|
554
|
+
* "row": the cell above or below; "column": left or right); from a caret or text selection in
|
|
555
|
+
* a cell (the view says it leaves the cell) a cell selection starts, from that cell to the next.
|
|
556
|
+
* Does not apply where there is no cell that way.
|
|
557
|
+
*/
|
|
558
|
+
export function extendCellSelection(axis: "row" | "column", dir: 1 | -1): Command {
|
|
559
|
+
return (state, dispatch) => {
|
|
560
|
+
const { selection } = state;
|
|
561
|
+
let anchor: number;
|
|
562
|
+
let head: number;
|
|
563
|
+
if (selection instanceof CellSelection) {
|
|
564
|
+
anchor = selection.anchor;
|
|
565
|
+
head = selection.head;
|
|
566
|
+
} else {
|
|
567
|
+
const found = headCell(state);
|
|
568
|
+
if (!found) return false;
|
|
569
|
+
head = found.$pos.before(found.depth);
|
|
570
|
+
const $a = selection.$anchor;
|
|
571
|
+
const a = cellDepth($a);
|
|
572
|
+
// A selection that started outside this cell's table is not a cell selection's.
|
|
573
|
+
anchor =
|
|
574
|
+
a === found.depth && $a.before(a - 2) === found.$pos.before(found.depth - 2)
|
|
575
|
+
? $a.before(a)
|
|
576
|
+
: head;
|
|
577
|
+
}
|
|
578
|
+
const $head = state.doc.resolve(head);
|
|
579
|
+
const tableDepth = $head.depth - 1;
|
|
580
|
+
const table = $head.node(tableDepth);
|
|
581
|
+
const tableStart = $head.before(tableDepth);
|
|
582
|
+
const grid = gridOf(table, tableStart);
|
|
583
|
+
const at = grid.find((c) => c.pos === head);
|
|
584
|
+
if (!at) return false;
|
|
585
|
+
let next: GridCell | undefined;
|
|
586
|
+
if (axis === "row") {
|
|
587
|
+
const row = dir > 0 ? at.row + at.rowSpan : at.row - 1;
|
|
588
|
+
next = grid.find(
|
|
589
|
+
(c) =>
|
|
590
|
+
c.row <= row &&
|
|
591
|
+
row < c.row + c.rowSpan &&
|
|
592
|
+
c.column <= at.column &&
|
|
593
|
+
at.column < c.column + c.span,
|
|
594
|
+
);
|
|
595
|
+
} else {
|
|
596
|
+
const column = dir > 0 ? at.column + at.span : at.column - 1;
|
|
597
|
+
next = grid.find(
|
|
598
|
+
(c) =>
|
|
599
|
+
c.row <= at.row &&
|
|
600
|
+
at.row < c.row + c.rowSpan &&
|
|
601
|
+
c.column <= column &&
|
|
602
|
+
column < c.column + c.span,
|
|
603
|
+
);
|
|
604
|
+
}
|
|
605
|
+
if (!next) return false;
|
|
606
|
+
dispatch?.(state.tr.setSelection(CellSelection.create(state.doc, anchor, next.pos)));
|
|
607
|
+
return true;
|
|
608
|
+
};
|
|
609
|
+
}
|
|
610
|
+
|
|
611
|
+
// ---- Copying and pasting cells (10.6d) ---------------------------------------------------------
|
|
612
|
+
|
|
613
|
+
/**
|
|
614
|
+
* The selected cells as a table of their own, for the clipboard (ADR-0006): the rows the
|
|
615
|
+
* rectangle covers, each cut to its columns (with the cells a merge covers), and the grid's
|
|
616
|
+
* widths of those columns. A row with no cell in those columns is left out.
|
|
617
|
+
*/
|
|
618
|
+
export function cellsAsTable(selection: CellSelection): Node {
|
|
619
|
+
const $cell = selection.$anchor;
|
|
620
|
+
const tableDepth = $cell.depth - 1;
|
|
621
|
+
const table = $cell.node(tableDepth);
|
|
622
|
+
const chosen = gridOf(table, $cell.before(tableDepth)).filter((c) =>
|
|
623
|
+
selection.cells.includes(c.pos),
|
|
624
|
+
);
|
|
625
|
+
const top = Math.min(...chosen.map((c) => c.row));
|
|
626
|
+
const bottom = Math.max(...chosen.map((c) => c.row + c.rowSpan - 1));
|
|
627
|
+
const left = Math.min(...chosen.map((c) => c.column));
|
|
628
|
+
const right = Math.max(...chosen.map((c) => c.column + c.span - 1));
|
|
629
|
+
const rows: Node[] = [];
|
|
630
|
+
table.forEach((row, _o, r) => {
|
|
631
|
+
if (r < top || r > bottom) return;
|
|
632
|
+
let column = (row.attrs.gridBefore as number | null) ?? 0;
|
|
633
|
+
let first = -1;
|
|
634
|
+
let end = 0;
|
|
635
|
+
const cells: Node[] = [];
|
|
636
|
+
row.forEach((cell) => {
|
|
637
|
+
const span = (cell.attrs.gridSpan as number | null) ?? 1;
|
|
638
|
+
if (column >= left && column + span - 1 <= right) {
|
|
639
|
+
if (first < 0) first = column;
|
|
640
|
+
end = column + span;
|
|
641
|
+
cells.push(cell);
|
|
642
|
+
}
|
|
643
|
+
column += span;
|
|
644
|
+
});
|
|
645
|
+
// shortcut: a row with no cell in these columns is left out, so the rows after it paste one
|
|
646
|
+
// row higher; keep a placeholder row once pasting can tell one apart.
|
|
647
|
+
if (!cells.length) return;
|
|
648
|
+
const before = first - left;
|
|
649
|
+
const after = right + 1 - end;
|
|
650
|
+
rows.push(
|
|
651
|
+
row.type.create(
|
|
652
|
+
{
|
|
653
|
+
...row.attrs,
|
|
654
|
+
// Not the row's identity (`w14:paraId`…) nor its revision: a copy is a new row.
|
|
655
|
+
revision: null,
|
|
656
|
+
attributes: null,
|
|
657
|
+
gridBefore: before || null,
|
|
658
|
+
gridAfter: after || null,
|
|
659
|
+
// The widths of the grid columns left empty, while they are the same columns.
|
|
660
|
+
widthBefore: before === (row.attrs.gridBefore ?? 0) ? row.attrs.widthBefore : null,
|
|
661
|
+
widthAfter: after === (row.attrs.gridAfter ?? 0) ? row.attrs.widthAfter : null,
|
|
662
|
+
},
|
|
663
|
+
cells,
|
|
664
|
+
),
|
|
665
|
+
);
|
|
666
|
+
});
|
|
667
|
+
const grid = table.attrs.grid as number[];
|
|
668
|
+
const whole = left === 0 && right + 1 >= grid.length;
|
|
669
|
+
return table.type.create(
|
|
670
|
+
whole
|
|
671
|
+
? table.attrs
|
|
672
|
+
: { ...table.attrs, grid: grid.slice(left, right + 1), gridExt: null, width: null },
|
|
673
|
+
rows,
|
|
674
|
+
);
|
|
675
|
+
}
|
|
676
|
+
|
|
677
|
+
/**
|
|
678
|
+
* Pastes `table`'s rows into the table at `tr`'s selection (ADR-0006), from a cell selection's
|
|
679
|
+
* first cell or the cell holding the selection: each pasted cell's content replaces that of the
|
|
680
|
+
* cell at the same place from there, rows like the last one are added as needed, cells past the
|
|
681
|
+
* table's last column are dropped, and a cell of the target a merge covers takes only the first
|
|
682
|
+
* pasted cell it covers. The pasted cells end up selected. Returns false, doing nothing, when the
|
|
683
|
+
* selection is not in one cell (or cells) of a table or no pasted cell lands in the table.
|
|
684
|
+
*/
|
|
685
|
+
export function pasteCells(tr: Transaction, table: Node): boolean {
|
|
686
|
+
const { selection } = tr;
|
|
687
|
+
let start: number;
|
|
688
|
+
if (selection instanceof CellSelection) start = selection.cells[0] ?? selection.anchor;
|
|
689
|
+
else {
|
|
690
|
+
const { $from, $to } = selection;
|
|
691
|
+
const depth = cellDepth($from);
|
|
692
|
+
// A selection reaching out of its cell is not pasted into cells.
|
|
693
|
+
if (depth < 0 || cellDepth($to) !== depth || $from.before(depth) !== $to.before(depth))
|
|
694
|
+
return false;
|
|
695
|
+
start = $from.before(depth);
|
|
696
|
+
}
|
|
697
|
+
const $start = tr.doc.resolve(start);
|
|
698
|
+
const tableDepth = $start.depth - 1;
|
|
699
|
+
const tablePos = $start.before(tableDepth);
|
|
700
|
+
let current = $start.node(tableDepth);
|
|
701
|
+
let grid = gridOf(current, tablePos);
|
|
702
|
+
const origin = grid.find((c) => c.pos === start);
|
|
703
|
+
if (!origin) return false;
|
|
704
|
+
const width = Math.max(
|
|
705
|
+
(current.attrs.grid as number[]).length,
|
|
706
|
+
...grid.map((c) => c.column + c.span),
|
|
707
|
+
);
|
|
708
|
+
const targets: { row: number; column: number; cell: Node }[] = [];
|
|
709
|
+
table.forEach((row, _o, i) => {
|
|
710
|
+
let column = origin.column + ((row.attrs.gridBefore as number | null) ?? 0);
|
|
711
|
+
row.forEach((cell) => {
|
|
712
|
+
if (cell.attrs.vMerge !== "continue" && column < width)
|
|
713
|
+
targets.push({ row: origin.row + i, column, cell });
|
|
714
|
+
column += (cell.attrs.gridSpan as number | null) ?? 1;
|
|
715
|
+
});
|
|
716
|
+
});
|
|
717
|
+
if (!targets.length) return false;
|
|
718
|
+
// The rows missing, added in one step.
|
|
719
|
+
const rows = Math.max(...targets.map((t) => t.row)) + 1;
|
|
720
|
+
if (current.childCount < rows) {
|
|
721
|
+
const added = emptyRowLike(current.lastChild as Node);
|
|
722
|
+
if (!added) return false;
|
|
723
|
+
const extra = Array.from({ length: rows - current.childCount }, () => added);
|
|
724
|
+
tr.insert(tablePos + current.nodeSize - 1, Fragment.from(extra));
|
|
725
|
+
current = tr.doc.nodeAt(tablePos) as Node;
|
|
726
|
+
grid = gridOf(current, tablePos);
|
|
727
|
+
}
|
|
728
|
+
// Each target cell once (the first pasted cell it covers), filled from the end so the
|
|
729
|
+
// positions found before stay valid.
|
|
730
|
+
const fills = new Map<number, { at: GridCell; cell: Node }>();
|
|
731
|
+
for (const { row, column, cell } of targets) {
|
|
732
|
+
const at = coveringAt(grid, row, column);
|
|
733
|
+
if (at && !fills.has(at.pos)) fills.set(at.pos, { at, cell });
|
|
734
|
+
}
|
|
735
|
+
if (!fills.size) return false;
|
|
736
|
+
for (const { at, cell } of [...fills.values()].sort((a, b) => b.at.pos - a.at.pos)) {
|
|
737
|
+
const target = tr.doc.nodeAt(at.pos) as Node;
|
|
738
|
+
tr.replace(at.pos + 1, at.pos + target.nodeSize - 1, new Slice(cell.content, 0, 0));
|
|
739
|
+
}
|
|
740
|
+
// The rectangle of the filled cells.
|
|
741
|
+
const filled = [...fills.values()].map((f) => f.at);
|
|
742
|
+
const after = gridOf(tr.doc.nodeAt(tablePos) as Node, tablePos);
|
|
743
|
+
const first = coveringAt(
|
|
744
|
+
after,
|
|
745
|
+
Math.min(...filled.map((c) => c.row)),
|
|
746
|
+
Math.min(...filled.map((c) => c.column)),
|
|
747
|
+
);
|
|
748
|
+
const last = coveringAt(
|
|
749
|
+
after,
|
|
750
|
+
Math.max(...filled.map((c) => c.row + c.rowSpan - 1)),
|
|
751
|
+
Math.max(...filled.map((c) => c.column + c.span - 1)),
|
|
752
|
+
);
|
|
753
|
+
// A corner no cell covers (a shorter row): the first and last cells filled instead.
|
|
754
|
+
const one = filled[0] as GridCell;
|
|
755
|
+
const end = filled[filled.length - 1] as GridCell;
|
|
756
|
+
const from = first ?? coveringAt(after, one.row, one.column);
|
|
757
|
+
const to = last ?? coveringAt(after, end.row, end.column);
|
|
758
|
+
if (from && to) tr.setSelection(CellSelection.create(tr.doc, from.pos, to.pos));
|
|
759
|
+
return true;
|
|
760
|
+
}
|