@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.
Files changed (202) hide show
  1. package/CHANGELOG.md +187 -0
  2. package/README.md +1 -1
  3. package/dist/document.d.ts +0 -7
  4. package/dist/document.d.ts.map +1 -1
  5. package/dist/document.js +5 -1
  6. package/dist/document.js.map +1 -1
  7. package/dist/index.d.ts +4 -4
  8. package/dist/index.d.ts.map +1 -1
  9. package/dist/index.js +8 -4
  10. package/dist/index.js.map +1 -1
  11. package/dist/internal.d.ts +5 -0
  12. package/dist/internal.d.ts.map +1 -0
  13. package/dist/internal.js +7 -0
  14. package/dist/internal.js.map +1 -0
  15. package/dist/model/attrs.d.ts +0 -10
  16. package/dist/model/attrs.d.ts.map +1 -1
  17. package/dist/model/attrs.js +10 -2
  18. package/dist/model/attrs.js.map +1 -1
  19. package/dist/model/content.d.ts +1 -1
  20. package/dist/model/content.d.ts.map +1 -1
  21. package/dist/model/content.js +1 -1
  22. package/dist/model/content.js.map +1 -1
  23. package/dist/model/errors.d.ts +0 -2
  24. package/dist/model/errors.d.ts.map +1 -1
  25. package/dist/model/errors.js +4 -1
  26. package/dist/model/errors.js.map +1 -1
  27. package/dist/model/fragment.d.ts +1 -1
  28. package/dist/model/fragment.d.ts.map +1 -1
  29. package/dist/model/fragment.js +3 -1
  30. package/dist/model/fragment.js.map +1 -1
  31. package/dist/model/mark.d.ts +1 -1
  32. package/dist/model/mark.d.ts.map +1 -1
  33. package/dist/model/mark.js +1 -1
  34. package/dist/model/mark.js.map +1 -1
  35. package/dist/model/node.d.ts +4 -4
  36. package/dist/model/node.d.ts.map +1 -1
  37. package/dist/model/node.js +8 -4
  38. package/dist/model/node.js.map +1 -1
  39. package/dist/model/schema.d.ts +8 -8
  40. package/dist/model/schema.d.ts.map +1 -1
  41. package/dist/model/schema.js +4 -3
  42. package/dist/model/schema.js.map +1 -1
  43. package/dist/schema.d.ts +0 -11
  44. package/dist/schema.d.ts.map +1 -1
  45. package/dist/schema.js +2 -0
  46. package/dist/schema.js.map +1 -1
  47. package/dist/state/attempt.d.ts +1 -3
  48. package/dist/state/attempt.d.ts.map +1 -1
  49. package/dist/state/commands.d.ts +2 -0
  50. package/dist/state/commands.d.ts.map +1 -1
  51. package/dist/state/commands.js +38 -6
  52. package/dist/state/commands.js.map +1 -1
  53. package/dist/state/comments.d.ts +1 -7
  54. package/dist/state/comments.d.ts.map +1 -1
  55. package/dist/state/comments.js +13 -3
  56. package/dist/state/comments.js.map +1 -1
  57. package/dist/state/controls.d.ts +110 -0
  58. package/dist/state/controls.d.ts.map +1 -0
  59. package/dist/state/controls.js +1473 -0
  60. package/dist/state/controls.js.map +1 -0
  61. package/dist/state/editor.d.ts +188 -68
  62. package/dist/state/editor.d.ts.map +1 -1
  63. package/dist/state/editor.js +265 -79
  64. package/dist/state/editor.js.map +1 -1
  65. package/dist/state/fields.d.ts +68 -0
  66. package/dist/state/fields.d.ts.map +1 -0
  67. package/dist/state/fields.js +410 -0
  68. package/dist/state/fields.js.map +1 -0
  69. package/dist/state/fieldupdate.d.ts +132 -0
  70. package/dist/state/fieldupdate.d.ts.map +1 -0
  71. package/dist/state/fieldupdate.js +1575 -0
  72. package/dist/state/fieldupdate.js.map +1 -0
  73. package/dist/state/find.d.ts +66 -0
  74. package/dist/state/find.d.ts.map +1 -0
  75. package/dist/state/find.js +405 -0
  76. package/dist/state/find.js.map +1 -0
  77. package/dist/state/history.d.ts +2 -0
  78. package/dist/state/history.d.ts.map +1 -1
  79. package/dist/state/history.js +44 -7
  80. package/dist/state/history.js.map +1 -1
  81. package/dist/state/index.d.ts +13 -6
  82. package/dist/state/index.d.ts.map +1 -1
  83. package/dist/state/index.js +12 -5
  84. package/dist/state/index.js.map +1 -1
  85. package/dist/state/lists.d.ts +3 -3
  86. package/dist/state/lists.d.ts.map +1 -1
  87. package/dist/state/lists.js.map +1 -1
  88. package/dist/state/mark.d.ts +1 -17
  89. package/dist/state/mark.d.ts.map +1 -1
  90. package/dist/state/mark.js +10 -1
  91. package/dist/state/mark.js.map +1 -1
  92. package/dist/state/navigation.d.ts +70 -0
  93. package/dist/state/navigation.d.ts.map +1 -0
  94. package/dist/state/navigation.js +226 -0
  95. package/dist/state/navigation.js.map +1 -0
  96. package/dist/state/notes.d.ts +84 -0
  97. package/dist/state/notes.d.ts.map +1 -0
  98. package/dist/state/notes.js +451 -0
  99. package/dist/state/notes.js.map +1 -0
  100. package/dist/state/review.d.ts +8 -4
  101. package/dist/state/review.d.ts.map +1 -1
  102. package/dist/state/review.js +134 -18
  103. package/dist/state/review.js.map +1 -1
  104. package/dist/state/sections.d.ts +91 -0
  105. package/dist/state/sections.d.ts.map +1 -0
  106. package/dist/state/sections.js +479 -0
  107. package/dist/state/sections.js.map +1 -0
  108. package/dist/state/state.d.ts +13 -7
  109. package/dist/state/state.d.ts.map +1 -1
  110. package/dist/state/state.js +9 -2
  111. package/dist/state/state.js.map +1 -1
  112. package/dist/state/stories.d.ts +2 -0
  113. package/dist/state/stories.d.ts.map +1 -0
  114. package/dist/state/stories.js +259 -0
  115. package/dist/state/stories.js.map +1 -0
  116. package/dist/state/tables.d.ts +1 -36
  117. package/dist/state/tables.d.ts.map +1 -1
  118. package/dist/state/tables.js +25 -6
  119. package/dist/state/tables.js.map +1 -1
  120. package/dist/state/track.d.ts +1 -6
  121. package/dist/state/track.d.ts.map +1 -1
  122. package/dist/state/track.js +65 -9
  123. package/dist/state/track.js.map +1 -1
  124. package/dist/state/word.d.ts +47 -55
  125. package/dist/state/word.d.ts.map +1 -1
  126. package/dist/state/word.js +306 -19
  127. package/dist/state/word.js.map +1 -1
  128. package/dist/state/wordclip.d.ts +17 -6
  129. package/dist/state/wordclip.d.ts.map +1 -1
  130. package/dist/state/wordclip.js +254 -17
  131. package/dist/state/wordclip.js.map +1 -1
  132. package/dist/state/wordformat.d.ts +1 -86
  133. package/dist/state/wordformat.d.ts.map +1 -1
  134. package/dist/state/wordformat.js +114 -12
  135. package/dist/state/wordformat.js.map +1 -1
  136. package/dist/transform/stepmap.d.ts +0 -2
  137. package/dist/transform/stepmap.d.ts.map +1 -1
  138. package/dist/v3/check.d.ts +0 -25
  139. package/dist/v3/check.d.ts.map +1 -1
  140. package/dist/v3/check.js +35 -7
  141. package/dist/v3/check.js.map +1 -1
  142. package/dist/v3/document.d.ts +135 -10
  143. package/dist/v3/document.d.ts.map +1 -1
  144. package/dist/v3/document.js +192 -38
  145. package/dist/v3/document.js.map +1 -1
  146. package/dist/v3/extensions.d.ts +43 -0
  147. package/dist/v3/extensions.d.ts.map +1 -0
  148. package/dist/v3/extensions.js +154 -0
  149. package/dist/v3/extensions.js.map +1 -0
  150. package/dist/v3/index.d.ts +3 -2
  151. package/dist/v3/index.d.ts.map +1 -1
  152. package/dist/v3/index.js +3 -2
  153. package/dist/v3/index.js.map +1 -1
  154. package/dist/v3/resolve.d.ts +9 -3
  155. package/dist/v3/resolve.d.ts.map +1 -1
  156. package/dist/v3/resolve.js +28 -4
  157. package/dist/v3/resolve.js.map +1 -1
  158. package/dist/v3/schema.d.ts +5 -16
  159. package/dist/v3/schema.d.ts.map +1 -1
  160. package/dist/v3/schema.js +167 -7
  161. package/dist/v3/schema.js.map +1 -1
  162. package/package.json +5 -1
  163. package/src/document.ts +5 -1
  164. package/src/index.ts +330 -4
  165. package/src/internal.ts +50 -0
  166. package/src/model/attrs.ts +10 -2
  167. package/src/model/content.ts +1 -1
  168. package/src/model/errors.ts +4 -1
  169. package/src/model/fragment.ts +2 -1
  170. package/src/model/mark.ts +1 -1
  171. package/src/model/node.ts +6 -4
  172. package/src/model/schema.ts +9 -3
  173. package/src/schema.ts +2 -0
  174. package/src/state/commands.ts +41 -4
  175. package/src/state/comments.ts +12 -3
  176. package/src/state/controls.ts +1612 -0
  177. package/src/state/editor.ts +508 -143
  178. package/src/state/fields.ts +444 -0
  179. package/src/state/fieldupdate.ts +1771 -0
  180. package/src/state/find.ts +518 -0
  181. package/src/state/history.ts +53 -7
  182. package/src/state/index.ts +125 -1
  183. package/src/state/lists.ts +2 -2
  184. package/src/state/mark.ts +10 -1
  185. package/src/state/navigation.ts +305 -0
  186. package/src/state/notes.ts +529 -0
  187. package/src/state/numeric-pictures.word.json +172 -0
  188. package/src/state/review.ts +172 -19
  189. package/src/state/sections.ts +578 -0
  190. package/src/state/state.ts +21 -3
  191. package/src/state/stories.ts +312 -0
  192. package/src/state/tables.ts +29 -7
  193. package/src/state/track.ts +72 -10
  194. package/src/state/word.ts +360 -24
  195. package/src/state/wordclip.ts +282 -32
  196. package/src/state/wordformat.ts +116 -12
  197. package/src/v3/check.ts +35 -7
  198. package/src/v3/document.ts +358 -50
  199. package/src/v3/extensions.ts +201 -0
  200. package/src/v3/index.ts +16 -0
  201. package/src/v3/resolve.ts +38 -9
  202. package/src/v3/schema.ts +174 -7
package/src/state/word.ts CHANGED
@@ -9,20 +9,48 @@
9
9
  * shortcut: v2 and v3 editing live side by side until 8.7d makes v3 the only schema; then these
10
10
  * become the commands themselves.
11
11
  */
12
- import type { Attrs, Mark, MarkType, Node } from "../model/index.js";
12
+ import { deepEqual } from "../model/attrs.js";
13
+ import type { Attrs, Mark, MarkType, Node, Schema } from "../model/index.js";
13
14
  import { Step, StepResult } from "../transform/steps.js";
14
- import type { CommentJSON, FoliDocument } from "../v3/document.js";
15
+ import type { CommentJSON, FoliDocument, StoryJSON } from "../v3/document.js";
16
+ import { parseContent } from "../v3/document.js";
17
+ import { writeCustom } from "../v3/extensions.js";
15
18
  import { Resolver, type RunFormat } from "../v3/resolve.js";
16
19
  import { MARK_ATTRS } from "./mark.js";
17
- import { NodeSelection, TextSelection } from "./selection.js";
18
- import { type EditorState, Plugin, PluginKey, type Transaction } from "./state.js";
20
+ import { NodeSelection, type SelectionJSON, TextSelection } from "./selection.js";
21
+ import { type Command, EditorState, Plugin, PluginKey, type Transaction } from "./state.js";
19
22
 
20
23
  /** A v3 document without its content: what the editor keeps beside the edited tree. */
21
24
  export type WordEnvelope = Omit<FoliDocument, "content">;
22
25
 
26
+ /**
27
+ * The document of an editor's `envelope` and tree `doc`, as apps get it (`getDocument`, React's
28
+ * `onChange`): a copy, foreign nodes and marks written back as the custom types they stand for.
29
+ */
30
+ export function documentJSON(envelope: WordEnvelope, doc: Node): FoliDocument {
31
+ const { stories, comments, ...rest } = structuredClone(envelope);
32
+ return {
33
+ ...rest,
34
+ comments: comments.map((c) => ({ ...c, content: c.content.map(writeCustom) })),
35
+ ...(stories && {
36
+ stories: Object.fromEntries(
37
+ Object.entries(stories).map(([id, s]) => [id, { ...s, content: writeCustom(s.content) }]),
38
+ ),
39
+ }),
40
+ content: writeCustom(doc.toJSON()),
41
+ };
42
+ }
43
+
44
+ /** A story as the editor holds it: its tree, and what the envelope says beside it. */
45
+ export interface Story extends Omit<StoryJSON, "content"> {
46
+ readonly doc: Node;
47
+ }
48
+
23
49
  export interface WordContext {
24
50
  readonly envelope: WordEnvelope;
25
51
  readonly resolver: Resolver;
52
+ /** The envelope's `stories` as trees: what `StoryStep`s hold and compare. */
53
+ readonly stories: ReadonlyMap<string, Story>;
26
54
  /**
27
55
  * Made when the document is loaded and kept while it is edited: a copy carries it, so a paste
28
56
  * tells this document's lists from another's (Word's `w:nsid`; Foli makes equal definitions
@@ -61,7 +89,10 @@ export class CommentStep extends Step {
61
89
  }
62
90
  }
63
91
 
64
- /** `comments` after the comment steps among `steps`. */
92
+ /**
93
+ * `comments` after the comment steps among `steps`.
94
+ * @internal
95
+ */
65
96
  export function applyCommentSteps(
66
97
  comments: readonly CommentJSON[],
67
98
  steps: readonly Step[],
@@ -108,27 +139,291 @@ export function setComments(
108
139
  return tr;
109
140
  }
110
141
 
142
+ /** What `EnvelopeStep` can change (ADR-0007). */
143
+ export type EnvelopeKey = "section" | "settings" | "controls";
144
+
145
+ /**
146
+ * Changes one value of the envelope (the last section's setup, the document's settings) from
147
+ * `before` to `after`. A step, unlike `withEnvelope`, so undo and redo take it with the edit it
148
+ * belongs to. It leaves the tree as it is; history refuses an undo whose `before` no longer holds.
149
+ */
150
+ export class EnvelopeStep<K extends EnvelopeKey = EnvelopeKey> extends Step {
151
+ constructor(
152
+ readonly key: K,
153
+ readonly before: WordEnvelope[K],
154
+ readonly after: WordEnvelope[K],
155
+ ) {
156
+ super();
157
+ }
158
+
159
+ apply(doc: Node): StepResult {
160
+ return StepResult.ok(doc);
161
+ }
162
+
163
+ invert(): Step {
164
+ return new EnvelopeStep(this.key, this.after, this.before);
165
+ }
166
+
167
+ map(): Step {
168
+ return this;
169
+ }
170
+ }
171
+
172
+ /**
173
+ * Changes story `id` from `before` to `after` (null: no story), a step of the body's transaction
174
+ * (ADR-0007): its inverse needs no document, it moves no position of the body. The selections are
175
+ * the story's around the change, so undo can give it back.
176
+ */
177
+ export class StoryStep extends Step {
178
+ constructor(
179
+ readonly id: string,
180
+ readonly before: Story | null,
181
+ readonly after: Story | null,
182
+ readonly selectionBefore: SelectionJSON | null = null,
183
+ readonly selectionAfter: SelectionJSON | null = null,
184
+ ) {
185
+ super();
186
+ }
187
+
188
+ apply(doc: Node): StepResult {
189
+ return StepResult.ok(doc);
190
+ }
191
+
192
+ invert(): Step {
193
+ return new StoryStep(
194
+ this.id,
195
+ this.after,
196
+ this.before,
197
+ this.selectionAfter,
198
+ this.selectionBefore,
199
+ );
200
+ }
201
+
202
+ map(): Step {
203
+ return this;
204
+ }
205
+
206
+ /** This step followed by `next`, when `next` starts from what this one leaves. */
207
+ merge(next: StoryStep): StoryStep | null {
208
+ if (next.id !== this.id || !sameStory(this.after, next.before)) return null;
209
+ return new StoryStep(
210
+ this.id,
211
+ this.before,
212
+ next.after,
213
+ this.selectionBefore,
214
+ next.selectionAfter,
215
+ );
216
+ }
217
+ }
218
+
219
+ /**
220
+ * Whether two stories are the same: identical trees are the usual case.
221
+ * @internal
222
+ */
223
+ export function sameStory(a: Story | null | undefined, b: Story | null | undefined): boolean {
224
+ if (a === b || (!a && !b)) return true;
225
+ return (
226
+ !!a &&
227
+ !!b &&
228
+ a.kind === b.kind &&
229
+ a.noteType === b.noteType &&
230
+ a.ext === b.ext &&
231
+ (a.doc === b.doc || a.doc.eq(b.doc))
232
+ );
233
+ }
234
+
235
+ /** A story's JSON in the envelope. */
236
+ function storyJSON({ doc, ...rest }: Story): StoryJSON {
237
+ return { ...rest, content: doc.toJSON() };
238
+ }
239
+
240
+ /** The value of `key` as `steps` leave it, from `value`. */
241
+ function afterEnvelopeSteps<K extends EnvelopeKey>(
242
+ key: K,
243
+ value: WordEnvelope[K],
244
+ steps: readonly Step[],
245
+ ): WordEnvelope[K] {
246
+ let current = value;
247
+ for (const step of steps)
248
+ if (step instanceof EnvelopeStep && step.key === key) current = step.after as WordEnvelope[K];
249
+ return current;
250
+ }
251
+
252
+ /**
253
+ * Makes `tr` set the envelope's `key` to `value`, from what it holds after `tr` so far.
254
+ * @internal
255
+ */
256
+ export function setEnvelopeValue<K extends EnvelopeKey>(
257
+ state: EditorState,
258
+ tr: Transaction,
259
+ key: K,
260
+ value: WordEnvelope[K],
261
+ ): Transaction {
262
+ const envelope = wordContext(state)?.envelope;
263
+ if (!envelope) return tr;
264
+ return tr.step(new EnvelopeStep(key, afterEnvelopeSteps(key, envelope[key], tr.steps), value));
265
+ }
266
+
267
+ /**
268
+ * Whether stored `steps` can apply to `context`: every `EnvelopeStep` and `StoryStep` finds what
269
+ * it expects, given the ones before it. A change made outside history makes them stale.
270
+ * @internal
271
+ */
272
+ export function storedStepsApply(context: WordContext, steps: readonly Step[]): boolean {
273
+ const values = new Map<EnvelopeKey, unknown>();
274
+ const stories = new Map<string, Story | null>();
275
+ for (const step of steps) {
276
+ if (step instanceof EnvelopeStep) {
277
+ const key = step.key as EnvelopeKey;
278
+ const current = values.has(key) ? values.get(key) : context.envelope[key];
279
+ if (!deepEqual(current, step.before)) return false;
280
+ values.set(key, step.after);
281
+ } else if (step instanceof StoryStep) {
282
+ const current = stories.has(step.id)
283
+ ? stories.get(step.id)
284
+ : (context.stories.get(step.id) ?? null);
285
+ if (!sameStory(current, step.before)) return false;
286
+ stories.set(step.id, step.after);
287
+ }
288
+ }
289
+ return true;
290
+ }
291
+
292
+ /**
293
+ * The story as `tr` leaves it (`state`'s, after the story steps `tr` already has).
294
+ * @internal
295
+ */
296
+ export function storyAfter(state: EditorState, tr: Transaction, id: string): Story | null {
297
+ let story = wordContext(state)?.stories.get(id) ?? null;
298
+ for (const step of tr.steps) if (step instanceof StoryStep && step.id === id) story = step.after;
299
+ return story;
300
+ }
301
+
302
+ /**
303
+ * What a story's editor knows of the body's: undo, and the trees it allocates ids across.
304
+ * @internal
305
+ */
306
+ export interface StoryBody {
307
+ readonly state: EditorState;
308
+ dispatch(tr: Transaction): void;
309
+ run(command: Command): boolean;
310
+ can(command: Command): boolean;
311
+ }
312
+
313
+ /**
314
+ * On the state of a story's editor: the body it belongs to.
315
+ * @internal
316
+ */
317
+ export const storyBodyKey = new PluginKey<StoryBody>("storyBody");
318
+
319
+ /**
320
+ * The body of the story `state` edits; null when `state` is the body's.
321
+ * @internal
322
+ */
323
+ export const storyBody = (state: EditorState): StoryBody | null =>
324
+ storyBodyKey.getState(state) ?? null;
325
+
326
+ /**
327
+ * A state over a story's `doc` for working on it, with `state`'s word context (and `envelope`).
328
+ * @internal
329
+ */
330
+ export function storyState(state: EditorState, doc: Node, envelope?: WordEnvelope): EditorState {
331
+ const context = wordContext(state) as WordContext;
332
+ const given = envelope ? { ...context, envelope } : context;
333
+ return EditorState.create({ doc, plugins: [wordPlugin(given.envelope, given)] });
334
+ }
335
+
336
+ /**
337
+ * Every tree of the document but `state`'s own: ids are allocated across them (ADR-0007).
338
+ * @internal
339
+ */
340
+ export function otherTrees(state: EditorState): Node[] {
341
+ const trees = [...(wordContext(state)?.stories.values() ?? [])].map((story) => story.doc);
342
+ const body = storyBody(state)?.state.doc;
343
+ return (body ? [body, ...trees] : trees).filter((tree) => tree !== state.doc);
344
+ }
345
+
346
+ /** The trees of `envelope`'s stories, in the editor's `schema`. */
347
+ function storyTrees(envelope: WordEnvelope, schema: Schema): Map<string, Story> {
348
+ return new Map(
349
+ Object.entries(envelope.stories ?? {}).map(([id, { content, ...rest }]) => [
350
+ id,
351
+ {
352
+ ...rest,
353
+ doc: parseContent(content, { path: `stories[${JSON.stringify(id)}].content`, schema }),
354
+ },
355
+ ]),
356
+ );
357
+ }
358
+
111
359
  /**
112
360
  * The plugin that gives commands a v3 document's envelope. A transaction changes it with
113
361
  * `withEnvelope` (a new list's numbering definition); undo leaves such additions, as Word does.
114
- * Comments change only through `CommentStep`s, which undo takes back.
362
+ * Comments change only through `CommentStep`s, the last section, the settings and the controls'
363
+ * properties through `EnvelopeStep`s and stories through `StoryStep`s, which undo takes back (a
364
+ * paste adds the properties of the controls it copies with `withEnvelope`). A story's editor is
365
+ * given its body's `context` as it stands.
366
+ * @internal
115
367
  */
116
- export function wordPlugin(envelope: WordEnvelope): Plugin<WordContext> {
368
+ export function wordPlugin(envelope: WordEnvelope, context?: WordContext): Plugin<WordContext> {
117
369
  // Not a secret: only tells documents apart.
118
370
  const id = `${Date.now().toString(36)}${Math.random().toString(36).slice(2)}`;
119
- const context: WordContext = { envelope, resolver: new Resolver(envelope), id };
120
371
  return new Plugin({
121
372
  key,
122
373
  state: {
123
- init: () => context,
374
+ init: (state) =>
375
+ context ?? {
376
+ envelope,
377
+ resolver: new Resolver(envelope),
378
+ stories: storyTrees(envelope, state.schema),
379
+ id,
380
+ },
124
381
  apply: (tr, value) => {
382
+ const adopted = tr.getMeta(ADOPT) as WordContext | undefined;
383
+ if (adopted) return adopted;
125
384
  const next = tr.getMeta(key) as WordEnvelope | undefined;
126
385
  const comments = applyCommentSteps(value.envelope.comments, tr.steps);
127
- if (!next && comments === value.envelope.comments) return value;
128
- const envelope = { ...(next ?? value.envelope), comments: comments as CommentJSON[] };
129
- // Only comments changed: they do not affect formatting, the resolver stays.
386
+ const base = next ?? value.envelope;
387
+ const section = afterEnvelopeSteps("section", base.section, tr.steps);
388
+ const settings = afterEnvelopeSteps("settings", value.envelope.settings, tr.steps);
389
+ const controls = afterEnvelopeSteps("controls", base.controls, tr.steps);
390
+ let { stories } = value;
391
+ let storiesJSON = value.envelope.stories;
392
+ for (const step of tr.steps) {
393
+ if (!(step instanceof StoryStep)) continue;
394
+ const trees = new Map(stories);
395
+ const json = { ...storiesJSON };
396
+ if (step.after) {
397
+ trees.set(step.id, step.after);
398
+ json[step.id] = storyJSON(step.after);
399
+ } else {
400
+ trees.delete(step.id);
401
+ delete json[step.id];
402
+ }
403
+ stories = trees;
404
+ storiesJSON = json;
405
+ }
406
+ if (
407
+ !next &&
408
+ comments === value.envelope.comments &&
409
+ section === base.section &&
410
+ settings === value.envelope.settings &&
411
+ controls === value.envelope.controls &&
412
+ stories === value.stories
413
+ )
414
+ return value;
415
+ const { stories: _stories, settings: _settings, controls: _controls, ...rest } = base;
416
+ const envelope: WordEnvelope = {
417
+ ...rest,
418
+ section,
419
+ comments: comments as CommentJSON[],
420
+ ...(storiesJSON && Object.keys(storiesJSON).length ? { stories: storiesJSON } : {}),
421
+ ...(settings ? { settings } : {}),
422
+ ...(controls ? { controls } : {}),
423
+ };
424
+ // Only comments, stories or steps changed: they do not affect formatting.
130
425
  const resolver = next ? new Resolver(envelope) : value.resolver;
131
- return { envelope, resolver, id: value.id };
426
+ return { envelope, resolver, stories, id: value.id };
132
427
  },
133
428
  },
134
429
  });
@@ -142,6 +437,22 @@ export function withEnvelope(tr: Transaction, envelope: WordEnvelope): Transacti
142
437
  return tr.setMeta(key, envelope);
143
438
  }
144
439
 
440
+ const ADOPT = "adoptWordContext";
441
+
442
+ /**
443
+ * Makes `tr` give a story's state the body's context as it now is.
444
+ * @internal
445
+ */
446
+ export const withContext = (tr: Transaction, context: WordContext): Transaction =>
447
+ tr.setMeta(ADOPT, context);
448
+
449
+ /**
450
+ * The envelope `tr` gives the document through `withEnvelope`, if any.
451
+ * @internal
452
+ */
453
+ export const envelopeChange = (tr: Transaction): WordEnvelope | undefined =>
454
+ tr.getMeta(key) as WordEnvelope | undefined;
455
+
145
456
  /** The envelope and resolver of the v3 document in `state`, or null for a v2 editor. */
146
457
  export function wordContext(state: EditorState): WordContext | null {
147
458
  return key.getState(state) ?? null;
@@ -166,6 +477,7 @@ function defaultParagraphStyle(envelope: WordEnvelope): string | undefined {
166
477
  * with the same properties; the second keeps the original mark. At the end of a paragraph whose
167
478
  * style names another as the next (a heading: Normal), the second paragraph is in that style,
168
479
  * without the first's direct formatting. `before` is null when the first keeps every attribute.
480
+ * @internal
169
481
  */
170
482
  export function splitAttrs(
171
483
  context: WordContext,
@@ -181,7 +493,8 @@ export function splitAttrs(
181
493
  const next = style?.next;
182
494
  if (next === undefined || next === id) return { before, after: attrs };
183
495
  const mark = Object.fromEntries(Object.keys(MARK_ONLY).map((k) => [k, attrs[k] ?? null]));
184
- return { before, after: { ...mark, style: next } };
496
+ // The new paragraph is in the same content controls.
497
+ return { before, after: { ...mark, controls: attrs.controls, style: next } };
185
498
  }
186
499
 
187
500
  /** Run toggles: whether text shows the property, from its resolved formatting. */
@@ -196,7 +509,10 @@ const TOGGLES: Record<string, (run: RunFormat) => boolean> = {
196
509
  hidden: (run) => run.hidden,
197
510
  };
198
511
 
199
- /** Whether `type` is a run property the toggles handle on a v3 document. */
512
+ /**
513
+ * Whether `type` is a run property the toggles handle on a v3 document.
514
+ * @internal
515
+ */
200
516
  export function isWordToggle(context: WordContext | null, type: MarkType): boolean {
201
517
  return context !== null && Object.hasOwn(TOGGLES, type.name);
202
518
  }
@@ -243,7 +559,10 @@ function inlineIn(state: EditorState, from: number, to: number, type: MarkType)
243
559
  return text.length ? { all: found, judged: text } : { all: found, judged: found };
244
560
  }
245
561
 
246
- /** Whether all the selected text (or the text typed at a caret) shows the toggle `type`. */
562
+ /**
563
+ * Whether all the selected text (or the text typed at a caret) shows the toggle `type`.
564
+ * @internal
565
+ */
247
566
  export function wordToggleActive(
248
567
  context: WordContext,
249
568
  state: EditorState,
@@ -259,6 +578,7 @@ export function wordToggleActive(
259
578
  /**
260
579
  * The stored marks for a toggle at a caret: what makes the next typed text show the opposite of
261
580
  * what text typed there would show now.
581
+ * @internal
262
582
  */
263
583
  export function toggledCaretMarks(
264
584
  context: WordContext,
@@ -276,6 +596,7 @@ export function toggledCaretMarks(
276
596
  /**
277
597
  * The mark changes that make every inline node in `from..to` show the toggle `type` as `on`:
278
598
  * per node, a direct mark where its style does not already give that, none where it does.
599
+ * @internal
279
600
  */
280
601
  export function toggleRanges(
281
602
  context: WordContext,
@@ -303,25 +624,40 @@ const MARKERS = new Set([
303
624
  "bookmarkEnd",
304
625
  "commentStart",
305
626
  "commentEnd",
627
+ "controlStart",
628
+ "controlEnd",
629
+ // A field's parts and a note's own number: deleting one alone would break what it belongs to.
630
+ "fieldStart",
631
+ "fieldCode",
632
+ "fieldSeparator",
633
+ "fieldEnd",
634
+ "noteMark",
306
635
  // A comment's reference mark: deleting it would take the comment's anchor.
307
636
  "commentReference",
308
637
  ]);
309
638
 
310
- /** Whether `node` is an invisible marker of a v3 document. */
639
+ /**
640
+ * Whether `node` is an invisible marker of a v3 document.
641
+ * @internal
642
+ */
311
643
  export function isMarker(context: WordContext | null, node: Node | null | undefined): boolean {
312
644
  return context !== null && !!node && MARKERS.has(node.type.name);
313
645
  }
314
646
 
315
- /** Opaque content: deleted only once selected, as Word selects a field before deleting it. */
647
+ const OPAQUE = new Set(["opaqueInline", "opaqueBlock", "foreignInline", "foreignBlock"]);
648
+
649
+ /**
650
+ * Opaque content (and foreign nodes, ADR-0008): deleted only once selected, as Word selects a field before deleting it.
651
+ * @internal
652
+ */
316
653
  export function isOpaque(context: WordContext | null, node: Node | null | undefined): boolean {
317
- return (
318
- context !== null &&
319
- !!node &&
320
- (node.type.name === "opaqueInline" || node.type.name === "opaqueBlock")
321
- );
654
+ return context !== null && !!node && OPAQUE.has(node.type.name);
322
655
  }
323
656
 
324
- /** A node selection of the node at `pos`, or a text selection when it cannot be selected. */
657
+ /**
658
+ * A node selection of the node at `pos`, or a text selection when it cannot be selected.
659
+ * @internal
660
+ */
325
661
  export function selectionAt(state: EditorState, pos: number): NodeSelection | TextSelection {
326
662
  const node = state.doc.nodeAt(pos);
327
663
  return node && NodeSelection.isSelectable(node)