@stll/folio-core 0.73.0 → 0.75.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 (221) hide show
  1. package/dist/ai-edits/clean-text.d.ts +4 -1
  2. package/dist/ai-edits/clean-text.js +21 -7
  3. package/dist/ai-edits/headless.d.ts +5 -2
  4. package/dist/ai-edits/headless.js +20 -14
  5. package/dist/ai-edits/note-references.d.ts +1 -1
  6. package/dist/ai-edits/note-references.js +1 -1
  7. package/dist/ai-edits/pending-suggestions.d.ts +9 -7
  8. package/dist/ai-edits/pending-suggestions.js +134 -88
  9. package/dist/ai-edits/pendingJoin.d.ts +5 -1
  10. package/dist/ai-edits/pendingJoin.js +12 -3
  11. package/dist/ai-edits/result-validation.js +1 -1
  12. package/dist/ai-edits/retired-comment-receipts.d.ts +2 -2
  13. package/dist/ai-edits/retired-comment-receipts.js +3 -18
  14. package/dist/ai-edits/snapshot.js +1 -1
  15. package/dist/ai-edits/table-cell-mutations.js +1 -1
  16. package/dist/ai-edits/tracked-batch-fidelity.d.ts +6 -10
  17. package/dist/ai-edits/tracked-batch-fidelity.js +3 -182
  18. package/dist/canonical/canonicalBatchReview.d.ts +4 -1
  19. package/dist/canonical/canonicalBatchReview.js +32 -14
  20. package/dist/canonical/canonicalComments.d.ts +6 -2
  21. package/dist/canonical/canonicalComments.js +8 -4
  22. package/dist/canonical/canonicalInlineGaps.d.ts +56 -0
  23. package/dist/canonical/canonicalInlineGaps.js +118 -0
  24. package/dist/canonical/canonicalInlineProjection.d.ts +4 -13
  25. package/dist/canonical/canonicalInlineProjection.js +6 -28
  26. package/dist/canonical/canonicalProjectionBlocks.d.ts +4 -4
  27. package/dist/canonical/canonicalPublicOperations.js +99 -36
  28. package/dist/canonical/canonicalRefusalGrounds.d.ts +4 -0
  29. package/dist/canonical/canonicalRefusalGrounds.js +56 -0
  30. package/dist/canonical/canonicalSelection.js +1 -1
  31. package/dist/canonical/canonicalSession.d.ts +12 -6
  32. package/dist/canonical/canonicalSession.js +129 -141
  33. package/dist/canonical/canonicalTrackedBatchFidelity.js +3 -3
  34. package/dist/compare/inline-atoms.d.ts +35 -41
  35. package/dist/compare/inline-atoms.js +2 -465
  36. package/dist/compare/section-boundary-properties.d.ts +3 -30
  37. package/dist/compare/section-boundary-properties.js +4 -72
  38. package/dist/controller/autocompleteSuggestion.js +1 -1
  39. package/dist/controller/canonicalClipboard.js +45 -44
  40. package/dist/controller/canonicalComposition.d.ts +1 -1
  41. package/dist/controller/canonicalComposition.js +6 -6
  42. package/dist/controller/canonicalInput.d.ts +4 -2
  43. package/dist/controller/canonicalInput.js +34 -17
  44. package/dist/controller/canonicalPresence.js +1 -1
  45. package/dist/controller/canonicalRoomAuthority.js +1 -1
  46. package/dist/controller/canonicalRoomWire.js +1 -1
  47. package/dist/controller/canonicalStoryEditor.d.ts +2 -3
  48. package/dist/controller/canonicalStoryEditor.js +43 -25
  49. package/dist/controller/canonicalStructure.d.ts +19 -2
  50. package/dist/controller/canonicalStructure.js +140 -43
  51. package/dist/controller/canonicalTabRoom.js +1 -1
  52. package/dist/controller/canonicalYjsRoom.js +1 -1
  53. package/dist/controller/deferredDisplayListPainter.d.ts +8 -0
  54. package/dist/controller/deferredDisplayListPainter.js +34 -0
  55. package/dist/controller/folioEditor.js +4 -2
  56. package/dist/controller/headerFooterEditorManager.js +3 -17
  57. package/dist/controller/hiddenEditorApi.d.ts +24 -3
  58. package/dist/controller/hiddenEditorApi.js +4 -17
  59. package/dist/controller/hiddenEditorManager.js +136 -197
  60. package/dist/controller/layoutPipeline.d.ts +4 -86
  61. package/dist/controller/layoutPipeline.js +8 -727
  62. package/dist/controller/layoutPipelineExecution.d.ts +94 -0
  63. package/dist/controller/layoutPipelineExecution.js +730 -0
  64. package/dist/controller/noteEditorManager.js +3 -16
  65. package/dist/document-operations.js +1 -1
  66. package/dist/docx/blockContentParser.d.ts +1 -3
  67. package/dist/docx/blockContentParser.js +3 -10
  68. package/dist/docx/canonicalSave.d.ts +4 -3
  69. package/dist/docx/canonicalSave.js +4 -3
  70. package/dist/docx/canonicalSessionInput.js +1 -1
  71. package/dist/docx/containerChildren.js +1 -1
  72. package/dist/docx/createDocx.d.ts +4 -0
  73. package/dist/docx/createDocx.js +6 -0
  74. package/dist/docx/listMarkerComputation.d.ts +24 -17
  75. package/dist/docx/listMarkerComputation.js +31 -60
  76. package/dist/docx/listModelNormalization.js +8 -15
  77. package/dist/docx/modelValidation.js +1 -1
  78. package/dist/docx/parser.js +1 -1
  79. package/dist/docx/reviewResolutionProvenance.js +1 -1
  80. package/dist/docx/rezip.d.ts +16 -3
  81. package/dist/docx/rezip.js +36 -10
  82. package/dist/docx/selectiveSave.js +13 -5
  83. package/dist/docx/serializer/borderSerializer.js +1 -1
  84. package/dist/docx/serializer/commentSerializer.js +1 -1
  85. package/dist/docx/serializer/documentSerializer.js +1 -1
  86. package/dist/docx/serializer/paragraphSerializer.js +3 -3
  87. package/dist/docx/serializer/runSerializer.js +1 -1
  88. package/dist/docx/serializer/sectionPropertiesSerializer.js +1 -1
  89. package/dist/docx/serializer/tableSerializer.js +1 -1
  90. package/dist/docx/serializer/textFormattingSerializer.js +1 -1
  91. package/dist/docx/serializer/trackedChangeAttributes.js +1 -1
  92. package/dist/docx/server/templatePlan/apply.js +107 -14
  93. package/dist/docx/server/templatePlan/blocks.d.ts +16 -1
  94. package/dist/docx/server/templatePlan/blocks.js +93 -7
  95. package/dist/docx/server/templatePlan/builders.d.ts +37 -2
  96. package/dist/docx/server/templatePlan/builders.js +196 -5
  97. package/dist/docx/server/templatePlan/emit.d.ts +2 -0
  98. package/dist/docx/server/templatePlan/emit.js +2 -0
  99. package/dist/docx/server/templatePlan/inspect.js +81 -61
  100. package/dist/docx/server/templatePlan/labels.d.ts +16 -2
  101. package/dist/docx/server/templatePlan/labels.js +131 -20
  102. package/dist/docx/server/templatePlan/plan.d.ts +15 -1
  103. package/dist/docx/server/templatePlan/plan.js +62 -13
  104. package/dist/docx/server/templatePlan/planShape.js +52 -4
  105. package/dist/docx/server/templatePlan/story.d.ts +8 -0
  106. package/dist/docx/server/templatePlan/story.js +11 -1
  107. package/dist/docx/server/templatePlan/types.d.ts +176 -18
  108. package/dist/docx/server/templatePlan/types.js +1 -0
  109. package/dist/docx/server/templatePlan/verify.js +60 -11
  110. package/dist/docx/server/templatePlan/xml.d.ts +5 -0
  111. package/dist/docx/server/templatePlan/xml.js +7 -1
  112. package/dist/docx/storyBlockReplay.js +1 -1
  113. package/dist/docx/xmlParser.js +1 -1
  114. package/dist/i18n/loadMessages.d.ts +5 -0
  115. package/dist/i18n/loadMessages.js +46 -0
  116. package/dist/i18n/locales.d.ts +3 -0
  117. package/dist/i18n/locales.js +17 -0
  118. package/dist/i18n/messages/ar.gen.js +1 -0
  119. package/dist/i18n/messages/catalogs.gen.d.ts +17 -0
  120. package/dist/i18n/messages/catalogs.gen.js +17 -0
  121. package/dist/i18n/messages/cs.gen.js +1 -0
  122. package/dist/i18n/messages/de.gen.js +1 -0
  123. package/dist/i18n/messages/en.gen.js +1 -0
  124. package/dist/i18n/messages/es.gen.js +1 -0
  125. package/dist/i18n/messages/et.gen.js +1 -0
  126. package/dist/i18n/messages/fr.gen.js +1 -0
  127. package/dist/i18n/messages/he.gen.js +1 -0
  128. package/dist/i18n/messages/hi.gen.js +1 -0
  129. package/dist/i18n/messages/hu.gen.js +1 -0
  130. package/dist/i18n/messages/lt.gen.js +1 -0
  131. package/dist/i18n/messages/lv.gen.js +1 -0
  132. package/dist/i18n/messages/messages.gen.d.ts +1 -0
  133. package/dist/i18n/messages/pl.gen.js +1 -0
  134. package/dist/i18n/messages/pt-BR.gen.js +1 -0
  135. package/dist/i18n/messages/sk.gen.js +1 -0
  136. package/dist/i18n/messages/tr.gen.js +1 -0
  137. package/dist/i18n/messages/zh-CN.gen.js +1 -0
  138. package/dist/i18n/messages.d.ts +3 -2
  139. package/dist/i18n/messages.js +4 -16
  140. package/dist/internal/clipboardTransformPlugin.d.ts +3 -0
  141. package/dist/internal/clipboardTransformPlugin.js +13 -0
  142. package/dist/internal/extensionRuntime.d.ts +7 -0
  143. package/dist/internal/extensionRuntime.js +28 -0
  144. package/dist/internal/paragraphFormattingSerialization.js +1 -1
  145. package/dist/internal/starterKitExtensions.d.ts +20 -0
  146. package/dist/internal/starterKitExtensions.js +123 -0
  147. package/dist/internal/wholeStoryRevisionResolution.js +2 -2
  148. package/dist/layout-bridge/convert/toFlowBlocks.js +0 -12
  149. package/dist/layout-bridge/convert/topLevelConversion.js +1 -10
  150. package/dist/prosemirror/attrs/index.js +1 -1
  151. package/dist/prosemirror/canonicalSelectionRange.js +1 -1
  152. package/dist/prosemirror/canonicalTableCommands.js +1 -1
  153. package/dist/prosemirror/commands/comments.js +3 -3
  154. package/dist/prosemirror/commands/contentControls.js +1 -1
  155. package/dist/prosemirror/commands/pastePlainText.js +2 -3
  156. package/dist/prosemirror/commands/replaceRangeText.js +1 -1
  157. package/dist/prosemirror/commentRecords.js +2 -2
  158. package/dist/prosemirror/conversion/fromProseDoc.d.ts +0 -5
  159. package/dist/prosemirror/conversion/fromProseDoc.js +2 -9
  160. package/dist/prosemirror/conversion/index.d.ts +2 -2
  161. package/dist/prosemirror/conversion/index.js +2 -2
  162. package/dist/prosemirror/extensions/ExtensionManager.js +4 -22
  163. package/dist/prosemirror/extensions/StarterKit.d.ts +4 -27
  164. package/dist/prosemirror/extensions/StarterKit.js +29 -115
  165. package/dist/prosemirror/extensions/core/ParagraphExtension.js +2 -2
  166. package/dist/prosemirror/extensions/features/BaseKeymapExtension.js +1 -1
  167. package/dist/prosemirror/extensions/features/ImagePasteExtension.js +1 -1
  168. package/dist/prosemirror/extensions/features/ListExtension.js +7 -2
  169. package/dist/prosemirror/extensions/features/pastedHeadingStyles.js +1 -1
  170. package/dist/prosemirror/extensions/index.d.ts +2 -1
  171. package/dist/prosemirror/extensions/marks/FootnoteRefExtension.js +8 -3
  172. package/dist/prosemirror/extensions/nodes/TableExtension.js +7 -40
  173. package/dist/prosemirror/hyperlinkRemoval.js +7 -2
  174. package/dist/prosemirror/index.d.ts +2 -2
  175. package/dist/prosemirror/index.js +2 -2
  176. package/dist/prosemirror/keptRangeBoundaries.js +1 -1
  177. package/dist/prosemirror/listMarker.d.ts +1 -6
  178. package/dist/prosemirror/listMarker.js +4 -56
  179. package/dist/prosemirror/listNumbering.js +1 -1
  180. package/dist/prosemirror/noteReferenceOccurrences.js +1 -1
  181. package/dist/prosemirror/noteReferenceReview.d.ts +2 -56
  182. package/dist/prosemirror/noteReferenceReview.js +2 -243
  183. package/dist/prosemirror/pastedCommentCopies.js +1 -1
  184. package/dist/prosemirror/plugins/documentNumbering.js +1 -1
  185. package/dist/prosemirror/plugins/documentStyles.js +1 -1
  186. package/dist/prosemirror/replacedAnnotations.js +1 -1
  187. package/dist/prosemirror/suggestionCommands.d.ts +47 -0
  188. package/dist/prosemirror/suggestionCommands.js +31 -0
  189. package/dist/prosemirror/tableCellPaste.js +1 -1
  190. package/dist/prosemirror/tableRunIn.js +1 -1
  191. package/dist/prosemirror/textInput.js +1 -1
  192. package/dist/server.d.ts +2 -2
  193. package/dist/types/canonicalCapabilities.d.ts +42 -93
  194. package/dist/types/canonicalCapabilities.js +53 -91
  195. package/dist/types/canonicalComments.d.ts +2 -2
  196. package/dist/types/canonicalSections.d.ts +2 -2
  197. package/dist/utils/clipboard.d.ts +2 -28
  198. package/dist/utils/clipboard.js +1 -96
  199. package/dist/utils/clipboardFiles.d.ts +27 -0
  200. package/dist/utils/clipboardFiles.js +98 -0
  201. package/dist/utils/rulerControl.d.ts +1 -0
  202. package/dist/utils/rulerControl.js +2 -1
  203. package/package.json +2 -2
  204. package/dist/ai-edits/apply.d.ts +0 -94
  205. package/dist/ai-edits/apply.js +0 -6637
  206. package/dist/ai-edits/note-story-inventory.d.ts +0 -5
  207. package/dist/ai-edits/note-story-inventory.js +0 -10
  208. package/dist/ai-edits/revisionStretches.d.ts +0 -32
  209. package/dist/ai-edits/revisionStretches.js +0 -147
  210. package/dist/document-operations-apply.d.ts +0 -15
  211. package/dist/document-operations-apply.js +0 -280
  212. package/dist/prosemirror/extensions/core/HistoryExtension.d.ts +0 -12
  213. package/dist/prosemirror/extensions/core/HistoryExtension.js +0 -114
  214. package/dist/prosemirror/extensions/features/ParaIdAllocatorExtension.d.ts +0 -23
  215. package/dist/prosemirror/extensions/features/ParaIdAllocatorExtension.js +0 -270
  216. package/dist/prosemirror/extensions/features/ParagraphChangeTrackerExtension.d.ts +0 -51
  217. package/dist/prosemirror/extensions/features/ParagraphChangeTrackerExtension.js +0 -411
  218. package/dist/prosemirror/plugins/suggestionMode.d.ts +0 -92
  219. package/dist/prosemirror/plugins/suggestionMode.js +0 -1515
  220. package/dist/prosemirror/tablePositionedBookmarks.d.ts +0 -8
  221. package/dist/prosemirror/tablePositionedBookmarks.js +0 -80
@@ -100,9 +100,9 @@ const checkCanonicalTrackedBatchFidelity = ({ createdCommentIds, retiredCommentI
100
100
  if (verdict !== void 0) return verdict;
101
101
  }
102
102
  }
103
- const rejectedBefore = review.reject("before");
104
- const rejectedAfter = review.reject("after");
105
- if (rejectedBefore === void 0 || rejectedAfter === void 0) return "Reject All could not resolve the tracked result.";
103
+ const rejected = review.reject();
104
+ if (rejected === null) return "Reject All could not resolve the tracked result.";
105
+ const { original: rejectedBefore, edited: rejectedAfter } = rejected;
106
106
  if (rejectedBefore !== rejectedAfter && !sameOriginalBlocks(rejectedBefore, rejectedAfter, createdCommentIds)) return "Reject All would change the original content or formatting.";
107
107
  const accepted = review.accept();
108
108
  if (accepted === null) return "Accept All could not resolve the tracked result.";
@@ -1,49 +1,43 @@
1
- import { FolioRevisionStamp } from "../ai-edits/applyTypes.js";
2
- import { NoteReferenceLabels } from "../ai-edits/note-references.js";
3
- import { FolioAIEditSnapshot } from "../ai-edits/types.js";
4
- import { MISSING_NOTE_DETAIL } from "./inline-atom-alignment.js";
5
- import { EditorState, Transaction } from "prosemirror-state";
1
+ import { BuildCleanBlockTextOptions } from "../ai-edits/clean-text.js";
2
+ import { NoteReferenceIdentity, NoteReferenceLabels } from "../ai-edits/note-references.js";
3
+ import { AlignableAtom, MISSING_NOTE_DETAIL } from "./inline-atom-alignment.js";
6
4
  import { Node } from "prosemirror-model";
7
- export type MatchInlineAtomsOptions = {
8
- state: EditorState;
9
- targetSnapshot: FolioAIEditSnapshot;
10
- revisionStamp: FolioRevisionStamp;
11
- /** First id reserved for this comparison, before planned operations allocated ids. */
12
- originalRevisionIdSeed: number;
13
- author: string;
14
- maxRanges: number;
15
- /**
16
- * The markers the comparison's operations wrote note references as.
17
- * Defaults to the target story's own numbering.
18
- */
19
- noteReferenceLabels?: NoteReferenceLabels;
20
- /**
21
- * The notes the base package defines, as `footnote:<id>` / `endnote:<id>`.
22
- * A reference the comparison writes may only name one of them. Defaults to
23
- * the notes the story already references.
24
- */
25
- baseNotes?: ReadonlySet<string>;
5
+ export type TextBlock = {
6
+ node: Node;
7
+ from: number;
26
8
  };
27
- export type MatchInlineAtomsResult = {
28
- status: "matched";
29
- transaction: Transaction;
30
- nextRevisionId: number;
31
- changedTargetBlockIds: readonly string[];
32
- rangeCount: number;
33
- } | {
34
- status: "unalignable";
35
- /** Why, when the cause is more specific than an alignment failure. */
36
- detail?: string;
37
- } | {
38
- status: "budget-exceeded";
9
+ export type InlineAtom = AlignableAtom & {
10
+ node: Node;
11
+ from: number;
12
+ };
13
+ /** A footnote or endnote reference read as its marker, over `length` clean-text characters. */
14
+ type NoteReferenceAt = NoteReferenceIdentity & {
15
+ offset: number;
16
+ length: number;
17
+ from: number;
18
+ };
19
+ export type AtomBlock = TextBlock & {
20
+ blockType: string;
21
+ cleanText: string;
22
+ offsets: readonly number[];
23
+ supported: readonly InlineAtom[];
24
+ unsupportedTopology: readonly {
25
+ offset: number;
26
+ key: string;
27
+ }[];
28
+ /** Empty unless the block was read with note reference labels. */
29
+ noteReferences: readonly NoteReferenceAt[];
39
30
  };
40
31
  /**
41
- * Restore field, image, and page-break atoms omitted by text-only comparison
42
- * operations, and the note references whose markers they wrote as text.
43
- * Positions come from the legacy review resolver's mapping, never a guessed
44
- * paragraph correspondence: paragraph-mark deletions can merge paragraphs.
32
+ * How a block's note references read. `"sourceText"` is the editor's own text
33
+ * for a reference (its package id); labels read it as the marker (`[^1]`) the
34
+ * comparison planned in, which is the text its operations write for one.
45
35
  */
46
- export declare const matchInlineAtoms: ({ state, targetSnapshot, revisionStamp, originalRevisionIdSeed, author, maxRanges, noteReferenceLabels, baseNotes }: MatchInlineAtomsOptions) => MatchInlineAtomsResult;
36
+ export type NoteReferenceReading = NoteReferenceLabels | "sourceText";
37
+ export declare const isSupportedAtom: (node: Node) => boolean;
38
+ export declare const hasSupportedAtom: (doc: Node) => boolean;
39
+ export declare const textBlocksOf: (doc: Node) => TextBlock[];
40
+ export declare const atomBlocksOf: (doc: Node, fieldResults: BuildCleanBlockTextOptions["fieldResults"], noteReferences?: NoteReferenceReading) => AtomBlock[];
47
41
  /** Compare supported inline atom identity after accept or reject projection. */
48
42
  export declare const sameInlineAtoms: (leftDocument: Node, rightDocument: Node) => boolean;
49
43
  export { MISSING_NOTE_DETAIL };
@@ -1,25 +1,10 @@
1
1
  import { buildCleanBlockText } from "../ai-edits/clean-text.js";
2
- import { noteReferenceOf } from "../ai-edits/note-references.js";
3
- import { collectNoteReferenceLabels, sourceDocumentOf } from "../ai-edits/snapshot.js";
4
2
  import { drawingXmlWithoutIdentity } from "../docx/drawingIdNormalization.js";
5
- import { addTrackedDeletionMark } from "../prosemirror/addTrackedDeletionMark.js";
6
- import { resolveAllChangesInHeadlessStateWithMapping } from "../prosemirror/commands/comments.js";
7
- import { mintNoteReferenceOccurrenceId } from "../prosemirror/noteReferenceOccurrences.js";
8
3
  import { runFormattingInlineAtomResultText } from "../prosemirror/runFormattingInlineCarriers.js";
9
4
  import { canonicalJson } from "../utils/canonicalJson.js";
10
- import { MISSING_NOTE_DETAIL, atomActionRangeCount, atomActionTieRank, fieldResultOwnershipOf, insertionAnchors, matchingAtoms, ownsFieldResult, sameBlockTopology, sameSupportedAtoms } from "./inline-atom-alignment.js";
5
+ import { MISSING_NOTE_DETAIL, sameSupportedAtoms } from "./inline-atom-alignment.js";
11
6
  import { prepareTargetInlineAtom } from "./inline-atom-resources.js";
12
7
  // src/compare/inline-atoms.ts
13
- const actionRangeCount = (action) => {
14
- switch (action.kind) {
15
- case "replace-text": return atomActionRangeCount(action.textDisposition);
16
- case "replace-atom": return atomActionRangeCount(action.atomDisposition);
17
- case "insert":
18
- case "delete":
19
- case "note-reference": return atomActionRangeCount(null);
20
- default: return action;
21
- }
22
- };
23
8
  const isSupportedAtom = (node) => node.isInline && node.isAtom && (node.type.name === "field" || node.type.name === "image" || node.type.name === "pageBreakRun");
24
9
  const hasSupportedAtom = (doc) => {
25
10
  let found = false;
@@ -154,455 +139,7 @@ const atomBlockOf = ({ node, from }, fieldResults, noteReferences) => {
154
139
  noteReferences: references
155
140
  };
156
141
  };
157
- const targetBlockIdLookup = (snapshot) => {
158
- const anchors = Object.values(snapshot.anchors).toSorted((left, right) => left.from - right.from || right.to - left.to);
159
- return (position) => {
160
- let low = 0;
161
- let high = anchors.length;
162
- while (low < high) {
163
- const middle = Math.floor((low + high) / 2);
164
- if ((anchors[middle]?.from ?? Number.POSITIVE_INFINITY) <= position) low = middle + 1;
165
- else high = middle;
166
- }
167
- let owner;
168
- for (let index = low - 1; index >= 0; index--) {
169
- const anchor = anchors[index];
170
- if (!anchor || anchor.to <= position) continue;
171
- if (!owner || anchor.to - anchor.from < owner.to - owner.from) owner = anchor;
172
- }
173
- return owner?.id;
174
- };
175
- };
176
142
  const atomBlocksOf = (doc, fieldResults, noteReferences = "sourceText") => textBlocksOf(doc).map((block) => atomBlockOf(block, fieldResults, noteReferences));
177
- const noteReferenceKey = ({ noteType, noteId }) => `${noteType}:${noteId}`;
178
- /** Every note a story references, tracked-deleted references included. */
179
- const referencedNotesOf = (doc) => {
180
- const notes = /* @__PURE__ */ new Set();
181
- doc.descendants((node) => {
182
- const reference = noteReferenceOf(node);
183
- if (reference) notes.add(noteReferenceKey(reference));
184
- return !node.isText;
185
- });
186
- return notes;
187
- };
188
- const hasNoteReference = (doc) => {
189
- let found = false;
190
- doc.descendants((node) => {
191
- if (found) return false;
192
- if (noteReferenceOf(node)) found = true;
193
- return !node.isText;
194
- });
195
- return found;
196
- };
197
- /**
198
- * Whether this comparison inserted text that may stand for a note reference.
199
- * Operations carry clean text, where a reference reads as its marker, so an
200
- * operation that brings a reference writes the marker as text.
201
- */
202
- const wroteNoteReferenceMarker = (doc, originalRevisionIdSeed) => {
203
- let found = false;
204
- doc.descendants((node) => {
205
- if (found) return false;
206
- if (!node.isText) return true;
207
- if (!node.text?.includes("[^") || noteReferenceOf(node)) return false;
208
- const revisionId = node.marks.find(({ type }) => type.name === "insertion")?.attrs["revisionId"];
209
- if (typeof revisionId === "number" && revisionId >= originalRevisionIdSeed) found = true;
210
- return false;
211
- });
212
- return found;
213
- };
214
- const mappedSourcePosition = ({ mapping, position }) => {
215
- const inverse = mapping.invert();
216
- const right = inverse.mapResult(position, 1);
217
- if (!right.deleted) return right.pos;
218
- const left = inverse.mapResult(position, -1);
219
- return left.deleted ? null : left.pos;
220
- };
221
- const sourceTextBlockIds = (source) => {
222
- const blocks = /* @__PURE__ */ new Map();
223
- for (const block of textBlocksOf(source)) {
224
- const paraId = block.node.attrs["paraId"];
225
- if (typeof paraId !== "string" || paraId.length === 0) continue;
226
- blocks.set(paraId, blocks.has(paraId) ? null : block);
227
- }
228
- return blocks;
229
- };
230
- const sameParagraphSourcePosition = ({ sourceBlocks, reviewed, offset, fieldResults, noteReferences }) => {
231
- const paraId = reviewed.node.attrs["paraId"];
232
- if (typeof paraId !== "string" || paraId.length === 0) return null;
233
- const source = sourceBlocks.get(paraId);
234
- if (!source || source.node.type !== reviewed.node.type) return null;
235
- const clean = buildCleanBlockText(source.node, source.from, {
236
- fieldResults,
237
- noteReferences
238
- });
239
- return clean.text === reviewed.cleanText ? clean.offsets[offset] ?? null : null;
240
- };
241
- const textRangeDisposition = ({ doc, from, to, expectedText, originalRevisionIdSeed }) => {
242
- if (from >= to || doc.textBetween(from, to) !== expectedText) return null;
243
- let disposition;
244
- let covered = 0;
245
- let invalid = false;
246
- doc.nodesBetween(from, to, (node, position) => {
247
- if (!node.isText) {
248
- if (position > from && position < to) invalid = true;
249
- return !invalid;
250
- }
251
- const overlapFrom = Math.max(from, position);
252
- const overlapTo = Math.min(to, position + node.nodeSize);
253
- if (overlapFrom >= overlapTo) return false;
254
- covered += overlapTo - overlapFrom;
255
- if (node.marks.some(({ type }) => type.name === "deletion")) {
256
- invalid = true;
257
- return false;
258
- }
259
- const insertion = node.marks.find(({ type }) => type.name === "insertion");
260
- let current = "retained";
261
- if (insertion) {
262
- const revisionId = insertion.attrs["revisionId"];
263
- if (typeof revisionId !== "number" || revisionId < originalRevisionIdSeed) {
264
- invalid = true;
265
- return false;
266
- }
267
- current = "inserted";
268
- }
269
- if (disposition !== void 0 && disposition !== current) {
270
- invalid = true;
271
- return false;
272
- }
273
- disposition = current;
274
- return false;
275
- });
276
- return !invalid && covered === to - from ? disposition ?? null : null;
277
- };
278
- const atomDisposition = ({ atom, originalRevisionIdSeed }) => {
279
- if (atom.node.marks.some(({ type }) => type.name === "deletion")) return null;
280
- const insertion = atom.node.marks.find(({ type }) => type.name === "insertion");
281
- if (!insertion) return "retained";
282
- const revisionId = insertion.attrs["revisionId"];
283
- return typeof revisionId === "number" && revisionId >= originalRevisionIdSeed ? "inserted" : null;
284
- };
285
- /**
286
- * Restore field, image, and page-break atoms omitted by text-only comparison
287
- * operations, and the note references whose markers they wrote as text.
288
- * Positions come from the legacy review resolver's mapping, never a guessed
289
- * paragraph correspondence: paragraph-mark deletions can merge paragraphs.
290
- */
291
- const matchInlineAtoms = ({ state, targetSnapshot, revisionStamp, originalRevisionIdSeed, author, maxRanges, noteReferenceLabels, baseNotes }) => {
292
- if (!Number.isSafeInteger(maxRanges) || maxRanges < 0) return { status: "budget-exceeded" };
293
- const targetDocument = sourceDocumentOf(targetSnapshot);
294
- const restoresNoteReferences = wroteNoteReferenceMarker(state.doc, originalRevisionIdSeed) && hasNoteReference(targetDocument);
295
- const noteReferences = restoresNoteReferences ? noteReferenceLabels ?? collectNoteReferenceLabels(targetDocument) : "sourceText";
296
- if (!restoresNoteReferences && !hasSupportedAtom(state.doc) && !hasSupportedAtom(targetDocument)) return {
297
- status: "matched",
298
- transaction: state.tr,
299
- nextRevisionId: revisionStamp.idSeed,
300
- changedTargetBlockIds: [],
301
- rangeCount: 0
302
- };
303
- const reviewed = resolveAllChangesInHeadlessStateWithMapping(state, "accept");
304
- const targetBlockIdAt = targetBlockIdLookup(targetSnapshot);
305
- let sourceBlocks;
306
- const liveBlocks = atomBlocksOf(reviewed.state.doc, "text", noteReferences);
307
- const targetBlocks = atomBlocksOf(targetDocument, "text", noteReferences);
308
- if (liveBlocks.length !== targetBlocks.length) return { status: "unalignable" };
309
- const actions = [];
310
- let rangeCount = 0;
311
- const planAction = (action) => {
312
- const nextRangeCount = rangeCount + actionRangeCount(action);
313
- if (nextRangeCount > maxRanges) return false;
314
- rangeCount = nextRangeCount;
315
- actions.push(action);
316
- return true;
317
- };
318
- let referencedNotes;
319
- let omittedLiveBlocks;
320
- let omittedTargetBlocks;
321
- for (const [index, fullLive] of liveBlocks.entries()) {
322
- const fullTarget = targetBlocks[index];
323
- if (!fullTarget) return { status: "unalignable" };
324
- if ((fullLive.noteReferences.length > 0 || fullTarget.noteReferences.length > 0) && fullLive.cleanText === fullTarget.cleanText) {
325
- const kept = new Map(fullLive.noteReferences.map((reference) => [reference.offset, reference]));
326
- for (const reference of fullTarget.noteReferences) {
327
- const live = kept.get(reference.offset);
328
- if (live) {
329
- kept.delete(reference.offset);
330
- if (noteReferenceKey(live) !== noteReferenceKey(reference) || live.length !== reference.length) return { status: "unalignable" };
331
- continue;
332
- }
333
- referencedNotes ??= baseNotes ?? referencedNotesOf(state.doc);
334
- if (!referencedNotes.has(noteReferenceKey(reference))) return {
335
- status: "unalignable",
336
- detail: MISSING_NOTE_DETAIL
337
- };
338
- const node = targetDocument.nodeAt(reference.from);
339
- const reviewedFrom = fullLive.offsets[reference.offset];
340
- const reviewedLast = fullLive.offsets[reference.offset + reference.length - 1];
341
- if (!node || !noteReferenceOf(node) || reviewedFrom === void 0 || reviewedLast === void 0) return { status: "unalignable" };
342
- const from = mappedSourcePosition({
343
- mapping: reviewed.mapping,
344
- position: reviewedFrom
345
- });
346
- const to = mappedSourcePosition({
347
- mapping: reviewed.mapping,
348
- position: reviewedLast + 1
349
- });
350
- if (from === null || to === null || textRangeDisposition({
351
- doc: state.doc,
352
- from,
353
- to,
354
- expectedText: fullTarget.cleanText.slice(reference.offset, reference.offset + reference.length),
355
- originalRevisionIdSeed
356
- }) !== "inserted") return { status: "unalignable" };
357
- if (!planAction({
358
- kind: "note-reference",
359
- from,
360
- to,
361
- node,
362
- targetBlockId: targetBlockIdAt(fullTarget.from)
363
- })) return { status: "budget-exceeded" };
364
- }
365
- if (kept.size > 0) return { status: "unalignable" };
366
- }
367
- if (fullLive.supported.length === 0 && fullTarget.supported.length === 0) continue;
368
- if (sameSupportedAtoms(fullLive, fullTarget)) continue;
369
- let live = fullLive;
370
- let target = fullTarget;
371
- let fieldResults = "text";
372
- if (!sameBlockTopology(live, target)) {
373
- omittedLiveBlocks ??= atomBlocksOf(reviewed.state.doc, "omitted", noteReferences);
374
- omittedTargetBlocks ??= atomBlocksOf(targetDocument, "omitted", noteReferences);
375
- const omittedLive = omittedLiveBlocks[index];
376
- const omittedTarget = omittedTargetBlocks[index];
377
- if (!omittedLive || !omittedTarget || !sameBlockTopology(omittedLive, omittedTarget)) return { status: "unalignable" };
378
- live = omittedLive;
379
- target = omittedTarget;
380
- fieldResults = "omitted";
381
- }
382
- const matches = matchingAtoms({
383
- live: live.supported,
384
- target: target.supported
385
- });
386
- const matchedLive = new Set(matches.map(([liveIndex]) => liveIndex));
387
- const matchedTarget = new Set(matches.map(([, targetIndex]) => targetIndex));
388
- const anchors = insertionAnchors({
389
- live: live.supported,
390
- target: target.supported,
391
- matches
392
- });
393
- const liveFieldResultOwnership = fieldResultOwnershipOf(live.supported);
394
- const targetFieldResultOwnership = fieldResultOwnershipOf(target.supported);
395
- for (const [liveIndex, atom] of live.supported.entries()) {
396
- if (matchedLive.has(liveIndex)) continue;
397
- const from = mappedSourcePosition({
398
- mapping: reviewed.mapping,
399
- position: atom.from
400
- });
401
- if (from === null) return { status: "unalignable" };
402
- const { resultText } = atom;
403
- if (fieldResults === "text" && resultText && !ownsFieldResult({
404
- ownership: targetFieldResultOwnership,
405
- offset: atom.offset,
406
- resultText
407
- })) {
408
- const disposition = atomDisposition({
409
- atom,
410
- originalRevisionIdSeed
411
- });
412
- if (disposition === null) return { status: "unalignable" };
413
- if (!planAction({
414
- kind: "replace-atom",
415
- from,
416
- to: from + atom.node.nodeSize,
417
- text: resultText,
418
- atomDisposition: disposition,
419
- marks: atom.node.marks,
420
- targetBlockId: targetBlockIdAt(target.from)
421
- })) return { status: "budget-exceeded" };
422
- continue;
423
- }
424
- if (!planAction({
425
- kind: "delete",
426
- from,
427
- to: from + atom.node.nodeSize,
428
- targetBlockId: targetBlockIdAt(target.from)
429
- })) return { status: "budget-exceeded" };
430
- }
431
- for (const [targetIndex, atom] of target.supported.entries()) {
432
- if (matchedTarget.has(targetIndex)) continue;
433
- const anchor = anchors.get(targetIndex);
434
- const anchorAtom = anchor === void 0 ? void 0 : live.supported[anchor.liveIndex];
435
- let position;
436
- if (anchor === void 0 || anchorAtom === void 0) position = live.offsets[atom.offset];
437
- else position = anchor.side === "before" ? anchorAtom.from : anchorAtom.from + anchorAtom.node.nodeSize;
438
- if (position === void 0) return { status: "unalignable" };
439
- const from = mappedSourcePosition({
440
- mapping: reviewed.mapping,
441
- position
442
- }) ?? sameParagraphSourcePosition({
443
- sourceBlocks: sourceBlocks ??= sourceTextBlockIds(state.doc),
444
- reviewed: live,
445
- offset: atom.offset,
446
- fieldResults,
447
- noteReferences
448
- });
449
- if (from === null) return { status: "unalignable" };
450
- const { resultText } = atom;
451
- if (fieldResults === "text" && resultText && !ownsFieldResult({
452
- ownership: liveFieldResultOwnership,
453
- offset: atom.offset,
454
- resultText
455
- })) {
456
- const reviewedLast = live.offsets[atom.offset + resultText.length - 1];
457
- const to = reviewedLast === void 0 ? null : mappedSourcePosition({
458
- mapping: reviewed.mapping,
459
- position: reviewedLast + 1
460
- });
461
- if (to === null) return { status: "unalignable" };
462
- const textDisposition = textRangeDisposition({
463
- doc: state.doc,
464
- from,
465
- to,
466
- expectedText: resultText,
467
- originalRevisionIdSeed
468
- });
469
- if (textDisposition === null) return { status: "unalignable" };
470
- if (!planAction({
471
- kind: "replace-text",
472
- from,
473
- to,
474
- node: atom.node,
475
- textDisposition,
476
- targetBlockId: targetBlockIdAt(target.from)
477
- })) return { status: "budget-exceeded" };
478
- continue;
479
- }
480
- if (!planAction({
481
- kind: "insert",
482
- from,
483
- node: atom.node,
484
- targetBlockId: targetBlockIdAt(target.from)
485
- })) return { status: "budget-exceeded" };
486
- }
487
- }
488
- const insertionType = state.schema.marks["insertion"];
489
- const deletionType = state.schema.marks["deletion"];
490
- if (!insertionType || !deletionType) return { status: "unalignable" };
491
- const transaction = state.tr;
492
- let nextRevisionId = revisionStamp.idSeed;
493
- const changedTargetBlockIds = /* @__PURE__ */ new Set();
494
- const ordered = actions.toSorted((left, right) => right.from - left.from || atomActionTieRank(left.kind) - atomActionTieRank(right.kind));
495
- for (const action of ordered) {
496
- if (action.kind === "insert") {
497
- const at = transaction.mapping.map(action.from, 1);
498
- const marked = action.node.mark(insertionType.create({
499
- revisionId: nextRevisionId++,
500
- author,
501
- date: revisionStamp.date
502
- }).addToSet(action.node.marks));
503
- transaction.insert(at, marked);
504
- if (action.targetBlockId) changedTargetBlockIds.add(action.targetBlockId);
505
- continue;
506
- }
507
- if (action.kind === "replace-text") {
508
- const from = transaction.mapping.map(action.from, 1);
509
- const to = transaction.mapping.map(action.to, -1);
510
- if (from >= to) return { status: "unalignable" };
511
- if (action.textDisposition === "inserted") transaction.delete(from, to);
512
- else addTrackedDeletionMark({
513
- insertionPolicy: "preserve-pending",
514
- tr: transaction,
515
- from,
516
- to,
517
- mark: deletionType.create({
518
- revisionId: nextRevisionId++,
519
- author,
520
- date: revisionStamp.date
521
- })
522
- });
523
- const at = transaction.mapping.map(action.from, 1);
524
- transaction.insert(at, action.node.mark(insertionType.create({
525
- revisionId: nextRevisionId++,
526
- author,
527
- date: revisionStamp.date
528
- }).addToSet(action.node.marks)));
529
- if (action.targetBlockId) changedTargetBlockIds.add(action.targetBlockId);
530
- continue;
531
- }
532
- if (action.kind === "note-reference") {
533
- const from = transaction.mapping.map(action.from, 1);
534
- const to = transaction.mapping.map(action.to, -1);
535
- const written = transaction.doc.nodeAt(from);
536
- const noteMark = action.node.marks.find(({ type }) => type.name === "footnoteRef");
537
- if (from >= to || !written?.isText || !noteMark) return { status: "unalignable" };
538
- const restoredNoteMark = noteMark.type.create({
539
- ...noteMark.attrs,
540
- occurrenceId: mintNoteReferenceOccurrenceId()
541
- });
542
- transaction.replaceWith(from, to, state.schema.text(action.node.text ?? "", restoredNoteMark.addToSet(written.marks)));
543
- if (action.targetBlockId) changedTargetBlockIds.add(action.targetBlockId);
544
- continue;
545
- }
546
- if (action.kind === "replace-atom") {
547
- const from = transaction.mapping.map(action.from, 1);
548
- const to = transaction.mapping.map(action.to, -1);
549
- const node = transaction.doc.nodeAt(from);
550
- if (!node || node.nodeSize !== to - from || node.type.name !== "field") return { status: "unalignable" };
551
- if (action.atomDisposition === "inserted") transaction.delete(from, to);
552
- else addTrackedDeletionMark({
553
- insertionPolicy: "preserve-pending",
554
- tr: transaction,
555
- from,
556
- to,
557
- mark: deletionType.create({
558
- revisionId: nextRevisionId++,
559
- author,
560
- date: revisionStamp.date
561
- })
562
- });
563
- const at = transaction.mapping.map(action.from, 1);
564
- transaction.insert(at, state.schema.text(action.text, insertionType.create({
565
- revisionId: nextRevisionId++,
566
- author,
567
- date: revisionStamp.date
568
- }).addToSet(action.marks)));
569
- if (action.targetBlockId) changedTargetBlockIds.add(action.targetBlockId);
570
- continue;
571
- }
572
- const from = transaction.mapping.map(action.from, 1);
573
- const to = transaction.mapping.map(action.to, -1);
574
- const node = transaction.doc.nodeAt(from);
575
- if (!node || node.nodeSize !== to - from || !isSupportedAtom(node)) return { status: "unalignable" };
576
- const insertion = node.marks.find(({ type }) => type === insertionType);
577
- if (insertion) {
578
- const revisionId = insertion.attrs["revisionId"];
579
- if (typeof revisionId !== "number" || revisionId < originalRevisionIdSeed) return { status: "unalignable" };
580
- transaction.delete(from, to);
581
- if (action.targetBlockId) changedTargetBlockIds.add(action.targetBlockId);
582
- continue;
583
- }
584
- if (node.marks.some(({ type }) => type === deletionType)) return { status: "unalignable" };
585
- addTrackedDeletionMark({
586
- insertionPolicy: "preserve-pending",
587
- tr: transaction,
588
- from,
589
- to,
590
- mark: deletionType.create({
591
- revisionId: nextRevisionId++,
592
- author,
593
- date: revisionStamp.date
594
- })
595
- });
596
- if (action.targetBlockId) changedTargetBlockIds.add(action.targetBlockId);
597
- }
598
- return {
599
- status: "matched",
600
- transaction,
601
- nextRevisionId,
602
- changedTargetBlockIds: [...changedTargetBlockIds],
603
- rangeCount
604
- };
605
- };
606
143
  /** Compare supported inline atom identity after accept or reject projection. */
607
144
  const sameInlineAtoms = (leftDocument, rightDocument) => {
608
145
  if (!hasSupportedAtom(leftDocument) && !hasSupportedAtom(rightDocument)) return true;
@@ -615,4 +152,4 @@ const sameInlineAtoms = (leftDocument, rightDocument) => {
615
152
  return right !== void 0 && left.cleanText === right.cleanText && sameSupportedAtoms(left, right);
616
153
  });
617
154
  };
618
- export { MISSING_NOTE_DETAIL, matchInlineAtoms, sameInlineAtoms };
155
+ export { MISSING_NOTE_DETAIL, atomBlocksOf, hasSupportedAtom, isSupportedAtom, sameInlineAtoms, textBlocksOf };
@@ -36,30 +36,6 @@ type MapTargetProperties = (args: {
36
36
  current: document_d_exports.SectionProperties | undefined;
37
37
  target: document_d_exports.SectionProperties;
38
38
  }) => MappedSectionBoundaryProperties | null;
39
- export type StageSectionBoundaryPropertiesResult = {
40
- status: "matched";
41
- transaction: import("prosemirror-state").Transaction;
42
- nextRevisionId: number;
43
- rangeCount: number;
44
- } | {
45
- status: "unalignable";
46
- detail: string;
47
- } | {
48
- status: "budget-exceeded";
49
- };
50
- /** Stage section deltas after document operations, against the accepted projection. */
51
- export declare const stageSectionBoundaryProperties: ({ state, target, originalRevisionIdSeed, revisionStamp, author, maxRanges, mapTargetProperties }: {
52
- state: import("prosemirror-state").EditorState;
53
- target: Node;
54
- originalRevisionIdSeed: number;
55
- revisionStamp: {
56
- idSeed: number;
57
- date: string;
58
- };
59
- author: string;
60
- maxRanges: number;
61
- mapTargetProperties: MapTargetProperties;
62
- }) => StageSectionBoundaryPropertiesResult;
63
39
  type StageSectionBoundaryOperationsResult = {
64
40
  status: "matched";
65
41
  operations: readonly SetSectionEndpointOp[];
@@ -71,12 +47,9 @@ type StageSectionBoundaryOperationsResult = {
71
47
  status: "budget-exceeded";
72
48
  };
73
49
  /**
74
- * Stage section deltas on a document model: planned as
75
- * {@link stageSectionBoundaryProperties} plans them, on `accepted`, the
76
- * accepted projection of `document`, with each boundary paragraph found in
77
- * `document` by its paragraph id. One section-endpoint operation per
78
- * endpoint, revision ids given from the last endpoint back as the
79
- * transaction gives them.
50
+ * Plan section deltas on the accepted projection of `document` and find each
51
+ * boundary in the authored document by paragraph id. Emit one operation per
52
+ * endpoint, assigning revision ids from the last endpoint back.
80
53
  */
81
54
  export declare const stageSectionBoundaryOperations: ({ document, accepted, target, originalRevisionIdSeed, revisionStamp, author, maxRanges, mapTargetProperties }: {
82
55
  document: document_d_exports.Document;