@stll/folio-core 0.76.0 → 0.77.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (86) hide show
  1. package/dist/ai-edits/headless.d.ts +6 -4
  2. package/dist/ai-edits/headless.js +32 -15
  3. package/dist/ai-edits/pending-suggestions.d.ts +9 -0
  4. package/dist/ai-edits/pending-suggestions.js +11 -0
  5. package/dist/ai-edits/read.js +24 -1
  6. package/dist/ai-edits/table-geometry.js +12 -4
  7. package/dist/canonical/canonicalInlineProjection.d.ts +2 -2
  8. package/dist/canonical/canonicalInlineProjection.js +2 -2
  9. package/dist/canonical/canonicalProjectionBlocks.d.ts +10 -1
  10. package/dist/canonical/canonicalPublicOperations.js +13 -38
  11. package/dist/canonical/canonicalSeedContent.d.ts +18 -3
  12. package/dist/canonical/canonicalSeedContent.js +28 -15
  13. package/dist/canonical/canonicalSession.d.ts +22 -12
  14. package/dist/canonical/canonicalSession.js +178 -186
  15. package/dist/controller/canonicalClipboard.d.ts +2 -1
  16. package/dist/controller/canonicalClipboard.js +312 -47
  17. package/dist/controller/canonicalComposition.js +67 -14
  18. package/dist/controller/canonicalStoryEditor.js +2 -0
  19. package/dist/controller/hiddenEditorManager.js +2 -0
  20. package/dist/docx/importedIdCollisions.js +29 -4
  21. package/dist/docx/server/templatePlan/apply.js +47 -2
  22. package/dist/docx/server/templatePlan/blocks.d.ts +27 -7
  23. package/dist/docx/server/templatePlan/blocks.js +103 -12
  24. package/dist/docx/server/templatePlan/builders.d.ts +6 -0
  25. package/dist/docx/server/templatePlan/builders.js +7 -1
  26. package/dist/docx/server/templatePlan/emit.d.ts +2 -0
  27. package/dist/docx/server/templatePlan/emit.js +108 -6
  28. package/dist/docx/server/templatePlan/plan.d.ts +24 -1
  29. package/dist/docx/server/templatePlan/plan.js +165 -12
  30. package/dist/docx/server/templatePlan/planShape.js +13 -2
  31. package/dist/docx/server/templatePlan/revisions.d.ts +55 -0
  32. package/dist/docx/server/templatePlan/revisions.js +276 -2
  33. package/dist/docx/server/templatePlan/story.d.ts +46 -1
  34. package/dist/docx/server/templatePlan/story.js +125 -11
  35. package/dist/docx/server/templatePlan/types.d.ts +97 -1
  36. package/dist/docx/server/templatePlan/verify.js +63 -9
  37. package/dist/docx/settingsHeaderFooterUpdate.d.ts +1 -1
  38. package/dist/docx/tableGridXml.d.ts +3 -7
  39. package/dist/docx/tableGridXml.js +5 -13
  40. package/dist/layout-bridge/convert/footnoteLayout.js +34 -8
  41. package/dist/prosemirror/canonicalMarkupDocument.d.ts +20 -0
  42. package/dist/prosemirror/canonicalMarkupDocument.js +73 -0
  43. package/dist/prosemirror/canonicalTableCommands.d.ts +1 -1
  44. package/dist/prosemirror/canonicalTableCommands.js +1 -3
  45. package/dist/prosemirror/conversion/markupViewSource.d.ts +17 -0
  46. package/dist/prosemirror/conversion/markupViewSource.js +11 -0
  47. package/dist/prosemirror/conversion/toProseDoc.d.ts +3 -0
  48. package/dist/prosemirror/conversion/toProseDoc.js +31 -14
  49. package/dist/prosemirror/independentMarkupProjection.d.ts +27 -0
  50. package/dist/prosemirror/independentMarkupProjection.js +147 -0
  51. package/dist/prosemirror/markupViewNotes.d.ts +1 -1
  52. package/dist/prosemirror/markupViewNotes.js +24 -27
  53. package/dist/prosemirror/markupViewPositionMap.d.ts +16 -0
  54. package/dist/prosemirror/markupViewPositionMap.js +316 -0
  55. package/dist/prosemirror/markupViewProjection.d.ts +3 -46
  56. package/dist/prosemirror/markupViewProjection.js +90 -17
  57. package/dist/prosemirror/replacedAnnotations.d.ts +4 -78
  58. package/dist/prosemirror/replacedAnnotations.js +11 -194
  59. package/dist/prosemirror/styles/styleResolver.d.ts +1 -0
  60. package/dist/prosemirror/styles/styleResolver.js +4 -1
  61. package/dist/server.d.ts +2 -2
  62. package/dist/types/canonicalCapabilities.d.ts +3 -10
  63. package/dist/types/canonicalCapabilities.js +3 -10
  64. package/package.json +2 -2
  65. package/dist/internal/revisionResolutionEdits.d.ts +0 -11
  66. package/dist/internal/revisionResolutionEdits.js +0 -18
  67. package/dist/internal/revisionResolutionInline.d.ts +0 -24
  68. package/dist/internal/revisionResolutionInline.js +0 -302
  69. package/dist/internal/revisionResolutionStep.d.ts +0 -3
  70. package/dist/internal/revisionResolutionStep.js +0 -113
  71. package/dist/internal/revisionResolutionTracking.d.ts +0 -9
  72. package/dist/internal/revisionResolutionTracking.js +0 -124
  73. package/dist/internal/wholeStoryRevisionResolution.d.ts +0 -56
  74. package/dist/internal/wholeStoryRevisionResolution.js +0 -783
  75. package/dist/prosemirror/commands/resolveAllTableChanges.d.ts +0 -38
  76. package/dist/prosemirror/commands/resolveAllTableChanges.js +0 -642
  77. package/dist/prosemirror/commands/resolveNodePropertyChangeAttrs.d.ts +0 -13
  78. package/dist/prosemirror/commands/resolveNodePropertyChangeAttrs.js +0 -45
  79. package/dist/prosemirror/commands/tableCellMergeResolution.d.ts +0 -8
  80. package/dist/prosemirror/commands/tableCellMergeResolution.js +0 -56
  81. package/dist/prosemirror/commands/tableMergeFoldDecisions.d.ts +0 -60
  82. package/dist/prosemirror/commands/tableMergeFoldDecisions.js +0 -78
  83. package/dist/prosemirror/rejoinRunCarriers.d.ts +0 -42
  84. package/dist/prosemirror/rejoinRunCarriers.js +0 -65
  85. package/dist/prosemirror/runPropertyChangeResolution.d.ts +0 -15
  86. package/dist/prosemirror/runPropertyChangeResolution.js +0 -34
@@ -1,783 +0,0 @@
1
- import { getProseParagraphPropertySourceToken, recreateProseNodeWithParagraphPropertySource } from "../docx/paragraphPropertySource.js";
2
- import { anchoredTextBoxId, droppedTextBoxAnchorIds, moveTextBoxesPastNextParagraph } from "../prosemirror/anchoredTextBoxes.js";
3
- import { expectParagraphAttrs } from "../prosemirror/attrs/index.js";
4
- import { inlineBookmarksOf } from "../prosemirror/commands/paragraphBookmarkJoin.js";
5
- import { resolveAllTableChanges } from "../prosemirror/commands/resolveAllTableChanges.js";
6
- import { resolveAllNodePropertyChangeAttrs } from "../prosemirror/commands/resolveNodePropertyChangeAttrs.js";
7
- import { resolveParagraphChangeAttrs } from "../prosemirror/commands/resolveParagraphProperties.js";
8
- import { resolveCommentReferences } from "../prosemirror/commentReferenceIntegrity.js";
9
- import { isBlockBoundaryNode, paragraphRemainsOf } from "../prosemirror/keptRangeBoundaries.js";
10
- import { joinedParagraphAttrs } from "../prosemirror/paragraphMarkJoin.js";
11
- import { rebaseParagraphRunContent } from "../prosemirror/rebaseParagraphRuns.js";
12
- import { createResolvedCommentPointCollector } from "../prosemirror/replacedAnnotations.js";
13
- import { expandRunFormattingCarrier } from "../prosemirror/runFormattingInlineCarriers.js";
14
- import { readAuthoredRunFormatting, reconcileRunFormattingMarks } from "../prosemirror/runFormattingReconciliation.js";
15
- import { paragraphRunStyleContext } from "../prosemirror/runStyleFormatting.js";
16
- import { sectionRecordCopyPositions } from "../prosemirror/sectionCarrier.js";
17
- import { paragraphBeforeRemovedBlock, runParagraphsIntoTables, runsIntoFollowingTable } from "../prosemirror/tableRunIn.js";
18
- import { paragraphContentOf } from "../prosemirror/zeroWidthAnchors.js";
19
- import { recordNodeResolution } from "./revisionResolutionEdits.js";
20
- import { resolveInlineRevisions } from "./revisionResolutionInline.js";
21
- import { composeRevisionResolutionMaps } from "./revisionResolutionStep.js";
22
- import { REVIEW_CARRIERS } from "@stll/docx-core/model";
23
- import { paragraphMarkOutcome } from "@stll/docx-core/ops";
24
- import { Fragment, Slice } from "prosemirror-model";
25
- import { Mapping, ReplaceStep, StepMap, Transform } from "prosemirror-transform";
26
- // src/internal/wholeStoryRevisionResolution.ts
27
- const resolveProperties = ({ doc, mode, styleResolver, numbering, steps, changedRanges }) => {
28
- const removedMark = doc.type.schema.marks[mode === "accept" ? "deletion" : "insertion"];
29
- const collector = createResolvedCommentPointCollector({
30
- removes: (node) => removedMark?.isInSet(node.marks) !== void 0,
31
- retiresStructure: () => mode === "accept"
32
- });
33
- let structural = false;
34
- let removedEndpointCount = 0;
35
- const removedReferences = [];
36
- let runsIntoTable = false;
37
- const walk = ({ node, position, eligibleComments = true, previous }) => {
38
- const childComments = eligibleComments && collector.visit(node, position, previous);
39
- if (node.isText) return node;
40
- if (!runsIntoTable && !node.isTextblock) for (let index = 0; index + 1 < node.childCount; index++) {
41
- const options = {
42
- parent: node,
43
- index,
44
- mode
45
- };
46
- if (runsIntoFollowingTable(options) || paragraphBeforeRemovedBlock(options)) {
47
- runsIntoTable = true;
48
- break;
49
- }
50
- }
51
- const nextAttrs = node.type.name === "paragraph" ? resolveParagraphChangeAttrs({
52
- node,
53
- mode,
54
- boundaryCovered: true,
55
- revisionSet: null,
56
- styleResolver,
57
- numbering
58
- }) : null;
59
- let resolved = node;
60
- if (nextAttrs) {
61
- if (node.type.name === "paragraph" && ownsSection(node) && nextAttrs["_sectionProperties"] == null) {
62
- const properties = expectParagraphAttrs(node)._sectionProperties;
63
- removedEndpointCount++;
64
- for (const { type, rId } of properties?.headerReferences ?? []) removedReferences.push({
65
- part: "header",
66
- type,
67
- relationshipId: rId
68
- });
69
- for (const { type, rId } of properties?.footerReferences ?? []) removedReferences.push({
70
- part: "footer",
71
- type,
72
- relationshipId: rId
73
- });
74
- }
75
- resolved = recreateProseNodeWithParagraphPropertySource(node, { attrs: nextAttrs });
76
- } else if (node.type.name !== "paragraph") resolved = resolveAllNodePropertyChangeAttrs(node, mode);
77
- if (resolved !== node) {
78
- if (node.type.name === "paragraph") changedRanges.push({
79
- from: position,
80
- to: position + node.nodeSize
81
- });
82
- structural ||= node.type.name !== "paragraph";
83
- }
84
- if (position >= 0) recordNodeResolution({
85
- before: node,
86
- after: resolved,
87
- position,
88
- steps
89
- });
90
- const rebased = /* @__PURE__ */ new Map();
91
- if (node.type.name === "paragraph" && styleResolver && nextAttrs && nextAttrs["styleId"] !== node.attrs["styleId"]) {
92
- const previousContext = paragraphRunStyleContext(node, styleResolver);
93
- const nextContext = paragraphRunStyleContext(resolved, styleResolver);
94
- node.descendants((inline, pos) => {
95
- if (!inline.isInline) return true;
96
- const carrier = expandRunFormattingCarrier(inline, pos);
97
- if (!carrier) return !inline.isAtom;
98
- for (const { node: representation } of carrier.representations) {
99
- const authoredFormatting = readAuthoredRunFormatting({
100
- context: previousContext,
101
- marks: representation.marks,
102
- styleResolver
103
- });
104
- const marks = reconcileRunFormattingMarks({
105
- authoredFormatting,
106
- context: nextContext,
107
- node: representation,
108
- styleResolver
109
- });
110
- rebased.set(representation, representation.mark(marks));
111
- }
112
- return false;
113
- });
114
- }
115
- const children = [];
116
- let changed = false;
117
- const visitChild = (child, childPosition, previousChild) => {
118
- const replacement = rebased.get(child) ?? child;
119
- if (rebased.size === 0) return walk({
120
- node: replacement,
121
- position: childPosition,
122
- eligibleComments: childComments,
123
- ...previousChild ? { previous: previousChild } : {}
124
- });
125
- if (childComments) collector.visit(child, childPosition, previousChild);
126
- recordNodeResolution({
127
- before: child,
128
- after: replacement,
129
- position: childPosition,
130
- steps
131
- });
132
- if (replacement.isLeaf) return replacement;
133
- const nested = [];
134
- replacement.forEach((inner, offset) => nested.push(visitChild(inner, childPosition + 1 + offset)));
135
- return recreateProseNodeWithParagraphPropertySource(replacement, { content: nested });
136
- };
137
- resolved.forEach((child, offset, index) => {
138
- const result = visitChild(child, position + 1 + offset, index > 0 ? node.maybeChild(index - 1) ?? void 0 : void 0);
139
- children.push(result);
140
- changed ||= result !== child;
141
- });
142
- return changed ? recreateProseNodeWithParagraphPropertySource(resolved, { content: children }) : resolved;
143
- };
144
- return {
145
- resolved: walk({
146
- node: doc,
147
- position: -1
148
- }),
149
- structural,
150
- runsIntoTable,
151
- removedEndpointCount,
152
- removedReferences,
153
- pointComments: mode === "accept" ? new Set(collector.finish().keys()) : /* @__PURE__ */ new Set()
154
- };
155
- };
156
- const structuralReplacementMap = ({ replacements, tables, bookmarkJoins, origin }) => {
157
- const tablesByPosition = new Map(tables.map((table) => [table.position, table]));
158
- const bookmarkJoinsByPosition = new Map(bookmarkJoins.map((join) => [join.from, join]));
159
- const ranges = [];
160
- const mapFor = ({ from, to, slice }) => {
161
- const table = tablesByPosition.get(from);
162
- if (table && table.sourceNodeSize === to - from) {
163
- const tableRanges = [];
164
- table.positionMap.forEach((oldStart, oldEnd, newStart, newEnd) => {
165
- tableRanges.push(from - origin + oldStart, oldEnd - oldStart, newEnd - newStart);
166
- });
167
- return new StepMap(tableRanges);
168
- }
169
- const bookmarkJoin = bookmarkJoinsByPosition.get(from);
170
- if (bookmarkJoin?.to === to) {
171
- const joinRanges = [];
172
- bookmarkJoin.positionMap.forEach((oldStart, oldEnd, newStart, newEnd) => {
173
- joinRanges.push(from - origin + oldStart, oldEnd - oldStart, newEnd - newStart);
174
- });
175
- return new StepMap(joinRanges);
176
- }
177
- return new StepMap([
178
- from - origin,
179
- to - from,
180
- slice.size
181
- ]);
182
- };
183
- const isDeletion = ({ from, to, slice }) => slice.size === 0 && !tablesByPosition.has(from) && to > from;
184
- const groupMap = (group) => {
185
- if (group.every(isDeletion)) {
186
- const first = group.at(0);
187
- if (!first) return StepMap.empty;
188
- let end = first.to;
189
- for (const replacement of group.slice(1)) end += replacement.to - replacement.from;
190
- return new StepMap([
191
- first.from - origin,
192
- end - first.from,
193
- 0
194
- ]);
195
- }
196
- let maps = group.toReversed().map(mapFor);
197
- while (maps.length > 1) {
198
- const combined = [];
199
- for (let index = 0; index < maps.length; index += 2) {
200
- const first = maps.at(index);
201
- const second = maps.at(index + 1);
202
- if (!first) break;
203
- combined.push(second ? composeRevisionResolutionMaps(first, second) : first);
204
- }
205
- maps = combined;
206
- }
207
- return maps.at(0) ?? StepMap.empty;
208
- };
209
- let group = [];
210
- let influenceEnd = -1;
211
- const flush = () => {
212
- groupMap(group).forEach((oldStart, oldEnd, newStart, newEnd) => {
213
- const last = ranges.length - 3;
214
- const previousStart = ranges.at(last);
215
- const previousOldSize = ranges.at(last + 1);
216
- const previousNewSize = ranges.at(last + 2);
217
- const newSize = newEnd - newStart;
218
- if (last >= 0 && previousStart !== void 0 && previousOldSize !== void 0 && previousNewSize !== void 0 && (previousNewSize === 0 || newSize === 0) && previousStart + previousOldSize === oldStart) {
219
- ranges[last + 1] = previousOldSize + oldEnd - oldStart;
220
- ranges[last + 2] = previousNewSize + newSize;
221
- return;
222
- }
223
- ranges.push(oldStart, oldEnd - oldStart, newSize);
224
- });
225
- };
226
- for (const replacement of replacements) {
227
- if (group.length > 0 && replacement.from >= influenceEnd) {
228
- flush();
229
- group = [];
230
- }
231
- if (group.length === 0) {
232
- group.push(replacement);
233
- influenceEnd = replacement.to;
234
- continue;
235
- }
236
- group.push(replacement);
237
- influenceEnd += replacement.to - replacement.from;
238
- }
239
- if (group.length > 0) flush();
240
- return new StepMap(ranges);
241
- };
242
- const removedEndpoint = (node, context) => {
243
- const properties = expectParagraphAttrs(node)._sectionProperties;
244
- if (properties === void 0) return;
245
- context.removedEndpointCount++;
246
- for (const { type, rId } of properties?.headerReferences ?? []) context.removedReferences.push({
247
- part: "header",
248
- type,
249
- relationshipId: rId
250
- });
251
- for (const { type, rId } of properties?.footerReferences ?? []) context.removedReferences.push({
252
- part: "footer",
253
- type,
254
- relationshipId: rId
255
- });
256
- };
257
- /** A cleared record (null) and an absent one (undefined) both end no section. */
258
- const ownsSection = (paragraph) => expectParagraphAttrs(paragraph)._sectionProperties != null;
259
- const resolveStructure = ({ node, position, context }) => {
260
- if (node.isLeaf) return node;
261
- const textBoxAnchor = anchoredTextBoxId(node);
262
- if (textBoxAnchor !== void 0 && context.droppedTextBoxAnchors.has(textBoxAnchor)) {
263
- context.structural = true;
264
- return null;
265
- }
266
- const structuralStart = {
267
- steps: context.steps.length,
268
- deleted: context.deleted.length,
269
- replacements: context.replacements.length,
270
- tableRanges: context.tableRanges.length,
271
- bookmarkJoins: context.bookmarkJoins.length
272
- };
273
- /**
274
- * Each child as its own resolution left it (`resolved`) and as this level
275
- * has it so far (`node`). A later sibling can rewrite an earlier one (its
276
- * section break or comment anchors move there), so what this level changed
277
- * is always read against `resolved`, never against `node`.
278
- */
279
- const entries = [];
280
- node.forEach((child, offset) => {
281
- const resolved = resolveStructure({
282
- node: child,
283
- position: position + 1 + offset,
284
- context
285
- });
286
- entries.push({
287
- node: resolved,
288
- resolved,
289
- original: child,
290
- position: position + 1 + offset
291
- });
292
- });
293
- const reversed = [];
294
- let chain = null;
295
- /** Where each paragraph a chain left starts, the paragraphs it joined included. */
296
- const chainStarts = /* @__PURE__ */ new Map();
297
- let pendingBookmarks = [];
298
- let following = "nothing";
299
- const precedingParagraphs = [];
300
- const endsWithAnyBlock = node.type.name === "blockSdt";
301
- let endsWell = false;
302
- for (const { node: child } of entries) {
303
- precedingParagraphs.push(endsWell);
304
- if (child !== null && !isBlockBoundaryNode(child)) endsWell = endsWithAnyBlock || child.type.name === "paragraph";
305
- }
306
- const flush = () => {
307
- if (!chain) return;
308
- let final = chain.node;
309
- if (chain.chunks.length > 1) {
310
- const children = [];
311
- for (let index = chain.chunks.length - 1; index >= 0; index--) {
312
- const chunk = chain.chunks[index];
313
- if (!chunk) continue;
314
- let content = chunk.type === "paragraph" ? chunk.source.content : chunk.content;
315
- if (chunk.type === "paragraph" && chunk.position !== chain.formattingOwnerPosition) content = rebaseParagraphRunContent({
316
- paragraph: chunk.source,
317
- target: chain.node,
318
- position: chunk.position,
319
- styleResolver: context.styleResolver,
320
- onRebased: ({ before, after, position: runPosition }) => recordNodeResolution({
321
- before,
322
- after,
323
- position: runPosition,
324
- steps: context.steps
325
- })
326
- });
327
- content.forEach((child) => children.push(child));
328
- }
329
- final = recreateProseNodeWithParagraphPropertySource(chain.node, { content: Fragment.fromArray(children) });
330
- }
331
- if (!chain.before.eq(final)) context.changedRanges.push({
332
- from: chain.position,
333
- to: chain.position + chain.before.nodeSize
334
- });
335
- recordNodeResolution({
336
- before: chain.before,
337
- after: final,
338
- position: chain.position,
339
- steps: context.steps
340
- });
341
- for (const { closing, bookmarks } of chain.joinBoundaries) {
342
- if (bookmarks.size === 0) {
343
- context.deleted.push({
344
- from: closing,
345
- to: closing + 2
346
- });
347
- continue;
348
- }
349
- const to = closing + bookmarks.size + 2;
350
- context.replacements.push({
351
- from: closing,
352
- to,
353
- slice: new Slice(bookmarks, 0, 0)
354
- });
355
- context.bookmarkJoins.push({
356
- from: closing,
357
- to,
358
- positionMap: new StepMap([
359
- 0,
360
- 1,
361
- 0,
362
- bookmarks.size + 1,
363
- 1,
364
- 0
365
- ])
366
- });
367
- }
368
- const left = chain.chunks.length === 1 && final.eq(chain.before) ? chain.before : final;
369
- chainStarts.set(left, chain.position);
370
- reversed.push(left);
371
- chain = null;
372
- };
373
- const flushBookmarks = () => {
374
- flush();
375
- for (const bookmark of pendingBookmarks) reversed.push(bookmark);
376
- pendingBookmarks = [];
377
- };
378
- for (let index = entries.length - 1; index >= 0; index--) {
379
- const entry = entries[index];
380
- if (!entry) continue;
381
- let paragraph = entry.node;
382
- if (paragraph === null) {
383
- const filler = index + reversed.length + pendingBookmarks.length + (chain ? 1 : 0) > 0 ? null : node.type.schema.nodes["paragraph"]?.createAndFill();
384
- context.replacements.push({
385
- from: entry.position,
386
- to: entry.position + entry.original.nodeSize,
387
- slice: filler ? new Slice(Fragment.from(filler), 0, 0) : Slice.empty
388
- });
389
- if (!filler) continue;
390
- paragraph = filler;
391
- }
392
- const before = entry.resolved ?? paragraph;
393
- if (paragraph.type.name !== "paragraph") {
394
- if (isBlockBoundaryNode(paragraph)) {
395
- pendingBookmarks.push(paragraph);
396
- continue;
397
- }
398
- flushBookmarks();
399
- reversed.push(paragraph);
400
- if (paragraph.type.spec["tableRole"] === "table") following = "table";
401
- else if (paragraph.type.name !== "textBox") following = "block";
402
- continue;
403
- }
404
- const marker = expectParagraphAttrs(paragraph).pPrMark;
405
- const markWasAdded = marker?.kind === "ins" || marker?.kind === "moveTo";
406
- const joins = marker && (marker.deletion !== void 0 || markWasAdded !== (context.mode === "accept"));
407
- if (joins && entries[index + 1]?.node?.type.name === "textBox") context.textBoxesStandInJoins = true;
408
- if (joins && chain) {
409
- const boundaries = pendingBookmarks.toReversed();
410
- let bookmarks = Fragment.empty;
411
- if (boundaries.length > 0) {
412
- const inline = inlineBookmarksOf(boundaries);
413
- if (!inline) {
414
- context.failed = true;
415
- return null;
416
- }
417
- bookmarks = Fragment.fromArray(inline);
418
- chain.chunks.push({
419
- type: "bookmarks",
420
- content: bookmarks
421
- });
422
- pendingBookmarks = [];
423
- }
424
- const joinedAttrs = joinedParagraphAttrs({
425
- paragraph,
426
- next: chain.node,
427
- between: bookmarks.size,
428
- styleResolver: context.styleResolver
429
- });
430
- if (joinedAttrs !== chain.node.attrs) chain.node = recreateProseNodeWithParagraphPropertySource(chain.node, { attrs: joinedAttrs });
431
- const displacedToken = getProseParagraphPropertySourceToken(paragraph);
432
- const selectedToken = getProseParagraphPropertySourceToken(chain.node);
433
- context.transfers.push({
434
- displacedToken: typeof displacedToken === "string" ? displacedToken : null,
435
- selectedToken: typeof selectedToken === "string" ? selectedToken : null
436
- });
437
- chain.chunks.push({
438
- type: "paragraph",
439
- source: paragraph,
440
- position: entry.position
441
- });
442
- chain.position = entry.position;
443
- chain.before = before;
444
- chain.joinBoundaries.push({
445
- closing: entry.position + before.nodeSize - 1,
446
- bookmarks
447
- });
448
- removedEndpoint(paragraph, context);
449
- continue;
450
- }
451
- flushBookmarks();
452
- let follows = "nothing";
453
- if (chain) follows = "paragraph";
454
- else if (following === "block") follows = "block";
455
- else if (following !== "nothing") follows = "table";
456
- let containerStillEndsWithParagraph = true;
457
- if (follows === "table" || follows === "block") containerStillEndsWithParagraph = node.lastChild?.type.name === "paragraph";
458
- else if (follows === "nothing") containerStillEndsWithParagraph = precedingParagraphs[index] === true;
459
- const outcome = paragraphMarkOutcome({
460
- follows,
461
- holdsContent: paragraphContentOf(paragraph),
462
- markWasAdded,
463
- containerStillEndsWithParagraph,
464
- followingTableCanReceiveRunIn: false
465
- });
466
- const remains = marker && !paragraphContentOf(paragraph) && outcome.kind === "remove" && joins && index + reversed.length + 1 > 1 ? paragraphRemainsOf(paragraph) : void 0;
467
- const toPrevious = remains ?? [];
468
- const commentHost = entries[index - 1]?.node;
469
- if (remains !== void 0 && (toPrevious.length === 0 || commentHost?.type.name === "paragraph")) {
470
- const hostEntry = entries[index - 1];
471
- if (toPrevious.length > 0 && commentHost && hostEntry) {
472
- const comments = Fragment.fromArray(toPrevious);
473
- hostEntry.node = recreateProseNodeWithParagraphPropertySource(commentHost, { content: commentHost.content.append(comments) });
474
- const hostEnd = hostEntry.position + hostEntry.original.nodeSize - 1;
475
- context.replacements.push({
476
- from: hostEnd,
477
- to: hostEnd,
478
- slice: new Slice(comments, 0, 0)
479
- });
480
- }
481
- context.replacements.push({
482
- from: entry.position,
483
- to: entry.position + entry.original.nodeSize,
484
- slice: Slice.empty
485
- });
486
- removedEndpoint(paragraph, context);
487
- continue;
488
- }
489
- const dropsSection = joins && markWasAdded && ownsSection(paragraph);
490
- if (dropsSection) removedEndpoint(paragraph, context);
491
- const resolved = marker ? recreateProseNodeWithParagraphPropertySource(paragraph, { attrs: {
492
- ...paragraph.attrs,
493
- pPrMark: null,
494
- ...dropsSection ? { _sectionProperties: null } : {}
495
- } }) : paragraph;
496
- chain = {
497
- node: resolved,
498
- formattingOwnerPosition: entry.position,
499
- chunks: [{
500
- type: "paragraph",
501
- source: resolved,
502
- position: entry.position
503
- }],
504
- position: entry.position,
505
- before,
506
- joinBoundaries: []
507
- };
508
- following = "paragraph";
509
- }
510
- flushBookmarks();
511
- reversed.reverse();
512
- if (node.type.name === "doc") {
513
- const carrierIndex = reversed.findLastIndex((child) => child.type.name !== "textBox");
514
- const carrier = reversed[carrierIndex];
515
- if (carrier?.type.name === "paragraph" && carrier.attrs["reviewCarrier"] === REVIEW_CARRIERS.TERMINAL_TABLE) {
516
- const table = reversed.findLast((child) => child.type.name === "table");
517
- let pending = false;
518
- table?.descendants((child) => {
519
- pending ||= child.type.name === "tableRow" && child.attrs["trDel"] !== null && typeof child.attrs["trDel"] === "object" || child.type.name !== "tableRow" && child.marks.some((mark) => mark.type.name === "deletion");
520
- return !pending;
521
- });
522
- const entry = entries.findLast((candidate) => candidate.node?.type.name !== "textBox");
523
- if (!pending && entry) {
524
- if (context.mode === "accept" || carrier.content.size > 0) {
525
- const cleared = recreateProseNodeWithParagraphPropertySource(carrier, { attrs: {
526
- ...carrier.attrs,
527
- reviewCarrier: void 0
528
- } });
529
- recordNodeResolution({
530
- before: carrier,
531
- after: cleared,
532
- position: entry.position,
533
- steps: context.steps
534
- });
535
- context.changedRanges.push({
536
- from: entry.position,
537
- to: entry.position + carrier.nodeSize
538
- });
539
- reversed[carrierIndex] = cleared;
540
- } else if (reversed.length > 1) {
541
- reversed.splice(carrierIndex, 1);
542
- const from = chainStarts.get(carrier) ?? entry.position;
543
- const to = entry.position + entry.original.nodeSize;
544
- const outside = (range) => range.to <= from || range.from >= to;
545
- const keep = (ranges) => ranges.splice(0, ranges.length, ...ranges.filter(outside));
546
- keep(context.deleted);
547
- keep(context.replacements);
548
- keep(context.bookmarkJoins);
549
- context.deleted.push({
550
- from,
551
- to
552
- });
553
- } else {
554
- const filler = node.type.schema.nodes["paragraph"]?.createAndFill();
555
- if (filler) {
556
- reversed[0] = filler;
557
- context.replacements.push({
558
- from: entry.position,
559
- to: entry.position + carrier.nodeSize,
560
- slice: new Slice(Fragment.from(filler), 0, 0)
561
- });
562
- }
563
- }
564
- }
565
- }
566
- }
567
- const rebuilt = reversed.length === node.childCount && reversed.every((child, index) => child === node.child(index)) ? node : recreateProseNodeWithParagraphPropertySource(node, { content: reversed });
568
- if (node.type.spec["tableRole"] !== "table") return rebuilt;
569
- const tableResult = resolveAllTableChanges({
570
- table: rebuilt,
571
- mode: context.mode
572
- });
573
- context.structural ||= tableResult.structural;
574
- context.failed ||= tableResult.failed;
575
- const final = tableResult.node;
576
- if (final) {
577
- const inside = [...context.replacements.slice(structuralStart.replacements), ...context.deleted.slice(structuralStart.deleted).map(({ from, to }) => ({
578
- from,
579
- to,
580
- slice: Slice.empty
581
- }))].sort((a, b) => a.from - b.from);
582
- const inputMap = structuralReplacementMap({
583
- replacements: inside,
584
- tables: context.tableRanges.slice(structuralStart.tableRanges),
585
- bookmarkJoins: context.bookmarkJoins.slice(structuralStart.bookmarkJoins),
586
- origin: position
587
- });
588
- const positionMap = composeRevisionResolutionMaps(inputMap, tableResult.positionMap);
589
- context.tableRanges.push({
590
- node: final,
591
- inputNode: rebuilt,
592
- sourceNodeSize: node.nodeSize,
593
- position,
594
- inputMap,
595
- positionMap,
596
- ranges: tableResult.changedParagraphRanges,
597
- paragraphOffsets: tableResult.paragraphOffsets
598
- });
599
- }
600
- context.steps.length = structuralStart.steps;
601
- context.deleted.length = structuralStart.deleted;
602
- context.replacements.length = structuralStart.replacements;
603
- context.bookmarkJoins.length = structuralStart.bookmarkJoins;
604
- if (final && !node.eq(final)) context.replacements.push({
605
- from: position,
606
- to: position + node.nodeSize,
607
- slice: new Slice(Fragment.from(final), 0, 0)
608
- });
609
- return final;
610
- };
611
- /**
612
- * The text boxes drawn in a paragraph stand between it and the next one. When
613
- * the paragraph's mark goes, the two join, and the boxes follow the result.
614
- */
615
- const moveTextBoxesOutOfJoins = (tr, mode) => {
616
- const joining = [];
617
- tr.doc.descendants((node, position) => {
618
- if (node.type.name !== "paragraph") return true;
619
- const mark = expectParagraphAttrs(node).pPrMark;
620
- const wasAdded = mark?.kind === "ins" || mark?.kind === "moveTo";
621
- if (mark && (mark.deletion !== void 0 || wasAdded !== (mode === "accept"))) joining.push(position);
622
- return false;
623
- });
624
- for (const position of joining.toReversed()) moveTextBoxesPastNextParagraph(tr, position);
625
- };
626
- const resolveWholeStory = ({ doc: storyDoc, mode, styleResolver, numbering }) => {
627
- const withoutCopies = new Transform(storyDoc);
628
- for (const position of sectionRecordCopyPositions(storyDoc)) withoutCopies.setNodeAttribute(position, "_sectionProperties", null);
629
- const doc = withoutCopies.doc;
630
- const { runsIntoTable, textBoxesStandInJoins, ...direct } = resolveStoryRevisions({
631
- doc,
632
- mode,
633
- styleResolver,
634
- numbering
635
- });
636
- if (textBoxesStandInJoins) {
637
- const prepared = new Transform(doc);
638
- moveTextBoxesOutOfJoins(prepared, mode);
639
- if (prepared.docChanged) {
640
- const result = resolveWholeStory({
641
- doc: prepared.doc,
642
- mode,
643
- styleResolver,
644
- numbering
645
- });
646
- let preparedMap = StepMap.empty;
647
- for (const map of prepared.mapping.maps) preparedMap = composeRevisionResolutionMaps(preparedMap, map);
648
- const positionMap = composeRevisionResolutionMaps(preparedMap, result.positionMap);
649
- return {
650
- ...result,
651
- steps: [
652
- ...withoutCopies.steps,
653
- ...prepared.steps,
654
- ...result.steps
655
- ],
656
- positionMap,
657
- mapping: new Mapping([positionMap])
658
- };
659
- }
660
- }
661
- if (!runsIntoTable) return {
662
- ...direct,
663
- steps: [...withoutCopies.steps, ...direct.steps]
664
- };
665
- const runIn = new Transform(doc);
666
- const { targets, maps, transfers } = runParagraphsIntoTables(runIn, mode, styleResolver);
667
- if (!runIn.docChanged) return {
668
- ...direct,
669
- steps: [...withoutCopies.steps, ...direct.steps]
670
- };
671
- const { runsIntoTable: _rerun, textBoxesStandInJoins: _standing, ...result } = resolveStoryRevisions({
672
- doc: runIn.doc,
673
- mode,
674
- styleResolver,
675
- numbering
676
- });
677
- for (const { position, step } of targets) {
678
- const from = runIn.mapping.slice(step).map(position);
679
- const target = runIn.doc.nodeAt(from);
680
- if (target) result.changedRanges.push({
681
- from,
682
- to: from + target.nodeSize
683
- });
684
- }
685
- let runInMap = StepMap.empty;
686
- for (const map of maps) runInMap = composeRevisionResolutionMaps(runInMap, map);
687
- const positionMap = composeRevisionResolutionMaps(runInMap, result.positionMap);
688
- return {
689
- ...result,
690
- steps: [
691
- ...withoutCopies.steps,
692
- ...runIn.steps,
693
- ...result.steps
694
- ],
695
- positionMap,
696
- mapping: new Mapping([positionMap]),
697
- transfers: [...transfers, ...result.transfers]
698
- };
699
- };
700
- const resolveStoryRevisions = ({ doc, mode, styleResolver, numbering }) => {
701
- const steps = [];
702
- const propertyRanges = [];
703
- const propertyResult = resolveProperties({
704
- doc,
705
- mode,
706
- styleResolver,
707
- numbering,
708
- steps,
709
- changedRanges: propertyRanges
710
- });
711
- const properties = propertyResult.resolved;
712
- const inline = resolveInlineRevisions({
713
- pointComments: propertyResult.pointComments,
714
- doc: properties,
715
- mode,
716
- styleResolver,
717
- keepType: doc.type.schema.marks[mode === "accept" ? "insertion" : "deletion"],
718
- removeType: doc.type.schema.marks[mode === "accept" ? "deletion" : "insertion"]
719
- });
720
- const inlineResolved = inline?.resolved ?? properties;
721
- const comments = inline ? resolveCommentReferences(inlineResolved, doc) : null;
722
- const context = {
723
- mode,
724
- styleResolver,
725
- steps: [],
726
- changedRanges: [],
727
- deleted: [],
728
- replacements: [],
729
- transfers: [],
730
- tableRanges: [],
731
- bookmarkJoins: [],
732
- structural: propertyResult.structural,
733
- failed: false,
734
- textBoxesStandInJoins: false,
735
- removedEndpointCount: propertyResult.removedEndpointCount,
736
- removedReferences: [...propertyResult.removedReferences],
737
- droppedTextBoxAnchors: inline ? droppedTextBoxAnchorIds(properties, inlineResolved) : /* @__PURE__ */ new Set()
738
- };
739
- const resolved = resolveStructure({
740
- node: comments?.doc ?? inlineResolved,
741
- position: -1,
742
- context
743
- }) ?? doc;
744
- if (inline) steps.push(...inline.steps);
745
- if (comments) steps.push(...comments.steps);
746
- steps.push(...context.steps);
747
- const replacements = [...context.replacements, ...context.deleted.map(({ from, to }) => ({
748
- from,
749
- to,
750
- slice: Slice.empty
751
- }))].sort((a, b) => a.from - b.from);
752
- const structuralMap = structuralReplacementMap({
753
- replacements,
754
- tables: context.tableRanges,
755
- bookmarkJoins: context.bookmarkJoins,
756
- origin: 0
757
- });
758
- for (const { from, to, slice } of replacements.toReversed()) steps.push(new ReplaceStep(from, to, slice));
759
- let preStructureMap = inline?.positionMap ?? StepMap.empty;
760
- for (const map of comments?.positionMaps ?? []) preStructureMap = composeRevisionResolutionMaps(preStructureMap, map);
761
- const positionMap = composeRevisionResolutionMaps(preStructureMap, structuralMap);
762
- return {
763
- resolved,
764
- mapping: new Mapping([positionMap]),
765
- positionMap,
766
- steps,
767
- changedRanges: [...propertyRanges, ...inline?.ranges ?? []],
768
- preStructureMap,
769
- tableRanges: context.tableRanges,
770
- transfers: context.transfers,
771
- structuralRanges: context.changedRanges,
772
- structuralMap,
773
- structural: context.structural,
774
- failed: context.failed,
775
- textBoxesStandInJoins: context.textBoxesStandInJoins,
776
- removedEndpointCount: context.removedEndpointCount,
777
- removedReferences: context.removedReferences,
778
- runsIntoTable: propertyResult.runsIntoTable,
779
- /** The boundaries removed content left standing, for the range repair. */
780
- keptBoundaries: inline?.keptBoundaries ?? /* @__PURE__ */ new Set()
781
- };
782
- };
783
- export { resolveWholeStory };