@stll/docx-core 0.28.1 → 0.30.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +6 -1
- package/dist/docx_kernel_bg.wasm +0 -0
- package/dist/index.d.ts +7 -0
- package/dist/index.js +127 -6
- package/dist/ops/documentOps.d.ts +426 -28
- package/dist/ops/documentOps.js +3653 -697
- package/package.json +2 -2
package/dist/ops/documentOps.js
CHANGED
|
@@ -37,11 +37,22 @@ const derivedFields = (entries) => entries.some(([key, value]) => key === "type"
|
|
|
37
37
|
const equalForStaleness = (left, right) => equal(left, right, derivedFields);
|
|
38
38
|
//#endregion
|
|
39
39
|
//#region src/ops/types.ts
|
|
40
|
-
/**
|
|
41
|
-
|
|
40
|
+
/**
|
|
41
|
+
* The operation schema this module reads and writes.
|
|
42
|
+
*
|
|
43
|
+
* Version 4 adds paragraph deletion through `deleteBlocks`, direct and tracked
|
|
44
|
+
* whole-table operations with their exact structural inverse, terminal insertion,
|
|
45
|
+
* and cell-ending paragraph marks on tracked row operations.
|
|
46
|
+
* Version 3 added paragraph insertion through `insertBlocks` and the table-row
|
|
47
|
+
* operations `insertRow`, `deleteRow` and `setTableRows`.
|
|
48
|
+
* Version 2 added tracked changes: the `revision` stamp on the text, formatting
|
|
49
|
+
* and paragraph operations, and the review operations `setParagraphReview`,
|
|
50
|
+
* `replaceInline` and `resolveRevision`.
|
|
51
|
+
*/
|
|
52
|
+
const DOCUMENT_OP_SCHEMA_VERSION = 4;
|
|
42
53
|
/**
|
|
43
54
|
* The stories an operation can address. Headers, footers, notes and comment
|
|
44
|
-
* bodies are stories too; version
|
|
55
|
+
* bodies are stories too; this version addresses the main story only.
|
|
45
56
|
*/
|
|
46
57
|
const OP_STORIES = Object.freeze({ MAIN: "main" });
|
|
47
58
|
/**
|
|
@@ -58,8 +69,10 @@ const EMPTY_PROPERTY_SETS = Object.freeze({
|
|
|
58
69
|
* unit before the position, or the one after it at the start of a paragraph.
|
|
59
70
|
*/
|
|
60
71
|
const INHERIT_RUN_PROPS = "inherit";
|
|
61
|
-
/** The operation kinds of schema version
|
|
72
|
+
/** The operation kinds of schema version 4. */
|
|
62
73
|
const DOCUMENT_OP_TYPES = Object.freeze({
|
|
74
|
+
DELETE_BLOCKS: "deleteBlocks",
|
|
75
|
+
INSERT_BLOCKS: "insertBlocks",
|
|
63
76
|
INSERT_TEXT: "insertText",
|
|
64
77
|
INSERT_CONTENT: "insertContent",
|
|
65
78
|
DELETE_RANGE: "deleteRange",
|
|
@@ -69,11 +82,36 @@ const DOCUMENT_OP_TYPES = Object.freeze({
|
|
|
69
82
|
SET_PARAGRAPH_PROPS: "setParagraphProps",
|
|
70
83
|
SPLIT_BLOCK: "splitBlock",
|
|
71
84
|
JOIN_BLOCKS: "joinBlocks",
|
|
72
|
-
REPLACE_BLOCKS: "replaceBlocks"
|
|
85
|
+
REPLACE_BLOCKS: "replaceBlocks",
|
|
86
|
+
SET_PARAGRAPH_REVIEW: "setParagraphReview",
|
|
87
|
+
REPLACE_INLINE: "replaceInline",
|
|
88
|
+
RESOLVE_REVISION: "resolveRevision",
|
|
89
|
+
INSERT_TABLE: "insertTable",
|
|
90
|
+
DELETE_TABLE: "deleteTable",
|
|
91
|
+
SET_CONTAINER_BLOCKS: "setContainerBlocks",
|
|
92
|
+
INSERT_ROW: "insertRow",
|
|
93
|
+
DELETE_ROW: "deleteRow",
|
|
94
|
+
SET_TABLE_ROWS: "setTableRows"
|
|
95
|
+
});
|
|
96
|
+
/**
|
|
97
|
+
* Paragraph formatting that belongs to the paragraph mark's run properties
|
|
98
|
+
* (`w:pPr/w:rPr`) rather than to the paragraph properties a `w:pPrChange`
|
|
99
|
+
* records.
|
|
100
|
+
*/
|
|
101
|
+
const PARAGRAPH_MARK_FORMATTING_KEYS = Object.freeze(["runProperties", "runInWithNext"]);
|
|
102
|
+
/** Which half of a split, or of a join, a rule applies to. */
|
|
103
|
+
const SPLIT_HALVES = Object.freeze({
|
|
104
|
+
FIRST: "first",
|
|
105
|
+
SECOND: "second"
|
|
106
|
+
});
|
|
107
|
+
/** What resolving a tracked change does with it. */
|
|
108
|
+
const REVISION_DECISIONS = Object.freeze({
|
|
109
|
+
ACCEPT: "accept",
|
|
110
|
+
REJECT: "reject"
|
|
73
111
|
});
|
|
74
112
|
/** An operation in the envelope it is journaled and sent in. */
|
|
75
113
|
const toOpEnvelope = (op) => ({
|
|
76
|
-
schema:
|
|
114
|
+
schema: 4,
|
|
77
115
|
op
|
|
78
116
|
});
|
|
79
117
|
//#endregion
|
|
@@ -147,6 +185,48 @@ const sameBlockList = (left, right) => left.length === right.length && left.ever
|
|
|
147
185
|
const other = right[index];
|
|
148
186
|
return other !== void 0 && sameStep(step, other);
|
|
149
187
|
});
|
|
188
|
+
/** The block list a location's steps lead to. */
|
|
189
|
+
const blockListAt = (blocks, list) => {
|
|
190
|
+
let current = blocks;
|
|
191
|
+
for (const step of list) {
|
|
192
|
+
const block = current[step.block];
|
|
193
|
+
switch (step.kind) {
|
|
194
|
+
case "tableCell": {
|
|
195
|
+
const cell = block?.type === "table" ? block.rows[step.row]?.cells[step.cell] : void 0;
|
|
196
|
+
if (cell === void 0) return panic(`Block list step does not name a table cell at block ${step.block}.`);
|
|
197
|
+
current = cell.content;
|
|
198
|
+
break;
|
|
199
|
+
}
|
|
200
|
+
case "blockSdt":
|
|
201
|
+
if (block?.type !== "blockSdt") return panic(`Block list step does not name a block content control at ${step.block}.`);
|
|
202
|
+
current = block.content;
|
|
203
|
+
break;
|
|
204
|
+
case "blockCustomXml":
|
|
205
|
+
if (block?.type !== "blockCustomXml") return panic(`Block list step does not name a custom XML wrapper at ${step.block}.`);
|
|
206
|
+
current = block.content;
|
|
207
|
+
break;
|
|
208
|
+
default: return step;
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
return current;
|
|
212
|
+
};
|
|
213
|
+
/**
|
|
214
|
+
* Whether a block is the last one of its story body or table cell. A block
|
|
215
|
+
* content control or custom XML wrapper is a step in the block list, not a
|
|
216
|
+
* container: the last block inside one ends the container only when the
|
|
217
|
+
* wrapper does.
|
|
218
|
+
*/
|
|
219
|
+
const endsItsContainer = (body, { list, index }) => {
|
|
220
|
+
let steps = list;
|
|
221
|
+
let position = index;
|
|
222
|
+
for (;;) {
|
|
223
|
+
if (position !== blockListAt(body.content, steps).length - 1) return false;
|
|
224
|
+
const last = steps.at(-1);
|
|
225
|
+
if (last === void 0 || last.kind === "tableCell") return true;
|
|
226
|
+
steps = steps.slice(0, -1);
|
|
227
|
+
position = last.block;
|
|
228
|
+
}
|
|
229
|
+
};
|
|
150
230
|
const updateBlockList = (blocks, list, update) => {
|
|
151
231
|
const [step, ...rest] = list;
|
|
152
232
|
if (step === void 0) return update(blocks);
|
|
@@ -372,7 +452,11 @@ const IDENTITY_SPACES = Object.freeze({
|
|
|
372
452
|
CONTROL: "control"
|
|
373
453
|
});
|
|
374
454
|
const slotKey = ({ space, id }) => `${space}:${id}`;
|
|
375
|
-
const revisionIdOf = (entries) => {
|
|
455
|
+
const revisionIdOf = (entries, heldBy) => {
|
|
456
|
+
if (heldBy === "gridChange" && Array.isArray(fieldOf(entries, "columnWidths"))) {
|
|
457
|
+
const id = fieldOf(entries, "id");
|
|
458
|
+
return typeof id === "number" ? id : void 0;
|
|
459
|
+
}
|
|
376
460
|
const info = fieldOf(entries, "info");
|
|
377
461
|
if (typeof info !== "object" || info === null) return void 0;
|
|
378
462
|
const infoEntries = Object.entries(info);
|
|
@@ -392,7 +476,7 @@ const identityKeysIn = (value) => {
|
|
|
392
476
|
const out = [];
|
|
393
477
|
const stacked = /* @__PURE__ */ new Map();
|
|
394
478
|
walk(value, (entries, heldBy) => {
|
|
395
|
-
const revision = revisionIdOf(entries);
|
|
479
|
+
const revision = revisionIdOf(entries, heldBy);
|
|
396
480
|
if (revision !== void 0) out.push(slotKey({
|
|
397
481
|
space: IDENTITY_SPACES.REVISION,
|
|
398
482
|
id: revision
|
|
@@ -570,6 +654,7 @@ const INLINE_SDT_CHILD_TYPES = {
|
|
|
570
654
|
preservedInline: true
|
|
571
655
|
};
|
|
572
656
|
const isHyperlinkChild = (item) => Object.hasOwn(HYPERLINK_CHILD_TYPES, item.type);
|
|
657
|
+
/** Whether a tracked change (`w:ins`, `w:del`, a move) can hold the record. */
|
|
573
658
|
const isTrackedChild = (item) => Object.hasOwn(TRACKED_CHILD_TYPES, item.type);
|
|
574
659
|
const isInlineSdtChild = (item) => Object.hasOwn(INLINE_SDT_CHILD_TYPES, item.type);
|
|
575
660
|
/**
|
|
@@ -726,6 +811,12 @@ const maskIdentity = (node) => {
|
|
|
726
811
|
* the gaps" and an insertion is "cut at a gap and merge the new leaves in",
|
|
727
812
|
* each is the other's exact inverse, whatever the records around them hold.
|
|
728
813
|
*/
|
|
814
|
+
const COMMENT_ANCHORS = new Set(Object.keys({
|
|
815
|
+
commentRangeStart: true,
|
|
816
|
+
commentRangeEnd: true,
|
|
817
|
+
commentReference: true
|
|
818
|
+
}));
|
|
819
|
+
const isCommentAnchor = (node) => COMMENT_ANCHORS.has(node.type);
|
|
729
820
|
const RUN_CONTENT_TYPES = {
|
|
730
821
|
text: true,
|
|
731
822
|
tab: true,
|
|
@@ -758,13 +849,13 @@ const kindOf = (node) => {
|
|
|
758
849
|
return inlineWidth(node) === 1 ? "unit" : "zeroWidth";
|
|
759
850
|
};
|
|
760
851
|
/**
|
|
761
|
-
* Whether a
|
|
762
|
-
*
|
|
852
|
+
* Whether a text node, run, or revision wrapper holds nothing. Such records say
|
|
853
|
+
* nothing, operations never create one, and a document is normalized so it
|
|
763
854
|
* holds none (see `contract.ts`): otherwise each is a zero-width leaf that
|
|
764
855
|
* positions would have to count. An empty hyperlink or content control is
|
|
765
856
|
* not empty in this sense: it is markup, and stays a zero-width leaf.
|
|
766
857
|
*/
|
|
767
|
-
const isEmptyRecord = (node) => node.type === "text" && node.text === "" || node.type === "run" && node.content.length === 0;
|
|
858
|
+
const isEmptyRecord = (node) => node.type === "text" && node.text === "" || (node.type === "run" || node.type === "insertion" || node.type === "deletion" || node.type === "moveFrom" || node.type === "moveTo") && node.content.length === 0;
|
|
768
859
|
const startCursor = () => ({
|
|
769
860
|
position: 0,
|
|
770
861
|
zeroWidthSeen: 0
|
|
@@ -799,6 +890,7 @@ const rebuildNode = (node, children) => {
|
|
|
799
890
|
if (isRunContent(node) || !isInlineContainer(node)) return panic(`A ${node.type} has no children to rebuild.`);
|
|
800
891
|
return withChildren(node, narrowNodes(children, isParagraphContent));
|
|
801
892
|
};
|
|
893
|
+
/** A record's pieces between gaps, the cursor moved past it. */
|
|
802
894
|
const partitionNode = (node, gaps, cursor) => {
|
|
803
895
|
if (node.type === "text" && node.text !== "") {
|
|
804
896
|
const start = cursor.position;
|
|
@@ -1113,233 +1205,55 @@ const runGaps = (items) => {
|
|
|
1113
1205
|
walk(items);
|
|
1114
1206
|
return out;
|
|
1115
1207
|
};
|
|
1116
|
-
//#endregion
|
|
1117
|
-
//#region src/ops/refusal.ts
|
|
1118
|
-
/**
|
|
1119
|
-
* Why an operation was not applied. A refused operation changes nothing: the
|
|
1120
|
-
* document it was applied to is returned as it was.
|
|
1121
|
-
*/
|
|
1122
|
-
const DOCUMENT_OP_REFUSAL_REASONS = Object.freeze({
|
|
1123
|
-
/** No paragraph in the story carries the id. */
|
|
1124
|
-
BLOCK_NOT_FOUND: "blockNotFound",
|
|
1125
|
-
/** An offset is not an integer from `0` to the paragraph's length, or a range runs backwards. */
|
|
1126
|
-
INVALID_OFFSET: "invalidOffset",
|
|
1127
|
-
/** A range starts and ends in different paragraphs or stories. */
|
|
1128
|
-
CROSS_BLOCK_RANGE: "crossBlockRange",
|
|
1129
|
-
/** A position falls between the two halves of a surrogate pair. */
|
|
1130
|
-
SPLITS_SURROGATE_PAIR: "splitsSurrogatePair",
|
|
1131
|
-
/** Inserted text is empty, holds a tab or line-break character, or cannot be written to XML. */
|
|
1132
|
-
INVALID_TEXT: "invalidText",
|
|
1133
|
-
/**
|
|
1134
|
-
* A paragraph id the operation (or its inverse) creates is not eight hex
|
|
1135
|
-
* digits below `0x80000000`, or is the reserved `00000000`.
|
|
1136
|
-
*/
|
|
1137
|
-
INVALID_BLOCK_ID: "invalidBlockId",
|
|
1138
|
-
/**
|
|
1139
|
-
* An id the operation brings in (a paragraph id, or a revision or
|
|
1140
|
-
* content-control id a replacement carries) is already used in the package.
|
|
1141
|
-
*/
|
|
1142
|
-
ID_COLLISION: "idCollision",
|
|
1143
|
-
/** The paragraphs to join or replace are not adjacent siblings of one container. */
|
|
1144
|
-
NOT_ADJACENT: "notAdjacent",
|
|
1145
|
-
/**
|
|
1146
|
-
* The operation would move or remove a section break (joining a paragraph
|
|
1147
|
-
* that ends a section, replacing paragraphs across or at one); that is a
|
|
1148
|
-
* section operation.
|
|
1149
|
-
*/
|
|
1150
|
-
SECTION_BOUNDARY: "sectionBoundary",
|
|
1151
|
-
/** Direct text would land inside tracked-deleted or moved-away content. */
|
|
1152
|
-
INSIDE_TRACKED_DELETION: "insideTrackedDeletion",
|
|
1153
|
-
/**
|
|
1154
|
-
* The operation cuts a record carrying a revision or content-control id in
|
|
1155
|
-
* two and names too few `newIds` for the halves after the first.
|
|
1156
|
-
*/
|
|
1157
|
-
NEEDS_NEW_IDS: "needsNewIds",
|
|
1158
|
-
/** A new revision or content-control id is not an integer from 0 to 2^31 - 1. */
|
|
1159
|
-
INVALID_NEW_ID: "invalidNewId",
|
|
1160
|
-
/**
|
|
1161
|
-
* The operation would merge two records carrying the same revision or
|
|
1162
|
-
* content-control id: nothing could tell them apart again to undo it.
|
|
1163
|
-
*/
|
|
1164
|
-
SHARED_ID: "sharedId",
|
|
1165
|
-
/**
|
|
1166
|
-
* What the operation expects (paragraphs to replace, a slice to delete,
|
|
1167
|
-
* the values a patch replaced) is not what is there.
|
|
1168
|
-
*/
|
|
1169
|
-
STALE: "stale",
|
|
1170
|
-
/**
|
|
1171
|
-
* The records at a position are not the shape the operation needs: an open
|
|
1172
|
-
* end meets a record it cannot merge with, records to join differ, a slice
|
|
1173
|
-
* would merge records that were separate, or there are fewer records to cut
|
|
1174
|
-
* than the operation names.
|
|
1175
|
-
*/
|
|
1176
|
-
STRUCTURE_MISMATCH: "structureMismatch",
|
|
1177
|
-
/** Inserted content is empty, or holds an empty run or text node. */
|
|
1178
|
-
EMPTY_CONTENT: "emptyContent",
|
|
1179
|
-
/** A replacement names no paragraph to replace, or none to put in its place. */
|
|
1180
|
-
EMPTY_BLOCK_LIST: "emptyBlockList",
|
|
1181
|
-
/** Seed contract: a main-story paragraph has no `paraId`. */
|
|
1182
|
-
MISSING_BLOCK_ID: "missingBlockId",
|
|
1183
|
-
/** Seed contract: two paragraphs in the package carry one id. */
|
|
1184
|
-
DUPLICATE_BLOCK_ID: "duplicateBlockId",
|
|
1185
|
-
/** Seed contract: two records in the package carry one revision or content-control id. */
|
|
1186
|
-
DUPLICATE_RECORD_ID: "duplicateRecordId",
|
|
1187
|
-
/** Seed contract: the body's section view does not match its blocks. */
|
|
1188
|
-
SECTIONS_OUT_OF_STEP: "sectionsOutOfStep",
|
|
1189
|
-
/** Seed contract: the main story holds an empty run or empty text node. */
|
|
1190
|
-
EMPTY_RECORD: "emptyRecord"
|
|
1191
|
-
});
|
|
1192
|
-
/** An operation the document cannot take as stated. */
|
|
1193
|
-
var DocumentOpRefusal = class extends TaggedError("DocumentOpRefusal") {};
|
|
1194
|
-
//#endregion
|
|
1195
|
-
//#region src/ops/contract.ts
|
|
1196
1208
|
/**
|
|
1197
|
-
*
|
|
1198
|
-
*
|
|
1199
|
-
*
|
|
1200
|
-
*
|
|
1201
|
-
* footers and notes included, and makes ids unique), parse, then
|
|
1202
|
-
* {@link normalizeForOps}. {@link validateOpsDocument} states the result:
|
|
1203
|
-
*
|
|
1204
|
-
* - every paragraph of the main story has a `w14:paraId`;
|
|
1205
|
-
* - no paragraph id repeats anywhere in the package, compared as hex, so an
|
|
1206
|
-
* inverse can always recreate the ids it removed;
|
|
1207
|
-
* - no revision id (tracked changes, property changes) or content-control id
|
|
1208
|
-
* repeats, so the one record carrying an id is the one it names;
|
|
1209
|
-
* - the body's section view says what its blocks say;
|
|
1210
|
-
* - the main story holds no empty run or empty text node.
|
|
1211
|
-
*
|
|
1212
|
-
* Paragraphs in text boxes, headers, footers, notes and comments are other
|
|
1213
|
-
* stories: they are not addressable in schema version 1, and their ids count
|
|
1214
|
-
* toward uniqueness like any other.
|
|
1215
|
-
*
|
|
1216
|
-
* Every operation checks the contract before applying and leaves it holding,
|
|
1217
|
-
* so a document an operation produced is not checked again: documents are
|
|
1218
|
-
* values, never modified in place.
|
|
1209
|
+
* How many levels the record ending `left` and the one starting `right` merge
|
|
1210
|
+
* before a pair differs: text with text, and records alike in everything but
|
|
1211
|
+
* their content and ids, down the chain of last and first children. `0` when
|
|
1212
|
+
* the two are not alike at all.
|
|
1219
1213
|
*/
|
|
1220
|
-
|
|
1221
|
-
|
|
1222
|
-
const
|
|
1223
|
-
|
|
1224
|
-
|
|
1225
|
-
const
|
|
1226
|
-
|
|
1227
|
-
|
|
1228
|
-
|
|
1229
|
-
});
|
|
1230
|
-
const repeated = [...countIds(packageParagraphIds(document.package))].find(([, count]) => count > 1);
|
|
1231
|
-
if (repeated !== void 0) return new DocumentOpsContractError({
|
|
1232
|
-
reason: DOCUMENT_OP_REFUSAL_REASONS.DUPLICATE_BLOCK_ID,
|
|
1233
|
-
message: `${repeated[1]} paragraphs in the package are ${repeated[0]}.`
|
|
1234
|
-
});
|
|
1235
|
-
const repeatedRecord = [...countKeys(packageIdentityKeys(document.package))].find(([, count]) => count > 1);
|
|
1236
|
-
if (repeatedRecord !== void 0) return new DocumentOpsContractError({
|
|
1237
|
-
reason: DOCUMENT_OP_REFUSAL_REASONS.DUPLICATE_RECORD_ID,
|
|
1238
|
-
message: `${repeatedRecord[1]} records in the package carry ${repeatedRecord[0]}.`
|
|
1239
|
-
});
|
|
1240
|
-
if (!sectionsInStep(body)) return new DocumentOpsContractError({
|
|
1241
|
-
reason: DOCUMENT_OP_REFUSAL_REASONS.SECTIONS_OUT_OF_STEP,
|
|
1242
|
-
message: "The body's section view does not match its blocks."
|
|
1243
|
-
});
|
|
1244
|
-
if (paragraphs.some(({ paragraph }) => holdsEmptyRecord$1(paragraph.content))) return new DocumentOpsContractError({
|
|
1245
|
-
reason: DOCUMENT_OP_REFUSAL_REASONS.EMPTY_RECORD,
|
|
1246
|
-
message: "The main story holds an empty run or text node; normalizeForOps removes them."
|
|
1247
|
-
});
|
|
1248
|
-
};
|
|
1249
|
-
/** Documents known to meet the contract: checked ones and ones an operation produced. */
|
|
1250
|
-
const MEETS_CONTRACT = /* @__PURE__ */ new WeakSet();
|
|
1251
|
-
/** Check a document against the seed contract. */
|
|
1252
|
-
const validateOpsDocument = (document) => {
|
|
1253
|
-
if (MEETS_CONTRACT.has(document)) return Result.ok(document);
|
|
1254
|
-
const found = violation(document);
|
|
1255
|
-
if (found !== void 0) return Result.err(found);
|
|
1256
|
-
MEETS_CONTRACT.add(document);
|
|
1257
|
-
return Result.ok(document);
|
|
1258
|
-
};
|
|
1259
|
-
/** Record that an operation produced `document` from one that met the contract. */
|
|
1260
|
-
const meetsContract = (document) => {
|
|
1261
|
-
MEETS_CONTRACT.add(document);
|
|
1262
|
-
};
|
|
1263
|
-
const withoutEmpty = (nodes) => {
|
|
1264
|
-
let changed = false;
|
|
1265
|
-
const out = [];
|
|
1266
|
-
for (const node of nodes) {
|
|
1267
|
-
if (isEmptyRecord(node)) {
|
|
1268
|
-
changed = true;
|
|
1269
|
-
continue;
|
|
1270
|
-
}
|
|
1271
|
-
const children = childNodes(node);
|
|
1272
|
-
const kept = children === void 0 ? children : withoutEmpty(children);
|
|
1273
|
-
if (kept !== children && kept !== void 0) {
|
|
1274
|
-
changed = true;
|
|
1275
|
-
const rebuilt = rebuildNode(node, kept);
|
|
1276
|
-
if (!isEmptyRecord(rebuilt)) out.push(rebuilt);
|
|
1277
|
-
continue;
|
|
1278
|
-
}
|
|
1279
|
-
out.push(node);
|
|
1280
|
-
}
|
|
1281
|
-
return changed ? out : nodes;
|
|
1214
|
+
const alikeDepth = (left, right) => {
|
|
1215
|
+
if (left === void 0 || right === void 0) return 0;
|
|
1216
|
+
const kind = kindOf(left);
|
|
1217
|
+
if (kind !== kindOf(right) || kind !== "characters" && kind !== "branch") return 0;
|
|
1218
|
+
if (left.type === "text" && right.type === "text") return 1;
|
|
1219
|
+
const leftChildren = childNodes(left);
|
|
1220
|
+
const rightChildren = childNodes(right);
|
|
1221
|
+
if (leftChildren === void 0 || rightChildren === void 0 || !sameOwnFields(left, right)) return 0;
|
|
1222
|
+
return 1 + alikeDepth(leftChildren.at(-1), rightChildren.at(0));
|
|
1282
1223
|
};
|
|
1283
|
-
|
|
1224
|
+
/** Two lists end to end, the records meeting there merged as far as they are alike. */
|
|
1225
|
+
const mergeAlike = (left, right) => mergeLists(left, right, alikeDepth(left.at(-1), right.at(0))) ?? panic("Records alike to a depth merge to that depth.");
|
|
1226
|
+
/** Every leaf of a list in document order: run text by text node, units, zero-width leaves. */
|
|
1227
|
+
const leafSpans = (items) => {
|
|
1284
1228
|
const out = [];
|
|
1285
|
-
|
|
1286
|
-
|
|
1287
|
-
|
|
1288
|
-
|
|
1289
|
-
...
|
|
1290
|
-
|
|
1291
|
-
});
|
|
1292
|
-
break;
|
|
1293
|
-
}
|
|
1294
|
-
case "table": {
|
|
1295
|
-
const rows = [];
|
|
1296
|
-
for (const row of block.rows) {
|
|
1297
|
-
const cells = [];
|
|
1298
|
-
for (const cell of row.cells) cells.push({
|
|
1299
|
-
...cell,
|
|
1300
|
-
content: normalizeBlocks(cell.content)
|
|
1301
|
-
});
|
|
1302
|
-
rows.push({
|
|
1303
|
-
...row,
|
|
1304
|
-
cells
|
|
1305
|
-
});
|
|
1229
|
+
const cursor = startCursor();
|
|
1230
|
+
const walk = (list, ancestors) => {
|
|
1231
|
+
for (const node of list) {
|
|
1232
|
+
if (kindOf(node) === "branch") {
|
|
1233
|
+
walk(childNodes(node) ?? [], [...ancestors, node]);
|
|
1234
|
+
continue;
|
|
1306
1235
|
}
|
|
1236
|
+
const before = {
|
|
1237
|
+
offset: cursor.position,
|
|
1238
|
+
zeroWidthBefore: cursor.zeroWidthSeen
|
|
1239
|
+
};
|
|
1240
|
+
advance(node, cursor);
|
|
1307
1241
|
out.push({
|
|
1308
|
-
|
|
1309
|
-
|
|
1310
|
-
|
|
1311
|
-
|
|
1312
|
-
|
|
1313
|
-
|
|
1314
|
-
|
|
1315
|
-
out.push({
|
|
1316
|
-
...block,
|
|
1317
|
-
content: normalizeBlocks(block.content)
|
|
1242
|
+
node,
|
|
1243
|
+
ancestors,
|
|
1244
|
+
before,
|
|
1245
|
+
after: {
|
|
1246
|
+
offset: cursor.position,
|
|
1247
|
+
zeroWidthBefore: cursor.zeroWidthSeen
|
|
1248
|
+
}
|
|
1318
1249
|
});
|
|
1319
|
-
break;
|
|
1320
|
-
case "preservedBlock":
|
|
1321
|
-
case "bookmarkStart":
|
|
1322
|
-
case "bookmarkEnd":
|
|
1323
|
-
out.push(block);
|
|
1324
|
-
break;
|
|
1325
|
-
default: return block;
|
|
1326
|
-
}
|
|
1327
|
-
return out;
|
|
1328
|
-
};
|
|
1329
|
-
/**
|
|
1330
|
-
* The document with every empty run and empty text node of the main story
|
|
1331
|
-
* removed, and its section view derived again. Nothing else changes.
|
|
1332
|
-
*/
|
|
1333
|
-
const normalizeForOps = (document) => {
|
|
1334
|
-
const body = document.package.document;
|
|
1335
|
-
return {
|
|
1336
|
-
...document,
|
|
1337
|
-
package: {
|
|
1338
|
-
...document.package,
|
|
1339
|
-
document: withBodyContent(body, normalizeBlocks(body.content))
|
|
1340
1250
|
}
|
|
1341
1251
|
};
|
|
1252
|
+
walk(items, []);
|
|
1253
|
+
return out;
|
|
1342
1254
|
};
|
|
1255
|
+
/** Gaps in document order: by offset, then by the zero-width leaves before them. */
|
|
1256
|
+
const compareGaps = (left, right) => left.offset === right.offset ? left.zeroWidthBefore - right.zeroWidthBefore : left.offset - right.offset;
|
|
1343
1257
|
//#endregion
|
|
1344
1258
|
//#region src/ops/identity.ts
|
|
1345
1259
|
/**
|
|
@@ -1519,448 +1433,2712 @@ const freshenIdentities = ({ before, after, newIds, usedElsewhere, inserted = /*
|
|
|
1519
1433
|
};
|
|
1520
1434
|
};
|
|
1521
1435
|
//#endregion
|
|
1522
|
-
//#region src/ops/
|
|
1436
|
+
//#region src/ops/refusal.ts
|
|
1523
1437
|
/**
|
|
1524
|
-
*
|
|
1525
|
-
*
|
|
1526
|
-
* SAFETY: `Object.keys` widens to `string[]`. Both callers pass a model
|
|
1527
|
-
* property set or a patch over one, object literals whose own keys are all
|
|
1528
|
-
* keys of `Formatting`.
|
|
1438
|
+
* Why an operation was not applied. A refused operation changes nothing: the
|
|
1439
|
+
* document it was applied to is returned as it was.
|
|
1529
1440
|
*/
|
|
1530
|
-
const
|
|
1531
|
-
/**
|
|
1532
|
-
|
|
1533
|
-
|
|
1534
|
-
|
|
1535
|
-
*/
|
|
1536
|
-
|
|
1537
|
-
|
|
1538
|
-
|
|
1539
|
-
|
|
1540
|
-
|
|
1541
|
-
|
|
1542
|
-
|
|
1543
|
-
|
|
1544
|
-
|
|
1545
|
-
|
|
1441
|
+
const DOCUMENT_OP_REFUSAL_REASONS = Object.freeze({
|
|
1442
|
+
/** No paragraph in the story carries the id. */
|
|
1443
|
+
BLOCK_NOT_FOUND: "blockNotFound",
|
|
1444
|
+
/** An offset is not an integer from `0` to the paragraph's length, or a range runs backwards. */
|
|
1445
|
+
INVALID_OFFSET: "invalidOffset",
|
|
1446
|
+
/** A range starts and ends in different paragraphs or stories. */
|
|
1447
|
+
CROSS_BLOCK_RANGE: "crossBlockRange",
|
|
1448
|
+
/** A position falls between the two halves of a surrogate pair. */
|
|
1449
|
+
SPLITS_SURROGATE_PAIR: "splitsSurrogatePair",
|
|
1450
|
+
/** Inserted text is empty, holds a tab or line-break character, or cannot be written to XML. */
|
|
1451
|
+
INVALID_TEXT: "invalidText",
|
|
1452
|
+
/**
|
|
1453
|
+
* A paragraph id the operation (or its inverse) creates is not eight hex
|
|
1454
|
+
* digits below `0x80000000`, or is the reserved `00000000`.
|
|
1455
|
+
*/
|
|
1456
|
+
INVALID_BLOCK_ID: "invalidBlockId",
|
|
1457
|
+
/**
|
|
1458
|
+
* An id the operation brings in (a paragraph id, or a revision or
|
|
1459
|
+
* content-control id a replacement carries) is already used in the package.
|
|
1460
|
+
*/
|
|
1461
|
+
ID_COLLISION: "idCollision",
|
|
1462
|
+
/** The paragraphs to join or replace are not adjacent siblings of one container. */
|
|
1463
|
+
NOT_ADJACENT: "notAdjacent",
|
|
1464
|
+
/**
|
|
1465
|
+
* The operation would move or remove a section break (joining a paragraph
|
|
1466
|
+
* that ends a section, replacing paragraphs across or at one); that is a
|
|
1467
|
+
* section operation.
|
|
1468
|
+
*/
|
|
1469
|
+
SECTION_BOUNDARY: "sectionBoundary",
|
|
1470
|
+
/** Direct text would land inside tracked-deleted or moved-away content. */
|
|
1471
|
+
INSIDE_TRACKED_DELETION: "insideTrackedDeletion",
|
|
1472
|
+
/**
|
|
1473
|
+
* The operation cuts a record carrying a revision or content-control id in
|
|
1474
|
+
* two and names too few `newIds` for the halves after the first.
|
|
1475
|
+
*/
|
|
1476
|
+
NEEDS_NEW_IDS: "needsNewIds",
|
|
1477
|
+
/** A new revision or content-control id (a stamp's too) is not an integer from 0 to 2^31 - 1. */
|
|
1478
|
+
INVALID_NEW_ID: "invalidNewId",
|
|
1479
|
+
/**
|
|
1480
|
+
* The operation would merge two records carrying the same revision or
|
|
1481
|
+
* content-control id: nothing could tell them apart again to undo it.
|
|
1482
|
+
*/
|
|
1483
|
+
SHARED_ID: "sharedId",
|
|
1484
|
+
/**
|
|
1485
|
+
* What the operation expects (paragraphs to replace, a slice to delete,
|
|
1486
|
+
* the values a patch replaced) is not what is there.
|
|
1487
|
+
*/
|
|
1488
|
+
STALE: "stale",
|
|
1489
|
+
/**
|
|
1490
|
+
* The records at a position are not the shape the operation needs: an open
|
|
1491
|
+
* end meets a record it cannot merge with, records to join differ, a slice
|
|
1492
|
+
* would merge records that were separate, or there are fewer records to cut
|
|
1493
|
+
* than the operation names.
|
|
1494
|
+
*/
|
|
1495
|
+
STRUCTURE_MISMATCH: "structureMismatch",
|
|
1496
|
+
/** Inserted content is empty, or holds an empty run or text node. */
|
|
1497
|
+
EMPTY_CONTENT: "emptyContent",
|
|
1498
|
+
/** A replacement names no paragraph to replace, or none to put in its place. */
|
|
1499
|
+
EMPTY_BLOCK_LIST: "emptyBlockList",
|
|
1500
|
+
/**
|
|
1501
|
+
* The operation would record a tracked change over one the target already
|
|
1502
|
+
* carries (a paragraph mark, a split that would drop a paragraph's property
|
|
1503
|
+
* change), or would give a tracked split a mark of its own.
|
|
1504
|
+
*/
|
|
1505
|
+
REVISION_CONFLICT: "revisionConflict",
|
|
1506
|
+
/** The operation would put a mark on a paragraph that ends its story body or table cell. */
|
|
1507
|
+
CONTAINER_FINAL_MARK: "containerFinalMark",
|
|
1508
|
+
/**
|
|
1509
|
+
* No tracked change can record the edit, or no resolution op can carry it
|
|
1510
|
+
* out yet: comment boundaries in a tracked range, the paragraph mark's run
|
|
1511
|
+
* properties, a join at a section break, a record inside a field.
|
|
1512
|
+
*/
|
|
1513
|
+
UNTRACKABLE: "untrackable",
|
|
1514
|
+
/** Seed contract: a main-story paragraph has no `paraId`. */
|
|
1515
|
+
MISSING_BLOCK_ID: "missingBlockId",
|
|
1516
|
+
/** Seed contract: two paragraphs in the package carry one id. */
|
|
1517
|
+
DUPLICATE_BLOCK_ID: "duplicateBlockId",
|
|
1518
|
+
/** Seed contract: two records in the package carry one revision or content-control id. */
|
|
1519
|
+
DUPLICATE_RECORD_ID: "duplicateRecordId",
|
|
1520
|
+
/** Seed contract: the body's section view does not match its blocks. */
|
|
1521
|
+
SECTIONS_OUT_OF_STEP: "sectionsOutOfStep",
|
|
1522
|
+
/** Seed contract: the main story holds an empty run or empty text node. */
|
|
1523
|
+
EMPTY_RECORD: "emptyRecord"
|
|
1524
|
+
});
|
|
1525
|
+
/** An operation the document cannot take as stated. */
|
|
1526
|
+
var DocumentOpRefusal = class extends TaggedError("DocumentOpRefusal") {};
|
|
1527
|
+
//#endregion
|
|
1528
|
+
//#region src/ops/review.ts
|
|
1529
|
+
/** The tracked-change metadata a stamp writes on the records it creates. */
|
|
1530
|
+
const stampInfo = (stamp) => {
|
|
1531
|
+
const info = {
|
|
1532
|
+
id: stamp.id,
|
|
1533
|
+
author: stamp.author,
|
|
1534
|
+
date: stamp.date
|
|
1535
|
+
};
|
|
1536
|
+
if (stamp.initials !== void 0) info.initials = stamp.initials;
|
|
1537
|
+
return info;
|
|
1538
|
+
};
|
|
1539
|
+
/** Whether a record's metadata is the stamp's, whatever its id. */
|
|
1540
|
+
const carriesStamp = (info, stamp) => structurallyEqual({
|
|
1541
|
+
...info,
|
|
1542
|
+
id: 0
|
|
1543
|
+
}, {
|
|
1544
|
+
...stampInfo(stamp),
|
|
1545
|
+
id: 0
|
|
1546
|
+
});
|
|
1547
|
+
const isTrackedWrapper = (node) => node.type === "insertion" || node.type === "deletion" || node.type === "moveFrom" || node.type === "moveTo";
|
|
1548
|
+
/** Tracked content on its way in: an insertion or the destination of a move. */
|
|
1549
|
+
const isAddedRevision = (node) => node.type === "insertion" || node.type === "moveTo";
|
|
1550
|
+
/** Tracked content on its way out: a deletion or the source of a move. */
|
|
1551
|
+
const isRemovedRevisionNode = (node) => node.type === "deletion" || node.type === "moveFrom";
|
|
1552
|
+
/** The tracked-change metadata of every record a paragraph and its content carry. */
|
|
1553
|
+
const revisionInfosOf = (paragraph) => {
|
|
1554
|
+
const out = [];
|
|
1555
|
+
for (const change of paragraph.propertyChanges ?? []) out.push(change.info);
|
|
1556
|
+
if (paragraph.pPrMark !== void 0) out.push(paragraph.pPrMark.info);
|
|
1557
|
+
const visit = (nodes) => {
|
|
1558
|
+
for (const node of nodes) {
|
|
1559
|
+
if (isTrackedWrapper(node)) out.push(node.info);
|
|
1560
|
+
else if (node.type === "run") for (const change of node.propertyChanges ?? []) out.push(change.info);
|
|
1561
|
+
visit(childNodes(node) ?? []);
|
|
1562
|
+
}
|
|
1563
|
+
};
|
|
1564
|
+
visit(paragraph.content);
|
|
1565
|
+
return out;
|
|
1546
1566
|
};
|
|
1547
1567
|
/**
|
|
1548
|
-
* The
|
|
1549
|
-
*
|
|
1568
|
+
* The revision ids of the records carrying the stamp that `known` does not
|
|
1569
|
+
* name (slot keys): the records an operation created, in document order.
|
|
1550
1570
|
*/
|
|
1551
|
-
const
|
|
1552
|
-
const
|
|
1553
|
-
for (const
|
|
1554
|
-
|
|
1555
|
-
|
|
1556
|
-
|
|
1571
|
+
const stampedRevisionIds = (paragraphs, stamp, known) => {
|
|
1572
|
+
const out = [];
|
|
1573
|
+
for (const paragraph of paragraphs) for (const info of revisionInfosOf(paragraph)) {
|
|
1574
|
+
const key = slotKey({
|
|
1575
|
+
space: IDENTITY_SPACES.REVISION,
|
|
1576
|
+
id: info.id
|
|
1577
|
+
});
|
|
1578
|
+
if (carriesStamp(info, stamp) && !known.has(key) && !out.includes(info.id)) out.push(info.id);
|
|
1557
1579
|
}
|
|
1558
|
-
return
|
|
1580
|
+
return out;
|
|
1559
1581
|
};
|
|
1560
|
-
|
|
1561
|
-
|
|
1582
|
+
const MARK_KEYS = new Set(PARAGRAPH_MARK_FORMATTING_KEYS);
|
|
1583
|
+
/** Whether a patch or property set names a key of the paragraph mark's run properties. */
|
|
1584
|
+
const namesMarkFormatting = (formatting) => Object.entries(formatting).some(([key, value]) => value !== void 0 && MARK_KEYS.has(key));
|
|
1585
|
+
const pick = (formatting, keep) => {
|
|
1586
|
+
if (formatting === void 0) return;
|
|
1587
|
+
return Object.fromEntries(Object.entries(formatting).filter(([key]) => keep(key)));
|
|
1588
|
+
};
|
|
1589
|
+
/** The paragraph properties a `w:pPrChange` records: all but the mark's run properties. */
|
|
1590
|
+
const paragraphPropertiesOf = (formatting) => pick(formatting, (key) => !MARK_KEYS.has(key));
|
|
1591
|
+
/** The paragraph mark's run properties of a property set; `undefined` when it states none. */
|
|
1592
|
+
const markFormattingOf = (formatting) => {
|
|
1593
|
+
const marks = pick(formatting, (key) => MARK_KEYS.has(key));
|
|
1594
|
+
return marks === void 0 || Object.keys(marks).length === 0 ? void 0 : marks;
|
|
1595
|
+
};
|
|
1596
|
+
/** Whether two property sets state the same paragraph properties. */
|
|
1597
|
+
const sameParagraphProperties = (left, right) => structurallyEqual(paragraphPropertiesOf(left) ?? {}, paragraphPropertiesOf(right) ?? {});
|
|
1598
|
+
/** Whether two property sets state the same run properties for the paragraph mark. */
|
|
1599
|
+
const sameMarkFormatting = (left, right) => structurallyEqual(markFormattingOf(left) ?? {}, markFormattingOf(right) ?? {});
|
|
1562
1600
|
/**
|
|
1563
|
-
*
|
|
1564
|
-
*
|
|
1565
|
-
*
|
|
1566
|
-
* path it changed and hands back every child it did not change as the same
|
|
1567
|
-
* object, so an untouched run is `===` to its input.
|
|
1601
|
+
* The paragraph properties of `properties` with the mark's run properties of
|
|
1602
|
+
* `marks`. `undefined` when the first is absent and the second states no mark
|
|
1603
|
+
* properties.
|
|
1568
1604
|
*/
|
|
1569
|
-
|
|
1570
|
-
const
|
|
1571
|
-
|
|
1572
|
-
|
|
1573
|
-
|
|
1605
|
+
const withMarkFormatting = (properties, marks) => {
|
|
1606
|
+
const markFormatting = markFormattingOf(marks);
|
|
1607
|
+
if (properties === void 0 && markFormatting === void 0) return;
|
|
1608
|
+
return {
|
|
1609
|
+
...paragraphPropertiesOf(properties),
|
|
1610
|
+
...markFormatting
|
|
1611
|
+
};
|
|
1612
|
+
};
|
|
1613
|
+
/** A tracked paragraph property change from `previous`. */
|
|
1614
|
+
const paragraphPropertyChange = (info, previous) => {
|
|
1615
|
+
const change = {
|
|
1616
|
+
type: "paragraphPropertyChange",
|
|
1617
|
+
info
|
|
1618
|
+
};
|
|
1619
|
+
const recorded = paragraphPropertiesOf(previous);
|
|
1620
|
+
if (recorded !== void 0) change.previousFormatting = recorded;
|
|
1621
|
+
return change;
|
|
1622
|
+
};
|
|
1623
|
+
/** The review fields a paragraph states, each only when present. */
|
|
1624
|
+
const reviewFieldsOf = (paragraph) => {
|
|
1625
|
+
const fields = {};
|
|
1626
|
+
if (paragraph.formatting !== void 0) fields.formatting = paragraph.formatting;
|
|
1627
|
+
if (paragraph.propertyChanges !== void 0) fields.propertyChanges = paragraph.propertyChanges;
|
|
1628
|
+
if (paragraph.pPrMark !== void 0) fields.pPrMark = paragraph.pPrMark;
|
|
1629
|
+
return fields;
|
|
1630
|
+
};
|
|
1631
|
+
/** The paragraph with exactly these review fields. */
|
|
1632
|
+
const withReviewFields = (paragraph, review) => {
|
|
1633
|
+
const next = { ...paragraph };
|
|
1634
|
+
delete next.formatting;
|
|
1635
|
+
delete next.propertyChanges;
|
|
1636
|
+
delete next.pPrMark;
|
|
1637
|
+
if (review.formatting !== void 0) next.formatting = review.formatting;
|
|
1638
|
+
if (review.propertyChanges !== void 0) next.propertyChanges = review.propertyChanges;
|
|
1639
|
+
if (review.pPrMark !== void 0) next.pPrMark = review.pPrMark;
|
|
1574
1640
|
return next;
|
|
1575
1641
|
};
|
|
1576
|
-
|
|
1577
|
-
|
|
1578
|
-
|
|
1642
|
+
/** Which tracked change a wrap records. */
|
|
1643
|
+
const WRAP_KINDS = Object.freeze({
|
|
1644
|
+
INSERTION: "insertion",
|
|
1645
|
+
DELETION: "deletion"
|
|
1579
1646
|
});
|
|
1580
|
-
|
|
1581
|
-
|
|
1582
|
-
|
|
1583
|
-
|
|
1584
|
-
|
|
1585
|
-
|
|
1586
|
-
|
|
1587
|
-
|
|
1588
|
-
type: "text",
|
|
1589
|
-
text
|
|
1590
|
-
}];
|
|
1591
|
-
return formatting === void 0 || Object.keys(formatting).length === 0 ? {
|
|
1592
|
-
type: "run",
|
|
1593
|
-
content
|
|
1594
|
-
} : {
|
|
1595
|
-
type: "run",
|
|
1596
|
-
formatting,
|
|
1597
|
-
content
|
|
1598
|
-
};
|
|
1647
|
+
const spanningAction = (node, kind, stamp) => {
|
|
1648
|
+
if (node.type === "inlineSdt" || node.type === "inlineWrapper") return "descend";
|
|
1649
|
+
if (isTrackedWrapper(node) && isAddedRevision(node)) {
|
|
1650
|
+
if (kind === WRAP_KINDS.DELETION) return "descend";
|
|
1651
|
+
return carriesStamp(node.info, stamp) ? "keep" : "cut";
|
|
1652
|
+
}
|
|
1653
|
+
if (isRemovedRevisionNode(node)) return kind === WRAP_KINDS.DELETION ? "keep" : "refuse";
|
|
1654
|
+
return "cut";
|
|
1599
1655
|
};
|
|
1600
|
-
|
|
1601
|
-
|
|
1602
|
-
|
|
1603
|
-
|
|
1604
|
-
|
|
1605
|
-
|
|
1606
|
-
|
|
1607
|
-
|
|
1608
|
-
|
|
1609
|
-
|
|
1610
|
-
|
|
1611
|
-
|
|
1612
|
-
|
|
1613
|
-
|
|
1614
|
-
|
|
1656
|
+
const coveredAction = (node, kind) => {
|
|
1657
|
+
if (isCommentAnchor(node)) return "refuse";
|
|
1658
|
+
if (kind === WRAP_KINDS.DELETION) {
|
|
1659
|
+
if (isRemovedRevisionNode(node)) return "skip";
|
|
1660
|
+
if (isAddedRevision(node)) return "descend";
|
|
1661
|
+
}
|
|
1662
|
+
return "wrap";
|
|
1663
|
+
};
|
|
1664
|
+
/** Merge a created wrapper into a neighbour carrying the same stamp: the left one, else the right. */
|
|
1665
|
+
const mergeWithStampedNeighbour = (out, index, state) => {
|
|
1666
|
+
const wrapper = out[index];
|
|
1667
|
+
if (wrapper === void 0 || !isTrackedWrapper(wrapper)) return;
|
|
1668
|
+
const matching = (neighbour) => neighbour !== void 0 && isTrackedWrapper(neighbour) && neighbour.type === wrapper.type && !state.created.has(neighbour) && carriesStamp(neighbour.info, state.stamp);
|
|
1669
|
+
const left = out[index - 1];
|
|
1670
|
+
if (matching(left)) {
|
|
1671
|
+
const merged = mergeLists([left], [wrapper], 1);
|
|
1672
|
+
if (merged !== void 0) {
|
|
1673
|
+
state.created.delete(wrapper);
|
|
1674
|
+
out.splice(index - 1, 2, ...merged);
|
|
1615
1675
|
}
|
|
1616
|
-
|
|
1676
|
+
return;
|
|
1677
|
+
}
|
|
1678
|
+
const right = out[index + 1];
|
|
1679
|
+
if (matching(right)) {
|
|
1680
|
+
const merged = mergeLists([wrapper], [right], 1, {
|
|
1681
|
+
mode: "exact",
|
|
1682
|
+
identity: "second"
|
|
1683
|
+
});
|
|
1684
|
+
if (merged !== void 0) {
|
|
1685
|
+
state.created.delete(wrapper);
|
|
1686
|
+
out.splice(index, 2, ...merged);
|
|
1687
|
+
}
|
|
1688
|
+
}
|
|
1617
1689
|
};
|
|
1618
|
-
|
|
1619
|
-
|
|
1620
|
-
|
|
1621
|
-
|
|
1622
|
-
|
|
1623
|
-
|
|
1624
|
-
|
|
1625
|
-
|
|
1626
|
-
|
|
1627
|
-
|
|
1628
|
-
});
|
|
1629
|
-
return whole === void 0 ? void 0 : asParagraphContent(whole);
|
|
1690
|
+
const trackedContent = (nodes, state) => {
|
|
1691
|
+
const out = [];
|
|
1692
|
+
for (const node of nodes) {
|
|
1693
|
+
if (!isParagraphContent(node) || !isTrackedChild(node)) {
|
|
1694
|
+
state.refusal = DOCUMENT_OP_REFUSAL_REASONS.UNTRACKABLE;
|
|
1695
|
+
return;
|
|
1696
|
+
}
|
|
1697
|
+
out.push(node);
|
|
1698
|
+
}
|
|
1699
|
+
return out;
|
|
1630
1700
|
};
|
|
1631
|
-
/**
|
|
1632
|
-
const
|
|
1633
|
-
const
|
|
1634
|
-
const
|
|
1635
|
-
|
|
1636
|
-
|
|
1637
|
-
|
|
1701
|
+
/** Group the covered entries of a list into wrappers; `undefined` once refused. */
|
|
1702
|
+
const groupCovered = (entries, state) => {
|
|
1703
|
+
const out = [];
|
|
1704
|
+
const wrapperAt = [];
|
|
1705
|
+
let stretch = [];
|
|
1706
|
+
const flush = () => {
|
|
1707
|
+
if (stretch.length === 0) return true;
|
|
1708
|
+
const content = trackedContent(stretch, state);
|
|
1709
|
+
if (content === void 0) return false;
|
|
1710
|
+
const info = stampInfo(state.stamp);
|
|
1711
|
+
const wrapper = state.kind === WRAP_KINDS.INSERTION ? {
|
|
1712
|
+
type: "insertion",
|
|
1713
|
+
info,
|
|
1714
|
+
content
|
|
1715
|
+
} : {
|
|
1716
|
+
type: "deletion",
|
|
1717
|
+
info,
|
|
1718
|
+
content
|
|
1719
|
+
};
|
|
1720
|
+
state.created.add(wrapper);
|
|
1721
|
+
state.recorded = true;
|
|
1722
|
+
wrapperAt.push(out.length);
|
|
1723
|
+
out.push(wrapper);
|
|
1724
|
+
stretch = [];
|
|
1725
|
+
return true;
|
|
1638
1726
|
};
|
|
1727
|
+
for (const { node, covered } of entries) {
|
|
1728
|
+
const action = covered ? coveredAction(node, state.kind) : "skip";
|
|
1729
|
+
switch (action) {
|
|
1730
|
+
case "refuse":
|
|
1731
|
+
state.refusal = DOCUMENT_OP_REFUSAL_REASONS.UNTRACKABLE;
|
|
1732
|
+
return;
|
|
1733
|
+
case "skip":
|
|
1734
|
+
if (!flush()) return void 0;
|
|
1735
|
+
out.push(node);
|
|
1736
|
+
break;
|
|
1737
|
+
case "descend": {
|
|
1738
|
+
if (!flush()) return void 0;
|
|
1739
|
+
const children = childNodes(node) ?? [];
|
|
1740
|
+
const wrapped = groupCovered(children.map((child) => ({
|
|
1741
|
+
node: child,
|
|
1742
|
+
covered: true
|
|
1743
|
+
})), state);
|
|
1744
|
+
if (wrapped === void 0) return void 0;
|
|
1745
|
+
out.push(rebuildNode(node, wrapped));
|
|
1746
|
+
break;
|
|
1747
|
+
}
|
|
1748
|
+
case "wrap":
|
|
1749
|
+
stretch.push(node);
|
|
1750
|
+
break;
|
|
1751
|
+
default: return action;
|
|
1752
|
+
}
|
|
1753
|
+
}
|
|
1754
|
+
if (!flush()) return void 0;
|
|
1755
|
+
for (const index of wrapperAt.toReversed()) mergeWithStampedNeighbour(out, index, state);
|
|
1756
|
+
return out;
|
|
1757
|
+
};
|
|
1758
|
+
const wrapList = (nodes, cursor, state) => {
|
|
1759
|
+
const entries = [];
|
|
1760
|
+
for (const node of nodes) {
|
|
1761
|
+
const start = { ...cursor };
|
|
1762
|
+
const measured = partitionNode(node, state.gaps, cursor);
|
|
1763
|
+
if (measured.min === measured.max) {
|
|
1764
|
+
entries.push({
|
|
1765
|
+
node,
|
|
1766
|
+
covered: measured.min === 1
|
|
1767
|
+
});
|
|
1768
|
+
continue;
|
|
1769
|
+
}
|
|
1770
|
+
const action = spanningAction(node, state.kind, state.stamp);
|
|
1771
|
+
switch (action) {
|
|
1772
|
+
case "refuse":
|
|
1773
|
+
state.refusal = DOCUMENT_OP_REFUSAL_REASONS.INSIDE_TRACKED_DELETION;
|
|
1774
|
+
return;
|
|
1775
|
+
case "keep":
|
|
1776
|
+
entries.push({
|
|
1777
|
+
node,
|
|
1778
|
+
covered: false
|
|
1779
|
+
});
|
|
1780
|
+
break;
|
|
1781
|
+
case "descend": {
|
|
1782
|
+
const end = { ...cursor };
|
|
1783
|
+
cursor.position = start.position;
|
|
1784
|
+
cursor.zeroWidthSeen = start.zeroWidthSeen;
|
|
1785
|
+
const children = wrapList(childNodes(node) ?? [], cursor, state);
|
|
1786
|
+
if (children === void 0) return void 0;
|
|
1787
|
+
cursor.position = end.position;
|
|
1788
|
+
cursor.zeroWidthSeen = end.zeroWidthSeen;
|
|
1789
|
+
entries.push({
|
|
1790
|
+
node: rebuildNode(node, children),
|
|
1791
|
+
covered: false
|
|
1792
|
+
});
|
|
1793
|
+
break;
|
|
1794
|
+
}
|
|
1795
|
+
case "cut":
|
|
1796
|
+
for (const [region, piece] of measured.pieces) {
|
|
1797
|
+
const unwrap = region === 1 && state.kind === WRAP_KINDS.INSERTION && isTrackedWrapper(piece);
|
|
1798
|
+
for (const part of unwrap ? childNodes(piece) ?? [] : [piece]) entries.push({
|
|
1799
|
+
node: part,
|
|
1800
|
+
covered: region === 1
|
|
1801
|
+
});
|
|
1802
|
+
}
|
|
1803
|
+
break;
|
|
1804
|
+
default: return action;
|
|
1805
|
+
}
|
|
1806
|
+
}
|
|
1807
|
+
return groupCovered(entries, state);
|
|
1639
1808
|
};
|
|
1640
1809
|
/**
|
|
1641
|
-
*
|
|
1642
|
-
*
|
|
1643
|
-
*
|
|
1644
|
-
* insertion merged records that were separate before it: no deletion could
|
|
1645
|
-
* tell them apart again, so such an insertion is refused.
|
|
1810
|
+
* Wrap the leaves between two gaps in a tracked change carrying the stamp.
|
|
1811
|
+
* `unchanged` when there is nothing to record: every leaf is already
|
|
1812
|
+
* deleted, or an insertion sits inside a wrapper carrying the same stamp.
|
|
1646
1813
|
*/
|
|
1647
|
-
const
|
|
1648
|
-
const
|
|
1649
|
-
|
|
1650
|
-
|
|
1651
|
-
|
|
1652
|
-
|
|
1653
|
-
|
|
1814
|
+
const wrapTracked = ({ items, from, to, kind, stamp }) => {
|
|
1815
|
+
const state = {
|
|
1816
|
+
gaps: [from, to],
|
|
1817
|
+
kind,
|
|
1818
|
+
stamp,
|
|
1819
|
+
created: /* @__PURE__ */ new Set(),
|
|
1820
|
+
recorded: false,
|
|
1821
|
+
refusal: void 0
|
|
1822
|
+
};
|
|
1823
|
+
const content = wrapList(items, startCursor(), state);
|
|
1824
|
+
if (content === void 0) return {
|
|
1825
|
+
kind: "refused",
|
|
1826
|
+
reason: state.refusal ?? DOCUMENT_OP_REFUSAL_REASONS.UNTRACKABLE
|
|
1827
|
+
};
|
|
1828
|
+
if (!state.recorded) return { kind: "unchanged" };
|
|
1829
|
+
const out = [];
|
|
1830
|
+
for (const node of content) {
|
|
1831
|
+
if (!isParagraphContent(node)) return {
|
|
1832
|
+
kind: "refused",
|
|
1833
|
+
reason: DOCUMENT_OP_REFUSAL_REASONS.UNTRACKABLE
|
|
1834
|
+
};
|
|
1835
|
+
out.push(node);
|
|
1836
|
+
}
|
|
1837
|
+
return {
|
|
1838
|
+
kind: "wrapped",
|
|
1839
|
+
content: out
|
|
1654
1840
|
};
|
|
1655
1841
|
};
|
|
1656
|
-
|
|
1657
|
-
|
|
1842
|
+
//#endregion
|
|
1843
|
+
//#region src/ops/blockDeletion.ts
|
|
1844
|
+
/** Whole-paragraph deletion within one block list, including its final paragraph. */
|
|
1845
|
+
const deleteBlocks = ({ document, op, applyOps }) => {
|
|
1846
|
+
const refuse = (reason, message) => Result.err(new DocumentOpRefusal({
|
|
1847
|
+
reason,
|
|
1848
|
+
message,
|
|
1849
|
+
opType: op.type
|
|
1850
|
+
}));
|
|
1851
|
+
if (op.blockIds.length === 0) return refuse(DOCUMENT_OP_REFUSAL_REASONS.EMPTY_BLOCK_LIST, "No paragraphs to delete.");
|
|
1852
|
+
const body = storyBody(document, op.story);
|
|
1853
|
+
const paragraphs = storyParagraphs(body);
|
|
1854
|
+
const found = [];
|
|
1855
|
+
for (const id of op.blockIds) {
|
|
1856
|
+
const location = paragraphs.find(({ paragraph }) => idKey(paragraph.paraId ?? "") === idKey(id));
|
|
1857
|
+
if (location === void 0) return refuse(DOCUMENT_OP_REFUSAL_REASONS.BLOCK_NOT_FOUND, "A selected paragraph does not exist.");
|
|
1858
|
+
found.push(location);
|
|
1859
|
+
}
|
|
1860
|
+
const first = found.at(0);
|
|
1861
|
+
const last = found.at(-1);
|
|
1862
|
+
if (first === void 0 || last === void 0) return refuse(DOCUMENT_OP_REFUSAL_REASONS.EMPTY_BLOCK_LIST, "No paragraphs to delete.");
|
|
1863
|
+
if (!found.every((location, index) => sameBlockList(location.list, first.list) && location.index === first.index + index)) return refuse(DOCUMENT_OP_REFUSAL_REASONS.NOT_ADJACENT, "Selected paragraphs must be adjacent in one block list.");
|
|
1864
|
+
const list = blockListAt(body.content, first.list);
|
|
1865
|
+
const following = list[last.index + 1];
|
|
1866
|
+
const terminal = last.index === list.length - 1;
|
|
1867
|
+
if (following?.type !== "paragraph" && !terminal) return refuse(DOCUMENT_OP_REFUSAL_REASONS.UNTRACKABLE, "Deletion requires a following paragraph or a container-final paragraph.");
|
|
1868
|
+
const preceding = terminal ? list[first.index - 1] : void 0;
|
|
1869
|
+
const previous = preceding?.type === "paragraph" ? preceding : void 0;
|
|
1870
|
+
const selected = found.map(({ paragraph }) => paragraph);
|
|
1871
|
+
const affected = previous === void 0 ? selected : [previous, ...selected];
|
|
1872
|
+
if (affected.some(({ sectionProperties }) => sectionProperties !== void 0)) return refuse(DOCUMENT_OP_REFUSAL_REASONS.UNTRACKABLE, "Deleting section boundaries requires section operations.");
|
|
1873
|
+
const replacement = (blocks) => ({
|
|
1874
|
+
type: DOCUMENT_OP_TYPES.REPLACE_BLOCKS,
|
|
1875
|
+
story: op.story,
|
|
1876
|
+
expected: following?.type === "paragraph" ? [...selected, following] : affected,
|
|
1877
|
+
blocks
|
|
1878
|
+
});
|
|
1879
|
+
const finalReview = reviewFieldsOf(last.paragraph);
|
|
1880
|
+
if (previous !== void 0 && paragraphLength(previous) > 0) {
|
|
1881
|
+
const formatting = withMarkFormatting(paragraphPropertiesOf(previous.formatting), last.paragraph.formatting);
|
|
1882
|
+
delete finalReview.formatting;
|
|
1883
|
+
if (formatting !== void 0) finalReview.formatting = formatting;
|
|
1884
|
+
}
|
|
1885
|
+
const survivor = withReviewFields({
|
|
1886
|
+
...last.paragraph,
|
|
1887
|
+
content: previous?.content ?? []
|
|
1888
|
+
}, finalReview);
|
|
1889
|
+
const direct = applyOps(document, [replacement(following?.type === "paragraph" ? [following] : [survivor])]);
|
|
1890
|
+
if (direct.isErr()) return refuse(direct.error.reason, direct.error.message);
|
|
1891
|
+
if (op.revision === void 0) return Result.ok({
|
|
1892
|
+
...direct.value,
|
|
1893
|
+
touched: {
|
|
1894
|
+
modified: terminal ? [last.paragraph.paraId ?? ""] : [],
|
|
1895
|
+
inserted: [],
|
|
1896
|
+
removed: affected.filter((paragraph) => !terminal || paragraph !== last.paragraph).map(({ paraId }) => paraId ?? "")
|
|
1897
|
+
}
|
|
1898
|
+
});
|
|
1899
|
+
if (affected.some(({ pPrMark, propertyChanges }) => pPrMark !== void 0 || (propertyChanges?.length ?? 0) > 0)) return refuse(DOCUMENT_OP_REFUSAL_REASONS.REVISION_CONFLICT, "Deletion would overwrite a paragraph review record.");
|
|
1900
|
+
if (identityKeysIn(selected.map(({ content }) => ({ content }))).length > 0) return refuse(DOCUMENT_OP_REFUSAL_REASONS.UNTRACKABLE, "Tracked paragraph deletion of identified review or content-control content is unsupported.");
|
|
1901
|
+
const selectedSet = new Set(selected);
|
|
1902
|
+
const tracked = [];
|
|
1903
|
+
for (const paragraph of affected) {
|
|
1904
|
+
let next = paragraph;
|
|
1905
|
+
if (selectedSet.has(paragraph)) {
|
|
1906
|
+
const wrapped = wrapTracked({
|
|
1907
|
+
items: paragraph.content,
|
|
1908
|
+
from: {
|
|
1909
|
+
offset: 0,
|
|
1910
|
+
zeroWidthBefore: 0
|
|
1911
|
+
},
|
|
1912
|
+
to: {
|
|
1913
|
+
offset: paragraphLength(paragraph),
|
|
1914
|
+
zeroWidthBefore: Number.MAX_SAFE_INTEGER
|
|
1915
|
+
},
|
|
1916
|
+
kind: WRAP_KINDS.DELETION,
|
|
1917
|
+
stamp: op.revision
|
|
1918
|
+
});
|
|
1919
|
+
if (wrapped.kind === "refused") return refuse(wrapped.reason, "The selected paragraph content cannot be tracked.");
|
|
1920
|
+
if (wrapped.kind === "wrapped") next = {
|
|
1921
|
+
...paragraph,
|
|
1922
|
+
content: wrapped.content
|
|
1923
|
+
};
|
|
1924
|
+
}
|
|
1925
|
+
if (!terminal || paragraph !== last.paragraph) next = {
|
|
1926
|
+
...next,
|
|
1927
|
+
pPrMark: {
|
|
1928
|
+
kind: "del",
|
|
1929
|
+
info: stampInfo(op.revision)
|
|
1930
|
+
}
|
|
1931
|
+
};
|
|
1932
|
+
else if (!sameParagraphProperties(next.formatting, survivor.formatting)) {
|
|
1933
|
+
const review = reviewFieldsOf(next);
|
|
1934
|
+
delete review.formatting;
|
|
1935
|
+
if (survivor.formatting !== void 0) review.formatting = survivor.formatting;
|
|
1936
|
+
review.propertyChanges = [paragraphPropertyChange(stampInfo(op.revision), next.formatting)];
|
|
1937
|
+
next = withReviewFields(next, review);
|
|
1938
|
+
}
|
|
1939
|
+
tracked.push(next);
|
|
1940
|
+
}
|
|
1941
|
+
const beforeKeys = new Set(identityKeysIn(affected));
|
|
1942
|
+
const fresh = freshenIdentities({
|
|
1943
|
+
before: affected,
|
|
1944
|
+
after: tracked,
|
|
1945
|
+
newIds: op.newIds ?? {},
|
|
1946
|
+
usedElsewhere: () => new Set(packageIdentityKeys(document.package).filter((key) => !beforeKeys.has(key)))
|
|
1947
|
+
});
|
|
1948
|
+
switch (fresh.kind) {
|
|
1949
|
+
case "needsIds": return refuse(DOCUMENT_OP_REFUSAL_REASONS.NEEDS_NEW_IDS, "Paragraph deletion needs more revision ids.");
|
|
1950
|
+
case "invalidId": return refuse(DOCUMENT_OP_REFUSAL_REASONS.INVALID_NEW_ID, "Paragraph deletion received an invalid revision id.");
|
|
1951
|
+
case "fresh": {
|
|
1952
|
+
const result = applyOps(document, [replacement(following?.type === "paragraph" ? [...fresh.paragraphs, following] : fresh.paragraphs)]);
|
|
1953
|
+
if (result.isErr()) return refuse(result.error.reason, result.error.message);
|
|
1954
|
+
return Result.ok({
|
|
1955
|
+
...result.value,
|
|
1956
|
+
touched: {
|
|
1957
|
+
modified: tracked.filter((paragraph, index) => paragraph !== affected[index]).map(({ paraId }) => paraId ?? ""),
|
|
1958
|
+
inserted: [],
|
|
1959
|
+
removed: []
|
|
1960
|
+
}
|
|
1961
|
+
});
|
|
1962
|
+
}
|
|
1963
|
+
default: return fresh;
|
|
1964
|
+
}
|
|
1965
|
+
};
|
|
1966
|
+
//#endregion
|
|
1967
|
+
//#region src/ops/blockInsertion.ts
|
|
1968
|
+
/** Paragraph insertion, isolated from the inline and table operation implementations. */
|
|
1969
|
+
const insertBlocks = ({ document, op, applyOps }) => {
|
|
1970
|
+
const refuse = (reason, message) => Result.err(new DocumentOpRefusal({
|
|
1971
|
+
reason,
|
|
1972
|
+
message,
|
|
1973
|
+
opType: op.type
|
|
1974
|
+
}));
|
|
1975
|
+
if (op.blocks.length === 0) return refuse(DOCUMENT_OP_REFUSAL_REASONS.EMPTY_BLOCK_LIST, "No paragraphs to insert.");
|
|
1976
|
+
if (op.at.type !== "before" && op.at.type !== "after") return refuse(DOCUMENT_OP_REFUSAL_REASONS.STRUCTURE_MISMATCH, "Insert before or after a paragraph.");
|
|
1977
|
+
const body = storyBody(document, op.story);
|
|
1978
|
+
const location = storyParagraphs(body).find(({ paragraph }) => idKey(paragraph.paraId ?? "") === idKey(op.at.blockId));
|
|
1979
|
+
if (location === void 0) return refuse(DOCUMENT_OP_REFUSAL_REASONS.BLOCK_NOT_FOUND, "The insertion anchor does not exist.");
|
|
1980
|
+
if (op.blocks.some(({ sectionProperties }) => sectionProperties !== void 0)) return refuse(DOCUMENT_OP_REFUSAL_REASONS.UNTRACKABLE, "Inserting section breaks requires section operations.");
|
|
1981
|
+
const following = blockListAt(body.content, location.list)[location.index + 1];
|
|
1982
|
+
const before = op.at.type === "before" || following?.type === "paragraph";
|
|
1983
|
+
const anchor = op.at.type === "after" && following?.type === "paragraph" ? following : location.paragraph;
|
|
1984
|
+
if (op.revision !== void 0 && !before) return refuse(DOCUMENT_OP_REFUSAL_REASONS.UNTRACKABLE, "Tracked insertion requires a following paragraph in the same block list.");
|
|
1985
|
+
const replacement = (blocks) => ({
|
|
1986
|
+
type: DOCUMENT_OP_TYPES.REPLACE_BLOCKS,
|
|
1987
|
+
story: op.story,
|
|
1988
|
+
expected: [anchor],
|
|
1989
|
+
blocks: before ? [...blocks, anchor] : [anchor, ...blocks]
|
|
1990
|
+
});
|
|
1991
|
+
const direct = applyOps(document, [replacement(op.blocks)]);
|
|
1992
|
+
if (direct.isErr()) return Result.err(new DocumentOpRefusal({
|
|
1993
|
+
reason: direct.error.reason,
|
|
1994
|
+
message: direct.error.message,
|
|
1995
|
+
opType: op.type
|
|
1996
|
+
}));
|
|
1997
|
+
const insertedIds = new Set(op.blocks.map(({ paraId }) => idKey(paraId ?? "")));
|
|
1998
|
+
if (storyParagraphs(storyBody(direct.value.document, op.story)).some((at) => insertedIds.has(idKey(at.paragraph.paraId ?? "")) && at.paragraph.pPrMark !== void 0 && endsItsContainer(storyBody(direct.value.document, op.story), at))) return refuse(DOCUMENT_OP_REFUSAL_REASONS.CONTAINER_FINAL_MARK, "Insertion would leave a tracked mark on a container-final paragraph.");
|
|
1999
|
+
const touched = {
|
|
2000
|
+
modified: [],
|
|
2001
|
+
inserted: op.blocks.flatMap(({ paraId }) => paraId === void 0 ? [] : [paraId]),
|
|
2002
|
+
removed: []
|
|
2003
|
+
};
|
|
2004
|
+
if (op.revision === void 0) return Result.ok({
|
|
2005
|
+
...direct.value,
|
|
2006
|
+
touched
|
|
2007
|
+
});
|
|
2008
|
+
if (identityKeysIn(op.blocks).length > 0) return refuse(DOCUMENT_OP_REFUSAL_REASONS.UNTRACKABLE, "Tracked insertion of identified review or content-control records is unsupported.");
|
|
2009
|
+
const blocks = [];
|
|
2010
|
+
for (const paragraph of op.blocks) {
|
|
2011
|
+
const wrapped = wrapTracked({
|
|
2012
|
+
items: paragraph.content,
|
|
2013
|
+
from: {
|
|
2014
|
+
offset: 0,
|
|
2015
|
+
zeroWidthBefore: 0
|
|
2016
|
+
},
|
|
2017
|
+
to: {
|
|
2018
|
+
offset: paragraphLength(paragraph),
|
|
2019
|
+
zeroWidthBefore: Number.MAX_SAFE_INTEGER
|
|
2020
|
+
},
|
|
2021
|
+
kind: WRAP_KINDS.INSERTION,
|
|
2022
|
+
stamp: op.revision
|
|
2023
|
+
});
|
|
2024
|
+
if (wrapped.kind === "refused") return refuse(wrapped.reason, "The inserted content cannot be tracked.");
|
|
2025
|
+
blocks.push({
|
|
2026
|
+
...paragraph,
|
|
2027
|
+
content: wrapped.kind === "wrapped" ? wrapped.content : paragraph.content,
|
|
2028
|
+
pPrMark: {
|
|
2029
|
+
kind: "ins",
|
|
2030
|
+
info: stampInfo(op.revision)
|
|
2031
|
+
}
|
|
2032
|
+
});
|
|
2033
|
+
}
|
|
2034
|
+
const fresh = freshenIdentities({
|
|
2035
|
+
before: [],
|
|
2036
|
+
after: blocks,
|
|
2037
|
+
newIds: op.newIds ?? {},
|
|
2038
|
+
usedElsewhere: () => new Set(packageIdentityKeys(document.package))
|
|
2039
|
+
});
|
|
2040
|
+
switch (fresh.kind) {
|
|
2041
|
+
case "needsIds": return refuse(DOCUMENT_OP_REFUSAL_REASONS.NEEDS_NEW_IDS, "Inserted paragraph review records need more revision ids.");
|
|
2042
|
+
case "invalidId": return refuse(DOCUMENT_OP_REFUSAL_REASONS.INVALID_NEW_ID, "An inserted review record has an invalid revision id.");
|
|
2043
|
+
case "fresh": {
|
|
2044
|
+
const tracked = applyOps(document, [replacement(fresh.paragraphs)]);
|
|
2045
|
+
return tracked.isErr() ? Result.err(new DocumentOpRefusal({
|
|
2046
|
+
reason: tracked.error.reason,
|
|
2047
|
+
message: tracked.error.message,
|
|
2048
|
+
opType: op.type
|
|
2049
|
+
})) : Result.ok({
|
|
2050
|
+
...tracked.value,
|
|
2051
|
+
touched
|
|
2052
|
+
});
|
|
2053
|
+
}
|
|
2054
|
+
default: return fresh;
|
|
2055
|
+
}
|
|
2056
|
+
};
|
|
2057
|
+
//#endregion
|
|
2058
|
+
//#region src/ops/contract.ts
|
|
2059
|
+
/**
|
|
2060
|
+
* The seed contract: what a document must be for operations to apply to it.
|
|
2061
|
+
*
|
|
2062
|
+
* A host establishes it once, when a document enters the journal: run
|
|
2063
|
+
* `ensureParaIds` over the package bytes (it stamps every story, headers,
|
|
2064
|
+
* footers and notes included, and makes ids unique), parse, then
|
|
2065
|
+
* {@link normalizeForOps}. {@link validateOpsDocument} states the result:
|
|
2066
|
+
*
|
|
2067
|
+
* - every paragraph of the main story has a `w14:paraId`;
|
|
2068
|
+
* - no paragraph id repeats anywhere in the package, compared as hex, so an
|
|
2069
|
+
* inverse can always recreate the ids it removed;
|
|
2070
|
+
* - no revision id (tracked changes, property changes) or content-control id
|
|
2071
|
+
* repeats, so the one record carrying an id is the one it names;
|
|
2072
|
+
* - the body's section view says what its blocks say;
|
|
2073
|
+
* - the main story holds no empty run, text node, or revision wrapper.
|
|
2074
|
+
*
|
|
2075
|
+
* Paragraphs in text boxes, headers, footers, notes and comments are other
|
|
2076
|
+
* stories: they are not addressable in schema version 1, and their ids count
|
|
2077
|
+
* toward uniqueness like any other.
|
|
2078
|
+
*
|
|
2079
|
+
* Every operation checks the contract before applying and leaves it holding,
|
|
2080
|
+
* so a document an operation produced is not checked again: documents are
|
|
2081
|
+
* values, never modified in place.
|
|
2082
|
+
*/
|
|
2083
|
+
/** A document that does not meet the seed contract. */
|
|
2084
|
+
var DocumentOpsContractError = class extends TaggedError("DocumentOpsContractError") {};
|
|
2085
|
+
const holdsEmptyRecord$1 = (nodes) => nodes.some((node) => isEmptyRecord(node) || holdsEmptyRecord$1(childNodes(node) ?? []));
|
|
2086
|
+
const violation = (document) => {
|
|
2087
|
+
const body = document.package.document;
|
|
2088
|
+
const paragraphs = storyParagraphs(body);
|
|
2089
|
+
if (paragraphs.some(({ paragraph }) => paragraph.paraId === void 0)) return new DocumentOpsContractError({
|
|
2090
|
+
reason: DOCUMENT_OP_REFUSAL_REASONS.MISSING_BLOCK_ID,
|
|
2091
|
+
message: "A main-story paragraph has no paraId; ensureParaIds establishes one."
|
|
2092
|
+
});
|
|
2093
|
+
const repeated = [...countIds(packageParagraphIds(document.package))].find(([, count]) => count > 1);
|
|
2094
|
+
if (repeated !== void 0) return new DocumentOpsContractError({
|
|
2095
|
+
reason: DOCUMENT_OP_REFUSAL_REASONS.DUPLICATE_BLOCK_ID,
|
|
2096
|
+
message: `${repeated[1]} paragraphs in the package are ${repeated[0]}.`
|
|
2097
|
+
});
|
|
2098
|
+
const repeatedRecord = [...countKeys(packageIdentityKeys(document.package))].find(([, count]) => count > 1);
|
|
2099
|
+
if (repeatedRecord !== void 0) return new DocumentOpsContractError({
|
|
2100
|
+
reason: DOCUMENT_OP_REFUSAL_REASONS.DUPLICATE_RECORD_ID,
|
|
2101
|
+
message: `${repeatedRecord[1]} records in the package carry ${repeatedRecord[0]}.`
|
|
2102
|
+
});
|
|
2103
|
+
if (!sectionsInStep(body)) return new DocumentOpsContractError({
|
|
2104
|
+
reason: DOCUMENT_OP_REFUSAL_REASONS.SECTIONS_OUT_OF_STEP,
|
|
2105
|
+
message: "The body's section view does not match its blocks."
|
|
2106
|
+
});
|
|
2107
|
+
if (paragraphs.some(({ paragraph }) => holdsEmptyRecord$1(paragraph.content))) return new DocumentOpsContractError({
|
|
2108
|
+
reason: DOCUMENT_OP_REFUSAL_REASONS.EMPTY_RECORD,
|
|
2109
|
+
message: "The main story holds an empty run, text node, or revision wrapper; normalizeForOps removes them."
|
|
2110
|
+
});
|
|
2111
|
+
};
|
|
2112
|
+
/** Documents known to meet the contract: checked ones and ones an operation produced. */
|
|
2113
|
+
const MEETS_CONTRACT = /* @__PURE__ */ new WeakSet();
|
|
2114
|
+
/** Check a document against the seed contract. */
|
|
2115
|
+
const validateOpsDocument = (document) => {
|
|
2116
|
+
if (MEETS_CONTRACT.has(document)) return Result.ok(document);
|
|
2117
|
+
const found = violation(document);
|
|
2118
|
+
if (found !== void 0) return Result.err(found);
|
|
2119
|
+
MEETS_CONTRACT.add(document);
|
|
2120
|
+
return Result.ok(document);
|
|
2121
|
+
};
|
|
2122
|
+
/** Record that an operation produced `document` from one that met the contract. */
|
|
2123
|
+
const meetsContract = (document) => {
|
|
2124
|
+
MEETS_CONTRACT.add(document);
|
|
2125
|
+
};
|
|
2126
|
+
const withoutEmpty = (nodes) => {
|
|
2127
|
+
let changed = false;
|
|
2128
|
+
const out = [];
|
|
2129
|
+
for (const node of nodes) {
|
|
2130
|
+
if (isEmptyRecord(node)) {
|
|
2131
|
+
changed = true;
|
|
2132
|
+
continue;
|
|
2133
|
+
}
|
|
2134
|
+
const children = childNodes(node);
|
|
2135
|
+
const kept = children === void 0 ? children : withoutEmpty(children);
|
|
2136
|
+
if (kept !== children && kept !== void 0) {
|
|
2137
|
+
changed = true;
|
|
2138
|
+
const rebuilt = rebuildNode(node, kept);
|
|
2139
|
+
if (!isEmptyRecord(rebuilt)) out.push(rebuilt);
|
|
2140
|
+
continue;
|
|
2141
|
+
}
|
|
2142
|
+
out.push(node);
|
|
2143
|
+
}
|
|
2144
|
+
return changed ? out : nodes;
|
|
2145
|
+
};
|
|
2146
|
+
const normalizeBlocks = (blocks) => {
|
|
2147
|
+
const out = [];
|
|
2148
|
+
for (const block of blocks) switch (block.type) {
|
|
2149
|
+
case "paragraph": {
|
|
2150
|
+
const content = withoutEmpty(block.content);
|
|
2151
|
+
out.push(content === block.content ? block : {
|
|
2152
|
+
...block,
|
|
2153
|
+
content: asParagraphContent(content)
|
|
2154
|
+
});
|
|
2155
|
+
break;
|
|
2156
|
+
}
|
|
2157
|
+
case "table": {
|
|
2158
|
+
const rows = [];
|
|
2159
|
+
for (const row of block.rows) {
|
|
2160
|
+
const cells = [];
|
|
2161
|
+
for (const cell of row.cells) cells.push({
|
|
2162
|
+
...cell,
|
|
2163
|
+
content: normalizeBlocks(cell.content)
|
|
2164
|
+
});
|
|
2165
|
+
rows.push({
|
|
2166
|
+
...row,
|
|
2167
|
+
cells
|
|
2168
|
+
});
|
|
2169
|
+
}
|
|
2170
|
+
out.push({
|
|
2171
|
+
...block,
|
|
2172
|
+
rows
|
|
2173
|
+
});
|
|
2174
|
+
break;
|
|
2175
|
+
}
|
|
2176
|
+
case "blockSdt":
|
|
2177
|
+
case "blockCustomXml":
|
|
2178
|
+
out.push({
|
|
2179
|
+
...block,
|
|
2180
|
+
content: normalizeBlocks(block.content)
|
|
2181
|
+
});
|
|
2182
|
+
break;
|
|
2183
|
+
case "preservedBlock":
|
|
2184
|
+
case "bookmarkStart":
|
|
2185
|
+
case "bookmarkEnd":
|
|
2186
|
+
out.push(block);
|
|
2187
|
+
break;
|
|
2188
|
+
default: return block;
|
|
2189
|
+
}
|
|
2190
|
+
return out;
|
|
2191
|
+
};
|
|
2192
|
+
/**
|
|
2193
|
+
* The document with every empty run and empty text node of the main story
|
|
2194
|
+
* removed, and its section view derived again. Nothing else changes.
|
|
2195
|
+
*/
|
|
2196
|
+
const normalizeForOps = (document) => {
|
|
2197
|
+
const body = document.package.document;
|
|
2198
|
+
return {
|
|
2199
|
+
...document,
|
|
2200
|
+
package: {
|
|
2201
|
+
...document.package,
|
|
2202
|
+
document: withBodyContent(body, normalizeBlocks(body.content))
|
|
2203
|
+
}
|
|
2204
|
+
};
|
|
2205
|
+
};
|
|
2206
|
+
//#endregion
|
|
2207
|
+
//#region src/ops/edits.ts
|
|
2208
|
+
const TOUCHED_STATES = [
|
|
2209
|
+
"modified",
|
|
2210
|
+
"inserted",
|
|
2211
|
+
"removed"
|
|
2212
|
+
];
|
|
2213
|
+
/** A block's net change after two changes in turn; `undefined` when it came and went. */
|
|
2214
|
+
const TOUCH_TRANSITIONS = {
|
|
2215
|
+
modified: {
|
|
2216
|
+
modified: "modified",
|
|
2217
|
+
inserted: "modified",
|
|
2218
|
+
removed: "removed"
|
|
2219
|
+
},
|
|
2220
|
+
inserted: {
|
|
2221
|
+
modified: "inserted",
|
|
2222
|
+
inserted: "inserted",
|
|
2223
|
+
removed: void 0
|
|
2224
|
+
},
|
|
2225
|
+
removed: {
|
|
2226
|
+
modified: "modified",
|
|
2227
|
+
inserted: "modified",
|
|
2228
|
+
removed: "removed"
|
|
2229
|
+
}
|
|
2230
|
+
};
|
|
2231
|
+
/** Edits made in turn, starting from `document`, as one edit. */
|
|
2232
|
+
const combineEdits = (document, edits) => {
|
|
2233
|
+
const touched = /* @__PURE__ */ new Map();
|
|
2234
|
+
for (const edit of edits) for (const state of TOUCHED_STATES) for (const id of edit.touched[state]) {
|
|
2235
|
+
const previous = touched.get(id);
|
|
2236
|
+
const next = previous === void 0 ? state : TOUCH_TRANSITIONS[previous][state];
|
|
2237
|
+
if (next === void 0) touched.delete(id);
|
|
2238
|
+
else touched.set(id, next);
|
|
2239
|
+
}
|
|
2240
|
+
const ids = (state) => [...touched].flatMap(([id, value]) => value === state ? [id] : []);
|
|
2241
|
+
return {
|
|
2242
|
+
document: edits.at(-1)?.document ?? document,
|
|
2243
|
+
inverse: edits.toReversed().flatMap(({ inverse }) => inverse),
|
|
2244
|
+
touched: {
|
|
2245
|
+
modified: ids("modified"),
|
|
2246
|
+
inserted: ids("inserted"),
|
|
2247
|
+
removed: ids("removed")
|
|
2248
|
+
}
|
|
2249
|
+
};
|
|
2250
|
+
};
|
|
2251
|
+
//#endregion
|
|
2252
|
+
//#region src/ops/patch.ts
|
|
2253
|
+
/**
|
|
2254
|
+
* The keys an object states.
|
|
2255
|
+
*
|
|
2256
|
+
* SAFETY: `Object.keys` widens to `string[]`. Both callers pass a model
|
|
2257
|
+
* property set or a patch over one, object literals whose own keys are all
|
|
2258
|
+
* keys of `Formatting`.
|
|
2259
|
+
*/
|
|
2260
|
+
const ownKeys = (value) => Object.keys(value);
|
|
2261
|
+
/**
|
|
2262
|
+
* A property set with a patch applied: a value sets its key, `null` clears
|
|
2263
|
+
* it, an absent key is untouched. The input is not modified. A set left with
|
|
2264
|
+
* no keys is `undefined`, the model's spelling of "states nothing".
|
|
2265
|
+
*/
|
|
2266
|
+
const applyFormattingPatch = (base, patch) => {
|
|
2267
|
+
const next = {};
|
|
2268
|
+
if (base !== void 0) {
|
|
2269
|
+
for (const key of ownKeys(base)) if (patch[key] === void 0) next[key] = base[key];
|
|
2270
|
+
}
|
|
2271
|
+
for (const key of ownKeys(patch)) {
|
|
2272
|
+
const value = patch[key];
|
|
2273
|
+
if (value !== void 0 && value !== null) next[key] = value;
|
|
2274
|
+
}
|
|
2275
|
+
return Object.keys(next).length === 0 ? void 0 : next;
|
|
2276
|
+
};
|
|
2277
|
+
/**
|
|
2278
|
+
* The patch that gives back what `patch` replaced in `base`: each key it names
|
|
2279
|
+
* set to the value it had, or cleared where it had none.
|
|
2280
|
+
*/
|
|
2281
|
+
const priorValues = (base, patch) => {
|
|
2282
|
+
const restore = {};
|
|
2283
|
+
for (const key of ownKeys(patch)) {
|
|
2284
|
+
if (patch[key] === void 0) continue;
|
|
2285
|
+
const prior = base?.[key];
|
|
2286
|
+
restore[key] = prior === void 0 ? null : prior;
|
|
2287
|
+
}
|
|
2288
|
+
return restore;
|
|
2289
|
+
};
|
|
2290
|
+
//#endregion
|
|
2291
|
+
//#region src/ops/inline.ts
|
|
2292
|
+
/**
|
|
2293
|
+
* Edits over a paragraph's content in its logical offset space.
|
|
2294
|
+
*
|
|
2295
|
+
* Every function here is pure: it returns new arrays and records along the
|
|
2296
|
+
* path it changed and hands back every child it did not change as the same
|
|
2297
|
+
* object, so an untouched run is `===` to its input.
|
|
2298
|
+
*/
|
|
2299
|
+
/** A run with the property set it states, or none. */
|
|
2300
|
+
const withRunFormatting = (run, formatting) => {
|
|
2301
|
+
const next = { ...run };
|
|
2302
|
+
if (formatting === void 0) delete next.formatting;
|
|
2303
|
+
else next.formatting = formatting;
|
|
2304
|
+
return next;
|
|
2305
|
+
};
|
|
2306
|
+
const withRunContent = (run, content) => ({
|
|
2307
|
+
...run,
|
|
2308
|
+
content
|
|
2309
|
+
});
|
|
2310
|
+
/** Two property sets state the same thing; an absent set states nothing. */
|
|
2311
|
+
const sameRunFormatting = (left, right) => structurallyEqual(left ?? {}, right ?? {});
|
|
2312
|
+
/** How an inverse spells a property set it gives back: absent, or an empty set. */
|
|
2313
|
+
const emptySetSpelling = (formatting) => formatting !== void 0 && Object.keys(formatting).length === 0 ? EMPTY_PROPERTY_SETS.KEEP : EMPTY_PROPERTY_SETS.OMIT;
|
|
2314
|
+
/** A patched property set, spelled as `whenEmpty` says once it has no keys left. */
|
|
2315
|
+
const patchedSet = (base, patch, whenEmpty) => applyFormattingPatch(base, patch) ?? (whenEmpty === EMPTY_PROPERTY_SETS.KEEP ? {} : void 0);
|
|
2316
|
+
const newTextRun = (text, formatting) => {
|
|
2317
|
+
const content = [{
|
|
2318
|
+
type: "text",
|
|
2319
|
+
text
|
|
2320
|
+
}];
|
|
2321
|
+
return formatting === void 0 || Object.keys(formatting).length === 0 ? {
|
|
2322
|
+
type: "run",
|
|
2323
|
+
content
|
|
2324
|
+
} : {
|
|
2325
|
+
type: "run",
|
|
2326
|
+
formatting,
|
|
2327
|
+
content
|
|
2328
|
+
};
|
|
2329
|
+
};
|
|
2330
|
+
/**
|
|
2331
|
+
* Remove the leaves between two gaps. A record the range passes through keeps
|
|
2332
|
+
* its two ends as one record; one left with nothing goes. Returns the new
|
|
2333
|
+
* content and the slice removed, with its cut ends.
|
|
2334
|
+
*/
|
|
2335
|
+
const deleteBetween = (items, from, to) => {
|
|
2336
|
+
const gaps = [from, to];
|
|
2337
|
+
const [before = [], middle = [], after = []] = partitionContent(items, gaps);
|
|
2338
|
+
const across = spanningRecords(items, gaps, 0, 2).length;
|
|
2339
|
+
return {
|
|
2340
|
+
content: asParagraphContent(mergeLists(before, after, across) ?? panic("The two ends of a cut record always merge.")),
|
|
2341
|
+
removed: {
|
|
2342
|
+
content: asParagraphContent(middle),
|
|
2343
|
+
openStart: spanningRecords(items, gaps, 0, 1).length,
|
|
2344
|
+
openEnd: spanningRecords(items, gaps, 1, 2).length
|
|
2345
|
+
}
|
|
2346
|
+
};
|
|
2347
|
+
};
|
|
2348
|
+
/**
|
|
2349
|
+
* Cut the content at a gap and put a slice in, merging its open ends with the
|
|
2350
|
+
* halves; `undefined` when an open end does not match the record it meets.
|
|
2351
|
+
*/
|
|
2352
|
+
const insertSliceAt = (items, at, slice) => {
|
|
2353
|
+
const [before = [], after = []] = partitionContent(items, [at]);
|
|
2354
|
+
const withStart = mergeLists(before, slice.content, slice.openStart);
|
|
2355
|
+
const whole = withStart === void 0 ? void 0 : mergeLists(withStart, after, slice.openEnd, {
|
|
2356
|
+
mode: "exact",
|
|
2357
|
+
identity: "second"
|
|
2358
|
+
});
|
|
2359
|
+
return whole === void 0 ? void 0 : asParagraphContent(whole);
|
|
2360
|
+
};
|
|
2361
|
+
/** The gap after inserted content: its trailing zero-width leaves come before it. */
|
|
2362
|
+
const gapAfterInserted = (at, inserted) => {
|
|
2363
|
+
const width = contentWidth(inserted);
|
|
2364
|
+
const trailing = zeroWidthLeavesAt(inserted, width).length;
|
|
2365
|
+
return {
|
|
2366
|
+
offset: at.offset + width,
|
|
2367
|
+
zeroWidthBefore: width === 0 ? at.zeroWidthBefore + trailing : trailing
|
|
2368
|
+
};
|
|
2369
|
+
};
|
|
2370
|
+
/**
|
|
2371
|
+
* What undoes an insertion: removing the inserted leaves, then merging the
|
|
2372
|
+
* records the insertion cut back to how `before` had them, `join` levels
|
|
2373
|
+
* below the ones still running across the gap. `undefined` when the
|
|
2374
|
+
* insertion merged records that were separate before it: no deletion could
|
|
2375
|
+
* tell them apart again, so such an insertion is refused.
|
|
2376
|
+
*/
|
|
2377
|
+
const insertionInverse = (before, after, start, end) => {
|
|
2378
|
+
const { content: restored, removed } = deleteBetween(after, start, end);
|
|
2379
|
+
const cut = spanningRecords(before, [start], 0, 1).length;
|
|
2380
|
+
const across = spanningRecords(restored, [start], 0, 1).length;
|
|
2381
|
+
return across > cut ? void 0 : {
|
|
2382
|
+
removed,
|
|
2383
|
+
join: cut - across
|
|
2384
|
+
};
|
|
2385
|
+
};
|
|
2386
|
+
/** Cut the innermost `depth` records running across a gap in two; `undefined` when fewer do. */
|
|
2387
|
+
const splitAt = (items, at, depth) => {
|
|
1658
2388
|
const spine = spanningRecords(items, [at], 0, 1);
|
|
1659
2389
|
if (depth > spine.length) return;
|
|
1660
2390
|
const [before = [], after = []] = partitionContent(items, [at]);
|
|
1661
2391
|
return asParagraphContent(mergeLists(before, after, spine.length - depth) ?? panic("The two ends of a cut record always merge."));
|
|
1662
2392
|
};
|
|
1663
|
-
/** Two sets of new ids, each space's in order: `first`'s then `second`'s. */
|
|
1664
|
-
const concatIds = (first, second) => {
|
|
1665
|
-
const revision = [...first.revision ?? [], ...second.revision ?? []];
|
|
1666
|
-
const control = [...first.control ?? [], ...second.control ?? []];
|
|
2393
|
+
/** Two sets of new ids, each space's in order: `first`'s then `second`'s. */
|
|
2394
|
+
const concatIds = (first, second) => {
|
|
2395
|
+
const revision = [...first.revision ?? [], ...second.revision ?? []];
|
|
2396
|
+
const control = [...first.control ?? [], ...second.control ?? []];
|
|
2397
|
+
return {
|
|
2398
|
+
...revision.length > 0 ? { revision } : {},
|
|
2399
|
+
...control.length > 0 ? { control } : {}
|
|
2400
|
+
};
|
|
2401
|
+
};
|
|
2402
|
+
/** Whether a set of new ids names any. */
|
|
2403
|
+
const namesIds = (ids) => (ids.revision?.length ?? 0) > 0 || (ids.control?.length ?? 0) > 0;
|
|
2404
|
+
/**
|
|
2405
|
+
* Merge `left` and `right`, `depth` levels, retiring ids from level `from`
|
|
2406
|
+
* down. Two records carrying the same id cannot be merged: a cut could not
|
|
2407
|
+
* give them back two.
|
|
2408
|
+
*/
|
|
2409
|
+
const mergeRetiring = (left, right, depth, from) => {
|
|
2410
|
+
const retired = {
|
|
2411
|
+
revision: [],
|
|
2412
|
+
control: []
|
|
2413
|
+
};
|
|
2414
|
+
let shared = false;
|
|
2415
|
+
const content = mergeLists(left, right, depth, {
|
|
2416
|
+
mode: "exact",
|
|
2417
|
+
onMerge: (first, second, level) => {
|
|
2418
|
+
if (level < from) return;
|
|
2419
|
+
const kept = identitySlots(first);
|
|
2420
|
+
for (const [index, slot] of identitySlots(second).entries()) {
|
|
2421
|
+
shared ||= kept[index]?.id === slot.id;
|
|
2422
|
+
retired[slot.space].push(slot.id);
|
|
2423
|
+
}
|
|
2424
|
+
}
|
|
2425
|
+
});
|
|
2426
|
+
if (content === void 0) return { kind: "notAlike" };
|
|
2427
|
+
if (shared) return { kind: "sharedId" };
|
|
2428
|
+
return {
|
|
2429
|
+
kind: "joined",
|
|
2430
|
+
content: asParagraphContent(content),
|
|
2431
|
+
retired: concatIds({}, retired)
|
|
2432
|
+
};
|
|
2433
|
+
};
|
|
2434
|
+
/** Merge the records meeting at a gap, `depth` levels below the ones already running across it. */
|
|
2435
|
+
const joinAt = (items, at, depth) => {
|
|
2436
|
+
const across = spanningRecords(items, [at], 0, 1).length;
|
|
2437
|
+
const [before = [], after = []] = partitionContent(items, [at]);
|
|
2438
|
+
return mergeRetiring(before, after, across + depth, across + 1);
|
|
2439
|
+
};
|
|
2440
|
+
/** A split paragraph's two halves and the records the cut ran through. */
|
|
2441
|
+
const cutAt = (items, at) => {
|
|
2442
|
+
const [before = [], after = []] = partitionContent(items, [at]);
|
|
2443
|
+
return {
|
|
2444
|
+
before: asParagraphContent(before),
|
|
2445
|
+
after: asParagraphContent(after),
|
|
2446
|
+
through: spanningRecords(items, [at], 0, 1)
|
|
2447
|
+
};
|
|
2448
|
+
};
|
|
2449
|
+
/** Two paragraphs' content end to end, `depth` levels of the records meeting there merged. */
|
|
2450
|
+
const joinContent = (first, second, depth) => mergeRetiring(first, second, depth, 1);
|
|
2451
|
+
const splitRun = (run, offset) => {
|
|
2452
|
+
const left = [];
|
|
2453
|
+
const right = [];
|
|
2454
|
+
let position = 0;
|
|
2455
|
+
for (const child of run.content) {
|
|
2456
|
+
const width = runContentWidth(child);
|
|
2457
|
+
const start = position;
|
|
2458
|
+
position += width;
|
|
2459
|
+
if (position <= offset) left.push(child);
|
|
2460
|
+
else if (start >= offset) right.push(child);
|
|
2461
|
+
else if (child.type === "text") {
|
|
2462
|
+
left.push({
|
|
2463
|
+
...child,
|
|
2464
|
+
text: child.text.slice(0, offset - start)
|
|
2465
|
+
});
|
|
2466
|
+
right.push({
|
|
2467
|
+
...child,
|
|
2468
|
+
text: child.text.slice(offset - start)
|
|
2469
|
+
});
|
|
2470
|
+
}
|
|
2471
|
+
}
|
|
2472
|
+
return {
|
|
2473
|
+
left: left.length === 0 ? void 0 : withRunContent(run, left),
|
|
2474
|
+
right: right.length === 0 ? void 0 : withRunContent(run, right)
|
|
2475
|
+
};
|
|
2476
|
+
};
|
|
2477
|
+
/** The run holding unit `unit`, or `undefined` when the unit is not run content. */
|
|
2478
|
+
const runAtUnit = (items, unit, insideRemovedRevision) => {
|
|
2479
|
+
let position = 0;
|
|
2480
|
+
for (const [index, item] of items.entries()) {
|
|
2481
|
+
const width = inlineWidth(item);
|
|
2482
|
+
const start = position;
|
|
2483
|
+
position += width;
|
|
2484
|
+
if (unit < start || unit >= position) continue;
|
|
2485
|
+
if (item.type === "run") return {
|
|
2486
|
+
path: [index],
|
|
2487
|
+
run: item,
|
|
2488
|
+
runOffset: unit - start,
|
|
2489
|
+
insideRemovedRevision
|
|
2490
|
+
};
|
|
2491
|
+
if (isInlineContainer(item)) {
|
|
2492
|
+
const hit = runAtUnit(childrenOf(item), unit - start, insideRemovedRevision || isRemovedRevision(item));
|
|
2493
|
+
return hit === void 0 ? void 0 : {
|
|
2494
|
+
...hit,
|
|
2495
|
+
path: [index, ...hit.path]
|
|
2496
|
+
};
|
|
2497
|
+
}
|
|
2498
|
+
return;
|
|
2499
|
+
}
|
|
2500
|
+
};
|
|
2501
|
+
/**
|
|
2502
|
+
* The run inserted text joins: the one holding the unit before the position,
|
|
2503
|
+
* else the one holding the unit after it, skipping runs inside tracked
|
|
2504
|
+
* deletions and moved-away content.
|
|
2505
|
+
*/
|
|
2506
|
+
const insertionHost = (items, offset) => {
|
|
2507
|
+
const before = offset > 0 ? runAtUnit(items, offset - 1, false) : void 0;
|
|
2508
|
+
if (before !== void 0 && !before.insideRemovedRevision) return {
|
|
2509
|
+
path: before.path,
|
|
2510
|
+
run: before.run,
|
|
2511
|
+
runOffset: before.runOffset + 1
|
|
2512
|
+
};
|
|
2513
|
+
const after = runAtUnit(items, offset, false);
|
|
2514
|
+
if (after !== void 0 && !after.insideRemovedRevision) return {
|
|
2515
|
+
path: after.path,
|
|
2516
|
+
run: after.run,
|
|
2517
|
+
runOffset: after.runOffset
|
|
2518
|
+
};
|
|
2519
|
+
};
|
|
2520
|
+
/** Replace the child at `path` with `replacement`. */
|
|
2521
|
+
const replaceAtPath = (items, path, replacement) => {
|
|
2522
|
+
const [index, ...rest] = path;
|
|
2523
|
+
if (index === void 0) return [...replacement];
|
|
2524
|
+
const out = [...items];
|
|
2525
|
+
if (rest.length === 0) {
|
|
2526
|
+
out.splice(index, 1, ...replacement);
|
|
2527
|
+
return out;
|
|
2528
|
+
}
|
|
2529
|
+
const item = items[index];
|
|
2530
|
+
if (item === void 0 || !isInlineContainer(item)) return panic(`Inline path step ${index} does not name a container.`);
|
|
2531
|
+
out[index] = withChildren(item, replaceAtPath(childrenOf(item), rest, replacement));
|
|
2532
|
+
return out;
|
|
2533
|
+
};
|
|
2534
|
+
/**
|
|
2535
|
+
* The run with text added at a unit offset, joining the text node there when
|
|
2536
|
+
* one does. An empty text node is a zero-width child of its own and is left
|
|
2537
|
+
* as it is.
|
|
2538
|
+
*/
|
|
2539
|
+
const runWithText = (run, runOffset, text) => {
|
|
2540
|
+
let position = 0;
|
|
2541
|
+
let insertAt = 0;
|
|
2542
|
+
for (const [index, child] of run.content.entries()) {
|
|
2543
|
+
const width = runContentWidth(child);
|
|
2544
|
+
if (child.type === "text" && width > 0 && position <= runOffset && runOffset <= position + width) {
|
|
2545
|
+
const cut = runOffset - position;
|
|
2546
|
+
const content = [...run.content];
|
|
2547
|
+
content[index] = {
|
|
2548
|
+
...child,
|
|
2549
|
+
text: child.text.slice(0, cut) + text + child.text.slice(cut)
|
|
2550
|
+
};
|
|
2551
|
+
return withRunContent(run, content);
|
|
2552
|
+
}
|
|
2553
|
+
position += width;
|
|
2554
|
+
if (position <= runOffset) insertAt = index + 1;
|
|
2555
|
+
}
|
|
2556
|
+
const content = [...run.content];
|
|
2557
|
+
content.splice(insertAt, 0, {
|
|
2558
|
+
type: "text",
|
|
2559
|
+
text
|
|
2560
|
+
});
|
|
2561
|
+
return withRunContent(run, content);
|
|
2562
|
+
};
|
|
2563
|
+
/**
|
|
2564
|
+
* Put a new run at a position that no run holds; `undefined` when the position
|
|
2565
|
+
* is inside tracked-deleted or moved-away content.
|
|
2566
|
+
*/
|
|
2567
|
+
const insertRunAtSlot = (items, offset, run) => {
|
|
2568
|
+
let position = 0;
|
|
2569
|
+
let insertAt = 0;
|
|
2570
|
+
for (const [index, item] of items.entries()) {
|
|
2571
|
+
const width = inlineWidth(item);
|
|
2572
|
+
const start = position;
|
|
2573
|
+
position += width;
|
|
2574
|
+
if (start < offset && offset < position && isInlineContainer(item)) {
|
|
2575
|
+
if (isRemovedRevision(item)) return;
|
|
2576
|
+
const children = insertRunAtSlot(childrenOf(item), offset - start, run);
|
|
2577
|
+
if (children === void 0) return;
|
|
2578
|
+
const out = [...items];
|
|
2579
|
+
out[index] = withChildren(item, children);
|
|
2580
|
+
return out;
|
|
2581
|
+
}
|
|
2582
|
+
if (position <= offset) insertAt = index + 1;
|
|
2583
|
+
}
|
|
2584
|
+
const out = [...items];
|
|
2585
|
+
out.splice(insertAt, 0, run);
|
|
2586
|
+
return out;
|
|
2587
|
+
};
|
|
2588
|
+
/**
|
|
2589
|
+
* Insert text at a position; `undefined` when the position is inside
|
|
2590
|
+
* tracked-deleted or moved-away content and no other run borders it.
|
|
2591
|
+
*/
|
|
2592
|
+
const insertTextInContent = (items, offset, text, runProps) => {
|
|
2593
|
+
const host = insertionHost(items, offset);
|
|
2594
|
+
const formatting = runProps === "inherit" ? void 0 : runProps;
|
|
2595
|
+
if (host === void 0) return insertRunAtSlot(items, offset, newTextRun(text, formatting));
|
|
2596
|
+
if (formatting === void 0 || sameRunFormatting(formatting, host.run.formatting)) return replaceAtPath(items, host.path, [runWithText(host.run, host.runOffset, text)]);
|
|
2597
|
+
const halves = splitRun(host.run, host.runOffset);
|
|
2598
|
+
const replacement = [];
|
|
2599
|
+
if (halves.left !== void 0) replacement.push(halves.left);
|
|
2600
|
+
replacement.push(newTextRun(text, formatting));
|
|
2601
|
+
if (halves.right !== void 0) replacement.push(halves.right);
|
|
2602
|
+
return replaceAtPath(items, host.path, replacement);
|
|
2603
|
+
};
|
|
2604
|
+
const patchRunsIn = (nodes, patch, whenEmpty, prior, decorate) => {
|
|
2605
|
+
const out = [];
|
|
2606
|
+
for (const node of nodes) {
|
|
2607
|
+
if (node.type === "run") {
|
|
2608
|
+
const formatting = patchedSet(node.formatting, patch, whenEmpty);
|
|
2609
|
+
if (runWidth(node) === 0 || sameRunFormatting(formatting, node.formatting)) {
|
|
2610
|
+
out.push(node);
|
|
2611
|
+
continue;
|
|
2612
|
+
}
|
|
2613
|
+
const patchedRun = withRunFormatting(node, formatting);
|
|
2614
|
+
const run = decorate === void 0 ? patchedRun : decorate(patchedRun, node);
|
|
2615
|
+
prior.set(run, node.formatting);
|
|
2616
|
+
out.push(run);
|
|
2617
|
+
continue;
|
|
2618
|
+
}
|
|
2619
|
+
if (isParagraphContent(node) && isInlineContainer(node)) {
|
|
2620
|
+
const children = childrenOf(node);
|
|
2621
|
+
const next = patchRunsIn(children, patch, whenEmpty, prior, decorate);
|
|
2622
|
+
const same = next.every((child, index) => child === children[index]);
|
|
2623
|
+
out.push(same ? node : withChildren(node, asParagraphContent(next)));
|
|
2624
|
+
continue;
|
|
2625
|
+
}
|
|
2626
|
+
out.push(node);
|
|
2627
|
+
}
|
|
2628
|
+
return out;
|
|
2629
|
+
};
|
|
2630
|
+
const collectRuns = (nodes, out) => {
|
|
2631
|
+
for (const node of nodes) {
|
|
2632
|
+
if (node.type === "run") {
|
|
2633
|
+
if (runWidth(node) > 0) out.push(node);
|
|
2634
|
+
continue;
|
|
2635
|
+
}
|
|
2636
|
+
if (isParagraphContent(node) && isInlineContainer(node)) collectRuns(childrenOf(node), out);
|
|
2637
|
+
}
|
|
2638
|
+
};
|
|
2639
|
+
/** The runs a patch between two gaps reaches, cut at the gaps: those with units there. */
|
|
2640
|
+
const runsBetween = (items, from, to) => {
|
|
2641
|
+
const [, middle = []] = partitionContent(items, [from, to]);
|
|
2642
|
+
const out = [];
|
|
2643
|
+
collectRuns(middle, out);
|
|
2644
|
+
return out;
|
|
2645
|
+
};
|
|
2646
|
+
/**
|
|
2647
|
+
* Patch the run properties of every run between two gaps, cutting runs at the
|
|
2648
|
+
* gaps. A run the patch leaves as it was is not cut. `undefined` when no run
|
|
2649
|
+
* changes.
|
|
2650
|
+
*
|
|
2651
|
+
* `restoring` gives the changed runs back the values the patch replaced, one
|
|
2652
|
+
* patch per stretch of adjacent runs that had the same values. A run the
|
|
2653
|
+
* patch left alone ends a stretch, so a restoring patch never reaches it.
|
|
2654
|
+
*
|
|
2655
|
+
* `decorate` adds to each changed run what a tracked patch records on it.
|
|
2656
|
+
*/
|
|
2657
|
+
const patchRunsBetween = (items, from, to, patch, whenEmpty, decorate) => {
|
|
2658
|
+
const gaps = [from, to];
|
|
2659
|
+
const [before = [], middle = [], after = []] = partitionContent(items, gaps);
|
|
2660
|
+
const prior = /* @__PURE__ */ new Map();
|
|
2661
|
+
const patchedMiddle = patchRunsIn(middle, patch, whenEmpty, prior, decorate);
|
|
2662
|
+
if (prior.size === 0) return;
|
|
2663
|
+
const alike = { mode: "asFarAsAlike" };
|
|
2664
|
+
const content = asParagraphContent(mergeLists(mergeLists(before, patchedMiddle, spanningRecords(items, gaps, 0, 1).length, alike) ?? [], after, spanningRecords(items, gaps, 1, 2).length, alike) ?? []);
|
|
2665
|
+
const restoring = [];
|
|
2666
|
+
let stretchOpen = false;
|
|
2667
|
+
for (const { node, before: start, after: end } of runGaps(content)) {
|
|
2668
|
+
if (node.type !== "run") continue;
|
|
2669
|
+
if (!prior.has(node)) {
|
|
2670
|
+
if (runWidth(node) > 0) stretchOpen = false;
|
|
2671
|
+
continue;
|
|
2672
|
+
}
|
|
2673
|
+
const previous = prior.get(node);
|
|
2674
|
+
const restore = priorValues(previous, patch);
|
|
2675
|
+
const spelling = emptySetSpelling(previous);
|
|
2676
|
+
const key = JSON.stringify([restore, spelling]);
|
|
2677
|
+
const last = restoring.at(-1);
|
|
2678
|
+
if (stretchOpen && last !== void 0 && last.key === key) last.to = end;
|
|
2679
|
+
else restoring.push({
|
|
2680
|
+
from: start,
|
|
2681
|
+
to: end,
|
|
2682
|
+
patch: restore,
|
|
2683
|
+
whenEmpty: spelling,
|
|
2684
|
+
key
|
|
2685
|
+
});
|
|
2686
|
+
stretchOpen = true;
|
|
2687
|
+
}
|
|
1667
2688
|
return {
|
|
1668
|
-
|
|
1669
|
-
|
|
2689
|
+
content,
|
|
2690
|
+
restoring: restoring.map(({ from: start, to: end, patch: restore, whenEmpty: spelling }) => ({
|
|
2691
|
+
from: start,
|
|
2692
|
+
to: end,
|
|
2693
|
+
patch: restore,
|
|
2694
|
+
whenEmpty: spelling
|
|
2695
|
+
}))
|
|
1670
2696
|
};
|
|
1671
2697
|
};
|
|
1672
|
-
|
|
1673
|
-
|
|
2698
|
+
//#endregion
|
|
2699
|
+
//#region src/ops/tableLocation.ts
|
|
2700
|
+
/** Shared addressing for row and whole-table operations. */
|
|
2701
|
+
/** A paragraph selects its innermost table, even through block wrappers. */
|
|
2702
|
+
const locateTableRow = (document, op) => {
|
|
2703
|
+
const body = storyBody(document, op.story);
|
|
2704
|
+
const location = storyParagraphs(body).find(({ paragraph }) => idKey(paragraph.paraId ?? "") === idKey(op.blockId));
|
|
2705
|
+
if (location === void 0) return Result.err(new DocumentOpRefusal({
|
|
2706
|
+
opType: op.type,
|
|
2707
|
+
reason: DOCUMENT_OP_REFUSAL_REASONS.BLOCK_NOT_FOUND,
|
|
2708
|
+
message: `No paragraph is ${op.blockId}.`
|
|
2709
|
+
}));
|
|
2710
|
+
const stepIndex = location.list.findLastIndex((step) => step.kind === "tableCell");
|
|
2711
|
+
const step = location.list[stepIndex];
|
|
2712
|
+
if (step?.kind !== "tableCell") return Result.err(new DocumentOpRefusal({
|
|
2713
|
+
opType: op.type,
|
|
2714
|
+
reason: DOCUMENT_OP_REFUSAL_REASONS.STRUCTURE_MISMATCH,
|
|
2715
|
+
message: "The paragraph is not in a table row."
|
|
2716
|
+
}));
|
|
2717
|
+
const list = location.list.slice(0, stepIndex);
|
|
2718
|
+
const table = blockListAt(body.content, list)[step.block];
|
|
2719
|
+
if (table?.type !== "table") return panic("A table-cell path must name a table.");
|
|
2720
|
+
return Result.ok({
|
|
2721
|
+
list,
|
|
2722
|
+
index: step.block,
|
|
2723
|
+
rowIndex: step.row,
|
|
2724
|
+
table
|
|
2725
|
+
});
|
|
2726
|
+
};
|
|
2727
|
+
/** Row operations address this table, never a nested table appearing first. */
|
|
2728
|
+
const tableRowAnchor = (rows) => storyParagraphs({ content: [{
|
|
2729
|
+
type: "table",
|
|
2730
|
+
rows: [...rows]
|
|
2731
|
+
}] }).find(({ list }) => list.filter((step) => step.kind === "tableCell").length === 1)?.paragraph.paraId;
|
|
2732
|
+
//#endregion
|
|
2733
|
+
//#region src/ops/resolveTableRows.ts
|
|
2734
|
+
/** Row resolution after inline review, before paragraph-mark joins. */
|
|
2735
|
+
const collectTables = (blocks, out) => {
|
|
2736
|
+
for (const block of blocks) switch (block.type) {
|
|
2737
|
+
case "table":
|
|
2738
|
+
for (const row of block.rows) for (const cell of row.cells) collectTables(cell.content, out);
|
|
2739
|
+
out.push(block);
|
|
2740
|
+
break;
|
|
2741
|
+
case "blockSdt":
|
|
2742
|
+
case "blockCustomXml":
|
|
2743
|
+
collectTables(block.content, out);
|
|
2744
|
+
break;
|
|
2745
|
+
case "paragraph":
|
|
2746
|
+
case "preservedBlock":
|
|
2747
|
+
case "bookmarkStart":
|
|
2748
|
+
case "bookmarkEnd": break;
|
|
2749
|
+
default: return block;
|
|
2750
|
+
}
|
|
2751
|
+
};
|
|
2752
|
+
const tablesIn = (document, story) => {
|
|
2753
|
+
const out = [];
|
|
2754
|
+
collectTables(storyBody(document, story).content, out);
|
|
2755
|
+
return out;
|
|
2756
|
+
};
|
|
2757
|
+
/** Only row insert/delete records are reachable; cell and property revisions remain refused. */
|
|
2758
|
+
const reachableRowIds = (document, story) => tablesIn(document, story).flatMap((table) => table.rows.flatMap((row) => {
|
|
2759
|
+
const change = row.structuralChange;
|
|
2760
|
+
return change?.type === "tableRowInsertion" || change?.type === "tableRowDeletion" ? [change.info.id] : [];
|
|
2761
|
+
}));
|
|
2762
|
+
const resolveTableRows = ({ document, op, applyOps }) => {
|
|
2763
|
+
const ids = new Set(op.revisionIds);
|
|
2764
|
+
let current = document;
|
|
2765
|
+
const edits = [];
|
|
2766
|
+
for (const table of tablesIn(document, op.story)) {
|
|
2767
|
+
if (!table.rows.some((row) => row.structuralChange !== void 0 && ids.has(row.structuralChange.info.id))) continue;
|
|
2768
|
+
const blockId = tableRowAnchor(table.rows);
|
|
2769
|
+
if (blockId === void 0) return Result.err(new DocumentOpRefusal({
|
|
2770
|
+
opType: op.type,
|
|
2771
|
+
reason: DOCUMENT_OP_REFUSAL_REASONS.UNTRACKABLE,
|
|
2772
|
+
message: "The table has no paragraph that can address its rows."
|
|
2773
|
+
}));
|
|
2774
|
+
const target = {
|
|
2775
|
+
type: DOCUMENT_OP_TYPES.SET_TABLE_ROWS,
|
|
2776
|
+
story: op.story,
|
|
2777
|
+
blockId,
|
|
2778
|
+
expected: table.rows,
|
|
2779
|
+
rows: table.rows
|
|
2780
|
+
};
|
|
2781
|
+
const located = locateTableRow(current, target);
|
|
2782
|
+
if (located.isErr()) return Result.err(located.error);
|
|
2783
|
+
const expected = located.value.table.rows;
|
|
2784
|
+
const rows = expected.flatMap((row) => {
|
|
2785
|
+
const change = row.structuralChange;
|
|
2786
|
+
if (change === void 0 || !ids.has(change.info.id)) return [row];
|
|
2787
|
+
if (change.type === "tableRowInsertion" !== (op.decision === REVISION_DECISIONS.ACCEPT)) return [];
|
|
2788
|
+
const next = { ...row };
|
|
2789
|
+
delete next.structuralChange;
|
|
2790
|
+
return [next];
|
|
2791
|
+
});
|
|
2792
|
+
const applied = applyOps(current, [{
|
|
2793
|
+
type: DOCUMENT_OP_TYPES.SET_TABLE_ROWS,
|
|
2794
|
+
story: op.story,
|
|
2795
|
+
blockId,
|
|
2796
|
+
expected,
|
|
2797
|
+
rows
|
|
2798
|
+
}]);
|
|
2799
|
+
if (applied.isErr()) return Result.err(applied.error);
|
|
2800
|
+
edits.push(applied.value);
|
|
2801
|
+
current = applied.value.document;
|
|
2802
|
+
}
|
|
2803
|
+
return Result.ok(combineEdits(document, edits));
|
|
2804
|
+
};
|
|
2805
|
+
//#endregion
|
|
2806
|
+
//#region src/ops/resolve.ts
|
|
1674
2807
|
/**
|
|
1675
|
-
*
|
|
1676
|
-
*
|
|
1677
|
-
*
|
|
2808
|
+
* `resolveRevision`: accepting or rejecting tracked changes, expanded to the
|
|
2809
|
+
* primitive operations that carry it out.
|
|
2810
|
+
*
|
|
2811
|
+
* The expansion follows the order an editor resolves a review in. First each
|
|
2812
|
+
* paragraph's inline changes (insertions, deletions and moves, nested ones
|
|
2813
|
+
* included, and run property changes) become one `replaceInline`, and its
|
|
2814
|
+
* property changes and kept marks one `setParagraphReview`. Then the marks
|
|
2815
|
+
* that go are removed, from the last paragraph to the first: each leaves the
|
|
2816
|
+
* next paragraph, which takes the content before it, so a chain of removed
|
|
2817
|
+
* marks ends in the paragraph after the last.
|
|
1678
2818
|
*/
|
|
1679
|
-
const
|
|
1680
|
-
|
|
1681
|
-
|
|
1682
|
-
|
|
2819
|
+
const refusal$1 = (op, reason, message) => new DocumentOpRefusal({
|
|
2820
|
+
message,
|
|
2821
|
+
reason,
|
|
2822
|
+
opType: op.type
|
|
2823
|
+
});
|
|
2824
|
+
/** Whether a decision removes the content of a tracked change of this kind. */
|
|
2825
|
+
const removesContent = (node, decision) => decision === REVISION_DECISIONS.ACCEPT ? !isAddedRevision(node) : isAddedRevision(node);
|
|
2826
|
+
/** Whether a mark of this kind recorded a break being added. */
|
|
2827
|
+
const markWasAdded = (kind) => kind === "ins" || kind === "moveTo";
|
|
2828
|
+
const removePropertyChanges = (changes, remove) => {
|
|
2829
|
+
const remaining = [];
|
|
2830
|
+
let removedPrevious = void 0;
|
|
2831
|
+
let removedAny = false;
|
|
2832
|
+
let removing = false;
|
|
2833
|
+
for (const change of changes) {
|
|
2834
|
+
if (remove(change)) {
|
|
2835
|
+
if (!removing) {
|
|
2836
|
+
removedPrevious = change.previousFormatting;
|
|
2837
|
+
removing = true;
|
|
2838
|
+
}
|
|
2839
|
+
removedAny = true;
|
|
2840
|
+
continue;
|
|
2841
|
+
}
|
|
2842
|
+
if (!removing) {
|
|
2843
|
+
remaining.push(change);
|
|
2844
|
+
continue;
|
|
2845
|
+
}
|
|
2846
|
+
const rebased = { ...change };
|
|
2847
|
+
delete rebased.previousFormatting;
|
|
2848
|
+
if (removedPrevious !== void 0) Object.assign(rebased, { previousFormatting: removedPrevious });
|
|
2849
|
+
remaining.push(rebased);
|
|
2850
|
+
removedPrevious = void 0;
|
|
2851
|
+
removing = false;
|
|
2852
|
+
}
|
|
2853
|
+
if (!removedAny) return { kind: "unchanged" };
|
|
2854
|
+
if (!removing) return {
|
|
2855
|
+
kind: "keepLive",
|
|
2856
|
+
remaining
|
|
1683
2857
|
};
|
|
1684
|
-
|
|
1685
|
-
|
|
1686
|
-
|
|
1687
|
-
|
|
1688
|
-
|
|
1689
|
-
|
|
1690
|
-
|
|
1691
|
-
|
|
1692
|
-
|
|
2858
|
+
return {
|
|
2859
|
+
kind: "restore",
|
|
2860
|
+
remaining,
|
|
2861
|
+
previous: removedPrevious
|
|
2862
|
+
};
|
|
2863
|
+
};
|
|
2864
|
+
const NOTHING_EMPTIED = { has: () => false };
|
|
2865
|
+
const resolveRun = (run, { ids, decision }) => {
|
|
2866
|
+
const changes = run.propertyChanges ?? [];
|
|
2867
|
+
const selected = (change) => ids.has(change.info.id);
|
|
2868
|
+
if (!changes.some(selected)) return run;
|
|
2869
|
+
const next = { ...run };
|
|
2870
|
+
let remaining = changes.filter((change) => !selected(change));
|
|
2871
|
+
if (decision === REVISION_DECISIONS.REJECT) {
|
|
2872
|
+
const removed = removePropertyChanges(changes, selected);
|
|
2873
|
+
if (removed.kind !== "unchanged") remaining = removed.remaining;
|
|
2874
|
+
if (removed.kind === "restore") if (removed.previous === void 0) delete next.formatting;
|
|
2875
|
+
else next.formatting = removed.previous;
|
|
2876
|
+
}
|
|
2877
|
+
if (remaining.length > 0) next.propertyChanges = remaining;
|
|
2878
|
+
else delete next.propertyChanges;
|
|
2879
|
+
return next;
|
|
2880
|
+
};
|
|
2881
|
+
/**
|
|
2882
|
+
* Whether `empty` is a piece of `other` that resolution left with nothing: a
|
|
2883
|
+
* container of the same kind and fields that the resolution emptied, where
|
|
2884
|
+
* the other holds something or was emptied too (two emptied pieces are one
|
|
2885
|
+
* piece, whichever emptied first). A container that held nothing before (an
|
|
2886
|
+
* empty content control or hyperlink) is markup of its own and is never merged.
|
|
2887
|
+
*/
|
|
2888
|
+
const emptyPieceOf = (empty, other, emptied) => {
|
|
2889
|
+
const emptyChildren = childNodes(empty);
|
|
2890
|
+
const otherChildren = childNodes(other);
|
|
2891
|
+
return empty.type !== "run" && emptyChildren !== void 0 && otherChildren !== void 0 && emptyChildren.length === 0 && (otherChildren.length > 0 || emptied.has(other)) && emptied.has(empty) && sameOwnFields(empty, other);
|
|
2892
|
+
};
|
|
2893
|
+
/**
|
|
2894
|
+
* An emptied piece folded into its neighbour, which keeps the first one's ids;
|
|
2895
|
+
* `undefined` when neither is one. When both are, the first stays as it is,
|
|
2896
|
+
* still emptied.
|
|
2897
|
+
*/
|
|
2898
|
+
const foldedPiece = (left, right, emptied) => {
|
|
2899
|
+
if (emptyPieceOf(right, left, emptied)) return left;
|
|
2900
|
+
if (emptyPieceOf(left, right, emptied)) return rebuildNode(left, childNodes(right) ?? []);
|
|
2901
|
+
};
|
|
2902
|
+
/**
|
|
2903
|
+
* Two records meeting beside a container resolution emptied, with no change
|
|
2904
|
+
* resolved between them: the emptied piece folds into its neighbour, down the
|
|
2905
|
+
* chain of alike containers at their facing edges when it sits inside one.
|
|
2906
|
+
* Nothing else merges there.
|
|
2907
|
+
*/
|
|
2908
|
+
const foldAtSeam = (left, right, emptied) => {
|
|
2909
|
+
const folded = foldedPiece(left, right, emptied);
|
|
2910
|
+
if (folded !== void 0) return [folded];
|
|
2911
|
+
const leftChildren = childNodes(left);
|
|
2912
|
+
const rightChildren = childNodes(right);
|
|
2913
|
+
const last = leftChildren?.at(-1);
|
|
2914
|
+
const first = rightChildren?.at(0);
|
|
2915
|
+
if (left.type === "run" || leftChildren === void 0 || rightChildren === void 0 || last === void 0 || first === void 0 || !sameOwnFields(left, right)) return [left, right];
|
|
2916
|
+
const inner = foldAtSeam(last, first, emptied);
|
|
2917
|
+
if (inner.length !== 1) return [left, right];
|
|
2918
|
+
return [rebuildNode(left, [
|
|
2919
|
+
...leftChildren.slice(0, -1),
|
|
2920
|
+
...inner,
|
|
2921
|
+
...rightChildren.slice(1)
|
|
2922
|
+
])];
|
|
2923
|
+
};
|
|
2924
|
+
/**
|
|
2925
|
+
* Two records meeting where a change was resolved, merged as far as they are
|
|
2926
|
+
* alike. A piece of a cut container that resolution emptied (one `emptied`
|
|
2927
|
+
* holds) goes into the piece it was cut from, which keeps the first one's ids.
|
|
2928
|
+
* Two alike containers merged meet inside too, and that seam is merged the
|
|
2929
|
+
* same way, emptied pieces included, so a merge leaves no seam it would merge
|
|
2930
|
+
* again.
|
|
2931
|
+
*/
|
|
2932
|
+
const mergeAtSeam = (left, right, emptied = NOTHING_EMPTIED) => {
|
|
2933
|
+
if (alikeDepth(left, right) > 0) {
|
|
2934
|
+
const leftChildren = childNodes(left);
|
|
2935
|
+
const rightChildren = childNodes(right);
|
|
2936
|
+
return leftChildren === void 0 || rightChildren === void 0 ? mergeAlike([left], [right]) : [rebuildNode(left, mergedAtSeam(leftChildren, rightChildren, emptied))];
|
|
2937
|
+
}
|
|
2938
|
+
const folded = foldedPiece(left, right, emptied);
|
|
2939
|
+
return folded === void 0 ? [left, right] : [folded];
|
|
2940
|
+
};
|
|
2941
|
+
/** Two lists end to end, merged at the seam as {@link mergeAtSeam} merges. */
|
|
2942
|
+
const mergedAtSeam = (left, right, emptied) => {
|
|
2943
|
+
const last = left.at(-1);
|
|
2944
|
+
const first = right.at(0);
|
|
2945
|
+
if (last === void 0 || first === void 0) return [...left, ...right];
|
|
2946
|
+
return [
|
|
2947
|
+
...left.slice(0, -1),
|
|
2948
|
+
...mergeAtSeam(last, first, emptied),
|
|
2949
|
+
...right.slice(1)
|
|
2950
|
+
];
|
|
2951
|
+
};
|
|
2952
|
+
/**
|
|
2953
|
+
* A list with its tracked changes resolved. Where a change was resolved,
|
|
2954
|
+
* the records left meeting are merged as far as they are alike.
|
|
2955
|
+
*/
|
|
2956
|
+
const resolveList = (nodes, resolution) => {
|
|
2957
|
+
const out = [];
|
|
2958
|
+
const seams = [];
|
|
2959
|
+
/** Seams beside an emptied container, where only its fold happens. */
|
|
2960
|
+
const folds = [];
|
|
2961
|
+
let changed = false;
|
|
2962
|
+
for (const node of nodes) {
|
|
2963
|
+
if (isTrackedWrapper(node) && resolution.ids.has(node.info.id)) {
|
|
2964
|
+
changed = true;
|
|
2965
|
+
seams.push(out.length);
|
|
2966
|
+
if (removesContent(node, resolution.decision)) continue;
|
|
2967
|
+
out.push(...resolveList(node.content, resolution).nodes);
|
|
2968
|
+
seams.push(out.length);
|
|
2969
|
+
continue;
|
|
2970
|
+
}
|
|
2971
|
+
if (node.type === "run") {
|
|
2972
|
+
const run = resolveRun(node, resolution);
|
|
2973
|
+
if (run !== node) {
|
|
2974
|
+
changed = true;
|
|
2975
|
+
seams.push(out.length, out.length + 1);
|
|
1693
2976
|
}
|
|
2977
|
+
out.push(run);
|
|
2978
|
+
continue;
|
|
1694
2979
|
}
|
|
1695
|
-
|
|
1696
|
-
|
|
1697
|
-
|
|
2980
|
+
const children = childNodes(node);
|
|
2981
|
+
const inner = children === void 0 ? void 0 : resolveList(children, resolution);
|
|
2982
|
+
if (children === void 0 || inner === void 0 || !inner.changed) {
|
|
2983
|
+
out.push(node);
|
|
2984
|
+
continue;
|
|
2985
|
+
}
|
|
2986
|
+
changed = true;
|
|
2987
|
+
if ((isTrackedWrapper(node) || resolution.decision === REVISION_DECISIONS.ACCEPT) && inner.nodes.length === 0 && children.length > 0) {
|
|
2988
|
+
seams.push(out.length);
|
|
2989
|
+
continue;
|
|
2990
|
+
}
|
|
2991
|
+
const rebuilt = rebuildNode(node, inner.nodes);
|
|
2992
|
+
const ends = inner.nodes.length === 0 ? {
|
|
2993
|
+
first: true,
|
|
2994
|
+
last: true
|
|
2995
|
+
} : emptiedEndsOf(inner.nodes, resolution.emptied);
|
|
2996
|
+
if (inner.nodes.length === 0) resolution.emptied.add(rebuilt);
|
|
2997
|
+
if (ends.first) folds.push(out.length);
|
|
2998
|
+
if (ends.last) folds.push(out.length + 1);
|
|
2999
|
+
out.push(rebuilt);
|
|
3000
|
+
}
|
|
3001
|
+
if (!changed) return {
|
|
3002
|
+
nodes: out,
|
|
3003
|
+
changed
|
|
3004
|
+
};
|
|
3005
|
+
const merges = new Set(seams);
|
|
3006
|
+
for (const seam of [.../* @__PURE__ */ new Set([...seams, ...folds])].toSorted((left, right) => right - left)) {
|
|
3007
|
+
const left = out[seam - 1];
|
|
3008
|
+
const right = out[seam];
|
|
3009
|
+
if (left === void 0 || right === void 0) continue;
|
|
3010
|
+
const met = merges.has(seam) ? mergeAtSeam(left, right, resolution.emptied) : foldAtSeam(left, right, resolution.emptied);
|
|
3011
|
+
out.splice(seam - 1, 2, ...met);
|
|
3012
|
+
}
|
|
1698
3013
|
return {
|
|
1699
|
-
|
|
1700
|
-
|
|
1701
|
-
retired: concatIds({}, retired)
|
|
3014
|
+
nodes: out,
|
|
3015
|
+
changed
|
|
1702
3016
|
};
|
|
1703
3017
|
};
|
|
1704
|
-
/**
|
|
1705
|
-
const
|
|
1706
|
-
const
|
|
1707
|
-
|
|
1708
|
-
|
|
3018
|
+
/** A paragraph's review fields once its property changes and mark are resolved; `undefined` when unchanged. */
|
|
3019
|
+
const resolveParagraphReview = (paragraph, { ids, decision }, keepsBreak) => {
|
|
3020
|
+
const review = reviewFieldsOf(paragraph);
|
|
3021
|
+
let changed = false;
|
|
3022
|
+
const changes = paragraph.propertyChanges ?? [];
|
|
3023
|
+
const selected = (change) => ids.has(change.info.id);
|
|
3024
|
+
if (changes.some(selected)) {
|
|
3025
|
+
changed = true;
|
|
3026
|
+
let remaining = changes.filter((change) => !selected(change));
|
|
3027
|
+
if (decision === REVISION_DECISIONS.REJECT) {
|
|
3028
|
+
const removed = removePropertyChanges(changes, selected);
|
|
3029
|
+
if (removed.kind !== "unchanged") remaining = removed.remaining;
|
|
3030
|
+
if (removed.kind === "restore") {
|
|
3031
|
+
const restored = withMarkFormatting(removed.previous, paragraph.formatting);
|
|
3032
|
+
if (restored === void 0) delete review.formatting;
|
|
3033
|
+
else review.formatting = restored;
|
|
3034
|
+
}
|
|
3035
|
+
}
|
|
3036
|
+
if (remaining.length > 0) review.propertyChanges = remaining;
|
|
3037
|
+
else delete review.propertyChanges;
|
|
3038
|
+
}
|
|
3039
|
+
if (keepsBreak) {
|
|
3040
|
+
changed = true;
|
|
3041
|
+
delete review.pPrMark;
|
|
3042
|
+
}
|
|
3043
|
+
return changed ? review : void 0;
|
|
1709
3044
|
};
|
|
1710
|
-
/**
|
|
1711
|
-
const
|
|
1712
|
-
const
|
|
1713
|
-
|
|
1714
|
-
|
|
1715
|
-
|
|
1716
|
-
|
|
3045
|
+
/** Revision ids resolution reaches: tracked changes and run property changes in content, and a paragraph's own. */
|
|
3046
|
+
const reachableIds = (paragraph) => {
|
|
3047
|
+
const out = /* @__PURE__ */ new Set();
|
|
3048
|
+
for (const change of paragraph.propertyChanges ?? []) out.add(change.info.id);
|
|
3049
|
+
if (paragraph.pPrMark !== void 0) out.add(paragraph.pPrMark.info.id);
|
|
3050
|
+
const visit = (nodes) => {
|
|
3051
|
+
for (const node of nodes) {
|
|
3052
|
+
if (isTrackedWrapper(node)) out.add(node.info.id);
|
|
3053
|
+
if (node.type === "run") for (const change of node.propertyChanges ?? []) out.add(change.info.id);
|
|
3054
|
+
visit(childNodes(node) ?? []);
|
|
3055
|
+
}
|
|
1717
3056
|
};
|
|
3057
|
+
visit(paragraph.content);
|
|
3058
|
+
return out;
|
|
1718
3059
|
};
|
|
1719
|
-
|
|
1720
|
-
|
|
1721
|
-
|
|
1722
|
-
|
|
1723
|
-
|
|
1724
|
-
|
|
1725
|
-
|
|
1726
|
-
|
|
1727
|
-
|
|
1728
|
-
|
|
1729
|
-
|
|
1730
|
-
|
|
1731
|
-
|
|
1732
|
-
left.push({
|
|
1733
|
-
...child,
|
|
1734
|
-
text: child.text.slice(0, offset - start)
|
|
1735
|
-
});
|
|
1736
|
-
right.push({
|
|
1737
|
-
...child,
|
|
1738
|
-
text: child.text.slice(offset - start)
|
|
1739
|
-
});
|
|
1740
|
-
}
|
|
3060
|
+
const NO_EMPTIED_ENDS = {
|
|
3061
|
+
first: false,
|
|
3062
|
+
last: false
|
|
3063
|
+
};
|
|
3064
|
+
/** The innermost record at one edge of a list, down the chain of first or last children. */
|
|
3065
|
+
const edgeRecord = (nodes, edge) => {
|
|
3066
|
+
const at = (list) => edge === "first" ? list.at(0) : list.at(-1);
|
|
3067
|
+
let node = at(nodes);
|
|
3068
|
+
while (node !== void 0) {
|
|
3069
|
+
const children = childNodes(node);
|
|
3070
|
+
const inner = children === void 0 ? void 0 : at(children);
|
|
3071
|
+
if (inner === void 0) return node;
|
|
3072
|
+
node = inner;
|
|
1741
3073
|
}
|
|
3074
|
+
return node;
|
|
3075
|
+
};
|
|
3076
|
+
/** Which edges of a list end in a record `emptied` holds. */
|
|
3077
|
+
const emptiedEndsOf = (nodes, emptied) => {
|
|
3078
|
+
const first = edgeRecord(nodes, "first");
|
|
3079
|
+
const last = edgeRecord(nodes, "last");
|
|
1742
3080
|
return {
|
|
1743
|
-
|
|
1744
|
-
|
|
3081
|
+
first: first !== void 0 && emptied.has(first),
|
|
3082
|
+
last: last !== void 0 && emptied.has(last)
|
|
1745
3083
|
};
|
|
1746
3084
|
};
|
|
1747
|
-
/**
|
|
1748
|
-
|
|
1749
|
-
|
|
1750
|
-
|
|
1751
|
-
|
|
1752
|
-
|
|
1753
|
-
|
|
1754
|
-
|
|
1755
|
-
|
|
1756
|
-
|
|
1757
|
-
|
|
1758
|
-
|
|
1759
|
-
|
|
1760
|
-
|
|
1761
|
-
|
|
1762
|
-
|
|
1763
|
-
|
|
1764
|
-
|
|
1765
|
-
|
|
1766
|
-
|
|
3085
|
+
/**
|
|
3086
|
+
* The operations that remove a paragraph's resolved mark, against the document
|
|
3087
|
+
* as it stands. `ends` is updated for the paragraph a join leaves.
|
|
3088
|
+
*/
|
|
3089
|
+
const joinOps = (document, { op, story, paraId, added }, ends) => {
|
|
3090
|
+
const body = storyBody(document, story);
|
|
3091
|
+
const location = storyParagraphs(body).find(({ paragraph }) => idKey(paragraph.paraId ?? "") === idKey(paraId));
|
|
3092
|
+
if (location === void 0) return Result.err(refusal$1(op, DOCUMENT_OP_REFUSAL_REASONS.BLOCK_NOT_FOUND, `No paragraph is ${paraId}.`));
|
|
3093
|
+
const { paragraph } = location;
|
|
3094
|
+
if (paragraph.sectionProperties !== void 0) return Result.err(refusal$1(op, DOCUMENT_OP_REFUSAL_REASONS.UNTRACKABLE, `The mark of ${paraId} ends a section: joining it is a section operation.`));
|
|
3095
|
+
const blocks = blockListAt(body.content, location.list);
|
|
3096
|
+
const next = blocks[location.index + 1];
|
|
3097
|
+
const empty = paragraphLength(paragraph) === 0;
|
|
3098
|
+
if (next?.type === "table" && next.rows.some((row) => row.structuralChange?.type === "tableRowInsertion")) return Result.err(refusal$1(op, DOCUMENT_OP_REFUSAL_REASONS.UNTRACKABLE, "Resolve the inserted table before removing its preceding paragraph break."));
|
|
3099
|
+
if (next?.type === "paragraph") {
|
|
3100
|
+
const firstEnds = ends.get(idKey(paraId)) ?? NO_EMPTIED_ENDS;
|
|
3101
|
+
const nextId = idKey(next.paraId ?? "");
|
|
3102
|
+
const nextEnds = ends.get(nextId) ?? NO_EMPTIED_ENDS;
|
|
3103
|
+
const emptied = /* @__PURE__ */ new Set();
|
|
3104
|
+
const marked = [
|
|
3105
|
+
[
|
|
3106
|
+
paragraph.content,
|
|
3107
|
+
"first",
|
|
3108
|
+
firstEnds.first
|
|
3109
|
+
],
|
|
3110
|
+
[
|
|
3111
|
+
paragraph.content,
|
|
3112
|
+
"last",
|
|
3113
|
+
firstEnds.last
|
|
3114
|
+
],
|
|
3115
|
+
[
|
|
3116
|
+
next.content,
|
|
3117
|
+
"first",
|
|
3118
|
+
nextEnds.first
|
|
3119
|
+
],
|
|
3120
|
+
[
|
|
3121
|
+
next.content,
|
|
3122
|
+
"last",
|
|
3123
|
+
nextEnds.last
|
|
3124
|
+
]
|
|
3125
|
+
];
|
|
3126
|
+
for (const [content, edge, isEmptied] of marked) {
|
|
3127
|
+
const record = edgeRecord(content, edge);
|
|
3128
|
+
if (isEmptied && record !== void 0) emptied.add(record);
|
|
1767
3129
|
}
|
|
1768
|
-
|
|
3130
|
+
const merged = mergedAtSeam(paragraph.content, next.content, emptied);
|
|
3131
|
+
ends.set(nextId, emptiedEndsOf(merged, emptied));
|
|
3132
|
+
const survivor = {
|
|
3133
|
+
...next,
|
|
3134
|
+
content: asParagraphContent(merged)
|
|
3135
|
+
};
|
|
3136
|
+
return Result.ok([{
|
|
3137
|
+
type: DOCUMENT_OP_TYPES.REPLACE_BLOCKS,
|
|
3138
|
+
story,
|
|
3139
|
+
expected: [paragraph, next],
|
|
3140
|
+
blocks: [survivor]
|
|
3141
|
+
}]);
|
|
1769
3142
|
}
|
|
3143
|
+
if (next !== void 0 && (next.type === "bookmarkStart" || next.type === "bookmarkEnd")) return Result.err(refusal$1(op, DOCUMENT_OP_REFUSAL_REASONS.UNTRACKABLE, `A block-level bookmark stands between ${paraId} and the next paragraph.`));
|
|
3144
|
+
const canGo = !(endsItsContainer(body, location) && location.list.some((step) => step.kind === "tableCell")) && (added || !endsItsContainer(body, location));
|
|
3145
|
+
if (empty && canGo && blocks.length > 1) return Result.err(refusal$1(op, DOCUMENT_OP_REFUSAL_REASONS.UNTRACKABLE, `Resolving the mark of ${paraId} removes the paragraph, which is a block operation.`));
|
|
3146
|
+
const review = reviewFieldsOf(paragraph);
|
|
3147
|
+
delete review.pPrMark;
|
|
3148
|
+
return Result.ok([{
|
|
3149
|
+
type: DOCUMENT_OP_TYPES.SET_PARAGRAPH_REVIEW,
|
|
3150
|
+
story,
|
|
3151
|
+
blockId: paraId,
|
|
3152
|
+
expected: reviewFieldsOf(paragraph),
|
|
3153
|
+
review
|
|
3154
|
+
}]);
|
|
1770
3155
|
};
|
|
3156
|
+
/** The main story's own content: the body's blocks, without its comments or section view. */
|
|
3157
|
+
const storyIdentityKeys = (document, story) => new Set(identityKeysIn(storyBody(document, story).content));
|
|
1771
3158
|
/**
|
|
1772
|
-
*
|
|
1773
|
-
*
|
|
1774
|
-
*
|
|
3159
|
+
* Resolve tracked changes by revision id, applied as the primitive
|
|
3160
|
+
* operations it expands to. An id no record in the story carries is
|
|
3161
|
+
* skipped; one carried by a record resolution does not reach yet (a table,
|
|
3162
|
+
* cell or section change, a record inside a field) is refused.
|
|
1775
3163
|
*/
|
|
1776
|
-
const
|
|
1777
|
-
|
|
1778
|
-
if (
|
|
1779
|
-
|
|
1780
|
-
|
|
1781
|
-
|
|
1782
|
-
|
|
1783
|
-
|
|
1784
|
-
if (
|
|
1785
|
-
|
|
1786
|
-
|
|
1787
|
-
|
|
3164
|
+
const resolveRevision = (document, op, applyOps) => {
|
|
3165
|
+
if (!Object.values(REVISION_DECISIONS).includes(op.decision)) return Result.err(refusal$1(op, DOCUMENT_OP_REFUSAL_REASONS.STRUCTURE_MISMATCH, "A decision accepts or rejects."));
|
|
3166
|
+
if (!op.revisionIds.every((id) => Number.isInteger(id))) return Result.err(refusal$1(op, DOCUMENT_OP_REFUSAL_REASONS.INVALID_NEW_ID, "A revision id is an integer."));
|
|
3167
|
+
const present = storyIdentityKeys(document, op.story);
|
|
3168
|
+
const ids = new Set(op.revisionIds.filter((id) => present.has(slotKey({
|
|
3169
|
+
space: IDENTITY_SPACES.REVISION,
|
|
3170
|
+
id
|
|
3171
|
+
}))));
|
|
3172
|
+
if (ids.size === 0) return Result.ok({
|
|
3173
|
+
document,
|
|
3174
|
+
inverse: [],
|
|
3175
|
+
touched: {
|
|
3176
|
+
modified: [],
|
|
3177
|
+
inserted: [],
|
|
3178
|
+
removed: []
|
|
3179
|
+
}
|
|
3180
|
+
});
|
|
3181
|
+
const resolution = {
|
|
3182
|
+
ids,
|
|
3183
|
+
decision: op.decision,
|
|
3184
|
+
emptied: /* @__PURE__ */ new WeakSet()
|
|
1788
3185
|
};
|
|
1789
|
-
|
|
1790
|
-
|
|
1791
|
-
const
|
|
1792
|
-
|
|
1793
|
-
|
|
1794
|
-
const
|
|
1795
|
-
|
|
1796
|
-
|
|
1797
|
-
|
|
3186
|
+
const paragraphs = storyParagraphs(storyBody(document, op.story));
|
|
3187
|
+
const reachable = /* @__PURE__ */ new Set([...paragraphs.flatMap(({ paragraph }) => [...reachableIds(paragraph)]), ...reachableRowIds(document, op.story)]);
|
|
3188
|
+
const unreachable = [...ids].find((id) => !reachable.has(id));
|
|
3189
|
+
if (unreachable !== void 0) return Result.err(refusal$1(op, DOCUMENT_OP_REFUSAL_REASONS.UNTRACKABLE, `Revision ${unreachable} is not an inline change, property change, row change or paragraph mark.`));
|
|
3190
|
+
const inline = [];
|
|
3191
|
+
const joins = [];
|
|
3192
|
+
const emptiedEnds = /* @__PURE__ */ new Map();
|
|
3193
|
+
for (const { paragraph } of paragraphs) {
|
|
3194
|
+
const paraId = paragraph.paraId ?? "";
|
|
3195
|
+
const resolved = resolveList(paragraph.content, resolution);
|
|
3196
|
+
if (resolved.changed) {
|
|
3197
|
+
emptiedEnds.set(idKey(paraId), emptiedEndsOf(resolved.nodes, resolution.emptied));
|
|
3198
|
+
inline.push({
|
|
3199
|
+
type: DOCUMENT_OP_TYPES.REPLACE_INLINE,
|
|
3200
|
+
story: op.story,
|
|
3201
|
+
blockId: paraId,
|
|
3202
|
+
expected: paragraph.content,
|
|
3203
|
+
content: asParagraphContent(resolved.nodes)
|
|
3204
|
+
});
|
|
3205
|
+
}
|
|
3206
|
+
const mark = paragraph.pPrMark;
|
|
3207
|
+
const markResolved = mark !== void 0 && ids.has(mark.info.id);
|
|
3208
|
+
const added = mark !== void 0 && markWasAdded(mark.kind);
|
|
3209
|
+
const keepsBreak = markResolved && added === (op.decision === REVISION_DECISIONS.ACCEPT);
|
|
3210
|
+
if (markResolved && !keepsBreak) joins.push({
|
|
3211
|
+
op,
|
|
3212
|
+
story: op.story,
|
|
3213
|
+
paraId,
|
|
3214
|
+
added
|
|
3215
|
+
});
|
|
3216
|
+
const review = resolveParagraphReview(paragraph, resolution, keepsBreak);
|
|
3217
|
+
if (review !== void 0) inline.push({
|
|
3218
|
+
type: DOCUMENT_OP_TYPES.SET_PARAGRAPH_REVIEW,
|
|
3219
|
+
story: op.story,
|
|
3220
|
+
blockId: paraId,
|
|
3221
|
+
expected: reviewFieldsOf(paragraph),
|
|
3222
|
+
review
|
|
3223
|
+
});
|
|
1798
3224
|
}
|
|
1799
|
-
const
|
|
1800
|
-
if (
|
|
1801
|
-
|
|
1802
|
-
|
|
3225
|
+
const staged = applyOps(document, inline);
|
|
3226
|
+
if (staged.isErr()) return Result.err(staged.error);
|
|
3227
|
+
const rows = resolveTableRows({
|
|
3228
|
+
document: staged.value.document,
|
|
3229
|
+
op,
|
|
3230
|
+
applyOps
|
|
3231
|
+
});
|
|
3232
|
+
if (rows.isErr()) return Result.err(rows.error);
|
|
3233
|
+
const removedParagraphs = new Set(rows.value.touched.removed.map(idKey));
|
|
3234
|
+
const edits = [staged.value, rows.value];
|
|
3235
|
+
let current = rows.value.document;
|
|
3236
|
+
for (const join of joins.toReversed()) {
|
|
3237
|
+
if (removedParagraphs.has(idKey(join.paraId))) continue;
|
|
3238
|
+
const planned = joinOps(current, join, emptiedEnds);
|
|
3239
|
+
if (planned.isErr()) return Result.err(planned.error);
|
|
3240
|
+
const joined = applyOps(current, planned.value);
|
|
3241
|
+
if (joined.isErr()) return Result.err(joined.error);
|
|
3242
|
+
edits.push(joined.value);
|
|
3243
|
+
current = joined.value.document;
|
|
3244
|
+
}
|
|
3245
|
+
return Result.ok(combineEdits(document, edits));
|
|
1803
3246
|
};
|
|
1804
|
-
|
|
1805
|
-
|
|
1806
|
-
|
|
1807
|
-
|
|
1808
|
-
|
|
1809
|
-
|
|
1810
|
-
|
|
1811
|
-
|
|
1812
|
-
|
|
1813
|
-
|
|
1814
|
-
|
|
1815
|
-
|
|
1816
|
-
|
|
1817
|
-
|
|
1818
|
-
|
|
1819
|
-
|
|
1820
|
-
|
|
1821
|
-
|
|
3247
|
+
//#endregion
|
|
3248
|
+
//#region src/ops/tableTracking.ts
|
|
3249
|
+
/** Shared tracked construction for table rows and whole tables. */
|
|
3250
|
+
const refused$1 = ({ op, reason, message }) => Result.err(new DocumentOpRefusal({
|
|
3251
|
+
opType: op.type,
|
|
3252
|
+
reason,
|
|
3253
|
+
message
|
|
3254
|
+
}));
|
|
3255
|
+
const paragraphsIn$2 = (rows) => storyParagraphs({ content: [{
|
|
3256
|
+
type: "table",
|
|
3257
|
+
rows: [...rows]
|
|
3258
|
+
}] }).map(({ paragraph }) => paragraph);
|
|
3259
|
+
/** Rebuild only paths to changed paragraphs, retaining every other record. */
|
|
3260
|
+
const mapParagraphs = (blocks, replace) => blocks.map((block) => {
|
|
3261
|
+
switch (block.type) {
|
|
3262
|
+
case "paragraph": return replace(block);
|
|
3263
|
+
case "blockSdt":
|
|
3264
|
+
case "blockCustomXml": return {
|
|
3265
|
+
...block,
|
|
3266
|
+
content: mapParagraphs(block.content, replace)
|
|
3267
|
+
};
|
|
3268
|
+
case "table": return {
|
|
3269
|
+
...block,
|
|
3270
|
+
rows: block.rows.map((row) => rowWithParagraphs(row, replace))
|
|
3271
|
+
};
|
|
3272
|
+
case "preservedBlock":
|
|
3273
|
+
case "bookmarkStart":
|
|
3274
|
+
case "bookmarkEnd": return block;
|
|
3275
|
+
default: return block;
|
|
3276
|
+
}
|
|
3277
|
+
});
|
|
3278
|
+
const rowWithParagraphs = (row, replace) => ({
|
|
3279
|
+
...row,
|
|
3280
|
+
cells: row.cells.map((cell) => ({
|
|
3281
|
+
...cell,
|
|
3282
|
+
content: mapParagraphs(cell.content, replace)
|
|
3283
|
+
}))
|
|
3284
|
+
});
|
|
3285
|
+
const trackableInline = (nodes) => nodes.every((node) => {
|
|
3286
|
+
switch (node.type) {
|
|
3287
|
+
case "moveFrom":
|
|
3288
|
+
case "moveTo":
|
|
3289
|
+
case "moveFromRangeStart":
|
|
3290
|
+
case "moveFromRangeEnd":
|
|
3291
|
+
case "moveToRangeStart":
|
|
3292
|
+
case "moveToRangeEnd":
|
|
3293
|
+
case "preservedInline":
|
|
3294
|
+
case "preservedXml": return false;
|
|
3295
|
+
default: return trackableInline(childNodes(node) ?? []);
|
|
3296
|
+
}
|
|
3297
|
+
});
|
|
3298
|
+
/** Nested tables, captured blocks and moves have no tracked-row construction. */
|
|
3299
|
+
const trackableBlocks = (blocks) => blocks.every((block) => {
|
|
3300
|
+
switch (block.type) {
|
|
3301
|
+
case "paragraph": return block.sectionProperties === void 0 && block.pPrMark?.kind !== "moveFrom" && block.pPrMark?.kind !== "moveTo" && trackableInline(block.content);
|
|
3302
|
+
case "blockSdt":
|
|
3303
|
+
case "blockCustomXml": return trackableBlocks(block.content);
|
|
3304
|
+
case "table":
|
|
3305
|
+
case "preservedBlock":
|
|
3306
|
+
case "bookmarkStart":
|
|
3307
|
+
case "bookmarkEnd": return false;
|
|
3308
|
+
default: return block;
|
|
3309
|
+
}
|
|
3310
|
+
});
|
|
3311
|
+
/** Row stamps own distinct final-cell marks as well as the inline revisions. */
|
|
3312
|
+
const trackTableRows = ({ document, op, rows, revision, newIds, kind }) => {
|
|
3313
|
+
if (rows.some((row) => row.structuralChange !== void 0)) return refused$1({
|
|
3314
|
+
op,
|
|
3315
|
+
reason: DOCUMENT_OP_REFUSAL_REASONS.REVISION_CONFLICT,
|
|
3316
|
+
message: "A row already carries a structural revision."
|
|
3317
|
+
});
|
|
3318
|
+
const finals = /* @__PURE__ */ new Set();
|
|
3319
|
+
let missingFinalParagraph = false;
|
|
3320
|
+
for (const row of rows) for (const cell of row.cells) {
|
|
3321
|
+
const body = { content: cell.content };
|
|
3322
|
+
const final = storyParagraphs(body).at(-1);
|
|
3323
|
+
if (final === void 0) return refused$1({
|
|
3324
|
+
op,
|
|
3325
|
+
reason: DOCUMENT_OP_REFUSAL_REASONS.STRUCTURE_MISMATCH,
|
|
3326
|
+
message: "Every cell must have a paragraph."
|
|
3327
|
+
});
|
|
3328
|
+
if (!endsItsContainer(body, final)) {
|
|
3329
|
+
missingFinalParagraph = true;
|
|
3330
|
+
continue;
|
|
1822
3331
|
}
|
|
1823
|
-
|
|
1824
|
-
if (position <= runOffset) insertAt = index + 1;
|
|
3332
|
+
finals.add(final.paragraph);
|
|
1825
3333
|
}
|
|
1826
|
-
|
|
1827
|
-
|
|
1828
|
-
|
|
1829
|
-
|
|
3334
|
+
if (missingFinalParagraph || rows.some((row) => row.preserved !== void 0 || row.bookmarks !== void 0 || row.contentControls !== void 0 || row.carrierStack !== void 0 || row.cells.some((cell) => cell.structuralChange !== void 0 || (cell.propertyChanges?.length ?? 0) > 0 || cell.contentControls !== void 0 || cell.carrierStack !== void 0 || !trackableBlocks(cell.content)))) return refused$1({
|
|
3335
|
+
op,
|
|
3336
|
+
reason: DOCUMENT_OP_REFUSAL_REASONS.UNTRACKABLE,
|
|
3337
|
+
message: "The rows contain an unsupported tracked structure."
|
|
1830
3338
|
});
|
|
1831
|
-
|
|
1832
|
-
|
|
1833
|
-
|
|
1834
|
-
|
|
1835
|
-
|
|
1836
|
-
|
|
1837
|
-
const
|
|
1838
|
-
|
|
1839
|
-
|
|
1840
|
-
|
|
1841
|
-
|
|
1842
|
-
|
|
1843
|
-
|
|
1844
|
-
|
|
1845
|
-
|
|
1846
|
-
|
|
1847
|
-
|
|
1848
|
-
|
|
1849
|
-
|
|
1850
|
-
|
|
3339
|
+
if ([...finals].some((paragraph) => paragraph.pPrMark !== void 0)) return refused$1({
|
|
3340
|
+
op,
|
|
3341
|
+
reason: DOCUMENT_OP_REFUSAL_REASONS.REVISION_CONFLICT,
|
|
3342
|
+
message: "A cell's final paragraph already carries a mark revision."
|
|
3343
|
+
});
|
|
3344
|
+
const before = paragraphsIn$2(rows);
|
|
3345
|
+
const outside = new Set(packageIdentityKeys(document.package));
|
|
3346
|
+
if (op.type === DOCUMENT_OP_TYPES.DELETE_ROW || op.type === DOCUMENT_OP_TYPES.DELETE_TABLE) for (const key of identityKeysIn(rows)) outside.delete(key);
|
|
3347
|
+
const taken = /* @__PURE__ */ new Set([...outside, ...identityKeysIn(rows)]);
|
|
3348
|
+
const firstKey = slotKey({
|
|
3349
|
+
space: IDENTITY_SPACES.REVISION,
|
|
3350
|
+
id: revision.id
|
|
3351
|
+
});
|
|
3352
|
+
outside.add(firstKey);
|
|
3353
|
+
taken.add(firstKey);
|
|
3354
|
+
const rowStamps = [];
|
|
3355
|
+
const pool = newIds?.revision ?? [];
|
|
3356
|
+
let cursor = 0;
|
|
3357
|
+
for (const [index] of rows.entries()) {
|
|
3358
|
+
if (index === 0) {
|
|
3359
|
+
rowStamps.push(revision);
|
|
3360
|
+
continue;
|
|
1851
3361
|
}
|
|
1852
|
-
|
|
3362
|
+
let id = pool[cursor];
|
|
3363
|
+
while (id !== void 0 && Number.isInteger(id) && taken.has(slotKey({
|
|
3364
|
+
space: IDENTITY_SPACES.REVISION,
|
|
3365
|
+
id
|
|
3366
|
+
}))) {
|
|
3367
|
+
cursor += 1;
|
|
3368
|
+
id = pool[cursor];
|
|
3369
|
+
}
|
|
3370
|
+
cursor += 1;
|
|
3371
|
+
if (id === void 0) return refused$1({
|
|
3372
|
+
op,
|
|
3373
|
+
reason: DOCUMENT_OP_REFUSAL_REASONS.NEEDS_NEW_IDS,
|
|
3374
|
+
message: `The rows need ${rows.length - index} additional structural revision ids.`
|
|
3375
|
+
});
|
|
3376
|
+
if (!Number.isInteger(id) || id < 0 || id > 2147483647) return refused$1({
|
|
3377
|
+
op,
|
|
3378
|
+
reason: DOCUMENT_OP_REFUSAL_REASONS.INVALID_NEW_ID,
|
|
3379
|
+
message: `${id} cannot be a new revision id.`
|
|
3380
|
+
});
|
|
3381
|
+
const key = slotKey({
|
|
3382
|
+
space: IDENTITY_SPACES.REVISION,
|
|
3383
|
+
id
|
|
3384
|
+
});
|
|
3385
|
+
taken.add(key);
|
|
3386
|
+
outside.add(key);
|
|
3387
|
+
rowStamps.push({
|
|
3388
|
+
...revision,
|
|
3389
|
+
id
|
|
3390
|
+
});
|
|
3391
|
+
}
|
|
3392
|
+
const after = [];
|
|
3393
|
+
for (const paragraph of before) {
|
|
3394
|
+
const wrapped = wrapTracked({
|
|
3395
|
+
items: paragraph.content,
|
|
3396
|
+
from: {
|
|
3397
|
+
offset: 0,
|
|
3398
|
+
zeroWidthBefore: 0
|
|
3399
|
+
},
|
|
3400
|
+
to: {
|
|
3401
|
+
offset: paragraphLength(paragraph),
|
|
3402
|
+
zeroWidthBefore: Number.MAX_SAFE_INTEGER
|
|
3403
|
+
},
|
|
3404
|
+
kind,
|
|
3405
|
+
stamp: revision
|
|
3406
|
+
});
|
|
3407
|
+
if (wrapped.kind === "refused") return refused$1({
|
|
3408
|
+
op,
|
|
3409
|
+
reason: wrapped.reason,
|
|
3410
|
+
message: "The rows' content cannot record this revision."
|
|
3411
|
+
});
|
|
3412
|
+
const content = wrapped.kind === "unchanged" ? paragraph.content : wrapped.content;
|
|
3413
|
+
if (finals.has(paragraph)) {
|
|
3414
|
+
after.push({
|
|
3415
|
+
...paragraph,
|
|
3416
|
+
content,
|
|
3417
|
+
pPrMark: {
|
|
3418
|
+
kind: kind === WRAP_KINDS.INSERTION ? "ins" : "del",
|
|
3419
|
+
info: stampInfo(revision)
|
|
3420
|
+
}
|
|
3421
|
+
});
|
|
3422
|
+
continue;
|
|
3423
|
+
}
|
|
3424
|
+
after.push(content === paragraph.content ? paragraph : {
|
|
3425
|
+
...paragraph,
|
|
3426
|
+
content
|
|
3427
|
+
});
|
|
3428
|
+
}
|
|
3429
|
+
const freshened = freshenIdentities({
|
|
3430
|
+
before,
|
|
3431
|
+
after,
|
|
3432
|
+
newIds: {
|
|
3433
|
+
...newIds,
|
|
3434
|
+
revision: pool.slice(cursor)
|
|
3435
|
+
},
|
|
3436
|
+
usedElsewhere: () => outside
|
|
3437
|
+
});
|
|
3438
|
+
switch (freshened.kind) {
|
|
3439
|
+
case "needsIds": return refused$1({
|
|
3440
|
+
op,
|
|
3441
|
+
reason: DOCUMENT_OP_REFUSAL_REASONS.NEEDS_NEW_IDS,
|
|
3442
|
+
message: `The rows need ${freshened.missing} additional revision ids.`
|
|
3443
|
+
});
|
|
3444
|
+
case "invalidId": return refused$1({
|
|
3445
|
+
op,
|
|
3446
|
+
reason: DOCUMENT_OP_REFUSAL_REASONS.INVALID_NEW_ID,
|
|
3447
|
+
message: `${freshened.id} cannot be a new revision id.`
|
|
3448
|
+
});
|
|
3449
|
+
case "fresh": {
|
|
3450
|
+
const byId = new Map(freshened.paragraphs.map((paragraph) => [idKey(paragraph.paraId ?? ""), paragraph]));
|
|
3451
|
+
return Result.ok(rows.map((row, index) => {
|
|
3452
|
+
const stamp = rowStamps[index] ?? panic("A tracked row lost its structural stamp.");
|
|
3453
|
+
return {
|
|
3454
|
+
...rowWithParagraphs(row, (paragraph) => byId.get(idKey(paragraph.paraId ?? "")) ?? panic("A tracked row lost a paragraph.")),
|
|
3455
|
+
structuralChange: {
|
|
3456
|
+
type: kind === WRAP_KINDS.INSERTION ? "tableRowInsertion" : "tableRowDeletion",
|
|
3457
|
+
info: stampInfo(stamp)
|
|
3458
|
+
}
|
|
3459
|
+
};
|
|
3460
|
+
}));
|
|
3461
|
+
}
|
|
3462
|
+
default: return freshened;
|
|
3463
|
+
}
|
|
3464
|
+
};
|
|
3465
|
+
/** A cell-final mark is legal only when its innermost row owns its stamp. */
|
|
3466
|
+
const permitsCellFinalMark = (body, location) => {
|
|
3467
|
+
const mark = location.paragraph.pPrMark;
|
|
3468
|
+
if (mark === void 0 || !endsItsContainer(body, location)) return false;
|
|
3469
|
+
const stepIndex = location.list.findLastIndex((step) => step.kind === "tableCell");
|
|
3470
|
+
const step = location.list[stepIndex];
|
|
3471
|
+
if (step?.kind !== "tableCell") return false;
|
|
3472
|
+
const table = blockListAt(body.content, location.list.slice(0, stepIndex))[step.block];
|
|
3473
|
+
const change = table?.type === "table" ? table.rows[step.row]?.structuralChange : void 0;
|
|
3474
|
+
if (change === void 0 || mark.info.id === change.info.id || !structurallyEqual({
|
|
3475
|
+
...mark.info,
|
|
3476
|
+
id: 0
|
|
3477
|
+
}, {
|
|
3478
|
+
...change.info,
|
|
3479
|
+
id: 0
|
|
3480
|
+
})) return false;
|
|
3481
|
+
switch (change.type) {
|
|
3482
|
+
case "tableRowInsertion": return mark.kind === "ins";
|
|
3483
|
+
case "tableRowDeletion": return mark.kind === "del";
|
|
3484
|
+
case "tableCellInsertion":
|
|
3485
|
+
case "tableCellDeletion":
|
|
3486
|
+
case "tableCellMerge": return false;
|
|
3487
|
+
default: return change;
|
|
1853
3488
|
}
|
|
1854
|
-
|
|
1855
|
-
|
|
3489
|
+
};
|
|
3490
|
+
/** Newly stamped row structural ids, including rows below wrappers and cells. */
|
|
3491
|
+
const stampedTableRowRevisionIds = (blocks, stamp, known) => {
|
|
3492
|
+
const out = [];
|
|
3493
|
+
const visit = (children) => {
|
|
3494
|
+
for (const block of children) switch (block.type) {
|
|
3495
|
+
case "table":
|
|
3496
|
+
for (const row of block.rows) {
|
|
3497
|
+
const change = row.structuralChange;
|
|
3498
|
+
if (change !== void 0 && (change.type === "tableRowInsertion" || change.type === "tableRowDeletion") && carriesStamp(change.info, stamp) && !known.has(slotKey({
|
|
3499
|
+
space: IDENTITY_SPACES.REVISION,
|
|
3500
|
+
id: change.info.id
|
|
3501
|
+
})) && !out.includes(change.info.id)) out.push(change.info.id);
|
|
3502
|
+
for (const cell of row.cells) visit(cell.content);
|
|
3503
|
+
}
|
|
3504
|
+
break;
|
|
3505
|
+
case "blockSdt":
|
|
3506
|
+
case "blockCustomXml":
|
|
3507
|
+
visit(block.content);
|
|
3508
|
+
break;
|
|
3509
|
+
case "paragraph":
|
|
3510
|
+
case "preservedBlock":
|
|
3511
|
+
case "bookmarkStart":
|
|
3512
|
+
case "bookmarkEnd": break;
|
|
3513
|
+
default: return block;
|
|
3514
|
+
}
|
|
3515
|
+
};
|
|
3516
|
+
visit(blocks);
|
|
1856
3517
|
return out;
|
|
1857
3518
|
};
|
|
1858
|
-
|
|
1859
|
-
|
|
1860
|
-
|
|
1861
|
-
|
|
1862
|
-
|
|
1863
|
-
|
|
1864
|
-
|
|
1865
|
-
|
|
1866
|
-
|
|
1867
|
-
|
|
1868
|
-
const
|
|
1869
|
-
if (
|
|
1870
|
-
|
|
1871
|
-
if (halves.right !== void 0) replacement.push(halves.right);
|
|
1872
|
-
return replaceAtPath(items, host.path, replacement);
|
|
3519
|
+
//#endregion
|
|
3520
|
+
//#region src/ops/tables.ts
|
|
3521
|
+
/** Whole-table edits and their exact structural inverse. */
|
|
3522
|
+
const refuse$1 = ({ op, reason, message }) => Result.err(new DocumentOpRefusal({
|
|
3523
|
+
opType: op.type,
|
|
3524
|
+
reason,
|
|
3525
|
+
message
|
|
3526
|
+
}));
|
|
3527
|
+
const paragraphsIn$1 = (blocks) => storyParagraphs({ content: [...blocks] }).map(({ paragraph }) => paragraph);
|
|
3528
|
+
const endsInParagraph = (blocks) => {
|
|
3529
|
+
const last = blocks.at(-1);
|
|
3530
|
+
if (last?.type === "blockSdt" || last?.type === "blockCustomXml") return endsInParagraph(last.content);
|
|
3531
|
+
return last?.type === "paragraph";
|
|
1873
3532
|
};
|
|
1874
|
-
|
|
1875
|
-
|
|
1876
|
-
|
|
1877
|
-
|
|
1878
|
-
|
|
1879
|
-
|
|
1880
|
-
|
|
1881
|
-
|
|
3533
|
+
/** Tables and cells must have usable content and end in a paragraph. */
|
|
3534
|
+
const validStructure = (blocks) => blocks.every((block) => {
|
|
3535
|
+
switch (block.type) {
|
|
3536
|
+
case "table": return block.rows.length > 0 && block.rows.every((row) => row.cells.length > 0 && row.cells.every((cell) => endsInParagraph(cell.content) && validStructure(cell.content)));
|
|
3537
|
+
case "blockSdt":
|
|
3538
|
+
case "blockCustomXml": return validStructure(block.content);
|
|
3539
|
+
case "paragraph":
|
|
3540
|
+
case "preservedBlock":
|
|
3541
|
+
case "bookmarkStart":
|
|
3542
|
+
case "bookmarkEnd": return true;
|
|
3543
|
+
default: return block;
|
|
3544
|
+
}
|
|
3545
|
+
});
|
|
3546
|
+
const permitsStructuralCellMark = ({ body, location, op }) => {
|
|
3547
|
+
if (permitsCellFinalMark(body, location)) return true;
|
|
3548
|
+
const mark = location.paragraph.pPrMark;
|
|
3549
|
+
return op.type === DOCUMENT_OP_TYPES.SET_CONTAINER_BLOCKS && location.list.some((step) => step.kind === "tableCell") && (mark?.kind === "ins" || mark?.kind === "del");
|
|
3550
|
+
};
|
|
3551
|
+
const commitBlocks = ({ document, op, anchor, blocks }) => {
|
|
3552
|
+
const body = storyBody(document, op.story);
|
|
3553
|
+
const before = blockListAt(body.content, anchor.list);
|
|
3554
|
+
if (structurallyEqual(before, blocks)) return Result.ok({
|
|
3555
|
+
document,
|
|
3556
|
+
inverse: [],
|
|
3557
|
+
touched: {
|
|
3558
|
+
modified: [],
|
|
3559
|
+
inserted: [],
|
|
3560
|
+
removed: []
|
|
3561
|
+
}
|
|
3562
|
+
});
|
|
3563
|
+
if (!blocks.some((block) => block.type === "paragraph" && idKey(block.paraId ?? "") === idKey(anchor.paragraph.paraId ?? ""))) return refuse$1({
|
|
3564
|
+
op,
|
|
3565
|
+
reason: DOCUMENT_OP_REFUSAL_REASONS.UNTRACKABLE,
|
|
3566
|
+
message: "The structural inverse requires a surviving paragraph in this block list."
|
|
3567
|
+
});
|
|
3568
|
+
if (blocks.length === 0 || !validStructure(before) || !validStructure(blocks) || endsItsContainer(body, {
|
|
3569
|
+
list: anchor.list,
|
|
3570
|
+
index: before.length - 1
|
|
3571
|
+
}) && (!endsInParagraph(before) || !endsInParagraph(blocks))) return refuse$1({
|
|
3572
|
+
op,
|
|
3573
|
+
reason: DOCUMENT_OP_REFUSAL_REASONS.STRUCTURE_MISMATCH,
|
|
3574
|
+
message: "A table has rows and cells; each cell and the story retain a final paragraph."
|
|
3575
|
+
});
|
|
3576
|
+
const fixedBlocks = (items) => items.filter((block) => block.type !== "table" && block.type !== "paragraph");
|
|
3577
|
+
if (!structurallyEqual(fixedBlocks(before), fixedBlocks(blocks))) return refuse$1({
|
|
3578
|
+
op,
|
|
3579
|
+
reason: DOCUMENT_OP_REFUSAL_REASONS.UNTRACKABLE,
|
|
3580
|
+
message: "A table structural inverse preserves surrounding block wrappers and markers."
|
|
3581
|
+
});
|
|
3582
|
+
const beforeParagraphs = paragraphsIn$1(before);
|
|
3583
|
+
const afterParagraphs = paragraphsIn$1(blocks);
|
|
3584
|
+
const beforeById = new Map(beforeParagraphs.map((paragraph) => [idKey(paragraph.paraId ?? ""), paragraph]));
|
|
3585
|
+
const afterById = new Map(afterParagraphs.map((paragraph) => [idKey(paragraph.paraId ?? ""), paragraph]));
|
|
3586
|
+
if ([...beforeParagraphs, ...afterParagraphs].some((paragraph) => {
|
|
3587
|
+
const id = idKey(paragraph.paraId ?? "");
|
|
3588
|
+
return !structurallyEqual(paragraph.sectionProperties, (beforeById.has(id) ? afterById : beforeById).get(id)?.sectionProperties);
|
|
3589
|
+
})) return refuse$1({
|
|
3590
|
+
op,
|
|
3591
|
+
reason: DOCUMENT_OP_REFUSAL_REASONS.SECTION_BOUNDARY,
|
|
3592
|
+
message: "Table edits preserve section boundaries."
|
|
3593
|
+
});
|
|
3594
|
+
if ([...beforeParagraphs, ...afterParagraphs].some(({ paraId }) => paraId === void 0 || (!beforeById.has(idKey(paraId)) || !afterById.has(idKey(paraId))) && !isParaId(paraId))) return refuse$1({
|
|
3595
|
+
op,
|
|
3596
|
+
reason: DOCUMENT_OP_REFUSAL_REASONS.INVALID_BLOCK_ID,
|
|
3597
|
+
message: "An added or removed paragraph needs a usable id."
|
|
3598
|
+
});
|
|
3599
|
+
if ([...beforeParagraphs, ...afterParagraphs].some(({ content }) => textsIn(content).some(hasIllegalXmlCharacters))) return refuse$1({
|
|
3600
|
+
op,
|
|
3601
|
+
reason: DOCUMENT_OP_REFUSAL_REASONS.INVALID_TEXT,
|
|
3602
|
+
message: "The table holds text that cannot be written to XML."
|
|
3603
|
+
});
|
|
3604
|
+
for (const location of storyParagraphs(body)) {
|
|
3605
|
+
const paragraph = location.paragraph;
|
|
3606
|
+
if (paragraph.pPrMark !== void 0 && beforeById.has(idKey(paragraph.paraId ?? "")) && endsItsContainer(body, location) && !permitsStructuralCellMark({
|
|
3607
|
+
body,
|
|
3608
|
+
location,
|
|
3609
|
+
op
|
|
3610
|
+
}) && !structurallyEqual(paragraph.pPrMark, afterById.get(idKey(paragraph.paraId ?? ""))?.pPrMark)) return refuse$1({
|
|
3611
|
+
op,
|
|
3612
|
+
reason: DOCUMENT_OP_REFUSAL_REASONS.CONTAINER_FINAL_MARK,
|
|
3613
|
+
message: "Removing this final paragraph mark would make the structural inverse invalid."
|
|
3614
|
+
});
|
|
3615
|
+
}
|
|
3616
|
+
const remaining = countIds(packageParagraphIds(document.package));
|
|
3617
|
+
for (const id of paragraphIdsIn(before)) remaining.set(idKey(id), (remaining.get(idKey(id)) ?? 1) - 1);
|
|
3618
|
+
if (collides(remaining, paragraphIdsIn(blocks))) return refuse$1({
|
|
3619
|
+
op,
|
|
3620
|
+
reason: DOCUMENT_OP_REFUSAL_REASONS.ID_COLLISION,
|
|
3621
|
+
message: "A paragraph id is already used in the package."
|
|
3622
|
+
});
|
|
3623
|
+
const records = countKeys(packageIdentityKeys(document.package));
|
|
3624
|
+
for (const key of identityKeysIn(before)) records.set(key, (records.get(key) ?? 1) - 1);
|
|
3625
|
+
const incoming = identityKeysIn(blocks);
|
|
3626
|
+
if (incoming.some((key) => (records.get(key) ?? 0) > 0) || new Set(incoming).size !== incoming.length) return refuse$1({
|
|
3627
|
+
op,
|
|
3628
|
+
reason: DOCUMENT_OP_REFUSAL_REASONS.ID_COLLISION,
|
|
3629
|
+
message: "A revision or content-control id is already used in the package."
|
|
3630
|
+
});
|
|
3631
|
+
const beforeBlocksById = new Map(before.flatMap((block) => {
|
|
3632
|
+
const id = paragraphIdsIn(block).at(0);
|
|
3633
|
+
return id === void 0 ? [] : [[idKey(id), block]];
|
|
3634
|
+
}));
|
|
3635
|
+
const reuseParagraphs = (items) => items.map((block) => {
|
|
3636
|
+
switch (block.type) {
|
|
3637
|
+
case "paragraph": {
|
|
3638
|
+
const known = beforeById.get(idKey(block.paraId ?? ""));
|
|
3639
|
+
return known !== void 0 && structurallyEqual(known, block) ? known : block;
|
|
1882
3640
|
}
|
|
1883
|
-
|
|
1884
|
-
|
|
1885
|
-
|
|
1886
|
-
|
|
3641
|
+
case "table": return {
|
|
3642
|
+
...block,
|
|
3643
|
+
rows: block.rows.map((row) => ({
|
|
3644
|
+
...row,
|
|
3645
|
+
cells: row.cells.map((cell) => ({
|
|
3646
|
+
...cell,
|
|
3647
|
+
content: reuseParagraphs(cell.content)
|
|
3648
|
+
}))
|
|
3649
|
+
}))
|
|
3650
|
+
};
|
|
3651
|
+
case "blockSdt":
|
|
3652
|
+
case "blockCustomXml": return {
|
|
3653
|
+
...block,
|
|
3654
|
+
content: reuseParagraphs(block.content)
|
|
3655
|
+
};
|
|
3656
|
+
case "preservedBlock":
|
|
3657
|
+
case "bookmarkStart":
|
|
3658
|
+
case "bookmarkEnd": return block;
|
|
3659
|
+
default: return block;
|
|
1887
3660
|
}
|
|
1888
|
-
|
|
1889
|
-
|
|
1890
|
-
|
|
1891
|
-
|
|
1892
|
-
|
|
1893
|
-
|
|
3661
|
+
});
|
|
3662
|
+
const replacement = blocks.map((block) => {
|
|
3663
|
+
const id = paragraphIdsIn(block).at(0);
|
|
3664
|
+
const existing = id === void 0 ? before.find((candidate) => structurallyEqual(candidate, block)) : beforeBlocksById.get(idKey(id));
|
|
3665
|
+
if (existing !== void 0 && structurallyEqual(existing, block)) return existing;
|
|
3666
|
+
const cloned = reuseParagraphs([structuredClone(block)]).at(0);
|
|
3667
|
+
if (cloned === void 0) return panic("Cloning one block must preserve one block.");
|
|
3668
|
+
return cloned;
|
|
3669
|
+
});
|
|
3670
|
+
const content = updateBlockList(body.content, anchor.list, () => replacement);
|
|
3671
|
+
const next = {
|
|
3672
|
+
...document,
|
|
3673
|
+
package: {
|
|
3674
|
+
...document.package,
|
|
3675
|
+
document: withBodyContent(body, content)
|
|
1894
3676
|
}
|
|
1895
|
-
|
|
3677
|
+
};
|
|
3678
|
+
const nextBody = storyBody(next, op.story);
|
|
3679
|
+
const previousParagraphs = new Map(storyParagraphs(body).map(({ paragraph }) => [idKey(paragraph.paraId ?? ""), paragraph]));
|
|
3680
|
+
for (const location of storyParagraphs(nextBody)) {
|
|
3681
|
+
const paragraph = location.paragraph;
|
|
3682
|
+
if (paragraph.pPrMark !== void 0 && endsItsContainer(nextBody, location) && !permitsStructuralCellMark({
|
|
3683
|
+
body: nextBody,
|
|
3684
|
+
location,
|
|
3685
|
+
op
|
|
3686
|
+
}) && !structurallyEqual(paragraph.pPrMark, previousParagraphs.get(idKey(paragraph.paraId ?? ""))?.pPrMark)) return refuse$1({
|
|
3687
|
+
op,
|
|
3688
|
+
reason: DOCUMENT_OP_REFUSAL_REASONS.CONTAINER_FINAL_MARK,
|
|
3689
|
+
message: "A table edit cannot add a final paragraph mark."
|
|
3690
|
+
});
|
|
1896
3691
|
}
|
|
1897
|
-
|
|
3692
|
+
const valid = validateOpsDocument(next);
|
|
3693
|
+
if (valid.isErr()) return refuse$1({
|
|
3694
|
+
op,
|
|
3695
|
+
reason: valid.error.reason,
|
|
3696
|
+
message: valid.error.message
|
|
3697
|
+
});
|
|
3698
|
+
return Result.ok({
|
|
3699
|
+
document: next,
|
|
3700
|
+
inverse: [{
|
|
3701
|
+
type: DOCUMENT_OP_TYPES.SET_CONTAINER_BLOCKS,
|
|
3702
|
+
story: op.story,
|
|
3703
|
+
blockId: anchor.paragraph.paraId ?? "",
|
|
3704
|
+
expected: replacement,
|
|
3705
|
+
blocks: before
|
|
3706
|
+
}],
|
|
3707
|
+
touched: {
|
|
3708
|
+
modified: afterParagraphs.filter((paragraph) => beforeById.has(idKey(paragraph.paraId ?? "")) && !structurallyEqual(paragraph, beforeById.get(idKey(paragraph.paraId ?? "")))).flatMap(({ paraId }) => paraId === void 0 ? [] : [paraId]),
|
|
3709
|
+
inserted: afterParagraphs.filter(({ paraId }) => !beforeById.has(idKey(paraId ?? ""))).flatMap(({ paraId }) => paraId === void 0 ? [] : [paraId]),
|
|
3710
|
+
removed: beforeParagraphs.filter(({ paraId }) => !afterById.has(idKey(paraId ?? ""))).flatMap(({ paraId }) => paraId === void 0 ? [] : [paraId])
|
|
3711
|
+
}
|
|
3712
|
+
});
|
|
1898
3713
|
};
|
|
1899
|
-
const
|
|
1900
|
-
|
|
1901
|
-
|
|
1902
|
-
|
|
1903
|
-
|
|
3714
|
+
const applyTableOp = (document, op) => {
|
|
3715
|
+
const body = storyBody(document, op.story);
|
|
3716
|
+
switch (op.type) {
|
|
3717
|
+
case DOCUMENT_OP_TYPES.INSERT_TABLE: {
|
|
3718
|
+
if (op.at.type !== "before" && op.at.type !== "after") return refuse$1({
|
|
3719
|
+
op,
|
|
3720
|
+
reason: DOCUMENT_OP_REFUSAL_REASONS.STRUCTURE_MISMATCH,
|
|
3721
|
+
message: "Insert before or after a paragraph."
|
|
3722
|
+
});
|
|
3723
|
+
const anchor = storyParagraphs(body).find(({ paragraph }) => idKey(paragraph.paraId ?? "") === idKey(op.at.blockId));
|
|
3724
|
+
if (anchor === void 0) return refuse$1({
|
|
3725
|
+
op,
|
|
3726
|
+
reason: DOCUMENT_OP_REFUSAL_REASONS.BLOCK_NOT_FOUND,
|
|
3727
|
+
message: "The table insertion anchor does not exist."
|
|
3728
|
+
});
|
|
3729
|
+
const blocks = [...blockListAt(body.content, anchor.list)];
|
|
3730
|
+
const terminal = op.at.type === "after" && endsItsContainer(body, anchor);
|
|
3731
|
+
if (op.terminal !== void 0 && !terminal) return refuse$1({
|
|
3732
|
+
op,
|
|
3733
|
+
reason: DOCUMENT_OP_REFUSAL_REASONS.STRUCTURE_MISMATCH,
|
|
3734
|
+
message: "A terminal insertion must follow its container's final paragraph."
|
|
3735
|
+
});
|
|
3736
|
+
if (terminal && op.terminal === void 0) return refuse$1({
|
|
3737
|
+
op,
|
|
3738
|
+
reason: DOCUMENT_OP_REFUSAL_REASONS.STRUCTURE_MISMATCH,
|
|
3739
|
+
message: "A terminal table insertion supplies the preceding paragraph id."
|
|
3740
|
+
});
|
|
3741
|
+
let preceding;
|
|
3742
|
+
if (op.terminal !== void 0) {
|
|
3743
|
+
if (op.revision !== void 0 && anchor.paragraph.pPrMark !== void 0) return refuse$1({
|
|
3744
|
+
op,
|
|
3745
|
+
reason: DOCUMENT_OP_REFUSAL_REASONS.REVISION_CONFLICT,
|
|
3746
|
+
message: "The final paragraph already carries a mark revision."
|
|
3747
|
+
});
|
|
3748
|
+
preceding = {
|
|
3749
|
+
type: "paragraph",
|
|
3750
|
+
paraId: op.terminal.beforeBlockId,
|
|
3751
|
+
...anchor.paragraph.formatting === void 0 ? {} : { formatting: anchor.paragraph.formatting },
|
|
3752
|
+
content: anchor.paragraph.content
|
|
3753
|
+
};
|
|
3754
|
+
blocks.splice(anchor.index, 1, preceding, op.table, {
|
|
3755
|
+
...anchor.paragraph,
|
|
3756
|
+
content: []
|
|
3757
|
+
});
|
|
3758
|
+
} else blocks.splice(anchor.index + (op.at.type === "after" ? 1 : 0), 0, op.table);
|
|
3759
|
+
if (op.revision === void 0) return commitBlocks({
|
|
3760
|
+
document,
|
|
3761
|
+
op,
|
|
3762
|
+
anchor,
|
|
3763
|
+
blocks
|
|
3764
|
+
});
|
|
3765
|
+
if (op.table.preserved !== void 0 || op.table.bookmarks !== void 0 || op.table.carrierStack !== void 0 || (op.table.propertyChanges?.length ?? 0) > 0) return refuse$1({
|
|
3766
|
+
op,
|
|
3767
|
+
reason: DOCUMENT_OP_REFUSAL_REASONS.UNTRACKABLE,
|
|
3768
|
+
message: "Tracked table construction does not support captured markup or table property revisions."
|
|
3769
|
+
});
|
|
3770
|
+
const tracked = trackTableRows({
|
|
3771
|
+
document,
|
|
3772
|
+
op,
|
|
3773
|
+
rows: op.table.rows,
|
|
3774
|
+
revision: op.revision,
|
|
3775
|
+
newIds: op.newIds,
|
|
3776
|
+
kind: WRAP_KINDS.INSERTION
|
|
3777
|
+
});
|
|
3778
|
+
if (tracked.isErr()) return Result.err(tracked.error);
|
|
3779
|
+
const direct = commitBlocks({
|
|
3780
|
+
document,
|
|
3781
|
+
op,
|
|
3782
|
+
anchor,
|
|
3783
|
+
blocks
|
|
3784
|
+
});
|
|
3785
|
+
if (direct.isErr()) return direct;
|
|
3786
|
+
const table = {
|
|
3787
|
+
...op.table,
|
|
3788
|
+
rows: tracked.value
|
|
3789
|
+
};
|
|
3790
|
+
const tableIndex = anchor.index + (op.at.type === "after" ? 1 : 0);
|
|
3791
|
+
blocks[tableIndex] = table;
|
|
3792
|
+
if (preceding !== void 0) {
|
|
3793
|
+
const outside = new Set(packageIdentityKeys(document.package));
|
|
3794
|
+
for (const key of identityKeysIn(anchor.paragraph)) outside.delete(key);
|
|
3795
|
+
for (const key of identityKeysIn(table)) outside.add(key);
|
|
3796
|
+
const fresh = freshenIdentities({
|
|
3797
|
+
before: [anchor.paragraph],
|
|
3798
|
+
after: [{
|
|
3799
|
+
...preceding,
|
|
3800
|
+
pPrMark: {
|
|
3801
|
+
kind: "ins",
|
|
3802
|
+
info: stampInfo(op.revision)
|
|
3803
|
+
}
|
|
3804
|
+
}],
|
|
3805
|
+
newIds: op.newIds ?? {},
|
|
3806
|
+
usedElsewhere: () => outside
|
|
3807
|
+
});
|
|
3808
|
+
switch (fresh.kind) {
|
|
3809
|
+
case "needsIds": return refuse$1({
|
|
3810
|
+
op,
|
|
3811
|
+
reason: DOCUMENT_OP_REFUSAL_REASONS.NEEDS_NEW_IDS,
|
|
3812
|
+
message: "The preceding table break needs an additional revision id."
|
|
3813
|
+
});
|
|
3814
|
+
case "invalidId": return refuse$1({
|
|
3815
|
+
op,
|
|
3816
|
+
reason: DOCUMENT_OP_REFUSAL_REASONS.INVALID_NEW_ID,
|
|
3817
|
+
message: "The preceding table break has an invalid revision id."
|
|
3818
|
+
});
|
|
3819
|
+
case "fresh": {
|
|
3820
|
+
const paragraph = fresh.paragraphs.at(0);
|
|
3821
|
+
if (paragraph === void 0) return panic("A terminal table insertion must retain its preceding paragraph.");
|
|
3822
|
+
blocks[anchor.index] = paragraph;
|
|
3823
|
+
break;
|
|
3824
|
+
}
|
|
3825
|
+
default: return fresh;
|
|
3826
|
+
}
|
|
3827
|
+
}
|
|
3828
|
+
return commitBlocks({
|
|
3829
|
+
document,
|
|
3830
|
+
op,
|
|
3831
|
+
anchor,
|
|
3832
|
+
blocks
|
|
3833
|
+
});
|
|
1904
3834
|
}
|
|
1905
|
-
|
|
3835
|
+
case DOCUMENT_OP_TYPES.DELETE_TABLE: {
|
|
3836
|
+
const located = locateTableRow(document, op);
|
|
3837
|
+
if (located.isErr()) return Result.err(located.error);
|
|
3838
|
+
const location = located.value;
|
|
3839
|
+
if (op.expected !== void 0 && !equalForStaleness(location.table, op.expected)) return refuse$1({
|
|
3840
|
+
op,
|
|
3841
|
+
reason: DOCUMENT_OP_REFUSAL_REASONS.STALE,
|
|
3842
|
+
message: "The table to remove has changed."
|
|
3843
|
+
});
|
|
3844
|
+
const anchor = storyParagraphs(body).find((paragraph) => sameBlockList(paragraph.list, location.list));
|
|
3845
|
+
if (anchor === void 0) return refuse$1({
|
|
3846
|
+
op,
|
|
3847
|
+
reason: DOCUMENT_OP_REFUSAL_REASONS.UNTRACKABLE,
|
|
3848
|
+
message: "Removing a table requires a surviving paragraph in its block list."
|
|
3849
|
+
});
|
|
3850
|
+
const blocks = [...blockListAt(body.content, location.list)];
|
|
3851
|
+
if (op.revision === void 0) blocks.splice(location.index, 1);
|
|
3852
|
+
else {
|
|
3853
|
+
if (location.table.preserved !== void 0 || location.table.bookmarks !== void 0 || location.table.carrierStack !== void 0 || (location.table.propertyChanges?.length ?? 0) > 0) return refuse$1({
|
|
3854
|
+
op,
|
|
3855
|
+
reason: DOCUMENT_OP_REFUSAL_REASONS.UNTRACKABLE,
|
|
3856
|
+
message: "Tracked table deletion does not support captured markup or table property revisions."
|
|
3857
|
+
});
|
|
3858
|
+
const tracked = trackTableRows({
|
|
3859
|
+
document,
|
|
3860
|
+
op,
|
|
3861
|
+
rows: location.table.rows,
|
|
3862
|
+
revision: op.revision,
|
|
3863
|
+
newIds: op.newIds,
|
|
3864
|
+
kind: WRAP_KINDS.DELETION
|
|
3865
|
+
});
|
|
3866
|
+
if (tracked.isErr()) return Result.err(tracked.error);
|
|
3867
|
+
blocks[location.index] = {
|
|
3868
|
+
...location.table,
|
|
3869
|
+
rows: tracked.value
|
|
3870
|
+
};
|
|
3871
|
+
}
|
|
3872
|
+
return commitBlocks({
|
|
3873
|
+
document,
|
|
3874
|
+
op,
|
|
3875
|
+
anchor,
|
|
3876
|
+
blocks
|
|
3877
|
+
});
|
|
3878
|
+
}
|
|
3879
|
+
case DOCUMENT_OP_TYPES.SET_CONTAINER_BLOCKS: {
|
|
3880
|
+
const anchor = storyParagraphs(body).find(({ paragraph }) => idKey(paragraph.paraId ?? "") === idKey(op.blockId));
|
|
3881
|
+
if (anchor === void 0) return refuse$1({
|
|
3882
|
+
op,
|
|
3883
|
+
reason: DOCUMENT_OP_REFUSAL_REASONS.BLOCK_NOT_FOUND,
|
|
3884
|
+
message: "The structural inverse anchor does not exist."
|
|
3885
|
+
});
|
|
3886
|
+
if (!equalForStaleness(blockListAt(body.content, anchor.list), op.expected)) return refuse$1({
|
|
3887
|
+
op,
|
|
3888
|
+
reason: DOCUMENT_OP_REFUSAL_REASONS.STALE,
|
|
3889
|
+
message: "The container blocks have changed."
|
|
3890
|
+
});
|
|
3891
|
+
return commitBlocks({
|
|
3892
|
+
document,
|
|
3893
|
+
op,
|
|
3894
|
+
anchor,
|
|
3895
|
+
blocks: op.blocks
|
|
3896
|
+
});
|
|
3897
|
+
}
|
|
3898
|
+
default: return op;
|
|
1906
3899
|
}
|
|
1907
3900
|
};
|
|
1908
|
-
|
|
1909
|
-
|
|
1910
|
-
|
|
1911
|
-
|
|
1912
|
-
|
|
1913
|
-
|
|
3901
|
+
//#endregion
|
|
3902
|
+
//#region src/ops/tableRows.ts
|
|
3903
|
+
/** Row edits and their exact inverse, addressed through a paragraph in the row. */
|
|
3904
|
+
const refused = ({ op, reason, message }) => Result.err(new DocumentOpRefusal({
|
|
3905
|
+
opType: op.type,
|
|
3906
|
+
reason,
|
|
3907
|
+
message
|
|
3908
|
+
}));
|
|
3909
|
+
const paragraphsIn = (rows) => storyParagraphs({ content: [{
|
|
3910
|
+
type: "table",
|
|
3911
|
+
rows: [...rows]
|
|
3912
|
+
}] }).map(({ paragraph }) => paragraph);
|
|
3913
|
+
/** Indexed markup and shared row wrappers need a structural table operation. */
|
|
3914
|
+
const needsTableEdit = (table) => table.preserved !== void 0 || table.bookmarks !== void 0 || table.carrierStack !== void 0 || table.rows.some((row) => row.contentControls !== void 0 || row.carrierStack !== void 0);
|
|
3915
|
+
const withRows = ({ document, story, location, rows }) => {
|
|
3916
|
+
const body = storyBody(document, story);
|
|
3917
|
+
const content = updateBlockList(body.content, location.list, (blocks) => {
|
|
3918
|
+
const out = [...blocks];
|
|
3919
|
+
out[location.index] = {
|
|
3920
|
+
...location.table,
|
|
3921
|
+
rows
|
|
3922
|
+
};
|
|
3923
|
+
return out;
|
|
3924
|
+
});
|
|
3925
|
+
switch (story) {
|
|
3926
|
+
case OP_STORIES.MAIN: return {
|
|
3927
|
+
...document,
|
|
3928
|
+
package: {
|
|
3929
|
+
...document.package,
|
|
3930
|
+
document: withBodyContent(body, content)
|
|
3931
|
+
}
|
|
3932
|
+
};
|
|
3933
|
+
default: return story;
|
|
3934
|
+
}
|
|
1914
3935
|
};
|
|
1915
|
-
|
|
1916
|
-
|
|
1917
|
-
|
|
1918
|
-
|
|
1919
|
-
|
|
1920
|
-
|
|
1921
|
-
|
|
1922
|
-
|
|
1923
|
-
|
|
1924
|
-
|
|
1925
|
-
|
|
1926
|
-
const
|
|
1927
|
-
|
|
1928
|
-
|
|
1929
|
-
|
|
1930
|
-
|
|
1931
|
-
|
|
1932
|
-
|
|
1933
|
-
|
|
1934
|
-
for (const { node, before: start, after: end } of runGaps(content)) {
|
|
1935
|
-
if (node.type !== "run") continue;
|
|
1936
|
-
if (!prior.has(node)) {
|
|
1937
|
-
if (runWidth(node) > 0) stretchOpen = false;
|
|
1938
|
-
continue;
|
|
3936
|
+
const commitRows = ({ document, op, location, rows }) => {
|
|
3937
|
+
if (rows.length === 0) return applyTableOp(document, {
|
|
3938
|
+
type: DOCUMENT_OP_TYPES.DELETE_TABLE,
|
|
3939
|
+
story: op.story,
|
|
3940
|
+
blockId: op.blockId,
|
|
3941
|
+
expected: location.table
|
|
3942
|
+
}).mapError((error) => new DocumentOpRefusal({
|
|
3943
|
+
opType: op.type,
|
|
3944
|
+
reason: error.reason,
|
|
3945
|
+
message: error.message
|
|
3946
|
+
}));
|
|
3947
|
+
const before = location.table.rows;
|
|
3948
|
+
if (structurallyEqual(before, rows)) return Result.ok({
|
|
3949
|
+
document,
|
|
3950
|
+
inverse: [],
|
|
3951
|
+
touched: {
|
|
3952
|
+
modified: [],
|
|
3953
|
+
inserted: [],
|
|
3954
|
+
removed: []
|
|
1939
3955
|
}
|
|
1940
|
-
|
|
1941
|
-
|
|
1942
|
-
|
|
1943
|
-
|
|
1944
|
-
|
|
1945
|
-
|
|
1946
|
-
|
|
1947
|
-
|
|
1948
|
-
|
|
1949
|
-
|
|
1950
|
-
|
|
1951
|
-
|
|
3956
|
+
});
|
|
3957
|
+
const afterParagraphs = paragraphsIn(rows);
|
|
3958
|
+
if (rows.some((row) => row.cells.length === 0 || row.cells.some((cell) => storyParagraphs({ content: cell.content }).length === 0))) return refused({
|
|
3959
|
+
op,
|
|
3960
|
+
reason: DOCUMENT_OP_REFUSAL_REASONS.STRUCTURE_MISMATCH,
|
|
3961
|
+
message: "Every row has cells, and every cell has a paragraph."
|
|
3962
|
+
});
|
|
3963
|
+
if (afterParagraphs.some(({ content }) => textsIn(content).some(hasIllegalXmlCharacters))) return refused({
|
|
3964
|
+
op,
|
|
3965
|
+
reason: DOCUMENT_OP_REFUSAL_REASONS.INVALID_TEXT,
|
|
3966
|
+
message: "The row holds text that cannot be written to XML."
|
|
3967
|
+
});
|
|
3968
|
+
const body = { content: [{
|
|
3969
|
+
type: "table",
|
|
3970
|
+
rows
|
|
3971
|
+
}] };
|
|
3972
|
+
const originalParagraphs = new Map(paragraphsIn(before).map((paragraph) => [idKey(paragraph.paraId ?? ""), paragraph]));
|
|
3973
|
+
for (const paragraphLocation of storyParagraphs(body)) {
|
|
3974
|
+
const mark = paragraphLocation.paragraph.pPrMark;
|
|
3975
|
+
if (mark !== void 0 && endsItsContainer(body, paragraphLocation) && !permitsCellFinalMark(body, paragraphLocation) && !structurallyEqual(mark, originalParagraphs.get(idKey(paragraphLocation.paragraph.paraId ?? ""))?.pPrMark)) return refused({
|
|
3976
|
+
op,
|
|
3977
|
+
reason: DOCUMENT_OP_REFUSAL_REASONS.CONTAINER_FINAL_MARK,
|
|
3978
|
+
message: "A cell-final mark must belong to its row structural revision."
|
|
1952
3979
|
});
|
|
1953
|
-
stretchOpen = true;
|
|
1954
3980
|
}
|
|
1955
|
-
|
|
1956
|
-
|
|
1957
|
-
|
|
1958
|
-
|
|
1959
|
-
|
|
1960
|
-
|
|
1961
|
-
|
|
1962
|
-
|
|
1963
|
-
|
|
3981
|
+
const beforeIds = new Set(paragraphIdsIn(before).map(idKey));
|
|
3982
|
+
if (afterParagraphs.some(({ paraId }) => paraId === void 0 || !beforeIds.has(idKey(paraId)) && !isParaId(paraId))) return refused({
|
|
3983
|
+
op,
|
|
3984
|
+
reason: DOCUMENT_OP_REFUSAL_REASONS.INVALID_BLOCK_ID,
|
|
3985
|
+
message: "An added paragraph has no usable id."
|
|
3986
|
+
});
|
|
3987
|
+
const remainingParagraphs = countIds(packageParagraphIds(document.package));
|
|
3988
|
+
for (const id of paragraphIdsIn(before)) remainingParagraphs.set(idKey(id), (remainingParagraphs.get(idKey(id)) ?? 1) - 1);
|
|
3989
|
+
if (collides(remainingParagraphs, paragraphIdsIn(rows))) return refused({
|
|
3990
|
+
op,
|
|
3991
|
+
reason: DOCUMENT_OP_REFUSAL_REASONS.ID_COLLISION,
|
|
3992
|
+
message: "A row paragraph id is already used in the package."
|
|
3993
|
+
});
|
|
3994
|
+
const remainingRecords = countKeys(packageIdentityKeys(document.package));
|
|
3995
|
+
for (const key of identityKeysIn(before)) remainingRecords.set(key, (remainingRecords.get(key) ?? 1) - 1);
|
|
3996
|
+
const incomingRecords = identityKeysIn(rows);
|
|
3997
|
+
if (incomingRecords.some((key) => (remainingRecords.get(key) ?? 0) > 0) || new Set(incomingRecords).size !== incomingRecords.length) return refused({
|
|
3998
|
+
op,
|
|
3999
|
+
reason: DOCUMENT_OP_REFUSAL_REASONS.ID_COLLISION,
|
|
4000
|
+
message: "A row revision or content-control id is already used in the package."
|
|
4001
|
+
});
|
|
4002
|
+
const next = withRows({
|
|
4003
|
+
document,
|
|
4004
|
+
story: op.story,
|
|
4005
|
+
location,
|
|
4006
|
+
rows
|
|
4007
|
+
});
|
|
4008
|
+
const valid = validateOpsDocument(next);
|
|
4009
|
+
if (valid.isErr()) return refused({
|
|
4010
|
+
op,
|
|
4011
|
+
reason: valid.error.reason,
|
|
4012
|
+
message: valid.error.message
|
|
4013
|
+
});
|
|
4014
|
+
const afterIds = new Set(paragraphIdsIn(rows).map(idKey));
|
|
4015
|
+
const beforeParagraphIds = paragraphIdsIn(before);
|
|
4016
|
+
const afterParagraphIds = paragraphIdsIn(rows);
|
|
4017
|
+
const changedIds = new Set(rows.filter((row) => !before.includes(row)).flatMap(paragraphIdsIn).map(idKey));
|
|
4018
|
+
const anchor = tableRowAnchor(rows);
|
|
4019
|
+
if (anchor === void 0) return refused({
|
|
4020
|
+
op,
|
|
4021
|
+
reason: DOCUMENT_OP_REFUSAL_REASONS.UNTRACKABLE,
|
|
4022
|
+
message: "The table has no paragraph that can address its rows."
|
|
4023
|
+
});
|
|
4024
|
+
return Result.ok({
|
|
4025
|
+
document: next,
|
|
4026
|
+
inverse: [{
|
|
4027
|
+
type: DOCUMENT_OP_TYPES.SET_TABLE_ROWS,
|
|
4028
|
+
story: op.story,
|
|
4029
|
+
blockId: anchor,
|
|
4030
|
+
expected: rows,
|
|
4031
|
+
rows: before
|
|
4032
|
+
}],
|
|
4033
|
+
touched: {
|
|
4034
|
+
modified: beforeParagraphIds.filter((id) => afterIds.has(idKey(id)) && changedIds.has(idKey(id))),
|
|
4035
|
+
inserted: afterParagraphIds.filter((id) => !beforeIds.has(idKey(id))),
|
|
4036
|
+
removed: beforeParagraphIds.filter((id) => !afterIds.has(idKey(id)))
|
|
4037
|
+
}
|
|
4038
|
+
});
|
|
4039
|
+
};
|
|
4040
|
+
const applyRowOp = (document, op) => {
|
|
4041
|
+
const located = locateTableRow(document, op);
|
|
4042
|
+
if (located.isErr()) return Result.err(located.error);
|
|
4043
|
+
const location = located.value;
|
|
4044
|
+
const before = location.table.rows;
|
|
4045
|
+
const rows = [...before];
|
|
4046
|
+
switch (op.type) {
|
|
4047
|
+
case DOCUMENT_OP_TYPES.INSERT_ROW: {
|
|
4048
|
+
if (!Number.isInteger(op.at) || op.at < 0 || op.at > rows.length) return refused({
|
|
4049
|
+
op,
|
|
4050
|
+
reason: DOCUMENT_OP_REFUSAL_REASONS.INVALID_OFFSET,
|
|
4051
|
+
message: "The row index is outside the table."
|
|
4052
|
+
});
|
|
4053
|
+
if (needsTableEdit(location.table)) return refused({
|
|
4054
|
+
op,
|
|
4055
|
+
reason: DOCUMENT_OP_REFUSAL_REASONS.UNTRACKABLE,
|
|
4056
|
+
message: "Indexed table markup or shared row wrappers require a table operation."
|
|
4057
|
+
});
|
|
4058
|
+
const incoming = structuredClone(op.row);
|
|
4059
|
+
if (paragraphIdsIn(incoming).some((id) => paragraphIdsIn(before).some((existing) => idKey(existing) === idKey(id)))) return refused({
|
|
4060
|
+
op,
|
|
4061
|
+
reason: DOCUMENT_OP_REFUSAL_REASONS.ID_COLLISION,
|
|
4062
|
+
message: "An inserted paragraph id is already used in the table."
|
|
4063
|
+
});
|
|
4064
|
+
const tracked = op.revision === void 0 ? Result.ok([incoming]) : trackTableRows({
|
|
4065
|
+
document,
|
|
4066
|
+
op,
|
|
4067
|
+
rows: [incoming],
|
|
4068
|
+
revision: op.revision,
|
|
4069
|
+
newIds: op.newIds,
|
|
4070
|
+
kind: WRAP_KINDS.INSERTION
|
|
4071
|
+
});
|
|
4072
|
+
if (tracked.isErr()) return Result.err(tracked.error);
|
|
4073
|
+
const row = tracked.value.at(0) ?? panic("A tracked insertion lost its row.");
|
|
4074
|
+
rows.splice(op.at, 0, row);
|
|
4075
|
+
return commitRows({
|
|
4076
|
+
document,
|
|
4077
|
+
op,
|
|
4078
|
+
location,
|
|
4079
|
+
rows
|
|
4080
|
+
});
|
|
4081
|
+
}
|
|
4082
|
+
case DOCUMENT_OP_TYPES.DELETE_ROW: {
|
|
4083
|
+
const row = before[location.rowIndex];
|
|
4084
|
+
if (row === void 0) return panic("A row location must name a row.");
|
|
4085
|
+
if (op.expected !== void 0 && !equalForStaleness(row, op.expected)) return refused({
|
|
4086
|
+
op,
|
|
4087
|
+
reason: DOCUMENT_OP_REFUSAL_REASONS.STALE,
|
|
4088
|
+
message: "The row to remove has changed."
|
|
4089
|
+
});
|
|
4090
|
+
if (op.revision === void 0 && before.length === 1) return commitRows({
|
|
4091
|
+
document,
|
|
4092
|
+
op,
|
|
4093
|
+
location,
|
|
4094
|
+
rows: []
|
|
4095
|
+
});
|
|
4096
|
+
if (needsTableEdit(location.table)) return refused({
|
|
4097
|
+
op,
|
|
4098
|
+
reason: DOCUMENT_OP_REFUSAL_REASONS.UNTRACKABLE,
|
|
4099
|
+
message: "Indexed table markup or shared row wrappers require a table operation."
|
|
4100
|
+
});
|
|
4101
|
+
if (op.revision === void 0) rows.splice(location.rowIndex, 1);
|
|
4102
|
+
else {
|
|
4103
|
+
const tracked = trackTableRows({
|
|
4104
|
+
document,
|
|
4105
|
+
op,
|
|
4106
|
+
rows: [row],
|
|
4107
|
+
revision: op.revision,
|
|
4108
|
+
newIds: op.newIds,
|
|
4109
|
+
kind: WRAP_KINDS.DELETION
|
|
4110
|
+
});
|
|
4111
|
+
if (tracked.isErr()) return Result.err(tracked.error);
|
|
4112
|
+
rows[location.rowIndex] = tracked.value.at(0) ?? panic("A tracked deletion lost its row.");
|
|
4113
|
+
}
|
|
4114
|
+
return commitRows({
|
|
4115
|
+
document,
|
|
4116
|
+
op,
|
|
4117
|
+
location,
|
|
4118
|
+
rows
|
|
4119
|
+
});
|
|
4120
|
+
}
|
|
4121
|
+
case DOCUMENT_OP_TYPES.SET_TABLE_ROWS: {
|
|
4122
|
+
if (!equalForStaleness(before, op.expected)) return refused({
|
|
4123
|
+
op,
|
|
4124
|
+
reason: DOCUMENT_OP_REFUSAL_REASONS.STALE,
|
|
4125
|
+
message: "The table rows have changed."
|
|
4126
|
+
});
|
|
4127
|
+
if (op.rows.length > 0 && op.rows.length !== before.length && needsTableEdit(location.table)) return refused({
|
|
4128
|
+
op,
|
|
4129
|
+
reason: DOCUMENT_OP_REFUSAL_REASONS.UNTRACKABLE,
|
|
4130
|
+
message: "Indexed table markup or shared row wrappers require a table operation."
|
|
4131
|
+
});
|
|
4132
|
+
const replacement = op.rows.map((row) => before.find((existing) => structurallyEqual(existing, row)) ?? structuredClone(row));
|
|
4133
|
+
return commitRows({
|
|
4134
|
+
document,
|
|
4135
|
+
op,
|
|
4136
|
+
location,
|
|
4137
|
+
rows: replacement
|
|
4138
|
+
});
|
|
4139
|
+
}
|
|
4140
|
+
default: return op;
|
|
4141
|
+
}
|
|
1964
4142
|
};
|
|
1965
4143
|
//#endregion
|
|
1966
4144
|
//#region src/ops/apply.ts
|
|
@@ -1988,9 +4166,19 @@ const patchRunsBetween = (items, from, to, patch, whenEmpty) => {
|
|
|
1988
4166
|
* | `joinInline` | `splitInline` |
|
|
1989
4167
|
* | `setRunProps` | `setRunProps` per stretch of prior values, joining cuts |
|
|
1990
4168
|
* | `setParagraphProps` | `setParagraphProps` with the prior values |
|
|
1991
|
-
* | `splitBlock` | `joinBlocks
|
|
1992
|
-
* | `joinBlocks` | `splitBlock` with the
|
|
4169
|
+
* | `splitBlock` | `joinBlocks`, then the kept half's review fields |
|
|
4170
|
+
* | `joinBlocks` | `splitBlock` with the retired paragraph's fields, then |
|
|
4171
|
+
* | | the survivor's review fields |
|
|
4172
|
+
* | `insertBlocks` | an anchored `replaceBlocks` |
|
|
1993
4173
|
* | `replaceBlocks` | `replaceBlocks` |
|
|
4174
|
+
* | `setParagraphReview`| `setParagraphReview` |
|
|
4175
|
+
* | `replaceInline` | `replaceInline` |
|
|
4176
|
+
* | `resolveRevision` | the inverses of the operations it expands to |
|
|
4177
|
+
*
|
|
4178
|
+
* A tracked operation (one carrying a `revision` stamp, see `review.ts`) is
|
|
4179
|
+
* undone as follows: a tracked insertion by `deleteRange`, a tracked split by
|
|
4180
|
+
* `joinBlocks`, a tracked deletion or run patch by `replaceInline`, and a
|
|
4181
|
+
* tracked paragraph patch or join by `setParagraphReview`.
|
|
1994
4182
|
*
|
|
1995
4183
|
* Each inverse carries its precondition: a deletion the slice it removes, a
|
|
1996
4184
|
* patch the values it replaces, a join the fields of the paragraph it merges
|
|
@@ -2014,6 +4202,35 @@ const refusal = (op, reason, message) => new DocumentOpRefusal({
|
|
|
2014
4202
|
opType: op.type
|
|
2015
4203
|
});
|
|
2016
4204
|
const refuse = (op, reason, message) => Result.err(refusal(op, reason, message));
|
|
4205
|
+
/**
|
|
4206
|
+
* Whether a tracked operation's stamp can name the first record it creates:
|
|
4207
|
+
* a revision id in range that no record in the package carries.
|
|
4208
|
+
*/
|
|
4209
|
+
const stampRefusal = (document, op, stamp) => {
|
|
4210
|
+
const revisionId = stamp.id;
|
|
4211
|
+
if (!Number.isInteger(revisionId) || revisionId < 0 || revisionId > 2147483647) return refusal(op, DOCUMENT_OP_REFUSAL_REASONS.INVALID_NEW_ID, `${revisionId} cannot be a revision id.`);
|
|
4212
|
+
const key = slotKey({
|
|
4213
|
+
space: IDENTITY_SPACES.REVISION,
|
|
4214
|
+
id: stamp.id
|
|
4215
|
+
});
|
|
4216
|
+
if (packageIdentityKeys(document.package).includes(key)) return refusal(op, DOCUMENT_OP_REFUSAL_REASONS.ID_COLLISION, `Revision id ${stamp.id} is already used in the package.`);
|
|
4217
|
+
};
|
|
4218
|
+
/** The replacement that gives a paragraph back the content it had. */
|
|
4219
|
+
const contentRestoring = (story, result, original) => ({
|
|
4220
|
+
type: DOCUMENT_OP_TYPES.REPLACE_INLINE,
|
|
4221
|
+
story,
|
|
4222
|
+
blockId: result.paraId ?? original.paraId ?? "",
|
|
4223
|
+
expected: result.content,
|
|
4224
|
+
content: original.content
|
|
4225
|
+
});
|
|
4226
|
+
/** The review fields' setting that gives a paragraph back the ones it had. */
|
|
4227
|
+
const reviewRestoring = (story, result, original) => ({
|
|
4228
|
+
type: DOCUMENT_OP_TYPES.SET_PARAGRAPH_REVIEW,
|
|
4229
|
+
story,
|
|
4230
|
+
blockId: result.paraId ?? original.paraId ?? "",
|
|
4231
|
+
expected: reviewFieldsOf(result),
|
|
4232
|
+
review: reviewFieldsOf(original)
|
|
4233
|
+
});
|
|
2017
4234
|
/** Characters `insertText` does not carry: each is an inline atom of its own. */
|
|
2018
4235
|
const NON_TEXT_CHARACTER_PATTERN = /[\t\n\r]/u;
|
|
2019
4236
|
const isHighSurrogate = (code) => code >= 55296 && code <= 56319;
|
|
@@ -2170,27 +4387,59 @@ const editOne = (commitOptions, inverseOf) => {
|
|
|
2170
4387
|
touched: committed.value.touched
|
|
2171
4388
|
});
|
|
2172
4389
|
};
|
|
2173
|
-
/**
|
|
2174
|
-
|
|
2175
|
-
|
|
2176
|
-
|
|
2177
|
-
|
|
2178
|
-
|
|
2179
|
-
|
|
2180
|
-
|
|
2181
|
-
|
|
2182
|
-
|
|
2183
|
-
|
|
2184
|
-
|
|
2185
|
-
|
|
2186
|
-
|
|
2187
|
-
|
|
2188
|
-
|
|
2189
|
-
|
|
2190
|
-
|
|
2191
|
-
|
|
2192
|
-
|
|
2193
|
-
|
|
4390
|
+
/**
|
|
4391
|
+
* Content with the leaves between two gaps recorded as a tracked change;
|
|
4392
|
+
* `undefined` when there is nothing to record.
|
|
4393
|
+
*/
|
|
4394
|
+
const wrappedContent = (op, options) => {
|
|
4395
|
+
const wrapped = wrapTracked(options);
|
|
4396
|
+
switch (wrapped.kind) {
|
|
4397
|
+
case "wrapped": return Result.ok(wrapped.content);
|
|
4398
|
+
case "unchanged": return Result.ok(void 0);
|
|
4399
|
+
case "refused": return Result.err(refusal(op, wrapped.reason, wrapped.reason === DOCUMENT_OP_REFUSAL_REASONS.UNTRACKABLE ? "A tracked change cannot hold a comment boundary or reference." : "The tracked edit falls inside tracked-removed content."));
|
|
4400
|
+
default: return wrapped;
|
|
4401
|
+
}
|
|
4402
|
+
};
|
|
4403
|
+
/**
|
|
4404
|
+
* Commit an insertion, recording the one deletion that undoes it. A tracked
|
|
4405
|
+
* insertion is wrapped first; the deletion removes the wrapper with it.
|
|
4406
|
+
*/
|
|
4407
|
+
const inserted = (options) => {
|
|
4408
|
+
const { document, op, story, location, start, end, newIds, revision } = options;
|
|
4409
|
+
let { content } = options;
|
|
4410
|
+
if (revision !== void 0) {
|
|
4411
|
+
if (insertionInverse(location.paragraph.content, content, start, end) === void 0) return refuse(op, DOCUMENT_OP_REFUSAL_REASONS.STRUCTURE_MISMATCH, "The insertion would merge records that were separate.");
|
|
4412
|
+
const wrapped = wrappedContent(op, {
|
|
4413
|
+
items: content,
|
|
4414
|
+
from: start,
|
|
4415
|
+
to: end,
|
|
4416
|
+
kind: WRAP_KINDS.INSERTION,
|
|
4417
|
+
stamp: revision
|
|
4418
|
+
});
|
|
4419
|
+
if (wrapped.isErr()) return Result.err(wrapped.error);
|
|
4420
|
+
content = wrapped.value ?? content;
|
|
4421
|
+
}
|
|
4422
|
+
return editOne({
|
|
4423
|
+
document,
|
|
4424
|
+
op,
|
|
4425
|
+
story,
|
|
4426
|
+
at: location,
|
|
4427
|
+
paragraph: withContent(location.paragraph, content),
|
|
4428
|
+
newIds,
|
|
4429
|
+
inserted: recordsBetween(content, start, end)
|
|
4430
|
+
}, (result) => {
|
|
4431
|
+
const inverse = insertionInverse(location.paragraph.content, result.content, start, end);
|
|
4432
|
+
if (inverse === void 0) return Result.err(refusal(op, DOCUMENT_OP_REFUSAL_REASONS.STRUCTURE_MISMATCH, "The insertion would merge records that were separate."));
|
|
4433
|
+
const deletion = {
|
|
4434
|
+
type: DOCUMENT_OP_TYPES.DELETE_RANGE,
|
|
4435
|
+
from: positionAt(story, result, start),
|
|
4436
|
+
to: positionAt(story, result, end),
|
|
4437
|
+
expected: inverse.removed
|
|
4438
|
+
};
|
|
4439
|
+
if (inverse.join > 0) deletion.join = inverse.join;
|
|
4440
|
+
return Result.ok([deletion]);
|
|
4441
|
+
});
|
|
4442
|
+
};
|
|
2194
4443
|
const insertText = (document, op) => {
|
|
2195
4444
|
if (op.text === "" || NON_TEXT_CHARACTER_PATTERN.test(op.text) || hasIllegalXmlCharacters(op.text)) return refuse(op, DOCUMENT_OP_REFUSAL_REASONS.INVALID_TEXT, "The text cannot be inserted.");
|
|
2196
4445
|
const located = locatePosition(document, op, op.at, "afterAll");
|
|
@@ -2215,7 +4464,8 @@ const insertText = (document, op) => {
|
|
|
2215
4464
|
content,
|
|
2216
4465
|
start,
|
|
2217
4466
|
end,
|
|
2218
|
-
newIds: op.newIds
|
|
4467
|
+
newIds: op.newIds,
|
|
4468
|
+
revision: op.revision
|
|
2219
4469
|
});
|
|
2220
4470
|
};
|
|
2221
4471
|
const holdsEmptyRecord = (nodes) => nodes.some((node) => isEmptyRecord(node) || holdsEmptyRecord(childNodes(node) ?? []));
|
|
@@ -2224,7 +4474,7 @@ const insertContent = (document, op) => {
|
|
|
2224
4474
|
...op.slice,
|
|
2225
4475
|
content: structuredClone(op.slice.content)
|
|
2226
4476
|
};
|
|
2227
|
-
if (slice.content.length === 0 || holdsEmptyRecord(slice.content)) return refuse(op, DOCUMENT_OP_REFUSAL_REASONS.EMPTY_CONTENT, "The slice holds nothing, or an empty run or
|
|
4477
|
+
if (slice.content.length === 0 || holdsEmptyRecord(slice.content)) return refuse(op, DOCUMENT_OP_REFUSAL_REASONS.EMPTY_CONTENT, "The slice holds nothing, or an empty run, text node, or revision wrapper.");
|
|
2228
4478
|
if (!Number.isInteger(slice.openStart) || !Number.isInteger(slice.openEnd) || slice.openStart < 0 || slice.openEnd < 0) return refuse(op, DOCUMENT_OP_REFUSAL_REASONS.STRUCTURE_MISMATCH, "Open depths are counts.");
|
|
2229
4479
|
if (textsIn(slice.content).some(hasIllegalXmlCharacters)) return refuse(op, DOCUMENT_OP_REFUSAL_REASONS.INVALID_TEXT, "The slice holds text that cannot be written.");
|
|
2230
4480
|
const incoming = paragraphIdsIn(slice.content);
|
|
@@ -2242,7 +4492,8 @@ const insertContent = (document, op) => {
|
|
|
2242
4492
|
content,
|
|
2243
4493
|
start: gap,
|
|
2244
4494
|
end: gapAfterInserted(gap, slice.content),
|
|
2245
|
-
newIds: op.newIds
|
|
4495
|
+
newIds: op.newIds,
|
|
4496
|
+
revision: op.revision
|
|
2246
4497
|
});
|
|
2247
4498
|
};
|
|
2248
4499
|
/** A merge's content, or the refusal naming why it cannot be made. */
|
|
@@ -2261,6 +4512,7 @@ const isCount = (value) => Number.isInteger(value) && value >= 0;
|
|
|
2261
4512
|
const deleteRange = (document, op) => {
|
|
2262
4513
|
const join = op.join ?? 0;
|
|
2263
4514
|
if (!isCount(join)) return refuse(op, DOCUMENT_OP_REFUSAL_REASONS.STRUCTURE_MISMATCH, "A join depth is a count.");
|
|
4515
|
+
if (op.revision !== void 0 && join !== 0) return refuse(op, DOCUMENT_OP_REFUSAL_REASONS.STRUCTURE_MISMATCH, "A tracked deletion joins nothing.");
|
|
2264
4516
|
const located = locateRange(document, op, op.from, op.to);
|
|
2265
4517
|
if (located.isErr()) return Result.err(located.error);
|
|
2266
4518
|
const { story, location, from, to, empty } = located.value;
|
|
@@ -2270,6 +4522,25 @@ const deleteRange = (document, op) => {
|
|
|
2270
4522
|
const { content: remaining, removed } = deleteBetween(paragraph.content, from, to);
|
|
2271
4523
|
if (expected !== void 0 && !equalForStaleness(expected, removed)) return refuse(op, DOCUMENT_OP_REFUSAL_REASONS.STALE, `The range of ${op.from.blockId} holds other content than expected.`);
|
|
2272
4524
|
if (removed.content.length === 0) return unchanged(document);
|
|
4525
|
+
if (op.revision !== void 0) {
|
|
4526
|
+
const wrapped = wrappedContent(op, {
|
|
4527
|
+
items: paragraph.content,
|
|
4528
|
+
from,
|
|
4529
|
+
to,
|
|
4530
|
+
kind: WRAP_KINDS.DELETION,
|
|
4531
|
+
stamp: op.revision
|
|
4532
|
+
});
|
|
4533
|
+
if (wrapped.isErr()) return Result.err(wrapped.error);
|
|
4534
|
+
if (wrapped.value === void 0) return unchanged(document);
|
|
4535
|
+
return editOne({
|
|
4536
|
+
document,
|
|
4537
|
+
op,
|
|
4538
|
+
story,
|
|
4539
|
+
at: location,
|
|
4540
|
+
paragraph: withContent(paragraph, wrapped.value),
|
|
4541
|
+
newIds: op.newIds
|
|
4542
|
+
}, (result) => Result.ok([contentRestoring(story, result, paragraph)]));
|
|
4543
|
+
}
|
|
2273
4544
|
let content = remaining;
|
|
2274
4545
|
let retired = {};
|
|
2275
4546
|
if (join > 0) {
|
|
@@ -2348,6 +4619,8 @@ const setRunProps = (document, op) => {
|
|
|
2348
4619
|
const joinStart = op.joinStart ?? 0;
|
|
2349
4620
|
const joinEnd = op.joinEnd ?? 0;
|
|
2350
4621
|
if (!isCount(joinStart) || !isCount(joinEnd)) return refuse(op, DOCUMENT_OP_REFUSAL_REASONS.STRUCTURE_MISMATCH, "A join depth is a count.");
|
|
4622
|
+
const { revision } = op;
|
|
4623
|
+
if (revision !== void 0 && (joinStart !== 0 || joinEnd !== 0)) return refuse(op, DOCUMENT_OP_REFUSAL_REASONS.STRUCTURE_MISMATCH, "A tracked patch joins nothing.");
|
|
2351
4624
|
const located = locateRange(document, op, op.from, op.to);
|
|
2352
4625
|
if (located.isErr()) return Result.err(located.error);
|
|
2353
4626
|
const { story, location, from, to, empty } = located.value;
|
|
@@ -2355,6 +4628,30 @@ const setRunProps = (document, op) => {
|
|
|
2355
4628
|
const { paragraph } = location;
|
|
2356
4629
|
const { expected } = op;
|
|
2357
4630
|
if (expected !== void 0 && !runsBetween(paragraph.content, from, to).every((run) => statesValues(run.formatting, expected))) return refuse(op, DOCUMENT_OP_REFUSAL_REASONS.STALE, `The runs of ${op.from.blockId} state other values than expected.`);
|
|
4631
|
+
if (revision !== void 0) {
|
|
4632
|
+
const recordChange = (patchedRun, previous) => {
|
|
4633
|
+
if ((previous.propertyChanges?.length ?? 0) > 0) return patchedRun;
|
|
4634
|
+
const change = {
|
|
4635
|
+
type: "runPropertyChange",
|
|
4636
|
+
info: stampInfo(revision)
|
|
4637
|
+
};
|
|
4638
|
+
if (previous.formatting !== void 0) change.previousFormatting = previous.formatting;
|
|
4639
|
+
return {
|
|
4640
|
+
...patchedRun,
|
|
4641
|
+
propertyChanges: [change]
|
|
4642
|
+
};
|
|
4643
|
+
};
|
|
4644
|
+
const tracked = patchRunsBetween(paragraph.content, from, to, op.patch, op.whenEmpty, recordChange);
|
|
4645
|
+
if (tracked === void 0) return unchanged(document);
|
|
4646
|
+
return editOne({
|
|
4647
|
+
document,
|
|
4648
|
+
op,
|
|
4649
|
+
story,
|
|
4650
|
+
at: location,
|
|
4651
|
+
paragraph: withContent(paragraph, tracked.content),
|
|
4652
|
+
newIds: op.newIds
|
|
4653
|
+
}, (result) => Result.ok([contentRestoring(story, result, paragraph)]));
|
|
4654
|
+
}
|
|
2358
4655
|
const patched = patchRunsBetween(paragraph.content, from, to, op.patch, op.whenEmpty);
|
|
2359
4656
|
const patchedContent = patched?.content ?? paragraph.content;
|
|
2360
4657
|
const freshened = withFreshIds({
|
|
@@ -2429,8 +4726,22 @@ const setParagraphProps = (document, op) => {
|
|
|
2429
4726
|
if (located.isErr()) return Result.err(located.error);
|
|
2430
4727
|
const { paragraph } = located.value;
|
|
2431
4728
|
if (op.expected !== void 0 && !statesValues(paragraph.formatting, op.expected)) return refuse(op, DOCUMENT_OP_REFUSAL_REASONS.STALE, `${op.blockId} states other paragraph properties than expected.`);
|
|
4729
|
+
const { revision } = op;
|
|
4730
|
+
if (revision !== void 0 && namesMarkFormatting(op.patch)) return refuse(op, DOCUMENT_OP_REFUSAL_REASONS.UNTRACKABLE, "A paragraph property change does not record the paragraph mark's run properties.");
|
|
2432
4731
|
const formatting = patchedSet(paragraph.formatting, op.patch, op.whenEmpty);
|
|
2433
4732
|
if (structurallyEqual(formatting ?? {}, paragraph.formatting ?? {})) return unchanged(document);
|
|
4733
|
+
if (revision !== void 0) {
|
|
4734
|
+
const next = withParagraphFormatting(paragraph, formatting);
|
|
4735
|
+
if ((paragraph.propertyChanges?.length ?? 0) === 0) next.propertyChanges = [paragraphPropertyChange(stampInfo(revision), paragraph.formatting)];
|
|
4736
|
+
return editOne({
|
|
4737
|
+
document,
|
|
4738
|
+
op,
|
|
4739
|
+
story: op.story,
|
|
4740
|
+
at: located.value,
|
|
4741
|
+
paragraph: next,
|
|
4742
|
+
newIds: void 0
|
|
4743
|
+
}, (result) => Result.ok([reviewRestoring(op.story, result, paragraph)]));
|
|
4744
|
+
}
|
|
2434
4745
|
return editOne({
|
|
2435
4746
|
document,
|
|
2436
4747
|
op,
|
|
@@ -2457,29 +4768,104 @@ const splitFieldsOf = (paragraph) => {
|
|
|
2457
4768
|
delete fields.pPrMark;
|
|
2458
4769
|
return fields;
|
|
2459
4770
|
};
|
|
4771
|
+
/**
|
|
4772
|
+
* The paragraph properties two paragraphs joined have: the first's paragraph
|
|
4773
|
+
* properties with the second's mark run properties, since the mark that stays
|
|
4774
|
+
* is the second's. A first paragraph that holds no content brings nothing:
|
|
4775
|
+
* the second keeps its own.
|
|
4776
|
+
*/
|
|
4777
|
+
const joinedFormatting = (first, second) => paragraphLength(first) === 0 ? second.formatting : withMarkFormatting(paragraphPropertiesOf(first.formatting), second.formatting);
|
|
4778
|
+
/** The review fields of two paragraphs once joined: the first's properties, the second's mark. */
|
|
4779
|
+
const joinedReview = (first, second) => {
|
|
4780
|
+
const review = {};
|
|
4781
|
+
const formatting = joinedFormatting(first, second);
|
|
4782
|
+
if (formatting !== void 0) review.formatting = formatting;
|
|
4783
|
+
if (second.propertyChanges !== void 0) review.propertyChanges = second.propertyChanges;
|
|
4784
|
+
if (second.pPrMark !== void 0) review.pPrMark = second.pPrMark;
|
|
4785
|
+
return review;
|
|
4786
|
+
};
|
|
4787
|
+
/** A review-field setting from `from` to `to`; none when they are the same. */
|
|
4788
|
+
const reviewSetting = (story, blockId, from, to) => structurallyEqual(from, to) ? [] : [{
|
|
4789
|
+
type: DOCUMENT_OP_TYPES.SET_PARAGRAPH_REVIEW,
|
|
4790
|
+
story,
|
|
4791
|
+
blockId,
|
|
4792
|
+
expected: from,
|
|
4793
|
+
review: to
|
|
4794
|
+
}];
|
|
4795
|
+
const isSplitHalf = (value) => value === SPLIT_HALVES.FIRST || value === SPLIT_HALVES.SECOND;
|
|
4796
|
+
/**
|
|
4797
|
+
* Record a split as tracked: the new mark is an insertion, and the new
|
|
4798
|
+
* paragraph records a property change from the source's properties when it
|
|
4799
|
+
* states other ones and carries no change already. Returns the refusal when
|
|
4800
|
+
* the operation states a mark or property change of its own, or other run
|
|
4801
|
+
* properties for the paragraph's own mark: no tracked change records those.
|
|
4802
|
+
*/
|
|
4803
|
+
const trackSplit = ({ op, stamp, paragraph, first, made, madeSecond }) => {
|
|
4804
|
+
if (op.firstMark !== void 0 || (op.newParagraph?.propertyChanges?.length ?? 0) > 0) return refusal(op, DOCUMENT_OP_REFUSAL_REASONS.REVISION_CONFLICT, "A tracked split records its own mark and property change.");
|
|
4805
|
+
if (madeSecond && !sameMarkFormatting(made.formatting, paragraph.formatting)) return refusal(op, DOCUMENT_OP_REFUSAL_REASONS.UNTRACKABLE, "A paragraph property change does not record the paragraph mark's run properties.");
|
|
4806
|
+
first.pPrMark = {
|
|
4807
|
+
kind: "ins",
|
|
4808
|
+
info: stampInfo(stamp)
|
|
4809
|
+
};
|
|
4810
|
+
if (!sameParagraphProperties(made.formatting, paragraph.formatting) && (made.propertyChanges?.length ?? 0) === 0) made.propertyChanges = [paragraphPropertyChange(stampInfo(stamp), paragraph.formatting)];
|
|
4811
|
+
};
|
|
2460
4812
|
const splitBlock = (document, op) => {
|
|
2461
4813
|
if (!isParaId(op.newBlockId)) return refuse(op, DOCUMENT_OP_REFUSAL_REASONS.INVALID_BLOCK_ID, `${op.newBlockId} is not a paragraph id.`);
|
|
2462
4814
|
if (collides(countIds(packageParagraphIds(document.package)), [op.newBlockId])) return refuse(op, DOCUMENT_OP_REFUSAL_REASONS.ID_COLLISION, `${op.newBlockId} is already used in the package.`);
|
|
4815
|
+
if (op.newHalf !== void 0 && !isSplitHalf(op.newHalf)) return refuse(op, DOCUMENT_OP_REFUSAL_REASONS.STRUCTURE_MISMATCH, "A half is first or second.");
|
|
2463
4816
|
const located = locatePosition(document, op, op.at, "insertion");
|
|
2464
4817
|
if (located.isErr()) return Result.err(located.error);
|
|
2465
4818
|
const { location, gap } = located.value;
|
|
2466
4819
|
const { paragraph } = location;
|
|
4820
|
+
const newHalf = op.newHalf ?? (gap.offset === paragraphLength(paragraph) ? SPLIT_HALVES.SECOND : SPLIT_HALVES.FIRST);
|
|
4821
|
+
if (newHalf === SPLIT_HALVES.SECOND && (op.newParagraph?.propertyChanges?.length ?? 0) > 0) return refuse(op, DOCUMENT_OP_REFUSAL_REASONS.STRUCTURE_MISMATCH, "The second half takes the paragraph's property changes with its mark.");
|
|
2467
4822
|
const cut = cutAt(paragraph.content, gap);
|
|
2468
|
-
const
|
|
2469
|
-
...paragraph,
|
|
2470
|
-
content: cut.before
|
|
2471
|
-
};
|
|
2472
|
-
delete first.sectionProperties;
|
|
2473
|
-
delete first.pPrMark;
|
|
2474
|
-
if (op.firstMark !== void 0) first.pPrMark = op.firstMark;
|
|
2475
|
-
const second = {
|
|
4823
|
+
const made = {
|
|
2476
4824
|
...op.newParagraph ?? (paragraph.formatting === void 0 ? {} : { formatting: paragraph.formatting }),
|
|
2477
4825
|
type: "paragraph",
|
|
2478
4826
|
paraId: op.newBlockId,
|
|
2479
|
-
content:
|
|
4827
|
+
content: []
|
|
2480
4828
|
};
|
|
2481
|
-
|
|
2482
|
-
|
|
4829
|
+
let first;
|
|
4830
|
+
let second;
|
|
4831
|
+
if (newHalf === SPLIT_HALVES.FIRST) {
|
|
4832
|
+
first = {
|
|
4833
|
+
...made,
|
|
4834
|
+
content: cut.before
|
|
4835
|
+
};
|
|
4836
|
+
second = {
|
|
4837
|
+
...paragraph,
|
|
4838
|
+
content: cut.after
|
|
4839
|
+
};
|
|
4840
|
+
} else {
|
|
4841
|
+
first = {
|
|
4842
|
+
...paragraph,
|
|
4843
|
+
content: cut.before
|
|
4844
|
+
};
|
|
4845
|
+
delete first.sectionProperties;
|
|
4846
|
+
delete first.pPrMark;
|
|
4847
|
+
delete first.propertyChanges;
|
|
4848
|
+
second = {
|
|
4849
|
+
...made,
|
|
4850
|
+
content: cut.after
|
|
4851
|
+
};
|
|
4852
|
+
if (paragraph.propertyChanges !== void 0) second.propertyChanges = paragraph.propertyChanges;
|
|
4853
|
+
if (paragraph.sectionProperties !== void 0) second.sectionProperties = paragraph.sectionProperties;
|
|
4854
|
+
if (paragraph.pPrMark !== void 0) second.pPrMark = paragraph.pPrMark;
|
|
4855
|
+
}
|
|
4856
|
+
if (op.firstMark !== void 0) first.pPrMark = op.firstMark;
|
|
4857
|
+
if (op.revision !== void 0) {
|
|
4858
|
+
const madeHalf = newHalf === SPLIT_HALVES.FIRST ? first : second;
|
|
4859
|
+
const tracked = trackSplit({
|
|
4860
|
+
op,
|
|
4861
|
+
stamp: op.revision,
|
|
4862
|
+
paragraph,
|
|
4863
|
+
first,
|
|
4864
|
+
made: madeHalf,
|
|
4865
|
+
madeSecond: newHalf === SPLIT_HALVES.SECOND
|
|
4866
|
+
});
|
|
4867
|
+
if (tracked !== void 0) return Result.err(tracked);
|
|
4868
|
+
}
|
|
2483
4869
|
const committed = commit({
|
|
2484
4870
|
document,
|
|
2485
4871
|
op,
|
|
@@ -2490,17 +4876,65 @@ const splitBlock = (document, op) => {
|
|
|
2490
4876
|
newIds: op.newIds
|
|
2491
4877
|
});
|
|
2492
4878
|
if (committed.isErr()) return Result.err(committed.error);
|
|
2493
|
-
const [, placedSecond] = committed.value.paragraphs;
|
|
4879
|
+
const [placedFirst = first, placedSecond = second] = committed.value.paragraphs;
|
|
4880
|
+
const kept = newHalf === SPLIT_HALVES.FIRST ? placedSecond : placedFirst;
|
|
4881
|
+
const madePlaced = newHalf === SPLIT_HALVES.FIRST ? placedFirst : placedSecond;
|
|
4882
|
+
const keptId = paragraph.paraId ?? op.at.blockId;
|
|
2494
4883
|
return Result.ok({
|
|
2495
4884
|
document: committed.value.document,
|
|
2496
4885
|
inverse: [{
|
|
2497
4886
|
type: DOCUMENT_OP_TYPES.JOIN_BLOCKS,
|
|
2498
4887
|
story: op.at.story,
|
|
2499
|
-
blockId:
|
|
2500
|
-
nextBlockId:
|
|
4888
|
+
blockId: placedFirst.paraId ?? "",
|
|
4889
|
+
nextBlockId: placedSecond.paraId ?? "",
|
|
2501
4890
|
depth: cut.through.length,
|
|
2502
|
-
|
|
2503
|
-
|
|
4891
|
+
survivor: newHalf === SPLIT_HALVES.FIRST ? SPLIT_HALVES.SECOND : SPLIT_HALVES.FIRST,
|
|
4892
|
+
expectedRetired: splitFieldsOf(madePlaced),
|
|
4893
|
+
expectedSurvivor: reviewFieldsOf(kept)
|
|
4894
|
+
}, ...reviewSetting(op.at.story, kept.paraId ?? keptId, joinedReview(placedFirst, placedSecond), reviewFieldsOf(paragraph))],
|
|
4895
|
+
touched: committed.value.touched
|
|
4896
|
+
});
|
|
4897
|
+
};
|
|
4898
|
+
/**
|
|
4899
|
+
* Record a join as tracked: the first paragraph's mark becomes a deletion,
|
|
4900
|
+
* and the second takes the first's paragraph properties as a property
|
|
4901
|
+
* change, so accepting leaves what the direct join leaves: the second
|
|
4902
|
+
* paragraph, with the first's properties.
|
|
4903
|
+
*/
|
|
4904
|
+
const trackJoin = ({ document, op, stamp, at, leading, trailing }) => {
|
|
4905
|
+
const depth = op.depth ?? 0;
|
|
4906
|
+
if (!isCount(depth)) return refuse(op, DOCUMENT_OP_REFUSAL_REASONS.STRUCTURE_MISMATCH, "A join depth is a count.");
|
|
4907
|
+
if ((op.survivor ?? SPLIT_HALVES.SECOND) !== SPLIT_HALVES.SECOND) return refuse(op, DOCUMENT_OP_REFUSAL_REASONS.STRUCTURE_MISMATCH, "Resolving a tracked join leaves the second paragraph.");
|
|
4908
|
+
if (leading.pPrMark !== void 0) return refuse(op, DOCUMENT_OP_REFUSAL_REASONS.REVISION_CONFLICT, `The mark of ${op.blockId} already carries a tracked change.`);
|
|
4909
|
+
const first = {
|
|
4910
|
+
...leading,
|
|
4911
|
+
pPrMark: {
|
|
4912
|
+
kind: "del",
|
|
4913
|
+
info: stampInfo(stamp)
|
|
4914
|
+
}
|
|
4915
|
+
};
|
|
4916
|
+
let second = trailing;
|
|
4917
|
+
const formatting = joinedFormatting(leading, trailing);
|
|
4918
|
+
if (!sameParagraphProperties(formatting, trailing.formatting)) {
|
|
4919
|
+
second = withParagraphFormatting(trailing, formatting);
|
|
4920
|
+
if ((trailing.propertyChanges?.length ?? 0) === 0) second.propertyChanges = [paragraphPropertyChange(stampInfo(stamp), trailing.formatting)];
|
|
4921
|
+
}
|
|
4922
|
+
const committed = commit({
|
|
4923
|
+
document,
|
|
4924
|
+
op,
|
|
4925
|
+
story: op.story,
|
|
4926
|
+
at,
|
|
4927
|
+
before: [leading, trailing],
|
|
4928
|
+
after: [first, second],
|
|
4929
|
+
newIds: op.newIds
|
|
4930
|
+
});
|
|
4931
|
+
if (committed.isErr()) return Result.err(committed.error);
|
|
4932
|
+
const [placedFirst = first, placedSecond = second] = committed.value.paragraphs;
|
|
4933
|
+
const inverse = [reviewRestoring(op.story, placedFirst, leading)];
|
|
4934
|
+
if (second !== trailing) inverse.push(reviewRestoring(op.story, placedSecond, trailing));
|
|
4935
|
+
return Result.ok({
|
|
4936
|
+
document: committed.value.document,
|
|
4937
|
+
inverse,
|
|
2504
4938
|
touched: committed.value.touched
|
|
2505
4939
|
});
|
|
2506
4940
|
};
|
|
@@ -2514,40 +4948,62 @@ const joinBlocks = (document, op) => {
|
|
|
2514
4948
|
const leading = first.value.paragraph;
|
|
2515
4949
|
const trailing = next.value.paragraph;
|
|
2516
4950
|
if (leading.sectionProperties !== void 0) return refuse(op, DOCUMENT_OP_REFUSAL_REASONS.SECTION_BOUNDARY, `${op.blockId} ends a section.`);
|
|
2517
|
-
|
|
4951
|
+
const survivorSide = op.survivor ?? SPLIT_HALVES.SECOND;
|
|
4952
|
+
if (!isSplitHalf(survivorSide)) return refuse(op, DOCUMENT_OP_REFUSAL_REASONS.STRUCTURE_MISMATCH, "A half is first or second.");
|
|
4953
|
+
const survivor = survivorSide === SPLIT_HALVES.SECOND ? trailing : leading;
|
|
4954
|
+
const retired = survivorSide === SPLIT_HALVES.SECOND ? leading : trailing;
|
|
4955
|
+
if (op.expectedRetired !== void 0 && !equalForStaleness({
|
|
2518
4956
|
type: "paragraph",
|
|
2519
|
-
...splitFieldsOf(
|
|
4957
|
+
...splitFieldsOf(retired)
|
|
2520
4958
|
}, {
|
|
2521
4959
|
type: "paragraph",
|
|
2522
|
-
...op.
|
|
2523
|
-
})) return refuse(op, DOCUMENT_OP_REFUSAL_REASONS.STALE, `${
|
|
2524
|
-
|
|
2525
|
-
if (
|
|
4960
|
+
...op.expectedRetired
|
|
4961
|
+
})) return refuse(op, DOCUMENT_OP_REFUSAL_REASONS.STALE, `${retired.paraId ?? ""} states other fields than expected.`);
|
|
4962
|
+
if (op.expectedSurvivor !== void 0 && !equalForStaleness(reviewFieldsOf(survivor), op.expectedSurvivor)) return refuse(op, DOCUMENT_OP_REFUSAL_REASONS.STALE, `${survivor.paraId ?? ""} states other review fields than expected.`);
|
|
4963
|
+
if (op.revision !== void 0) return trackJoin({
|
|
4964
|
+
document,
|
|
4965
|
+
op,
|
|
4966
|
+
stamp: op.revision,
|
|
4967
|
+
at: first.value,
|
|
4968
|
+
leading,
|
|
4969
|
+
trailing
|
|
4970
|
+
});
|
|
4971
|
+
const retiredId = retired.paraId ?? "";
|
|
4972
|
+
if (!isParaId(retiredId)) return refuse(op, DOCUMENT_OP_REFUSAL_REASONS.INVALID_BLOCK_ID, `${retiredId} could not name the paragraph again.`);
|
|
2526
4973
|
const depth = op.depth ?? 0;
|
|
2527
4974
|
if (!isCount(depth)) return refuse(op, DOCUMENT_OP_REFUSAL_REASONS.STRUCTURE_MISMATCH, "A join depth is a count.");
|
|
2528
4975
|
const merged = joinedContent(op, joinContent(leading.content, trailing.content, depth), `between ${op.blockId} and ${op.nextBlockId}`);
|
|
2529
4976
|
if (merged.isErr()) return Result.err(merged.error);
|
|
2530
|
-
const joined = {
|
|
2531
|
-
...
|
|
4977
|
+
const joined = withReviewFields({
|
|
4978
|
+
...survivor,
|
|
2532
4979
|
content: merged.value.content
|
|
2533
|
-
};
|
|
2534
|
-
delete joined.
|
|
4980
|
+
}, joinedReview(leading, trailing));
|
|
4981
|
+
delete joined.sectionProperties;
|
|
2535
4982
|
if (trailing.sectionProperties !== void 0) joined.sectionProperties = trailing.sectionProperties;
|
|
2536
|
-
if (trailing.pPrMark !== void 0) joined.pPrMark = trailing.pPrMark;
|
|
2537
4983
|
const length = paragraphLength(leading);
|
|
4984
|
+
const newHalf = survivorSide === SPLIT_HALVES.SECOND ? SPLIT_HALVES.FIRST : SPLIT_HALVES.SECOND;
|
|
4985
|
+
const retiredFields = splitFieldsOf(retired);
|
|
4986
|
+
if (newHalf === SPLIT_HALVES.SECOND) delete retiredFields.propertyChanges;
|
|
2538
4987
|
const split = {
|
|
2539
4988
|
type: DOCUMENT_OP_TYPES.SPLIT_BLOCK,
|
|
2540
4989
|
at: {
|
|
2541
4990
|
story: op.story,
|
|
2542
|
-
blockId:
|
|
4991
|
+
blockId: survivor.paraId ?? "",
|
|
2543
4992
|
offset: length,
|
|
2544
4993
|
zeroWidthBefore: zeroWidthLeavesAt(leading.content, length).length
|
|
2545
4994
|
},
|
|
2546
|
-
newBlockId:
|
|
2547
|
-
|
|
4995
|
+
newBlockId: retiredId,
|
|
4996
|
+
newHalf,
|
|
4997
|
+
newParagraph: retiredFields
|
|
2548
4998
|
};
|
|
2549
4999
|
if (leading.pPrMark !== void 0) split.firstMark = leading.pPrMark;
|
|
2550
5000
|
if (namesIds(merged.value.retired)) split.newIds = merged.value.retired;
|
|
5001
|
+
const splitReview = reviewFieldsOf(joined);
|
|
5002
|
+
if (survivorSide === SPLIT_HALVES.FIRST) {
|
|
5003
|
+
delete splitReview.propertyChanges;
|
|
5004
|
+
delete splitReview.pPrMark;
|
|
5005
|
+
if (leading.pPrMark !== void 0) splitReview.pPrMark = leading.pPrMark;
|
|
5006
|
+
}
|
|
2551
5007
|
const committed = commit({
|
|
2552
5008
|
document,
|
|
2553
5009
|
op,
|
|
@@ -2560,7 +5016,7 @@ const joinBlocks = (document, op) => {
|
|
|
2560
5016
|
if (committed.isErr()) return Result.err(committed.error);
|
|
2561
5017
|
return Result.ok({
|
|
2562
5018
|
document: committed.value.document,
|
|
2563
|
-
inverse: [split],
|
|
5019
|
+
inverse: [split, ...reviewSetting(op.story, survivor.paraId ?? "", splitReview, reviewFieldsOf(survivor))],
|
|
2564
5020
|
touched: committed.value.touched
|
|
2565
5021
|
});
|
|
2566
5022
|
};
|
|
@@ -2590,7 +5046,7 @@ const replaceBlocks = (document, op) => {
|
|
|
2590
5046
|
if (!found.every((location, offset) => sameBlockList(location.list, start.list) && location.index === start.index + offset)) return refuse(op, DOCUMENT_OP_REFUSAL_REASONS.NOT_ADJACENT, "The replaced paragraphs are not adjacent.");
|
|
2591
5047
|
if (found.some((location, offset) => !equalForStaleness(location.paragraph, op.expected[offset]))) return refuse(op, DOCUMENT_OP_REFUSAL_REASONS.STALE, "The paragraphs to replace have changed.");
|
|
2592
5048
|
if (op.blocks.some(({ paraId }) => paraId === void 0)) return refuse(op, DOCUMENT_OP_REFUSAL_REASONS.INVALID_BLOCK_ID, "A replacement paragraph has no id.");
|
|
2593
|
-
if (op.blocks.some(({ content }) => holdsEmptyRecord(content))) return refuse(op, DOCUMENT_OP_REFUSAL_REASONS.EMPTY_CONTENT, "A replacement paragraph holds an empty run or
|
|
5049
|
+
if (op.blocks.some(({ content }) => holdsEmptyRecord(content))) return refuse(op, DOCUMENT_OP_REFUSAL_REASONS.EMPTY_CONTENT, "A replacement paragraph holds an empty run, text node, or revision wrapper.");
|
|
2594
5050
|
const before = found.map(({ paragraph }) => paragraph);
|
|
2595
5051
|
const kept = new Set(before.map(({ paraId }) => idKey(paraId ?? "")));
|
|
2596
5052
|
const placed = new Set(op.blocks.map(({ paraId }) => idKey(paraId ?? "")));
|
|
@@ -2619,8 +5075,77 @@ const replaceBlocks = (document, op) => {
|
|
|
2619
5075
|
touched: touchedBetween(before, op.blocks)
|
|
2620
5076
|
});
|
|
2621
5077
|
};
|
|
5078
|
+
/** Revision and control ids a paragraph's own review fields carry, as slot keys. */
|
|
5079
|
+
const reviewKeysOf = (review) => identityKeysIn({
|
|
5080
|
+
propertyChanges: review.propertyChanges,
|
|
5081
|
+
pPrMark: review.pPrMark
|
|
5082
|
+
});
|
|
5083
|
+
const setParagraphReview = (document, op) => {
|
|
5084
|
+
const located = locate(document, op, op.story, op.blockId);
|
|
5085
|
+
if (located.isErr()) return Result.err(located.error);
|
|
5086
|
+
const { paragraph } = located.value;
|
|
5087
|
+
if (!equalForStaleness(reviewFieldsOf(paragraph), op.expected)) return refuse(op, DOCUMENT_OP_REFUSAL_REASONS.STALE, `${op.blockId} states other review fields than expected.`);
|
|
5088
|
+
const review = structuredClone(op.review);
|
|
5089
|
+
const next = withReviewFields(paragraph, review);
|
|
5090
|
+
if (structurallyEqual(next, paragraph)) return unchanged(document);
|
|
5091
|
+
if (review.pPrMark !== void 0 && !structurallyEqual(review.pPrMark, paragraph.pPrMark) && endsItsContainer(storyBody(document, op.story), located.value) && !(located.value.list.some((step) => step.kind === "tableCell") && (review.pPrMark.kind === "ins" || review.pPrMark.kind === "del"))) return refuse(op, DOCUMENT_OP_REFUSAL_REASONS.CONTAINER_FINAL_MARK, `${op.blockId} ends its container: there is no next paragraph its mark could join.`);
|
|
5092
|
+
const taken = identitiesOutside(document, [paragraph]);
|
|
5093
|
+
for (const key of identityKeysIn(paragraph.content)) taken.add(key);
|
|
5094
|
+
const incoming = reviewKeysOf(review);
|
|
5095
|
+
if (incoming.some((key) => taken.has(key)) || new Set(incoming).size !== incoming.length) return refuse(op, DOCUMENT_OP_REFUSAL_REASONS.ID_COLLISION, "The review fields carry a revision id already used in the package.");
|
|
5096
|
+
return Result.ok({
|
|
5097
|
+
document: replaceParagraphs({
|
|
5098
|
+
document,
|
|
5099
|
+
story: op.story,
|
|
5100
|
+
at: located.value,
|
|
5101
|
+
count: 1,
|
|
5102
|
+
replacement: [next]
|
|
5103
|
+
}),
|
|
5104
|
+
inverse: [reviewRestoring(op.story, next, paragraph)],
|
|
5105
|
+
touched: touchedBetween([paragraph], [next])
|
|
5106
|
+
});
|
|
5107
|
+
};
|
|
5108
|
+
const replaceInline = (document, op) => {
|
|
5109
|
+
const located = locate(document, op, op.story, op.blockId);
|
|
5110
|
+
if (located.isErr()) return Result.err(located.error);
|
|
5111
|
+
const { paragraph } = located.value;
|
|
5112
|
+
if (!equalForStaleness(paragraph.content, op.expected)) return refuse(op, DOCUMENT_OP_REFUSAL_REASONS.STALE, `${op.blockId} holds other content than expected.`);
|
|
5113
|
+
const content = structuredClone(op.content);
|
|
5114
|
+
if (holdsEmptyRecord(content)) return refuse(op, DOCUMENT_OP_REFUSAL_REASONS.EMPTY_CONTENT, "The content holds an empty run, text node, or revision wrapper.");
|
|
5115
|
+
if (textsIn(content).some(hasIllegalXmlCharacters)) return refuse(op, DOCUMENT_OP_REFUSAL_REASONS.INVALID_TEXT, "The content holds text that cannot be written.");
|
|
5116
|
+
if (structurallyEqual(content, paragraph.content)) return unchanged(document);
|
|
5117
|
+
const remaining = countIds(packageParagraphIds(document.package));
|
|
5118
|
+
for (const id of paragraphIdsIn(paragraph.content)) remaining.set(idKey(id), (remaining.get(idKey(id)) ?? 1) - 1);
|
|
5119
|
+
if (collides(remaining, paragraphIdsIn(content))) return refuse(op, DOCUMENT_OP_REFUSAL_REASONS.ID_COLLISION, "The content holds a paragraph id the package already uses.");
|
|
5120
|
+
const taken = identitiesOutside(document, [paragraph]);
|
|
5121
|
+
for (const key of reviewKeysOf(reviewFieldsOf(paragraph))) taken.add(key);
|
|
5122
|
+
const incoming = identityKeysIn(content);
|
|
5123
|
+
if (incoming.some((key) => taken.has(key)) || new Set(incoming).size !== incoming.length) return refuse(op, DOCUMENT_OP_REFUSAL_REASONS.ID_COLLISION, "The content carries a revision or content-control id already used in the package.");
|
|
5124
|
+
const next = withContent(paragraph, content);
|
|
5125
|
+
return Result.ok({
|
|
5126
|
+
document: replaceParagraphs({
|
|
5127
|
+
document,
|
|
5128
|
+
story: op.story,
|
|
5129
|
+
at: located.value,
|
|
5130
|
+
count: 1,
|
|
5131
|
+
replacement: [next]
|
|
5132
|
+
}),
|
|
5133
|
+
inverse: [contentRestoring(op.story, next, paragraph)],
|
|
5134
|
+
touched: touchedBetween([paragraph], [next])
|
|
5135
|
+
});
|
|
5136
|
+
};
|
|
2622
5137
|
const dispatch = (document, op) => {
|
|
2623
5138
|
switch (op.type) {
|
|
5139
|
+
case DOCUMENT_OP_TYPES.DELETE_BLOCKS: return deleteBlocks({
|
|
5140
|
+
document,
|
|
5141
|
+
op,
|
|
5142
|
+
applyOps: applyDocumentOps
|
|
5143
|
+
});
|
|
5144
|
+
case DOCUMENT_OP_TYPES.INSERT_BLOCKS: return insertBlocks({
|
|
5145
|
+
document,
|
|
5146
|
+
op,
|
|
5147
|
+
applyOps: applyDocumentOps
|
|
5148
|
+
});
|
|
2624
5149
|
case DOCUMENT_OP_TYPES.INSERT_TEXT: return insertText(document, op);
|
|
2625
5150
|
case DOCUMENT_OP_TYPES.INSERT_CONTENT: return insertContent(document, op);
|
|
2626
5151
|
case DOCUMENT_OP_TYPES.DELETE_RANGE: return deleteRange(document, op);
|
|
@@ -2631,68 +5156,499 @@ const dispatch = (document, op) => {
|
|
|
2631
5156
|
case DOCUMENT_OP_TYPES.SPLIT_BLOCK: return splitBlock(document, op);
|
|
2632
5157
|
case DOCUMENT_OP_TYPES.JOIN_BLOCKS: return joinBlocks(document, op);
|
|
2633
5158
|
case DOCUMENT_OP_TYPES.REPLACE_BLOCKS: return replaceBlocks(document, op);
|
|
5159
|
+
case DOCUMENT_OP_TYPES.SET_PARAGRAPH_REVIEW: return setParagraphReview(document, op);
|
|
5160
|
+
case DOCUMENT_OP_TYPES.REPLACE_INLINE: return replaceInline(document, op);
|
|
5161
|
+
case DOCUMENT_OP_TYPES.RESOLVE_REVISION: return resolveRevision(document, op, applyDocumentOps);
|
|
5162
|
+
case DOCUMENT_OP_TYPES.INSERT_TABLE:
|
|
5163
|
+
case DOCUMENT_OP_TYPES.DELETE_TABLE:
|
|
5164
|
+
case DOCUMENT_OP_TYPES.SET_CONTAINER_BLOCKS: return applyTableOp(document, op);
|
|
5165
|
+
case DOCUMENT_OP_TYPES.INSERT_ROW:
|
|
5166
|
+
case DOCUMENT_OP_TYPES.DELETE_ROW:
|
|
5167
|
+
case DOCUMENT_OP_TYPES.SET_TABLE_ROWS: return applyRowOp(document, op);
|
|
5168
|
+
default: return op;
|
|
5169
|
+
}
|
|
5170
|
+
};
|
|
5171
|
+
/** The stamp of a tracked operation; `undefined` for a direct one. */
|
|
5172
|
+
const stampOf = (op) => {
|
|
5173
|
+
switch (op.type) {
|
|
5174
|
+
case DOCUMENT_OP_TYPES.DELETE_BLOCKS:
|
|
5175
|
+
case DOCUMENT_OP_TYPES.INSERT_BLOCKS:
|
|
5176
|
+
case DOCUMENT_OP_TYPES.INSERT_TEXT:
|
|
5177
|
+
case DOCUMENT_OP_TYPES.INSERT_CONTENT:
|
|
5178
|
+
case DOCUMENT_OP_TYPES.DELETE_RANGE:
|
|
5179
|
+
case DOCUMENT_OP_TYPES.SET_RUN_PROPS:
|
|
5180
|
+
case DOCUMENT_OP_TYPES.SET_PARAGRAPH_PROPS:
|
|
5181
|
+
case DOCUMENT_OP_TYPES.SPLIT_BLOCK:
|
|
5182
|
+
case DOCUMENT_OP_TYPES.JOIN_BLOCKS:
|
|
5183
|
+
case DOCUMENT_OP_TYPES.INSERT_ROW:
|
|
5184
|
+
case DOCUMENT_OP_TYPES.DELETE_ROW:
|
|
5185
|
+
case DOCUMENT_OP_TYPES.INSERT_TABLE:
|
|
5186
|
+
case DOCUMENT_OP_TYPES.DELETE_TABLE: return op.revision;
|
|
5187
|
+
case DOCUMENT_OP_TYPES.SPLIT_INLINE:
|
|
5188
|
+
case DOCUMENT_OP_TYPES.JOIN_INLINE:
|
|
5189
|
+
case DOCUMENT_OP_TYPES.REPLACE_BLOCKS:
|
|
5190
|
+
case DOCUMENT_OP_TYPES.SET_PARAGRAPH_REVIEW:
|
|
5191
|
+
case DOCUMENT_OP_TYPES.REPLACE_INLINE:
|
|
5192
|
+
case DOCUMENT_OP_TYPES.RESOLVE_REVISION:
|
|
5193
|
+
case DOCUMENT_OP_TYPES.SET_TABLE_ROWS:
|
|
5194
|
+
case DOCUMENT_OP_TYPES.SET_CONTAINER_BLOCKS: return;
|
|
2634
5195
|
default: return op;
|
|
2635
5196
|
}
|
|
2636
5197
|
};
|
|
5198
|
+
/** The revision ids a tracked operation's records took. */
|
|
5199
|
+
const recordedRevisions = (before, edit, stamp) => {
|
|
5200
|
+
const touched = new Set([...edit.touched.modified, ...edit.touched.inserted].map(idKey));
|
|
5201
|
+
const paragraphs = storyParagraphs(edit.document.package.document).map(({ paragraph }) => paragraph).filter(({ paraId }) => paraId !== void 0 && touched.has(idKey(paraId)));
|
|
5202
|
+
const known = new Set(packageIdentityKeys(before.package));
|
|
5203
|
+
const revisions = stampedRevisionIds(paragraphs, stamp, known);
|
|
5204
|
+
revisions.unshift(...stampedTableRowRevisionIds(edit.document.package.document.content, stamp, known));
|
|
5205
|
+
return revisions;
|
|
5206
|
+
};
|
|
2637
5207
|
/** Apply one operation to a document that meets the seed contract. */
|
|
2638
5208
|
const applyDocumentOp = (document, op) => {
|
|
2639
5209
|
const valid = validateOpsDocument(document);
|
|
2640
|
-
if (valid.isErr()) return
|
|
5210
|
+
if (valid.isErr()) return Result.err(refusal(op, valid.error.reason, valid.error.message));
|
|
5211
|
+
const stamp = stampOf(op);
|
|
5212
|
+
const badStamp = stamp === void 0 ? void 0 : stampRefusal(document, op, stamp);
|
|
5213
|
+
if (badStamp !== void 0) return Result.err(badStamp);
|
|
2641
5214
|
const applied = dispatch(document, op);
|
|
2642
|
-
if (applied.
|
|
2643
|
-
|
|
2644
|
-
|
|
2645
|
-
|
|
2646
|
-
|
|
2647
|
-
|
|
2648
|
-
inserted: "modified",
|
|
2649
|
-
removed: "removed"
|
|
2650
|
-
},
|
|
2651
|
-
inserted: {
|
|
2652
|
-
modified: "inserted",
|
|
2653
|
-
inserted: "inserted",
|
|
2654
|
-
removed: void 0
|
|
2655
|
-
},
|
|
2656
|
-
removed: {
|
|
2657
|
-
modified: "modified",
|
|
2658
|
-
inserted: "modified",
|
|
2659
|
-
removed: "removed"
|
|
2660
|
-
}
|
|
5215
|
+
if (applied.isErr()) return Result.err(applied.error);
|
|
5216
|
+
meetsContract(applied.value.document);
|
|
5217
|
+
return Result.ok({
|
|
5218
|
+
...applied.value,
|
|
5219
|
+
revisions: stamp === void 0 ? [] : recordedRevisions(document, applied.value, stamp)
|
|
5220
|
+
});
|
|
2661
5221
|
};
|
|
2662
5222
|
/**
|
|
2663
5223
|
* Apply operations in order, atomically: the first refusal is returned and
|
|
2664
|
-
* the document is unchanged. The inverse undoes the whole list
|
|
5224
|
+
* the document is unchanged. The inverse undoes the whole list, and
|
|
5225
|
+
* `revisions` lists each operation's in turn.
|
|
2665
5226
|
*/
|
|
2666
5227
|
const applyDocumentOps = (document, ops) => {
|
|
2667
5228
|
let current = document;
|
|
2668
|
-
const
|
|
2669
|
-
const
|
|
5229
|
+
const edits = [];
|
|
5230
|
+
const revisions = [];
|
|
2670
5231
|
for (const op of ops) {
|
|
2671
5232
|
const applied = applyDocumentOp(current, op);
|
|
2672
5233
|
if (applied.isErr()) return applied;
|
|
2673
5234
|
current = applied.value.document;
|
|
2674
|
-
|
|
2675
|
-
|
|
2676
|
-
"modified",
|
|
2677
|
-
"inserted",
|
|
2678
|
-
"removed"
|
|
2679
|
-
]) for (const id of applied.value.touched[state]) {
|
|
2680
|
-
const previous = touched.get(id);
|
|
2681
|
-
const nextState = previous === void 0 ? state : TOUCH_TRANSITIONS[previous][state];
|
|
2682
|
-
if (nextState === void 0) touched.delete(id);
|
|
2683
|
-
else touched.set(id, nextState);
|
|
2684
|
-
}
|
|
5235
|
+
edits.push(applied.value);
|
|
5236
|
+
revisions.push(...applied.value.revisions);
|
|
2685
5237
|
}
|
|
2686
|
-
const ids = (state) => [...touched].flatMap(([id, value]) => value === state ? [id] : []);
|
|
2687
5238
|
return Result.ok({
|
|
2688
|
-
document
|
|
2689
|
-
|
|
2690
|
-
|
|
2691
|
-
|
|
2692
|
-
|
|
2693
|
-
|
|
5239
|
+
...combineEdits(document, edits),
|
|
5240
|
+
revisions
|
|
5241
|
+
});
|
|
5242
|
+
};
|
|
5243
|
+
//#endregion
|
|
5244
|
+
//#region src/ops/plan.ts
|
|
5245
|
+
/**
|
|
5246
|
+
* Pure planning around tracked operations: how many new revision ids an
|
|
5247
|
+
* operation takes, and the operations a tracked deletion of a range is made
|
|
5248
|
+
* of once the author's own insertions and comment anchors are accounted for.
|
|
5249
|
+
*/
|
|
5250
|
+
/** The ids of one space among slot keys. */
|
|
5251
|
+
const idsIn = (keys, space) => {
|
|
5252
|
+
const prefix = `${space}:`;
|
|
5253
|
+
return keys.flatMap((key) => key.startsWith(prefix) ? [Number(key.slice(prefix.length))] : []);
|
|
5254
|
+
};
|
|
5255
|
+
/** The ids a package uses in one space. */
|
|
5256
|
+
const usedIds = (document, space) => new Set(idsIn(packageIdentityKeys(document.package), space));
|
|
5257
|
+
/** The same operation with other new ids; one that takes none is returned as it is. */
|
|
5258
|
+
const withNewIds = (op, newIds) => {
|
|
5259
|
+
switch (op.type) {
|
|
5260
|
+
case DOCUMENT_OP_TYPES.DELETE_BLOCKS:
|
|
5261
|
+
case DOCUMENT_OP_TYPES.INSERT_BLOCKS:
|
|
5262
|
+
case DOCUMENT_OP_TYPES.INSERT_TEXT:
|
|
5263
|
+
case DOCUMENT_OP_TYPES.INSERT_CONTENT:
|
|
5264
|
+
case DOCUMENT_OP_TYPES.DELETE_RANGE:
|
|
5265
|
+
case DOCUMENT_OP_TYPES.SPLIT_INLINE:
|
|
5266
|
+
case DOCUMENT_OP_TYPES.SET_RUN_PROPS:
|
|
5267
|
+
case DOCUMENT_OP_TYPES.SPLIT_BLOCK:
|
|
5268
|
+
case DOCUMENT_OP_TYPES.JOIN_BLOCKS:
|
|
5269
|
+
case DOCUMENT_OP_TYPES.INSERT_ROW:
|
|
5270
|
+
case DOCUMENT_OP_TYPES.DELETE_ROW:
|
|
5271
|
+
case DOCUMENT_OP_TYPES.INSERT_TABLE:
|
|
5272
|
+
case DOCUMENT_OP_TYPES.DELETE_TABLE: return {
|
|
5273
|
+
...op,
|
|
5274
|
+
newIds
|
|
5275
|
+
};
|
|
5276
|
+
case DOCUMENT_OP_TYPES.JOIN_INLINE:
|
|
5277
|
+
case DOCUMENT_OP_TYPES.SET_PARAGRAPH_PROPS:
|
|
5278
|
+
case DOCUMENT_OP_TYPES.REPLACE_BLOCKS:
|
|
5279
|
+
case DOCUMENT_OP_TYPES.SET_PARAGRAPH_REVIEW:
|
|
5280
|
+
case DOCUMENT_OP_TYPES.REPLACE_INLINE:
|
|
5281
|
+
case DOCUMENT_OP_TYPES.RESOLVE_REVISION:
|
|
5282
|
+
case DOCUMENT_OP_TYPES.SET_TABLE_ROWS:
|
|
5283
|
+
case DOCUMENT_OP_TYPES.SET_CONTAINER_BLOCKS: return op;
|
|
5284
|
+
default: return op;
|
|
5285
|
+
}
|
|
5286
|
+
};
|
|
5287
|
+
/** The largest id count {@link revisionIdDemand} searches before giving up. */
|
|
5288
|
+
const MAX_DEMAND = 65536;
|
|
5289
|
+
/**
|
|
5290
|
+
* How many `newIds.revision` entries an operation takes on a document,
|
|
5291
|
+
* besides its stamp's id: the length of the shortest list of unused ids it
|
|
5292
|
+
* applies with. Content-control ids are supplied freely while counting. The
|
|
5293
|
+
* refusal is the one the operation meets whatever ids it is given.
|
|
5294
|
+
*/
|
|
5295
|
+
const revisionIdDemand = (document, op) => {
|
|
5296
|
+
const largest = (ids, floor) => {
|
|
5297
|
+
let out = floor;
|
|
5298
|
+
for (const id of ids) out = Math.max(out, id);
|
|
5299
|
+
return out;
|
|
5300
|
+
};
|
|
5301
|
+
const carried = identityKeysIn(op);
|
|
5302
|
+
const first = largest(/* @__PURE__ */ new Set([...usedIds(document, IDENTITY_SPACES.REVISION), ...idsIn(carried, IDENTITY_SPACES.REVISION)]), stampOf(op)?.id ?? 0) + 1;
|
|
5303
|
+
const pool = (count) => Array.from({ length: count }, (_, index) => first + index);
|
|
5304
|
+
const firstControl = largest(/* @__PURE__ */ new Set([...usedIds(document, IDENTITY_SPACES.CONTROL), ...idsIn(carried, IDENTITY_SPACES.CONTROL)]), 0) + 1;
|
|
5305
|
+
const control = Array.from({ length: 64 }, (_, index) => firstControl + index);
|
|
5306
|
+
const attempt = (count) => applyDocumentOp(document, withNewIds(op, {
|
|
5307
|
+
revision: pool(count),
|
|
5308
|
+
control
|
|
5309
|
+
}));
|
|
5310
|
+
const needsMore = (count) => {
|
|
5311
|
+
const applied = attempt(count);
|
|
5312
|
+
if (applied.isOk()) return false;
|
|
5313
|
+
return applied.error.reason === DOCUMENT_OP_REFUSAL_REASONS.NEEDS_NEW_IDS ? true : applied.error;
|
|
5314
|
+
};
|
|
5315
|
+
const none = needsMore(0);
|
|
5316
|
+
if (none !== true) return none === false ? Result.ok(0) : Result.err(none);
|
|
5317
|
+
let low = 0;
|
|
5318
|
+
let high = 1;
|
|
5319
|
+
for (;;) {
|
|
5320
|
+
const outcome = needsMore(high);
|
|
5321
|
+
if (outcome === false) break;
|
|
5322
|
+
if (outcome !== true) return Result.err(outcome);
|
|
5323
|
+
if (high >= MAX_DEMAND || first + high > 2147483647) return Result.err(new DocumentOpRefusal({
|
|
5324
|
+
message: "The operation takes more new ids than a package can hold.",
|
|
5325
|
+
reason: DOCUMENT_OP_REFUSAL_REASONS.NEEDS_NEW_IDS,
|
|
5326
|
+
opType: op.type
|
|
5327
|
+
}));
|
|
5328
|
+
low = high;
|
|
5329
|
+
high *= 2;
|
|
5330
|
+
}
|
|
5331
|
+
while (high - low > 1) {
|
|
5332
|
+
const middle = Math.floor((low + high) / 2);
|
|
5333
|
+
if (needsMore(middle) === false) high = middle;
|
|
5334
|
+
else low = middle;
|
|
5335
|
+
}
|
|
5336
|
+
return Result.ok(high);
|
|
5337
|
+
};
|
|
5338
|
+
const leafPlan = ({ node, ancestors }, author) => {
|
|
5339
|
+
if (ancestors.some(isRemovedRevisionNode)) return "untouched";
|
|
5340
|
+
if (isCommentAnchor(node)) return "anchor";
|
|
5341
|
+
return ancestors.some((ancestor) => ancestor.type === "insertion" && isTrackedWrapper(ancestor) && ancestor.info.author === author) ? "direct" : "tracked";
|
|
5342
|
+
};
|
|
5343
|
+
const laterGap = (left, right) => compareGaps(left, right) >= 0 ? left : right;
|
|
5344
|
+
const earlierGap = (left, right) => compareGaps(left, right) <= 0 ? left : right;
|
|
5345
|
+
/**
|
|
5346
|
+
* The stretches of a range to delete directly and with tracking, in document
|
|
5347
|
+
* order. A comment anchor ends a stretch; a tracked stretch runs across
|
|
5348
|
+
* deleted content, which it leaves alone, and one holding nothing else is
|
|
5349
|
+
* dropped.
|
|
5350
|
+
*/
|
|
5351
|
+
const segmentsOf = (spans, from, to, author) => {
|
|
5352
|
+
const out = [];
|
|
5353
|
+
let open;
|
|
5354
|
+
for (const span of spans) {
|
|
5355
|
+
const start = laterGap(span.before, from);
|
|
5356
|
+
const end = earlierGap(span.after, to);
|
|
5357
|
+
if (compareGaps(start, end) >= 0) continue;
|
|
5358
|
+
const plan = leafPlan(span, author);
|
|
5359
|
+
if (plan === "anchor") {
|
|
5360
|
+
open = void 0;
|
|
5361
|
+
continue;
|
|
5362
|
+
}
|
|
5363
|
+
const segmentPlan = plan === "untouched" ? "tracked" : plan;
|
|
5364
|
+
const records = plan !== "untouched";
|
|
5365
|
+
if (open !== void 0 && open.plan === segmentPlan && compareGaps(open.to, start) === 0) {
|
|
5366
|
+
open.to = end;
|
|
5367
|
+
open.records ||= records;
|
|
5368
|
+
continue;
|
|
5369
|
+
}
|
|
5370
|
+
open = {
|
|
5371
|
+
plan: segmentPlan,
|
|
5372
|
+
from: start,
|
|
5373
|
+
to: end,
|
|
5374
|
+
records
|
|
5375
|
+
};
|
|
5376
|
+
out.push(open);
|
|
5377
|
+
}
|
|
5378
|
+
return out.filter(({ records }) => records);
|
|
5379
|
+
};
|
|
5380
|
+
const positionOf = (at, gap) => ({
|
|
5381
|
+
story: at.story,
|
|
5382
|
+
blockId: at.blockId,
|
|
5383
|
+
offset: gap.offset,
|
|
5384
|
+
zeroWidthBefore: gap.zeroWidthBefore
|
|
5385
|
+
});
|
|
5386
|
+
const outOfIds = () => Result.err(new DocumentOpRefusal({
|
|
5387
|
+
message: "The planned deletions need more new revision ids.",
|
|
5388
|
+
reason: DOCUMENT_OP_REFUSAL_REASONS.NEEDS_NEW_IDS,
|
|
5389
|
+
opType: DOCUMENT_OP_TYPES.DELETE_RANGE
|
|
5390
|
+
}));
|
|
5391
|
+
/** Shared allocation and simulation for all pieces of one planned edit. */
|
|
5392
|
+
const createTrackedPlan = ({ document, revision, newIds }) => {
|
|
5393
|
+
const used = usedIds(document, IDENTITY_SPACES.REVISION);
|
|
5394
|
+
const pool = [...new Set(newIds?.revision ?? [])].filter((id) => !used.has(id) && id !== revision.id);
|
|
5395
|
+
let taken = 0;
|
|
5396
|
+
let stampUsed = false;
|
|
5397
|
+
let current = document;
|
|
5398
|
+
const ops = [];
|
|
5399
|
+
const take = (count) => {
|
|
5400
|
+
if (taken + count > pool.length) return void 0;
|
|
5401
|
+
const ids = pool.slice(taken, taken + count);
|
|
5402
|
+
taken += count;
|
|
5403
|
+
return ids;
|
|
5404
|
+
};
|
|
5405
|
+
const append = (input) => {
|
|
5406
|
+
let op = input;
|
|
5407
|
+
if (input.revision !== void 0) {
|
|
5408
|
+
const stampId = stampUsed ? take(1)?.at(0) : revision.id;
|
|
5409
|
+
if (stampId === void 0) return outOfIds();
|
|
5410
|
+
op = {
|
|
5411
|
+
...input,
|
|
5412
|
+
revision: {
|
|
5413
|
+
...revision,
|
|
5414
|
+
id: stampId
|
|
5415
|
+
}
|
|
5416
|
+
};
|
|
5417
|
+
const demand = revisionIdDemand(current, op);
|
|
5418
|
+
if (demand.isErr()) return Result.err(demand.error);
|
|
5419
|
+
const ids = take(demand.value);
|
|
5420
|
+
if (ids === void 0) return outOfIds();
|
|
5421
|
+
const controls = usedIds(current, IDENTITY_SPACES.CONTROL);
|
|
5422
|
+
op = withNewIds(op, {
|
|
5423
|
+
revision: ids,
|
|
5424
|
+
control: (newIds?.control ?? []).filter((id) => !controls.has(id))
|
|
5425
|
+
});
|
|
5426
|
+
}
|
|
5427
|
+
const applied = applyDocumentOp(current, op);
|
|
5428
|
+
if (applied.isErr()) return Result.err(applied.error);
|
|
5429
|
+
stampUsed ||= applied.value.revisions.length > 0;
|
|
5430
|
+
current = applied.value.document;
|
|
5431
|
+
ops.push(op);
|
|
5432
|
+
return Result.ok(void 0);
|
|
5433
|
+
};
|
|
5434
|
+
return {
|
|
5435
|
+
append,
|
|
5436
|
+
document: () => current,
|
|
5437
|
+
ops
|
|
5438
|
+
};
|
|
5439
|
+
};
|
|
5440
|
+
/** Validate and segment each paragraph before planning any edits. */
|
|
5441
|
+
const appendTrackedDeletion = ({ document, options, plan }) => {
|
|
5442
|
+
const { from, to, revision } = options;
|
|
5443
|
+
const refuse = (reason, message) => Result.err(new DocumentOpRefusal({
|
|
5444
|
+
reason,
|
|
5445
|
+
message,
|
|
5446
|
+
opType: DOCUMENT_OP_TYPES.DELETE_RANGE
|
|
5447
|
+
}));
|
|
5448
|
+
if (from.story !== to.story) return refuse(DOCUMENT_OP_REFUSAL_REASONS.UNTRACKABLE, "A planned range must stay in one story.");
|
|
5449
|
+
const body = storyBody(document, from.story);
|
|
5450
|
+
const paragraphs = storyParagraphs(body);
|
|
5451
|
+
const first = paragraphs.find(({ paragraph }) => idKey(paragraph.paraId ?? "") === idKey(from.blockId));
|
|
5452
|
+
const last = paragraphs.find(({ paragraph }) => idKey(paragraph.paraId ?? "") === idKey(to.blockId));
|
|
5453
|
+
if (first === void 0 || last === void 0) return refuse(DOCUMENT_OP_REFUSAL_REASONS.BLOCK_NOT_FOUND, "A range endpoint does not exist.");
|
|
5454
|
+
if (!sameBlockList(first.list, last.list)) return refuse(DOCUMENT_OP_REFUSAL_REASONS.UNTRACKABLE, "A planned range must stay in one block list.");
|
|
5455
|
+
if (first.index > last.index) return refuse(DOCUMENT_OP_REFUSAL_REASONS.NOT_ADJACENT, "Range endpoints must be in document order.");
|
|
5456
|
+
const list = blockListAt(body.content, first.list);
|
|
5457
|
+
const ranges = [];
|
|
5458
|
+
for (let index = first.index; index <= last.index; index += 1) {
|
|
5459
|
+
const paragraph = list[index];
|
|
5460
|
+
if (paragraph?.type !== "paragraph") return refuse(DOCUMENT_OP_REFUSAL_REASONS.UNTRACKABLE, "A planned range cannot cross a non-paragraph block.");
|
|
5461
|
+
const at = {
|
|
5462
|
+
story: from.story,
|
|
5463
|
+
blockId: paragraph.paraId ?? "",
|
|
5464
|
+
offset: 0
|
|
5465
|
+
};
|
|
5466
|
+
const start = index === first.index ? from : {
|
|
5467
|
+
...at,
|
|
5468
|
+
zeroWidthBefore: 0
|
|
5469
|
+
};
|
|
5470
|
+
const end = index === last.index ? to : {
|
|
5471
|
+
...at,
|
|
5472
|
+
offset: paragraphLength(paragraph),
|
|
5473
|
+
zeroWidthBefore: zeroWidthLeavesAt(paragraph.content, paragraphLength(paragraph)).length
|
|
5474
|
+
};
|
|
5475
|
+
const check = applyDocumentOp(document, {
|
|
5476
|
+
type: DOCUMENT_OP_TYPES.DELETE_RANGE,
|
|
5477
|
+
from: start,
|
|
5478
|
+
to: end
|
|
5479
|
+
});
|
|
5480
|
+
if (check.isErr()) return Result.err(check.error);
|
|
5481
|
+
if (first.index !== last.index) {
|
|
5482
|
+
if (index < last.index && paragraph.sectionProperties !== void 0) return refuse(DOCUMENT_OP_REFUSAL_REASONS.UNTRACKABLE, "A planned range cannot cross a section boundary.");
|
|
5483
|
+
if ((paragraph.propertyChanges?.length ?? 0) > 0) return refuse(DOCUMENT_OP_REFUSAL_REASONS.REVISION_CONFLICT, "Cross-paragraph planning cannot replace an existing property review.");
|
|
2694
5484
|
}
|
|
5485
|
+
const content = paragraph.content;
|
|
5486
|
+
const segments = segmentsOf(leafSpans(content), {
|
|
5487
|
+
offset: start.offset,
|
|
5488
|
+
zeroWidthBefore: start.zeroWidthBefore ?? zeroWidthLeavesAt(content, start.offset).length
|
|
5489
|
+
}, {
|
|
5490
|
+
offset: end.offset,
|
|
5491
|
+
zeroWidthBefore: end.zeroWidthBefore ?? 0
|
|
5492
|
+
}, revision.author);
|
|
5493
|
+
ranges.push({
|
|
5494
|
+
paragraph,
|
|
5495
|
+
at,
|
|
5496
|
+
segments
|
|
5497
|
+
});
|
|
5498
|
+
}
|
|
5499
|
+
let direct = document;
|
|
5500
|
+
for (const { at, segments } of ranges.toReversed()) for (const segment of segments.toReversed()) {
|
|
5501
|
+
const op = {
|
|
5502
|
+
type: DOCUMENT_OP_TYPES.DELETE_RANGE,
|
|
5503
|
+
from: positionOf(at, segment.from),
|
|
5504
|
+
to: positionOf(at, segment.to)
|
|
5505
|
+
};
|
|
5506
|
+
const appended = plan.append(segment.plan === "tracked" ? {
|
|
5507
|
+
...op,
|
|
5508
|
+
revision
|
|
5509
|
+
} : op);
|
|
5510
|
+
if (appended.isErr()) return appended;
|
|
5511
|
+
const simulated = applyDocumentOp(direct, op);
|
|
5512
|
+
if (simulated.isErr()) return Result.err(simulated.error);
|
|
5513
|
+
direct = simulated.value.document;
|
|
5514
|
+
}
|
|
5515
|
+
if (first.index === last.index) return Result.ok(void 0);
|
|
5516
|
+
for (let index = ranges.length - 2; index >= 0; index -= 1) {
|
|
5517
|
+
const leading = ranges[index];
|
|
5518
|
+
const following = ranges[index + 1];
|
|
5519
|
+
if (leading === void 0 || following === void 0) return refuse(DOCUMENT_OP_REFUSAL_REASONS.UNTRACKABLE, "A planned join has no endpoint.");
|
|
5520
|
+
const op = {
|
|
5521
|
+
type: DOCUMENT_OP_TYPES.JOIN_BLOCKS,
|
|
5522
|
+
story: from.story,
|
|
5523
|
+
blockId: leading.at.blockId,
|
|
5524
|
+
nextBlockId: following.at.blockId,
|
|
5525
|
+
revision
|
|
5526
|
+
};
|
|
5527
|
+
const appended = plan.append(op);
|
|
5528
|
+
if (appended.isErr()) return appended;
|
|
5529
|
+
const simulated = applyDocumentOp(direct, {
|
|
5530
|
+
type: DOCUMENT_OP_TYPES.JOIN_BLOCKS,
|
|
5531
|
+
story: from.story,
|
|
5532
|
+
blockId: leading.at.blockId,
|
|
5533
|
+
nextBlockId: to.blockId
|
|
5534
|
+
});
|
|
5535
|
+
if (simulated.isErr()) return Result.err(simulated.error);
|
|
5536
|
+
direct = simulated.value.document;
|
|
5537
|
+
}
|
|
5538
|
+
const survivor = storyParagraphs(storyBody(direct, from.story)).find(({ paragraph }) => idKey(paragraph.paraId ?? "") === idKey(to.blockId));
|
|
5539
|
+
const trackedSurvivor = storyParagraphs(storyBody(plan.document(), from.story)).find(({ paragraph }) => idKey(paragraph.paraId ?? "") === idKey(to.blockId));
|
|
5540
|
+
if (survivor === void 0 || trackedSurvivor === void 0) return refuse(DOCUMENT_OP_REFUSAL_REASONS.BLOCK_NOT_FOUND, "The planned survivor does not exist.");
|
|
5541
|
+
const current = paragraphPropertiesOf(trackedSurvivor.paragraph.formatting);
|
|
5542
|
+
const desired = paragraphPropertiesOf(survivor.paragraph.formatting);
|
|
5543
|
+
if (structurallyEqual(current, desired)) return Result.ok(void 0);
|
|
5544
|
+
const patch = Object.fromEntries([...Object.keys(current ?? {}).map((key) => [key, null]), ...Object.entries(desired ?? {})]);
|
|
5545
|
+
return plan.append({
|
|
5546
|
+
type: DOCUMENT_OP_TYPES.SET_PARAGRAPH_PROPS,
|
|
5547
|
+
story: from.story,
|
|
5548
|
+
blockId: to.blockId,
|
|
5549
|
+
patch,
|
|
5550
|
+
whenEmpty: survivor.paragraph.formatting === void 0 ? EMPTY_PROPERTY_SETS.OMIT : EMPTY_PROPERTY_SETS.KEEP,
|
|
5551
|
+
revision
|
|
5552
|
+
});
|
|
5553
|
+
};
|
|
5554
|
+
/**
|
|
5555
|
+
* Plan a same-list range as inline deletions followed by tracked paragraph
|
|
5556
|
+
* joins. Own insertions are retracted directly; comment anchors remain.
|
|
5557
|
+
* Inline ranges run in reverse order, then joins run from last to first.
|
|
5558
|
+
* All physical revision ids come from one pool for the complete batch.
|
|
5559
|
+
*/
|
|
5560
|
+
const planTrackedDeletion = (document, options) => {
|
|
5561
|
+
const plan = createTrackedPlan({
|
|
5562
|
+
document,
|
|
5563
|
+
revision: options.revision,
|
|
5564
|
+
newIds: options.newIds
|
|
5565
|
+
});
|
|
5566
|
+
const appended = appendTrackedDeletion({
|
|
5567
|
+
document,
|
|
5568
|
+
options,
|
|
5569
|
+
plan
|
|
5570
|
+
});
|
|
5571
|
+
return appended.isErr() ? Result.err(appended.error) : Result.ok(plan.ops);
|
|
5572
|
+
};
|
|
5573
|
+
//#endregion
|
|
5574
|
+
//#region src/ops/rangeReplacement.ts
|
|
5575
|
+
/** Tracked replacement fragments share deletion's validation and physical-id pool. */
|
|
5576
|
+
/**
|
|
5577
|
+
* Plan a tracked replacement of a same-list range: the range's tracked
|
|
5578
|
+
* deletion followed by the replacement fragment as tracked insertions.
|
|
5579
|
+
* All physical revision ids come from one pool for the complete batch.
|
|
5580
|
+
*/
|
|
5581
|
+
const planTrackedReplace = (document, options) => {
|
|
5582
|
+
const { replacement, from, revision, newIds } = options;
|
|
5583
|
+
const refuse = (message) => Result.err(new DocumentOpRefusal({
|
|
5584
|
+
reason: DOCUMENT_OP_REFUSAL_REASONS.UNTRACKABLE,
|
|
5585
|
+
message,
|
|
5586
|
+
opType: DOCUMENT_OP_TYPES.INSERT_CONTENT
|
|
5587
|
+
}));
|
|
5588
|
+
if (replacement.tail.openStart !== 0 || replacement.tail.openEnd !== 0) return refuse("Tracked replacement requires a closed tail slice.");
|
|
5589
|
+
if (identityKeysIn(replacement).length > 0 || replacement.paragraphs.some(({ sectionProperties }) => sectionProperties !== void 0)) return refuse("Tracked replacement cannot insert identified review, content-control or section records.");
|
|
5590
|
+
const plan = createTrackedPlan({
|
|
5591
|
+
document,
|
|
5592
|
+
revision,
|
|
5593
|
+
newIds
|
|
2695
5594
|
});
|
|
5595
|
+
const deleted = appendTrackedDeletion({
|
|
5596
|
+
document,
|
|
5597
|
+
options,
|
|
5598
|
+
plan
|
|
5599
|
+
});
|
|
5600
|
+
if (deleted.isErr()) return Result.err(deleted.error);
|
|
5601
|
+
let at = from;
|
|
5602
|
+
for (const paragraph of replacement.paragraphs) {
|
|
5603
|
+
const source = storyParagraphs(storyBody(plan.document(), from.story)).find(({ paragraph: sourceParagraph }) => idKey(sourceParagraph.paraId ?? "") === idKey(from.blockId));
|
|
5604
|
+
if (source === void 0) return refuse("The replacement's source paragraph does not exist.");
|
|
5605
|
+
const end = gapAfterInserted(at.zeroWidthBefore === void 0 ? defaultInsertionGap(source.paragraph.content, at.offset) : {
|
|
5606
|
+
offset: at.offset,
|
|
5607
|
+
zeroWidthBefore: at.zeroWidthBefore
|
|
5608
|
+
}, paragraph.content);
|
|
5609
|
+
if (paragraph.content.length > 0) {
|
|
5610
|
+
const inserted = plan.append({
|
|
5611
|
+
type: DOCUMENT_OP_TYPES.INSERT_CONTENT,
|
|
5612
|
+
at,
|
|
5613
|
+
slice: {
|
|
5614
|
+
content: paragraph.content,
|
|
5615
|
+
openStart: 0,
|
|
5616
|
+
openEnd: 0
|
|
5617
|
+
},
|
|
5618
|
+
revision
|
|
5619
|
+
});
|
|
5620
|
+
if (inserted.isErr()) return Result.err(inserted.error);
|
|
5621
|
+
}
|
|
5622
|
+
const { type: _type, paraId, content: _content, sectionProperties: _section, pPrMark: _mark, ...newParagraph } = paragraph;
|
|
5623
|
+
const split = plan.append({
|
|
5624
|
+
type: DOCUMENT_OP_TYPES.SPLIT_BLOCK,
|
|
5625
|
+
at: {
|
|
5626
|
+
...at,
|
|
5627
|
+
...end
|
|
5628
|
+
},
|
|
5629
|
+
newBlockId: paraId ?? "",
|
|
5630
|
+
newHalf: SPLIT_HALVES.FIRST,
|
|
5631
|
+
newParagraph,
|
|
5632
|
+
revision
|
|
5633
|
+
});
|
|
5634
|
+
if (split.isErr()) return Result.err(split.error);
|
|
5635
|
+
at = {
|
|
5636
|
+
story: from.story,
|
|
5637
|
+
blockId: from.blockId,
|
|
5638
|
+
offset: 0,
|
|
5639
|
+
zeroWidthBefore: 0
|
|
5640
|
+
};
|
|
5641
|
+
}
|
|
5642
|
+
if (replacement.tail.content.length > 0) {
|
|
5643
|
+
const inserted = plan.append({
|
|
5644
|
+
type: DOCUMENT_OP_TYPES.INSERT_CONTENT,
|
|
5645
|
+
at,
|
|
5646
|
+
slice: replacement.tail,
|
|
5647
|
+
revision
|
|
5648
|
+
});
|
|
5649
|
+
if (inserted.isErr()) return Result.err(inserted.error);
|
|
5650
|
+
}
|
|
5651
|
+
return Result.ok(plan.ops);
|
|
2696
5652
|
};
|
|
2697
5653
|
//#endregion
|
|
2698
|
-
export { DOCUMENT_OP_REFUSAL_REASONS, DOCUMENT_OP_SCHEMA_VERSION, DOCUMENT_OP_TYPES, DocumentOpRefusal, DocumentOpsContractError, EMPTY_PROPERTY_SETS, INHERIT_RUN_PROPS, OBJECT_REPLACEMENT_CHARACTER, OP_STORIES, applyDocumentOp, applyDocumentOps, normalizeForOps, paragraphLength, paragraphLogicalText, toOpEnvelope, validateOpsDocument };
|
|
5654
|
+
export { DOCUMENT_OP_REFUSAL_REASONS, DOCUMENT_OP_SCHEMA_VERSION, DOCUMENT_OP_TYPES, DocumentOpRefusal, DocumentOpsContractError, EMPTY_PROPERTY_SETS, INHERIT_RUN_PROPS, OBJECT_REPLACEMENT_CHARACTER, OP_STORIES, PARAGRAPH_MARK_FORMATTING_KEYS, REVISION_DECISIONS, SPLIT_HALVES, applyDocumentOp, applyDocumentOps, normalizeForOps, paragraphLength, paragraphLogicalText, planTrackedDeletion, planTrackedReplace, revisionIdDemand, toOpEnvelope, validateOpsDocument };
|