@folijs/core 0.5.0 → 1.0.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 +187 -0
- package/README.md +1 -1
- package/dist/document.d.ts +0 -7
- package/dist/document.d.ts.map +1 -1
- package/dist/document.js +5 -1
- package/dist/document.js.map +1 -1
- package/dist/index.d.ts +4 -4
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +8 -4
- package/dist/index.js.map +1 -1
- package/dist/internal.d.ts +5 -0
- package/dist/internal.d.ts.map +1 -0
- package/dist/internal.js +7 -0
- package/dist/internal.js.map +1 -0
- package/dist/model/attrs.d.ts +0 -10
- package/dist/model/attrs.d.ts.map +1 -1
- package/dist/model/attrs.js +10 -2
- package/dist/model/attrs.js.map +1 -1
- package/dist/model/content.d.ts +1 -1
- package/dist/model/content.d.ts.map +1 -1
- package/dist/model/content.js +1 -1
- package/dist/model/content.js.map +1 -1
- package/dist/model/errors.d.ts +0 -2
- package/dist/model/errors.d.ts.map +1 -1
- package/dist/model/errors.js +4 -1
- package/dist/model/errors.js.map +1 -1
- package/dist/model/fragment.d.ts +1 -1
- package/dist/model/fragment.d.ts.map +1 -1
- package/dist/model/fragment.js +3 -1
- package/dist/model/fragment.js.map +1 -1
- package/dist/model/mark.d.ts +1 -1
- package/dist/model/mark.d.ts.map +1 -1
- package/dist/model/mark.js +1 -1
- package/dist/model/mark.js.map +1 -1
- package/dist/model/node.d.ts +4 -4
- package/dist/model/node.d.ts.map +1 -1
- package/dist/model/node.js +8 -4
- package/dist/model/node.js.map +1 -1
- package/dist/model/schema.d.ts +8 -8
- package/dist/model/schema.d.ts.map +1 -1
- package/dist/model/schema.js +4 -3
- package/dist/model/schema.js.map +1 -1
- package/dist/schema.d.ts +0 -11
- package/dist/schema.d.ts.map +1 -1
- package/dist/schema.js +2 -0
- package/dist/schema.js.map +1 -1
- package/dist/state/attempt.d.ts +1 -3
- package/dist/state/attempt.d.ts.map +1 -1
- package/dist/state/commands.d.ts +2 -0
- package/dist/state/commands.d.ts.map +1 -1
- package/dist/state/commands.js +38 -6
- package/dist/state/commands.js.map +1 -1
- package/dist/state/comments.d.ts +1 -7
- package/dist/state/comments.d.ts.map +1 -1
- package/dist/state/comments.js +13 -3
- package/dist/state/comments.js.map +1 -1
- package/dist/state/controls.d.ts +110 -0
- package/dist/state/controls.d.ts.map +1 -0
- package/dist/state/controls.js +1473 -0
- package/dist/state/controls.js.map +1 -0
- package/dist/state/editor.d.ts +188 -68
- package/dist/state/editor.d.ts.map +1 -1
- package/dist/state/editor.js +265 -79
- package/dist/state/editor.js.map +1 -1
- package/dist/state/fields.d.ts +68 -0
- package/dist/state/fields.d.ts.map +1 -0
- package/dist/state/fields.js +410 -0
- package/dist/state/fields.js.map +1 -0
- package/dist/state/fieldupdate.d.ts +132 -0
- package/dist/state/fieldupdate.d.ts.map +1 -0
- package/dist/state/fieldupdate.js +1575 -0
- package/dist/state/fieldupdate.js.map +1 -0
- package/dist/state/find.d.ts +66 -0
- package/dist/state/find.d.ts.map +1 -0
- package/dist/state/find.js +405 -0
- package/dist/state/find.js.map +1 -0
- package/dist/state/history.d.ts +2 -0
- package/dist/state/history.d.ts.map +1 -1
- package/dist/state/history.js +44 -7
- package/dist/state/history.js.map +1 -1
- package/dist/state/index.d.ts +13 -6
- package/dist/state/index.d.ts.map +1 -1
- package/dist/state/index.js +12 -5
- package/dist/state/index.js.map +1 -1
- package/dist/state/lists.d.ts +3 -3
- package/dist/state/lists.d.ts.map +1 -1
- package/dist/state/lists.js.map +1 -1
- package/dist/state/mark.d.ts +1 -17
- package/dist/state/mark.d.ts.map +1 -1
- package/dist/state/mark.js +10 -1
- package/dist/state/mark.js.map +1 -1
- package/dist/state/navigation.d.ts +70 -0
- package/dist/state/navigation.d.ts.map +1 -0
- package/dist/state/navigation.js +226 -0
- package/dist/state/navigation.js.map +1 -0
- package/dist/state/notes.d.ts +84 -0
- package/dist/state/notes.d.ts.map +1 -0
- package/dist/state/notes.js +451 -0
- package/dist/state/notes.js.map +1 -0
- package/dist/state/review.d.ts +8 -4
- package/dist/state/review.d.ts.map +1 -1
- package/dist/state/review.js +134 -18
- package/dist/state/review.js.map +1 -1
- package/dist/state/sections.d.ts +91 -0
- package/dist/state/sections.d.ts.map +1 -0
- package/dist/state/sections.js +479 -0
- package/dist/state/sections.js.map +1 -0
- package/dist/state/state.d.ts +13 -7
- package/dist/state/state.d.ts.map +1 -1
- package/dist/state/state.js +9 -2
- package/dist/state/state.js.map +1 -1
- package/dist/state/stories.d.ts +2 -0
- package/dist/state/stories.d.ts.map +1 -0
- package/dist/state/stories.js +259 -0
- package/dist/state/stories.js.map +1 -0
- package/dist/state/tables.d.ts +1 -36
- package/dist/state/tables.d.ts.map +1 -1
- package/dist/state/tables.js +25 -6
- package/dist/state/tables.js.map +1 -1
- package/dist/state/track.d.ts +1 -6
- package/dist/state/track.d.ts.map +1 -1
- package/dist/state/track.js +65 -9
- package/dist/state/track.js.map +1 -1
- package/dist/state/word.d.ts +47 -55
- package/dist/state/word.d.ts.map +1 -1
- package/dist/state/word.js +306 -19
- package/dist/state/word.js.map +1 -1
- package/dist/state/wordclip.d.ts +17 -6
- package/dist/state/wordclip.d.ts.map +1 -1
- package/dist/state/wordclip.js +254 -17
- package/dist/state/wordclip.js.map +1 -1
- package/dist/state/wordformat.d.ts +1 -86
- package/dist/state/wordformat.d.ts.map +1 -1
- package/dist/state/wordformat.js +114 -12
- package/dist/state/wordformat.js.map +1 -1
- package/dist/transform/stepmap.d.ts +0 -2
- package/dist/transform/stepmap.d.ts.map +1 -1
- package/dist/v3/check.d.ts +0 -25
- package/dist/v3/check.d.ts.map +1 -1
- package/dist/v3/check.js +35 -7
- package/dist/v3/check.js.map +1 -1
- package/dist/v3/document.d.ts +135 -10
- package/dist/v3/document.d.ts.map +1 -1
- package/dist/v3/document.js +192 -38
- package/dist/v3/document.js.map +1 -1
- package/dist/v3/extensions.d.ts +43 -0
- package/dist/v3/extensions.d.ts.map +1 -0
- package/dist/v3/extensions.js +154 -0
- package/dist/v3/extensions.js.map +1 -0
- package/dist/v3/index.d.ts +3 -2
- package/dist/v3/index.d.ts.map +1 -1
- package/dist/v3/index.js +3 -2
- package/dist/v3/index.js.map +1 -1
- package/dist/v3/resolve.d.ts +9 -3
- package/dist/v3/resolve.d.ts.map +1 -1
- package/dist/v3/resolve.js +28 -4
- package/dist/v3/resolve.js.map +1 -1
- package/dist/v3/schema.d.ts +5 -16
- package/dist/v3/schema.d.ts.map +1 -1
- package/dist/v3/schema.js +167 -7
- package/dist/v3/schema.js.map +1 -1
- package/package.json +5 -1
- package/src/document.ts +5 -1
- package/src/index.ts +330 -4
- package/src/internal.ts +50 -0
- package/src/model/attrs.ts +10 -2
- package/src/model/content.ts +1 -1
- package/src/model/errors.ts +4 -1
- package/src/model/fragment.ts +2 -1
- package/src/model/mark.ts +1 -1
- package/src/model/node.ts +6 -4
- package/src/model/schema.ts +9 -3
- package/src/schema.ts +2 -0
- package/src/state/commands.ts +41 -4
- package/src/state/comments.ts +12 -3
- package/src/state/controls.ts +1612 -0
- package/src/state/editor.ts +508 -143
- package/src/state/fields.ts +444 -0
- package/src/state/fieldupdate.ts +1771 -0
- package/src/state/find.ts +518 -0
- package/src/state/history.ts +53 -7
- package/src/state/index.ts +125 -1
- package/src/state/lists.ts +2 -2
- package/src/state/mark.ts +10 -1
- package/src/state/navigation.ts +305 -0
- package/src/state/notes.ts +529 -0
- package/src/state/numeric-pictures.word.json +172 -0
- package/src/state/review.ts +172 -19
- package/src/state/sections.ts +578 -0
- package/src/state/state.ts +21 -3
- package/src/state/stories.ts +312 -0
- package/src/state/tables.ts +29 -7
- package/src/state/track.ts +72 -10
- package/src/state/word.ts +360 -24
- package/src/state/wordclip.ts +282 -32
- package/src/state/wordformat.ts +116 -12
- package/src/v3/check.ts +35 -7
- package/src/v3/document.ts +358 -50
- package/src/v3/extensions.ts +201 -0
- package/src/v3/index.ts +16 -0
- package/src/v3/resolve.ts +38 -9
- package/src/v3/schema.ts +174 -7
|
@@ -0,0 +1,518 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Find and replace over the body and every story (ADR-0007, 11.11), with the rules Word 16 was seen
|
|
3
|
+
* to follow through its object model: a match runs across runs, marks and whatever draws nothing
|
|
4
|
+
* (field markers, bookmarks, comment anchors) but not across a note reference or a picture; a
|
|
5
|
+
* straight quote finds curly ones and a space a no-break space, not the other way round; "whole
|
|
6
|
+
* words" takes letters and digits as word characters (`_` and `'` end a word) and is ignored when
|
|
7
|
+
* the query has a space; the replacement takes the formatting of the match's first character and,
|
|
8
|
+
* unless the case must match, follows its capitals.
|
|
9
|
+
*
|
|
10
|
+
* Regular expressions are left out: a user's pattern can backtrack for ever on the main thread, and
|
|
11
|
+
* Word's own wildcards are another language.
|
|
12
|
+
*/
|
|
13
|
+
import { Fragment, type Node, Slice } from "../model/index.js";
|
|
14
|
+
import type { ControlJSON } from "../v3/document.js";
|
|
15
|
+
import { controlAt, controlsIn, readOnlyAt } from "./controls.js";
|
|
16
|
+
import { createEditor, type Editor, type EditorDocument } from "./editor.js";
|
|
17
|
+
import { fieldAround, fieldCodesShown, fieldsIn, isUnseen } from "./fields.js";
|
|
18
|
+
import { inlineText, storyIdOf, withoutReview } from "./fieldupdate.js";
|
|
19
|
+
import { TextSelection } from "./selection.js";
|
|
20
|
+
import type { EditorState, Transaction } from "./state.js";
|
|
21
|
+
import { editTrees } from "./stories.js";
|
|
22
|
+
import { trackState, trackTransaction } from "./track.js";
|
|
23
|
+
import { storyBody, wordContext } from "./word.js";
|
|
24
|
+
|
|
25
|
+
export interface FindOptions {
|
|
26
|
+
/** Capitals count. Default false. */
|
|
27
|
+
matchCase?: boolean | undefined;
|
|
28
|
+
/** Only whole words (ignored when the query has a space, as in Word). Default false. */
|
|
29
|
+
wholeWord?: boolean | undefined;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/** A place the query was found. Positions are those of its tree. */
|
|
33
|
+
export interface FindMatch {
|
|
34
|
+
/** The story it is in; null: the body. */
|
|
35
|
+
story: string | null;
|
|
36
|
+
from: number;
|
|
37
|
+
to: number;
|
|
38
|
+
/** The text found, as the document has it. */
|
|
39
|
+
text: string;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/** Why a match was left as it is. */
|
|
43
|
+
export type ReplaceRefusal =
|
|
44
|
+
/** In a content control that is locked or filled from bound data, or in a custom node. */
|
|
45
|
+
| "locked"
|
|
46
|
+
/** A control's kind does not take it, or the match runs over a control's edge. */
|
|
47
|
+
| "control"
|
|
48
|
+
/** The match runs over the edge of a field's result. */
|
|
49
|
+
| "field"
|
|
50
|
+
/** In a field's code, which shows only while codes are shown. */
|
|
51
|
+
| "field-code"
|
|
52
|
+
/** In text already deleted as a tracked change. */
|
|
53
|
+
| "deleted"
|
|
54
|
+
/** The document no longer has this text there. */
|
|
55
|
+
| "stale"
|
|
56
|
+
/** It could not be made or recorded as a tracked change. */
|
|
57
|
+
| "refused";
|
|
58
|
+
|
|
59
|
+
export interface ReplaceReport {
|
|
60
|
+
replaced: number;
|
|
61
|
+
refused: { match: FindMatch; reason: ReplaceRefusal }[];
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
// ---------- matching text ----------
|
|
65
|
+
|
|
66
|
+
/** What stands for something that draws but is not text: nothing matches it. */
|
|
67
|
+
const BARRIER = "";
|
|
68
|
+
/** What a plain character in a query also finds. */
|
|
69
|
+
const LOOSE: Readonly<Record<string, string>> = {
|
|
70
|
+
"‘": "'",
|
|
71
|
+
"’": "'",
|
|
72
|
+
"“": '"',
|
|
73
|
+
"”": '"',
|
|
74
|
+
" ": " ",
|
|
75
|
+
};
|
|
76
|
+
const LOOSE_CHARS = /[‘’“” ]/g;
|
|
77
|
+
const loosen = (text: string) => text.replace(LOOSE_CHARS, (c) => LOOSE[c] as string);
|
|
78
|
+
/** Letters, digits and what combines with them. */
|
|
79
|
+
const WORD_CHAR = /^[\p{L}\p{N}\p{M}]/u;
|
|
80
|
+
|
|
81
|
+
/** Lower case, unit for unit, so offsets stay the text's. */
|
|
82
|
+
function fold(text: string): string {
|
|
83
|
+
let out = text.toLowerCase();
|
|
84
|
+
if (out.length !== text.length) {
|
|
85
|
+
// By code point, so a capital outside the BMP still folds; one that grows stays as it is.
|
|
86
|
+
out = "";
|
|
87
|
+
for (const char of text) {
|
|
88
|
+
const lower = char.toLowerCase();
|
|
89
|
+
out += lower.length === char.length ? lower : char;
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
return out.replace(/ς/g, "σ"); // a final sigma is a sigma
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
interface Needle {
|
|
96
|
+
query: string;
|
|
97
|
+
text: string;
|
|
98
|
+
matchCase: boolean;
|
|
99
|
+
whole: boolean;
|
|
100
|
+
/** Offsets of the characters that find only themselves. */
|
|
101
|
+
exact: number[];
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
function needleFor(query: string, { matchCase = false, wholeWord = false }: FindOptions) {
|
|
105
|
+
if (!query || query.includes(BARRIER)) return null;
|
|
106
|
+
const plain = loosen(query);
|
|
107
|
+
const exact: number[] = [];
|
|
108
|
+
for (let i = 0; i < query.length; i++) if (query[i] !== plain[i]) exact.push(i);
|
|
109
|
+
const whole = wholeWord && !/\s/.test(query);
|
|
110
|
+
return { query, text: matchCase ? plain : fold(plain), matchCase, whole, exact } as Needle;
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
const charAfter = (text: string, at: number) =>
|
|
114
|
+
String.fromCodePoint(text.codePointAt(at) as number);
|
|
115
|
+
function charBefore(text: string, at: number): string {
|
|
116
|
+
const unit = text.charCodeAt(at - 1);
|
|
117
|
+
return unit >= 0xdc00 && unit <= 0xdfff && at > 1 ? text.slice(at - 2, at) : (text[at - 1] ?? "");
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
/** Whether no word goes on past either end of `from..to`. */
|
|
121
|
+
function isWhole(text: string, from: number, to: number): boolean {
|
|
122
|
+
if (from > 0 && WORD_CHAR.test(charAfter(text, from)) && WORD_CHAR.test(charBefore(text, from)))
|
|
123
|
+
return false;
|
|
124
|
+
return !(
|
|
125
|
+
to < text.length &&
|
|
126
|
+
WORD_CHAR.test(charBefore(text, to)) &&
|
|
127
|
+
WORD_CHAR.test(charAfter(text, to))
|
|
128
|
+
);
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
function eachHit(
|
|
132
|
+
raw: string,
|
|
133
|
+
haystack: string,
|
|
134
|
+
needle: Needle,
|
|
135
|
+
hit: (from: number, to: number) => void,
|
|
136
|
+
): void {
|
|
137
|
+
const { text, query, exact, whole } = needle;
|
|
138
|
+
let at = haystack.indexOf(text);
|
|
139
|
+
while (at >= 0) {
|
|
140
|
+
const end = at + text.length;
|
|
141
|
+
if (exact.every((i) => raw[at + i] === query[i]) && (!whole || isWhole(raw, at, end))) {
|
|
142
|
+
hit(at, end);
|
|
143
|
+
at = haystack.indexOf(text, end);
|
|
144
|
+
} else at = haystack.indexOf(text, at + 1);
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
/** Where `query` is in a plain text, by Word's rules: `[from, to]` pairs, none overlapping. */
|
|
149
|
+
export function findInText(
|
|
150
|
+
text: string,
|
|
151
|
+
query: string,
|
|
152
|
+
options: FindOptions = {},
|
|
153
|
+
): [number, number][] {
|
|
154
|
+
const needle = needleFor(query, options);
|
|
155
|
+
if (!needle) return [];
|
|
156
|
+
const out: [number, number][] = [];
|
|
157
|
+
const plain = loosen(text);
|
|
158
|
+
eachHit(text, needle.matchCase ? plain : fold(plain), needle, (from, to) => {
|
|
159
|
+
out.push([from, to]);
|
|
160
|
+
});
|
|
161
|
+
return out;
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
// ---------- what can be searched ----------
|
|
165
|
+
|
|
166
|
+
/** A stretch of a line's text and where it is in the tree. */
|
|
167
|
+
interface Piece {
|
|
168
|
+
at: number;
|
|
169
|
+
length: number;
|
|
170
|
+
pos: number;
|
|
171
|
+
/** A field code or a custom node: one node, however long its text. */
|
|
172
|
+
code: boolean;
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
/** A paragraph as find sees it. */
|
|
176
|
+
interface Line {
|
|
177
|
+
text: string;
|
|
178
|
+
pieces: Piece[];
|
|
179
|
+
plain?: string;
|
|
180
|
+
folded?: string;
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
/** What shows, which decides what can be found. */
|
|
184
|
+
interface Scope {
|
|
185
|
+
codes: boolean;
|
|
186
|
+
hideDeleted: boolean;
|
|
187
|
+
controls: Readonly<Record<string, ControlJSON>> | undefined;
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
interface Hidden {
|
|
191
|
+
from: number;
|
|
192
|
+
to: number;
|
|
193
|
+
/** It shows as something else: a match cannot run across it. */
|
|
194
|
+
barrier: boolean;
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
const PAGE_FIELDS = new Set(["PAGE", "NUMPAGES", "SECTIONPAGES"]);
|
|
198
|
+
const hasMark = (node: Node, name: string) =>
|
|
199
|
+
node.marks.some((m) => m.type.name === name && m.attrs.value !== false);
|
|
200
|
+
|
|
201
|
+
/** What of `tree` is not on the page, in order, overlaps joined. */
|
|
202
|
+
function hiddenRanges(tree: Node, scope: Scope, story: boolean): Hidden[] {
|
|
203
|
+
const ranges: Hidden[] = [];
|
|
204
|
+
for (const field of fieldsIn(tree)) {
|
|
205
|
+
if (scope.codes) {
|
|
206
|
+
if (field.separator !== null)
|
|
207
|
+
ranges.push({ from: field.separator + 1, to: field.resultTo, barrier: false });
|
|
208
|
+
continue;
|
|
209
|
+
}
|
|
210
|
+
ranges.push({ from: field.from, to: field.codeEnd, barrier: false });
|
|
211
|
+
// The view draws these page by page: what the tree stores is not what shows.
|
|
212
|
+
if (story && PAGE_FIELDS.has(field.name))
|
|
213
|
+
ranges.push({ from: field.resultFrom, to: field.resultTo, barrier: true });
|
|
214
|
+
}
|
|
215
|
+
const { controls } = scope;
|
|
216
|
+
if (controls)
|
|
217
|
+
for (const range of controlsIn(tree))
|
|
218
|
+
if (Object.hasOwn(controls, range.id) && controls[range.id]?.placeholder?.showing)
|
|
219
|
+
ranges.push({ from: range.contentFrom, to: range.contentTo, barrier: true });
|
|
220
|
+
ranges.sort((a, b) => a.from - b.from);
|
|
221
|
+
const out: Hidden[] = [];
|
|
222
|
+
for (const range of ranges) {
|
|
223
|
+
const last = out[out.length - 1];
|
|
224
|
+
if (last && range.from <= last.to) {
|
|
225
|
+
last.to = Math.max(last.to, range.to);
|
|
226
|
+
last.barrier ||= range.barrier;
|
|
227
|
+
} else out.push({ ...range });
|
|
228
|
+
}
|
|
229
|
+
return out;
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
const known = new WeakMap<Node, { key: string; scope: Scope; lines: Line[] }>();
|
|
233
|
+
|
|
234
|
+
/** The lines of `tree`, kept while the tree and what shows stay the same (typing in the panel). */
|
|
235
|
+
function linesOf(tree: Node, scope: Scope, story: boolean): Line[] {
|
|
236
|
+
const key = `${scope.codes}|${scope.hideDeleted}`;
|
|
237
|
+
const cached = known.get(tree);
|
|
238
|
+
if (cached?.key === key && cached.scope.controls === scope.controls) return cached.lines;
|
|
239
|
+
const hidden = hiddenRanges(tree, scope, story);
|
|
240
|
+
const lines: Line[] = [];
|
|
241
|
+
let h = 0;
|
|
242
|
+
tree.descendants((node, pos) => {
|
|
243
|
+
if (!node.isTextblock) return true;
|
|
244
|
+
const pieces: Piece[] = [];
|
|
245
|
+
let text = "";
|
|
246
|
+
let walled = -1;
|
|
247
|
+
const add = (value: string, at: number, code = false) => {
|
|
248
|
+
pieces.push({ at: text.length, length: value.length, pos: at, code });
|
|
249
|
+
text += value;
|
|
250
|
+
};
|
|
251
|
+
node.forEach((child, offset) => {
|
|
252
|
+
const at = pos + 1 + offset;
|
|
253
|
+
while (h < hidden.length && (hidden[h] as Hidden).to <= at) h++;
|
|
254
|
+
const range = hidden[h];
|
|
255
|
+
if (range && range.from <= at) {
|
|
256
|
+
if (range.barrier && walled !== h) {
|
|
257
|
+
walled = h;
|
|
258
|
+
text += BARRIER;
|
|
259
|
+
}
|
|
260
|
+
return;
|
|
261
|
+
}
|
|
262
|
+
if (hasMark(child, "hidden") || (scope.hideDeleted && hasMark(child, "deletion"))) return;
|
|
263
|
+
const name = child.type.name;
|
|
264
|
+
if (child.isText) add(child.text as string, at);
|
|
265
|
+
else if (name === "tab") add("\t", at);
|
|
266
|
+
else if (name === "hardBreak") add("\n", at);
|
|
267
|
+
else if (name === "fieldCode" && scope.codes) add(String(child.attrs.text ?? ""), at, true);
|
|
268
|
+
// A custom or foreign node (ADR-0008): found by its text, one position, never replaced.
|
|
269
|
+
else if (child.type.spec.text) add(child.type.spec.text(child.attrs), at, true);
|
|
270
|
+
else if (!isUnseen(child)) text += BARRIER;
|
|
271
|
+
});
|
|
272
|
+
if (text) lines.push({ text, pieces });
|
|
273
|
+
return false;
|
|
274
|
+
});
|
|
275
|
+
known.set(tree, { key, scope, lines });
|
|
276
|
+
return lines;
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
/** A found place with the stretches of the tree it covers. */
|
|
280
|
+
interface Hit {
|
|
281
|
+
match: FindMatch;
|
|
282
|
+
parts: { from: number; to: number; code: boolean }[];
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
function hitsIn(lines: readonly Line[], story: string | null, needle: Needle, out: Hit[]): void {
|
|
286
|
+
for (const line of lines) {
|
|
287
|
+
line.plain ??= loosen(line.text);
|
|
288
|
+
if (!needle.matchCase) line.folded ??= fold(line.plain);
|
|
289
|
+
const haystack = needle.matchCase ? line.plain : (line.folded as string);
|
|
290
|
+
if (haystack.length < needle.text.length) continue;
|
|
291
|
+
eachHit(line.text, haystack, needle, (a, b) => {
|
|
292
|
+
const parts: Hit["parts"] = [];
|
|
293
|
+
for (const { at, length, pos, code } of line.pieces) {
|
|
294
|
+
const start = Math.max(a, at);
|
|
295
|
+
const end = Math.min(b, at + length);
|
|
296
|
+
if (start >= end) continue;
|
|
297
|
+
if (code) parts.push({ from: pos, to: pos + 1, code });
|
|
298
|
+
else parts.push({ from: pos + start - at, to: pos + end - at, code });
|
|
299
|
+
}
|
|
300
|
+
const first = parts[0];
|
|
301
|
+
const last = parts[parts.length - 1];
|
|
302
|
+
if (!first || !last) return;
|
|
303
|
+
const match = { story, from: first.from, to: last.to, text: line.text.slice(a, b) };
|
|
304
|
+
out.push({ match, parts });
|
|
305
|
+
});
|
|
306
|
+
}
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
/** The body's state of `target`, whichever of its editors it is. */
|
|
310
|
+
const bodyState = (state: EditorState) => storyBody(state)?.state ?? state;
|
|
311
|
+
|
|
312
|
+
const scopeOf = (body: EditorState): Scope => {
|
|
313
|
+
const view = trackState(body)?.view ?? "markup";
|
|
314
|
+
return {
|
|
315
|
+
codes: fieldCodesShown(body),
|
|
316
|
+
hideDeleted: view !== "markup",
|
|
317
|
+
controls: wordContext(body)?.envelope.controls,
|
|
318
|
+
};
|
|
319
|
+
};
|
|
320
|
+
|
|
321
|
+
/** The trees searched: the body, then the stories that hold text (no note separators). */
|
|
322
|
+
function treesOf(body: EditorState): { id: string | null; tree: Node }[] {
|
|
323
|
+
const stories = wordContext(body)?.stories ?? new Map();
|
|
324
|
+
return [
|
|
325
|
+
{ id: null, tree: body.doc },
|
|
326
|
+
...[...stories]
|
|
327
|
+
.filter(([, story]) => !story.noteType)
|
|
328
|
+
.map(([id, story]) => ({ id: id as string, tree: story.doc as Node })),
|
|
329
|
+
];
|
|
330
|
+
}
|
|
331
|
+
|
|
332
|
+
function hitsOf(body: EditorState, query: string, options: FindOptions): Hit[] {
|
|
333
|
+
const needle = needleFor(query, options);
|
|
334
|
+
if (!needle) return [];
|
|
335
|
+
const scope = scopeOf(body);
|
|
336
|
+
const out: Hit[] = [];
|
|
337
|
+
for (const { id, tree } of treesOf(body))
|
|
338
|
+
hitsIn(linesOf(tree, scope, id !== null), id, needle, out);
|
|
339
|
+
return out;
|
|
340
|
+
}
|
|
341
|
+
|
|
342
|
+
/**
|
|
343
|
+
* Every place `query` is, in the body then in each story, as the document shows now: a field's
|
|
344
|
+
* code only while codes are shown (its result then not), text deleted as a tracked change only in
|
|
345
|
+
* the `markup` review view, no control's placeholder, no hidden text. A `document` is read as an
|
|
346
|
+
* editor would load it, with those at their defaults.
|
|
347
|
+
*/
|
|
348
|
+
export function findAll(
|
|
349
|
+
target: Editor | EditorDocument,
|
|
350
|
+
query: string,
|
|
351
|
+
options: FindOptions = {},
|
|
352
|
+
): FindMatch[] {
|
|
353
|
+
const editor = "getDocument" in target ? target : createEditor({ document: target });
|
|
354
|
+
return hitsOf(bodyState(editor.state), query, options).map((hit) => hit.match);
|
|
355
|
+
}
|
|
356
|
+
|
|
357
|
+
// ---------- replacing ----------
|
|
358
|
+
|
|
359
|
+
/** The replacement as Word writes it over `found`: its capitals followed. */
|
|
360
|
+
function withCaseOf(found: string, text: string): string {
|
|
361
|
+
const first = found[0] ?? "";
|
|
362
|
+
if (first === first.toLowerCase() || !text) return text;
|
|
363
|
+
if (found === found.toUpperCase()) return text.toUpperCase();
|
|
364
|
+
return (text[0] as string).toUpperCase() + text.slice(1);
|
|
365
|
+
}
|
|
366
|
+
|
|
367
|
+
/** Control characters other than tab and line ends cannot be in a Word file. */
|
|
368
|
+
// biome-ignore lint/suspicious/noControlCharactersInRegex: that is what it removes
|
|
369
|
+
const cleaned = (text: string) => text.replace(/[\u0000-\u0008\u000B\u000C\u000E-\u001F]/g, "");
|
|
370
|
+
|
|
371
|
+
/** Puts `text` where `hit` is: in its first stretch, with that stretch's marks. */
|
|
372
|
+
function applyHit(tr: Transaction, { parts }: Hit, text: string): number {
|
|
373
|
+
const first = parts[0] as Hit["parts"][number];
|
|
374
|
+
for (const part of parts.slice(1).reverse()) tr.delete(part.from, part.to);
|
|
375
|
+
const marks = withoutReview(tr.doc.nodeAt(first.from)?.marks ?? []);
|
|
376
|
+
const nodes = inlineText(tr.doc.type.schema, text, marks);
|
|
377
|
+
tr.replace(first.from, first.to, new Slice(Fragment.fromArray(nodes), 0, 0));
|
|
378
|
+
return first.from + nodes.reduce((size, node) => size + node.nodeSize, 0);
|
|
379
|
+
}
|
|
380
|
+
|
|
381
|
+
/** Why `hit` may not be replaced in `state` (its tree's), if so. */
|
|
382
|
+
function refusalOf(state: EditorState, scope: Scope, hit: Hit, text: string) {
|
|
383
|
+
const tree = state.doc;
|
|
384
|
+
const { parts, match } = hit;
|
|
385
|
+
if (parts.some((part) => part.code))
|
|
386
|
+
return parts.some((part) => part.code && tree.nodeAt(part.from)?.type.spec.text)
|
|
387
|
+
? "locked"
|
|
388
|
+
: "field-code";
|
|
389
|
+
if (parts.some((part) => tree.nodeAt(part.from)?.marks.some((m) => m.type.name === "deletion")))
|
|
390
|
+
return "deleted";
|
|
391
|
+
const first = parts[0] as Hit["parts"][number];
|
|
392
|
+
if (parts.length > 1) {
|
|
393
|
+
const field = fieldAround(tree, first.from);
|
|
394
|
+
if (parts.some((part) => fieldAround(tree, part.from) !== field)) return "field";
|
|
395
|
+
const control = controlAt(tree, first.from);
|
|
396
|
+
if (parts.some((part) => controlAt(tree, part.from) !== control)) return "control";
|
|
397
|
+
}
|
|
398
|
+
if (!controlAt(tree, first.from)) return null;
|
|
399
|
+
if (readOnlyAt(tree, scope.controls, match.from, match.to)) return "locked";
|
|
400
|
+
// What else a control takes is the filter's to say.
|
|
401
|
+
const tr = state.tr;
|
|
402
|
+
applyHit(tr, hit, text);
|
|
403
|
+
const made = trackTransaction(state, tr);
|
|
404
|
+
return made && state.applyTransaction(made).transactions.length ? null : "control";
|
|
405
|
+
}
|
|
406
|
+
|
|
407
|
+
/** Replaces `hits` tree by tree in one undo step; `after`: a caret after the single one. */
|
|
408
|
+
function replaceHits(
|
|
409
|
+
editor: Editor,
|
|
410
|
+
hits: readonly Hit[],
|
|
411
|
+
text: string,
|
|
412
|
+
matchCase: boolean,
|
|
413
|
+
caretAfter: boolean,
|
|
414
|
+
): ReplaceReport {
|
|
415
|
+
const body = bodyState(editor.state);
|
|
416
|
+
const scope = scopeOf(body);
|
|
417
|
+
const report: ReplaceReport = { replaced: 0, refused: [] };
|
|
418
|
+
const value = cleaned(text);
|
|
419
|
+
const textOf = (hit: Hit) => (matchCase ? value : withCaseOf(hit.match.text, value));
|
|
420
|
+
const byTree = new Map<string | null, Hit[]>();
|
|
421
|
+
for (const hit of hits) {
|
|
422
|
+
const { story } = hit.match;
|
|
423
|
+
const state = story === null ? body : editor.story(story)?.state;
|
|
424
|
+
const reason = state ? refusalOf(state, scope, hit, textOf(hit)) : "stale";
|
|
425
|
+
if (reason) report.refused.push({ match: hit.match, reason });
|
|
426
|
+
else byTree.set(story, [...(byTree.get(story) ?? []), hit]);
|
|
427
|
+
}
|
|
428
|
+
const edits = new Map<string | null, (tr: Transaction) => void>();
|
|
429
|
+
for (const [story, list] of byTree)
|
|
430
|
+
edits.set(story, (tr) => {
|
|
431
|
+
let caret = 0;
|
|
432
|
+
for (const hit of [...list].reverse()) caret = applyHit(tr, hit, textOf(hit));
|
|
433
|
+
if (caretAfter) tr.setSelection(TextSelection.create(tr.doc, caret));
|
|
434
|
+
});
|
|
435
|
+
const failed = edits.size ? editTrees(editor, edits, { tracked: true }) : new Set();
|
|
436
|
+
for (const [story, list] of byTree) {
|
|
437
|
+
if (failed.has(story))
|
|
438
|
+
report.refused.push(...list.map((hit) => ({ match: hit.match, reason: "refused" as const })));
|
|
439
|
+
else report.replaced += list.length;
|
|
440
|
+
}
|
|
441
|
+
return report;
|
|
442
|
+
}
|
|
443
|
+
|
|
444
|
+
/**
|
|
445
|
+
* Replaces one match found by `findAll`: tracked when tracking is on, in the formatting of the
|
|
446
|
+
* match's first character, its capitals followed unless `matchCase`. Anything between its pieces
|
|
447
|
+
* that draws nothing (a bookmark, a comment's anchor) stays. A line feed in `text` makes a line
|
|
448
|
+
* break, a tab a tab. The caret goes after it. One undo step.
|
|
449
|
+
*/
|
|
450
|
+
export function replace(
|
|
451
|
+
editor: Editor,
|
|
452
|
+
match: FindMatch,
|
|
453
|
+
text: string,
|
|
454
|
+
{ matchCase = false }: Pick<FindOptions, "matchCase"> = {},
|
|
455
|
+
): ReplaceReport {
|
|
456
|
+
const body = bodyState(editor.state);
|
|
457
|
+
const needle = needleFor(match.text, { matchCase: true }) as Needle | null;
|
|
458
|
+
const tree = match.story === null ? body.doc : wordContext(body)?.stories.get(match.story)?.doc;
|
|
459
|
+
const hits: Hit[] = [];
|
|
460
|
+
if (needle && tree)
|
|
461
|
+
hitsIn(linesOf(tree, scopeOf(body), match.story !== null), match.story, needle, hits);
|
|
462
|
+
const hit = hits.find((h) => h.match.from === match.from && h.match.to === match.to);
|
|
463
|
+
if (!hit) return { replaced: 0, refused: [{ match, reason: "stale" }] };
|
|
464
|
+
return replaceHits(editor, [hit], text, matchCase, true);
|
|
465
|
+
}
|
|
466
|
+
|
|
467
|
+
/**
|
|
468
|
+
* Replaces every match of `query`, in the body and every story, as one undo step. What may not be
|
|
469
|
+
* replaced stays and is in `refused`, with why.
|
|
470
|
+
*/
|
|
471
|
+
export function replaceAll(
|
|
472
|
+
editor: Editor,
|
|
473
|
+
query: string,
|
|
474
|
+
text: string,
|
|
475
|
+
options: FindOptions = {},
|
|
476
|
+
): ReplaceReport {
|
|
477
|
+
const hits = hitsOf(bodyState(editor.state), query, options);
|
|
478
|
+
return replaceHits(editor, hits, text, options.matchCase === true, false);
|
|
479
|
+
}
|
|
480
|
+
|
|
481
|
+
/**
|
|
482
|
+
* The story `editor` edits; null for the body's.
|
|
483
|
+
* @internal
|
|
484
|
+
*/
|
|
485
|
+
export function storyOfEditor(editor: Editor): string | null {
|
|
486
|
+
const body = storyBody(editor.state)?.state;
|
|
487
|
+
return body ? storyIdOf(body, editor.state.doc) : null;
|
|
488
|
+
}
|
|
489
|
+
|
|
490
|
+
/**
|
|
491
|
+
* The match to go to from the selection of the story open (or the body): the next, or with
|
|
492
|
+
* `backward` the previous, around the end of the document.
|
|
493
|
+
*/
|
|
494
|
+
export function nextMatch(
|
|
495
|
+
editor: Editor,
|
|
496
|
+
matches: readonly FindMatch[],
|
|
497
|
+
backward = false,
|
|
498
|
+
): FindMatch | null {
|
|
499
|
+
if (!matches.length) return null;
|
|
500
|
+
const open = editor.storyView?.open?.id ?? storyOfEditor(editor);
|
|
501
|
+
const body = bodyState(editor.state);
|
|
502
|
+
const order = new Map(treesOf(body).map(({ id }, i) => [id, i]));
|
|
503
|
+
const rank = (story: string | null) => order.get(story) ?? -1;
|
|
504
|
+
const state = (open && editor.story(open)?.state) || body;
|
|
505
|
+
const here = rank(open);
|
|
506
|
+
const { from, to } = state.selection;
|
|
507
|
+
const after = (m: FindMatch) => {
|
|
508
|
+
const tree = rank(m.story);
|
|
509
|
+
return tree > here || (tree === here && m.from >= to);
|
|
510
|
+
};
|
|
511
|
+
const before = (m: FindMatch) => {
|
|
512
|
+
const tree = rank(m.story);
|
|
513
|
+
return tree < here || (tree === here && m.to <= from);
|
|
514
|
+
};
|
|
515
|
+
if (backward)
|
|
516
|
+
return [...matches].reverse().find(before) ?? (matches[matches.length - 1] as FindMatch);
|
|
517
|
+
return matches.find(after) ?? (matches[0] as FindMatch);
|
|
518
|
+
}
|
package/src/state/history.ts
CHANGED
|
@@ -4,6 +4,7 @@ import { AddMarkStep, RemoveMarkStep, ReplaceStep, type Step } from "../transfor
|
|
|
4
4
|
import { TransformError } from "../transform/transform.js";
|
|
5
5
|
import { Selection, type SelectionJSON } from "./selection.js";
|
|
6
6
|
import { type Command, type EditorState, Plugin, PluginKey, type Transaction } from "./state.js";
|
|
7
|
+
import { StoryStep, storedStepsApply, storyBody, wordContext } from "./word.js";
|
|
7
8
|
|
|
8
9
|
/** One undoable unit: inverted steps in the order they apply, and the selection to restore. */
|
|
9
10
|
interface HistoryEvent {
|
|
@@ -20,6 +21,8 @@ interface HistoryState {
|
|
|
20
21
|
/** Time and changed ranges (in current-document positions) of the last recorded change. */
|
|
21
22
|
readonly prevTime: number;
|
|
22
23
|
readonly prevRanges: readonly number[] | null;
|
|
24
|
+
/** The story `prevRanges` are positions of (ADR-0007); none: the body. */
|
|
25
|
+
readonly prevStory?: string | null;
|
|
23
26
|
}
|
|
24
27
|
|
|
25
28
|
export interface HistoryOptions {
|
|
@@ -36,6 +39,21 @@ const historyOp = "historyOp";
|
|
|
36
39
|
/** Set this meta on a transaction to make it an undo step of its own (no merging either side). */
|
|
37
40
|
export const closeHistory = "closeHistory";
|
|
38
41
|
|
|
42
|
+
/** Set this meta on a transaction to make it part of the undo step before it. */
|
|
43
|
+
export const joinHistory = "joinHistory";
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* On a body transaction carrying a story's change: the story's id and its own transaction, which
|
|
47
|
+
* decides what typing joins, as the body's does for body typing.
|
|
48
|
+
* @internal
|
|
49
|
+
*/
|
|
50
|
+
export const storyEdit = "storyEdit";
|
|
51
|
+
/** @internal */
|
|
52
|
+
export interface StoryEdit {
|
|
53
|
+
id: string;
|
|
54
|
+
tr: Transaction;
|
|
55
|
+
}
|
|
56
|
+
|
|
39
57
|
const EMPTY: HistoryState = { done: [], undone: [], prevTime: 0, prevRanges: null };
|
|
40
58
|
|
|
41
59
|
/**
|
|
@@ -84,12 +102,15 @@ export function history({ newGroupDelay = 500, depth = 100 }: HistoryOptions = {
|
|
|
84
102
|
}
|
|
85
103
|
|
|
86
104
|
const last = hist.done[hist.done.length - 1];
|
|
105
|
+
const story = tr.getMeta(storyEdit) as StoryEdit | undefined;
|
|
87
106
|
const merge =
|
|
88
107
|
last !== undefined &&
|
|
89
108
|
(root !== undefined ||
|
|
109
|
+
tr.getMeta(joinHistory) === true ||
|
|
90
110
|
(!tr.getMeta(closeHistory) &&
|
|
91
111
|
tr.time - hist.prevTime < newGroupDelay &&
|
|
92
|
-
|
|
112
|
+
(hist.prevStory ?? null) === (story?.id ?? null) &&
|
|
113
|
+
isAdjacent(hist.prevRanges, story?.tr ?? tr)));
|
|
93
114
|
const done = merge
|
|
94
115
|
? [
|
|
95
116
|
...hist.done.slice(0, -1),
|
|
@@ -101,18 +122,26 @@ export function history({ newGroupDelay = 500, depth = 100 }: HistoryOptions = {
|
|
|
101
122
|
undone: [],
|
|
102
123
|
// A closed change is a step of its own on both sides: what follows does not join it.
|
|
103
124
|
prevTime: root ? hist.prevTime : tr.getMeta(closeHistory) ? 0 : tr.time,
|
|
104
|
-
prevRanges: root
|
|
125
|
+
prevRanges: root
|
|
126
|
+
? mapRanges(hist.prevRanges, tr.mapping)
|
|
127
|
+
: changedRanges(story?.tr ?? tr),
|
|
128
|
+
prevStory: root ? (hist.prevStory ?? null) : (story?.id ?? null),
|
|
105
129
|
};
|
|
106
130
|
},
|
|
107
131
|
},
|
|
108
132
|
});
|
|
109
133
|
}
|
|
110
134
|
|
|
111
|
-
/** Puts `inverted` (applied first) before `steps`, merging adjacent
|
|
135
|
+
/** Puts `inverted` (applied first) before `steps`, merging adjacent steps (typing runs). */
|
|
112
136
|
function prepend(inverted: readonly Step[], steps: readonly Step[]): Step[] {
|
|
113
137
|
const last = inverted[inverted.length - 1];
|
|
114
138
|
const first = steps[0];
|
|
115
|
-
const merged =
|
|
139
|
+
const merged =
|
|
140
|
+
last instanceof ReplaceStep && first
|
|
141
|
+
? last.merge(first)
|
|
142
|
+
: last instanceof StoryStep && first instanceof StoryStep
|
|
143
|
+
? last.merge(first)
|
|
144
|
+
: null;
|
|
116
145
|
return merged ? [...inverted.slice(0, -1), merged, ...steps.slice(1)] : [...inverted, ...steps];
|
|
117
146
|
}
|
|
118
147
|
|
|
@@ -261,13 +290,29 @@ function applyStored(tr: Transaction, step: Step): boolean {
|
|
|
261
290
|
}
|
|
262
291
|
|
|
263
292
|
function historyCommand(redo: boolean): Command {
|
|
264
|
-
|
|
293
|
+
const command: Command = (state, dispatch) => {
|
|
294
|
+
// One history, the body's: a story's editor has none of its own.
|
|
295
|
+
const body = storyBody(state);
|
|
296
|
+
if (body) return dispatch ? body.run(command) : body.can(command);
|
|
265
297
|
const hist = historyKey.getState(state);
|
|
266
298
|
const source = hist && (redo ? hist.undone : hist.done);
|
|
267
299
|
const event = source?.[source.length - 1];
|
|
268
300
|
if (!hist || !source || !event) return false;
|
|
269
301
|
if (dispatch) {
|
|
270
302
|
const tr = state.tr;
|
|
303
|
+
// The envelope changed outside history: the event is dropped whole, never half applied.
|
|
304
|
+
const context = wordContext(state);
|
|
305
|
+
if (context && !storedStepsApply(context, event.steps)) {
|
|
306
|
+
const rest = source.slice(0, -1);
|
|
307
|
+
tr.setMeta(historyKey, {
|
|
308
|
+
done: redo ? hist.done : rest,
|
|
309
|
+
undone: redo ? rest : hist.undone,
|
|
310
|
+
prevTime: 0,
|
|
311
|
+
prevRanges: null,
|
|
312
|
+
} satisfies HistoryState);
|
|
313
|
+
dispatch(tr);
|
|
314
|
+
return true;
|
|
315
|
+
}
|
|
271
316
|
// Rebasing keeps stored steps applicable; should one still fail, later steps are remapped
|
|
272
317
|
// as if it never existed rather than applied at positions that assumed it.
|
|
273
318
|
let remap: RebaseMapping | null = null;
|
|
@@ -297,15 +342,16 @@ function historyCommand(redo: boolean): Command {
|
|
|
297
342
|
}
|
|
298
343
|
return true;
|
|
299
344
|
};
|
|
345
|
+
return command;
|
|
300
346
|
}
|
|
301
347
|
|
|
302
348
|
export const undo: Command = historyCommand(false);
|
|
303
349
|
export const redo: Command = historyCommand(true);
|
|
304
350
|
|
|
305
351
|
export function undoDepth(state: EditorState): number {
|
|
306
|
-
return historyKey.getState(state)?.done.length ?? 0;
|
|
352
|
+
return historyKey.getState(storyBody(state)?.state ?? state)?.done.length ?? 0;
|
|
307
353
|
}
|
|
308
354
|
|
|
309
355
|
export function redoDepth(state: EditorState): number {
|
|
310
|
-
return historyKey.getState(state)?.undone.length ?? 0;
|
|
356
|
+
return historyKey.getState(storyBody(state)?.state ?? state)?.undone.length ?? 0;
|
|
311
357
|
}
|