@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,168 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Picture commands (10.8, ADR-0006): inserting an inline picture (its image a new resource of
|
|
3
|
+
* the envelope; alt text required, or the picture marked decorative), and changing a picture's
|
|
4
|
+
* size and alt text. Inserting and deleting a picture is tracked like text while tracking; its
|
|
5
|
+
* size does not change while tracking (Foli does not record it yet); alt text, which Word does
|
|
6
|
+
* not track either, changes any time.
|
|
7
|
+
*/
|
|
8
|
+
import type { Node } from "../model/index.js";
|
|
9
|
+
import { TransformError } from "../transform/index.js";
|
|
10
|
+
import type { ResourceJSON } from "../v3/document.js";
|
|
11
|
+
import { checkResource } from "../v3/document.js";
|
|
12
|
+
import { attempt } from "./attempt.js";
|
|
13
|
+
import { deleteSelectionKeepingTables } from "./commands.js";
|
|
14
|
+
import { closeHistory } from "./history.js";
|
|
15
|
+
import { typedMarks } from "./mark.js";
|
|
16
|
+
import { marksAt, NodeSelection, TextSelection } from "./selection.js";
|
|
17
|
+
import type { Command, EditorState } from "./state.js";
|
|
18
|
+
import { trackState, untracked } from "./track.js";
|
|
19
|
+
import { withEnvelope, wordContext } from "./word.js";
|
|
20
|
+
|
|
21
|
+
/** The largest size the schema takes, in EMU. */
|
|
22
|
+
const MAX_EMU = 2_147_483_647;
|
|
23
|
+
|
|
24
|
+
/** A picture to insert: its image (a `data:` URL or a URL the app gave) and its size in EMU. */
|
|
25
|
+
export interface NewPicture {
|
|
26
|
+
/** The image's media type (`image/png`…). */
|
|
27
|
+
contentType: string;
|
|
28
|
+
/** Where the image is: a URL the app gave (an upload's) … */
|
|
29
|
+
url?: string;
|
|
30
|
+
/** … or, opted into, a `data:` URL holding it. */
|
|
31
|
+
data?: string;
|
|
32
|
+
width: number;
|
|
33
|
+
height: number;
|
|
34
|
+
/** Required unless `decorative`. */
|
|
35
|
+
alt?: string;
|
|
36
|
+
title?: string;
|
|
37
|
+
/** Marked decorative: no alt text needed (a screen reader passes it by). */
|
|
38
|
+
decorative?: boolean;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
const validSize = (n: number) => Number.isInteger(n) && n >= 1 && n <= MAX_EMU;
|
|
42
|
+
/** Alt text and titles as the schema holds them. */
|
|
43
|
+
const validText = (value: unknown) =>
|
|
44
|
+
value === undefined || (typeof value === "string" && value.length <= 65_535);
|
|
45
|
+
/** A `data:` URL's media type, lower case. */
|
|
46
|
+
const dataType = (data: string) => /^data:([^;,]*)/i.exec(data)?.[1]?.toLowerCase();
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* Inserts `picture` at the selection (replacing it), its image added to the document's
|
|
50
|
+
* resources under a new key. Does not apply without alt text (unless decorative, or
|
|
51
|
+
* `requireAlt` is false: a picture pasted or dropped, asked for later and marked), with an image
|
|
52
|
+
* the document cannot hold (not an image type, no URL or `data:` URL, an unsafe URL), or where
|
|
53
|
+
* the selection is not in text.
|
|
54
|
+
*/
|
|
55
|
+
export function insertPicture(
|
|
56
|
+
picture: NewPicture,
|
|
57
|
+
{ requireAlt = true }: { requireAlt?: boolean } = {},
|
|
58
|
+
): Command {
|
|
59
|
+
return (state, dispatch) => {
|
|
60
|
+
const context = wordContext(state);
|
|
61
|
+
const type = state.schema.nodes.picture;
|
|
62
|
+
if (!context || !type) return false;
|
|
63
|
+
if (!validText(picture.alt) || !validText(picture.title)) return false;
|
|
64
|
+
const alt = picture.decorative ? "" : (picture.alt?.trim() ?? "");
|
|
65
|
+
if (!alt && !picture.decorative && requireAlt) return false;
|
|
66
|
+
if (!validSize(picture.width) || !validSize(picture.height)) return false;
|
|
67
|
+
// The bytes are of the type the document says (a part is named from it on export).
|
|
68
|
+
if (
|
|
69
|
+
picture.data !== undefined &&
|
|
70
|
+
dataType(String(picture.data)) !== String(picture.contentType).toLowerCase()
|
|
71
|
+
)
|
|
72
|
+
return false;
|
|
73
|
+
const resource: ResourceJSON = {
|
|
74
|
+
contentType: picture.contentType,
|
|
75
|
+
...(picture.url !== undefined ? { url: picture.url } : {}),
|
|
76
|
+
...(picture.data !== undefined ? { data: picture.data } : {}),
|
|
77
|
+
};
|
|
78
|
+
if (checkResource(resource) !== undefined) return false;
|
|
79
|
+
const resources = context.envelope.resources ?? {};
|
|
80
|
+
let n = Object.keys(resources).length + 1;
|
|
81
|
+
while (Object.hasOwn(resources, `foli-picture-${n}`)) n++;
|
|
82
|
+
const key = `foli-picture-${n}`;
|
|
83
|
+
const node = type.create({
|
|
84
|
+
resource: key,
|
|
85
|
+
width: picture.width,
|
|
86
|
+
height: picture.height,
|
|
87
|
+
alt: alt || null,
|
|
88
|
+
title: picture.title || null,
|
|
89
|
+
decorative: picture.decorative || null,
|
|
90
|
+
});
|
|
91
|
+
return attempt(state, dispatch, (tr) => {
|
|
92
|
+
tr.setMeta(closeHistory, true);
|
|
93
|
+
// Over selected cells, into the anchor cell (as typing goes).
|
|
94
|
+
if (!tr.selection.empty) deleteSelectionKeepingTables(tr, true);
|
|
95
|
+
const $pos = tr.selection.$from;
|
|
96
|
+
if (!$pos.parent.inlineContent || !$pos.parent.type.contentMatch.matchType(type))
|
|
97
|
+
throw new TransformError("No text where the picture would go");
|
|
98
|
+
// The marks typing would get there (its run's properties, a link), never a tracked
|
|
99
|
+
// change's: tracking marks it as its own.
|
|
100
|
+
const marks = typedMarks(state.storedMarks ?? marksAt($pos)).filter((m) =>
|
|
101
|
+
$pos.parent.type.allowsMarkType(m.type),
|
|
102
|
+
);
|
|
103
|
+
tr.insert($pos.pos, node.mark(marks));
|
|
104
|
+
withEnvelope(tr, { ...context.envelope, resources: { ...resources, [key]: resource } });
|
|
105
|
+
tr.setSelection(TextSelection.create(tr.doc, $pos.pos + 1));
|
|
106
|
+
});
|
|
107
|
+
};
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
/** The selected picture (a node selection of one) and its position, if any. */
|
|
111
|
+
export function selectedPicture(state: EditorState): { node: Node; pos: number } | null {
|
|
112
|
+
const { selection } = state;
|
|
113
|
+
return selection instanceof NodeSelection && selection.node.type.name === "picture"
|
|
114
|
+
? { node: selection.node, pos: selection.from }
|
|
115
|
+
: null;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
/** The picture at `pos`, if there is one. */
|
|
119
|
+
const pictureAt = (state: EditorState, pos: number) => {
|
|
120
|
+
const node = pos >= 0 && pos < state.doc.content.size ? state.doc.nodeAt(pos) : null;
|
|
121
|
+
return node?.type.name === "picture" ? node : null;
|
|
122
|
+
};
|
|
123
|
+
|
|
124
|
+
/**
|
|
125
|
+
* Sets the size (EMU) of the picture at `pos`. Not while tracking (Foli does not record a
|
|
126
|
+
* picture's size change yet).
|
|
127
|
+
*/
|
|
128
|
+
export function setPictureSize(pos: number, width: number, height: number): Command {
|
|
129
|
+
return (state, dispatch) => {
|
|
130
|
+
const node = pictureAt(state, pos);
|
|
131
|
+
if (!node || trackState(state)?.on === true) return false;
|
|
132
|
+
if (!validSize(width) || !validSize(height)) return false;
|
|
133
|
+
if (node.attrs.width === width && node.attrs.height === height) return false;
|
|
134
|
+
return attempt(state, dispatch, (tr) => {
|
|
135
|
+
tr.setMeta(closeHistory, true);
|
|
136
|
+
tr.setNodeMarkup(pos, node.type, { ...node.attrs, width, height });
|
|
137
|
+
// The picture stays selected (a resize keeps working on it).
|
|
138
|
+
if (state.selection instanceof NodeSelection && state.selection.from === pos)
|
|
139
|
+
tr.setSelection(NodeSelection.create(tr.doc, pos));
|
|
140
|
+
});
|
|
141
|
+
};
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
/**
|
|
145
|
+
* Sets the alt text of the picture at `pos`, or marks it decorative (no alt text). Does not
|
|
146
|
+
* apply with empty alt text unless decorative.
|
|
147
|
+
*/
|
|
148
|
+
export function setPictureAlt(pos: number, alt: string, decorative = false): Command {
|
|
149
|
+
return (state, dispatch) => {
|
|
150
|
+
const node = pictureAt(state, pos);
|
|
151
|
+
if (!node || !validText(alt)) return false;
|
|
152
|
+
// A decorative picture has no alt text (a screen reader passes it by).
|
|
153
|
+
const text = decorative ? null : alt.trim() || null;
|
|
154
|
+
if (!text && !decorative) return false;
|
|
155
|
+
if (node.attrs.alt === text && (node.attrs.decorative === true) === decorative) return false;
|
|
156
|
+
return attempt(state, dispatch, (tr) => {
|
|
157
|
+
// Not a tracked change, as in Word.
|
|
158
|
+
tr.setMeta(closeHistory, true).setMeta(untracked, true);
|
|
159
|
+
tr.setNodeMarkup(pos, node.type, {
|
|
160
|
+
...node.attrs,
|
|
161
|
+
alt: text,
|
|
162
|
+
decorative: decorative || null,
|
|
163
|
+
});
|
|
164
|
+
if (state.selection instanceof NodeSelection && state.selection.from === pos)
|
|
165
|
+
tr.setSelection(NodeSelection.create(tr.doc, pos));
|
|
166
|
+
});
|
|
167
|
+
};
|
|
168
|
+
}
|
package/src/state/review.ts
CHANGED
|
@@ -10,11 +10,13 @@
|
|
|
10
10
|
* together. These edits are not tracked themselves, and undo takes them back.
|
|
11
11
|
*/
|
|
12
12
|
import type { Attrs, Mark, Node } from "../model/index.js";
|
|
13
|
+
import { TransformError } from "../transform/transform.js";
|
|
13
14
|
import { RUN_PROPERTY_MARKS } from "../v3/schema.js";
|
|
14
15
|
import { closeHistory } from "./history.js";
|
|
15
16
|
import { MARK_ATTRS } from "./mark.js";
|
|
16
17
|
import { TextSelection } from "./selection.js";
|
|
17
18
|
import type { Command, EditorState, Transaction } from "./state.js";
|
|
19
|
+
import { keepTablesApart } from "./tables.js";
|
|
18
20
|
import { RECORDED, trackState, untracked } from "./track.js";
|
|
19
21
|
|
|
20
22
|
export type ChangeKind =
|
|
@@ -23,7 +25,9 @@ export type ChangeKind =
|
|
|
23
25
|
| "formatting"
|
|
24
26
|
| "paragraphInsertion"
|
|
25
27
|
| "paragraphDeletion"
|
|
26
|
-
| "paragraphFormatting"
|
|
28
|
+
| "paragraphFormatting"
|
|
29
|
+
| "rowInsertion"
|
|
30
|
+
| "rowDeletion";
|
|
27
31
|
|
|
28
32
|
/** One tracked change of the document, where it is. */
|
|
29
33
|
export interface Change {
|
|
@@ -71,8 +75,27 @@ export function trackedChanges(doc: Node): Change[] {
|
|
|
71
75
|
move: (attrs.move as string | null | undefined) ?? null,
|
|
72
76
|
});
|
|
73
77
|
};
|
|
74
|
-
|
|
75
|
-
|
|
78
|
+
// Every paragraph, those in table cells too; a table row's own insertion or deletion.
|
|
79
|
+
doc.descendants((block, offset) => {
|
|
80
|
+
if (block.type.name === "tableRow") {
|
|
81
|
+
const revision = block.attrs.revision as { insertion?: Revision; deletion?: Revision } | null;
|
|
82
|
+
for (const [kind, r] of [
|
|
83
|
+
["rowInsertion", revision?.insertion],
|
|
84
|
+
["rowDeletion", revision?.deletion],
|
|
85
|
+
] as const)
|
|
86
|
+
if (r)
|
|
87
|
+
changes.push({
|
|
88
|
+
kind,
|
|
89
|
+
id: r.id,
|
|
90
|
+
author: r.author,
|
|
91
|
+
date: r.date ?? null,
|
|
92
|
+
from: offset,
|
|
93
|
+
to: offset + block.nodeSize,
|
|
94
|
+
move: null,
|
|
95
|
+
});
|
|
96
|
+
return true;
|
|
97
|
+
}
|
|
98
|
+
if (!block.isTextblock) return true;
|
|
76
99
|
const change = block.attrs.formatChange as Revision | null;
|
|
77
100
|
if (change) changes.push(paragraphChange("paragraphFormatting", change, offset, block));
|
|
78
101
|
// Runs of content with the same change mark, extended while the next node has it too.
|
|
@@ -95,6 +118,7 @@ export function trackedChanges(doc: Node): Change[] {
|
|
|
95
118
|
changes.push(paragraphChange("paragraphInsertion", revision.insertion, offset, block));
|
|
96
119
|
if (revision?.deletion)
|
|
97
120
|
changes.push(paragraphChange("paragraphDeletion", revision.deletion, offset, block));
|
|
121
|
+
return false;
|
|
98
122
|
});
|
|
99
123
|
return changes.sort((a, b) => a.from - b.from || a.to - b.to);
|
|
100
124
|
}
|
|
@@ -154,6 +178,10 @@ function resolve(tr: Transaction, changes: readonly Change[], accept: boolean):
|
|
|
154
178
|
const schema = tr.doc.type.schema;
|
|
155
179
|
const removed: [number, number][] = [];
|
|
156
180
|
const joins: { pos: number; keepFirst: boolean }[] = [];
|
|
181
|
+
/** Rows that go, by their table (its position): a table losing every row goes whole. */
|
|
182
|
+
const rowsGoing = new Map<number, [number, number][]>();
|
|
183
|
+
/** Whether whole rows or paragraphs go: what lies inside them must not be removed again. */
|
|
184
|
+
let wholes = false;
|
|
157
185
|
for (const change of changes) {
|
|
158
186
|
const { kind, from, to } = change;
|
|
159
187
|
const markType = schema.marks[kind === "formatting" ? "formatChange" : kind];
|
|
@@ -196,6 +224,21 @@ function resolve(tr: Transaction, changes: readonly Change[], accept: boolean):
|
|
|
196
224
|
tr.setNodeMarkup(start, node.type, attrs);
|
|
197
225
|
break;
|
|
198
226
|
}
|
|
227
|
+
case "rowInsertion":
|
|
228
|
+
case "rowDeletion": {
|
|
229
|
+
const row = tr.doc.nodeAt(from);
|
|
230
|
+
if (row?.type.name !== "tableRow") break;
|
|
231
|
+
if ((kind === "rowDeletion") === accept) {
|
|
232
|
+
const table = tr.doc.resolve(from).before();
|
|
233
|
+
rowsGoing.set(table, [...(rowsGoing.get(table) ?? []), [from, to]]);
|
|
234
|
+
} else {
|
|
235
|
+
const revision = { ...(row.attrs.revision as Record<string, unknown>) };
|
|
236
|
+
delete revision[kind === "rowInsertion" ? "insertion" : "deletion"];
|
|
237
|
+
const next = Object.keys(revision).length ? revision : null;
|
|
238
|
+
tr.setNodeMarkup(from, row.type, { ...row.attrs, revision: next });
|
|
239
|
+
}
|
|
240
|
+
break;
|
|
241
|
+
}
|
|
199
242
|
case "paragraphInsertion":
|
|
200
243
|
case "paragraphDeletion": {
|
|
201
244
|
const start = tr.doc.resolve(from).before();
|
|
@@ -212,16 +255,46 @@ function resolve(tr: Transaction, changes: readonly Change[], accept: boolean):
|
|
|
212
255
|
const emptied = covered(node, start + 1, removed);
|
|
213
256
|
if (goes && tr.doc.resolve(end).nodeAfter?.isTextblock)
|
|
214
257
|
joins.push({ pos: end, keepFirst: accept && !emptied });
|
|
258
|
+
// Nothing to join with (a table follows, or a cell ends): an inserted paragraph left
|
|
259
|
+
// empty goes whole when rejected (Word's rules are restored after: `keepTablesApart`).
|
|
260
|
+
// (Never a cell's only paragraph: a cell ends with one.)
|
|
261
|
+
else if (
|
|
262
|
+
goes &&
|
|
263
|
+
!accept &&
|
|
264
|
+
(!node.content.size || emptied) &&
|
|
265
|
+
tr.doc.resolve(start).parent.childCount > 1
|
|
266
|
+
) {
|
|
267
|
+
removed.push([start, end]);
|
|
268
|
+
wholes = true;
|
|
269
|
+
}
|
|
215
270
|
break;
|
|
216
271
|
}
|
|
217
272
|
}
|
|
218
273
|
}
|
|
274
|
+
// Rows that go, or their table when every row does; what lies inside them is not removed again.
|
|
275
|
+
for (const [table, rows] of rowsGoing) {
|
|
276
|
+
const node = tr.doc.nodeAt(table);
|
|
277
|
+
if (node && rows.length === node.childCount) removed.push([table, table + node.nodeSize]);
|
|
278
|
+
else removed.push(...rows);
|
|
279
|
+
}
|
|
280
|
+
if (rowsGoing.size || wholes) {
|
|
281
|
+
const outer = removed.filter(
|
|
282
|
+
([s, e]) => !removed.some(([s2, e2]) => (s2 < s && e <= e2) || (s2 <= s && e < e2)),
|
|
283
|
+
);
|
|
284
|
+
removed.length = 0;
|
|
285
|
+
removed.push(...outer);
|
|
286
|
+
}
|
|
219
287
|
// Content removed and paragraphs joined, from the end so earlier positions stay.
|
|
220
288
|
const ops = [
|
|
221
289
|
...removed.map(([s, e]) => ({ at: s, run: () => tr.delete(s, e) })),
|
|
222
|
-
...joins
|
|
290
|
+
...joins
|
|
291
|
+
.filter(
|
|
292
|
+
({ pos }) => !(rowsGoing.size || wholes) || !removed.some(([s, e]) => s < pos && pos < e),
|
|
293
|
+
)
|
|
294
|
+
.map(({ pos, keepFirst }) => ({ at: pos - 1, run: () => join(tr, pos, keepFirst) })),
|
|
223
295
|
].sort((a, b) => b.at - a.at);
|
|
224
296
|
for (const op of ops) op.run();
|
|
297
|
+
keepTablesApart(tr);
|
|
225
298
|
}
|
|
226
299
|
|
|
227
300
|
/** Whether every child of `node` (content at `start`) but markers lies in `removed`. */
|
|
@@ -266,30 +339,39 @@ function join(tr: Transaction, pos: number, keepFirst: boolean): void {
|
|
|
266
339
|
|
|
267
340
|
function command(
|
|
268
341
|
accept: boolean,
|
|
269
|
-
range: (state: EditorState) =>
|
|
342
|
+
range: (state: EditorState) => readonly { from: number; to: number }[],
|
|
270
343
|
filter?: ChangeFilter,
|
|
271
344
|
): Command {
|
|
272
345
|
return (state, dispatch) => {
|
|
273
346
|
if (!state.schema.marks.insertion) return false;
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
const changes =
|
|
347
|
+
// A change around several selected cells (a row revision, a move's partner) counts once.
|
|
348
|
+
const seen = new Set<string>();
|
|
349
|
+
const changes = range(state)
|
|
350
|
+
.flatMap((r) => changesIn(state.doc, r.from, r.to, filter))
|
|
351
|
+
.filter((c) => {
|
|
352
|
+
const key = `${c.kind} ${c.id} ${c.from} ${c.to}`;
|
|
353
|
+
return !seen.has(key) && !!seen.add(key);
|
|
354
|
+
});
|
|
277
355
|
if (!changes.length) return false;
|
|
278
356
|
if (dispatch) {
|
|
279
357
|
// Its own undo step, never merged with the typing before it.
|
|
280
358
|
const tr = state.tr.setMeta(untracked, true).setMeta(closeHistory, true);
|
|
281
|
-
|
|
359
|
+
// A document the result would not fit (a structure Word allows and Foli cannot change
|
|
360
|
+
// yet): nothing is changed.
|
|
361
|
+
try {
|
|
362
|
+
resolve(tr, changes, accept);
|
|
363
|
+
} catch (error) {
|
|
364
|
+
if (error instanceof TransformError) return false;
|
|
365
|
+
throw error;
|
|
366
|
+
}
|
|
282
367
|
dispatch(tr);
|
|
283
368
|
}
|
|
284
369
|
return true;
|
|
285
370
|
};
|
|
286
371
|
}
|
|
287
372
|
|
|
288
|
-
const selected = (state: EditorState)
|
|
289
|
-
|
|
290
|
-
state.selection.to,
|
|
291
|
-
];
|
|
292
|
-
const everything = (state: EditorState): [number, number] => [0, state.doc.content.size];
|
|
373
|
+
const selected = (state: EditorState) => state.selection.ranges;
|
|
374
|
+
const everything = (state: EditorState) => [{ from: 0, to: state.doc.content.size }];
|
|
293
375
|
|
|
294
376
|
/** Accepts the change at the caret, or the changes in the selection. */
|
|
295
377
|
export const acceptChange: Command = command(true, selected);
|
package/src/state/selection.ts
CHANGED
|
@@ -2,7 +2,8 @@ import { Mark, type Node, type ResolvedPos } from "../model/index.js";
|
|
|
2
2
|
import type { Mappable } from "../transform/stepmap.js";
|
|
3
3
|
|
|
4
4
|
export interface SelectionJSON {
|
|
5
|
-
|
|
5
|
+
/** `cell`: a `CellSelection` (anchor and head are the positions before two cells). */
|
|
6
|
+
type: "text" | "node" | "all" | "cell";
|
|
6
7
|
anchor: number;
|
|
7
8
|
head: number;
|
|
8
9
|
}
|
|
@@ -42,6 +43,14 @@ export abstract class Selection {
|
|
|
42
43
|
return this.from === this.to;
|
|
43
44
|
}
|
|
44
45
|
|
|
46
|
+
/**
|
|
47
|
+
* The ranges the selection covers, in document order: `from..to`, or one per selected cell
|
|
48
|
+
* for a cell selection (so commands that act on a range leave the cells outside it alone).
|
|
49
|
+
*/
|
|
50
|
+
get ranges(): readonly { from: number; to: number }[] {
|
|
51
|
+
return [{ from: this.from, to: this.to }];
|
|
52
|
+
}
|
|
53
|
+
|
|
45
54
|
/** This selection moved into `doc` through `mapping`. */
|
|
46
55
|
abstract map(doc: Node, mapping: Mappable): Selection;
|
|
47
56
|
|
|
@@ -74,9 +83,14 @@ export abstract class Selection {
|
|
|
74
83
|
return Selection.near(doc.resolve(doc.content.size), -1);
|
|
75
84
|
}
|
|
76
85
|
|
|
86
|
+
/** Selection kinds defined elsewhere (`cell`: tables), by their JSON `type`. */
|
|
87
|
+
static readonly kinds = new Map<string, (doc: Node, json: SelectionJSON) => Selection>();
|
|
88
|
+
|
|
77
89
|
/** Restores a selection, clamping positions into the document. */
|
|
78
90
|
static fromJSON(doc: Node, json: SelectionJSON): Selection {
|
|
79
91
|
if (json.type === "all") return new AllSelection(doc);
|
|
92
|
+
const kind = Selection.kinds.get(json.type);
|
|
93
|
+
if (kind) return kind(doc, json);
|
|
80
94
|
const clamp = (pos: number) => Math.max(0, Math.min(pos, doc.content.size));
|
|
81
95
|
if (json.type === "node") {
|
|
82
96
|
const $pos = doc.resolve(clamp(json.anchor));
|