@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
@@ -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,97 @@ 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
+ * @internal
425
+ */
426
+ export function resolveStoryChanges(
427
+ state: EditorState,
428
+ tr: Transaction,
429
+ id: string,
430
+ accept: boolean,
431
+ kind: "insertion" | "deletion",
432
+ ): void {
433
+ const story = storyAfter(state, tr, id);
434
+ if (!story) return;
435
+ const paragraph = kind === "insertion" ? "paragraphInsertion" : "paragraphDeletion";
436
+ const changes = changesIn(story.doc, undefined, 0, story.doc.content.size).filter(
437
+ (c) => c.kind === kind || c.kind === paragraph,
438
+ );
439
+ if (!changes.length) return;
440
+ const inner = storyState(state, story.doc);
441
+ const storyTr = inner.tr;
442
+ resolve(inner, storyTr, changes, accept);
443
+ tr.step(new StoryStep(id, story, { ...story, doc: storyTr.doc }));
444
+ }
445
+
340
446
  function command(
341
447
  accept: boolean,
342
448
  range: (state: EditorState) => readonly { from: number; to: number }[],
343
449
  filter?: ChangeFilter,
450
+ /** Every story too: accepting or rejecting all changes of the document. */
451
+ everywhere = false,
344
452
  ): Command {
345
- return (state, dispatch) => {
453
+ const run: Command = (state, dispatch) => {
346
454
  if (!state.schema.marks.insertion) return false;
455
+ const body = everywhere ? storyBody(state) : null;
456
+ if (body) return dispatch ? body.run(run) : body.can(run);
457
+ const controls = wordContext(state)?.envelope.controls;
458
+ // As Word, in content that may not be edited: what leaves its text as it shows goes through
459
+ // (accepting an insertion, rejecting a deletion); the opposite is refused.
460
+ const open = (doc: Node) => (c: Change) =>
461
+ c.kind.toLowerCase().includes("deletion") !== accept ||
462
+ !readOnlyAt(doc, controls, c.from, c.to);
347
463
  // A change around several selected cells (a row revision, a move's partner) counts once.
348
464
  const seen = new Set<string>();
349
465
  const changes = range(state)
350
- .flatMap((r) => changesIn(state.doc, r.from, r.to, filter))
466
+ .flatMap((r) =>
467
+ changesIn(state.doc, wordContext(state)?.envelope.section, r.from, r.to, filter),
468
+ )
469
+ .filter(open(state.doc))
351
470
  .filter((c) => {
352
471
  const key = `${c.kind} ${c.id} ${c.from} ${c.to}`;
353
472
  return !seen.has(key) && !!seen.add(key);
354
473
  });
355
- if (!changes.length) return false;
474
+ const stories = everywhere
475
+ ? [...(wordContext(state)?.stories ?? [])].flatMap(([id, story]) => {
476
+ const inStory = changesIn(story.doc, undefined, 0, story.doc.content.size, filter).filter(
477
+ open(story.doc),
478
+ );
479
+ return inStory.length ? [{ id, story, changes: inStory }] : [];
480
+ })
481
+ : [];
482
+ if (!changes.length && !stories.length) return false;
356
483
  if (dispatch) {
357
484
  // Its own undo step, never merged with the typing before it.
358
485
  const tr = state.tr.setMeta(untracked, true).setMeta(closeHistory, true);
486
+ const edit = accept ? "accept" : "reject";
487
+ tr.setMeta(controlEdit, edit);
488
+ let envelope = wordContext(state)?.envelope;
489
+ const base = envelope;
359
490
  // A document the result would not fit (a structure Word allows and Foli cannot change
360
491
  // yet): nothing is changed.
361
492
  try {
362
- resolve(tr, changes, accept);
493
+ if (changes.length) resolve(state, tr, changes, accept);
494
+ for (const { id, story, changes: inStory } of stories) {
495
+ // With the controls' rules, as in the story's own editor.
496
+ const inner = EditorState.create({
497
+ doc: story.doc,
498
+ plugins: [...storyState(state, story.doc, envelope).plugins, controlsPlugin()],
499
+ });
500
+ const storyTr = inner.tr.setMeta(controlEdit, edit);
501
+ resolve(inner, storyTr, inStory, accept);
502
+ const done = inner.applyTransaction(storyTr).state;
503
+ envelope = wordContext(done)?.envelope;
504
+ tr.step(new StoryStep(id, story, { ...story, doc: done.doc }));
505
+ }
506
+ if (envelope && envelope !== base) {
507
+ if (envelope.styles !== base?.styles)
508
+ withEnvelope(tr, { ...(base as WordEnvelope), styles: envelope.styles });
509
+ if (envelope.controls !== base?.controls)
510
+ setEnvelopeValue(state, tr, "controls", envelope.controls);
511
+ }
363
512
  } catch (error) {
364
513
  if (error instanceof TransformError) return false;
365
514
  throw error;
@@ -368,6 +517,7 @@ function command(
368
517
  }
369
518
  return true;
370
519
  };
520
+ return run;
371
521
  }
372
522
 
373
523
  const selected = (state: EditorState) => state.selection.ranges;
@@ -379,17 +529,17 @@ export const acceptChange: Command = command(true, selected);
379
529
  export const rejectChange: Command = command(false, selected);
380
530
  /** Accepts every change (of `author`, if given). */
381
531
  export const acceptAllChanges = (author?: string): Command =>
382
- command(true, everything, author === undefined ? {} : { author });
532
+ command(true, everything, author === undefined ? {} : { author }, true);
383
533
  /** Rejects every change (of `author`, if given). */
384
534
  export const rejectAllChanges = (author?: string): Command =>
385
- command(false, everything, author === undefined ? {} : { author });
535
+ command(false, everything, author === undefined ? {} : { author }, true);
386
536
 
387
537
  /** Selects the next (or previous) change after (before) the selection. */
388
538
  function goTo(forward: boolean): Command {
389
539
  return (state, dispatch) => {
390
540
  // Where the review view hides deleted content, only what it shows.
391
541
  const hidden = (trackState(state)?.view ?? "markup") !== "markup";
392
- const changes = trackedChanges(state.doc).filter(
542
+ const changes = trackedChanges(state.doc, wordContext(state)?.envelope.section).filter(
393
543
  (c) => !hidden || (c.kind !== "deletion" && c.kind !== "paragraphDeletion"),
394
544
  );
395
545
  const { from, to } = state.selection;
@@ -400,7 +550,10 @@ function goTo(forward: boolean): Command {
400
550
  if (!target) return false;
401
551
  if (dispatch) {
402
552
  // A paragraph mark: the caret at the end of its paragraph.
403
- const mark = target.kind === "paragraphInsertion" || target.kind === "paragraphDeletion";
553
+ const mark =
554
+ target.kind === "paragraphInsertion" ||
555
+ target.kind === "paragraphDeletion" ||
556
+ target.kind === "sectionFormatting";
404
557
  const head = mark ? target.from : target.to;
405
558
  dispatch(state.tr.setSelection(TextSelection.create(state.doc, target.from, head)));
406
559
  }