@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,38 +0,0 @@
1
- import { Node } from "prosemirror-model";
2
- import { StepMap } from "prosemirror-transform";
3
- type ResolveMode = "accept" | "reject";
4
- type ResolveAllTableChangesOptions = {
5
- /** The table before resolution; its topology determines row and cell positions. */
6
- table: Node;
7
- /** Descendants after the caller's inline, paragraph, and nested-table pass. */
8
- resolvedRows?: readonly Node[];
9
- mode: ResolveMode;
10
- };
11
- export type ResolvedTableChanges = {
12
- /** Null means every row was removed; the caller owns the enclosing schema fallback. */
13
- node: Node | null;
14
- changed: boolean;
15
- structural: boolean;
16
- failed: boolean;
17
- /** Final-table-relative ranges whose surviving paragraphs changed. */
18
- changedParagraphRanges: readonly {
19
- from: number;
20
- to: number;
21
- }[];
22
- /** Source and final paragraph starts, each relative to its table node start. */
23
- paragraphOffsets: readonly {
24
- source: number;
25
- final: number;
26
- }[];
27
- /** Table-node-relative source-to-final positions, preserving surviving cells. */
28
- positionMap: StepMap;
29
- };
30
- /**
31
- * Resolve a table in one grid pass for row revisions and ordinary cell
32
- * membership revisions. The grid records each original cell rectangle once;
33
- * retaining rows reduces its rowspan, while a cell whose starting row is
34
- * removed moves to the next retained row inside that rectangle. This is the
35
- * same topology operation as prosemirror-tables' removeRow.
36
- */
37
- export declare const resolveAllTableChanges: ({ table, resolvedRows, mode }: ResolveAllTableChangesOptions) => ResolvedTableChanges;
38
- export {};
@@ -1,642 +0,0 @@
1
- import { restoreTableCellsWithParagraphPropertySources, transportTableCellsWithParagraphPropertySources } from "../../docx/paragraphPropertySource.js";
2
- import { isTableCellMergeRevisionContinuation, isTableCellMergeRevisionValue } from "../../docx/tableParser.js";
3
- import { maxOf } from "../../utils/extremes.js";
4
- import { expectTableAttrs, mergeTableAttrs } from "../attrs/index.js";
5
- import { nodePropertyRevisionSites, propertyRevisionRecords } from "../revisionCarriers.js";
6
- import { getTableCellMergeChange } from "../tableCellMergeRevision.js";
7
- import { isTableCellRetainedInReviewView } from "../tableCellRevisionVisibility.js";
8
- import { resolveAllNodePropertyChangeAttrs } from "./resolveNodePropertyChangeAttrs.js";
9
- import { createRestoredTableCell, hasMatchingCollapsedTableCellMerge, tableCellContinuationCells, tableCellContinuationFromNode, tableCellContinuationPayload } from "./tableCellMergeResolution.js";
10
- import { resolvedTableMergeOriginAttrs, resolvedVisibleTableCellMergeAttrs, tableMergeFoldDecisions } from "./tableMergeFoldDecisions.js";
11
- import { panic } from "better-result";
12
- import { TableMap } from "prosemirror-tables";
13
- import { StepMap } from "prosemirror-transform";
14
- // src/prosemirror/commands/resolveAllTableChanges.ts
15
- const indexedTableCells = (map) => {
16
- const rectangles = /* @__PURE__ */ new Map();
17
- for (let index = 0; index < map.map.length; index++) {
18
- const position = map.map[index];
19
- if (position === void 0 || rectangles.has(position)) continue;
20
- const left = index % map.width;
21
- const top = Math.floor(index / map.width);
22
- let right = left + 1;
23
- let bottom = top + 1;
24
- while (right < map.width && map.map[index + right - left] === position) right++;
25
- while (bottom < map.height && map.map[index + (bottom - top) * map.width] === position) bottom++;
26
- rectangles.set(position, {
27
- left,
28
- top,
29
- right,
30
- bottom
31
- });
32
- }
33
- return (position) => rectangles.get(position) ?? panic("Missing table resolution rectangle", { position });
34
- };
35
- const elementAt = (items, index) => {
36
- const item = items.at(index);
37
- return item === void 0 ? panic("Missing table resolution element", { index }) : item;
38
- };
39
- const tableResolutionMetadata = ({ sourceTable, table, rows, tableTouched }) => {
40
- const changedParagraphRanges = [];
41
- const paragraphOffsets = [];
42
- const anchors = [];
43
- const finalCellStarts = [];
44
- if (tableTouched) changedParagraphRanges.push({
45
- from: 0,
46
- to: table.nodeSize
47
- });
48
- let rowStart = 1;
49
- for (let rowIndex = 0; rowIndex < table.childCount; rowIndex++) {
50
- const row = table.child(rowIndex);
51
- const metadata = elementAt(rows, rowIndex);
52
- if (!tableTouched && metadata.touched) changedParagraphRanges.push({
53
- from: rowStart,
54
- to: rowStart + row.nodeSize
55
- });
56
- let cellStart = rowStart + 1;
57
- for (let cellIndex = 0; cellIndex < row.childCount; cellIndex++) {
58
- const cell = row.child(cellIndex);
59
- const cellMetadata = elementAt(metadata.cells, cellIndex);
60
- finalCellStarts.push(cellStart);
61
- if (cellMetadata.mapSourcePosition >= 0) anchors.push({
62
- source: cellMetadata.mapSourcePosition + 1,
63
- final: cellStart,
64
- size: cell.nodeSize
65
- });
66
- if (!tableTouched && !metadata.touched && cellMetadata.touched) changedParagraphRanges.push({
67
- from: cellStart,
68
- to: cellStart + cell.nodeSize
69
- });
70
- if (cellMetadata.sourcePosition >= 0) {
71
- const sourceCellPosition = cellMetadata.sourcePosition;
72
- const finalCellPosition = cellStart;
73
- cell.descendants((node, offset) => {
74
- if (node.type.name === "table") return false;
75
- if (node.type.name !== "paragraph") return true;
76
- paragraphOffsets.push({
77
- source: sourceCellPosition + 2 + offset,
78
- final: finalCellPosition + 1 + offset
79
- });
80
- return false;
81
- });
82
- }
83
- cellStart += cell.nodeSize;
84
- }
85
- rowStart += row.nodeSize;
86
- }
87
- const sourceCellStarts = [];
88
- let sourceRowStart = 1;
89
- for (let rowIndex = 0; rowIndex < sourceTable.childCount; rowIndex++) {
90
- const row = sourceTable.child(rowIndex);
91
- let cellStart = sourceRowStart + 1;
92
- for (let cellIndex = 0; cellIndex < row.childCount; cellIndex++) {
93
- sourceCellStarts.push(cellStart);
94
- cellStart += row.child(cellIndex).nodeSize;
95
- }
96
- sourceRowStart += row.nodeSize;
97
- }
98
- const ranges = [];
99
- let sourceCursor = 0;
100
- let finalCursor = 0;
101
- let sourceCellIndex = 0;
102
- let finalCellIndex = 0;
103
- for (const anchor of [...anchors, {
104
- source: sourceTable.nodeSize,
105
- final: table.nodeSize,
106
- size: 0
107
- }]) {
108
- if (anchor.source < sourceCursor || anchor.final < finalCursor) continue;
109
- while (sourceCellStarts.at(sourceCellIndex) !== void 0 && elementAt(sourceCellStarts, sourceCellIndex) < sourceCursor) sourceCellIndex++;
110
- while (finalCellStarts.at(finalCellIndex) !== void 0 && elementAt(finalCellStarts, finalCellIndex) < finalCursor) finalCellIndex++;
111
- const sourceGap = anchor.source - sourceCursor;
112
- const finalGap = anchor.final - finalCursor;
113
- const hasUnmatchedCell = (sourceCellStarts.at(sourceCellIndex) ?? 1e400) < anchor.source || (finalCellStarts.at(finalCellIndex) ?? 1e400) < anchor.final;
114
- if (sourceGap !== finalGap || hasUnmatchedCell) ranges.push(sourceCursor, sourceGap, finalGap);
115
- sourceCursor = anchor.source + anchor.size;
116
- finalCursor = anchor.final + anchor.size;
117
- }
118
- return {
119
- changedParagraphRanges,
120
- paragraphOffsets,
121
- positionMap: new StepMap(ranges)
122
- };
123
- };
124
- const hasNodePropertyRevision = (node) => nodePropertyRevisionSites(node.type.name).some((site) => site.resolution === "node-attrs" && propertyRevisionRecords(node, site).length > 0);
125
- const rowRevisionKind = (row) => {
126
- for (const kind of ["trIns", "trDel"]) {
127
- const marker = row.attrs[kind];
128
- if (typeof marker === "object" && marker !== null && "revisionId" in marker && typeof marker.revisionId === "number") return kind;
129
- }
130
- return null;
131
- };
132
- const cellRevisionMarker = (cell) => {
133
- const marker = cell.attrs["cellMarker"];
134
- if (typeof marker !== "object" || marker === null || !("kind" in marker) || marker.kind !== "ins" && marker.kind !== "del" && marker.kind !== "merge" || !("info" in marker) || typeof marker.info !== "object" || marker.info === null || !("revisionId" in marker.info) || typeof marker.info.revisionId !== "number") return null;
135
- const verticalMergeOriginal = "verticalMergeOriginal" in marker && isTableCellMergeRevisionValue(marker.verticalMergeOriginal) ? marker.verticalMergeOriginal : void 0;
136
- const verticalMerge = "verticalMerge" in marker && isTableCellMergeRevisionValue(marker.verticalMerge) ? marker.verticalMerge : void 0;
137
- return {
138
- kind: marker.kind,
139
- info: { revisionId: marker.info.revisionId },
140
- ...verticalMergeOriginal ? { verticalMergeOriginal } : {},
141
- ...verticalMerge ? { verticalMerge } : {}
142
- };
143
- };
144
- /**
145
- * Resolve a table in one grid pass for row revisions and ordinary cell
146
- * membership revisions. The grid records each original cell rectangle once;
147
- * retaining rows reduces its rowspan, while a cell whose starting row is
148
- * removed moves to the next retained row inside that rectangle. This is the
149
- * same topology operation as prosemirror-tables' removeRow.
150
- */
151
- const resolveAllTableChanges = ({ table, resolvedRows, mode }) => {
152
- const sourceRows = resolvedRows ?? Array.from({ length: table.childCount }, (_, i) => table.child(i));
153
- const tableWithResolvedChildren = table.type.create(table.attrs, sourceRows, table.marks);
154
- const tablePropertyTouched = hasNodePropertyRevision(tableWithResolvedChildren);
155
- const rowPropertyTouched = sourceRows.map(hasNodePropertyRevision);
156
- const propertyResolvedTable = resolveTablePropertyAttrs(tableWithResolvedChildren, mode);
157
- const rows = Array.from({ length: propertyResolvedTable.childCount }, (_, i) => propertyResolvedTable.child(i));
158
- const rowKinds = rows.map(rowRevisionKind);
159
- const keepRows = rowKinds.map((kind) => kind === null || kind === "trIns" === (mode === "accept"));
160
- const hasRowRevision = rowKinds.some((kind) => kind !== null);
161
- const hasMerge = rows.some((row) => {
162
- for (let i = 0; i < row.childCount; i++) {
163
- const cell = row.child(i);
164
- if (cellRevisionMarker(cell)?.kind === "merge" || hasMatchingCollapsedTableCellMerge(cell, null)) return true;
165
- }
166
- return false;
167
- });
168
- const map = TableMap.get(propertyResolvedTable);
169
- const cellRectangle = indexedTableCells(map);
170
- const cells = [];
171
- let rowOffset = 0;
172
- let hasCellRevision = false;
173
- for (let rowIndex = 0; rowIndex < rows.length; rowIndex++) {
174
- const row = elementAt(rows, rowIndex);
175
- let cellOffset = rowOffset + 1;
176
- for (let cellIndex = 0; cellIndex < row.childCount; cellIndex++) {
177
- const cell = row.child(cellIndex);
178
- const rectangle = cellRectangle(cellOffset);
179
- const marker = cellRevisionMarker(cell);
180
- if ((marker?.kind === "ins" || marker?.kind === "del" ? marker : null) !== null) hasCellRevision = true;
181
- cells.push({
182
- node: cell,
183
- left: rectangle.left,
184
- originRow: rowIndex,
185
- bottom: rectangle.bottom,
186
- sourcePosition: cellOffset,
187
- mapSourcePosition: cellOffset,
188
- touched: hasNodePropertyRevision(elementAt(sourceRows, rowIndex).child(cellIndex))
189
- });
190
- cellOffset += cell.nodeSize;
191
- }
192
- rowOffset += row.nodeSize;
193
- }
194
- if (!keepRows.some(Boolean)) return {
195
- node: null,
196
- changed: true,
197
- structural: hasRowRevision || hasCellRevision,
198
- failed: false,
199
- changedParagraphRanges: [],
200
- paragraphOffsets: [],
201
- positionMap: new StepMap([
202
- 0,
203
- tableWithResolvedChildren.nodeSize,
204
- 0
205
- ])
206
- };
207
- const keptBefore = [0];
208
- for (const keep of keepRows) keptBefore.push((keptBefore.at(-1) ?? 0) + Number(keep));
209
- const nextKeptRow = rows.map(() => rows.length);
210
- let next = rows.length;
211
- for (let index = rows.length - 1; index >= 0; index--) {
212
- if (elementAt(keepRows, index)) next = index;
213
- nextKeptRow[index] = next;
214
- }
215
- const placements = rows.map(() => []);
216
- for (const cell of cells) {
217
- const destination = elementAt(nextKeptRow, cell.originRow);
218
- if (destination >= cell.bottom) continue;
219
- const rowspan = elementAt(keptBefore, cell.bottom) - elementAt(keptBefore, destination);
220
- elementAt(placements, destination)[cell.left] = {
221
- ...cell,
222
- mapSourcePosition: destination === cell.originRow ? cell.mapSourcePosition : -1,
223
- touched: cell.touched || rowspan !== cell.node.attrs["rowspan"],
224
- node: rowspan === cell.node.attrs["rowspan"] ? cell.node : cell.node.type.create({
225
- ...cell.node.attrs,
226
- rowspan
227
- }, cell.node.content, cell.node.marks)
228
- };
229
- }
230
- const nextRows = [];
231
- const rowMetadata = [];
232
- for (let index = 0; index < rows.length; index++) {
233
- if (!elementAt(keepRows, index)) continue;
234
- const row = elementAt(rows, index);
235
- const rowCells = elementAt(placements, index).flatMap((placement) => placement === void 0 ? [] : [placement.node]);
236
- const kind = elementAt(rowKinds, index);
237
- const attrs = kind ? {
238
- ...row.attrs,
239
- [kind]: null
240
- } : row.attrs;
241
- nextRows.push(row.type.create(attrs, rowCells, row.marks));
242
- rowMetadata.push({
243
- touched: elementAt(rowPropertyTouched, index) || kind !== null,
244
- cells: elementAt(placements, index).flatMap((placement) => placement === void 0 ? [] : [placement])
245
- });
246
- }
247
- const nextTable = propertyResolvedTable.type.create(propertyResolvedTable.attrs, nextRows, propertyResolvedTable.marks);
248
- if (hasMerge || hasCellRevision) return resolvePureTableMerges({
249
- table: nextTable,
250
- mode,
251
- rowMetadata,
252
- tablePropertyTouched,
253
- sourceTable: tableWithResolvedChildren
254
- });
255
- const structural = hasRowRevision || hasCellRevision || hasTablePropertyRevision(tableWithResolvedChildren);
256
- return {
257
- node: nextTable,
258
- changed: structural || !nextTable.eq(table),
259
- structural,
260
- failed: false,
261
- ...tableResolutionMetadata({
262
- table: nextTable,
263
- rows: rowMetadata,
264
- tableTouched: tablePropertyTouched,
265
- sourceTable: tableWithResolvedChildren
266
- })
267
- };
268
- };
269
- const resolveTablePropertyAttrs = (table, mode) => {
270
- const rows = [];
271
- for (let rowIndex = 0; rowIndex < table.childCount; rowIndex++) {
272
- const row = table.child(rowIndex);
273
- const cells = [];
274
- for (let cellIndex = 0; cellIndex < row.childCount; cellIndex++) cells.push(resolveAllNodePropertyChangeAttrs(row.child(cellIndex), mode));
275
- rows.push(resolveAllNodePropertyChangeAttrs(row.type.create(row.attrs, cells, row.marks), mode));
276
- }
277
- return resolveAllNodePropertyChangeAttrs(table.type.create(table.attrs, rows, table.marks), mode);
278
- };
279
- const hasTablePropertyRevision = (table) => {
280
- if (table.attrs["tblPrChange"] != null) return true;
281
- for (let rowIndex = 0; rowIndex < table.childCount; rowIndex++) {
282
- const row = table.child(rowIndex);
283
- if (row.attrs["trPrChange"] != null || row.attrs["tblPrExChange"] != null) return true;
284
- for (let cellIndex = 0; cellIndex < row.childCount; cellIndex++) if (row.child(cellIndex).attrs["tcPrChange"] != null) return true;
285
- }
286
- return false;
287
- };
288
- const appendContinuation = (sequence, cell) => {
289
- const link = {
290
- cell,
291
- next: null
292
- };
293
- if (sequence.tail) sequence.tail.next = link;
294
- else sequence.head = link;
295
- sequence.tail = link;
296
- };
297
- const continuationSequence = (cells) => {
298
- const sequence = {
299
- head: null,
300
- tail: null
301
- };
302
- for (const cell of cells) appendContinuation(sequence, cell);
303
- return sequence;
304
- };
305
- const appendContinuationSequence = (destination, source) => {
306
- if (!source.head) return;
307
- if (destination.tail) destination.tail.next = source.head;
308
- else destination.head = source.head;
309
- destination.tail = source.tail;
310
- };
311
- const materializeContinuation = (entry) => {
312
- if (!entry.continuation) return;
313
- const cells = [];
314
- for (let link = entry.continuation.head; link !== null; link = link.next) cells.push(link.cell);
315
- entry.node = entry.node.type.create({
316
- ...entry.node.attrs,
317
- _docxVMergeContinuationCells: transportTableCellsWithParagraphPropertySources(cells)
318
- }, entry.node.content, entry.node.marks);
319
- entry.continuation = null;
320
- };
321
- const failedTableResolution = (table) => ({
322
- node: table,
323
- changed: false,
324
- structural: false,
325
- failed: true,
326
- changedParagraphRanges: [],
327
- paragraphOffsets: [],
328
- positionMap: StepMap.empty
329
- });
330
- /** Replay width deletions by live column rank without shifting the remaining array. */
331
- const compactColumnWidths = (widths, removals) => {
332
- if (removals.length === 0) return [...widths];
333
- const length = widths.length;
334
- const tree = new Int32Array(length + 1);
335
- const removed = new Uint8Array(length);
336
- for (let index = 1; index <= length; index++) tree[index] = index & -index;
337
- let highestBit = 1;
338
- while (highestBit * 2 <= length) highestBit *= 2;
339
- let remaining = length;
340
- for (const { left, count } of removals) {
341
- const start = Math.min(left, remaining);
342
- const removedCount = Math.min(count, remaining - start);
343
- for (let deleted = 0; deleted < removedCount; deleted++) {
344
- let rank = start + 1;
345
- let originalIndex = 0;
346
- for (let bit = highestBit; bit > 0; bit >>= 1) {
347
- const next = originalIndex + bit;
348
- if (next <= length && (tree[next] ?? 0) < rank) {
349
- originalIndex = next;
350
- rank -= tree[next] ?? 0;
351
- }
352
- }
353
- removed[originalIndex] = 1;
354
- for (let index = originalIndex + 1; index <= length; index += index & -index) tree[index] = (tree[index] ?? 0) - 1;
355
- remaining--;
356
- }
357
- }
358
- return widths.filter((_width, index) => removed[index] === 0);
359
- };
360
- /** Resolve all cell edits in reverse order against one grid and mutable slots. */
361
- const resolvePureTableMerges = ({ table, mode, rowMetadata, tablePropertyTouched, sourceTable }) => {
362
- const map = TableMap.get(table);
363
- const mergeDecisions = tableMergeFoldDecisions({
364
- table,
365
- mode,
366
- revisionSet: null
367
- });
368
- if (mergeDecisions.invalid.size > 0) return failedTableResolution(table);
369
- const cellRectangle = indexedTableCells(map);
370
- const rows = Array.from({ length: table.childCount }, (_, index) => table.child(index));
371
- const rowCells = rows.map(() => /* @__PURE__ */ new Map());
372
- const rowOccupancy = rows.map(() => 0);
373
- const activeRows = rows.map(() => true);
374
- const widthCounts = /* @__PURE__ */ new Map();
375
- const authoredWidths = expectTableAttrs(table).columnWidths;
376
- const columnWidths = authoredWidths?.length === map.width ? [...authoredWidths] : void 0;
377
- const widthRemovals = [];
378
- let currentWidth = map.width;
379
- let removedCell = false;
380
- const removedColumns = [];
381
- const deletedRows = /* @__PURE__ */ new Set();
382
- const byPosition = /* @__PURE__ */ new Map();
383
- const ordered = [];
384
- let rowOffset = 0;
385
- for (let rowIndex = 0; rowIndex < rows.length; rowIndex++) {
386
- const row = elementAt(rows, rowIndex);
387
- let cellOffset = rowOffset + 1;
388
- for (let cellIndex = 0; cellIndex < row.childCount; cellIndex++) {
389
- const node = row.child(cellIndex);
390
- const rectangle = cellRectangle(cellOffset);
391
- const left = rectangle.left;
392
- for (let coveredRow = rectangle.top; coveredRow < rectangle.bottom && !node.attrs["_omittedGridSlot"]; coveredRow++) rowOccupancy[coveredRow] = elementAt(rowOccupancy, coveredRow) + (rectangle.right - rectangle.left);
393
- const initial = elementAt(elementAt(rowMetadata, rowIndex).cells, cellIndex);
394
- const entry = {
395
- node: mergeDecisions.origins.has(cellOffset) ? node.type.create(resolvedTableMergeOriginAttrs(node), node.content, node.marks) : node,
396
- position: cellOffset,
397
- left,
398
- row: rowIndex,
399
- sourcePosition: initial.sourcePosition,
400
- mapSourcePosition: initial.mapSourcePosition,
401
- touched: initial.touched,
402
- continuation: null
403
- };
404
- elementAt(rowCells, rowIndex).set(left, entry);
405
- byPosition.set(cellOffset, entry);
406
- ordered.push(entry);
407
- cellOffset += node.nodeSize;
408
- }
409
- rowOffset += row.nodeSize;
410
- }
411
- for (const width of rowOccupancy) widthCounts.set(width, (widthCounts.get(width) ?? 0) + 1);
412
- const spanOf = ({ node }) => Number(node.attrs["colspan"]) || 1;
413
- const omittedIn = (rowIndex) => [...elementAt(rowCells, rowIndex).values()].filter(({ node }) => node.attrs["_omittedGridSlot"] != null);
414
- currentWidth = maxOf(rowOccupancy, 0);
415
- const changeRowWidth = (rowIndex, nextWidth) => {
416
- if (!elementAt(activeRows, rowIndex)) return;
417
- const previous = elementAt(rowOccupancy, rowIndex);
418
- widthCounts.set(previous, (widthCounts.get(previous) ?? 0) - 1);
419
- rowOccupancy[rowIndex] = nextWidth;
420
- widthCounts.set(nextWidth, (widthCounts.get(nextWidth) ?? 0) + 1);
421
- };
422
- for (let index = ordered.length - 1; index >= 0; index--) {
423
- const entry = elementAt(ordered, index);
424
- const marker = cellRevisionMarker(entry.node);
425
- if (marker && marker.kind !== "merge") {
426
- const view = mode === "accept" ? "final" : "original";
427
- if (isTableCellRetainedInReviewView(marker.kind, view)) {
428
- entry.touched = true;
429
- entry.node = entry.node.type.create({
430
- ...entry.node.attrs,
431
- cellMarker: null
432
- }, entry.node.content, entry.node.marks);
433
- } else {
434
- removedCell = true;
435
- removedColumns.push(entry.left);
436
- const previousWidth = currentWidth;
437
- const rectangle = cellRectangle(entry.position);
438
- const colspan = rectangle.right - rectangle.left;
439
- for (let coveredRow = rectangle.top; coveredRow < rectangle.bottom; coveredRow++) changeRowWidth(coveredRow, elementAt(rowOccupancy, coveredRow) - colspan);
440
- elementAt(rowCells, entry.row).delete(entry.left);
441
- if ([...elementAt(rowCells, entry.row).values()].every(({ node }) => node.attrs["_omittedGridSlot"] != null)) {
442
- deletedRows.add(entry.row);
443
- activeRows[entry.row] = false;
444
- const rowWidth = elementAt(rowOccupancy, entry.row);
445
- widthCounts.set(rowWidth, (widthCounts.get(rowWidth) ?? 0) - 1);
446
- }
447
- while (currentWidth > 0 && (widthCounts.get(currentWidth) ?? 0) === 0) currentWidth--;
448
- if (columnWidths && currentWidth < previousWidth) widthRemovals.push({
449
- left: entry.left,
450
- count: previousWidth - currentWidth
451
- });
452
- }
453
- } else if (marker?.kind === "merge") {
454
- const joinsAbove = mergeDecisions.folds.get(entry.position);
455
- if (joinsAbove === void 0) return panic("Merge resolution lost its cell");
456
- if (mode === "reject" && isTableCellMergeRevisionContinuation(marker.verticalMergeOriginal) && cellRectangle(entry.position).top === 0) return failedTableResolution(table);
457
- if (!joinsAbove) entry.node = entry.node.type.create(resolvedVisibleTableCellMergeAttrs({
458
- cell: entry.node,
459
- mode,
460
- origin: mergeDecisions.origins.has(entry.position)
461
- }), entry.node.content, entry.node.marks);
462
- else {
463
- const rectangle = cellRectangle(entry.position);
464
- if (rectangle.top === 0) return failedTableResolution(table);
465
- const abovePosition = map.map[(rectangle.top - 1) * map.width + rectangle.left];
466
- if (abovePosition === void 0) return failedTableResolution(table);
467
- const aboveRectangle = cellRectangle(abovePosition);
468
- const above = byPosition.get(abovePosition);
469
- if (!above || !elementAt(rowCells, above.row).has(above.left) || aboveRectangle.left !== rectangle.left || aboveRectangle.right !== rectangle.right || aboveRectangle.bottom !== rectangle.top) return failedTableResolution(table);
470
- const aboveRowspan = above.node.attrs["rowspan"];
471
- const cellRowspan = entry.node.attrs["rowspan"];
472
- if (typeof aboveRowspan !== "number" || typeof cellRowspan !== "number" || aboveRowspan < 1 || cellRowspan < 1) return failedTableResolution(table);
473
- above.continuation ??= continuationSequence(tableCellContinuationCells(above.node, aboveRowspan));
474
- appendContinuation(above.continuation, tableCellContinuationFromNode(entry.node));
475
- const nested = entry.continuation ?? continuationSequence(tableCellContinuationPayload(entry.node)?.cells ?? []);
476
- appendContinuationSequence(above.continuation, nested);
477
- above.node = above.node.type.create({
478
- ...above.node.attrs,
479
- rowspan: aboveRowspan + cellRowspan
480
- }, above.node.content, above.node.marks);
481
- elementAt(rowCells, entry.row).delete(entry.left);
482
- }
483
- }
484
- if (!elementAt(rowCells, entry.row).has(entry.left)) continue;
485
- if (!tableCellContinuationPayload(entry.node)?.cells.some((cell) => getTableCellMergeChange(cell) !== null)) continue;
486
- materializeContinuation(entry);
487
- const payload = tableCellContinuationPayload(entry.node);
488
- if (!payload) return failedTableResolution(table);
489
- const stored = payload.cells;
490
- const splitIndices = [];
491
- let matched = false;
492
- const nextCells = stored.map((source, sourceIndex) => {
493
- const change = getTableCellMergeChange(source);
494
- if (!change) return source;
495
- matched = true;
496
- if (mode === "reject" && isTableCellMergeRevisionContinuation(change.verticalMerge) && !isTableCellMergeRevisionContinuation(change.verticalMergeOriginal)) splitIndices.push(sourceIndex);
497
- const cleaned = { ...source };
498
- delete cleaned.structuralChange;
499
- return cleaned;
500
- });
501
- if (!matched) continue;
502
- if (mode === "accept" || splitIndices.length === 0) {
503
- entry.touched = true;
504
- entry.node = entry.node.type.create({
505
- ...entry.node.attrs,
506
- _docxVMergeContinuationCells: nextCells
507
- }, entry.node.content, entry.node.marks);
508
- continue;
509
- }
510
- const firstSplitIndex = splitIndices.at(0);
511
- if (firstSplitIndex === void 0 || splitIndices.length !== stored.length - firstSplitIndex || splitIndices.some((splitIndex, offset) => splitIndex !== firstSplitIndex + offset)) return failedTableResolution(table);
512
- const rectangle = cellRectangle(entry.position);
513
- const rowspan = entry.node.attrs["rowspan"];
514
- if (typeof rowspan !== "number" || rowspan !== stored.length + 1 || rectangle.bottom - rectangle.top !== rowspan) return failedTableResolution(table);
515
- const restored = restoreTableCellsWithParagraphPropertySources(payload);
516
- for (const splitIndex of splitIndices) {
517
- const source = restored[splitIndex];
518
- const restoredCell = source && createRestoredTableCell(entry.node, source);
519
- const rowIndex = rectangle.top + splitIndex + 1;
520
- if (!restoredCell || elementAt(rowCells, rowIndex).has(rectangle.left)) return failedTableResolution(table);
521
- elementAt(rowCells, rowIndex).set(rectangle.left, {
522
- node: restoredCell,
523
- position: -1,
524
- left: rectangle.left,
525
- row: rowIndex,
526
- sourcePosition: -1,
527
- mapSourcePosition: -1,
528
- touched: true,
529
- continuation: null
530
- });
531
- }
532
- const remaining = nextCells.slice(0, firstSplitIndex);
533
- entry.node = entry.node.type.create({
534
- ...entry.node.attrs,
535
- rowspan: firstSplitIndex + 1,
536
- _docxVMergeContinuationCells: remaining.length > 0 ? remaining : null
537
- }, entry.node.content, entry.node.marks);
538
- }
539
- for (let rowIndex = 0; rowIndex < rows.length; rowIndex++) {
540
- if (deletedRows.has(rowIndex)) continue;
541
- const slots = omittedIn(rowIndex);
542
- let excess = elementAt(rowOccupancy, rowIndex) + slots.reduce((total, slot) => total + spanOf(slot), 0) - currentWidth;
543
- const atRemoval = ({ left, node }) => removedColumns.some((column) => column >= left && column < left + (Number(node.attrs["colspan"]) || 1));
544
- const order = [...slots.filter(atRemoval), ...slots.filter((slot) => !atRemoval(slot)).toReversed()];
545
- for (const slot of order) {
546
- if (excess <= 0) break;
547
- const span = spanOf(slot);
548
- if (span <= excess) {
549
- elementAt(rowCells, rowIndex).delete(slot.left);
550
- removedCell = true;
551
- excess -= span;
552
- continue;
553
- }
554
- slot.touched = true;
555
- slot.node = slot.node.type.create({
556
- ...slot.node.attrs,
557
- colspan: span - excess
558
- }, slot.node.content, slot.node.marks);
559
- excess = 0;
560
- }
561
- }
562
- if (deletedRows.size === rows.length) return {
563
- node: null,
564
- changed: true,
565
- structural: true,
566
- failed: false,
567
- changedParagraphRanges: [],
568
- paragraphOffsets: [],
569
- positionMap: new StepMap([
570
- 0,
571
- sourceTable.nodeSize,
572
- 0
573
- ])
574
- };
575
- const keptBefore = [0];
576
- for (let rowIndex = 0; rowIndex < rows.length; rowIndex++) keptBefore.push((keptBefore.at(-1) ?? 0) + Number(!deletedRows.has(rowIndex)));
577
- const compacted = rows.map(() => /* @__PURE__ */ new Map());
578
- for (let rowIndex = 0; rowIndex < rows.length; rowIndex++) for (const entry of elementAt(rowCells, rowIndex).values()) {
579
- materializeContinuation(entry);
580
- const originalRowspan = entry.node.attrs["rowspan"];
581
- if (typeof originalRowspan !== "number") continue;
582
- const bottom = Math.min(rowIndex + originalRowspan, rows.length);
583
- let destination = rowIndex;
584
- while (destination < bottom && deletedRows.has(destination)) destination++;
585
- if (destination === bottom) continue;
586
- const rowspan = elementAt(keptBefore, bottom) - elementAt(keptBefore, destination);
587
- const node = rowspan === originalRowspan ? entry.node : entry.node.type.create({
588
- ...entry.node.attrs,
589
- rowspan
590
- }, entry.node.content, entry.node.marks);
591
- elementAt(compacted, destination).set(entry.left, {
592
- ...entry,
593
- node,
594
- row: destination,
595
- touched: entry.touched || rowspan !== originalRowspan
596
- });
597
- }
598
- const nextRowMetadata = [];
599
- const nextRows = rows.flatMap((row, rowIndex) => {
600
- if (deletedRows.has(rowIndex)) return [];
601
- const cells = [];
602
- const cellMetadata = [];
603
- for (let column = 0; column < map.width; column++) {
604
- const entry = elementAt(compacted, rowIndex).get(column);
605
- if (entry) {
606
- cells.push(entry.node);
607
- cellMetadata.push(entry);
608
- }
609
- }
610
- nextRowMetadata.push({
611
- touched: elementAt(rowMetadata, rowIndex).touched,
612
- cells: cellMetadata
613
- });
614
- return [row.type.create(row.attrs, cells, row.marks)];
615
- });
616
- let nextTable = table.type.create(table.attrs, nextRows, table.marks);
617
- const nextWidth = TableMap.get(nextTable).width;
618
- const gridChanged = removedCell && nextWidth < map.width;
619
- if (gridChanged) {
620
- const survivingColumnWidths = columnWidths === void 0 ? void 0 : compactColumnWidths(columnWidths, widthRemovals);
621
- const formatting = expectTableAttrs(nextTable)._originalFormatting;
622
- const originalFormatting = formatting ? { ...formatting } : void 0;
623
- if (originalFormatting) delete originalFormatting.gridSourceXml;
624
- nextTable = nextTable.type.create(mergeTableAttrs(nextTable, {
625
- columnWidths: survivingColumnWidths?.length === nextWidth ? survivingColumnWidths : void 0,
626
- _originalFormatting: originalFormatting
627
- }), nextTable.content, nextTable.marks);
628
- }
629
- return {
630
- node: nextTable,
631
- changed: true,
632
- structural: true,
633
- failed: false,
634
- ...tableResolutionMetadata({
635
- table: nextTable,
636
- rows: nextRowMetadata,
637
- tableTouched: tablePropertyTouched || gridChanged,
638
- sourceTable
639
- })
640
- };
641
- };
642
- export { resolveAllTableChanges };
@@ -1,13 +0,0 @@
1
- import { NodeAttrsPropertyRevisionSite } from "../revisionCarriers.js";
2
- import { Node } from "prosemirror-model";
3
- /** Resolve one property-change site without a transaction. */
4
- type ResolveNodePropertyChangeAttrsOptions = {
5
- node: Node;
6
- site: NodeAttrsPropertyRevisionSite;
7
- mode: "accept" | "reject";
8
- revisionSet: ReadonlySet<number> | null;
9
- };
10
- export declare const resolveNodePropertyChangeAttrs: ({ node, site, mode, revisionSet }: ResolveNodePropertyChangeAttrsOptions) => Record<string, unknown> | null;
11
- /** Resolve every node-attr site, including both independent sites on a row. */
12
- export declare const resolveAllNodePropertyChangeAttrs: (node: Node, mode: "accept" | "reject") => Node;
13
- export {};