@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
@@ -14,6 +14,11 @@
14
14
  * - From another document, a style or list the copy does not define is not one: the text is in
15
15
  * the default style, not numbered (not the target's style or list of that id).
16
16
  * - Paragraph ids (`w14:paraId`, `w14:textId`) are left out: export makes the ones it needs.
17
+ * - A note reference brings its note, carried by the copy, as a new story with a new id: a copy
18
+ * is a second note, and what was cut was removed with its reference (ADR-0007).
19
+ * - A content control copied inside its document is a new control with the same properties while
20
+ * the original stays (cut and pasted, it is the same one). From elsewhere its content comes
21
+ * without it.
17
22
  *
18
23
  * shortcut: ids inside opaque content (a table's bookmarks and paragraph ids) and tracked-change
19
24
  * ids are pasted as they are; Word tolerates repeats of both. Map them when a file shows otherwise.
@@ -24,19 +29,31 @@ import { type DocJSON, isSafeLink } from "../schema.js";
24
29
  import {
25
30
  type AbstractNumberingJSON,
26
31
  type CommentJSON,
32
+ type ControlJSON,
27
33
  migrateToV4,
34
+ migrateToV5,
28
35
  type NumberingInstanceJSON,
29
36
  type NumberingLevelJSON,
30
37
  type ParagraphPropertiesJSON,
31
38
  parseContent,
32
39
  parseDocument,
33
40
  SCHEMA_VERSION,
41
+ type StoryJSON,
34
42
  type StyleJSON,
35
43
  } from "../v3/document.js";
36
44
  import { migrateToV3 } from "../v3/migrate.js";
37
45
  import { Resolver } from "../v3/resolve.js";
46
+ import { controlsIn, copiedControl, freshControlId } from "./controls.js";
47
+ import { noteIds, noteKindOf } from "./notes.js";
38
48
  import type { EditorState } from "./state.js";
39
- import { type WordContext, type WordEnvelope, wordContext } from "./word.js";
49
+ import {
50
+ otherTrees,
51
+ type Story,
52
+ storyBody,
53
+ type WordContext,
54
+ type WordEnvelope,
55
+ wordContext,
56
+ } from "./word.js";
40
57
  import { builtinStyle, levels } from "./wordformat.js";
41
58
 
42
59
  /** What a v3 copy carries beside its content: the definitions the content references. */
@@ -46,6 +63,16 @@ export interface WordDefinitions {
46
63
  styles: StyleJSON[];
47
64
  numbering: { abstracts: AbstractNumberingJSON[]; instances: NumberingInstanceJSON[] };
48
65
  comments: CommentJSON[];
66
+ /** The notes the content refers to, by story id. */
67
+ notes?: Record<string, StoryJSON>;
68
+ }
69
+
70
+ /** What a paste brings: the content, the envelope it needs, and new notes. */
71
+ export interface FittedPaste {
72
+ doc: Node;
73
+ envelope: WordEnvelope;
74
+ /** The stories of the notes the references in `doc` now name, to add with it. */
75
+ notes?: { id: string; story: Story }[];
49
76
  }
50
77
 
51
78
  type Json = Record<string, unknown>;
@@ -68,7 +95,8 @@ type Properties = Pick<ParagraphPropertiesJSON, "style" | "numbering" | "markFor
68
95
  * comments; with the document's id.
69
96
  */
70
97
  export function copiedDefinitions(context: WordContext, content: Node): WordDefinitions {
71
- const { styles, numbering, comments } = context.envelope;
98
+ const { styles, numbering, comments, stories } = context.envelope;
99
+ const noteStories = new Set<string>();
72
100
  const styleIds = new Set<string>();
73
101
  const numberingIds = new Set<number>();
74
102
  const commentIds = new Set<number>();
@@ -94,6 +122,7 @@ export function copiedDefinitions(context: WordContext, content: Node): WordDefi
94
122
  if (node.type === "table") properties(attrs);
95
123
  if (node.type === "tableRow") properties((attrs as { exceptions?: Properties }).exceptions);
96
124
  if (COMMENT_ANCHORS.has(node.type)) commentIds.add(attrs.id as number);
125
+ if (node.type === "noteReference") noteStories.add(String(node.attrs?.story));
97
126
  marks(node.marks);
98
127
  };
99
128
  visit([content.toJSON()], references);
@@ -102,6 +131,16 @@ export function copiedDefinitions(context: WordContext, content: Node): WordDefi
102
131
  copied.flatMap((c) => c.content),
103
132
  references,
104
133
  );
134
+ const notes = Object.fromEntries(
135
+ [...noteStories].flatMap((id) => {
136
+ const story = stories && Object.hasOwn(stories, id) ? stories[id] : undefined;
137
+ return story ? [[id, story] as const] : [];
138
+ }),
139
+ );
140
+ visit(
141
+ Object.values(notes).map((note) => note.content),
142
+ references,
143
+ );
105
144
  const instances = () => numbering.instances.filter((n) => numberingIds.has(n.id));
106
145
  const abstracts = () => {
107
146
  const ids = new Set(instances().map((n) => n.abstract));
@@ -132,6 +171,7 @@ export function copiedDefinitions(context: WordContext, content: Node): WordDefi
132
171
  styles: styles.filter((s) => styleIds.has(s.id)),
133
172
  numbering: { abstracts: abstracts(), instances: instances() },
134
173
  comments: copied,
174
+ ...(Object.keys(notes).length ? { notes } : {}),
135
175
  };
136
176
  }
137
177
 
@@ -145,12 +185,14 @@ export function fitPastedV2(
145
185
  state: EditorState,
146
186
  content: Node,
147
187
  ): { doc: Node; envelope: WordEnvelope } {
148
- const migrated = migrateToV4(
149
- migrateToV3({
150
- schemaVersion: 2,
151
- settings: DEFAULT_SETTINGS,
152
- content: content.toJSON() as DocJSON,
153
- }),
188
+ const migrated = migrateToV5(
189
+ migrateToV4(
190
+ migrateToV3({
191
+ schemaVersion: 2,
192
+ settings: DEFAULT_SETTINGS,
193
+ content: content.toJSON() as DocJSON,
194
+ }),
195
+ ),
154
196
  );
155
197
  const definitions: WordDefinitions = {
156
198
  styles: migrated.styles.map((style) => {
@@ -210,17 +252,13 @@ function safeLinks(json: NodeJSON): NodeJSON {
210
252
  * text, older copies). Returns the content to insert and the envelope with what it added (the
211
253
  * same object when nothing was added). Throws FoliDocumentError on invalid input.
212
254
  */
213
- export function fitPaste(
214
- state: EditorState,
215
- content: unknown,
216
- definitions?: unknown,
217
- ): { doc: Node; envelope: WordEnvelope } {
255
+ export function fitPaste(state: EditorState, content: unknown, definitions?: unknown): FittedPaste {
218
256
  const context = wordContext(state) as WordContext;
219
257
  const target = context.envelope;
220
258
  // Taken as this document's own, but it cannot be told to be: no unsafe link comes in.
221
259
  if (definitions === undefined || definitions === null)
222
260
  return fit(state, target, safeLinks(parseContent(content, "content", true).toJSON()), true);
223
- const { document, styles, numbering, comments } = definitions as Partial<WordDefinitions>;
261
+ const { document, styles, numbering, comments, notes } = definitions as Partial<WordDefinitions>;
224
262
  // Validated as a document's envelope: types, ranges, no style based on itself; the content
225
263
  // as content to paste (a table may end it).
226
264
  const checked = parseDocument({
@@ -230,10 +268,56 @@ export function fitPaste(
230
268
  styles,
231
269
  numbering,
232
270
  comments,
271
+ ...(notes ? { stories: notes } : {}),
233
272
  content: { type: "doc", content: [{ type: "paragraph" }] },
234
273
  });
235
274
  const json = parseContent(content, "content", true).toJSON();
236
- return fit(state, checked, json, document === context.id);
275
+ const source: WordDefinitions = {
276
+ styles: checked.styles,
277
+ numbering: checked.numbering,
278
+ comments: checked.comments,
279
+ ...(checked.stories ? { notes: checked.stories } : {}),
280
+ };
281
+ return fit(state, source, json, document === context.id);
282
+ }
283
+
284
+ /**
285
+ * Adds to `out` the field and inline control markers of `nodes` without partners (ADR-0007): a
286
+ * field that lost its end loses its start and code too, so the code never becomes text. What they
287
+ * held stays. `open` carries what is open from one node to the next.
288
+ */
289
+ function unpairedMarkers(
290
+ nodes: readonly NodeJSON[],
291
+ out: Set<NodeJSON>,
292
+ open: {
293
+ fields: { parts: NodeJSON[]; separated: boolean }[];
294
+ controls: Map<unknown, NodeJSON[]>;
295
+ } = { fields: [], controls: new Map() },
296
+ top = true,
297
+ ): void {
298
+ for (const node of nodes) {
299
+ const field = open.fields[open.fields.length - 1];
300
+ const id = node.attrs?.id;
301
+ if (node.type === "fieldStart") open.fields.push({ parts: [node], separated: false });
302
+ else if (node.type === "fieldCode") {
303
+ if (field && !field.separated) field.parts.push(node);
304
+ else out.add(node);
305
+ } else if (node.type === "fieldSeparator") {
306
+ if (field && !field.separated) {
307
+ field.parts.push(node);
308
+ field.separated = true;
309
+ } else out.add(node);
310
+ } else if (node.type === "fieldEnd") {
311
+ if (!open.fields.pop()) out.add(node);
312
+ } else if (node.type === "controlStart")
313
+ open.controls.set(id, [...(open.controls.get(id) ?? []), node]);
314
+ else if (node.type === "controlEnd") {
315
+ if (!open.controls.get(id)?.pop()) out.add(node);
316
+ } else if (node.content) unpairedMarkers(node.content, out, open, false);
317
+ }
318
+ if (!top) return;
319
+ for (const { parts } of open.fields) for (const part of parts) out.add(part);
320
+ for (const starts of open.controls.values()) for (const start of starts) out.add(start);
237
321
  }
238
322
 
239
323
  /**
@@ -245,7 +329,7 @@ function fit(
245
329
  source: WordDefinitions,
246
330
  json: NodeJSON,
247
331
  same: boolean,
248
- ): { doc: Node; envelope: WordEnvelope } {
332
+ ): FittedPaste {
249
333
  const target = (wordContext(state) as WordContext).envelope;
250
334
  let styles = target.styles;
251
335
  let { abstracts, instances } = target.numbering;
@@ -361,15 +445,21 @@ function fit(
361
445
  for (const c of target.comments) nextId.comment = Math.max(nextId.comment, c.id + 1);
362
446
  const names = new Set<string>();
363
447
  const { from, to } = state.selection;
364
- state.doc.descendants((node, pos) => {
365
- const id = (node.attrs.id as number) + 1;
366
- if (COMMENT_ANCHORS.has(node.type.name)) nextId.comment = Math.max(nextId.comment, id);
367
- // A bookmark in the selection goes with it.
368
- if (node.type.name === "bookmarkStart" && (pos < from || pos >= to))
369
- names.add(String(node.attrs.name));
370
- if (node.type.name.startsWith("bookmark")) nextId.bookmark = Math.max(nextId.bookmark, id);
371
- return node.type.name === "doc" || node.isTextblock;
372
- });
448
+ /** What every tree says about ids and names in use (ids are allocated across them). */
449
+ const scan = (tree: Node, inSelection: (pos: number) => boolean) =>
450
+ tree.descendants((node, pos) => {
451
+ const id = (node.attrs.id as number) + 1;
452
+ if (COMMENT_ANCHORS.has(node.type.name)) nextId.comment = Math.max(nextId.comment, id);
453
+ // A bookmark in the selection goes with it.
454
+ if (node.type.name === "bookmarkStart" && !inSelection(pos))
455
+ names.add(String(node.attrs.name));
456
+ if (node.type.name.startsWith("bookmark")) nextId.bookmark = Math.max(nextId.bookmark, id);
457
+ return node.type.name === "doc" || node.isBlock;
458
+ });
459
+ scan(state.doc, (pos) => pos >= from && pos < to);
460
+ for (const tree of otherTrees(state)) scan(tree, () => false);
461
+ // A story holds no comments, note references or section breaks (ADR-0007).
462
+ const inStory = storyBody(state) !== null;
373
463
  const fresh = (kind: keyof typeof nextId, map: Map<number, number>, id: number) => {
374
464
  let next = map.get(id);
375
465
  if (next === undefined) {
@@ -383,10 +473,63 @@ function fit(
383
473
  const bookmarkIds = new Map<number, number>();
384
474
  const leftOut = new Set<number>();
385
475
 
476
+ // Controls that stay in the document: what is pasted of them is a copy, under a new id.
477
+ let controls = target.controls;
478
+ const staying = new Set<string>();
479
+ for (const range of controlsIn(state.doc))
480
+ if (range.from < from || range.to > to) staying.add(range.id);
481
+ for (const tree of otherTrees(state)) for (const range of controlsIn(tree)) staying.add(range.id);
482
+ const copiedControls = new Map<string, string>();
483
+ const mapControl = (id: unknown): string | null => {
484
+ if (!same || typeof id !== "string" || !Object.hasOwn(target.controls ?? {}, id)) return null;
485
+ if (!staying.has(id)) return id;
486
+ let copy = copiedControls.get(id);
487
+ if (!copy) {
488
+ copy = freshControlId(controls);
489
+ copiedControls.set(id, copy);
490
+ controls = { ...controls, [copy]: copiedControl(target.controls?.[id] as ControlJSON) };
491
+ }
492
+ return copy;
493
+ };
494
+ // A note has one reference: one still in the document keeps it (cut and paste moves it).
495
+ const referredTo = new Set<string>();
496
+ state.doc.descendants((node, pos) => {
497
+ if (node.type.name === "noteReference" && (pos < from || pos >= to))
498
+ referredTo.add(String(node.attrs.story));
499
+ });
500
+ const noteAvailable = (story: string) =>
501
+ same && Object.hasOwn(target.stories ?? {}, story) && !referredTo.has(story);
502
+ const carried = (story: string) =>
503
+ source.notes && Object.hasOwn(source.notes, story) ? source.notes[story] : undefined;
504
+ const newNoteId = noteIds(state);
505
+ /** The new note of each story a pasted reference was copied with. */
506
+ const pastedNotes = new Map<string, string>();
507
+
508
+ // Half a field or control from elsewhere: its markers go, what it held stays.
509
+ const unpaired = new Set<NodeJSON>();
510
+ unpairedMarkers([json], unpaired);
511
+ for (const comment of source.comments) unpairedMarkers(comment.content, unpaired);
512
+ for (const note of Object.values(source.notes ?? {})) unpairedMarkers([note.content], unpaired);
513
+
386
514
  const mapNode = (node: NodeJSON): NodeJSON | null => {
515
+ if (unpaired.has(node)) return null;
387
516
  const attrs = node.attrs as Json | undefined;
517
+ if (inStory && (COMMENT_ANCHORS.has(node.type) || node.type === "noteReference")) return null;
388
518
  let next: Json | undefined = attrs;
389
- if (node.type === "paragraph" && attrs) {
519
+ if (node.type === "noteReference") {
520
+ const story = String(attrs?.story);
521
+ const kind = noteKindOf(story);
522
+ if (kind && carried(story)) {
523
+ const id = pastedNotes.get(story) ?? newNoteId(kind);
524
+ pastedNotes.set(story, id);
525
+ next = { ...attrs, story: id };
526
+ } else if (!noteAvailable(story)) return null;
527
+ } else if (node.type === "controlStart" || node.type === "controlEnd") {
528
+ // From another document their definitions are not here: the content comes without them.
529
+ const id = mapControl(attrs?.id);
530
+ if (!id) return null;
531
+ next = { ...attrs, id };
532
+ } else if (node.type === "paragraph" && attrs) {
390
533
  next = paragraphProperties(attrs as ParagraphPropertiesJSON) as Json;
391
534
  const change = attrs.formatChange as { previous: ParagraphPropertiesJSON } | null | undefined;
392
535
  if (change) next.formatChange = { ...change, previous: paragraphProperties(change.previous) };
@@ -417,6 +560,16 @@ function fit(
417
560
  if (node.type === "bookmarkStart") names.add(String(attrs?.name));
418
561
  next = { ...attrs, id: fresh("bookmark", bookmarkIds, id) };
419
562
  }
563
+ if (inStory && next?.section) {
564
+ next = { ...next };
565
+ delete next.section;
566
+ }
567
+ if (next?.controls) {
568
+ const ids = (next.controls as string[]).flatMap((id) => mapControl(id) ?? []);
569
+ next = { ...next };
570
+ if (ids.length) next.controls = ids;
571
+ else delete next.controls;
572
+ }
420
573
  const marks = node.marks && runMarks(node.marks);
421
574
  const children = node.content
422
575
  ?.map(mapNode)
@@ -429,7 +582,28 @@ function fit(
429
582
  } as NodeJSON;
430
583
  };
431
584
 
432
- const doc = parseContent(mapNode(json), "content", true);
585
+ // Pasted into a block control, the new paragraphs are in it as the one at the caret is.
586
+ const around = (
587
+ from === to || state.selection.$from.parent === state.selection.$to.parent
588
+ ? state.selection.$from.parent.attrs.controls
589
+ : null
590
+ ) as string[] | null | undefined;
591
+ const mapped = mapNode(json) as NodeJSON;
592
+ const inPlace = around?.length
593
+ ? {
594
+ ...mapped,
595
+ content: mapped.content?.map((block) => {
596
+ const attrs = block.attrs as Json | undefined;
597
+ const own = (attrs?.controls as string[] | undefined) ?? [];
598
+ return { ...block, attrs: { ...attrs, controls: [...around, ...own] } };
599
+ }),
600
+ }
601
+ : mapped;
602
+ const doc = parseContent(inPlace, "content", true);
603
+ const notes = [...pastedNotes].map(([from, id]) => {
604
+ const note = carried(from) as StoryJSON;
605
+ return { id, story: { kind: note.kind, doc: parseContent(mapNode(note.content)) } };
606
+ });
433
607
  let comments = target.comments;
434
608
  const copiedComments = source.comments.filter((c) => commentIds.has(c.id));
435
609
  if (copiedComments.length) {
@@ -452,11 +626,19 @@ function fit(
452
626
  styles !== target.styles ||
453
627
  abstracts !== target.numbering.abstracts ||
454
628
  instances !== target.numbering.instances ||
455
- comments !== target.comments;
629
+ comments !== target.comments ||
630
+ controls !== target.controls;
456
631
  return {
457
632
  doc,
458
633
  envelope: changed
459
- ? { ...target, styles, numbering: { abstracts, instances }, comments }
634
+ ? {
635
+ ...target,
636
+ styles,
637
+ numbering: { abstracts, instances },
638
+ comments,
639
+ ...(controls ? { controls } : {}),
640
+ }
460
641
  : target,
642
+ ...(notes.length ? { notes } : {}),
461
643
  };
462
644
  }
@@ -111,7 +111,7 @@ const builtinName = (id: string) =>
111
111
 
112
112
  /**
113
113
  * Word's definition of built-in style `name` ("heading 1"–"heading 9", "List Paragraph",
114
- * "Hyperlink"), as Word adds it to a document the first time it is used (the Word 2013–2019
114
+ * "Hyperlink", "Placeholder Text"), as Word adds it to a document the first time it is used (the Word 2013–2019
115
115
  * template's values; not checked against newer Word).
116
116
  */
117
117
  export function builtinStyle(context: HasEnvelope, name: string): StyleJSON | null {
@@ -158,6 +158,20 @@ export function builtinStyle(context: HasEnvelope, name: string): StyleJSON | nu
158
158
  ],
159
159
  };
160
160
  }
161
+ const contents = /^toc ([1-9])$/.exec(name)?.[1];
162
+ if (contents) {
163
+ // Word indents each level by the default font's size (11 pt: 220 twips).
164
+ const size = context.envelope.defaults.run?.find((m) => m.type === "fontSize")?.attrs?.value;
165
+ const indent = (Number(contents) - 1) * (typeof size === "number" ? size * 10 : 220);
166
+ return {
167
+ id: `TOC${contents}`,
168
+ type: "paragraph",
169
+ name,
170
+ ...based,
171
+ ...(normal ? { next: normal } : {}),
172
+ paragraph: { spacing: { after: 100 }, ...(indent ? { indent: { left: indent } } : {}) },
173
+ };
174
+ }
161
175
  if (name === "List Paragraph")
162
176
  return {
163
177
  id: "ListParagraph",
@@ -188,6 +202,52 @@ export function builtinStyle(context: HasEnvelope, name: string): StyleJSON | nu
188
202
  },
189
203
  };
190
204
  }
205
+ const note = /^(foot|end)note (text|reference)$/.exec(name);
206
+ const textChar = /^(Foot|End)note Text Char$/.exec(name);
207
+ if (note || textChar) {
208
+ // Word's: 10 pt text without space after, and a superscript reference.
209
+ const font = defaultParagraphStyleOf(context, "character");
210
+ const fontBased = font ? { basedOn: font } : {};
211
+ const size = { type: "fontSize", attrs: { value: 20, complexScript: 20 } };
212
+ const stem =
213
+ (note?.[1] ?? textChar?.[1] ?? "").toLowerCase() === "foot" ? "Footnote" : "Endnote";
214
+ if (textChar)
215
+ return {
216
+ id: `${stem}TextChar`,
217
+ type: "character",
218
+ name,
219
+ ...fontBased,
220
+ link: `${stem}Text`,
221
+ run: [size],
222
+ };
223
+ if (note?.[2] === "reference")
224
+ return {
225
+ id: `${stem}Reference`,
226
+ type: "character",
227
+ name,
228
+ ...fontBased,
229
+ run: [{ type: "verticalAlign", attrs: { value: "superscript" } }],
230
+ };
231
+ return {
232
+ id: `${stem}Text`,
233
+ type: "paragraph",
234
+ name,
235
+ ...based,
236
+ link: `${stem}TextChar`,
237
+ paragraph: { spacing: { after: 0, line: 240, lineRule: "auto" } },
238
+ run: [size],
239
+ };
240
+ }
241
+ if (name === "Placeholder Text") {
242
+ const font = defaultParagraphStyleOf(context, "character");
243
+ return {
244
+ id: "PlaceholderText",
245
+ type: "character",
246
+ name,
247
+ ...(font ? { basedOn: font } : {}),
248
+ run: [{ type: "color", attrs: { value: "666666" } }],
249
+ };
250
+ }
191
251
  if (name === "Hyperlink") {
192
252
  const font = defaultParagraphStyleOf(context, "character");
193
253
  return {