@folijs/core 0.4.0 → 0.6.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 (153) hide show
  1. package/CHANGELOG.md +49 -0
  2. package/dist/model/schema.d.ts +9 -1
  3. package/dist/model/schema.d.ts.map +1 -1
  4. package/dist/model/schema.js +13 -3
  5. package/dist/model/schema.js.map +1 -1
  6. package/dist/state/commands.d.ts +10 -1
  7. package/dist/state/commands.d.ts.map +1 -1
  8. package/dist/state/commands.js +125 -40
  9. package/dist/state/commands.js.map +1 -1
  10. package/dist/state/comments.d.ts.map +1 -1
  11. package/dist/state/comments.js +15 -6
  12. package/dist/state/comments.js.map +1 -1
  13. package/dist/state/controls.d.ts +128 -0
  14. package/dist/state/controls.d.ts.map +1 -0
  15. package/dist/state/controls.js +1459 -0
  16. package/dist/state/controls.js.map +1 -0
  17. package/dist/state/editor.d.ts +86 -1
  18. package/dist/state/editor.d.ts.map +1 -1
  19. package/dist/state/editor.js +169 -41
  20. package/dist/state/editor.js.map +1 -1
  21. package/dist/state/fields.d.ts +79 -0
  22. package/dist/state/fields.d.ts.map +1 -0
  23. package/dist/state/fields.js +398 -0
  24. package/dist/state/fields.js.map +1 -0
  25. package/dist/state/fieldupdate.d.ts +178 -0
  26. package/dist/state/fieldupdate.d.ts.map +1 -0
  27. package/dist/state/fieldupdate.js +1574 -0
  28. package/dist/state/fieldupdate.js.map +1 -0
  29. package/dist/state/find.d.ts +68 -0
  30. package/dist/state/find.d.ts.map +1 -0
  31. package/dist/state/find.js +397 -0
  32. package/dist/state/find.js.map +1 -0
  33. package/dist/state/history.d.ts +12 -1
  34. package/dist/state/history.d.ts.map +1 -1
  35. package/dist/state/history.js +43 -7
  36. package/dist/state/history.js.map +1 -1
  37. package/dist/state/index.d.ts +15 -5
  38. package/dist/state/index.d.ts.map +1 -1
  39. package/dist/state/index.js +13 -3
  40. package/dist/state/index.js.map +1 -1
  41. package/dist/state/links.d.ts.map +1 -1
  42. package/dist/state/links.js +30 -19
  43. package/dist/state/links.js.map +1 -1
  44. package/dist/state/mark.d.ts.map +1 -1
  45. package/dist/state/mark.js +8 -1
  46. package/dist/state/mark.js.map +1 -1
  47. package/dist/state/navigation.d.ts +70 -0
  48. package/dist/state/navigation.d.ts.map +1 -0
  49. package/dist/state/navigation.js +226 -0
  50. package/dist/state/navigation.js.map +1 -0
  51. package/dist/state/notes.d.ts +94 -0
  52. package/dist/state/notes.d.ts.map +1 -0
  53. package/dist/state/notes.js +444 -0
  54. package/dist/state/notes.js.map +1 -0
  55. package/dist/state/pictures.d.ts +51 -0
  56. package/dist/state/pictures.d.ts.map +1 -0
  57. package/dist/state/pictures.js +140 -0
  58. package/dist/state/pictures.js.map +1 -0
  59. package/dist/state/review.d.ts +13 -4
  60. package/dist/state/review.d.ts.map +1 -1
  61. package/dist/state/review.js +223 -29
  62. package/dist/state/review.js.map +1 -1
  63. package/dist/state/sections.d.ts +101 -0
  64. package/dist/state/sections.d.ts.map +1 -0
  65. package/dist/state/sections.js +472 -0
  66. package/dist/state/sections.js.map +1 -0
  67. package/dist/state/selection.d.ts +12 -1
  68. package/dist/state/selection.d.ts.map +1 -1
  69. package/dist/state/selection.js +12 -0
  70. package/dist/state/selection.js.map +1 -1
  71. package/dist/state/state.d.ts +2 -0
  72. package/dist/state/state.d.ts.map +1 -1
  73. package/dist/state/state.js +4 -0
  74. package/dist/state/state.js.map +1 -1
  75. package/dist/state/stories.d.ts +41 -0
  76. package/dist/state/stories.d.ts.map +1 -0
  77. package/dist/state/stories.js +243 -0
  78. package/dist/state/stories.js.map +1 -0
  79. package/dist/state/tablecommands.d.ts +77 -0
  80. package/dist/state/tablecommands.d.ts.map +1 -0
  81. package/dist/state/tablecommands.js +675 -0
  82. package/dist/state/tablecommands.js.map +1 -0
  83. package/dist/state/tables.d.ts +110 -0
  84. package/dist/state/tables.d.ts.map +1 -0
  85. package/dist/state/tables.js +730 -0
  86. package/dist/state/tables.js.map +1 -0
  87. package/dist/state/track.d.ts +23 -0
  88. package/dist/state/track.d.ts.map +1 -1
  89. package/dist/state/track.js +113 -15
  90. package/dist/state/track.js.map +1 -1
  91. package/dist/state/word.d.ts +78 -16
  92. package/dist/state/word.d.ts.map +1 -1
  93. package/dist/state/word.js +231 -11
  94. package/dist/state/word.js.map +1 -1
  95. package/dist/state/wordclip.d.ts +15 -6
  96. package/dist/state/wordclip.d.ts.map +1 -1
  97. package/dist/state/wordclip.js +213 -18
  98. package/dist/state/wordclip.js.map +1 -1
  99. package/dist/state/wordformat.d.ts +10 -2
  100. package/dist/state/wordformat.d.ts.map +1 -1
  101. package/dist/state/wordformat.js +98 -17
  102. package/dist/state/wordformat.js.map +1 -1
  103. package/dist/v3/document.d.ts +237 -7
  104. package/dist/v3/document.d.ts.map +1 -1
  105. package/dist/v3/document.js +314 -15
  106. package/dist/v3/document.js.map +1 -1
  107. package/dist/v3/index.d.ts +2 -2
  108. package/dist/v3/index.d.ts.map +1 -1
  109. package/dist/v3/index.js +2 -2
  110. package/dist/v3/index.js.map +1 -1
  111. package/dist/v3/migrate.d.ts +2 -2
  112. package/dist/v3/migrate.d.ts.map +1 -1
  113. package/dist/v3/resolve.d.ts +57 -5
  114. package/dist/v3/resolve.d.ts.map +1 -1
  115. package/dist/v3/resolve.js +224 -16
  116. package/dist/v3/resolve.js.map +1 -1
  117. package/dist/v3/schema.d.ts +6 -0
  118. package/dist/v3/schema.d.ts.map +1 -1
  119. package/dist/v3/schema.js +254 -8
  120. package/dist/v3/schema.js.map +1 -1
  121. package/package.json +1 -1
  122. package/src/model/schema.ts +18 -2
  123. package/src/state/commands.ts +121 -32
  124. package/src/state/comments.ts +14 -6
  125. package/src/state/controls.ts +1596 -0
  126. package/src/state/editor.ts +292 -46
  127. package/src/state/fields.ts +432 -0
  128. package/src/state/fieldupdate.ts +1770 -0
  129. package/src/state/find.ts +510 -0
  130. package/src/state/history.ts +51 -7
  131. package/src/state/index.ts +157 -0
  132. package/src/state/links.ts +27 -17
  133. package/src/state/mark.ts +8 -1
  134. package/src/state/navigation.ts +305 -0
  135. package/src/state/notes.ts +519 -0
  136. package/src/state/numeric-pictures.word.json +172 -0
  137. package/src/state/pictures.ts +168 -0
  138. package/src/state/review.ts +265 -31
  139. package/src/state/sections.ts +571 -0
  140. package/src/state/selection.ts +15 -1
  141. package/src/state/state.ts +5 -0
  142. package/src/state/stories.ts +285 -0
  143. package/src/state/tablecommands.ts +741 -0
  144. package/src/state/tables.ts +761 -0
  145. package/src/state/track.ts +128 -16
  146. package/src/state/word.ts +276 -13
  147. package/src/state/wordclip.ts +231 -32
  148. package/src/state/wordformat.ts +96 -14
  149. package/src/v3/document.ts +572 -16
  150. package/src/v3/index.ts +21 -0
  151. package/src/v3/migrate.ts +2 -2
  152. package/src/v3/resolve.ts +289 -21
  153. package/src/v3/schema.ts +278 -9
@@ -10,12 +10,27 @@
10
10
  * together. These edits are not tracked themselves, and undo takes them back.
11
11
  */
12
12
  import type { Attrs, Mark, Node } from "../model/index.js";
13
+ import { TransformError } from "../transform/transform.js";
14
+ import type { SectionJSON } from "../v3/document.js";
13
15
  import { RUN_PROPERTY_MARKS } from "../v3/schema.js";
16
+ import { controlEdit, controlsPlugin, readOnlyAt } from "./controls.js";
14
17
  import { closeHistory } from "./history.js";
15
18
  import { MARK_ATTRS } from "./mark.js";
19
+ import { absorbSection, lastSection, resolveSectionChange } from "./sections.js";
16
20
  import { TextSelection } from "./selection.js";
17
- import type { Command, EditorState, Transaction } from "./state.js";
21
+ import { type Command, EditorState, type Transaction } from "./state.js";
22
+ import { keepTablesApart } from "./tables.js";
18
23
  import { RECORDED, trackState, untracked } from "./track.js";
24
+ import {
25
+ StoryStep,
26
+ setEnvelopeValue,
27
+ storyAfter,
28
+ storyBody,
29
+ storyState,
30
+ type WordEnvelope,
31
+ withEnvelope,
32
+ wordContext,
33
+ } from "./word.js";
19
34
 
20
35
  export type ChangeKind =
21
36
  | "insertion"
@@ -23,7 +38,10 @@ export type ChangeKind =
23
38
  | "formatting"
24
39
  | "paragraphInsertion"
25
40
  | "paragraphDeletion"
26
- | "paragraphFormatting";
41
+ | "paragraphFormatting"
42
+ | "sectionFormatting"
43
+ | "rowInsertion"
44
+ | "rowDeletion";
27
45
 
28
46
  /** One tracked change of the document, where it is. */
29
47
  export interface Change {
@@ -56,9 +74,23 @@ interface Revision {
56
74
  date?: string | null;
57
75
  }
58
76
 
59
- /** Every tracked change of `doc`, in document order. */
60
- export function trackedChanges(doc: Node): Change[] {
77
+ /**
78
+ * Every tracked change of `doc`, in document order; with `last` (the envelope's section) the
79
+ * change of the last section's setup too, at the document's last paragraph mark.
80
+ */
81
+ export function trackedChanges(doc: Node, last?: SectionJSON): Change[] {
61
82
  const changes: Change[] = [];
83
+ const end = doc.content.size;
84
+ if (last?.change && doc.lastChild?.isTextblock)
85
+ changes.push({
86
+ kind: "sectionFormatting",
87
+ id: last.change.id,
88
+ author: last.change.author,
89
+ date: last.change.date ?? null,
90
+ from: end - 1,
91
+ to: end,
92
+ move: null,
93
+ });
62
94
  const flush = (run: { mark: Mark; from: number; to: number }) => {
63
95
  const attrs = run.mark.attrs;
64
96
  changes.push({
@@ -71,10 +103,31 @@ export function trackedChanges(doc: Node): Change[] {
71
103
  move: (attrs.move as string | null | undefined) ?? null,
72
104
  });
73
105
  };
74
- doc.forEach((block, offset) => {
75
- if (!block.isTextblock) return;
106
+ // Every paragraph, those in table cells too; a table row's own insertion or deletion.
107
+ doc.descendants((block, offset) => {
108
+ if (block.type.name === "tableRow") {
109
+ const revision = block.attrs.revision as { insertion?: Revision; deletion?: Revision } | null;
110
+ for (const [kind, r] of [
111
+ ["rowInsertion", revision?.insertion],
112
+ ["rowDeletion", revision?.deletion],
113
+ ] as const)
114
+ if (r)
115
+ changes.push({
116
+ kind,
117
+ id: r.id,
118
+ author: r.author,
119
+ date: r.date ?? null,
120
+ from: offset,
121
+ to: offset + block.nodeSize,
122
+ move: null,
123
+ });
124
+ return true;
125
+ }
126
+ if (!block.isTextblock) return true;
76
127
  const change = block.attrs.formatChange as Revision | null;
77
128
  if (change) changes.push(paragraphChange("paragraphFormatting", change, offset, block));
129
+ const setup = (block.attrs.section as SectionJSON | null | undefined)?.change;
130
+ if (setup) changes.push(paragraphChange("sectionFormatting", setup, offset, block));
78
131
  // Runs of content with the same change mark, extended while the next node has it too.
79
132
  let open: { mark: Mark; from: number; to: number }[] = [];
80
133
  block.forEach((inline, at) => {
@@ -95,6 +148,7 @@ export function trackedChanges(doc: Node): Change[] {
95
148
  changes.push(paragraphChange("paragraphInsertion", revision.insertion, offset, block));
96
149
  if (revision?.deletion)
97
150
  changes.push(paragraphChange("paragraphDeletion", revision.deletion, offset, block));
151
+ return false;
98
152
  });
99
153
  return changes.sort((a, b) => a.from - b.from || a.to - b.to);
100
154
  }
@@ -119,19 +173,34 @@ function paragraphChange(
119
173
  }
120
174
  const INLINE: ReadonlySet<ChangeKind> = new Set(["insertion", "deletion", "formatting"]);
121
175
  /** Inline nodes no change marks: they stay when the text around them goes. */
122
- const MARKERS = new Set(["bookmarkStart", "bookmarkEnd", "commentStart", "commentEnd"]);
176
+ const MARKERS = new Set([
177
+ "bookmarkStart",
178
+ "bookmarkEnd",
179
+ "commentStart",
180
+ "commentEnd",
181
+ "controlStart",
182
+ "controlEnd",
183
+ ]);
123
184
 
124
185
  /**
125
186
  * The changes `from..to` touches, with their moves' other ends. At a caret: the ones around it,
126
187
  * the paragraph's formatting only when nothing else is. In a selection, changes to content only
127
188
  * as far as the selection goes (Word accepts the selected part).
128
189
  */
129
- function changesIn(doc: Node, from: number, to: number, filter: ChangeFilter = {}): Change[] {
130
- const all = trackedChanges(doc).filter((c) => !filter.author || c.author === filter.author);
190
+ function changesIn(
191
+ doc: Node,
192
+ last: SectionJSON | undefined,
193
+ from: number,
194
+ to: number,
195
+ filter: ChangeFilter = {},
196
+ ): Change[] {
197
+ const all = trackedChanges(doc, last).filter((c) => !filter.author || c.author === filter.author);
131
198
  let picked: Change[];
132
199
  if (from === to) {
133
200
  const around = all.filter((c) => c.from <= from && from <= c.to);
134
- const own = around.filter((c) => c.kind !== "paragraphFormatting");
201
+ const own = around.filter(
202
+ (c) => c.kind !== "paragraphFormatting" && c.kind !== "sectionFormatting",
203
+ );
135
204
  picked = own.length ? own : around;
136
205
  } else
137
206
  picked = all
@@ -150,11 +219,25 @@ function changesIn(doc: Node, from: number, to: number, filter: ChangeFilter = {
150
219
  }
151
220
 
152
221
  /** Applies accepting (or rejecting) `changes` to `tr`. */
153
- function resolve(tr: Transaction, changes: readonly Change[], accept: boolean): void {
222
+ function resolve(
223
+ state: EditorState,
224
+ tr: Transaction,
225
+ changes: readonly Change[],
226
+ accept: boolean,
227
+ ): void {
154
228
  const schema = tr.doc.type.schema;
155
229
  const removed: [number, number][] = [];
156
230
  const joins: { pos: number; keepFirst: boolean }[] = [];
157
- for (const change of changes) {
231
+ /** Rows that go, by their table (its position): a table losing every row goes whole. */
232
+ const rowsGoing = new Map<number, [number, number][]>();
233
+ /** Whether whole rows or paragraphs go: what lies inside them must not be removed again. */
234
+ let wholes = false;
235
+ // Setups first: a section break that goes then hands the next section what is restored.
236
+ const ordered = [
237
+ ...changes.filter((c) => c.kind === "sectionFormatting"),
238
+ ...changes.filter((c) => c.kind !== "sectionFormatting"),
239
+ ];
240
+ for (const change of ordered) {
158
241
  const { kind, from, to } = change;
159
242
  const markType = schema.marks[kind === "formatting" ? "formatChange" : kind];
160
243
  switch (kind) {
@@ -196,6 +279,39 @@ function resolve(tr: Transaction, changes: readonly Change[], accept: boolean):
196
279
  tr.setNodeMarkup(start, node.type, attrs);
197
280
  break;
198
281
  }
282
+ case "sectionFormatting": {
283
+ const start = tr.doc.resolve(from).before();
284
+ const node = tr.doc.nodeAt(start);
285
+ const own = node?.attrs.section as SectionJSON | null | undefined;
286
+ if (node && own?.change?.id === change.id)
287
+ tr.setNodeMarkup(start, node.type, {
288
+ ...node.attrs,
289
+ section: resolveSectionChange(own, accept),
290
+ });
291
+ else
292
+ setEnvelopeValue(
293
+ state,
294
+ tr,
295
+ "section",
296
+ resolveSectionChange(lastSection(state, tr), accept),
297
+ );
298
+ break;
299
+ }
300
+ case "rowInsertion":
301
+ case "rowDeletion": {
302
+ const row = tr.doc.nodeAt(from);
303
+ if (row?.type.name !== "tableRow") break;
304
+ if ((kind === "rowDeletion") === accept) {
305
+ const table = tr.doc.resolve(from).before();
306
+ rowsGoing.set(table, [...(rowsGoing.get(table) ?? []), [from, to]]);
307
+ } else {
308
+ const revision = { ...(row.attrs.revision as Record<string, unknown>) };
309
+ delete revision[kind === "rowInsertion" ? "insertion" : "deletion"];
310
+ const next = Object.keys(revision).length ? revision : null;
311
+ tr.setNodeMarkup(from, row.type, { ...row.attrs, revision: next });
312
+ }
313
+ break;
314
+ }
199
315
  case "paragraphInsertion":
200
316
  case "paragraphDeletion": {
201
317
  const start = tr.doc.resolve(from).before();
@@ -210,18 +326,56 @@ function resolve(tr: Transaction, changes: readonly Change[], accept: boolean):
210
326
  const end = start + node.nodeSize;
211
327
  // A paragraph whose text goes too is removed whole: the next one stays as it is.
212
328
  const emptied = covered(node, start + 1, removed);
213
- if (goes && tr.doc.resolve(end).nodeAfter?.isTextblock)
214
- joins.push({ pos: end, keepFirst: accept && !emptied });
329
+ const joining = goes && !!tr.doc.resolve(end).nodeAfter?.isTextblock;
330
+ // A section break that goes: the next section takes over. Without a paragraph to join
331
+ // (a table follows) the mark stays, no longer ending a section.
332
+ const section = node.attrs.section as SectionJSON | null | undefined;
333
+ if (goes && section) {
334
+ absorbSection(state, tr, section, end);
335
+ if (!joining)
336
+ tr.setNodeMarkup(start, node.type, { ...node.attrs, revision: next, section: null });
337
+ }
338
+ if (joining) joins.push({ pos: end, keepFirst: accept && !emptied });
339
+ // Nothing to join with (a table follows, or a cell ends): an inserted paragraph left
340
+ // empty goes whole when rejected (Word's rules are restored after: `keepTablesApart`).
341
+ // (Never a cell's only paragraph: a cell ends with one.)
342
+ else if (
343
+ goes &&
344
+ !accept &&
345
+ (!node.content.size || emptied) &&
346
+ tr.doc.resolve(start).parent.childCount > 1
347
+ ) {
348
+ removed.push([start, end]);
349
+ wholes = true;
350
+ }
215
351
  break;
216
352
  }
217
353
  }
218
354
  }
355
+ // Rows that go, or their table when every row does; what lies inside them is not removed again.
356
+ for (const [table, rows] of rowsGoing) {
357
+ const node = tr.doc.nodeAt(table);
358
+ if (node && rows.length === node.childCount) removed.push([table, table + node.nodeSize]);
359
+ else removed.push(...rows);
360
+ }
361
+ if (rowsGoing.size || wholes) {
362
+ const outer = removed.filter(
363
+ ([s, e]) => !removed.some(([s2, e2]) => (s2 < s && e <= e2) || (s2 <= s && e < e2)),
364
+ );
365
+ removed.length = 0;
366
+ removed.push(...outer);
367
+ }
219
368
  // Content removed and paragraphs joined, from the end so earlier positions stay.
220
369
  const ops = [
221
370
  ...removed.map(([s, e]) => ({ at: s, run: () => tr.delete(s, e) })),
222
- ...joins.map(({ pos, keepFirst }) => ({ at: pos - 1, run: () => join(tr, pos, keepFirst) })),
371
+ ...joins
372
+ .filter(
373
+ ({ pos }) => !(rowsGoing.size || wholes) || !removed.some(([s, e]) => s < pos && pos < e),
374
+ )
375
+ .map(({ pos, keepFirst }) => ({ at: pos - 1, run: () => join(tr, pos, keepFirst) })),
223
376
  ].sort((a, b) => b.at - a.at);
224
377
  for (const op of ops) op.run();
378
+ keepTablesApart(tr);
225
379
  }
226
380
 
227
381
  /** Whether every child of `node` (content at `start`) but markers lies in `removed`. */
@@ -264,32 +418,109 @@ function join(tr: Transaction, pos: number, keepFirst: boolean): void {
264
418
  tr.setNodeMarkup(start, first.type, attrs);
265
419
  }
266
420
 
421
+ /**
422
+ * Accepts or rejects every insertion or deletion recorded in story `id`, in `tr`: a note's, when
423
+ * the reference's own record is settled and the note stays.
424
+ */
425
+ export function resolveStoryChanges(
426
+ state: EditorState,
427
+ tr: Transaction,
428
+ id: string,
429
+ accept: boolean,
430
+ kind: "insertion" | "deletion",
431
+ ): void {
432
+ const story = storyAfter(state, tr, id);
433
+ if (!story) return;
434
+ const paragraph = kind === "insertion" ? "paragraphInsertion" : "paragraphDeletion";
435
+ const changes = changesIn(story.doc, undefined, 0, story.doc.content.size).filter(
436
+ (c) => c.kind === kind || c.kind === paragraph,
437
+ );
438
+ if (!changes.length) return;
439
+ const inner = storyState(state, story.doc);
440
+ const storyTr = inner.tr;
441
+ resolve(inner, storyTr, changes, accept);
442
+ tr.step(new StoryStep(id, story, { ...story, doc: storyTr.doc }));
443
+ }
444
+
267
445
  function command(
268
446
  accept: boolean,
269
- range: (state: EditorState) => [number, number] | null,
447
+ range: (state: EditorState) => readonly { from: number; to: number }[],
270
448
  filter?: ChangeFilter,
449
+ /** Every story too: accepting or rejecting all changes of the document. */
450
+ everywhere = false,
271
451
  ): Command {
272
- return (state, dispatch) => {
452
+ const run: Command = (state, dispatch) => {
273
453
  if (!state.schema.marks.insertion) return false;
274
- const r = range(state);
275
- if (!r) return false;
276
- const changes = changesIn(state.doc, r[0], r[1], filter);
277
- if (!changes.length) return false;
454
+ const body = everywhere ? storyBody(state) : null;
455
+ if (body) return dispatch ? body.run(run) : body.can(run);
456
+ const controls = wordContext(state)?.envelope.controls;
457
+ // As Word, in content that may not be edited: what leaves its text as it shows goes through
458
+ // (accepting an insertion, rejecting a deletion); the opposite is refused.
459
+ const open = (doc: Node) => (c: Change) =>
460
+ c.kind.toLowerCase().includes("deletion") !== accept ||
461
+ !readOnlyAt(doc, controls, c.from, c.to);
462
+ // A change around several selected cells (a row revision, a move's partner) counts once.
463
+ const seen = new Set<string>();
464
+ const changes = range(state)
465
+ .flatMap((r) =>
466
+ changesIn(state.doc, wordContext(state)?.envelope.section, r.from, r.to, filter),
467
+ )
468
+ .filter(open(state.doc))
469
+ .filter((c) => {
470
+ const key = `${c.kind} ${c.id} ${c.from} ${c.to}`;
471
+ return !seen.has(key) && !!seen.add(key);
472
+ });
473
+ const stories = everywhere
474
+ ? [...(wordContext(state)?.stories ?? [])].flatMap(([id, story]) => {
475
+ const inStory = changesIn(story.doc, undefined, 0, story.doc.content.size, filter).filter(
476
+ open(story.doc),
477
+ );
478
+ return inStory.length ? [{ id, story, changes: inStory }] : [];
479
+ })
480
+ : [];
481
+ if (!changes.length && !stories.length) return false;
278
482
  if (dispatch) {
279
483
  // Its own undo step, never merged with the typing before it.
280
484
  const tr = state.tr.setMeta(untracked, true).setMeta(closeHistory, true);
281
- resolve(tr, changes, accept);
485
+ const edit = accept ? "accept" : "reject";
486
+ tr.setMeta(controlEdit, edit);
487
+ let envelope = wordContext(state)?.envelope;
488
+ const base = envelope;
489
+ // A document the result would not fit (a structure Word allows and Foli cannot change
490
+ // yet): nothing is changed.
491
+ try {
492
+ if (changes.length) resolve(state, tr, changes, accept);
493
+ for (const { id, story, changes: inStory } of stories) {
494
+ // With the controls' rules, as in the story's own editor.
495
+ const inner = EditorState.create({
496
+ doc: story.doc,
497
+ plugins: [...storyState(state, story.doc, envelope).plugins, controlsPlugin()],
498
+ });
499
+ const storyTr = inner.tr.setMeta(controlEdit, edit);
500
+ resolve(inner, storyTr, inStory, accept);
501
+ const done = inner.applyTransaction(storyTr).state;
502
+ envelope = wordContext(done)?.envelope;
503
+ tr.step(new StoryStep(id, story, { ...story, doc: done.doc }));
504
+ }
505
+ if (envelope && envelope !== base) {
506
+ if (envelope.styles !== base?.styles)
507
+ withEnvelope(tr, { ...(base as WordEnvelope), styles: envelope.styles });
508
+ if (envelope.controls !== base?.controls)
509
+ setEnvelopeValue(state, tr, "controls", envelope.controls);
510
+ }
511
+ } catch (error) {
512
+ if (error instanceof TransformError) return false;
513
+ throw error;
514
+ }
282
515
  dispatch(tr);
283
516
  }
284
517
  return true;
285
518
  };
519
+ return run;
286
520
  }
287
521
 
288
- const selected = (state: EditorState): [number, number] => [
289
- state.selection.from,
290
- state.selection.to,
291
- ];
292
- const everything = (state: EditorState): [number, number] => [0, state.doc.content.size];
522
+ const selected = (state: EditorState) => state.selection.ranges;
523
+ const everything = (state: EditorState) => [{ from: 0, to: state.doc.content.size }];
293
524
 
294
525
  /** Accepts the change at the caret, or the changes in the selection. */
295
526
  export const acceptChange: Command = command(true, selected);
@@ -297,17 +528,17 @@ export const acceptChange: Command = command(true, selected);
297
528
  export const rejectChange: Command = command(false, selected);
298
529
  /** Accepts every change (of `author`, if given). */
299
530
  export const acceptAllChanges = (author?: string): Command =>
300
- command(true, everything, author === undefined ? {} : { author });
531
+ command(true, everything, author === undefined ? {} : { author }, true);
301
532
  /** Rejects every change (of `author`, if given). */
302
533
  export const rejectAllChanges = (author?: string): Command =>
303
- command(false, everything, author === undefined ? {} : { author });
534
+ command(false, everything, author === undefined ? {} : { author }, true);
304
535
 
305
536
  /** Selects the next (or previous) change after (before) the selection. */
306
537
  function goTo(forward: boolean): Command {
307
538
  return (state, dispatch) => {
308
539
  // Where the review view hides deleted content, only what it shows.
309
540
  const hidden = (trackState(state)?.view ?? "markup") !== "markup";
310
- const changes = trackedChanges(state.doc).filter(
541
+ const changes = trackedChanges(state.doc, wordContext(state)?.envelope.section).filter(
311
542
  (c) => !hidden || (c.kind !== "deletion" && c.kind !== "paragraphDeletion"),
312
543
  );
313
544
  const { from, to } = state.selection;
@@ -318,7 +549,10 @@ function goTo(forward: boolean): Command {
318
549
  if (!target) return false;
319
550
  if (dispatch) {
320
551
  // A paragraph mark: the caret at the end of its paragraph.
321
- const mark = target.kind === "paragraphInsertion" || target.kind === "paragraphDeletion";
552
+ const mark =
553
+ target.kind === "paragraphInsertion" ||
554
+ target.kind === "paragraphDeletion" ||
555
+ target.kind === "sectionFormatting";
322
556
  const head = mark ? target.from : target.to;
323
557
  dispatch(state.tr.setSelection(TextSelection.create(state.doc, target.from, head)));
324
558
  }