@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
package/src/state/word.ts CHANGED
@@ -9,20 +9,29 @@
9
9
  * shortcut: v2 and v3 editing live side by side until 8.7d makes v3 the only schema; then these
10
10
  * become the commands themselves.
11
11
  */
12
+ import { deepEqual } from "../model/attrs.js";
12
13
  import type { Attrs, Mark, MarkType, Node } from "../model/index.js";
13
14
  import { Step, StepResult } from "../transform/steps.js";
14
- import type { CommentJSON, FoliDocument } from "../v3/document.js";
15
+ import type { CommentJSON, FoliDocument, StoryJSON } from "../v3/document.js";
16
+ import { parseContent } from "../v3/document.js";
15
17
  import { Resolver, type RunFormat } from "../v3/resolve.js";
16
18
  import { MARK_ATTRS } from "./mark.js";
17
- import { NodeSelection, TextSelection } from "./selection.js";
18
- import { type EditorState, Plugin, PluginKey, type Transaction } from "./state.js";
19
+ import { NodeSelection, type SelectionJSON, TextSelection } from "./selection.js";
20
+ import { type Command, EditorState, Plugin, PluginKey, type Transaction } from "./state.js";
19
21
 
20
22
  /** A v3 document without its content: what the editor keeps beside the edited tree. */
21
23
  export type WordEnvelope = Omit<FoliDocument, "content">;
22
24
 
25
+ /** A story as the editor holds it: its tree, and what the envelope says beside it. */
26
+ export interface Story extends Omit<StoryJSON, "content"> {
27
+ readonly doc: Node;
28
+ }
29
+
23
30
  export interface WordContext {
24
31
  readonly envelope: WordEnvelope;
25
32
  readonly resolver: Resolver;
33
+ /** The envelope's `stories` as trees: what `StoryStep`s hold and compare. */
34
+ readonly stories: ReadonlyMap<string, Story>;
26
35
  /**
27
36
  * Made when the document is loaded and kept while it is edited: a copy carries it, so a paste
28
37
  * tells this document's lists from another's (Word's `w:nsid`; Foli makes equal definitions
@@ -108,27 +117,262 @@ export function setComments(
108
117
  return tr;
109
118
  }
110
119
 
120
+ /** What `EnvelopeStep` can change (ADR-0007). */
121
+ export type EnvelopeKey = "section" | "settings" | "controls";
122
+
123
+ /**
124
+ * Changes one value of the envelope (the last section's setup, the document's settings) from
125
+ * `before` to `after`. A step, unlike `withEnvelope`, so undo and redo take it with the edit it
126
+ * belongs to. It leaves the tree as it is; history refuses an undo whose `before` no longer holds.
127
+ */
128
+ export class EnvelopeStep<K extends EnvelopeKey = EnvelopeKey> extends Step {
129
+ constructor(
130
+ readonly key: K,
131
+ readonly before: WordEnvelope[K],
132
+ readonly after: WordEnvelope[K],
133
+ ) {
134
+ super();
135
+ }
136
+
137
+ apply(doc: Node): StepResult {
138
+ return StepResult.ok(doc);
139
+ }
140
+
141
+ invert(): Step {
142
+ return new EnvelopeStep(this.key, this.after, this.before);
143
+ }
144
+
145
+ map(): Step {
146
+ return this;
147
+ }
148
+ }
149
+
150
+ /**
151
+ * Changes story `id` from `before` to `after` (null: no story), a step of the body's transaction
152
+ * (ADR-0007): its inverse needs no document, it moves no position of the body. The selections are
153
+ * the story's around the change, so undo can give it back.
154
+ */
155
+ export class StoryStep extends Step {
156
+ constructor(
157
+ readonly id: string,
158
+ readonly before: Story | null,
159
+ readonly after: Story | null,
160
+ readonly selectionBefore: SelectionJSON | null = null,
161
+ readonly selectionAfter: SelectionJSON | null = null,
162
+ ) {
163
+ super();
164
+ }
165
+
166
+ apply(doc: Node): StepResult {
167
+ return StepResult.ok(doc);
168
+ }
169
+
170
+ invert(): Step {
171
+ return new StoryStep(
172
+ this.id,
173
+ this.after,
174
+ this.before,
175
+ this.selectionAfter,
176
+ this.selectionBefore,
177
+ );
178
+ }
179
+
180
+ map(): Step {
181
+ return this;
182
+ }
183
+
184
+ /** This step followed by `next`, when `next` starts from what this one leaves. */
185
+ merge(next: StoryStep): StoryStep | null {
186
+ if (next.id !== this.id || !sameStory(this.after, next.before)) return null;
187
+ return new StoryStep(
188
+ this.id,
189
+ this.before,
190
+ next.after,
191
+ this.selectionBefore,
192
+ next.selectionAfter,
193
+ );
194
+ }
195
+ }
196
+
197
+ /** Whether two stories are the same: identical trees are the usual case. */
198
+ export function sameStory(a: Story | null | undefined, b: Story | null | undefined): boolean {
199
+ if (a === b || (!a && !b)) return true;
200
+ return (
201
+ !!a &&
202
+ !!b &&
203
+ a.kind === b.kind &&
204
+ a.noteType === b.noteType &&
205
+ a.ext === b.ext &&
206
+ (a.doc === b.doc || a.doc.eq(b.doc))
207
+ );
208
+ }
209
+
210
+ /** A story's JSON in the envelope. */
211
+ function storyJSON({ doc, ...rest }: Story): StoryJSON {
212
+ return { ...rest, content: doc.toJSON() };
213
+ }
214
+
215
+ /** The value of `key` as `steps` leave it, from `value`. */
216
+ function afterEnvelopeSteps<K extends EnvelopeKey>(
217
+ key: K,
218
+ value: WordEnvelope[K],
219
+ steps: readonly Step[],
220
+ ): WordEnvelope[K] {
221
+ let current = value;
222
+ for (const step of steps)
223
+ if (step instanceof EnvelopeStep && step.key === key) current = step.after as WordEnvelope[K];
224
+ return current;
225
+ }
226
+
227
+ /** Makes `tr` set the envelope's `key` to `value`, from what it holds after `tr` so far. */
228
+ export function setEnvelopeValue<K extends EnvelopeKey>(
229
+ state: EditorState,
230
+ tr: Transaction,
231
+ key: K,
232
+ value: WordEnvelope[K],
233
+ ): Transaction {
234
+ const envelope = wordContext(state)?.envelope;
235
+ if (!envelope) return tr;
236
+ return tr.step(new EnvelopeStep(key, afterEnvelopeSteps(key, envelope[key], tr.steps), value));
237
+ }
238
+
239
+ /**
240
+ * Whether stored `steps` can apply to `context`: every `EnvelopeStep` and `StoryStep` finds what
241
+ * it expects, given the ones before it. A change made outside history makes them stale.
242
+ */
243
+ export function storedStepsApply(context: WordContext, steps: readonly Step[]): boolean {
244
+ const values = new Map<EnvelopeKey, unknown>();
245
+ const stories = new Map<string, Story | null>();
246
+ for (const step of steps) {
247
+ if (step instanceof EnvelopeStep) {
248
+ const key = step.key as EnvelopeKey;
249
+ const current = values.has(key) ? values.get(key) : context.envelope[key];
250
+ if (!deepEqual(current, step.before)) return false;
251
+ values.set(key, step.after);
252
+ } else if (step instanceof StoryStep) {
253
+ const current = stories.has(step.id)
254
+ ? stories.get(step.id)
255
+ : (context.stories.get(step.id) ?? null);
256
+ if (!sameStory(current, step.before)) return false;
257
+ stories.set(step.id, step.after);
258
+ }
259
+ }
260
+ return true;
261
+ }
262
+
263
+ /** The story as `tr` leaves it (`state`'s, after the story steps `tr` already has). */
264
+ export function storyAfter(state: EditorState, tr: Transaction, id: string): Story | null {
265
+ let story = wordContext(state)?.stories.get(id) ?? null;
266
+ for (const step of tr.steps) if (step instanceof StoryStep && step.id === id) story = step.after;
267
+ return story;
268
+ }
269
+
270
+ /** What a story's editor knows of the body's: undo, and the trees it allocates ids across. */
271
+ export interface StoryBody {
272
+ readonly state: EditorState;
273
+ dispatch(tr: Transaction): void;
274
+ run(command: Command): boolean;
275
+ can(command: Command): boolean;
276
+ }
277
+
278
+ /** On the state of a story's editor: the body it belongs to. */
279
+ export const storyBodyKey = new PluginKey<StoryBody>("storyBody");
280
+
281
+ /** The body of the story `state` edits; null when `state` is the body's. */
282
+ export const storyBody = (state: EditorState): StoryBody | null =>
283
+ storyBodyKey.getState(state) ?? null;
284
+
285
+ /** A state over a story's `doc` for working on it, with `state`'s word context (and `envelope`). */
286
+ export function storyState(state: EditorState, doc: Node, envelope?: WordEnvelope): EditorState {
287
+ const context = wordContext(state) as WordContext;
288
+ const given = envelope ? { ...context, envelope } : context;
289
+ return EditorState.create({ doc, plugins: [wordPlugin(given.envelope, given)] });
290
+ }
291
+
292
+ /** Every tree of the document but `state`'s own: ids are allocated across them (ADR-0007). */
293
+ export function otherTrees(state: EditorState): Node[] {
294
+ const trees = [...(wordContext(state)?.stories.values() ?? [])].map((story) => story.doc);
295
+ const body = storyBody(state)?.state.doc;
296
+ return (body ? [body, ...trees] : trees).filter((tree) => tree !== state.doc);
297
+ }
298
+
299
+ /** The trees of `envelope`'s stories. */
300
+ function storyTrees(envelope: WordEnvelope): Map<string, Story> {
301
+ return new Map(
302
+ Object.entries(envelope.stories ?? {}).map(([id, { content, ...rest }]) => [
303
+ id,
304
+ { ...rest, doc: parseContent(content, `stories[${JSON.stringify(id)}].content`) },
305
+ ]),
306
+ );
307
+ }
308
+
111
309
  /**
112
310
  * The plugin that gives commands a v3 document's envelope. A transaction changes it with
113
311
  * `withEnvelope` (a new list's numbering definition); undo leaves such additions, as Word does.
114
- * Comments change only through `CommentStep`s, which undo takes back.
312
+ * Comments change only through `CommentStep`s, the last section, the settings and the controls'
313
+ * properties through `EnvelopeStep`s and stories through `StoryStep`s, which undo takes back (a
314
+ * paste adds the properties of the controls it copies with `withEnvelope`). A story's editor is
315
+ * given its body's `context` as it stands.
115
316
  */
116
- export function wordPlugin(envelope: WordEnvelope): Plugin<WordContext> {
317
+ export function wordPlugin(envelope: WordEnvelope, context?: WordContext): Plugin<WordContext> {
117
318
  // Not a secret: only tells documents apart.
118
319
  const id = `${Date.now().toString(36)}${Math.random().toString(36).slice(2)}`;
119
- const context: WordContext = { envelope, resolver: new Resolver(envelope), id };
320
+ const initial: WordContext = context ?? {
321
+ envelope,
322
+ resolver: new Resolver(envelope),
323
+ stories: storyTrees(envelope),
324
+ id,
325
+ };
120
326
  return new Plugin({
121
327
  key,
122
328
  state: {
123
- init: () => context,
329
+ init: () => initial,
124
330
  apply: (tr, value) => {
331
+ const adopted = tr.getMeta(ADOPT) as WordContext | undefined;
332
+ if (adopted) return adopted;
125
333
  const next = tr.getMeta(key) as WordEnvelope | undefined;
126
334
  const comments = applyCommentSteps(value.envelope.comments, tr.steps);
127
- if (!next && comments === value.envelope.comments) return value;
128
- const envelope = { ...(next ?? value.envelope), comments: comments as CommentJSON[] };
129
- // Only comments changed: they do not affect formatting, the resolver stays.
335
+ const base = next ?? value.envelope;
336
+ const section = afterEnvelopeSteps("section", base.section, tr.steps);
337
+ const settings = afterEnvelopeSteps("settings", value.envelope.settings, tr.steps);
338
+ const controls = afterEnvelopeSteps("controls", base.controls, tr.steps);
339
+ let { stories } = value;
340
+ let storiesJSON = value.envelope.stories;
341
+ for (const step of tr.steps) {
342
+ if (!(step instanceof StoryStep)) continue;
343
+ const trees = new Map(stories);
344
+ const json = { ...storiesJSON };
345
+ if (step.after) {
346
+ trees.set(step.id, step.after);
347
+ json[step.id] = storyJSON(step.after);
348
+ } else {
349
+ trees.delete(step.id);
350
+ delete json[step.id];
351
+ }
352
+ stories = trees;
353
+ storiesJSON = json;
354
+ }
355
+ if (
356
+ !next &&
357
+ comments === value.envelope.comments &&
358
+ section === base.section &&
359
+ settings === value.envelope.settings &&
360
+ controls === value.envelope.controls &&
361
+ stories === value.stories
362
+ )
363
+ return value;
364
+ const { stories: _stories, settings: _settings, controls: _controls, ...rest } = base;
365
+ const envelope: WordEnvelope = {
366
+ ...rest,
367
+ section,
368
+ comments: comments as CommentJSON[],
369
+ ...(storiesJSON && Object.keys(storiesJSON).length ? { stories: storiesJSON } : {}),
370
+ ...(settings ? { settings } : {}),
371
+ ...(controls ? { controls } : {}),
372
+ };
373
+ // Only comments, stories or steps changed: they do not affect formatting.
130
374
  const resolver = next ? new Resolver(envelope) : value.resolver;
131
- return { envelope, resolver, id: value.id };
375
+ return { envelope, resolver, stories, id: value.id };
132
376
  },
133
377
  },
134
378
  });
@@ -142,6 +386,16 @@ export function withEnvelope(tr: Transaction, envelope: WordEnvelope): Transacti
142
386
  return tr.setMeta(key, envelope);
143
387
  }
144
388
 
389
+ const ADOPT = "adoptWordContext";
390
+
391
+ /** Makes `tr` give a story's state the body's context as it now is. */
392
+ export const withContext = (tr: Transaction, context: WordContext): Transaction =>
393
+ tr.setMeta(ADOPT, context);
394
+
395
+ /** The envelope `tr` gives the document through `withEnvelope`, if any. */
396
+ export const envelopeChange = (tr: Transaction): WordEnvelope | undefined =>
397
+ tr.getMeta(key) as WordEnvelope | undefined;
398
+
145
399
  /** The envelope and resolver of the v3 document in `state`, or null for a v2 editor. */
146
400
  export function wordContext(state: EditorState): WordContext | null {
147
401
  return key.getState(state) ?? null;
@@ -181,7 +435,8 @@ export function splitAttrs(
181
435
  const next = style?.next;
182
436
  if (next === undefined || next === id) return { before, after: attrs };
183
437
  const mark = Object.fromEntries(Object.keys(MARK_ONLY).map((k) => [k, attrs[k] ?? null]));
184
- return { before, after: { ...mark, style: next } };
438
+ // The new paragraph is in the same content controls.
439
+ return { before, after: { ...mark, controls: attrs.controls, style: next } };
185
440
  }
186
441
 
187
442
  /** Run toggles: whether text shows the property, from its resolved formatting. */
@@ -303,6 +558,14 @@ const MARKERS = new Set([
303
558
  "bookmarkEnd",
304
559
  "commentStart",
305
560
  "commentEnd",
561
+ "controlStart",
562
+ "controlEnd",
563
+ // A field's parts and a note's own number: deleting one alone would break what it belongs to.
564
+ "fieldStart",
565
+ "fieldCode",
566
+ "fieldSeparator",
567
+ "fieldEnd",
568
+ "noteMark",
306
569
  // A comment's reference mark: deleting it would take the comment's anchor.
307
570
  "commentReference",
308
571
  ]);