@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,312 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Editing a story (ADR-0007): a header, footer or note has an `EditorState` of its own (selection,
|
|
3
|
+
* stored marks, tracking) but no history. **The body goes first:** a story's transaction is
|
|
4
|
+
* computed without being kept, the body receives it as a `StoryStep`, and the story takes the new
|
|
5
|
+
* state only if the body then holds its tree. A `StoryStep` arriving from the body (undo, another
|
|
6
|
+
* editor of the story) replaces the state's document.
|
|
7
|
+
*/
|
|
8
|
+
import { FoliDocumentError, type Node } from "../model/index.js";
|
|
9
|
+
import type { StoryJSON } from "../v3/document.js";
|
|
10
|
+
import { storyRules } from "../v3/document.js";
|
|
11
|
+
import type { Editor, EditorListener } from "./editor.js";
|
|
12
|
+
import { closeHistory, joinHistory, type StoryEdit, storyEdit } from "./history.js";
|
|
13
|
+
import { Selection, type SelectionJSON } from "./selection.js";
|
|
14
|
+
import { EditorState, Plugin, type Transaction } from "./state.js";
|
|
15
|
+
import {
|
|
16
|
+
maxRevisionIn,
|
|
17
|
+
revisionFloor,
|
|
18
|
+
type TrackState,
|
|
19
|
+
trackState,
|
|
20
|
+
trackTransaction,
|
|
21
|
+
untracked,
|
|
22
|
+
withTrackState,
|
|
23
|
+
} from "./track.js";
|
|
24
|
+
import {
|
|
25
|
+
CommentStep,
|
|
26
|
+
EnvelopeStep,
|
|
27
|
+
envelopeChange,
|
|
28
|
+
type Story,
|
|
29
|
+
type StoryBody,
|
|
30
|
+
StoryStep,
|
|
31
|
+
storyBody,
|
|
32
|
+
storyBodyKey,
|
|
33
|
+
type WordContext,
|
|
34
|
+
withContext,
|
|
35
|
+
withEnvelope,
|
|
36
|
+
wordContext,
|
|
37
|
+
} from "./word.js";
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* Calls a listener; one that throws is reported (`console.error`) and the others still run.
|
|
41
|
+
* @internal
|
|
42
|
+
*/
|
|
43
|
+
export function callListener(
|
|
44
|
+
listener: EditorListener,
|
|
45
|
+
state: EditorState,
|
|
46
|
+
transactions: readonly Transaction[],
|
|
47
|
+
): void {
|
|
48
|
+
try {
|
|
49
|
+
listener(state, transactions);
|
|
50
|
+
} catch (error) {
|
|
51
|
+
console.error("Foli: an editor listener failed", error);
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* What a story's editor needs of the body's.
|
|
57
|
+
* @internal
|
|
58
|
+
*/
|
|
59
|
+
export interface StoryHost extends StoryBody {
|
|
60
|
+
/** The plugins of a story's state (keymaps, tracking, the word context). */
|
|
61
|
+
plugins(context: WordContext, track: TrackState | null): Plugin[];
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/** Puts on `bodyTr` what `transactions` did to story `id`, from `before` to `next`. */
|
|
65
|
+
function carryStoryChange(
|
|
66
|
+
bodyTr: Transaction,
|
|
67
|
+
bodyContext: WordContext,
|
|
68
|
+
id: string,
|
|
69
|
+
before: EditorState,
|
|
70
|
+
next: EditorState,
|
|
71
|
+
transactions: readonly Transaction[],
|
|
72
|
+
): { after: Story; patched: boolean } {
|
|
73
|
+
const story = bodyContext.stories.get(id) as Story;
|
|
74
|
+
let after = story;
|
|
75
|
+
if (next.doc !== before.doc) {
|
|
76
|
+
after = { ...story, doc: next.doc };
|
|
77
|
+
bodyTr.step(
|
|
78
|
+
new StoryStep(id, story, after, before.selection.toJSON(), next.selection.toJSON()),
|
|
79
|
+
);
|
|
80
|
+
}
|
|
81
|
+
let patched = false;
|
|
82
|
+
for (const t of transactions) {
|
|
83
|
+
// What belongs to the document, not to the story: the body takes it.
|
|
84
|
+
for (const step of t.steps)
|
|
85
|
+
if (step instanceof CommentStep || step instanceof EnvelopeStep) bodyTr.step(step);
|
|
86
|
+
patched ||= envelopeChange(t) !== undefined;
|
|
87
|
+
}
|
|
88
|
+
// Only these: the story's copy of the rest of the envelope may be behind the body's.
|
|
89
|
+
if (patched) {
|
|
90
|
+
const { numbering, styles, resources, controls } = (wordContext(next) as WordContext).envelope;
|
|
91
|
+
const {
|
|
92
|
+
resources: _resources,
|
|
93
|
+
controls: _controls,
|
|
94
|
+
...rest
|
|
95
|
+
} = envelopeChange(bodyTr) ?? bodyContext.envelope;
|
|
96
|
+
withEnvelope(bodyTr, {
|
|
97
|
+
...rest,
|
|
98
|
+
numbering,
|
|
99
|
+
styles,
|
|
100
|
+
...(resources ? { resources } : {}),
|
|
101
|
+
...(controls ? { controls } : {}),
|
|
102
|
+
});
|
|
103
|
+
}
|
|
104
|
+
return { after, patched };
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
/**
|
|
108
|
+
* What changes one tree of the document: its id (null: the body) and the edit.
|
|
109
|
+
* @internal
|
|
110
|
+
*/
|
|
111
|
+
export type TreeEdits = ReadonlyMap<string | null, (tr: Transaction) => void>;
|
|
112
|
+
|
|
113
|
+
/**
|
|
114
|
+
* Edits several trees as one transaction of the body, so one undo step. `tracked`: as tracked
|
|
115
|
+
* changes when tracking is on. Returns the trees whose edit was refused; the others are made.
|
|
116
|
+
* @internal
|
|
117
|
+
*/
|
|
118
|
+
export function editTrees(
|
|
119
|
+
editor: Editor,
|
|
120
|
+
edits: TreeEdits,
|
|
121
|
+
{ tracked }: { tracked: boolean },
|
|
122
|
+
): Set<string | null> {
|
|
123
|
+
const host = storyBody(editor.state) ?? editor;
|
|
124
|
+
const state = host.state;
|
|
125
|
+
const context = wordContext(state) as WordContext;
|
|
126
|
+
const failed = new Set<string | null>();
|
|
127
|
+
const keep = (tr: Transaction) => (tracked ? trackTransaction(state, tr) : tr);
|
|
128
|
+
let out = state.tr;
|
|
129
|
+
const bodyEdit = edits.get(null);
|
|
130
|
+
if (bodyEdit) {
|
|
131
|
+
bodyEdit(out);
|
|
132
|
+
const made = keep(out);
|
|
133
|
+
if (made) out = made;
|
|
134
|
+
else {
|
|
135
|
+
failed.add(null);
|
|
136
|
+
out = state.tr;
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
let floor = tracked ? maxRevisionIn(out.doc) + 1 : 0;
|
|
140
|
+
for (const [id, edit] of edits) {
|
|
141
|
+
if (id === null) continue;
|
|
142
|
+
const story = editor.story(id);
|
|
143
|
+
if (!story) {
|
|
144
|
+
failed.add(id);
|
|
145
|
+
continue;
|
|
146
|
+
}
|
|
147
|
+
const before = story.state;
|
|
148
|
+
const tr = before.tr;
|
|
149
|
+
edit(tr);
|
|
150
|
+
if (!tr.docChanged) continue;
|
|
151
|
+
const made = tracked
|
|
152
|
+
? trackTransaction(before, tr.setMeta(revisionFloor, floor))
|
|
153
|
+
: tr.setMeta(untracked, true);
|
|
154
|
+
const result = made ? before.applyTransaction(made) : null;
|
|
155
|
+
if (!result?.transactions.length) {
|
|
156
|
+
failed.add(id);
|
|
157
|
+
continue;
|
|
158
|
+
}
|
|
159
|
+
carryStoryChange(out, context, id, before, result.state, result.transactions);
|
|
160
|
+
if (tracked) floor = Math.max(floor, maxRevisionIn(result.state.doc) + 1);
|
|
161
|
+
}
|
|
162
|
+
if (!out.docChanged) return failed;
|
|
163
|
+
host.dispatch(out.setMeta(untracked, true).setMeta(closeHistory, true));
|
|
164
|
+
// Refused by the body's filters: nothing happened, in any tree.
|
|
165
|
+
if (wordContext(host.state) === context && host.state.doc === state.doc)
|
|
166
|
+
for (const id of edits.keys()) failed.add(id);
|
|
167
|
+
return failed;
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
/** @internal */
|
|
171
|
+
export type StoryListener = (state: EditorState, transactions: readonly Transaction[]) => void;
|
|
172
|
+
|
|
173
|
+
/**
|
|
174
|
+
* A story's editing state, kept in step with the body.
|
|
175
|
+
* @internal
|
|
176
|
+
*/
|
|
177
|
+
export class StoryEditing {
|
|
178
|
+
state: EditorState;
|
|
179
|
+
/** Set while the story's own transaction goes through the body. */
|
|
180
|
+
private own = false;
|
|
181
|
+
/** Whether the story still exists: a removed one takes no more changes. */
|
|
182
|
+
alive = true;
|
|
183
|
+
readonly listeners = new Set<StoryListener>();
|
|
184
|
+
|
|
185
|
+
constructor(
|
|
186
|
+
readonly id: string,
|
|
187
|
+
private readonly host: StoryHost,
|
|
188
|
+
) {
|
|
189
|
+
const doc = this.tree()?.doc as Node;
|
|
190
|
+
this.state = this.create(doc);
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
private tree(): Story | undefined {
|
|
194
|
+
return wordContext(this.host.state)?.stories.get(this.id);
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
private create(doc: Node): EditorState {
|
|
198
|
+
const { host, id } = this;
|
|
199
|
+
const context = wordContext(host.state) as WordContext;
|
|
200
|
+
const kind = context.stories.get(id)?.kind as StoryJSON["kind"];
|
|
201
|
+
return EditorState.create({
|
|
202
|
+
doc,
|
|
203
|
+
selection: Selection.atStart(doc),
|
|
204
|
+
plugins: [
|
|
205
|
+
new Plugin<StoryBody>({
|
|
206
|
+
key: storyBodyKey,
|
|
207
|
+
state: { init: () => host, apply: () => host },
|
|
208
|
+
}),
|
|
209
|
+
// What a story may not hold (a section break, a comment) is never put in it.
|
|
210
|
+
new Plugin({
|
|
211
|
+
filterTransaction(tr) {
|
|
212
|
+
if (!tr.docChanged) return true;
|
|
213
|
+
try {
|
|
214
|
+
storyRules(tr.doc, kind, "story");
|
|
215
|
+
return true;
|
|
216
|
+
} catch (error) {
|
|
217
|
+
if (error instanceof FoliDocumentError) return false;
|
|
218
|
+
throw error;
|
|
219
|
+
}
|
|
220
|
+
},
|
|
221
|
+
}),
|
|
222
|
+
...host.plugins(context, trackState(host.state)),
|
|
223
|
+
],
|
|
224
|
+
});
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
private notify(transactions: readonly Transaction[]): void {
|
|
228
|
+
for (const listener of this.listeners) callListener(listener, this.state, transactions);
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
/** `state` with the body's context and tracking; its plugins keep their state. */
|
|
232
|
+
private current(state: EditorState): EditorState {
|
|
233
|
+
const context = wordContext(this.host.state) as WordContext;
|
|
234
|
+
const track = trackState(this.host.state);
|
|
235
|
+
if (wordContext(state) === context && trackState(state) === track) return state;
|
|
236
|
+
const tr = withContext(state.tr, context);
|
|
237
|
+
if (track) withTrackState(tr, track);
|
|
238
|
+
return state.apply(tr);
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
dispatch = (tr: Transaction): void => {
|
|
242
|
+
if (!this.alive) return;
|
|
243
|
+
const { host, id, state: before } = this;
|
|
244
|
+
const tracked = trackTransaction(before, tr);
|
|
245
|
+
if (!tracked) return;
|
|
246
|
+
const result = before.applyTransaction(tracked);
|
|
247
|
+
if (!result.transactions.length) return;
|
|
248
|
+
const next = result.state;
|
|
249
|
+
const bodyState = host.state;
|
|
250
|
+
const bodyContext = wordContext(bodyState) as WordContext;
|
|
251
|
+
const story = bodyContext.stories.get(id) as Story;
|
|
252
|
+
const bodyTr = bodyState.tr;
|
|
253
|
+
const { after, patched } = carryStoryChange(
|
|
254
|
+
bodyTr,
|
|
255
|
+
bodyContext,
|
|
256
|
+
id,
|
|
257
|
+
before,
|
|
258
|
+
next,
|
|
259
|
+
result.transactions,
|
|
260
|
+
);
|
|
261
|
+
const track = trackState(next);
|
|
262
|
+
const trackChanged = !!track && track !== trackState(before);
|
|
263
|
+
if (trackChanged) withTrackState(bodyTr, track);
|
|
264
|
+
if (bodyTr.steps.length || patched || trackChanged) {
|
|
265
|
+
bodyTr.setMeta(untracked, true);
|
|
266
|
+
bodyTr.setMeta(storyEdit, { id, tr: tracked } satisfies StoryEdit);
|
|
267
|
+
bodyTr.time = tracked.time;
|
|
268
|
+
if (!tracked.addToHistory) bodyTr.setMeta("addToHistory", false);
|
|
269
|
+
if (tracked.getMeta(closeHistory)) bodyTr.setMeta(closeHistory, true);
|
|
270
|
+
if (tracked.getMeta(joinHistory)) bodyTr.setMeta(joinHistory, true);
|
|
271
|
+
this.own = true;
|
|
272
|
+
try {
|
|
273
|
+
host.dispatch(bodyTr);
|
|
274
|
+
} finally {
|
|
275
|
+
this.own = false;
|
|
276
|
+
}
|
|
277
|
+
// Refused by the body: the story stays as it was.
|
|
278
|
+
if (after !== story && wordContext(host.state)?.stories.get(id) !== after) return;
|
|
279
|
+
}
|
|
280
|
+
this.state = this.current(next);
|
|
281
|
+
this.notify(result.transactions);
|
|
282
|
+
};
|
|
283
|
+
|
|
284
|
+
/** After a change of the body. */
|
|
285
|
+
sync(transactions: readonly Transaction[]): void {
|
|
286
|
+
if (this.own) return;
|
|
287
|
+
const tree = this.tree();
|
|
288
|
+
if (!tree) {
|
|
289
|
+
this.retire();
|
|
290
|
+
return;
|
|
291
|
+
}
|
|
292
|
+
const { state } = this;
|
|
293
|
+
let selection: SelectionJSON | null = null;
|
|
294
|
+
for (const tr of transactions)
|
|
295
|
+
for (const step of tr.steps)
|
|
296
|
+
if (step instanceof StoryStep && step.id === this.id) selection = step.selectionAfter;
|
|
297
|
+
if (tree.doc !== state.doc) {
|
|
298
|
+
const json = selection ?? state.selection.toJSON();
|
|
299
|
+
this.state = this.current(state.withDocument(tree.doc, Selection.fromJSON(tree.doc, json)));
|
|
300
|
+
this.notify([]);
|
|
301
|
+
return;
|
|
302
|
+
}
|
|
303
|
+
const current = this.current(state);
|
|
304
|
+
if (current === state) return;
|
|
305
|
+
this.state = current;
|
|
306
|
+
this.notify([]);
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
retire(): void {
|
|
310
|
+
this.alive = false;
|
|
311
|
+
}
|
|
312
|
+
}
|
package/src/state/tables.ts
CHANGED
|
@@ -13,7 +13,10 @@ import { type Command, type EditorState, Plugin, type Transaction } from "./stat
|
|
|
13
13
|
|
|
14
14
|
const isTable = (node: Node) => node.type.name === "table";
|
|
15
15
|
|
|
16
|
-
/**
|
|
16
|
+
/**
|
|
17
|
+
* The depth of the innermost table cell around `$pos`, or -1.
|
|
18
|
+
* @internal
|
|
19
|
+
*/
|
|
17
20
|
export function cellDepth($pos: ResolvedPos): number {
|
|
18
21
|
for (let d = $pos.depth; d > 0; d--) if ($pos.node(d).type.name === "tableCell") return d;
|
|
19
22
|
return -1;
|
|
@@ -30,6 +33,7 @@ function tableBelow($pos: ResolvedPos, shared: number): number {
|
|
|
30
33
|
/**
|
|
31
34
|
* Deletes `from..to` in `tr` keeping tables valid; returns false (and does nothing) when an
|
|
32
35
|
* ordinary delete keeps them valid (no end of the range inside a table it leaves).
|
|
36
|
+
* @internal
|
|
33
37
|
*/
|
|
34
38
|
export function deleteAcrossTables(tr: Transaction, from: number, to: number): boolean {
|
|
35
39
|
const doc = tr.doc;
|
|
@@ -112,7 +116,8 @@ function deleteBetween(tr: Transaction, a: number, b: number): void {
|
|
|
112
116
|
* (paragraphs in a cell joined), so a tracked clear can be rejected; a cell holding other
|
|
113
117
|
* blocks (a nested table) gets one empty paragraph instead.
|
|
114
118
|
*
|
|
115
|
-
* shortcut: the cells in document order, not Word's rectangle (
|
|
119
|
+
* shortcut: the cells in document order between the range's ends, not Word's rectangle (a
|
|
120
|
+
* `CellSelection` deletes its own rectangle).
|
|
116
121
|
*/
|
|
117
122
|
function clearCells(tr: Transaction, from: number, to: number, start: number, end: number): void {
|
|
118
123
|
const cells: { pos: number; node: Node }[] = [];
|
|
@@ -134,6 +139,7 @@ function clearCells(tr: Transaction, from: number, to: number, start: number, en
|
|
|
134
139
|
* Empties the cells at positions (positions before them, any order): their text deleted
|
|
135
140
|
* (paragraphs in a cell joined), so a tracked clear can be rejected; a cell holding other blocks
|
|
136
141
|
* (a nested table) gets one empty paragraph instead.
|
|
142
|
+
* @internal
|
|
137
143
|
*/
|
|
138
144
|
export function emptyCells(tr: Transaction, positions: readonly number[]): void {
|
|
139
145
|
for (const pos of [...positions].sort((a, b) => b - a)) {
|
|
@@ -259,6 +265,7 @@ export function goToCell(dir: 1 | -1): Command {
|
|
|
259
265
|
* A new row like `last`: its properties, not its identity (`w14:paraId`…) or revisions; its
|
|
260
266
|
* cells empty, unmerged, their paragraph with the first one's properties, without what only one
|
|
261
267
|
* paragraph has. Null when the schema has no paragraph.
|
|
268
|
+
* @internal
|
|
262
269
|
*/
|
|
263
270
|
export function emptyRowLike(last: Node): Node | null {
|
|
264
271
|
const paragraph = last.type.schema.nodes.paragraph;
|
|
@@ -361,14 +368,20 @@ function cellAtColumn(table: Node, tableStart: number, r: number, column: number
|
|
|
361
368
|
}
|
|
362
369
|
return null;
|
|
363
370
|
}
|
|
364
|
-
/**
|
|
371
|
+
/**
|
|
372
|
+
* Tab and Shift+Tab between cells: before the lists' Tab (in a cell Word moves, numbered or not).
|
|
373
|
+
* @internal
|
|
374
|
+
*/
|
|
365
375
|
export function tableKeymap(): Plugin {
|
|
366
376
|
return new Plugin({ keymap: { Tab: goToCell(1), "Shift-Tab": goToCell(-1) } });
|
|
367
377
|
}
|
|
368
378
|
|
|
369
379
|
// ---- Cell selections (10.6c) ---------------------------------------------------------------------
|
|
370
380
|
|
|
371
|
-
/**
|
|
381
|
+
/**
|
|
382
|
+
* A cell where a table's grid has it: its position and the rows and grid columns it covers.
|
|
383
|
+
* @internal
|
|
384
|
+
*/
|
|
372
385
|
export interface GridCell {
|
|
373
386
|
pos: number;
|
|
374
387
|
row: number;
|
|
@@ -377,7 +390,10 @@ export interface GridCell {
|
|
|
377
390
|
span: number;
|
|
378
391
|
}
|
|
379
392
|
|
|
380
|
-
/**
|
|
393
|
+
/**
|
|
394
|
+
* A table's cells on its grid (a merge's first cell spans the rows its merge covers).
|
|
395
|
+
* @internal
|
|
396
|
+
*/
|
|
381
397
|
export function gridOf(table: Node, start: number): GridCell[] {
|
|
382
398
|
const out: GridCell[] = [];
|
|
383
399
|
const open = new Map<number, GridCell>();
|
|
@@ -472,7 +488,10 @@ export class CellSelection extends Selection {
|
|
|
472
488
|
}
|
|
473
489
|
}
|
|
474
490
|
|
|
475
|
-
/**
|
|
491
|
+
/**
|
|
492
|
+
* The grid cell covering the cell at `pos` of `table` (at `start`): a merge's, for a covered one.
|
|
493
|
+
* @internal
|
|
494
|
+
*/
|
|
476
495
|
export function coveringCell(
|
|
477
496
|
table: Node,
|
|
478
497
|
start: number,
|
|
@@ -495,7 +514,10 @@ export function coveringCell(
|
|
|
495
514
|
return undefined;
|
|
496
515
|
}
|
|
497
516
|
|
|
498
|
-
/**
|
|
517
|
+
/**
|
|
518
|
+
* The grid cell covering `row` and `column`.
|
|
519
|
+
* @internal
|
|
520
|
+
*/
|
|
499
521
|
export const coveringAt = (grid: readonly GridCell[], row: number, column: number) =>
|
|
500
522
|
grid.find(
|
|
501
523
|
(c) =>
|
package/src/state/track.ts
CHANGED
|
@@ -30,12 +30,12 @@ import {
|
|
|
30
30
|
type Step,
|
|
31
31
|
} from "../transform/steps.js";
|
|
32
32
|
import { TransformError } from "../transform/transform.js";
|
|
33
|
-
import type { CommentJSON } from "../v3/document.js";
|
|
33
|
+
import type { CommentJSON, SectionJSON } from "../v3/document.js";
|
|
34
34
|
import { PARAGRAPH_PROPERTY_KEYS, RUN_PROPERTY_MARKS } from "../v3/schema.js";
|
|
35
35
|
import { MARK_ATTRS } from "./mark.js";
|
|
36
36
|
import { NodeSelection, Selection, TextSelection } from "./selection.js";
|
|
37
37
|
import { type Command, type EditorState, Plugin, PluginKey, type Transaction } from "./state.js";
|
|
38
|
-
import { wordContext } from "./word.js";
|
|
38
|
+
import { otherTrees, wordContext } from "./word.js";
|
|
39
39
|
|
|
40
40
|
/**
|
|
41
41
|
* How tracked changes show: `markup` (inserted text underlined and deleted text struck through,
|
|
@@ -58,8 +58,27 @@ const key = new PluginKey<TrackState>("track");
|
|
|
58
58
|
/** Set this meta (true) on a transaction to keep it out of tracking. */
|
|
59
59
|
export const untracked = "untracked";
|
|
60
60
|
|
|
61
|
+
/**
|
|
62
|
+
* @internal On a transaction: the lowest id its tracked changes may take, for one operation over
|
|
63
|
+
* several trees (each is rebuilt before the body holds the others' new ids).
|
|
64
|
+
*/
|
|
65
|
+
export const revisionFloor = "revisionFloor";
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* On a rebuilt transaction: the ranges it records as deleted, as `[from, to]` of the document before.
|
|
69
|
+
* @internal
|
|
70
|
+
*/
|
|
71
|
+
export const trackedDeletes = "trackedDeletes";
|
|
72
|
+
|
|
61
73
|
/** Inline nodes a tracked change does not mark: they stand between runs in Word. */
|
|
62
|
-
const MARKERS = new Set([
|
|
74
|
+
const MARKERS = new Set([
|
|
75
|
+
"bookmarkStart",
|
|
76
|
+
"bookmarkEnd",
|
|
77
|
+
"commentStart",
|
|
78
|
+
"commentEnd",
|
|
79
|
+
"controlStart",
|
|
80
|
+
"controlEnd",
|
|
81
|
+
]);
|
|
63
82
|
|
|
64
83
|
const RUN_MARKS = new Set<string>(RUN_PROPERTY_MARKS);
|
|
65
84
|
/**
|
|
@@ -87,6 +106,7 @@ interface ParagraphRevision {
|
|
|
87
106
|
deletion?: Revision;
|
|
88
107
|
}
|
|
89
108
|
|
|
109
|
+
/** @internal */
|
|
90
110
|
export function trackPlugin(state: TrackState): Plugin<TrackState> {
|
|
91
111
|
return new Plugin({
|
|
92
112
|
key,
|
|
@@ -102,6 +122,13 @@ export function trackState(state: EditorState): TrackState | null {
|
|
|
102
122
|
return key.getState(state) ?? null;
|
|
103
123
|
}
|
|
104
124
|
|
|
125
|
+
/**
|
|
126
|
+
* Makes `tr` set the tracking state (a story's editor passes its change to the body).
|
|
127
|
+
* @internal
|
|
128
|
+
*/
|
|
129
|
+
export const withTrackState = (tr: Transaction, state: TrackState): Transaction =>
|
|
130
|
+
tr.setMeta(key, state);
|
|
131
|
+
|
|
105
132
|
/** Turns tracking on or off (and sets the author). Applies only to a schema v3 editor. */
|
|
106
133
|
export function setTrackChanges(on: boolean, author?: string): Command {
|
|
107
134
|
return (state, dispatch) => {
|
|
@@ -129,26 +156,43 @@ export function now(): string {
|
|
|
129
156
|
|
|
130
157
|
const REVIEW_MARKS = new Set(["insertion", "deletion", "formatChange"]);
|
|
131
158
|
|
|
132
|
-
/** The largest change id
|
|
133
|
-
function
|
|
159
|
+
/** @internal The largest change id on a tree's nodes, or -1. */
|
|
160
|
+
export function maxRevisionIn(doc: Node): number {
|
|
134
161
|
let max = -1;
|
|
135
162
|
const see = (value: unknown) => {
|
|
136
163
|
const id = (value as { id?: unknown } | null | undefined)?.id;
|
|
137
164
|
if (typeof id === "number" && id > max) max = id;
|
|
138
165
|
};
|
|
139
166
|
doc.descendants((node) => {
|
|
140
|
-
for (const mark of node.marks)
|
|
141
|
-
const name = mark.type.name;
|
|
142
|
-
if (REVIEW_MARKS.has(name)) see(mark.attrs);
|
|
143
|
-
}
|
|
167
|
+
for (const mark of node.marks) if (REVIEW_MARKS.has(mark.type.name)) see(mark.attrs);
|
|
144
168
|
if (node.isTextblock) {
|
|
145
169
|
const revision = node.attrs.revision as ParagraphRevision | null | undefined;
|
|
146
170
|
see(revision?.insertion);
|
|
147
171
|
see(revision?.deletion);
|
|
148
172
|
see(node.attrs.formatChange);
|
|
173
|
+
see((node.attrs.section as SectionJSON | null | undefined)?.change);
|
|
149
174
|
}
|
|
150
175
|
return true;
|
|
151
176
|
});
|
|
177
|
+
return max;
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
/**
|
|
181
|
+
* The largest change id in the document's trees (the body and every story: ids are allocated
|
|
182
|
+
* across them), the comments' content (marks, paragraph revisions) and the sections' tracked
|
|
183
|
+
* setups, or -1.
|
|
184
|
+
*/
|
|
185
|
+
function maxRevisionId(state: EditorState): number {
|
|
186
|
+
const envelope = wordContext(state)?.envelope;
|
|
187
|
+
const comments: readonly CommentJSON[] = envelope?.comments ?? [];
|
|
188
|
+
const last: SectionJSON | undefined = envelope?.section;
|
|
189
|
+
let max = -1;
|
|
190
|
+
const see = (value: unknown) => {
|
|
191
|
+
const id = (value as { id?: unknown } | null | undefined)?.id;
|
|
192
|
+
if (typeof id === "number" && id > max) max = id;
|
|
193
|
+
};
|
|
194
|
+
see(last?.change);
|
|
195
|
+
for (const doc of [state.doc, ...otherTrees(state)]) max = Math.max(max, maxRevisionIn(doc));
|
|
152
196
|
// Comments hold paragraphs too, which can carry changes (JSON: walked as it is).
|
|
153
197
|
const walk = (value: unknown): void => {
|
|
154
198
|
if (Array.isArray(value)) value.forEach(walk);
|
|
@@ -168,6 +212,18 @@ function maxRevisionId(doc: Node, comments: readonly CommentJSON[]): number {
|
|
|
168
212
|
return max;
|
|
169
213
|
}
|
|
170
214
|
|
|
215
|
+
/** @internal A new tracked change's id, author and date, when `state` is tracking; else null. */
|
|
216
|
+
export function newRevision(state: EditorState): Revision | null {
|
|
217
|
+
const track = key.getState(state);
|
|
218
|
+
if (!track?.on) return null;
|
|
219
|
+
return { id: nextRevisionId(state), author: track.author, date: now() };
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
/** @internal The id after every change of the document, in all its trees. */
|
|
223
|
+
export function nextRevisionId(state: EditorState): number {
|
|
224
|
+
return maxRevisionId(state) + 1;
|
|
225
|
+
}
|
|
226
|
+
|
|
171
227
|
/** A replaced range: `a..b` before the transaction, `c..d` after it. */
|
|
172
228
|
interface Range {
|
|
173
229
|
a: number;
|
|
@@ -252,11 +308,17 @@ function rebuild(
|
|
|
252
308
|
deletion: MarkType,
|
|
253
309
|
): Transaction {
|
|
254
310
|
const out = state.tr.copyMeta(tr);
|
|
311
|
+
out.setMeta(
|
|
312
|
+
trackedDeletes,
|
|
313
|
+
changedRanges(tr)
|
|
314
|
+
.filter((r) => r.b > r.a)
|
|
315
|
+
.map((r) => [r.a, r.b]),
|
|
316
|
+
);
|
|
255
317
|
const date = now();
|
|
256
318
|
let nextId = -1;
|
|
257
319
|
const newId = () => {
|
|
258
320
|
if (nextId < 0)
|
|
259
|
-
nextId = maxRevisionId(state
|
|
321
|
+
nextId = Math.max(maxRevisionId(state) + 1, (tr.getMeta(revisionFloor) as number) ?? 0);
|
|
260
322
|
return nextId++;
|
|
261
323
|
};
|
|
262
324
|
const at = (x: At, assoc: number) => out.mapping.slice(x.step).map(x.pos, assoc);
|