@folijs/agent 0.3.2 → 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 +32 -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 +147 -18
- 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 +256 -20
- package/src/stories.ts +92 -0
- package/src/tables.ts +664 -0
- package/src/tools.ts +925 -0
package/src/inserts.ts
ADDED
|
@@ -0,0 +1,339 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Inserting what is not text (23.6): pictures, page and section breaks, horizontal rules,
|
|
3
|
+
* footnotes and endnotes, next to or in a paragraph, each through core's command for it so
|
|
4
|
+
* every insertion is tracked as the editor tracks it.
|
|
5
|
+
*/
|
|
6
|
+
import {
|
|
7
|
+
insertNote,
|
|
8
|
+
insertPageBreak,
|
|
9
|
+
insertPicture,
|
|
10
|
+
insertSectionBreak,
|
|
11
|
+
type Node,
|
|
12
|
+
type SectionBreakKind,
|
|
13
|
+
} from "@folijs/core";
|
|
14
|
+
import { wordContext } from "@folijs/core/internal";
|
|
15
|
+
import {
|
|
16
|
+
type Changed,
|
|
17
|
+
type Context,
|
|
18
|
+
changed,
|
|
19
|
+
inline,
|
|
20
|
+
insert,
|
|
21
|
+
lockedControls,
|
|
22
|
+
MAX_WRITTEN,
|
|
23
|
+
paragraphAt,
|
|
24
|
+
placesIn,
|
|
25
|
+
quote,
|
|
26
|
+
} from "./operations.js";
|
|
27
|
+
import { type Refusal, type Result, refuse } from "./result.js";
|
|
28
|
+
|
|
29
|
+
/** A picture's image: its bytes, its type, and what it shows for a reader who cannot see it. */
|
|
30
|
+
export interface PictureItem {
|
|
31
|
+
kind: "picture";
|
|
32
|
+
data: Uint8Array;
|
|
33
|
+
/** `image/png`, `image/jpeg`, `image/gif`: left out, read from the bytes. */
|
|
34
|
+
contentType?: string;
|
|
35
|
+
/** Alt text, required: what the picture shows. */
|
|
36
|
+
alt: string;
|
|
37
|
+
/** In points; with one of them, the other keeps the image's proportions. Default: its size, fitted to the page. */
|
|
38
|
+
width?: number;
|
|
39
|
+
height?: number;
|
|
40
|
+
}
|
|
41
|
+
/** What `insertItem` inserts. */
|
|
42
|
+
export type InsertItem =
|
|
43
|
+
| PictureItem
|
|
44
|
+
| { kind: "pageBreak" }
|
|
45
|
+
| { kind: "sectionBreak"; start?: SectionBreakKind }
|
|
46
|
+
| { kind: "horizontalRule" }
|
|
47
|
+
| { kind: "footnote" | "endnote"; text: string; on?: string };
|
|
48
|
+
|
|
49
|
+
export interface InsertItemOptions {
|
|
50
|
+
/** A picture or a rule as a paragraph of its own after (default) or before the block; a break at the paragraph's end or start. */
|
|
51
|
+
where?: "after" | "before";
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* A paragraph `insert` made to hold a picture or a rule: none of its neighbour's properties
|
|
56
|
+
* (a heading's style, a list's number), only the content controls it is in.
|
|
57
|
+
*/
|
|
58
|
+
function plain(context: Context, handle: string, extra: Record<string, unknown> = {}): boolean {
|
|
59
|
+
const found = context.handles.resolve(handle);
|
|
60
|
+
if (!found) return false;
|
|
61
|
+
const { editor } = context;
|
|
62
|
+
editor.dispatch(
|
|
63
|
+
editor.state.tr.setNodeMarkup(found.pos, found.node.type, {
|
|
64
|
+
controls: found.node.attrs.controls ?? null,
|
|
65
|
+
// Its own insertion stays recorded: rejecting it takes the paragraph out.
|
|
66
|
+
revision: found.node.attrs.revision ?? null,
|
|
67
|
+
...extra,
|
|
68
|
+
}),
|
|
69
|
+
);
|
|
70
|
+
return true;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/** EMU per point, as DrawingML measures. */
|
|
74
|
+
const EMU = 12_700;
|
|
75
|
+
/** The largest size a picture has, in EMU (as core's schema holds it). */
|
|
76
|
+
const MAX_EMU = 2_147_483_647;
|
|
77
|
+
/** The text width when the page does not say, in points (A4 with 2.54 cm margins). */
|
|
78
|
+
const TEXT_WIDTH = 451;
|
|
79
|
+
|
|
80
|
+
/** An image's size in pixels and its type, from its first bytes; null when not known. */
|
|
81
|
+
export function imageInfo(
|
|
82
|
+
bytes: Uint8Array,
|
|
83
|
+
): { width: number; height: number; contentType: string } | null {
|
|
84
|
+
const at = (i: number) => bytes[i] ?? 0;
|
|
85
|
+
const be16 = (i: number) => (at(i) << 8) | at(i + 1);
|
|
86
|
+
const be32 = (i: number) =>
|
|
87
|
+
((at(i) << 24) >>> 0) + (at(i + 1) << 16) + (at(i + 2) << 8) + at(i + 3);
|
|
88
|
+
// PNG: the signature, then IHDR's width and height.
|
|
89
|
+
if (
|
|
90
|
+
at(0) === 0x89 &&
|
|
91
|
+
at(1) === 0x50 &&
|
|
92
|
+
at(2) === 0x4e &&
|
|
93
|
+
at(3) === 0x47 &&
|
|
94
|
+
String.fromCharCode(at(12), at(13), at(14), at(15)) === "IHDR"
|
|
95
|
+
)
|
|
96
|
+
return { width: be32(16), height: be32(20), contentType: "image/png" };
|
|
97
|
+
// GIF: GIF87a or GIF89a, then the screen's width and height, little-endian.
|
|
98
|
+
const gif = String.fromCharCode(at(0), at(1), at(2), at(3), at(4), at(5));
|
|
99
|
+
if (gif === "GIF87a" || gif === "GIF89a")
|
|
100
|
+
return { width: at(6) | (at(7) << 8), height: at(8) | (at(9) << 8), contentType: "image/gif" };
|
|
101
|
+
// JPEG: markers up to a start of frame (SOF0–SOF15 but DHT, JPG and DAC).
|
|
102
|
+
if (at(0) === 0xff && at(1) === 0xd8) {
|
|
103
|
+
let i = 2;
|
|
104
|
+
while (i + 9 < bytes.length) {
|
|
105
|
+
if (at(i) !== 0xff) return null;
|
|
106
|
+
const marker = at(i + 1);
|
|
107
|
+
if (marker >= 0xc0 && marker <= 0xcf && ![0xc4, 0xc8, 0xcc].includes(marker))
|
|
108
|
+
return { width: be16(i + 7), height: be16(i + 5), contentType: "image/jpeg" };
|
|
109
|
+
i += 2 + be16(i + 2);
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
return null;
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
const B64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
|
|
116
|
+
|
|
117
|
+
/** @internal Bytes as base64, with no platform's encoder (the package runs anywhere core does). */
|
|
118
|
+
export function base64(bytes: Uint8Array): string {
|
|
119
|
+
const chunks: string[] = [];
|
|
120
|
+
let out: string[] = [];
|
|
121
|
+
for (let i = 0; i < bytes.length; i += 3) {
|
|
122
|
+
// A string per 196 K characters (48 K entries of 4), not one array of all: a large picture's memory.
|
|
123
|
+
if (out.length >= 0xc000) {
|
|
124
|
+
chunks.push(out.join(""));
|
|
125
|
+
out = [];
|
|
126
|
+
}
|
|
127
|
+
const a = bytes[i] as number;
|
|
128
|
+
const b = bytes[i + 1];
|
|
129
|
+
const c = bytes[i + 2];
|
|
130
|
+
const n = (a << 16) | ((b ?? 0) << 8) | (c ?? 0);
|
|
131
|
+
out.push(
|
|
132
|
+
B64[(n >> 18) & 63] as string,
|
|
133
|
+
B64[(n >> 12) & 63] as string,
|
|
134
|
+
b === undefined ? "=" : (B64[(n >> 6) & 63] as string),
|
|
135
|
+
c === undefined ? "=" : (B64[n & 63] as string),
|
|
136
|
+
);
|
|
137
|
+
}
|
|
138
|
+
chunks.push(out.join(""));
|
|
139
|
+
return chunks.join("");
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
/** The body's text width in points, from the last section's page. */
|
|
143
|
+
function textWidth(context: Context): number {
|
|
144
|
+
const section = wordContext(context.editor.state)?.envelope.section;
|
|
145
|
+
const width = section?.pageSize?.width;
|
|
146
|
+
const margins = section?.margins as { left?: number; right?: number } | undefined;
|
|
147
|
+
if (typeof width !== "number") return TEXT_WIDTH;
|
|
148
|
+
return Math.max(36, (width - (margins?.left ?? 1440) - (margins?.right ?? 1440)) / 20);
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
const KINDS = ["picture", "pageBreak", "sectionBreak", "horizontalRule", "footnote", "endnote"];
|
|
152
|
+
const STARTS: readonly SectionBreakKind[] = ["nextPage", "continuous", "evenPage", "oddPage"];
|
|
153
|
+
|
|
154
|
+
/** Why `item` cannot be inserted as given, or null. */
|
|
155
|
+
function wrong(item: unknown): string | null {
|
|
156
|
+
if (!item || typeof item !== "object" || !KINDS.includes((item as { kind?: string }).kind ?? ""))
|
|
157
|
+
return `kind is one of ${KINDS.join(", ")}.`;
|
|
158
|
+
const i = item as Record<string, unknown>;
|
|
159
|
+
const size = (v: unknown) =>
|
|
160
|
+
v === undefined || (typeof v === "number" && v >= 1 && v <= 22 * 72 * 10);
|
|
161
|
+
if (i.kind === "picture") {
|
|
162
|
+
if (!(i.data instanceof Uint8Array) || !i.data.length)
|
|
163
|
+
return "A picture needs its image's bytes.";
|
|
164
|
+
if (typeof i.alt !== "string" || !i.alt.trim())
|
|
165
|
+
return "A picture needs alt text: what it shows, for a reader who cannot see it.";
|
|
166
|
+
if (!size(i.width) || !size(i.height)) return "width and height are in points, 1 to 15840.";
|
|
167
|
+
}
|
|
168
|
+
if (
|
|
169
|
+
i.kind === "sectionBreak" &&
|
|
170
|
+
i.start !== undefined &&
|
|
171
|
+
!STARTS.includes(i.start as SectionBreakKind)
|
|
172
|
+
)
|
|
173
|
+
return `start is one of ${STARTS.join(", ")}.`;
|
|
174
|
+
if (
|
|
175
|
+
(i.kind === "footnote" || i.kind === "endnote") &&
|
|
176
|
+
(typeof i.text !== "string" || !i.text.trim())
|
|
177
|
+
)
|
|
178
|
+
return "A note needs its text.";
|
|
179
|
+
if ((i.kind === "footnote" || i.kind === "endnote") && (i.text as string).length > MAX_WRITTEN)
|
|
180
|
+
return `A note's text is at most ${MAX_WRITTEN} characters.`;
|
|
181
|
+
return null;
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
/** @internal */
|
|
185
|
+
export function insertItem(
|
|
186
|
+
context: Context,
|
|
187
|
+
handle: string,
|
|
188
|
+
item: InsertItem,
|
|
189
|
+
options: InsertItemOptions = {},
|
|
190
|
+
): Result<Changed> {
|
|
191
|
+
const { editor } = context;
|
|
192
|
+
const why = wrong(item);
|
|
193
|
+
if (why) return refuse("invalid", why);
|
|
194
|
+
const where = options.where ?? "after";
|
|
195
|
+
if (where !== "after" && where !== "before")
|
|
196
|
+
return refuse("invalid", 'where is "after" or "before".');
|
|
197
|
+
const target: Refusal | { pos: number; node: Node } = paragraphAt(
|
|
198
|
+
context,
|
|
199
|
+
handle,
|
|
200
|
+
"it goes next to or in a paragraph",
|
|
201
|
+
);
|
|
202
|
+
if ("ok" in target) return target;
|
|
203
|
+
if (context.story && item.kind !== "picture" && item.kind !== "horizontalRule")
|
|
204
|
+
return refuse(
|
|
205
|
+
"invalid",
|
|
206
|
+
`A header or footer holds no page or section breaks and no notes: put the ${item.kind === "footnote" || item.kind === "endnote" ? "note" : "break"} in the body.`,
|
|
207
|
+
);
|
|
208
|
+
const locked = lockedControls(context, target.pos, target.node)[0];
|
|
209
|
+
if (locked)
|
|
210
|
+
return refuse(
|
|
211
|
+
"read-only",
|
|
212
|
+
`Block ${handle} is in the locked content control ${quote(locked)}: nothing goes in it.`,
|
|
213
|
+
);
|
|
214
|
+
const end = target.pos + target.node.nodeSize - 1;
|
|
215
|
+
const caret = where === "after" ? end : target.pos + 1;
|
|
216
|
+
const before = editor.state.doc;
|
|
217
|
+
const failed = (what: string) =>
|
|
218
|
+
refuse(
|
|
219
|
+
"refused",
|
|
220
|
+
`${what} could not be inserted ${where === "after" ? "after" : "before"} block ${handle}. Nothing was changed.`,
|
|
221
|
+
);
|
|
222
|
+
|
|
223
|
+
switch (item.kind) {
|
|
224
|
+
case "pageBreak": {
|
|
225
|
+
editor.select(caret);
|
|
226
|
+
if (!editor.run(insertPageBreak) || editor.state.doc === before)
|
|
227
|
+
return failed("A page break");
|
|
228
|
+
return { ok: true, ...changed(context, [handle]) };
|
|
229
|
+
}
|
|
230
|
+
case "sectionBreak": {
|
|
231
|
+
if (editor.state.doc.resolve(target.pos).depth !== 0)
|
|
232
|
+
return refuse(
|
|
233
|
+
"invalid",
|
|
234
|
+
"A section break goes in a paragraph of the body, not in a table.",
|
|
235
|
+
);
|
|
236
|
+
editor.select(caret);
|
|
237
|
+
if (!editor.run(insertSectionBreak(item.start ?? "nextPage")) || editor.state.doc === before)
|
|
238
|
+
return failed("A section break");
|
|
239
|
+
// The paragraph split there. The handle follows the paragraph mark, which is the second
|
|
240
|
+
// part's: the first part, ending the section, is a paragraph of its own.
|
|
241
|
+
const now = context.handles.resolve(handle);
|
|
242
|
+
const first = now ? editor.state.doc.resolve(now.pos).nodeBefore : null;
|
|
243
|
+
const handles = [handle];
|
|
244
|
+
if (now && first) handles.unshift(context.handles.at(now.pos - first.nodeSize, first));
|
|
245
|
+
return { ok: true, ...changed(context, handles) };
|
|
246
|
+
}
|
|
247
|
+
case "horizontalRule": {
|
|
248
|
+
// Word's horizontal line: an empty paragraph with a bottom border, as typing --- makes.
|
|
249
|
+
const made = insert(context, handle, [""], where);
|
|
250
|
+
if (!made.ok) return made;
|
|
251
|
+
const rule = made.blocks[0]?.handle as string;
|
|
252
|
+
const borders = { bottom: { style: "single", size: 6, space: 1, color: "auto" } };
|
|
253
|
+
if (!plain(context, rule, { borders })) return failed("A horizontal line");
|
|
254
|
+
return { ok: true, ...changed(context, [rule]) };
|
|
255
|
+
}
|
|
256
|
+
case "footnote":
|
|
257
|
+
case "endnote": {
|
|
258
|
+
const places =
|
|
259
|
+
item.on === undefined
|
|
260
|
+
? [{ from: end, to: end }]
|
|
261
|
+
: placesIn(context, handle, target, item.on, false);
|
|
262
|
+
if (!Array.isArray(places)) return places;
|
|
263
|
+
const at = (places[0] as { to: number }).to;
|
|
264
|
+
const stories = () => new Set(Object.keys(wordContext(editor.state)?.envelope.stories ?? {}));
|
|
265
|
+
const had = stories();
|
|
266
|
+
editor.select(at);
|
|
267
|
+
if (!editor.run(insertNote(item.kind)) || editor.state.doc === before)
|
|
268
|
+
return failed(`A ${item.kind}`);
|
|
269
|
+
const id = [...stories()].find((s) => !had.has(s));
|
|
270
|
+
const story = id ? editor.story(id) : null;
|
|
271
|
+
if (!story) return failed(`A ${item.kind}`);
|
|
272
|
+
// Word's new note: its mark and a space; the text goes after them.
|
|
273
|
+
const content = story.state.doc.firstChild;
|
|
274
|
+
if (!content) return failed(`A ${item.kind}`);
|
|
275
|
+
story.dispatch(story.state.tr.insert(1 + content.content.size, inline(editor, item.text)));
|
|
276
|
+
return { ok: true, ...changed(context, [handle]) };
|
|
277
|
+
}
|
|
278
|
+
case "picture": {
|
|
279
|
+
const info = imageInfo(item.data);
|
|
280
|
+
const contentType = item.contentType ?? info?.contentType;
|
|
281
|
+
if (!contentType)
|
|
282
|
+
return refuse(
|
|
283
|
+
"invalid",
|
|
284
|
+
"The image's type could not be told from its bytes: give its contentType (image/png, image/jpeg…).",
|
|
285
|
+
);
|
|
286
|
+
if (
|
|
287
|
+
info &&
|
|
288
|
+
(info.width < 1 || info.height < 1) &&
|
|
289
|
+
(item.width === undefined || item.height === undefined)
|
|
290
|
+
)
|
|
291
|
+
return refuse(
|
|
292
|
+
"invalid",
|
|
293
|
+
"The image's header gives it no size: give its width and height in points.",
|
|
294
|
+
);
|
|
295
|
+
if (!info && (item.width === undefined || item.height === undefined))
|
|
296
|
+
return refuse(
|
|
297
|
+
"invalid",
|
|
298
|
+
"The image's size could not be read from its bytes: give its width and height in points.",
|
|
299
|
+
);
|
|
300
|
+
// Pixels at 96 per inch, fitted to the text width; or as given, keeping the proportions.
|
|
301
|
+
const ratio = info && info.width > 0 ? info.height / info.width : 1;
|
|
302
|
+
let width =
|
|
303
|
+
item.width ?? (item.height !== undefined ? item.height / ratio : (info?.width ?? 0) * 0.75);
|
|
304
|
+
let height = item.height ?? width * ratio;
|
|
305
|
+
const most = textWidth(context);
|
|
306
|
+
if (item.width === undefined && item.height === undefined && width > most) {
|
|
307
|
+
height = (height * most) / width;
|
|
308
|
+
width = most;
|
|
309
|
+
}
|
|
310
|
+
if (width * EMU > MAX_EMU || height * EMU > MAX_EMU)
|
|
311
|
+
return refuse(
|
|
312
|
+
"invalid",
|
|
313
|
+
"The picture would be larger than Word holds (about 2,350 inches): give a smaller width or height.",
|
|
314
|
+
);
|
|
315
|
+
const made = insert(context, handle, [""], where);
|
|
316
|
+
if (!made.ok) return made;
|
|
317
|
+
const holder = made.blocks[0]?.handle as string;
|
|
318
|
+
if (!plain(context, holder)) return failed("A picture");
|
|
319
|
+
const found = context.handles.resolve(holder);
|
|
320
|
+
if (!found) return failed("A picture");
|
|
321
|
+
editor.select(found.pos + 1);
|
|
322
|
+
const inserted = editor.run(
|
|
323
|
+
insertPicture({
|
|
324
|
+
contentType,
|
|
325
|
+
data: `data:${contentType};base64,${base64(item.data)}`,
|
|
326
|
+
width: Math.max(1, Math.round(width * EMU)),
|
|
327
|
+
height: Math.max(1, Math.round(height * EMU)),
|
|
328
|
+
alt: item.alt.trim(),
|
|
329
|
+
}),
|
|
330
|
+
);
|
|
331
|
+
if (!inserted)
|
|
332
|
+
return refuse(
|
|
333
|
+
"refused",
|
|
334
|
+
`The picture could not be inserted: its type ${quote(contentType)} or its bytes are not an image the document holds. Nothing was changed.`,
|
|
335
|
+
);
|
|
336
|
+
return { ok: true, ...changed(context, [holder]) };
|
|
337
|
+
}
|
|
338
|
+
}
|
|
339
|
+
}
|
package/src/operations.ts
CHANGED
|
@@ -47,6 +47,8 @@ export interface Context {
|
|
|
47
47
|
editor: Editor;
|
|
48
48
|
handles: Handles;
|
|
49
49
|
author: string;
|
|
50
|
+
/** The header's or footer's story id when this is one's (23.7); the body's: none. */
|
|
51
|
+
story?: string;
|
|
50
52
|
}
|
|
51
53
|
|
|
52
54
|
type Attrs = Record<string, unknown>;
|
|
@@ -82,7 +84,8 @@ const cut = (text: string, most: number): string => {
|
|
|
82
84
|
return text.slice(0, low >= 0xdc00 && low <= 0xdfff ? most - 1 : most);
|
|
83
85
|
};
|
|
84
86
|
|
|
85
|
-
|
|
87
|
+
/** @internal A text quoted for a message, cut, with look-alike characters shown. */
|
|
88
|
+
export const quote = (text: string, most = 80) =>
|
|
86
89
|
JSON.stringify(text.length > most ? `${cut(text, most - 3)}…` : text).replace(
|
|
87
90
|
LOOKALIKE,
|
|
88
91
|
(c) => `\\u${c.charCodeAt(0).toString(16).padStart(4, "0")}`,
|
|
@@ -103,7 +106,10 @@ export function resolve(context: Context, handle: string): Refusal | { pos: numb
|
|
|
103
106
|
"gone",
|
|
104
107
|
`Block ${handle} is no longer in the document. Read again for the blocks there are.`,
|
|
105
108
|
)
|
|
106
|
-
: refuse(
|
|
109
|
+
: refuse(
|
|
110
|
+
"unknown-handle",
|
|
111
|
+
`There is no block ${handle}. Handles come from read: [b1], [b2]… (headers' [h1] and footers' [f1] from read with headers).`,
|
|
112
|
+
);
|
|
107
113
|
}
|
|
108
114
|
|
|
109
115
|
/** @internal The blocks with these handles as they read now, in document order. */
|
|
@@ -135,7 +141,8 @@ export function styleId(context: Context, name: string): string | Refusal {
|
|
|
135
141
|
}
|
|
136
142
|
|
|
137
143
|
/** The content controls `node` at `pos` is in or holds that may not be edited, by name. */
|
|
138
|
-
|
|
144
|
+
/** @internal The locked content controls a block is in or holds, by name. */
|
|
145
|
+
export function lockedControls(context: Context, pos: number, node: Node): string[] {
|
|
139
146
|
const { state } = context.editor;
|
|
140
147
|
const ids = new Set<string>((node.attrs.controls as string[] | null) ?? []);
|
|
141
148
|
const end = pos + node.nodeSize;
|
|
@@ -173,7 +180,7 @@ export function replaceText(
|
|
|
173
180
|
const from = target.pos;
|
|
174
181
|
const to = target.pos + target.node.nodeSize;
|
|
175
182
|
const matches = findAll(editor, query, { matchCase: true }).filter(
|
|
176
|
-
(m) => m.story === null && m.from >= from && m.to <= to,
|
|
183
|
+
(m) => m.story === (context.story ?? null) && m.from >= from && m.to <= to,
|
|
177
184
|
);
|
|
178
185
|
if (!matches.length) {
|
|
179
186
|
const now = changed(context, [handle]).blocks[0]?.text ?? "";
|
|
@@ -224,8 +231,8 @@ export function replaceText(
|
|
|
224
231
|
return { ok: true, replaced, refused, ...result };
|
|
225
232
|
}
|
|
226
233
|
|
|
227
|
-
/**
|
|
228
|
-
function inline(editor: Editor, text: string): Node[] {
|
|
234
|
+
/** @internal Text as a model writes it, as inline nodes: ↵ a line break, a tab a tab. */
|
|
235
|
+
export function inline(editor: Editor, text: string): Node[] {
|
|
229
236
|
const { schema } = editor.state;
|
|
230
237
|
const nodes: Node[] = [];
|
|
231
238
|
for (const part of typed(text).split(/(\n|\t)/)) {
|
|
@@ -389,7 +396,10 @@ export function deleteBlock(context: Context, handle: string): Result<{ text: st
|
|
|
389
396
|
editor.run(deleteTable);
|
|
390
397
|
if (editor.state.doc === before || changed(context, [handle]).blocks.length)
|
|
391
398
|
return refuse("refused", `Table ${handle} was not deleted: the document refused the change.`);
|
|
392
|
-
return {
|
|
399
|
+
return {
|
|
400
|
+
ok: true,
|
|
401
|
+
text: `Deleted table ${handle} with its cells${howDeleted(editor.state.doc, before, pos)}.`,
|
|
402
|
+
};
|
|
393
403
|
}
|
|
394
404
|
// Word's rules: a cell and the body end with a paragraph, two tables never touch, and a
|
|
395
405
|
// paragraph mark that cannot join a paragraph after it stays when its deletion is accepted.
|
|
@@ -410,7 +420,8 @@ export function deleteBlock(context: Context, handle: string): Result<{ text: st
|
|
|
410
420
|
) {
|
|
411
421
|
// The session's own paragraph, never accepted: it goes for real, as its text would.
|
|
412
422
|
editor.dispatch(editor.state.tr.delete(pos, pos + node.nodeSize).setMeta(untracked, true));
|
|
413
|
-
if (editor.state.doc !== before)
|
|
423
|
+
if (editor.state.doc !== before)
|
|
424
|
+
return { ok: true, text: `Deleted block ${handle}: your own insertion, removed.` };
|
|
414
425
|
}
|
|
415
426
|
if (stays && !hasContent(node))
|
|
416
427
|
return refuse(
|
|
@@ -429,11 +440,23 @@ export function deleteBlock(context: Context, handle: string): Result<{ text: st
|
|
|
429
440
|
ok: true,
|
|
430
441
|
text: `Block ${handle} was emptied but stays: ${stays ?? "its paragraph mark could not be deleted"}.\n${left.text}`,
|
|
431
442
|
};
|
|
432
|
-
return { ok: true, text: `Deleted block ${handle}.` };
|
|
443
|
+
return { ok: true, text: `Deleted block ${handle}${howDeleted(editor.state.doc, before, pos)}.` };
|
|
444
|
+
}
|
|
445
|
+
|
|
446
|
+
/**
|
|
447
|
+
* Whether what was at `pos` stays as a tracked deletion or went for real (one's own insertion):
|
|
448
|
+
* what `deleteBlock` says it did.
|
|
449
|
+
*/
|
|
450
|
+
function howDeleted(doc: Node, before: Node, pos: number): string {
|
|
451
|
+
const was = before.nodeAt(pos);
|
|
452
|
+
const now = doc.nodeAt(pos);
|
|
453
|
+
const stays = !!now && !!was && now.type === was.type && now.nodeSize >= was.nodeSize;
|
|
454
|
+
return stays ? ", as a tracked deletion" : ": your own insertion, removed";
|
|
433
455
|
}
|
|
434
456
|
|
|
435
457
|
/** @internal The paragraph `handle` names, or why an operation on a paragraph cannot go on. */
|
|
436
|
-
|
|
458
|
+
/** @internal The paragraph `handle` names, or why it is not one. */
|
|
459
|
+
export function paragraphAt(
|
|
437
460
|
context: Context,
|
|
438
461
|
handle: string,
|
|
439
462
|
what: string,
|
|
@@ -446,6 +469,41 @@ function paragraphAt(
|
|
|
446
469
|
);
|
|
447
470
|
}
|
|
448
471
|
|
|
472
|
+
/**
|
|
473
|
+
* @internal
|
|
474
|
+
* Where text `on` is in a paragraph (`on` is text as read shows it; without it, the whole
|
|
475
|
+
* paragraph): once, or with `all` every place; refused when it is not there, or is there more
|
|
476
|
+
* than once without `all`.
|
|
477
|
+
*/
|
|
478
|
+
export function placesIn(
|
|
479
|
+
context: Context,
|
|
480
|
+
handle: string,
|
|
481
|
+
target: { pos: number; node: Node },
|
|
482
|
+
on: unknown,
|
|
483
|
+
all: boolean,
|
|
484
|
+
): Refusal | { from: number; to: number }[] {
|
|
485
|
+
const from = target.pos + 1;
|
|
486
|
+
const to = target.pos + target.node.nodeSize - 1;
|
|
487
|
+
if (on === undefined) return [{ from, to }];
|
|
488
|
+
const query = typeof on === "string" ? typed(on) : "";
|
|
489
|
+
if (!query) return refuse("invalid", "on must be text of the block, as read shows it.");
|
|
490
|
+
const matches = findAll(context.editor, query, { matchCase: true }).filter(
|
|
491
|
+
(m) => m.story === (context.story ?? null) && m.from >= from && m.to <= to,
|
|
492
|
+
);
|
|
493
|
+
if (matches.length === 1 || (all && matches.length))
|
|
494
|
+
return matches.map((m) => ({ from: m.from, to: m.to }));
|
|
495
|
+
const now = changed(context, [handle]).blocks[0]?.text ?? "";
|
|
496
|
+
return matches.length
|
|
497
|
+
? refuse(
|
|
498
|
+
"several-matches",
|
|
499
|
+
`${quote(query)} is in block ${handle} ${matches.length} times. Give a longer text that is there once${all ? "" : ", or pass all"}.`,
|
|
500
|
+
)
|
|
501
|
+
: refuse(
|
|
502
|
+
"no-match",
|
|
503
|
+
`${quote(query)} is not in block ${handle}. Capitals and spaces count. Its text is: ${quote(now, 2000)}`,
|
|
504
|
+
);
|
|
505
|
+
}
|
|
506
|
+
|
|
449
507
|
/** @internal The paragraph styles there are to name, as `read` names them. */
|
|
450
508
|
export function styles(context: Context): string[] {
|
|
451
509
|
return paragraphStyles(context.editor.state).map((style) => oneLine(style.name));
|
|
@@ -495,30 +553,9 @@ export function comment(
|
|
|
495
553
|
if ("ok" in target) return target;
|
|
496
554
|
const content = typeof text === "string" ? typed(text).trim() : "";
|
|
497
555
|
if (!content) return refuse("invalid", "A comment needs its text, a string.");
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
const
|
|
501
|
-
if (on !== undefined) {
|
|
502
|
-
const query = typeof on === "string" ? typed(on) : "";
|
|
503
|
-
if (!query) return refuse("invalid", "on must be text of the block, as read shows it.");
|
|
504
|
-
const matches = findAll(editor, query, { matchCase: true }).filter(
|
|
505
|
-
(m) => m.story === null && m.from >= from && m.to <= to,
|
|
506
|
-
);
|
|
507
|
-
const match = matches[0];
|
|
508
|
-
if (!match || matches.length > 1) {
|
|
509
|
-
const now = changed(context, [handle]).blocks[0]?.text ?? "";
|
|
510
|
-
return match
|
|
511
|
-
? refuse(
|
|
512
|
-
"several-matches",
|
|
513
|
-
`${quote(query)} is in block ${handle} ${matches.length} times. Give a longer text that is there once.`,
|
|
514
|
-
)
|
|
515
|
-
: refuse(
|
|
516
|
-
"no-match",
|
|
517
|
-
`${quote(query)} is not in block ${handle}. Capitals and spaces count. Its text is: ${quote(now, 2000)}`,
|
|
518
|
-
);
|
|
519
|
-
}
|
|
520
|
-
({ from, to } = match);
|
|
521
|
-
}
|
|
556
|
+
const places = placesIn(context, handle, target, options?.on, false);
|
|
557
|
+
if (!Array.isArray(places)) return places;
|
|
558
|
+
const { from, to } = places[0] as { from: number; to: number };
|
|
522
559
|
const ids = () => new Set(wordContext(editor.state)?.envelope.comments.map((c) => c.id));
|
|
523
560
|
const before = ids();
|
|
524
561
|
editor.select(from, to);
|