@stll/docx-core 0.43.0 → 0.43.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/{document-Ca6wPoTu.d.ts → document-CaGfolcU.d.ts} +28 -0
- package/dist/docx_kernel_bg.wasm +0 -0
- package/dist/index.d.ts +1 -1
- package/dist/index.js +1 -1
- package/dist/model/document.d.ts +1 -1
- package/dist/ops/documentOps.d.ts +170 -72
- package/dist/ops/documentOps.js +1568 -464
- package/dist/schema/sequenceChildren.js +1 -1
- package/dist/{sequenceChildren-CSn0-1ov.js → sequenceChildren-BEcOm3tR.js} +1 -1
- package/dist/xml/xmlNamespaceContext.d.ts +25 -0
- package/dist/xml/xmlNamespaceContext.js +25 -1
- package/package.json +1 -2
package/dist/ops/documentOps.js
CHANGED
|
@@ -1050,8 +1050,15 @@ const paragraphContentVerdict = ({ width, zeroWidthItems }) => {
|
|
|
1050
1050
|
return holds;
|
|
1051
1051
|
};
|
|
1052
1052
|
const isRangeBoundaryItem = (item) => isRangeBoundary(item);
|
|
1053
|
-
const collectParagraphRetentionFacts = (items, facts) => {
|
|
1053
|
+
const collectParagraphRetentionFacts = (items, facts, onceAccepted = false) => {
|
|
1054
1054
|
for (const item of items) {
|
|
1055
|
+
if (onceAccepted && (item.type === "deletion" || item.type === "moveFrom")) {
|
|
1056
|
+
collectParagraphRetentionFacts(childrenOf(item), {
|
|
1057
|
+
width: 0,
|
|
1058
|
+
zeroWidthItems: facts.zeroWidthItems
|
|
1059
|
+
}, true);
|
|
1060
|
+
continue;
|
|
1061
|
+
}
|
|
1055
1062
|
if (item.type === "commentReference") {
|
|
1056
1063
|
facts.zeroWidthItems.push(item.type);
|
|
1057
1064
|
continue;
|
|
@@ -1061,7 +1068,7 @@ const collectParagraphRetentionFacts = (items, facts) => {
|
|
|
1061
1068
|
continue;
|
|
1062
1069
|
}
|
|
1063
1070
|
if (isInlineContainer(item)) {
|
|
1064
|
-
collectParagraphRetentionFacts(childrenOf(item), facts);
|
|
1071
|
+
collectParagraphRetentionFacts(childrenOf(item), facts, onceAccepted);
|
|
1065
1072
|
continue;
|
|
1066
1073
|
}
|
|
1067
1074
|
facts.width += inlineWidth(item);
|
|
@@ -1076,6 +1083,18 @@ const paragraphHoldsContent = (paragraph) => {
|
|
|
1076
1083
|
collectParagraphRetentionFacts(paragraph.content, facts);
|
|
1077
1084
|
return paragraphContentVerdict(facts);
|
|
1078
1085
|
};
|
|
1086
|
+
/**
|
|
1087
|
+
* Whether a paragraph still holds content once every change in it is
|
|
1088
|
+
* accepted: what its pending deletions and moves away hold does not count.
|
|
1089
|
+
*/
|
|
1090
|
+
const paragraphHoldsContentOnceAccepted = (paragraph) => {
|
|
1091
|
+
const facts = {
|
|
1092
|
+
width: 0,
|
|
1093
|
+
zeroWidthItems: []
|
|
1094
|
+
};
|
|
1095
|
+
collectParagraphRetentionFacts(paragraph.content, facts, true);
|
|
1096
|
+
return paragraphContentVerdict(facts);
|
|
1097
|
+
};
|
|
1079
1098
|
/** Tracked content that direct text must not join: it is on its way out. */
|
|
1080
1099
|
const isRemovedRevision = (item) => item.type === "deletion" || item.type === "moveFrom";
|
|
1081
1100
|
const runContentWidth = (content) => {
|
|
@@ -1412,7 +1431,11 @@ const joined = (parts) => {
|
|
|
1412
1431
|
if (filled.length === 1) return filled[0] ?? NO_KEYS;
|
|
1413
1432
|
return filled.flat();
|
|
1414
1433
|
};
|
|
1415
|
-
const
|
|
1434
|
+
const ROOT_CONTEXT = {
|
|
1435
|
+
heldBy: void 0,
|
|
1436
|
+
ownerType: void 0
|
|
1437
|
+
};
|
|
1438
|
+
const censusWalker = (ownKeys, sharing) => {
|
|
1416
1439
|
const known = /* @__PURE__ */ new WeakMap();
|
|
1417
1440
|
const remembered = (value, key, census) => {
|
|
1418
1441
|
if (!census.shareable || sharing === "full") return census;
|
|
@@ -1454,16 +1477,12 @@ const subtreeCensus = (ownKeys, sharing = "shared") => {
|
|
|
1454
1477
|
const entries = Object.entries(value);
|
|
1455
1478
|
const own = ownKeys(entries, context);
|
|
1456
1479
|
let shareable = own.shareable;
|
|
1457
|
-
const type = fieldOf(entries, "type");
|
|
1458
|
-
const ownerType = typeof type === "string" ? type : void 0;
|
|
1459
|
-
const table = type === "table" ? value : context.table;
|
|
1460
1480
|
const parts = [own.keys];
|
|
1481
|
+
const inner = innerContext(entries, value, context);
|
|
1461
1482
|
for (const [heldBy, child] of entries) {
|
|
1462
1483
|
const census = censusOf(child, {
|
|
1463
|
-
|
|
1464
|
-
|
|
1465
|
-
table,
|
|
1466
|
-
stacked: context.stacked
|
|
1484
|
+
...inner,
|
|
1485
|
+
heldBy
|
|
1467
1486
|
});
|
|
1468
1487
|
parts.push(census.keys);
|
|
1469
1488
|
if (!census.shareable) shareable = false;
|
|
@@ -1473,11 +1492,24 @@ const subtreeCensus = (ownKeys, sharing = "shared") => {
|
|
|
1473
1492
|
shareable
|
|
1474
1493
|
});
|
|
1475
1494
|
};
|
|
1476
|
-
return
|
|
1477
|
-
|
|
1478
|
-
|
|
1479
|
-
|
|
1480
|
-
|
|
1495
|
+
return censusOf;
|
|
1496
|
+
};
|
|
1497
|
+
/** The context a record's fields are walked in, but for the field's own name. */
|
|
1498
|
+
const innerContext = (entries, record, context) => {
|
|
1499
|
+
const type = fieldOf(entries, "type");
|
|
1500
|
+
return {
|
|
1501
|
+
ownerType: typeof type === "string" ? type : void 0,
|
|
1502
|
+
table: type === "table" ? record : context.table,
|
|
1503
|
+
stacked: context.stacked
|
|
1504
|
+
};
|
|
1505
|
+
};
|
|
1506
|
+
const freshContext = () => ({
|
|
1507
|
+
...ROOT_CONTEXT,
|
|
1508
|
+
stacked: /* @__PURE__ */ new Map()
|
|
1509
|
+
});
|
|
1510
|
+
const subtreeCensus = (ownKeys, sharing = "shared") => {
|
|
1511
|
+
const censusOf = censusWalker(ownKeys, sharing);
|
|
1512
|
+
return (value) => [...censusOf(value, freshContext()).keys];
|
|
1481
1513
|
};
|
|
1482
1514
|
const ownIdentityKeys = (entries, context) => {
|
|
1483
1515
|
const { heldBy, ownerType, table } = context;
|
|
@@ -1530,7 +1562,7 @@ const identityKeysIn = subtreeCensus(ownIdentityKeys, "full");
|
|
|
1530
1562
|
* census met. Only for packages, which no operation changes (see
|
|
1531
1563
|
* {@link censusOnce}).
|
|
1532
1564
|
*/
|
|
1533
|
-
const
|
|
1565
|
+
const sharedIdentityCensus = censusWalker(ownIdentityKeys, "shared");
|
|
1534
1566
|
/** The field holding the stack of content controls a table row or cell sits inside. */
|
|
1535
1567
|
const CONTENT_CONTROL_STACK = "contentControls";
|
|
1536
1568
|
const ownReservedKeys = (entries, { heldBy }) => {
|
|
@@ -1574,7 +1606,7 @@ const ownReservedKeys = (entries, { heldBy }) => {
|
|
|
1574
1606
|
*/
|
|
1575
1607
|
const reservedIdentityKeysIn = subtreeCensus(ownReservedKeys, "full");
|
|
1576
1608
|
/** {@link reservedIdentityKeysIn} reusing earlier counts; only for packages. */
|
|
1577
|
-
const
|
|
1609
|
+
const sharedReservedCensus = censusWalker(ownReservedKeys, "shared");
|
|
1578
1610
|
/**
|
|
1579
1611
|
* A census of a package, once per package object. Operations never change a
|
|
1580
1612
|
* package they are given: each returns a new one, sharing what it left as it
|
|
@@ -1591,9 +1623,196 @@ const censusOnce = (census) => {
|
|
|
1591
1623
|
};
|
|
1592
1624
|
};
|
|
1593
1625
|
/** Every revision and content-control id of a package, each story walked once. */
|
|
1594
|
-
const packageIdentityKeys = censusOnce((pkg) =>
|
|
1626
|
+
const packageIdentityKeys = censusOnce((pkg) => [...sharedIdentityCensus(storiesOf(pkg), freshContext()).keys]);
|
|
1595
1627
|
/** The ids a package's pending cut references reserve. */
|
|
1596
|
-
const packageReservedIdentityKeys = censusOnce((pkg) =>
|
|
1628
|
+
const packageReservedIdentityKeys = censusOnce((pkg) => [...sharedReservedCensus(pkg, freshContext()).keys]);
|
|
1629
|
+
const MAX_CHAIN = 32;
|
|
1630
|
+
const countIn = (node, key) => {
|
|
1631
|
+
let count = 0;
|
|
1632
|
+
for (let at = node; at !== void 0; at = at.parent) count += at.changes.get(key) ?? 0;
|
|
1633
|
+
return count;
|
|
1634
|
+
};
|
|
1635
|
+
/**
|
|
1636
|
+
* Turns a node into a root holding every count. Its own total is unchanged,
|
|
1637
|
+
* so nodes derived from it still add up to theirs.
|
|
1638
|
+
*/
|
|
1639
|
+
const settle = (node) => {
|
|
1640
|
+
if (node.parent === void 0) return;
|
|
1641
|
+
const chain = [];
|
|
1642
|
+
for (let at = node; at !== void 0; at = at.parent) chain.push(at);
|
|
1643
|
+
chain.reverse();
|
|
1644
|
+
const counts = new Map(chain[0]?.changes);
|
|
1645
|
+
for (const step of chain.slice(1)) for (const [key, change] of step.changes) {
|
|
1646
|
+
const count = (counts.get(key) ?? 0) + change;
|
|
1647
|
+
if (count === 0) counts.delete(key);
|
|
1648
|
+
else counts.set(key, count);
|
|
1649
|
+
}
|
|
1650
|
+
node.parent = void 0;
|
|
1651
|
+
node.changes = counts;
|
|
1652
|
+
node.depth = 0;
|
|
1653
|
+
};
|
|
1654
|
+
const kindOf$1 = (value) => {
|
|
1655
|
+
if (typeof value !== "object" || value === null) return "leaf";
|
|
1656
|
+
if (Array.isArray(value)) return "list";
|
|
1657
|
+
if (value instanceof Map) return "map";
|
|
1658
|
+
return isPlainRecord(value) ? "record" : "leaf";
|
|
1659
|
+
};
|
|
1660
|
+
const tally = (changes, keys, sign) => {
|
|
1661
|
+
for (const key of keys) changes.set(key, (changes.get(key) ?? 0) + sign);
|
|
1662
|
+
};
|
|
1663
|
+
/**
|
|
1664
|
+
* Adds to `changes` what turning `before` into `after` does to a census.
|
|
1665
|
+
*
|
|
1666
|
+
* A census is the sum of what each record contributes in its context, so
|
|
1667
|
+
* the change is found by walking both values side by side: a value both
|
|
1668
|
+
* share counts the same in each, and a record or list both hold is compared
|
|
1669
|
+
* field by field and item by item. The rest is counted out of `before` and
|
|
1670
|
+
* into `after`. `before` must be shareable (see {@link censusWalker}); the
|
|
1671
|
+
* walk stops, answering `false`, when `after` is not.
|
|
1672
|
+
*/
|
|
1673
|
+
const censusChange = (censusOf, ownKeys) => {
|
|
1674
|
+
const replaced = (before, after, context, changes) => {
|
|
1675
|
+
tally(changes, censusOf(before, context).keys, -1);
|
|
1676
|
+
const added = censusOf(after, context);
|
|
1677
|
+
if (!added.shareable) return false;
|
|
1678
|
+
tally(changes, added.keys, 1);
|
|
1679
|
+
return true;
|
|
1680
|
+
};
|
|
1681
|
+
const items = (before, after, context, changes) => {
|
|
1682
|
+
let start = 0;
|
|
1683
|
+
while (start < before.length && start < after.length && before[start] === after[start]) start += 1;
|
|
1684
|
+
let beforeEnd = before.length;
|
|
1685
|
+
let afterEnd = after.length;
|
|
1686
|
+
while (beforeEnd > start && afterEnd > start && before[beforeEnd - 1] === after[afterEnd - 1]) {
|
|
1687
|
+
beforeEnd -= 1;
|
|
1688
|
+
afterEnd -= 1;
|
|
1689
|
+
}
|
|
1690
|
+
const paired = Math.min(beforeEnd, afterEnd) - start;
|
|
1691
|
+
for (let offset = 0; offset < paired; offset += 1) if (!change(before[start + offset], after[start + offset], context, changes)) return false;
|
|
1692
|
+
for (let index = start + paired; index < beforeEnd; index += 1) replaced(before[index], void 0, context, changes);
|
|
1693
|
+
for (let index = start + paired; index < afterEnd; index += 1) if (!replaced(void 0, after[index], context, changes)) return false;
|
|
1694
|
+
return true;
|
|
1695
|
+
};
|
|
1696
|
+
const fields = (before, after, context, changes) => {
|
|
1697
|
+
const beforeEntries = Object.entries(before);
|
|
1698
|
+
const afterEntries = Object.entries(after);
|
|
1699
|
+
if (fieldOf(beforeEntries, "type") !== fieldOf(afterEntries, "type")) return replaced(before, after, context, changes);
|
|
1700
|
+
tally(changes, ownKeys(beforeEntries, context).keys, -1);
|
|
1701
|
+
const own = ownKeys(afterEntries, context);
|
|
1702
|
+
if (!own.shareable) return false;
|
|
1703
|
+
tally(changes, own.keys, 1);
|
|
1704
|
+
const inner = innerContext(afterEntries, after, context);
|
|
1705
|
+
const beforeFields = new Map(beforeEntries);
|
|
1706
|
+
for (const [heldBy, value] of afterEntries) {
|
|
1707
|
+
const was = beforeFields.get(heldBy);
|
|
1708
|
+
beforeFields.delete(heldBy);
|
|
1709
|
+
if (!change(was, value, {
|
|
1710
|
+
...inner,
|
|
1711
|
+
heldBy
|
|
1712
|
+
}, changes)) return false;
|
|
1713
|
+
}
|
|
1714
|
+
for (const [heldBy, was] of beforeFields) replaced(was, void 0, {
|
|
1715
|
+
...inner,
|
|
1716
|
+
heldBy
|
|
1717
|
+
}, changes);
|
|
1718
|
+
return true;
|
|
1719
|
+
};
|
|
1720
|
+
const change = (before, after, context, changes) => {
|
|
1721
|
+
if (before === after) return true;
|
|
1722
|
+
const kind = kindOf$1(before);
|
|
1723
|
+
if (kind !== kindOf$1(after)) return replaced(before, after, context, changes);
|
|
1724
|
+
if (kind === "leaf") return true;
|
|
1725
|
+
if (Array.isArray(before) && Array.isArray(after)) return items(before, after, context, changes);
|
|
1726
|
+
if (before instanceof Map && after instanceof Map) {
|
|
1727
|
+
for (const [key, value] of after) if (!change(before.get(key), value, context, changes)) return false;
|
|
1728
|
+
for (const [key, value] of before) if (!after.has(key)) replaced(value, void 0, context, changes);
|
|
1729
|
+
return true;
|
|
1730
|
+
}
|
|
1731
|
+
if (typeof before !== "object" || before === null) return true;
|
|
1732
|
+
if (typeof after !== "object" || after === null) return true;
|
|
1733
|
+
return fields(before, after, context, changes);
|
|
1734
|
+
};
|
|
1735
|
+
return (before, after, changes) => change(before, after, freshContext(), changes);
|
|
1736
|
+
};
|
|
1737
|
+
/**
|
|
1738
|
+
* A key index per package, each derived from the package indexed last.
|
|
1739
|
+
*
|
|
1740
|
+
* Operations never change a package: each returns a new one sharing what it
|
|
1741
|
+
* left as it was, so the package before an edit and the one after differ
|
|
1742
|
+
* only along the paths to what changed, and an index of one is found from
|
|
1743
|
+
* the other's by walking those paths. A package holding a record counted
|
|
1744
|
+
* against others (see {@link censusWalker}) is counted in full.
|
|
1745
|
+
*/
|
|
1746
|
+
const keyIndex = (censusOf, ownKeys, viewOf) => {
|
|
1747
|
+
const nodes = /* @__PURE__ */ new WeakMap();
|
|
1748
|
+
const changeBetween = censusChange(censusOf, ownKeys);
|
|
1749
|
+
const recent = [];
|
|
1750
|
+
const baseFor = () => {
|
|
1751
|
+
for (const reference of recent) {
|
|
1752
|
+
const previous = reference.deref();
|
|
1753
|
+
const base = previous === void 0 ? void 0 : nodes.get(previous);
|
|
1754
|
+
if (previous !== void 0 && base?.shareable === true) return {
|
|
1755
|
+
previous,
|
|
1756
|
+
base
|
|
1757
|
+
};
|
|
1758
|
+
}
|
|
1759
|
+
};
|
|
1760
|
+
const remember = (pkg) => {
|
|
1761
|
+
if (recent[0]?.deref() === pkg) return;
|
|
1762
|
+
const alive = recent.filter((reference) => {
|
|
1763
|
+
const held = reference.deref();
|
|
1764
|
+
return held !== void 0 && held !== pkg;
|
|
1765
|
+
});
|
|
1766
|
+
recent.length = 0;
|
|
1767
|
+
recent.push(new WeakRef(pkg), ...alive.slice(0, 7));
|
|
1768
|
+
};
|
|
1769
|
+
const derived = (pkg) => {
|
|
1770
|
+
const found = baseFor();
|
|
1771
|
+
if (found === void 0) return void 0;
|
|
1772
|
+
const { previous, base } = found;
|
|
1773
|
+
const changes = /* @__PURE__ */ new Map();
|
|
1774
|
+
if (!changeBetween(viewOf(previous), viewOf(pkg), changes)) return void 0;
|
|
1775
|
+
for (const [key, change] of changes) if (change === 0) changes.delete(key);
|
|
1776
|
+
const node = {
|
|
1777
|
+
parent: base,
|
|
1778
|
+
changes,
|
|
1779
|
+
depth: base.depth + 1,
|
|
1780
|
+
shareable: true
|
|
1781
|
+
};
|
|
1782
|
+
if (node.depth >= MAX_CHAIN) settle(node);
|
|
1783
|
+
return node;
|
|
1784
|
+
};
|
|
1785
|
+
const counted = (pkg) => {
|
|
1786
|
+
const census = censusOf(viewOf(pkg), freshContext());
|
|
1787
|
+
return {
|
|
1788
|
+
parent: void 0,
|
|
1789
|
+
changes: countKeys(census.keys),
|
|
1790
|
+
depth: 0,
|
|
1791
|
+
shareable: census.shareable
|
|
1792
|
+
};
|
|
1793
|
+
};
|
|
1794
|
+
return (pkg) => {
|
|
1795
|
+
let node = nodes.get(pkg);
|
|
1796
|
+
if (node === void 0) {
|
|
1797
|
+
node = derived(pkg) ?? counted(pkg);
|
|
1798
|
+
nodes.set(pkg, node);
|
|
1799
|
+
}
|
|
1800
|
+
remember(pkg);
|
|
1801
|
+
const indexed = node;
|
|
1802
|
+
return {
|
|
1803
|
+
count: (key) => countIn(indexed, key),
|
|
1804
|
+
has: (key) => countIn(indexed, key) > 0,
|
|
1805
|
+
counts: () => {
|
|
1806
|
+
settle(indexed);
|
|
1807
|
+
return indexed.changes;
|
|
1808
|
+
}
|
|
1809
|
+
};
|
|
1810
|
+
};
|
|
1811
|
+
};
|
|
1812
|
+
/** How many records of a package carry each revision and content-control id. */
|
|
1813
|
+
const packageIdentityKeyIndex = keyIndex(sharedIdentityCensus, ownIdentityKeys, storiesOf);
|
|
1814
|
+
/** How many of a package's pending cut references reserve each id. */
|
|
1815
|
+
const packageReservedKeyIndex = keyIndex(sharedReservedCensus, ownReservedKeys, (pkg) => pkg);
|
|
1597
1816
|
// src/ops/slots.ts
|
|
1598
1817
|
const revision$1 = (id) => ({
|
|
1599
1818
|
space: IDENTITY_SPACES.REVISION,
|
|
@@ -2165,6 +2384,25 @@ const leafSpans = (items) => {
|
|
|
2165
2384
|
walk(items, []);
|
|
2166
2385
|
return out;
|
|
2167
2386
|
};
|
|
2387
|
+
/**
|
|
2388
|
+
* The stretch of a paragraph's content the given revisions hold: from the
|
|
2389
|
+
* first leaf inside one of their tracked wrappers to the last, or `undefined`
|
|
2390
|
+
* when none is there.
|
|
2391
|
+
*/
|
|
2392
|
+
const revisionSpan = (items, revisionIds) => {
|
|
2393
|
+
let span;
|
|
2394
|
+
for (const { ancestors, before, after } of leafSpans(items)) {
|
|
2395
|
+
if (!ancestors.some((ancestor) => (ancestor.type === "insertion" || ancestor.type === "deletion" || ancestor.type === "moveFrom" || ancestor.type === "moveTo") && revisionIds.has(ancestor.info.id))) continue;
|
|
2396
|
+
span = span === void 0 ? {
|
|
2397
|
+
from: before.offset,
|
|
2398
|
+
to: after.offset
|
|
2399
|
+
} : {
|
|
2400
|
+
from: Math.min(span.from, before.offset),
|
|
2401
|
+
to: Math.max(span.to, after.offset)
|
|
2402
|
+
};
|
|
2403
|
+
}
|
|
2404
|
+
return span;
|
|
2405
|
+
};
|
|
2168
2406
|
/** Source leaf coordinates without the ancestry needed by edit planning. */
|
|
2169
2407
|
const inlineLeafSpans = (items) => leafSpans(items).map(({ node, before, after }) => ({
|
|
2170
2408
|
node,
|
|
@@ -2541,8 +2779,39 @@ const referencedNoteKeys = (document) => {
|
|
|
2541
2779
|
DOCUMENT_NOTE_KEYS.set(document, keys);
|
|
2542
2780
|
return keys;
|
|
2543
2781
|
};
|
|
2782
|
+
/** The note keys a run of blocks references, sorted. */
|
|
2783
|
+
const noteKeysIn = (blocks) => storyParagraphs({ content: [...blocks] }).flatMap(({ paragraph }) => sharedParagraphNoteKeys(paragraph)).sort();
|
|
2784
|
+
/**
|
|
2785
|
+
* Whether `after` references exactly the notes `before` does, read from the
|
|
2786
|
+
* top-level blocks of each story that are not the very blocks `before` held
|
|
2787
|
+
* at the same place from either end: when those carry the same references
|
|
2788
|
+
* as the ones they replace, every other block's references are unchanged.
|
|
2789
|
+
* False when the stories differ or the references might.
|
|
2790
|
+
*/
|
|
2791
|
+
const sameNoteReferences = (before, after) => {
|
|
2792
|
+
const stories = documentStories(after);
|
|
2793
|
+
const earlier = documentStories(before);
|
|
2794
|
+
if (stories.length !== earlier.length || stories.some((story, index) => {
|
|
2795
|
+
const other = earlier[index];
|
|
2796
|
+
return other === void 0 || !sameStory(story, other);
|
|
2797
|
+
})) return false;
|
|
2798
|
+
for (const story of stories) {
|
|
2799
|
+
const old = storyBody(before, story).content;
|
|
2800
|
+
const now = storyBody(after, story).content;
|
|
2801
|
+
if (old === now) continue;
|
|
2802
|
+
let start = 0;
|
|
2803
|
+
while (start < old.length && start < now.length && old[start] === now[start]) start += 1;
|
|
2804
|
+
let end = 0;
|
|
2805
|
+
while (end < old.length - start && end < now.length - start && old[old.length - 1 - end] === now[now.length - 1 - end]) end += 1;
|
|
2806
|
+
const removed = noteKeysIn(old.slice(start, old.length - end));
|
|
2807
|
+
const added = noteKeysIn(now.slice(start, now.length - end));
|
|
2808
|
+
if (removed.length !== added.length || removed.some((key, index) => added[index] !== key)) return false;
|
|
2809
|
+
}
|
|
2810
|
+
return true;
|
|
2811
|
+
};
|
|
2544
2812
|
/** Note stories whose final live reference an edit removes. */
|
|
2545
2813
|
const removedNoteStories = (before, after) => {
|
|
2814
|
+
if (sameNoteReferences(before, after)) return [];
|
|
2546
2815
|
const beforeReferences = referencedNoteKeys(before);
|
|
2547
2816
|
const afterReferences = referencedNoteKeys(after);
|
|
2548
2817
|
return [...beforeReferences].filter((key) => !afterReferences.has(key)).map((key) => {
|
|
@@ -2805,13 +3074,71 @@ const anchorId = (node) => {
|
|
|
2805
3074
|
default: return;
|
|
2806
3075
|
}
|
|
2807
3076
|
};
|
|
2808
|
-
|
|
2809
|
-
|
|
2810
|
-
const
|
|
2811
|
-
|
|
2812
|
-
|
|
2813
|
-
|
|
3077
|
+
/** A paragraph's facts, read from every leaf of it. */
|
|
3078
|
+
const freshParagraphAnchorFacts = (paragraph) => {
|
|
3079
|
+
const spans = leafSpans(paragraph.content);
|
|
3080
|
+
const anchors = [];
|
|
3081
|
+
const textBoxes = [];
|
|
3082
|
+
for (const span of spans) {
|
|
3083
|
+
if (isCommentAnchor(span.node)) anchors.push(span);
|
|
3084
|
+
if (span.node.type === "shape" && span.node.shape.textBody !== void 0) textBoxes.push(span.node.shape.textBody.content);
|
|
2814
3085
|
}
|
|
3086
|
+
return {
|
|
3087
|
+
anchors,
|
|
3088
|
+
start: spans.at(0)?.before ?? {
|
|
3089
|
+
offset: 0,
|
|
3090
|
+
zeroWidthBefore: 0
|
|
3091
|
+
},
|
|
3092
|
+
end: spans.at(-1)?.after ?? {
|
|
3093
|
+
offset: 0,
|
|
3094
|
+
zeroWidthBefore: 0
|
|
3095
|
+
},
|
|
3096
|
+
textBoxes
|
|
3097
|
+
};
|
|
3098
|
+
};
|
|
3099
|
+
/**
|
|
3100
|
+
* A paragraph's facts, read once per paragraph object. A package's
|
|
3101
|
+
* paragraphs are never changed in place: an edit replaces the paragraphs it
|
|
3102
|
+
* changes and keeps the rest, so a kept paragraph's facts still hold.
|
|
3103
|
+
*/
|
|
3104
|
+
const keptParagraphAnchorFacts = (() => {
|
|
3105
|
+
const known = /* @__PURE__ */ new WeakMap();
|
|
3106
|
+
return (paragraph) => {
|
|
3107
|
+
const cached = known.get(paragraph);
|
|
3108
|
+
if (cached !== void 0) return cached;
|
|
3109
|
+
const facts = freshParagraphAnchorFacts(paragraph);
|
|
3110
|
+
known.set(paragraph, facts);
|
|
3111
|
+
return facts;
|
|
3112
|
+
};
|
|
3113
|
+
})();
|
|
3114
|
+
/** A story's body, then the bodies of the text boxes in it, depth first in document order. */
|
|
3115
|
+
const storyBodiesIncludingTextBoxes = (body, facts) => {
|
|
3116
|
+
const bodies = [body];
|
|
3117
|
+
const visit = (blocks) => {
|
|
3118
|
+
for (const block of blocks) switch (block.type) {
|
|
3119
|
+
case "paragraph":
|
|
3120
|
+
for (const content of facts(block).textBoxes) bodies.push(...storyBodiesIncludingTextBoxes({ content }, facts));
|
|
3121
|
+
break;
|
|
3122
|
+
case "table":
|
|
3123
|
+
for (const row of block.rows) for (const cell of row.cells) visit(cell.content);
|
|
3124
|
+
break;
|
|
3125
|
+
case "blockSdt":
|
|
3126
|
+
case "blockCustomXml":
|
|
3127
|
+
visit(block.content);
|
|
3128
|
+
break;
|
|
3129
|
+
case "preservedBlock":
|
|
3130
|
+
case "bookmarkStart":
|
|
3131
|
+
case "bookmarkEnd":
|
|
3132
|
+
case "commentRangeStart":
|
|
3133
|
+
case "commentRangeEnd":
|
|
3134
|
+
case "moveFromRangeStart":
|
|
3135
|
+
case "moveFromRangeEnd":
|
|
3136
|
+
case "moveToRangeStart":
|
|
3137
|
+
case "moveToRangeEnd": break;
|
|
3138
|
+
default: return block;
|
|
3139
|
+
}
|
|
3140
|
+
};
|
|
3141
|
+
visit(body.content);
|
|
2815
3142
|
return bodies;
|
|
2816
3143
|
};
|
|
2817
3144
|
/**
|
|
@@ -2819,7 +3146,7 @@ const storyBodiesIncludingTextBoxes = (body) => {
|
|
|
2819
3146
|
* is one of them, placed at the edge of the text beside it: a start at the
|
|
2820
3147
|
* front of the next paragraph, an end at the back of the one before.
|
|
2821
3148
|
*/
|
|
2822
|
-
const storyAnchors = (story, body) => {
|
|
3149
|
+
const storyAnchors = (story, body, facts) => {
|
|
2823
3150
|
const anchors = [];
|
|
2824
3151
|
let waiting = [];
|
|
2825
3152
|
let previous;
|
|
@@ -2836,24 +3163,17 @@ const storyAnchors = (story, body) => {
|
|
|
2836
3163
|
for (const block of blocks) switch (block.type) {
|
|
2837
3164
|
case "paragraph": {
|
|
2838
3165
|
const blockId = block.paraId ?? "";
|
|
2839
|
-
const
|
|
2840
|
-
const start = spans.at(0)?.before ?? {
|
|
2841
|
-
offset: 0,
|
|
2842
|
-
zeroWidthBefore: 0
|
|
2843
|
-
};
|
|
3166
|
+
const { anchors: own, start, end } = facts(block);
|
|
2844
3167
|
for (const node of waiting) anchors.push(boundary(node, blockId, start));
|
|
2845
3168
|
waiting = [];
|
|
2846
|
-
for (const span of
|
|
3169
|
+
for (const span of own) anchors.push({
|
|
2847
3170
|
story,
|
|
2848
3171
|
blockId,
|
|
2849
3172
|
...span
|
|
2850
3173
|
});
|
|
2851
3174
|
previous = {
|
|
2852
3175
|
blockId,
|
|
2853
|
-
end
|
|
2854
|
-
offset: 0,
|
|
2855
|
-
zeroWidthBefore: 0
|
|
2856
|
-
}
|
|
3176
|
+
end
|
|
2857
3177
|
};
|
|
2858
3178
|
break;
|
|
2859
3179
|
}
|
|
@@ -2888,11 +3208,17 @@ const storyAnchors = (story, body) => {
|
|
|
2888
3208
|
}));
|
|
2889
3209
|
return anchors;
|
|
2890
3210
|
};
|
|
2891
|
-
|
|
3211
|
+
/**
|
|
3212
|
+
* Every comment anchor of a document, story by story, each story's text
|
|
3213
|
+
* boxes after its body. `facts` is for comparing the kept reading with a
|
|
3214
|
+
* fresh one.
|
|
3215
|
+
*/
|
|
3216
|
+
const commentAnchorsIn = (document, facts = keptParagraphAnchorFacts) => {
|
|
2892
3217
|
const anchors = [];
|
|
2893
|
-
for (const story of documentStories(document)) for (const body of storyBodiesIncludingTextBoxes(storyBody(document, story))) anchors.push(...storyAnchors(story, body));
|
|
3218
|
+
for (const story of documentStories(document)) for (const body of storyBodiesIncludingTextBoxes(storyBody(document, story), facts)) anchors.push(...storyAnchors(story, body, facts));
|
|
2894
3219
|
return anchors;
|
|
2895
3220
|
};
|
|
3221
|
+
const anchorsIn = (document) => commentAnchorsIn(document);
|
|
2896
3222
|
/** A comment, the comments it replies to and the replies under it. */
|
|
2897
3223
|
const threadIds = (document, id) => {
|
|
2898
3224
|
const comments = document.package.document.comments ?? [];
|
|
@@ -2949,7 +3275,7 @@ const storyCoverageNodes = (body) => {
|
|
|
2949
3275
|
/** Range coverage in source order; comment anchors and range boundaries are only carriers. */
|
|
2950
3276
|
const coveredCommentIds = (document) => {
|
|
2951
3277
|
const covered = /* @__PURE__ */ new Set();
|
|
2952
|
-
for (const story of documentStories(document)) for (const body of storyBodiesIncludingTextBoxes(storyBody(document, story))) {
|
|
3278
|
+
for (const story of documentStories(document)) for (const body of storyBodiesIncludingTextBoxes(storyBody(document, story), keptParagraphAnchorFacts)) {
|
|
2953
3279
|
const open = /* @__PURE__ */ new Set();
|
|
2954
3280
|
for (const { node } of storyCoverageNodes(body)) {
|
|
2955
3281
|
if (node.type === "commentRangeStart") {
|
|
@@ -3028,15 +3354,28 @@ const commentDocumentIssue = (document, allowedOrphans = /* @__PURE__ */ new Set
|
|
|
3028
3354
|
if (comments.some(({ id }) => !validId(id)) || byId.size !== comments.length) return "Comment identities must be unique bounded integers.";
|
|
3029
3355
|
const anchors = anchorsIn(document);
|
|
3030
3356
|
if (anchors.some(({ node }) => !byId.has(anchorId(node) ?? -1))) return "A comment anchor has no definition.";
|
|
3357
|
+
const ownById = /* @__PURE__ */ new Map();
|
|
3358
|
+
for (const [at, anchor] of anchors.entries()) {
|
|
3359
|
+
const id = anchorId(anchor.node) ?? -1;
|
|
3360
|
+
const own = ownById.get(id);
|
|
3361
|
+
if (own === void 0) ownById.set(id, [{
|
|
3362
|
+
anchor,
|
|
3363
|
+
at
|
|
3364
|
+
}]);
|
|
3365
|
+
else own.push({
|
|
3366
|
+
anchor,
|
|
3367
|
+
at
|
|
3368
|
+
});
|
|
3369
|
+
}
|
|
3031
3370
|
for (const comment of comments) {
|
|
3032
|
-
const own =
|
|
3033
|
-
const starts = own.filter(({
|
|
3034
|
-
const ends = own.filter(({
|
|
3035
|
-
const references = own.filter(({
|
|
3371
|
+
const own = ownById.get(comment.id) ?? [];
|
|
3372
|
+
const starts = own.filter(({ anchor }) => anchor.node.type === "commentRangeStart");
|
|
3373
|
+
const ends = own.filter(({ anchor }) => anchor.node.type === "commentRangeEnd");
|
|
3374
|
+
const references = own.filter(({ anchor }) => anchor.node.type === "commentReference");
|
|
3036
3375
|
const start = starts.at(0);
|
|
3037
3376
|
const end = ends.at(0);
|
|
3038
3377
|
if (starts.length !== ends.length || starts.length > 1 || references.length > 1) return "Comment anchors are unbalanced or duplicated.";
|
|
3039
|
-
if (start && end && (!sameStory(start.story, end.story) ||
|
|
3378
|
+
if (start && end && (!sameStory(start.anchor.story, end.anchor.story) || start.at > end.at)) return "Comment range boundaries must be ordered in one story.";
|
|
3040
3379
|
const parent = comment.parentId === void 0 ? void 0 : byId.get(comment.parentId);
|
|
3041
3380
|
if (!parent && own.length === 0 && allowedOrphans !== "all" && !allowedOrphans.has(comment.id)) return "A root or revision-associated comment needs an owned source anchor.";
|
|
3042
3381
|
if (comment.parentId !== void 0 && !validId(comment.parentId)) return "A comment parent identity is invalid.";
|
|
@@ -3599,19 +3938,22 @@ const applyCommentOp = (document, op, applyOps) => {
|
|
|
3599
3938
|
}))) return fail(op, "A comment without a source anchor is preserved read-only.");
|
|
3600
3939
|
if (op.type === DOCUMENT_OP_TYPES.UPDATE_COMMENT_CONTENT) {
|
|
3601
3940
|
if (!Array.isArray(op.content) || op.content.length === 0 || op.content.some(({ paraId }) => paraId === void 0 || !isParaId(paraId))) return fail(op, "Comment content needs explicit paragraph identities.", DOCUMENT_OP_REFUSAL_REASONS.INVALID_BLOCK_ID);
|
|
3602
|
-
if (op.patch && Object.keys(op.patch).some((key) => key !== "author" && key !== "initials" && key !== "date")) return fail(op, "A content edit cannot change comment ownership.");
|
|
3941
|
+
if (op.patch && Object.keys(op.patch).some((key) => key !== "author" && key !== "initials" && key !== "date" && key !== "suggestionId")) return fail(op, "A content edit cannot change comment ownership.");
|
|
3942
|
+
if (op.patch !== void 0 && Object.hasOwn(op.patch, "suggestionId") && op.patch.suggestionId !== null) return fail(op, "Only a suggestion can propose a comment.");
|
|
3603
3943
|
if (op.patch && Object.hasOwn(op.patch, "author") && (op.patch.author === null || op.patch.author === void 0)) return fail(op, "A comment must retain an author.");
|
|
3604
3944
|
const updated = {
|
|
3605
3945
|
...comment,
|
|
3606
3946
|
...applyFormattingPatch({
|
|
3607
3947
|
author: comment.author,
|
|
3608
3948
|
...Object.hasOwn(comment, "initials") ? { initials: comment.initials } : {},
|
|
3609
|
-
...Object.hasOwn(comment, "date") ? { date: comment.date } : {}
|
|
3949
|
+
...Object.hasOwn(comment, "date") ? { date: comment.date } : {},
|
|
3950
|
+
...Object.hasOwn(comment, "suggestionId") ? { suggestionId: comment.suggestionId } : {}
|
|
3610
3951
|
}, op.patch ?? {}),
|
|
3611
3952
|
content: cloneModel([...op.content])
|
|
3612
3953
|
};
|
|
3613
3954
|
if (op.patch?.initials === null) delete updated.initials;
|
|
3614
3955
|
if (op.patch?.date === null) delete updated.date;
|
|
3956
|
+
if (op.patch?.suggestionId === null) delete updated.suggestionId;
|
|
3615
3957
|
return edited(document, withComments(document, comments.map((value) => value === comment ? updated : value)), op, /* @__PURE__ */ new Set([comment.id]));
|
|
3616
3958
|
}
|
|
3617
3959
|
if (op.type === DOCUMENT_OP_TYPES.SET_COMMENT_RESOLUTION) {
|
|
@@ -4100,88 +4442,262 @@ const applyStoryLifecycle = (document, op) => {
|
|
|
4100
4442
|
}
|
|
4101
4443
|
return storyLifecycleEdit(document, next, op);
|
|
4102
4444
|
};
|
|
4103
|
-
// src/ops/
|
|
4445
|
+
// src/ops/runMerge.ts
|
|
4104
4446
|
/**
|
|
4105
|
-
*
|
|
4106
|
-
* its edit instead of making it, and the pieces resolution shares with it.
|
|
4447
|
+
* Check if two TextFormatting objects are equivalent
|
|
4107
4448
|
*
|
|
4108
|
-
*
|
|
4109
|
-
*
|
|
4110
|
-
* that admits one: the paragraph, an inline content control, a transparent
|
|
4111
|
-
* wrapper, and for a deletion also an insertion or a move the range falls
|
|
4112
|
-
* inside. Records below that level which the range cuts (runs, hyperlinks,
|
|
4113
|
-
* and for an insertion another stamp's insertion) are cut at the range ends,
|
|
4114
|
-
* each piece keeping every field of the record, as a direct edit cuts them.
|
|
4449
|
+
* Uses deep comparison of all properties to determine if runs
|
|
4450
|
+
* can be merged without losing formatting information.
|
|
4115
4451
|
*/
|
|
4116
|
-
|
|
4117
|
-
|
|
4118
|
-
|
|
4119
|
-
|
|
4120
|
-
|
|
4121
|
-
|
|
4122
|
-
|
|
4123
|
-
if (
|
|
4124
|
-
if (
|
|
4125
|
-
return
|
|
4126
|
-
|
|
4127
|
-
|
|
4128
|
-
|
|
4129
|
-
|
|
4130
|
-
|
|
4131
|
-
|
|
4132
|
-
|
|
4133
|
-
|
|
4134
|
-
|
|
4135
|
-
|
|
4136
|
-
|
|
4137
|
-
|
|
4138
|
-
|
|
4139
|
-
|
|
4140
|
-
|
|
4141
|
-
|
|
4142
|
-
|
|
4143
|
-
|
|
4144
|
-
if (
|
|
4145
|
-
|
|
4146
|
-
|
|
4147
|
-
|
|
4148
|
-
|
|
4149
|
-
|
|
4150
|
-
|
|
4151
|
-
|
|
4152
|
-
|
|
4153
|
-
|
|
4154
|
-
};
|
|
4452
|
+
function formattingEquals(a, b) {
|
|
4453
|
+
if (!a && !b) return true;
|
|
4454
|
+
if (!a || !b) return false;
|
|
4455
|
+
if (a.bold !== b.bold) return false;
|
|
4456
|
+
if (a.boldCs !== b.boldCs) return false;
|
|
4457
|
+
if (a.italic !== b.italic) return false;
|
|
4458
|
+
if (a.italicCs !== b.italicCs) return false;
|
|
4459
|
+
if (a.strike !== b.strike) return false;
|
|
4460
|
+
if (a.doubleStrike !== b.doubleStrike) return false;
|
|
4461
|
+
if (a.smallCaps !== b.smallCaps) return false;
|
|
4462
|
+
if (a.allCaps !== b.allCaps) return false;
|
|
4463
|
+
if (a.hidden !== b.hidden) return false;
|
|
4464
|
+
if (a.noProof !== b.noProof) return false;
|
|
4465
|
+
if (a.emboss !== b.emboss) return false;
|
|
4466
|
+
if (a.imprint !== b.imprint) return false;
|
|
4467
|
+
if (a.outline !== b.outline) return false;
|
|
4468
|
+
if (a.shadow !== b.shadow) return false;
|
|
4469
|
+
if (a.rtl !== b.rtl) return false;
|
|
4470
|
+
if (a.cs !== b.cs) return false;
|
|
4471
|
+
if (a.fontSize !== b.fontSize) return false;
|
|
4472
|
+
if (a.fontSizeCs !== b.fontSizeCs) return false;
|
|
4473
|
+
if (a.spacing !== b.spacing) return false;
|
|
4474
|
+
if (a.position !== b.position) return false;
|
|
4475
|
+
if (a.scale !== b.scale) return false;
|
|
4476
|
+
if (a.kerning !== b.kerning) return false;
|
|
4477
|
+
if (a.vertAlign !== b.vertAlign) return false;
|
|
4478
|
+
if (a.highlight !== b.highlight) return false;
|
|
4479
|
+
if (a.effect !== b.effect) return false;
|
|
4480
|
+
if (a.emphasisMark !== b.emphasisMark) return false;
|
|
4481
|
+
if (a.styleId !== b.styleId) return false;
|
|
4482
|
+
if (!underlineEquals(a.underline, b.underline)) return false;
|
|
4483
|
+
if (!colorEquals(a.color, b.color)) return false;
|
|
4484
|
+
if (!shadingEquals(a.shading, b.shading)) return false;
|
|
4485
|
+
if (!fontFamilyEquals(a.fontFamily, b.fontFamily)) return false;
|
|
4486
|
+
if (!languageEquals(a.language, b.language)) return false;
|
|
4487
|
+
if (!preservedMarkupEquals(a.preserved, b.preserved)) return false;
|
|
4488
|
+
return true;
|
|
4489
|
+
}
|
|
4155
4490
|
/**
|
|
4156
|
-
*
|
|
4157
|
-
*
|
|
4491
|
+
* Compare two verbatim sinks position by position.
|
|
4492
|
+
*
|
|
4493
|
+
* The sink is ordered — by `index`, then by source order within an index — and
|
|
4494
|
+
* the order is what puts the markup back between the same modelled siblings,
|
|
4495
|
+
* so equality is by sequence, not by set.
|
|
4158
4496
|
*/
|
|
4159
|
-
|
|
4160
|
-
const
|
|
4161
|
-
|
|
4162
|
-
|
|
4163
|
-
|
|
4164
|
-
|
|
4165
|
-
|
|
4166
|
-
|
|
4167
|
-
|
|
4168
|
-
|
|
4169
|
-
|
|
4170
|
-
|
|
4171
|
-
|
|
4172
|
-
|
|
4173
|
-
|
|
4174
|
-
|
|
4175
|
-
|
|
4176
|
-
|
|
4177
|
-
|
|
4178
|
-
const
|
|
4179
|
-
|
|
4180
|
-
|
|
4181
|
-
|
|
4182
|
-
|
|
4183
|
-
|
|
4184
|
-
|
|
4497
|
+
function preservedMarkupEquals(a, b) {
|
|
4498
|
+
const left = a?.children ?? [];
|
|
4499
|
+
const right = b?.children ?? [];
|
|
4500
|
+
if (left.length !== right.length) return false;
|
|
4501
|
+
return left.every(({ index, xml }, position) => {
|
|
4502
|
+
const other = right[position];
|
|
4503
|
+
return other !== void 0 && other.index === index && other.xml === xml;
|
|
4504
|
+
});
|
|
4505
|
+
}
|
|
4506
|
+
/**
|
|
4507
|
+
* Compare two attribute remainders as sets.
|
|
4508
|
+
*
|
|
4509
|
+
* An element cannot carry one expanded name twice, so (namespace, name) orders
|
|
4510
|
+
* a remainder totally and the sorted keys compare exactly. Order is a set
|
|
4511
|
+
* comparison rather than a sequence one because attribute order in XML says
|
|
4512
|
+
* nothing: two runs that spelled the same attributes in a different order
|
|
4513
|
+
* carry the same remainder and may still become one run.
|
|
4514
|
+
*/
|
|
4515
|
+
function preservedAttributesEqual(a, b) {
|
|
4516
|
+
const left = canonicalRemainderKeys(a);
|
|
4517
|
+
const right = canonicalRemainderKeys(b);
|
|
4518
|
+
return left.length === right.length && left.every((key, index) => key === right[index]);
|
|
4519
|
+
}
|
|
4520
|
+
/** `\u0000` cannot appear in an XML name, namespace URI or attribute value. */
|
|
4521
|
+
const REMAINDER_KEY_SEPARATOR = "\0";
|
|
4522
|
+
const canonicalRemainderKeys = (attributes) => (attributes ?? []).map(({ namespace, name, value }) => [
|
|
4523
|
+
namespace ?? "",
|
|
4524
|
+
name,
|
|
4525
|
+
value
|
|
4526
|
+
].join(REMAINDER_KEY_SEPARATOR)).sort();
|
|
4527
|
+
function languageEquals(a, b) {
|
|
4528
|
+
return a?.val === b?.val && a?.eastAsia === b?.eastAsia && a?.bidi === b?.bidi;
|
|
4529
|
+
}
|
|
4530
|
+
/**
|
|
4531
|
+
* Compare underline settings
|
|
4532
|
+
*/
|
|
4533
|
+
function underlineEquals(a, b) {
|
|
4534
|
+
if (!a && !b) return true;
|
|
4535
|
+
if (!a || !b) return false;
|
|
4536
|
+
if (a.style !== b.style) return false;
|
|
4537
|
+
return colorEquals(a.color, b.color);
|
|
4538
|
+
}
|
|
4539
|
+
/**
|
|
4540
|
+
* Compare color values
|
|
4541
|
+
*/
|
|
4542
|
+
function colorEquals(a, b) {
|
|
4543
|
+
if (!a && !b) return true;
|
|
4544
|
+
if (!a || !b) return false;
|
|
4545
|
+
return a.rgb === b.rgb && a.auto === b.auto && a.themeColor === b.themeColor && a.themeTint === b.themeTint && a.themeShade === b.themeShade;
|
|
4546
|
+
}
|
|
4547
|
+
/**
|
|
4548
|
+
* Compare shading properties
|
|
4549
|
+
*/
|
|
4550
|
+
function shadingEquals(a, b) {
|
|
4551
|
+
if (!a && !b) return true;
|
|
4552
|
+
if (!a || !b) return false;
|
|
4553
|
+
if (a.pattern !== b.pattern) return false;
|
|
4554
|
+
if (!colorEquals(a.color, b.color)) return false;
|
|
4555
|
+
if (!colorEquals(a.fill, b.fill)) return false;
|
|
4556
|
+
return true;
|
|
4557
|
+
}
|
|
4558
|
+
/**
|
|
4559
|
+
* Compare font family settings
|
|
4560
|
+
*/
|
|
4561
|
+
function fontFamilyEquals(a, b) {
|
|
4562
|
+
if (!a && !b) return true;
|
|
4563
|
+
if (!a || !b) return false;
|
|
4564
|
+
return a.ascii === b.ascii && a.hAnsi === b.hAnsi && a.eastAsia === b.eastAsia && a.cs === b.cs && a.asciiTheme === b.asciiTheme && a.hAnsiTheme === b.hAnsiTheme && a.eastAsiaTheme === b.eastAsiaTheme && a.csTheme === b.csTheme;
|
|
4565
|
+
}
|
|
4566
|
+
/**
|
|
4567
|
+
* Check if a run contains only text content
|
|
4568
|
+
* (runs with special content like images, fields, etc. should not be merged)
|
|
4569
|
+
*/
|
|
4570
|
+
function isTextOnlyRun(run) {
|
|
4571
|
+
return run.content.every((c) => c.type === "text" || c.type === "softHyphen" || c.type === "noBreakHyphen");
|
|
4572
|
+
}
|
|
4573
|
+
/**
|
|
4574
|
+
* Check if run content can be merged (simple text types)
|
|
4575
|
+
*/
|
|
4576
|
+
function isMergeableContent(content) {
|
|
4577
|
+
return content.type === "text" || content.type === "softHyphen" || content.type === "noBreakHyphen";
|
|
4578
|
+
}
|
|
4579
|
+
/**
|
|
4580
|
+
* Check if a run can be merged with another run
|
|
4581
|
+
* Runs with breaks, tabs, images, fields, etc. act as merge boundaries
|
|
4582
|
+
*/
|
|
4583
|
+
function canMergeRun(run) {
|
|
4584
|
+
if (run.propertyChanges && run.propertyChanges.length > 0) return false;
|
|
4585
|
+
if (run.content.length === 0) return false;
|
|
4586
|
+
return run.content.every(isMergeableContent);
|
|
4587
|
+
}
|
|
4588
|
+
/**
|
|
4589
|
+
* May two adjacent runs become one?
|
|
4590
|
+
*
|
|
4591
|
+
* Every consolidation site asks this one question, because a merge that any
|
|
4592
|
+
* one site decides differently is a merge the next parse undoes. A run holds
|
|
4593
|
+
* three records a merged run can hold only one of: its typed formatting, the
|
|
4594
|
+
* attributes `w:r` carried that the model has no field for, and the `w:rPr`
|
|
4595
|
+
* children no reader took a value from. Merging two runs that disagree on any
|
|
4596
|
+
* of them discards the loser's copy, which is how both runs' `w:rsid*` used to
|
|
4597
|
+
* vanish at parse time — before the editor, before any gate could see it.
|
|
4598
|
+
*/
|
|
4599
|
+
function runsMergeable(a, b) {
|
|
4600
|
+
return canMergeRun(a) && canMergeRun(b) && formattingEquals(a.formatting, b.formatting) && preservedAttributesEqual(a.preservedAttributes, b.preservedAttributes);
|
|
4601
|
+
}
|
|
4602
|
+
/**
|
|
4603
|
+
* Merge the content of two runs into a single content array
|
|
4604
|
+
*/
|
|
4605
|
+
function mergeRunContent(content1, content2) {
|
|
4606
|
+
const result = [];
|
|
4607
|
+
for (const c of content1) result.push(c);
|
|
4608
|
+
const lastText = result.at(-1);
|
|
4609
|
+
const firstText = content2[0];
|
|
4610
|
+
if (lastText?.type === "text" && firstText?.type === "text") {
|
|
4611
|
+
result[result.length - 1] = {
|
|
4612
|
+
type: "text",
|
|
4613
|
+
text: lastText.text + firstText.text
|
|
4614
|
+
};
|
|
4615
|
+
for (let i = 1; i < content2.length; i++) result.push(content2[i]);
|
|
4616
|
+
} else for (const c of content2) result.push(c);
|
|
4617
|
+
return result;
|
|
4618
|
+
}
|
|
4619
|
+
// src/ops/review.ts
|
|
4620
|
+
/**
|
|
4621
|
+
* Tracked changes: how an operation carrying a {@link RevisionStamp} records
|
|
4622
|
+
* its edit instead of making it, and the pieces resolution shares with it.
|
|
4623
|
+
*
|
|
4624
|
+
* A tracked insertion or deletion wraps the leaves it names in an `Insertion`
|
|
4625
|
+
* or `Deletion` carrying the stamp. The wrapper goes at the deepest level
|
|
4626
|
+
* that admits one: the paragraph, an inline content control, a transparent
|
|
4627
|
+
* wrapper, and for a deletion also an insertion or a move the range falls
|
|
4628
|
+
* inside. Records below that level which the range cuts (runs, hyperlinks,
|
|
4629
|
+
* and for an insertion another stamp's insertion) are cut at the range ends,
|
|
4630
|
+
* each piece keeping every field of the record, as a direct edit cuts them.
|
|
4631
|
+
*/
|
|
4632
|
+
/** The tracked-change metadata a stamp writes on the records it creates. */
|
|
4633
|
+
const stampInfo = (stamp) => {
|
|
4634
|
+
const info = {
|
|
4635
|
+
id: stamp.id,
|
|
4636
|
+
author: stamp.author,
|
|
4637
|
+
date: stamp.date
|
|
4638
|
+
};
|
|
4639
|
+
if (stamp.initials !== void 0) info.initials = stamp.initials;
|
|
4640
|
+
if (stamp.suggestionId !== void 0) info.suggestionId = stamp.suggestionId;
|
|
4641
|
+
return info;
|
|
4642
|
+
};
|
|
4643
|
+
/** Whether a record's metadata is the stamp's, whatever its id. */
|
|
4644
|
+
const carriesStamp = (info, stamp) => structurallyEqual({
|
|
4645
|
+
...info,
|
|
4646
|
+
id: 0
|
|
4647
|
+
}, {
|
|
4648
|
+
...stampInfo(stamp),
|
|
4649
|
+
id: 0
|
|
4650
|
+
});
|
|
4651
|
+
const isTrackedWrapper = (node) => node.type === "insertion" || node.type === "deletion" || node.type === "moveFrom" || node.type === "moveTo";
|
|
4652
|
+
/** Tracked content on its way in: an insertion or the destination of a move. */
|
|
4653
|
+
const isAddedRevision = (node) => node.type === "insertion" || node.type === "moveTo";
|
|
4654
|
+
/** Tracked content on its way out: a deletion or the source of a move. */
|
|
4655
|
+
const isRemovedRevisionNode = (node) => node.type === "deletion" || node.type === "moveFrom";
|
|
4656
|
+
/** The tracked-change metadata of every record a paragraph and its content carry. */
|
|
4657
|
+
const revisionInfosOf = (paragraph) => {
|
|
4658
|
+
const out = [];
|
|
4659
|
+
for (const change of paragraph.propertyChanges ?? []) out.push(change.info);
|
|
4660
|
+
if (paragraph.pPrMark !== void 0) out.push(paragraph.pPrMark.info);
|
|
4661
|
+
const visit = (nodes) => {
|
|
4662
|
+
for (const node of nodes) {
|
|
4663
|
+
if (isTrackedWrapper(node)) out.push(node.info);
|
|
4664
|
+
else if (node.type === "run") for (const change of node.propertyChanges ?? []) out.push(change.info);
|
|
4665
|
+
visit(childNodes(node) ?? []);
|
|
4666
|
+
}
|
|
4667
|
+
};
|
|
4668
|
+
visit(paragraph.content);
|
|
4669
|
+
return out;
|
|
4670
|
+
};
|
|
4671
|
+
/**
|
|
4672
|
+
* The revision ids of the records carrying the stamp that `known` does not
|
|
4673
|
+
* name (slot keys): the records an operation created, in document order.
|
|
4674
|
+
*/
|
|
4675
|
+
const stampedRevisionIds = (paragraphs, stamp, known) => {
|
|
4676
|
+
const out = [];
|
|
4677
|
+
for (const paragraph of paragraphs) for (const info of revisionInfosOf(paragraph)) {
|
|
4678
|
+
const key = slotKey({
|
|
4679
|
+
space: IDENTITY_SPACES.REVISION,
|
|
4680
|
+
id: info.id
|
|
4681
|
+
});
|
|
4682
|
+
if (carriesStamp(info, stamp) && !known.has(key) && !out.includes(info.id)) out.push(info.id);
|
|
4683
|
+
}
|
|
4684
|
+
return out;
|
|
4685
|
+
};
|
|
4686
|
+
const MARK_KEYS = new Set(PARAGRAPH_MARK_FORMATTING_KEYS);
|
|
4687
|
+
/** Whether a patch or property set names a key of the paragraph mark's run properties. */
|
|
4688
|
+
const namesMarkFormatting = (formatting) => Object.entries(formatting).some(([key, value]) => value !== void 0 && MARK_KEYS.has(key));
|
|
4689
|
+
const pick = (formatting, keep) => {
|
|
4690
|
+
if (formatting === void 0) return;
|
|
4691
|
+
return Object.fromEntries(Object.entries(formatting).filter(([key]) => keep(key)));
|
|
4692
|
+
};
|
|
4693
|
+
/** The paragraph properties a `w:pPrChange` records: all but the mark's run properties. */
|
|
4694
|
+
const paragraphPropertiesOf = (formatting) => pick(formatting, (key) => !MARK_KEYS.has(key));
|
|
4695
|
+
/** The paragraph mark's run properties of a property set; `undefined` when it states none. */
|
|
4696
|
+
const markFormattingOf = (formatting) => {
|
|
4697
|
+
const marks = pick(formatting, (key) => MARK_KEYS.has(key));
|
|
4698
|
+
return marks === void 0 || Object.keys(marks).length === 0 ? void 0 : marks;
|
|
4699
|
+
};
|
|
4700
|
+
/** Whether two property sets state the same paragraph properties. */
|
|
4185
4701
|
const sameParagraphProperties = (left, right) => structurallyEqual(paragraphPropertiesOf(left), paragraphPropertiesOf(right));
|
|
4186
4702
|
/** Whether two property sets state the same run properties for the paragraph mark. */
|
|
4187
4703
|
const sameMarkFormatting = (left, right) => structurallyEqual(markFormattingOf(left) ?? {}, markFormattingOf(right) ?? {});
|
|
@@ -4273,10 +4789,71 @@ const coveredAction = (node, kind) => {
|
|
|
4273
4789
|
}
|
|
4274
4790
|
return "wrap";
|
|
4275
4791
|
};
|
|
4792
|
+
/** Cut facts a deletion's merge can carry: the join depths at its edges and on removal. */
|
|
4793
|
+
const plainCutJoins = (joins) => joins !== void 0 && Object.keys(joins).every((key) => key === "before" || key === "after" || key === "remove");
|
|
4794
|
+
/**
|
|
4795
|
+
* Two neighbouring deletions carrying one stamp as one record, when each was
|
|
4796
|
+
* cut by an edit and records nothing but its join depths, or came with the
|
|
4797
|
+
* file and records no cut at all. Resolving the one record does what
|
|
4798
|
+
* resolving both does. Rejecting joins the content at each outer edge as its
|
|
4799
|
+
* side's deletion did and, between them, as deeply as either did; accepting
|
|
4800
|
+
* rejoins what is left either side as deeply as either removal did. A
|
|
4801
|
+
* deletion that came with the file joins alike runs at its edges, so the
|
|
4802
|
+
* record either one came with keeps no depths: alike runs meeting between
|
|
4803
|
+
* the two are merged, as rejecting would merge them. `keep` names the
|
|
4804
|
+
* wrapper whose identity stands. `undefined` when the pair records anything
|
|
4805
|
+
* else.
|
|
4806
|
+
*/
|
|
4807
|
+
const mergedCutDeletions = (left, right, keep) => {
|
|
4808
|
+
if (left.type !== "deletion" || right.type !== "deletion") return void 0;
|
|
4809
|
+
const leftJoins = left.resolutionJoins;
|
|
4810
|
+
const rightJoins = right.resolutionJoins;
|
|
4811
|
+
const cut = (joins) => joins === void 0 || plainCutJoins(joins);
|
|
4812
|
+
if (!cut(leftJoins) || !cut(rightJoins) || leftJoins === void 0 && rightJoins === void 0) return void 0;
|
|
4813
|
+
const last = left.content.at(-1);
|
|
4814
|
+
const first = right.content.at(0);
|
|
4815
|
+
let depth = 0;
|
|
4816
|
+
if (plainCutJoins(leftJoins) && plainCutJoins(rightJoins)) depth = Math.max(leftJoins.after, rightJoins.before);
|
|
4817
|
+
else if (last?.type === "run" && first?.type === "run" && runsMergeable(last, first)) depth = alikeDepth(last, first);
|
|
4818
|
+
let retires = false;
|
|
4819
|
+
const content = mergeLists(left.content, right.content, depth, {
|
|
4820
|
+
mode: "exact",
|
|
4821
|
+
onMerge: (_first, second) => {
|
|
4822
|
+
retires ||= identitySlots(second).length > 0;
|
|
4823
|
+
}
|
|
4824
|
+
});
|
|
4825
|
+
if (content === void 0 || retires) return void 0;
|
|
4826
|
+
const merged = rebuildNode(keep, content);
|
|
4827
|
+
if (merged.type !== "deletion") return void 0;
|
|
4828
|
+
const { resolutionJoins: _joins, ...authored } = merged;
|
|
4829
|
+
return plainCutJoins(leftJoins) && plainCutJoins(rightJoins) ? {
|
|
4830
|
+
...authored,
|
|
4831
|
+
resolutionJoins: {
|
|
4832
|
+
before: leftJoins.before,
|
|
4833
|
+
after: rightJoins.after,
|
|
4834
|
+
remove: Math.max(leftJoins.remove, rightJoins.remove)
|
|
4835
|
+
}
|
|
4836
|
+
} : authored;
|
|
4837
|
+
};
|
|
4276
4838
|
/** Merge a created wrapper into a neighbour carrying the same stamp: the left one, else the right. */
|
|
4277
4839
|
const mergeWithStampedNeighbour = (out, index, state) => {
|
|
4278
4840
|
const wrapper = out[index];
|
|
4279
4841
|
if (wrapper === void 0 || !isTrackedWrapper(wrapper)) return;
|
|
4842
|
+
const stamped = (neighbour) => neighbour !== void 0 && isTrackedWrapper(neighbour) && neighbour.type === wrapper.type && !state.created.has(neighbour) && carriesStamp(neighbour.info, state.stamp);
|
|
4843
|
+
const before = out[index - 1];
|
|
4844
|
+
const after = out[index + 1];
|
|
4845
|
+
const intoBefore = stamped(before) ? mergedCutDeletions(before, wrapper, before) : void 0;
|
|
4846
|
+
if (intoBefore !== void 0) {
|
|
4847
|
+
state.created.delete(wrapper);
|
|
4848
|
+
out.splice(index - 1, 2, intoBefore);
|
|
4849
|
+
return;
|
|
4850
|
+
}
|
|
4851
|
+
const intoAfter = stamped(after) ? mergedCutDeletions(wrapper, after, after) : void 0;
|
|
4852
|
+
if (intoAfter !== void 0) {
|
|
4853
|
+
state.created.delete(wrapper);
|
|
4854
|
+
out.splice(index, 2, intoAfter);
|
|
4855
|
+
return;
|
|
4856
|
+
}
|
|
4280
4857
|
const matching = (neighbour) => neighbour !== void 0 && isTrackedWrapper(neighbour) && neighbour.type === wrapper.type && wrapper.resolutionJoins?.acceptance === void 0 && neighbour.resolutionJoins?.acceptance === void 0 && !(wrapper.resolutionJoins !== void 0 && neighbour.resolutionJoins !== void 0 && (wrapper.resolutionJoins.before > 0 || wrapper.resolutionJoins.after > 0 || wrapper.resolutionJoins.remove > 0 || neighbour.resolutionJoins.before > 0 || neighbour.resolutionJoins.after > 0 || neighbour.resolutionJoins.remove > 0)) && !state.created.has(neighbour) && carriesStamp(neighbour.info, state.stamp);
|
|
4281
4858
|
const left = out[index - 1];
|
|
4282
4859
|
if (matching(left)) {
|
|
@@ -4871,11 +5448,7 @@ const freshenIdentities = ({ before, after, newIds, usedElsewhere, inserted = /*
|
|
|
4871
5448
|
kept.add(index);
|
|
4872
5449
|
keepers.set(record, kept);
|
|
4873
5450
|
}
|
|
4874
|
-
const taken = /* @__PURE__ */ new Set([
|
|
4875
|
-
...elsewhere,
|
|
4876
|
-
...identityKeysOf(before),
|
|
4877
|
-
...identityKeysOf(after)
|
|
4878
|
-
]);
|
|
5451
|
+
const taken = /* @__PURE__ */ new Set([...identityKeysOf(before), ...identityKeysOf(after)]);
|
|
4879
5452
|
const pools = {
|
|
4880
5453
|
[IDENTITY_SPACES.REVISION]: newIds.revision ?? [],
|
|
4881
5454
|
[IDENTITY_SPACES.CONTROL]: newIds.control ?? []
|
|
@@ -4897,7 +5470,7 @@ const freshenIdentities = ({ before, after, newIds, usedElsewhere, inserted = /*
|
|
|
4897
5470
|
space: slot.space,
|
|
4898
5471
|
id
|
|
4899
5472
|
});
|
|
4900
|
-
return taken.has(key) || reserved.has(key);
|
|
5473
|
+
return taken.has(key) || elsewhere.has(key) || reserved.has(key);
|
|
4901
5474
|
};
|
|
4902
5475
|
let fresh = pool[next[slot.space]];
|
|
4903
5476
|
while (fresh !== void 0 && Number.isInteger(fresh) && isTaken(fresh)) {
|
|
@@ -4964,6 +5537,45 @@ const freshenIdentities = ({ before, after, newIds, usedElsewhere, inserted = /*
|
|
|
4964
5537
|
id: invalid
|
|
4965
5538
|
};
|
|
4966
5539
|
};
|
|
5540
|
+
// src/ops/moves.ts
|
|
5541
|
+
const RENAMED = {
|
|
5542
|
+
from: {
|
|
5543
|
+
record: "deletion",
|
|
5544
|
+
move: "moveFrom"
|
|
5545
|
+
},
|
|
5546
|
+
to: {
|
|
5547
|
+
record: "insertion",
|
|
5548
|
+
move: "moveTo"
|
|
5549
|
+
}
|
|
5550
|
+
};
|
|
5551
|
+
/**
|
|
5552
|
+
* `content` with every record the operation's own revision `revisionId`
|
|
5553
|
+
* created renamed to the move half it is: a deletion to `w:moveFrom`, an
|
|
5554
|
+
* insertion to `w:moveTo`. Records of other revisions are left as they are.
|
|
5555
|
+
*/
|
|
5556
|
+
const asMoveHalf = (content, revisionId, half) => content.map((item) => movedItem(item, revisionId, half));
|
|
5557
|
+
const movedItem = (item, revisionId, half) => {
|
|
5558
|
+
if (typeof item !== "object" || item === null) return item;
|
|
5559
|
+
if (Array.isArray(item)) return item.map((child) => movedItem(child, revisionId, half));
|
|
5560
|
+
const record = item;
|
|
5561
|
+
let next = record;
|
|
5562
|
+
const content = record["content"];
|
|
5563
|
+
if (Array.isArray(content)) next = {
|
|
5564
|
+
...next,
|
|
5565
|
+
content: movedItem(content, revisionId, half)
|
|
5566
|
+
};
|
|
5567
|
+
const children = record["children"];
|
|
5568
|
+
if (Array.isArray(children)) next = {
|
|
5569
|
+
...next,
|
|
5570
|
+
children: movedItem(children, revisionId, half)
|
|
5571
|
+
};
|
|
5572
|
+
const info = record["info"];
|
|
5573
|
+
if (record["type"] === RENAMED[half].record && info?.id === revisionId) next = {
|
|
5574
|
+
...next,
|
|
5575
|
+
type: RENAMED[half].move
|
|
5576
|
+
};
|
|
5577
|
+
return next === record ? item : next;
|
|
5578
|
+
};
|
|
4967
5579
|
// src/ops/rangeBoundaries.ts
|
|
4968
5580
|
/**
|
|
4969
5581
|
* Range boundaries in content a resolution removes.
|
|
@@ -5491,8 +6103,60 @@ const DIRECT_DELETION_STAMP = {
|
|
|
5491
6103
|
author: "",
|
|
5492
6104
|
date: ""
|
|
5493
6105
|
};
|
|
6106
|
+
/**
|
|
6107
|
+
* The paragraph that receives `paragraph`'s content when its mark runs into
|
|
6108
|
+
* the following `table`: the first paragraph of its first cell, through
|
|
6109
|
+
* nested tables, where no pending row or cell change stands in the way.
|
|
6110
|
+
*/
|
|
6111
|
+
const paragraphTableRunInTarget = (paragraph, table) => {
|
|
6112
|
+
if (paragraph.sectionProperties !== void 0) return void 0;
|
|
6113
|
+
const tables = [];
|
|
6114
|
+
let current = table;
|
|
6115
|
+
for (;;) {
|
|
6116
|
+
const row = current.rows.at(0);
|
|
6117
|
+
const cell = row?.cells.at(0);
|
|
6118
|
+
if (!row || !cell) return void 0;
|
|
6119
|
+
if (row.structuralChange?.type === "tableRowInsertion" || row.structuralChange?.type === "tableRowDeletion" || cell.structuralChange !== void 0) return;
|
|
6120
|
+
tables.push(current);
|
|
6121
|
+
const target = cell.content.at(0);
|
|
6122
|
+
if (target?.type === "paragraph") return target.sectionProperties === void 0 ? {
|
|
6123
|
+
receiver: target,
|
|
6124
|
+
tables
|
|
6125
|
+
} : void 0;
|
|
6126
|
+
if (target?.type !== "table") return void 0;
|
|
6127
|
+
current = target;
|
|
6128
|
+
}
|
|
6129
|
+
};
|
|
6130
|
+
/**
|
|
6131
|
+
* Whether the mark of `paragraph`, which `table` follows in `blocks`, can be
|
|
6132
|
+
* resolved away: the block list goes on ending with a paragraph, and the
|
|
6133
|
+
* table has a paragraph to receive what runs in. A tracked deletion before a
|
|
6134
|
+
* table is admitted only where this holds, so each decision on it resolves.
|
|
6135
|
+
*/
|
|
6136
|
+
const markBeforeTableResolves = ({ blocks, paragraph, table }) => blocks.at(-1)?.type === "paragraph" && paragraphTableRunInTarget(paragraph, table) !== void 0;
|
|
6137
|
+
/**
|
|
6138
|
+
* Whether `block`, following a paragraph, is a block that paragraph's mark
|
|
6139
|
+
* neither joins nor runs into: a block content control, custom XML or a
|
|
6140
|
+
* preserved block. Resolving the mark away removes the paragraph only when
|
|
6141
|
+
* nothing is left in it ({@link paragraphMarkOutcome}).
|
|
6142
|
+
*/
|
|
6143
|
+
const isOpaqueFollower = (block) => block !== void 0 && block.type !== "paragraph" && block.type !== "table" && !isBlockBoundary(block);
|
|
6144
|
+
/**
|
|
6145
|
+
* The paragraph that receives `paragraph`'s content when its mark runs into
|
|
6146
|
+
* the following `block`, a block content control or custom XML: its first
|
|
6147
|
+
* paragraph, through nested ones. A control whose content is locked, or one
|
|
6148
|
+
* that starts with anything else, offers none.
|
|
6149
|
+
*/
|
|
6150
|
+
const paragraphBlockRunInTarget = (paragraph, block) => {
|
|
6151
|
+
if (paragraph.sectionProperties !== void 0) return void 0;
|
|
6152
|
+
if (block.type !== "blockSdt" && block.type !== "blockCustomXml") return void 0;
|
|
6153
|
+
if (block.type === "blockSdt" && (block.properties.lock === "contentLocked" || block.properties.lock === "sdtContentLocked")) return void 0;
|
|
6154
|
+
const first = block.content.at(0);
|
|
6155
|
+
if (first?.type !== "paragraph") return first === void 0 ? void 0 : paragraphBlockRunInTarget(paragraph, first);
|
|
6156
|
+
return first.sectionProperties === void 0 ? { receiver: first } : void 0;
|
|
6157
|
+
};
|
|
5494
6158
|
/** The single structural outcome of resolving a paragraph mark. */
|
|
5495
|
-
const paragraphMarkOutcome = ({ follows, holdsContent, markWasAdded, containerStillEndsWithParagraph, followingTableCanReceiveRunIn, needsFollowingCommentHost = false, tableRemovalIsTrackable = true, removedSectionProperties = false, precedingSectionOwner = "unavailable", followingHasSectionProperties = false }) => {
|
|
6159
|
+
const paragraphMarkOutcome = ({ follows, holdsContent, markWasAdded, containerStillEndsWithParagraph, followingTableCanReceiveRunIn, holdsContentOnceAccepted = holdsContent, followingBlockCanReceiveRunIn = false, needsFollowingCommentHost = false, tableRemovalIsTrackable = true, removedSectionProperties = false, precedingSectionOwner = "unavailable", followingHasSectionProperties = false }) => {
|
|
5496
6160
|
let sectionDestination = "none";
|
|
5497
6161
|
if (removedSectionProperties && precedingSectionOwner === "available") sectionDestination = "preceding";
|
|
5498
6162
|
else if (removedSectionProperties && precedingSectionOwner !== "same" && !followingHasSectionProperties) sectionDestination = "following";
|
|
@@ -5528,6 +6192,29 @@ const paragraphMarkOutcome = ({ follows, holdsContent, markWasAdded, containerSt
|
|
|
5528
6192
|
kind: "keep",
|
|
5529
6193
|
sectionDestination
|
|
5530
6194
|
};
|
|
6195
|
+
case "block":
|
|
6196
|
+
if (removalWouldDropSection) return {
|
|
6197
|
+
kind: "keep",
|
|
6198
|
+
sectionDestination: "none"
|
|
6199
|
+
};
|
|
6200
|
+
if (markWasAdded) return {
|
|
6201
|
+
kind: "keep",
|
|
6202
|
+
sectionDestination
|
|
6203
|
+
};
|
|
6204
|
+
if (!holdsContent) return containerStillEndsWithParagraph ? {
|
|
6205
|
+
kind: "remove",
|
|
6206
|
+
sectionDestination
|
|
6207
|
+
} : {
|
|
6208
|
+
kind: "keep",
|
|
6209
|
+
sectionDestination
|
|
6210
|
+
};
|
|
6211
|
+
return !holdsContentOnceAccepted && followingBlockCanReceiveRunIn ? {
|
|
6212
|
+
kind: "run-in",
|
|
6213
|
+
sectionDestination
|
|
6214
|
+
} : {
|
|
6215
|
+
kind: "keep",
|
|
6216
|
+
sectionDestination
|
|
6217
|
+
};
|
|
5531
6218
|
case "nothing":
|
|
5532
6219
|
if (removalWouldDropSection) return {
|
|
5533
6220
|
kind: "keep",
|
|
@@ -5564,7 +6251,13 @@ const deleteBlocks = ({ document, op, applyOps }) => {
|
|
|
5564
6251
|
const list = blockListAt(body.content, first.list);
|
|
5565
6252
|
const following = list[last.index + 1];
|
|
5566
6253
|
const terminal = last.index === list.length - 1;
|
|
5567
|
-
|
|
6254
|
+
const trackedBeforeTable = op.revision !== void 0 && following?.type === "table" && markBeforeTableResolves({
|
|
6255
|
+
blocks: list,
|
|
6256
|
+
paragraph: last.paragraph,
|
|
6257
|
+
table: following
|
|
6258
|
+
});
|
|
6259
|
+
const trackedBeforeBlock = op.revision !== void 0 && isOpaqueFollower(following) && list.at(-1)?.type === "paragraph";
|
|
6260
|
+
if (following?.type !== "paragraph" && !terminal && !trackedBeforeTable && !trackedBeforeBlock) return refuse(DOCUMENT_OP_REFUSAL_REASONS.UNTRACKABLE, "Deletion requires a following paragraph or a container-final paragraph.");
|
|
5568
6261
|
const preceding = terminal ? list[first.index - 1] : void 0;
|
|
5569
6262
|
const previous = preceding?.type === "paragraph" ? preceding : void 0;
|
|
5570
6263
|
const selected = found.map(({ paragraph }) => paragraph);
|
|
@@ -5645,7 +6338,10 @@ const deleteBlocks = ({ document, op, applyOps }) => {
|
|
|
5645
6338
|
}
|
|
5646
6339
|
});
|
|
5647
6340
|
}
|
|
5648
|
-
|
|
6341
|
+
const revision = op.revision;
|
|
6342
|
+
const losesMark = (paragraph) => !terminal || paragraph !== last.paragraph;
|
|
6343
|
+
const joinsNext = new Set(affected.filter((paragraph) => losesMark(paragraph) && (paragraph !== last.paragraph || following?.type === "paragraph") && (paragraph.pPrMark?.kind === "ins" || paragraph.pPrMark?.kind === "moveTo") && paragraph.pPrMark.info.author === revision.author));
|
|
6344
|
+
if (affected.some((paragraph) => paragraph.pPrMark !== void 0 && !joinsNext.has(paragraph))) return refuse(DOCUMENT_OP_REFUSAL_REASONS.REVISION_CONFLICT, "Deletion would overwrite a paragraph review record.");
|
|
5649
6345
|
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.");
|
|
5650
6346
|
const selectedSet = new Set(selected);
|
|
5651
6347
|
const tracked = [];
|
|
@@ -5656,13 +6352,16 @@ const deleteBlocks = ({ document, op, applyOps }) => {
|
|
|
5656
6352
|
if (form.kind === "refused") return refuse(form.reason, "The selected paragraph content cannot be tracked.");
|
|
5657
6353
|
if (form.kind === "deleted") next = {
|
|
5658
6354
|
...paragraph,
|
|
5659
|
-
content: form.content
|
|
6355
|
+
content: op.move === true ? asMoveHalf(form.content, op.revision.id, "from") : form.content
|
|
5660
6356
|
};
|
|
5661
6357
|
}
|
|
5662
|
-
if (
|
|
6358
|
+
if (joinsNext.has(paragraph)) {
|
|
6359
|
+
const { pPrMark: _removed, ...unmarked } = next;
|
|
6360
|
+
next = unmarked;
|
|
6361
|
+
} else if (losesMark(paragraph)) next = {
|
|
5663
6362
|
...next,
|
|
5664
6363
|
pPrMark: {
|
|
5665
|
-
kind: "del",
|
|
6364
|
+
kind: op.move === true ? "moveFrom" : "del",
|
|
5666
6365
|
info: stampInfo(op.revision)
|
|
5667
6366
|
}
|
|
5668
6367
|
};
|
|
@@ -5690,14 +6389,36 @@ const deleteBlocks = ({ document, op, applyOps }) => {
|
|
|
5690
6389
|
case "needsIds": return refuse(DOCUMENT_OP_REFUSAL_REASONS.NEEDS_NEW_IDS, "Paragraph deletion needs more revision ids.");
|
|
5691
6390
|
case "invalidId": return refuse(DOCUMENT_OP_REFUSAL_REASONS.INVALID_NEW_ID, "Paragraph deletion received an invalid revision id.");
|
|
5692
6391
|
case "fresh": {
|
|
5693
|
-
const
|
|
6392
|
+
const marked = following?.type === "paragraph" ? [...fresh.paragraphs, following] : fresh.paragraphs;
|
|
6393
|
+
const original = following?.type === "paragraph" ? [...affected, following] : affected;
|
|
6394
|
+
const changed = following?.type === "paragraph" ? [...tracked, following] : tracked;
|
|
6395
|
+
const blocks = [];
|
|
6396
|
+
const removed = [];
|
|
6397
|
+
const modified = /* @__PURE__ */ new Set();
|
|
6398
|
+
let carried;
|
|
6399
|
+
for (const [index, paragraph] of marked.entries()) {
|
|
6400
|
+
const source = original[index];
|
|
6401
|
+
if (source !== void 0 && joinsNext.has(source)) {
|
|
6402
|
+
carried = [...carried ?? [], ...paragraph.content];
|
|
6403
|
+
removed.push(paragraph.paraId ?? "");
|
|
6404
|
+
continue;
|
|
6405
|
+
}
|
|
6406
|
+
const joined = carried === void 0 ? paragraph : {
|
|
6407
|
+
...paragraph,
|
|
6408
|
+
content: [...carried, ...paragraph.content]
|
|
6409
|
+
};
|
|
6410
|
+
if (carried !== void 0 || changed[index] !== source) modified.add(joined.paraId ?? "");
|
|
6411
|
+
carried = void 0;
|
|
6412
|
+
blocks.push(joined);
|
|
6413
|
+
}
|
|
6414
|
+
const result = applyOps(document, [replacement(blocks)]);
|
|
5694
6415
|
if (result.isErr()) return refuse(result.error.reason, result.error.message);
|
|
5695
6416
|
return Result.ok({
|
|
5696
6417
|
...result.value,
|
|
5697
6418
|
touched: {
|
|
5698
|
-
modified:
|
|
6419
|
+
modified: [...modified],
|
|
5699
6420
|
inserted: [],
|
|
5700
|
-
removed
|
|
6421
|
+
removed
|
|
5701
6422
|
}
|
|
5702
6423
|
});
|
|
5703
6424
|
}
|
|
@@ -5722,7 +6443,7 @@ const insertBlocks = ({ document, op, applyOps }) => {
|
|
|
5722
6443
|
const following = blockListAt(body.content, location.list)[location.index + 1];
|
|
5723
6444
|
const before = op.at.type === "before" || following?.type === "paragraph";
|
|
5724
6445
|
const anchor = op.at.type === "after" && following?.type === "paragraph" ? following : location.paragraph;
|
|
5725
|
-
const takesOverMark = op.revision !== void 0 && !before && following === void 0;
|
|
6446
|
+
const takesOverMark = op.revision !== void 0 && !before && (following === void 0 || isBlockBoundary(following) || following.type !== "table");
|
|
5726
6447
|
if (takesOverMark && (location.paragraph.pPrMark !== void 0 || location.paragraph.reviewCarrier !== void 0 || (location.paragraph.propertyChanges?.length ?? 0) > 0)) return refuse(DOCUMENT_OP_REFUSAL_REASONS.UNTRACKABLE, "A tracked insertion after a paragraph no paragraph follows needs the anchor's mark free of review records.");
|
|
5727
6448
|
const replacement = (blocks, replacedAnchor = anchor) => ({
|
|
5728
6449
|
type: DOCUMENT_OP_TYPES.REPLACE_BLOCKS,
|
|
@@ -5755,6 +6476,7 @@ const insertBlocks = ({ document, op, applyOps }) => {
|
|
|
5755
6476
|
});
|
|
5756
6477
|
if (identityKeysIn(op.blocks).length > 0) return refuse(DOCUMENT_OP_REFUSAL_REASONS.UNTRACKABLE, "Tracked insertion of identified review or content-control records is unsupported.");
|
|
5757
6478
|
const revision = op.revision;
|
|
6479
|
+
const markKind = op.move === true ? "moveTo" : "ins";
|
|
5758
6480
|
const blocks = [];
|
|
5759
6481
|
for (const [index, paragraph] of canonicalBlocks.entries()) {
|
|
5760
6482
|
const wrapped = wrapTracked({
|
|
@@ -5771,14 +6493,16 @@ const insertBlocks = ({ document, op, applyOps }) => {
|
|
|
5771
6493
|
stamp: op.revision
|
|
5772
6494
|
});
|
|
5773
6495
|
if (wrapped.kind === "refused") return refuse(wrapped.reason, "The inserted content cannot be tracked.");
|
|
5774
|
-
const
|
|
6496
|
+
const wrappedContent = wrapped.kind === "wrapped" ? wrapped.content : paragraph.content;
|
|
6497
|
+
const content = op.move === true ? asMoveHalf(wrappedContent, revision.id, "to") : wrappedContent;
|
|
5775
6498
|
if (!takesOverMark || index < canonicalBlocks.length - 1) {
|
|
5776
6499
|
blocks.push({
|
|
5777
6500
|
...paragraph,
|
|
5778
6501
|
content,
|
|
5779
6502
|
pPrMark: {
|
|
5780
|
-
kind:
|
|
5781
|
-
info: stampInfo(revision)
|
|
6503
|
+
kind: markKind,
|
|
6504
|
+
info: stampInfo(revision),
|
|
6505
|
+
...takesOverMark ? { survivor: "first" } : {}
|
|
5782
6506
|
}
|
|
5783
6507
|
});
|
|
5784
6508
|
continue;
|
|
@@ -5798,13 +6522,13 @@ const insertBlocks = ({ document, op, applyOps }) => {
|
|
|
5798
6522
|
const anchorBlocks = takesOverMark ? [{
|
|
5799
6523
|
...location.paragraph,
|
|
5800
6524
|
pPrMark: {
|
|
5801
|
-
kind:
|
|
6525
|
+
kind: markKind,
|
|
5802
6526
|
info: stampInfo(revision),
|
|
5803
6527
|
survivor: "first"
|
|
5804
6528
|
}
|
|
5805
6529
|
}] : [];
|
|
5806
6530
|
const fresh = freshenIdentities({
|
|
5807
|
-
before: [],
|
|
6531
|
+
before: takesOverMark ? [location.paragraph] : [],
|
|
5808
6532
|
after: [...anchorBlocks, ...blocks],
|
|
5809
6533
|
newIds: op.newIds ?? {},
|
|
5810
6534
|
usedElsewhere: () => new Set(packageIdentityKeys(document.package))
|
|
@@ -5889,188 +6613,14 @@ const resolveGap = ({ paragraph, position, fallback }) => {
|
|
|
5889
6613
|
offset,
|
|
5890
6614
|
zeroWidthBefore: available
|
|
5891
6615
|
};
|
|
5892
|
-
case "beforeAll": return {
|
|
5893
|
-
offset,
|
|
5894
|
-
zeroWidthBefore: 0
|
|
5895
|
-
};
|
|
5896
|
-
case "insertion": return defaultInsertionGap(paragraph.content, offset);
|
|
5897
|
-
default: return fallback;
|
|
5898
|
-
}
|
|
5899
|
-
};
|
|
5900
|
-
// src/ops/runMerge.ts
|
|
5901
|
-
/**
|
|
5902
|
-
* Check if two TextFormatting objects are equivalent
|
|
5903
|
-
*
|
|
5904
|
-
* Uses deep comparison of all properties to determine if runs
|
|
5905
|
-
* can be merged without losing formatting information.
|
|
5906
|
-
*/
|
|
5907
|
-
function formattingEquals(a, b) {
|
|
5908
|
-
if (!a && !b) return true;
|
|
5909
|
-
if (!a || !b) return false;
|
|
5910
|
-
if (a.bold !== b.bold) return false;
|
|
5911
|
-
if (a.boldCs !== b.boldCs) return false;
|
|
5912
|
-
if (a.italic !== b.italic) return false;
|
|
5913
|
-
if (a.italicCs !== b.italicCs) return false;
|
|
5914
|
-
if (a.strike !== b.strike) return false;
|
|
5915
|
-
if (a.doubleStrike !== b.doubleStrike) return false;
|
|
5916
|
-
if (a.smallCaps !== b.smallCaps) return false;
|
|
5917
|
-
if (a.allCaps !== b.allCaps) return false;
|
|
5918
|
-
if (a.hidden !== b.hidden) return false;
|
|
5919
|
-
if (a.noProof !== b.noProof) return false;
|
|
5920
|
-
if (a.emboss !== b.emboss) return false;
|
|
5921
|
-
if (a.imprint !== b.imprint) return false;
|
|
5922
|
-
if (a.outline !== b.outline) return false;
|
|
5923
|
-
if (a.shadow !== b.shadow) return false;
|
|
5924
|
-
if (a.rtl !== b.rtl) return false;
|
|
5925
|
-
if (a.cs !== b.cs) return false;
|
|
5926
|
-
if (a.fontSize !== b.fontSize) return false;
|
|
5927
|
-
if (a.fontSizeCs !== b.fontSizeCs) return false;
|
|
5928
|
-
if (a.spacing !== b.spacing) return false;
|
|
5929
|
-
if (a.position !== b.position) return false;
|
|
5930
|
-
if (a.scale !== b.scale) return false;
|
|
5931
|
-
if (a.kerning !== b.kerning) return false;
|
|
5932
|
-
if (a.vertAlign !== b.vertAlign) return false;
|
|
5933
|
-
if (a.highlight !== b.highlight) return false;
|
|
5934
|
-
if (a.effect !== b.effect) return false;
|
|
5935
|
-
if (a.emphasisMark !== b.emphasisMark) return false;
|
|
5936
|
-
if (a.styleId !== b.styleId) return false;
|
|
5937
|
-
if (!underlineEquals(a.underline, b.underline)) return false;
|
|
5938
|
-
if (!colorEquals(a.color, b.color)) return false;
|
|
5939
|
-
if (!shadingEquals(a.shading, b.shading)) return false;
|
|
5940
|
-
if (!fontFamilyEquals(a.fontFamily, b.fontFamily)) return false;
|
|
5941
|
-
if (!languageEquals(a.language, b.language)) return false;
|
|
5942
|
-
if (!preservedMarkupEquals(a.preserved, b.preserved)) return false;
|
|
5943
|
-
return true;
|
|
5944
|
-
}
|
|
5945
|
-
/**
|
|
5946
|
-
* Compare two verbatim sinks position by position.
|
|
5947
|
-
*
|
|
5948
|
-
* The sink is ordered — by `index`, then by source order within an index — and
|
|
5949
|
-
* the order is what puts the markup back between the same modelled siblings,
|
|
5950
|
-
* so equality is by sequence, not by set.
|
|
5951
|
-
*/
|
|
5952
|
-
function preservedMarkupEquals(a, b) {
|
|
5953
|
-
const left = a?.children ?? [];
|
|
5954
|
-
const right = b?.children ?? [];
|
|
5955
|
-
if (left.length !== right.length) return false;
|
|
5956
|
-
return left.every(({ index, xml }, position) => {
|
|
5957
|
-
const other = right[position];
|
|
5958
|
-
return other !== void 0 && other.index === index && other.xml === xml;
|
|
5959
|
-
});
|
|
5960
|
-
}
|
|
5961
|
-
/**
|
|
5962
|
-
* Compare two attribute remainders as sets.
|
|
5963
|
-
*
|
|
5964
|
-
* An element cannot carry one expanded name twice, so (namespace, name) orders
|
|
5965
|
-
* a remainder totally and the sorted keys compare exactly. Order is a set
|
|
5966
|
-
* comparison rather than a sequence one because attribute order in XML says
|
|
5967
|
-
* nothing: two runs that spelled the same attributes in a different order
|
|
5968
|
-
* carry the same remainder and may still become one run.
|
|
5969
|
-
*/
|
|
5970
|
-
function preservedAttributesEqual(a, b) {
|
|
5971
|
-
const left = canonicalRemainderKeys(a);
|
|
5972
|
-
const right = canonicalRemainderKeys(b);
|
|
5973
|
-
return left.length === right.length && left.every((key, index) => key === right[index]);
|
|
5974
|
-
}
|
|
5975
|
-
/** `\u0000` cannot appear in an XML name, namespace URI or attribute value. */
|
|
5976
|
-
const REMAINDER_KEY_SEPARATOR = "\0";
|
|
5977
|
-
const canonicalRemainderKeys = (attributes) => (attributes ?? []).map(({ namespace, name, value }) => [
|
|
5978
|
-
namespace ?? "",
|
|
5979
|
-
name,
|
|
5980
|
-
value
|
|
5981
|
-
].join(REMAINDER_KEY_SEPARATOR)).sort();
|
|
5982
|
-
function languageEquals(a, b) {
|
|
5983
|
-
return a?.val === b?.val && a?.eastAsia === b?.eastAsia && a?.bidi === b?.bidi;
|
|
5984
|
-
}
|
|
5985
|
-
/**
|
|
5986
|
-
* Compare underline settings
|
|
5987
|
-
*/
|
|
5988
|
-
function underlineEquals(a, b) {
|
|
5989
|
-
if (!a && !b) return true;
|
|
5990
|
-
if (!a || !b) return false;
|
|
5991
|
-
if (a.style !== b.style) return false;
|
|
5992
|
-
return colorEquals(a.color, b.color);
|
|
5993
|
-
}
|
|
5994
|
-
/**
|
|
5995
|
-
* Compare color values
|
|
5996
|
-
*/
|
|
5997
|
-
function colorEquals(a, b) {
|
|
5998
|
-
if (!a && !b) return true;
|
|
5999
|
-
if (!a || !b) return false;
|
|
6000
|
-
return a.rgb === b.rgb && a.auto === b.auto && a.themeColor === b.themeColor && a.themeTint === b.themeTint && a.themeShade === b.themeShade;
|
|
6001
|
-
}
|
|
6002
|
-
/**
|
|
6003
|
-
* Compare shading properties
|
|
6004
|
-
*/
|
|
6005
|
-
function shadingEquals(a, b) {
|
|
6006
|
-
if (!a && !b) return true;
|
|
6007
|
-
if (!a || !b) return false;
|
|
6008
|
-
if (a.pattern !== b.pattern) return false;
|
|
6009
|
-
if (!colorEquals(a.color, b.color)) return false;
|
|
6010
|
-
if (!colorEquals(a.fill, b.fill)) return false;
|
|
6011
|
-
return true;
|
|
6012
|
-
}
|
|
6013
|
-
/**
|
|
6014
|
-
* Compare font family settings
|
|
6015
|
-
*/
|
|
6016
|
-
function fontFamilyEquals(a, b) {
|
|
6017
|
-
if (!a && !b) return true;
|
|
6018
|
-
if (!a || !b) return false;
|
|
6019
|
-
return a.ascii === b.ascii && a.hAnsi === b.hAnsi && a.eastAsia === b.eastAsia && a.cs === b.cs && a.asciiTheme === b.asciiTheme && a.hAnsiTheme === b.hAnsiTheme && a.eastAsiaTheme === b.eastAsiaTheme && a.csTheme === b.csTheme;
|
|
6020
|
-
}
|
|
6021
|
-
/**
|
|
6022
|
-
* Check if a run contains only text content
|
|
6023
|
-
* (runs with special content like images, fields, etc. should not be merged)
|
|
6024
|
-
*/
|
|
6025
|
-
function isTextOnlyRun(run) {
|
|
6026
|
-
return run.content.every((c) => c.type === "text" || c.type === "softHyphen" || c.type === "noBreakHyphen");
|
|
6027
|
-
}
|
|
6028
|
-
/**
|
|
6029
|
-
* Check if run content can be merged (simple text types)
|
|
6030
|
-
*/
|
|
6031
|
-
function isMergeableContent(content) {
|
|
6032
|
-
return content.type === "text" || content.type === "softHyphen" || content.type === "noBreakHyphen";
|
|
6033
|
-
}
|
|
6034
|
-
/**
|
|
6035
|
-
* Check if a run can be merged with another run
|
|
6036
|
-
* Runs with breaks, tabs, images, fields, etc. act as merge boundaries
|
|
6037
|
-
*/
|
|
6038
|
-
function canMergeRun(run) {
|
|
6039
|
-
if (run.propertyChanges && run.propertyChanges.length > 0) return false;
|
|
6040
|
-
if (run.content.length === 0) return false;
|
|
6041
|
-
return run.content.every(isMergeableContent);
|
|
6042
|
-
}
|
|
6043
|
-
/**
|
|
6044
|
-
* May two adjacent runs become one?
|
|
6045
|
-
*
|
|
6046
|
-
* Every consolidation site asks this one question, because a merge that any
|
|
6047
|
-
* one site decides differently is a merge the next parse undoes. A run holds
|
|
6048
|
-
* three records a merged run can hold only one of: its typed formatting, the
|
|
6049
|
-
* attributes `w:r` carried that the model has no field for, and the `w:rPr`
|
|
6050
|
-
* children no reader took a value from. Merging two runs that disagree on any
|
|
6051
|
-
* of them discards the loser's copy, which is how both runs' `w:rsid*` used to
|
|
6052
|
-
* vanish at parse time — before the editor, before any gate could see it.
|
|
6053
|
-
*/
|
|
6054
|
-
function runsMergeable(a, b) {
|
|
6055
|
-
return canMergeRun(a) && canMergeRun(b) && formattingEquals(a.formatting, b.formatting) && preservedAttributesEqual(a.preservedAttributes, b.preservedAttributes);
|
|
6056
|
-
}
|
|
6057
|
-
/**
|
|
6058
|
-
* Merge the content of two runs into a single content array
|
|
6059
|
-
*/
|
|
6060
|
-
function mergeRunContent(content1, content2) {
|
|
6061
|
-
const result = [];
|
|
6062
|
-
for (const c of content1) result.push(c);
|
|
6063
|
-
const lastText = result.at(-1);
|
|
6064
|
-
const firstText = content2[0];
|
|
6065
|
-
if (lastText?.type === "text" && firstText?.type === "text") {
|
|
6066
|
-
result[result.length - 1] = {
|
|
6067
|
-
type: "text",
|
|
6068
|
-
text: lastText.text + firstText.text
|
|
6616
|
+
case "beforeAll": return {
|
|
6617
|
+
offset,
|
|
6618
|
+
zeroWidthBefore: 0
|
|
6069
6619
|
};
|
|
6070
|
-
|
|
6071
|
-
|
|
6072
|
-
|
|
6073
|
-
}
|
|
6620
|
+
case "insertion": return defaultInsertionGap(paragraph.content, offset);
|
|
6621
|
+
default: return fallback;
|
|
6622
|
+
}
|
|
6623
|
+
};
|
|
6074
6624
|
// src/ops/inline.ts
|
|
6075
6625
|
/**
|
|
6076
6626
|
* Edits over a paragraph's content in its logical offset space.
|
|
@@ -7082,6 +7632,45 @@ const applyPackageResourcesOp = (document, op) => {
|
|
|
7082
7632
|
}
|
|
7083
7633
|
});
|
|
7084
7634
|
};
|
|
7635
|
+
// src/ops/paragraphSnapshotTransfers.ts
|
|
7636
|
+
const paragraphsOf = (blocks) => storyParagraphs({ content: [...blocks] }).map(({ paragraph }) => paragraph);
|
|
7637
|
+
const transfersBetween = (previous, next, modified, transfers) => {
|
|
7638
|
+
const originals = new Map(previous.flatMap((paragraph) => paragraph.paraId !== void 0 && modified.has(idKey(paragraph.paraId)) ? [[idKey(paragraph.paraId), paragraph]] : []));
|
|
7639
|
+
if (originals.size === 0) return;
|
|
7640
|
+
for (const paragraph of next) {
|
|
7641
|
+
if (paragraph.paraId === void 0) continue;
|
|
7642
|
+
const original = originals.get(idKey(paragraph.paraId));
|
|
7643
|
+
if (original === void 0 || original === paragraph || !structurallyEqual(original.formatting, paragraph.formatting)) continue;
|
|
7644
|
+
transfers.push({
|
|
7645
|
+
source: original,
|
|
7646
|
+
target: paragraph
|
|
7647
|
+
});
|
|
7648
|
+
}
|
|
7649
|
+
};
|
|
7650
|
+
/**
|
|
7651
|
+
* The transfers an edit makes, read from the top-level blocks of each story
|
|
7652
|
+
* it changed that are not the very blocks `before` held at the same place
|
|
7653
|
+
* from either end. A paragraph of a kept block is its own original, and the
|
|
7654
|
+
* seed contract keeps paragraph ids unique, so no other paragraph is one.
|
|
7655
|
+
*/
|
|
7656
|
+
const paragraphSnapshotTransfers = ({ before, edit }) => {
|
|
7657
|
+
const transfers = [];
|
|
7658
|
+
if (edit.touched.modified.length === 0) return transfers;
|
|
7659
|
+
const modified = new Set(edit.touched.modified.map(idKey));
|
|
7660
|
+
for (const story of documentStories(edit.document)) {
|
|
7661
|
+
const previous = findStoryBody(before, story);
|
|
7662
|
+
const next = storyBody(edit.document, story);
|
|
7663
|
+
if (previous === void 0 || previous === next) continue;
|
|
7664
|
+
const old = previous.content;
|
|
7665
|
+
const now = next.content;
|
|
7666
|
+
let start = 0;
|
|
7667
|
+
while (start < old.length && start < now.length && old[start] === now[start]) start += 1;
|
|
7668
|
+
let after = 0;
|
|
7669
|
+
while (after < old.length - start && after < now.length - start && old[old.length - 1 - after] === now[now.length - 1 - after]) after += 1;
|
|
7670
|
+
transfersBetween(paragraphsOf(old.slice(start, old.length - after)), paragraphsOf(now.slice(start, now.length - after)), modified, transfers);
|
|
7671
|
+
}
|
|
7672
|
+
return transfers;
|
|
7673
|
+
};
|
|
7085
7674
|
// src/model/tableCellVerticalMerge.ts
|
|
7086
7675
|
const TABLE_CELL_VERTICAL_MERGES = {
|
|
7087
7676
|
NONE: "none",
|
|
@@ -7672,25 +8261,6 @@ const precedingSectionOwner = (previous, paragraph) => {
|
|
|
7672
8261
|
if (previous.sectionProperties === void 0) return "available";
|
|
7673
8262
|
return JSON.stringify(previous.sectionProperties) === JSON.stringify(paragraph.sectionProperties) ? "same" : "occupied";
|
|
7674
8263
|
};
|
|
7675
|
-
const paragraphTableRunInTarget = (paragraph, table) => {
|
|
7676
|
-
if (paragraph.sectionProperties !== void 0) return void 0;
|
|
7677
|
-
const tables = [];
|
|
7678
|
-
let current = table;
|
|
7679
|
-
for (;;) {
|
|
7680
|
-
const row = current.rows.at(0);
|
|
7681
|
-
const cell = row?.cells.at(0);
|
|
7682
|
-
if (!row || !cell) return void 0;
|
|
7683
|
-
if (row.structuralChange?.type === "tableRowInsertion" || row.structuralChange?.type === "tableRowDeletion" || cell.structuralChange !== void 0) return;
|
|
7684
|
-
tables.push(current);
|
|
7685
|
-
const target = cell.content.at(0);
|
|
7686
|
-
if (target?.type === "paragraph") return target.sectionProperties === void 0 ? {
|
|
7687
|
-
receiver: target,
|
|
7688
|
-
tables
|
|
7689
|
-
} : void 0;
|
|
7690
|
-
if (target?.type !== "table") return void 0;
|
|
7691
|
-
current = target;
|
|
7692
|
-
}
|
|
7693
|
-
};
|
|
7694
8264
|
const runParagraphIntoTable = (paragraph, target) => {
|
|
7695
8265
|
let replacement = {
|
|
7696
8266
|
...target.receiver,
|
|
@@ -7733,6 +8303,27 @@ const removedNoteReferences = (document, op, ids) => {
|
|
|
7733
8303
|
return [...references.values()];
|
|
7734
8304
|
};
|
|
7735
8305
|
const hasNoteReference = (document, reference) => documentStories(document).some((story) => storyParagraphs(storyBody(document, story)).some(({ paragraph }) => leafSpans(paragraph.content).some(({ node }) => (node.type === "footnoteRef" || node.type === "endnoteRef") && node.type === (reference.kind === "footnote" ? "footnoteRef" : "endnoteRef") && node.id === reference.id)));
|
|
8306
|
+
/**
|
|
8307
|
+
* The paragraph a rejected mark that keeps the first paragraph leaves: the
|
|
8308
|
+
* first paragraph's identity, and everything the mark that stays carries,
|
|
8309
|
+
* which is the next paragraph's. Where that mark is the first paragraph's
|
|
8310
|
+
* former one, the first paragraph comes back as it was once the reviews
|
|
8311
|
+
* pending on it resolve; where it is another break the same edit inserted,
|
|
8312
|
+
* that break says itself which paragraph its rejection keeps.
|
|
8313
|
+
*/
|
|
8314
|
+
const joinedKeepingFirst = (paragraph, next, content, receivesSection) => {
|
|
8315
|
+
const { paraId: _paraId, textId: _textId, preservedAttributes: _preservedAttributes, renderedPageBreakBefore: _renderedPageBreakBefore, sectionProperties: _sectionProperties, ...marked } = next;
|
|
8316
|
+
const sectionProperties = receivesSection ? paragraph.sectionProperties : next.sectionProperties;
|
|
8317
|
+
return {
|
|
8318
|
+
...marked,
|
|
8319
|
+
content,
|
|
8320
|
+
...paragraph.paraId === void 0 ? {} : { paraId: paragraph.paraId },
|
|
8321
|
+
...paragraph.textId === void 0 ? {} : { textId: paragraph.textId },
|
|
8322
|
+
...paragraph.preservedAttributes === void 0 ? {} : { preservedAttributes: paragraph.preservedAttributes },
|
|
8323
|
+
...paragraph.renderedPageBreakBefore === void 0 ? {} : { renderedPageBreakBefore: paragraph.renderedPageBreakBefore },
|
|
8324
|
+
...sectionProperties === void 0 ? {} : { sectionProperties }
|
|
8325
|
+
};
|
|
8326
|
+
};
|
|
7736
8327
|
/** Whether a mark of this kind recorded a break being added. */
|
|
7737
8328
|
const markWasAdded = (kind) => kind === "ins" || kind === "moveTo";
|
|
7738
8329
|
const removePropertyChanges = (changes, remove) => {
|
|
@@ -7909,6 +8500,7 @@ const hasSlots = (node, expected) => {
|
|
|
7909
8500
|
};
|
|
7910
8501
|
const restoreRetainedIdentities = ({ nodes, transfers, resolution }) => {
|
|
7911
8502
|
const references = /* @__PURE__ */ new Map();
|
|
8503
|
+
const unmatched = [];
|
|
7912
8504
|
let out = [...nodes];
|
|
7913
8505
|
for (const transfer of transfers) {
|
|
7914
8506
|
const source = transfer.source.map((slot) => references.get(slotKey(slot)) ?? slot);
|
|
@@ -7925,7 +8517,13 @@ const restoreRetainedIdentities = ({ nodes, transfers, resolution }) => {
|
|
|
7925
8517
|
return next;
|
|
7926
8518
|
};
|
|
7927
8519
|
out = out.map(restore);
|
|
7928
|
-
if (!matched)
|
|
8520
|
+
if (!matched) {
|
|
8521
|
+
unmatched.push({
|
|
8522
|
+
...transfer,
|
|
8523
|
+
source
|
|
8524
|
+
});
|
|
8525
|
+
continue;
|
|
8526
|
+
}
|
|
7929
8527
|
for (const [index, target] of transfer.target.entries()) {
|
|
7930
8528
|
const restored = source.at(index);
|
|
7931
8529
|
if (restored === void 0 || restored.space !== target.space) panic("Validated identity transfers retain slot order and space.");
|
|
@@ -7942,6 +8540,11 @@ const restoreRetainedIdentities = ({ nodes, transfers, resolution }) => {
|
|
|
7942
8540
|
source: entry.source.map((slot) => references.get(slotKey(slot)) ?? slot),
|
|
7943
8541
|
target: entry.target.map((slot) => references.get(slotKey(slot)) ?? slot)
|
|
7944
8542
|
}));
|
|
8543
|
+
if (joins.retainedEnclosing !== void 0) joins.retainedEnclosing = joins.retainedEnclosing.map((entry) => ({
|
|
8544
|
+
levels: entry.levels,
|
|
8545
|
+
source: entry.source.map((slot) => references.get(slotKey(slot)) ?? slot),
|
|
8546
|
+
target: entry.target.map((slot) => references.get(slotKey(slot)) ?? slot)
|
|
8547
|
+
}));
|
|
7945
8548
|
if (joins.deferredRemove !== void 0) joins.deferredRemove = canonicalDeferredGroups(joins.deferredRemove.map((group) => ({
|
|
7946
8549
|
depth: group.depth,
|
|
7947
8550
|
blockers: [...new Set(group.blockers.map((id) => {
|
|
@@ -7957,8 +8560,40 @@ const restoreRetainedIdentities = ({ nodes, transfers, resolution }) => {
|
|
|
7957
8560
|
const children = childNodes(next);
|
|
7958
8561
|
return children === void 0 ? next : rebuildNode(next, children.map(rebase));
|
|
7959
8562
|
};
|
|
7960
|
-
return
|
|
8563
|
+
return {
|
|
8564
|
+
nodes: out.map(rebase),
|
|
8565
|
+
unmatched
|
|
8566
|
+
};
|
|
7961
8567
|
};
|
|
8568
|
+
/**
|
|
8569
|
+
* Hand outstanding identities to the pieces they name among `nodes`, content
|
|
8570
|
+
* that follows the removed prefixes in the story. A target id names one
|
|
8571
|
+
* record, so the piece is found wherever the replacement left it.
|
|
8572
|
+
*/
|
|
8573
|
+
const restoreOutstandingIdentities = (nodes, resolution) => {
|
|
8574
|
+
if (resolution.outstanding.length === 0) return nodes;
|
|
8575
|
+
const restored = restoreRetainedIdentities({
|
|
8576
|
+
nodes,
|
|
8577
|
+
transfers: resolution.outstanding,
|
|
8578
|
+
resolution
|
|
8579
|
+
});
|
|
8580
|
+
resolution.outstanding = restored.unmatched;
|
|
8581
|
+
return restored.nodes;
|
|
8582
|
+
};
|
|
8583
|
+
const isIdentitySlot = (slot) => {
|
|
8584
|
+
if (typeof slot !== "object" || slot === null || !("space" in slot) || !("id" in slot)) return false;
|
|
8585
|
+
const retainedId = slot.id;
|
|
8586
|
+
return (slot.space === IDENTITY_SPACES.REVISION || slot.space === IDENTITY_SPACES.CONTROL) && typeof retainedId === "number" && Number.isInteger(retainedId) && retainedId >= 0 && retainedId <= 2147483647;
|
|
8587
|
+
};
|
|
8588
|
+
/**
|
|
8589
|
+
* Handoffs to records above a wrapper name paired slots one level up or more;
|
|
8590
|
+
* whether that record holds them is checked where it is resolved.
|
|
8591
|
+
*/
|
|
8592
|
+
const validEnclosingHandoffs = (handoffs) => handoffs === void 0 || Array.isArray(handoffs) && handoffs.every((entry) => {
|
|
8593
|
+
if (typeof entry !== "object" || entry === null || !("levels" in entry) || !("source" in entry) || !("target" in entry)) return false;
|
|
8594
|
+
const { levels, source, target } = entry;
|
|
8595
|
+
return typeof levels === "number" && Number.isInteger(levels) && levels >= 1 && levels <= 2147483647 && Array.isArray(source) && Array.isArray(target) && source.length > 0 && source.length === target.length && source.every(isIdentitySlot) && target.every(isIdentitySlot) && new Set(source.map(slotKey)).size === source.length && new Set(target.map(slotKey)).size === target.length && source.every((slot, index) => slot.space === target.at(index)?.space);
|
|
8596
|
+
});
|
|
7962
8597
|
/** Journal-supplied provenance must name actual source slots at its recorded depth. */
|
|
7963
8598
|
const validRetainedIdentities = (node) => {
|
|
7964
8599
|
if (!isTrackedWrapper(node) || node.resolutionJoins === void 0) return true;
|
|
@@ -7979,6 +8614,7 @@ const validRetainedIdentities = (node) => {
|
|
|
7979
8614
|
groupKeys.add(key);
|
|
7980
8615
|
}
|
|
7981
8616
|
}
|
|
8617
|
+
if (!validEnclosingHandoffs(joins.retainedEnclosing)) return false;
|
|
7982
8618
|
if (joins.retainedAfter === void 0) return true;
|
|
7983
8619
|
if (!Array.isArray(joins.retainedAfter)) return false;
|
|
7984
8620
|
const atDepth = ({ records, depth, source }) => {
|
|
@@ -8189,6 +8825,7 @@ const resolveList = (nodes, resolution) => {
|
|
|
8189
8825
|
/** Boundaries kept from removed content just before a seam, and its depth. */
|
|
8190
8826
|
const floating = /* @__PURE__ */ new Map();
|
|
8191
8827
|
let changed = false;
|
|
8828
|
+
const enclosing = [];
|
|
8192
8829
|
for (const [index, node] of pending.entries()) {
|
|
8193
8830
|
if (isTrackedWrapper(node) && resolution.ids.has(node.info.id)) {
|
|
8194
8831
|
changed = true;
|
|
@@ -8224,7 +8861,8 @@ const resolveList = (nodes, resolution) => {
|
|
|
8224
8861
|
transfers,
|
|
8225
8862
|
resolution
|
|
8226
8863
|
});
|
|
8227
|
-
|
|
8864
|
+
resolution.outstanding.push(...restored.unmatched);
|
|
8865
|
+
const prepared = prepareDeferredRemovals(restored.nodes, resolution);
|
|
8228
8866
|
out.splice(0, out.length, ...prepared.slice(0, prefixLength).map((next, prefixIndex) => {
|
|
8229
8867
|
const previous = out.at(prefixIndex) ?? panic("Restoring source identities preserves every output occurrence.");
|
|
8230
8868
|
const restoredOccurrence = {
|
|
@@ -8236,6 +8874,14 @@ const resolveList = (nodes, resolution) => {
|
|
|
8236
8874
|
}));
|
|
8237
8875
|
pending.splice(index + 1, pending.length - index - 1, ...prepared.slice(prefixLength));
|
|
8238
8876
|
}
|
|
8877
|
+
for (const entry of node.resolutionJoins?.retainedEnclosing ?? []) enclosing.push({
|
|
8878
|
+
levels: entry.levels,
|
|
8879
|
+
transfer: {
|
|
8880
|
+
depth: 0,
|
|
8881
|
+
source: entry.source,
|
|
8882
|
+
target: entry.target
|
|
8883
|
+
}
|
|
8884
|
+
});
|
|
8239
8885
|
const removalContent = resolution.decision === REVISION_DECISIONS.REJECT && isAddedRevision(node) ? rejectCopyOriginAncestorsInNodes(node.content, 1) : node.content;
|
|
8240
8886
|
const kept = removedContentBoundaries({
|
|
8241
8887
|
nodes: removalContent,
|
|
@@ -8296,6 +8942,20 @@ const resolveList = (nodes, resolution) => {
|
|
|
8296
8942
|
}
|
|
8297
8943
|
const children = childNodes(node);
|
|
8298
8944
|
const inner = children === void 0 ? void 0 : resolveList(children, resolution);
|
|
8945
|
+
for (const handoff of inner?.enclosing ?? []) {
|
|
8946
|
+
if (handoff.levels > 1) {
|
|
8947
|
+
enclosing.push({
|
|
8948
|
+
levels: handoff.levels - 1,
|
|
8949
|
+
transfer: handoff.transfer
|
|
8950
|
+
});
|
|
8951
|
+
continue;
|
|
8952
|
+
}
|
|
8953
|
+
if (inner !== void 0 && inner.nodes.length === 0 && (children?.length ?? 0) > 0 && hasSlots(node, handoff.transfer.source)) resolution.outstanding.push(handoff.transfer);
|
|
8954
|
+
}
|
|
8955
|
+
if (resolution.outstanding.length > 0) {
|
|
8956
|
+
const following = restoreOutstandingIdentities(pending.slice(index + 1), resolution);
|
|
8957
|
+
pending.splice(index + 1, pending.length - index - 1, ...prepareDeferredRemovals(following, resolution));
|
|
8958
|
+
}
|
|
8299
8959
|
if (children === void 0 || inner === void 0 || !inner.changed) {
|
|
8300
8960
|
appendSource(node, index);
|
|
8301
8961
|
continue;
|
|
@@ -8303,6 +8963,7 @@ const resolveList = (nodes, resolution) => {
|
|
|
8303
8963
|
changed = true;
|
|
8304
8964
|
if ((isTrackedWrapper(node) || resolution.decision === REVISION_DECISIONS.ACCEPT) && inner.nodes.length === 0 && children.length > 0) {
|
|
8305
8965
|
folds.push(out.length);
|
|
8966
|
+
if (isTrackedWrapper(node) && (node.resolutionJoins?.plainSides?.length ?? 0) > 0) plainSeams.add(out.length);
|
|
8306
8967
|
continue;
|
|
8307
8968
|
}
|
|
8308
8969
|
let rebuilt = rebuildNode(node, inner.nodes);
|
|
@@ -8368,12 +9029,16 @@ const resolveList = (nodes, resolution) => {
|
|
|
8368
9029
|
const lastDepth = sourceEdges.get(out.length);
|
|
8369
9030
|
if (firstDepth !== void 0) edges.first = firstDepth;
|
|
8370
9031
|
if (lastDepth !== void 0) edges.last = lastDepth;
|
|
8371
|
-
const pendingRemoval = (node) => node !== void 0 && (node.type === "deletion" || node.type === "moveFrom") && !resolution.ids.has(node.info.id) && node.resolutionJoins !== void 0;
|
|
9032
|
+
const pendingRemoval = (node) => node !== void 0 && ((node.type === "deletion" || node.type === "moveFrom") && !resolution.ids.has(node.info.id) && node.resolutionJoins !== void 0 || isTrackedWrapper(node) && !resolution.ids.has(node.info.id) && node.content.length > 0 && node.content.every((child) => pendingRemoval(child)));
|
|
8372
9033
|
const acceptAsPlain = (index, facing) => {
|
|
8373
9034
|
const occurrence = out[index];
|
|
8374
9035
|
const node = occurrence?.node;
|
|
8375
|
-
if (occurrence === void 0 || node === void 0 || !isTrackedWrapper(node)
|
|
8376
|
-
out[index] = occurrenceOf(Object.assign({}, node, { resolutionJoins: Object.assign({
|
|
9036
|
+
if (occurrence === void 0 || node === void 0 || !isTrackedWrapper(node)) return;
|
|
9037
|
+
out[index] = occurrenceOf(Object.assign({}, node, { resolutionJoins: Object.assign({
|
|
9038
|
+
before: 0,
|
|
9039
|
+
after: 0,
|
|
9040
|
+
remove: 0
|
|
9041
|
+
}, node.resolutionJoins, { plainSides: [.../* @__PURE__ */ new Set([...node.resolutionJoins?.plainSides ?? [], facing])] }) }), occurrence);
|
|
8377
9042
|
changed = true;
|
|
8378
9043
|
};
|
|
8379
9044
|
for (const seam of plainSeams) {
|
|
@@ -8520,7 +9185,8 @@ const resolveList = (nodes, resolution) => {
|
|
|
8520
9185
|
return {
|
|
8521
9186
|
nodes: out.map(({ node }) => node),
|
|
8522
9187
|
changed,
|
|
8523
|
-
edges
|
|
9188
|
+
edges,
|
|
9189
|
+
...enclosing.length === 0 ? {} : { enclosing }
|
|
8524
9190
|
};
|
|
8525
9191
|
};
|
|
8526
9192
|
/** A paragraph's review fields once its property changes and mark are resolved; `undefined` when unchanged. */
|
|
@@ -8659,7 +9325,7 @@ const paragraphMarkPlan = (ops, receiverEffects = []) => Result.ok({
|
|
|
8659
9325
|
ops,
|
|
8660
9326
|
receiverEffects
|
|
8661
9327
|
});
|
|
8662
|
-
const joinOps = (document, { op, story, paraId, added, depth }, ends, ranInto = /* @__PURE__ */ new Set()) => {
|
|
9328
|
+
const joinOps = (document, { op, story, paraId, added, depth }, ends, ranInto = /* @__PURE__ */ new Set(), joinsBefore = /* @__PURE__ */ new Set()) => {
|
|
8663
9329
|
const body = storyBody(document, story);
|
|
8664
9330
|
const location = storyParagraphs(body).find(({ paragraph }) => idKey(paragraph.paraId ?? "") === idKey(paraId));
|
|
8665
9331
|
if (location === void 0) return Result.err(refusal$2(op, DOCUMENT_OP_REFUSAL_REASONS.BLOCK_NOT_FOUND, `No paragraph is ${paraId}.`));
|
|
@@ -8729,15 +9395,12 @@ const joinOps = (document, { op, story, paraId, added, depth }, ends, ranInto =
|
|
|
8729
9395
|
];
|
|
8730
9396
|
} else merged = [...seam.left, ...seam.right];
|
|
8731
9397
|
}
|
|
8732
|
-
|
|
9398
|
+
const keepsFirst = paragraph.pPrMark?.survivor === "first";
|
|
9399
|
+
ends.set(keepsFirst ? idKey(paraId) : nextId, emptiedEndsOf(merged, emptied));
|
|
8733
9400
|
const mergedContent = asParagraphContent(merged);
|
|
8734
9401
|
const receivesSection = outcome.sectionDestination === "following";
|
|
8735
|
-
const { pPrMark: _rejectedMark, ...first } = paragraph;
|
|
8736
9402
|
let survivor;
|
|
8737
|
-
if (
|
|
8738
|
-
...first,
|
|
8739
|
-
content: mergedContent
|
|
8740
|
-
};
|
|
9403
|
+
if (keepsFirst) survivor = joinedKeepingFirst(paragraph, next, mergedContent, receivesSection);
|
|
8741
9404
|
else if (!receivesSection && structurallyEqual(mergedContent, next.content)) survivor = next;
|
|
8742
9405
|
else survivor = {
|
|
8743
9406
|
...next,
|
|
@@ -8773,25 +9436,47 @@ const joinOps = (document, { op, story, paraId, added, depth }, ends, ranInto =
|
|
|
8773
9436
|
}], receiverEffects);
|
|
8774
9437
|
}
|
|
8775
9438
|
if (next !== void 0 && isBlockBoundary(next)) return Result.err(refusal$2(op, DOCUMENT_OP_REFUSAL_REASONS.UNTRACKABLE, `A range boundary stands between ${paraId} and the next block.`));
|
|
9439
|
+
const followsBlock = isOpaqueFollower(next);
|
|
8776
9440
|
const containerFinal = endsItsContainer(body, location);
|
|
8777
9441
|
const cellFinal = containerFinal && location.list.some((step) => step.kind === "tableCell");
|
|
8778
9442
|
const tableTarget = next?.type === "table" ? paragraphTableRunInTarget(paragraph, next) : void 0;
|
|
9443
|
+
const blockTarget = next !== void 0 && followsBlock ? paragraphBlockRunInTarget(paragraph, next) : void 0;
|
|
9444
|
+
const runInReceiver = tableTarget?.receiver ?? blockTarget?.receiver;
|
|
8779
9445
|
const remains = paragraphRemains(paragraph.content);
|
|
9446
|
+
let follows = "nothing";
|
|
9447
|
+
if (next?.type === "table") follows = "table";
|
|
9448
|
+
else if (followsBlock) follows = "block";
|
|
9449
|
+
const joining = [];
|
|
9450
|
+
for (let index = location.index - 1; follows === "block"; index -= 1) {
|
|
9451
|
+
const before = blocks[index];
|
|
9452
|
+
if (before?.type !== "paragraph" || !joinsBefore.has(idKey(before.paraId ?? ""))) break;
|
|
9453
|
+
joining.unshift(before);
|
|
9454
|
+
}
|
|
9455
|
+
const chained = joining.length === 0 ? paragraph : {
|
|
9456
|
+
...paragraph,
|
|
9457
|
+
content: [...joining, paragraph].flatMap(({ content }) => content)
|
|
9458
|
+
};
|
|
8780
9459
|
const outcome = paragraphMarkOutcome({
|
|
8781
|
-
follows
|
|
8782
|
-
holdsContent: paragraphHoldsContent(
|
|
9460
|
+
follows,
|
|
9461
|
+
holdsContent: paragraphHoldsContent(chained),
|
|
8783
9462
|
markWasAdded: added,
|
|
8784
|
-
containerStillEndsWithParagraph:
|
|
9463
|
+
containerStillEndsWithParagraph: follows !== "nothing" ? blocks.at(-1)?.type === "paragraph" : containerFinal && !cellFinal && blocks.at(location.index - 1)?.type === "paragraph",
|
|
8785
9464
|
followingTableCanReceiveRunIn: tableTarget !== void 0,
|
|
9465
|
+
holdsContentOnceAccepted: paragraphHoldsContentOnceAccepted(chained),
|
|
9466
|
+
followingBlockCanReceiveRunIn: blockTarget !== void 0,
|
|
8786
9467
|
needsFollowingCommentHost: next?.type === "table" && previous?.type !== "paragraph" && remains.blocks.length === 0 && remains.comments.length > 0,
|
|
8787
|
-
tableRemovalIsTrackable: next?.type !== "table" ||
|
|
9468
|
+
tableRemovalIsTrackable: next?.type !== "table" || markBeforeTableResolves({
|
|
9469
|
+
blocks,
|
|
9470
|
+
paragraph,
|
|
9471
|
+
table: next
|
|
9472
|
+
}),
|
|
8788
9473
|
removedSectionProperties: added && paragraph.sectionProperties !== void 0,
|
|
8789
9474
|
precedingSectionOwner: precedingSectionOwner(previous, paragraph),
|
|
8790
9475
|
followingHasSectionProperties: false
|
|
8791
9476
|
});
|
|
8792
9477
|
if (outcome.kind === "refuse") return Result.err(refusal$2(op, outcome.reason, `Resolving the mark of ${paraId} would leave its container without a final paragraph.`));
|
|
8793
|
-
const tableOutcome = outcome.kind === "remove" &&
|
|
8794
|
-
if ((tableOutcome === "remove" || tableOutcome === "run-in") && blocks.length > 1 && next?.type === "table") {
|
|
9478
|
+
const tableOutcome = outcome.kind === "remove" && runInReceiver !== void 0 && paragraph.content.length > 0 && ranInto.has(idKey(runInReceiver.paraId ?? "")) ? "run-in" : outcome.kind;
|
|
9479
|
+
if ((tableOutcome === "remove" || tableOutcome === "run-in") && blocks.length > 1 && (next?.type === "table" || followsBlock)) {
|
|
8795
9480
|
const anchor = blocks.find((block, index) => index !== location.index && block.type === "paragraph");
|
|
8796
9481
|
if (anchor === void 0) return Result.err(refusal$2(op, DOCUMENT_OP_REFUSAL_REASONS.UNTRACKABLE, `Resolving the mark of ${paraId} leaves no structural inverse anchor.`));
|
|
8797
9482
|
if (tableOutcome === "remove") {
|
|
@@ -8807,6 +9492,26 @@ const joinOps = (document, { op, story, paraId, added, depth }, ends, ranInto =
|
|
|
8807
9492
|
expected: blocks,
|
|
8808
9493
|
blocks: removal.blocks
|
|
8809
9494
|
}], [...removal.effects]);
|
|
9495
|
+
} else if (blockTarget !== void 0) {
|
|
9496
|
+
const { receiver } = blockTarget;
|
|
9497
|
+
return paragraphMarkPlan([{
|
|
9498
|
+
type: DOCUMENT_OP_TYPES.SET_CONTAINER_BLOCKS,
|
|
9499
|
+
story,
|
|
9500
|
+
blockId: anchor.paraId ?? "",
|
|
9501
|
+
expected: blocks,
|
|
9502
|
+
blocks: blocks.filter((_, index) => index !== location.index)
|
|
9503
|
+
}, {
|
|
9504
|
+
type: DOCUMENT_OP_TYPES.REPLACE_BLOCKS,
|
|
9505
|
+
story,
|
|
9506
|
+
expected: [receiver],
|
|
9507
|
+
blocks: [{
|
|
9508
|
+
...receiver,
|
|
9509
|
+
content: joinParagraphSeam(paragraph.content, receiver.content)
|
|
9510
|
+
}]
|
|
9511
|
+
}], [{
|
|
9512
|
+
type: "block-run-in",
|
|
9513
|
+
receiver
|
|
9514
|
+
}]);
|
|
8810
9515
|
} else {
|
|
8811
9516
|
if (tableTarget === void 0) panic("An eligible table run-in has a validated receiver.");
|
|
8812
9517
|
const replacement = runParagraphIntoTable(paragraph, tableTarget);
|
|
@@ -8930,7 +9635,8 @@ const planRevisionResolution = ({ document, op, applyOps }) => {
|
|
|
8930
9635
|
},
|
|
8931
9636
|
cutEdges: /* @__PURE__ */ new WeakMap(),
|
|
8932
9637
|
pairedComments: /* @__PURE__ */ new Set(),
|
|
8933
|
-
relocated: noRelocatedBoundaries()
|
|
9638
|
+
relocated: noRelocatedBoundaries(),
|
|
9639
|
+
outstanding: []
|
|
8934
9640
|
};
|
|
8935
9641
|
const noteReferences = removedNoteReferences(document, op, ids);
|
|
8936
9642
|
const paragraphs = storyParagraphs(storyBody(document, op.story));
|
|
@@ -8963,8 +9669,10 @@ const planRevisionResolution = ({ document, op, applyOps }) => {
|
|
|
8963
9669
|
resolution.pairedComments = pairedCommentRanges(preparedContents.flatMap(({ content }) => content));
|
|
8964
9670
|
const removedCommentIds = removedCommentRangeIds(preparedContents.flatMap(({ content }) => content), resolution);
|
|
8965
9671
|
const resolvedChanged = /* @__PURE__ */ new Set();
|
|
8966
|
-
for (const { paragraph, content:
|
|
9672
|
+
for (const { paragraph, content: source } of preparedContents) {
|
|
8967
9673
|
const paraId = paragraph.paraId ?? "";
|
|
9674
|
+
const handed = restoreOutstandingIdentities(source, resolution);
|
|
9675
|
+
const prepared = handed === source ? source : prepareDeferredRemovals(handed, resolution);
|
|
8968
9676
|
const resolved = resolveList(prepared, resolution);
|
|
8969
9677
|
resolvedContents.set(idKey(paraId), withoutCommentAnchors(resolved.nodes, removedCommentIds));
|
|
8970
9678
|
if (resolved.edges !== void 0) resolvedCutEdges.set(idKey(paraId), resolved.edges);
|
|
@@ -9081,12 +9789,14 @@ const planRevisionResolution = ({ document, op, applyOps }) => {
|
|
|
9081
9789
|
const edits = [staged.value, rows.value];
|
|
9082
9790
|
let current = rows.value.document;
|
|
9083
9791
|
const ranInto = /* @__PURE__ */ new Set();
|
|
9792
|
+
const joinsBefore = new Set(joins.map(({ paraId }) => idKey(paraId)));
|
|
9084
9793
|
for (const join of joins.toReversed()) {
|
|
9794
|
+
joinsBefore.delete(idKey(join.paraId));
|
|
9085
9795
|
if (removedParagraphs.has(idKey(join.paraId))) continue;
|
|
9086
|
-
const planned = joinOps(current, join, emptiedEnds, ranInto);
|
|
9796
|
+
const planned = joinOps(current, join, emptiedEnds, ranInto, joinsBefore);
|
|
9087
9797
|
if (planned.isErr()) return Result.err(planned.error);
|
|
9088
9798
|
receiverEffects.push(...planned.value.receiverEffects);
|
|
9089
|
-
for (const effect of planned.value.receiverEffects) if (effect.type === "table-run-in") ranInto.add(idKey(effect.receiver.paraId ?? ""));
|
|
9799
|
+
for (const effect of planned.value.receiverEffects) if (effect.type === "table-run-in" || effect.type === "block-run-in") ranInto.add(idKey(effect.receiver.paraId ?? ""));
|
|
9090
9800
|
const joined = applyOps(current, planned.value.ops);
|
|
9091
9801
|
if (joined.isErr()) return Result.err(joined.error);
|
|
9092
9802
|
edits.push(joined.value);
|
|
@@ -11265,7 +11975,7 @@ const stampRefusal = (document, op, stamp) => {
|
|
|
11265
11975
|
space: IDENTITY_SPACES.REVISION,
|
|
11266
11976
|
id: stamp.id
|
|
11267
11977
|
});
|
|
11268
|
-
if (
|
|
11978
|
+
if (packageIdentityKeyIndex(document.package).has(key)) return refusal$1(op, DOCUMENT_OP_REFUSAL_REASONS.ID_COLLISION, `Revision id ${stamp.id} is already used in the package.`);
|
|
11269
11979
|
};
|
|
11270
11980
|
/** The replacement that gives a paragraph back the content it had. */
|
|
11271
11981
|
const contentRestoring = (story, result, original) => ({
|
|
@@ -11361,9 +12071,9 @@ const touchedBetween = (before, after) => {
|
|
|
11361
12071
|
};
|
|
11362
12072
|
/** Revision and control ids the package uses outside `before`. */
|
|
11363
12073
|
const identitiesOutside = (document, before) => {
|
|
11364
|
-
const
|
|
11365
|
-
|
|
11366
|
-
return
|
|
12074
|
+
const index = packageIdentityKeyIndex(document.package);
|
|
12075
|
+
const inside = countKeys(identityKeysIn(before));
|
|
12076
|
+
return { has: (key) => index.count(key) > (inside.get(key) ?? 0) };
|
|
11367
12077
|
};
|
|
11368
12078
|
/** `after` with the records the edit would leave sharing an id given the operation's new ids. */
|
|
11369
12079
|
const withFreshIds = ({ document, op, before, after, newIds, inserted }) => {
|
|
@@ -11372,7 +12082,7 @@ const withFreshIds = ({ document, op, before, after, newIds, inserted }) => {
|
|
|
11372
12082
|
after,
|
|
11373
12083
|
newIds: newIds ?? {},
|
|
11374
12084
|
usedElsewhere: () => identitiesOutside(document, before),
|
|
11375
|
-
reserved:
|
|
12085
|
+
reserved: packageReservedKeyIndex(document.package),
|
|
11376
12086
|
...inserted === void 0 ? {} : { inserted }
|
|
11377
12087
|
});
|
|
11378
12088
|
switch (freshened.kind) {
|
|
@@ -11977,7 +12687,8 @@ const trackSplit = ({ op, stamp, paragraph, first, made, madeSecond, resolutionJ
|
|
|
11977
12687
|
first.pPrMark = {
|
|
11978
12688
|
kind: "ins",
|
|
11979
12689
|
info: stampInfo(stamp),
|
|
11980
|
-
resolutionJoin
|
|
12690
|
+
resolutionJoin,
|
|
12691
|
+
...madeSecond ? { survivor: "first" } : {}
|
|
11981
12692
|
};
|
|
11982
12693
|
if (!sameParagraphProperties(made.formatting, paragraph.formatting)) made.propertyChanges = [madeSecond ? foldedParagraphPropertyChange({
|
|
11983
12694
|
paragraph,
|
|
@@ -12329,10 +13040,10 @@ const setParagraphReview = (document, op) => {
|
|
|
12329
13040
|
const next = withReviewFields(paragraph, review);
|
|
12330
13041
|
if (structurallyEqual(next, paragraph)) return unchanged(document);
|
|
12331
13042
|
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$2(op, DOCUMENT_OP_REFUSAL_REASONS.CONTAINER_FINAL_MARK, `${op.blockId} ends its container: there is no next paragraph its mark could join.`);
|
|
12332
|
-
const
|
|
12333
|
-
|
|
13043
|
+
const outside = identitiesOutside(document, [paragraph]);
|
|
13044
|
+
const own = new Set(identityKeysIn(paragraph.content));
|
|
12334
13045
|
const incoming = reviewKeysOf(review);
|
|
12335
|
-
if (incoming.some((key) =>
|
|
13046
|
+
if (incoming.some((key) => outside.has(key) || own.has(key)) || new Set(incoming).size !== incoming.length) return refuse$2(op, DOCUMENT_OP_REFUSAL_REASONS.ID_COLLISION, "The review fields carry a revision id already used in the package.");
|
|
12336
13047
|
const replaced = replaceParagraphs({
|
|
12337
13048
|
document,
|
|
12338
13049
|
story: op.story,
|
|
@@ -12364,10 +13075,10 @@ const replaceInline = (document, op) => {
|
|
|
12364
13075
|
const remaining = countIds(packageParagraphIds(document.package));
|
|
12365
13076
|
for (const id of paragraphIdsIn(paragraph.content)) remaining.set(idKey(id), (remaining.get(idKey(id)) ?? 1) - 1);
|
|
12366
13077
|
if (collides(remaining, paragraphIdsIn(content))) return refuse$2(op, DOCUMENT_OP_REFUSAL_REASONS.ID_COLLISION, "The content holds a paragraph id the package already uses.");
|
|
12367
|
-
const
|
|
12368
|
-
|
|
13078
|
+
const outside = identitiesOutside(document, [paragraph]);
|
|
13079
|
+
const own = new Set(reviewKeysOf(reviewFieldsOf(paragraph)));
|
|
12369
13080
|
const incoming = identityKeysIn(content);
|
|
12370
|
-
if (incoming.some((key) =>
|
|
13081
|
+
if (incoming.some((key) => outside.has(key) || own.has(key)) || new Set(incoming).size !== incoming.length) return refuse$2(op, DOCUMENT_OP_REFUSAL_REASONS.ID_COLLISION, "The content carries a revision or content-control id already used in the package.");
|
|
12371
13082
|
const next = withContent(paragraph, content);
|
|
12372
13083
|
const replaced = replaceParagraphs({
|
|
12373
13084
|
document,
|
|
@@ -12618,7 +13329,7 @@ const stampOf = (op) => {
|
|
|
12618
13329
|
const recordedRevisions = (before, edit, stamp) => {
|
|
12619
13330
|
const touched = new Set([...edit.touched.modified, ...edit.touched.inserted].map(idKey));
|
|
12620
13331
|
const paragraphs = documentStories(edit.document).flatMap((story) => storyParagraphs(storyBody(edit.document, story))).map(({ paragraph }) => paragraph).filter(({ paraId }) => paraId !== void 0 && touched.has(idKey(paraId)));
|
|
12621
|
-
const known =
|
|
13332
|
+
const known = packageIdentityKeyIndex(before.package);
|
|
12622
13333
|
const revisions = paragraphs.flatMap((paragraph) => stampedRevisionIds([paragraph], stamp, known));
|
|
12623
13334
|
revisions.unshift(...documentStories(edit.document).flatMap((story) => stampedTableRevisionIds({
|
|
12624
13335
|
blocks: storyBody(edit.document, story).content,
|
|
@@ -12628,25 +13339,11 @@ const recordedRevisions = (before, edit, stamp) => {
|
|
|
12628
13339
|
return revisions;
|
|
12629
13340
|
};
|
|
12630
13341
|
/** Content edits retain authored snapshots across immutable paragraph replacements. */
|
|
12631
|
-
const preserveParagraphPropertySnapshots = (
|
|
12632
|
-
|
|
12633
|
-
|
|
12634
|
-
|
|
12635
|
-
|
|
12636
|
-
const next = storyBody(edit.document, story);
|
|
12637
|
-
if (previous === void 0 || previous === next) continue;
|
|
12638
|
-
const originals = new Map(storyParagraphs(previous).flatMap(({ paragraph }) => paragraph.paraId !== void 0 && modified.has(idKey(paragraph.paraId)) ? [[idKey(paragraph.paraId), paragraph]] : []));
|
|
12639
|
-
if (originals.size === 0) continue;
|
|
12640
|
-
for (const { paragraph } of storyParagraphs(next)) {
|
|
12641
|
-
if (paragraph.paraId === void 0) continue;
|
|
12642
|
-
const original = originals.get(idKey(paragraph.paraId));
|
|
12643
|
-
if (original === void 0 || original === paragraph || !structurallyEqual(original.formatting, paragraph.formatting)) continue;
|
|
12644
|
-
transferParagraphPropertySnapshot({
|
|
12645
|
-
source: original,
|
|
12646
|
-
target: paragraph
|
|
12647
|
-
});
|
|
12648
|
-
}
|
|
12649
|
-
}
|
|
13342
|
+
const preserveParagraphPropertySnapshots = (options) => {
|
|
13343
|
+
for (const { source, target } of paragraphSnapshotTransfers(options)) transferParagraphPropertySnapshot({
|
|
13344
|
+
source,
|
|
13345
|
+
target
|
|
13346
|
+
});
|
|
12650
13347
|
};
|
|
12651
13348
|
/** Apply one operation to a document that meets the seed contract. */
|
|
12652
13349
|
const applyDocumentOp = (document, input) => {
|
|
@@ -12758,13 +13455,35 @@ const suggestedRevisionIdsIn = (value, out) => {
|
|
|
12758
13455
|
for (const child of Object.values(record)) suggestedRevisionIdsIn(child, out);
|
|
12759
13456
|
};
|
|
12760
13457
|
/**
|
|
13458
|
+
* The ids of the comments pending suggestions propose: every one, or those of
|
|
13459
|
+
* one suggestion. A reply is removed with the comment it answers.
|
|
13460
|
+
*/
|
|
13461
|
+
const suggestedCommentIds = (document, suggestionId) => (document.package.document.comments ?? []).flatMap((comment) => comment.suggestionId !== void 0 && (suggestionId === void 0 || comment.suggestionId === suggestionId) ? [comment.id] : []);
|
|
13462
|
+
/** Remove proposed comments with their ranges and replies. */
|
|
13463
|
+
const withoutSuggestedComments = (document, ids) => {
|
|
13464
|
+
let current = document;
|
|
13465
|
+
for (const id of ids) {
|
|
13466
|
+
const removed = applyDocumentOp(current, {
|
|
13467
|
+
type: DOCUMENT_OP_TYPES.DELETE_COMMENT,
|
|
13468
|
+
id,
|
|
13469
|
+
scope: "thread"
|
|
13470
|
+
});
|
|
13471
|
+
if (removed.isErr()) return Result.err(removed.error);
|
|
13472
|
+
current = removed.value.document;
|
|
13473
|
+
}
|
|
13474
|
+
return Result.ok(current);
|
|
13475
|
+
};
|
|
13476
|
+
/**
|
|
12761
13477
|
* The document as a save writes it: every pending suggestion rejected. A
|
|
12762
13478
|
* suggestion is a proposal the host holds, not a change in the file, so the
|
|
12763
|
-
* file reads as if it had never been made; ordinary tracked changes stay.
|
|
13479
|
+
* file reads as if it had never been made; ordinary tracked changes stay. A
|
|
13480
|
+
* comment a suggestion proposes is left out with it.
|
|
12764
13481
|
*/
|
|
12765
13482
|
const withoutPendingSuggestions = (document) => {
|
|
12766
|
-
|
|
12767
|
-
|
|
13483
|
+
const uncommented = withoutSuggestedComments(document, suggestedCommentIds(document));
|
|
13484
|
+
if (uncommented.isErr()) return uncommented;
|
|
13485
|
+
let current = uncommented.value;
|
|
13486
|
+
for (const story of documentStories(current)) {
|
|
12768
13487
|
const ids = /* @__PURE__ */ new Set();
|
|
12769
13488
|
suggestedRevisionIdsIn(storyBody(current, story), ids);
|
|
12770
13489
|
if (ids.size === 0) continue;
|
|
@@ -12799,9 +13518,13 @@ const suggestionRevisionInfos = (document, story, suggestionId) => {
|
|
|
12799
13518
|
visit(storyBody(document, story).content);
|
|
12800
13519
|
return [...out.values()];
|
|
12801
13520
|
};
|
|
12802
|
-
/**
|
|
13521
|
+
/**
|
|
13522
|
+
* The ids of every pending suggestion in a document, in every story, and of
|
|
13523
|
+
* every suggestion that proposes a comment.
|
|
13524
|
+
*/
|
|
12803
13525
|
const pendingSuggestionIdsIn = (document) => {
|
|
12804
13526
|
const out = /* @__PURE__ */ new Set();
|
|
13527
|
+
for (const comment of document.package.document.comments ?? []) if (comment.suggestionId !== void 0) out.add(comment.suggestionId);
|
|
12805
13528
|
const visit = (value) => {
|
|
12806
13529
|
if (typeof value !== "object" || value === null) return;
|
|
12807
13530
|
if (Array.isArray(value)) {
|
|
@@ -13298,7 +14021,7 @@ const trimAppliedNewIds = ({ before, op, applied, story }) => {
|
|
|
13298
14021
|
const paragraphs = storyParagraphs(storyBody(applied.document, story)).map(({ paragraph }) => paragraph).filter(({ paraId }) => paraId !== void 0 && touched.has(idKey(paraId)));
|
|
13299
14022
|
const identities = new Set(identityKeysIn(paragraphs));
|
|
13300
14023
|
const revisions = new Set(applied.revisions);
|
|
13301
|
-
const known =
|
|
14024
|
+
const known = packageIdentityKeyIndex(before.package);
|
|
13302
14025
|
return filterNewIds(op, (space, id) => {
|
|
13303
14026
|
const key = `${space}:${id}`;
|
|
13304
14027
|
return !known.has(key) && (identities.has(key) || space === IDENTITY_SPACES.REVISION && revisions.has(id));
|
|
@@ -13486,7 +14209,7 @@ const storyOf = (input) => {
|
|
|
13486
14209
|
/** Whether an operation, its unused ids trimmed, still names a new id. */
|
|
13487
14210
|
const takesNewIds = (op) => "newIds" in op && op.newIds !== void 0 && ((op.newIds.revision?.length ?? 0) > 0 || (op.newIds.control?.length ?? 0) > 0);
|
|
13488
14211
|
/** Shared allocation and simulation for all pieces of one planned edit. */
|
|
13489
|
-
const createTrackedPlan = ({ document, revision, newIds, countEveryPiece = false }) => {
|
|
14212
|
+
const createTrackedPlan = ({ document, revision, newIds, countEveryPiece = false, deletionRevision }) => {
|
|
13490
14213
|
const used = usedIds(document, IDENTITY_SPACES.REVISION);
|
|
13491
14214
|
const pool = [...new Set(newIds?.revision ?? [])].filter((id) => !used.has(id) && id !== revision.id);
|
|
13492
14215
|
let taken = 0;
|
|
@@ -13504,15 +14227,16 @@ const createTrackedPlan = ({ document, revision, newIds, countEveryPiece = false
|
|
|
13504
14227
|
let op = input;
|
|
13505
14228
|
if (input.type !== DOCUMENT_OP_TYPES.REPLACE_INLINE && input.revision !== void 0) {
|
|
13506
14229
|
let stampId = revision.id;
|
|
14230
|
+
let stamp = revision;
|
|
13507
14231
|
if (stampUsed) {
|
|
13508
14232
|
if (remaining <= taken) return outOfIds();
|
|
13509
14233
|
remaining -= 1;
|
|
13510
14234
|
stampId = pool.at(remaining) ?? panic("A reserved stamp has an allocated pool entry.");
|
|
13511
|
-
}
|
|
14235
|
+
} else if (input.type === DOCUMENT_OP_TYPES.DELETE_RANGE && deletionRevision !== void 0) stamp = deletionRevision;
|
|
13512
14236
|
op = {
|
|
13513
14237
|
...input,
|
|
13514
14238
|
revision: {
|
|
13515
|
-
...
|
|
14239
|
+
...stamp,
|
|
13516
14240
|
id: stampId
|
|
13517
14241
|
}
|
|
13518
14242
|
};
|
|
@@ -14048,20 +14772,42 @@ const compileHyperlinkIntent = (document, { intent: sourceIntent, mode, compileE
|
|
|
14048
14772
|
};
|
|
14049
14773
|
// src/ops/rangeReplacement.ts
|
|
14050
14774
|
/** Tracked replacement fragments share deletion's validation and physical-id pool. */
|
|
14051
|
-
/**
|
|
14052
|
-
|
|
14053
|
-
|
|
14054
|
-
|
|
14055
|
-
|
|
14056
|
-
|
|
14057
|
-
|
|
14058
|
-
|
|
14059
|
-
|
|
14060
|
-
|
|
14775
|
+
/**
|
|
14776
|
+
* The gap just after a range deletion, where what replaces the range goes:
|
|
14777
|
+
* after the struck content in a redline, and after whatever a deletion keeps
|
|
14778
|
+
* in place, so that accepting a tracked replacement gives the direct one.
|
|
14779
|
+
* A range across containers is replaced in its first paragraph run, at that
|
|
14780
|
+
* run's end. Content after the range's end is untouched by the deletion, so
|
|
14781
|
+
* the gap is found by its distance from the end of the paragraph now holding
|
|
14782
|
+
* that content; joins may have moved it into an earlier paragraph of the run.
|
|
14783
|
+
* `after` may also hold content inserted at the gap; the gap then follows it.
|
|
14784
|
+
*/
|
|
14785
|
+
const rangeEndAfterDeletion = ({ before, after, from, to }) => {
|
|
14786
|
+
const runs = selectedParagraphRuns(before, from, to);
|
|
14787
|
+
if (runs.isErr()) panic("A successful range deletion must have its input paragraphs.");
|
|
14788
|
+
const run = runs.value.at(0) ?? [];
|
|
14789
|
+
const source = run.at(-1)?.paragraph;
|
|
14790
|
+
if (source === void 0) panic("A successful range deletion must select a paragraph.");
|
|
14791
|
+
const sourceLength = paragraphLength(source);
|
|
14792
|
+
const caret = idKey(from.blockId) === idKey(to.blockId) && from.offset === to.offset && from.zeroWidthBefore === to.zeroWidthBefore;
|
|
14793
|
+
const end = idKey(source.paraId ?? "") === idKey(to.blockId) ? {
|
|
14794
|
+
offset: to.offset,
|
|
14795
|
+
zeroWidthBefore: to.zeroWidthBefore ?? (caret ? defaultInsertionGap(source.content, to.offset).zeroWidthBefore : 0)
|
|
14796
|
+
} : {
|
|
14797
|
+
offset: sourceLength,
|
|
14798
|
+
zeroWidthBefore: zeroWidthLeavesAt(source.content, sourceLength).length
|
|
14799
|
+
};
|
|
14800
|
+
const current = new Map(storyParagraphs(storyBody(after, from.story)).map(({ paragraph }) => [idKey(paragraph.paraId ?? ""), paragraph]));
|
|
14801
|
+
const holder = run.findLast(({ paragraph }) => current.has(idKey(paragraph.paraId ?? "")))?.paragraph;
|
|
14802
|
+
const target = holder === void 0 ? void 0 : current.get(idKey(holder.paraId ?? ""));
|
|
14803
|
+
if (holder === void 0 || target === void 0) panic("A successful range deletion must retain a selected paragraph.");
|
|
14804
|
+
const offset = paragraphLength(target) - (sourceLength - end.offset);
|
|
14805
|
+
const following = zeroWidthLeavesAt(source.content, end.offset).length - end.zeroWidthBefore;
|
|
14061
14806
|
return {
|
|
14062
|
-
|
|
14063
|
-
blockId:
|
|
14064
|
-
|
|
14807
|
+
story: from.story,
|
|
14808
|
+
blockId: holder.paraId ?? to.blockId,
|
|
14809
|
+
offset,
|
|
14810
|
+
zeroWidthBefore: zeroWidthLeavesAt(target.content, offset).length - following
|
|
14065
14811
|
};
|
|
14066
14812
|
};
|
|
14067
14813
|
/**
|
|
@@ -14081,7 +14827,8 @@ const planTrackedReplace = (document, options) => {
|
|
|
14081
14827
|
const plan = createTrackedPlan({
|
|
14082
14828
|
document,
|
|
14083
14829
|
revision,
|
|
14084
|
-
newIds
|
|
14830
|
+
newIds,
|
|
14831
|
+
...options.deletionRevision === void 0 ? {} : { deletionRevision: options.deletionRevision }
|
|
14085
14832
|
});
|
|
14086
14833
|
const deleted = appendTrackedDeletion({
|
|
14087
14834
|
document,
|
|
@@ -14118,7 +14865,7 @@ const planTrackedReplace = (document, options) => {
|
|
|
14118
14865
|
if (patched.isErr()) return Result.err(patched.error);
|
|
14119
14866
|
}
|
|
14120
14867
|
}
|
|
14121
|
-
let at =
|
|
14868
|
+
let at = rangeEndAfterDeletion({
|
|
14122
14869
|
before: document,
|
|
14123
14870
|
after: plan.document(),
|
|
14124
14871
|
from,
|
|
@@ -14167,6 +14914,30 @@ const planTrackedReplace = (document, options) => {
|
|
|
14167
14914
|
zeroWidthBefore: 0
|
|
14168
14915
|
};
|
|
14169
14916
|
}
|
|
14917
|
+
const leadingFragment = replacement.paragraphs.at(0);
|
|
14918
|
+
const toParagraph = storyParagraphs(storyBody(document, from.story)).find(({ paragraph }) => idKey(paragraph.paraId ?? "") === idKey(options.to.blockId))?.paragraph;
|
|
14919
|
+
const cutDepth = toParagraph === void 0 ? 0 : spanningRecords(toParagraph.content, [{
|
|
14920
|
+
offset: options.to.offset,
|
|
14921
|
+
zeroWidthBefore: options.to.zeroWidthBefore ?? 0
|
|
14922
|
+
}], 0, 1).length;
|
|
14923
|
+
if (cutDepth > 0 && leadingFragment !== void 0 && leadingFragment.content.length > 0) {
|
|
14924
|
+
const first = storyParagraphs(storyBody(plan.document(), from.story)).find(({ paragraph }) => idKey(paragraph.paraId ?? "") === idKey(leadingFragment.paraId ?? ""))?.paragraph;
|
|
14925
|
+
const payload = first?.content.at(-1);
|
|
14926
|
+
if (first !== void 0 && payload?.type === "insertion" && payload.info.author === revision.author && (payload.resolutionJoins?.remove ?? 0) < cutDepth) {
|
|
14927
|
+
const recorded = plan.append({
|
|
14928
|
+
type: DOCUMENT_OP_TYPES.REPLACE_INLINE,
|
|
14929
|
+
story: from.story,
|
|
14930
|
+
blockId: first.paraId ?? "",
|
|
14931
|
+
expected: first.content,
|
|
14932
|
+
content: asParagraphContent([...first.content.slice(0, -1), Object.assign({}, payload, { resolutionJoins: Object.assign({
|
|
14933
|
+
before: 0,
|
|
14934
|
+
after: 0,
|
|
14935
|
+
remove: 0
|
|
14936
|
+
}, payload.resolutionJoins, { remove: cutDepth }) })])
|
|
14937
|
+
});
|
|
14938
|
+
if (recorded.isErr()) return Result.err(recorded.error);
|
|
14939
|
+
}
|
|
14940
|
+
}
|
|
14170
14941
|
if (replacement.tail.content.length > 0) {
|
|
14171
14942
|
const inserted = plan.append({
|
|
14172
14943
|
type: DOCUMENT_OP_TYPES.INSERT_CONTENT,
|
|
@@ -14179,8 +14950,135 @@ const planTrackedReplace = (document, options) => {
|
|
|
14179
14950
|
}
|
|
14180
14951
|
return Result.ok(plan.ops);
|
|
14181
14952
|
};
|
|
14953
|
+
/**
|
|
14954
|
+
* Handoffs for the records an edit cut whose first piece holds only content
|
|
14955
|
+
* on its way out. Accepting that removal empties the first piece, which then
|
|
14956
|
+
* goes; the piece cut off after it, wherever the edit left it in the story,
|
|
14957
|
+
* takes back the record's identities, as the same edit made directly leaves
|
|
14958
|
+
* the record whole. Each removal in the first piece carries the handoff.
|
|
14959
|
+
*/
|
|
14960
|
+
const cutPieceHandoffs = ({ before, after, story }) => {
|
|
14961
|
+
const present = new Set(identityKeysIn(storyBody(before, story).content));
|
|
14962
|
+
const paragraphs = storyParagraphs(storyBody(after, story)).map(({ paragraph }) => paragraph);
|
|
14963
|
+
const order = [];
|
|
14964
|
+
const visit = (nodes, paragraph) => {
|
|
14965
|
+
for (const node of nodes) {
|
|
14966
|
+
order.push({
|
|
14967
|
+
node,
|
|
14968
|
+
paragraph
|
|
14969
|
+
});
|
|
14970
|
+
visit(childNodes(node) ?? [], paragraph);
|
|
14971
|
+
}
|
|
14972
|
+
};
|
|
14973
|
+
for (const [index, paragraph] of paragraphs.entries()) visit(paragraph.content, index);
|
|
14974
|
+
const handoffs = /* @__PURE__ */ new Map();
|
|
14975
|
+
for (const [index, { node: first }] of order.entries()) {
|
|
14976
|
+
const children = childNodes(first);
|
|
14977
|
+
const source = identitySlots(first);
|
|
14978
|
+
if (children === void 0 || children.length === 0 || !children.every(isRemovedRevisionNode) || source.length === 0 || !source.every((slot) => present.has(slotKey(slot)))) continue;
|
|
14979
|
+
const piece = order.slice(index + 1).find(({ node }) => sameOwnFields(node, first) && identitySlots(node).length === source.length && identitySlots(node).every((slot) => !present.has(slotKey(slot))))?.node;
|
|
14980
|
+
if (piece === void 0) continue;
|
|
14981
|
+
const target = identitySlots(piece);
|
|
14982
|
+
for (const removal of children) handoffs.set(removal, [...handoffs.get(removal) ?? [], {
|
|
14983
|
+
levels: 1,
|
|
14984
|
+
source,
|
|
14985
|
+
target
|
|
14986
|
+
}]);
|
|
14987
|
+
}
|
|
14988
|
+
if (handoffs.size === 0) return [];
|
|
14989
|
+
const carry = (node) => {
|
|
14990
|
+
const entries = handoffs.get(node);
|
|
14991
|
+
let next = node;
|
|
14992
|
+
if (entries !== void 0 && (node.type === "deletion" || node.type === "moveFrom")) {
|
|
14993
|
+
const joins = node.resolutionJoins ?? {
|
|
14994
|
+
before: 0,
|
|
14995
|
+
after: 0,
|
|
14996
|
+
remove: 0
|
|
14997
|
+
};
|
|
14998
|
+
next = Object.assign({}, node, { resolutionJoins: Object.assign({}, joins, { retainedEnclosing: [...joins.retainedEnclosing ?? [], ...entries] }) });
|
|
14999
|
+
}
|
|
15000
|
+
const children = childNodes(next);
|
|
15001
|
+
if (children === void 0) return next;
|
|
15002
|
+
const changed = children.map(carry);
|
|
15003
|
+
return changed.every((child, index) => child === children[index]) ? next : rebuildNode(next, changed);
|
|
15004
|
+
};
|
|
15005
|
+
return paragraphs.flatMap((paragraph) => {
|
|
15006
|
+
const content = paragraph.content.map(carry);
|
|
15007
|
+
return content.every((node, index) => node === paragraph.content[index]) ? [] : [{
|
|
15008
|
+
type: DOCUMENT_OP_TYPES.REPLACE_INLINE,
|
|
15009
|
+
story,
|
|
15010
|
+
blockId: paragraph.paraId ?? "",
|
|
15011
|
+
expected: paragraph.content,
|
|
15012
|
+
content: asParagraphContent(content)
|
|
15013
|
+
}];
|
|
15014
|
+
});
|
|
15015
|
+
};
|
|
15016
|
+
// src/ops/revisionGrouping.ts
|
|
15017
|
+
/** The leaf just before a gap and the leaf just after it, partly covered ones included. */
|
|
15018
|
+
const leavesBeside = (spans, gap) => [spans.findLast(({ before }) => compareGaps(before, gap) < 0), spans.find(({ after }) => compareGaps(after, gap) > 0)];
|
|
15019
|
+
/** The innermost tracked record of `kind` holding a text leaf, when `author` made it. */
|
|
15020
|
+
const ownRecord = (span, kind, author) => {
|
|
15021
|
+
if (span?.node.type !== "text") return void 0;
|
|
15022
|
+
const record = span.ancestors.findLast((ancestor) => isTrackedWrapper(ancestor) && ancestor.type === kind);
|
|
15023
|
+
return record !== void 0 && isTrackedWrapper(record) && record.info.author === author && record.info.suggestionId === void 0 ? record : void 0;
|
|
15024
|
+
};
|
|
15025
|
+
/**
|
|
15026
|
+
* The stamp a tracked deletion of the range takes: that of the author's own
|
|
15027
|
+
* pending deletion beside the range, looked for before and after its start,
|
|
15028
|
+
* then before and after its end, with the deletion's own id; else `stamp`.
|
|
15029
|
+
*/
|
|
15030
|
+
const groupedDeletionStamp = ({ start, end, stamp }) => {
|
|
15031
|
+
if (stamp.suggestionId !== void 0) return stamp;
|
|
15032
|
+
const beside = [...leavesBeside(leafSpans(start.items), start.gap), ...leavesBeside(leafSpans(end.items), end.gap)];
|
|
15033
|
+
for (const span of beside) {
|
|
15034
|
+
const record = ownRecord(span, "deletion", stamp.author);
|
|
15035
|
+
const date = record?.info.date;
|
|
15036
|
+
if (record === void 0 || date === void 0) continue;
|
|
15037
|
+
const continued = {
|
|
15038
|
+
id: stamp.id,
|
|
15039
|
+
author: record.info.author,
|
|
15040
|
+
date,
|
|
15041
|
+
...record.info.initials === void 0 ? {} : { initials: record.info.initials }
|
|
15042
|
+
};
|
|
15043
|
+
if (carriesStamp(record.info, continued)) return continued;
|
|
15044
|
+
}
|
|
15045
|
+
return stamp;
|
|
15046
|
+
};
|
|
15047
|
+
/**
|
|
15048
|
+
* Where text typed at a caret goes when it continues the author's own
|
|
15049
|
+
* pending insertion beside it, the one before the caret first: `content`
|
|
15050
|
+
* inside that insertion and the containers the typed text continues anyway,
|
|
15051
|
+
* open into them on each side the source holds them. `undefined` when no
|
|
15052
|
+
* such insertion is beside the caret.
|
|
15053
|
+
*/
|
|
15054
|
+
const ownInsertionSlice = ({ items, gap, continued, author, content }) => {
|
|
15055
|
+
const [before, after] = leavesBeside(leafSpans(items), gap);
|
|
15056
|
+
for (const span of [before, after]) {
|
|
15057
|
+
if (span === void 0 || !continued.every((container, depth) => span.ancestors[depth] === container)) continue;
|
|
15058
|
+
const record = ownRecord(span, "insertion", author);
|
|
15059
|
+
if (record === void 0 || span.ancestors[continued.length] !== record) continue;
|
|
15060
|
+
const chain = [...continued, record];
|
|
15061
|
+
const open = (side) => {
|
|
15062
|
+
let depth = 0;
|
|
15063
|
+
while (depth < chain.length && side?.ancestors[depth] === chain[depth]) depth += 1;
|
|
15064
|
+
return depth;
|
|
15065
|
+
};
|
|
15066
|
+
let wrapped = content.slice();
|
|
15067
|
+
for (const container of chain.toReversed()) wrapped = [rebuildNode(container, wrapped)];
|
|
15068
|
+
return {
|
|
15069
|
+
openStart: open(before),
|
|
15070
|
+
openEnd: open(after),
|
|
15071
|
+
content: asParagraphContent(wrapped)
|
|
15072
|
+
};
|
|
15073
|
+
}
|
|
15074
|
+
};
|
|
14182
15075
|
// src/ops/editorIntent.ts
|
|
14183
15076
|
/** One editor intent, compiled to direct or tracked document operations. */
|
|
15077
|
+
const newListOps = (newList) => newList === void 0 ? [] : [{
|
|
15078
|
+
type: DOCUMENT_OP_TYPES.CREATE_NUMBERING_INSTANCE,
|
|
15079
|
+
num: newList.num,
|
|
15080
|
+
...newList.abstractNum === void 0 ? {} : { abstractNum: newList.abstractNum }
|
|
15081
|
+
}];
|
|
14184
15082
|
const intentEndpoints = (intent) => {
|
|
14185
15083
|
switch (intent.type) {
|
|
14186
15084
|
case "table":
|
|
@@ -14199,6 +15097,11 @@ const intentEndpoints = (intent) => {
|
|
|
14199
15097
|
fromOffset: 0,
|
|
14200
15098
|
toOffset: 0
|
|
14201
15099
|
};
|
|
15100
|
+
case "deleteParagraphs": return {
|
|
15101
|
+
story: intent.story,
|
|
15102
|
+
fromId: intent.blockIds[0] ?? "",
|
|
15103
|
+
toId: intent.blockIds.at(-1) ?? ""
|
|
15104
|
+
};
|
|
14202
15105
|
case "setHyperlink":
|
|
14203
15106
|
case "removeHyperlink":
|
|
14204
15107
|
case "insertHyperlink":
|
|
@@ -14258,8 +15161,12 @@ const intentEndpoints = (intent) => {
|
|
|
14258
15161
|
};
|
|
14259
15162
|
const PARAGRAPH_REVISION_DEMAND = 3;
|
|
14260
15163
|
const leafRevisionDemand = (ancestorCount) => 4 * (1 + ancestorCount);
|
|
14261
|
-
/**
|
|
14262
|
-
|
|
15164
|
+
/**
|
|
15165
|
+
* Cache only immutable document versions; never retain a retired version
|
|
15166
|
+
* strongly. A new paragraph identity avoids `reservedBlockIds`, the ones
|
|
15167
|
+
* consumed elsewhere, such as an undone insertion's.
|
|
15168
|
+
*/
|
|
15169
|
+
const createEditorIntentIdAllocator = ({ reservedBlockIds } = {}) => {
|
|
14263
15170
|
const readCensus = createCensusReader();
|
|
14264
15171
|
return (document, intent) => {
|
|
14265
15172
|
const census = readCensus(document);
|
|
@@ -14357,7 +15264,8 @@ const createEditorIntentIdAllocator = () => {
|
|
|
14357
15264
|
}
|
|
14358
15265
|
const revision = fresh(census.revisions, demand + 1);
|
|
14359
15266
|
let nextParagraph = 1;
|
|
14360
|
-
|
|
15267
|
+
const taken = (id) => census.paragraphs.has(id) || (reservedBlockIds?.has(id) ?? false);
|
|
15268
|
+
while (taken(nextParagraph.toString(16).padStart(8, "0").toUpperCase())) nextParagraph += 1;
|
|
14361
15269
|
return {
|
|
14362
15270
|
revisionId: revision.at(0) ?? 2147483648,
|
|
14363
15271
|
newBlockId: nextParagraph.toString(16).padStart(8, "0").toUpperCase(),
|
|
@@ -14710,7 +15618,12 @@ const runSeamDepth = ({ paragraph, at, policy }) => {
|
|
|
14710
15618
|
}
|
|
14711
15619
|
return 0;
|
|
14712
15620
|
};
|
|
14713
|
-
|
|
15621
|
+
/**
|
|
15622
|
+
* Where a direct replacement inside a source run rejoins the run it cut: at
|
|
15623
|
+
* the replacement's start, where accepting the tracked replacement (struck
|
|
15624
|
+
* content first, then the new) removes the deletion and joins its sides.
|
|
15625
|
+
*/
|
|
15626
|
+
const replacementSourceRunStart = (document, op) => {
|
|
14714
15627
|
const paragraph = paragraphAt(document, op.at);
|
|
14715
15628
|
if (paragraph === void 0) panic("An accepted replacement insertion has its source paragraph.");
|
|
14716
15629
|
const at = {
|
|
@@ -14720,7 +15633,7 @@ const replacementSourceRunEnd = (document, op) => {
|
|
|
14720
15633
|
if (!spanningRecords(paragraph.content, [at], 0, 1).some((node) => node.type === "run")) return void 0;
|
|
14721
15634
|
return {
|
|
14722
15635
|
...op.at,
|
|
14723
|
-
...
|
|
15636
|
+
...at
|
|
14724
15637
|
};
|
|
14725
15638
|
};
|
|
14726
15639
|
const replacementPreservesOutsideContent = ({ paragraph, from, to }) => {
|
|
@@ -14739,6 +15652,35 @@ const replacementPreservesOutsideContent = ({ paragraph, from, to }) => {
|
|
|
14739
15652
|
const preserved = (node) => isParagraphContent(node) && (isRangeBoundary(node) || node.type === "commentReference");
|
|
14740
15653
|
return before.every(preserved) && after.every(preserved);
|
|
14741
15654
|
};
|
|
15655
|
+
/**
|
|
15656
|
+
* Where the caret lands once whole paragraphs go directly: at the start of the
|
|
15657
|
+
* paragraph that follows them, or, when they end their container, in the
|
|
15658
|
+
* final paragraph that stays, where its predecessor's content ends.
|
|
15659
|
+
*/
|
|
15660
|
+
const directDeletionCaret = (document, { story, blockIds }) => {
|
|
15661
|
+
const located = storyParagraphs(storyBody(document, story));
|
|
15662
|
+
const indexOf = (id) => located.findIndex(({ paragraph }) => idKey(paragraph.paraId ?? "") === idKey(id ?? ""));
|
|
15663
|
+
const first = located[indexOf(blockIds[0])];
|
|
15664
|
+
const last = located[indexOf(blockIds.at(-1))];
|
|
15665
|
+
if (first === void 0 || last === void 0) return {
|
|
15666
|
+
story,
|
|
15667
|
+
blockId: blockIds[0] ?? "",
|
|
15668
|
+
offset: 0
|
|
15669
|
+
};
|
|
15670
|
+
const list = blockListAt(storyBody(document, story).content, last.list);
|
|
15671
|
+
const following = list[last.index + 1];
|
|
15672
|
+
if (following?.type === "paragraph" && following.paraId !== void 0) return {
|
|
15673
|
+
story,
|
|
15674
|
+
blockId: following.paraId,
|
|
15675
|
+
offset: 0
|
|
15676
|
+
};
|
|
15677
|
+
const preceding = list[first.index - 1];
|
|
15678
|
+
return {
|
|
15679
|
+
story,
|
|
15680
|
+
blockId: last.paragraph.paraId ?? "",
|
|
15681
|
+
offset: last.index === list.length - 1 && preceding?.type === "paragraph" ? paragraphLength(preceding) : 0
|
|
15682
|
+
};
|
|
15683
|
+
};
|
|
14742
15684
|
/** Compile one batch; the caller applies it atomically and journals its exact inverse. */
|
|
14743
15685
|
const compileEditorIntent = (document, { intent, mode, firstBlockId }) => {
|
|
14744
15686
|
if (firstBlockId !== void 0 && (!Number.isInteger(firstBlockId) || firstBlockId < 1 || firstBlockId > 2147483648)) return Result.err(new DocumentOpRefusal({
|
|
@@ -14753,6 +15695,7 @@ const compileEditorIntent = (document, { intent, mode, firstBlockId }) => {
|
|
|
14753
15695
|
} : allocationFields;
|
|
14754
15696
|
let ops;
|
|
14755
15697
|
let selection;
|
|
15698
|
+
let handedBack;
|
|
14756
15699
|
const editedSeams = [];
|
|
14757
15700
|
switch (intent.type) {
|
|
14758
15701
|
case "generateTOC": {
|
|
@@ -14874,25 +15817,12 @@ const compileEditorIntent = (document, { intent, mode, firstBlockId }) => {
|
|
|
14874
15817
|
ops = planned.value;
|
|
14875
15818
|
const applied = applyDocumentOps(document, ops);
|
|
14876
15819
|
if (applied.isErr()) return Result.err(applied.error);
|
|
14877
|
-
|
|
15820
|
+
selection = rangeEndAfterDeletion({
|
|
14878
15821
|
before: document,
|
|
14879
15822
|
after: applied.value.document,
|
|
14880
15823
|
from: intent.from,
|
|
14881
15824
|
to: intent.to
|
|
14882
15825
|
});
|
|
14883
|
-
const insertionAt = leading.length > 0 ? {
|
|
14884
|
-
...at,
|
|
14885
|
-
offset: 0,
|
|
14886
|
-
zeroWidthBefore: 0
|
|
14887
|
-
} : at;
|
|
14888
|
-
const insertionParagraph = paragraphAt(document, insertionAt);
|
|
14889
|
-
selection = {
|
|
14890
|
-
...insertionAt,
|
|
14891
|
-
...gapAfterInserted({
|
|
14892
|
-
offset: insertionAt.offset,
|
|
14893
|
-
zeroWidthBefore: insertionAt.zeroWidthBefore ?? (insertionParagraph === void 0 ? 0 : defaultInsertionGap(insertionParagraph.content, insertionAt.offset).zeroWidthBefore)
|
|
14894
|
-
}, tail?.content ?? [])
|
|
14895
|
-
};
|
|
14896
15826
|
} else {
|
|
14897
15827
|
const deleted = compileEditorIntent(document, {
|
|
14898
15828
|
intent: {
|
|
@@ -15358,6 +16288,28 @@ const compileEditorIntent = (document, { intent, mode, firstBlockId }) => {
|
|
|
15358
16288
|
};
|
|
15359
16289
|
break;
|
|
15360
16290
|
}
|
|
16291
|
+
case "deleteParagraphs": {
|
|
16292
|
+
const first = intent.blockIds[0];
|
|
16293
|
+
if (first === void 0) return Result.err(new DocumentOpRefusal({
|
|
16294
|
+
reason: DOCUMENT_OP_REFUSAL_REASONS.EMPTY_BLOCK_LIST,
|
|
16295
|
+
message: "No paragraphs to delete.",
|
|
16296
|
+
opType: DOCUMENT_OP_TYPES.DELETE_BLOCKS
|
|
16297
|
+
}));
|
|
16298
|
+
return Result.ok({
|
|
16299
|
+
ops: [{
|
|
16300
|
+
type: DOCUMENT_OP_TYPES.DELETE_BLOCKS,
|
|
16301
|
+
story: intent.story,
|
|
16302
|
+
blockIds: intent.blockIds,
|
|
16303
|
+
...tracked,
|
|
16304
|
+
...mode.type === "suggesting" && intent.move === true ? { move: true } : {}
|
|
16305
|
+
}],
|
|
16306
|
+
selection: mode.type === "editing" ? directDeletionCaret(document, intent) : {
|
|
16307
|
+
story: intent.story,
|
|
16308
|
+
blockId: first,
|
|
16309
|
+
offset: 0
|
|
16310
|
+
}
|
|
16311
|
+
});
|
|
16312
|
+
}
|
|
15361
16313
|
case "insertParagraphs": {
|
|
15362
16314
|
const fresh = freshParagraphIds({
|
|
15363
16315
|
document,
|
|
@@ -15377,12 +16329,13 @@ const compileEditorIntent = (document, { intent, mode, firstBlockId }) => {
|
|
|
15377
16329
|
opType: DOCUMENT_OP_TYPES.INSERT_BLOCKS
|
|
15378
16330
|
}));
|
|
15379
16331
|
return Result.ok({
|
|
15380
|
-
ops: [{
|
|
16332
|
+
ops: [...newListOps(intent.newList), {
|
|
15381
16333
|
type: DOCUMENT_OP_TYPES.INSERT_BLOCKS,
|
|
15382
16334
|
story: intent.story,
|
|
15383
16335
|
at: intent.at,
|
|
15384
16336
|
blocks,
|
|
15385
|
-
...tracked
|
|
16337
|
+
...tracked,
|
|
16338
|
+
...mode.type === "suggesting" && intent.move === true ? { move: true } : {}
|
|
15386
16339
|
}],
|
|
15387
16340
|
selection: {
|
|
15388
16341
|
story: intent.story,
|
|
@@ -15597,6 +16550,7 @@ const compileEditorIntent = (document, { intent, mode, firstBlockId }) => {
|
|
|
15597
16550
|
}
|
|
15598
16551
|
ops = plan.ops;
|
|
15599
16552
|
}
|
|
16553
|
+
ops = [...newListOps(intent.newList), ...ops];
|
|
15600
16554
|
selection = intent.at;
|
|
15601
16555
|
break;
|
|
15602
16556
|
case "formatRun": {
|
|
@@ -15743,7 +16697,80 @@ const compileEditorIntent = (document, { intent, mode, firstBlockId }) => {
|
|
|
15743
16697
|
text
|
|
15744
16698
|
}]
|
|
15745
16699
|
}];
|
|
15746
|
-
const
|
|
16700
|
+
const continued = idKey(from.blockId) === idKey(to.blockId) && from.offset === to.offset && (from.zeroWidthBefore ?? 0) === (to.zeroWidthBefore ?? 0) && text !== "" ? continuation(paragraph.content, from, to, { ...intent.inside === void 0 ? {} : { inside: intent.inside } }) : void 0;
|
|
16701
|
+
const continuing = continued === void 0 ? void 0 : continuedSlice(continued, content);
|
|
16702
|
+
if (mode.type === "suggesting" && continued !== void 0) {
|
|
16703
|
+
const at = {
|
|
16704
|
+
...from,
|
|
16705
|
+
zeroWidthBefore: from.zeroWidthBefore ?? defaultInsertionGap(paragraph.content, from.offset).zeroWidthBefore
|
|
16706
|
+
};
|
|
16707
|
+
const slice = ownInsertionSlice({
|
|
16708
|
+
items: paragraph.content,
|
|
16709
|
+
gap: {
|
|
16710
|
+
offset: at.offset,
|
|
16711
|
+
zeroWidthBefore: at.zeroWidthBefore
|
|
16712
|
+
},
|
|
16713
|
+
continued: continued.containers,
|
|
16714
|
+
author: mode.revision.author,
|
|
16715
|
+
content
|
|
16716
|
+
});
|
|
16717
|
+
if (slice !== void 0) {
|
|
16718
|
+
const insert = {
|
|
16719
|
+
type: DOCUMENT_OP_TYPES.INSERT_CONTENT,
|
|
16720
|
+
at,
|
|
16721
|
+
slice,
|
|
16722
|
+
...mode.newIds === void 0 ? {} : { newIds: mode.newIds }
|
|
16723
|
+
};
|
|
16724
|
+
const inserted = applyDocumentOp(document, insert);
|
|
16725
|
+
if (inserted.isErr()) return Result.err(inserted.error);
|
|
16726
|
+
ops = [trimAppliedNewIds({
|
|
16727
|
+
before: document,
|
|
16728
|
+
op: insert,
|
|
16729
|
+
applied: inserted.value,
|
|
16730
|
+
story: at.story
|
|
16731
|
+
})];
|
|
16732
|
+
const steps = [inserted.value];
|
|
16733
|
+
let current = inserted.value.document;
|
|
16734
|
+
for (const seam of [{
|
|
16735
|
+
offset: at.offset + text.length,
|
|
16736
|
+
zeroWidthBefore: 0
|
|
16737
|
+
}, {
|
|
16738
|
+
offset: at.offset,
|
|
16739
|
+
zeroWidthBefore: at.zeroWidthBefore
|
|
16740
|
+
}]) {
|
|
16741
|
+
const typedInto = paragraphAt(current, at);
|
|
16742
|
+
if (typedInto === void 0) panic("A typed paragraph must exist at its seam.");
|
|
16743
|
+
const depth = runSeamDepth({
|
|
16744
|
+
paragraph: typedInto,
|
|
16745
|
+
at: seam,
|
|
16746
|
+
policy: RUN_SEAM_POLICIES.plain
|
|
16747
|
+
});
|
|
16748
|
+
if (depth === 0) continue;
|
|
16749
|
+
const join = {
|
|
16750
|
+
type: DOCUMENT_OP_TYPES.JOIN_INLINE,
|
|
16751
|
+
at: {
|
|
16752
|
+
...at,
|
|
16753
|
+
...seam
|
|
16754
|
+
},
|
|
16755
|
+
depth
|
|
16756
|
+
};
|
|
16757
|
+
const joined = applyDocumentOp(current, join);
|
|
16758
|
+
if (joined.isErr()) return Result.err(joined.error);
|
|
16759
|
+
ops.push(join);
|
|
16760
|
+
steps.push(joined.value);
|
|
16761
|
+
current = joined.value.document;
|
|
16762
|
+
}
|
|
16763
|
+
handedBack = {
|
|
16764
|
+
ops: [...ops],
|
|
16765
|
+
result: {
|
|
16766
|
+
...combineEdits(document, steps),
|
|
16767
|
+
revisions: steps.flatMap(({ revisions }) => revisions)
|
|
16768
|
+
}
|
|
16769
|
+
};
|
|
16770
|
+
selection = replacementCaret(at, text);
|
|
16771
|
+
break;
|
|
16772
|
+
}
|
|
16773
|
+
}
|
|
15747
16774
|
if (mode.type === "suggesting" && continuing !== void 0) {
|
|
15748
16775
|
const at = {
|
|
15749
16776
|
...from,
|
|
@@ -15761,11 +16788,29 @@ const compileEditorIntent = (document, { intent, mode, firstBlockId }) => {
|
|
|
15761
16788
|
break;
|
|
15762
16789
|
}
|
|
15763
16790
|
if (mode.type === "suggesting") {
|
|
16791
|
+
const struck = groupedDeletionStamp({
|
|
16792
|
+
start: {
|
|
16793
|
+
items: paragraph.content,
|
|
16794
|
+
gap: {
|
|
16795
|
+
offset: from.offset,
|
|
16796
|
+
zeroWidthBefore: from.zeroWidthBefore ?? zeroWidthLeavesAt(paragraph.content, from.offset).length
|
|
16797
|
+
}
|
|
16798
|
+
},
|
|
16799
|
+
end: {
|
|
16800
|
+
items: (paragraphAt(document, to) ?? paragraph).content,
|
|
16801
|
+
gap: {
|
|
16802
|
+
offset: to.offset,
|
|
16803
|
+
zeroWidthBefore: to.zeroWidthBefore ?? 0
|
|
16804
|
+
}
|
|
16805
|
+
},
|
|
16806
|
+
stamp: mode.revision
|
|
16807
|
+
});
|
|
15764
16808
|
const planned = planTrackedReplace(document, {
|
|
15765
16809
|
sourceContainerPolicy: "join",
|
|
15766
16810
|
from,
|
|
15767
16811
|
to,
|
|
15768
|
-
revision: mode.revision,
|
|
16812
|
+
revision: text === "" ? struck : mode.revision,
|
|
16813
|
+
...text !== "" && struck !== mode.revision ? { deletionRevision: struck } : {},
|
|
15769
16814
|
newIds: mode.newIds,
|
|
15770
16815
|
seamPolicy: INSERTION_SEAM_POLICIES.MERGE_PLAIN_RUNS,
|
|
15771
16816
|
replacement: {
|
|
@@ -15778,7 +16823,10 @@ const compileEditorIntent = (document, { intent, mode, firstBlockId }) => {
|
|
|
15778
16823
|
}
|
|
15779
16824
|
});
|
|
15780
16825
|
if (planned.isErr()) return Result.err(planned.error);
|
|
15781
|
-
const resumed = idKey(from.blockId) === idKey(to.blockId) && text !== "" ? continuation(paragraph.content, from, to, {
|
|
16826
|
+
const resumed = idKey(from.blockId) === idKey(to.blockId) && text !== "" ? continuation(paragraph.content, from, to, {
|
|
16827
|
+
...intent.inside === void 0 ? {} : { inside: intent.inside },
|
|
16828
|
+
struck: true
|
|
16829
|
+
}) : void 0;
|
|
15782
16830
|
ops = [];
|
|
15783
16831
|
for (const op of planned.value) {
|
|
15784
16832
|
if (resumed === void 0 || op.type !== DOCUMENT_OP_TYPES.INSERT_CONTENT || idKey(op.at.blockId) !== idKey(from.blockId) || op.slice.openStart !== 0 || op.slice.openEnd !== 0) {
|
|
@@ -15829,7 +16877,8 @@ const compileEditorIntent = (document, { intent, mode, firstBlockId }) => {
|
|
|
15829
16877
|
}
|
|
15830
16878
|
ops = [];
|
|
15831
16879
|
let survivorId = from.blockId;
|
|
15832
|
-
|
|
16880
|
+
const deletes = from.blockId !== to.blockId || from.offset !== to.offset || (from.zeroWidthBefore ?? 0) !== (to.zeroWidthBefore ?? 0);
|
|
16881
|
+
if (deletes) {
|
|
15833
16882
|
const partitioned = selectedParagraphRuns(document, from, to);
|
|
15834
16883
|
if (partitioned.isErr()) return Result.err(partitioned.error);
|
|
15835
16884
|
survivorId = partitioned.value.at(0)?.at(-1)?.paragraph.paraId ?? from.blockId;
|
|
@@ -15886,11 +16935,21 @@ const compileEditorIntent = (document, { intent, mode, firstBlockId }) => {
|
|
|
15886
16935
|
});
|
|
15887
16936
|
}
|
|
15888
16937
|
}
|
|
15889
|
-
|
|
16938
|
+
let at = {
|
|
15890
16939
|
...from,
|
|
15891
16940
|
blockId: survivorId,
|
|
15892
16941
|
zeroWidthBefore: from.zeroWidthBefore ?? defaultInsertionGap(paragraph.content, from.offset).zeroWidthBefore
|
|
15893
16942
|
};
|
|
16943
|
+
if (deletes) {
|
|
16944
|
+
const deleted = applyDocumentOps(document, ops);
|
|
16945
|
+
if (deleted.isErr()) return Result.err(deleted.error);
|
|
16946
|
+
at = rangeEndAfterDeletion({
|
|
16947
|
+
before: document,
|
|
16948
|
+
after: deleted.value.document,
|
|
16949
|
+
from,
|
|
16950
|
+
to
|
|
16951
|
+
});
|
|
16952
|
+
}
|
|
15894
16953
|
if (text !== "") ops.push({
|
|
15895
16954
|
type: DOCUMENT_OP_TYPES.INSERT_CONTENT,
|
|
15896
16955
|
at,
|
|
@@ -15910,7 +16969,7 @@ const compileEditorIntent = (document, { intent, mode, firstBlockId }) => {
|
|
|
15910
16969
|
type: DOCUMENT_OP_TYPES.SPLIT_BLOCK,
|
|
15911
16970
|
at: intent.at,
|
|
15912
16971
|
newBlockId: intent.newBlockId,
|
|
15913
|
-
newHalf: SPLIT_HALVES.FIRST,
|
|
16972
|
+
newHalf: intent.newHalf ?? SPLIT_HALVES.FIRST,
|
|
15914
16973
|
newParagraph: splitParagraphFields(document, intent.at),
|
|
15915
16974
|
...tracked
|
|
15916
16975
|
};
|
|
@@ -15948,7 +17007,7 @@ const compileEditorIntent = (document, { intent, mode, firstBlockId }) => {
|
|
|
15948
17007
|
plan
|
|
15949
17008
|
});
|
|
15950
17009
|
if (deletion.isErr()) return Result.err(deletion.error);
|
|
15951
|
-
const at =
|
|
17010
|
+
const at = rangeEndAfterDeletion({
|
|
15952
17011
|
before: document,
|
|
15953
17012
|
after: plan.document(),
|
|
15954
17013
|
from: intent.at,
|
|
@@ -15958,7 +17017,7 @@ const compileEditorIntent = (document, { intent, mode, firstBlockId }) => {
|
|
|
15958
17017
|
type: DOCUMENT_OP_TYPES.SPLIT_BLOCK,
|
|
15959
17018
|
at,
|
|
15960
17019
|
newBlockId: intent.newBlockId,
|
|
15961
|
-
newHalf: SPLIT_HALVES.FIRST,
|
|
17020
|
+
newHalf: intent.newHalf ?? SPLIT_HALVES.FIRST,
|
|
15962
17021
|
newParagraph: splitParagraphFields(plan.document(), at),
|
|
15963
17022
|
newIds: mode.newIds,
|
|
15964
17023
|
revision: mode.revision
|
|
@@ -15988,7 +17047,7 @@ const compileEditorIntent = (document, { intent, mode, firstBlockId }) => {
|
|
|
15988
17047
|
type: DOCUMENT_OP_TYPES.SPLIT_BLOCK,
|
|
15989
17048
|
at: deletion.value.selection,
|
|
15990
17049
|
newBlockId: intent.newBlockId,
|
|
15991
|
-
newHalf: SPLIT_HALVES.FIRST,
|
|
17050
|
+
newHalf: intent.newHalf ?? SPLIT_HALVES.FIRST,
|
|
15992
17051
|
newParagraph: splitParagraphFields(deleted.value.document, deletion.value.selection),
|
|
15993
17052
|
...allocationFields
|
|
15994
17053
|
}];
|
|
@@ -16059,9 +17118,10 @@ const compileEditorIntent = (document, { intent, mode, firstBlockId }) => {
|
|
|
16059
17118
|
} else ops = [join];
|
|
16060
17119
|
let offset = 0;
|
|
16061
17120
|
if ((mode.type === "editing" || ownInsertedMark) && paragraph !== void 0) offset = paragraphLength(paragraph);
|
|
17121
|
+
const survivorId = mode.type === "editing" && intent.survivor === "first" ? intent.blockId : intent.nextBlockId;
|
|
16062
17122
|
selection = {
|
|
16063
17123
|
story: intent.story,
|
|
16064
|
-
blockId:
|
|
17124
|
+
blockId: survivorId,
|
|
16065
17125
|
offset
|
|
16066
17126
|
};
|
|
16067
17127
|
break;
|
|
@@ -16071,6 +17131,7 @@ const compileEditorIntent = (document, { intent, mode, firstBlockId }) => {
|
|
|
16071
17131
|
if (mode.type === "editing") {
|
|
16072
17132
|
let current = document;
|
|
16073
17133
|
const compact = [];
|
|
17134
|
+
const steps = [];
|
|
16074
17135
|
const takenRevision = /* @__PURE__ */ new Set();
|
|
16075
17136
|
const takenControl = /* @__PURE__ */ new Set();
|
|
16076
17137
|
const sourceRunSeams = [];
|
|
@@ -16080,7 +17141,7 @@ const compileEditorIntent = (document, { intent, mode, firstBlockId }) => {
|
|
|
16080
17141
|
const op = filterNewIds(input, (space, id) => !(space === IDENTITY_SPACES.REVISION ? takenRevision : takenControl).has(id));
|
|
16081
17142
|
const applied = applyDocumentOp(current, op);
|
|
16082
17143
|
if (applied.isErr()) return Result.err(applied.error);
|
|
16083
|
-
const
|
|
17144
|
+
const sourceRunStart = replacesSourceContent && op.type === DOCUMENT_OP_TYPES.INSERT_CONTENT ? replacementSourceRunStart(current, op) : void 0;
|
|
16084
17145
|
const trimmed = trimAppliedNewIds({
|
|
16085
17146
|
before: current,
|
|
16086
17147
|
op,
|
|
@@ -16092,8 +17153,9 @@ const compileEditorIntent = (document, { intent, mode, firstBlockId }) => {
|
|
|
16092
17153
|
for (const id of trimmed.newIds?.control ?? []) takenControl.add(id);
|
|
16093
17154
|
}
|
|
16094
17155
|
compact.push(trimmed);
|
|
17156
|
+
steps.push(applied.value);
|
|
16095
17157
|
current = applied.value.document;
|
|
16096
|
-
if (
|
|
17158
|
+
if (sourceRunStart !== void 0) sourceRunSeams.push(sourceRunStart);
|
|
16097
17159
|
}
|
|
16098
17160
|
if (intent.type === "joinParagraphs") {
|
|
16099
17161
|
const alignment = joinParagraphPropertyAlignment({
|
|
@@ -16105,6 +17167,7 @@ const compileEditorIntent = (document, { intent, mode, firstBlockId }) => {
|
|
|
16105
17167
|
const aligned = applyDocumentOp(current, alignment);
|
|
16106
17168
|
if (aligned.isErr()) return Result.err(aligned.error);
|
|
16107
17169
|
compact.push(alignment);
|
|
17170
|
+
steps.push(aligned.value);
|
|
16108
17171
|
current = aligned.value.document;
|
|
16109
17172
|
}
|
|
16110
17173
|
}
|
|
@@ -16155,15 +17218,46 @@ const compileEditorIntent = (document, { intent, mode, firstBlockId }) => {
|
|
|
16155
17218
|
const joined = applyDocumentOp(current, join);
|
|
16156
17219
|
if (joined.isErr()) return Result.err(joined.error);
|
|
16157
17220
|
compact.push(join);
|
|
17221
|
+
steps.push(joined.value);
|
|
16158
17222
|
current = joined.value.document;
|
|
16159
17223
|
}
|
|
16160
17224
|
ops = compact;
|
|
17225
|
+
if (intent.type === "replaceText") handedBack = {
|
|
17226
|
+
ops: [...compact],
|
|
17227
|
+
result: {
|
|
17228
|
+
...combineEdits(document, steps),
|
|
17229
|
+
revisions: steps.flatMap(({ revisions }) => revisions)
|
|
17230
|
+
}
|
|
17231
|
+
};
|
|
17232
|
+
if (paragraphAt(current, selection) === void 0) panic("A direct edit's caret must name a paragraph the edit keeps.");
|
|
17233
|
+
}
|
|
17234
|
+
const appliedAsCompiled = handedBack !== void 0 && handedBack.ops.length === ops.length && handedBack.ops.every((op, index) => op === ops[index]) ? handedBack.result : void 0;
|
|
17235
|
+
if (mode.type === "suggesting" && cutsBesideRemoval(document, ops)) {
|
|
17236
|
+
const cut = appliedAsCompiled === void 0 ? applyDocumentOps(document, ops) : Result.ok(appliedAsCompiled);
|
|
17237
|
+
if (cut.isErr()) return Result.err(cut.error);
|
|
17238
|
+
ops = [...ops, ...cutPieceHandoffs({
|
|
17239
|
+
before: document,
|
|
17240
|
+
after: cut.value.document,
|
|
17241
|
+
story: intentEndpoints(intent).story
|
|
17242
|
+
})];
|
|
17243
|
+
return Result.ok({
|
|
17244
|
+
ops: ops.map(captureDocumentOp),
|
|
17245
|
+
selection
|
|
17246
|
+
});
|
|
16161
17247
|
}
|
|
16162
17248
|
return Result.ok({
|
|
16163
17249
|
ops: ops.map(captureDocumentOp),
|
|
16164
|
-
selection
|
|
17250
|
+
selection,
|
|
17251
|
+
...appliedAsCompiled === void 0 ? {} : { applied: appliedAsCompiled }
|
|
16165
17252
|
});
|
|
16166
17253
|
};
|
|
17254
|
+
/** A record holding removed content inside its own identity, in a paragraph. */
|
|
17255
|
+
const holdsNestedRemoval = (nodes) => nodes.some((node) => identitySlots(node).length > 0 && (childNodes(node) ?? []).some(isRemovedRevisionNode) || holdsNestedRemoval(childNodes(node) ?? []));
|
|
17256
|
+
/**
|
|
17257
|
+
* Whether a tracked edit may cut a record whose first piece then holds only
|
|
17258
|
+
* removed content: it removes a range, or inserts where such content sits.
|
|
17259
|
+
*/
|
|
17260
|
+
const cutsBesideRemoval = (document, ops) => ops.some((op) => op.type === DOCUMENT_OP_TYPES.DELETE_RANGE && op.revision !== void 0 || (op.type === DOCUMENT_OP_TYPES.INSERT_CONTENT || op.type === DOCUMENT_OP_TYPES.SPLIT_BLOCK) && holdsNestedRemoval(paragraphAt(document, op.at)?.content ?? []));
|
|
16167
17261
|
/** The lowest paragraph identities neither the package nor the batch holds. */
|
|
16168
17262
|
const freshParagraphIds = ({ document, reserved, count, opType }) => {
|
|
16169
17263
|
const occupied = new Set([...packageParagraphIds(document.package), ...reserved ?? []].map(idKey));
|
|
@@ -16231,12 +17325,18 @@ const CONTINUED_CONTAINERS = /* @__PURE__ */ new Set(["inlineSdt", "inlineWrappe
|
|
|
16231
17325
|
* so typing within one extends it rather than cutting it in two. At an edge
|
|
16232
17326
|
* only one side is inside, and the text stays outside, as a mark that does not
|
|
16233
17327
|
* extend past its end, unless the caret stood just inside that edge.
|
|
17328
|
+
*
|
|
17329
|
+
* Text replacing a range takes the place after the range's last leaf, where
|
|
17330
|
+
* a tracked replacement follows the struck content: it continues what holds
|
|
17331
|
+
* that leaf and the leaf after the range. A tracked replacement meets the
|
|
17332
|
+
* struck leaf on its start side; a direct one meets the leaf before the range.
|
|
16234
17333
|
*/
|
|
16235
|
-
const continuation = (items, from, to, { inside = {} } = {}) => {
|
|
17334
|
+
const continuation = (items, from, to, { inside = {}, struck = false } = {}) => {
|
|
16236
17335
|
const spans = leafSpans(items);
|
|
17336
|
+
const caret = from.offset === to.offset && from.zeroWidthBefore === to.zeroWidthBefore;
|
|
16237
17337
|
const start = {
|
|
16238
17338
|
offset: from.offset,
|
|
16239
|
-
zeroWidthBefore: from.zeroWidthBefore ?? 0
|
|
17339
|
+
zeroWidthBefore: from.zeroWidthBefore ?? (caret ? 0 : zeroWidthLeavesAt(items, from.offset).length)
|
|
16240
17340
|
};
|
|
16241
17341
|
const end = {
|
|
16242
17342
|
offset: to.offset,
|
|
@@ -16244,7 +17344,8 @@ const continuation = (items, from, to, { inside = {} } = {}) => {
|
|
|
16244
17344
|
};
|
|
16245
17345
|
const outerLeft = spans.findLast(({ before }) => compareGaps(before, start) < 0);
|
|
16246
17346
|
const outerRight = spans.find(({ after }) => compareGaps(after, end) > 0);
|
|
16247
|
-
const
|
|
17347
|
+
const last = spans.findLast(({ before, after }) => compareGaps(before, end) < 0 && compareGaps(after, start) > 0);
|
|
17348
|
+
const left = last ?? (inside.start ? spans.find(({ after }) => compareGaps(after, start) > 0) : outerLeft);
|
|
16248
17349
|
const right = inside.end ? spans.findLast(({ before }) => compareGaps(before, end) < 0) : outerRight;
|
|
16249
17350
|
const containers = [];
|
|
16250
17351
|
for (const [depth, ancestor] of (left?.ancestors ?? []).entries()) {
|
|
@@ -16256,9 +17357,10 @@ const continuation = (items, from, to, { inside = {} } = {}) => {
|
|
|
16256
17357
|
while (depth < containers.length && side?.ancestors[depth] === containers[depth]) depth += 1;
|
|
16257
17358
|
return depth;
|
|
16258
17359
|
};
|
|
17360
|
+
const kept = struck || caret ? void 0 : spans.findLast(({ node, before, after }) => isParagraphContent(node) && isRangeBoundary(node) && compareGaps(before, start) >= 0 && compareGaps(after, end) <= 0);
|
|
16259
17361
|
return {
|
|
16260
17362
|
containers,
|
|
16261
|
-
openStart: continued(outerLeft),
|
|
17363
|
+
openStart: continued(struck && last !== void 0 ? last : kept ?? outerLeft),
|
|
16262
17364
|
openEnd: continued(outerRight)
|
|
16263
17365
|
};
|
|
16264
17366
|
};
|
|
@@ -16543,14 +17645,16 @@ const operationFields = {
|
|
|
16543
17645
|
at: blockAnchor,
|
|
16544
17646
|
blocks: array(paragraph),
|
|
16545
17647
|
newIds,
|
|
16546
|
-
revision
|
|
17648
|
+
revision,
|
|
17649
|
+
move: optional(literal(true))
|
|
16547
17650
|
},
|
|
16548
17651
|
deleteBlocks: {
|
|
16549
17652
|
type: literal("deleteBlocks"),
|
|
16550
17653
|
story: isOpStory,
|
|
16551
17654
|
blockIds: array(blockId),
|
|
16552
17655
|
newIds,
|
|
16553
|
-
revision
|
|
17656
|
+
revision,
|
|
17657
|
+
move: optional(literal(true))
|
|
16554
17658
|
},
|
|
16555
17659
|
resolveRevision: {
|
|
16556
17660
|
type: literal("resolveRevision"),
|
|
@@ -17676,4 +18780,4 @@ const groupInsertionRuns = (operations, insertionPositionOf, keepsGap = () => fa
|
|
|
17676
18780
|
}
|
|
17677
18781
|
return groups;
|
|
17678
18782
|
};
|
|
17679
|
-
export { BATCH_REJECTION_REASONS, BATCH_WIRE_OP_TYPES, BatchRejection, DOCUMENT_OP_REFUSAL_REASONS, DOCUMENT_OP_SCHEMA_VERSION, DOCUMENT_OP_TYPES, DocumentOpRefusal, DocumentOpsContractError, EMPTY_PROPERTY_SETS, INHERIT_RUN_PROPS, MAX_BATCH_WIRE_BYTES, OBJECT_REPLACEMENT_CHARACTER, OP_STORIES, PARAGRAPH_MARK_FORMATTING_KEYS, PROPERTY_REVIEW_POLICIES, REVISION_DECISIONS, SPLIT_HALVES, acceptCopyOriginAncestors, addCopyOriginAncestor, allocateCommentAnchorIds, applyDocumentOp, applyDocumentOpEnvelope, applyDocumentOps, captureDocumentOp, combineEdits, commentDocumentIssue, compareBatchOrder, compareGaps, compileEditorIntent, contentRank, createClient, createEditorIntentIdAllocator, createSequencer, defaultInsertionGap, deletionRemovesBlock, documentStories, editorParagraphGroups, editorPositionAtPhysicalPosition, findStoryBody, formattingEquals, freshCommentId, groupInsertionRuns, idKey, inlineLeafSpans, isCommentAnchorCarrierType, isCommentExhausted, isCopyOriginAncestors, isTextOnlyRun, mapTocBookmarkPosition, mergeRunContent, normalizeForOps, noteUsesCustomMark, orderAddressingBeforeRemoval, orderBatchOperations, packageParagraphIds, packageResourcesOpOf, paragraphContentVerdict, paragraphIdsIn, paragraphLength, paragraphLogicalText, paragraphMarkOutcome, paragraphPropertySnapshotToken, paragraphRemains, paragraphVisibleText, parseDocumentBatch, pendingSuggestionIdsIn, physicalOffsetAtVisibleOffset, physicalPositionAtEditorOffset, planTrackedDeletion, planTrackedReplace, planWellFormedRanges, reconcileDocumentPartRelationships, registerParagraphPropertySnapshot, rejectCopyOriginAncestors, revisionIdDemand, runsMergeable, sameStory, sectionPropertiesAt, shiftCopyOriginAncestors, storyBody, suggestionRevisionInfos, survivingComments, toOpEnvelope, transformBatch, validateDocumentBatch, validateOpsDocument, validateSequencedBatch, withBodyContent, withoutCommentThreads, withoutPendingSuggestions, zeroWidthLeavesAt };
|
|
18783
|
+
export { BATCH_REJECTION_REASONS, BATCH_WIRE_OP_TYPES, BatchRejection, DOCUMENT_OP_REFUSAL_REASONS, DOCUMENT_OP_SCHEMA_VERSION, DOCUMENT_OP_TYPES, DocumentOpRefusal, DocumentOpsContractError, EMPTY_PROPERTY_SETS, INHERIT_RUN_PROPS, MAX_BATCH_WIRE_BYTES, OBJECT_REPLACEMENT_CHARACTER, OP_STORIES, PARAGRAPH_MARK_FORMATTING_KEYS, PROPERTY_REVIEW_POLICIES, REVISION_DECISIONS, SPLIT_HALVES, acceptCopyOriginAncestors, addCopyOriginAncestor, allocateCommentAnchorIds, applyDocumentOp, applyDocumentOpEnvelope, applyDocumentOps, captureDocumentOp, combineEdits, commentDocumentIssue, compareBatchOrder, compareGaps, compileEditorIntent, contentRank, createClient, createEditorIntentIdAllocator, createSequencer, defaultInsertionGap, deletionRemovesBlock, documentStories, editorParagraphGroups, editorPositionAtPhysicalPosition, findStoryBody, formattingEquals, freshCommentId, groupInsertionRuns, idKey, inlineLeafSpans, isCommentAnchorCarrierType, isCommentExhausted, isCopyOriginAncestors, isTextOnlyRun, mapTocBookmarkPosition, mergeRunContent, normalizeForOps, noteUsesCustomMark, orderAddressingBeforeRemoval, orderBatchOperations, packageParagraphIds, packageResourcesOpOf, paragraphContentVerdict, paragraphIdsIn, paragraphLength, paragraphLogicalText, paragraphMarkOutcome, paragraphPropertySnapshotToken, paragraphRemains, paragraphVisibleText, parseDocumentBatch, pendingSuggestionIdsIn, physicalOffsetAtVisibleOffset, physicalPositionAtEditorOffset, planTrackedDeletion, planTrackedReplace, planWellFormedRanges, reconcileDocumentPartRelationships, registerParagraphPropertySnapshot, rejectCopyOriginAncestors, replaceStoryBody, revisionIdDemand, revisionSpan, runsMergeable, sameStory, sectionPropertiesAt, shiftCopyOriginAncestors, storyBody, storyParagraphs, suggestedCommentIds, suggestionRevisionInfos, survivingComments, toOpEnvelope, transformBatch, validateDocumentBatch, validateOpsDocument, validateSequencedBatch, withBodyContent, withoutCommentThreads, withoutPendingSuggestions, zeroWidthLeavesAt };
|