@stll/docx-core 0.29.0 → 0.30.1
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.
- package/dist/docx_kernel_bg.wasm +0 -0
- package/dist/index.d.ts +50 -1
- package/dist/index.js +205 -3
- package/dist/ops/documentOps.d.ts +115 -28
- package/dist/ops/documentOps.js +1498 -573
- package/package.json +1 -1
package/dist/ops/documentOps.js
CHANGED
|
@@ -40,13 +40,16 @@ const equalForStaleness = (left, right) => equal(left, right, derivedFields);
|
|
|
40
40
|
/**
|
|
41
41
|
* The operation schema this module reads and writes.
|
|
42
42
|
*
|
|
43
|
-
* Version
|
|
43
|
+
* Version 4 adds paragraph deletion through `deleteBlocks`, direct and tracked
|
|
44
|
+
* whole-table operations with their exact structural inverse, terminal insertion,
|
|
45
|
+
* and cell-ending paragraph marks on tracked row operations.
|
|
46
|
+
* Version 3 added paragraph insertion through `insertBlocks` and the table-row
|
|
44
47
|
* operations `insertRow`, `deleteRow` and `setTableRows`.
|
|
45
48
|
* Version 2 added tracked changes: the `revision` stamp on the text, formatting
|
|
46
49
|
* and paragraph operations, and the review operations `setParagraphReview`,
|
|
47
50
|
* `replaceInline` and `resolveRevision`.
|
|
48
51
|
*/
|
|
49
|
-
const DOCUMENT_OP_SCHEMA_VERSION =
|
|
52
|
+
const DOCUMENT_OP_SCHEMA_VERSION = 4;
|
|
50
53
|
/**
|
|
51
54
|
* The stories an operation can address. Headers, footers, notes and comment
|
|
52
55
|
* bodies are stories too; this version addresses the main story only.
|
|
@@ -66,8 +69,9 @@ const EMPTY_PROPERTY_SETS = Object.freeze({
|
|
|
66
69
|
* unit before the position, or the one after it at the start of a paragraph.
|
|
67
70
|
*/
|
|
68
71
|
const INHERIT_RUN_PROPS = "inherit";
|
|
69
|
-
/** The operation kinds of schema version
|
|
72
|
+
/** The operation kinds of schema version 4. */
|
|
70
73
|
const DOCUMENT_OP_TYPES = Object.freeze({
|
|
74
|
+
DELETE_BLOCKS: "deleteBlocks",
|
|
71
75
|
INSERT_BLOCKS: "insertBlocks",
|
|
72
76
|
INSERT_TEXT: "insertText",
|
|
73
77
|
INSERT_CONTENT: "insertContent",
|
|
@@ -82,6 +86,9 @@ const DOCUMENT_OP_TYPES = Object.freeze({
|
|
|
82
86
|
SET_PARAGRAPH_REVIEW: "setParagraphReview",
|
|
83
87
|
REPLACE_INLINE: "replaceInline",
|
|
84
88
|
RESOLVE_REVISION: "resolveRevision",
|
|
89
|
+
INSERT_TABLE: "insertTable",
|
|
90
|
+
DELETE_TABLE: "deleteTable",
|
|
91
|
+
SET_CONTAINER_BLOCKS: "setContainerBlocks",
|
|
85
92
|
INSERT_ROW: "insertRow",
|
|
86
93
|
DELETE_ROW: "deleteRow",
|
|
87
94
|
SET_TABLE_ROWS: "setTableRows"
|
|
@@ -104,7 +111,7 @@ const REVISION_DECISIONS = Object.freeze({
|
|
|
104
111
|
});
|
|
105
112
|
/** An operation in the envelope it is journaled and sent in. */
|
|
106
113
|
const toOpEnvelope = (op) => ({
|
|
107
|
-
schema:
|
|
114
|
+
schema: 4,
|
|
108
115
|
op
|
|
109
116
|
});
|
|
110
117
|
//#endregion
|
|
@@ -804,6 +811,12 @@ const maskIdentity = (node) => {
|
|
|
804
811
|
* the gaps" and an insertion is "cut at a gap and merge the new leaves in",
|
|
805
812
|
* each is the other's exact inverse, whatever the records around them hold.
|
|
806
813
|
*/
|
|
814
|
+
const COMMENT_ANCHORS = new Set(Object.keys({
|
|
815
|
+
commentRangeStart: true,
|
|
816
|
+
commentRangeEnd: true,
|
|
817
|
+
commentReference: true
|
|
818
|
+
}));
|
|
819
|
+
const isCommentAnchor = (node) => COMMENT_ANCHORS.has(node.type);
|
|
807
820
|
const RUN_CONTENT_TYPES = {
|
|
808
821
|
text: true,
|
|
809
822
|
tab: true,
|
|
@@ -1582,6 +1595,8 @@ const markFormattingOf = (formatting) => {
|
|
|
1582
1595
|
};
|
|
1583
1596
|
/** Whether two property sets state the same paragraph properties. */
|
|
1584
1597
|
const sameParagraphProperties = (left, right) => structurallyEqual(paragraphPropertiesOf(left) ?? {}, paragraphPropertiesOf(right) ?? {});
|
|
1598
|
+
/** Whether two property sets state the same run properties for the paragraph mark. */
|
|
1599
|
+
const sameMarkFormatting = (left, right) => structurallyEqual(markFormattingOf(left) ?? {}, markFormattingOf(right) ?? {});
|
|
1585
1600
|
/**
|
|
1586
1601
|
* The paragraph properties of `properties` with the mark's run properties of
|
|
1587
1602
|
* `marks`. `undefined` when the first is absent and the second states no mark
|
|
@@ -1629,12 +1644,6 @@ const WRAP_KINDS = Object.freeze({
|
|
|
1629
1644
|
INSERTION: "insertion",
|
|
1630
1645
|
DELETION: "deletion"
|
|
1631
1646
|
});
|
|
1632
|
-
/** Comment boundaries and references: no tracked change can hold them. */
|
|
1633
|
-
const COMMENT_ANCHORS$1 = /* @__PURE__ */ new Set([
|
|
1634
|
-
"commentRangeStart",
|
|
1635
|
-
"commentRangeEnd",
|
|
1636
|
-
"commentReference"
|
|
1637
|
-
]);
|
|
1638
1647
|
const spanningAction = (node, kind, stamp) => {
|
|
1639
1648
|
if (node.type === "inlineSdt" || node.type === "inlineWrapper") return "descend";
|
|
1640
1649
|
if (isTrackedWrapper(node) && isAddedRevision(node)) {
|
|
@@ -1645,7 +1654,7 @@ const spanningAction = (node, kind, stamp) => {
|
|
|
1645
1654
|
return "cut";
|
|
1646
1655
|
};
|
|
1647
1656
|
const coveredAction = (node, kind) => {
|
|
1648
|
-
if (
|
|
1657
|
+
if (isCommentAnchor(node)) return "refuse";
|
|
1649
1658
|
if (kind === WRAP_KINDS.DELETION) {
|
|
1650
1659
|
if (isRemovedRevisionNode(node)) return "skip";
|
|
1651
1660
|
if (isAddedRevision(node)) return "descend";
|
|
@@ -1831,6 +1840,130 @@ const wrapTracked = ({ items, from, to, kind, stamp }) => {
|
|
|
1831
1840
|
};
|
|
1832
1841
|
};
|
|
1833
1842
|
//#endregion
|
|
1843
|
+
//#region src/ops/blockDeletion.ts
|
|
1844
|
+
/** Whole-paragraph deletion within one block list, including its final paragraph. */
|
|
1845
|
+
const deleteBlocks = ({ document, op, applyOps }) => {
|
|
1846
|
+
const refuse = (reason, message) => Result.err(new DocumentOpRefusal({
|
|
1847
|
+
reason,
|
|
1848
|
+
message,
|
|
1849
|
+
opType: op.type
|
|
1850
|
+
}));
|
|
1851
|
+
if (op.blockIds.length === 0) return refuse(DOCUMENT_OP_REFUSAL_REASONS.EMPTY_BLOCK_LIST, "No paragraphs to delete.");
|
|
1852
|
+
const body = storyBody(document, op.story);
|
|
1853
|
+
const paragraphs = storyParagraphs(body);
|
|
1854
|
+
const found = [];
|
|
1855
|
+
for (const id of op.blockIds) {
|
|
1856
|
+
const location = paragraphs.find(({ paragraph }) => idKey(paragraph.paraId ?? "") === idKey(id));
|
|
1857
|
+
if (location === void 0) return refuse(DOCUMENT_OP_REFUSAL_REASONS.BLOCK_NOT_FOUND, "A selected paragraph does not exist.");
|
|
1858
|
+
found.push(location);
|
|
1859
|
+
}
|
|
1860
|
+
const first = found.at(0);
|
|
1861
|
+
const last = found.at(-1);
|
|
1862
|
+
if (first === void 0 || last === void 0) return refuse(DOCUMENT_OP_REFUSAL_REASONS.EMPTY_BLOCK_LIST, "No paragraphs to delete.");
|
|
1863
|
+
if (!found.every((location, index) => sameBlockList(location.list, first.list) && location.index === first.index + index)) return refuse(DOCUMENT_OP_REFUSAL_REASONS.NOT_ADJACENT, "Selected paragraphs must be adjacent in one block list.");
|
|
1864
|
+
const list = blockListAt(body.content, first.list);
|
|
1865
|
+
const following = list[last.index + 1];
|
|
1866
|
+
const terminal = last.index === list.length - 1;
|
|
1867
|
+
if (following?.type !== "paragraph" && !terminal) return refuse(DOCUMENT_OP_REFUSAL_REASONS.UNTRACKABLE, "Deletion requires a following paragraph or a container-final paragraph.");
|
|
1868
|
+
const preceding = terminal ? list[first.index - 1] : void 0;
|
|
1869
|
+
const previous = preceding?.type === "paragraph" ? preceding : void 0;
|
|
1870
|
+
const selected = found.map(({ paragraph }) => paragraph);
|
|
1871
|
+
const affected = previous === void 0 ? selected : [previous, ...selected];
|
|
1872
|
+
if (affected.some(({ sectionProperties }) => sectionProperties !== void 0)) return refuse(DOCUMENT_OP_REFUSAL_REASONS.UNTRACKABLE, "Deleting section boundaries requires section operations.");
|
|
1873
|
+
const replacement = (blocks) => ({
|
|
1874
|
+
type: DOCUMENT_OP_TYPES.REPLACE_BLOCKS,
|
|
1875
|
+
story: op.story,
|
|
1876
|
+
expected: following?.type === "paragraph" ? [...selected, following] : affected,
|
|
1877
|
+
blocks
|
|
1878
|
+
});
|
|
1879
|
+
const finalReview = reviewFieldsOf(last.paragraph);
|
|
1880
|
+
if (previous !== void 0 && paragraphLength(previous) > 0) {
|
|
1881
|
+
const formatting = withMarkFormatting(paragraphPropertiesOf(previous.formatting), last.paragraph.formatting);
|
|
1882
|
+
delete finalReview.formatting;
|
|
1883
|
+
if (formatting !== void 0) finalReview.formatting = formatting;
|
|
1884
|
+
}
|
|
1885
|
+
const survivor = withReviewFields({
|
|
1886
|
+
...last.paragraph,
|
|
1887
|
+
content: previous?.content ?? []
|
|
1888
|
+
}, finalReview);
|
|
1889
|
+
const direct = applyOps(document, [replacement(following?.type === "paragraph" ? [following] : [survivor])]);
|
|
1890
|
+
if (direct.isErr()) return refuse(direct.error.reason, direct.error.message);
|
|
1891
|
+
if (op.revision === void 0) return Result.ok({
|
|
1892
|
+
...direct.value,
|
|
1893
|
+
touched: {
|
|
1894
|
+
modified: terminal ? [last.paragraph.paraId ?? ""] : [],
|
|
1895
|
+
inserted: [],
|
|
1896
|
+
removed: affected.filter((paragraph) => !terminal || paragraph !== last.paragraph).map(({ paraId }) => paraId ?? "")
|
|
1897
|
+
}
|
|
1898
|
+
});
|
|
1899
|
+
if (affected.some(({ pPrMark, propertyChanges }) => pPrMark !== void 0 || (propertyChanges?.length ?? 0) > 0)) return refuse(DOCUMENT_OP_REFUSAL_REASONS.REVISION_CONFLICT, "Deletion would overwrite a paragraph review record.");
|
|
1900
|
+
if (identityKeysIn(selected.map(({ content }) => ({ content }))).length > 0) return refuse(DOCUMENT_OP_REFUSAL_REASONS.UNTRACKABLE, "Tracked paragraph deletion of identified review or content-control content is unsupported.");
|
|
1901
|
+
const selectedSet = new Set(selected);
|
|
1902
|
+
const tracked = [];
|
|
1903
|
+
for (const paragraph of affected) {
|
|
1904
|
+
let next = paragraph;
|
|
1905
|
+
if (selectedSet.has(paragraph)) {
|
|
1906
|
+
const wrapped = wrapTracked({
|
|
1907
|
+
items: paragraph.content,
|
|
1908
|
+
from: {
|
|
1909
|
+
offset: 0,
|
|
1910
|
+
zeroWidthBefore: 0
|
|
1911
|
+
},
|
|
1912
|
+
to: {
|
|
1913
|
+
offset: paragraphLength(paragraph),
|
|
1914
|
+
zeroWidthBefore: Number.MAX_SAFE_INTEGER
|
|
1915
|
+
},
|
|
1916
|
+
kind: WRAP_KINDS.DELETION,
|
|
1917
|
+
stamp: op.revision
|
|
1918
|
+
});
|
|
1919
|
+
if (wrapped.kind === "refused") return refuse(wrapped.reason, "The selected paragraph content cannot be tracked.");
|
|
1920
|
+
if (wrapped.kind === "wrapped") next = {
|
|
1921
|
+
...paragraph,
|
|
1922
|
+
content: wrapped.content
|
|
1923
|
+
};
|
|
1924
|
+
}
|
|
1925
|
+
if (!terminal || paragraph !== last.paragraph) next = {
|
|
1926
|
+
...next,
|
|
1927
|
+
pPrMark: {
|
|
1928
|
+
kind: "del",
|
|
1929
|
+
info: stampInfo(op.revision)
|
|
1930
|
+
}
|
|
1931
|
+
};
|
|
1932
|
+
else if (!sameParagraphProperties(next.formatting, survivor.formatting)) {
|
|
1933
|
+
const review = reviewFieldsOf(next);
|
|
1934
|
+
delete review.formatting;
|
|
1935
|
+
if (survivor.formatting !== void 0) review.formatting = survivor.formatting;
|
|
1936
|
+
review.propertyChanges = [paragraphPropertyChange(stampInfo(op.revision), next.formatting)];
|
|
1937
|
+
next = withReviewFields(next, review);
|
|
1938
|
+
}
|
|
1939
|
+
tracked.push(next);
|
|
1940
|
+
}
|
|
1941
|
+
const beforeKeys = new Set(identityKeysIn(affected));
|
|
1942
|
+
const fresh = freshenIdentities({
|
|
1943
|
+
before: affected,
|
|
1944
|
+
after: tracked,
|
|
1945
|
+
newIds: op.newIds ?? {},
|
|
1946
|
+
usedElsewhere: () => new Set(packageIdentityKeys(document.package).filter((key) => !beforeKeys.has(key)))
|
|
1947
|
+
});
|
|
1948
|
+
switch (fresh.kind) {
|
|
1949
|
+
case "needsIds": return refuse(DOCUMENT_OP_REFUSAL_REASONS.NEEDS_NEW_IDS, "Paragraph deletion needs more revision ids.");
|
|
1950
|
+
case "invalidId": return refuse(DOCUMENT_OP_REFUSAL_REASONS.INVALID_NEW_ID, "Paragraph deletion received an invalid revision id.");
|
|
1951
|
+
case "fresh": {
|
|
1952
|
+
const result = applyOps(document, [replacement(following?.type === "paragraph" ? [...fresh.paragraphs, following] : fresh.paragraphs)]);
|
|
1953
|
+
if (result.isErr()) return refuse(result.error.reason, result.error.message);
|
|
1954
|
+
return Result.ok({
|
|
1955
|
+
...result.value,
|
|
1956
|
+
touched: {
|
|
1957
|
+
modified: tracked.filter((paragraph, index) => paragraph !== affected[index]).map(({ paraId }) => paraId ?? ""),
|
|
1958
|
+
inserted: [],
|
|
1959
|
+
removed: []
|
|
1960
|
+
}
|
|
1961
|
+
});
|
|
1962
|
+
}
|
|
1963
|
+
default: return fresh;
|
|
1964
|
+
}
|
|
1965
|
+
};
|
|
1966
|
+
//#endregion
|
|
1834
1967
|
//#region src/ops/blockInsertion.ts
|
|
1835
1968
|
/** Paragraph insertion, isolated from the inline and table operation implementations. */
|
|
1836
1969
|
const insertBlocks = ({ document, op, applyOps }) => {
|
|
@@ -2563,29 +2696,24 @@ const patchRunsBetween = (items, from, to, patch, whenEmpty, decorate) => {
|
|
|
2563
2696
|
};
|
|
2564
2697
|
};
|
|
2565
2698
|
//#endregion
|
|
2566
|
-
//#region src/ops/
|
|
2567
|
-
/**
|
|
2568
|
-
const refused = ({ op, reason, message }) => Result.err(new DocumentOpRefusal({
|
|
2569
|
-
opType: op.type,
|
|
2570
|
-
reason,
|
|
2571
|
-
message
|
|
2572
|
-
}));
|
|
2699
|
+
//#region src/ops/tableLocation.ts
|
|
2700
|
+
/** Shared addressing for row and whole-table operations. */
|
|
2573
2701
|
/** A paragraph selects its innermost table, even through block wrappers. */
|
|
2574
2702
|
const locateTableRow = (document, op) => {
|
|
2575
2703
|
const body = storyBody(document, op.story);
|
|
2576
2704
|
const location = storyParagraphs(body).find(({ paragraph }) => idKey(paragraph.paraId ?? "") === idKey(op.blockId));
|
|
2577
|
-
if (location === void 0) return
|
|
2578
|
-
op,
|
|
2705
|
+
if (location === void 0) return Result.err(new DocumentOpRefusal({
|
|
2706
|
+
opType: op.type,
|
|
2579
2707
|
reason: DOCUMENT_OP_REFUSAL_REASONS.BLOCK_NOT_FOUND,
|
|
2580
2708
|
message: `No paragraph is ${op.blockId}.`
|
|
2581
|
-
});
|
|
2709
|
+
}));
|
|
2582
2710
|
const stepIndex = location.list.findLastIndex((step) => step.kind === "tableCell");
|
|
2583
2711
|
const step = location.list[stepIndex];
|
|
2584
|
-
if (step?.kind !== "tableCell") return
|
|
2585
|
-
op,
|
|
2712
|
+
if (step?.kind !== "tableCell") return Result.err(new DocumentOpRefusal({
|
|
2713
|
+
opType: op.type,
|
|
2586
2714
|
reason: DOCUMENT_OP_REFUSAL_REASONS.STRUCTURE_MISMATCH,
|
|
2587
2715
|
message: "The paragraph is not in a table row."
|
|
2588
|
-
});
|
|
2716
|
+
}));
|
|
2589
2717
|
const list = location.list.slice(0, stepIndex);
|
|
2590
2718
|
const table = blockListAt(body.content, list)[step.block];
|
|
2591
2719
|
if (table?.type !== "table") return panic("A table-cell path must name a table.");
|
|
@@ -2596,419 +2724,83 @@ const locateTableRow = (document, op) => {
|
|
|
2596
2724
|
table
|
|
2597
2725
|
});
|
|
2598
2726
|
};
|
|
2599
|
-
|
|
2727
|
+
/** Row operations address this table, never a nested table appearing first. */
|
|
2728
|
+
const tableRowAnchor = (rows) => storyParagraphs({ content: [{
|
|
2600
2729
|
type: "table",
|
|
2601
2730
|
rows: [...rows]
|
|
2602
|
-
}] }).
|
|
2603
|
-
|
|
2604
|
-
|
|
2605
|
-
|
|
2606
|
-
|
|
2607
|
-
|
|
2608
|
-
case "blockCustomXml": return {
|
|
2609
|
-
...block,
|
|
2610
|
-
content: mapParagraphs(block.content, replace)
|
|
2611
|
-
};
|
|
2612
|
-
case "table": return {
|
|
2613
|
-
...block,
|
|
2614
|
-
rows: block.rows.map((row) => ({
|
|
2615
|
-
...row,
|
|
2616
|
-
cells: row.cells.map((cell) => ({
|
|
2617
|
-
...cell,
|
|
2618
|
-
content: mapParagraphs(cell.content, replace)
|
|
2619
|
-
}))
|
|
2620
|
-
}))
|
|
2621
|
-
};
|
|
2622
|
-
case "preservedBlock":
|
|
2623
|
-
case "bookmarkStart":
|
|
2624
|
-
case "bookmarkEnd": return block;
|
|
2625
|
-
default: return block;
|
|
2626
|
-
}
|
|
2627
|
-
});
|
|
2628
|
-
const rowWithParagraphs = (row, replace) => ({
|
|
2629
|
-
...row,
|
|
2630
|
-
cells: row.cells.map((cell) => ({
|
|
2631
|
-
...cell,
|
|
2632
|
-
content: mapParagraphs(cell.content, replace)
|
|
2633
|
-
}))
|
|
2634
|
-
});
|
|
2635
|
-
/** Indexed markup and shared row wrappers need a structural table operation. */
|
|
2636
|
-
const needsTableEdit = (table) => table.preserved !== void 0 || table.bookmarks !== void 0 || table.carrierStack !== void 0 || table.rows.some((row) => row.contentControls !== void 0 || row.carrierStack !== void 0);
|
|
2637
|
-
/** Nested tables, captured blocks and cell revisions have no tracked-row construction yet. */
|
|
2638
|
-
const trackableBlocks = (blocks) => blocks.every((block) => {
|
|
2639
|
-
switch (block.type) {
|
|
2640
|
-
case "paragraph": return block.sectionProperties === void 0;
|
|
2641
|
-
case "blockSdt":
|
|
2642
|
-
case "blockCustomXml": return trackableBlocks(block.content);
|
|
2731
|
+
}] }).find(({ list }) => list.filter((step) => step.kind === "tableCell").length === 1)?.paragraph.paraId;
|
|
2732
|
+
//#endregion
|
|
2733
|
+
//#region src/ops/resolveTableRows.ts
|
|
2734
|
+
/** Row resolution after inline review, before paragraph-mark joins. */
|
|
2735
|
+
const collectTables = (blocks, out) => {
|
|
2736
|
+
for (const block of blocks) switch (block.type) {
|
|
2643
2737
|
case "table":
|
|
2738
|
+
for (const row of block.rows) for (const cell of row.cells) collectTables(cell.content, out);
|
|
2739
|
+
out.push(block);
|
|
2740
|
+
break;
|
|
2741
|
+
case "blockSdt":
|
|
2742
|
+
case "blockCustomXml":
|
|
2743
|
+
collectTables(block.content, out);
|
|
2744
|
+
break;
|
|
2745
|
+
case "paragraph":
|
|
2644
2746
|
case "preservedBlock":
|
|
2645
2747
|
case "bookmarkStart":
|
|
2646
|
-
case "bookmarkEnd":
|
|
2748
|
+
case "bookmarkEnd": break;
|
|
2647
2749
|
default: return block;
|
|
2648
2750
|
}
|
|
2649
|
-
});
|
|
2650
|
-
const trackRow = ({ document, op, row, revision, newIds, kind }) => {
|
|
2651
|
-
if (row.structuralChange !== void 0) return refused({
|
|
2652
|
-
op,
|
|
2653
|
-
reason: DOCUMENT_OP_REFUSAL_REASONS.REVISION_CONFLICT,
|
|
2654
|
-
message: "The row already carries a structural revision."
|
|
2655
|
-
});
|
|
2656
|
-
if (row.cells.some((cell) => cell.structuralChange !== void 0 || !trackableBlocks(cell.content))) return refused({
|
|
2657
|
-
op,
|
|
2658
|
-
reason: DOCUMENT_OP_REFUSAL_REASONS.UNTRACKABLE,
|
|
2659
|
-
message: "The row contains an unsupported tracked structure."
|
|
2660
|
-
});
|
|
2661
|
-
const before = paragraphsIn([row]);
|
|
2662
|
-
const after = [];
|
|
2663
|
-
for (const paragraph of before) {
|
|
2664
|
-
const wrapped = wrapTracked({
|
|
2665
|
-
items: paragraph.content,
|
|
2666
|
-
from: {
|
|
2667
|
-
offset: 0,
|
|
2668
|
-
zeroWidthBefore: 0
|
|
2669
|
-
},
|
|
2670
|
-
to: {
|
|
2671
|
-
offset: paragraphLength(paragraph),
|
|
2672
|
-
zeroWidthBefore: Number.MAX_SAFE_INTEGER
|
|
2673
|
-
},
|
|
2674
|
-
kind,
|
|
2675
|
-
stamp: revision
|
|
2676
|
-
});
|
|
2677
|
-
if (wrapped.kind === "refused") return refused({
|
|
2678
|
-
op,
|
|
2679
|
-
reason: wrapped.reason,
|
|
2680
|
-
message: "The row's content cannot record this revision."
|
|
2681
|
-
});
|
|
2682
|
-
after.push(wrapped.kind === "unchanged" ? paragraph : {
|
|
2683
|
-
...paragraph,
|
|
2684
|
-
content: wrapped.content
|
|
2685
|
-
});
|
|
2686
|
-
}
|
|
2687
|
-
const outside = new Set(packageIdentityKeys(document.package));
|
|
2688
|
-
if (op.type === DOCUMENT_OP_TYPES.DELETE_ROW) for (const key of identityKeysIn(before)) outside.delete(key);
|
|
2689
|
-
outside.add(slotKey({
|
|
2690
|
-
space: IDENTITY_SPACES.REVISION,
|
|
2691
|
-
id: revision.id
|
|
2692
|
-
}));
|
|
2693
|
-
const freshened = freshenIdentities({
|
|
2694
|
-
before,
|
|
2695
|
-
after,
|
|
2696
|
-
newIds: newIds ?? {},
|
|
2697
|
-
usedElsewhere: () => outside
|
|
2698
|
-
});
|
|
2699
|
-
switch (freshened.kind) {
|
|
2700
|
-
case "needsIds": return refused({
|
|
2701
|
-
op,
|
|
2702
|
-
reason: DOCUMENT_OP_REFUSAL_REASONS.NEEDS_NEW_IDS,
|
|
2703
|
-
message: `The row needs ${freshened.missing} additional revision ids.`
|
|
2704
|
-
});
|
|
2705
|
-
case "invalidId": return refused({
|
|
2706
|
-
op,
|
|
2707
|
-
reason: DOCUMENT_OP_REFUSAL_REASONS.INVALID_NEW_ID,
|
|
2708
|
-
message: `${freshened.id} cannot be a new revision id.`
|
|
2709
|
-
});
|
|
2710
|
-
case "fresh": {
|
|
2711
|
-
const byId = new Map(freshened.paragraphs.map((paragraph) => [idKey(paragraph.paraId ?? ""), paragraph]));
|
|
2712
|
-
return Result.ok({
|
|
2713
|
-
...rowWithParagraphs(row, (paragraph) => byId.get(idKey(paragraph.paraId ?? "")) ?? panic("A tracked row lost a paragraph.")),
|
|
2714
|
-
structuralChange: {
|
|
2715
|
-
type: kind === WRAP_KINDS.INSERTION ? "tableRowInsertion" : "tableRowDeletion",
|
|
2716
|
-
info: stampInfo(revision)
|
|
2717
|
-
}
|
|
2718
|
-
});
|
|
2719
|
-
}
|
|
2720
|
-
default: return freshened;
|
|
2721
|
-
}
|
|
2722
2751
|
};
|
|
2723
|
-
const
|
|
2724
|
-
const
|
|
2725
|
-
|
|
2726
|
-
|
|
2727
|
-
|
|
2728
|
-
|
|
2729
|
-
|
|
2730
|
-
|
|
2731
|
-
|
|
2732
|
-
|
|
2733
|
-
|
|
2734
|
-
|
|
2735
|
-
|
|
2736
|
-
|
|
2737
|
-
|
|
2738
|
-
|
|
2739
|
-
|
|
2752
|
+
const tablesIn = (document, story) => {
|
|
2753
|
+
const out = [];
|
|
2754
|
+
collectTables(storyBody(document, story).content, out);
|
|
2755
|
+
return out;
|
|
2756
|
+
};
|
|
2757
|
+
/** Only row insert/delete records are reachable; cell and property revisions remain refused. */
|
|
2758
|
+
const reachableRowIds = (document, story) => tablesIn(document, story).flatMap((table) => table.rows.flatMap((row) => {
|
|
2759
|
+
const change = row.structuralChange;
|
|
2760
|
+
return change?.type === "tableRowInsertion" || change?.type === "tableRowDeletion" ? [change.info.id] : [];
|
|
2761
|
+
}));
|
|
2762
|
+
const resolveTableRows = ({ document, op, applyOps }) => {
|
|
2763
|
+
const ids = new Set(op.revisionIds);
|
|
2764
|
+
let current = document;
|
|
2765
|
+
const edits = [];
|
|
2766
|
+
for (const table of tablesIn(document, op.story)) {
|
|
2767
|
+
if (!table.rows.some((row) => row.structuralChange !== void 0 && ids.has(row.structuralChange.info.id))) continue;
|
|
2768
|
+
const blockId = tableRowAnchor(table.rows);
|
|
2769
|
+
if (blockId === void 0) return Result.err(new DocumentOpRefusal({
|
|
2770
|
+
opType: op.type,
|
|
2771
|
+
reason: DOCUMENT_OP_REFUSAL_REASONS.UNTRACKABLE,
|
|
2772
|
+
message: "The table has no paragraph that can address its rows."
|
|
2773
|
+
}));
|
|
2774
|
+
const target = {
|
|
2775
|
+
type: DOCUMENT_OP_TYPES.SET_TABLE_ROWS,
|
|
2776
|
+
story: op.story,
|
|
2777
|
+
blockId,
|
|
2778
|
+
expected: table.rows,
|
|
2779
|
+
rows: table.rows
|
|
2740
2780
|
};
|
|
2741
|
-
|
|
2781
|
+
const located = locateTableRow(current, target);
|
|
2782
|
+
if (located.isErr()) return Result.err(located.error);
|
|
2783
|
+
const expected = located.value.table.rows;
|
|
2784
|
+
const rows = expected.flatMap((row) => {
|
|
2785
|
+
const change = row.structuralChange;
|
|
2786
|
+
if (change === void 0 || !ids.has(change.info.id)) return [row];
|
|
2787
|
+
if (change.type === "tableRowInsertion" !== (op.decision === REVISION_DECISIONS.ACCEPT)) return [];
|
|
2788
|
+
const next = { ...row };
|
|
2789
|
+
delete next.structuralChange;
|
|
2790
|
+
return [next];
|
|
2791
|
+
});
|
|
2792
|
+
const applied = applyOps(current, [{
|
|
2793
|
+
type: DOCUMENT_OP_TYPES.SET_TABLE_ROWS,
|
|
2794
|
+
story: op.story,
|
|
2795
|
+
blockId,
|
|
2796
|
+
expected,
|
|
2797
|
+
rows
|
|
2798
|
+
}]);
|
|
2799
|
+
if (applied.isErr()) return Result.err(applied.error);
|
|
2800
|
+
edits.push(applied.value);
|
|
2801
|
+
current = applied.value.document;
|
|
2742
2802
|
}
|
|
2743
|
-
|
|
2744
|
-
const commitRows = ({ document, op, location, rows }) => {
|
|
2745
|
-
if (rows.length === 0) return refused({
|
|
2746
|
-
op,
|
|
2747
|
-
reason: DOCUMENT_OP_REFUSAL_REASONS.UNTRACKABLE,
|
|
2748
|
-
message: "Removing the last row requires a table operation."
|
|
2749
|
-
});
|
|
2750
|
-
const before = location.table.rows;
|
|
2751
|
-
if (structurallyEqual(before, rows)) return Result.ok({
|
|
2752
|
-
document,
|
|
2753
|
-
inverse: [],
|
|
2754
|
-
touched: {
|
|
2755
|
-
modified: [],
|
|
2756
|
-
inserted: [],
|
|
2757
|
-
removed: []
|
|
2758
|
-
}
|
|
2759
|
-
});
|
|
2760
|
-
const afterParagraphs = paragraphsIn(rows);
|
|
2761
|
-
if (rows.some((row) => row.cells.length === 0 || row.cells.some((cell) => storyParagraphs({ content: cell.content }).length === 0))) return refused({
|
|
2762
|
-
op,
|
|
2763
|
-
reason: DOCUMENT_OP_REFUSAL_REASONS.STRUCTURE_MISMATCH,
|
|
2764
|
-
message: "Every row has cells, and every cell has a paragraph."
|
|
2765
|
-
});
|
|
2766
|
-
if (afterParagraphs.some(({ content }) => textsIn(content).some(hasIllegalXmlCharacters))) return refused({
|
|
2767
|
-
op,
|
|
2768
|
-
reason: DOCUMENT_OP_REFUSAL_REASONS.INVALID_TEXT,
|
|
2769
|
-
message: "The row holds text that cannot be written to XML."
|
|
2770
|
-
});
|
|
2771
|
-
const body = { content: [{
|
|
2772
|
-
type: "table",
|
|
2773
|
-
rows
|
|
2774
|
-
}] };
|
|
2775
|
-
const originalParagraphs = new Map(paragraphsIn(before).map((paragraph) => [idKey(paragraph.paraId ?? ""), paragraph]));
|
|
2776
|
-
for (const paragraphLocation of storyParagraphs(body)) {
|
|
2777
|
-
const mark = paragraphLocation.paragraph.pPrMark;
|
|
2778
|
-
if (mark !== void 0 && endsItsContainer(body, paragraphLocation) && !structurallyEqual(mark, originalParagraphs.get(idKey(paragraphLocation.paragraph.paraId ?? ""))?.pPrMark)) return refused({
|
|
2779
|
-
op,
|
|
2780
|
-
reason: DOCUMENT_OP_REFUSAL_REASONS.CONTAINER_FINAL_MARK,
|
|
2781
|
-
message: "A row operation cannot add a mark to a cell's final paragraph."
|
|
2782
|
-
});
|
|
2783
|
-
}
|
|
2784
|
-
const beforeIds = new Set(paragraphIdsIn(before).map(idKey));
|
|
2785
|
-
if (afterParagraphs.some(({ paraId }) => paraId === void 0 || !beforeIds.has(idKey(paraId)) && !isParaId(paraId))) return refused({
|
|
2786
|
-
op,
|
|
2787
|
-
reason: DOCUMENT_OP_REFUSAL_REASONS.INVALID_BLOCK_ID,
|
|
2788
|
-
message: "An added paragraph has no usable id."
|
|
2789
|
-
});
|
|
2790
|
-
const remainingParagraphs = countIds(packageParagraphIds(document.package));
|
|
2791
|
-
for (const id of paragraphIdsIn(before)) remainingParagraphs.set(idKey(id), (remainingParagraphs.get(idKey(id)) ?? 1) - 1);
|
|
2792
|
-
if (collides(remainingParagraphs, paragraphIdsIn(rows))) return refused({
|
|
2793
|
-
op,
|
|
2794
|
-
reason: DOCUMENT_OP_REFUSAL_REASONS.ID_COLLISION,
|
|
2795
|
-
message: "A row paragraph id is already used in the package."
|
|
2796
|
-
});
|
|
2797
|
-
const remainingRecords = countKeys(packageIdentityKeys(document.package));
|
|
2798
|
-
for (const key of identityKeysIn(before)) remainingRecords.set(key, (remainingRecords.get(key) ?? 1) - 1);
|
|
2799
|
-
const incomingRecords = identityKeysIn(rows);
|
|
2800
|
-
if (incomingRecords.some((key) => (remainingRecords.get(key) ?? 0) > 0) || new Set(incomingRecords).size !== incomingRecords.length) return refused({
|
|
2801
|
-
op,
|
|
2802
|
-
reason: DOCUMENT_OP_REFUSAL_REASONS.ID_COLLISION,
|
|
2803
|
-
message: "A row revision or content-control id is already used in the package."
|
|
2804
|
-
});
|
|
2805
|
-
const next = withRows({
|
|
2806
|
-
document,
|
|
2807
|
-
story: op.story,
|
|
2808
|
-
location,
|
|
2809
|
-
rows
|
|
2810
|
-
});
|
|
2811
|
-
const valid = validateOpsDocument(next);
|
|
2812
|
-
if (valid.isErr()) return refused({
|
|
2813
|
-
op,
|
|
2814
|
-
reason: valid.error.reason,
|
|
2815
|
-
message: valid.error.message
|
|
2816
|
-
});
|
|
2817
|
-
const afterIds = new Set(paragraphIdsIn(rows).map(idKey));
|
|
2818
|
-
const beforeParagraphIds = paragraphIdsIn(before);
|
|
2819
|
-
const afterParagraphIds = paragraphIdsIn(rows);
|
|
2820
|
-
const changedIds = new Set(rows.filter((row) => !before.includes(row)).flatMap(paragraphIdsIn).map(idKey));
|
|
2821
|
-
const anchor = afterParagraphIds.at(0);
|
|
2822
|
-
if (anchor === void 0) return panic("A row list must have an addressable paragraph.");
|
|
2823
|
-
return Result.ok({
|
|
2824
|
-
document: next,
|
|
2825
|
-
inverse: [{
|
|
2826
|
-
type: DOCUMENT_OP_TYPES.SET_TABLE_ROWS,
|
|
2827
|
-
story: op.story,
|
|
2828
|
-
blockId: anchor,
|
|
2829
|
-
expected: rows,
|
|
2830
|
-
rows: before
|
|
2831
|
-
}],
|
|
2832
|
-
touched: {
|
|
2833
|
-
modified: beforeParagraphIds.filter((id) => afterIds.has(idKey(id)) && changedIds.has(idKey(id))),
|
|
2834
|
-
inserted: afterParagraphIds.filter((id) => !beforeIds.has(idKey(id))),
|
|
2835
|
-
removed: beforeParagraphIds.filter((id) => !afterIds.has(idKey(id)))
|
|
2836
|
-
}
|
|
2837
|
-
});
|
|
2838
|
-
};
|
|
2839
|
-
const applyRowOp = (document, op) => {
|
|
2840
|
-
const located = locateTableRow(document, op);
|
|
2841
|
-
if (located.isErr()) return Result.err(located.error);
|
|
2842
|
-
const location = located.value;
|
|
2843
|
-
const before = location.table.rows;
|
|
2844
|
-
const rows = [...before];
|
|
2845
|
-
switch (op.type) {
|
|
2846
|
-
case DOCUMENT_OP_TYPES.INSERT_ROW: {
|
|
2847
|
-
if (!Number.isInteger(op.at) || op.at < 0 || op.at > rows.length) return refused({
|
|
2848
|
-
op,
|
|
2849
|
-
reason: DOCUMENT_OP_REFUSAL_REASONS.INVALID_OFFSET,
|
|
2850
|
-
message: "The row index is outside the table."
|
|
2851
|
-
});
|
|
2852
|
-
if (needsTableEdit(location.table)) return refused({
|
|
2853
|
-
op,
|
|
2854
|
-
reason: DOCUMENT_OP_REFUSAL_REASONS.UNTRACKABLE,
|
|
2855
|
-
message: "Indexed table markup or shared row wrappers require a table operation."
|
|
2856
|
-
});
|
|
2857
|
-
const incoming = structuredClone(op.row);
|
|
2858
|
-
if (paragraphIdsIn(incoming).some((id) => paragraphIdsIn(before).some((existing) => idKey(existing) === idKey(id)))) return refused({
|
|
2859
|
-
op,
|
|
2860
|
-
reason: DOCUMENT_OP_REFUSAL_REASONS.ID_COLLISION,
|
|
2861
|
-
message: "An inserted paragraph id is already used in the table."
|
|
2862
|
-
});
|
|
2863
|
-
const tracked = op.revision === void 0 ? Result.ok(incoming) : trackRow({
|
|
2864
|
-
document,
|
|
2865
|
-
op,
|
|
2866
|
-
row: incoming,
|
|
2867
|
-
revision: op.revision,
|
|
2868
|
-
newIds: op.newIds,
|
|
2869
|
-
kind: WRAP_KINDS.INSERTION
|
|
2870
|
-
});
|
|
2871
|
-
if (tracked.isErr()) return Result.err(tracked.error);
|
|
2872
|
-
rows.splice(op.at, 0, tracked.value);
|
|
2873
|
-
return commitRows({
|
|
2874
|
-
document,
|
|
2875
|
-
op,
|
|
2876
|
-
location,
|
|
2877
|
-
rows
|
|
2878
|
-
});
|
|
2879
|
-
}
|
|
2880
|
-
case DOCUMENT_OP_TYPES.DELETE_ROW: {
|
|
2881
|
-
const row = before[location.rowIndex];
|
|
2882
|
-
if (row === void 0) return panic("A row location must name a row.");
|
|
2883
|
-
if (op.expected !== void 0 && !equalForStaleness(row, op.expected)) return refused({
|
|
2884
|
-
op,
|
|
2885
|
-
reason: DOCUMENT_OP_REFUSAL_REASONS.STALE,
|
|
2886
|
-
message: "The row to remove has changed."
|
|
2887
|
-
});
|
|
2888
|
-
if (needsTableEdit(location.table)) return refused({
|
|
2889
|
-
op,
|
|
2890
|
-
reason: DOCUMENT_OP_REFUSAL_REASONS.UNTRACKABLE,
|
|
2891
|
-
message: "Indexed table markup or shared row wrappers require a table operation."
|
|
2892
|
-
});
|
|
2893
|
-
if (op.revision === void 0) rows.splice(location.rowIndex, 1);
|
|
2894
|
-
else {
|
|
2895
|
-
if (before.filter((candidate) => candidate.structuralChange?.type !== "tableRowDeletion").length <= 1) return refused({
|
|
2896
|
-
op,
|
|
2897
|
-
reason: DOCUMENT_OP_REFUSAL_REASONS.UNTRACKABLE,
|
|
2898
|
-
message: "Deleting the last remaining row requires a table operation."
|
|
2899
|
-
});
|
|
2900
|
-
const tracked = trackRow({
|
|
2901
|
-
document,
|
|
2902
|
-
op,
|
|
2903
|
-
row,
|
|
2904
|
-
revision: op.revision,
|
|
2905
|
-
newIds: op.newIds,
|
|
2906
|
-
kind: WRAP_KINDS.DELETION
|
|
2907
|
-
});
|
|
2908
|
-
if (tracked.isErr()) return Result.err(tracked.error);
|
|
2909
|
-
rows[location.rowIndex] = tracked.value;
|
|
2910
|
-
}
|
|
2911
|
-
return commitRows({
|
|
2912
|
-
document,
|
|
2913
|
-
op,
|
|
2914
|
-
location,
|
|
2915
|
-
rows
|
|
2916
|
-
});
|
|
2917
|
-
}
|
|
2918
|
-
case DOCUMENT_OP_TYPES.SET_TABLE_ROWS: {
|
|
2919
|
-
if (!equalForStaleness(before, op.expected)) return refused({
|
|
2920
|
-
op,
|
|
2921
|
-
reason: DOCUMENT_OP_REFUSAL_REASONS.STALE,
|
|
2922
|
-
message: "The table rows have changed."
|
|
2923
|
-
});
|
|
2924
|
-
if (op.rows.length !== before.length && needsTableEdit(location.table)) return refused({
|
|
2925
|
-
op,
|
|
2926
|
-
reason: DOCUMENT_OP_REFUSAL_REASONS.UNTRACKABLE,
|
|
2927
|
-
message: "Indexed table markup or shared row wrappers require a table operation."
|
|
2928
|
-
});
|
|
2929
|
-
const replacement = op.rows.map((row) => before.find((existing) => structurallyEqual(existing, row)) ?? structuredClone(row));
|
|
2930
|
-
return commitRows({
|
|
2931
|
-
document,
|
|
2932
|
-
op,
|
|
2933
|
-
location,
|
|
2934
|
-
rows: replacement
|
|
2935
|
-
});
|
|
2936
|
-
}
|
|
2937
|
-
default: return op;
|
|
2938
|
-
}
|
|
2939
|
-
};
|
|
2940
|
-
//#endregion
|
|
2941
|
-
//#region src/ops/resolveTableRows.ts
|
|
2942
|
-
/** Row resolution after inline review, before paragraph-mark joins. */
|
|
2943
|
-
const collectTables = (blocks, out) => {
|
|
2944
|
-
for (const block of blocks) switch (block.type) {
|
|
2945
|
-
case "table":
|
|
2946
|
-
for (const row of block.rows) for (const cell of row.cells) collectTables(cell.content, out);
|
|
2947
|
-
out.push(block);
|
|
2948
|
-
break;
|
|
2949
|
-
case "blockSdt":
|
|
2950
|
-
case "blockCustomXml":
|
|
2951
|
-
collectTables(block.content, out);
|
|
2952
|
-
break;
|
|
2953
|
-
case "paragraph":
|
|
2954
|
-
case "preservedBlock":
|
|
2955
|
-
case "bookmarkStart":
|
|
2956
|
-
case "bookmarkEnd": break;
|
|
2957
|
-
default: return block;
|
|
2958
|
-
}
|
|
2959
|
-
};
|
|
2960
|
-
const tablesIn = (document, story) => {
|
|
2961
|
-
const out = [];
|
|
2962
|
-
collectTables(storyBody(document, story).content, out);
|
|
2963
|
-
return out;
|
|
2964
|
-
};
|
|
2965
|
-
/** Only row insert/delete records are reachable; cell and property revisions remain refused. */
|
|
2966
|
-
const reachableRowIds = (document, story) => tablesIn(document, story).flatMap((table) => table.rows.flatMap((row) => {
|
|
2967
|
-
const change = row.structuralChange;
|
|
2968
|
-
return change?.type === "tableRowInsertion" || change?.type === "tableRowDeletion" ? [change.info.id] : [];
|
|
2969
|
-
}));
|
|
2970
|
-
const resolveTableRows = ({ document, op, applyOps }) => {
|
|
2971
|
-
const ids = new Set(op.revisionIds);
|
|
2972
|
-
let current = document;
|
|
2973
|
-
const edits = [];
|
|
2974
|
-
for (const table of tablesIn(document, op.story)) {
|
|
2975
|
-
if (!table.rows.some((row) => row.structuralChange !== void 0 && ids.has(row.structuralChange.info.id))) continue;
|
|
2976
|
-
const blockId = storyParagraphs({ content: [table] }).find(({ list }) => list.filter((step) => step.kind === "tableCell").length === 1)?.paragraph.paraId;
|
|
2977
|
-
if (blockId === void 0) return Result.err(new DocumentOpRefusal({
|
|
2978
|
-
opType: op.type,
|
|
2979
|
-
reason: DOCUMENT_OP_REFUSAL_REASONS.UNTRACKABLE,
|
|
2980
|
-
message: "The table has no paragraph that can address its rows."
|
|
2981
|
-
}));
|
|
2982
|
-
const target = {
|
|
2983
|
-
type: DOCUMENT_OP_TYPES.SET_TABLE_ROWS,
|
|
2984
|
-
story: op.story,
|
|
2985
|
-
blockId,
|
|
2986
|
-
expected: table.rows,
|
|
2987
|
-
rows: table.rows
|
|
2988
|
-
};
|
|
2989
|
-
const located = locateTableRow(current, target);
|
|
2990
|
-
if (located.isErr()) return Result.err(located.error);
|
|
2991
|
-
const expected = located.value.table.rows;
|
|
2992
|
-
const rows = expected.flatMap((row) => {
|
|
2993
|
-
const change = row.structuralChange;
|
|
2994
|
-
if (change === void 0 || !ids.has(change.info.id)) return [row];
|
|
2995
|
-
if (change.type === "tableRowInsertion" !== (op.decision === REVISION_DECISIONS.ACCEPT)) return [];
|
|
2996
|
-
const next = { ...row };
|
|
2997
|
-
delete next.structuralChange;
|
|
2998
|
-
return [next];
|
|
2999
|
-
});
|
|
3000
|
-
const applied = applyOps(current, [{
|
|
3001
|
-
type: DOCUMENT_OP_TYPES.SET_TABLE_ROWS,
|
|
3002
|
-
story: op.story,
|
|
3003
|
-
blockId,
|
|
3004
|
-
expected,
|
|
3005
|
-
rows
|
|
3006
|
-
}]);
|
|
3007
|
-
if (applied.isErr()) return Result.err(applied.error);
|
|
3008
|
-
edits.push(applied.value);
|
|
3009
|
-
current = applied.value.document;
|
|
3010
|
-
}
|
|
3011
|
-
return Result.ok(combineEdits(document, edits));
|
|
2803
|
+
return Result.ok(combineEdits(document, edits));
|
|
3012
2804
|
};
|
|
3013
2805
|
//#endregion
|
|
3014
2806
|
//#region src/ops/resolve.ts
|
|
@@ -3089,13 +2881,45 @@ const resolveRun = (run, { ids, decision }) => {
|
|
|
3089
2881
|
/**
|
|
3090
2882
|
* Whether `empty` is a piece of `other` that resolution left with nothing: a
|
|
3091
2883
|
* container of the same kind and fields that the resolution emptied, where
|
|
3092
|
-
* the other holds something
|
|
3093
|
-
*
|
|
2884
|
+
* the other holds something or was emptied too (two emptied pieces are one
|
|
2885
|
+
* piece, whichever emptied first). A container that held nothing before (an
|
|
2886
|
+
* empty content control or hyperlink) is markup of its own and is never merged.
|
|
3094
2887
|
*/
|
|
3095
2888
|
const emptyPieceOf = (empty, other, emptied) => {
|
|
3096
2889
|
const emptyChildren = childNodes(empty);
|
|
3097
2890
|
const otherChildren = childNodes(other);
|
|
3098
|
-
return empty.type !== "run" && emptyChildren !== void 0 && otherChildren !== void 0 && emptyChildren.length === 0 && otherChildren.length > 0 && emptied.has(empty) && sameOwnFields(empty, other);
|
|
2891
|
+
return empty.type !== "run" && emptyChildren !== void 0 && otherChildren !== void 0 && emptyChildren.length === 0 && (otherChildren.length > 0 || emptied.has(other)) && emptied.has(empty) && sameOwnFields(empty, other);
|
|
2892
|
+
};
|
|
2893
|
+
/**
|
|
2894
|
+
* An emptied piece folded into its neighbour, which keeps the first one's ids;
|
|
2895
|
+
* `undefined` when neither is one. When both are, the first stays as it is,
|
|
2896
|
+
* still emptied.
|
|
2897
|
+
*/
|
|
2898
|
+
const foldedPiece = (left, right, emptied) => {
|
|
2899
|
+
if (emptyPieceOf(right, left, emptied)) return left;
|
|
2900
|
+
if (emptyPieceOf(left, right, emptied)) return rebuildNode(left, childNodes(right) ?? []);
|
|
2901
|
+
};
|
|
2902
|
+
/**
|
|
2903
|
+
* Two records meeting beside a container resolution emptied, with no change
|
|
2904
|
+
* resolved between them: the emptied piece folds into its neighbour, down the
|
|
2905
|
+
* chain of alike containers at their facing edges when it sits inside one.
|
|
2906
|
+
* Nothing else merges there.
|
|
2907
|
+
*/
|
|
2908
|
+
const foldAtSeam = (left, right, emptied) => {
|
|
2909
|
+
const folded = foldedPiece(left, right, emptied);
|
|
2910
|
+
if (folded !== void 0) return [folded];
|
|
2911
|
+
const leftChildren = childNodes(left);
|
|
2912
|
+
const rightChildren = childNodes(right);
|
|
2913
|
+
const last = leftChildren?.at(-1);
|
|
2914
|
+
const first = rightChildren?.at(0);
|
|
2915
|
+
if (left.type === "run" || leftChildren === void 0 || rightChildren === void 0 || last === void 0 || first === void 0 || !sameOwnFields(left, right)) return [left, right];
|
|
2916
|
+
const inner = foldAtSeam(last, first, emptied);
|
|
2917
|
+
if (inner.length !== 1) return [left, right];
|
|
2918
|
+
return [rebuildNode(left, [
|
|
2919
|
+
...leftChildren.slice(0, -1),
|
|
2920
|
+
...inner,
|
|
2921
|
+
...rightChildren.slice(1)
|
|
2922
|
+
])];
|
|
3099
2923
|
};
|
|
3100
2924
|
/**
|
|
3101
2925
|
* Two records meeting where a change was resolved, merged as far as they are
|
|
@@ -3111,8 +2935,8 @@ const mergeAtSeam = (left, right, emptied = NOTHING_EMPTIED) => {
|
|
|
3111
2935
|
const rightChildren = childNodes(right);
|
|
3112
2936
|
return leftChildren === void 0 || rightChildren === void 0 ? mergeAlike([left], [right]) : [rebuildNode(left, mergedAtSeam(leftChildren, rightChildren, emptied))];
|
|
3113
2937
|
}
|
|
3114
|
-
|
|
3115
|
-
return
|
|
2938
|
+
const folded = foldedPiece(left, right, emptied);
|
|
2939
|
+
return folded === void 0 ? [left, right] : [folded];
|
|
3116
2940
|
};
|
|
3117
2941
|
/** Two lists end to end, merged at the seam as {@link mergeAtSeam} merges. */
|
|
3118
2942
|
const mergedAtSeam = (left, right, emptied) => {
|
|
@@ -3132,6 +2956,8 @@ const mergedAtSeam = (left, right, emptied) => {
|
|
|
3132
2956
|
const resolveList = (nodes, resolution) => {
|
|
3133
2957
|
const out = [];
|
|
3134
2958
|
const seams = [];
|
|
2959
|
+
/** Seams beside an emptied container, where only its fold happens. */
|
|
2960
|
+
const folds = [];
|
|
3135
2961
|
let changed = false;
|
|
3136
2962
|
for (const node of nodes) {
|
|
3137
2963
|
if (isTrackedWrapper(node) && resolution.ids.has(node.info.id)) {
|
|
@@ -3158,23 +2984,31 @@ const resolveList = (nodes, resolution) => {
|
|
|
3158
2984
|
continue;
|
|
3159
2985
|
}
|
|
3160
2986
|
changed = true;
|
|
3161
|
-
if (isTrackedWrapper(node) && inner.nodes.length === 0 && children.length > 0) {
|
|
2987
|
+
if ((isTrackedWrapper(node) || resolution.decision === REVISION_DECISIONS.ACCEPT) && inner.nodes.length === 0 && children.length > 0) {
|
|
3162
2988
|
seams.push(out.length);
|
|
3163
2989
|
continue;
|
|
3164
2990
|
}
|
|
3165
2991
|
const rebuilt = rebuildNode(node, inner.nodes);
|
|
2992
|
+
const ends = inner.nodes.length === 0 ? {
|
|
2993
|
+
first: true,
|
|
2994
|
+
last: true
|
|
2995
|
+
} : emptiedEndsOf(inner.nodes, resolution.emptied);
|
|
3166
2996
|
if (inner.nodes.length === 0) resolution.emptied.add(rebuilt);
|
|
2997
|
+
if (ends.first) folds.push(out.length);
|
|
2998
|
+
if (ends.last) folds.push(out.length + 1);
|
|
3167
2999
|
out.push(rebuilt);
|
|
3168
3000
|
}
|
|
3169
3001
|
if (!changed) return {
|
|
3170
3002
|
nodes: out,
|
|
3171
3003
|
changed
|
|
3172
3004
|
};
|
|
3173
|
-
|
|
3005
|
+
const merges = new Set(seams);
|
|
3006
|
+
for (const seam of [.../* @__PURE__ */ new Set([...seams, ...folds])].toSorted((left, right) => right - left)) {
|
|
3174
3007
|
const left = out[seam - 1];
|
|
3175
3008
|
const right = out[seam];
|
|
3176
3009
|
if (left === void 0 || right === void 0) continue;
|
|
3177
|
-
|
|
3010
|
+
const met = merges.has(seam) ? mergeAtSeam(left, right, resolution.emptied) : foldAtSeam(left, right, resolution.emptied);
|
|
3011
|
+
out.splice(seam - 1, 2, ...met);
|
|
3178
3012
|
}
|
|
3179
3013
|
return {
|
|
3180
3014
|
nodes: out,
|
|
@@ -3261,6 +3095,7 @@ const joinOps = (document, { op, story, paraId, added }, ends) => {
|
|
|
3261
3095
|
const blocks = blockListAt(body.content, location.list);
|
|
3262
3096
|
const next = blocks[location.index + 1];
|
|
3263
3097
|
const empty = paragraphLength(paragraph) === 0;
|
|
3098
|
+
if (next?.type === "table" && next.rows.some((row) => row.structuralChange?.type === "tableRowInsertion")) return Result.err(refusal$1(op, DOCUMENT_OP_REFUSAL_REASONS.UNTRACKABLE, "Resolve the inserted table before removing its preceding paragraph break."));
|
|
3264
3099
|
if (next?.type === "paragraph") {
|
|
3265
3100
|
const firstEnds = ends.get(idKey(paraId)) ?? NO_EMPTIED_ENDS;
|
|
3266
3101
|
const nextId = idKey(next.paraId ?? "");
|
|
@@ -3306,7 +3141,7 @@ const joinOps = (document, { op, story, paraId, added }, ends) => {
|
|
|
3306
3141
|
}]);
|
|
3307
3142
|
}
|
|
3308
3143
|
if (next !== void 0 && (next.type === "bookmarkStart" || next.type === "bookmarkEnd")) return Result.err(refusal$1(op, DOCUMENT_OP_REFUSAL_REASONS.UNTRACKABLE, `A block-level bookmark stands between ${paraId} and the next paragraph.`));
|
|
3309
|
-
const canGo = added || !endsItsContainer(body, location);
|
|
3144
|
+
const canGo = !(endsItsContainer(body, location) && location.list.some((step) => step.kind === "tableCell")) && (added || !endsItsContainer(body, location));
|
|
3310
3145
|
if (empty && canGo && blocks.length > 1) return Result.err(refusal$1(op, DOCUMENT_OP_REFUSAL_REASONS.UNTRACKABLE, `Resolving the mark of ${paraId} removes the paragraph, which is a block operation.`));
|
|
3311
3146
|
const review = reviewFieldsOf(paragraph);
|
|
3312
3147
|
delete review.pPrMark;
|
|
@@ -3338,76 +3173,972 @@ const resolveRevision = (document, op, applyOps) => {
|
|
|
3338
3173
|
document,
|
|
3339
3174
|
inverse: [],
|
|
3340
3175
|
touched: {
|
|
3341
|
-
modified: [],
|
|
3342
|
-
inserted: [],
|
|
3343
|
-
removed: []
|
|
3176
|
+
modified: [],
|
|
3177
|
+
inserted: [],
|
|
3178
|
+
removed: []
|
|
3179
|
+
}
|
|
3180
|
+
});
|
|
3181
|
+
const resolution = {
|
|
3182
|
+
ids,
|
|
3183
|
+
decision: op.decision,
|
|
3184
|
+
emptied: /* @__PURE__ */ new WeakSet()
|
|
3185
|
+
};
|
|
3186
|
+
const paragraphs = storyParagraphs(storyBody(document, op.story));
|
|
3187
|
+
const reachable = /* @__PURE__ */ new Set([...paragraphs.flatMap(({ paragraph }) => [...reachableIds(paragraph)]), ...reachableRowIds(document, op.story)]);
|
|
3188
|
+
const unreachable = [...ids].find((id) => !reachable.has(id));
|
|
3189
|
+
if (unreachable !== void 0) return Result.err(refusal$1(op, DOCUMENT_OP_REFUSAL_REASONS.UNTRACKABLE, `Revision ${unreachable} is not an inline change, property change, row change or paragraph mark.`));
|
|
3190
|
+
const inline = [];
|
|
3191
|
+
const joins = [];
|
|
3192
|
+
const emptiedEnds = /* @__PURE__ */ new Map();
|
|
3193
|
+
for (const { paragraph } of paragraphs) {
|
|
3194
|
+
const paraId = paragraph.paraId ?? "";
|
|
3195
|
+
const resolved = resolveList(paragraph.content, resolution);
|
|
3196
|
+
if (resolved.changed) {
|
|
3197
|
+
emptiedEnds.set(idKey(paraId), emptiedEndsOf(resolved.nodes, resolution.emptied));
|
|
3198
|
+
inline.push({
|
|
3199
|
+
type: DOCUMENT_OP_TYPES.REPLACE_INLINE,
|
|
3200
|
+
story: op.story,
|
|
3201
|
+
blockId: paraId,
|
|
3202
|
+
expected: paragraph.content,
|
|
3203
|
+
content: asParagraphContent(resolved.nodes)
|
|
3204
|
+
});
|
|
3205
|
+
}
|
|
3206
|
+
const mark = paragraph.pPrMark;
|
|
3207
|
+
const markResolved = mark !== void 0 && ids.has(mark.info.id);
|
|
3208
|
+
const added = mark !== void 0 && markWasAdded(mark.kind);
|
|
3209
|
+
const keepsBreak = markResolved && added === (op.decision === REVISION_DECISIONS.ACCEPT);
|
|
3210
|
+
if (markResolved && !keepsBreak) joins.push({
|
|
3211
|
+
op,
|
|
3212
|
+
story: op.story,
|
|
3213
|
+
paraId,
|
|
3214
|
+
added
|
|
3215
|
+
});
|
|
3216
|
+
const review = resolveParagraphReview(paragraph, resolution, keepsBreak);
|
|
3217
|
+
if (review !== void 0) inline.push({
|
|
3218
|
+
type: DOCUMENT_OP_TYPES.SET_PARAGRAPH_REVIEW,
|
|
3219
|
+
story: op.story,
|
|
3220
|
+
blockId: paraId,
|
|
3221
|
+
expected: reviewFieldsOf(paragraph),
|
|
3222
|
+
review
|
|
3223
|
+
});
|
|
3224
|
+
}
|
|
3225
|
+
const staged = applyOps(document, inline);
|
|
3226
|
+
if (staged.isErr()) return Result.err(staged.error);
|
|
3227
|
+
const rows = resolveTableRows({
|
|
3228
|
+
document: staged.value.document,
|
|
3229
|
+
op,
|
|
3230
|
+
applyOps
|
|
3231
|
+
});
|
|
3232
|
+
if (rows.isErr()) return Result.err(rows.error);
|
|
3233
|
+
const removedParagraphs = new Set(rows.value.touched.removed.map(idKey));
|
|
3234
|
+
const edits = [staged.value, rows.value];
|
|
3235
|
+
let current = rows.value.document;
|
|
3236
|
+
for (const join of joins.toReversed()) {
|
|
3237
|
+
if (removedParagraphs.has(idKey(join.paraId))) continue;
|
|
3238
|
+
const planned = joinOps(current, join, emptiedEnds);
|
|
3239
|
+
if (planned.isErr()) return Result.err(planned.error);
|
|
3240
|
+
const joined = applyOps(current, planned.value);
|
|
3241
|
+
if (joined.isErr()) return Result.err(joined.error);
|
|
3242
|
+
edits.push(joined.value);
|
|
3243
|
+
current = joined.value.document;
|
|
3244
|
+
}
|
|
3245
|
+
return Result.ok(combineEdits(document, edits));
|
|
3246
|
+
};
|
|
3247
|
+
//#endregion
|
|
3248
|
+
//#region src/ops/tableTracking.ts
|
|
3249
|
+
/** Shared tracked construction for table rows and whole tables. */
|
|
3250
|
+
const refused$1 = ({ op, reason, message }) => Result.err(new DocumentOpRefusal({
|
|
3251
|
+
opType: op.type,
|
|
3252
|
+
reason,
|
|
3253
|
+
message
|
|
3254
|
+
}));
|
|
3255
|
+
const paragraphsIn$2 = (rows) => storyParagraphs({ content: [{
|
|
3256
|
+
type: "table",
|
|
3257
|
+
rows: [...rows]
|
|
3258
|
+
}] }).map(({ paragraph }) => paragraph);
|
|
3259
|
+
/** Rebuild only paths to changed paragraphs, retaining every other record. */
|
|
3260
|
+
const mapParagraphs = (blocks, replace) => blocks.map((block) => {
|
|
3261
|
+
switch (block.type) {
|
|
3262
|
+
case "paragraph": return replace(block);
|
|
3263
|
+
case "blockSdt":
|
|
3264
|
+
case "blockCustomXml": return {
|
|
3265
|
+
...block,
|
|
3266
|
+
content: mapParagraphs(block.content, replace)
|
|
3267
|
+
};
|
|
3268
|
+
case "table": return {
|
|
3269
|
+
...block,
|
|
3270
|
+
rows: block.rows.map((row) => rowWithParagraphs(row, replace))
|
|
3271
|
+
};
|
|
3272
|
+
case "preservedBlock":
|
|
3273
|
+
case "bookmarkStart":
|
|
3274
|
+
case "bookmarkEnd": return block;
|
|
3275
|
+
default: return block;
|
|
3276
|
+
}
|
|
3277
|
+
});
|
|
3278
|
+
const rowWithParagraphs = (row, replace) => ({
|
|
3279
|
+
...row,
|
|
3280
|
+
cells: row.cells.map((cell) => ({
|
|
3281
|
+
...cell,
|
|
3282
|
+
content: mapParagraphs(cell.content, replace)
|
|
3283
|
+
}))
|
|
3284
|
+
});
|
|
3285
|
+
const trackableInline = (nodes) => nodes.every((node) => {
|
|
3286
|
+
switch (node.type) {
|
|
3287
|
+
case "moveFrom":
|
|
3288
|
+
case "moveTo":
|
|
3289
|
+
case "moveFromRangeStart":
|
|
3290
|
+
case "moveFromRangeEnd":
|
|
3291
|
+
case "moveToRangeStart":
|
|
3292
|
+
case "moveToRangeEnd":
|
|
3293
|
+
case "preservedInline":
|
|
3294
|
+
case "preservedXml": return false;
|
|
3295
|
+
default: return trackableInline(childNodes(node) ?? []);
|
|
3296
|
+
}
|
|
3297
|
+
});
|
|
3298
|
+
/** Nested tables, captured blocks and moves have no tracked-row construction. */
|
|
3299
|
+
const trackableBlocks = (blocks) => blocks.every((block) => {
|
|
3300
|
+
switch (block.type) {
|
|
3301
|
+
case "paragraph": return block.sectionProperties === void 0 && block.pPrMark?.kind !== "moveFrom" && block.pPrMark?.kind !== "moveTo" && trackableInline(block.content);
|
|
3302
|
+
case "blockSdt":
|
|
3303
|
+
case "blockCustomXml": return trackableBlocks(block.content);
|
|
3304
|
+
case "table":
|
|
3305
|
+
case "preservedBlock":
|
|
3306
|
+
case "bookmarkStart":
|
|
3307
|
+
case "bookmarkEnd": return false;
|
|
3308
|
+
default: return block;
|
|
3309
|
+
}
|
|
3310
|
+
});
|
|
3311
|
+
/** Row stamps own distinct final-cell marks as well as the inline revisions. */
|
|
3312
|
+
const trackTableRows = ({ document, op, rows, revision, newIds, kind }) => {
|
|
3313
|
+
if (rows.some((row) => row.structuralChange !== void 0)) return refused$1({
|
|
3314
|
+
op,
|
|
3315
|
+
reason: DOCUMENT_OP_REFUSAL_REASONS.REVISION_CONFLICT,
|
|
3316
|
+
message: "A row already carries a structural revision."
|
|
3317
|
+
});
|
|
3318
|
+
const finals = /* @__PURE__ */ new Set();
|
|
3319
|
+
let missingFinalParagraph = false;
|
|
3320
|
+
for (const row of rows) for (const cell of row.cells) {
|
|
3321
|
+
const body = { content: cell.content };
|
|
3322
|
+
const final = storyParagraphs(body).at(-1);
|
|
3323
|
+
if (final === void 0) return refused$1({
|
|
3324
|
+
op,
|
|
3325
|
+
reason: DOCUMENT_OP_REFUSAL_REASONS.STRUCTURE_MISMATCH,
|
|
3326
|
+
message: "Every cell must have a paragraph."
|
|
3327
|
+
});
|
|
3328
|
+
if (!endsItsContainer(body, final)) {
|
|
3329
|
+
missingFinalParagraph = true;
|
|
3330
|
+
continue;
|
|
3331
|
+
}
|
|
3332
|
+
finals.add(final.paragraph);
|
|
3333
|
+
}
|
|
3334
|
+
if (missingFinalParagraph || rows.some((row) => row.preserved !== void 0 || row.bookmarks !== void 0 || row.contentControls !== void 0 || row.carrierStack !== void 0 || row.cells.some((cell) => cell.structuralChange !== void 0 || (cell.propertyChanges?.length ?? 0) > 0 || cell.contentControls !== void 0 || cell.carrierStack !== void 0 || !trackableBlocks(cell.content)))) return refused$1({
|
|
3335
|
+
op,
|
|
3336
|
+
reason: DOCUMENT_OP_REFUSAL_REASONS.UNTRACKABLE,
|
|
3337
|
+
message: "The rows contain an unsupported tracked structure."
|
|
3338
|
+
});
|
|
3339
|
+
if ([...finals].some((paragraph) => paragraph.pPrMark !== void 0)) return refused$1({
|
|
3340
|
+
op,
|
|
3341
|
+
reason: DOCUMENT_OP_REFUSAL_REASONS.REVISION_CONFLICT,
|
|
3342
|
+
message: "A cell's final paragraph already carries a mark revision."
|
|
3343
|
+
});
|
|
3344
|
+
const before = paragraphsIn$2(rows);
|
|
3345
|
+
const outside = new Set(packageIdentityKeys(document.package));
|
|
3346
|
+
if (op.type === DOCUMENT_OP_TYPES.DELETE_ROW || op.type === DOCUMENT_OP_TYPES.DELETE_TABLE) for (const key of identityKeysIn(rows)) outside.delete(key);
|
|
3347
|
+
const taken = /* @__PURE__ */ new Set([...outside, ...identityKeysIn(rows)]);
|
|
3348
|
+
const firstKey = slotKey({
|
|
3349
|
+
space: IDENTITY_SPACES.REVISION,
|
|
3350
|
+
id: revision.id
|
|
3351
|
+
});
|
|
3352
|
+
outside.add(firstKey);
|
|
3353
|
+
taken.add(firstKey);
|
|
3354
|
+
const rowStamps = [];
|
|
3355
|
+
const pool = newIds?.revision ?? [];
|
|
3356
|
+
let cursor = 0;
|
|
3357
|
+
for (const [index] of rows.entries()) {
|
|
3358
|
+
if (index === 0) {
|
|
3359
|
+
rowStamps.push(revision);
|
|
3360
|
+
continue;
|
|
3361
|
+
}
|
|
3362
|
+
let id = pool[cursor];
|
|
3363
|
+
while (id !== void 0 && Number.isInteger(id) && taken.has(slotKey({
|
|
3364
|
+
space: IDENTITY_SPACES.REVISION,
|
|
3365
|
+
id
|
|
3366
|
+
}))) {
|
|
3367
|
+
cursor += 1;
|
|
3368
|
+
id = pool[cursor];
|
|
3369
|
+
}
|
|
3370
|
+
cursor += 1;
|
|
3371
|
+
if (id === void 0) return refused$1({
|
|
3372
|
+
op,
|
|
3373
|
+
reason: DOCUMENT_OP_REFUSAL_REASONS.NEEDS_NEW_IDS,
|
|
3374
|
+
message: `The rows need ${rows.length - index} additional structural revision ids.`
|
|
3375
|
+
});
|
|
3376
|
+
if (!Number.isInteger(id) || id < 0 || id > 2147483647) return refused$1({
|
|
3377
|
+
op,
|
|
3378
|
+
reason: DOCUMENT_OP_REFUSAL_REASONS.INVALID_NEW_ID,
|
|
3379
|
+
message: `${id} cannot be a new revision id.`
|
|
3380
|
+
});
|
|
3381
|
+
const key = slotKey({
|
|
3382
|
+
space: IDENTITY_SPACES.REVISION,
|
|
3383
|
+
id
|
|
3384
|
+
});
|
|
3385
|
+
taken.add(key);
|
|
3386
|
+
outside.add(key);
|
|
3387
|
+
rowStamps.push({
|
|
3388
|
+
...revision,
|
|
3389
|
+
id
|
|
3390
|
+
});
|
|
3391
|
+
}
|
|
3392
|
+
const after = [];
|
|
3393
|
+
for (const paragraph of before) {
|
|
3394
|
+
const wrapped = wrapTracked({
|
|
3395
|
+
items: paragraph.content,
|
|
3396
|
+
from: {
|
|
3397
|
+
offset: 0,
|
|
3398
|
+
zeroWidthBefore: 0
|
|
3399
|
+
},
|
|
3400
|
+
to: {
|
|
3401
|
+
offset: paragraphLength(paragraph),
|
|
3402
|
+
zeroWidthBefore: Number.MAX_SAFE_INTEGER
|
|
3403
|
+
},
|
|
3404
|
+
kind,
|
|
3405
|
+
stamp: revision
|
|
3406
|
+
});
|
|
3407
|
+
if (wrapped.kind === "refused") return refused$1({
|
|
3408
|
+
op,
|
|
3409
|
+
reason: wrapped.reason,
|
|
3410
|
+
message: "The rows' content cannot record this revision."
|
|
3411
|
+
});
|
|
3412
|
+
const content = wrapped.kind === "unchanged" ? paragraph.content : wrapped.content;
|
|
3413
|
+
if (finals.has(paragraph)) {
|
|
3414
|
+
after.push({
|
|
3415
|
+
...paragraph,
|
|
3416
|
+
content,
|
|
3417
|
+
pPrMark: {
|
|
3418
|
+
kind: kind === WRAP_KINDS.INSERTION ? "ins" : "del",
|
|
3419
|
+
info: stampInfo(revision)
|
|
3420
|
+
}
|
|
3421
|
+
});
|
|
3422
|
+
continue;
|
|
3423
|
+
}
|
|
3424
|
+
after.push(content === paragraph.content ? paragraph : {
|
|
3425
|
+
...paragraph,
|
|
3426
|
+
content
|
|
3427
|
+
});
|
|
3428
|
+
}
|
|
3429
|
+
const freshened = freshenIdentities({
|
|
3430
|
+
before,
|
|
3431
|
+
after,
|
|
3432
|
+
newIds: {
|
|
3433
|
+
...newIds,
|
|
3434
|
+
revision: pool.slice(cursor)
|
|
3435
|
+
},
|
|
3436
|
+
usedElsewhere: () => outside
|
|
3437
|
+
});
|
|
3438
|
+
switch (freshened.kind) {
|
|
3439
|
+
case "needsIds": return refused$1({
|
|
3440
|
+
op,
|
|
3441
|
+
reason: DOCUMENT_OP_REFUSAL_REASONS.NEEDS_NEW_IDS,
|
|
3442
|
+
message: `The rows need ${freshened.missing} additional revision ids.`
|
|
3443
|
+
});
|
|
3444
|
+
case "invalidId": return refused$1({
|
|
3445
|
+
op,
|
|
3446
|
+
reason: DOCUMENT_OP_REFUSAL_REASONS.INVALID_NEW_ID,
|
|
3447
|
+
message: `${freshened.id} cannot be a new revision id.`
|
|
3448
|
+
});
|
|
3449
|
+
case "fresh": {
|
|
3450
|
+
const byId = new Map(freshened.paragraphs.map((paragraph) => [idKey(paragraph.paraId ?? ""), paragraph]));
|
|
3451
|
+
return Result.ok(rows.map((row, index) => {
|
|
3452
|
+
const stamp = rowStamps[index] ?? panic("A tracked row lost its structural stamp.");
|
|
3453
|
+
return {
|
|
3454
|
+
...rowWithParagraphs(row, (paragraph) => byId.get(idKey(paragraph.paraId ?? "")) ?? panic("A tracked row lost a paragraph.")),
|
|
3455
|
+
structuralChange: {
|
|
3456
|
+
type: kind === WRAP_KINDS.INSERTION ? "tableRowInsertion" : "tableRowDeletion",
|
|
3457
|
+
info: stampInfo(stamp)
|
|
3458
|
+
}
|
|
3459
|
+
};
|
|
3460
|
+
}));
|
|
3461
|
+
}
|
|
3462
|
+
default: return freshened;
|
|
3463
|
+
}
|
|
3464
|
+
};
|
|
3465
|
+
/** A cell-final mark is legal only when its innermost row owns its stamp. */
|
|
3466
|
+
const permitsCellFinalMark = (body, location) => {
|
|
3467
|
+
const mark = location.paragraph.pPrMark;
|
|
3468
|
+
if (mark === void 0 || !endsItsContainer(body, location)) return false;
|
|
3469
|
+
const stepIndex = location.list.findLastIndex((step) => step.kind === "tableCell");
|
|
3470
|
+
const step = location.list[stepIndex];
|
|
3471
|
+
if (step?.kind !== "tableCell") return false;
|
|
3472
|
+
const table = blockListAt(body.content, location.list.slice(0, stepIndex))[step.block];
|
|
3473
|
+
const change = table?.type === "table" ? table.rows[step.row]?.structuralChange : void 0;
|
|
3474
|
+
if (change === void 0 || mark.info.id === change.info.id || !structurallyEqual({
|
|
3475
|
+
...mark.info,
|
|
3476
|
+
id: 0
|
|
3477
|
+
}, {
|
|
3478
|
+
...change.info,
|
|
3479
|
+
id: 0
|
|
3480
|
+
})) return false;
|
|
3481
|
+
switch (change.type) {
|
|
3482
|
+
case "tableRowInsertion": return mark.kind === "ins";
|
|
3483
|
+
case "tableRowDeletion": return mark.kind === "del";
|
|
3484
|
+
case "tableCellInsertion":
|
|
3485
|
+
case "tableCellDeletion":
|
|
3486
|
+
case "tableCellMerge": return false;
|
|
3487
|
+
default: return change;
|
|
3488
|
+
}
|
|
3489
|
+
};
|
|
3490
|
+
/** Newly stamped row structural ids, including rows below wrappers and cells. */
|
|
3491
|
+
const stampedTableRowRevisionIds = (blocks, stamp, known) => {
|
|
3492
|
+
const out = [];
|
|
3493
|
+
const visit = (children) => {
|
|
3494
|
+
for (const block of children) switch (block.type) {
|
|
3495
|
+
case "table":
|
|
3496
|
+
for (const row of block.rows) {
|
|
3497
|
+
const change = row.structuralChange;
|
|
3498
|
+
if (change !== void 0 && (change.type === "tableRowInsertion" || change.type === "tableRowDeletion") && carriesStamp(change.info, stamp) && !known.has(slotKey({
|
|
3499
|
+
space: IDENTITY_SPACES.REVISION,
|
|
3500
|
+
id: change.info.id
|
|
3501
|
+
})) && !out.includes(change.info.id)) out.push(change.info.id);
|
|
3502
|
+
for (const cell of row.cells) visit(cell.content);
|
|
3503
|
+
}
|
|
3504
|
+
break;
|
|
3505
|
+
case "blockSdt":
|
|
3506
|
+
case "blockCustomXml":
|
|
3507
|
+
visit(block.content);
|
|
3508
|
+
break;
|
|
3509
|
+
case "paragraph":
|
|
3510
|
+
case "preservedBlock":
|
|
3511
|
+
case "bookmarkStart":
|
|
3512
|
+
case "bookmarkEnd": break;
|
|
3513
|
+
default: return block;
|
|
3514
|
+
}
|
|
3515
|
+
};
|
|
3516
|
+
visit(blocks);
|
|
3517
|
+
return out;
|
|
3518
|
+
};
|
|
3519
|
+
//#endregion
|
|
3520
|
+
//#region src/ops/tables.ts
|
|
3521
|
+
/** Whole-table edits and their exact structural inverse. */
|
|
3522
|
+
const refuse$1 = ({ op, reason, message }) => Result.err(new DocumentOpRefusal({
|
|
3523
|
+
opType: op.type,
|
|
3524
|
+
reason,
|
|
3525
|
+
message
|
|
3526
|
+
}));
|
|
3527
|
+
const paragraphsIn$1 = (blocks) => storyParagraphs({ content: [...blocks] }).map(({ paragraph }) => paragraph);
|
|
3528
|
+
const endsInParagraph = (blocks) => {
|
|
3529
|
+
const last = blocks.at(-1);
|
|
3530
|
+
if (last?.type === "blockSdt" || last?.type === "blockCustomXml") return endsInParagraph(last.content);
|
|
3531
|
+
return last?.type === "paragraph";
|
|
3532
|
+
};
|
|
3533
|
+
/** Tables and cells must have usable content and end in a paragraph. */
|
|
3534
|
+
const validStructure = (blocks) => blocks.every((block) => {
|
|
3535
|
+
switch (block.type) {
|
|
3536
|
+
case "table": return block.rows.length > 0 && block.rows.every((row) => row.cells.length > 0 && row.cells.every((cell) => endsInParagraph(cell.content) && validStructure(cell.content)));
|
|
3537
|
+
case "blockSdt":
|
|
3538
|
+
case "blockCustomXml": return validStructure(block.content);
|
|
3539
|
+
case "paragraph":
|
|
3540
|
+
case "preservedBlock":
|
|
3541
|
+
case "bookmarkStart":
|
|
3542
|
+
case "bookmarkEnd": return true;
|
|
3543
|
+
default: return block;
|
|
3544
|
+
}
|
|
3545
|
+
});
|
|
3546
|
+
const permitsStructuralCellMark = ({ body, location, op }) => {
|
|
3547
|
+
if (permitsCellFinalMark(body, location)) return true;
|
|
3548
|
+
const mark = location.paragraph.pPrMark;
|
|
3549
|
+
return op.type === DOCUMENT_OP_TYPES.SET_CONTAINER_BLOCKS && location.list.some((step) => step.kind === "tableCell") && (mark?.kind === "ins" || mark?.kind === "del");
|
|
3550
|
+
};
|
|
3551
|
+
const commitBlocks = ({ document, op, anchor, blocks }) => {
|
|
3552
|
+
const body = storyBody(document, op.story);
|
|
3553
|
+
const before = blockListAt(body.content, anchor.list);
|
|
3554
|
+
if (structurallyEqual(before, blocks)) return Result.ok({
|
|
3555
|
+
document,
|
|
3556
|
+
inverse: [],
|
|
3557
|
+
touched: {
|
|
3558
|
+
modified: [],
|
|
3559
|
+
inserted: [],
|
|
3560
|
+
removed: []
|
|
3561
|
+
}
|
|
3562
|
+
});
|
|
3563
|
+
if (!blocks.some((block) => block.type === "paragraph" && idKey(block.paraId ?? "") === idKey(anchor.paragraph.paraId ?? ""))) return refuse$1({
|
|
3564
|
+
op,
|
|
3565
|
+
reason: DOCUMENT_OP_REFUSAL_REASONS.UNTRACKABLE,
|
|
3566
|
+
message: "The structural inverse requires a surviving paragraph in this block list."
|
|
3567
|
+
});
|
|
3568
|
+
if (blocks.length === 0 || !validStructure(before) || !validStructure(blocks) || endsItsContainer(body, {
|
|
3569
|
+
list: anchor.list,
|
|
3570
|
+
index: before.length - 1
|
|
3571
|
+
}) && (!endsInParagraph(before) || !endsInParagraph(blocks))) return refuse$1({
|
|
3572
|
+
op,
|
|
3573
|
+
reason: DOCUMENT_OP_REFUSAL_REASONS.STRUCTURE_MISMATCH,
|
|
3574
|
+
message: "A table has rows and cells; each cell and the story retain a final paragraph."
|
|
3575
|
+
});
|
|
3576
|
+
const fixedBlocks = (items) => items.filter((block) => block.type !== "table" && block.type !== "paragraph");
|
|
3577
|
+
if (!structurallyEqual(fixedBlocks(before), fixedBlocks(blocks))) return refuse$1({
|
|
3578
|
+
op,
|
|
3579
|
+
reason: DOCUMENT_OP_REFUSAL_REASONS.UNTRACKABLE,
|
|
3580
|
+
message: "A table structural inverse preserves surrounding block wrappers and markers."
|
|
3581
|
+
});
|
|
3582
|
+
const beforeParagraphs = paragraphsIn$1(before);
|
|
3583
|
+
const afterParagraphs = paragraphsIn$1(blocks);
|
|
3584
|
+
const beforeById = new Map(beforeParagraphs.map((paragraph) => [idKey(paragraph.paraId ?? ""), paragraph]));
|
|
3585
|
+
const afterById = new Map(afterParagraphs.map((paragraph) => [idKey(paragraph.paraId ?? ""), paragraph]));
|
|
3586
|
+
if ([...beforeParagraphs, ...afterParagraphs].some((paragraph) => {
|
|
3587
|
+
const id = idKey(paragraph.paraId ?? "");
|
|
3588
|
+
return !structurallyEqual(paragraph.sectionProperties, (beforeById.has(id) ? afterById : beforeById).get(id)?.sectionProperties);
|
|
3589
|
+
})) return refuse$1({
|
|
3590
|
+
op,
|
|
3591
|
+
reason: DOCUMENT_OP_REFUSAL_REASONS.SECTION_BOUNDARY,
|
|
3592
|
+
message: "Table edits preserve section boundaries."
|
|
3593
|
+
});
|
|
3594
|
+
if ([...beforeParagraphs, ...afterParagraphs].some(({ paraId }) => paraId === void 0 || (!beforeById.has(idKey(paraId)) || !afterById.has(idKey(paraId))) && !isParaId(paraId))) return refuse$1({
|
|
3595
|
+
op,
|
|
3596
|
+
reason: DOCUMENT_OP_REFUSAL_REASONS.INVALID_BLOCK_ID,
|
|
3597
|
+
message: "An added or removed paragraph needs a usable id."
|
|
3598
|
+
});
|
|
3599
|
+
if ([...beforeParagraphs, ...afterParagraphs].some(({ content }) => textsIn(content).some(hasIllegalXmlCharacters))) return refuse$1({
|
|
3600
|
+
op,
|
|
3601
|
+
reason: DOCUMENT_OP_REFUSAL_REASONS.INVALID_TEXT,
|
|
3602
|
+
message: "The table holds text that cannot be written to XML."
|
|
3603
|
+
});
|
|
3604
|
+
for (const location of storyParagraphs(body)) {
|
|
3605
|
+
const paragraph = location.paragraph;
|
|
3606
|
+
if (paragraph.pPrMark !== void 0 && beforeById.has(idKey(paragraph.paraId ?? "")) && endsItsContainer(body, location) && !permitsStructuralCellMark({
|
|
3607
|
+
body,
|
|
3608
|
+
location,
|
|
3609
|
+
op
|
|
3610
|
+
}) && !structurallyEqual(paragraph.pPrMark, afterById.get(idKey(paragraph.paraId ?? ""))?.pPrMark)) return refuse$1({
|
|
3611
|
+
op,
|
|
3612
|
+
reason: DOCUMENT_OP_REFUSAL_REASONS.CONTAINER_FINAL_MARK,
|
|
3613
|
+
message: "Removing this final paragraph mark would make the structural inverse invalid."
|
|
3614
|
+
});
|
|
3615
|
+
}
|
|
3616
|
+
const remaining = countIds(packageParagraphIds(document.package));
|
|
3617
|
+
for (const id of paragraphIdsIn(before)) remaining.set(idKey(id), (remaining.get(idKey(id)) ?? 1) - 1);
|
|
3618
|
+
if (collides(remaining, paragraphIdsIn(blocks))) return refuse$1({
|
|
3619
|
+
op,
|
|
3620
|
+
reason: DOCUMENT_OP_REFUSAL_REASONS.ID_COLLISION,
|
|
3621
|
+
message: "A paragraph id is already used in the package."
|
|
3622
|
+
});
|
|
3623
|
+
const records = countKeys(packageIdentityKeys(document.package));
|
|
3624
|
+
for (const key of identityKeysIn(before)) records.set(key, (records.get(key) ?? 1) - 1);
|
|
3625
|
+
const incoming = identityKeysIn(blocks);
|
|
3626
|
+
if (incoming.some((key) => (records.get(key) ?? 0) > 0) || new Set(incoming).size !== incoming.length) return refuse$1({
|
|
3627
|
+
op,
|
|
3628
|
+
reason: DOCUMENT_OP_REFUSAL_REASONS.ID_COLLISION,
|
|
3629
|
+
message: "A revision or content-control id is already used in the package."
|
|
3630
|
+
});
|
|
3631
|
+
const beforeBlocksById = new Map(before.flatMap((block) => {
|
|
3632
|
+
const id = paragraphIdsIn(block).at(0);
|
|
3633
|
+
return id === void 0 ? [] : [[idKey(id), block]];
|
|
3634
|
+
}));
|
|
3635
|
+
const reuseParagraphs = (items) => items.map((block) => {
|
|
3636
|
+
switch (block.type) {
|
|
3637
|
+
case "paragraph": {
|
|
3638
|
+
const known = beforeById.get(idKey(block.paraId ?? ""));
|
|
3639
|
+
return known !== void 0 && structurallyEqual(known, block) ? known : block;
|
|
3640
|
+
}
|
|
3641
|
+
case "table": return {
|
|
3642
|
+
...block,
|
|
3643
|
+
rows: block.rows.map((row) => ({
|
|
3644
|
+
...row,
|
|
3645
|
+
cells: row.cells.map((cell) => ({
|
|
3646
|
+
...cell,
|
|
3647
|
+
content: reuseParagraphs(cell.content)
|
|
3648
|
+
}))
|
|
3649
|
+
}))
|
|
3650
|
+
};
|
|
3651
|
+
case "blockSdt":
|
|
3652
|
+
case "blockCustomXml": return {
|
|
3653
|
+
...block,
|
|
3654
|
+
content: reuseParagraphs(block.content)
|
|
3655
|
+
};
|
|
3656
|
+
case "preservedBlock":
|
|
3657
|
+
case "bookmarkStart":
|
|
3658
|
+
case "bookmarkEnd": return block;
|
|
3659
|
+
default: return block;
|
|
3660
|
+
}
|
|
3661
|
+
});
|
|
3662
|
+
const replacement = blocks.map((block) => {
|
|
3663
|
+
const id = paragraphIdsIn(block).at(0);
|
|
3664
|
+
const existing = id === void 0 ? before.find((candidate) => structurallyEqual(candidate, block)) : beforeBlocksById.get(idKey(id));
|
|
3665
|
+
if (existing !== void 0 && structurallyEqual(existing, block)) return existing;
|
|
3666
|
+
const cloned = reuseParagraphs([structuredClone(block)]).at(0);
|
|
3667
|
+
if (cloned === void 0) return panic("Cloning one block must preserve one block.");
|
|
3668
|
+
return cloned;
|
|
3669
|
+
});
|
|
3670
|
+
const content = updateBlockList(body.content, anchor.list, () => replacement);
|
|
3671
|
+
const next = {
|
|
3672
|
+
...document,
|
|
3673
|
+
package: {
|
|
3674
|
+
...document.package,
|
|
3675
|
+
document: withBodyContent(body, content)
|
|
3676
|
+
}
|
|
3677
|
+
};
|
|
3678
|
+
const nextBody = storyBody(next, op.story);
|
|
3679
|
+
const previousParagraphs = new Map(storyParagraphs(body).map(({ paragraph }) => [idKey(paragraph.paraId ?? ""), paragraph]));
|
|
3680
|
+
for (const location of storyParagraphs(nextBody)) {
|
|
3681
|
+
const paragraph = location.paragraph;
|
|
3682
|
+
if (paragraph.pPrMark !== void 0 && endsItsContainer(nextBody, location) && !permitsStructuralCellMark({
|
|
3683
|
+
body: nextBody,
|
|
3684
|
+
location,
|
|
3685
|
+
op
|
|
3686
|
+
}) && !structurallyEqual(paragraph.pPrMark, previousParagraphs.get(idKey(paragraph.paraId ?? ""))?.pPrMark)) return refuse$1({
|
|
3687
|
+
op,
|
|
3688
|
+
reason: DOCUMENT_OP_REFUSAL_REASONS.CONTAINER_FINAL_MARK,
|
|
3689
|
+
message: "A table edit cannot add a final paragraph mark."
|
|
3690
|
+
});
|
|
3691
|
+
}
|
|
3692
|
+
const valid = validateOpsDocument(next);
|
|
3693
|
+
if (valid.isErr()) return refuse$1({
|
|
3694
|
+
op,
|
|
3695
|
+
reason: valid.error.reason,
|
|
3696
|
+
message: valid.error.message
|
|
3697
|
+
});
|
|
3698
|
+
return Result.ok({
|
|
3699
|
+
document: next,
|
|
3700
|
+
inverse: [{
|
|
3701
|
+
type: DOCUMENT_OP_TYPES.SET_CONTAINER_BLOCKS,
|
|
3702
|
+
story: op.story,
|
|
3703
|
+
blockId: anchor.paragraph.paraId ?? "",
|
|
3704
|
+
expected: replacement,
|
|
3705
|
+
blocks: before
|
|
3706
|
+
}],
|
|
3707
|
+
touched: {
|
|
3708
|
+
modified: afterParagraphs.filter((paragraph) => beforeById.has(idKey(paragraph.paraId ?? "")) && !structurallyEqual(paragraph, beforeById.get(idKey(paragraph.paraId ?? "")))).flatMap(({ paraId }) => paraId === void 0 ? [] : [paraId]),
|
|
3709
|
+
inserted: afterParagraphs.filter(({ paraId }) => !beforeById.has(idKey(paraId ?? ""))).flatMap(({ paraId }) => paraId === void 0 ? [] : [paraId]),
|
|
3710
|
+
removed: beforeParagraphs.filter(({ paraId }) => !afterById.has(idKey(paraId ?? ""))).flatMap(({ paraId }) => paraId === void 0 ? [] : [paraId])
|
|
3711
|
+
}
|
|
3712
|
+
});
|
|
3713
|
+
};
|
|
3714
|
+
const applyTableOp = (document, op) => {
|
|
3715
|
+
const body = storyBody(document, op.story);
|
|
3716
|
+
switch (op.type) {
|
|
3717
|
+
case DOCUMENT_OP_TYPES.INSERT_TABLE: {
|
|
3718
|
+
if (op.at.type !== "before" && op.at.type !== "after") return refuse$1({
|
|
3719
|
+
op,
|
|
3720
|
+
reason: DOCUMENT_OP_REFUSAL_REASONS.STRUCTURE_MISMATCH,
|
|
3721
|
+
message: "Insert before or after a paragraph."
|
|
3722
|
+
});
|
|
3723
|
+
const anchor = storyParagraphs(body).find(({ paragraph }) => idKey(paragraph.paraId ?? "") === idKey(op.at.blockId));
|
|
3724
|
+
if (anchor === void 0) return refuse$1({
|
|
3725
|
+
op,
|
|
3726
|
+
reason: DOCUMENT_OP_REFUSAL_REASONS.BLOCK_NOT_FOUND,
|
|
3727
|
+
message: "The table insertion anchor does not exist."
|
|
3728
|
+
});
|
|
3729
|
+
const blocks = [...blockListAt(body.content, anchor.list)];
|
|
3730
|
+
const terminal = op.at.type === "after" && endsItsContainer(body, anchor);
|
|
3731
|
+
if (op.terminal !== void 0 && !terminal) return refuse$1({
|
|
3732
|
+
op,
|
|
3733
|
+
reason: DOCUMENT_OP_REFUSAL_REASONS.STRUCTURE_MISMATCH,
|
|
3734
|
+
message: "A terminal insertion must follow its container's final paragraph."
|
|
3735
|
+
});
|
|
3736
|
+
if (terminal && op.terminal === void 0) return refuse$1({
|
|
3737
|
+
op,
|
|
3738
|
+
reason: DOCUMENT_OP_REFUSAL_REASONS.STRUCTURE_MISMATCH,
|
|
3739
|
+
message: "A terminal table insertion supplies the preceding paragraph id."
|
|
3740
|
+
});
|
|
3741
|
+
let preceding;
|
|
3742
|
+
if (op.terminal !== void 0) {
|
|
3743
|
+
if (op.revision !== void 0 && anchor.paragraph.pPrMark !== void 0) return refuse$1({
|
|
3744
|
+
op,
|
|
3745
|
+
reason: DOCUMENT_OP_REFUSAL_REASONS.REVISION_CONFLICT,
|
|
3746
|
+
message: "The final paragraph already carries a mark revision."
|
|
3747
|
+
});
|
|
3748
|
+
preceding = {
|
|
3749
|
+
type: "paragraph",
|
|
3750
|
+
paraId: op.terminal.beforeBlockId,
|
|
3751
|
+
...anchor.paragraph.formatting === void 0 ? {} : { formatting: anchor.paragraph.formatting },
|
|
3752
|
+
content: anchor.paragraph.content
|
|
3753
|
+
};
|
|
3754
|
+
blocks.splice(anchor.index, 1, preceding, op.table, {
|
|
3755
|
+
...anchor.paragraph,
|
|
3756
|
+
content: []
|
|
3757
|
+
});
|
|
3758
|
+
} else blocks.splice(anchor.index + (op.at.type === "after" ? 1 : 0), 0, op.table);
|
|
3759
|
+
if (op.revision === void 0) return commitBlocks({
|
|
3760
|
+
document,
|
|
3761
|
+
op,
|
|
3762
|
+
anchor,
|
|
3763
|
+
blocks
|
|
3764
|
+
});
|
|
3765
|
+
if (op.table.preserved !== void 0 || op.table.bookmarks !== void 0 || op.table.carrierStack !== void 0 || (op.table.propertyChanges?.length ?? 0) > 0) return refuse$1({
|
|
3766
|
+
op,
|
|
3767
|
+
reason: DOCUMENT_OP_REFUSAL_REASONS.UNTRACKABLE,
|
|
3768
|
+
message: "Tracked table construction does not support captured markup or table property revisions."
|
|
3769
|
+
});
|
|
3770
|
+
const tracked = trackTableRows({
|
|
3771
|
+
document,
|
|
3772
|
+
op,
|
|
3773
|
+
rows: op.table.rows,
|
|
3774
|
+
revision: op.revision,
|
|
3775
|
+
newIds: op.newIds,
|
|
3776
|
+
kind: WRAP_KINDS.INSERTION
|
|
3777
|
+
});
|
|
3778
|
+
if (tracked.isErr()) return Result.err(tracked.error);
|
|
3779
|
+
const direct = commitBlocks({
|
|
3780
|
+
document,
|
|
3781
|
+
op,
|
|
3782
|
+
anchor,
|
|
3783
|
+
blocks
|
|
3784
|
+
});
|
|
3785
|
+
if (direct.isErr()) return direct;
|
|
3786
|
+
const table = {
|
|
3787
|
+
...op.table,
|
|
3788
|
+
rows: tracked.value
|
|
3789
|
+
};
|
|
3790
|
+
const tableIndex = anchor.index + (op.at.type === "after" ? 1 : 0);
|
|
3791
|
+
blocks[tableIndex] = table;
|
|
3792
|
+
if (preceding !== void 0) {
|
|
3793
|
+
const outside = new Set(packageIdentityKeys(document.package));
|
|
3794
|
+
for (const key of identityKeysIn(anchor.paragraph)) outside.delete(key);
|
|
3795
|
+
for (const key of identityKeysIn(table)) outside.add(key);
|
|
3796
|
+
const fresh = freshenIdentities({
|
|
3797
|
+
before: [anchor.paragraph],
|
|
3798
|
+
after: [{
|
|
3799
|
+
...preceding,
|
|
3800
|
+
pPrMark: {
|
|
3801
|
+
kind: "ins",
|
|
3802
|
+
info: stampInfo(op.revision)
|
|
3803
|
+
}
|
|
3804
|
+
}],
|
|
3805
|
+
newIds: op.newIds ?? {},
|
|
3806
|
+
usedElsewhere: () => outside
|
|
3807
|
+
});
|
|
3808
|
+
switch (fresh.kind) {
|
|
3809
|
+
case "needsIds": return refuse$1({
|
|
3810
|
+
op,
|
|
3811
|
+
reason: DOCUMENT_OP_REFUSAL_REASONS.NEEDS_NEW_IDS,
|
|
3812
|
+
message: "The preceding table break needs an additional revision id."
|
|
3813
|
+
});
|
|
3814
|
+
case "invalidId": return refuse$1({
|
|
3815
|
+
op,
|
|
3816
|
+
reason: DOCUMENT_OP_REFUSAL_REASONS.INVALID_NEW_ID,
|
|
3817
|
+
message: "The preceding table break has an invalid revision id."
|
|
3818
|
+
});
|
|
3819
|
+
case "fresh": {
|
|
3820
|
+
const paragraph = fresh.paragraphs.at(0);
|
|
3821
|
+
if (paragraph === void 0) return panic("A terminal table insertion must retain its preceding paragraph.");
|
|
3822
|
+
blocks[anchor.index] = paragraph;
|
|
3823
|
+
break;
|
|
3824
|
+
}
|
|
3825
|
+
default: return fresh;
|
|
3826
|
+
}
|
|
3827
|
+
}
|
|
3828
|
+
return commitBlocks({
|
|
3829
|
+
document,
|
|
3830
|
+
op,
|
|
3831
|
+
anchor,
|
|
3832
|
+
blocks
|
|
3833
|
+
});
|
|
3834
|
+
}
|
|
3835
|
+
case DOCUMENT_OP_TYPES.DELETE_TABLE: {
|
|
3836
|
+
const located = locateTableRow(document, op);
|
|
3837
|
+
if (located.isErr()) return Result.err(located.error);
|
|
3838
|
+
const location = located.value;
|
|
3839
|
+
if (op.expected !== void 0 && !equalForStaleness(location.table, op.expected)) return refuse$1({
|
|
3840
|
+
op,
|
|
3841
|
+
reason: DOCUMENT_OP_REFUSAL_REASONS.STALE,
|
|
3842
|
+
message: "The table to remove has changed."
|
|
3843
|
+
});
|
|
3844
|
+
const anchor = storyParagraphs(body).find((paragraph) => sameBlockList(paragraph.list, location.list));
|
|
3845
|
+
if (anchor === void 0) return refuse$1({
|
|
3846
|
+
op,
|
|
3847
|
+
reason: DOCUMENT_OP_REFUSAL_REASONS.UNTRACKABLE,
|
|
3848
|
+
message: "Removing a table requires a surviving paragraph in its block list."
|
|
3849
|
+
});
|
|
3850
|
+
const blocks = [...blockListAt(body.content, location.list)];
|
|
3851
|
+
if (op.revision === void 0) blocks.splice(location.index, 1);
|
|
3852
|
+
else {
|
|
3853
|
+
if (location.table.preserved !== void 0 || location.table.bookmarks !== void 0 || location.table.carrierStack !== void 0 || (location.table.propertyChanges?.length ?? 0) > 0) return refuse$1({
|
|
3854
|
+
op,
|
|
3855
|
+
reason: DOCUMENT_OP_REFUSAL_REASONS.UNTRACKABLE,
|
|
3856
|
+
message: "Tracked table deletion does not support captured markup or table property revisions."
|
|
3857
|
+
});
|
|
3858
|
+
const tracked = trackTableRows({
|
|
3859
|
+
document,
|
|
3860
|
+
op,
|
|
3861
|
+
rows: location.table.rows,
|
|
3862
|
+
revision: op.revision,
|
|
3863
|
+
newIds: op.newIds,
|
|
3864
|
+
kind: WRAP_KINDS.DELETION
|
|
3865
|
+
});
|
|
3866
|
+
if (tracked.isErr()) return Result.err(tracked.error);
|
|
3867
|
+
blocks[location.index] = {
|
|
3868
|
+
...location.table,
|
|
3869
|
+
rows: tracked.value
|
|
3870
|
+
};
|
|
3871
|
+
}
|
|
3872
|
+
return commitBlocks({
|
|
3873
|
+
document,
|
|
3874
|
+
op,
|
|
3875
|
+
anchor,
|
|
3876
|
+
blocks
|
|
3877
|
+
});
|
|
3878
|
+
}
|
|
3879
|
+
case DOCUMENT_OP_TYPES.SET_CONTAINER_BLOCKS: {
|
|
3880
|
+
const anchor = storyParagraphs(body).find(({ paragraph }) => idKey(paragraph.paraId ?? "") === idKey(op.blockId));
|
|
3881
|
+
if (anchor === void 0) return refuse$1({
|
|
3882
|
+
op,
|
|
3883
|
+
reason: DOCUMENT_OP_REFUSAL_REASONS.BLOCK_NOT_FOUND,
|
|
3884
|
+
message: "The structural inverse anchor does not exist."
|
|
3885
|
+
});
|
|
3886
|
+
if (!equalForStaleness(blockListAt(body.content, anchor.list), op.expected)) return refuse$1({
|
|
3887
|
+
op,
|
|
3888
|
+
reason: DOCUMENT_OP_REFUSAL_REASONS.STALE,
|
|
3889
|
+
message: "The container blocks have changed."
|
|
3890
|
+
});
|
|
3891
|
+
return commitBlocks({
|
|
3892
|
+
document,
|
|
3893
|
+
op,
|
|
3894
|
+
anchor,
|
|
3895
|
+
blocks: op.blocks
|
|
3896
|
+
});
|
|
3897
|
+
}
|
|
3898
|
+
default: return op;
|
|
3899
|
+
}
|
|
3900
|
+
};
|
|
3901
|
+
//#endregion
|
|
3902
|
+
//#region src/ops/tableRows.ts
|
|
3903
|
+
/** Row edits and their exact inverse, addressed through a paragraph in the row. */
|
|
3904
|
+
const refused = ({ op, reason, message }) => Result.err(new DocumentOpRefusal({
|
|
3905
|
+
opType: op.type,
|
|
3906
|
+
reason,
|
|
3907
|
+
message
|
|
3908
|
+
}));
|
|
3909
|
+
const paragraphsIn = (rows) => storyParagraphs({ content: [{
|
|
3910
|
+
type: "table",
|
|
3911
|
+
rows: [...rows]
|
|
3912
|
+
}] }).map(({ paragraph }) => paragraph);
|
|
3913
|
+
/** Indexed markup and shared row wrappers need a structural table operation. */
|
|
3914
|
+
const needsTableEdit = (table) => table.preserved !== void 0 || table.bookmarks !== void 0 || table.carrierStack !== void 0 || table.rows.some((row) => row.contentControls !== void 0 || row.carrierStack !== void 0);
|
|
3915
|
+
const withRows = ({ document, story, location, rows }) => {
|
|
3916
|
+
const body = storyBody(document, story);
|
|
3917
|
+
const content = updateBlockList(body.content, location.list, (blocks) => {
|
|
3918
|
+
const out = [...blocks];
|
|
3919
|
+
out[location.index] = {
|
|
3920
|
+
...location.table,
|
|
3921
|
+
rows
|
|
3922
|
+
};
|
|
3923
|
+
return out;
|
|
3924
|
+
});
|
|
3925
|
+
switch (story) {
|
|
3926
|
+
case OP_STORIES.MAIN: return {
|
|
3927
|
+
...document,
|
|
3928
|
+
package: {
|
|
3929
|
+
...document.package,
|
|
3930
|
+
document: withBodyContent(body, content)
|
|
3931
|
+
}
|
|
3932
|
+
};
|
|
3933
|
+
default: return story;
|
|
3934
|
+
}
|
|
3935
|
+
};
|
|
3936
|
+
const commitRows = ({ document, op, location, rows }) => {
|
|
3937
|
+
if (rows.length === 0) return applyTableOp(document, {
|
|
3938
|
+
type: DOCUMENT_OP_TYPES.DELETE_TABLE,
|
|
3939
|
+
story: op.story,
|
|
3940
|
+
blockId: op.blockId,
|
|
3941
|
+
expected: location.table
|
|
3942
|
+
}).mapError((error) => new DocumentOpRefusal({
|
|
3943
|
+
opType: op.type,
|
|
3944
|
+
reason: error.reason,
|
|
3945
|
+
message: error.message
|
|
3946
|
+
}));
|
|
3947
|
+
const before = location.table.rows;
|
|
3948
|
+
if (structurallyEqual(before, rows)) return Result.ok({
|
|
3949
|
+
document,
|
|
3950
|
+
inverse: [],
|
|
3951
|
+
touched: {
|
|
3952
|
+
modified: [],
|
|
3953
|
+
inserted: [],
|
|
3954
|
+
removed: []
|
|
3955
|
+
}
|
|
3956
|
+
});
|
|
3957
|
+
const afterParagraphs = paragraphsIn(rows);
|
|
3958
|
+
if (rows.some((row) => row.cells.length === 0 || row.cells.some((cell) => storyParagraphs({ content: cell.content }).length === 0))) return refused({
|
|
3959
|
+
op,
|
|
3960
|
+
reason: DOCUMENT_OP_REFUSAL_REASONS.STRUCTURE_MISMATCH,
|
|
3961
|
+
message: "Every row has cells, and every cell has a paragraph."
|
|
3962
|
+
});
|
|
3963
|
+
if (afterParagraphs.some(({ content }) => textsIn(content).some(hasIllegalXmlCharacters))) return refused({
|
|
3964
|
+
op,
|
|
3965
|
+
reason: DOCUMENT_OP_REFUSAL_REASONS.INVALID_TEXT,
|
|
3966
|
+
message: "The row holds text that cannot be written to XML."
|
|
3967
|
+
});
|
|
3968
|
+
const body = { content: [{
|
|
3969
|
+
type: "table",
|
|
3970
|
+
rows
|
|
3971
|
+
}] };
|
|
3972
|
+
const originalParagraphs = new Map(paragraphsIn(before).map((paragraph) => [idKey(paragraph.paraId ?? ""), paragraph]));
|
|
3973
|
+
for (const paragraphLocation of storyParagraphs(body)) {
|
|
3974
|
+
const mark = paragraphLocation.paragraph.pPrMark;
|
|
3975
|
+
if (mark !== void 0 && endsItsContainer(body, paragraphLocation) && !permitsCellFinalMark(body, paragraphLocation) && !structurallyEqual(mark, originalParagraphs.get(idKey(paragraphLocation.paragraph.paraId ?? ""))?.pPrMark)) return refused({
|
|
3976
|
+
op,
|
|
3977
|
+
reason: DOCUMENT_OP_REFUSAL_REASONS.CONTAINER_FINAL_MARK,
|
|
3978
|
+
message: "A cell-final mark must belong to its row structural revision."
|
|
3979
|
+
});
|
|
3980
|
+
}
|
|
3981
|
+
const beforeIds = new Set(paragraphIdsIn(before).map(idKey));
|
|
3982
|
+
if (afterParagraphs.some(({ paraId }) => paraId === void 0 || !beforeIds.has(idKey(paraId)) && !isParaId(paraId))) return refused({
|
|
3983
|
+
op,
|
|
3984
|
+
reason: DOCUMENT_OP_REFUSAL_REASONS.INVALID_BLOCK_ID,
|
|
3985
|
+
message: "An added paragraph has no usable id."
|
|
3986
|
+
});
|
|
3987
|
+
const remainingParagraphs = countIds(packageParagraphIds(document.package));
|
|
3988
|
+
for (const id of paragraphIdsIn(before)) remainingParagraphs.set(idKey(id), (remainingParagraphs.get(idKey(id)) ?? 1) - 1);
|
|
3989
|
+
if (collides(remainingParagraphs, paragraphIdsIn(rows))) return refused({
|
|
3990
|
+
op,
|
|
3991
|
+
reason: DOCUMENT_OP_REFUSAL_REASONS.ID_COLLISION,
|
|
3992
|
+
message: "A row paragraph id is already used in the package."
|
|
3993
|
+
});
|
|
3994
|
+
const remainingRecords = countKeys(packageIdentityKeys(document.package));
|
|
3995
|
+
for (const key of identityKeysIn(before)) remainingRecords.set(key, (remainingRecords.get(key) ?? 1) - 1);
|
|
3996
|
+
const incomingRecords = identityKeysIn(rows);
|
|
3997
|
+
if (incomingRecords.some((key) => (remainingRecords.get(key) ?? 0) > 0) || new Set(incomingRecords).size !== incomingRecords.length) return refused({
|
|
3998
|
+
op,
|
|
3999
|
+
reason: DOCUMENT_OP_REFUSAL_REASONS.ID_COLLISION,
|
|
4000
|
+
message: "A row revision or content-control id is already used in the package."
|
|
4001
|
+
});
|
|
4002
|
+
const next = withRows({
|
|
4003
|
+
document,
|
|
4004
|
+
story: op.story,
|
|
4005
|
+
location,
|
|
4006
|
+
rows
|
|
4007
|
+
});
|
|
4008
|
+
const valid = validateOpsDocument(next);
|
|
4009
|
+
if (valid.isErr()) return refused({
|
|
4010
|
+
op,
|
|
4011
|
+
reason: valid.error.reason,
|
|
4012
|
+
message: valid.error.message
|
|
4013
|
+
});
|
|
4014
|
+
const afterIds = new Set(paragraphIdsIn(rows).map(idKey));
|
|
4015
|
+
const beforeParagraphIds = paragraphIdsIn(before);
|
|
4016
|
+
const afterParagraphIds = paragraphIdsIn(rows);
|
|
4017
|
+
const changedIds = new Set(rows.filter((row) => !before.includes(row)).flatMap(paragraphIdsIn).map(idKey));
|
|
4018
|
+
const anchor = tableRowAnchor(rows);
|
|
4019
|
+
if (anchor === void 0) return refused({
|
|
4020
|
+
op,
|
|
4021
|
+
reason: DOCUMENT_OP_REFUSAL_REASONS.UNTRACKABLE,
|
|
4022
|
+
message: "The table has no paragraph that can address its rows."
|
|
4023
|
+
});
|
|
4024
|
+
return Result.ok({
|
|
4025
|
+
document: next,
|
|
4026
|
+
inverse: [{
|
|
4027
|
+
type: DOCUMENT_OP_TYPES.SET_TABLE_ROWS,
|
|
4028
|
+
story: op.story,
|
|
4029
|
+
blockId: anchor,
|
|
4030
|
+
expected: rows,
|
|
4031
|
+
rows: before
|
|
4032
|
+
}],
|
|
4033
|
+
touched: {
|
|
4034
|
+
modified: beforeParagraphIds.filter((id) => afterIds.has(idKey(id)) && changedIds.has(idKey(id))),
|
|
4035
|
+
inserted: afterParagraphIds.filter((id) => !beforeIds.has(idKey(id))),
|
|
4036
|
+
removed: beforeParagraphIds.filter((id) => !afterIds.has(idKey(id)))
|
|
3344
4037
|
}
|
|
3345
4038
|
});
|
|
3346
|
-
|
|
3347
|
-
|
|
3348
|
-
|
|
3349
|
-
|
|
3350
|
-
|
|
3351
|
-
const
|
|
3352
|
-
const
|
|
3353
|
-
|
|
3354
|
-
|
|
3355
|
-
|
|
3356
|
-
|
|
3357
|
-
|
|
3358
|
-
|
|
3359
|
-
|
|
3360
|
-
|
|
3361
|
-
|
|
3362
|
-
|
|
3363
|
-
|
|
3364
|
-
|
|
3365
|
-
|
|
3366
|
-
|
|
3367
|
-
|
|
3368
|
-
|
|
4039
|
+
};
|
|
4040
|
+
const applyRowOp = (document, op) => {
|
|
4041
|
+
const located = locateTableRow(document, op);
|
|
4042
|
+
if (located.isErr()) return Result.err(located.error);
|
|
4043
|
+
const location = located.value;
|
|
4044
|
+
const before = location.table.rows;
|
|
4045
|
+
const rows = [...before];
|
|
4046
|
+
switch (op.type) {
|
|
4047
|
+
case DOCUMENT_OP_TYPES.INSERT_ROW: {
|
|
4048
|
+
if (!Number.isInteger(op.at) || op.at < 0 || op.at > rows.length) return refused({
|
|
4049
|
+
op,
|
|
4050
|
+
reason: DOCUMENT_OP_REFUSAL_REASONS.INVALID_OFFSET,
|
|
4051
|
+
message: "The row index is outside the table."
|
|
4052
|
+
});
|
|
4053
|
+
if (needsTableEdit(location.table)) return refused({
|
|
4054
|
+
op,
|
|
4055
|
+
reason: DOCUMENT_OP_REFUSAL_REASONS.UNTRACKABLE,
|
|
4056
|
+
message: "Indexed table markup or shared row wrappers require a table operation."
|
|
4057
|
+
});
|
|
4058
|
+
const incoming = structuredClone(op.row);
|
|
4059
|
+
if (paragraphIdsIn(incoming).some((id) => paragraphIdsIn(before).some((existing) => idKey(existing) === idKey(id)))) return refused({
|
|
4060
|
+
op,
|
|
4061
|
+
reason: DOCUMENT_OP_REFUSAL_REASONS.ID_COLLISION,
|
|
4062
|
+
message: "An inserted paragraph id is already used in the table."
|
|
4063
|
+
});
|
|
4064
|
+
const tracked = op.revision === void 0 ? Result.ok([incoming]) : trackTableRows({
|
|
4065
|
+
document,
|
|
4066
|
+
op,
|
|
4067
|
+
rows: [incoming],
|
|
4068
|
+
revision: op.revision,
|
|
4069
|
+
newIds: op.newIds,
|
|
4070
|
+
kind: WRAP_KINDS.INSERTION
|
|
4071
|
+
});
|
|
4072
|
+
if (tracked.isErr()) return Result.err(tracked.error);
|
|
4073
|
+
const row = tracked.value.at(0) ?? panic("A tracked insertion lost its row.");
|
|
4074
|
+
rows.splice(op.at, 0, row);
|
|
4075
|
+
return commitRows({
|
|
4076
|
+
document,
|
|
4077
|
+
op,
|
|
4078
|
+
location,
|
|
4079
|
+
rows
|
|
3369
4080
|
});
|
|
3370
4081
|
}
|
|
3371
|
-
|
|
3372
|
-
|
|
3373
|
-
|
|
3374
|
-
|
|
3375
|
-
|
|
3376
|
-
|
|
3377
|
-
|
|
3378
|
-
|
|
3379
|
-
|
|
3380
|
-
|
|
3381
|
-
|
|
3382
|
-
|
|
3383
|
-
|
|
3384
|
-
|
|
3385
|
-
|
|
3386
|
-
|
|
3387
|
-
|
|
3388
|
-
|
|
3389
|
-
|
|
3390
|
-
|
|
3391
|
-
|
|
3392
|
-
|
|
3393
|
-
|
|
3394
|
-
|
|
3395
|
-
|
|
3396
|
-
|
|
3397
|
-
|
|
3398
|
-
|
|
3399
|
-
|
|
3400
|
-
|
|
3401
|
-
|
|
3402
|
-
|
|
3403
|
-
|
|
3404
|
-
|
|
3405
|
-
|
|
3406
|
-
|
|
3407
|
-
|
|
3408
|
-
|
|
4082
|
+
case DOCUMENT_OP_TYPES.DELETE_ROW: {
|
|
4083
|
+
const row = before[location.rowIndex];
|
|
4084
|
+
if (row === void 0) return panic("A row location must name a row.");
|
|
4085
|
+
if (op.expected !== void 0 && !equalForStaleness(row, op.expected)) return refused({
|
|
4086
|
+
op,
|
|
4087
|
+
reason: DOCUMENT_OP_REFUSAL_REASONS.STALE,
|
|
4088
|
+
message: "The row to remove has changed."
|
|
4089
|
+
});
|
|
4090
|
+
if (op.revision === void 0 && before.length === 1) return commitRows({
|
|
4091
|
+
document,
|
|
4092
|
+
op,
|
|
4093
|
+
location,
|
|
4094
|
+
rows: []
|
|
4095
|
+
});
|
|
4096
|
+
if (needsTableEdit(location.table)) return refused({
|
|
4097
|
+
op,
|
|
4098
|
+
reason: DOCUMENT_OP_REFUSAL_REASONS.UNTRACKABLE,
|
|
4099
|
+
message: "Indexed table markup or shared row wrappers require a table operation."
|
|
4100
|
+
});
|
|
4101
|
+
if (op.revision === void 0) rows.splice(location.rowIndex, 1);
|
|
4102
|
+
else {
|
|
4103
|
+
const tracked = trackTableRows({
|
|
4104
|
+
document,
|
|
4105
|
+
op,
|
|
4106
|
+
rows: [row],
|
|
4107
|
+
revision: op.revision,
|
|
4108
|
+
newIds: op.newIds,
|
|
4109
|
+
kind: WRAP_KINDS.DELETION
|
|
4110
|
+
});
|
|
4111
|
+
if (tracked.isErr()) return Result.err(tracked.error);
|
|
4112
|
+
rows[location.rowIndex] = tracked.value.at(0) ?? panic("A tracked deletion lost its row.");
|
|
4113
|
+
}
|
|
4114
|
+
return commitRows({
|
|
4115
|
+
document,
|
|
4116
|
+
op,
|
|
4117
|
+
location,
|
|
4118
|
+
rows
|
|
4119
|
+
});
|
|
4120
|
+
}
|
|
4121
|
+
case DOCUMENT_OP_TYPES.SET_TABLE_ROWS: {
|
|
4122
|
+
if (!equalForStaleness(before, op.expected)) return refused({
|
|
4123
|
+
op,
|
|
4124
|
+
reason: DOCUMENT_OP_REFUSAL_REASONS.STALE,
|
|
4125
|
+
message: "The table rows have changed."
|
|
4126
|
+
});
|
|
4127
|
+
if (op.rows.length > 0 && op.rows.length !== before.length && needsTableEdit(location.table)) return refused({
|
|
4128
|
+
op,
|
|
4129
|
+
reason: DOCUMENT_OP_REFUSAL_REASONS.UNTRACKABLE,
|
|
4130
|
+
message: "Indexed table markup or shared row wrappers require a table operation."
|
|
4131
|
+
});
|
|
4132
|
+
const replacement = op.rows.map((row) => before.find((existing) => structurallyEqual(existing, row)) ?? structuredClone(row));
|
|
4133
|
+
return commitRows({
|
|
4134
|
+
document,
|
|
4135
|
+
op,
|
|
4136
|
+
location,
|
|
4137
|
+
rows: replacement
|
|
4138
|
+
});
|
|
4139
|
+
}
|
|
4140
|
+
default: return op;
|
|
3409
4141
|
}
|
|
3410
|
-
return Result.ok(combineEdits(document, edits));
|
|
3411
4142
|
};
|
|
3412
4143
|
//#endregion
|
|
3413
4144
|
//#region src/ops/apply.ts
|
|
@@ -4066,10 +4797,12 @@ const isSplitHalf = (value) => value === SPLIT_HALVES.FIRST || value === SPLIT_H
|
|
|
4066
4797
|
* Record a split as tracked: the new mark is an insertion, and the new
|
|
4067
4798
|
* paragraph records a property change from the source's properties when it
|
|
4068
4799
|
* states other ones and carries no change already. Returns the refusal when
|
|
4069
|
-
* the operation states a mark or property change of its own
|
|
4800
|
+
* the operation states a mark or property change of its own, or other run
|
|
4801
|
+
* properties for the paragraph's own mark: no tracked change records those.
|
|
4070
4802
|
*/
|
|
4071
|
-
const trackSplit = ({ op, stamp, paragraph, first, made }) => {
|
|
4803
|
+
const trackSplit = ({ op, stamp, paragraph, first, made, madeSecond }) => {
|
|
4072
4804
|
if (op.firstMark !== void 0 || (op.newParagraph?.propertyChanges?.length ?? 0) > 0) return refusal(op, DOCUMENT_OP_REFUSAL_REASONS.REVISION_CONFLICT, "A tracked split records its own mark and property change.");
|
|
4805
|
+
if (madeSecond && !sameMarkFormatting(made.formatting, paragraph.formatting)) return refusal(op, DOCUMENT_OP_REFUSAL_REASONS.UNTRACKABLE, "A paragraph property change does not record the paragraph mark's run properties.");
|
|
4073
4806
|
first.pPrMark = {
|
|
4074
4807
|
kind: "ins",
|
|
4075
4808
|
info: stampInfo(stamp)
|
|
@@ -4128,7 +4861,8 @@ const splitBlock = (document, op) => {
|
|
|
4128
4861
|
stamp: op.revision,
|
|
4129
4862
|
paragraph,
|
|
4130
4863
|
first,
|
|
4131
|
-
made: madeHalf
|
|
4864
|
+
made: madeHalf,
|
|
4865
|
+
madeSecond: newHalf === SPLIT_HALVES.SECOND
|
|
4132
4866
|
});
|
|
4133
4867
|
if (tracked !== void 0) return Result.err(tracked);
|
|
4134
4868
|
}
|
|
@@ -4354,7 +5088,7 @@ const setParagraphReview = (document, op) => {
|
|
|
4354
5088
|
const review = structuredClone(op.review);
|
|
4355
5089
|
const next = withReviewFields(paragraph, review);
|
|
4356
5090
|
if (structurallyEqual(next, paragraph)) return unchanged(document);
|
|
4357
|
-
if (review.pPrMark !== void 0 && !structurallyEqual(review.pPrMark, paragraph.pPrMark) && endsItsContainer(storyBody(document, op.story), located.value)) return refuse(op, DOCUMENT_OP_REFUSAL_REASONS.CONTAINER_FINAL_MARK, `${op.blockId} ends its container: there is no next paragraph its mark could join.`);
|
|
5091
|
+
if (review.pPrMark !== void 0 && !structurallyEqual(review.pPrMark, paragraph.pPrMark) && endsItsContainer(storyBody(document, op.story), located.value) && !(located.value.list.some((step) => step.kind === "tableCell") && (review.pPrMark.kind === "ins" || review.pPrMark.kind === "del"))) return refuse(op, DOCUMENT_OP_REFUSAL_REASONS.CONTAINER_FINAL_MARK, `${op.blockId} ends its container: there is no next paragraph its mark could join.`);
|
|
4358
5092
|
const taken = identitiesOutside(document, [paragraph]);
|
|
4359
5093
|
for (const key of identityKeysIn(paragraph.content)) taken.add(key);
|
|
4360
5094
|
const incoming = reviewKeysOf(review);
|
|
@@ -4402,6 +5136,11 @@ const replaceInline = (document, op) => {
|
|
|
4402
5136
|
};
|
|
4403
5137
|
const dispatch = (document, op) => {
|
|
4404
5138
|
switch (op.type) {
|
|
5139
|
+
case DOCUMENT_OP_TYPES.DELETE_BLOCKS: return deleteBlocks({
|
|
5140
|
+
document,
|
|
5141
|
+
op,
|
|
5142
|
+
applyOps: applyDocumentOps
|
|
5143
|
+
});
|
|
4405
5144
|
case DOCUMENT_OP_TYPES.INSERT_BLOCKS: return insertBlocks({
|
|
4406
5145
|
document,
|
|
4407
5146
|
op,
|
|
@@ -4420,6 +5159,9 @@ const dispatch = (document, op) => {
|
|
|
4420
5159
|
case DOCUMENT_OP_TYPES.SET_PARAGRAPH_REVIEW: return setParagraphReview(document, op);
|
|
4421
5160
|
case DOCUMENT_OP_TYPES.REPLACE_INLINE: return replaceInline(document, op);
|
|
4422
5161
|
case DOCUMENT_OP_TYPES.RESOLVE_REVISION: return resolveRevision(document, op, applyDocumentOps);
|
|
5162
|
+
case DOCUMENT_OP_TYPES.INSERT_TABLE:
|
|
5163
|
+
case DOCUMENT_OP_TYPES.DELETE_TABLE:
|
|
5164
|
+
case DOCUMENT_OP_TYPES.SET_CONTAINER_BLOCKS: return applyTableOp(document, op);
|
|
4423
5165
|
case DOCUMENT_OP_TYPES.INSERT_ROW:
|
|
4424
5166
|
case DOCUMENT_OP_TYPES.DELETE_ROW:
|
|
4425
5167
|
case DOCUMENT_OP_TYPES.SET_TABLE_ROWS: return applyRowOp(document, op);
|
|
@@ -4429,6 +5171,7 @@ const dispatch = (document, op) => {
|
|
|
4429
5171
|
/** The stamp of a tracked operation; `undefined` for a direct one. */
|
|
4430
5172
|
const stampOf = (op) => {
|
|
4431
5173
|
switch (op.type) {
|
|
5174
|
+
case DOCUMENT_OP_TYPES.DELETE_BLOCKS:
|
|
4432
5175
|
case DOCUMENT_OP_TYPES.INSERT_BLOCKS:
|
|
4433
5176
|
case DOCUMENT_OP_TYPES.INSERT_TEXT:
|
|
4434
5177
|
case DOCUMENT_OP_TYPES.INSERT_CONTENT:
|
|
@@ -4438,14 +5181,17 @@ const stampOf = (op) => {
|
|
|
4438
5181
|
case DOCUMENT_OP_TYPES.SPLIT_BLOCK:
|
|
4439
5182
|
case DOCUMENT_OP_TYPES.JOIN_BLOCKS:
|
|
4440
5183
|
case DOCUMENT_OP_TYPES.INSERT_ROW:
|
|
4441
|
-
case DOCUMENT_OP_TYPES.DELETE_ROW:
|
|
5184
|
+
case DOCUMENT_OP_TYPES.DELETE_ROW:
|
|
5185
|
+
case DOCUMENT_OP_TYPES.INSERT_TABLE:
|
|
5186
|
+
case DOCUMENT_OP_TYPES.DELETE_TABLE: return op.revision;
|
|
4442
5187
|
case DOCUMENT_OP_TYPES.SPLIT_INLINE:
|
|
4443
5188
|
case DOCUMENT_OP_TYPES.JOIN_INLINE:
|
|
4444
5189
|
case DOCUMENT_OP_TYPES.REPLACE_BLOCKS:
|
|
4445
5190
|
case DOCUMENT_OP_TYPES.SET_PARAGRAPH_REVIEW:
|
|
4446
5191
|
case DOCUMENT_OP_TYPES.REPLACE_INLINE:
|
|
4447
5192
|
case DOCUMENT_OP_TYPES.RESOLVE_REVISION:
|
|
4448
|
-
case DOCUMENT_OP_TYPES.SET_TABLE_ROWS:
|
|
5193
|
+
case DOCUMENT_OP_TYPES.SET_TABLE_ROWS:
|
|
5194
|
+
case DOCUMENT_OP_TYPES.SET_CONTAINER_BLOCKS: return;
|
|
4449
5195
|
default: return op;
|
|
4450
5196
|
}
|
|
4451
5197
|
};
|
|
@@ -4455,11 +5201,7 @@ const recordedRevisions = (before, edit, stamp) => {
|
|
|
4455
5201
|
const paragraphs = storyParagraphs(edit.document.package.document).map(({ paragraph }) => paragraph).filter(({ paraId }) => paraId !== void 0 && touched.has(idKey(paraId)));
|
|
4456
5202
|
const known = new Set(packageIdentityKeys(before.package));
|
|
4457
5203
|
const revisions = stampedRevisionIds(paragraphs, stamp, known);
|
|
4458
|
-
|
|
4459
|
-
space: IDENTITY_SPACES.REVISION,
|
|
4460
|
-
id: stamp.id
|
|
4461
|
-
});
|
|
4462
|
-
if (!known.has(structuralKey) && packageIdentityKeys(edit.document.package).includes(structuralKey) && !revisions.includes(stamp.id)) revisions.unshift(stamp.id);
|
|
5204
|
+
revisions.unshift(...stampedTableRowRevisionIds(edit.document.package.document.content, stamp, known));
|
|
4463
5205
|
return revisions;
|
|
4464
5206
|
};
|
|
4465
5207
|
/** Apply one operation to a document that meets the seed contract. */
|
|
@@ -4515,6 +5257,7 @@ const usedIds = (document, space) => new Set(idsIn(packageIdentityKeys(document.
|
|
|
4515
5257
|
/** The same operation with other new ids; one that takes none is returned as it is. */
|
|
4516
5258
|
const withNewIds = (op, newIds) => {
|
|
4517
5259
|
switch (op.type) {
|
|
5260
|
+
case DOCUMENT_OP_TYPES.DELETE_BLOCKS:
|
|
4518
5261
|
case DOCUMENT_OP_TYPES.INSERT_BLOCKS:
|
|
4519
5262
|
case DOCUMENT_OP_TYPES.INSERT_TEXT:
|
|
4520
5263
|
case DOCUMENT_OP_TYPES.INSERT_CONTENT:
|
|
@@ -4524,7 +5267,9 @@ const withNewIds = (op, newIds) => {
|
|
|
4524
5267
|
case DOCUMENT_OP_TYPES.SPLIT_BLOCK:
|
|
4525
5268
|
case DOCUMENT_OP_TYPES.JOIN_BLOCKS:
|
|
4526
5269
|
case DOCUMENT_OP_TYPES.INSERT_ROW:
|
|
4527
|
-
case DOCUMENT_OP_TYPES.DELETE_ROW:
|
|
5270
|
+
case DOCUMENT_OP_TYPES.DELETE_ROW:
|
|
5271
|
+
case DOCUMENT_OP_TYPES.INSERT_TABLE:
|
|
5272
|
+
case DOCUMENT_OP_TYPES.DELETE_TABLE: return {
|
|
4528
5273
|
...op,
|
|
4529
5274
|
newIds
|
|
4530
5275
|
};
|
|
@@ -4534,7 +5279,8 @@ const withNewIds = (op, newIds) => {
|
|
|
4534
5279
|
case DOCUMENT_OP_TYPES.SET_PARAGRAPH_REVIEW:
|
|
4535
5280
|
case DOCUMENT_OP_TYPES.REPLACE_INLINE:
|
|
4536
5281
|
case DOCUMENT_OP_TYPES.RESOLVE_REVISION:
|
|
4537
|
-
case DOCUMENT_OP_TYPES.SET_TABLE_ROWS:
|
|
5282
|
+
case DOCUMENT_OP_TYPES.SET_TABLE_ROWS:
|
|
5283
|
+
case DOCUMENT_OP_TYPES.SET_CONTAINER_BLOCKS: return op;
|
|
4538
5284
|
default: return op;
|
|
4539
5285
|
}
|
|
4540
5286
|
};
|
|
@@ -4589,14 +5335,9 @@ const revisionIdDemand = (document, op) => {
|
|
|
4589
5335
|
}
|
|
4590
5336
|
return Result.ok(high);
|
|
4591
5337
|
};
|
|
4592
|
-
const COMMENT_ANCHORS = /* @__PURE__ */ new Set([
|
|
4593
|
-
"commentRangeStart",
|
|
4594
|
-
"commentRangeEnd",
|
|
4595
|
-
"commentReference"
|
|
4596
|
-
]);
|
|
4597
5338
|
const leafPlan = ({ node, ancestors }, author) => {
|
|
4598
5339
|
if (ancestors.some(isRemovedRevisionNode)) return "untouched";
|
|
4599
|
-
if (
|
|
5340
|
+
if (isCommentAnchor(node)) return "anchor";
|
|
4600
5341
|
return ancestors.some((ancestor) => ancestor.type === "insertion" && isTrackedWrapper(ancestor) && ancestor.info.author === author) ? "direct" : "tracked";
|
|
4601
5342
|
};
|
|
4602
5343
|
const laterGap = (left, right) => compareGaps(left, right) >= 0 ? left : right;
|
|
@@ -4647,63 +5388,27 @@ const outOfIds = () => Result.err(new DocumentOpRefusal({
|
|
|
4647
5388
|
reason: DOCUMENT_OP_REFUSAL_REASONS.NEEDS_NEW_IDS,
|
|
4648
5389
|
opType: DOCUMENT_OP_TYPES.DELETE_RANGE
|
|
4649
5390
|
}));
|
|
4650
|
-
/**
|
|
4651
|
-
|
|
4652
|
-
* order as one batch: a direct `deleteRange` for each stretch of the author's
|
|
4653
|
-
* own insertions (retracting a suggestion removes it), and a tracked one for
|
|
4654
|
-
* each other stretch, split around comment boundaries and references, which
|
|
4655
|
-
* no tracked change can hold and which therefore stay. Stretches go from the
|
|
4656
|
-
* last to the first, so each position still names what it named in the
|
|
4657
|
-
* input. The positions default their `zeroWidthBefore` as `deleteRange` does.
|
|
4658
|
-
*
|
|
4659
|
-
* The first tracked deletion carries the stamp's id. Each later one takes the
|
|
4660
|
-
* next id of `newIds.revision` the package does not use for its stamp, then
|
|
4661
|
-
* the ones it needs for the records it cuts.
|
|
4662
|
-
*/
|
|
4663
|
-
const planTrackedDeletion = (document, options) => {
|
|
4664
|
-
const { from, to, revision, newIds } = options;
|
|
4665
|
-
const check = applyDocumentOp(document, {
|
|
4666
|
-
type: DOCUMENT_OP_TYPES.DELETE_RANGE,
|
|
4667
|
-
from,
|
|
4668
|
-
to
|
|
4669
|
-
});
|
|
4670
|
-
if (check.isErr()) return Result.err(check.error);
|
|
4671
|
-
const location = storyParagraphs(storyBody(document, from.story)).find(({ paragraph }) => idKey(paragraph.paraId ?? "") === idKey(from.blockId));
|
|
4672
|
-
if (location === void 0) return Result.ok([]);
|
|
4673
|
-
const { content } = location.paragraph;
|
|
4674
|
-
const start = {
|
|
4675
|
-
offset: from.offset,
|
|
4676
|
-
zeroWidthBefore: from.zeroWidthBefore ?? zeroWidthLeavesAt(content, from.offset).length
|
|
4677
|
-
};
|
|
4678
|
-
const end = {
|
|
4679
|
-
offset: to.offset,
|
|
4680
|
-
zeroWidthBefore: to.zeroWidthBefore ?? 0
|
|
4681
|
-
};
|
|
4682
|
-
const segments = segmentsOf(leafSpans(content), start, end, revision.author);
|
|
5391
|
+
/** Shared allocation and simulation for all pieces of one planned edit. */
|
|
5392
|
+
const createTrackedPlan = ({ document, revision, newIds }) => {
|
|
4683
5393
|
const used = usedIds(document, IDENTITY_SPACES.REVISION);
|
|
4684
|
-
const pool = (newIds?.revision ?? []).filter((id) => !used.has(id) && id !== revision.id);
|
|
5394
|
+
const pool = [...new Set(newIds?.revision ?? [])].filter((id) => !used.has(id) && id !== revision.id);
|
|
4685
5395
|
let taken = 0;
|
|
5396
|
+
let stampUsed = false;
|
|
5397
|
+
let current = document;
|
|
5398
|
+
const ops = [];
|
|
4686
5399
|
const take = (count) => {
|
|
4687
5400
|
if (taken + count > pool.length) return void 0;
|
|
4688
5401
|
const ids = pool.slice(taken, taken + count);
|
|
4689
5402
|
taken += count;
|
|
4690
5403
|
return ids;
|
|
4691
5404
|
};
|
|
4692
|
-
const
|
|
4693
|
-
|
|
4694
|
-
|
|
4695
|
-
|
|
4696
|
-
let op = {
|
|
4697
|
-
type: DOCUMENT_OP_TYPES.DELETE_RANGE,
|
|
4698
|
-
from: positionOf(from, segment.from),
|
|
4699
|
-
to: positionOf(from, segment.to)
|
|
4700
|
-
};
|
|
4701
|
-
if (segment.plan === "tracked") {
|
|
4702
|
-
const stampId = stampUsed ? take(1)?.[0] : revision.id;
|
|
5405
|
+
const append = (input) => {
|
|
5406
|
+
let op = input;
|
|
5407
|
+
if (input.revision !== void 0) {
|
|
5408
|
+
const stampId = stampUsed ? take(1)?.at(0) : revision.id;
|
|
4703
5409
|
if (stampId === void 0) return outOfIds();
|
|
4704
|
-
stampUsed = true;
|
|
4705
5410
|
op = {
|
|
4706
|
-
...
|
|
5411
|
+
...input,
|
|
4707
5412
|
revision: {
|
|
4708
5413
|
...revision,
|
|
4709
5414
|
id: stampId
|
|
@@ -4713,17 +5418,237 @@ const planTrackedDeletion = (document, options) => {
|
|
|
4713
5418
|
if (demand.isErr()) return Result.err(demand.error);
|
|
4714
5419
|
const ids = take(demand.value);
|
|
4715
5420
|
if (ids === void 0) return outOfIds();
|
|
4716
|
-
|
|
4717
|
-
|
|
4718
|
-
|
|
4719
|
-
|
|
5421
|
+
const controls = usedIds(current, IDENTITY_SPACES.CONTROL);
|
|
5422
|
+
op = withNewIds(op, {
|
|
5423
|
+
revision: ids,
|
|
5424
|
+
control: (newIds?.control ?? []).filter((id) => !controls.has(id))
|
|
5425
|
+
});
|
|
4720
5426
|
}
|
|
4721
5427
|
const applied = applyDocumentOp(current, op);
|
|
4722
5428
|
if (applied.isErr()) return Result.err(applied.error);
|
|
5429
|
+
stampUsed ||= applied.value.revisions.length > 0;
|
|
4723
5430
|
current = applied.value.document;
|
|
4724
5431
|
ops.push(op);
|
|
5432
|
+
return Result.ok(void 0);
|
|
5433
|
+
};
|
|
5434
|
+
return {
|
|
5435
|
+
append,
|
|
5436
|
+
document: () => current,
|
|
5437
|
+
ops
|
|
5438
|
+
};
|
|
5439
|
+
};
|
|
5440
|
+
/** Validate and segment each paragraph before planning any edits. */
|
|
5441
|
+
const appendTrackedDeletion = ({ document, options, plan }) => {
|
|
5442
|
+
const { from, to, revision } = options;
|
|
5443
|
+
const refuse = (reason, message) => Result.err(new DocumentOpRefusal({
|
|
5444
|
+
reason,
|
|
5445
|
+
message,
|
|
5446
|
+
opType: DOCUMENT_OP_TYPES.DELETE_RANGE
|
|
5447
|
+
}));
|
|
5448
|
+
if (from.story !== to.story) return refuse(DOCUMENT_OP_REFUSAL_REASONS.UNTRACKABLE, "A planned range must stay in one story.");
|
|
5449
|
+
const body = storyBody(document, from.story);
|
|
5450
|
+
const paragraphs = storyParagraphs(body);
|
|
5451
|
+
const first = paragraphs.find(({ paragraph }) => idKey(paragraph.paraId ?? "") === idKey(from.blockId));
|
|
5452
|
+
const last = paragraphs.find(({ paragraph }) => idKey(paragraph.paraId ?? "") === idKey(to.blockId));
|
|
5453
|
+
if (first === void 0 || last === void 0) return refuse(DOCUMENT_OP_REFUSAL_REASONS.BLOCK_NOT_FOUND, "A range endpoint does not exist.");
|
|
5454
|
+
if (!sameBlockList(first.list, last.list)) return refuse(DOCUMENT_OP_REFUSAL_REASONS.UNTRACKABLE, "A planned range must stay in one block list.");
|
|
5455
|
+
if (first.index > last.index) return refuse(DOCUMENT_OP_REFUSAL_REASONS.NOT_ADJACENT, "Range endpoints must be in document order.");
|
|
5456
|
+
const list = blockListAt(body.content, first.list);
|
|
5457
|
+
const ranges = [];
|
|
5458
|
+
for (let index = first.index; index <= last.index; index += 1) {
|
|
5459
|
+
const paragraph = list[index];
|
|
5460
|
+
if (paragraph?.type !== "paragraph") return refuse(DOCUMENT_OP_REFUSAL_REASONS.UNTRACKABLE, "A planned range cannot cross a non-paragraph block.");
|
|
5461
|
+
const at = {
|
|
5462
|
+
story: from.story,
|
|
5463
|
+
blockId: paragraph.paraId ?? "",
|
|
5464
|
+
offset: 0
|
|
5465
|
+
};
|
|
5466
|
+
const start = index === first.index ? from : {
|
|
5467
|
+
...at,
|
|
5468
|
+
zeroWidthBefore: 0
|
|
5469
|
+
};
|
|
5470
|
+
const end = index === last.index ? to : {
|
|
5471
|
+
...at,
|
|
5472
|
+
offset: paragraphLength(paragraph),
|
|
5473
|
+
zeroWidthBefore: zeroWidthLeavesAt(paragraph.content, paragraphLength(paragraph)).length
|
|
5474
|
+
};
|
|
5475
|
+
const check = applyDocumentOp(document, {
|
|
5476
|
+
type: DOCUMENT_OP_TYPES.DELETE_RANGE,
|
|
5477
|
+
from: start,
|
|
5478
|
+
to: end
|
|
5479
|
+
});
|
|
5480
|
+
if (check.isErr()) return Result.err(check.error);
|
|
5481
|
+
if (first.index !== last.index) {
|
|
5482
|
+
if (index < last.index && paragraph.sectionProperties !== void 0) return refuse(DOCUMENT_OP_REFUSAL_REASONS.UNTRACKABLE, "A planned range cannot cross a section boundary.");
|
|
5483
|
+
if ((paragraph.propertyChanges?.length ?? 0) > 0) return refuse(DOCUMENT_OP_REFUSAL_REASONS.REVISION_CONFLICT, "Cross-paragraph planning cannot replace an existing property review.");
|
|
5484
|
+
}
|
|
5485
|
+
const content = paragraph.content;
|
|
5486
|
+
const segments = segmentsOf(leafSpans(content), {
|
|
5487
|
+
offset: start.offset,
|
|
5488
|
+
zeroWidthBefore: start.zeroWidthBefore ?? zeroWidthLeavesAt(content, start.offset).length
|
|
5489
|
+
}, {
|
|
5490
|
+
offset: end.offset,
|
|
5491
|
+
zeroWidthBefore: end.zeroWidthBefore ?? 0
|
|
5492
|
+
}, revision.author);
|
|
5493
|
+
ranges.push({
|
|
5494
|
+
paragraph,
|
|
5495
|
+
at,
|
|
5496
|
+
segments
|
|
5497
|
+
});
|
|
5498
|
+
}
|
|
5499
|
+
let direct = document;
|
|
5500
|
+
for (const { at, segments } of ranges.toReversed()) for (const segment of segments.toReversed()) {
|
|
5501
|
+
const op = {
|
|
5502
|
+
type: DOCUMENT_OP_TYPES.DELETE_RANGE,
|
|
5503
|
+
from: positionOf(at, segment.from),
|
|
5504
|
+
to: positionOf(at, segment.to)
|
|
5505
|
+
};
|
|
5506
|
+
const appended = plan.append(segment.plan === "tracked" ? {
|
|
5507
|
+
...op,
|
|
5508
|
+
revision
|
|
5509
|
+
} : op);
|
|
5510
|
+
if (appended.isErr()) return appended;
|
|
5511
|
+
const simulated = applyDocumentOp(direct, op);
|
|
5512
|
+
if (simulated.isErr()) return Result.err(simulated.error);
|
|
5513
|
+
direct = simulated.value.document;
|
|
5514
|
+
}
|
|
5515
|
+
if (first.index === last.index) return Result.ok(void 0);
|
|
5516
|
+
for (let index = ranges.length - 2; index >= 0; index -= 1) {
|
|
5517
|
+
const leading = ranges[index];
|
|
5518
|
+
const following = ranges[index + 1];
|
|
5519
|
+
if (leading === void 0 || following === void 0) return refuse(DOCUMENT_OP_REFUSAL_REASONS.UNTRACKABLE, "A planned join has no endpoint.");
|
|
5520
|
+
const op = {
|
|
5521
|
+
type: DOCUMENT_OP_TYPES.JOIN_BLOCKS,
|
|
5522
|
+
story: from.story,
|
|
5523
|
+
blockId: leading.at.blockId,
|
|
5524
|
+
nextBlockId: following.at.blockId,
|
|
5525
|
+
revision
|
|
5526
|
+
};
|
|
5527
|
+
const appended = plan.append(op);
|
|
5528
|
+
if (appended.isErr()) return appended;
|
|
5529
|
+
const simulated = applyDocumentOp(direct, {
|
|
5530
|
+
type: DOCUMENT_OP_TYPES.JOIN_BLOCKS,
|
|
5531
|
+
story: from.story,
|
|
5532
|
+
blockId: leading.at.blockId,
|
|
5533
|
+
nextBlockId: to.blockId
|
|
5534
|
+
});
|
|
5535
|
+
if (simulated.isErr()) return Result.err(simulated.error);
|
|
5536
|
+
direct = simulated.value.document;
|
|
5537
|
+
}
|
|
5538
|
+
const survivor = storyParagraphs(storyBody(direct, from.story)).find(({ paragraph }) => idKey(paragraph.paraId ?? "") === idKey(to.blockId));
|
|
5539
|
+
const trackedSurvivor = storyParagraphs(storyBody(plan.document(), from.story)).find(({ paragraph }) => idKey(paragraph.paraId ?? "") === idKey(to.blockId));
|
|
5540
|
+
if (survivor === void 0 || trackedSurvivor === void 0) return refuse(DOCUMENT_OP_REFUSAL_REASONS.BLOCK_NOT_FOUND, "The planned survivor does not exist.");
|
|
5541
|
+
const current = paragraphPropertiesOf(trackedSurvivor.paragraph.formatting);
|
|
5542
|
+
const desired = paragraphPropertiesOf(survivor.paragraph.formatting);
|
|
5543
|
+
if (structurallyEqual(current, desired)) return Result.ok(void 0);
|
|
5544
|
+
const patch = Object.fromEntries([...Object.keys(current ?? {}).map((key) => [key, null]), ...Object.entries(desired ?? {})]);
|
|
5545
|
+
return plan.append({
|
|
5546
|
+
type: DOCUMENT_OP_TYPES.SET_PARAGRAPH_PROPS,
|
|
5547
|
+
story: from.story,
|
|
5548
|
+
blockId: to.blockId,
|
|
5549
|
+
patch,
|
|
5550
|
+
whenEmpty: survivor.paragraph.formatting === void 0 ? EMPTY_PROPERTY_SETS.OMIT : EMPTY_PROPERTY_SETS.KEEP,
|
|
5551
|
+
revision
|
|
5552
|
+
});
|
|
5553
|
+
};
|
|
5554
|
+
/**
|
|
5555
|
+
* Plan a same-list range as inline deletions followed by tracked paragraph
|
|
5556
|
+
* joins. Own insertions are retracted directly; comment anchors remain.
|
|
5557
|
+
* Inline ranges run in reverse order, then joins run from last to first.
|
|
5558
|
+
* All physical revision ids come from one pool for the complete batch.
|
|
5559
|
+
*/
|
|
5560
|
+
const planTrackedDeletion = (document, options) => {
|
|
5561
|
+
const plan = createTrackedPlan({
|
|
5562
|
+
document,
|
|
5563
|
+
revision: options.revision,
|
|
5564
|
+
newIds: options.newIds
|
|
5565
|
+
});
|
|
5566
|
+
const appended = appendTrackedDeletion({
|
|
5567
|
+
document,
|
|
5568
|
+
options,
|
|
5569
|
+
plan
|
|
5570
|
+
});
|
|
5571
|
+
return appended.isErr() ? Result.err(appended.error) : Result.ok(plan.ops);
|
|
5572
|
+
};
|
|
5573
|
+
//#endregion
|
|
5574
|
+
//#region src/ops/rangeReplacement.ts
|
|
5575
|
+
/** Tracked replacement fragments share deletion's validation and physical-id pool. */
|
|
5576
|
+
/**
|
|
5577
|
+
* Plan a tracked replacement of a same-list range: the range's tracked
|
|
5578
|
+
* deletion followed by the replacement fragment as tracked insertions.
|
|
5579
|
+
* All physical revision ids come from one pool for the complete batch.
|
|
5580
|
+
*/
|
|
5581
|
+
const planTrackedReplace = (document, options) => {
|
|
5582
|
+
const { replacement, from, revision, newIds } = options;
|
|
5583
|
+
const refuse = (message) => Result.err(new DocumentOpRefusal({
|
|
5584
|
+
reason: DOCUMENT_OP_REFUSAL_REASONS.UNTRACKABLE,
|
|
5585
|
+
message,
|
|
5586
|
+
opType: DOCUMENT_OP_TYPES.INSERT_CONTENT
|
|
5587
|
+
}));
|
|
5588
|
+
if (replacement.tail.openStart !== 0 || replacement.tail.openEnd !== 0) return refuse("Tracked replacement requires a closed tail slice.");
|
|
5589
|
+
if (identityKeysIn(replacement).length > 0 || replacement.paragraphs.some(({ sectionProperties }) => sectionProperties !== void 0)) return refuse("Tracked replacement cannot insert identified review, content-control or section records.");
|
|
5590
|
+
const plan = createTrackedPlan({
|
|
5591
|
+
document,
|
|
5592
|
+
revision,
|
|
5593
|
+
newIds
|
|
5594
|
+
});
|
|
5595
|
+
const deleted = appendTrackedDeletion({
|
|
5596
|
+
document,
|
|
5597
|
+
options,
|
|
5598
|
+
plan
|
|
5599
|
+
});
|
|
5600
|
+
if (deleted.isErr()) return Result.err(deleted.error);
|
|
5601
|
+
let at = from;
|
|
5602
|
+
for (const paragraph of replacement.paragraphs) {
|
|
5603
|
+
const source = storyParagraphs(storyBody(plan.document(), from.story)).find(({ paragraph: sourceParagraph }) => idKey(sourceParagraph.paraId ?? "") === idKey(from.blockId));
|
|
5604
|
+
if (source === void 0) return refuse("The replacement's source paragraph does not exist.");
|
|
5605
|
+
const end = gapAfterInserted(at.zeroWidthBefore === void 0 ? defaultInsertionGap(source.paragraph.content, at.offset) : {
|
|
5606
|
+
offset: at.offset,
|
|
5607
|
+
zeroWidthBefore: at.zeroWidthBefore
|
|
5608
|
+
}, paragraph.content);
|
|
5609
|
+
if (paragraph.content.length > 0) {
|
|
5610
|
+
const inserted = plan.append({
|
|
5611
|
+
type: DOCUMENT_OP_TYPES.INSERT_CONTENT,
|
|
5612
|
+
at,
|
|
5613
|
+
slice: {
|
|
5614
|
+
content: paragraph.content,
|
|
5615
|
+
openStart: 0,
|
|
5616
|
+
openEnd: 0
|
|
5617
|
+
},
|
|
5618
|
+
revision
|
|
5619
|
+
});
|
|
5620
|
+
if (inserted.isErr()) return Result.err(inserted.error);
|
|
5621
|
+
}
|
|
5622
|
+
const { type: _type, paraId, content: _content, sectionProperties: _section, pPrMark: _mark, ...newParagraph } = paragraph;
|
|
5623
|
+
const split = plan.append({
|
|
5624
|
+
type: DOCUMENT_OP_TYPES.SPLIT_BLOCK,
|
|
5625
|
+
at: {
|
|
5626
|
+
...at,
|
|
5627
|
+
...end
|
|
5628
|
+
},
|
|
5629
|
+
newBlockId: paraId ?? "",
|
|
5630
|
+
newHalf: SPLIT_HALVES.FIRST,
|
|
5631
|
+
newParagraph,
|
|
5632
|
+
revision
|
|
5633
|
+
});
|
|
5634
|
+
if (split.isErr()) return Result.err(split.error);
|
|
5635
|
+
at = {
|
|
5636
|
+
story: from.story,
|
|
5637
|
+
blockId: from.blockId,
|
|
5638
|
+
offset: 0,
|
|
5639
|
+
zeroWidthBefore: 0
|
|
5640
|
+
};
|
|
5641
|
+
}
|
|
5642
|
+
if (replacement.tail.content.length > 0) {
|
|
5643
|
+
const inserted = plan.append({
|
|
5644
|
+
type: DOCUMENT_OP_TYPES.INSERT_CONTENT,
|
|
5645
|
+
at,
|
|
5646
|
+
slice: replacement.tail,
|
|
5647
|
+
revision
|
|
5648
|
+
});
|
|
5649
|
+
if (inserted.isErr()) return Result.err(inserted.error);
|
|
4725
5650
|
}
|
|
4726
|
-
return Result.ok(ops);
|
|
5651
|
+
return Result.ok(plan.ops);
|
|
4727
5652
|
};
|
|
4728
5653
|
//#endregion
|
|
4729
|
-
export { DOCUMENT_OP_REFUSAL_REASONS, DOCUMENT_OP_SCHEMA_VERSION, DOCUMENT_OP_TYPES, DocumentOpRefusal, DocumentOpsContractError, EMPTY_PROPERTY_SETS, INHERIT_RUN_PROPS, OBJECT_REPLACEMENT_CHARACTER, OP_STORIES, PARAGRAPH_MARK_FORMATTING_KEYS, REVISION_DECISIONS, SPLIT_HALVES, applyDocumentOp, applyDocumentOps, normalizeForOps, paragraphLength, paragraphLogicalText, planTrackedDeletion, revisionIdDemand, toOpEnvelope, validateOpsDocument };
|
|
5654
|
+
export { DOCUMENT_OP_REFUSAL_REASONS, DOCUMENT_OP_SCHEMA_VERSION, DOCUMENT_OP_TYPES, DocumentOpRefusal, DocumentOpsContractError, EMPTY_PROPERTY_SETS, INHERIT_RUN_PROPS, OBJECT_REPLACEMENT_CHARACTER, OP_STORIES, PARAGRAPH_MARK_FORMATTING_KEYS, REVISION_DECISIONS, SPLIT_HALVES, applyDocumentOp, applyDocumentOps, normalizeForOps, paragraphLength, paragraphLogicalText, planTrackedDeletion, planTrackedReplace, revisionIdDemand, toOpEnvelope, validateOpsDocument };
|