@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
@@ -10,10 +10,14 @@ import {
10
10
  Slice,
11
11
  } from "../model/index.js";
12
12
  import { isSafeLink } from "../schema.js";
13
+ import { TransformError } from "../transform/index.js";
14
+ import type { SectionJSON } from "../v3/document.js";
13
15
  import { attempt } from "./attempt.js";
16
+ import { fieldOfPart, fieldSelection, isFieldPart } from "./fields.js";
14
17
  import { redo, undo } from "./history.js";
15
18
  import { openLinkEditor } from "./links.js";
16
19
  import { keepLastMark, typedMarks } from "./mark.js";
20
+ import { absorbSection } from "./sections.js";
17
21
  import { AllSelection, marksAt, NodeSelection, Selection, TextSelection } from "./selection.js";
18
22
  import {
19
23
  type Command,
@@ -22,6 +26,7 @@ import {
22
26
  Plugin,
23
27
  type Transaction,
24
28
  } from "./state.js";
29
+ import { CellSelection, deleteAcrossTables, emptyCells, keepTablesApart } from "./tables.js";
25
30
  import { trackState } from "./track.js";
26
31
  import {
27
32
  isMarker,
@@ -60,9 +65,30 @@ function caret(state: EditorState): ResolvedPos | null {
60
65
 
61
66
  export const deleteSelection: Command = (state, dispatch) => {
62
67
  if (state.selection.empty) return false;
63
- return attempt(state, dispatch, (tr) => tr.deleteSelection());
68
+ return attempt(state, dispatch, (tr) => deleteSelectionKeepingTables(tr));
64
69
  };
65
70
 
71
+ /**
72
+ * Deletes `tr`'s selection; across a table's edge it takes whole rows (or empties cells), never
73
+ * joining text across the edge (`deleteAcrossTables`), and Word's table rules hold after it.
74
+ * Selected cells are emptied (`onlyAnchorCell`: just the anchor cell, what typing or pasting
75
+ * over them does), the caret left in the anchor cell.
76
+ */
77
+ export function deleteSelectionKeepingTables(tr: Transaction, onlyAnchorCell = false): void {
78
+ const selection = tr.selection;
79
+ if (selection instanceof CellSelection) {
80
+ // Selected cells are emptied (typing empties the anchor cell only), the caret in the anchor.
81
+ const anchor = selection.anchor;
82
+ emptyCells(tr, onlyAnchorCell ? [anchor] : selection.cells);
83
+ tr.setSelection(Selection.near(tr.doc.resolve(tr.mapping.map(anchor, 1) + 1), 1));
84
+ return;
85
+ }
86
+ const { from, to } = selection;
87
+ if (!deleteAcrossTables(tr, from, to)) tr.deleteSelection();
88
+ else tr.setSelection(Selection.near(tr.doc.resolve(tr.mapping.map(from, -1)), 1));
89
+ keepTablesApart(tr);
90
+ }
91
+
66
92
  /** Replaces the selection with text, using stored marks or the marks at the selection start. */
67
93
  export function insertText(text: string): Command {
68
94
  return (state, dispatch) => {
@@ -76,14 +102,24 @@ export function insertText(text: string): Command {
76
102
  state.schema.text(text, state.storedMarks ?? []),
77
103
  );
78
104
  }
79
- if (!(selection instanceof AllSelection) && !selection.$from.parent.inlineContent) return false;
105
+ if (
106
+ !(selection instanceof AllSelection) &&
107
+ !(selection instanceof CellSelection) &&
108
+ !selection.$from.parent.inlineContent
109
+ )
110
+ return false;
80
111
  return attempt(state, dispatch, (tr) => {
81
- const $from = selection.$from;
112
+ // Over cells, the text typed into the anchor cell keeps that cell's first text's marks.
113
+ const $from =
114
+ selection instanceof CellSelection
115
+ ? Selection.near(state.doc.resolve(selection.anchor + 1), 1).$from
116
+ : selection.$from;
82
117
  const marks =
83
118
  state.storedMarks ??
84
119
  (selection.empty ? marksAt($from) : ($from.nodeAfter?.marks ?? marksAt($from)));
85
- tr.deleteSelection();
120
+ if (!selection.empty) deleteSelectionKeepingTables(tr, true);
86
121
  const $pos = tr.selection.$from;
122
+ if (!$pos.parent.inlineContent) throw new TransformError("No text where the selection was");
87
123
  const allowed = typedMarks(marks).filter((mark) =>
88
124
  $pos.parent.type.allowsMarkType(mark.type),
89
125
  );
@@ -143,6 +179,23 @@ function skipsDeleted(state: EditorState): boolean {
143
179
  /** Content a tracked change deleted (still in the document until accepted). */
144
180
  const isDeleted = (node: Node) => node.marks.some((m) => m.type.name === "deletion");
145
181
 
182
+ /**
183
+ * Backspace or Delete arriving at a field's marker (`node`, at `pos`) selects the field whole, as
184
+ * Word does: the next press deletes it. False for anything else, and a marker without a field.
185
+ */
186
+ function selectFieldAt(
187
+ state: EditorState,
188
+ dispatch: ((tr: Transaction) => void) | undefined,
189
+ node: Node | null | undefined,
190
+ pos: number,
191
+ ): boolean {
192
+ if (!isFieldPart(node)) return false;
193
+ const field = fieldOfPart(state.doc, pos);
194
+ if (!field || field.deleted) return false;
195
+ dispatch?.(state.tr.setSelection(fieldSelection(state.doc, field)));
196
+ return true;
197
+ }
198
+
146
199
  /** Backspace: deletes the selection, else the previous character or word, else joins with the previous block. */
147
200
  export function deleteBackward(unit: DeleteUnit = "char"): Command {
148
201
  return (state, dispatch) => {
@@ -156,9 +209,10 @@ export function deleteBackward(unit: DeleteUnit = "char"): Command {
156
209
  let $pos = found;
157
210
  while ($pos.parentOffset > 0) {
158
211
  const before = $pos.nodeBefore;
159
- if (isMarker(word, before)) $pos = state.doc.resolve($pos.pos - 1);
160
- else if (tracking && before && isDeleted(before))
212
+ if (tracking && before && isDeleted(before))
161
213
  $pos = state.doc.resolve($pos.pos - before.nodeSize);
214
+ else if (selectFieldAt(state, dispatch, before, $pos.pos - 1)) return true;
215
+ else if (isMarker(word, before)) $pos = state.doc.resolve($pos.pos - 1);
162
216
  else break;
163
217
  }
164
218
  if ($pos.parentOffset === 0) return joinBackward(at(state, $pos.pos), dispatch);
@@ -183,9 +237,10 @@ export function deleteForward(unit: DeleteUnit = "char"): Command {
183
237
  let $pos = found;
184
238
  while ($pos.parentOffset < $pos.parent.content.size) {
185
239
  const after = $pos.nodeAfter;
186
- if (isMarker(word, after)) $pos = state.doc.resolve($pos.pos + 1);
187
- else if (tracking && after && isDeleted(after))
240
+ if (tracking && after && isDeleted(after))
188
241
  $pos = state.doc.resolve($pos.pos + after.nodeSize);
242
+ else if (selectFieldAt(state, dispatch, after, $pos.pos)) return true;
243
+ else if (isMarker(word, after)) $pos = state.doc.resolve($pos.pos + 1);
189
244
  else break;
190
245
  }
191
246
  if ($pos.parentOffset === $pos.parent.content.size)
@@ -199,6 +254,15 @@ export function deleteForward(unit: DeleteUnit = "char"): Command {
199
254
  };
200
255
  }
201
256
 
257
+ /**
258
+ * `first` was joined with the next paragraph at `pos`: if its mark ended a section, the next one
259
+ * takes over. While tracking the mark stays, recorded as deleted: accepting it does this.
260
+ */
261
+ function sectionBreakGone(state: EditorState, tr: Transaction, first: Node, pos: number): void {
262
+ const section = first.attrs.section as SectionJSON | null | undefined;
263
+ if (section && !trackState(state)?.on) absorbSection(state, tr, section, pos);
264
+ }
265
+
202
266
  /**
203
267
  * At the start of a textblock: merges it into the previous textblock, or selects a previous
204
268
  * opaque block (a table), as Word does. Does nothing at the start of the first block.
@@ -215,8 +279,14 @@ export const joinBackward: Command = (state, dispatch) => {
215
279
  tr.join(boundary);
216
280
  // The merged paragraph ends with this one's mark (schema v3: its section break).
217
281
  keepLastMark(tr, boundary - before.nodeSize, $pos.parent);
282
+ sectionBreakGone(state, tr, before, boundary - before.nodeSize);
218
283
  });
219
- if (before.isLeaf && isOpaque(wordContext(state), before)) {
284
+ // A table: the caret goes to the end of its last cell, as Word moves into it.
285
+ if (before.type.name === "table") {
286
+ dispatch?.(state.tr.setSelection(Selection.near(state.doc.resolve(boundary - 1), -1)));
287
+ return true;
288
+ }
289
+ if (isOpaque(wordContext(state), before)) {
220
290
  dispatch?.(state.tr.setSelection(selectionAt(state, boundary - before.nodeSize)));
221
291
  return true;
222
292
  }
@@ -238,9 +308,15 @@ export const joinForward: Command = (state, dispatch) => {
238
308
  return attempt(state, dispatch, (tr) => {
239
309
  tr.join(boundary);
240
310
  keepLastMark(tr, $pos.before(), after);
311
+ sectionBreakGone(state, tr, $pos.parent, $pos.before());
241
312
  });
242
313
  }
243
- if (after.isLeaf && isOpaque(wordContext(state), after)) {
314
+ // A table: the caret goes to the start of its first cell.
315
+ if (after.type.name === "table") {
316
+ dispatch?.(state.tr.setSelection(Selection.near(state.doc.resolve(boundary + 1), 1)));
317
+ return true;
318
+ }
319
+ if (isOpaque(wordContext(state), after)) {
244
320
  dispatch?.(state.tr.setSelection(selectionAt(state, boundary)));
245
321
  return true;
246
322
  }
@@ -264,7 +340,9 @@ export const splitBlock: Command = (state, dispatch) => {
264
340
  if (!(selection instanceof AllSelection) && !selection.$from.parent.isTextblock) return false;
265
341
  return attempt(state, dispatch, (tr) => {
266
342
  const marks = state.storedMarks ?? marksAt(selection.$from);
267
- tr.deleteSelection();
343
+ if (!selection.empty) deleteSelectionKeepingTables(tr);
344
+ if (!tr.selection.$from.parent.inlineContent)
345
+ throw new TransformError("No text where the selection was");
268
346
  const $pos = tr.selection.$from;
269
347
  const atEnd = $pos.parentOffset === $pos.parent.content.size;
270
348
  const word = wordContext(state);
@@ -376,6 +454,10 @@ export const insertHardBreak: Command = (state, dispatch) =>
376
454
  export const insertPageBreak: Command = (state, dispatch) =>
377
455
  insertLeaf("pageBreak", state, dispatch);
378
456
 
457
+ /** A column break at the selection: what follows it starts the next column. */
458
+ export const insertColumnBreak: Command = (state, dispatch) =>
459
+ insertLeaf("columnBreak", state, dispatch);
460
+
379
461
  /** Replaces the selection with an inline leaf node of `name`, keeping the marks typing gets. */
380
462
  function insertLeaf(name: string, ...[state, dispatch]: Parameters<Command>): boolean {
381
463
  const type = state.schema.nodes[name];
@@ -385,7 +467,9 @@ function insertLeaf(name: string, ...[state, dispatch]: Parameters<Command>): bo
385
467
  }
386
468
  return attempt(state, dispatch, (tr) => {
387
469
  const marks = state.storedMarks ?? marksAt(selection.$from);
388
- tr.deleteSelection();
470
+ if (!selection.empty) deleteSelectionKeepingTables(tr);
471
+ if (!tr.selection.$from.parent.inlineContent)
472
+ throw new TransformError("No text where the selection was");
389
473
  const pos = tr.selection.from;
390
474
  tr.insert(
391
475
  pos,
@@ -408,16 +492,17 @@ function markType(state: EditorState, type: string | MarkType): MarkType | undef
408
492
  return typeof type === "string" ? state.schema.marks[type] : type;
409
493
  }
410
494
 
411
- /** True if every inline node overlapping `from..to` (in parents that allow the mark) has it. */
412
- function rangeHasMark(state: EditorState, from: number, to: number, type: MarkType): boolean {
495
+ /** True if every inline node in the selection's ranges (in parents that allow the mark) has it. */
496
+ function rangeHasMark(state: EditorState, type: MarkType): boolean {
413
497
  let all = true;
414
498
  let any = false;
415
- state.doc.nodesBetween(from, to, (node, _pos, parent) => {
416
- if (!all || !node.isInline || !parent?.type.allowsMarkType(type)) return undefined;
417
- any = true;
418
- if (!type.isInSet(node.marks)) all = false;
419
- return false;
420
- });
499
+ for (const { from, to } of state.selection.ranges)
500
+ state.doc.nodesBetween(from, to, (node, _pos, parent) => {
501
+ if (!all || !node.isInline || !parent?.type.allowsMarkType(type)) return undefined;
502
+ any = true;
503
+ if (!type.isInSet(node.marks)) all = false;
504
+ return false;
505
+ });
421
506
  return any && all;
422
507
  }
423
508
 
@@ -449,28 +534,32 @@ export function toggleMark(type: string | MarkType, attrs?: Attrs | null): Comma
449
534
  );
450
535
  return true;
451
536
  }
537
+ const { ranges } = selection;
452
538
  let applicable = false;
453
- state.doc.nodesBetween(selection.from, selection.to, (node) => {
454
- if (node.isTextblock && node.type.allowsMarkType(mark)) applicable = true;
455
- return !applicable;
456
- });
539
+ for (const { from, to } of ranges)
540
+ state.doc.nodesBetween(from, to, (node) => {
541
+ if (node.isTextblock && node.type.allowsMarkType(mark)) applicable = true;
542
+ return !applicable;
543
+ });
457
544
  if (!applicable) return false;
458
545
  const word = wordContext(state);
459
546
  if (word && isWordToggle(word, mark)) {
460
547
  const on = !wordToggleActive(word, state, mark, null);
461
548
  return attempt(state, dispatch, (tr) => {
462
- for (const range of toggleRanges(word, state, selection.from, selection.to, mark, on)) {
463
- tr.removeMark(range.from, range.to, mark);
464
- if (range.mark) tr.addMark(range.from, range.to, range.mark);
465
- }
549
+ for (const { from, to } of ranges)
550
+ for (const range of toggleRanges(word, state, from, to, mark, on)) {
551
+ tr.removeMark(range.from, range.to, mark);
552
+ if (range.mark) tr.addMark(range.from, range.to, range.mark);
553
+ }
466
554
  });
467
555
  }
468
- const remove = rangeHasMark(state, selection.from, selection.to, mark);
556
+ const remove = rangeHasMark(state, mark);
469
557
  const created = remove ? null : createMark(mark, attrs);
470
558
  if (!remove && !created) return false;
471
559
  return attempt(state, dispatch, (tr) => {
472
- if (remove) tr.removeMark(selection.from, selection.to, mark);
473
- else tr.addMark(selection.from, selection.to, created as Mark);
560
+ for (const { from, to } of ranges)
561
+ if (remove) tr.removeMark(from, to, mark);
562
+ else tr.addMark(from, to, created as Mark);
474
563
  });
475
564
  };
476
565
  }
@@ -501,7 +590,7 @@ export function isMarkActive(state: EditorState, type: string | MarkType): boole
501
590
  $caret ? (state.storedMarks ?? marksAt($caret)) : null,
502
591
  );
503
592
  if ($caret) return mark.isInSet(state.storedMarks ?? marksAt($caret)) !== undefined;
504
- return rangeHasMark(state, state.selection.from, state.selection.to, mark);
593
+ return rangeHasMark(state, mark);
505
594
  }
506
595
 
507
596
  /**
@@ -13,7 +13,7 @@ import { closeHistory } from "./history.js";
13
13
  import { AllSelection, Selection, TextSelection } from "./selection.js";
14
14
  import { type Command, type EditorState, Plugin, PluginKey, type Transaction } from "./state.js";
15
15
  import { now, trackState, untracked } from "./track.js";
16
- import { setComments, wordContext } from "./word.js";
16
+ import { setComments, storyBody, wordContext } from "./word.js";
17
17
 
18
18
  const ANCHORS = new Set(["commentStart", "commentEnd", "commentReference"]);
19
19
 
@@ -41,7 +41,7 @@ const INITIALS = 9;
41
41
  const initialsOf = (options: CommentOptions) =>
42
42
  options.initials !== undefined ? { initials: options.initials.slice(0, INITIALS) } : {};
43
43
 
44
- /** Comment anchors inside content Foli keeps verbatim (a table): their ids. */
44
+ /** Comment anchors inside content Foli keeps verbatim (an opaque block): their ids. */
45
45
  function opaqueCommentIds(doc: Node): Set<number> {
46
46
  const ids = new Set<number>();
47
47
  doc.descendants((node) => {
@@ -117,7 +117,9 @@ export function addComment(content: CommentContent, options: CommentOptions = {}
117
117
  end: schema.nodes.commentEnd,
118
118
  reference: schema.nodes.commentReference,
119
119
  };
120
- if (!context || !types.start || !types.end || !types.reference) return false;
120
+ // Comments anchor in the body only (ADR-0007).
121
+ if (!context || storyBody(state) || !types.start || !types.end || !types.reference)
122
+ return false;
121
123
  // Select-all comments the whole text: from the first textblock to the last.
122
124
  const all = state.selection instanceof AllSelection;
123
125
  const $from = all ? Selection.atStart(state.doc).$from : state.selection.$from;
@@ -262,11 +264,14 @@ function removeComments(tr: Transaction, comments: readonly CommentJSON[], ids:
262
264
 
263
265
  /** Deletes comment `id` with its replies and their anchors. */
264
266
  export function deleteComment(id: number): Command {
265
- return (state, dispatch) => {
267
+ const command: Command = (state, dispatch) => {
268
+ // The anchors are in the body.
269
+ const body = storyBody(state);
270
+ if (body) return dispatch ? body.run(command) : body.can(command);
266
271
  const comments = wordContext(state)?.envelope.comments ?? [];
267
272
  const ids = thread(comments, id);
268
- // shortcut: a comment anchored in content Foli keeps verbatim (a table) stays, its anchors
269
- // there cannot be removed; Phase 10 models tables.
273
+ // shortcut: a comment anchored in content Foli keeps verbatim (an opaque block) stays, its
274
+ // anchors there cannot be removed; model that content to remove them.
270
275
  const verbatim = opaqueCommentIds(state.doc);
271
276
  if (!comments.some((c) => c.id === id) || [...ids].some((i) => verbatim.has(i))) return false;
272
277
  if (dispatch) {
@@ -276,10 +281,13 @@ export function deleteComment(id: number): Command {
276
281
  }
277
282
  return true;
278
283
  };
284
+ return command;
279
285
  }
280
286
 
281
287
  /** Deletes every comment and its anchors (not the threads anchored in verbatim content). */
282
288
  export const deleteAllComments: Command = (state, dispatch) => {
289
+ const body = storyBody(state);
290
+ if (body) return dispatch ? body.run(deleteAllComments) : body.can(deleteAllComments);
283
291
  const comments = wordContext(state)?.envelope.comments ?? [];
284
292
  const verbatim = opaqueCommentIds(state.doc);
285
293
  const kept = new Set<number>();