@folijs/core 0.5.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 (125) hide show
  1. package/CHANGELOG.md +28 -0
  2. package/dist/state/commands.d.ts +2 -0
  3. package/dist/state/commands.d.ts.map +1 -1
  4. package/dist/state/commands.js +38 -6
  5. package/dist/state/commands.js.map +1 -1
  6. package/dist/state/comments.d.ts.map +1 -1
  7. package/dist/state/comments.js +12 -3
  8. package/dist/state/comments.js.map +1 -1
  9. package/dist/state/controls.d.ts +128 -0
  10. package/dist/state/controls.d.ts.map +1 -0
  11. package/dist/state/controls.js +1459 -0
  12. package/dist/state/controls.js.map +1 -0
  13. package/dist/state/editor.d.ts +86 -1
  14. package/dist/state/editor.d.ts.map +1 -1
  15. package/dist/state/editor.js +168 -42
  16. package/dist/state/editor.js.map +1 -1
  17. package/dist/state/fields.d.ts +79 -0
  18. package/dist/state/fields.d.ts.map +1 -0
  19. package/dist/state/fields.js +398 -0
  20. package/dist/state/fields.js.map +1 -0
  21. package/dist/state/fieldupdate.d.ts +178 -0
  22. package/dist/state/fieldupdate.d.ts.map +1 -0
  23. package/dist/state/fieldupdate.js +1574 -0
  24. package/dist/state/fieldupdate.js.map +1 -0
  25. package/dist/state/find.d.ts +68 -0
  26. package/dist/state/find.d.ts.map +1 -0
  27. package/dist/state/find.js +397 -0
  28. package/dist/state/find.js.map +1 -0
  29. package/dist/state/history.d.ts +12 -1
  30. package/dist/state/history.d.ts.map +1 -1
  31. package/dist/state/history.js +43 -7
  32. package/dist/state/history.js.map +1 -1
  33. package/dist/state/index.d.ts +12 -5
  34. package/dist/state/index.d.ts.map +1 -1
  35. package/dist/state/index.js +10 -3
  36. package/dist/state/index.js.map +1 -1
  37. package/dist/state/mark.d.ts.map +1 -1
  38. package/dist/state/mark.js +8 -1
  39. package/dist/state/mark.js.map +1 -1
  40. package/dist/state/navigation.d.ts +70 -0
  41. package/dist/state/navigation.d.ts.map +1 -0
  42. package/dist/state/navigation.js +226 -0
  43. package/dist/state/navigation.js.map +1 -0
  44. package/dist/state/notes.d.ts +94 -0
  45. package/dist/state/notes.d.ts.map +1 -0
  46. package/dist/state/notes.js +444 -0
  47. package/dist/state/notes.js.map +1 -0
  48. package/dist/state/review.d.ts +13 -4
  49. package/dist/state/review.d.ts.map +1 -1
  50. package/dist/state/review.js +133 -18
  51. package/dist/state/review.js.map +1 -1
  52. package/dist/state/sections.d.ts +101 -0
  53. package/dist/state/sections.d.ts.map +1 -0
  54. package/dist/state/sections.js +472 -0
  55. package/dist/state/sections.js.map +1 -0
  56. package/dist/state/state.d.ts +2 -0
  57. package/dist/state/state.d.ts.map +1 -1
  58. package/dist/state/state.js +4 -0
  59. package/dist/state/state.js.map +1 -1
  60. package/dist/state/stories.d.ts +41 -0
  61. package/dist/state/stories.d.ts.map +1 -0
  62. package/dist/state/stories.js +243 -0
  63. package/dist/state/stories.js.map +1 -0
  64. package/dist/state/tables.d.ts.map +1 -1
  65. package/dist/state/tables.js +2 -1
  66. package/dist/state/tables.js.map +1 -1
  67. package/dist/state/track.d.ts +23 -0
  68. package/dist/state/track.d.ts.map +1 -1
  69. package/dist/state/track.js +58 -9
  70. package/dist/state/track.js.map +1 -1
  71. package/dist/state/word.d.ts +78 -16
  72. package/dist/state/word.d.ts.map +1 -1
  73. package/dist/state/word.js +230 -10
  74. package/dist/state/word.js.map +1 -1
  75. package/dist/state/wordclip.d.ts +15 -6
  76. package/dist/state/wordclip.d.ts.map +1 -1
  77. package/dist/state/wordclip.js +192 -14
  78. package/dist/state/wordclip.js.map +1 -1
  79. package/dist/state/wordformat.d.ts +1 -1
  80. package/dist/state/wordformat.d.ts.map +1 -1
  81. package/dist/state/wordformat.js +60 -1
  82. package/dist/state/wordformat.js.map +1 -1
  83. package/dist/v3/document.d.ts +130 -7
  84. package/dist/v3/document.d.ts.map +1 -1
  85. package/dist/v3/document.js +172 -30
  86. package/dist/v3/document.js.map +1 -1
  87. package/dist/v3/index.d.ts +1 -1
  88. package/dist/v3/index.d.ts.map +1 -1
  89. package/dist/v3/index.js +1 -1
  90. package/dist/v3/index.js.map +1 -1
  91. package/dist/v3/resolve.d.ts +9 -3
  92. package/dist/v3/resolve.d.ts.map +1 -1
  93. package/dist/v3/resolve.js +22 -4
  94. package/dist/v3/resolve.js.map +1 -1
  95. package/dist/v3/schema.d.ts +2 -0
  96. package/dist/v3/schema.d.ts.map +1 -1
  97. package/dist/v3/schema.js +89 -3
  98. package/dist/v3/schema.js.map +1 -1
  99. package/package.json +1 -1
  100. package/src/state/commands.ts +41 -4
  101. package/src/state/comments.ts +11 -3
  102. package/src/state/controls.ts +1596 -0
  103. package/src/state/editor.ts +291 -47
  104. package/src/state/fields.ts +432 -0
  105. package/src/state/fieldupdate.ts +1770 -0
  106. package/src/state/find.ts +510 -0
  107. package/src/state/history.ts +51 -7
  108. package/src/state/index.ts +123 -0
  109. package/src/state/mark.ts +8 -1
  110. package/src/state/navigation.ts +305 -0
  111. package/src/state/notes.ts +519 -0
  112. package/src/state/numeric-pictures.word.json +172 -0
  113. package/src/state/review.ts +171 -19
  114. package/src/state/sections.ts +571 -0
  115. package/src/state/state.ts +5 -0
  116. package/src/state/stories.ts +285 -0
  117. package/src/state/tables.ts +2 -1
  118. package/src/state/track.ts +65 -10
  119. package/src/state/word.ts +275 -12
  120. package/src/state/wordclip.ts +211 -29
  121. package/src/state/wordformat.ts +61 -1
  122. package/src/v3/document.ts +326 -41
  123. package/src/v3/index.ts +7 -0
  124. package/src/v3/resolve.ts +33 -9
  125. package/src/v3/schema.ts +92 -3
@@ -0,0 +1,172 @@
1
+ {
2
+ "word": "16.0, Spanish separators swapped back",
3
+ "values": [0, 5, 12, 1234.5, 1234567.891, -5, -1234.5, 0.5, 0.126],
4
+ "pictures": [
5
+ "0",
6
+ "00",
7
+ "000",
8
+ "#",
9
+ "##",
10
+ "###",
11
+ "####",
12
+ "#,##0",
13
+ "#,##0.00",
14
+ "0.00",
15
+ "0.0",
16
+ "#.##",
17
+ "#.00",
18
+ "##.##",
19
+ "x",
20
+ "xx",
21
+ "x#",
22
+ "x0",
23
+ "0x",
24
+ "##x",
25
+ "0.0x",
26
+ "0.x",
27
+ "0.xx",
28
+ "x.x",
29
+ "$#,##0.00",
30
+ "$0",
31
+ "0 $",
32
+ "0%",
33
+ "#%",
34
+ "+0",
35
+ "-0",
36
+ "+#",
37
+ "-#",
38
+ "+0.0",
39
+ "-0.0",
40
+ "0+",
41
+ "0-",
42
+ "0;-0",
43
+ "0;(0)",
44
+ "0;(0);'zero'",
45
+ "0;;",
46
+ ";;",
47
+ "#;#;#",
48
+ "'a#b' 0",
49
+ "0 'units'",
50
+ "'x'0",
51
+ "'0'0",
52
+ "#,#",
53
+ "0,0",
54
+ "0,000",
55
+ "0.00;-0.00;0",
56
+ "00.00",
57
+ ",0",
58
+ "0.",
59
+ ".0",
60
+ "#.#",
61
+ "abc",
62
+ "a0b",
63
+ "0 a",
64
+ "*0",
65
+ "(0)",
66
+ "#,##0;(#,##0)"
67
+ ],
68
+ "results": [
69
+ ["0", "5", "12", "1235", "1234568", "-5", "-1235", "1", "0"],
70
+ ["00", "05", "12", "1235", "1234568", "-05", "-1235", "01", "00"],
71
+ ["000", "005", "012", "1235", "1234568", "-005", "-1235", "001", "000"],
72
+ [" ", "5", "12", "1235", "1234568", "-5", "-1235", "1", " "],
73
+ [" ", " 5", "12", "1235", "1234568", "- 5", "-1235", " 1", " "],
74
+ [" ", " 5", " 12", "1235", "1234568", "- 5", "-1235", " 1", " "],
75
+ [" ", " 5", " 12", "1235", "1234568", "- 5", "-1235", " 1", " "],
76
+ [" 0", " 5", " 12", "1,235", "1,234,568", "- 5", "-1,235", " 1", " 0"],
77
+ [
78
+ " 0.00",
79
+ " 5.00",
80
+ " 12.00",
81
+ "1,234.50",
82
+ "1,234,567.89",
83
+ "- 5.00",
84
+ "-1,234.50",
85
+ " 0.50",
86
+ " 0.13"
87
+ ],
88
+ ["0.00", "5.00", "12.00", "1234.50", "1234567.89", "-5.00", "-1234.50", "0.50", "0.13"],
89
+ ["0.0", "5.0", "12.0", "1234.5", "1234567.9", "-5.0", "-1234.5", "0.5", "0.1"],
90
+ [" .0 ", "5. ", "12. ", "1234.5 ", "1234567.89", "-5. ", "-1234.5 ", " .5 ", " .13"],
91
+ [" .00", "5.00", "12.00", "1234.50", "1234567.89", "-5.00", "-1234.50", " .50", " .13"],
92
+ [" .0 ", " 5. ", "12. ", "1234.5 ", "1234567.89", "- 5. ", "-1234.5 ", " .5 ", " .13"],
93
+ [" ", "5", "2", "5", "8", "5", "5", "1", " "],
94
+ [" ", " 5", "12", "35", "68", " 5", "35", " 1", " "],
95
+ [" ", " 5", "12", "35", "68", " -5", "35", " 1", " "],
96
+ [" 0", " 5", "12", "35", "68", " -5", "35", " 1", " 0"],
97
+ ["0 ", "05", "12", "1235", "1234568", "-05", "-1235", "01", "0 "],
98
+ [" ", " 5", " 12", "1235", "1234568", "- 5", "-1235", " 1", " "],
99
+ ["0.0", "5.0", "12.0", "1234.5", "1234567.89", "-5.0", "-1234.5", "0.5", "0.13"],
100
+ ["0.0", "5.", "12.", "1234.5", "1234567.9", "-5.", "-1234.5", "0.5", "0.1"],
101
+ ["0.0", "5.", "12.", "1234.5", "1234567.89", "-5.", "-1234.5", "0.5", "0.13"],
102
+ [" .0", "5.", "2.", "4.5", "7.9", "5.", "4.5", " .5", " .1"],
103
+ [
104
+ "$ 0.00",
105
+ "$ 5.00",
106
+ "$ 12.00",
107
+ "$1,234.50",
108
+ "$1,234,567.89",
109
+ "$- 5.00",
110
+ "$-1,234.50",
111
+ "$ 0.50",
112
+ "$ 0.13"
113
+ ],
114
+ ["$0", "$5", "$12", "$1235", "$1234568", "$-5", "$-1235", "$1", "$0"],
115
+ ["0 $", "5 $", "12 $", "1235 $", "1234568 $", "-5 $", "-1235 $", "1 $", "0 $"],
116
+ ["0%", "5%", "12%", "1235%", "1234568%", "-5%", "-1235%", "1%", "0%"],
117
+ [" %", "5%", "12%", "1235%", "1234568%", "-5%", "-1235%", "1%", " %"],
118
+ [" 0", "+5", "+12", "+1235", "+1234568", "-5", "-1235", "+1", " 0"],
119
+ [" 0", " 5", " 12", " 1235", " 1234568", "-5", "-1235", " 1", " 0"],
120
+ [" ", "+5", "+12", "+1235", "+1234568", "-5", "-1235", "+1", " "],
121
+ [" ", " 5", " 12", " 1235", " 1234568", "-5", "-1235", " 1", " "],
122
+ [" 0.0", "+5.0", "+12.0", "+1234.5", "+1234567.9", "-5.0", "-1234.5", "+0.5", "+0.1"],
123
+ [" 0.0", " 5.0", " 12.0", " 1234.5", " 1234567.9", "-5.0", "-1234.5", " 0.5", " 0.1"],
124
+ ["0 ", "5+", "12+", "1235+", "1234568+", "5-", "1235-", "1+", "0 "],
125
+ ["0 ", "5 ", "12 ", "1235 ", "1234568 ", "5-", "1235-", "1 ", "0 "],
126
+ ["0", "5", "12", "1235", "1234568", "-5", "-1235", "1", "0"],
127
+ ["0", "5", "12", "1235", "1234568", "(5)", "(1235)", "1", "0"],
128
+ ["zero", "5", "12", "1235", "1234568", "(5)", "(1235)", "1", "0"],
129
+ ["", "5", "12", "1235", "1234568", "", "", "1", "0"],
130
+ ["", "", "", "", "", "", "", "", ""],
131
+ [" ", "5", "12", "1235", "1234568", "5", "1235", "1", " "],
132
+ [
133
+ "a#b 0",
134
+ "a#b 5",
135
+ "a#b 12",
136
+ "a#b 1235",
137
+ "a#b 1234568",
138
+ "a#b -5",
139
+ "a#b -1235",
140
+ "a#b 1",
141
+ "a#b 0"
142
+ ],
143
+ [
144
+ "0 units",
145
+ "5 units",
146
+ "12 units",
147
+ "1235 units",
148
+ "1234568 units",
149
+ "-5 units",
150
+ "-1235 units",
151
+ "1 units",
152
+ "0 units"
153
+ ],
154
+ ["x0", "x5", "x12", "x1235", "x1234568", "x-5", "x-1235", "x1", "x0"],
155
+ ["00", "05", "012", "01235", "01234568", "0-5", "0-1235", "01", "00"],
156
+ [" ", " 5", "12", "1,235", "1,234,568", "- 5", "-1,235", " 1", " "],
157
+ ["00", "05", "12", "1,235", "1,234,568", "-05", "-1,235", "01", "00"],
158
+ ["0,000", "0,005", "0,012", "1,235", "1,234,568", "-0,005", "-1,235", "0,001", "0,000"],
159
+ ["0", "5.00", "12.00", "1234.50", "1234567.89", "-5.00", "-1234.50", "0.50", "0.13"],
160
+ ["00.00", "05.00", "12.00", "1234.50", "1234567.89", "-05.00", "-1234.50", "00.50", "00.13"],
161
+ ["0", "5", "12", "1,235", "1,234,568", "-5", "-1,235", "1", "0"],
162
+ ["0.", "5.", "12.", "1235.", "1234568.", "-5.", "-1235.", "1.", "0."],
163
+ [".0", ".0", ".0", ".5", ".9", ".0", ".5", ".5", ".1"],
164
+ [" .0", "5. ", "12. ", "1234.5", "1234567.9", "-5. ", "-1234.5", " .5", " .1"],
165
+ ["abc", "abc", "abc", "abc", "abc", "abc", "abc", "abc", "abc"],
166
+ ["a0b", "a5b", "a12b", "a1235b", "a1234568b", "a-5b", "a-1235b", "a1b", "a0b"],
167
+ ["0 a", "5 a", "12 a", "1235 a", "1234568 a", "-5 a", "-1235 a", "1 a", "0 a"],
168
+ ["*0", "*5", "*12", "*1235", "*1234568", "*-5", "*-1235", "*1", "*0"],
169
+ ["(0)", "(5)", "(12)", "(1235)", "(1234568)", "(-5)", "(-1235)", "(1)", "(0)"],
170
+ [" 0", " 5", " 12", "1,235", "1,234,568", "( 5)", "(1,235)", " 1", " 0"]
171
+ ]
172
+ }
@@ -11,13 +11,26 @@
11
11
  */
12
12
  import type { Attrs, Mark, Node } from "../model/index.js";
13
13
  import { TransformError } from "../transform/transform.js";
14
+ import type { SectionJSON } from "../v3/document.js";
14
15
  import { RUN_PROPERTY_MARKS } from "../v3/schema.js";
16
+ import { controlEdit, controlsPlugin, readOnlyAt } from "./controls.js";
15
17
  import { closeHistory } from "./history.js";
16
18
  import { MARK_ATTRS } from "./mark.js";
19
+ import { absorbSection, lastSection, resolveSectionChange } from "./sections.js";
17
20
  import { TextSelection } from "./selection.js";
18
- import type { Command, EditorState, Transaction } from "./state.js";
21
+ import { type Command, EditorState, type Transaction } from "./state.js";
19
22
  import { keepTablesApart } from "./tables.js";
20
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";
21
34
 
22
35
  export type ChangeKind =
23
36
  | "insertion"
@@ -26,6 +39,7 @@ export type ChangeKind =
26
39
  | "paragraphInsertion"
27
40
  | "paragraphDeletion"
28
41
  | "paragraphFormatting"
42
+ | "sectionFormatting"
29
43
  | "rowInsertion"
30
44
  | "rowDeletion";
31
45
 
@@ -60,9 +74,23 @@ interface Revision {
60
74
  date?: string | null;
61
75
  }
62
76
 
63
- /** Every tracked change of `doc`, in document order. */
64
- 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[] {
65
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
+ });
66
94
  const flush = (run: { mark: Mark; from: number; to: number }) => {
67
95
  const attrs = run.mark.attrs;
68
96
  changes.push({
@@ -98,6 +126,8 @@ export function trackedChanges(doc: Node): Change[] {
98
126
  if (!block.isTextblock) return true;
99
127
  const change = block.attrs.formatChange as Revision | null;
100
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));
101
131
  // Runs of content with the same change mark, extended while the next node has it too.
102
132
  let open: { mark: Mark; from: number; to: number }[] = [];
103
133
  block.forEach((inline, at) => {
@@ -143,19 +173,34 @@ function paragraphChange(
143
173
  }
144
174
  const INLINE: ReadonlySet<ChangeKind> = new Set(["insertion", "deletion", "formatting"]);
145
175
  /** Inline nodes no change marks: they stay when the text around them goes. */
146
- 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
+ ]);
147
184
 
148
185
  /**
149
186
  * The changes `from..to` touches, with their moves' other ends. At a caret: the ones around it,
150
187
  * the paragraph's formatting only when nothing else is. In a selection, changes to content only
151
188
  * as far as the selection goes (Word accepts the selected part).
152
189
  */
153
- function changesIn(doc: Node, from: number, to: number, filter: ChangeFilter = {}): Change[] {
154
- 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);
155
198
  let picked: Change[];
156
199
  if (from === to) {
157
200
  const around = all.filter((c) => c.from <= from && from <= c.to);
158
- const own = around.filter((c) => c.kind !== "paragraphFormatting");
201
+ const own = around.filter(
202
+ (c) => c.kind !== "paragraphFormatting" && c.kind !== "sectionFormatting",
203
+ );
159
204
  picked = own.length ? own : around;
160
205
  } else
161
206
  picked = all
@@ -174,7 +219,12 @@ function changesIn(doc: Node, from: number, to: number, filter: ChangeFilter = {
174
219
  }
175
220
 
176
221
  /** Applies accepting (or rejecting) `changes` to `tr`. */
177
- 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 {
178
228
  const schema = tr.doc.type.schema;
179
229
  const removed: [number, number][] = [];
180
230
  const joins: { pos: number; keepFirst: boolean }[] = [];
@@ -182,7 +232,12 @@ function resolve(tr: Transaction, changes: readonly Change[], accept: boolean):
182
232
  const rowsGoing = new Map<number, [number, number][]>();
183
233
  /** Whether whole rows or paragraphs go: what lies inside them must not be removed again. */
184
234
  let wholes = false;
185
- for (const change of changes) {
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) {
186
241
  const { kind, from, to } = change;
187
242
  const markType = schema.marks[kind === "formatting" ? "formatChange" : kind];
188
243
  switch (kind) {
@@ -224,6 +279,24 @@ function resolve(tr: Transaction, changes: readonly Change[], accept: boolean):
224
279
  tr.setNodeMarkup(start, node.type, attrs);
225
280
  break;
226
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
+ }
227
300
  case "rowInsertion":
228
301
  case "rowDeletion": {
229
302
  const row = tr.doc.nodeAt(from);
@@ -253,8 +326,16 @@ function resolve(tr: Transaction, changes: readonly Change[], accept: boolean):
253
326
  const end = start + node.nodeSize;
254
327
  // A paragraph whose text goes too is removed whole: the next one stays as it is.
255
328
  const emptied = covered(node, start + 1, removed);
256
- if (goes && tr.doc.resolve(end).nodeAfter?.isTextblock)
257
- 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 });
258
339
  // Nothing to join with (a table follows, or a cell ends): an inserted paragraph left
259
340
  // empty goes whole when rejected (Word's rules are restored after: `keepTablesApart`).
260
341
  // (Never a cell's only paragraph: a cell ends with one.)
@@ -337,29 +418,96 @@ function join(tr: Transaction, pos: number, keepFirst: boolean): void {
337
418
  tr.setNodeMarkup(start, first.type, attrs);
338
419
  }
339
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
+
340
445
  function command(
341
446
  accept: boolean,
342
447
  range: (state: EditorState) => readonly { from: number; to: number }[],
343
448
  filter?: ChangeFilter,
449
+ /** Every story too: accepting or rejecting all changes of the document. */
450
+ everywhere = false,
344
451
  ): Command {
345
- return (state, dispatch) => {
452
+ const run: Command = (state, dispatch) => {
346
453
  if (!state.schema.marks.insertion) 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);
347
462
  // A change around several selected cells (a row revision, a move's partner) counts once.
348
463
  const seen = new Set<string>();
349
464
  const changes = range(state)
350
- .flatMap((r) => changesIn(state.doc, r.from, r.to, filter))
465
+ .flatMap((r) =>
466
+ changesIn(state.doc, wordContext(state)?.envelope.section, r.from, r.to, filter),
467
+ )
468
+ .filter(open(state.doc))
351
469
  .filter((c) => {
352
470
  const key = `${c.kind} ${c.id} ${c.from} ${c.to}`;
353
471
  return !seen.has(key) && !!seen.add(key);
354
472
  });
355
- if (!changes.length) return false;
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;
356
482
  if (dispatch) {
357
483
  // Its own undo step, never merged with the typing before it.
358
484
  const tr = state.tr.setMeta(untracked, true).setMeta(closeHistory, true);
485
+ const edit = accept ? "accept" : "reject";
486
+ tr.setMeta(controlEdit, edit);
487
+ let envelope = wordContext(state)?.envelope;
488
+ const base = envelope;
359
489
  // A document the result would not fit (a structure Word allows and Foli cannot change
360
490
  // yet): nothing is changed.
361
491
  try {
362
- resolve(tr, changes, accept);
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
+ }
363
511
  } catch (error) {
364
512
  if (error instanceof TransformError) return false;
365
513
  throw error;
@@ -368,6 +516,7 @@ function command(
368
516
  }
369
517
  return true;
370
518
  };
519
+ return run;
371
520
  }
372
521
 
373
522
  const selected = (state: EditorState) => state.selection.ranges;
@@ -379,17 +528,17 @@ export const acceptChange: Command = command(true, selected);
379
528
  export const rejectChange: Command = command(false, selected);
380
529
  /** Accepts every change (of `author`, if given). */
381
530
  export const acceptAllChanges = (author?: string): Command =>
382
- command(true, everything, author === undefined ? {} : { author });
531
+ command(true, everything, author === undefined ? {} : { author }, true);
383
532
  /** Rejects every change (of `author`, if given). */
384
533
  export const rejectAllChanges = (author?: string): Command =>
385
- command(false, everything, author === undefined ? {} : { author });
534
+ command(false, everything, author === undefined ? {} : { author }, true);
386
535
 
387
536
  /** Selects the next (or previous) change after (before) the selection. */
388
537
  function goTo(forward: boolean): Command {
389
538
  return (state, dispatch) => {
390
539
  // Where the review view hides deleted content, only what it shows.
391
540
  const hidden = (trackState(state)?.view ?? "markup") !== "markup";
392
- const changes = trackedChanges(state.doc).filter(
541
+ const changes = trackedChanges(state.doc, wordContext(state)?.envelope.section).filter(
393
542
  (c) => !hidden || (c.kind !== "deletion" && c.kind !== "paragraphDeletion"),
394
543
  );
395
544
  const { from, to } = state.selection;
@@ -400,7 +549,10 @@ function goTo(forward: boolean): Command {
400
549
  if (!target) return false;
401
550
  if (dispatch) {
402
551
  // A paragraph mark: the caret at the end of its paragraph.
403
- const mark = target.kind === "paragraphInsertion" || target.kind === "paragraphDeletion";
552
+ const mark =
553
+ target.kind === "paragraphInsertion" ||
554
+ target.kind === "paragraphDeletion" ||
555
+ target.kind === "sectionFormatting";
404
556
  const head = mark ? target.from : target.to;
405
557
  dispatch(state.tr.setSelection(TextSelection.create(state.doc, target.from, head)));
406
558
  }