@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
@@ -0,0 +1,285 @@
1
+ /**
2
+ * Editing a story (ADR-0007): a header, footer or note has an `EditorState` of its own (selection,
3
+ * stored marks, tracking) but no history. **The body goes first:** a story's transaction is
4
+ * computed without being kept, the body receives it as a `StoryStep`, and the story takes the new
5
+ * state only if the body then holds its tree. A `StoryStep` arriving from the body (undo, another
6
+ * editor of the story) replaces the state's document.
7
+ */
8
+ import { FoliDocumentError, type Node } from "../model/index.js";
9
+ import type { StoryJSON } from "../v3/document.js";
10
+ import { storyRules } from "../v3/document.js";
11
+ import type { Editor } from "./editor.js";
12
+ import { closeHistory, joinHistory, type StoryEdit, storyEdit } from "./history.js";
13
+ import { Selection, type SelectionJSON } from "./selection.js";
14
+ import { EditorState, Plugin, type Transaction } from "./state.js";
15
+ import {
16
+ maxRevisionIn,
17
+ revisionFloor,
18
+ type TrackState,
19
+ trackState,
20
+ trackTransaction,
21
+ untracked,
22
+ withTrackState,
23
+ } from "./track.js";
24
+ import {
25
+ CommentStep,
26
+ EnvelopeStep,
27
+ envelopeChange,
28
+ type Story,
29
+ type StoryBody,
30
+ StoryStep,
31
+ storyBody,
32
+ storyBodyKey,
33
+ type WordContext,
34
+ withContext,
35
+ withEnvelope,
36
+ wordContext,
37
+ } from "./word.js";
38
+
39
+ /** What a story's editor needs of the body's. */
40
+ export interface StoryHost extends StoryBody {
41
+ /** The plugins of a story's state (keymaps, tracking, the word context). */
42
+ plugins(context: WordContext, track: TrackState | null): Plugin[];
43
+ }
44
+
45
+ /** Puts on `bodyTr` what `transactions` did to story `id`, from `before` to `next`. */
46
+ function carryStoryChange(
47
+ bodyTr: Transaction,
48
+ bodyContext: WordContext,
49
+ id: string,
50
+ before: EditorState,
51
+ next: EditorState,
52
+ transactions: readonly Transaction[],
53
+ ): { after: Story; patched: boolean } {
54
+ const story = bodyContext.stories.get(id) as Story;
55
+ let after = story;
56
+ if (next.doc !== before.doc) {
57
+ after = { ...story, doc: next.doc };
58
+ bodyTr.step(
59
+ new StoryStep(id, story, after, before.selection.toJSON(), next.selection.toJSON()),
60
+ );
61
+ }
62
+ let patched = false;
63
+ for (const t of transactions) {
64
+ // What belongs to the document, not to the story: the body takes it.
65
+ for (const step of t.steps)
66
+ if (step instanceof CommentStep || step instanceof EnvelopeStep) bodyTr.step(step);
67
+ patched ||= envelopeChange(t) !== undefined;
68
+ }
69
+ // Only these: the story's copy of the rest of the envelope may be behind the body's.
70
+ if (patched) {
71
+ const { numbering, styles, resources, controls } = (wordContext(next) as WordContext).envelope;
72
+ const {
73
+ resources: _resources,
74
+ controls: _controls,
75
+ ...rest
76
+ } = envelopeChange(bodyTr) ?? bodyContext.envelope;
77
+ withEnvelope(bodyTr, {
78
+ ...rest,
79
+ numbering,
80
+ styles,
81
+ ...(resources ? { resources } : {}),
82
+ ...(controls ? { controls } : {}),
83
+ });
84
+ }
85
+ return { after, patched };
86
+ }
87
+
88
+ /** What changes one tree of the document: its id (null: the body) and the edit. */
89
+ export type TreeEdits = ReadonlyMap<string | null, (tr: Transaction) => void>;
90
+
91
+ /**
92
+ * Edits several trees as one transaction of the body, so one undo step. `tracked`: as tracked
93
+ * changes when tracking is on. Returns the trees whose edit was refused; the others are made.
94
+ */
95
+ export function editTrees(
96
+ editor: Editor,
97
+ edits: TreeEdits,
98
+ { tracked }: { tracked: boolean },
99
+ ): Set<string | null> {
100
+ const host = storyBody(editor.state) ?? editor;
101
+ const state = host.state;
102
+ const context = wordContext(state) as WordContext;
103
+ const failed = new Set<string | null>();
104
+ const keep = (tr: Transaction) => (tracked ? trackTransaction(state, tr) : tr);
105
+ let out = state.tr;
106
+ const bodyEdit = edits.get(null);
107
+ if (bodyEdit) {
108
+ bodyEdit(out);
109
+ const made = keep(out);
110
+ if (made) out = made;
111
+ else {
112
+ failed.add(null);
113
+ out = state.tr;
114
+ }
115
+ }
116
+ let floor = tracked ? maxRevisionIn(out.doc) + 1 : 0;
117
+ for (const [id, edit] of edits) {
118
+ if (id === null) continue;
119
+ const story = editor.story(id);
120
+ if (!story) {
121
+ failed.add(id);
122
+ continue;
123
+ }
124
+ const before = story.state;
125
+ const tr = before.tr;
126
+ edit(tr);
127
+ if (!tr.docChanged) continue;
128
+ const made = tracked
129
+ ? trackTransaction(before, tr.setMeta(revisionFloor, floor))
130
+ : tr.setMeta(untracked, true);
131
+ const result = made ? before.applyTransaction(made) : null;
132
+ if (!result?.transactions.length) {
133
+ failed.add(id);
134
+ continue;
135
+ }
136
+ carryStoryChange(out, context, id, before, result.state, result.transactions);
137
+ if (tracked) floor = Math.max(floor, maxRevisionIn(result.state.doc) + 1);
138
+ }
139
+ if (!out.docChanged) return failed;
140
+ host.dispatch(out.setMeta(untracked, true).setMeta(closeHistory, true));
141
+ // Refused by the body's filters: nothing happened, in any tree.
142
+ if (wordContext(host.state) === context && host.state.doc === state.doc)
143
+ for (const id of edits.keys()) failed.add(id);
144
+ return failed;
145
+ }
146
+
147
+ export type StoryListener = (state: EditorState, transactions: readonly Transaction[]) => void;
148
+
149
+ /** A story's editing state, kept in step with the body. */
150
+ export class StoryEditing {
151
+ state: EditorState;
152
+ /** Set while the story's own transaction goes through the body. */
153
+ private own = false;
154
+ /** Whether the story still exists: a removed one takes no more changes. */
155
+ alive = true;
156
+ readonly listeners = new Set<StoryListener>();
157
+
158
+ constructor(
159
+ readonly id: string,
160
+ private readonly host: StoryHost,
161
+ ) {
162
+ const doc = this.tree()?.doc as Node;
163
+ this.state = this.create(doc);
164
+ }
165
+
166
+ private tree(): Story | undefined {
167
+ return wordContext(this.host.state)?.stories.get(this.id);
168
+ }
169
+
170
+ private create(doc: Node): EditorState {
171
+ const { host, id } = this;
172
+ const context = wordContext(host.state) as WordContext;
173
+ const kind = context.stories.get(id)?.kind as StoryJSON["kind"];
174
+ return EditorState.create({
175
+ doc,
176
+ selection: Selection.atStart(doc),
177
+ plugins: [
178
+ new Plugin<StoryBody>({
179
+ key: storyBodyKey,
180
+ state: { init: () => host, apply: () => host },
181
+ }),
182
+ // What a story may not hold (a section break, a comment) is never put in it.
183
+ new Plugin({
184
+ filterTransaction(tr) {
185
+ if (!tr.docChanged) return true;
186
+ try {
187
+ storyRules(tr.doc, kind, "story");
188
+ return true;
189
+ } catch (error) {
190
+ if (error instanceof FoliDocumentError) return false;
191
+ throw error;
192
+ }
193
+ },
194
+ }),
195
+ ...host.plugins(context, trackState(host.state)),
196
+ ],
197
+ });
198
+ }
199
+
200
+ private notify(transactions: readonly Transaction[]): void {
201
+ for (const listener of this.listeners) listener(this.state, transactions);
202
+ }
203
+
204
+ /** `state` with the body's context and tracking; its plugins keep their state. */
205
+ private current(state: EditorState): EditorState {
206
+ const context = wordContext(this.host.state) as WordContext;
207
+ const track = trackState(this.host.state);
208
+ if (wordContext(state) === context && trackState(state) === track) return state;
209
+ const tr = withContext(state.tr, context);
210
+ if (track) withTrackState(tr, track);
211
+ return state.apply(tr);
212
+ }
213
+
214
+ dispatch = (tr: Transaction): void => {
215
+ if (!this.alive) return;
216
+ const { host, id, state: before } = this;
217
+ const tracked = trackTransaction(before, tr);
218
+ if (!tracked) return;
219
+ const result = before.applyTransaction(tracked);
220
+ if (!result.transactions.length) return;
221
+ const next = result.state;
222
+ const bodyState = host.state;
223
+ const bodyContext = wordContext(bodyState) as WordContext;
224
+ const story = bodyContext.stories.get(id) as Story;
225
+ const bodyTr = bodyState.tr;
226
+ const { after, patched } = carryStoryChange(
227
+ bodyTr,
228
+ bodyContext,
229
+ id,
230
+ before,
231
+ next,
232
+ result.transactions,
233
+ );
234
+ const track = trackState(next);
235
+ const trackChanged = !!track && track !== trackState(before);
236
+ if (trackChanged) withTrackState(bodyTr, track);
237
+ if (bodyTr.steps.length || patched || trackChanged) {
238
+ bodyTr.setMeta(untracked, true);
239
+ bodyTr.setMeta(storyEdit, { id, tr: tracked } satisfies StoryEdit);
240
+ bodyTr.time = tracked.time;
241
+ if (!tracked.addToHistory) bodyTr.setMeta("addToHistory", false);
242
+ if (tracked.getMeta(closeHistory)) bodyTr.setMeta(closeHistory, true);
243
+ if (tracked.getMeta(joinHistory)) bodyTr.setMeta(joinHistory, true);
244
+ this.own = true;
245
+ try {
246
+ host.dispatch(bodyTr);
247
+ } finally {
248
+ this.own = false;
249
+ }
250
+ // Refused by the body: the story stays as it was.
251
+ if (after !== story && wordContext(host.state)?.stories.get(id) !== after) return;
252
+ }
253
+ this.state = this.current(next);
254
+ this.notify(result.transactions);
255
+ };
256
+
257
+ /** After a change of the body. */
258
+ sync(transactions: readonly Transaction[]): void {
259
+ if (this.own) return;
260
+ const tree = this.tree();
261
+ if (!tree) {
262
+ this.retire();
263
+ return;
264
+ }
265
+ const { state } = this;
266
+ let selection: SelectionJSON | null = null;
267
+ for (const tr of transactions)
268
+ for (const step of tr.steps)
269
+ if (step instanceof StoryStep && step.id === this.id) selection = step.selectionAfter;
270
+ if (tree.doc !== state.doc) {
271
+ const json = selection ?? state.selection.toJSON();
272
+ this.state = this.current(state.withDocument(tree.doc, Selection.fromJSON(tree.doc, json)));
273
+ this.notify([]);
274
+ return;
275
+ }
276
+ const current = this.current(state);
277
+ if (current === state) return;
278
+ this.state = current;
279
+ this.notify([]);
280
+ }
281
+
282
+ retire(): void {
283
+ this.alive = false;
284
+ }
285
+ }