@folijs/agent 0.3.3 → 0.4.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 +19 -0
- package/README.md +124 -21
- package/dist/changes.d.ts +23 -0
- package/dist/changes.d.ts.map +1 -0
- package/dist/changes.js +136 -0
- package/dist/changes.js.map +1 -0
- package/dist/formatting.d.ts +24 -0
- package/dist/formatting.d.ts.map +1 -0
- package/dist/formatting.js +106 -0
- package/dist/formatting.js.map +1 -0
- package/dist/handles.js +32 -12
- package/dist/handles.js.map +1 -1
- package/dist/index.d.ts +7 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +3 -0
- package/dist/index.js.map +1 -1
- package/dist/inserts.d.ts +42 -0
- package/dist/inserts.d.ts.map +1 -0
- package/dist/inserts.js +242 -0
- package/dist/inserts.js.map +1 -0
- package/dist/operations.d.ts.map +1 -1
- package/dist/operations.js +52 -27
- package/dist/operations.js.map +1 -1
- package/dist/paragraphs.d.ts +16 -0
- package/dist/paragraphs.d.ts.map +1 -0
- package/dist/paragraphs.js +146 -0
- package/dist/paragraphs.js.map +1 -0
- package/dist/read.d.ts +15 -1
- package/dist/read.d.ts.map +1 -1
- package/dist/read.js +138 -6
- package/dist/read.js.map +1 -1
- package/dist/schema.d.ts +25 -0
- package/dist/schema.d.ts.map +1 -0
- package/dist/schema.js +51 -0
- package/dist/schema.js.map +1 -0
- package/dist/session.d.ts +79 -2
- package/dist/session.d.ts.map +1 -1
- package/dist/session.js +141 -16
- package/dist/session.js.map +1 -1
- package/dist/stories.d.ts +2 -0
- package/dist/stories.d.ts.map +1 -0
- package/dist/stories.js +80 -0
- package/dist/stories.js.map +1 -0
- package/dist/tables.d.ts +21 -0
- package/dist/tables.d.ts.map +1 -0
- package/dist/tables.js +485 -0
- package/dist/tables.js.map +1 -0
- package/dist/tools.d.ts +89 -0
- package/dist/tools.d.ts.map +1 -0
- package/dist/tools.js +717 -0
- package/dist/tools.js.map +1 -0
- package/package.json +4 -4
- package/src/changes.ts +193 -0
- package/src/formatting.ts +149 -0
- package/src/handles.ts +40 -13
- package/src/index.ts +23 -0
- package/src/inserts.ts +339 -0
- package/src/operations.ts +71 -34
- package/src/paragraphs.ts +188 -0
- package/src/read.ts +141 -9
- package/src/schema.ts +74 -0
- package/src/session.ts +247 -18
- package/src/stories.ts +92 -0
- package/src/tables.ts +664 -0
- package/src/tools.ts +925 -0
package/src/tables.ts
ADDED
|
@@ -0,0 +1,664 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Table operations (23.3, ADR-0013): a table named by its handle, its rows and columns by the
|
|
3
|
+
* indexes `read` prints (`r1 c1`: rows as with every change accepted, grid columns). Each is a
|
|
4
|
+
* core command run with the selection where it acts, so every edit is tracked as the editor
|
|
5
|
+
* tracks it.
|
|
6
|
+
*/
|
|
7
|
+
import {
|
|
8
|
+
CellSelection,
|
|
9
|
+
deleteRows as deleteRowsCommand,
|
|
10
|
+
Fragment,
|
|
11
|
+
insertRow,
|
|
12
|
+
insertTable as insertTableCommand,
|
|
13
|
+
type Node,
|
|
14
|
+
type ResolvedPos,
|
|
15
|
+
Slice,
|
|
16
|
+
setCellShading,
|
|
17
|
+
setTableStyle as setTableStyleCommand,
|
|
18
|
+
tableStyles,
|
|
19
|
+
toggleHeaderRow,
|
|
20
|
+
} from "@folijs/core";
|
|
21
|
+
import {
|
|
22
|
+
type Changed,
|
|
23
|
+
type Context,
|
|
24
|
+
changed,
|
|
25
|
+
inline,
|
|
26
|
+
MAX_WRITTEN,
|
|
27
|
+
quote,
|
|
28
|
+
resolve,
|
|
29
|
+
} from "./operations.js";
|
|
30
|
+
import { readBlocks } from "./read.js";
|
|
31
|
+
import { type Refusal, type Result, refuse } from "./result.js";
|
|
32
|
+
|
|
33
|
+
/** The depth of the innermost cell around `$pos`, or -1. */
|
|
34
|
+
function cellDepth($pos: ResolvedPos): number {
|
|
35
|
+
for (let d = $pos.depth; d > 0; d--) if ($pos.node(d).type.name === "tableCell") return d;
|
|
36
|
+
return -1;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/** Word's limits: what `insertTable` of core takes. */
|
|
40
|
+
const MAX_ROWS = 1000;
|
|
41
|
+
const MAX_COLUMNS = 63;
|
|
42
|
+
|
|
43
|
+
/** A cell where `read` puts it. */
|
|
44
|
+
interface Placed {
|
|
45
|
+
pos: number;
|
|
46
|
+
node: Node;
|
|
47
|
+
column: number;
|
|
48
|
+
span: number;
|
|
49
|
+
}
|
|
50
|
+
/** A row `read` counts: its index among the table's rows, and its cells. */
|
|
51
|
+
interface Row {
|
|
52
|
+
index: number;
|
|
53
|
+
pos: number;
|
|
54
|
+
node: Node;
|
|
55
|
+
cells: Placed[];
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/** The rows of the table at `pos` as `read` numbers them: those not deleted as tracked changes. */
|
|
59
|
+
function rowsOf(pos: number, table: Node): Row[] {
|
|
60
|
+
const rows: Row[] = [];
|
|
61
|
+
table.forEach((row, offset, index) => {
|
|
62
|
+
if ((row.attrs.revision as { deletion?: unknown } | null)?.deletion) return;
|
|
63
|
+
const start = pos + 1 + offset;
|
|
64
|
+
const cells: Placed[] = [];
|
|
65
|
+
let column = 1 + ((row.attrs.gridBefore as number | null) ?? 0);
|
|
66
|
+
row.forEach((cell, cellOffset) => {
|
|
67
|
+
const span = (cell.attrs.gridSpan as number | null) ?? 1;
|
|
68
|
+
cells.push({ pos: start + 1 + cellOffset, node: cell, column, span });
|
|
69
|
+
column += span;
|
|
70
|
+
});
|
|
71
|
+
rows.push({ index, pos: start, node: row, cells });
|
|
72
|
+
});
|
|
73
|
+
return rows;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
interface Table {
|
|
77
|
+
handle: string;
|
|
78
|
+
pos: number;
|
|
79
|
+
node: Node;
|
|
80
|
+
rows: Row[];
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/** The table `handle` names: a table's own, or a paragraph's in one of its cells. */
|
|
84
|
+
function tableOf(context: Context, handle: string): Table | Refusal {
|
|
85
|
+
const found = resolve(context, handle);
|
|
86
|
+
if ("ok" in found) return found;
|
|
87
|
+
let { pos, node } = found;
|
|
88
|
+
if (node.type.name !== "table") {
|
|
89
|
+
const $pos = context.editor.state.doc.resolve(pos);
|
|
90
|
+
const depth = cellDepth($pos);
|
|
91
|
+
if (depth < 0)
|
|
92
|
+
return refuse(
|
|
93
|
+
"invalid",
|
|
94
|
+
`Block ${handle} is not a table or in one. A table is named by the handle of its "table" line, or of a paragraph in one of its cells.`,
|
|
95
|
+
);
|
|
96
|
+
pos = $pos.before(depth - 2);
|
|
97
|
+
node = $pos.node(depth - 2);
|
|
98
|
+
}
|
|
99
|
+
return { handle: context.handles.at(pos, node), pos, node, rows: rowsOf(pos, node) };
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
const count = (n: number, one: string) => `${n} ${one}${n === 1 ? "" : "s"}`;
|
|
103
|
+
|
|
104
|
+
/** The row `row` (1-based, as read prints it), or why there is none. */
|
|
105
|
+
function rowAt(table: Table, row: unknown): Row | Refusal {
|
|
106
|
+
const found = Number.isInteger(row) ? table.rows[(row as number) - 1] : undefined;
|
|
107
|
+
return (
|
|
108
|
+
found ??
|
|
109
|
+
refuse(
|
|
110
|
+
"invalid",
|
|
111
|
+
`Table ${table.handle} has ${count(table.rows.length, "row")}: row is 1 to ${table.rows.length}.`,
|
|
112
|
+
)
|
|
113
|
+
);
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
/**
|
|
117
|
+
* The cell at `row`, grid column `column`: the one spanning it, or the top of the vertical merge
|
|
118
|
+
* that covers it (where its text is).
|
|
119
|
+
*/
|
|
120
|
+
function cellAt(table: Table, row: unknown, column: unknown): Placed | Refusal {
|
|
121
|
+
let r = rowAt(table, row);
|
|
122
|
+
if ("ok" in r) return r;
|
|
123
|
+
const columns = Math.max(
|
|
124
|
+
0,
|
|
125
|
+
...table.rows.map((x) => x.cells.reduce((n, c) => Math.max(n, c.column + c.span - 1), 0)),
|
|
126
|
+
);
|
|
127
|
+
const at = (x: Row) =>
|
|
128
|
+
x.cells.find((c) => (column as number) >= c.column && (column as number) < c.column + c.span);
|
|
129
|
+
let cell = Number.isInteger(column) ? at(r) : undefined;
|
|
130
|
+
if (!cell)
|
|
131
|
+
return refuse(
|
|
132
|
+
"invalid",
|
|
133
|
+
`Row ${row as number} of table ${table.handle} has no cell in column ${String(column)}: columns are 1 to ${columns}.`,
|
|
134
|
+
);
|
|
135
|
+
// Merged up: the text is in the merge's top cell.
|
|
136
|
+
for (let i = table.rows.indexOf(r) - 1; cell.node.attrs.vMerge === "continue" && i >= 0; i--) {
|
|
137
|
+
r = table.rows[i] as Row;
|
|
138
|
+
cell = at(r) ?? cell;
|
|
139
|
+
}
|
|
140
|
+
return cell;
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
/** The table's blocks as they read now: its line and its cells' blocks. */
|
|
144
|
+
function tableBlocks(context: Context, handle: string): Changed {
|
|
145
|
+
const blocks = readBlocks(context.editor.state, context.handles);
|
|
146
|
+
const inTable = new Set([handle]);
|
|
147
|
+
const handles = blocks
|
|
148
|
+
.filter((b) => {
|
|
149
|
+
const mine = b.handle === handle || (b.cell && inTable.has(b.cell.table));
|
|
150
|
+
// A table nested in a cell is part of it too.
|
|
151
|
+
if (mine && b.kind === "table") inTable.add(b.handle);
|
|
152
|
+
return mine;
|
|
153
|
+
})
|
|
154
|
+
.map((b) => b.handle);
|
|
155
|
+
return changed(context, handles);
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
/** Puts the caret in a cell's first paragraph. */
|
|
159
|
+
const caretIn = (context: Context, cell: { pos: number }) => context.editor.select(cell.pos + 2);
|
|
160
|
+
|
|
161
|
+
/**
|
|
162
|
+
* Puts the caret where core's row commands act on `row` alone: a cell not in a vertical merge
|
|
163
|
+
* (a merged cell stands for every row of its merge). False when every cell of the row is.
|
|
164
|
+
*/
|
|
165
|
+
function caretInRow(context: Context, row: Row): boolean {
|
|
166
|
+
const cell = row.cells.find((c) => !c.node.attrs.vMerge);
|
|
167
|
+
if (cell) caretIn(context, cell);
|
|
168
|
+
return !!cell;
|
|
169
|
+
}
|
|
170
|
+
const MERGED = (table: string, row: number) =>
|
|
171
|
+
`Every cell of row ${row} of table ${table} is part of a vertical merge, which runs over rows around it: Foli cannot insert or delete next to it alone. Nothing was changed.`;
|
|
172
|
+
|
|
173
|
+
/** Handles of the paragraphs in `node` (at `pos`): a row's or a cell's. */
|
|
174
|
+
function paragraphsIn(context: Context, pos: number, node: Node): string[] {
|
|
175
|
+
const out: string[] = [];
|
|
176
|
+
node.descendants((child, offset) => {
|
|
177
|
+
if (child.type.name !== "paragraph") return true;
|
|
178
|
+
out.push(context.handles.at(pos + 1 + offset, child));
|
|
179
|
+
return false;
|
|
180
|
+
});
|
|
181
|
+
return out;
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
/**
|
|
185
|
+
* The answer of a table operation: the blocks it changed (their handles; default the table's
|
|
186
|
+
* own line), and the whole table as it reads now.
|
|
187
|
+
*/
|
|
188
|
+
function answer(context: Context, table: string, changedHandles?: readonly string[]): Changed {
|
|
189
|
+
const whole = tableBlocks(context, table);
|
|
190
|
+
const wanted = new Set(changedHandles ?? [table]);
|
|
191
|
+
return { blocks: whole.blocks.filter((b) => wanted.has(b.handle)), text: whole.text };
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
/** Rows of cell texts as a model gives them, or why they are not. */
|
|
195
|
+
function cellTexts(value: unknown, what: string): string[][] | Refusal {
|
|
196
|
+
const rows = Array.isArray(value) ? value : null;
|
|
197
|
+
if (
|
|
198
|
+
!rows?.length ||
|
|
199
|
+
!rows.every((r) => Array.isArray(r) && r.every((c) => typeof c === "string"))
|
|
200
|
+
)
|
|
201
|
+
return refuse(
|
|
202
|
+
"invalid",
|
|
203
|
+
`${what} is a list of rows, each a list of its cells' texts: [["Name", "Price"], ["Tea", "3.50"]].`,
|
|
204
|
+
);
|
|
205
|
+
const written = rows.flat().reduce((n: number, t: string) => n + t.length, 0);
|
|
206
|
+
if (written > MAX_WRITTEN)
|
|
207
|
+
return refuse("invalid", `That is ${written} characters: more than ${MAX_WRITTEN} at once.`);
|
|
208
|
+
return rows as string[][];
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
/** Fills `cells` (positions in the document as it is) with `texts`, in one transaction. */
|
|
212
|
+
function fill(
|
|
213
|
+
context: Context,
|
|
214
|
+
cells: readonly { pos: number; node: Node }[],
|
|
215
|
+
texts: readonly string[],
|
|
216
|
+
) {
|
|
217
|
+
const { editor } = context;
|
|
218
|
+
const tr = editor.state.tr;
|
|
219
|
+
// From the last: a filled cell moves the ones after it.
|
|
220
|
+
for (let i = Math.min(cells.length, texts.length) - 1; i >= 0; i--) {
|
|
221
|
+
const cell = cells[i] as { pos: number; node: Node };
|
|
222
|
+
// A cell a vertical merge goes on through takes no text: it is the merge's top cell's.
|
|
223
|
+
if (cell.node.attrs.vMerge === "continue") continue;
|
|
224
|
+
const nodes = inline(editor, texts[i] as string);
|
|
225
|
+
if (nodes.length) tr.insert(cell.pos + 2, nodes);
|
|
226
|
+
}
|
|
227
|
+
if (tr.docChanged) editor.dispatch(tr);
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
export interface InsertTableOptions {
|
|
231
|
+
/** Next to the block: after it (default) or before it. */
|
|
232
|
+
where?: "after" | "before";
|
|
233
|
+
/** Make the first row a header row (repeated on each page). */
|
|
234
|
+
header?: boolean;
|
|
235
|
+
/** A table style by name, as `tableStyles` lists them; null: none. Default: Word's Table Grid. */
|
|
236
|
+
style?: string | null;
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
/** The table style named `name` (capitals do not count), or why there is none. */
|
|
240
|
+
function tableStyle(context: Context, name: unknown): string | Refusal {
|
|
241
|
+
const names = tableStyles(context.editor.state);
|
|
242
|
+
const found = names.find(
|
|
243
|
+
(n) => typeof name === "string" && n.toLowerCase() === name.trim().toLowerCase(),
|
|
244
|
+
);
|
|
245
|
+
return (
|
|
246
|
+
found ??
|
|
247
|
+
refuse(
|
|
248
|
+
"unknown-style",
|
|
249
|
+
`There is no table style ${quote(String(name))}. The table styles are: ${names.map((n) => quote(n)).join(", ")}.`,
|
|
250
|
+
)
|
|
251
|
+
);
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
/**
|
|
255
|
+
* Where to put the caret for core's `insertTable` to put a table next to the block at `pos`:
|
|
256
|
+
* at a paragraph's end it goes after, at its start before.
|
|
257
|
+
*/
|
|
258
|
+
function caretFor(
|
|
259
|
+
context: Context,
|
|
260
|
+
pos: number,
|
|
261
|
+
node: Node,
|
|
262
|
+
where: "after" | "before",
|
|
263
|
+
): number | null {
|
|
264
|
+
const $pos = context.editor.state.doc.resolve(pos);
|
|
265
|
+
const parent = $pos.parent;
|
|
266
|
+
const index = $pos.index();
|
|
267
|
+
const paragraph = (n: Node | null | undefined) => n?.type.name === "paragraph";
|
|
268
|
+
if (paragraph(node)) {
|
|
269
|
+
if (where === "before") return pos + 1;
|
|
270
|
+
if (node.content.size) return pos + node.nodeSize - 1;
|
|
271
|
+
// After an empty paragraph: before the next one, else at its own start (before it).
|
|
272
|
+
const next = parent.maybeChild(index + 1);
|
|
273
|
+
return paragraph(next) ? pos + node.nodeSize + 1 : pos + 1;
|
|
274
|
+
}
|
|
275
|
+
if (where === "after") {
|
|
276
|
+
const next = parent.maybeChild(index + 1);
|
|
277
|
+
return paragraph(next) ? pos + node.nodeSize + 1 : null;
|
|
278
|
+
}
|
|
279
|
+
const previous = parent.maybeChild(index - 1);
|
|
280
|
+
return paragraph(previous) ? pos - 1 : null;
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
/** @internal */
|
|
284
|
+
export function insertTable(
|
|
285
|
+
context: Context,
|
|
286
|
+
handle: string,
|
|
287
|
+
cells: readonly (readonly string[])[],
|
|
288
|
+
options: InsertTableOptions = {},
|
|
289
|
+
): Result<Changed> {
|
|
290
|
+
const { editor } = context;
|
|
291
|
+
const texts = cellTexts(cells, "cells");
|
|
292
|
+
if ("ok" in texts) return texts;
|
|
293
|
+
const columns = Math.max(...texts.map((r) => r.length));
|
|
294
|
+
if (texts.length > MAX_ROWS || columns > MAX_COLUMNS || columns < 1)
|
|
295
|
+
return refuse("invalid", `A table has 1 to ${MAX_ROWS} rows and 1 to ${MAX_COLUMNS} columns.`);
|
|
296
|
+
const where = options.where ?? "after";
|
|
297
|
+
if (where !== "after" && where !== "before")
|
|
298
|
+
return refuse("invalid", 'where is "after" or "before".');
|
|
299
|
+
const style =
|
|
300
|
+
options.style === undefined || options.style === null
|
|
301
|
+
? null
|
|
302
|
+
: tableStyle(context, options.style);
|
|
303
|
+
if (style && typeof style !== "string") return style;
|
|
304
|
+
const found = resolve(context, handle);
|
|
305
|
+
if ("ok" in found) return found;
|
|
306
|
+
const caret = caretFor(context, found.pos, found.node, where);
|
|
307
|
+
if (caret === null)
|
|
308
|
+
return refuse(
|
|
309
|
+
"invalid",
|
|
310
|
+
`A table goes next to a paragraph, and there is none ${where} block ${handle}: insert one there first (insert_paragraphs), then the table next to it.`,
|
|
311
|
+
);
|
|
312
|
+
editor.select(caret);
|
|
313
|
+
const before = editor.state.doc;
|
|
314
|
+
if (!editor.run(insertTableCommand(texts.length, columns)) || editor.state.doc === before)
|
|
315
|
+
return refuse(
|
|
316
|
+
"refused",
|
|
317
|
+
`A table could not be inserted ${where} block ${handle} (a locked content control?). Nothing was changed.`,
|
|
318
|
+
);
|
|
319
|
+
// The caret is in its first cell.
|
|
320
|
+
const $from = editor.state.selection.$from;
|
|
321
|
+
const depth = cellDepth($from);
|
|
322
|
+
const tablePos = $from.before(depth - 2);
|
|
323
|
+
const made = (): Table => {
|
|
324
|
+
const node = editor.state.doc.nodeAt(tablePos) as Node;
|
|
325
|
+
return {
|
|
326
|
+
handle: context.handles.at(tablePos, node),
|
|
327
|
+
pos: tablePos,
|
|
328
|
+
node,
|
|
329
|
+
rows: rowsOf(tablePos, node),
|
|
330
|
+
};
|
|
331
|
+
};
|
|
332
|
+
const table = made();
|
|
333
|
+
fill(
|
|
334
|
+
context,
|
|
335
|
+
table.rows.flatMap((r) => r.cells),
|
|
336
|
+
texts.flatMap((r) => Array.from({ length: columns }, (_, i) => r[i] ?? "")),
|
|
337
|
+
);
|
|
338
|
+
if (options.header) {
|
|
339
|
+
caretIn(context, made().rows[0]?.cells[0] as Placed);
|
|
340
|
+
if (!editor.run(toggleHeaderRow))
|
|
341
|
+
return refuse(
|
|
342
|
+
"refused",
|
|
343
|
+
"The table's first row could not be made a header row. Nothing was changed.",
|
|
344
|
+
);
|
|
345
|
+
}
|
|
346
|
+
if ((style && style !== "Table Grid") || options.style === null) {
|
|
347
|
+
caretIn(context, made().rows[0]?.cells[0] as Placed);
|
|
348
|
+
if (!editor.run(setTableStyleCommand(style ?? null)))
|
|
349
|
+
return refuse("refused", "The table's style could not be set. Nothing was changed.");
|
|
350
|
+
}
|
|
351
|
+
return { ok: true, ...tableBlocks(context, table.handle) };
|
|
352
|
+
}
|
|
353
|
+
|
|
354
|
+
/** @internal */
|
|
355
|
+
export function insertRows(
|
|
356
|
+
context: Context,
|
|
357
|
+
handle: string,
|
|
358
|
+
row: number,
|
|
359
|
+
cells: readonly (readonly string[])[],
|
|
360
|
+
where: "after" | "before" = "after",
|
|
361
|
+
): Result<Changed> {
|
|
362
|
+
const { editor } = context;
|
|
363
|
+
const table = tableOf(context, handle);
|
|
364
|
+
if ("ok" in table) return table;
|
|
365
|
+
const reference = rowAt(table, row);
|
|
366
|
+
if ("ok" in reference) return reference;
|
|
367
|
+
const texts = cellTexts(cells, "cells");
|
|
368
|
+
if ("ok" in texts) return texts;
|
|
369
|
+
if (where !== "after" && where !== "before")
|
|
370
|
+
return refuse("invalid", 'where is "after" or "before".');
|
|
371
|
+
const most = reference.cells.length;
|
|
372
|
+
const long = texts.findIndex((r) => r.length > most);
|
|
373
|
+
if (long >= 0)
|
|
374
|
+
return refuse(
|
|
375
|
+
"invalid",
|
|
376
|
+
`A new row is made like row ${row}, which has ${count(most, "cell")}: row ${long + 1} of cells gives ${texts[long]?.length}.`,
|
|
377
|
+
);
|
|
378
|
+
if (texts.length > MAX_ROWS) return refuse("invalid", `At most ${MAX_ROWS} rows at once.`);
|
|
379
|
+
if (!caretInRow(context, reference)) return refuse("refused", MERGED(table.handle, row));
|
|
380
|
+
const made: number[] = [];
|
|
381
|
+
for (const [i, rowTexts] of texts.entries()) {
|
|
382
|
+
const before = editor.state.doc;
|
|
383
|
+
// The first next to the reference row; the others after the one just made.
|
|
384
|
+
if (
|
|
385
|
+
!editor.run(insertRow(i === 0 && where === "before" ? -1 : 1)) ||
|
|
386
|
+
editor.state.doc === before
|
|
387
|
+
)
|
|
388
|
+
return refuse(
|
|
389
|
+
"refused",
|
|
390
|
+
`Rows could not be inserted in table ${table.handle} (a locked content control?). Nothing was changed.`,
|
|
391
|
+
);
|
|
392
|
+
const $from = editor.state.selection.$from;
|
|
393
|
+
const depth = cellDepth($from);
|
|
394
|
+
const rows = () => rowsOf(table.pos, editor.state.doc.nodeAt(table.pos) as Node);
|
|
395
|
+
const index = rows().find((r) => r.pos === $from.before(depth - 1))?.index;
|
|
396
|
+
if (index === undefined)
|
|
397
|
+
return refuse(
|
|
398
|
+
"refused",
|
|
399
|
+
`Rows could not be inserted in table ${table.handle}. Nothing was changed.`,
|
|
400
|
+
);
|
|
401
|
+
made.push(index);
|
|
402
|
+
const fresh = rows().find((r) => r.index === index) as Row;
|
|
403
|
+
// A cell a vertical merge goes on through has no text of its own: text for it is refused.
|
|
404
|
+
const merged = fresh.cells.findIndex(
|
|
405
|
+
(c, k) => c.node.attrs.vMerge === "continue" && (rowTexts[k] ?? "") !== "",
|
|
406
|
+
);
|
|
407
|
+
if (merged >= 0)
|
|
408
|
+
return refuse(
|
|
409
|
+
"invalid",
|
|
410
|
+
`Cell ${merged + 1} of a new row (column ${fresh.cells[merged]?.column}) goes on the vertical merge above it, whose text is its top cell's: give "" for it. Nothing was changed.`,
|
|
411
|
+
);
|
|
412
|
+
fill(context, fresh.cells, rowTexts);
|
|
413
|
+
// The caret back in the new row (filling moved it): the next one goes after it.
|
|
414
|
+
caretInRow(context, rows().find((r) => r.index === index) as Row);
|
|
415
|
+
}
|
|
416
|
+
const node = editor.state.doc.nodeAt(table.pos) as Node;
|
|
417
|
+
const handles = rowsOf(table.pos, node)
|
|
418
|
+
.filter((r) => made.includes(r.index))
|
|
419
|
+
.flatMap((r) => paragraphsIn(context, r.pos, r.node));
|
|
420
|
+
return { ok: true, ...answer(context, table.handle, handles) };
|
|
421
|
+
}
|
|
422
|
+
|
|
423
|
+
/** @internal */
|
|
424
|
+
export function deleteRows(
|
|
425
|
+
context: Context,
|
|
426
|
+
handle: string,
|
|
427
|
+
rows: readonly number[],
|
|
428
|
+
): Result<{ text: string; blocks: Changed["blocks"]; rows: number }> {
|
|
429
|
+
const { editor } = context;
|
|
430
|
+
const table = tableOf(context, handle);
|
|
431
|
+
if ("ok" in table) return table;
|
|
432
|
+
if (!Array.isArray(rows) || !rows.length)
|
|
433
|
+
return refuse("invalid", "rows is a list of row numbers, as read prints them: [2, 3].");
|
|
434
|
+
const picked: Row[] = [];
|
|
435
|
+
for (const r of new Set(rows)) {
|
|
436
|
+
const found = rowAt(table, r);
|
|
437
|
+
if ("ok" in found) return found;
|
|
438
|
+
picked.push(found);
|
|
439
|
+
}
|
|
440
|
+
// A row starting a vertical merge that goes on below it: its text is the merge's, which a
|
|
441
|
+
// tracked deletion would hide. Deleted only with every row the merge covers.
|
|
442
|
+
for (const row of picked) {
|
|
443
|
+
const at = table.rows.indexOf(row);
|
|
444
|
+
for (const cell of row.cells) {
|
|
445
|
+
if (cell.node.attrs.vMerge !== "restart") continue;
|
|
446
|
+
const covered: Row[] = [];
|
|
447
|
+
for (const below of table.rows.slice(at + 1)) {
|
|
448
|
+
const goesOn = below.cells.some(
|
|
449
|
+
(c) => c.column === cell.column && c.node.attrs.vMerge === "continue",
|
|
450
|
+
);
|
|
451
|
+
if (!goesOn) break;
|
|
452
|
+
covered.push(below);
|
|
453
|
+
}
|
|
454
|
+
if (covered.some((r) => !picked.includes(r)))
|
|
455
|
+
return refuse(
|
|
456
|
+
"refused",
|
|
457
|
+
`Row ${at + 1} of table ${table.handle} starts a vertical merge in column ${cell.column} that goes on to row ${table.rows.indexOf(covered[covered.length - 1] as Row) + 1}, and holds its text: delete those rows with it, or set the text of a row that stays first. Nothing was changed.`,
|
|
458
|
+
);
|
|
459
|
+
}
|
|
460
|
+
}
|
|
461
|
+
// From the last: rows deleted as tracked changes stay, and those before keep their place.
|
|
462
|
+
picked.sort((a, b) => b.index - a.index);
|
|
463
|
+
for (const row of picked) {
|
|
464
|
+
const node = editor.state.doc.nodeAt(table.pos);
|
|
465
|
+
if (node?.type.name !== "table") break;
|
|
466
|
+
const now = rowsOf(table.pos, node).find((r) => r.index === row.index);
|
|
467
|
+
if (!now) continue;
|
|
468
|
+
const number = table.rows.indexOf(row) + 1;
|
|
469
|
+
if (!caretInRow(context, now)) return refuse("refused", MERGED(table.handle, number));
|
|
470
|
+
const before = editor.state.doc;
|
|
471
|
+
if (!editor.run(deleteRowsCommand) || editor.state.doc === before)
|
|
472
|
+
return refuse(
|
|
473
|
+
"refused",
|
|
474
|
+
`Row ${number} of table ${table.handle} could not be deleted (a locked content control?). Nothing was changed.`,
|
|
475
|
+
);
|
|
476
|
+
}
|
|
477
|
+
const all = picked.length === table.rows.length;
|
|
478
|
+
const numbers = [...new Set(rows)].sort((a, b) => a - b).join(", ");
|
|
479
|
+
const { text } = all ? { text: "" } : tableBlocks(context, table.handle);
|
|
480
|
+
// A row of one's own insertion goes; any other stays, a tracked deletion.
|
|
481
|
+
const how = "(a tracked deletion; a row you inserted yourself is removed)";
|
|
482
|
+
return {
|
|
483
|
+
ok: true,
|
|
484
|
+
blocks: [],
|
|
485
|
+
rows: picked.length,
|
|
486
|
+
text: all
|
|
487
|
+
? `Deleted table ${table.handle}, every row of it ${how}.`
|
|
488
|
+
: `Deleted ${picked.length === 1 ? "row" : "rows"} ${numbers} of table ${table.handle} ${how}. The table now reads:\n${text}`,
|
|
489
|
+
};
|
|
490
|
+
}
|
|
491
|
+
|
|
492
|
+
/** A cell's new text: where it is, as read prints it. */
|
|
493
|
+
export interface CellText {
|
|
494
|
+
row: number;
|
|
495
|
+
column: number;
|
|
496
|
+
text: string;
|
|
497
|
+
}
|
|
498
|
+
|
|
499
|
+
/** @internal */
|
|
500
|
+
export function setCells(
|
|
501
|
+
context: Context,
|
|
502
|
+
handle: string,
|
|
503
|
+
cells: readonly CellText[],
|
|
504
|
+
): Result<Changed> {
|
|
505
|
+
const { editor } = context;
|
|
506
|
+
const table = tableOf(context, handle);
|
|
507
|
+
if ("ok" in table) return table;
|
|
508
|
+
if (!Array.isArray(cells) || !cells.length)
|
|
509
|
+
return refuse(
|
|
510
|
+
"invalid",
|
|
511
|
+
'cells is a list of { row, column, text }: [{ "row": 2, "column": 1, "text": "Tea" }].',
|
|
512
|
+
);
|
|
513
|
+
const targets = new Map<number, { cell: Placed; text: string }>();
|
|
514
|
+
let written = 0;
|
|
515
|
+
for (const given of cells) {
|
|
516
|
+
if (typeof given?.text !== "string")
|
|
517
|
+
return refuse("invalid", "Each cell has its text: a string.");
|
|
518
|
+
const cell = cellAt(table, given.row, given.column);
|
|
519
|
+
if ("ok" in cell) return cell;
|
|
520
|
+
if (cell.node.content.content.some((b) => b.type.name !== "paragraph"))
|
|
521
|
+
return refuse(
|
|
522
|
+
"invalid",
|
|
523
|
+
`The cell at row ${given.row}, column ${given.column} of table ${table.handle} holds a table: edit its paragraphs by their handles instead.`,
|
|
524
|
+
);
|
|
525
|
+
written += given.text.length;
|
|
526
|
+
targets.set(cell.pos, { cell, text: given.text });
|
|
527
|
+
}
|
|
528
|
+
if (written > MAX_WRITTEN)
|
|
529
|
+
return refuse("invalid", `That is ${written} characters: more than ${MAX_WRITTEN} at once.`);
|
|
530
|
+
const before = editor.state.doc;
|
|
531
|
+
const tr = editor.state.tr;
|
|
532
|
+
// From the last cell: each replacement moves the ones after it.
|
|
533
|
+
for (const { cell, text } of [...targets.values()].sort((a, b) => b.cell.pos - a.cell.pos)) {
|
|
534
|
+
const from = cell.pos + 2;
|
|
535
|
+
const to = cell.pos + cell.node.nodeSize - 2;
|
|
536
|
+
const marks = cell.node.firstChild?.firstChild?.marks ?? [];
|
|
537
|
+
const nodes = inline(editor, text).map((n) =>
|
|
538
|
+
n.isText
|
|
539
|
+
? n.mark(
|
|
540
|
+
marks.filter((m) => !["insertion", "deletion", "formatChange"].includes(m.type.name)),
|
|
541
|
+
)
|
|
542
|
+
: n,
|
|
543
|
+
);
|
|
544
|
+
if (from === to && !nodes.length) continue;
|
|
545
|
+
tr.replace(from, to, new Slice(Fragment.from(nodes), 0, 0));
|
|
546
|
+
}
|
|
547
|
+
if (tr.docChanged) editor.dispatch(tr);
|
|
548
|
+
if (tr.docChanged && editor.state.doc === before)
|
|
549
|
+
return refuse(
|
|
550
|
+
"refused",
|
|
551
|
+
`The cells of table ${table.handle} could not be changed (a locked content control?). Nothing was changed.`,
|
|
552
|
+
);
|
|
553
|
+
const now = tableOf(context, table.handle);
|
|
554
|
+
if ("ok" in now) return { ok: true, ...tableBlocks(context, table.handle) };
|
|
555
|
+
const handles = cells.flatMap((c) => {
|
|
556
|
+
const cell = cellAt(now, c.row, c.column);
|
|
557
|
+
return "ok" in cell ? [] : paragraphsIn(context, cell.pos, cell.node);
|
|
558
|
+
});
|
|
559
|
+
return { ok: true, ...answer(context, table.handle, handles) };
|
|
560
|
+
}
|
|
561
|
+
|
|
562
|
+
/** @internal */
|
|
563
|
+
export function setTableStyle(
|
|
564
|
+
context: Context,
|
|
565
|
+
handle: string,
|
|
566
|
+
style: string | null,
|
|
567
|
+
): Result<Changed> {
|
|
568
|
+
const { editor } = context;
|
|
569
|
+
const table = tableOf(context, handle);
|
|
570
|
+
if ("ok" in table) return table;
|
|
571
|
+
const name = style === null ? null : tableStyle(context, style);
|
|
572
|
+
if (name !== null && typeof name !== "string") return name;
|
|
573
|
+
caretIn(context, table.rows[0]?.cells[0] as Placed);
|
|
574
|
+
if (!editor.run(setTableStyleCommand(name)))
|
|
575
|
+
return refuse("refused", `The style of table ${table.handle} could not be set.`);
|
|
576
|
+
return { ok: true, ...answer(context, table.handle) };
|
|
577
|
+
}
|
|
578
|
+
|
|
579
|
+
export interface ShadeOptions {
|
|
580
|
+
/** Only this row (with `column`: only that cell). */
|
|
581
|
+
row?: number;
|
|
582
|
+
/** Only this column (with `row`: only that cell). */
|
|
583
|
+
column?: number;
|
|
584
|
+
}
|
|
585
|
+
|
|
586
|
+
/** @internal */
|
|
587
|
+
export function shadeCells(
|
|
588
|
+
context: Context,
|
|
589
|
+
handle: string,
|
|
590
|
+
fill: string | null,
|
|
591
|
+
options: ShadeOptions = {},
|
|
592
|
+
): Result<Changed> {
|
|
593
|
+
const { editor } = context;
|
|
594
|
+
const table = tableOf(context, handle);
|
|
595
|
+
if ("ok" in table) return table;
|
|
596
|
+
const color = typeof fill === "string" ? fill.trim().replace(/^#/, "") : fill;
|
|
597
|
+
if (color !== null && !/^[0-9A-Fa-f]{6}$/.test(color))
|
|
598
|
+
return refuse(
|
|
599
|
+
"invalid",
|
|
600
|
+
'fill is a color as RRGGBB ("D9E2F3"), or null to take the shading off.',
|
|
601
|
+
);
|
|
602
|
+
const { row, column } = options;
|
|
603
|
+
const last = table.rows.length;
|
|
604
|
+
const rows = row === undefined ? [1, last] : [row, row];
|
|
605
|
+
const firsts = table.rows.map((r) => r.cells[0]?.column ?? 1);
|
|
606
|
+
const ends = table.rows.map((r) =>
|
|
607
|
+
r.cells.reduce((n, c) => Math.max(n, c.column + c.span - 1), 0),
|
|
608
|
+
);
|
|
609
|
+
const columns =
|
|
610
|
+
column === undefined ? [Math.min(...firsts), Math.max(...ends)] : [column, column];
|
|
611
|
+
const pick = (r: number, c: number, side: "first" | "last") => {
|
|
612
|
+
const x = rowAt(table, r);
|
|
613
|
+
if ("ok" in x) return x;
|
|
614
|
+
if (column !== undefined) return cellAt(table, r, c);
|
|
615
|
+
const cell = side === "first" ? x.cells[0] : x.cells[x.cells.length - 1];
|
|
616
|
+
return cell ?? refuse("invalid", `Row ${r} has no cells.`);
|
|
617
|
+
};
|
|
618
|
+
const anchor = pick(rows[0] as number, columns[0] as number, "first");
|
|
619
|
+
if ("ok" in anchor) return anchor;
|
|
620
|
+
const head = pick(rows[1] as number, columns[1] as number, "last");
|
|
621
|
+
if ("ok" in head) return head;
|
|
622
|
+
editor.dispatch(
|
|
623
|
+
editor.state.tr.setSelection(CellSelection.create(editor.state.doc, anchor.pos, head.pos)),
|
|
624
|
+
);
|
|
625
|
+
if (!editor.run(setCellShading(color)))
|
|
626
|
+
return refuse("refused", `The cells of table ${table.handle} could not be shaded.`);
|
|
627
|
+
return { ok: true, ...answer(context, table.handle) };
|
|
628
|
+
}
|
|
629
|
+
|
|
630
|
+
/**
|
|
631
|
+
* @internal
|
|
632
|
+
* Makes the first `count` rows the header rows (repeated on each page), and no others: each
|
|
633
|
+
* row's own property, tracked as its formatting change (23.2).
|
|
634
|
+
*/
|
|
635
|
+
export function setHeaderRows(context: Context, handle: string, count: number): Result<Changed> {
|
|
636
|
+
const { editor } = context;
|
|
637
|
+
const table = tableOf(context, handle);
|
|
638
|
+
if ("ok" in table) return table;
|
|
639
|
+
if (!Number.isInteger(count) || count < 0 || count > table.rows.length)
|
|
640
|
+
return refuse(
|
|
641
|
+
"invalid",
|
|
642
|
+
`count is how many rows from the top are header rows: 0 to ${table.rows.length}.`,
|
|
643
|
+
);
|
|
644
|
+
const tr = editor.state.tr;
|
|
645
|
+
table.rows.forEach((row, i) => {
|
|
646
|
+
const header = i < count;
|
|
647
|
+
if ((row.node.attrs.header === true) !== header)
|
|
648
|
+
tr.setNodeMarkup(row.pos, row.node.type, { ...row.node.attrs, header: header || null });
|
|
649
|
+
});
|
|
650
|
+
if (tr.docChanged) {
|
|
651
|
+
const before = editor.state.doc;
|
|
652
|
+
editor.dispatch(tr);
|
|
653
|
+
if (editor.state.doc === before)
|
|
654
|
+
return refuse(
|
|
655
|
+
"refused",
|
|
656
|
+
`The header rows of table ${table.handle} could not be set (a locked content control?).`,
|
|
657
|
+
);
|
|
658
|
+
}
|
|
659
|
+
return { ok: true, ...answer(context, table.handle) };
|
|
660
|
+
}
|
|
661
|
+
|
|
662
|
+
/** @internal What the table actions Foli cannot record as tracked changes say. */
|
|
663
|
+
export const UNTRACKED_TABLE_EDIT =
|
|
664
|
+
"Adding or deleting columns, merging or splitting cells and changing column widths cannot be recorded as tracked changes yet, and every edit here is one: nothing was changed. To give the reader a table with other columns, insert a new table next to this one with the cells you want, delete this one, and say why in a comment.";
|