@stll/folio-core 0.75.0 → 0.76.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 (92) hide show
  1. package/dist/ai-edits/headless.d.ts +2 -1
  2. package/dist/ai-edits/headless.js +18 -12
  3. package/dist/ai-edits/pending-suggestions.js +2 -3
  4. package/dist/ai-edits/read.js +9 -3
  5. package/dist/ai-edits/types.d.ts +9 -4
  6. package/dist/canonical/batchCommentReferences.js +2 -1
  7. package/dist/canonical/canonicalBatchReview.js +5 -8
  8. package/dist/canonical/canonicalComments.js +8 -2
  9. package/dist/canonical/canonicalCompilerRefusal.d.ts +2 -4
  10. package/dist/canonical/canonicalCompilerRefusal.js +20 -14
  11. package/dist/canonical/canonicalPublicOperations.js +203 -71
  12. package/dist/canonical/canonicalSession.js +3 -1
  13. package/dist/controller/canonicalClipboard.d.ts +1 -1
  14. package/dist/controller/canonicalClipboard.js +71 -17
  15. package/dist/controller/canonicalContentControlValue.js +7 -7
  16. package/dist/controller/canonicalReview.d.ts +21 -0
  17. package/dist/controller/canonicalReview.js +44 -9
  18. package/dist/controller/canonicalStructure.d.ts +1 -2
  19. package/dist/controller/canonicalStructure.js +51 -24
  20. package/dist/controller/fontReadiness.js +50 -23
  21. package/dist/controller/hiddenEditorManager.js +5 -20
  22. package/dist/docx/bulletMarkers.d.ts +8 -0
  23. package/dist/docx/bulletMarkers.js +9 -1
  24. package/dist/docx/canonicalSessionInput.js +25 -11
  25. package/dist/docx/ensureParaIds.d.ts +31 -0
  26. package/dist/docx/ensureParaIds.js +57 -32
  27. package/dist/docx/ensureUniqueControlIds.d.ts +13 -0
  28. package/dist/docx/ensureUniqueControlIds.js +47 -29
  29. package/dist/docx/ensureUniqueRevisionIds.d.ts +13 -0
  30. package/dist/docx/ensureUniqueRevisionIds.js +52 -29
  31. package/dist/docx/server/templatePlan/apply.js +62 -8
  32. package/dist/docx/server/templatePlan/blocks.d.ts +55 -3
  33. package/dist/docx/server/templatePlan/blocks.js +182 -6
  34. package/dist/docx/server/templatePlan/builders.d.ts +16 -1
  35. package/dist/docx/server/templatePlan/builders.js +31 -3
  36. package/dist/docx/server/templatePlan/effects.d.ts +27 -0
  37. package/dist/docx/server/templatePlan/effects.js +188 -0
  38. package/dist/docx/server/templatePlan/emit.d.ts +2 -1
  39. package/dist/docx/server/templatePlan/emit.js +44 -2
  40. package/dist/docx/server/templatePlan/inspect.js +21 -1
  41. package/dist/docx/server/templatePlan/labels.js +16 -1
  42. package/dist/docx/server/templatePlan/plan.d.ts +12 -3
  43. package/dist/docx/server/templatePlan/plan.js +36 -12
  44. package/dist/docx/server/templatePlan/planShape.js +47 -22
  45. package/dist/docx/server/templatePlan/revisions.d.ts +46 -0
  46. package/dist/docx/server/templatePlan/revisions.js +407 -0
  47. package/dist/docx/server/templatePlan/story.d.ts +15 -1
  48. package/dist/docx/server/templatePlan/story.js +50 -2
  49. package/dist/docx/server/templatePlan/types.d.ts +217 -1
  50. package/dist/docx/server/templatePlan/verify.js +117 -8
  51. package/dist/docx/server/templatePlan/xml.d.ts +3 -0
  52. package/dist/docx/server/templatePlan/xml.js +4 -1
  53. package/dist/internal/revisionResolutionStep.d.ts +2 -31
  54. package/dist/internal/revisionResolutionStep.js +2 -171
  55. package/dist/prosemirror/commands/comments.d.ts +6 -234
  56. package/dist/prosemirror/commands/comments.js +5 -2464
  57. package/dist/prosemirror/commands/index.d.ts +4 -2
  58. package/dist/prosemirror/commands/index.js +4 -2
  59. package/dist/prosemirror/commands/tableCellMergeResolution.d.ts +1 -11
  60. package/dist/prosemirror/commands/tableCellMergeResolution.js +3 -154
  61. package/dist/prosemirror/extensions/marks/HyperlinkExtension.js +6 -8
  62. package/dist/prosemirror/hyperlinkRemoval.d.ts +11 -8
  63. package/dist/prosemirror/hyperlinkRemoval.js +24 -15
  64. package/dist/prosemirror/paraIds.d.ts +1 -14
  65. package/dist/prosemirror/paraIds.js +1 -14
  66. package/dist/prosemirror/paragraphMarkJoin.d.ts +0 -16
  67. package/dist/prosemirror/paragraphMarkJoin.js +3 -46
  68. package/dist/prosemirror/projectionReview.d.ts +49 -0
  69. package/dist/prosemirror/projectionReview.js +292 -0
  70. package/dist/prosemirror/rebaseParagraphRunFormatting.d.ts +1 -13
  71. package/dist/prosemirror/rebaseParagraphRunFormatting.js +1 -16
  72. package/dist/prosemirror/replacedAnnotations.d.ts +2 -5
  73. package/dist/prosemirror/replacedAnnotations.js +1 -10
  74. package/dist/prosemirror/revisionCarriers.d.ts +9 -1
  75. package/dist/prosemirror/revisionCarriers.js +15 -1
  76. package/dist/prosemirror/revisionRangeQueries.d.ts +32 -0
  77. package/dist/prosemirror/revisionRangeQueries.js +76 -0
  78. package/dist/prosemirror/runFormattingMarkNames.d.ts +1 -1
  79. package/dist/prosemirror/runFormattingMarkNames.js +1 -1
  80. package/dist/prosemirror/suggestionQueries.d.ts +114 -0
  81. package/dist/prosemirror/suggestionQueries.js +320 -0
  82. package/dist/prosemirror/tableGridMutation.d.ts +1 -16
  83. package/dist/prosemirror/tableGridMutation.js +1 -78
  84. package/dist/prosemirror/tableRowRevisionOwnership.d.ts +18 -0
  85. package/dist/prosemirror/tableRowRevisionOwnership.js +7 -0
  86. package/dist/prosemirror/utils/extractTrackedChanges.js +14 -4
  87. package/dist/server.d.ts +2 -2
  88. package/dist/types/canonicalCapabilities.d.ts +1 -25
  89. package/dist/types/canonicalCapabilities.js +1 -23
  90. package/package.json +2 -2
  91. package/dist/internal/headlessRevisionResolutionGuard.d.ts +0 -3
  92. package/dist/internal/headlessRevisionResolutionGuard.js +0 -18
@@ -4,7 +4,7 @@ import { describeCharacterSplit } from "../ai-edits/character-boundaries.js";
4
4
  import { buildCleanBlockText, resolveCleanTextRange } from "../ai-edits/clean-text.js";
5
5
  import { directJoinSurvivor, directJoinTakes } from "../ai-edits/directJoin.js";
6
6
  import { EXISTING_REVISION_CONFLICT_MESSAGE, createExistingRevisionGuard } from "../ai-edits/existingRevisions.js";
7
- import { parseInlineEmphasisRuns, stripInlineEmphasisMarkers } from "../ai-edits/inline-emphasis.js";
7
+ import { hasInlineEmphasis, parseInlineEmphasisRuns, stripInlineEmphasisMarkers } from "../ai-edits/inline-emphasis.js";
8
8
  import { compileMarkdownInsertion } from "../ai-edits/markdownInsertion.js";
9
9
  import { BACKGROUND_MARK_NAMES, changesFromSegments, cutAroundRepeatedSeparators, planReplacementChanges, planTextChanges, touchedBackgroundStretches } from "../ai-edits/minimal-replacement.js";
10
10
  import { createBatchListMinter } from "../ai-edits/newListNumbering.js";
@@ -35,7 +35,7 @@ import { preservesReplacementCommentAlignment, splitAtKeptCommentBoundaries, sur
35
35
  import { createStyleResolver } from "../prosemirror/styles/styleResolver.js";
36
36
  import { insertedColumnWidth, omittedSlotTakingColumn } from "../prosemirror/tableGridMutation.js";
37
37
  import { paragraphGoes, tableGoes } from "../prosemirror/tableRunIn.js";
38
- import { CANONICAL_GAP } from "../types/canonicalCapabilities.js";
38
+ import { CANONICAL_CONDITION, CANONICAL_GAP, CANONICAL_RULE } from "../types/canonicalCapabilities.js";
39
39
  import { readModel } from "../types/documentModel.js";
40
40
  import { batchCommentReferenceShift, mapIntentPositions } from "./batchCommentReferences.js";
41
41
  import { createCanonicalBatchReview } from "./canonicalBatchReview.js";
@@ -45,6 +45,7 @@ import { insertedCommentRange, plainInsertedParagraphIds } from "./canonicalInse
45
45
  import { planReplayParagraphIds, startParagraphAdmissionCursor } from "./canonicalInsertionReplayIds.js";
46
46
  import { addJoinedParagraphReceipts } from "./canonicalJoinedReceipts.js";
47
47
  import { lockViolationBetween } from "./canonicalLockGuard.js";
48
+ import { groundForDocumentOpRefusal } from "./canonicalRefusalGrounds.js";
48
49
  import { keepStandingRevisionReceipts } from "./canonicalRevisionReceipts.js";
49
50
  import { CANONICAL_ADMISSION } from "./canonicalSession.js";
50
51
  import { canonicalTrackedBatchFidelityError } from "./canonicalTrackedBatchFidelity.js";
@@ -55,7 +56,7 @@ import { replacementRunSources } from "./replacementRunSources.js";
55
56
  import { Result, TaggedError, panic } from "better-result";
56
57
  import { commentAuthorIssue, trackedChangeAuthorIssue } from "@stll/docx-core";
57
58
  import { CONTENT_CONTROL_LOCK, MAX_REVISION_ID, lockedControlMessage, paragraphNumberingReference } from "@stll/docx-core/model";
58
- import { BATCH_ANNOTATION_KINDS, DOCUMENT_OP_TYPES, ID_KIND_LIMITS, ID_LEASE_KINDS, OP_STORIES, SPLIT_HALVES, TYPED_TEXT_FORMATTING, applyDocumentOps, combineRecordedRevisions, compileEditorIntent, contentRank, createEditorIntentIdAllocator, deletionRemovesBlock, documentStories, finalBreakCarrier, findStoryBody, formatParaId, freshCommentId, groupInsertionRuns, idKey, introducedDocumentIds, isBlockBoundary, isCommentAnchorCarrierType, joinPropertyChangeLink, leasedIdCensus, locateParagraphThroughTextBoxes, noteDeletionOps, orderAddressingBeforeRemoval, orderBatchOperations, packageResourcesOpOf, paragraphHoldsContent, paragraphHoldsContentOnceAccepted, paragraphLength, paragraphsThroughTextBoxes, pendingSuggestionIdsIn, revisionSpan } from "@stll/docx-core/ops";
59
+ import { BATCH_ANNOTATION_KINDS, DOCUMENT_OP_REFUSAL_REASONS, DOCUMENT_OP_TYPES, DocumentOpRefusal, ID_KIND_LIMITS, ID_LEASE_KINDS, OP_STORIES, SPLIT_HALVES, TYPED_TEXT_FORMATTING, applyDocumentOps, combineRecordedRevisions, compileEditorIntent, contentRank, createEditorIntentIdAllocator, deletionRemovesBlock, documentStories, finalBreakCarrier, findStoryBody, formatParaId, freshCommentId, groupInsertionRuns, idKey, introducedDocumentIds, isBlockBoundary, isCommentAnchorCarrierType, joinPropertyChangeLink, leasedIdCensus, locateParagraphThroughTextBoxes, noteDeletionOps, orderAddressingBeforeRemoval, orderBatchOperations, packageResourcesOpOf, paragraphHoldsContent, paragraphHoldsContentOnceAccepted, paragraphLength, paragraphsThroughTextBoxes, pendingSuggestionIdsIn, revisionSpan } from "@stll/docx-core/ops";
59
60
  import { EditorState } from "prosemirror-state";
60
61
  import { TableMap } from "prosemirror-tables";
61
62
  import { Mark } from "prosemirror-model";
@@ -604,6 +605,28 @@ const CANONICAL_PUBLIC_OPERATION_DISPOSITIONS = {
604
605
  mergeTableCells: "compile",
605
606
  splitTableCell: "compile"
606
607
  };
608
+ /**
609
+ * A request the operation vocabulary does not take, or names a place the
610
+ * document has no address for: refused as the request, not as a capability
611
+ * the session lacks.
612
+ */
613
+ const INVALID_REQUEST = { condition: CANONICAL_CONDITION.invalidRequest };
614
+ /** A place the operation named that an earlier change of its batch moved on. */
615
+ const STALE = { condition: CANONICAL_CONDITION.stale };
616
+ /** A shape the session built itself and then could not read back: a defect. */
617
+ const INTERNAL = { condition: CANONICAL_CONDITION.internal };
618
+ /**
619
+ * The public reason a batch the session refused to stage reports: a no-op
620
+ * for one that changes nothing, a content control's lock as the lock, a
621
+ * document that moved on as a stale range, and any other refusal as a block
622
+ * the session does not take, under the session's own ground.
623
+ */
624
+ const stagingRefusalReason = (reason, ground) => {
625
+ if (reason === "noChange") return "noopOperation";
626
+ if (ground.rule === CANONICAL_RULE.contentControlLock) return "contentControlLock";
627
+ if (ground.condition === CANONICAL_CONDITION.stale) return "staleRange";
628
+ return "unsupportedBlock";
629
+ };
607
630
  const resolveComment = ({ operation, from, to }) => {
608
631
  if ("comment" in operation && operation.comment !== void 0) return {
609
632
  type: "commented",
@@ -690,7 +713,7 @@ const resolveParagraphProperties = ({ operation, paragraph, projection, mintList
690
713
  });
691
714
  if (Object.keys(patch).length === 0) return refusal("emptyOperation");
692
715
  const at = projection.addressAt(paragraph.start);
693
- if (at.isErr()) return refusal("unsupportedBlock");
716
+ if (at.isErr()) return refusal("unsupportedBlock", void 0, at.error.ground);
694
717
  const intents = [{
695
718
  type: "formatParagraph",
696
719
  at: at.value,
@@ -1017,7 +1040,7 @@ const resolveBlockInsertion = ({ operation, listStart, paragraph, named, formatt
1017
1040
  };
1018
1041
  const stated = statedNumbering(requested, mintList);
1019
1042
  const inline = operation.lineBreakMode === "inline";
1020
- if (markdown === void 0 && !noteReferenceEditsAllowed() && /\[\^(?:e)?\d+\]/u.test(text)) return refusal("unsupportedBlock", "Canonical block insertion requires plain text.");
1043
+ if (markdown === void 0 && !noteReferenceEditsAllowed() && /\[\^(?:e)?\d+\]/u.test(text)) return refusal("protectedReference", "An insertion writes no note marker of its own.");
1021
1044
  const split = markdown === void 0 && !inline && LINE_BREAK_PATTERN.test(text);
1022
1045
  const lines = split ? text.split(LINE_BREAK_PATTERN).filter((line) => line.trim().length > 0) : [text];
1023
1046
  const texts = lines.length === 0 ? [""] : lines;
@@ -1081,7 +1104,7 @@ const resolveBlockInsertion = ({ operation, listStart, paragraph, named, formatt
1081
1104
  },
1082
1105
  position: listStart.position
1083
1106
  };
1084
- if (place === void 0) return refusal("unsupportedBlock", "Canonical insertion beside a table needs a paragraph on that side of it.");
1107
+ if (place === void 0) return refusal("unsupportedBlock", "Canonical insertion beside a table needs a paragraph on that side of it.", INVALID_REQUEST);
1085
1108
  const intents = [{
1086
1109
  type: "insertParagraphs",
1087
1110
  story,
@@ -1090,7 +1113,7 @@ const resolveBlockInsertion = ({ operation, listStart, paragraph, named, formatt
1090
1113
  ...stated.newList === void 0 ? {} : { newList: stated.newList }
1091
1114
  }];
1092
1115
  const { position } = place;
1093
- if (operation.comment !== void 0 && texts.every((line) => line.length === 0)) return refusal("unsupportedBlock");
1116
+ if (operation.comment !== void 0 && texts.every((line) => line.length === 0)) return refusal("unsupportedBlock", void 0, INVALID_REQUEST);
1094
1117
  const resolved = {
1095
1118
  intents,
1096
1119
  normalizations: split ? [{
@@ -1118,13 +1141,17 @@ const resolveBlockInsertion = ({ operation, listStart, paragraph, named, formatt
1118
1141
  blockId: place.at.blockId,
1119
1142
  offset: 0
1120
1143
  };
1144
+ const beside = place.at.type === "after" ? projection.paragraph(place.at.blockId) : void 0;
1121
1145
  return Result.ok({
1122
1146
  type: "commented",
1123
1147
  operation,
1124
1148
  comment: {
1125
1149
  text: operation.comment.text,
1126
1150
  from: anchor,
1127
- to: anchor,
1151
+ to: beside === void 0 ? anchor : {
1152
+ ...anchor,
1153
+ offset: paragraphLength(beside.source)
1154
+ },
1128
1155
  inserted: true
1129
1156
  },
1130
1157
  ...resolved
@@ -1705,7 +1732,7 @@ const resolveTableInsertion = ({ operation, listStart, paragraph, projection, st
1705
1732
  parties: operation.parties
1706
1733
  });
1707
1734
  }
1708
- if (node === null) return refusal("unsupportedBlock");
1735
+ if (node === null) return refusal("unsupportedBlock", void 0, INVALID_REQUEST);
1709
1736
  const attrs = expectTableAttrs(node);
1710
1737
  const formatting = tableAttrsToFormatting(attrs);
1711
1738
  const table = {
@@ -1798,7 +1825,7 @@ const resolveTableInsertion = ({ operation, listStart, paragraph, projection, st
1798
1825
  table: outerTable,
1799
1826
  tableStart: outer.before + 1
1800
1827
  });
1801
- if (blockId === void 0) return refusal("unsupportedBlock");
1828
+ if (blockId === void 0) return refusal("unsupportedBlock", void 0, INVALID_REQUEST);
1802
1829
  return besideTable(blockId, outer[side]);
1803
1830
  }
1804
1831
  const { place } = besidePendingJoin({
@@ -2093,7 +2120,7 @@ const resolveTableStructure = ({ operation, paragraph, projection, story, refusa
2093
2120
  bottom: 0
2094
2121
  }, column);
2095
2122
  const addressed = at === void 0 ? void 0 : projection.addressAt(at);
2096
- if (addressed === void 0 || addressed.isErr()) return refusal("unsupportedBlock");
2123
+ if (addressed === void 0 || addressed.isErr()) return refusal("unsupportedBlock", void 0, INVALID_REQUEST);
2097
2124
  const address = addressed.value;
2098
2125
  const blockId = deleted.has(address.blockId) ? keptTableAddress({
2099
2126
  projection,
@@ -2131,7 +2158,7 @@ const resolveTableStructure = ({ operation, paragraph, projection, story, refusa
2131
2158
  if (overflow !== null) return refusal("payloadDoesNotFit", overflow);
2132
2159
  if (tracked && columnInsertionWidensCell(row.table, insertion.columnIndex)) return skipTracked("A suggested column cannot widen a cell that spans other columns.");
2133
2160
  const blockId = tableBlockAddress(projection, row, deleted);
2134
- if (blockId === void 0) return refusal("unsupportedBlock");
2161
+ if (blockId === void 0) return refusal("unsupportedBlock", void 0, INVALID_REQUEST);
2135
2162
  return resolved({
2136
2163
  type: "table",
2137
2164
  operation: {
@@ -2175,7 +2202,7 @@ const resolveTableStructure = ({ operation, paragraph, projection, story, refusa
2175
2202
  if (new Set(cellsIn(map, rectangle)).size < 2) return skipAlike("unsupportedBlock", "The merge covers a single merged cell.");
2176
2203
  if (tracked && !mergesDownOverEmptyCells(row.table, map, rectangle)) return skipTracked("A suggested merge takes one column over empty cells.");
2177
2204
  const blockId = tableBlockAddress(projection, row, deleted);
2178
- if (blockId === void 0) return refusal("unsupportedBlock");
2205
+ if (blockId === void 0) return refusal("unsupportedBlock", void 0, INVALID_REQUEST);
2179
2206
  return resolved({
2180
2207
  type: "table",
2181
2208
  operation: {
@@ -2221,12 +2248,12 @@ const resolveTableStructure = ({ operation, paragraph, projection, story, refusa
2221
2248
  revision: null,
2222
2249
  ...template === void 0 ? {} : { template }
2223
2250
  });
2224
- if (built.type !== "applied") return refusal("unsupportedBlock");
2251
+ if (built.type !== "applied") return refusal("unsupportedBlock", void 0, INTERNAL);
2225
2252
  const table = built.transaction.doc.nodeAt(built.transaction.mapping.map(row.tablePosition, -1));
2226
- if (table === null || table.type.name !== "table") return refusal("unsupportedBlock");
2253
+ if (table === null || table.type.name !== "table") return refusal("unsupportedBlock", void 0, INTERNAL);
2227
2254
  const index = side === "before" ? row.rowIndex : row.rowIndex + 1;
2228
2255
  const inserted = standaloneTableRowsFromProseMirror(table)[index];
2229
- if (inserted === void 0) return refusal("unsupportedBlock");
2256
+ if (inserted === void 0) return refusal("unsupportedBlock", void 0, INTERNAL);
2230
2257
  const { structuralChange: _structuralChange, propertyChanges: _propertyChanges, ...plain } = inserted;
2231
2258
  return resolved({
2232
2259
  type: "tableRow",
@@ -2294,6 +2321,49 @@ const emphasisRuns = (line) => parseInlineEmphasisRuns(line).flatMap(({ text, bo
2294
2321
  }
2295
2322
  }];
2296
2323
  });
2324
+ /**
2325
+ * The spans of the words a replacement writes that its paired `**bold**` and
2326
+ * `***bold italic***` markers mark, and the emphasis each states.
2327
+ */
2328
+ const emphasizedSpans = (emphasis) => {
2329
+ const spans = [];
2330
+ let offset = 0;
2331
+ for (const { text, bold, italic } of emphasis ?? []) {
2332
+ const end = offset + text.length;
2333
+ if (end > offset && (bold || italic)) spans.push({
2334
+ start: offset,
2335
+ end,
2336
+ patch: {
2337
+ ...bold ? { bold: true } : {},
2338
+ ...italic ? { italic: true } : {}
2339
+ }
2340
+ });
2341
+ offset = end;
2342
+ }
2343
+ return spans;
2344
+ };
2345
+ /** A plan that replaces the whole match, its words standing together. */
2346
+ const rewriteWhole = (source, target) => [{
2347
+ start: 0,
2348
+ end: source.length,
2349
+ text: target
2350
+ }];
2351
+ /**
2352
+ * Paragraph properties kept by a replacement that drops the paragraph's
2353
+ * formatting: the review records of its numbering, which only resolving
2354
+ * them retires.
2355
+ */
2356
+ const NUMBERING_REVIEW_RECORDS = /* @__PURE__ */ new Set(["numberingChangeXml", "numberingInsertionXml"]);
2357
+ /**
2358
+ * The patch a replacement that drops the paragraph's formatting writes:
2359
+ * every property the paragraph states goes, as the default engine writes a
2360
+ * fresh paragraph, save the review records of its numbering.
2361
+ */
2362
+ const droppedParagraphFormatting = (formatting) => {
2363
+ const patch = {};
2364
+ for (const key of Object.keys(formatting ?? {})) if (!NUMBERING_REVIEW_RECORDS.has(key)) patch[key] = null;
2365
+ return patch;
2366
+ };
2297
2367
  const inlineText = (line) => line.split(/(\t|\r\n|\r|\n)/u).flatMap((part) => {
2298
2368
  if (part === " ") return [{ type: "tab" }];
2299
2369
  if (LINE_BREAK_PATTERN.test(part)) return [{ type: "break" }];
@@ -2302,6 +2372,15 @@ const inlineText = (line) => line.split(/(\t|\r\n|\r|\n)/u).flatMap((part) => {
2302
2372
  text: part
2303
2373
  }];
2304
2374
  });
2375
+ const PROPERTY_FOLDING_INTENTS = /* @__PURE__ */ new Set([
2376
+ "joinParagraphs",
2377
+ "splitParagraph",
2378
+ "formatParagraph",
2379
+ "setList",
2380
+ "deleteParagraphs"
2381
+ ]);
2382
+ /** Each paragraph property change of the story, by revision id. */
2383
+ const propertyChangesById = (document, story) => new Map(paragraphsThroughTextBoxes(document, story).flatMap(({ paragraph }) => paragraph.propertyChanges?.map((change) => [change.info.id, change]) ?? []));
2305
2384
  /**
2306
2385
  * Room past the highest revision or annotation id below which a batch draws
2307
2386
  * its revision ids from the free ones instead: more than any one batch asks.
@@ -2359,12 +2438,13 @@ const primaryRevisionId = ({ operation, intents, document, revisionIds }) => {
2359
2438
  const deletion = mark?.kind === "del" ? mark.info.id : mark?.deletion?.info.id;
2360
2439
  return deletion !== void 0 && revisionIds.includes(deletion) ? deletion : revisionIds.at(0);
2361
2440
  };
2362
- const compilePublicIntents = ({ document, intents, mode, author, date, suggestionId, nextRevisionId, allocate, reservedBlockIds, idLease, paragraphAllocationLease, allocations: admittedAllocations, verifyOperation }) => {
2441
+ const compilePublicIntents = ({ document, intents, mode, author, date, suggestionId, nextRevisionId, allocate, reservedBlockIds, idLease, paragraphAllocationLease, allocations: admittedAllocations, verifyOperation, story }) => {
2363
2442
  const ops = [];
2364
2443
  const recorded = [];
2365
2444
  let verifiedOperation;
2366
2445
  const absorbed = [];
2367
2446
  const source = document;
2447
+ const standing = mode === "direct" || !intents.some((intent) => PROPERTY_FOLDING_INTENTS.has(intent.type)) ? [] : [...propertyChangesById(source, story)].flatMap(([id, change]) => change.resolvesWith !== void 0 && change.info.author === author && change.info.date === date && change.info.suggestionId === suggestionId ? [[id, JSON.stringify(change)]] : []);
2368
2448
  const joinedInto = /* @__PURE__ */ new Map();
2369
2449
  const allocated = /* @__PURE__ */ new Map();
2370
2450
  const allocations = [];
@@ -2460,6 +2540,12 @@ const compilePublicIntents = ({ document, intents, mode, author, date, suggestio
2460
2540
  appliedRevisions.push(...notes.revisions);
2461
2541
  }
2462
2542
  document = after;
2543
+ const now = standing.length === 0 ? void 0 : propertyChangesById(after, story);
2544
+ if (now !== void 0 && standing.some(([id, change]) => JSON.stringify(now.get(id)) !== change)) return Result.err(new DocumentOpRefusal({
2545
+ reason: DOCUMENT_OP_REFUSAL_REASONS.PENDING_PARAGRAPH_PROPERTY_CHANGE,
2546
+ message: "Resolve the pending paragraph join before tracking a different change to its properties.",
2547
+ opType: void 0
2548
+ }));
2463
2549
  ops.push(...compiled.value.ops, ...notes?.ops ?? []);
2464
2550
  recorded.push({
2465
2551
  revisions: appliedRevisions,
@@ -2939,8 +3025,7 @@ var CanonicalPublicOperations = class {
2939
3025
  });
2940
3026
  };
2941
3027
  const refuseResolution = (id, error) => {
2942
- const gap = error.gap === null ? void 0 : error.gap ?? (error.reason === "unsupportedBlock" ? CANONICAL_GAP.publicUnsupportedInline : void 0);
2943
- if (gap !== void 0) refusals.set(id, { gap });
3028
+ if (error.ground !== void 0) refusals.set(id, error.ground);
2944
3029
  skip(id, error.reason, error.detail);
2945
3030
  };
2946
3031
  const result = (applied, undoHandle, disposition = "normal") => {
@@ -3054,12 +3139,6 @@ var CanonicalPublicOperations = class {
3054
3139
  skip(operation.id, undefinedReference.reason, undefinedReference.message);
3055
3140
  continue;
3056
3141
  }
3057
- const disposition = CANONICAL_PUBLIC_OPERATION_DISPOSITIONS[operation.type];
3058
- if (disposition !== "compile") {
3059
- refusals.set(operation.id, { gap: disposition });
3060
- skip(operation.id, "unsupportedBlock");
3061
- continue;
3062
- }
3063
3142
  const compiled = this.resolve({
3064
3143
  operation,
3065
3144
  options,
@@ -3315,6 +3394,7 @@ var CanonicalPublicOperations = class {
3315
3394
  };
3316
3395
  const compiled = compilePublicIntents({
3317
3396
  document: withHeadings,
3397
+ story,
3318
3398
  intents: mappedIntents,
3319
3399
  allocate,
3320
3400
  reservedBlockIds,
@@ -3359,8 +3439,13 @@ var CanonicalPublicOperations = class {
3359
3439
  story,
3360
3440
  ops: compiled.value.ops
3361
3441
  });
3442
+ if (id.isErr()) return declined({
3443
+ reason: "unsupportedBlock",
3444
+ message: id.error.message,
3445
+ refusal: groundForDocumentOpRefusal(id.error.reason)
3446
+ });
3362
3447
  const commentContent = admittedComment?.content ?? canonicalCommentBody(nextDocument, comment.text, commentLease);
3363
- const commented = id.isErr() || firstAdded?.paraId === void 0 || lastAdded?.paraId === void 0 ? void 0 : compileCanonicalComments({
3448
+ const commented = compileCanonicalComments({
3364
3449
  document: nextDocument,
3365
3450
  idLease,
3366
3451
  command: {
@@ -3373,7 +3458,10 @@ var CanonicalPublicOperations = class {
3373
3458
  content: commentContent,
3374
3459
  ...proposedBy
3375
3460
  },
3376
- anchor: {
3461
+ anchor: firstAdded?.paraId === void 0 || lastAdded?.paraId === void 0 ? {
3462
+ kind: "point",
3463
+ at: comment.to
3464
+ } : {
3377
3465
  kind: "range",
3378
3466
  from: {
3379
3467
  story,
@@ -3388,10 +3476,10 @@ var CanonicalPublicOperations = class {
3388
3476
  }
3389
3477
  }
3390
3478
  });
3391
- if (commented === void 0 || commented.isErr() || id.isErr()) return declined({
3479
+ if (commented.isErr()) return declined({
3392
3480
  reason: "unsupportedBlock",
3393
- message: commented?.isErr() === true ? commented.error.message : "The inserted paragraphs cannot carry the comment.",
3394
- refusal: { gap: CANONICAL_GAP.publicComments }
3481
+ message: commented.error.message,
3482
+ refusal: commented.error.ground
3395
3483
  });
3396
3484
  nextDocument = commented.value.document;
3397
3485
  operationOps.push(...commented.value.ops);
@@ -3409,10 +3497,15 @@ var CanonicalPublicOperations = class {
3409
3497
  });
3410
3498
  const before = anchorIn(sourceDocument);
3411
3499
  const after = anchorIn(nextDocument);
3412
- if (id.isErr() || before === void 0 || after === void 0) return declined({
3500
+ if (id.isErr()) return declined({
3413
3501
  reason: "unsupportedBlock",
3414
- message: id.isErr() ? id.error.message : "The comment anchor no longer exists.",
3415
- refusal: { gap: CANONICAL_GAP.publicComments }
3502
+ message: id.error.message,
3503
+ refusal: groundForDocumentOpRefusal(id.error.reason)
3504
+ });
3505
+ if (before === void 0 || after === void 0) return declined({
3506
+ reason: "missingBlock",
3507
+ message: "The comment anchor no longer exists.",
3508
+ refusal: STALE
3416
3509
  });
3417
3510
  const changed = batch.mode === "direct" ? void 0 : revisionSpan(after.content, new Set(compiled.value.revisions));
3418
3511
  const from = changed === void 0 ? shift(comment.from) : {
@@ -3455,7 +3548,7 @@ var CanonicalPublicOperations = class {
3455
3548
  if (commented.isErr()) return declined({
3456
3549
  reason: "unsupportedBlock",
3457
3550
  message: commented.error.message,
3458
- refusal: { gap: CANONICAL_GAP.publicComments }
3551
+ refusal: commented.error.ground
3459
3552
  });
3460
3553
  nextDocument = commented.value.document;
3461
3554
  operationOps.push(...commented.value.ops);
@@ -3876,10 +3969,11 @@ var CanonicalPublicOperations = class {
3876
3969
  if (ops.length === 0) return result([], null);
3877
3970
  const prepared = projected.value.selectionAt(state).andThen((selection) => reuseStaging ? operationStaging.prepare(state, selection) : operationStaging.prepareOps(state, ops, selection));
3878
3971
  if (prepared.isErr()) {
3879
- const noChange = prepared.error.reason === "noChange";
3972
+ const { reason, ground, message } = prepared.error;
3973
+ const skipReason = stagingRefusalReason(reason, ground);
3880
3974
  for (const { id } of appliedById.values()) {
3881
- if (!noChange) refusals.set(id, { gap: CANONICAL_GAP.publicUnsupportedInline });
3882
- skip(id, noChange ? "noopOperation" : "unsupportedBlock", prepared.error.message);
3975
+ if (reason !== "noChange") refusals.set(id, ground);
3976
+ skip(id, skipReason, message);
3883
3977
  }
3884
3978
  return result([], null);
3885
3979
  }
@@ -4025,7 +4119,7 @@ var CanonicalPublicOperations = class {
4025
4119
  * the mark are marked deleted.
4026
4120
  */
4027
4121
  resolveBlockDeletion({ operation, paragraph, projection, projectionStory, doc, tracked, deletesJoinedWords, gainsFollower, keepsTablesApart, followsInsertedTable, precedesInsertedTable, bordersRemovedTable, previousGainsFollower, tableBeforeGainsFollower, endsOnceAccepted, deletedRun, deletedByBatch, refusal }) {
4028
- if (operation.comment !== void 0 && !tracked) return refusal("unsupportedBlock");
4122
+ if (operation.comment !== void 0 && !tracked) return refusal("unsupportedBlock", void 0, INVALID_REQUEST);
4029
4123
  if (tracked && goesWholeOnAcceptance(paragraph.node)) return refusal("noopOperation");
4030
4124
  const end = paragraph.start + paragraph.node.content.size;
4031
4125
  const after = doc.resolve(end + 1).nodeAfter;
@@ -4038,7 +4132,7 @@ var CanonicalPublicOperations = class {
4038
4132
  const previous = followsInsertedTable ? void 0 : neighbour(before);
4039
4133
  const commentFrom = projection.addressAt(paragraph.start);
4040
4134
  const commentTo = projection.addressAt(end);
4041
- if (commentFrom.isErr() || commentTo.isErr()) return refusal("unsupportedBlock");
4135
+ if (commentFrom.isErr() || commentTo.isErr()) return refusal("unsupportedBlock", void 0, INVALID_REQUEST);
4042
4136
  const tracking = (resolved) => Result.ok(operation.comment === void 0 ? {
4043
4137
  type: "plain",
4044
4138
  operation,
@@ -4066,7 +4160,7 @@ var CanonicalPublicOperations = class {
4066
4160
  const start = keepsTablesApart ? paragraph.start : joinedWordsStart ?? paragraph.start;
4067
4161
  const from = projection.addressAt(start);
4068
4162
  const to = projection.addressAt(end);
4069
- if (from.isErr() || to.isErr()) return refusal("unsupportedBlock");
4163
+ if (from.isErr() || to.isErr()) return refusal("unsupportedBlock", void 0, INVALID_REQUEST);
4070
4164
  return tracking({
4071
4165
  intents: [{
4072
4166
  type: "replaceText",
@@ -4121,7 +4215,7 @@ var CanonicalPublicOperations = class {
4121
4215
  const own = (from, to) => {
4122
4216
  const start = projection.addressAt(from);
4123
4217
  const stop = projection.addressAt(to);
4124
- return start.isErr() || stop.isErr() ? refusal("unsupportedBlock") : Result.ok(from === to ? [] : [{
4218
+ return start.isErr() || stop.isErr() ? refusal("unsupportedBlock", void 0, INVALID_REQUEST) : Result.ok(from === to ? [] : [{
4125
4219
  type: "replaceText",
4126
4220
  from: start.value,
4127
4221
  to: stop.value,
@@ -4210,7 +4304,7 @@ var CanonicalPublicOperations = class {
4210
4304
  if (previousGainsFollower && noDeletedRunBefore(deletedRun)) {
4211
4305
  const from = projection.addressAt(range.from);
4212
4306
  const to = projection.addressAt(end);
4213
- if (from.isErr() || to.isErr()) return refusal("unsupportedBlock");
4307
+ if (from.isErr() || to.isErr()) return refusal("unsupportedBlock", void 0, INVALID_REQUEST);
4214
4308
  return tracking({
4215
4309
  intents: [{
4216
4310
  type: "replaceText",
@@ -4228,7 +4322,7 @@ var CanonicalPublicOperations = class {
4228
4322
  if (after === null && tableBeforeGainsFollower) {
4229
4323
  const from = projection.addressAt(paragraph.start);
4230
4324
  const to = projection.addressAt(end);
4231
- if (from.isErr() || to.isErr()) return refusal("unsupportedBlock");
4325
+ if (from.isErr() || to.isErr()) return refusal("unsupportedBlock", void 0, INVALID_REQUEST);
4232
4326
  return tracking({
4233
4327
  intents: [{
4234
4328
  type: "replaceText",
@@ -4251,7 +4345,7 @@ var CanonicalPublicOperations = class {
4251
4345
  }
4252
4346
  const from = projection.addressAt(range.from);
4253
4347
  const to = projection.addressAt(range.to);
4254
- if (from.isErr() || to.isErr()) return refusal("unsupportedBlock");
4348
+ if (from.isErr() || to.isErr()) return refusal("unsupportedBlock", void 0, INVALID_REQUEST);
4255
4349
  return tracking({
4256
4350
  intents: [{
4257
4351
  type: "replaceText",
@@ -4292,10 +4386,11 @@ var CanonicalPublicOperations = class {
4292
4386
  if (anchor === void 0 || projection.isErr() || projection.value.paragraph(blockId) !== void 0) return Result.ok({ operation });
4293
4387
  const control = emptyControlOfFiller(state.doc, blockId);
4294
4388
  if (control === void 0) return Result.ok({ operation });
4295
- const refusal = (reason, message) => Result.err(new CanonicalPublicOperationResolutionError({
4389
+ const refusal = (reason, message, ground) => Result.err(new CanonicalPublicOperationResolutionError({
4296
4390
  reason,
4297
4391
  message: message ?? `The operation was refused: ${reason}.`,
4298
- detail: message
4392
+ detail: message,
4393
+ ...ground === void 0 ? {} : { ground }
4299
4394
  }));
4300
4395
  const fillerHash = hashFolioAIBlockContent("");
4301
4396
  if (operation.precondition && fillerHash !== operation.precondition.blockTextHash) return refusal("preconditionFailed");
@@ -4306,7 +4401,7 @@ var CanonicalPublicOperations = class {
4306
4401
  projection: projection.value
4307
4402
  });
4308
4403
  switch (beside.type) {
4309
- case "refused": return refusal("unsupportedBlock", beside.message);
4404
+ case "refused": return refusal("unsupportedBlock", beside.message, INVALID_REQUEST);
4310
4405
  case "beside": return Result.ok({ operation: beside.operation });
4311
4406
  case "listStart": return Result.ok({
4312
4407
  operation: beside.operation,
@@ -4316,22 +4411,21 @@ var CanonicalPublicOperations = class {
4316
4411
  }
4317
4412
  }
4318
4413
  resolveOperation({ operation, listStart, options, state, story, diff, tracked, suggested, mintList, besideDeletions, batchComments }) {
4319
- const refusal = (reason, message) => Result.err(new CanonicalPublicOperationResolutionError({
4414
+ const refusal = (reason, message, ground) => Result.err(new CanonicalPublicOperationResolutionError({
4320
4415
  reason,
4321
4416
  message: message ?? `The operation was refused: ${reason}.`,
4322
- detail: message
4417
+ detail: message,
4418
+ ...ground === void 0 ? {} : { ground }
4323
4419
  }));
4324
4420
  const skipAlike = (reason, message) => Result.err(new CanonicalPublicOperationResolutionError({
4325
4421
  reason,
4326
4422
  message,
4327
- detail: message,
4328
- gap: null
4423
+ detail: message
4329
4424
  }));
4330
4425
  const skipTracked = (message) => Result.err(new CanonicalPublicOperationResolutionError({
4331
4426
  reason: "unsupportedBlock",
4332
4427
  message,
4333
4428
  detail: message,
4334
- gap: null,
4335
4429
  refusedIn: "trackedMode"
4336
4430
  }));
4337
4431
  const notInTable = () => skipAlike("unsupportedBlock", "The block is not in a table.");
@@ -4364,7 +4458,7 @@ var CanonicalPublicOperations = class {
4364
4458
  const projection = this.options.session.projectStory(story);
4365
4459
  if (!anchor || projection.isErr()) return refusal("missingBlock");
4366
4460
  const paragraph = projection.value.paragraph(blockId);
4367
- if (!paragraph) return holdsParagraph(state.doc, blockId) ? refusal("unsupportedBlock", "The canonical session holds no address in this paragraph.") : refusal("missingBlock");
4461
+ if (!paragraph) return holdsParagraph(state.doc, blockId) ? refusal("unsupportedBlock", "The canonical session holds no address in this paragraph.", INVALID_REQUEST) : refusal("missingBlock");
4368
4462
  if ((operation.type === "insertTable" || operation.type === "insertSignatureTable") && !supportsTableInsertionMode(operation.type, options.batch.mode ?? "tracked-changes")) return refusal("unsupportedMode");
4369
4463
  const noteReferences = collectNoteReferenceLabels(state.doc);
4370
4464
  const clean = buildCleanBlockText(paragraph.node, paragraph.start - 1, {
@@ -4383,7 +4477,7 @@ var CanonicalPublicOperations = class {
4383
4477
  reason: pendingRefusal.reason,
4384
4478
  message: pendingRefusal.message ?? `The operation was refused: ${pendingRefusal.reason}.`,
4385
4479
  detail: pendingRefusal.message,
4386
- ...pendingRefusal.canonicalRefusal === void 0 ? {} : { gap: pendingRefusal.canonicalRefusal.gap }
4480
+ ...pendingRefusal.canonicalRefusal === void 0 ? {} : { ground: pendingRefusal.canonicalRefusal }
4387
4481
  }));
4388
4482
  const hash = hashFolioAIBlockContent(clean.text);
4389
4483
  if (operation.precondition && hash !== operation.precondition.blockTextHash) return refusal("preconditionFailed");
@@ -4589,7 +4683,7 @@ var CanonicalPublicOperations = class {
4589
4683
  if (start <= 0 || start >= clean.text.length || clean.text.slice(start, end) !== (operation.separator ?? "")) return refusal("staleRange");
4590
4684
  break;
4591
4685
  case "mergeBlockWithNext":
4592
- if (/[\t\r\n]/u.test(operation.separator ?? "")) return refusal("unsupportedBlock", "Canonical public join separators require plain text.");
4686
+ if (/\r/u.test(operation.separator ?? "")) return refusal("unsupportedBlock", "Canonical public join separators hold no carriage return.", INVALID_REQUEST);
4593
4687
  start = clean.text.length;
4594
4688
  end = start;
4595
4689
  break;
@@ -4608,12 +4702,15 @@ var CanonicalPublicOperations = class {
4608
4702
  if (!range) return Result.err(new CanonicalPublicOperationResolutionError({
4609
4703
  reason: "unsupportedBlock",
4610
4704
  message: "The range begins or ends inside a field result or holds a page-break run.",
4611
- detail: void 0,
4612
- gap: null
4705
+ detail: void 0
4613
4706
  }));
4614
4707
  const dropsFormatting = operation.type === "replaceBlock" && operation.preserveFormatting === false;
4615
- if (replacesText && references.length > 0 && (dropsFormatting || stripInlineEmphasisMarkers(text) !== text)) return refusal("protectedReference");
4616
- if (dropsFormatting) return refusal("unsupportedBlock", "Canonical replacements that drop the paragraph's formatting are unavailable.");
4708
+ const emphasis = replacesText && hasInlineEmphasis(text) ? parseInlineEmphasisRuns(text) : void 0;
4709
+ if (replacesText && references.length > 0 && (dropsFormatting || emphasis !== void 0)) return refusal("protectedReference");
4710
+ if (emphasis !== void 0) text = stripInlineEmphasisMarkers(text);
4711
+ const rebuilds = !tracked && dropsFormatting;
4712
+ const emphasized = tracked ? [] : emphasizedSpans(emphasis);
4713
+ const rewritesWhole = rebuilds || emphasized.length > 0;
4617
4714
  const kept = segmentsAroundNoteReferences(clean.text.slice(start, end), replacesText ? text : clean.text.slice(start, end), references);
4618
4715
  const editsReferences = kept === null && replacesText && noteReferenceEditsAllowed();
4619
4716
  const segments = editsReferences ? {
@@ -4625,11 +4722,11 @@ var CanonicalPublicOperations = class {
4625
4722
  } : kept;
4626
4723
  if (segments === null) return refusal("protectedReference");
4627
4724
  const prose = replacesText ? segments.replacement.join("") : "";
4628
- if (stripInlineEmphasisMarkers(text) !== text) return refusal("unsupportedBlock", "Canonical public emphasis replacements are unavailable.");
4629
- if (!editsReferences && /\[\^(?:e)?\d+\]/u.test(prose) || /\r/u.test(prose)) return refusal("unsupportedBlock", "Canonical public replacements require plain text.");
4725
+ if (!editsReferences && /\[\^(?:e)?\d+\]/u.test(prose)) return refusal("protectedReference", "A replacement writes no note marker of its own.");
4726
+ if (/\r/u.test(prose)) return refusal("unsupportedBlock", "Canonical public replacements require plain text.", INVALID_REQUEST);
4630
4727
  const from = projection.value.addressAt(range.from);
4631
4728
  const to = projection.value.addressAt(range.to);
4632
- if (from.isErr() || to.isErr()) return refusal("unsupportedBlock");
4729
+ if (from.isErr() || to.isErr()) return refusal("unsupportedBlock", void 0, INVALID_REQUEST);
4633
4730
  const resolvedComment = resolveComment({
4634
4731
  operation,
4635
4732
  from: from.value,
@@ -4651,7 +4748,7 @@ var CanonicalPublicOperations = class {
4651
4748
  properties: { styleId: operation.styleId },
4652
4749
  mintList
4653
4750
  }) : [];
4654
- if (resolvedComment.type === "plain" && operation.type !== "splitBlock" && operation.type !== "formatRange" && operation.type !== "mergeBlockWithNext" && restyle.length === 0 && clean.text.slice(start, end) === text) return refusal("noopOperation");
4751
+ if (resolvedComment.type === "plain" && operation.type !== "splitBlock" && operation.type !== "formatRange" && operation.type !== "mergeBlockWithNext" && restyle.length === 0 && !rewritesWhole && clean.text.slice(start, end) === text) return refusal("noopOperation");
4655
4752
  let intent;
4656
4753
  switch (operation.type) {
4657
4754
  case "splitBlock":
@@ -4735,9 +4832,12 @@ var CanonicalPublicOperations = class {
4735
4832
  length: boundary.length
4736
4833
  }] : [])
4737
4834
  };
4835
+ let planChanges = planTextChanges;
4836
+ if (tracked) planChanges = (source, target) => changesFromSegments(diff(source, target));
4837
+ if (rewritesWhole) planChanges = rewriteWhole;
4738
4838
  let planned = planReplacementChanges({
4739
4839
  ...replacementSource,
4740
- planChanges: tracked ? (source, target) => changesFromSegments(diff(source, target)) : planTextChanges,
4840
+ planChanges,
4741
4841
  ...tracked ? { keepFieldsOf: planTextChanges } : {}
4742
4842
  });
4743
4843
  const addedComments = tracked ? batchCommentSpans(batchComments, blockId, clean.text) : [];
@@ -4793,7 +4893,7 @@ var CanonicalPublicOperations = class {
4793
4893
  };
4794
4894
  const addressable = (placed) => placed.every(({ from: changeFrom, to: changeTo }) => projection.value.addressAt(changeFrom, changeFrom < changeTo ? "afterMarkers" : void 0).isOk() && projection.value.addressAt(changeTo, changeFrom < changeTo ? "beforeMarkers" : void 0).isOk());
4795
4895
  let replacementChanges = placeChanges(changes);
4796
- if (replacementChanges === void 0) return refusal("unsupportedBlock");
4896
+ if (replacementChanges === void 0) return refusal("unsupportedBlock", void 0, INVALID_REQUEST);
4797
4897
  if (!addressable(replacementChanges)) {
4798
4898
  const whole = placeChanges(segments.source.flatMap((piece, index) => {
4799
4899
  const replacement = segments.replacement[index] ?? "";
@@ -4805,7 +4905,7 @@ var CanonicalPublicOperations = class {
4805
4905
  text: replacement
4806
4906
  }];
4807
4907
  }));
4808
- if (whole === void 0) return refusal("unsupportedBlock");
4908
+ if (whole === void 0) return refusal("unsupportedBlock", void 0, INVALID_REQUEST);
4809
4909
  replacementChanges = whole;
4810
4910
  }
4811
4911
  const comments = readModel(this.options.session.document).package.document.comments ?? [];
@@ -4833,7 +4933,7 @@ var CanonicalPublicOperations = class {
4833
4933
  }
4834
4934
  })) return Result.err(new CanonicalPublicOperationResolutionError({
4835
4935
  reason: "unsupportedBlock",
4836
- gap: CANONICAL_GAP.commentReplacementAlignment,
4936
+ ground: { gap: CANONICAL_GAP.commentReplacementAlignment },
4837
4937
  message: "Canonical replacement cannot preserve the aligned comment ranges.",
4838
4938
  detail: "Canonical replacement cannot preserve the aligned comment ranges."
4839
4939
  }));
@@ -4870,7 +4970,7 @@ var CanonicalPublicOperations = class {
4870
4970
  });
4871
4971
  const clearedFrom = cleared && projection.value.addressAt(cleared.from, "afterMarkers");
4872
4972
  const clearedTo = cleared && projection.value.addressAt(cleared.to, "beforeMarkers");
4873
- if (!clearedFrom || !clearedTo || clearedFrom.isErr() || clearedTo.isErr()) return refusal("unsupportedBlock");
4973
+ if (!clearedFrom || !clearedTo || clearedFrom.isErr() || clearedTo.isErr()) return refusal("unsupportedBlock", void 0, INVALID_REQUEST);
4874
4974
  intents.push({
4875
4975
  type: "formatRun",
4876
4976
  from: clearedFrom.value,
@@ -4919,6 +5019,7 @@ var CanonicalPublicOperations = class {
4919
5019
  });
4920
5020
  }
4921
5021
  }
5022
+ let wordsStart;
4922
5023
  for (const [index, change] of [...replacementChanges.entries()].reverse()) {
4923
5024
  let gap = {
4924
5025
  from: change.from,
@@ -4931,7 +5032,7 @@ var CanonicalPublicOperations = class {
4931
5032
  const tailStart = sourceHolder.after();
4932
5033
  const tailFrom = projection.value.addressAt(tailStart, "afterMarkers");
4933
5034
  const tailTo = projection.value.addressAt(gap.to, "beforeMarkers");
4934
- if (tailFrom.isErr() || tailTo.isErr()) return refusal("unsupportedBlock");
5035
+ if (tailFrom.isErr() || tailTo.isErr()) return refusal("unsupportedBlock", void 0, INVALID_REQUEST);
4935
5036
  intents.push({
4936
5037
  type: "replaceText",
4937
5038
  from: tailFrom.value,
@@ -4951,7 +5052,7 @@ var CanonicalPublicOperations = class {
4951
5052
  } : void 0;
4952
5053
  const changeFrom = projection.value.addressAt(gap.from, edges?.start);
4953
5054
  const changeTo = projection.value.addressAt(gap.to, edges?.end);
4954
- if (changeFrom.isErr() || changeTo.isErr()) return refusal("unsupportedBlock");
5055
+ if (changeFrom.isErr() || changeTo.isErr()) return refusal("unsupportedBlock", void 0, INVALID_REQUEST);
4955
5056
  let inside = controlEdgesAt(projection.value.doc, gap.from, gap.to);
4956
5057
  if (gap.from === gap.to && (inside.start === true || inside.end === true)) {
4957
5058
  const edge = projection.value.doc.resolve(gap.from);
@@ -4961,6 +5062,7 @@ var CanonicalPublicOperations = class {
4961
5062
  ...inside,
4962
5063
  ...change.start === start ? { start: true } : { end: true }
4963
5064
  };
5065
+ if (index === 0) wordsStart = changeFrom.value;
4964
5066
  intents.push({
4965
5067
  type: "replaceText",
4966
5068
  from: changeFrom.value,
@@ -4968,15 +5070,45 @@ var CanonicalPublicOperations = class {
4968
5070
  text: change.text,
4969
5071
  ...sources === void 0 ? {} : { formattingSources: sources },
4970
5072
  ...Object.keys(inside).length === 0 ? {} : { inside },
5073
+ ...rebuilds ? { runProps: {} } : {},
4971
5074
  ...intent.runPropsPatch === void 0 ? {} : { runPropsPatch: intent.runPropsPatch }
4972
5075
  });
4973
5076
  }
5077
+ if (wordsStart !== void 0) for (const { start: spanStart, end: spanEnd, patch } of emphasized) {
5078
+ const { story: wordsStory, blockId: wordsBlockId } = wordsStart;
5079
+ intents.push({
5080
+ type: "formatRun",
5081
+ from: spanStart === 0 ? wordsStart : {
5082
+ story: wordsStory,
5083
+ blockId: wordsBlockId,
5084
+ offset: wordsStart.offset + spanStart
5085
+ },
5086
+ to: {
5087
+ story: wordsStory,
5088
+ blockId: wordsBlockId,
5089
+ offset: wordsStart.offset + spanEnd
5090
+ },
5091
+ patch
5092
+ });
5093
+ }
5094
+ if (rebuilds) {
5095
+ const patch = droppedParagraphFormatting(paragraph.source.formatting);
5096
+ if (Object.keys(patch).length > 0) intents.push({
5097
+ type: "formatParagraph",
5098
+ at: {
5099
+ story,
5100
+ blockId: paragraph.blockId,
5101
+ offset: 0
5102
+ },
5103
+ patch
5104
+ });
5105
+ }
4974
5106
  intents.push(...restyle);
4975
5107
  } else if (operation.type === "mergeBlockWithNext" && intent.type === "joinParagraphs") {
4976
5108
  const separator = operation.separator ?? "";
4977
5109
  if (separator !== "") {
4978
5110
  const at = projection.value.addressAt(paragraph.start + paragraph.node.content.size);
4979
- if (at.isErr()) return refusal("unsupportedBlock");
5111
+ if (at.isErr()) return refusal("unsupportedBlock", void 0, at.error.ground);
4980
5112
  intents.push({
4981
5113
  type: "replaceText",
4982
5114
  from: at.value,