@stll/folio-core 0.76.0 → 0.77.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 (86) hide show
  1. package/dist/ai-edits/headless.d.ts +6 -4
  2. package/dist/ai-edits/headless.js +32 -15
  3. package/dist/ai-edits/pending-suggestions.d.ts +9 -0
  4. package/dist/ai-edits/pending-suggestions.js +11 -0
  5. package/dist/ai-edits/read.js +24 -1
  6. package/dist/ai-edits/table-geometry.js +12 -4
  7. package/dist/canonical/canonicalInlineProjection.d.ts +2 -2
  8. package/dist/canonical/canonicalInlineProjection.js +2 -2
  9. package/dist/canonical/canonicalProjectionBlocks.d.ts +10 -1
  10. package/dist/canonical/canonicalPublicOperations.js +13 -38
  11. package/dist/canonical/canonicalSeedContent.d.ts +18 -3
  12. package/dist/canonical/canonicalSeedContent.js +28 -15
  13. package/dist/canonical/canonicalSession.d.ts +22 -12
  14. package/dist/canonical/canonicalSession.js +178 -186
  15. package/dist/controller/canonicalClipboard.d.ts +2 -1
  16. package/dist/controller/canonicalClipboard.js +312 -47
  17. package/dist/controller/canonicalComposition.js +67 -14
  18. package/dist/controller/canonicalStoryEditor.js +2 -0
  19. package/dist/controller/hiddenEditorManager.js +2 -0
  20. package/dist/docx/importedIdCollisions.js +29 -4
  21. package/dist/docx/server/templatePlan/apply.js +47 -2
  22. package/dist/docx/server/templatePlan/blocks.d.ts +27 -7
  23. package/dist/docx/server/templatePlan/blocks.js +103 -12
  24. package/dist/docx/server/templatePlan/builders.d.ts +6 -0
  25. package/dist/docx/server/templatePlan/builders.js +7 -1
  26. package/dist/docx/server/templatePlan/emit.d.ts +2 -0
  27. package/dist/docx/server/templatePlan/emit.js +108 -6
  28. package/dist/docx/server/templatePlan/plan.d.ts +24 -1
  29. package/dist/docx/server/templatePlan/plan.js +165 -12
  30. package/dist/docx/server/templatePlan/planShape.js +13 -2
  31. package/dist/docx/server/templatePlan/revisions.d.ts +55 -0
  32. package/dist/docx/server/templatePlan/revisions.js +276 -2
  33. package/dist/docx/server/templatePlan/story.d.ts +46 -1
  34. package/dist/docx/server/templatePlan/story.js +125 -11
  35. package/dist/docx/server/templatePlan/types.d.ts +97 -1
  36. package/dist/docx/server/templatePlan/verify.js +63 -9
  37. package/dist/docx/settingsHeaderFooterUpdate.d.ts +1 -1
  38. package/dist/docx/tableGridXml.d.ts +3 -7
  39. package/dist/docx/tableGridXml.js +5 -13
  40. package/dist/layout-bridge/convert/footnoteLayout.js +34 -8
  41. package/dist/prosemirror/canonicalMarkupDocument.d.ts +20 -0
  42. package/dist/prosemirror/canonicalMarkupDocument.js +73 -0
  43. package/dist/prosemirror/canonicalTableCommands.d.ts +1 -1
  44. package/dist/prosemirror/canonicalTableCommands.js +1 -3
  45. package/dist/prosemirror/conversion/markupViewSource.d.ts +17 -0
  46. package/dist/prosemirror/conversion/markupViewSource.js +11 -0
  47. package/dist/prosemirror/conversion/toProseDoc.d.ts +3 -0
  48. package/dist/prosemirror/conversion/toProseDoc.js +31 -14
  49. package/dist/prosemirror/independentMarkupProjection.d.ts +27 -0
  50. package/dist/prosemirror/independentMarkupProjection.js +147 -0
  51. package/dist/prosemirror/markupViewNotes.d.ts +1 -1
  52. package/dist/prosemirror/markupViewNotes.js +24 -27
  53. package/dist/prosemirror/markupViewPositionMap.d.ts +16 -0
  54. package/dist/prosemirror/markupViewPositionMap.js +316 -0
  55. package/dist/prosemirror/markupViewProjection.d.ts +3 -46
  56. package/dist/prosemirror/markupViewProjection.js +90 -17
  57. package/dist/prosemirror/replacedAnnotations.d.ts +4 -78
  58. package/dist/prosemirror/replacedAnnotations.js +11 -194
  59. package/dist/prosemirror/styles/styleResolver.d.ts +1 -0
  60. package/dist/prosemirror/styles/styleResolver.js +4 -1
  61. package/dist/server.d.ts +2 -2
  62. package/dist/types/canonicalCapabilities.d.ts +3 -10
  63. package/dist/types/canonicalCapabilities.js +3 -10
  64. package/package.json +2 -2
  65. package/dist/internal/revisionResolutionEdits.d.ts +0 -11
  66. package/dist/internal/revisionResolutionEdits.js +0 -18
  67. package/dist/internal/revisionResolutionInline.d.ts +0 -24
  68. package/dist/internal/revisionResolutionInline.js +0 -302
  69. package/dist/internal/revisionResolutionStep.d.ts +0 -3
  70. package/dist/internal/revisionResolutionStep.js +0 -113
  71. package/dist/internal/revisionResolutionTracking.d.ts +0 -9
  72. package/dist/internal/revisionResolutionTracking.js +0 -124
  73. package/dist/internal/wholeStoryRevisionResolution.d.ts +0 -56
  74. package/dist/internal/wholeStoryRevisionResolution.js +0 -783
  75. package/dist/prosemirror/commands/resolveAllTableChanges.d.ts +0 -38
  76. package/dist/prosemirror/commands/resolveAllTableChanges.js +0 -642
  77. package/dist/prosemirror/commands/resolveNodePropertyChangeAttrs.d.ts +0 -13
  78. package/dist/prosemirror/commands/resolveNodePropertyChangeAttrs.js +0 -45
  79. package/dist/prosemirror/commands/tableCellMergeResolution.d.ts +0 -8
  80. package/dist/prosemirror/commands/tableCellMergeResolution.js +0 -56
  81. package/dist/prosemirror/commands/tableMergeFoldDecisions.d.ts +0 -60
  82. package/dist/prosemirror/commands/tableMergeFoldDecisions.js +0 -78
  83. package/dist/prosemirror/rejoinRunCarriers.d.ts +0 -42
  84. package/dist/prosemirror/rejoinRunCarriers.js +0 -65
  85. package/dist/prosemirror/runPropertyChangeResolution.d.ts +0 -15
  86. package/dist/prosemirror/runPropertyChangeResolution.js +0 -34
@@ -12,10 +12,11 @@ import { canonicalOperationStagingAccess } from "../internal/canonicalOperationS
12
12
  import { recordCanonicalTyping } from "../internal/canonicalTyping.js";
13
13
  import { canonicalCellSelectionRanges, canonicalContentPosition, canonicalSelectionRange } from "../prosemirror/canonicalSelectionRange.js";
14
14
  import { expectCommentRangeBoundaryAttrs } from "../prosemirror/commentRangeBoundaryAttrs.js";
15
+ import { markupViewSources, markupViewStateSources } from "../prosemirror/conversion/markupViewSource.js";
15
16
  import { storyConversionOptions } from "../prosemirror/conversion/storyConversionOptions.js";
16
17
  import { ProjectionBlockCache, collectPairedBookmarkIds, footnoteToProseDoc, headerFooterToProseDoc, toProseDoc } from "../prosemirror/conversion/toProseDoc.js";
18
+ import "../prosemirror/extensions/nodes/CommentRangeBoundaryExtension.js";
17
19
  import { heldUnreferencedNotes } from "../prosemirror/noteReferenceReview.js";
18
- import { preservesReplacementCommentAlignment, splitAtKeptCommentBoundaries } from "../prosemirror/replacedAnnotations.js";
19
20
  import { runFormattingPatchFromMarks } from "../prosemirror/runFormattingFromMarks.js";
20
21
  import { CANONICAL_CONDITION, CANONICAL_GAP, CANONICAL_RULE } from "../types/canonicalCapabilities.js";
21
22
  import { canonicalJson } from "../utils/canonicalJson.js";
@@ -37,7 +38,7 @@ import { ownedSourceCopies, preservePropertySources, preservePropertySourcesWhol
37
38
  import { Result, TaggedError, panic } from "better-result";
38
39
  import { hasIllegalXmlCharacters } from "@stll/docx-core";
39
40
  import { lockedControlMessage } from "@stll/docx-core/model";
40
- import { DOCUMENT_OP_REFUSAL_REASONS, DOCUMENT_OP_TYPES, ID_LEASE_KINDS, OP_STORIES, applyDocumentOp, applyDocumentOps, combineEdits, combineRecordedRevisions, compileEditorIntent, createEditorIntentIdAllocator, documentStories, editorParagraphGroups, findStoryBody, formatParaId, freshLeasedIds, inverseMeetingDocument, isBlockBoundary, normalizeForOps, packageParagraphIds, paragraphIdsIn, paragraphsThroughTextBoxes, planWholeParagraphReplacement, sameStory, suggestedCommentIds, suggestionRevisionInfos, survivingComments, validateOpsDocument, withParagraphCopies } from "@stll/docx-core/ops";
41
+ import { DOCUMENT_OP_REFUSAL_REASONS, DOCUMENT_OP_TYPES, ID_LEASE_KINDS, OP_STORIES, applyDocumentOp, applyDocumentOps, combineEdits, combineRecordedRevisions, compileEditorIntent, createEditorIntentIdAllocator, documentStories, editorParagraphGroups, findStoryBody, formatParaId, freshLeasedIds, inverseMeetingDocument, isBlockBoundary, normalizeForOps, packageParagraphIds, paragraphIdsIn, paragraphsThroughTextBoxes, sameStory, suggestedCommentIds, suggestionRevisionInfos, survivingComments, validateOpsDocument, withParagraphCopies } from "@stll/docx-core/ops";
41
42
  import { AllSelection, NodeSelection, Selection, TextSelection } from "prosemirror-state";
42
43
  import { CellSelection } from "prosemirror-tables";
43
44
  import { Fragment } from "prosemirror-model";
@@ -50,6 +51,32 @@ const INTERNAL = { condition: CANONICAL_CONDITION.internal };
50
51
  const COMPOSING = { condition: CANONICAL_CONDITION.composing };
51
52
  const ID_SPACE = { condition: CANONICAL_CONDITION.idSpace };
52
53
  const HOST_TRANSACTION = { condition: CANONICAL_CONDITION.hostTransaction };
54
+ /**
55
+ * The ground of an edit whose result the session cannot hold. Comments and
56
+ * anchors that are no valid set, and a story left with no block, are a
57
+ * request the document does not allow. Content no operation writes (an
58
+ * unstructured field character, text holding a tab or a line feed, a block
59
+ * of no kind the model has) can come only from an operation the session
60
+ * applied wrongly: the operations refuse such text and write fields whole.
61
+ */
62
+ const editIssueGround = (issue) => issue.kind === "content" ? INTERNAL : INVALID_REQUEST;
63
+ /** The ids of the comments whose range or reference lies in a story. */
64
+ const commentIdsAnchoredIn = (document, story) => {
65
+ const ids = /* @__PURE__ */ new Set();
66
+ const visit = (value) => {
67
+ if (typeof value !== "object" || value === null) return;
68
+ if (Array.isArray(value)) {
69
+ for (const item of value) visit(item);
70
+ return;
71
+ }
72
+ const record = value;
73
+ const id = record["id"];
74
+ if ((record["type"] === "commentRangeStart" || record["type"] === "commentRangeEnd" || record["type"] === "commentReference") && typeof id === "number") ids.add(id);
75
+ for (const child of Object.values(record)) visit(child);
76
+ };
77
+ visit(findStoryBody(document, story)?.content);
78
+ return ids;
79
+ };
53
80
  /** A refusal under the gap, rule or condition it belongs to. */
54
81
  const refuse = (message, ground) => Result.err(new CanonicalSessionError({
55
82
  ground,
@@ -258,21 +285,32 @@ var CanonicalProjection = class CanonicalProjection {
258
285
  addresses: this.addresses
259
286
  });
260
287
  }
288
+ /**
289
+ * A paragraph's address, its text and gaps aligned on the first address:
290
+ * refused when its source and its editor node do not align.
291
+ */
261
292
  addressOf(block, paragraph) {
262
293
  const entry = this.blocks[block]?.paragraphs[paragraph] ?? panic("Missing canonical paragraph");
263
294
  const known = this.addresses.get(entry.blockId);
264
- if (known !== void 0) return known;
295
+ if (known !== void 0) return Result.ok(known);
296
+ const inline = entry.inline();
297
+ if (inline.isErr()) return Result.err(inlineRefusal(inline.error));
265
298
  const address = {
266
299
  blockId: entry.blockId,
267
300
  container: entry.container ?? this.content,
268
301
  start: (this.starts[block] ?? panic("Missing block start")) + entry.offset,
269
- text: entry.inline.text,
270
- gaps: entry.inline.gaps,
302
+ text: inline.value.text,
303
+ gaps: inline.value.gaps,
271
304
  node: entry.node,
272
305
  source: entry.source
273
306
  };
274
307
  this.addresses.set(entry.blockId, address);
275
- return address;
308
+ return Result.ok(address);
309
+ }
310
+ /** {@link addressOf}, none for a paragraph it refuses. */
311
+ alignedAddressOf(block, paragraph) {
312
+ const address = this.addressOf(block, paragraph);
313
+ return address.isOk() ? address.value : void 0;
276
314
  }
277
315
  /**
278
316
  * The paragraph whose content holds `position`, its ends included: a
@@ -280,6 +318,11 @@ var CanonicalProjection = class CanonicalProjection {
280
318
  * projection lists after its host as the story's own.
281
319
  */
282
320
  paragraphAround(position) {
321
+ const slot = this.slotAround(position);
322
+ return slot === void 0 ? void 0 : this.alignedAddressOf(slot.block, slot.paragraph);
323
+ }
324
+ /** Where {@link paragraphAround}'s paragraph stands. */
325
+ slotAround(position) {
283
326
  let low = 0;
284
327
  let high = this.starts.length - 1;
285
328
  let block = -1;
@@ -305,14 +348,20 @@ var CanonicalProjection = class CanonicalProjection {
305
348
  }
306
349
  const paragraph = paragraphs[found];
307
350
  if (paragraph === void 0 || relative > paragraph.offset + paragraph.node.content.size) return void 0;
308
- return this.addressOf(block, found);
351
+ return {
352
+ block,
353
+ paragraph: found
354
+ };
309
355
  }
310
356
  addressAt(position, edge = "caret") {
311
357
  const unaddressed = (message) => refuse(message, { condition: CANONICAL_CONDITION.invalidRequest });
312
358
  if (!Number.isInteger(position)) return unaddressed("The input position is not an integer.");
313
359
  position = canonicalContentPosition(this.doc, position);
314
- const paragraph = this.paragraphAround(position);
315
- if (paragraph === void 0) return unaddressed("The input is outside a plain paragraph.");
360
+ const slot = this.slotAround(position);
361
+ if (slot === void 0) return unaddressed("The input is outside a plain paragraph.");
362
+ const address = this.addressOf(slot.block, slot.paragraph);
363
+ if (address.isErr()) return address;
364
+ const paragraph = address.value;
316
365
  const relative = position - paragraph.start;
317
366
  const offset = paragraph.gaps.offsetOf(relative);
318
367
  if (offset < 0) return unaddressed("The input splits an indivisible inline element.");
@@ -363,7 +412,13 @@ var CanonicalProjection = class CanonicalProjection {
363
412
  return destination === position ? address : this.addressAt(destination, edge);
364
413
  }
365
414
  positionAt(address) {
366
- const paragraph = this.paragraph(address.blockId);
415
+ const slot = sameStory(address.story, this.story) ? this.index.get(address.blockId) : void 0;
416
+ let paragraph;
417
+ if (slot !== void 0 && slot.paragraph >= 0) {
418
+ const aligned = this.addressOf(slot.block, slot.paragraph);
419
+ if (aligned.isErr()) return Result.err(aligned.error);
420
+ paragraph = aligned.value;
421
+ }
367
422
  if (!sameStory(address.story, this.story) || paragraph === void 0 || !Number.isInteger(address.offset) || address.offset < 0 || address.offset > paragraph.text.length || splitsSurrogatePair(paragraph.text, address.offset)) return refuse("The canonical selection is outside a plain paragraph.", INVALID_REQUEST);
368
423
  const gaps = paragraph.gaps.at(address.offset) ?? panic("Missing canonical boundary");
369
424
  const lastOrdinal = (gaps.at(-1) ?? panic("Missing canonical seam")).last;
@@ -403,12 +458,13 @@ var CanonicalProjection = class CanonicalProjection {
403
458
  /** The first paragraph the projection addresses, in document order. */
404
459
  get firstParagraph() {
405
460
  const block = this.blocks.findIndex(({ paragraphs }) => paragraphs.length > 0);
406
- return block < 0 ? void 0 : this.addressOf(block, 0);
461
+ return block < 0 ? void 0 : this.alignedAddressOf(block, 0);
407
462
  }
463
+ /** A paragraph's address; none for one the projection cannot align. */
408
464
  paragraph(blockId) {
409
465
  const slot = this.index.get(blockId);
410
466
  if (slot === void 0 || slot.paragraph < 0) return void 0;
411
- return this.addressOf(slot.block, slot.paragraph);
467
+ return this.alignedAddressOf(slot.block, slot.paragraph);
412
468
  }
413
469
  /** The paragraph whose node starts at `before`. */
414
470
  paragraphStartingAt(before) {
@@ -418,7 +474,8 @@ var CanonicalProjection = class CanonicalProjection {
418
474
  nextParagraphInContainer(blockId) {
419
475
  const slot = this.index.get(blockId);
420
476
  if (slot === void 0 || slot.paragraph < 0) return void 0;
421
- const paragraph = this.addressOf(slot.block, slot.paragraph);
477
+ const paragraph = this.alignedAddressOf(slot.block, slot.paragraph);
478
+ if (paragraph === void 0) return void 0;
422
479
  const index = paragraph.container.indexOf(paragraph.source);
423
480
  if (index >= 0) {
424
481
  let at = index + 1;
@@ -435,8 +492,8 @@ var CanonicalProjection = class CanonicalProjection {
435
492
  next = 0;
436
493
  }
437
494
  if (block >= this.blocks.length) return void 0;
438
- const following = this.addressOf(block, next);
439
- return paragraph.container === following.container ? following : void 0;
495
+ const following = this.alignedAddressOf(block, next);
496
+ return paragraph.container === following?.container ? following : void 0;
440
497
  }
441
498
  };
442
499
  /** Immutable operations retain content identities for every untouched story. */
@@ -518,8 +575,13 @@ const placeShownSelection = (transaction, projection) => {
518
575
  const read = projection.selectionAt(transaction);
519
576
  return read.isOk() ? read.value : void 0;
520
577
  };
578
+ /**
579
+ * A projected paragraph the document does not hold, or holds once for two:
580
+ * the projection and the document it was made from are out of step, a defect
581
+ * of the session.
582
+ */
521
583
  const structureRefusal = () => new CanonicalSessionError({
522
- ground: { gap: CANONICAL_GAP.storyContentProjection },
584
+ ground: INTERNAL,
523
585
  reason: "refused",
524
586
  message: "The projection changed paragraph structure."
525
587
  });
@@ -621,6 +683,56 @@ var OpenComments = class {
621
683
  return this.kept;
622
684
  }
623
685
  };
686
+ const NO_OPEN_COMMENTS = /* @__PURE__ */ new Set();
687
+ /**
688
+ * The refusal of a paragraph whose source the projection cannot align with
689
+ * its node. Every inline item the document holds has a projection policy, so
690
+ * such a paragraph is a projection out of step with the document.
691
+ */
692
+ const inlineRefusal = (message) => new CanonicalSessionError({
693
+ ground: INTERNAL,
694
+ reason: "refused",
695
+ message
696
+ });
697
+ /** Whether a paragraph's editor node shows a comment's mark or boundary. */
698
+ const showsComments = (paragraph) => {
699
+ let shows = false;
700
+ paragraph.descendants((node) => {
701
+ if (node.type.name === "commentRangeBoundary" || node.marks.some((mark) => mark.type.name === "comment")) shows = true;
702
+ return !shows;
703
+ });
704
+ return shows;
705
+ };
706
+ /**
707
+ * A paragraph's text and gaps for a block walk. A walk that defers them
708
+ * still refuses now what the paragraph's editor node alone refuses (an
709
+ * inline construct canonical editing cannot place), and projects now a
710
+ * paragraph comments run through or mark, in its source or its node, with
711
+ * the ranges open at its start; any other paragraph's source is aligned
712
+ * with its node when it is first addressed, as it would have been now.
713
+ */
714
+ const walkInline = (args, walk) => {
715
+ const project = (projected) => {
716
+ const inline = walk.inline === void 0 ? projectCanonicalInline(projected) : walk.inline.project(projected);
717
+ return inline.isOk() ? Result.ok(inline.value) : Result.err(inline.error.message);
718
+ };
719
+ if (!walk.deferInline || args.commentContext.openIds.size > 0 || walk.markersOf(args.source).length > 0 || showsComments(args.paragraph)) {
720
+ const inline = project(args);
721
+ if (inline.isErr()) return inline;
722
+ return Result.ok(() => inline);
723
+ }
724
+ const native = canonicalNativeCells(args.paragraph);
725
+ if (native.isErr()) return Result.err(native.error.message);
726
+ const deferred = {
727
+ ...args,
728
+ commentContext: {
729
+ ...args.commentContext,
730
+ openIds: NO_OPEN_COMMENTS
731
+ }
732
+ };
733
+ let inline;
734
+ return Result.ok(() => inline ??= project(deferred));
735
+ };
624
736
  /**
625
737
  * The paragraphs of a text box's editor node, addressed as its story's
626
738
  * paragraphs. A paragraph the session cannot place in the box's source (one
@@ -639,13 +751,12 @@ const projectTextBoxParagraphs = ({ box, offset, walk, paragraphs }) => {
639
751
  const paraId = node.attrs["paraId"];
640
752
  const located = typeof paraId === "string" ? walk.locateBoxed(paraId) : void 0;
641
753
  if (typeof paraId !== "string" || located === void 0 || !walk.takeBoxed(located.source)) return false;
642
- const inlineArgs = {
754
+ const inline = walkInline({
643
755
  source: located.source,
644
756
  paragraph: node,
645
757
  pairedBookmarkIds: walk.facts.pairedBookmarkIds,
646
758
  commentContext
647
- };
648
- const inline = walk.inline === void 0 ? projectCanonicalInline(inlineArgs) : walk.inline.project(inlineArgs);
759
+ }, walk);
649
760
  if (inline.isOk()) paragraphs.push({
650
761
  blockId: paraId,
651
762
  container: located.container,
@@ -705,19 +816,14 @@ const projectBlock = (top, index, walk) => {
705
816
  }
706
817
  if (located.top !== index) local = false;
707
818
  passCommentMarkers(walk.positionedComments.before.get(source), walk.open);
708
- const inlineArgs = {
819
+ const inline = walkInline({
709
820
  source,
710
821
  paragraph: node,
711
822
  pairedBookmarkIds: walk.facts.pairedBookmarkIds,
712
823
  commentContext
713
- };
714
- const inline = walk.inline === void 0 ? projectCanonicalInline(inlineArgs) : walk.inline.project(inlineArgs);
824
+ }, walk);
715
825
  if (inline.isErr()) {
716
- failure = new CanonicalSessionError({
717
- ground: { gap: CANONICAL_GAP.storyContentProjection },
718
- reason: "refused",
719
- message: inline.error.message
720
- });
826
+ failure = inlineRefusal(inline.error);
721
827
  return false;
722
828
  }
723
829
  paragraphs.push({
@@ -748,7 +854,7 @@ const passCommentMarkers = (markers, open) => {
748
854
  /** Opens and closes the ranges a paragraph's markers start and end. */
749
855
  const commentPasser = (markersOf, open) => (paragraph) => passCommentMarkers(markersOf(paragraph), open);
750
856
  /** Every paragraph's address, walking every top-level node. */
751
- const projectEveryBlock = ({ doc, content, story, markersOf, inline }) => {
857
+ const projectEveryBlock = ({ doc, content, story, markersOf, inline, deferInline }) => {
752
858
  const pairedBookmarkIds = collectPairedBookmarkIds(content);
753
859
  const rangedIds = /* @__PURE__ */ new Set();
754
860
  const referencedIds = /* @__PURE__ */ new Set();
@@ -815,7 +921,8 @@ const projectEveryBlock = ({ doc, content, story, markersOf, inline }) => {
815
921
  },
816
922
  passComments,
817
923
  positionedComments,
818
- inline
924
+ inline,
925
+ deferInline
819
926
  };
820
927
  const blocks = [];
821
928
  for (let index = 0; index < doc.childCount; index += 1) {
@@ -894,7 +1001,8 @@ const projectChangedBlocks = (previous, { doc, content, story, markersOf, inline
894
1001
  },
895
1002
  passComments: commentPasser(markersOf, open),
896
1003
  positionedComments: positionedCommentGaps(content.slice(start, newEnd), continuationSources),
897
- inline
1004
+ inline,
1005
+ deferInline: false
898
1006
  };
899
1007
  const window = [];
900
1008
  for (let index = start; index < newEnd; index += 1) {
@@ -935,7 +1043,7 @@ const projectChangedBlocks = (previous, { doc, content, story, markersOf, inline
935
1043
  reusable: true
936
1044
  });
937
1045
  };
938
- const project = ({ document, styles, story = OP_STORIES.MAIN, caches, previous }) => {
1046
+ const project = ({ document, styles, story = OP_STORIES.MAIN, caches, previous, deferInline }) => {
939
1047
  const body = findStoryBody(document, story);
940
1048
  if (!body) return refuse("The canonical story no longer exists.", STALE);
941
1049
  const currentStyles = Object.hasOwn(document.package, "styles") ? document.package.styles : styles;
@@ -965,10 +1073,17 @@ const project = ({ document, styles, story = OP_STORIES.MAIN, caches, previous }
965
1073
  content: body.content,
966
1074
  story,
967
1075
  markersOf: caches?.comments.read ?? paragraphCommentMarkers,
968
- inline: caches?.inline
1076
+ inline: caches?.inline,
1077
+ deferInline: deferInline === true && previous === void 0
969
1078
  };
970
1079
  const kept = previous === void 0 ? void 0 : projectChangedBlocks(previous, walk);
971
- return kept === void 0 ? projectEveryBlock(walk) : Result.ok(kept);
1080
+ const result = kept === void 0 ? projectEveryBlock(walk) : Result.ok(kept);
1081
+ if (result.isOk() && !markupViewSources.has(result.value.doc)) markupViewSources.set(result.value.doc, {
1082
+ document,
1083
+ story,
1084
+ options
1085
+ });
1086
+ return result;
972
1087
  };
973
1088
  const intentStory = (intent) => {
974
1089
  switch (intent.type) {
@@ -1016,11 +1131,12 @@ const continuesGroup = (previous, next) => {
1016
1131
  }
1017
1132
  };
1018
1133
  const authorizedProjections = /* @__PURE__ */ new WeakMap();
1019
- /** Metadata is descriptive; transaction identity and session version authorize mutations. */
1020
- const isCanonicalProjectionTransaction = (transaction, session) => {
1134
+ const canonicalProjectionAuthorization = (transaction, session) => {
1021
1135
  const authorization = authorizedProjections.get(transaction);
1022
- return authorization?.session === session && authorization.version === session.version && transaction.doc.eq(authorization.doc);
1136
+ if (authorization?.session === session && authorization.version === session.version && transaction.doc.eq(authorization.doc)) return authorization;
1023
1137
  };
1138
+ /** Metadata is descriptive; transaction identity and session version authorize mutations. */
1139
+ const isCanonicalProjectionTransaction = (transaction, session) => canonicalProjectionAuthorization(transaction, session) !== void 0;
1024
1140
  /** A node's shape without its text, for a refusal that names a change. */
1025
1141
  const nodeShapeOf = (node) => JSON.stringify({
1026
1142
  type: node.type.name,
@@ -1044,7 +1160,8 @@ const projectionChangeOf = (projected, published) => {
1044
1160
  };
1045
1161
  /** The model publishes only after PM and every plugin accept its exact projection. */
1046
1162
  const publishCanonicalProjection = ({ state, commit, session }) => {
1047
- if (!isCanonicalProjectionTransaction(commit.transaction, session)) return refuse("The staged canonical projection is unauthorized.", INTERNAL);
1163
+ const authorization = canonicalProjectionAuthorization(commit.transaction, session);
1164
+ if (authorization === void 0) return refuse("The staged canonical projection is unauthorized.", INTERNAL);
1048
1165
  const applied = Result.try({
1049
1166
  try: () => state.applyTransaction(commit.transaction),
1050
1167
  catch: (cause) => new CanonicalSessionError({
@@ -1059,6 +1176,12 @@ const publishCanonicalProjection = ({ state, commit, session }) => {
1059
1176
  if (!staged.state.doc.eq(commit.projection.doc) || staged.transactions.some((transaction) => transaction.docChanged && !isCanonicalProjectionTransaction(transaction, session))) return refuse(`A plugin refused or changed the canonical projection: ${projectionChangeOf(commit.projection.doc, staged.state.doc)}.`, HOST_TRANSACTION);
1060
1177
  const published = commit.publish();
1061
1178
  if (published.isErr()) return published;
1179
+ const source = markupViewSources.get(authorization.doc);
1180
+ markupViewStateSources.set(staged.state, {
1181
+ ...source,
1182
+ document: commit.document,
1183
+ story: commit.projection.story
1184
+ });
1062
1185
  return Result.ok(staged);
1063
1186
  };
1064
1187
  /**
@@ -1220,76 +1343,6 @@ var CanonicalSession = class {
1220
1343
  hasStyle(styleId) {
1221
1344
  return (this.styles ?? this.currentDocument.package.styles)?.styles.some((style) => style.styleId === styleId) ?? false;
1222
1345
  }
1223
- /** How a replacement in the current mode removes the words it changes. */
1224
- /**
1225
- * A replacement cut into pieces at the comment boundaries it keeps, within
1226
- * one paragraph: the last piece takes the text, the others are cleared, and
1227
- * each is compiled after the ones behind it, so no earlier address moves.
1228
- * The caret follows the new text, moved back by what the pieces before it
1229
- * cleared.
1230
- */
1231
- compileReplacementPieces({ projection, pieces, start, end, intent }) {
1232
- const refused = (message) => refuse(message, { gap: CANONICAL_GAP.commentReplacementAlignment });
1233
- const addressed = [];
1234
- for (const [index, piece] of pieces.entries()) {
1235
- const from = index === 0 ? Result.ok(start) : projection.inputAddressAt(piece.from, "afterMarkers");
1236
- const to = index === pieces.length - 1 ? Result.ok(end) : projection.inputAddressAt(piece.to, "beforeMarkers");
1237
- if (from.isErr() || to.isErr()) return refused("A replacement piece has no address.");
1238
- if (from.value.blockId !== start.blockId || to.value.blockId !== start.blockId) return refused("A replacement cut at comment boundaries stays in one paragraph.");
1239
- addressed.push({
1240
- from: from.value,
1241
- to: to.value,
1242
- text: piece.text
1243
- });
1244
- }
1245
- let document = this.currentDocument;
1246
- const ops = [];
1247
- let caret;
1248
- let cleared = 0;
1249
- const { inside, ...shared } = intent;
1250
- for (const [index, piece] of [...addressed.entries()].reverse()) {
1251
- const last = index === addressed.length - 1;
1252
- const step = {
1253
- ...shared,
1254
- from: piece.from,
1255
- to: piece.to,
1256
- text: piece.text,
1257
- ...last && inside?.end === true ? { inside: { end: true } } : {}
1258
- };
1259
- const compiled = compileEditorIntent(document, {
1260
- intent: step,
1261
- mode: this.intentMode(document, step)
1262
- });
1263
- if (compiled.isErr()) return refused(compiled.error.message);
1264
- const applied = compiled.value.applied === void 0 ? applyDocumentOps(document, compiled.value.ops) : Result.ok(compiled.value.applied);
1265
- if (applied.isErr()) return refused(applied.error.message);
1266
- document = applied.value.document;
1267
- ops.push(...compiled.value.ops);
1268
- if (last) caret = compiled.value.selection;
1269
- else cleared += piece.to.offset - piece.from.offset;
1270
- }
1271
- if (caret === void 0) return refused("A replacement cut at comment boundaries lost its text.");
1272
- return Result.ok({
1273
- ops,
1274
- selection: {
1275
- ...caret,
1276
- offset: caret.offset - cleared
1277
- }
1278
- });
1279
- }
1280
- replacementRemoval() {
1281
- switch (this.mode.type) {
1282
- case "editing": return {
1283
- type: "direct",
1284
- comments: this.currentDocument.package.document.comments ?? []
1285
- };
1286
- case "suggesting": return { type: "tracked" };
1287
- default: {
1288
- const unhandled = this.mode;
1289
- return panic(`Unknown canonical session mode ${String(unhandled)}`);
1290
- }
1291
- }
1292
- }
1293
1346
  advanceBlockId() {
1294
1347
  while (this.allocatedBlockIds.has(this.nextBlockId.toString(16).padStart(8, "0").toUpperCase())) this.nextBlockId += 1;
1295
1348
  }
@@ -1663,11 +1716,8 @@ var CanonicalSession = class {
1663
1716
  if (state.selection instanceof CellSelection) {
1664
1717
  if (from !== state.selection.from || to !== state.selection.to) return refuse("A replacement range cannot partially address a cell selection.", INVALID_REQUEST);
1665
1718
  const ranges = canonicalCellSelectionRanges(state.selection);
1666
- const firstRange = ranges.at(0);
1667
- const lastRange = ranges.at(-1);
1668
- if (firstRange === void 0 || lastRange === void 0) return refuse("The selected cells contain no editable text.", INVALID_REQUEST);
1719
+ if (ranges.length === 0) return refuse("The selected cells contain no editable text.", INVALID_REQUEST);
1669
1720
  const intents = [];
1670
- const commentChanges = [];
1671
1721
  let first = true;
1672
1722
  for (const range of ranges) {
1673
1723
  const edges = projection.rangeEdgesAt(range.from, range.to);
@@ -1681,23 +1731,8 @@ var CanonicalSession = class {
1681
1731
  to: cellTo.value,
1682
1732
  text: first ? text : ""
1683
1733
  });
1684
- commentChanges.push({
1685
- from: range.from,
1686
- to: range.to,
1687
- text: first ? text : "",
1688
- markers: edges.start === "beforeMarkers" || edges.end === "afterMarkers" ? "selected" : "retained"
1689
- });
1690
1734
  first = false;
1691
1735
  }
1692
- if (text !== "" && !preservesReplacementCommentAlignment({
1693
- doc: state.doc,
1694
- source: {
1695
- from: firstRange.from,
1696
- to: lastRange.to
1697
- },
1698
- changes: commentChanges,
1699
- removal: this.replacementRemoval()
1700
- })) return refuse("Canonical replacement cannot preserve the aligned comment ranges.", { gap: CANONICAL_GAP.commentReplacementAlignment });
1701
1736
  return this.prepareIntents(state, {
1702
1737
  intents,
1703
1738
  semantic
@@ -1715,43 +1750,6 @@ var CanonicalSession = class {
1715
1750
  if (start.isErr()) return start;
1716
1751
  const end = inputFrom === inputTo && editCaret !== void 0 ? Result.ok(editCaret) : projection.inputAddressAt(inputTo, edges.end);
1717
1752
  if (end.isErr()) return end;
1718
- let pieces = [];
1719
- if (inputFrom < inputTo && text !== "") {
1720
- const retirement = planWholeParagraphReplacement({
1721
- document: this.currentDocument,
1722
- from: start.value,
1723
- to: end.value,
1724
- suggesting: this.mode.type === "suggesting" ? { author: this.mode.author } : void 0
1725
- });
1726
- if (retirement.isErr()) return refuseOp(retirement.error);
1727
- const removal = this.replacementRemoval();
1728
- const whole = [{
1729
- from: inputFrom,
1730
- to: inputTo,
1731
- text,
1732
- markers: edges.start === "beforeMarkers" || edges.end === "afterMarkers" ? "selected" : "retained"
1733
- }];
1734
- const aligned = (changes, retiredCommentIds) => preservesReplacementCommentAlignment({
1735
- doc: state.doc,
1736
- retiredCommentIds,
1737
- removal,
1738
- source: {
1739
- from: inputFrom,
1740
- to: inputTo
1741
- },
1742
- changes
1743
- });
1744
- if (!aligned(whole, retirement.value?.commentIds)) {
1745
- const split = removal.type === "direct" ? splitAtKeptCommentBoundaries({
1746
- doc: state.doc,
1747
- changes: whole,
1748
- comments: removal.comments
1749
- }) : void 0;
1750
- const retired = /* @__PURE__ */ new Set([...retirement.value?.commentIds ?? [], ...split?.retiredCommentIds ?? []]);
1751
- if (split === void 0 || split.changes.length < 2 || !aligned(split.changes, retired)) return refuse("Canonical replacement cannot preserve the aligned comment ranges.", { gap: CANONICAL_GAP.commentReplacementAlignment });
1752
- pieces = split.changes;
1753
- }
1754
- }
1755
1753
  const preSelection = projection.selectionAt(state);
1756
1754
  if (preSelection.isErr()) return preSelection;
1757
1755
  const inside = controlEdgesAt(state.doc, inputFrom, inputTo);
@@ -1763,20 +1761,11 @@ var CanonicalSession = class {
1763
1761
  ...Object.keys(inside).length === 0 ? {} : { inside },
1764
1762
  ...state.storedMarks === null ? {} : { runPropsPatch: runFormattingPatchFromMarks(state.selection.$from.marks(), state.storedMarks) }
1765
1763
  };
1766
- const compiled = pieces.length > 1 ? this.compileReplacementPieces({
1767
- projection,
1768
- pieces,
1769
- start: start.value,
1770
- end: end.value,
1771
- intent
1772
- }) : compileEditorIntent(this.currentDocument, {
1764
+ const compiled = compileEditorIntent(this.currentDocument, {
1773
1765
  intent,
1774
1766
  mode: this.intentMode(this.currentDocument, intent)
1775
1767
  });
1776
- if (compiled.isErr()) {
1777
- if (compiled.error instanceof CanonicalSessionError) return Result.err(compiled.error);
1778
- return compiled.error.reason === DOCUMENT_OP_REFUSAL_REASONS.NO_CHANGE ? noChange(compiled.error.message) : refuseOp(compiled.error);
1779
- }
1768
+ if (compiled.isErr()) return compiled.error.reason === DOCUMENT_OP_REFUSAL_REASONS.NO_CHANGE ? noChange(compiled.error.message) : refuseOp(compiled.error);
1780
1769
  const { ops, selection: caret } = compiled.value;
1781
1770
  const postSelection = {
1782
1771
  type: "text",
@@ -2231,13 +2220,18 @@ var CanonicalSession = class {
2231
2220
  * revisions as ordinary tracked changes by `author`, keeping their ids, as
2232
2221
  * the editor does: the change then waits for review like any other. Its
2233
2222
  * comments stay as ordinary comments.
2223
+ *
2224
+ * With `story`, only the suggestion's part in that story resolves: its
2225
+ * revisions there and the comments it anchors there. Its records in other
2226
+ * stories stay pending, and resolve later the same way or whole.
2234
2227
  */
2235
- prepareSuggestion(state, { suggestionId, resolution, author, date }) {
2236
- const revisions = documentStories(this.currentDocument).map((story) => ({
2228
+ prepareSuggestion(state, { suggestionId, resolution, author, date, story: only }) {
2229
+ const revisions = documentStories(this.currentDocument).filter((story) => only === void 0 || sameStory(story, only)).map((story) => ({
2237
2230
  story,
2238
2231
  infos: suggestionRevisionInfos(this.currentDocument, story, suggestionId)
2239
2232
  })).filter(({ infos }) => infos.length > 0);
2240
- const commentIds = suggestedCommentIds(this.currentDocument, suggestionId);
2233
+ const anchored = only === void 0 ? void 0 : commentIdsAnchoredIn(this.currentDocument, only);
2234
+ const commentIds = suggestedCommentIds(this.currentDocument, suggestionId).filter((id) => anchored === void 0 || anchored.has(id));
2241
2235
  if (revisions.length === 0 && commentIds.length === 0) return noChange(`No pending suggestion ${suggestionId}.`);
2242
2236
  if (resolution === "reject") return this.prepareResolve(state, {
2243
2237
  revisionIds: [],
@@ -2647,15 +2641,12 @@ var CanonicalSession = class {
2647
2641
  document: applied.value.document,
2648
2642
  previous: this.currentDocument
2649
2643
  });
2650
- if (unsupportedEditReason({
2644
+ const unsupported = unsupportedEditReason({
2651
2645
  previous: this.currentDocument,
2652
2646
  document: applied.value.document,
2653
2647
  stories: changed
2654
- }) !== null) return Result.err(new CanonicalSessionError({
2655
- ground: { gap: CANONICAL_GAP.storyContentProjection },
2656
- reason: "refused",
2657
- message: "The operations produce unsupported canonical story content."
2658
- }));
2648
+ });
2649
+ if (unsupported !== null) return refuse("The operations produce unsupported canonical story content.", editIssueGround(unsupported));
2659
2650
  applied.value.document = preservePropertySources({
2660
2651
  target: applied.value.document,
2661
2652
  source: this.currentDocument,
@@ -2836,7 +2827,7 @@ const createCanonicalSession = (document, styles, { idLease, newOpId } = {}) =>
2836
2827
  const owned = cloneDocumentWithParagraphPropertySources(document);
2837
2828
  const source = normalizeCanonicalSource(owned);
2838
2829
  const unsupported = unsupportedSeedReason(source.document);
2839
- if (unsupported !== null) return refuse(unsupported, { gap: CANONICAL_GAP.storyContentProjection });
2830
+ if (unsupported !== null) return refuse(unsupported.message, INVALID_REQUEST);
2840
2831
  const identified = preservePropertySourcesWholeStories({
2841
2832
  target: normalizeForOps(source.document),
2842
2833
  source: owned
@@ -2851,7 +2842,8 @@ const createCanonicalSession = (document, styles, { idLease, newOpId } = {}) =>
2851
2842
  const projected = project({
2852
2843
  document: normalized,
2853
2844
  styles,
2854
- caches
2845
+ caches,
2846
+ deferInline: true
2855
2847
  });
2856
2848
  if (projected.isErr()) return projected;
2857
2849
  return Result.ok(new CanonicalSession({
@@ -2891,7 +2883,7 @@ const visibleDeletionContext = (state) => {
2891
2883
  const native = canonicalNativeCells(paragraph);
2892
2884
  if (native.isErr()) {
2893
2885
  issue = new CanonicalSessionError({
2894
- ground: { gap: CANONICAL_GAP.storyContentProjection },
2886
+ ground: INTERNAL,
2895
2887
  reason: "refused",
2896
2888
  message: native.error.message
2897
2889
  });
@@ -12,7 +12,8 @@ type ClipboardAnnotationsOptions = {
12
12
  * The notes and comments the clipboard's references and markers name, read
13
13
  * from the document they were copied from; the paste copies each under a
14
14
  * fresh id. Without that document the ids could name the destination's own
15
- * notes and comments, so the paste refuses.
15
+ * notes and comments: the paste keeps the text and leaves the markers and
16
+ * references out, as a copy made outside the editor holds no comment or note.
16
17
  */
17
18
  export declare const clipboardAnnotations: ({ paragraphs, source }: ClipboardAnnotationsOptions) => Result<PasteAnnotations | undefined, CanonicalSessionError>;
18
19
  /** Clipboard slices are input payloads; their paragraph identities and private captures are not owners. */