@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
@@ -1,270 +0,0 @@
1
- import { PROSE_PARAGRAPH_SOURCE_TOKEN_ATTR, getExplicitParagraphPropertySourceTransfers, getProseDocumentParagraphPropertySourceContract, getProseParagraphPropertySourceToken, paragraphPropertySourceTokenMatchesContract, setProseParagraphMarkupWithPropertySource } from "../../../docx/paragraphPropertySource.js";
2
- import { deterministicHexId, generateHexId } from "../../../utils/hexId.js";
3
- import { deterministicParaIdSeedKey, isUsableParaId, paraIdAllocatorKey, visitAuthoredParagraphs } from "../../paraIds.js";
4
- import { ignoreTrackedChanges } from "../../paragraphChangeTrackerMeta.js";
5
- import { sweepPositions } from "../../positionSweep.js";
6
- import { createExtension } from "../create.js";
7
- import { panic } from "better-result";
8
- import { Plugin, Transaction } from "prosemirror-state";
9
- // src/prosemirror/extensions/features/ParaIdAllocatorExtension.ts
10
- /**
11
- * ParaIdAllocator — assigns a stable `w14:paraId` to every paragraph.
12
- *
13
- * Lifted from
14
- * https://github.com/eigenpal/docx-editor/blob/main/packages/core/src/prosemirror/extensions/features/ParaIdAllocatorExtension.ts
15
- * (Apache-2.0). Adapted to folio's `createExtension` + import style;
16
- * keep behaviour in sync upstream. folio divergences: duplicate
17
- * resolution maps the original paragraph's position through the
18
- * transaction (see below), and the appended allocation transaction is
19
- * excluded from paragraph change tracking like `ensureParaIdsInState`.
20
- *
21
- * Why: AI tooling, chat citation chips, and the change tracker all
22
- * anchor on `paraId`. A paragraph with `paraId: null` is invisible
23
- * to those surfaces; a duplicated paraId (the second half of an
24
- * Enter-split, or content pasted from another doc) silently desyncs
25
- * their anchors. This plugin closes both gaps by allocating fresh
26
- * ids in an `appendTransaction` hook after every doc-changed step.
27
- *
28
- * Id lifecycle rules (locked by the co-located tests):
29
- * - Load: a missing/empty/reserved-zero or duplicate id gets one derived
30
- * from the paragraph's text and ordinal (`ensureParaIdsInDoc`), the same
31
- * id the headless reviewer derives for the same document.
32
- * - Missing/empty/reserved-zero id after an edit: a fresh random id is minted.
33
- * - Split: the half holding the paragraph's original start position
34
- * keeps the id; the other half gets a fresh one. Anchors inside the
35
- * content stay resolvable on the half that carries them.
36
- * - Merge/join: the surviving paragraph keeps its id; the absorbed id
37
- * dangles by design (consumers degrade to snapshot fallback).
38
- * - Paste/duplicate carrying an id already in the doc: the paragraph
39
- * whose mapped pre-transaction position matches keeps it — pasting a
40
- * copy above its source no longer steals the source's id. When no
41
- * occurrence maps back (e.g. both are new), the first in document
42
- * order keeps it.
43
- * - Paste of an id unknown to this doc (cross-doc paste, cut-then-
44
- * paste move): the id is kept, so a moved paragraph stays anchored.
45
- * - Undo/redo: allocation applies with `addToHistory: false`, and
46
- * ProseMirror remaps it into the originating history event. Redoing a
47
- * split therefore restores the same allocated id instead of minting
48
- * another one.
49
- */
50
- const deterministicSeedOf = (transaction) => {
51
- const seed = transaction.getMeta(deterministicParaIdSeedKey);
52
- if (typeof seed === "string") return seed;
53
- const root = transaction.getMeta("appendedTransaction");
54
- if (!(root instanceof Transaction)) return void 0;
55
- const rootSeed = root.getMeta(deterministicParaIdSeedKey);
56
- return typeof rootSeed === "string" ? rootSeed : void 0;
57
- };
58
- /**
59
- * A fresh id not already in `taken`: derived from `seed` and the paragraph's
60
- * position when the caller asked for reproducibility, random otherwise. Both
61
- * loops terminate — the id space is 2^31 wide and `taken` holds at most one
62
- * entry per paragraph.
63
- */
64
- const mintParaId = (taken, seed, pos) => {
65
- if (seed === null) {
66
- let id = generateHexId();
67
- while (taken.has(id)) id = generateHexId();
68
- return id;
69
- }
70
- let id = deterministicHexId(`${seed}:${String(pos)}`);
71
- for (let salt = 1; taken.has(id); salt++) id = deterministicHexId(`${seed}:${String(pos)}:${String(salt)}`);
72
- return id;
73
- };
74
- const collectParagraphPropertySourceSeed = (doc) => {
75
- const contract = getProseDocumentParagraphPropertySourceContract(doc);
76
- if (!contract) return {
77
- contract: null,
78
- tokens: /* @__PURE__ */ new Set()
79
- };
80
- const counts = /* @__PURE__ */ new Map();
81
- visitAuthoredParagraphs(doc, (node) => {
82
- const token = getProseParagraphPropertySourceToken(node);
83
- if (paragraphPropertySourceTokenMatchesContract(token, contract)) counts.set(token, (counts.get(token) ?? 0) + 1);
84
- });
85
- return {
86
- contract,
87
- tokens: new Set([...counts].filter(([, count]) => count === 1).map(([token]) => token))
88
- };
89
- };
90
- /**
91
- * Where each id's and each seeded source token's owner went: the first
92
- * occurrence that survives the batch keeps an id.
93
- *
94
- * @internal Exported for the per-step equivalence test.
95
- */
96
- const mapParagraphKeepers = (oldDoc, transactions, sourceSeed) => {
97
- const paragraphs = [];
98
- visitAuthoredParagraphs(oldDoc, (node, pos) => {
99
- const id = node.attrs["paraId"];
100
- const token = getProseParagraphPropertySourceToken(node);
101
- if (isUsableParaId(id) || typeof token === "string" && sourceSeed.tokens.has(token)) paragraphs.push({
102
- pos,
103
- id,
104
- token
105
- });
106
- });
107
- const swept = sweepPositions(transactions.map((transaction) => transaction.mapping), paragraphs.flatMap(({ pos }) => [{
108
- pos,
109
- assoc: 1,
110
- from: 0
111
- }, {
112
- pos: pos + 1,
113
- assoc: -1,
114
- from: 0
115
- }]));
116
- const paraIds = /* @__PURE__ */ new Map();
117
- const sourceTokens = /* @__PURE__ */ new Map();
118
- for (const [index, { id, token }] of paragraphs.entries()) {
119
- const mapped = swept[2 * index].pos;
120
- const deleted = swept[2 * index + 1].deletedAcross;
121
- if (isUsableParaId(id) && !paraIds.has(id) && !deleted) paraIds.set(id, mapped);
122
- if (typeof token === "string" && sourceSeed.tokens.has(token)) sourceTokens.set(token, sourceTokens.has(token) ? { status: "ambiguous" } : {
123
- pos: mapped,
124
- status: deleted ? "deleted" : "mapped"
125
- });
126
- }
127
- return {
128
- paraIds,
129
- sourceTokens
130
- };
131
- };
132
- const collectParagraphCensus = (doc, sourceSeed) => {
133
- const census = {
134
- invalidSourceTokens: [],
135
- missingParaIds: [],
136
- paragraphs: /* @__PURE__ */ new Map(),
137
- paraIds: /* @__PURE__ */ new Map(),
138
- sourceTokens: /* @__PURE__ */ new Map()
139
- };
140
- const contractMatchesSeed = sourceSeed?.contract !== null && sourceSeed?.contract !== void 0 && getProseDocumentParagraphPropertySourceContract(doc) === sourceSeed.contract;
141
- visitAuthoredParagraphs(doc, (node, pos) => {
142
- const occurrence = {
143
- pos,
144
- attrs: node.attrs
145
- };
146
- census.paragraphs.set(pos, occurrence);
147
- const id = node.attrs["paraId"];
148
- if (isUsableParaId(id)) {
149
- const occurrences = census.paraIds.get(id) ?? [];
150
- occurrences.push(occurrence);
151
- census.paraIds.set(id, occurrences);
152
- } else census.missingParaIds.push(occurrence);
153
- if (sourceSeed) {
154
- const token = getProseParagraphPropertySourceToken(node);
155
- if (token !== null && token !== void 0) {
156
- if (typeof token !== "string" || !contractMatchesSeed || !sourceSeed.contract || !sourceSeed.tokens.has(token) || !paragraphPropertySourceTokenMatchesContract(token, sourceSeed.contract)) census.invalidSourceTokens.push(occurrence);
157
- else {
158
- const occurrences = census.sourceTokens.get(token) ?? [];
159
- occurrences.push(occurrence);
160
- census.sourceTokens.set(token, occurrences);
161
- }
162
- }
163
- }
164
- });
165
- return census;
166
- };
167
- const collectParaIdUpdatesFromCensus = (census, keeperPositions, deterministicSeed = null) => {
168
- const needFreshId = [...census.missingParaIds];
169
- for (const [id, occurrences] of census.paraIds) {
170
- if (occurrences.length === 1) continue;
171
- const keeperPos = keeperPositions?.get(id);
172
- const keeper = occurrences.find(({ pos }) => pos === keeperPos) ?? occurrences[0];
173
- for (const occurrence of occurrences) if (occurrence !== keeper) needFreshId.push(occurrence);
174
- }
175
- const taken = new Set(census.paraIds.keys());
176
- const updates = [];
177
- for (const { pos, attrs } of needFreshId) {
178
- const newId = mintParaId(taken, deterministicSeed, pos);
179
- taken.add(newId);
180
- updates.push({
181
- pos,
182
- attrs: {
183
- ...attrs,
184
- paraId: newId
185
- }
186
- });
187
- }
188
- updates.sort((left, right) => left.pos - right.pos);
189
- return updates;
190
- };
191
- const collectParagraphPropertySourceUpdates = (census, keeperPositions, transactions) => {
192
- const updates = /* @__PURE__ */ new Map();
193
- for (const { pos } of census.invalidSourceTokens) updates.set(pos, null);
194
- const explicitTransfers = transactions.flatMap((transaction) => [...getExplicitParagraphPropertySourceTransfers(transaction)]);
195
- const explicitlySelected = new Set(explicitTransfers.map(({ selectedToken }) => selectedToken).filter((token) => token !== null));
196
- const explicitlyDisplaced = new Set(explicitTransfers.map(({ displacedToken }) => displacedToken).filter((token) => token !== null));
197
- const keptPositions = /* @__PURE__ */ new Set();
198
- const keptTokens = /* @__PURE__ */ new Set();
199
- for (const [token, occurrences] of census.sourceTokens) {
200
- const mappedOwner = keeperPositions.get(token);
201
- const mappedKeeper = mappedOwner?.status === "mapped" ? occurrences.find(({ pos }) => pos === mappedOwner.pos) : void 0;
202
- const transferredKeeper = !mappedKeeper && explicitlySelected.has(token) && occurrences.length === 1 ? occurrences.at(0) : void 0;
203
- const detachedKeeper = !mappedKeeper && !transferredKeeper && occurrences.length === 1 && (mappedOwner === void 0 || mappedOwner.status === "deleted" && mappedOwner.pos === occurrences.at(0)?.pos) ? occurrences.at(0) : void 0;
204
- const keeper = mappedKeeper ?? transferredKeeper ?? detachedKeeper;
205
- if (keeper) {
206
- keptPositions.add(keeper.pos);
207
- keptTokens.add(token);
208
- }
209
- for (const occurrence of occurrences) if (occurrence !== keeper) updates.set(occurrence.pos, null);
210
- }
211
- for (const [token, mappedOwner] of keeperPositions) {
212
- if (mappedOwner.status !== "mapped" || keptTokens.has(token) || explicitlyDisplaced.has(token) || keptPositions.has(mappedOwner.pos) || !census.paragraphs.has(mappedOwner.pos)) continue;
213
- updates.set(mappedOwner.pos, token);
214
- }
215
- return [...updates].map(([pos, token]) => ({
216
- pos,
217
- token
218
- })).sort((left, right) => left.pos - right.pos);
219
- };
220
- const createParaIdAllocatorPlugin = () => new Plugin({
221
- key: paraIdAllocatorKey.createKey(),
222
- state: {
223
- init: (_config, state) => collectParagraphPropertySourceSeed(state.doc),
224
- apply: (_transaction, seed) => seed
225
- },
226
- appendTransaction(transactions, oldState, newState) {
227
- if (!transactions.some((t) => t.docChanged)) return null;
228
- const paragraphSourceSeed = paraIdAllocatorKey.getState(oldState);
229
- if (!paragraphSourceSeed) panic("ParaId allocator lost its paragraph-property source seed");
230
- const keeperPositions = mapParagraphKeepers(oldState.doc, transactions, paragraphSourceSeed);
231
- const deterministicSeed = transactions.map(deterministicSeedOf).find((value) => typeof value === "string") ?? null;
232
- const census = collectParagraphCensus(newState.doc, paragraphSourceSeed);
233
- const updates = collectParaIdUpdatesFromCensus(census, keeperPositions.paraIds, deterministicSeed);
234
- const paragraphSourceUpdates = collectParagraphPropertySourceUpdates(census, keeperPositions.sourceTokens, transactions);
235
- if (updates.length === 0 && paragraphSourceUpdates.length === 0) return null;
236
- const tr = newState.tr;
237
- for (const u of updates) setProseParagraphMarkupWithPropertySource({
238
- attrs: u.attrs,
239
- ownership: "transfer-allocated-id",
240
- pos: u.pos,
241
- transaction: tr
242
- });
243
- for (const { pos, token } of paragraphSourceUpdates) {
244
- const paragraph = tr.doc.nodeAt(pos);
245
- if (!paragraph || paragraph.type.name !== "paragraph") panic("Paragraph-property token update lost its paragraph");
246
- setProseParagraphMarkupWithPropertySource({
247
- attrs: {
248
- ...paragraph.attrs,
249
- [PROSE_PARAGRAPH_SOURCE_TOKEN_ATTR]: token
250
- },
251
- ownership: "preserve",
252
- pos,
253
- transaction: tr
254
- });
255
- }
256
- ignoreTrackedChanges(tr);
257
- tr.setMeta(paraIdAllocatorKey, "allocated");
258
- tr.setMeta("addToHistory", false);
259
- if (newState.storedMarks !== null) tr.setStoredMarks(newState.storedMarks);
260
- return tr;
261
- }
262
- });
263
- const ParaIdAllocatorExtension = createExtension({
264
- name: "paraIdAllocator",
265
- defaultOptions: {},
266
- onSchemaReady() {
267
- return { plugins: [createParaIdAllocatorPlugin()] };
268
- }
269
- });
270
- export { ParaIdAllocatorExtension, mapParagraphKeepers };
@@ -1,51 +0,0 @@
1
- /**
2
- * Paragraph Change Tracker Extension
3
- *
4
- * Watches ProseMirror transactions and records which paragraph IDs (paraId)
5
- * were modified. Also detects paragraph and block-structure changes.
6
- * Used by the selective save system to patch only changed paragraphs in document.xml.
7
- */
8
- import { TrackedSectionEndpointRemoval } from "../../../internal/sectionEndpointResolution.js";
9
- import { document_d_exports } from "../../../types/document.js";
10
- import { InternalParagraphChangeTrackerState, ParagraphChangeTrackerState } from "../../paragraphChangeTrackerMeta.js";
11
- import { Extension } from "../types.js";
12
- import { EditorState, Plugin } from "prosemirror-state";
13
- export declare function createParagraphChangeTrackerPlugin(): Plugin<InternalParagraphChangeTrackerState>;
14
- /**
15
- * Get the change tracker state from an EditorState
16
- */
17
- export declare function getChangeTrackerState(state: EditorState): ParagraphChangeTrackerState | undefined;
18
- /**
19
- * Get the set of changed paragraph IDs from an EditorState
20
- */
21
- export declare function getChangedParagraphIds(state: EditorState): Set<string>;
22
- /**
23
- * Check if paragraph membership, order, or block structure changed
24
- */
25
- export declare function hasStructuralChanges(state: EditorState): boolean;
26
- /**
27
- * Check if any changes affected paragraphs without paraId
28
- */
29
- export declare function hasUntrackedChanges(state: EditorState): boolean;
30
- /** Exact section-count transition authorized by tracked paragraph-mark resolution. */
31
- export declare function getTrackedSectionEndpointRemoval(state: EditorState): TrackedSectionEndpointRemoval | null;
32
- type RepackWithEditorSectionRemovalsOptions<T> = {
33
- /** The editor state the document was built from. */
34
- state: EditorState;
35
- /** The exact document object `repack` hands to `repackDocx`. */
36
- document: document_d_exports.Document;
37
- repack: () => Promise<T>;
38
- };
39
- /**
40
- * Run an editor save's full repack with the section removals the editor's own
41
- * edits made.
42
- *
43
- * A save refuses a package with fewer sections than its source unless it is
44
- * told which removals were meant: deleting the paragraph that ends a section,
45
- * or accepting its tracked deletion, removes that section on purpose. The
46
- * tracker records exactly those removals; without them the repack throws and
47
- * the edit cannot be saved.
48
- */
49
- export declare const repackWithEditorSectionRemovals: <T>({ state, document, repack }: RepackWithEditorSectionRemovalsOptions<T>) => Promise<T>;
50
- export declare const ParagraphChangeTrackerExtension: (options?: Partial<Record<never, never>> | undefined) => Extension;
51
- export {};