@stll/docx-core 0.31.0 → 0.33.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/dist/{document-3WKQA1r3.js → document-CQ0oovh0.js} +12 -1
- package/dist/{document-w6pPLpob.d.ts → document-YlRuGzLX.d.ts} +16 -5
- package/dist/docx_kernel_bg.wasm +0 -0
- package/dist/{xmlEscape-T-2DeGlS.js → href-2XSCK-T8.js} +49 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.js +49 -61
- package/dist/model/document.d.ts +2 -2
- package/dist/model/document.js +2 -2
- package/dist/ops/documentOps.d.ts +333 -92
- package/dist/ops/documentOps.js +2896 -527
- package/dist/projection.d.ts +2 -2
- package/dist/zip/writeZipPart.d.ts +12 -0
- package/dist/zip/writeZipPart.js +12 -0
- package/package.json +5 -1
package/dist/ops/documentOps.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
1
|
+
import { D as PARAGRAPH_ALIGNMENTS, Q as isNumberingReference, Y as isNumberingLevel, nt as paragraphNumberingReference, o as INSERTION_SEAM_POLICIES, s as MAX_REVISION_ID, t as COMMENT_PART_RELATIONSHIPS } from "../document-CQ0oovh0.js";
|
|
2
|
+
import { a as hasIllegalXmlCharacters, t as sanitizeExternalUrl } from "../href-2XSCK-T8.js";
|
|
3
3
|
import { Result, TaggedError, panic } from "better-result";
|
|
4
4
|
//#region src/ops/equality.ts
|
|
5
5
|
const NOTHING = /* @__PURE__ */ new Set();
|
|
@@ -225,10 +225,10 @@ const captureDocumentOp = (op) => {
|
|
|
225
225
|
}
|
|
226
226
|
};
|
|
227
227
|
visit(op, []);
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
228
|
+
if (fields.length === 0) return op;
|
|
229
|
+
const captured = cloneModel(op);
|
|
230
|
+
captured.undefinedFields = cloneModel(fields);
|
|
231
|
+
return captured;
|
|
232
232
|
};
|
|
233
233
|
/** Restore validated presence metadata without mutating the supplied operation. */
|
|
234
234
|
const restoreDocumentOp = (op) => {
|
|
@@ -274,6 +274,7 @@ const restoreDocumentOp = (op) => {
|
|
|
274
274
|
/**
|
|
275
275
|
* The operation schema this module reads and writes.
|
|
276
276
|
*
|
|
277
|
+
* Version 9 adds exact, JSON-safe package resource replacement for clipboard imports.
|
|
277
278
|
* Version 8 adds semantic table edits and exact whole-table restoration.
|
|
278
279
|
* Version 7 adds lossless own-undefined operation snapshot presence and
|
|
279
280
|
* explicit revision-boundary provenance. An explicit undefined patch value now
|
|
@@ -295,7 +296,7 @@ const restoreDocumentOp = (op) => {
|
|
|
295
296
|
* and paragraph operations, and the review operations `setParagraphReview`,
|
|
296
297
|
* `replaceInline` and `resolveRevision`.
|
|
297
298
|
*/
|
|
298
|
-
const DOCUMENT_OP_SCHEMA_VERSION =
|
|
299
|
+
const DOCUMENT_OP_SCHEMA_VERSION = 10;
|
|
299
300
|
/**
|
|
300
301
|
* The main story has a fixed address; other editable parts use their stable
|
|
301
302
|
* relationship id or note id. Comment bodies are not yet editable stories.
|
|
@@ -320,8 +321,13 @@ const SECTION_BOUNDARY_POLICIES = Object.freeze({
|
|
|
320
321
|
REMOVE: "remove",
|
|
321
322
|
REPLACE: "replace"
|
|
322
323
|
});
|
|
323
|
-
/** The operation kinds of schema version
|
|
324
|
+
/** The operation kinds of schema version 10. */
|
|
324
325
|
const DOCUMENT_OP_TYPES = Object.freeze({
|
|
326
|
+
CREATE_COMMENT: "createComment",
|
|
327
|
+
UPDATE_COMMENT_CONTENT: "updateCommentContent",
|
|
328
|
+
SET_COMMENT_RESOLUTION: "setCommentResolution",
|
|
329
|
+
DELETE_COMMENT: "deleteComment",
|
|
330
|
+
RESTORE_COMMENT_STATE: "restoreCommentState",
|
|
325
331
|
CREATE_HEADER_FOOTER: "createHeaderFooter",
|
|
326
332
|
REMOVE_HEADER_FOOTER: "removeHeaderFooter",
|
|
327
333
|
ADD_NOTE: "addNote",
|
|
@@ -360,7 +366,8 @@ const DOCUMENT_OP_TYPES = Object.freeze({
|
|
|
360
366
|
SET_TABLE: "setTable",
|
|
361
367
|
CREATE_NUMBERING_INSTANCE: "createNumberingInstance",
|
|
362
368
|
DELETE_NUMBERING_INSTANCE: "deleteNumberingInstance",
|
|
363
|
-
SET_SECTION_ENDPOINT: "setSectionEndpoint"
|
|
369
|
+
SET_SECTION_ENDPOINT: "setSectionEndpoint",
|
|
370
|
+
SET_PACKAGE_RESOURCES: "setPackageResources"
|
|
364
371
|
});
|
|
365
372
|
/** Whether a tracked run patch appends an independently rejectable review. */
|
|
366
373
|
const PROPERTY_REVIEW_POLICIES = Object.freeze({ APPEND: "append" });
|
|
@@ -382,7 +389,7 @@ const REVISION_DECISIONS = Object.freeze({
|
|
|
382
389
|
});
|
|
383
390
|
/** An operation in the envelope it is journaled and sent in. */
|
|
384
391
|
const toOpEnvelope = (op) => ({
|
|
385
|
-
schema:
|
|
392
|
+
schema: 10,
|
|
386
393
|
op: captureDocumentOp(op)
|
|
387
394
|
});
|
|
388
395
|
//#endregion
|
|
@@ -977,6 +984,8 @@ const INLINE_KINDS = {
|
|
|
977
984
|
const isInlineContainer = (item) => INLINE_KINDS[item.type] === "container";
|
|
978
985
|
/** A zero-width boundary that opens a range: it belongs with what follows it. */
|
|
979
986
|
const isOpeningMarker = (item) => INLINE_KINDS[item.type] === "openingMarker";
|
|
987
|
+
/** A source range boundary is zero-width and remains outside replacement revisions. */
|
|
988
|
+
const isRangeBoundary = (item) => INLINE_KINDS[item.type] === "openingMarker" || INLINE_KINDS[item.type] === "closingMarker";
|
|
980
989
|
/** Tracked content that direct text must not join: it is on its way out. */
|
|
981
990
|
const isRemovedRevision = (item) => item.type === "deletion" || item.type === "moveFrom";
|
|
982
991
|
const runContentWidth = (content) => {
|
|
@@ -1127,20 +1136,40 @@ const isPlainRecord = (value) => {
|
|
|
1127
1136
|
const prototype = Object.getPrototypeOf(value);
|
|
1128
1137
|
return prototype === Object.prototype || prototype === null;
|
|
1129
1138
|
};
|
|
1130
|
-
const walk = (value, visit,
|
|
1139
|
+
const walk = ({ value, visit, context = {
|
|
1140
|
+
heldBy: void 0,
|
|
1141
|
+
ownerType: void 0
|
|
1142
|
+
} }) => {
|
|
1131
1143
|
if (typeof value !== "object" || value === null) return;
|
|
1132
1144
|
if (Array.isArray(value)) {
|
|
1133
|
-
for (const item of value) walk(
|
|
1145
|
+
for (const item of value) walk({
|
|
1146
|
+
value: item,
|
|
1147
|
+
visit,
|
|
1148
|
+
context
|
|
1149
|
+
});
|
|
1134
1150
|
return;
|
|
1135
1151
|
}
|
|
1136
1152
|
if (value instanceof Map) {
|
|
1137
|
-
for (const item of value.values()) walk(
|
|
1153
|
+
for (const item of value.values()) walk({
|
|
1154
|
+
value: item,
|
|
1155
|
+
visit,
|
|
1156
|
+
context
|
|
1157
|
+
});
|
|
1138
1158
|
return;
|
|
1139
1159
|
}
|
|
1140
1160
|
if (!isPlainRecord(value)) return;
|
|
1141
1161
|
const entries = Object.entries(value);
|
|
1142
|
-
visit(entries,
|
|
1143
|
-
|
|
1162
|
+
visit(entries, context);
|
|
1163
|
+
const type = fieldOf(entries, "type");
|
|
1164
|
+
const ownerType = typeof type === "string" ? type : void 0;
|
|
1165
|
+
for (const [heldBy, field] of entries) walk({
|
|
1166
|
+
value: field,
|
|
1167
|
+
visit,
|
|
1168
|
+
context: {
|
|
1169
|
+
heldBy,
|
|
1170
|
+
ownerType
|
|
1171
|
+
}
|
|
1172
|
+
});
|
|
1144
1173
|
};
|
|
1145
1174
|
/** The package's stories, each once: the body without its derived section view. */
|
|
1146
1175
|
const storiesOf = (pkg) => ({
|
|
@@ -1159,14 +1188,54 @@ const paragraphIdOf = (entries) => {
|
|
|
1159
1188
|
/** Every `paraId` of every paragraph in a value, nested ones included, in document order. */
|
|
1160
1189
|
const paragraphIdsIn = (value) => {
|
|
1161
1190
|
const out = [];
|
|
1162
|
-
walk(
|
|
1163
|
-
|
|
1164
|
-
|
|
1191
|
+
walk({
|
|
1192
|
+
value,
|
|
1193
|
+
visit: (entries) => {
|
|
1194
|
+
const id = paragraphIdOf(entries);
|
|
1195
|
+
if (id !== void 0) out.push(id);
|
|
1196
|
+
}
|
|
1165
1197
|
});
|
|
1166
1198
|
return out;
|
|
1167
1199
|
};
|
|
1168
1200
|
/** Every paragraph id of a package, each story walked once. */
|
|
1169
1201
|
const packageParagraphIds = (pkg) => paragraphIdsIn(storiesOf(pkg));
|
|
1202
|
+
/** Bookmark ids, names and uniquely paired starts from every package story. */
|
|
1203
|
+
const packageBookmarkCensus = (pkg) => {
|
|
1204
|
+
const ids = /* @__PURE__ */ new Set();
|
|
1205
|
+
const names = /* @__PURE__ */ new Set();
|
|
1206
|
+
const starts = /* @__PURE__ */ new Map();
|
|
1207
|
+
const ends = /* @__PURE__ */ new Map();
|
|
1208
|
+
walk({
|
|
1209
|
+
value: storiesOf(pkg),
|
|
1210
|
+
visit: (entries) => {
|
|
1211
|
+
const type = fieldOf(entries, "type");
|
|
1212
|
+
const id = fieldOf(entries, "id");
|
|
1213
|
+
if (typeof id !== "number") return;
|
|
1214
|
+
if (type === "bookmarkStart") {
|
|
1215
|
+
ids.add(id);
|
|
1216
|
+
const name = fieldOf(entries, "name");
|
|
1217
|
+
if (typeof name !== "string") return;
|
|
1218
|
+
names.add(name);
|
|
1219
|
+
const prior = starts.get(id);
|
|
1220
|
+
starts.set(id, {
|
|
1221
|
+
name,
|
|
1222
|
+
count: (prior?.count ?? 0) + 1
|
|
1223
|
+
});
|
|
1224
|
+
} else if (type === "bookmarkEnd") {
|
|
1225
|
+
ids.add(id);
|
|
1226
|
+
ends.set(id, (ends.get(id) ?? 0) + 1);
|
|
1227
|
+
}
|
|
1228
|
+
}
|
|
1229
|
+
});
|
|
1230
|
+
return {
|
|
1231
|
+
ids,
|
|
1232
|
+
names,
|
|
1233
|
+
pairs: [...starts].flatMap(([id, start]) => start.count === 1 && ends.get(id) === 1 ? [{
|
|
1234
|
+
id,
|
|
1235
|
+
name: start.name
|
|
1236
|
+
}] : [])
|
|
1237
|
+
};
|
|
1238
|
+
};
|
|
1170
1239
|
/**
|
|
1171
1240
|
* The key two spellings of one id share. `ST_LongHexNumber` is hex, so
|
|
1172
1241
|
* `0000abcd` and `0000ABCD` are the same id; every lookup and comparison of
|
|
@@ -1214,9 +1283,10 @@ const revisionIdOf = (entries, heldBy) => {
|
|
|
1214
1283
|
const id = fieldOf(infoEntries, "id");
|
|
1215
1284
|
return typeof id === "number" && typeof fieldOf(infoEntries, "author") === "string" ? id : void 0;
|
|
1216
1285
|
};
|
|
1217
|
-
const controlIdOf = (entries) => {
|
|
1286
|
+
const controlIdOf = (entries, { heldBy, ownerType }) => {
|
|
1218
1287
|
const id = fieldOf(entries, "id");
|
|
1219
|
-
|
|
1288
|
+
if (typeof id !== "number") return void 0;
|
|
1289
|
+
return heldBy === "properties" && (ownerType === "inlineSdt" || ownerType === "blockSdt") || heldBy === CONTENT_CONTROL_STACK || typeof fieldOf(entries, "sdtType") === "string" ? id : void 0;
|
|
1220
1290
|
};
|
|
1221
1291
|
/**
|
|
1222
1292
|
* Every revision and content-control id in a value, as slot keys: tracked
|
|
@@ -1226,25 +1296,31 @@ const controlIdOf = (entries) => {
|
|
|
1226
1296
|
const identityKeysIn = (value) => {
|
|
1227
1297
|
const out = [];
|
|
1228
1298
|
const stacked = /* @__PURE__ */ new Map();
|
|
1229
|
-
walk(
|
|
1230
|
-
|
|
1231
|
-
|
|
1232
|
-
|
|
1233
|
-
|
|
1234
|
-
|
|
1235
|
-
|
|
1236
|
-
|
|
1237
|
-
|
|
1238
|
-
|
|
1239
|
-
|
|
1240
|
-
|
|
1241
|
-
|
|
1242
|
-
|
|
1243
|
-
|
|
1244
|
-
|
|
1245
|
-
|
|
1246
|
-
|
|
1247
|
-
|
|
1299
|
+
walk({
|
|
1300
|
+
value,
|
|
1301
|
+
visit: (entries, { heldBy, ownerType }) => {
|
|
1302
|
+
const revision = revisionIdOf(entries, heldBy);
|
|
1303
|
+
if (revision !== void 0) out.push(slotKey({
|
|
1304
|
+
space: IDENTITY_SPACES.REVISION,
|
|
1305
|
+
id: revision
|
|
1306
|
+
}));
|
|
1307
|
+
const control = controlIdOf(entries, {
|
|
1308
|
+
heldBy,
|
|
1309
|
+
ownerType
|
|
1310
|
+
});
|
|
1311
|
+
if (control === void 0) return;
|
|
1312
|
+
if (heldBy === CONTENT_CONTROL_STACK) {
|
|
1313
|
+
const record = Object.fromEntries(entries);
|
|
1314
|
+
const seen = stacked.get(control) ?? [];
|
|
1315
|
+
if (seen.some((other) => structurallyEqual(other, record))) return;
|
|
1316
|
+
seen.push(record);
|
|
1317
|
+
stacked.set(control, seen);
|
|
1318
|
+
}
|
|
1319
|
+
out.push(slotKey({
|
|
1320
|
+
space: IDENTITY_SPACES.CONTROL,
|
|
1321
|
+
id: control
|
|
1322
|
+
}));
|
|
1323
|
+
}
|
|
1248
1324
|
});
|
|
1249
1325
|
return out;
|
|
1250
1326
|
};
|
|
@@ -1253,27 +1329,30 @@ const CONTENT_CONTROL_STACK = "contentControls";
|
|
|
1253
1329
|
/** Pending cut references reserve IDs without becoming physical model identities. */
|
|
1254
1330
|
const reservedIdentityKeysIn = (value) => {
|
|
1255
1331
|
const out = [];
|
|
1256
|
-
walk(
|
|
1257
|
-
|
|
1258
|
-
|
|
1259
|
-
if (
|
|
1260
|
-
|
|
1261
|
-
|
|
1262
|
-
id
|
|
1263
|
-
|
|
1332
|
+
walk({
|
|
1333
|
+
value,
|
|
1334
|
+
visit: (entries, { heldBy }) => {
|
|
1335
|
+
if (heldBy === "deferredRemove") {
|
|
1336
|
+
const blockers = fieldOf(entries, "blockers");
|
|
1337
|
+
if (Array.isArray(blockers)) {
|
|
1338
|
+
for (const id of blockers) if (typeof id === "number") out.push(slotKey({
|
|
1339
|
+
space: IDENTITY_SPACES.REVISION,
|
|
1340
|
+
id
|
|
1341
|
+
}));
|
|
1342
|
+
}
|
|
1343
|
+
return;
|
|
1264
1344
|
}
|
|
1265
|
-
return;
|
|
1266
|
-
|
|
1267
|
-
|
|
1268
|
-
|
|
1269
|
-
|
|
1270
|
-
|
|
1271
|
-
|
|
1272
|
-
|
|
1273
|
-
|
|
1274
|
-
|
|
1275
|
-
|
|
1276
|
-
}));
|
|
1345
|
+
if (heldBy !== "retainedAfter") return;
|
|
1346
|
+
for (const field of ["source", "target"]) {
|
|
1347
|
+
const slots = fieldOf(entries, field);
|
|
1348
|
+
if (!Array.isArray(slots)) continue;
|
|
1349
|
+
for (const slot of slots) {
|
|
1350
|
+
if (typeof slot !== "object" || slot === null || !("space" in slot) || !("id" in slot)) continue;
|
|
1351
|
+
if ((slot.space === IDENTITY_SPACES.REVISION || slot.space === IDENTITY_SPACES.CONTROL) && typeof slot.id === "number") out.push(slotKey({
|
|
1352
|
+
space: slot.space,
|
|
1353
|
+
id: slot.id
|
|
1354
|
+
}));
|
|
1355
|
+
}
|
|
1277
1356
|
}
|
|
1278
1357
|
}
|
|
1279
1358
|
});
|
|
@@ -1852,6 +1931,12 @@ const leafSpans = (items) => {
|
|
|
1852
1931
|
walk(items, []);
|
|
1853
1932
|
return out;
|
|
1854
1933
|
};
|
|
1934
|
+
/** Source leaf coordinates without the ancestry needed by edit planning. */
|
|
1935
|
+
const inlineLeafSpans = (items) => leafSpans(items).map(({ node, before, after }) => ({
|
|
1936
|
+
node,
|
|
1937
|
+
before,
|
|
1938
|
+
after
|
|
1939
|
+
}));
|
|
1855
1940
|
/** Gaps in document order: by offset, then by the zero-width leaves before them. */
|
|
1856
1941
|
const compareGaps = (left, right) => left.offset === right.offset ? left.zeroWidthBefore - right.zeroWidthBefore : left.offset - right.offset;
|
|
1857
1942
|
//#endregion
|
|
@@ -2097,7 +2182,7 @@ const priorValues = (base, patch) => {
|
|
|
2097
2182
|
//#endregion
|
|
2098
2183
|
//#region src/ops/storyLifecycle.ts
|
|
2099
2184
|
/** Explicit story lifecycle and section property edits, with exact JSON-safe inverses. */
|
|
2100
|
-
const refuse$
|
|
2185
|
+
const refuse$5 = (op, message) => Result.err(new DocumentOpRefusal({
|
|
2101
2186
|
opType: op.type,
|
|
2102
2187
|
reason: DOCUMENT_OP_REFUSAL_REASONS.STRUCTURE_MISMATCH,
|
|
2103
2188
|
message
|
|
@@ -2137,6 +2222,28 @@ const captureStoryParts = (document, owned) => {
|
|
|
2137
2222
|
if (owned.footnotes !== void 0) parts.footnotes = pkg.footnotes ?? null;
|
|
2138
2223
|
if (owned.endnotes !== void 0) parts.endnotes = pkg.endnotes ?? null;
|
|
2139
2224
|
if (owned.settings !== void 0) parts.settings = pkg.settings ?? null;
|
|
2225
|
+
const undefinedFields = [];
|
|
2226
|
+
const bodyKeys = Object.keys(owned.body ?? {}).filter((key) => key in values).filter((key) => Object.hasOwn(body, key) && Reflect.get(body, key) === void 0);
|
|
2227
|
+
if (bodyKeys.length > 0) undefinedFields.push({
|
|
2228
|
+
target: "body",
|
|
2229
|
+
keys: bodyKeys
|
|
2230
|
+
});
|
|
2231
|
+
const packageKeys = Object.keys(owned).filter((key) => key !== "body" && key !== "sections" && key !== "undefinedFields").filter((key) => Object.hasOwn(pkg, key) && Reflect.get(pkg, key) === void 0);
|
|
2232
|
+
if (packageKeys.length > 0) undefinedFields.push({
|
|
2233
|
+
target: "package",
|
|
2234
|
+
keys: packageKeys
|
|
2235
|
+
});
|
|
2236
|
+
for (const fields of owned.sections ?? []) {
|
|
2237
|
+
const section = body.sections?.at(fields.index);
|
|
2238
|
+
if (!section) continue;
|
|
2239
|
+
const keys = Object.keys(fields).filter((key) => key !== "index").filter((key) => Object.hasOwn(section, key) && Reflect.get(section, key) === void 0);
|
|
2240
|
+
if (keys.length > 0) undefinedFields.push({
|
|
2241
|
+
target: "section",
|
|
2242
|
+
index: fields.index,
|
|
2243
|
+
keys
|
|
2244
|
+
});
|
|
2245
|
+
}
|
|
2246
|
+
if (undefinedFields.length > 0) parts.undefinedFields = undefinedFields;
|
|
2140
2247
|
return parts;
|
|
2141
2248
|
};
|
|
2142
2249
|
/** Immutable operations share unowned fields; section content is a derived mirror. */
|
|
@@ -2166,7 +2273,7 @@ const changedParts = (before, after) => {
|
|
|
2166
2273
|
if (before.package.footnotes !== after.package.footnotes) parts.footnotes = before.package.footnotes ?? null;
|
|
2167
2274
|
if (before.package.endnotes !== after.package.endnotes) parts.endnotes = before.package.endnotes ?? null;
|
|
2168
2275
|
if (before.package.settings !== after.package.settings) parts.settings = before.package.settings ?? null;
|
|
2169
|
-
return parts;
|
|
2276
|
+
return captureStoryParts(before, parts);
|
|
2170
2277
|
};
|
|
2171
2278
|
const restoreParts = (document, parts) => {
|
|
2172
2279
|
const pkg = { ...document.package };
|
|
@@ -2208,12 +2315,14 @@ const restoreParts = (document, parts) => {
|
|
|
2208
2315
|
if (fields.headers !== void 0) if (fields.headers === null) delete section.headers;
|
|
2209
2316
|
else section.headers = new Map(fields.headers.map(([variant, part]) => {
|
|
2210
2317
|
const rId = section.properties.headerReferences?.find(({ type }) => type === variant)?.rId;
|
|
2211
|
-
|
|
2318
|
+
const owned = rId === void 0 ? void 0 : pkg.headers?.get(rId);
|
|
2319
|
+
return [variant, owned && structurallyEqual(owned, part) ? owned : part];
|
|
2212
2320
|
}));
|
|
2213
2321
|
if (fields.footers !== void 0) if (fields.footers === null) delete section.footers;
|
|
2214
2322
|
else section.footers = new Map(fields.footers.map(([variant, part]) => {
|
|
2215
2323
|
const rId = section.properties.footerReferences?.find(({ type }) => type === variant)?.rId;
|
|
2216
|
-
|
|
2324
|
+
const owned = rId === void 0 ? void 0 : pkg.footers?.get(rId);
|
|
2325
|
+
return [variant, owned && structurallyEqual(owned, part) ? owned : part];
|
|
2217
2326
|
}));
|
|
2218
2327
|
sections[fields.index] = section;
|
|
2219
2328
|
}
|
|
@@ -2241,6 +2350,30 @@ const restoreParts = (document, parts) => {
|
|
|
2241
2350
|
}
|
|
2242
2351
|
if (parts.settings !== void 0) if (parts.settings === null) delete pkg.settings;
|
|
2243
2352
|
else pkg.settings = parts.settings;
|
|
2353
|
+
for (const fields of parts.undefinedFields ?? []) switch (fields.target) {
|
|
2354
|
+
case "body":
|
|
2355
|
+
for (const key of fields.keys) {
|
|
2356
|
+
if (!parts.body || !Object.hasOwn(parts.body, key) || Reflect.get(parts.body, key) !== null) return panic("Undefined body metadata names an unowned field.");
|
|
2357
|
+
Reflect.set(pkg.document, key, void 0);
|
|
2358
|
+
}
|
|
2359
|
+
break;
|
|
2360
|
+
case "package":
|
|
2361
|
+
for (const key of fields.keys) {
|
|
2362
|
+
if (!Object.hasOwn(parts, key) || Reflect.get(parts, key) !== null) return panic("Undefined package metadata names an unowned field.");
|
|
2363
|
+
Reflect.set(pkg, key, void 0);
|
|
2364
|
+
}
|
|
2365
|
+
break;
|
|
2366
|
+
case "section": {
|
|
2367
|
+
const owned = parts.sections?.find(({ index }) => index === fields.index);
|
|
2368
|
+
const section = pkg.document.sections?.at(fields.index);
|
|
2369
|
+
if (!owned || !section) return panic("Undefined section metadata names an unowned section.");
|
|
2370
|
+
for (const key of fields.keys) {
|
|
2371
|
+
if (!Object.hasOwn(owned, key) || Reflect.get(owned, key) !== null) return panic("Undefined section metadata names an unowned field.");
|
|
2372
|
+
Reflect.set(section, key, void 0);
|
|
2373
|
+
}
|
|
2374
|
+
break;
|
|
2375
|
+
}
|
|
2376
|
+
}
|
|
2244
2377
|
return {
|
|
2245
2378
|
...document,
|
|
2246
2379
|
package: pkg
|
|
@@ -2349,8 +2482,25 @@ const withSectionProperties = (document, sectionIndex, properties) => {
|
|
|
2349
2482
|
}
|
|
2350
2483
|
};
|
|
2351
2484
|
};
|
|
2485
|
+
const undefinedFieldsIssue = (parts) => {
|
|
2486
|
+
for (const fields of parts.undefinedFields ?? []) switch (fields.target) {
|
|
2487
|
+
case "body":
|
|
2488
|
+
if (fields.keys.some((key) => !parts.body || !Object.hasOwn(parts.body, key) || Reflect.get(parts.body, key) !== null)) return "Undefined body metadata must name owned null fields.";
|
|
2489
|
+
break;
|
|
2490
|
+
case "package":
|
|
2491
|
+
if (fields.keys.some((key) => !Object.hasOwn(parts, key) || Reflect.get(parts, key) !== null)) return "Undefined package metadata must name owned null fields.";
|
|
2492
|
+
break;
|
|
2493
|
+
case "section": {
|
|
2494
|
+
const owned = parts.sections?.find(({ index }) => index === fields.index);
|
|
2495
|
+
if (!owned || fields.keys.some((key) => !Object.hasOwn(owned, key) || Reflect.get(owned, key) !== null)) return "Undefined section metadata must name owned null fields.";
|
|
2496
|
+
break;
|
|
2497
|
+
}
|
|
2498
|
+
}
|
|
2499
|
+
};
|
|
2352
2500
|
const applyStoryLifecycle = (document, op) => {
|
|
2353
2501
|
if (op.type === DOCUMENT_OP_TYPES.RESTORE_STORY_PARTS) {
|
|
2502
|
+
const metadataIssue = undefinedFieldsIssue(op.parts);
|
|
2503
|
+
if (metadataIssue) return refuse$5(op, metadataIssue);
|
|
2354
2504
|
if (!structurallyEqual(captureStoryParts(document, op.expected), op.expected)) return Result.err(new DocumentOpRefusal({
|
|
2355
2505
|
opType: op.type,
|
|
2356
2506
|
reason: DOCUMENT_OP_REFUSAL_REASONS.STALE,
|
|
@@ -2359,7 +2509,7 @@ const applyStoryLifecycle = (document, op) => {
|
|
|
2359
2509
|
return storyLifecycleEdit(document, restoreParts(document, cloneModel(op.parts)), op);
|
|
2360
2510
|
}
|
|
2361
2511
|
const properties = sectionPropertiesAt(document, op.sectionIndex);
|
|
2362
|
-
if (!properties) return refuse$
|
|
2512
|
+
if (!properties) return refuse$5(op, "The section does not exist.");
|
|
2363
2513
|
if (op.type === DOCUMENT_OP_TYPES.SET_SECTION_PROPS) {
|
|
2364
2514
|
const nextProps = applyFormattingPatch(properties, op.patch) ?? {};
|
|
2365
2515
|
let next = withSectionProperties(document, op.sectionIndex, nextProps);
|
|
@@ -2384,8 +2534,8 @@ const applyStoryLifecycle = (document, op) => {
|
|
|
2384
2534
|
let nextRefs = [...refs ?? []];
|
|
2385
2535
|
const nextProps = { ...properties };
|
|
2386
2536
|
if (op.type === DOCUMENT_OP_TYPES.CREATE_HEADER_FOOTER) {
|
|
2387
|
-
if (!op.story.rId || document.package.relationships?.has(op.story.rId) || document.package.headers?.has(op.story.rId) || document.package.footers?.has(op.story.rId) || refs?.some(({ type }) => type === op.referenceType)) return refuse$
|
|
2388
|
-
if (op.content.length === 0 || storyParagraphs({ content: op.content }).length === 0) return refuse$
|
|
2537
|
+
if (!op.story.rId || document.package.relationships?.has(op.story.rId) || document.package.headers?.has(op.story.rId) || document.package.footers?.has(op.story.rId) || refs?.some(({ type }) => type === op.referenceType)) return refuse$5(op, "The header/footer identity or section variant is already used.");
|
|
2538
|
+
if (op.content.length === 0 || storyParagraphs({ content: op.content }).length === 0) return refuse$5(op, "A header/footer needs an addressable paragraph.");
|
|
2389
2539
|
const part = {
|
|
2390
2540
|
type: kind,
|
|
2391
2541
|
hdrFtrType: op.referenceType,
|
|
@@ -2399,7 +2549,7 @@ const applyStoryLifecycle = (document, op) => {
|
|
|
2399
2549
|
if (op.referenceType === "first") nextProps.titlePg = true;
|
|
2400
2550
|
if (op.referenceType === "even") nextProps.evenAndOddHeaders = true;
|
|
2401
2551
|
} else {
|
|
2402
|
-
if (!parts?.has(op.story.rId) || !refs?.some(({ type, rId }) => type === op.referenceType && rId === op.story.rId)) return refuse$
|
|
2552
|
+
if (!parts?.has(op.story.rId) || !refs?.some(({ type, rId }) => type === op.referenceType && rId === op.story.rId)) return refuse$5(op, "The section does not own this header/footer variant.");
|
|
2403
2553
|
nextRefs = nextRefs.filter(({ type }) => type !== op.referenceType);
|
|
2404
2554
|
if (![...sectionEnds(document.package.document).map(({ properties: sectionProps }) => sectionProps), document.package.document.finalSectionProperties ?? {}].some((sectionProps, index) => index !== op.sectionIndex && (kind === "header" ? sectionProps.headerReferences : sectionProps.footerReferences)?.some(({ rId }) => rId === op.story.rId)) && !nextRefs.some(({ rId }) => rId === op.story.rId)) nextParts.delete(op.story.rId);
|
|
2405
2555
|
}
|
|
@@ -2458,154 +2608,770 @@ const applyStoryLifecycle = (document, op) => {
|
|
|
2458
2608
|
return storyLifecycleEdit(document, next, op);
|
|
2459
2609
|
};
|
|
2460
2610
|
//#endregion
|
|
2461
|
-
//#region src/ops/
|
|
2462
|
-
|
|
2463
|
-
|
|
2464
|
-
|
|
2465
|
-
|
|
2466
|
-
|
|
2467
|
-
|
|
2468
|
-
const
|
|
2469
|
-
|
|
2470
|
-
|
|
2471
|
-
|
|
2472
|
-
|
|
2473
|
-
|
|
2474
|
-
|
|
2475
|
-
modified: "inserted",
|
|
2476
|
-
inserted: "inserted",
|
|
2477
|
-
removed: void 0
|
|
2478
|
-
},
|
|
2479
|
-
removed: {
|
|
2480
|
-
modified: "modified",
|
|
2481
|
-
inserted: "modified",
|
|
2482
|
-
removed: "removed"
|
|
2483
|
-
}
|
|
2484
|
-
};
|
|
2485
|
-
/** Edits made in turn, starting from `document`, as one edit. */
|
|
2486
|
-
const combineEdits = (document, edits) => {
|
|
2487
|
-
const touched = /* @__PURE__ */ new Map();
|
|
2488
|
-
for (const edit of edits) for (const state of TOUCHED_STATES) for (const id of edit.touched[state]) {
|
|
2489
|
-
const previous = touched.get(id);
|
|
2490
|
-
const next = previous === void 0 ? state : TOUCH_TRANSITIONS[previous][state];
|
|
2491
|
-
if (next === void 0) touched.delete(id);
|
|
2492
|
-
else touched.set(id, next);
|
|
2611
|
+
//#region src/ops/comments.ts
|
|
2612
|
+
/** Semantic comment definitions and anchors, with narrow exact-state inverses. */
|
|
2613
|
+
const fail = (op, message, reason = DOCUMENT_OP_REFUSAL_REASONS.STRUCTURE_MISMATCH) => Result.err(new DocumentOpRefusal({
|
|
2614
|
+
opType: op.type,
|
|
2615
|
+
reason,
|
|
2616
|
+
message
|
|
2617
|
+
}));
|
|
2618
|
+
const validId = (id) => Number.isInteger(id) && id >= 0 && id <= 2147483647;
|
|
2619
|
+
const anchorId = (node) => {
|
|
2620
|
+
switch (node.type) {
|
|
2621
|
+
case "commentRangeStart":
|
|
2622
|
+
case "commentRangeEnd":
|
|
2623
|
+
case "commentReference": return node.id;
|
|
2624
|
+
default: return;
|
|
2493
2625
|
}
|
|
2494
|
-
const ids = (state) => [...touched].flatMap(([id, value]) => value === state ? [id] : []);
|
|
2495
|
-
return {
|
|
2496
|
-
document: edits.at(-1)?.document ?? document,
|
|
2497
|
-
inverse: edits.toReversed().flatMap(({ inverse }) => inverse),
|
|
2498
|
-
touched: {
|
|
2499
|
-
modified: ids("modified"),
|
|
2500
|
-
inserted: ids("inserted"),
|
|
2501
|
-
removed: ids("removed")
|
|
2502
|
-
}
|
|
2503
|
-
};
|
|
2504
2626
|
};
|
|
2505
|
-
|
|
2506
|
-
|
|
2507
|
-
|
|
2508
|
-
|
|
2509
|
-
|
|
2510
|
-
|
|
2511
|
-
|
|
2512
|
-
|
|
2513
|
-
};
|
|
2514
|
-
if (stamp.initials !== void 0) info.initials = stamp.initials;
|
|
2515
|
-
return info;
|
|
2627
|
+
const anchorsIn = (document) => {
|
|
2628
|
+
const anchors = [];
|
|
2629
|
+
for (const story of documentStories(document)) for (const { paragraph } of storyParagraphs(storyBody(document, story))) for (const span of leafSpans(paragraph.content)) if (isCommentAnchor(span.node)) anchors.push({
|
|
2630
|
+
story,
|
|
2631
|
+
blockId: paragraph.paraId ?? "",
|
|
2632
|
+
...span
|
|
2633
|
+
});
|
|
2634
|
+
return anchors;
|
|
2516
2635
|
};
|
|
2517
|
-
/**
|
|
2518
|
-
|
|
2519
|
-
|
|
2520
|
-
|
|
2521
|
-
|
|
2522
|
-
|
|
2523
|
-
|
|
2524
|
-
});
|
|
2525
|
-
const
|
|
2526
|
-
|
|
2527
|
-
const isAddedRevision = (node) => node.type === "insertion" || node.type === "moveTo";
|
|
2528
|
-
/** Tracked content on its way out: a deletion or the source of a move. */
|
|
2529
|
-
const isRemovedRevisionNode = (node) => node.type === "deletion" || node.type === "moveFrom";
|
|
2530
|
-
/** The tracked-change metadata of every record a paragraph and its content carry. */
|
|
2531
|
-
const revisionInfosOf = (paragraph) => {
|
|
2532
|
-
const out = [];
|
|
2533
|
-
for (const change of paragraph.propertyChanges ?? []) out.push(change.info);
|
|
2534
|
-
if (paragraph.pPrMark !== void 0) out.push(paragraph.pPrMark.info);
|
|
2535
|
-
const visit = (nodes) => {
|
|
2536
|
-
for (const node of nodes) {
|
|
2537
|
-
if (isTrackedWrapper(node)) out.push(node.info);
|
|
2538
|
-
else if (node.type === "run") for (const change of node.propertyChanges ?? []) out.push(change.info);
|
|
2539
|
-
visit(childNodes(node) ?? []);
|
|
2540
|
-
}
|
|
2541
|
-
};
|
|
2542
|
-
visit(paragraph.content);
|
|
2543
|
-
return out;
|
|
2636
|
+
/**
|
|
2637
|
+
* Root comments without any owned source anchor. Loaded packages may carry
|
|
2638
|
+
* them; they stay preserved as read-only entries rather than refusing the
|
|
2639
|
+
* document, and no comment operation may create or target one.
|
|
2640
|
+
*/
|
|
2641
|
+
const orphanRootCommentIds = (document) => {
|
|
2642
|
+
const comments = document.package.document.comments ?? [];
|
|
2643
|
+
const ids = new Set(comments.map(({ id }) => id));
|
|
2644
|
+
const anchored = new Set(anchorsIn(document).map(({ node }) => anchorId(node)));
|
|
2645
|
+
return new Set(comments.filter(({ id, parentId }) => (parentId === void 0 || !ids.has(parentId)) && !anchored.has(id)).map(({ id }) => id));
|
|
2544
2646
|
};
|
|
2545
2647
|
/**
|
|
2546
|
-
*
|
|
2547
|
-
*
|
|
2648
|
+
* Shared loaded-comment activation contract; legacy parentId revision associations stay explicit.
|
|
2649
|
+
* `allowedOrphans` names unanchored root comments the caller accepts ("all" for a loaded package).
|
|
2548
2650
|
*/
|
|
2549
|
-
const
|
|
2550
|
-
const
|
|
2551
|
-
|
|
2552
|
-
|
|
2553
|
-
|
|
2554
|
-
|
|
2651
|
+
const commentDocumentIssue = (document, allowedOrphans = /* @__PURE__ */ new Set()) => {
|
|
2652
|
+
const comments = document.package.document.comments ?? [];
|
|
2653
|
+
const byId = new Map(comments.map((comment) => [comment.id, comment]));
|
|
2654
|
+
if (comments.some(({ id }) => !validId(id)) || byId.size !== comments.length) return "Comment identities must be unique bounded integers.";
|
|
2655
|
+
const anchors = anchorsIn(document);
|
|
2656
|
+
if (anchors.some(({ node }) => !byId.has(anchorId(node) ?? -1))) return "A comment anchor has no definition.";
|
|
2657
|
+
for (const comment of comments) {
|
|
2658
|
+
const own = anchors.filter(({ node }) => anchorId(node) === comment.id);
|
|
2659
|
+
const starts = own.filter(({ node }) => node.type === "commentRangeStart");
|
|
2660
|
+
const ends = own.filter(({ node }) => node.type === "commentRangeEnd");
|
|
2661
|
+
const references = own.filter(({ node }) => node.type === "commentReference");
|
|
2662
|
+
const start = starts.at(0);
|
|
2663
|
+
const end = ends.at(0);
|
|
2664
|
+
if (starts.length !== ends.length || starts.length > 1 || references.length > 1) return "Comment anchors are unbalanced or duplicated.";
|
|
2665
|
+
if (start && end && (!sameStory(start.story, end.story) || anchors.indexOf(start) > anchors.indexOf(end))) return "Comment range boundaries must be ordered in one story.";
|
|
2666
|
+
const parent = comment.parentId === void 0 ? void 0 : byId.get(comment.parentId);
|
|
2667
|
+
if (!parent && own.length === 0 && allowedOrphans !== "all" && !allowedOrphans.has(comment.id)) return "A root or revision-associated comment needs an owned source anchor.";
|
|
2668
|
+
if (comment.parentId !== void 0 && !validId(comment.parentId)) return "A comment parent identity is invalid.";
|
|
2669
|
+
const visited = /* @__PURE__ */ new Set([comment.id]);
|
|
2670
|
+
let current = parent;
|
|
2671
|
+
while (current) {
|
|
2672
|
+
if (visited.has(current.id)) return "Comment parent relations cannot cycle.";
|
|
2673
|
+
visited.add(current.id);
|
|
2674
|
+
current = current.parentId === void 0 ? void 0 : byId.get(current.parentId);
|
|
2675
|
+
}
|
|
2676
|
+
}
|
|
2677
|
+
};
|
|
2678
|
+
/** Allocate from the package's annotation domains without a hidden mutable counter. */
|
|
2679
|
+
const freshCommentId = (document) => {
|
|
2680
|
+
let maximum = -1;
|
|
2681
|
+
for (const comment of document.package.document.comments ?? []) maximum = Math.max(maximum, comment.id, comment.parentId ?? -1);
|
|
2682
|
+
for (const { node } of anchorsIn(document)) maximum = Math.max(maximum, anchorId(node) ?? -1);
|
|
2683
|
+
for (const key of packageIdentityKeys(document.package)) {
|
|
2684
|
+
if (!Object.values(IDENTITY_SPACES).some((space) => key.startsWith(`${space}:`))) continue;
|
|
2685
|
+
maximum = Math.max(maximum, Number(key.slice(key.lastIndexOf(":") + 1)));
|
|
2686
|
+
}
|
|
2687
|
+
if (!Number.isInteger(maximum) || maximum >= 2147483647) return Result.err(new DocumentOpRefusal({
|
|
2688
|
+
opType: DOCUMENT_OP_TYPES.CREATE_COMMENT,
|
|
2689
|
+
reason: DOCUMENT_OP_REFUSAL_REASONS.INVALID_NEW_ID,
|
|
2690
|
+
message: "The package annotation id space is exhausted."
|
|
2691
|
+
}));
|
|
2692
|
+
return Result.ok(maximum + 1);
|
|
2693
|
+
};
|
|
2694
|
+
const propertyPresence = (record, key) => {
|
|
2695
|
+
if (!Object.hasOwn(record, key)) return "absent";
|
|
2696
|
+
return Reflect.get(record, key) === void 0 ? "undefined" : "present";
|
|
2697
|
+
};
|
|
2698
|
+
const capture = (document, ids, addressed) => {
|
|
2699
|
+
const body = document.package.document;
|
|
2700
|
+
const relationships = [];
|
|
2701
|
+
let relationshipIndex = 0;
|
|
2702
|
+
for (const [key, relationship] of document.package.relationships ?? []) {
|
|
2703
|
+
if (isCommentRelationship(relationship.type)) relationships.push({
|
|
2704
|
+
index: relationshipIndex,
|
|
2705
|
+
key,
|
|
2706
|
+
relationship
|
|
2707
|
+
});
|
|
2708
|
+
relationshipIndex += 1;
|
|
2709
|
+
}
|
|
2710
|
+
const records = [];
|
|
2711
|
+
for (const [index, comment] of (body.comments ?? []).entries()) if (ids.has(comment.id)) records.push({
|
|
2712
|
+
index,
|
|
2713
|
+
comment
|
|
2714
|
+
});
|
|
2715
|
+
const anchors = [];
|
|
2716
|
+
for (const { story, blockId } of addressed) {
|
|
2717
|
+
const found = findStoryBody(document, story);
|
|
2718
|
+
const paragraph = found && storyParagraphs(found).find(({ paragraph: value }) => idKey(value.paraId ?? "") === idKey(blockId))?.paragraph;
|
|
2719
|
+
if (paragraph) anchors.push({
|
|
2720
|
+
story,
|
|
2721
|
+
blockId,
|
|
2722
|
+
content: paragraph.content
|
|
2555
2723
|
});
|
|
2556
|
-
if (carriesStamp(info, stamp) && !known.has(key) && !out.includes(info.id)) out.push(info.id);
|
|
2557
2724
|
}
|
|
2558
|
-
return
|
|
2725
|
+
return {
|
|
2726
|
+
relationshipPresence: propertyPresence(document.package, "relationships"),
|
|
2727
|
+
relationships,
|
|
2728
|
+
listPresence: propertyPresence(body, "comments"),
|
|
2729
|
+
records,
|
|
2730
|
+
anchors
|
|
2731
|
+
};
|
|
2732
|
+
};
|
|
2733
|
+
const changedAnchors = (before, after) => {
|
|
2734
|
+
const anchors = [];
|
|
2735
|
+
for (const story of documentStories(before)) {
|
|
2736
|
+
const next = new Map(storyParagraphs(storyBody(after, story)).map(({ paragraph }) => [idKey(paragraph.paraId ?? ""), paragraph]));
|
|
2737
|
+
for (const { paragraph } of storyParagraphs(storyBody(before, story))) if (paragraph.content !== next.get(idKey(paragraph.paraId ?? ""))?.content) anchors.push({
|
|
2738
|
+
story,
|
|
2739
|
+
blockId: paragraph.paraId ?? "",
|
|
2740
|
+
content: paragraph.content
|
|
2741
|
+
});
|
|
2742
|
+
}
|
|
2743
|
+
return anchors;
|
|
2559
2744
|
};
|
|
2560
|
-
const
|
|
2561
|
-
|
|
2562
|
-
const
|
|
2563
|
-
const
|
|
2564
|
-
|
|
2565
|
-
return
|
|
2745
|
+
const scaffoldDelta = (before, after) => {
|
|
2746
|
+
const old = new Set(identityKeysIn(before.anchors));
|
|
2747
|
+
const next = new Set(identityKeysIn(after.anchors));
|
|
2748
|
+
const changed = [...old].filter((id) => !next.has(id)).concat([...next].filter((id) => !old.has(id)));
|
|
2749
|
+
const values = (space) => changed.filter((key) => key.startsWith(`${space}:`)).map((key) => Number(key.slice(key.indexOf(":") + 1)));
|
|
2750
|
+
return {
|
|
2751
|
+
revision: values(IDENTITY_SPACES.REVISION),
|
|
2752
|
+
control: values(IDENTITY_SPACES.CONTROL)
|
|
2753
|
+
};
|
|
2566
2754
|
};
|
|
2567
|
-
/**
|
|
2568
|
-
const
|
|
2569
|
-
|
|
2570
|
-
|
|
2571
|
-
|
|
2572
|
-
|
|
2755
|
+
/** Remove only owned anchors, then merge scaffolding with the shared exact field predicate. */
|
|
2756
|
+
const ownedSeams = (content, ids) => {
|
|
2757
|
+
let references = 0;
|
|
2758
|
+
return leafSpans(content).flatMap(({ node, before }) => {
|
|
2759
|
+
const id = anchorId(node);
|
|
2760
|
+
if (id === void 0 || !ids.has(id)) return [];
|
|
2761
|
+
const position = before.offset - references;
|
|
2762
|
+
if (node.type === "commentReference") references++;
|
|
2763
|
+
return [position];
|
|
2764
|
+
});
|
|
2573
2765
|
};
|
|
2574
|
-
|
|
2575
|
-
|
|
2576
|
-
|
|
2577
|
-
const
|
|
2578
|
-
|
|
2579
|
-
|
|
2580
|
-
|
|
2581
|
-
|
|
2582
|
-
|
|
2583
|
-
|
|
2584
|
-
|
|
2585
|
-
|
|
2766
|
+
const normalizedWithoutAnchors = ({ content, ids, seams, start = 0 }) => {
|
|
2767
|
+
let normalized = [];
|
|
2768
|
+
let position = start;
|
|
2769
|
+
for (const node of withoutAnchors(content, ids)) {
|
|
2770
|
+
const children = childNodes(node);
|
|
2771
|
+
const normalizedNode = children === void 0 ? node : rebuildNode(node, normalizedWithoutAnchors({
|
|
2772
|
+
content: children,
|
|
2773
|
+
ids,
|
|
2774
|
+
seams,
|
|
2775
|
+
start: position
|
|
2776
|
+
}));
|
|
2777
|
+
if (seams.has(position)) normalized = mergeAlike(normalized, [normalizedNode]);
|
|
2778
|
+
else normalized.push(normalizedNode);
|
|
2779
|
+
position += leafSpans([normalizedNode]).at(-1)?.after.offset ?? 0;
|
|
2780
|
+
}
|
|
2781
|
+
return normalized;
|
|
2782
|
+
};
|
|
2783
|
+
const isCommentRelationship = (type) => Object.values(COMMENT_PART_RELATIONSHIPS).some((part) => part.type === type);
|
|
2784
|
+
/** Keep the same part ids as the packager: numeric rId maximum plus one. */
|
|
2785
|
+
const reconcileRelationships = (document) => {
|
|
2786
|
+
const comments = document.package.document.comments ?? [];
|
|
2787
|
+
const relationships = new Map(document.package.relationships ?? []);
|
|
2788
|
+
let next = Math.max(0, ...[...relationships.keys()].map((id) => Number(/^rId(\d+)$/u.exec(id)?.at(1) ?? 0))) + 1;
|
|
2789
|
+
const extended = comments.some((comment) => comment.parentId !== void 0 || comment.done !== void 0);
|
|
2790
|
+
for (const [kind, part] of Object.entries(COMMENT_PART_RELATIONSHIPS)) {
|
|
2791
|
+
const needed = kind === "comments" ? comments.length > 0 : extended;
|
|
2792
|
+
const matching = [...relationships].filter(([, relationship]) => relationship.type === part.type);
|
|
2793
|
+
if (needed && matching.length === 0) {
|
|
2794
|
+
const id = `rId${next++}`;
|
|
2795
|
+
relationships.set(id, {
|
|
2796
|
+
id,
|
|
2797
|
+
type: part.type,
|
|
2798
|
+
target: part.target
|
|
2799
|
+
});
|
|
2800
|
+
}
|
|
2801
|
+
if (kind === "commentsExtended" && !needed) for (const [id] of matching) relationships.delete(id);
|
|
2802
|
+
}
|
|
2803
|
+
if (structurallyEqual([...relationships], [...document.package.relationships ?? []])) return document;
|
|
2586
2804
|
return {
|
|
2587
|
-
...
|
|
2588
|
-
|
|
2805
|
+
...document,
|
|
2806
|
+
package: {
|
|
2807
|
+
...document.package,
|
|
2808
|
+
relationships
|
|
2809
|
+
}
|
|
2589
2810
|
};
|
|
2590
2811
|
};
|
|
2591
|
-
|
|
2592
|
-
|
|
2593
|
-
const
|
|
2594
|
-
|
|
2595
|
-
|
|
2596
|
-
|
|
2597
|
-
const
|
|
2598
|
-
|
|
2599
|
-
|
|
2812
|
+
const edited = (before, after, op, ids) => {
|
|
2813
|
+
if (op.type !== DOCUMENT_OP_TYPES.RESTORE_COMMENT_STATE) after = reconcileRelationships(after);
|
|
2814
|
+
const invalid = contractViolation(after);
|
|
2815
|
+
if (invalid) return fail(op, invalid.message, invalid.reason);
|
|
2816
|
+
const issue = commentDocumentIssue(after, orphanRootCommentIds(before));
|
|
2817
|
+
if (issue) return fail(op, issue);
|
|
2818
|
+
const anchors = changedAnchors(before, after);
|
|
2819
|
+
const prior = capture(before, ids, anchors);
|
|
2820
|
+
const next = capture(after, ids, anchors);
|
|
2821
|
+
if (structurallyEqual(prior, next)) return Result.ok({
|
|
2822
|
+
document: before,
|
|
2823
|
+
inverse: [],
|
|
2824
|
+
touched: {
|
|
2825
|
+
modified: [],
|
|
2826
|
+
inserted: [],
|
|
2827
|
+
removed: []
|
|
2828
|
+
}
|
|
2829
|
+
});
|
|
2830
|
+
return Result.ok({
|
|
2831
|
+
document: after,
|
|
2832
|
+
inverse: [{
|
|
2833
|
+
type: DOCUMENT_OP_TYPES.RESTORE_COMMENT_STATE,
|
|
2834
|
+
ids: [...ids],
|
|
2835
|
+
scaffoldIds: scaffoldDelta(prior, next),
|
|
2836
|
+
expected: cloneModel(next),
|
|
2837
|
+
state: cloneModel(prior)
|
|
2838
|
+
}],
|
|
2839
|
+
touched: {
|
|
2840
|
+
modified: anchors.map(({ blockId }) => blockId),
|
|
2841
|
+
inserted: [],
|
|
2842
|
+
removed: []
|
|
2843
|
+
}
|
|
2844
|
+
});
|
|
2600
2845
|
};
|
|
2601
|
-
|
|
2602
|
-
const
|
|
2603
|
-
|
|
2604
|
-
|
|
2605
|
-
|
|
2606
|
-
|
|
2607
|
-
|
|
2608
|
-
|
|
2846
|
+
const withComments = (document, comments, presence = "present") => {
|
|
2847
|
+
const body = { ...document.package.document };
|
|
2848
|
+
switch (presence) {
|
|
2849
|
+
case "present":
|
|
2850
|
+
body.comments = comments;
|
|
2851
|
+
break;
|
|
2852
|
+
case "undefined":
|
|
2853
|
+
Reflect.set(body, "comments", void 0);
|
|
2854
|
+
break;
|
|
2855
|
+
case "absent":
|
|
2856
|
+
delete body.comments;
|
|
2857
|
+
break;
|
|
2858
|
+
default: return presence;
|
|
2859
|
+
}
|
|
2860
|
+
return {
|
|
2861
|
+
...document,
|
|
2862
|
+
package: {
|
|
2863
|
+
...document.package,
|
|
2864
|
+
document: body
|
|
2865
|
+
}
|
|
2866
|
+
};
|
|
2867
|
+
};
|
|
2868
|
+
const withoutAnchors = (nodes, ids) => {
|
|
2869
|
+
let changed = false;
|
|
2870
|
+
const kept = [];
|
|
2871
|
+
for (const node of nodes) {
|
|
2872
|
+
const id = anchorId(node);
|
|
2873
|
+
if (id !== void 0 && ids.has(id)) {
|
|
2874
|
+
changed = true;
|
|
2875
|
+
continue;
|
|
2876
|
+
}
|
|
2877
|
+
const children = childNodes(node);
|
|
2878
|
+
const filtered = children === void 0 ? void 0 : withoutAnchors(children, ids);
|
|
2879
|
+
if (filtered === children || filtered === void 0) {
|
|
2880
|
+
kept.push(node);
|
|
2881
|
+
continue;
|
|
2882
|
+
}
|
|
2883
|
+
changed = true;
|
|
2884
|
+
const next = rebuildNode(node, filtered);
|
|
2885
|
+
if (!isEmptyRecord(next)) kept.push(next);
|
|
2886
|
+
}
|
|
2887
|
+
return changed ? kept : nodes;
|
|
2888
|
+
};
|
|
2889
|
+
const restoreAnchors = ({ document, anchors, op }) => {
|
|
2890
|
+
let current = document;
|
|
2891
|
+
for (const { story, blockId, content } of anchors) {
|
|
2892
|
+
const body = findStoryBody(current, story);
|
|
2893
|
+
const location = body && storyParagraphs(body).find(({ paragraph }) => idKey(paragraph.paraId ?? "") === idKey(blockId));
|
|
2894
|
+
if (!location) return fail(op, "An owned comment paragraph is missing.", DOCUMENT_OP_REFUSAL_REASONS.STALE);
|
|
2895
|
+
const changed = replaceParagraphs({
|
|
2896
|
+
document: current,
|
|
2897
|
+
story,
|
|
2898
|
+
at: location,
|
|
2899
|
+
count: 1,
|
|
2900
|
+
replacement: [{
|
|
2901
|
+
...location.paragraph,
|
|
2902
|
+
content: cloneModel([...content])
|
|
2903
|
+
}]
|
|
2904
|
+
});
|
|
2905
|
+
if (changed.isErr()) return fail(op, changed.error.message, changed.error.reason);
|
|
2906
|
+
current = changed.value;
|
|
2907
|
+
}
|
|
2908
|
+
return Result.ok(current);
|
|
2909
|
+
};
|
|
2910
|
+
const restore = (document, op) => {
|
|
2911
|
+
const ids = new Set(op.ids);
|
|
2912
|
+
if (ids.size !== op.ids.length || op.ids.some((id) => !validId(id)) || !structurallyEqual(capture(document, ids, op.expected.anchors), op.expected)) return fail(op, "The comment inverse is stale.", DOCUMENT_OP_REFUSAL_REASONS.STALE);
|
|
2913
|
+
if (op.state.records.some(({ comment, index }) => !ids.has(comment.id) || !Number.isInteger(index) || index < 0) || new Set(op.state.records.map(({ comment }) => comment.id)).size !== op.state.records.length) return fail(op, "The restored comment records do not match the owned identities.");
|
|
2914
|
+
if (!op.scaffoldIds || !structurallyEqual(scaffoldDelta(op.expected, op.state), op.scaffoldIds)) return fail(op, "The comment inverse scaffold identities do not match its owned cuts.");
|
|
2915
|
+
const allowed = /* @__PURE__ */ new Set([...(op.scaffoldIds.revision ?? []).map((id) => `${IDENTITY_SPACES.REVISION}:${id}`), ...(op.scaffoldIds.control ?? []).map((id) => `${IDENTITY_SPACES.CONTROL}:${id}`)]);
|
|
2916
|
+
const identities = (state) => identityKeysIn(state.anchors).filter((id) => !allowed.has(id)).toSorted();
|
|
2917
|
+
if (!structurallyEqual(identities(op.expected), identities(op.state))) return fail(op, "The comment inverse changes an unowned identified record.");
|
|
2918
|
+
if (op.state.anchors.length !== op.expected.anchors.length || op.state.anchors.some((entry, index) => {
|
|
2919
|
+
const expected = op.expected.anchors.at(index);
|
|
2920
|
+
if (!expected || !sameStory(entry.story, expected.story) || idKey(entry.blockId) !== idKey(expected.blockId)) return true;
|
|
2921
|
+
const seams = /* @__PURE__ */ new Set([...ownedSeams(entry.content, ids), ...ownedSeams(expected.content, ids)]);
|
|
2922
|
+
const preserved = (content) => normalizedWithoutAnchors({
|
|
2923
|
+
content,
|
|
2924
|
+
ids,
|
|
2925
|
+
seams
|
|
2926
|
+
});
|
|
2927
|
+
const unowned = (content) => leafSpans(content).filter(({ node }) => {
|
|
2928
|
+
const id = anchorId(node);
|
|
2929
|
+
return id !== void 0 && !ids.has(id);
|
|
2930
|
+
}).map(({ node }) => node);
|
|
2931
|
+
return !structurallyEqual(preserved(entry.content), preserved(expected.content)) || !structurallyEqual(unowned(entry.content), unowned(expected.content));
|
|
2932
|
+
})) return fail(op, "A comment inverse cannot replace text or unowned anchors.");
|
|
2933
|
+
const anchors = restoreAnchors({
|
|
2934
|
+
document,
|
|
2935
|
+
op,
|
|
2936
|
+
anchors: op.state.anchors
|
|
2937
|
+
});
|
|
2938
|
+
if (anchors.isErr()) return anchors;
|
|
2939
|
+
const comments = (document.package.document.comments ?? []).filter(({ id }) => !ids.has(id));
|
|
2940
|
+
for (const { index, comment } of op.state.records) {
|
|
2941
|
+
if (index > comments.length) return fail(op, "A restored comment declaration position is stale.", DOCUMENT_OP_REFUSAL_REASONS.STALE);
|
|
2942
|
+
comments.splice(index, 0, cloneModel(comment));
|
|
2943
|
+
}
|
|
2944
|
+
if (op.state.listPresence !== "present" && comments.length > 0) return fail(op, "A missing comment list cannot discard unowned definitions.");
|
|
2945
|
+
const relationships = [...anchors.value.package.relationships ?? []].filter(([, relation]) => !isCommentRelationship(relation.type));
|
|
2946
|
+
for (const { index, key, relationship } of op.state.relationships) {
|
|
2947
|
+
const existing = op.expected.relationships.find((entry) => entry.key === key);
|
|
2948
|
+
if (existing && !structurallyEqual(existing.relationship, relationship)) return fail(op, "A comment inverse cannot alter an existing relationship payload.");
|
|
2949
|
+
if (!isCommentRelationship(relationship.type) || relationship.id !== key || index < 0 || index > relationships.length || relationships.some(([id]) => id === key)) return fail(op, "The comment inverse cannot replace an unowned relationship.");
|
|
2950
|
+
relationships.splice(index, 0, [key, cloneModel(relationship)]);
|
|
2951
|
+
}
|
|
2952
|
+
if (op.state.relationshipPresence !== "present" && relationships.length > 0) return fail(op, "A missing relationship map cannot discard unowned relationships.");
|
|
2953
|
+
const restored = withComments(anchors.value, comments, op.state.listPresence);
|
|
2954
|
+
const pkg = { ...restored.package };
|
|
2955
|
+
switch (op.state.relationshipPresence) {
|
|
2956
|
+
case "present":
|
|
2957
|
+
pkg.relationships = new Map(relationships);
|
|
2958
|
+
break;
|
|
2959
|
+
case "undefined":
|
|
2960
|
+
Reflect.set(pkg, "relationships", void 0);
|
|
2961
|
+
break;
|
|
2962
|
+
case "absent":
|
|
2963
|
+
delete pkg.relationships;
|
|
2964
|
+
break;
|
|
2965
|
+
}
|
|
2966
|
+
return edited(document, {
|
|
2967
|
+
...restored,
|
|
2968
|
+
package: pkg
|
|
2969
|
+
}, op, ids);
|
|
2970
|
+
};
|
|
2971
|
+
const revisionRange = (document, story, revisionId) => {
|
|
2972
|
+
const body = findStoryBody(document, story);
|
|
2973
|
+
if (!body) return void 0;
|
|
2974
|
+
const spans = storyParagraphs(body).flatMap(({ paragraph }) => {
|
|
2975
|
+
const leaves = leafSpans(paragraph.content);
|
|
2976
|
+
const position = (gap) => ({
|
|
2977
|
+
story,
|
|
2978
|
+
blockId: paragraph.paraId ?? "",
|
|
2979
|
+
offset: gap.offset,
|
|
2980
|
+
zeroWidthBefore: gap.zeroWidthBefore
|
|
2981
|
+
});
|
|
2982
|
+
if (paragraph.propertyChanges?.some(({ info }) => info.id === revisionId) || paragraph.pPrMark?.info.id === revisionId) {
|
|
2983
|
+
const start = leaves.at(0)?.before ?? {
|
|
2984
|
+
offset: 0,
|
|
2985
|
+
zeroWidthBefore: 0
|
|
2986
|
+
};
|
|
2987
|
+
const end = leaves.at(-1)?.after ?? start;
|
|
2988
|
+
return [{
|
|
2989
|
+
from: position(start),
|
|
2990
|
+
to: position(end)
|
|
2991
|
+
}];
|
|
2992
|
+
}
|
|
2993
|
+
return leaves.flatMap((span) => {
|
|
2994
|
+
return [span.node, ...span.ancestors].some((node) => (node.type === "insertion" || node.type === "deletion" || node.type === "moveFrom" || node.type === "moveTo") && node.info.id === revisionId || node.type === "run" && node.propertyChanges?.some(({ info }) => info.id === revisionId)) ? [{
|
|
2995
|
+
from: position(span.before),
|
|
2996
|
+
to: position(span.after)
|
|
2997
|
+
}] : [];
|
|
2998
|
+
});
|
|
2999
|
+
});
|
|
3000
|
+
const first = spans.at(0);
|
|
3001
|
+
const last = spans.at(-1);
|
|
3002
|
+
return first && last ? {
|
|
3003
|
+
from: first.from,
|
|
3004
|
+
to: last.to
|
|
3005
|
+
} : void 0;
|
|
3006
|
+
};
|
|
3007
|
+
const remainingIds = (document, ids) => {
|
|
3008
|
+
const used = new Set(packageIdentityKeys(document.package));
|
|
3009
|
+
return {
|
|
3010
|
+
...ids?.revision === void 0 ? {} : { revision: ids.revision.filter((id) => !used.has(`${IDENTITY_SPACES.REVISION}:${id}`)) },
|
|
3011
|
+
...ids?.control === void 0 ? {} : { control: ids.control.filter((id) => !used.has(`${IDENTITY_SPACES.CONTROL}:${id}`)) }
|
|
3012
|
+
};
|
|
3013
|
+
};
|
|
3014
|
+
const create = (document, op, applyOps) => {
|
|
3015
|
+
const comments = document.package.document.comments ?? [];
|
|
3016
|
+
if (!validId(op.comment.id) || comments.some(({ id }) => id === op.comment.id) || anchorsIn(document).some(({ node }) => anchorId(node) === op.comment.id) || new Set(packageIdentityKeys(document.package)).has(`${IDENTITY_SPACES.REVISION}:${op.comment.id}`)) return fail(op, "The fresh comment identity is invalid or already used.", DOCUMENT_OP_REFUSAL_REASONS.ID_COLLISION);
|
|
3017
|
+
if (Object.hasOwn(op.comment, "parentId")) return fail(op, "Comment parent ownership belongs to the anchor discriminator.");
|
|
3018
|
+
if (op.comment.content.length === 0 || op.comment.content.some(({ paraId }) => paraId === void 0 || !isParaId(paraId))) return fail(op, "Comment paragraphs need explicit package paragraph identities.", DOCUMENT_OP_REFUSAL_REASONS.INVALID_BLOCK_ID);
|
|
3019
|
+
let comment = cloneModel(op.comment);
|
|
3020
|
+
let positions = [];
|
|
3021
|
+
let range;
|
|
3022
|
+
switch (op.anchor.kind) {
|
|
3023
|
+
case "reply": {
|
|
3024
|
+
const parentId = op.anchor.parentId;
|
|
3025
|
+
if (!comments.some(({ id }) => id === parentId)) return fail(op, "The parent comment does not exist.");
|
|
3026
|
+
comment = {
|
|
3027
|
+
...comment,
|
|
3028
|
+
parentId: op.anchor.parentId
|
|
3029
|
+
};
|
|
3030
|
+
const anchors = anchorsIn(document);
|
|
3031
|
+
const parentById = new Map(comments.map(({ id, parentId: owner }) => [id, owner]));
|
|
3032
|
+
const visited = /* @__PURE__ */ new Set();
|
|
3033
|
+
let anchorOwner = parentId;
|
|
3034
|
+
let parentAnchors = [];
|
|
3035
|
+
while (anchorOwner !== void 0 && !visited.has(anchorOwner)) {
|
|
3036
|
+
visited.add(anchorOwner);
|
|
3037
|
+
const owner = anchorOwner;
|
|
3038
|
+
parentAnchors = anchors.filter(({ node }) => anchorId(node) === owner);
|
|
3039
|
+
if (parentAnchors.length > 0) break;
|
|
3040
|
+
const next = parentById.get(owner);
|
|
3041
|
+
anchorOwner = typeof next === "number" && parentById.has(next) ? next : void 0;
|
|
3042
|
+
}
|
|
3043
|
+
if (parentAnchors.length === 0) return fail(op, "The parent comment has no representable owned anchors.");
|
|
3044
|
+
positions = parentAnchors.toReversed().map(({ story, blockId, node, after }) => {
|
|
3045
|
+
switch (node.type) {
|
|
3046
|
+
case "commentRangeStart":
|
|
3047
|
+
case "commentRangeEnd":
|
|
3048
|
+
case "commentReference": return {
|
|
3049
|
+
at: {
|
|
3050
|
+
story,
|
|
3051
|
+
blockId,
|
|
3052
|
+
offset: after.offset,
|
|
3053
|
+
zeroWidthBefore: after.zeroWidthBefore
|
|
3054
|
+
},
|
|
3055
|
+
content: [{
|
|
3056
|
+
type: node.type,
|
|
3057
|
+
id: comment.id
|
|
3058
|
+
}]
|
|
3059
|
+
};
|
|
3060
|
+
default: return {
|
|
3061
|
+
at: {
|
|
3062
|
+
story,
|
|
3063
|
+
blockId,
|
|
3064
|
+
offset: after.offset,
|
|
3065
|
+
zeroWidthBefore: after.zeroWidthBefore
|
|
3066
|
+
},
|
|
3067
|
+
content: []
|
|
3068
|
+
};
|
|
3069
|
+
}
|
|
3070
|
+
});
|
|
3071
|
+
break;
|
|
3072
|
+
}
|
|
3073
|
+
case "revision": {
|
|
3074
|
+
const revisionId = op.anchor.revisionId;
|
|
3075
|
+
if (!validId(revisionId) || comments.some(({ id }) => id === revisionId)) return fail(op, "Revision association cannot ambiguously name a comment parent.");
|
|
3076
|
+
range = revisionRange(document, op.anchor.story, op.anchor.revisionId);
|
|
3077
|
+
if (!range) return fail(op, "The associated revision has no representable source range.");
|
|
3078
|
+
comment = {
|
|
3079
|
+
...comment,
|
|
3080
|
+
parentId: op.anchor.revisionId
|
|
3081
|
+
};
|
|
3082
|
+
if (range.from.blockId === range.to.blockId && range.from.offset === range.to.offset && range.from.zeroWidthBefore === range.to.zeroWidthBefore) {
|
|
3083
|
+
positions = [{
|
|
3084
|
+
at: range.from,
|
|
3085
|
+
content: [{
|
|
3086
|
+
type: "commentReference",
|
|
3087
|
+
id: comment.id
|
|
3088
|
+
}]
|
|
3089
|
+
}];
|
|
3090
|
+
range = void 0;
|
|
3091
|
+
}
|
|
3092
|
+
break;
|
|
3093
|
+
}
|
|
3094
|
+
case "point":
|
|
3095
|
+
positions = [{
|
|
3096
|
+
at: op.anchor.at,
|
|
3097
|
+
content: [{
|
|
3098
|
+
type: "commentReference",
|
|
3099
|
+
id: comment.id
|
|
3100
|
+
}]
|
|
3101
|
+
}];
|
|
3102
|
+
break;
|
|
3103
|
+
case "range":
|
|
3104
|
+
range = {
|
|
3105
|
+
from: op.anchor.from,
|
|
3106
|
+
to: op.anchor.to
|
|
3107
|
+
};
|
|
3108
|
+
break;
|
|
3109
|
+
default:
|
|
3110
|
+
op.anchor;
|
|
3111
|
+
return fail(op, "The comment anchor discriminator is invalid.");
|
|
3112
|
+
}
|
|
3113
|
+
if (range) {
|
|
3114
|
+
if (!sameStory(range.from.story, range.to.story)) return fail(op, "A comment range must stay in one story.", DOCUMENT_OP_REFUSAL_REASONS.CROSS_BLOCK_RANGE);
|
|
3115
|
+
const body = findStoryBody(document, range.from.story);
|
|
3116
|
+
if (!body) return fail(op, "The comment story does not exist.", DOCUMENT_OP_REFUSAL_REASONS.BLOCK_NOT_FOUND);
|
|
3117
|
+
const paragraphs = storyParagraphs(body).map(({ paragraph }) => paragraph);
|
|
3118
|
+
const fromIndex = paragraphs.findIndex(({ paraId }) => idKey(paraId ?? "") === idKey(range.from.blockId));
|
|
3119
|
+
const toIndex = paragraphs.findIndex(({ paraId }) => idKey(paraId ?? "") === idKey(range.to.blockId));
|
|
3120
|
+
const fromParagraph = paragraphs.at(fromIndex);
|
|
3121
|
+
const toParagraph = paragraphs.at(toIndex);
|
|
3122
|
+
if (fromIndex < 0 || toIndex < 0 || !fromParagraph || !toParagraph) return fail(op, "A comment boundary paragraph does not exist.", DOCUMENT_OP_REFUSAL_REASONS.BLOCK_NOT_FOUND);
|
|
3123
|
+
const fromGap = range.from.zeroWidthBefore === void 0 ? defaultInsertionGap(fromParagraph.content, range.from.offset) : {
|
|
3124
|
+
offset: range.from.offset,
|
|
3125
|
+
zeroWidthBefore: range.from.zeroWidthBefore
|
|
3126
|
+
};
|
|
3127
|
+
const toGap = range.to.zeroWidthBefore === void 0 ? defaultInsertionGap(toParagraph.content, range.to.offset) : {
|
|
3128
|
+
offset: range.to.offset,
|
|
3129
|
+
zeroWidthBefore: range.to.zeroWidthBefore
|
|
3130
|
+
};
|
|
3131
|
+
if (fromIndex > toIndex || fromIndex === toIndex && compareGaps(fromGap, toGap) > 0) return fail(op, "A comment range cannot run backwards.", DOCUMENT_OP_REFUSAL_REASONS.INVALID_OFFSET);
|
|
3132
|
+
positions = [{
|
|
3133
|
+
at: {
|
|
3134
|
+
...range.to,
|
|
3135
|
+
zeroWidthBefore: toGap.zeroWidthBefore
|
|
3136
|
+
},
|
|
3137
|
+
content: [{
|
|
3138
|
+
type: "commentRangeEnd",
|
|
3139
|
+
id: comment.id
|
|
3140
|
+
}, {
|
|
3141
|
+
type: "commentReference",
|
|
3142
|
+
id: comment.id
|
|
3143
|
+
}]
|
|
3144
|
+
}, {
|
|
3145
|
+
at: {
|
|
3146
|
+
...range.from,
|
|
3147
|
+
zeroWidthBefore: fromGap.zeroWidthBefore
|
|
3148
|
+
},
|
|
3149
|
+
content: [{
|
|
3150
|
+
type: "commentRangeStart",
|
|
3151
|
+
id: comment.id
|
|
3152
|
+
}]
|
|
3153
|
+
}];
|
|
3154
|
+
}
|
|
3155
|
+
let current = withComments(document, [...comments, comment]);
|
|
3156
|
+
for (const { at, content } of positions) {
|
|
3157
|
+
const inserted = applyOps(current, [{
|
|
3158
|
+
type: DOCUMENT_OP_TYPES.INSERT_CONTENT,
|
|
3159
|
+
at,
|
|
3160
|
+
slice: {
|
|
3161
|
+
content,
|
|
3162
|
+
openStart: 0,
|
|
3163
|
+
openEnd: 0
|
|
3164
|
+
},
|
|
3165
|
+
newIds: remainingIds(current, op.newIds)
|
|
3166
|
+
}]);
|
|
3167
|
+
if (inserted.isErr()) return Result.err(inserted.error);
|
|
3168
|
+
current = inserted.value.document;
|
|
3169
|
+
}
|
|
3170
|
+
return edited(document, current, op, /* @__PURE__ */ new Set([comment.id]));
|
|
3171
|
+
};
|
|
3172
|
+
const applyCommentOp = (document, op, applyOps) => {
|
|
3173
|
+
if (op.type === DOCUMENT_OP_TYPES.RESTORE_COMMENT_STATE) {
|
|
3174
|
+
if (!Array.isArray(op.ids) || !op.expected || !op.state || !Array.isArray(op.expected.relationships) || !Array.isArray(op.state.relationships) || !Array.isArray(op.expected.records) || !Array.isArray(op.expected.anchors) || !Array.isArray(op.state.records) || !Array.isArray(op.state.anchors)) return fail(op, "The comment inverse payload is invalid.");
|
|
3175
|
+
return restore(document, op);
|
|
3176
|
+
}
|
|
3177
|
+
const issue = commentDocumentIssue(document, "all");
|
|
3178
|
+
if (issue) return fail(op, issue);
|
|
3179
|
+
const orphans = orphanRootCommentIds(document);
|
|
3180
|
+
if (op.type === DOCUMENT_OP_TYPES.CREATE_COMMENT) {
|
|
3181
|
+
if (op.anchor?.kind === "reply" && orphans.has(op.anchor.parentId)) return fail(op, "A comment without a source anchor is preserved read-only.");
|
|
3182
|
+
if (!op.comment || typeof op.comment.author !== "string" || !Array.isArray(op.comment.content) || !op.anchor || typeof op.anchor !== "object") return fail(op, "The comment creation payload is invalid.");
|
|
3183
|
+
return create(document, op, applyOps);
|
|
3184
|
+
}
|
|
3185
|
+
const comments = document.package.document.comments ?? [];
|
|
3186
|
+
const comment = comments.find(({ id }) => id === op.id);
|
|
3187
|
+
if (!comment) return fail(op, "The comment does not exist.");
|
|
3188
|
+
if (orphans.has(comment.id)) return fail(op, "A comment without a source anchor is preserved read-only.");
|
|
3189
|
+
if (op.type === DOCUMENT_OP_TYPES.UPDATE_COMMENT_CONTENT) {
|
|
3190
|
+
if (!Array.isArray(op.content) || op.content.length === 0 || op.content.some(({ paraId }) => paraId === void 0 || !isParaId(paraId))) return fail(op, "Comment content needs explicit paragraph identities.", DOCUMENT_OP_REFUSAL_REASONS.INVALID_BLOCK_ID);
|
|
3191
|
+
if (op.patch && Object.keys(op.patch).some((key) => key !== "author" && key !== "initials" && key !== "date")) return fail(op, "A content edit cannot change comment ownership.");
|
|
3192
|
+
if (op.patch && Object.hasOwn(op.patch, "author") && (op.patch.author === null || op.patch.author === void 0)) return fail(op, "A comment must retain an author.");
|
|
3193
|
+
const updated = {
|
|
3194
|
+
...comment,
|
|
3195
|
+
...applyFormattingPatch({
|
|
3196
|
+
author: comment.author,
|
|
3197
|
+
...Object.hasOwn(comment, "initials") ? { initials: comment.initials } : {},
|
|
3198
|
+
...Object.hasOwn(comment, "date") ? { date: comment.date } : {}
|
|
3199
|
+
}, op.patch ?? {}),
|
|
3200
|
+
content: cloneModel([...op.content])
|
|
3201
|
+
};
|
|
3202
|
+
if (op.patch?.initials === null) delete updated.initials;
|
|
3203
|
+
if (op.patch?.date === null) delete updated.date;
|
|
3204
|
+
return edited(document, withComments(document, comments.map((value) => value === comment ? updated : value)), op, /* @__PURE__ */ new Set([comment.id]));
|
|
3205
|
+
}
|
|
3206
|
+
if (op.type === DOCUMENT_OP_TYPES.SET_COMMENT_RESOLUTION) {
|
|
3207
|
+
if (op.status !== "open" && op.status !== "resolved") return fail(op, "Comment resolution status is invalid.");
|
|
3208
|
+
return edited(document, withComments(document, comments.map((value) => value === comment ? {
|
|
3209
|
+
...comment,
|
|
3210
|
+
done: op.status === "resolved"
|
|
3211
|
+
} : value)), op, /* @__PURE__ */ new Set([comment.id]));
|
|
3212
|
+
}
|
|
3213
|
+
const parentIsComment = comment.parentId !== void 0 && comments.some(({ id }) => id === comment.parentId);
|
|
3214
|
+
if (op.scope !== "thread" && op.scope !== "reply" || op.scope === "reply" !== parentIsComment) return fail(op, "Comment deletion scope must match root or reply ownership.");
|
|
3215
|
+
const ids = /* @__PURE__ */ new Set([comment.id]);
|
|
3216
|
+
for (let previousSize = -1; previousSize !== ids.size;) {
|
|
3217
|
+
previousSize = ids.size;
|
|
3218
|
+
for (const value of comments) if (value.parentId !== void 0 && ids.has(value.parentId)) ids.add(value.id);
|
|
3219
|
+
}
|
|
3220
|
+
let current = document;
|
|
3221
|
+
for (const story of documentStories(document)) for (const { paragraph } of storyParagraphs(storyBody(document, story))) {
|
|
3222
|
+
const seams = new Set(ownedSeams(paragraph.content, ids));
|
|
3223
|
+
if (seams.size === 0) continue;
|
|
3224
|
+
const content = normalizedWithoutAnchors({
|
|
3225
|
+
content: paragraph.content,
|
|
3226
|
+
ids,
|
|
3227
|
+
seams
|
|
3228
|
+
});
|
|
3229
|
+
if (content === paragraph.content) continue;
|
|
3230
|
+
const changed = restoreAnchors({
|
|
3231
|
+
document: current,
|
|
3232
|
+
op,
|
|
3233
|
+
anchors: [{
|
|
3234
|
+
story,
|
|
3235
|
+
blockId: paragraph.paraId ?? "",
|
|
3236
|
+
content: asParagraphContent(content)
|
|
3237
|
+
}]
|
|
3238
|
+
});
|
|
3239
|
+
if (changed.isErr()) return changed;
|
|
3240
|
+
current = changed.value;
|
|
3241
|
+
}
|
|
3242
|
+
return edited(document, withComments(current, comments.filter(({ id }) => !ids.has(id)), comments.every(({ id }) => ids.has(id)) ? "absent" : "present"), op, ids);
|
|
3243
|
+
};
|
|
3244
|
+
//#endregion
|
|
3245
|
+
//#region src/ops/gaps.ts
|
|
3246
|
+
const isHighSurrogate = (code) => code >= 55296 && code <= 56319;
|
|
3247
|
+
const isLowSurrogate = (code) => code >= 56320 && code <= 57343;
|
|
3248
|
+
const resolveGap = ({ paragraph, position, fallback }) => {
|
|
3249
|
+
const text = paragraphLogicalText(paragraph);
|
|
3250
|
+
const { offset, zeroWidthBefore } = position;
|
|
3251
|
+
if (!Number.isInteger(offset) || offset < 0 || offset > text.length) return DOCUMENT_OP_REFUSAL_REASONS.INVALID_OFFSET;
|
|
3252
|
+
if (offset > 0 && offset < text.length && isHighSurrogate(text.charCodeAt(offset - 1)) && isLowSurrogate(text.charCodeAt(offset))) return DOCUMENT_OP_REFUSAL_REASONS.SPLITS_SURROGATE_PAIR;
|
|
3253
|
+
const available = zeroWidthLeavesAt(paragraph.content, offset).length;
|
|
3254
|
+
if (zeroWidthBefore !== void 0) return Number.isInteger(zeroWidthBefore) && zeroWidthBefore >= 0 && zeroWidthBefore <= available ? {
|
|
3255
|
+
offset,
|
|
3256
|
+
zeroWidthBefore
|
|
3257
|
+
} : DOCUMENT_OP_REFUSAL_REASONS.INVALID_OFFSET;
|
|
3258
|
+
switch (fallback) {
|
|
3259
|
+
case "afterAll": return {
|
|
3260
|
+
offset,
|
|
3261
|
+
zeroWidthBefore: available
|
|
3262
|
+
};
|
|
3263
|
+
case "beforeAll": return {
|
|
3264
|
+
offset,
|
|
3265
|
+
zeroWidthBefore: 0
|
|
3266
|
+
};
|
|
3267
|
+
case "insertion": return defaultInsertionGap(paragraph.content, offset);
|
|
3268
|
+
default: return fallback;
|
|
3269
|
+
}
|
|
3270
|
+
};
|
|
3271
|
+
//#endregion
|
|
3272
|
+
//#region src/ops/review.ts
|
|
3273
|
+
/** The tracked-change metadata a stamp writes on the records it creates. */
|
|
3274
|
+
const stampInfo = (stamp) => {
|
|
3275
|
+
const info = {
|
|
3276
|
+
id: stamp.id,
|
|
3277
|
+
author: stamp.author,
|
|
3278
|
+
date: stamp.date
|
|
3279
|
+
};
|
|
3280
|
+
if (stamp.initials !== void 0) info.initials = stamp.initials;
|
|
3281
|
+
return info;
|
|
3282
|
+
};
|
|
3283
|
+
/** Whether a record's metadata is the stamp's, whatever its id. */
|
|
3284
|
+
const carriesStamp = (info, stamp) => structurallyEqual({
|
|
3285
|
+
...info,
|
|
3286
|
+
id: 0
|
|
3287
|
+
}, {
|
|
3288
|
+
...stampInfo(stamp),
|
|
3289
|
+
id: 0
|
|
3290
|
+
});
|
|
3291
|
+
const isTrackedWrapper = (node) => node.type === "insertion" || node.type === "deletion" || node.type === "moveFrom" || node.type === "moveTo";
|
|
3292
|
+
/** Tracked content on its way in: an insertion or the destination of a move. */
|
|
3293
|
+
const isAddedRevision = (node) => node.type === "insertion" || node.type === "moveTo";
|
|
3294
|
+
/** Tracked content on its way out: a deletion or the source of a move. */
|
|
3295
|
+
const isRemovedRevisionNode = (node) => node.type === "deletion" || node.type === "moveFrom";
|
|
3296
|
+
/** The tracked-change metadata of every record a paragraph and its content carry. */
|
|
3297
|
+
const revisionInfosOf = (paragraph) => {
|
|
3298
|
+
const out = [];
|
|
3299
|
+
for (const change of paragraph.propertyChanges ?? []) out.push(change.info);
|
|
3300
|
+
if (paragraph.pPrMark !== void 0) out.push(paragraph.pPrMark.info);
|
|
3301
|
+
const visit = (nodes) => {
|
|
3302
|
+
for (const node of nodes) {
|
|
3303
|
+
if (isTrackedWrapper(node)) out.push(node.info);
|
|
3304
|
+
else if (node.type === "run") for (const change of node.propertyChanges ?? []) out.push(change.info);
|
|
3305
|
+
visit(childNodes(node) ?? []);
|
|
3306
|
+
}
|
|
3307
|
+
};
|
|
3308
|
+
visit(paragraph.content);
|
|
3309
|
+
return out;
|
|
3310
|
+
};
|
|
3311
|
+
/**
|
|
3312
|
+
* The revision ids of the records carrying the stamp that `known` does not
|
|
3313
|
+
* name (slot keys): the records an operation created, in document order.
|
|
3314
|
+
*/
|
|
3315
|
+
const stampedRevisionIds = (paragraphs, stamp, known) => {
|
|
3316
|
+
const out = [];
|
|
3317
|
+
for (const paragraph of paragraphs) for (const info of revisionInfosOf(paragraph)) {
|
|
3318
|
+
const key = slotKey({
|
|
3319
|
+
space: IDENTITY_SPACES.REVISION,
|
|
3320
|
+
id: info.id
|
|
3321
|
+
});
|
|
3322
|
+
if (carriesStamp(info, stamp) && !known.has(key) && !out.includes(info.id)) out.push(info.id);
|
|
3323
|
+
}
|
|
3324
|
+
return out;
|
|
3325
|
+
};
|
|
3326
|
+
const MARK_KEYS = new Set(PARAGRAPH_MARK_FORMATTING_KEYS);
|
|
3327
|
+
/** Whether a patch or property set names a key of the paragraph mark's run properties. */
|
|
3328
|
+
const namesMarkFormatting = (formatting) => Object.entries(formatting).some(([key, value]) => value !== void 0 && MARK_KEYS.has(key));
|
|
3329
|
+
const pick = (formatting, keep) => {
|
|
3330
|
+
if (formatting === void 0) return;
|
|
3331
|
+
return Object.fromEntries(Object.entries(formatting).filter(([key]) => keep(key)));
|
|
3332
|
+
};
|
|
3333
|
+
/** The paragraph properties a `w:pPrChange` records: all but the mark's run properties. */
|
|
3334
|
+
const paragraphPropertiesOf = (formatting) => pick(formatting, (key) => !MARK_KEYS.has(key));
|
|
3335
|
+
/** The paragraph mark's run properties of a property set; `undefined` when it states none. */
|
|
3336
|
+
const markFormattingOf = (formatting) => {
|
|
3337
|
+
const marks = pick(formatting, (key) => MARK_KEYS.has(key));
|
|
3338
|
+
return marks === void 0 || Object.keys(marks).length === 0 ? void 0 : marks;
|
|
3339
|
+
};
|
|
3340
|
+
/** Whether two property sets state the same paragraph properties. */
|
|
3341
|
+
const sameParagraphProperties = (left, right) => structurallyEqual(paragraphPropertiesOf(left), paragraphPropertiesOf(right));
|
|
3342
|
+
/** Whether two property sets state the same run properties for the paragraph mark. */
|
|
3343
|
+
const sameMarkFormatting = (left, right) => structurallyEqual(markFormattingOf(left) ?? {}, markFormattingOf(right) ?? {});
|
|
3344
|
+
/**
|
|
3345
|
+
* The paragraph properties of `properties` with the mark's run properties of
|
|
3346
|
+
* `marks`. `undefined` when the first is absent and the second states no mark
|
|
3347
|
+
* properties.
|
|
3348
|
+
*/
|
|
3349
|
+
const withMarkFormatting = (properties, marks) => {
|
|
3350
|
+
const markFormatting = markFormattingOf(marks);
|
|
3351
|
+
if (properties === void 0 && markFormatting === void 0) return;
|
|
3352
|
+
return {
|
|
3353
|
+
...paragraphPropertiesOf(properties),
|
|
3354
|
+
...markFormatting
|
|
3355
|
+
};
|
|
3356
|
+
};
|
|
3357
|
+
/** A tracked paragraph property change from `previous`. */
|
|
3358
|
+
const paragraphPropertyChange = (info, previous) => {
|
|
3359
|
+
const change = {
|
|
3360
|
+
type: "paragraphPropertyChange",
|
|
3361
|
+
info
|
|
3362
|
+
};
|
|
3363
|
+
const recorded = paragraphPropertiesOf(previous);
|
|
3364
|
+
if (recorded !== void 0) change.previousFormatting = recorded;
|
|
3365
|
+
return change;
|
|
3366
|
+
};
|
|
3367
|
+
/** Fold paragraph formatting into one review, retaining its id and original baseline. */
|
|
3368
|
+
const foldedParagraphPropertyChange = ({ paragraph, formatting, stamp }) => {
|
|
3369
|
+
const existing = paragraph.propertyChanges?.at(0);
|
|
3370
|
+
if (existing === void 0) return paragraphPropertyChange(stampInfo(stamp), paragraph.formatting);
|
|
3371
|
+
const info = {
|
|
3372
|
+
...existing.info,
|
|
3373
|
+
...stampInfo(stamp),
|
|
3374
|
+
id: existing.info.id
|
|
2609
3375
|
};
|
|
2610
3376
|
if (stamp.initials === void 0) delete info.initials;
|
|
2611
3377
|
delete info.utcDate;
|
|
@@ -2812,10 +3578,9 @@ const wrapList = (nodes, cursor, state) => {
|
|
|
2812
3578
|
offset: gap.offset - start.position,
|
|
2813
3579
|
zeroWidthBefore: gap.offset === start.position ? gap.zeroWidthBefore - start.zeroWidthSeen : gap.zeroWidthBefore
|
|
2814
3580
|
});
|
|
2815
|
-
const beforeChain = spanningRecords([node], [localGap(state.gaps[0])], 0, 1);
|
|
2816
3581
|
const afterChain = spanningRecords([node], [localGap(state.gaps[1])], 0, 1);
|
|
2817
3582
|
const retainedAfter = region === 1 && state.kind === WRAP_KINDS.DELETION ? afterChain.flatMap((record, depth) => {
|
|
2818
|
-
const source =
|
|
3583
|
+
const source = identitySlots(record);
|
|
2819
3584
|
return source.length === 0 ? [] : [{
|
|
2820
3585
|
depth,
|
|
2821
3586
|
source,
|
|
@@ -3536,33 +4301,78 @@ const insertBlocks = ({ document, op, applyOps }) => {
|
|
|
3536
4301
|
}
|
|
3537
4302
|
};
|
|
3538
4303
|
//#endregion
|
|
3539
|
-
//#region src/ops/
|
|
3540
|
-
|
|
3541
|
-
|
|
3542
|
-
|
|
3543
|
-
|
|
3544
|
-
|
|
3545
|
-
*/
|
|
3546
|
-
|
|
3547
|
-
|
|
3548
|
-
|
|
3549
|
-
|
|
3550
|
-
|
|
3551
|
-
|
|
3552
|
-
|
|
3553
|
-
|
|
3554
|
-
|
|
3555
|
-
|
|
3556
|
-
|
|
3557
|
-
|
|
3558
|
-
|
|
3559
|
-
|
|
3560
|
-
|
|
3561
|
-
|
|
3562
|
-
|
|
3563
|
-
|
|
3564
|
-
|
|
3565
|
-
|
|
4304
|
+
//#region src/ops/edits.ts
|
|
4305
|
+
const TOUCHED_STATES = [
|
|
4306
|
+
"modified",
|
|
4307
|
+
"inserted",
|
|
4308
|
+
"removed"
|
|
4309
|
+
];
|
|
4310
|
+
/** A block's net change after two changes in turn; `undefined` when it came and went. */
|
|
4311
|
+
const TOUCH_TRANSITIONS = {
|
|
4312
|
+
modified: {
|
|
4313
|
+
modified: "modified",
|
|
4314
|
+
inserted: "modified",
|
|
4315
|
+
removed: "removed"
|
|
4316
|
+
},
|
|
4317
|
+
inserted: {
|
|
4318
|
+
modified: "inserted",
|
|
4319
|
+
inserted: "inserted",
|
|
4320
|
+
removed: void 0
|
|
4321
|
+
},
|
|
4322
|
+
removed: {
|
|
4323
|
+
modified: "modified",
|
|
4324
|
+
inserted: "modified",
|
|
4325
|
+
removed: "removed"
|
|
4326
|
+
}
|
|
4327
|
+
};
|
|
4328
|
+
/** Edits made in turn, starting from `document`, as one edit. */
|
|
4329
|
+
const combineEdits = (document, edits) => {
|
|
4330
|
+
const touched = /* @__PURE__ */ new Map();
|
|
4331
|
+
for (const edit of edits) for (const state of TOUCHED_STATES) for (const id of edit.touched[state]) {
|
|
4332
|
+
const previous = touched.get(id);
|
|
4333
|
+
const next = previous === void 0 ? state : TOUCH_TRANSITIONS[previous][state];
|
|
4334
|
+
if (next === void 0) touched.delete(id);
|
|
4335
|
+
else touched.set(id, next);
|
|
4336
|
+
}
|
|
4337
|
+
const ids = (state) => [...touched].flatMap(([id, value]) => value === state ? [id] : []);
|
|
4338
|
+
return {
|
|
4339
|
+
document: edits.at(-1)?.document ?? document,
|
|
4340
|
+
inverse: edits.toReversed().flatMap(({ inverse }) => inverse),
|
|
4341
|
+
touched: {
|
|
4342
|
+
modified: ids("modified"),
|
|
4343
|
+
inserted: ids("inserted"),
|
|
4344
|
+
removed: ids("removed")
|
|
4345
|
+
}
|
|
4346
|
+
};
|
|
4347
|
+
};
|
|
4348
|
+
//#endregion
|
|
4349
|
+
//#region src/ops/runMerge.ts
|
|
4350
|
+
/**
|
|
4351
|
+
* Check if two TextFormatting objects are equivalent
|
|
4352
|
+
*
|
|
4353
|
+
* Uses deep comparison of all properties to determine if runs
|
|
4354
|
+
* can be merged without losing formatting information.
|
|
4355
|
+
*/
|
|
4356
|
+
function formattingEquals(a, b) {
|
|
4357
|
+
if (!a && !b) return true;
|
|
4358
|
+
if (!a || !b) return false;
|
|
4359
|
+
if (a.bold !== b.bold) return false;
|
|
4360
|
+
if (a.boldCs !== b.boldCs) return false;
|
|
4361
|
+
if (a.italic !== b.italic) return false;
|
|
4362
|
+
if (a.italicCs !== b.italicCs) return false;
|
|
4363
|
+
if (a.strike !== b.strike) return false;
|
|
4364
|
+
if (a.doubleStrike !== b.doubleStrike) return false;
|
|
4365
|
+
if (a.smallCaps !== b.smallCaps) return false;
|
|
4366
|
+
if (a.allCaps !== b.allCaps) return false;
|
|
4367
|
+
if (a.hidden !== b.hidden) return false;
|
|
4368
|
+
if (a.noProof !== b.noProof) return false;
|
|
4369
|
+
if (a.emboss !== b.emboss) return false;
|
|
4370
|
+
if (a.imprint !== b.imprint) return false;
|
|
4371
|
+
if (a.outline !== b.outline) return false;
|
|
4372
|
+
if (a.shadow !== b.shadow) return false;
|
|
4373
|
+
if (a.rtl !== b.rtl) return false;
|
|
4374
|
+
if (a.cs !== b.cs) return false;
|
|
4375
|
+
if (a.fontSize !== b.fontSize) return false;
|
|
3566
4376
|
if (a.fontSizeCs !== b.fontSizeCs) return false;
|
|
3567
4377
|
if (a.spacing !== b.spacing) return false;
|
|
3568
4378
|
if (a.position !== b.position) return false;
|
|
@@ -5614,6 +6424,7 @@ const resolveRevision = (document, op, applyOps) => {
|
|
|
5614
6424
|
if (candidate?.type !== "paragraph") break;
|
|
5615
6425
|
leadingCut = Math.max(leadingCut, resolvedCutEdges.get(idKey(candidate.paraId ?? ""))?.first ?? 0);
|
|
5616
6426
|
if ((resolvedContents.get(idKey(candidate.paraId ?? ""))?.length ?? 0) !== 0 || candidate.pPrMark === void 0 || !markWasAdded(candidate.pPrMark.kind)) break;
|
|
6427
|
+
if (op.decision === REVISION_DECISIONS.REJECT && ids.has(candidate.pPrMark.info.id)) leadingCut = Math.max(leadingCut, candidate.pPrMark.resolutionJoin ?? 0);
|
|
5617
6428
|
}
|
|
5618
6429
|
let depth = Math.max(mark.resolutionJoin, resolvedCutEdges.get(idKey(paragraph.paraId ?? ""))?.last ?? 0, leadingCut);
|
|
5619
6430
|
if (trailing !== void 0) depth = Math.min(depth, trailing);
|
|
@@ -5970,7 +6781,7 @@ const stampedTableRevisionIds = ({ blocks, stamp, known }) => {
|
|
|
5970
6781
|
//#endregion
|
|
5971
6782
|
//#region src/ops/tables.ts
|
|
5972
6783
|
/** Whole-table edits and their exact structural inverse. */
|
|
5973
|
-
const refuse$
|
|
6784
|
+
const refuse$4 = ({ op, reason, message }) => Result.err(new DocumentOpRefusal({
|
|
5974
6785
|
opType: op.type,
|
|
5975
6786
|
reason,
|
|
5976
6787
|
message
|
|
@@ -6011,7 +6822,7 @@ const commitBlocks = ({ document, op, anchor, blocks }) => {
|
|
|
6011
6822
|
removed: []
|
|
6012
6823
|
}
|
|
6013
6824
|
});
|
|
6014
|
-
if (!blocks.some((block) => block.type === "paragraph" && idKey(block.paraId ?? "") === idKey(anchor.paragraph.paraId ?? ""))) return refuse$
|
|
6825
|
+
if (!blocks.some((block) => block.type === "paragraph" && idKey(block.paraId ?? "") === idKey(anchor.paragraph.paraId ?? ""))) return refuse$4({
|
|
6015
6826
|
op,
|
|
6016
6827
|
reason: DOCUMENT_OP_REFUSAL_REASONS.UNTRACKABLE,
|
|
6017
6828
|
message: "The structural inverse requires a surviving paragraph in this block list."
|
|
@@ -6019,13 +6830,13 @@ const commitBlocks = ({ document, op, anchor, blocks }) => {
|
|
|
6019
6830
|
if (blocks.length === 0 || !validStructure(before) || !validStructure(blocks) || endsItsContainer(body, {
|
|
6020
6831
|
list: anchor.list,
|
|
6021
6832
|
index: before.length - 1
|
|
6022
|
-
}) && (!endsInParagraph(before) || !endsInParagraph(blocks))) return refuse$
|
|
6833
|
+
}) && (!endsInParagraph(before) || !endsInParagraph(blocks))) return refuse$4({
|
|
6023
6834
|
op,
|
|
6024
6835
|
reason: DOCUMENT_OP_REFUSAL_REASONS.STRUCTURE_MISMATCH,
|
|
6025
6836
|
message: "A table has rows and cells; each cell and the story retain a final paragraph."
|
|
6026
6837
|
});
|
|
6027
6838
|
const fixedBlocks = (items) => items.filter((block) => block.type !== "table" && block.type !== "paragraph");
|
|
6028
|
-
if (!structurallyEqual(fixedBlocks(before), fixedBlocks(blocks))) return refuse$
|
|
6839
|
+
if (!structurallyEqual(fixedBlocks(before), fixedBlocks(blocks))) return refuse$4({
|
|
6029
6840
|
op,
|
|
6030
6841
|
reason: DOCUMENT_OP_REFUSAL_REASONS.UNTRACKABLE,
|
|
6031
6842
|
message: "A table structural inverse preserves surrounding block wrappers and markers."
|
|
@@ -6037,17 +6848,17 @@ const commitBlocks = ({ document, op, anchor, blocks }) => {
|
|
|
6037
6848
|
if ([...beforeParagraphs, ...afterParagraphs].some((paragraph) => {
|
|
6038
6849
|
const id = idKey(paragraph.paraId ?? "");
|
|
6039
6850
|
return !structurallyEqual(paragraph.sectionProperties, (beforeById.has(id) ? afterById : beforeById).get(id)?.sectionProperties);
|
|
6040
|
-
})) return refuse$
|
|
6851
|
+
})) return refuse$4({
|
|
6041
6852
|
op,
|
|
6042
6853
|
reason: DOCUMENT_OP_REFUSAL_REASONS.SECTION_BOUNDARY,
|
|
6043
6854
|
message: "Table edits preserve section boundaries."
|
|
6044
6855
|
});
|
|
6045
|
-
if ([...beforeParagraphs, ...afterParagraphs].some(({ paraId }) => paraId === void 0 || (!beforeById.has(idKey(paraId)) || !afterById.has(idKey(paraId))) && !isParaId(paraId))) return refuse$
|
|
6856
|
+
if ([...beforeParagraphs, ...afterParagraphs].some(({ paraId }) => paraId === void 0 || (!beforeById.has(idKey(paraId)) || !afterById.has(idKey(paraId))) && !isParaId(paraId))) return refuse$4({
|
|
6046
6857
|
op,
|
|
6047
6858
|
reason: DOCUMENT_OP_REFUSAL_REASONS.INVALID_BLOCK_ID,
|
|
6048
6859
|
message: "An added or removed paragraph needs a usable id."
|
|
6049
6860
|
});
|
|
6050
|
-
if ([...beforeParagraphs, ...afterParagraphs].some(({ content }) => textsIn(content).some(hasIllegalXmlCharacters))) return refuse$
|
|
6861
|
+
if ([...beforeParagraphs, ...afterParagraphs].some(({ content }) => textsIn(content).some(hasIllegalXmlCharacters))) return refuse$4({
|
|
6051
6862
|
op,
|
|
6052
6863
|
reason: DOCUMENT_OP_REFUSAL_REASONS.INVALID_TEXT,
|
|
6053
6864
|
message: "The table holds text that cannot be written to XML."
|
|
@@ -6058,7 +6869,7 @@ const commitBlocks = ({ document, op, anchor, blocks }) => {
|
|
|
6058
6869
|
body,
|
|
6059
6870
|
location,
|
|
6060
6871
|
op
|
|
6061
|
-
}) && !structurallyEqual(paragraph.pPrMark, afterById.get(idKey(paragraph.paraId ?? ""))?.pPrMark)) return refuse$
|
|
6872
|
+
}) && !structurallyEqual(paragraph.pPrMark, afterById.get(idKey(paragraph.paraId ?? ""))?.pPrMark)) return refuse$4({
|
|
6062
6873
|
op,
|
|
6063
6874
|
reason: DOCUMENT_OP_REFUSAL_REASONS.CONTAINER_FINAL_MARK,
|
|
6064
6875
|
message: "Removing this final paragraph mark would make the structural inverse invalid."
|
|
@@ -6066,7 +6877,7 @@ const commitBlocks = ({ document, op, anchor, blocks }) => {
|
|
|
6066
6877
|
}
|
|
6067
6878
|
const remaining = countIds(packageParagraphIds(document.package));
|
|
6068
6879
|
for (const id of paragraphIdsIn(before)) remaining.set(idKey(id), (remaining.get(idKey(id)) ?? 1) - 1);
|
|
6069
|
-
if (collides(remaining, paragraphIdsIn(blocks))) return refuse$
|
|
6880
|
+
if (collides(remaining, paragraphIdsIn(blocks))) return refuse$4({
|
|
6070
6881
|
op,
|
|
6071
6882
|
reason: DOCUMENT_OP_REFUSAL_REASONS.ID_COLLISION,
|
|
6072
6883
|
message: "A paragraph id is already used in the package."
|
|
@@ -6074,7 +6885,7 @@ const commitBlocks = ({ document, op, anchor, blocks }) => {
|
|
|
6074
6885
|
const records = countKeys(packageIdentityKeys(document.package));
|
|
6075
6886
|
for (const key of identityKeysIn(before)) records.set(key, (records.get(key) ?? 1) - 1);
|
|
6076
6887
|
const incoming = identityKeysIn(blocks);
|
|
6077
|
-
if (incoming.some((key) => (records.get(key) ?? 0) > 0) || new Set(incoming).size !== incoming.length) return refuse$
|
|
6888
|
+
if (incoming.some((key) => (records.get(key) ?? 0) > 0) || new Set(incoming).size !== incoming.length) return refuse$4({
|
|
6078
6889
|
op,
|
|
6079
6890
|
reason: DOCUMENT_OP_REFUSAL_REASONS.ID_COLLISION,
|
|
6080
6891
|
message: "A revision or content-control id is already used in the package."
|
|
@@ -6132,14 +6943,14 @@ const commitBlocks = ({ document, op, anchor, blocks }) => {
|
|
|
6132
6943
|
body: nextBody,
|
|
6133
6944
|
location,
|
|
6134
6945
|
op
|
|
6135
|
-
}) && !structurallyEqual(paragraph.pPrMark, previousParagraphs.get(idKey(paragraph.paraId ?? ""))?.pPrMark)) return refuse$
|
|
6946
|
+
}) && !structurallyEqual(paragraph.pPrMark, previousParagraphs.get(idKey(paragraph.paraId ?? ""))?.pPrMark)) return refuse$4({
|
|
6136
6947
|
op,
|
|
6137
6948
|
reason: DOCUMENT_OP_REFUSAL_REASONS.CONTAINER_FINAL_MARK,
|
|
6138
6949
|
message: "A table edit cannot add a final paragraph mark."
|
|
6139
6950
|
});
|
|
6140
6951
|
}
|
|
6141
6952
|
const valid = validateOpsDocument(next);
|
|
6142
|
-
if (valid.isErr()) return refuse$
|
|
6953
|
+
if (valid.isErr()) return refuse$4({
|
|
6143
6954
|
op,
|
|
6144
6955
|
reason: valid.error.reason,
|
|
6145
6956
|
message: valid.error.message
|
|
@@ -6164,32 +6975,32 @@ const applyTableOp = (document, op) => {
|
|
|
6164
6975
|
const body = storyBody(document, op.story);
|
|
6165
6976
|
switch (op.type) {
|
|
6166
6977
|
case DOCUMENT_OP_TYPES.INSERT_TABLE: {
|
|
6167
|
-
if (op.at.type !== "before" && op.at.type !== "after") return refuse$
|
|
6978
|
+
if (op.at.type !== "before" && op.at.type !== "after") return refuse$4({
|
|
6168
6979
|
op,
|
|
6169
6980
|
reason: DOCUMENT_OP_REFUSAL_REASONS.STRUCTURE_MISMATCH,
|
|
6170
6981
|
message: "Insert before or after a paragraph."
|
|
6171
6982
|
});
|
|
6172
6983
|
const anchor = storyParagraphs(body).find(({ paragraph }) => idKey(paragraph.paraId ?? "") === idKey(op.at.blockId));
|
|
6173
|
-
if (anchor === void 0) return refuse$
|
|
6984
|
+
if (anchor === void 0) return refuse$4({
|
|
6174
6985
|
op,
|
|
6175
6986
|
reason: DOCUMENT_OP_REFUSAL_REASONS.BLOCK_NOT_FOUND,
|
|
6176
6987
|
message: "The table insertion anchor does not exist."
|
|
6177
6988
|
});
|
|
6178
6989
|
const blocks = [...blockListAt(body.content, anchor.list)];
|
|
6179
6990
|
const terminal = op.at.type === "after" && endsItsContainer(body, anchor);
|
|
6180
|
-
if (op.terminal !== void 0 && !terminal) return refuse$
|
|
6991
|
+
if (op.terminal !== void 0 && !terminal) return refuse$4({
|
|
6181
6992
|
op,
|
|
6182
6993
|
reason: DOCUMENT_OP_REFUSAL_REASONS.STRUCTURE_MISMATCH,
|
|
6183
6994
|
message: "A terminal insertion must follow its container's final paragraph."
|
|
6184
6995
|
});
|
|
6185
|
-
if (terminal && op.terminal === void 0) return refuse$
|
|
6996
|
+
if (terminal && op.terminal === void 0) return refuse$4({
|
|
6186
6997
|
op,
|
|
6187
6998
|
reason: DOCUMENT_OP_REFUSAL_REASONS.STRUCTURE_MISMATCH,
|
|
6188
6999
|
message: "A terminal table insertion supplies the preceding paragraph id."
|
|
6189
7000
|
});
|
|
6190
7001
|
let preceding;
|
|
6191
7002
|
if (op.terminal !== void 0) {
|
|
6192
|
-
if (op.revision !== void 0 && anchor.paragraph.pPrMark !== void 0) return refuse$
|
|
7003
|
+
if (op.revision !== void 0 && anchor.paragraph.pPrMark !== void 0) return refuse$4({
|
|
6193
7004
|
op,
|
|
6194
7005
|
reason: DOCUMENT_OP_REFUSAL_REASONS.REVISION_CONFLICT,
|
|
6195
7006
|
message: "The final paragraph already carries a mark revision."
|
|
@@ -6211,7 +7022,7 @@ const applyTableOp = (document, op) => {
|
|
|
6211
7022
|
anchor,
|
|
6212
7023
|
blocks
|
|
6213
7024
|
});
|
|
6214
|
-
if (op.table.preserved !== void 0 || op.table.bookmarks !== void 0 || op.table.carrierStack !== void 0 || (op.table.propertyChanges?.length ?? 0) > 0) return refuse$
|
|
7025
|
+
if (op.table.preserved !== void 0 || op.table.bookmarks !== void 0 || op.table.carrierStack !== void 0 || (op.table.propertyChanges?.length ?? 0) > 0) return refuse$4({
|
|
6215
7026
|
op,
|
|
6216
7027
|
reason: DOCUMENT_OP_REFUSAL_REASONS.UNTRACKABLE,
|
|
6217
7028
|
message: "Tracked table construction does not support captured markup or table property revisions."
|
|
@@ -6255,12 +7066,12 @@ const applyTableOp = (document, op) => {
|
|
|
6255
7066
|
usedElsewhere: () => outside
|
|
6256
7067
|
});
|
|
6257
7068
|
switch (fresh.kind) {
|
|
6258
|
-
case "needsIds": return refuse$
|
|
7069
|
+
case "needsIds": return refuse$4({
|
|
6259
7070
|
op,
|
|
6260
7071
|
reason: DOCUMENT_OP_REFUSAL_REASONS.NEEDS_NEW_IDS,
|
|
6261
7072
|
message: "The preceding table break needs an additional revision id."
|
|
6262
7073
|
});
|
|
6263
|
-
case "invalidId": return refuse$
|
|
7074
|
+
case "invalidId": return refuse$4({
|
|
6264
7075
|
op,
|
|
6265
7076
|
reason: DOCUMENT_OP_REFUSAL_REASONS.INVALID_NEW_ID,
|
|
6266
7077
|
message: "The preceding table break has an invalid revision id."
|
|
@@ -6285,13 +7096,13 @@ const applyTableOp = (document, op) => {
|
|
|
6285
7096
|
const located = locateTableRow(document, op);
|
|
6286
7097
|
if (located.isErr()) return Result.err(located.error);
|
|
6287
7098
|
const location = located.value;
|
|
6288
|
-
if (op.expected !== void 0 && !equalForStaleness(location.table, op.expected)) return refuse$
|
|
7099
|
+
if (op.expected !== void 0 && !equalForStaleness(location.table, op.expected)) return refuse$4({
|
|
6289
7100
|
op,
|
|
6290
7101
|
reason: DOCUMENT_OP_REFUSAL_REASONS.STALE,
|
|
6291
7102
|
message: "The table to remove has changed."
|
|
6292
7103
|
});
|
|
6293
7104
|
const anchor = storyParagraphs(body).find((paragraph) => sameBlockList(paragraph.list, location.list));
|
|
6294
|
-
if (anchor === void 0) return refuse$
|
|
7105
|
+
if (anchor === void 0) return refuse$4({
|
|
6295
7106
|
op,
|
|
6296
7107
|
reason: DOCUMENT_OP_REFUSAL_REASONS.UNTRACKABLE,
|
|
6297
7108
|
message: "Removing a table requires a surviving paragraph in its block list."
|
|
@@ -6299,7 +7110,7 @@ const applyTableOp = (document, op) => {
|
|
|
6299
7110
|
const blocks = [...blockListAt(body.content, location.list)];
|
|
6300
7111
|
if (op.revision === void 0) blocks.splice(location.index, 1);
|
|
6301
7112
|
else {
|
|
6302
|
-
if (location.table.preserved !== void 0 || location.table.bookmarks !== void 0 || location.table.carrierStack !== void 0 || (location.table.propertyChanges?.length ?? 0) > 0) return refuse$
|
|
7113
|
+
if (location.table.preserved !== void 0 || location.table.bookmarks !== void 0 || location.table.carrierStack !== void 0 || (location.table.propertyChanges?.length ?? 0) > 0) return refuse$4({
|
|
6303
7114
|
op,
|
|
6304
7115
|
reason: DOCUMENT_OP_REFUSAL_REASONS.UNTRACKABLE,
|
|
6305
7116
|
message: "Tracked table deletion does not support captured markup or table property revisions."
|
|
@@ -6327,12 +7138,12 @@ const applyTableOp = (document, op) => {
|
|
|
6327
7138
|
}
|
|
6328
7139
|
case DOCUMENT_OP_TYPES.SET_CONTAINER_BLOCKS: {
|
|
6329
7140
|
const anchor = storyParagraphs(body).find(({ paragraph }) => idKey(paragraph.paraId ?? "") === idKey(op.blockId));
|
|
6330
|
-
if (anchor === void 0) return refuse$
|
|
7141
|
+
if (anchor === void 0) return refuse$4({
|
|
6331
7142
|
op,
|
|
6332
7143
|
reason: DOCUMENT_OP_REFUSAL_REASONS.BLOCK_NOT_FOUND,
|
|
6333
7144
|
message: "The structural inverse anchor does not exist."
|
|
6334
7145
|
});
|
|
6335
|
-
if (!equalForStaleness(blockListAt(body.content, anchor.list), op.expected)) return refuse$
|
|
7146
|
+
if (!equalForStaleness(blockListAt(body.content, anchor.list), op.expected)) return refuse$4({
|
|
6336
7147
|
op,
|
|
6337
7148
|
reason: DOCUMENT_OP_REFUSAL_REASONS.STALE,
|
|
6338
7149
|
message: "The container blocks have changed."
|
|
@@ -6584,6 +7395,297 @@ const applyRowOp = (document, op) => {
|
|
|
6584
7395
|
}
|
|
6585
7396
|
};
|
|
6586
7397
|
//#endregion
|
|
7398
|
+
//#region src/ops/packageResources.ts
|
|
7399
|
+
const partOf = (present, value) => {
|
|
7400
|
+
if (!present) return { type: "omitted" };
|
|
7401
|
+
return value === void 0 ? { type: "undefined" } : {
|
|
7402
|
+
type: "present",
|
|
7403
|
+
value: cloneModel(value)
|
|
7404
|
+
};
|
|
7405
|
+
};
|
|
7406
|
+
/** Capture style and numbering definitions as ordinary JSON data, preserving field presence. */
|
|
7407
|
+
const packageResourcesOf = ({ package: pkg }) => ({
|
|
7408
|
+
styles: partOf(Object.hasOwn(pkg, "styles"), pkg.styles),
|
|
7409
|
+
numbering: partOf(Object.hasOwn(pkg, "numbering"), pkg.numbering)
|
|
7410
|
+
});
|
|
7411
|
+
const valueOf = (part) => {
|
|
7412
|
+
switch (part.type) {
|
|
7413
|
+
case "omitted":
|
|
7414
|
+
case "undefined": return;
|
|
7415
|
+
case "present": return part.value;
|
|
7416
|
+
default: return part;
|
|
7417
|
+
}
|
|
7418
|
+
};
|
|
7419
|
+
/** Binary media travels as a byte array, rather than an ArrayBuffer that JSON discards. */
|
|
7420
|
+
const mediaRecordOf = (media) => Object.assign({}, media, { data: [...new Uint8Array(media.data)] });
|
|
7421
|
+
const mediaFileOf = (record) => Object.assign({}, record, { data: new Uint8Array(record.data).buffer });
|
|
7422
|
+
const presenceOf = (present, map) => {
|
|
7423
|
+
if (!present) return "omitted";
|
|
7424
|
+
return map === void 0 ? "undefined" : "present";
|
|
7425
|
+
};
|
|
7426
|
+
const packageMapOf = (present, map) => ({
|
|
7427
|
+
presence: presenceOf(present, map),
|
|
7428
|
+
entries: map ?? /* @__PURE__ */ new Map()
|
|
7429
|
+
});
|
|
7430
|
+
const relationshipsOf = ({ package: pkg }) => packageMapOf(Object.hasOwn(pkg, "relationships"), pkg.relationships);
|
|
7431
|
+
const mediaOf = ({ package: pkg }) => packageMapOf(Object.hasOwn(pkg, "media"), pkg.media);
|
|
7432
|
+
const entryOf = (stored, record) => stored === void 0 ? { type: "absent" } : {
|
|
7433
|
+
type: "present",
|
|
7434
|
+
value: record(stored)
|
|
7435
|
+
};
|
|
7436
|
+
const mapChangeOf = ({ before, after, record }) => {
|
|
7437
|
+
const entries = [];
|
|
7438
|
+
for (const key of /* @__PURE__ */ new Set([...before.entries.keys(), ...after.entries.keys()])) {
|
|
7439
|
+
const previous = before.entries.get(key);
|
|
7440
|
+
const next = after.entries.get(key);
|
|
7441
|
+
if (previous === next) continue;
|
|
7442
|
+
const change = {
|
|
7443
|
+
key,
|
|
7444
|
+
expected: entryOf(previous, record),
|
|
7445
|
+
next: entryOf(next, record)
|
|
7446
|
+
};
|
|
7447
|
+
if (!structurallyEqual(change.expected, change.next)) entries.push(change);
|
|
7448
|
+
}
|
|
7449
|
+
return {
|
|
7450
|
+
expected: before.presence,
|
|
7451
|
+
next: after.presence,
|
|
7452
|
+
entries
|
|
7453
|
+
};
|
|
7454
|
+
};
|
|
7455
|
+
/** Describe the package resource difference as one operation carrying only changed entries. */
|
|
7456
|
+
const packageResourcesOpOf = ({ before, after }) => ({
|
|
7457
|
+
type: DOCUMENT_OP_TYPES.SET_PACKAGE_RESOURCES,
|
|
7458
|
+
expected: packageResourcesOf(before),
|
|
7459
|
+
resources: packageResourcesOf(after),
|
|
7460
|
+
relationships: mapChangeOf({
|
|
7461
|
+
before: relationshipsOf(before),
|
|
7462
|
+
after: relationshipsOf(after),
|
|
7463
|
+
record: (relationship) => cloneModel(relationship)
|
|
7464
|
+
}),
|
|
7465
|
+
media: mapChangeOf({
|
|
7466
|
+
before: mediaOf(before),
|
|
7467
|
+
after: mediaOf(after),
|
|
7468
|
+
record: mediaRecordOf
|
|
7469
|
+
})
|
|
7470
|
+
});
|
|
7471
|
+
const applyMapChange = ({ current, change, record, stored }) => {
|
|
7472
|
+
if (current.presence !== change.expected) return {
|
|
7473
|
+
type: "refused",
|
|
7474
|
+
reason: DOCUMENT_OP_REFUSAL_REASONS.STALE,
|
|
7475
|
+
message: "The package resources changed."
|
|
7476
|
+
};
|
|
7477
|
+
if (duplicates(change.entries.map(({ key }) => key))) return {
|
|
7478
|
+
type: "refused",
|
|
7479
|
+
reason: DOCUMENT_OP_REFUSAL_REASONS.STRUCTURE_MISMATCH,
|
|
7480
|
+
message: "Package resource changes repeat a key."
|
|
7481
|
+
};
|
|
7482
|
+
const entries = new Map(current.entries);
|
|
7483
|
+
for (const { key, expected, next } of change.entries) {
|
|
7484
|
+
if (!structurallyEqual(entryOf(current.entries.get(key), record), expected)) return {
|
|
7485
|
+
type: "refused",
|
|
7486
|
+
reason: DOCUMENT_OP_REFUSAL_REASONS.STALE,
|
|
7487
|
+
message: "The package resources changed."
|
|
7488
|
+
};
|
|
7489
|
+
if (next.type === "present") entries.set(key, stored(next.value));
|
|
7490
|
+
else entries.delete(key);
|
|
7491
|
+
}
|
|
7492
|
+
if (change.next !== "present" && entries.size > 0) return {
|
|
7493
|
+
type: "refused",
|
|
7494
|
+
reason: DOCUMENT_OP_REFUSAL_REASONS.STRUCTURE_MISMATCH,
|
|
7495
|
+
message: "A removed package map still has entries."
|
|
7496
|
+
};
|
|
7497
|
+
return {
|
|
7498
|
+
type: "applied",
|
|
7499
|
+
map: {
|
|
7500
|
+
presence: change.next,
|
|
7501
|
+
entries
|
|
7502
|
+
}
|
|
7503
|
+
};
|
|
7504
|
+
};
|
|
7505
|
+
const inverseMapChange = (change) => ({
|
|
7506
|
+
expected: change.next,
|
|
7507
|
+
next: change.expected,
|
|
7508
|
+
entries: change.entries.map(({ key, expected, next }) => ({
|
|
7509
|
+
key,
|
|
7510
|
+
expected: next,
|
|
7511
|
+
next: expected
|
|
7512
|
+
}))
|
|
7513
|
+
});
|
|
7514
|
+
const presentEntries = (change) => change.entries.flatMap(({ key, next }) => next.type === "present" ? [[key, next.value]] : []);
|
|
7515
|
+
const duplicates = (ids) => new Set(ids).size !== ids.length;
|
|
7516
|
+
/** Validate the definitions and entries this operation adds or changes. */
|
|
7517
|
+
const resourceViolation = ({ op: { resources, relationships: relationshipChange, media: mediaChange }, expected, relationships, media }) => {
|
|
7518
|
+
const styles = valueOf(resources.styles);
|
|
7519
|
+
if (styles !== void 0 && duplicates(styles.styles.map(({ styleId }) => styleId))) return "Imported styles contain duplicate ids.";
|
|
7520
|
+
const numbering = valueOf(resources.numbering);
|
|
7521
|
+
if (numbering !== void 0) {
|
|
7522
|
+
const abstracts = numbering.abstractNums.map(({ abstractNumId }) => abstractNumId);
|
|
7523
|
+
const nums = numbering.nums.map(({ numId }) => numId);
|
|
7524
|
+
if (duplicates(abstracts) || duplicates(nums) || abstracts.some((id) => !Number.isSafeInteger(id) || id < 0 || id > 2147483647) || nums.some((id) => !Number.isSafeInteger(id) || id <= 0 || id > 2147483647)) return "Imported numbering contains invalid or duplicate ids.";
|
|
7525
|
+
if (numbering.nums.some(({ abstractNumId }) => !abstracts.includes(abstractNumId))) return "An imported numbering instance has no abstract definition.";
|
|
7526
|
+
}
|
|
7527
|
+
const changedRelationships = presentEntries(relationshipChange);
|
|
7528
|
+
if (changedRelationships.some(([key, relationship]) => key.length === 0 || key !== relationship.id)) return "Imported relationships contain invalid ids.";
|
|
7529
|
+
if (presentEntries(mediaChange).some(([key, file]) => key !== file.path || !key.startsWith("word/media/") || key.split("/").includes("..") || file.data.some((byte) => !Number.isInteger(byte) || byte < 0 || byte > 255))) return "Imported media contains invalid paths or bytes.";
|
|
7530
|
+
for (const [, relationship] of changedRelationships) {
|
|
7531
|
+
if (!relationship.type.endsWith("/image") || relationship.targetMode === "External") continue;
|
|
7532
|
+
const target = relationship.target.startsWith("/") ? relationship.target.slice(1) : `word/${relationship.target}`;
|
|
7533
|
+
if (!media.has(target)) return "An imported image relationship has no media part.";
|
|
7534
|
+
}
|
|
7535
|
+
const available = {
|
|
7536
|
+
style: new Set(styles?.styles.map(({ styleId }) => styleId) ?? []),
|
|
7537
|
+
numbering: new Set(numbering?.nums.map(({ numId }) => numId) ?? []),
|
|
7538
|
+
relationship: new Set(relationships.keys())
|
|
7539
|
+
};
|
|
7540
|
+
const unavailable = (kind, id) => !available[kind].has(id);
|
|
7541
|
+
const previousStyles = new Map(valueOf(expected.styles)?.styles.map((style) => [style.styleId, style]));
|
|
7542
|
+
for (const style of styles?.styles ?? []) {
|
|
7543
|
+
if (structurallyEqual(style, previousStyles.get(style.styleId))) continue;
|
|
7544
|
+
if (resourceReferencesMatch({
|
|
7545
|
+
value: style,
|
|
7546
|
+
matches: unavailable,
|
|
7547
|
+
relationshipScope: "main"
|
|
7548
|
+
})) return "An imported style references an unavailable package resource.";
|
|
7549
|
+
}
|
|
7550
|
+
const previousAbstracts = new Map(valueOf(expected.numbering)?.abstractNums.map((abstract) => [abstract.abstractNumId, abstract]));
|
|
7551
|
+
for (const abstract of numbering?.abstractNums ?? []) {
|
|
7552
|
+
if (structurallyEqual(abstract, previousAbstracts.get(abstract.abstractNumId))) continue;
|
|
7553
|
+
if (resourceReferencesMatch({
|
|
7554
|
+
value: abstract,
|
|
7555
|
+
matches: unavailable,
|
|
7556
|
+
relationshipScope: "main"
|
|
7557
|
+
})) return "An imported numbering definition references an unavailable style.";
|
|
7558
|
+
}
|
|
7559
|
+
const previousNums = new Map(valueOf(expected.numbering)?.nums.map((num) => [num.numId, num]));
|
|
7560
|
+
for (const num of numbering?.nums ?? []) {
|
|
7561
|
+
if (structurallyEqual(num, previousNums.get(num.numId))) continue;
|
|
7562
|
+
if (resourceReferencesMatch({
|
|
7563
|
+
value: num,
|
|
7564
|
+
matches: unavailable,
|
|
7565
|
+
relationshipScope: "main"
|
|
7566
|
+
})) return "An imported numbering instance references an unavailable style.";
|
|
7567
|
+
}
|
|
7568
|
+
};
|
|
7569
|
+
const referenceKinds = new Map(Object.entries({
|
|
7570
|
+
styleId: "style",
|
|
7571
|
+
basedOn: "style",
|
|
7572
|
+
next: "style",
|
|
7573
|
+
link: "style",
|
|
7574
|
+
pStyle: "style",
|
|
7575
|
+
numStyleLink: "style",
|
|
7576
|
+
styleLink: "style",
|
|
7577
|
+
numId: "numbering",
|
|
7578
|
+
rId: "relationship",
|
|
7579
|
+
hlinkRId: "relationship",
|
|
7580
|
+
imageRId: "relationship"
|
|
7581
|
+
}));
|
|
7582
|
+
const resourceReferencesMatch = ({ value, matches, relationshipScope }) => {
|
|
7583
|
+
if (typeof value !== "object" || value === null) return false;
|
|
7584
|
+
if (value instanceof Map) return [...value.values()].some((item) => resourceReferencesMatch({
|
|
7585
|
+
value: item,
|
|
7586
|
+
matches,
|
|
7587
|
+
relationshipScope
|
|
7588
|
+
}));
|
|
7589
|
+
if (Array.isArray(value)) return value.some((item) => resourceReferencesMatch({
|
|
7590
|
+
value: item,
|
|
7591
|
+
matches,
|
|
7592
|
+
relationshipScope
|
|
7593
|
+
}));
|
|
7594
|
+
return Object.entries(value).some(([key, item]) => {
|
|
7595
|
+
if (key === "listRendering") return false;
|
|
7596
|
+
const kind = referenceKinds.get(key);
|
|
7597
|
+
if (kind !== void 0 && (kind !== "relationship" || relationshipScope === "main") && (typeof item === "string" || typeof item === "number") && matches(kind, item)) return true;
|
|
7598
|
+
return resourceReferencesMatch({
|
|
7599
|
+
value: item,
|
|
7600
|
+
matches,
|
|
7601
|
+
relationshipScope: key === "headers" || key === "footers" ? "part" : relationshipScope
|
|
7602
|
+
});
|
|
7603
|
+
});
|
|
7604
|
+
};
|
|
7605
|
+
const missingIds = (before, after) => {
|
|
7606
|
+
const kept = new Set(after);
|
|
7607
|
+
return new Set(before.filter((id) => !kept.has(id)));
|
|
7608
|
+
};
|
|
7609
|
+
const applyPackageResourcesOp = (document, op) => {
|
|
7610
|
+
const expected = packageResourcesOf(document);
|
|
7611
|
+
const failed = (reason, message) => Result.err(new DocumentOpRefusal({
|
|
7612
|
+
reason,
|
|
7613
|
+
message,
|
|
7614
|
+
opType: op.type
|
|
7615
|
+
}));
|
|
7616
|
+
if (!structurallyEqual(expected, op.expected)) return failed(DOCUMENT_OP_REFUSAL_REASONS.STALE, "The package resources changed.");
|
|
7617
|
+
const owned = cloneModel(op);
|
|
7618
|
+
const relationships = applyMapChange({
|
|
7619
|
+
current: relationshipsOf(document),
|
|
7620
|
+
change: owned.relationships,
|
|
7621
|
+
record: (relationship) => cloneModel(relationship),
|
|
7622
|
+
stored: (relationship) => cloneModel(relationship)
|
|
7623
|
+
});
|
|
7624
|
+
if (relationships.type === "refused") return failed(relationships.reason, relationships.message);
|
|
7625
|
+
const media = applyMapChange({
|
|
7626
|
+
current: mediaOf(document),
|
|
7627
|
+
change: owned.media,
|
|
7628
|
+
record: mediaRecordOf,
|
|
7629
|
+
stored: mediaFileOf
|
|
7630
|
+
});
|
|
7631
|
+
if (media.type === "refused") return failed(media.reason, media.message);
|
|
7632
|
+
const violation = resourceViolation({
|
|
7633
|
+
op: owned,
|
|
7634
|
+
expected,
|
|
7635
|
+
relationships: relationships.map.entries,
|
|
7636
|
+
media: media.map.entries
|
|
7637
|
+
});
|
|
7638
|
+
if (violation !== void 0) return failed(DOCUMENT_OP_REFUSAL_REASONS.STRUCTURE_MISMATCH, violation);
|
|
7639
|
+
const { resources } = owned;
|
|
7640
|
+
const pkg = { ...document.package };
|
|
7641
|
+
delete pkg.styles;
|
|
7642
|
+
delete pkg.numbering;
|
|
7643
|
+
delete pkg.relationships;
|
|
7644
|
+
delete pkg.media;
|
|
7645
|
+
if (resources.styles.type !== "omitted") pkg.styles = valueOf(resources.styles);
|
|
7646
|
+
if (resources.numbering.type !== "omitted") pkg.numbering = valueOf(resources.numbering);
|
|
7647
|
+
if (relationships.map.presence !== "omitted") pkg.relationships = relationships.map.presence === "present" ? new Map(relationships.map.entries) : void 0;
|
|
7648
|
+
if (media.map.presence !== "omitted") pkg.media = media.map.presence === "present" ? new Map(media.map.entries) : void 0;
|
|
7649
|
+
const removed = {
|
|
7650
|
+
style: missingIds(document.package.styles?.styles.map(({ styleId }) => styleId) ?? [], pkg.styles?.styles.map(({ styleId }) => styleId) ?? []),
|
|
7651
|
+
numbering: missingIds(document.package.numbering?.nums.map(({ numId }) => numId) ?? [], pkg.numbering?.nums.map(({ numId }) => numId) ?? []),
|
|
7652
|
+
relationship: missingIds([...document.package.relationships?.keys() ?? []], [...pkg.relationships?.keys() ?? []])
|
|
7653
|
+
};
|
|
7654
|
+
if (resourceReferencesMatch({
|
|
7655
|
+
value: pkg,
|
|
7656
|
+
matches: (kind, id) => removed[kind].has(id),
|
|
7657
|
+
relationshipScope: "main"
|
|
7658
|
+
})) return failed(DOCUMENT_OP_REFUSAL_REASONS.STALE, "A removed package resource is still referenced.");
|
|
7659
|
+
if (structurallyEqual(expected, resources) && owned.relationships.expected === owned.relationships.next && owned.relationships.entries.length === 0 && owned.media.expected === owned.media.next && owned.media.entries.length === 0) return Result.ok({
|
|
7660
|
+
document,
|
|
7661
|
+
inverse: [],
|
|
7662
|
+
touched: {
|
|
7663
|
+
modified: [],
|
|
7664
|
+
inserted: [],
|
|
7665
|
+
removed: []
|
|
7666
|
+
}
|
|
7667
|
+
});
|
|
7668
|
+
const updated = {
|
|
7669
|
+
...document,
|
|
7670
|
+
package: pkg
|
|
7671
|
+
};
|
|
7672
|
+
return Result.ok({
|
|
7673
|
+
document: updated,
|
|
7674
|
+
inverse: [{
|
|
7675
|
+
type: DOCUMENT_OP_TYPES.SET_PACKAGE_RESOURCES,
|
|
7676
|
+
expected: packageResourcesOf(updated),
|
|
7677
|
+
resources: expected,
|
|
7678
|
+
relationships: inverseMapChange(owned.relationships),
|
|
7679
|
+
media: inverseMapChange(owned.media)
|
|
7680
|
+
}],
|
|
7681
|
+
touched: {
|
|
7682
|
+
modified: [],
|
|
7683
|
+
inserted: [],
|
|
7684
|
+
removed: []
|
|
7685
|
+
}
|
|
7686
|
+
});
|
|
7687
|
+
};
|
|
7688
|
+
//#endregion
|
|
6587
7689
|
//#region src/ops/numberingSections.ts
|
|
6588
7690
|
const failed = (op, reason, message) => Result.err(new DocumentOpRefusal({
|
|
6589
7691
|
message,
|
|
@@ -6880,12 +7982,12 @@ const CELL_VERTICAL_MERGES = {
|
|
|
6880
7982
|
RESTART: "restart",
|
|
6881
7983
|
CONTINUE: "continue"
|
|
6882
7984
|
};
|
|
6883
|
-
const refuse$
|
|
7985
|
+
const refuse$3 = ({ op, reason, message }) => Result.err(new DocumentOpRefusal({
|
|
6884
7986
|
opType: op.type,
|
|
6885
7987
|
reason,
|
|
6886
7988
|
message
|
|
6887
7989
|
}));
|
|
6888
|
-
const mismatch = (op, message) => refuse$
|
|
7990
|
+
const mismatch = (op, message) => refuse$3({
|
|
6889
7991
|
op,
|
|
6890
7992
|
reason: DOCUMENT_OP_REFUSAL_REASONS.STRUCTURE_MISMATCH,
|
|
6891
7993
|
message
|
|
@@ -6903,19 +8005,19 @@ const authored = (formatting) => {
|
|
|
6903
8005
|
return next;
|
|
6904
8006
|
};
|
|
6905
8007
|
const freshCells = ({ document, op, ids, count }) => {
|
|
6906
|
-
if (ids.length !== count) return refuse$
|
|
8008
|
+
if (ids.length !== count) return refuse$3({
|
|
6907
8009
|
op,
|
|
6908
8010
|
reason: DOCUMENT_OP_REFUSAL_REASONS.NEEDS_NEW_IDS,
|
|
6909
8011
|
message: `This edit requires ${count} fresh paragraph ids.`
|
|
6910
8012
|
});
|
|
6911
|
-
if (ids.some((id) => !isParaId(id))) return refuse$
|
|
8013
|
+
if (ids.some((id) => !isParaId(id))) return refuse$3({
|
|
6912
8014
|
op,
|
|
6913
8015
|
reason: DOCUMENT_OP_REFUSAL_REASONS.INVALID_BLOCK_ID,
|
|
6914
8016
|
message: "New table paragraphs need usable paragraph ids."
|
|
6915
8017
|
});
|
|
6916
8018
|
const occupied = new Set(packageParagraphIds(document.package).map(idKey));
|
|
6917
8019
|
for (const id of ids) {
|
|
6918
|
-
if (occupied.has(idKey(id))) return refuse$
|
|
8020
|
+
if (occupied.has(idKey(id))) return refuse$3({
|
|
6919
8021
|
op,
|
|
6920
8022
|
reason: DOCUMENT_OP_REFUSAL_REASONS.ID_COLLISION,
|
|
6921
8023
|
message: "A new table paragraph id is already in use."
|
|
@@ -6942,7 +8044,7 @@ const commitTable = ({ document, op, location, table }) => {
|
|
|
6942
8044
|
const afterIds = paragraphIdsIn(table);
|
|
6943
8045
|
const old = new Set(beforeIds.map(idKey));
|
|
6944
8046
|
const nextIds = new Set(afterIds.map(idKey));
|
|
6945
|
-
if (afterIds.some((id) => !old.has(idKey(id)) && !isParaId(id))) return refuse$
|
|
8047
|
+
if (afterIds.some((id) => !old.has(idKey(id)) && !isParaId(id))) return refuse$3({
|
|
6946
8048
|
op,
|
|
6947
8049
|
reason: DOCUMENT_OP_REFUSAL_REASONS.INVALID_BLOCK_ID,
|
|
6948
8050
|
message: "An added table paragraph has an invalid id."
|
|
@@ -6961,7 +8063,7 @@ const commitTable = ({ document, op, location, table }) => {
|
|
|
6961
8063
|
}
|
|
6962
8064
|
};
|
|
6963
8065
|
const valid = validateOpsDocument(next);
|
|
6964
|
-
if (valid.isErr()) return refuse$
|
|
8066
|
+
if (valid.isErr()) return refuse$3({
|
|
6965
8067
|
op,
|
|
6966
8068
|
reason: valid.error.reason,
|
|
6967
8069
|
message: valid.error.message
|
|
@@ -7037,17 +8139,17 @@ const stampsFor = (op, count) => {
|
|
|
7037
8139
|
const revision = op.revision;
|
|
7038
8140
|
if (revision === void 0) return Result.ok([]);
|
|
7039
8141
|
const ids = [revision.id, ...op.newIds?.revision ?? []];
|
|
7040
|
-
if (new Set(ids).size !== ids.length) return refuse$
|
|
8142
|
+
if (new Set(ids).size !== ids.length) return refuse$3({
|
|
7041
8143
|
op,
|
|
7042
8144
|
reason: DOCUMENT_OP_REFUSAL_REASONS.ID_COLLISION,
|
|
7043
8145
|
message: "Revision ids must be distinct within the operation."
|
|
7044
8146
|
});
|
|
7045
|
-
if (ids.slice(0, count).some((id) => !Number.isInteger(id) || id < 0 || id > 2147483647)) return refuse$
|
|
8147
|
+
if (ids.slice(0, count).some((id) => !Number.isInteger(id) || id < 0 || id > 2147483647)) return refuse$3({
|
|
7046
8148
|
op,
|
|
7047
8149
|
reason: DOCUMENT_OP_REFUSAL_REASONS.INVALID_NEW_ID,
|
|
7048
8150
|
message: "Revision ids must be nonnegative 31-bit integers."
|
|
7049
8151
|
});
|
|
7050
|
-
if (ids.length < count) return refuse$
|
|
8152
|
+
if (ids.length < count) return refuse$3({
|
|
7051
8153
|
op,
|
|
7052
8154
|
reason: DOCUMENT_OP_REFUSAL_REASONS.NEEDS_NEW_IDS,
|
|
7053
8155
|
message: `This table change needs ${count} distinct revision ids.`
|
|
@@ -7059,12 +8161,12 @@ const columnEdit = ({ document, op, table, grid }) => {
|
|
|
7059
8161
|
if (!Number.isSafeInteger(op.column) || op.column < 0 || op.column >= grid.width + (insert ? 1 : 0)) return mismatch(op, "Column is outside the logical grid.");
|
|
7060
8162
|
if (!insert && grid.width === 1) return mismatch(op, "Deleting the final column requires deleting the table.");
|
|
7061
8163
|
if (insert && (!Number.isSafeInteger(op.width) || op.width <= 0)) return mismatch(op, "A column width must be a positive twip count.");
|
|
7062
|
-
if (hasStructuralMarkup(table)) return refuse$
|
|
8164
|
+
if (hasStructuralMarkup(table)) return refuse$3({
|
|
7063
8165
|
op,
|
|
7064
8166
|
reason: DOCUMENT_OP_REFUSAL_REASONS.UNTRACKABLE,
|
|
7065
8167
|
message: "Indexed table markup must be relocated before changing columns."
|
|
7066
8168
|
});
|
|
7067
|
-
if (hasReview(table)) return refuse$
|
|
8169
|
+
if (hasReview(table)) return refuse$3({
|
|
7068
8170
|
op,
|
|
7069
8171
|
reason: DOCUMENT_OP_REFUSAL_REASONS.REVISION_CONFLICT,
|
|
7070
8172
|
message: "Resolve table reviews before changing its columns."
|
|
@@ -7081,7 +8183,7 @@ const columnEdit = ({ document, op, table, grid }) => {
|
|
|
7081
8183
|
count: boundaryRows
|
|
7082
8184
|
}) : Result.ok([]);
|
|
7083
8185
|
if (fresh.isErr()) return Result.err(fresh.error);
|
|
7084
|
-
if (op.revision !== void 0 && table.columnWidths === void 0) return refuse$
|
|
8186
|
+
if (op.revision !== void 0 && table.columnWidths === void 0) return refuse$3({
|
|
7085
8187
|
op,
|
|
7086
8188
|
reason: DOCUMENT_OP_REFUSAL_REASONS.UNTRACKABLE,
|
|
7087
8189
|
message: "Tracked column edits require an explicit previous grid."
|
|
@@ -7147,7 +8249,7 @@ const columnEdit = ({ document, op, table, grid }) => {
|
|
|
7147
8249
|
continue;
|
|
7148
8250
|
}
|
|
7149
8251
|
if (!insert && hit) {
|
|
7150
|
-
if (entry.end - entry.start === 1 && hasTableCellDependencies(entry.cell)) return refuse$
|
|
8252
|
+
if (entry.end - entry.start === 1 && hasTableCellDependencies(entry.cell)) return refuse$3({
|
|
7151
8253
|
op,
|
|
7152
8254
|
reason: DOCUMENT_OP_REFUSAL_REASONS.DEPENDENT_RECORDS,
|
|
7153
8255
|
message: "Resolve dependent anchors and review records before removing this cell."
|
|
@@ -7187,7 +8289,7 @@ const columnEdit = ({ document, op, table, grid }) => {
|
|
|
7187
8289
|
}
|
|
7188
8290
|
});
|
|
7189
8291
|
}
|
|
7190
|
-
if (!cells.some((cell) => cell.structuralChange?.type !== "tableCellDeletion")) return refuse$
|
|
8292
|
+
if (!cells.some((cell) => cell.structuralChange?.type !== "tableCellDeletion")) return refuse$3({
|
|
7191
8293
|
op,
|
|
7192
8294
|
reason: DOCUMENT_OP_REFUSAL_REASONS.TABLE_ROW_EMPTY,
|
|
7193
8295
|
message: "Deleting this column would leave a row without an active cell."
|
|
@@ -7240,12 +8342,12 @@ const mergeSelection = ({ table, grid, op }) => {
|
|
|
7240
8342
|
});
|
|
7241
8343
|
};
|
|
7242
8344
|
const merge = ({ document, op, table, grid }) => {
|
|
7243
|
-
if (hasStructuralMarkup(table)) return refuse$
|
|
8345
|
+
if (hasStructuralMarkup(table)) return refuse$3({
|
|
7244
8346
|
op,
|
|
7245
8347
|
reason: DOCUMENT_OP_REFUSAL_REASONS.UNTRACKABLE,
|
|
7246
8348
|
message: "Indexed table markup must be relocated before changing cells."
|
|
7247
8349
|
});
|
|
7248
|
-
if (hasReview(table)) return refuse$
|
|
8350
|
+
if (hasReview(table)) return refuse$3({
|
|
7249
8351
|
op,
|
|
7250
8352
|
reason: DOCUMENT_OP_REFUSAL_REASONS.REVISION_CONFLICT,
|
|
7251
8353
|
message: "Resolve table reviews before changing cells."
|
|
@@ -7302,7 +8404,7 @@ const merge = ({ document, op, table, grid }) => {
|
|
|
7302
8404
|
rows
|
|
7303
8405
|
});
|
|
7304
8406
|
}
|
|
7305
|
-
if (op.revision !== void 0) return refuse$
|
|
8407
|
+
if (op.revision !== void 0) return refuse$3({
|
|
7306
8408
|
op,
|
|
7307
8409
|
reason: DOCUMENT_OP_REFUSAL_REASONS.UNTRACKABLE,
|
|
7308
8410
|
message: "Cell merge markup cannot record horizontal content relocation; tracked moves are required."
|
|
@@ -7355,12 +8457,12 @@ const split = ({ document, op, table, grid, location }) => {
|
|
|
7355
8457
|
const target = grid.rows[location.rowIndex]?.find((entry) => entry.index === location.cellIndex);
|
|
7356
8458
|
if (!target) return mismatch(op, "The addressed cell is absent.");
|
|
7357
8459
|
if (target.row !== target.ownerRow || target.index !== target.ownerIndex) return mismatch(op, "A vertical merge continuation must be addressed through its owner cell.");
|
|
7358
|
-
if (hasStructuralMarkup(table)) return refuse$
|
|
8460
|
+
if (hasStructuralMarkup(table)) return refuse$3({
|
|
7359
8461
|
op,
|
|
7360
8462
|
reason: DOCUMENT_OP_REFUSAL_REASONS.UNTRACKABLE,
|
|
7361
8463
|
message: "Indexed table markup must be relocated before changing cells."
|
|
7362
8464
|
});
|
|
7363
|
-
if (hasReview(table)) return refuse$
|
|
8465
|
+
if (hasReview(table)) return refuse$3({
|
|
7364
8466
|
op,
|
|
7365
8467
|
reason: DOCUMENT_OP_REFUSAL_REASONS.REVISION_CONFLICT,
|
|
7366
8468
|
message: "Resolve table reviews before changing cells."
|
|
@@ -7368,7 +8470,7 @@ const split = ({ document, op, table, grid, location }) => {
|
|
|
7368
8470
|
const group = grid.rows.flatMap((row) => row.filter((cell) => cell.ownerRow === target.ownerRow && cell.ownerIndex === target.ownerIndex));
|
|
7369
8471
|
if (group.length > 1) return mismatch(op, "Splitting a cell inside a vertical merge is unsupported.");
|
|
7370
8472
|
const span = target.end - target.start;
|
|
7371
|
-
if (span === 1) return refuse$
|
|
8473
|
+
if (span === 1) return refuse$3({
|
|
7372
8474
|
op,
|
|
7373
8475
|
reason: DOCUMENT_OP_REFUSAL_REASONS.NO_CHANGE,
|
|
7374
8476
|
message: "A one-cell, one-column split would not change the table."
|
|
@@ -7467,7 +8569,7 @@ const tableEditParagraphDemand = (location, op) => {
|
|
|
7467
8569
|
const group = grid.value.rows.flatMap((row) => row.filter((cell) => cell.ownerRow === target.ownerRow && cell.ownerIndex === target.ownerIndex));
|
|
7468
8570
|
if (group.length > 1) return mismatch(op, "Splitting a cell inside a vertical merge is unsupported.");
|
|
7469
8571
|
const span = target.end - target.start;
|
|
7470
|
-
if (span === 1) return refuse$
|
|
8572
|
+
if (span === 1) return refuse$3({
|
|
7471
8573
|
op,
|
|
7472
8574
|
reason: DOCUMENT_OP_REFUSAL_REASONS.NO_CHANGE,
|
|
7473
8575
|
message: "A one-cell, one-column split would not change the table."
|
|
@@ -7485,7 +8587,7 @@ const applyTableEdit = (document, op) => {
|
|
|
7485
8587
|
const location = located.value;
|
|
7486
8588
|
const table = location.table;
|
|
7487
8589
|
if (op.type === DOCUMENT_OP_TYPES.SET_TABLE) {
|
|
7488
|
-
if (!equalForStaleness(table, op.expected)) return refuse$
|
|
8590
|
+
if (!equalForStaleness(table, op.expected)) return refuse$3({
|
|
7489
8591
|
op,
|
|
7490
8592
|
reason: DOCUMENT_OP_REFUSAL_REASONS.STALE,
|
|
7491
8593
|
message: "The table changed since this inverse was recorded."
|
|
@@ -7503,7 +8605,7 @@ const applyTableEdit = (document, op) => {
|
|
|
7503
8605
|
"propertyChanges",
|
|
7504
8606
|
"structuralChange"
|
|
7505
8607
|
].some((key) => Object.hasOwn(record, key) && Reflect.get(record, key) === void 0);
|
|
7506
|
-
if (ambiguous(table) || table.rows.some((row) => ambiguous(row) || row.cells.some(ambiguous))) return refuse$
|
|
8608
|
+
if (ambiguous(table) || table.rows.some((row) => ambiguous(row) || row.cells.some(ambiguous))) return refuse$3({
|
|
7507
8609
|
op,
|
|
7508
8610
|
reason: DOCUMENT_OP_REFUSAL_REASONS.UNTRACKABLE,
|
|
7509
8611
|
message: "Explicit undefined review fields have no persisted OOXML property snapshot."
|
|
@@ -7512,7 +8614,7 @@ const applyTableEdit = (document, op) => {
|
|
|
7512
8614
|
const grid = tableGrid(table, op.type);
|
|
7513
8615
|
if (grid.isErr()) return Result.err(grid.error);
|
|
7514
8616
|
if (op.type === DOCUMENT_OP_TYPES.DELETE_COLUMN && op.column === 0 && grid.value.width === 1) {
|
|
7515
|
-
if (table.rows.some((row) => row.cells.some(hasTableCellDependencies))) return refuse$
|
|
8617
|
+
if (table.rows.some((row) => row.cells.some(hasTableCellDependencies))) return refuse$3({
|
|
7516
8618
|
op,
|
|
7517
8619
|
reason: DOCUMENT_OP_REFUSAL_REASONS.DEPENDENT_RECORDS,
|
|
7518
8620
|
message: "Resolve dependent anchors and review records before removing the table."
|
|
@@ -7559,17 +8661,17 @@ const applyTableEdit = (document, op) => {
|
|
|
7559
8661
|
break;
|
|
7560
8662
|
case DOCUMENT_OP_TYPES.SET_TABLE_GRID: {
|
|
7561
8663
|
if (op.columnWidths.length !== grid.value.width || op.columnWidths.some((width) => !Number.isSafeInteger(width) || width <= 0)) return mismatch(op, "Column widths must match the grid and be positive twip counts.");
|
|
7562
|
-
if (table.formatting?.gridChange !== void 0) return refuse$
|
|
8664
|
+
if (table.formatting?.gridChange !== void 0) return refuse$3({
|
|
7563
8665
|
op,
|
|
7564
8666
|
reason: DOCUMENT_OP_REFUSAL_REASONS.REVISION_CONFLICT,
|
|
7565
8667
|
message: "Resolve the existing grid revision before resizing."
|
|
7566
8668
|
});
|
|
7567
|
-
if (op.revision !== void 0 && table.columnWidths === void 0) return refuse$
|
|
8669
|
+
if (op.revision !== void 0 && table.columnWidths === void 0) return refuse$3({
|
|
7568
8670
|
op,
|
|
7569
8671
|
reason: DOCUMENT_OP_REFUSAL_REASONS.UNTRACKABLE,
|
|
7570
8672
|
message: "A grid revision requires an explicit previous grid."
|
|
7571
8673
|
});
|
|
7572
|
-
if (table.propertyChanges !== void 0) return refuse$
|
|
8674
|
+
if (table.propertyChanges !== void 0) return refuse$3({
|
|
7573
8675
|
op,
|
|
7574
8676
|
reason: DOCUMENT_OP_REFUSAL_REASONS.REVISION_CONFLICT,
|
|
7575
8677
|
message: "Resolve the table property history before resizing its grid."
|
|
@@ -7606,7 +8708,7 @@ const applyTableEdit = (document, op) => {
|
|
|
7606
8708
|
"gridChange",
|
|
7607
8709
|
"preserved"
|
|
7608
8710
|
].includes(key))) return mismatch(op, "Grid and source provenance require a grid operation.");
|
|
7609
|
-
if (table.propertyChanges !== void 0) return refuse$
|
|
8711
|
+
if (table.propertyChanges !== void 0) return refuse$3({
|
|
7610
8712
|
op,
|
|
7611
8713
|
reason: DOCUMENT_OP_REFUSAL_REASONS.REVISION_CONFLICT,
|
|
7612
8714
|
message: "Resolve the existing table property revision."
|
|
@@ -7641,7 +8743,7 @@ const applyTableEdit = (document, op) => {
|
|
|
7641
8743
|
].includes(key))) return mismatch(op, "Omitted grid slots require a topology operation.");
|
|
7642
8744
|
const row = table.rows[location.rowIndex];
|
|
7643
8745
|
if (!row) return mismatch(op, "The addressed row is absent.");
|
|
7644
|
-
if (row.propertyChanges !== void 0) return refuse$
|
|
8746
|
+
if (row.propertyChanges !== void 0) return refuse$3({
|
|
7645
8747
|
op,
|
|
7646
8748
|
reason: DOCUMENT_OP_REFUSAL_REASONS.REVISION_CONFLICT,
|
|
7647
8749
|
message: "Resolve the existing row property revision."
|
|
@@ -7682,7 +8784,7 @@ const applyTableEdit = (document, op) => {
|
|
|
7682
8784
|
const row = table.rows[location.rowIndex];
|
|
7683
8785
|
const cell = row?.cells[location.cellIndex];
|
|
7684
8786
|
if (!row || !cell) return mismatch(op, "The addressed cell is absent.");
|
|
7685
|
-
if (cell.propertyChanges !== void 0) return refuse$
|
|
8787
|
+
if (cell.propertyChanges !== void 0) return refuse$3({
|
|
7686
8788
|
op,
|
|
7687
8789
|
reason: DOCUMENT_OP_REFUSAL_REASONS.REVISION_CONFLICT,
|
|
7688
8790
|
message: "Resolve the existing cell property revision."
|
|
@@ -7798,7 +8900,7 @@ const refusal$1 = (op, reason, message) => new DocumentOpRefusal({
|
|
|
7798
8900
|
reason,
|
|
7799
8901
|
opType: op?.type
|
|
7800
8902
|
});
|
|
7801
|
-
const refuse = (op, reason, message) => Result.err(refusal$1(op, reason, message));
|
|
8903
|
+
const refuse$2 = (op, reason, message) => Result.err(refusal$1(op, reason, message));
|
|
7802
8904
|
/**
|
|
7803
8905
|
* Whether a tracked operation's stamp can name the first record it creates:
|
|
7804
8906
|
* a revision id in range that no record in the package carries.
|
|
@@ -7831,8 +8933,6 @@ const reviewRestoring = (story, result, original) => ({
|
|
|
7831
8933
|
});
|
|
7832
8934
|
/** Characters `insertText` does not carry: each is an inline atom of its own. */
|
|
7833
8935
|
const NON_TEXT_CHARACTER_PATTERN = /[\t\n\r]/u;
|
|
7834
|
-
const isHighSurrogate = (code) => code >= 55296 && code <= 56319;
|
|
7835
|
-
const isLowSurrogate = (code) => code >= 56320 && code <= 57343;
|
|
7836
8936
|
/** The paragraph carrying an id, compared as hex; the contract makes it unique. */
|
|
7837
8937
|
const findParagraph = (op, paragraphs, blockId) => {
|
|
7838
8938
|
const key = idKey(blockId);
|
|
@@ -7840,30 +8940,6 @@ const findParagraph = (op, paragraphs, blockId) => {
|
|
|
7840
8940
|
return match === void 0 ? Result.err(refusal$1(op, DOCUMENT_OP_REFUSAL_REASONS.BLOCK_NOT_FOUND, `No paragraph is ${blockId}.`)) : Result.ok(match);
|
|
7841
8941
|
};
|
|
7842
8942
|
const locate = (document, op, story, blockId) => findParagraph(op, storyParagraphs(storyBody(document, story)), blockId);
|
|
7843
|
-
/** The gap a position names, or why it names none in this paragraph. */
|
|
7844
|
-
const resolveGap = (paragraph, position, fallback) => {
|
|
7845
|
-
const text = paragraphLogicalText(paragraph);
|
|
7846
|
-
const { offset, zeroWidthBefore } = position;
|
|
7847
|
-
if (!Number.isInteger(offset) || offset < 0 || offset > text.length) return DOCUMENT_OP_REFUSAL_REASONS.INVALID_OFFSET;
|
|
7848
|
-
if (offset > 0 && offset < text.length && isHighSurrogate(text.charCodeAt(offset - 1)) && isLowSurrogate(text.charCodeAt(offset))) return DOCUMENT_OP_REFUSAL_REASONS.SPLITS_SURROGATE_PAIR;
|
|
7849
|
-
const available = zeroWidthLeavesAt(paragraph.content, offset).length;
|
|
7850
|
-
if (zeroWidthBefore !== void 0) return Number.isInteger(zeroWidthBefore) && zeroWidthBefore >= 0 && zeroWidthBefore <= available ? {
|
|
7851
|
-
offset,
|
|
7852
|
-
zeroWidthBefore
|
|
7853
|
-
} : DOCUMENT_OP_REFUSAL_REASONS.INVALID_OFFSET;
|
|
7854
|
-
switch (fallback) {
|
|
7855
|
-
case "afterAll": return {
|
|
7856
|
-
offset,
|
|
7857
|
-
zeroWidthBefore: available
|
|
7858
|
-
};
|
|
7859
|
-
case "beforeAll": return {
|
|
7860
|
-
offset,
|
|
7861
|
-
zeroWidthBefore: 0
|
|
7862
|
-
};
|
|
7863
|
-
case "insertion": return defaultInsertionGap(paragraph.content, offset);
|
|
7864
|
-
default: return fallback;
|
|
7865
|
-
}
|
|
7866
|
-
};
|
|
7867
8943
|
const isGap = (value) => typeof value === "object";
|
|
7868
8944
|
/**
|
|
7869
8945
|
* A range of one paragraph. Absent `zeroWidthBefore` leaves the zero-width
|
|
@@ -7874,8 +8950,16 @@ const locateRange = (document, op, from, to) => {
|
|
|
7874
8950
|
const located = locate(document, op, from.story, from.blockId);
|
|
7875
8951
|
if (located.isErr()) return Result.err(located.error);
|
|
7876
8952
|
const { paragraph } = located.value;
|
|
7877
|
-
const start = resolveGap(
|
|
7878
|
-
|
|
8953
|
+
const start = resolveGap({
|
|
8954
|
+
paragraph,
|
|
8955
|
+
position: from,
|
|
8956
|
+
fallback: "afterAll"
|
|
8957
|
+
});
|
|
8958
|
+
const end = resolveGap({
|
|
8959
|
+
paragraph,
|
|
8960
|
+
position: to,
|
|
8961
|
+
fallback: "beforeAll"
|
|
8962
|
+
});
|
|
7879
8963
|
const invalid = (reason) => Result.err(refusal$1(op, reason, `[${from.offset}, ${to.offset}) is not a range of ${from.blockId}.`));
|
|
7880
8964
|
if (!isGap(start)) return invalid(start);
|
|
7881
8965
|
if (!isGap(end)) return invalid(end);
|
|
@@ -7893,7 +8977,11 @@ const locateRange = (document, op, from, to) => {
|
|
|
7893
8977
|
const locatePosition = (document, op, at, fallback) => {
|
|
7894
8978
|
const located = locate(document, op, at.story, at.blockId);
|
|
7895
8979
|
if (located.isErr()) return Result.err(located.error);
|
|
7896
|
-
const gap = resolveGap(
|
|
8980
|
+
const gap = resolveGap({
|
|
8981
|
+
paragraph: located.value.paragraph,
|
|
8982
|
+
position: at,
|
|
8983
|
+
fallback
|
|
8984
|
+
});
|
|
7897
8985
|
if (!isGap(gap)) return Result.err(refusal$1(op, gap, `${at.offset} is not a position in ${at.blockId}.`));
|
|
7898
8986
|
return Result.ok({
|
|
7899
8987
|
location: located.value,
|
|
@@ -8011,7 +9099,7 @@ const inserted = (options) => {
|
|
|
8011
9099
|
let { content } = options;
|
|
8012
9100
|
if (revision !== void 0) {
|
|
8013
9101
|
const inverse = insertionInverse(location.paragraph.content, content, start, end);
|
|
8014
|
-
if (inverse === void 0) return refuse(op, DOCUMENT_OP_REFUSAL_REASONS.STRUCTURE_MISMATCH, "The insertion would merge records that were separate.");
|
|
9102
|
+
if (inverse === void 0) return refuse$2(op, DOCUMENT_OP_REFUSAL_REASONS.STRUCTURE_MISMATCH, "The insertion would merge records that were separate.");
|
|
8015
9103
|
const wrapped = wrappedContent(op, {
|
|
8016
9104
|
items: content,
|
|
8017
9105
|
from: start,
|
|
@@ -8050,13 +9138,13 @@ const inserted = (options) => {
|
|
|
8050
9138
|
});
|
|
8051
9139
|
};
|
|
8052
9140
|
const insertText = (document, op) => {
|
|
8053
|
-
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.");
|
|
9141
|
+
if (op.text === "" || NON_TEXT_CHARACTER_PATTERN.test(op.text) || hasIllegalXmlCharacters(op.text)) return refuse$2(op, DOCUMENT_OP_REFUSAL_REASONS.INVALID_TEXT, "The text cannot be inserted.");
|
|
8054
9142
|
const located = locatePosition(document, op, op.at, "afterAll");
|
|
8055
9143
|
if (located.isErr()) return Result.err(located.error);
|
|
8056
9144
|
const { location } = located.value;
|
|
8057
9145
|
const { offset } = op.at;
|
|
8058
9146
|
const content = insertTextInContent(location.paragraph.content, offset, op.text, op.runProps);
|
|
8059
|
-
if (content === void 0) return refuse(op, DOCUMENT_OP_REFUSAL_REASONS.INSIDE_TRACKED_DELETION, `${offset} in ${op.at.blockId} is inside tracked-removed content.`);
|
|
9147
|
+
if (content === void 0) return refuse$2(op, DOCUMENT_OP_REFUSAL_REASONS.INSIDE_TRACKED_DELETION, `${offset} in ${op.at.blockId} is inside tracked-removed content.`);
|
|
8060
9148
|
const start = {
|
|
8061
9149
|
offset,
|
|
8062
9150
|
zeroWidthBefore: zeroWidthLeavesAt(content, offset).length
|
|
@@ -8083,16 +9171,16 @@ const insertContent = (document, op) => {
|
|
|
8083
9171
|
...op.slice,
|
|
8084
9172
|
content: cloneModel(op.slice.content)
|
|
8085
9173
|
};
|
|
8086
|
-
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.");
|
|
8087
|
-
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.");
|
|
8088
|
-
if (textsIn(slice.content).some(hasIllegalXmlCharacters)) return refuse(op, DOCUMENT_OP_REFUSAL_REASONS.INVALID_TEXT, "The slice holds text that cannot be written.");
|
|
9174
|
+
if (slice.content.length === 0 || holdsEmptyRecord(slice.content)) return refuse$2(op, DOCUMENT_OP_REFUSAL_REASONS.EMPTY_CONTENT, "The slice holds nothing, or an empty run, text node, or revision wrapper.");
|
|
9175
|
+
if (!Number.isInteger(slice.openStart) || !Number.isInteger(slice.openEnd) || slice.openStart < 0 || slice.openEnd < 0) return refuse$2(op, DOCUMENT_OP_REFUSAL_REASONS.STRUCTURE_MISMATCH, "Open depths are counts.");
|
|
9176
|
+
if (textsIn(slice.content).some(hasIllegalXmlCharacters)) return refuse$2(op, DOCUMENT_OP_REFUSAL_REASONS.INVALID_TEXT, "The slice holds text that cannot be written.");
|
|
8089
9177
|
const incoming = paragraphIdsIn(slice.content);
|
|
8090
|
-
if (incoming.length > 0 && collides(countIds(packageParagraphIds(document.package)), incoming)) return refuse(op, DOCUMENT_OP_REFUSAL_REASONS.ID_COLLISION, "The slice holds a paragraph id the package already uses.");
|
|
9178
|
+
if (incoming.length > 0 && collides(countIds(packageParagraphIds(document.package)), incoming)) return refuse$2(op, DOCUMENT_OP_REFUSAL_REASONS.ID_COLLISION, "The slice holds a paragraph id the package already uses.");
|
|
8091
9179
|
const located = locatePosition(document, op, op.at, "insertion");
|
|
8092
9180
|
if (located.isErr()) return Result.err(located.error);
|
|
8093
9181
|
const { location, gap } = located.value;
|
|
8094
9182
|
const content = insertSliceAt(location.paragraph.content, gap, slice);
|
|
8095
|
-
if (content === void 0) return refuse(op, DOCUMENT_OP_REFUSAL_REASONS.STRUCTURE_MISMATCH, `The slice's open ends do not fit ${op.at.offset} in ${op.at.blockId}.`);
|
|
9183
|
+
if (content === void 0) return refuse$2(op, DOCUMENT_OP_REFUSAL_REASONS.STRUCTURE_MISMATCH, `The slice's open ends do not fit ${op.at.offset} in ${op.at.blockId}.`);
|
|
8096
9184
|
return inserted({
|
|
8097
9185
|
document,
|
|
8098
9186
|
op,
|
|
@@ -8120,16 +9208,16 @@ const joinedContent = (op, joined, where) => {
|
|
|
8120
9208
|
const isCount = (value) => Number.isInteger(value) && value >= 0;
|
|
8121
9209
|
const deleteRange = (document, op) => {
|
|
8122
9210
|
const join = op.join ?? 0;
|
|
8123
|
-
if (!isCount(join)) return refuse(op, DOCUMENT_OP_REFUSAL_REASONS.STRUCTURE_MISMATCH, "A join depth is a count.");
|
|
8124
|
-
if (op.revision !== void 0 && join !== 0) return refuse(op, DOCUMENT_OP_REFUSAL_REASONS.STRUCTURE_MISMATCH, "A tracked deletion joins nothing.");
|
|
9211
|
+
if (!isCount(join)) return refuse$2(op, DOCUMENT_OP_REFUSAL_REASONS.STRUCTURE_MISMATCH, "A join depth is a count.");
|
|
9212
|
+
if (op.revision !== void 0 && join !== 0) return refuse$2(op, DOCUMENT_OP_REFUSAL_REASONS.STRUCTURE_MISMATCH, "A tracked deletion joins nothing.");
|
|
8125
9213
|
const located = locateRange(document, op, op.from, op.to);
|
|
8126
9214
|
if (located.isErr()) return Result.err(located.error);
|
|
8127
9215
|
const { story, location, from, to, empty } = located.value;
|
|
8128
9216
|
const { expected } = op;
|
|
8129
|
-
if (empty) return expected === void 0 || expected.content.length === 0 ? unchanged(document) : refuse(op, DOCUMENT_OP_REFUSAL_REASONS.STALE, "The range to delete is empty.");
|
|
9217
|
+
if (empty) return expected === void 0 || expected.content.length === 0 ? unchanged(document) : refuse$2(op, DOCUMENT_OP_REFUSAL_REASONS.STALE, "The range to delete is empty.");
|
|
8130
9218
|
const { paragraph } = location;
|
|
8131
9219
|
const { content: remaining, removed } = deleteBetween(paragraph.content, from, to);
|
|
8132
|
-
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.`);
|
|
9220
|
+
if (expected !== void 0 && !equalForStaleness(expected, removed)) return refuse$2(op, DOCUMENT_OP_REFUSAL_REASONS.STALE, `The range of ${op.from.blockId} holds other content than expected.`);
|
|
8133
9221
|
if (removed.content.length === 0) return unchanged(document);
|
|
8134
9222
|
if (op.revision !== void 0) {
|
|
8135
9223
|
const wrapped = wrappedContent(op, {
|
|
@@ -8180,12 +9268,12 @@ const deleteRange = (document, op) => {
|
|
|
8180
9268
|
});
|
|
8181
9269
|
};
|
|
8182
9270
|
const splitInline = (document, op) => {
|
|
8183
|
-
if (!isCount(op.depth) || op.depth === 0) return refuse(op, DOCUMENT_OP_REFUSAL_REASONS.STRUCTURE_MISMATCH, "A split cuts one level or more.");
|
|
9271
|
+
if (!isCount(op.depth) || op.depth === 0) return refuse$2(op, DOCUMENT_OP_REFUSAL_REASONS.STRUCTURE_MISMATCH, "A split cuts one level or more.");
|
|
8184
9272
|
const located = locatePosition(document, op, op.at, "beforeAll");
|
|
8185
9273
|
if (located.isErr()) return Result.err(located.error);
|
|
8186
9274
|
const { location, gap } = located.value;
|
|
8187
9275
|
const content = splitAt(location.paragraph.content, gap, op.depth);
|
|
8188
|
-
if (content === void 0) return refuse(op, DOCUMENT_OP_REFUSAL_REASONS.STRUCTURE_MISMATCH, `Fewer than ${op.depth} records run across ${op.at.offset} in ${op.at.blockId}.`);
|
|
9276
|
+
if (content === void 0) return refuse$2(op, DOCUMENT_OP_REFUSAL_REASONS.STRUCTURE_MISMATCH, `Fewer than ${op.depth} records run across ${op.at.offset} in ${op.at.blockId}.`);
|
|
8189
9277
|
return editOne({
|
|
8190
9278
|
document,
|
|
8191
9279
|
op,
|
|
@@ -8206,7 +9294,7 @@ const splitInline = (document, op) => {
|
|
|
8206
9294
|
});
|
|
8207
9295
|
};
|
|
8208
9296
|
const joinInline = (document, op) => {
|
|
8209
|
-
if (!isCount(op.depth) || op.depth === 0) return refuse(op, DOCUMENT_OP_REFUSAL_REASONS.STRUCTURE_MISMATCH, "A join merges one level or more.");
|
|
9297
|
+
if (!isCount(op.depth) || op.depth === 0) return refuse$2(op, DOCUMENT_OP_REFUSAL_REASONS.STRUCTURE_MISMATCH, "A join merges one level or more.");
|
|
8210
9298
|
const located = locatePosition(document, op, op.at, "beforeAll");
|
|
8211
9299
|
if (located.isErr()) return Result.err(located.error);
|
|
8212
9300
|
const { location, gap } = located.value;
|
|
@@ -8246,16 +9334,16 @@ const statesValues = (formatting, expected) => {
|
|
|
8246
9334
|
const setRunProps = (document, op) => {
|
|
8247
9335
|
const joinStart = op.joinStart ?? 0;
|
|
8248
9336
|
const joinEnd = op.joinEnd ?? 0;
|
|
8249
|
-
if (!isCount(joinStart) || !isCount(joinEnd)) return refuse(op, DOCUMENT_OP_REFUSAL_REASONS.STRUCTURE_MISMATCH, "A join depth is a count.");
|
|
9337
|
+
if (!isCount(joinStart) || !isCount(joinEnd)) return refuse$2(op, DOCUMENT_OP_REFUSAL_REASONS.STRUCTURE_MISMATCH, "A join depth is a count.");
|
|
8250
9338
|
const { revision } = op;
|
|
8251
|
-
if (revision !== void 0 && (joinStart !== 0 || joinEnd !== 0)) return refuse(op, DOCUMENT_OP_REFUSAL_REASONS.STRUCTURE_MISMATCH, "A tracked patch joins nothing.");
|
|
9339
|
+
if (revision !== void 0 && (joinStart !== 0 || joinEnd !== 0)) return refuse$2(op, DOCUMENT_OP_REFUSAL_REASONS.STRUCTURE_MISMATCH, "A tracked patch joins nothing.");
|
|
8252
9340
|
const located = locateRange(document, op, op.from, op.to);
|
|
8253
9341
|
if (located.isErr()) return Result.err(located.error);
|
|
8254
9342
|
const { story, location, from, to, empty } = located.value;
|
|
8255
9343
|
if (empty) return unchanged(document);
|
|
8256
9344
|
const { paragraph } = location;
|
|
8257
9345
|
const { expected } = op;
|
|
8258
|
-
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.`);
|
|
9346
|
+
if (expected !== void 0 && !runsBetween(paragraph.content, from, to).every((run) => statesValues(run.formatting, expected))) return refuse$2(op, DOCUMENT_OP_REFUSAL_REASONS.STALE, `The runs of ${op.from.blockId} state other values than expected.`);
|
|
8259
9347
|
if (revision !== void 0) {
|
|
8260
9348
|
const changes = /* @__PURE__ */ new Map();
|
|
8261
9349
|
const recordChange = (options) => {
|
|
@@ -8390,9 +9478,9 @@ const setParagraphProps = (document, op) => {
|
|
|
8390
9478
|
const located = locate(document, op, op.story, op.blockId);
|
|
8391
9479
|
if (located.isErr()) return Result.err(located.error);
|
|
8392
9480
|
const { paragraph } = located.value;
|
|
8393
|
-
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.`);
|
|
9481
|
+
if (op.expected !== void 0 && !statesValues(paragraph.formatting, op.expected)) return refuse$2(op, DOCUMENT_OP_REFUSAL_REASONS.STALE, `${op.blockId} states other paragraph properties than expected.`);
|
|
8394
9482
|
const { revision } = op;
|
|
8395
|
-
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.");
|
|
9483
|
+
if (revision !== void 0 && namesMarkFormatting(op.patch)) return refuse$2(op, DOCUMENT_OP_REFUSAL_REASONS.UNTRACKABLE, "A paragraph property change does not record the paragraph mark's run properties.");
|
|
8396
9484
|
const formatting = patchedSet(paragraph.formatting, op.patch, op.whenEmpty);
|
|
8397
9485
|
if (structurallyEqual(formatting, paragraph.formatting)) return unchanged(document);
|
|
8398
9486
|
if (revision !== void 0) {
|
|
@@ -8484,15 +9572,15 @@ const trackSplit = ({ op, stamp, paragraph, first, made, madeSecond, resolutionJ
|
|
|
8484
9572
|
}) : paragraphPropertyChange(stampInfo(stamp), paragraph.formatting)];
|
|
8485
9573
|
};
|
|
8486
9574
|
const splitBlock = (document, op) => {
|
|
8487
|
-
if (!isParaId(op.newBlockId)) return refuse(op, DOCUMENT_OP_REFUSAL_REASONS.INVALID_BLOCK_ID, `${op.newBlockId} is not a paragraph id.`);
|
|
8488
|
-
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.`);
|
|
8489
|
-
if (op.newHalf !== void 0 && !isSplitHalf(op.newHalf)) return refuse(op, DOCUMENT_OP_REFUSAL_REASONS.STRUCTURE_MISMATCH, "A half is first or second.");
|
|
9575
|
+
if (!isParaId(op.newBlockId)) return refuse$2(op, DOCUMENT_OP_REFUSAL_REASONS.INVALID_BLOCK_ID, `${op.newBlockId} is not a paragraph id.`);
|
|
9576
|
+
if (collides(countIds(packageParagraphIds(document.package)), [op.newBlockId])) return refuse$2(op, DOCUMENT_OP_REFUSAL_REASONS.ID_COLLISION, `${op.newBlockId} is already used in the package.`);
|
|
9577
|
+
if (op.newHalf !== void 0 && !isSplitHalf(op.newHalf)) return refuse$2(op, DOCUMENT_OP_REFUSAL_REASONS.STRUCTURE_MISMATCH, "A half is first or second.");
|
|
8490
9578
|
const located = locatePosition(document, op, op.at, "insertion");
|
|
8491
9579
|
if (located.isErr()) return Result.err(located.error);
|
|
8492
9580
|
const { location, gap } = located.value;
|
|
8493
9581
|
const { paragraph } = location;
|
|
8494
9582
|
const newHalf = op.newHalf ?? (gap.offset === paragraphLength(paragraph) ? SPLIT_HALVES.SECOND : SPLIT_HALVES.FIRST);
|
|
8495
|
-
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.");
|
|
9583
|
+
if (newHalf === SPLIT_HALVES.SECOND && (op.newParagraph?.propertyChanges?.length ?? 0) > 0) return refuse$2(op, DOCUMENT_OP_REFUSAL_REASONS.STRUCTURE_MISMATCH, "The second half takes the paragraph's property changes with its mark.");
|
|
8496
9584
|
const cut = cutAt(paragraph.content, gap);
|
|
8497
9585
|
const made = {
|
|
8498
9586
|
...op.newParagraph ?? (paragraph.formatting === void 0 ? {} : { formatting: paragraph.formatting }),
|
|
@@ -8594,10 +9682,10 @@ const splitBlock = (document, op) => {
|
|
|
8594
9682
|
*/
|
|
8595
9683
|
const trackJoin = ({ document, op, stamp, at, leading, trailing }) => {
|
|
8596
9684
|
const depth = op.depth ?? 0;
|
|
8597
|
-
if (!isCount(depth)) return refuse(op, DOCUMENT_OP_REFUSAL_REASONS.STRUCTURE_MISMATCH, "A join depth is a count.");
|
|
8598
|
-
if (depth !== 0) return refuse(op, DOCUMENT_OP_REFUSAL_REASONS.UNTRACKABLE, "A tracked paragraph mark cannot record an inline merge depth.");
|
|
8599
|
-
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.");
|
|
8600
|
-
if (leading.pPrMark !== void 0) return refuse(op, DOCUMENT_OP_REFUSAL_REASONS.REVISION_CONFLICT, `The mark of ${op.blockId} already carries a tracked change.`);
|
|
9685
|
+
if (!isCount(depth)) return refuse$2(op, DOCUMENT_OP_REFUSAL_REASONS.STRUCTURE_MISMATCH, "A join depth is a count.");
|
|
9686
|
+
if (depth !== 0) return refuse$2(op, DOCUMENT_OP_REFUSAL_REASONS.UNTRACKABLE, "A tracked paragraph mark cannot record an inline merge depth.");
|
|
9687
|
+
if ((op.survivor ?? SPLIT_HALVES.SECOND) !== SPLIT_HALVES.SECOND) return refuse$2(op, DOCUMENT_OP_REFUSAL_REASONS.STRUCTURE_MISMATCH, "Resolving a tracked join leaves the second paragraph.");
|
|
9688
|
+
if (leading.pPrMark !== void 0) return refuse$2(op, DOCUMENT_OP_REFUSAL_REASONS.REVISION_CONFLICT, `The mark of ${op.blockId} already carries a tracked change.`);
|
|
8601
9689
|
const first = {
|
|
8602
9690
|
...leading,
|
|
8603
9691
|
pPrMark: {
|
|
@@ -8640,12 +9728,12 @@ const joinBlocks = (document, op) => {
|
|
|
8640
9728
|
if (first.isErr()) return Result.err(first.error);
|
|
8641
9729
|
const next = findParagraph(op, paragraphs, op.nextBlockId);
|
|
8642
9730
|
if (next.isErr()) return Result.err(next.error);
|
|
8643
|
-
if (!sameBlockList(first.value.list, next.value.list) || next.value.index !== first.value.index + 1) return refuse(op, DOCUMENT_OP_REFUSAL_REASONS.NOT_ADJACENT, `${op.nextBlockId} does not directly follow ${op.blockId}.`);
|
|
9731
|
+
if (!sameBlockList(first.value.list, next.value.list) || next.value.index !== first.value.index + 1) return refuse$2(op, DOCUMENT_OP_REFUSAL_REASONS.NOT_ADJACENT, `${op.nextBlockId} does not directly follow ${op.blockId}.`);
|
|
8644
9732
|
const leading = first.value.paragraph;
|
|
8645
9733
|
const trailing = next.value.paragraph;
|
|
8646
|
-
if (leading.sectionProperties !== void 0 && op.sectionBoundary !== SECTION_BOUNDARY_POLICIES.REMOVE) return refuse(op, DOCUMENT_OP_REFUSAL_REASONS.SECTION_BOUNDARY, `${op.blockId} ends a section.`);
|
|
9734
|
+
if (leading.sectionProperties !== void 0 && op.sectionBoundary !== SECTION_BOUNDARY_POLICIES.REMOVE) return refuse$2(op, DOCUMENT_OP_REFUSAL_REASONS.SECTION_BOUNDARY, `${op.blockId} ends a section.`);
|
|
8647
9735
|
const survivorSide = op.survivor ?? SPLIT_HALVES.SECOND;
|
|
8648
|
-
if (!isSplitHalf(survivorSide)) return refuse(op, DOCUMENT_OP_REFUSAL_REASONS.STRUCTURE_MISMATCH, "A half is first or second.");
|
|
9736
|
+
if (!isSplitHalf(survivorSide)) return refuse$2(op, DOCUMENT_OP_REFUSAL_REASONS.STRUCTURE_MISMATCH, "A half is first or second.");
|
|
8649
9737
|
const survivor = survivorSide === SPLIT_HALVES.SECOND ? trailing : leading;
|
|
8650
9738
|
const retired = survivorSide === SPLIT_HALVES.SECOND ? leading : trailing;
|
|
8651
9739
|
if (op.expectedRetired !== void 0 && !equalForStaleness({
|
|
@@ -8654,8 +9742,8 @@ const joinBlocks = (document, op) => {
|
|
|
8654
9742
|
}, {
|
|
8655
9743
|
type: "paragraph",
|
|
8656
9744
|
...op.expectedRetired
|
|
8657
|
-
})) return refuse(op, DOCUMENT_OP_REFUSAL_REASONS.STALE, `${retired.paraId ?? ""} states other fields than expected.`);
|
|
8658
|
-
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.`);
|
|
9745
|
+
})) return refuse$2(op, DOCUMENT_OP_REFUSAL_REASONS.STALE, `${retired.paraId ?? ""} states other fields than expected.`);
|
|
9746
|
+
if (op.expectedSurvivor !== void 0 && !equalForStaleness(reviewFieldsOf(survivor), op.expectedSurvivor)) return refuse$2(op, DOCUMENT_OP_REFUSAL_REASONS.STALE, `${survivor.paraId ?? ""} states other review fields than expected.`);
|
|
8659
9747
|
if (op.revision !== void 0) return trackJoin({
|
|
8660
9748
|
document,
|
|
8661
9749
|
op,
|
|
@@ -8665,9 +9753,9 @@ const joinBlocks = (document, op) => {
|
|
|
8665
9753
|
trailing
|
|
8666
9754
|
});
|
|
8667
9755
|
const retiredId = retired.paraId ?? "";
|
|
8668
|
-
if (!isParaId(retiredId)) return refuse(op, DOCUMENT_OP_REFUSAL_REASONS.INVALID_BLOCK_ID, `${retiredId} could not name the paragraph again.`);
|
|
9756
|
+
if (!isParaId(retiredId)) return refuse$2(op, DOCUMENT_OP_REFUSAL_REASONS.INVALID_BLOCK_ID, `${retiredId} could not name the paragraph again.`);
|
|
8669
9757
|
const depth = op.depth ?? 0;
|
|
8670
|
-
if (!isCount(depth)) return refuse(op, DOCUMENT_OP_REFUSAL_REASONS.STRUCTURE_MISMATCH, "A join depth is a count.");
|
|
9758
|
+
if (!isCount(depth)) return refuse$2(op, DOCUMENT_OP_REFUSAL_REASONS.STRUCTURE_MISMATCH, "A join depth is a count.");
|
|
8671
9759
|
const merged = joinedContent(op, joinContent(leading.content, trailing.content, depth), `between ${op.blockId} and ${op.nextBlockId}`);
|
|
8672
9760
|
if (merged.isErr()) return Result.err(merged.error);
|
|
8673
9761
|
const joined = withReviewFields({
|
|
@@ -8766,32 +9854,32 @@ const sameSectionBreaks = (before, after) => {
|
|
|
8766
9854
|
};
|
|
8767
9855
|
const replaceBlocks = (document, op) => {
|
|
8768
9856
|
const [firstExpected] = op.expected;
|
|
8769
|
-
if (firstExpected === void 0 || op.blocks.length === 0) return refuse(op, DOCUMENT_OP_REFUSAL_REASONS.EMPTY_BLOCK_LIST, "A replacement names the paragraphs it replaces and the ones it puts in their place.");
|
|
9857
|
+
if (firstExpected === void 0 || op.blocks.length === 0) return refuse$2(op, DOCUMENT_OP_REFUSAL_REASONS.EMPTY_BLOCK_LIST, "A replacement names the paragraphs it replaces and the ones it puts in their place.");
|
|
8770
9858
|
const paragraphs = storyParagraphs(storyBody(document, op.story));
|
|
8771
9859
|
const found = [];
|
|
8772
9860
|
for (const expected of op.expected) {
|
|
8773
|
-
if (expected.paraId === void 0) return refuse(op, DOCUMENT_OP_REFUSAL_REASONS.BLOCK_NOT_FOUND, "A replaced paragraph has no id.");
|
|
9861
|
+
if (expected.paraId === void 0) return refuse$2(op, DOCUMENT_OP_REFUSAL_REASONS.BLOCK_NOT_FOUND, "A replaced paragraph has no id.");
|
|
8774
9862
|
const located = findParagraph(op, paragraphs, expected.paraId);
|
|
8775
9863
|
if (located.isErr()) return Result.err(located.error);
|
|
8776
9864
|
found.push(located.value);
|
|
8777
9865
|
}
|
|
8778
9866
|
const [start] = found;
|
|
8779
|
-
if (start === void 0) return refuse(op, DOCUMENT_OP_REFUSAL_REASONS.EMPTY_BLOCK_LIST, "Nothing to replace.");
|
|
8780
|
-
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.");
|
|
8781
|
-
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.");
|
|
8782
|
-
if (op.blocks.some(({ paraId }) => paraId === void 0)) return refuse(op, DOCUMENT_OP_REFUSAL_REASONS.INVALID_BLOCK_ID, "A replacement paragraph has no id.");
|
|
8783
|
-
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.");
|
|
9867
|
+
if (start === void 0) return refuse$2(op, DOCUMENT_OP_REFUSAL_REASONS.EMPTY_BLOCK_LIST, "Nothing to replace.");
|
|
9868
|
+
if (!found.every((location, offset) => sameBlockList(location.list, start.list) && location.index === start.index + offset)) return refuse$2(op, DOCUMENT_OP_REFUSAL_REASONS.NOT_ADJACENT, "The replaced paragraphs are not adjacent.");
|
|
9869
|
+
if (found.some((location, offset) => !equalForStaleness(location.paragraph, op.expected[offset]))) return refuse$2(op, DOCUMENT_OP_REFUSAL_REASONS.STALE, "The paragraphs to replace have changed.");
|
|
9870
|
+
if (op.blocks.some(({ paraId }) => paraId === void 0)) return refuse$2(op, DOCUMENT_OP_REFUSAL_REASONS.INVALID_BLOCK_ID, "A replacement paragraph has no id.");
|
|
9871
|
+
if (op.blocks.some(({ content }) => holdsEmptyRecord(content))) return refuse$2(op, DOCUMENT_OP_REFUSAL_REASONS.EMPTY_CONTENT, "A replacement paragraph holds an empty run, text node, or revision wrapper.");
|
|
8784
9872
|
const before = found.map(({ paragraph }) => paragraph);
|
|
8785
9873
|
const kept = new Set(before.map(({ paraId }) => idKey(paraId ?? "")));
|
|
8786
9874
|
const placed = new Set(op.blocks.map(({ paraId }) => idKey(paraId ?? "")));
|
|
8787
|
-
if ([...op.blocks.filter(({ paraId }) => !kept.has(idKey(paraId ?? ""))), ...before.filter(({ paraId }) => !placed.has(idKey(paraId ?? "")))].some(({ paraId }) => paraId === void 0 || !isParaId(paraId))) return refuse(op, DOCUMENT_OP_REFUSAL_REASONS.INVALID_BLOCK_ID, "A paragraph the replacement adds or removes has no usable id.");
|
|
8788
|
-
if (op.sectionBoundaries !== SECTION_BOUNDARY_POLICIES.REPLACE && !sameSectionBreaks(before, op.blocks)) return refuse(op, DOCUMENT_OP_REFUSAL_REASONS.SECTION_BOUNDARY, "A replacement keeps every section break where it is.");
|
|
9875
|
+
if ([...op.blocks.filter(({ paraId }) => !kept.has(idKey(paraId ?? ""))), ...before.filter(({ paraId }) => !placed.has(idKey(paraId ?? "")))].some(({ paraId }) => paraId === void 0 || !isParaId(paraId))) return refuse$2(op, DOCUMENT_OP_REFUSAL_REASONS.INVALID_BLOCK_ID, "A paragraph the replacement adds or removes has no usable id.");
|
|
9876
|
+
if (op.sectionBoundaries !== SECTION_BOUNDARY_POLICIES.REPLACE && !sameSectionBreaks(before, op.blocks)) return refuse$2(op, DOCUMENT_OP_REFUSAL_REASONS.SECTION_BOUNDARY, "A replacement keeps every section break where it is.");
|
|
8789
9877
|
const remaining = countIds(packageParagraphIds(document.package));
|
|
8790
9878
|
for (const id of paragraphIdsIn(before)) remaining.set(idKey(id), (remaining.get(idKey(id)) ?? 1) - 1);
|
|
8791
|
-
if (collides(remaining, paragraphIdsIn(op.blocks))) return refuse(op, DOCUMENT_OP_REFUSAL_REASONS.ID_COLLISION, "A replacement paragraph id is already used in the package.");
|
|
9879
|
+
if (collides(remaining, paragraphIdsIn(op.blocks))) return refuse$2(op, DOCUMENT_OP_REFUSAL_REASONS.ID_COLLISION, "A replacement paragraph id is already used in the package.");
|
|
8792
9880
|
const outside = identitiesOutside(document, before);
|
|
8793
9881
|
const incomingRecords = identityKeysIn(op.blocks);
|
|
8794
|
-
if (incomingRecords.some((key) => outside.has(key)) || new Set(incomingRecords).size !== incomingRecords.length) return refuse(op, DOCUMENT_OP_REFUSAL_REASONS.ID_COLLISION, "A replacement carries a revision or content-control id already used in the package.");
|
|
9882
|
+
if (incomingRecords.some((key) => outside.has(key)) || new Set(incomingRecords).size !== incomingRecords.length) return refuse$2(op, DOCUMENT_OP_REFUSAL_REASONS.ID_COLLISION, "A replacement carries a revision or content-control id already used in the package.");
|
|
8795
9883
|
const replaced = replaceParagraphs({
|
|
8796
9884
|
document,
|
|
8797
9885
|
story: op.story,
|
|
@@ -8822,15 +9910,15 @@ const setParagraphReview = (document, op) => {
|
|
|
8822
9910
|
const located = locate(document, op, op.story, op.blockId);
|
|
8823
9911
|
if (located.isErr()) return Result.err(located.error);
|
|
8824
9912
|
const { paragraph } = located.value;
|
|
8825
|
-
if (!equalForStaleness(reviewFieldsOf(paragraph), op.expected)) return refuse(op, DOCUMENT_OP_REFUSAL_REASONS.STALE, `${op.blockId} states other review fields than expected.`);
|
|
9913
|
+
if (!equalForStaleness(reviewFieldsOf(paragraph), op.expected)) return refuse$2(op, DOCUMENT_OP_REFUSAL_REASONS.STALE, `${op.blockId} states other review fields than expected.`);
|
|
8826
9914
|
const review = cloneModel(op.review);
|
|
8827
9915
|
const next = withReviewFields(paragraph, review);
|
|
8828
9916
|
if (structurallyEqual(next, paragraph)) return unchanged(document);
|
|
8829
|
-
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.`);
|
|
9917
|
+
if (review.pPrMark !== void 0 && !structurallyEqual(review.pPrMark, paragraph.pPrMark) && endsItsContainer(storyBody(document, op.story), located.value) && !(located.value.list.some((step) => step.kind === "tableCell") && (review.pPrMark.kind === "ins" || review.pPrMark.kind === "del"))) return refuse$2(op, DOCUMENT_OP_REFUSAL_REASONS.CONTAINER_FINAL_MARK, `${op.blockId} ends its container: there is no next paragraph its mark could join.`);
|
|
8830
9918
|
const taken = identitiesOutside(document, [paragraph]);
|
|
8831
9919
|
for (const key of identityKeysIn(paragraph.content)) taken.add(key);
|
|
8832
9920
|
const incoming = reviewKeysOf(review);
|
|
8833
|
-
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.");
|
|
9921
|
+
if (incoming.some((key) => taken.has(key)) || new Set(incoming).size !== incoming.length) return refuse$2(op, DOCUMENT_OP_REFUSAL_REASONS.ID_COLLISION, "The review fields carry a revision id already used in the package.");
|
|
8834
9922
|
const replaced = replaceParagraphs({
|
|
8835
9923
|
document,
|
|
8836
9924
|
story: op.story,
|
|
@@ -8849,18 +9937,18 @@ const replaceInline = (document, op) => {
|
|
|
8849
9937
|
const located = locate(document, op, op.story, op.blockId);
|
|
8850
9938
|
if (located.isErr()) return Result.err(located.error);
|
|
8851
9939
|
const { paragraph } = located.value;
|
|
8852
|
-
if (!equalForStaleness(paragraph.content, op.expected)) return refuse(op, DOCUMENT_OP_REFUSAL_REASONS.STALE, `${op.blockId} holds other content than expected.`);
|
|
9940
|
+
if (!equalForStaleness(paragraph.content, op.expected)) return refuse$2(op, DOCUMENT_OP_REFUSAL_REASONS.STALE, `${op.blockId} holds other content than expected.`);
|
|
8853
9941
|
const content = cloneModel(op.content);
|
|
8854
|
-
if (holdsEmptyRecord(content)) return refuse(op, DOCUMENT_OP_REFUSAL_REASONS.EMPTY_CONTENT, "The content holds an empty run, text node, or revision wrapper.");
|
|
8855
|
-
if (textsIn(content).some(hasIllegalXmlCharacters)) return refuse(op, DOCUMENT_OP_REFUSAL_REASONS.INVALID_TEXT, "The content holds text that cannot be written.");
|
|
9942
|
+
if (holdsEmptyRecord(content)) return refuse$2(op, DOCUMENT_OP_REFUSAL_REASONS.EMPTY_CONTENT, "The content holds an empty run, text node, or revision wrapper.");
|
|
9943
|
+
if (textsIn(content).some(hasIllegalXmlCharacters)) return refuse$2(op, DOCUMENT_OP_REFUSAL_REASONS.INVALID_TEXT, "The content holds text that cannot be written.");
|
|
8856
9944
|
if (structurallyEqual(content, paragraph.content)) return unchanged(document);
|
|
8857
9945
|
const remaining = countIds(packageParagraphIds(document.package));
|
|
8858
9946
|
for (const id of paragraphIdsIn(paragraph.content)) remaining.set(idKey(id), (remaining.get(idKey(id)) ?? 1) - 1);
|
|
8859
|
-
if (collides(remaining, paragraphIdsIn(content))) return refuse(op, DOCUMENT_OP_REFUSAL_REASONS.ID_COLLISION, "The content holds a paragraph id the package already uses.");
|
|
9947
|
+
if (collides(remaining, paragraphIdsIn(content))) return refuse$2(op, DOCUMENT_OP_REFUSAL_REASONS.ID_COLLISION, "The content holds a paragraph id the package already uses.");
|
|
8860
9948
|
const taken = identitiesOutside(document, [paragraph]);
|
|
8861
9949
|
for (const key of reviewKeysOf(reviewFieldsOf(paragraph))) taken.add(key);
|
|
8862
9950
|
const incoming = identityKeysIn(content);
|
|
8863
|
-
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.");
|
|
9951
|
+
if (incoming.some((key) => taken.has(key)) || new Set(incoming).size !== incoming.length) return refuse$2(op, DOCUMENT_OP_REFUSAL_REASONS.ID_COLLISION, "The content carries a revision or content-control id already used in the package.");
|
|
8864
9952
|
const next = withContent(paragraph, content);
|
|
8865
9953
|
const replaced = replaceParagraphs({
|
|
8866
9954
|
document,
|
|
@@ -8879,15 +9967,15 @@ const replaceInline = (document, op) => {
|
|
|
8879
9967
|
const editNote = (document, op) => {
|
|
8880
9968
|
const kind = op.type === DOCUMENT_OP_TYPES.ADD_NOTE ? op.note.type : op.story.kind;
|
|
8881
9969
|
const id = op.type === DOCUMENT_OP_TYPES.ADD_NOTE ? op.note.id : op.story.id;
|
|
8882
|
-
if (!Number.isInteger(id) || id <= 0) return refuse(op, DOCUMENT_OP_REFUSAL_REASONS.INVALID_NEW_ID, "A normal note needs a positive integer id.");
|
|
9970
|
+
if (!Number.isInteger(id) || id <= 0) return refuse$2(op, DOCUMENT_OP_REFUSAL_REASONS.INVALID_NEW_ID, "A normal note needs a positive integer id.");
|
|
8883
9971
|
const collection = kind === "footnote" ? document.package.footnotes : document.package.endnotes;
|
|
8884
9972
|
if (op.type === DOCUMENT_OP_TYPES.ADD_NOTE) {
|
|
8885
|
-
if (collection?.some((note) => note.id === id)) return refuse(op, DOCUMENT_OP_REFUSAL_REASONS.ID_COLLISION, "The note id is already used.");
|
|
8886
|
-
if (op.note.noteType !== void 0 && op.note.noteType !== "normal") return refuse(op, DOCUMENT_OP_REFUSAL_REASONS.STRUCTURE_MISMATCH, "A reference can only create a normal note.");
|
|
8887
|
-
if (op.note.content.length === 0) return refuse(op, DOCUMENT_OP_REFUSAL_REASONS.EMPTY_CONTENT, "A note needs an addressable paragraph.");
|
|
9973
|
+
if (collection?.some((note) => note.id === id)) return refuse$2(op, DOCUMENT_OP_REFUSAL_REASONS.ID_COLLISION, "The note id is already used.");
|
|
9974
|
+
if (op.note.noteType !== void 0 && op.note.noteType !== "normal") return refuse$2(op, DOCUMENT_OP_REFUSAL_REASONS.STRUCTURE_MISMATCH, "A reference can only create a normal note.");
|
|
9975
|
+
if (op.note.content.length === 0) return refuse$2(op, DOCUMENT_OP_REFUSAL_REASONS.EMPTY_CONTENT, "A note needs an addressable paragraph.");
|
|
8888
9976
|
const note = cloneModel(op.note);
|
|
8889
9977
|
const markers = storyParagraphs({ content: note.content }).flatMap(({ paragraph }) => leafSpans(paragraph.content)).filter(({ node }) => node.type === "noteMarker");
|
|
8890
|
-
if (markers.length > 1 || markers.some(({ node }) => node.type === "noteMarker" && node.kind !== kind)) return refuse(op, DOCUMENT_OP_REFUSAL_REASONS.STRUCTURE_MISMATCH, "A note needs one matching automatic reference mark.");
|
|
9978
|
+
if (markers.length > 1 || markers.some(({ node }) => node.type === "noteMarker" && node.kind !== kind)) return refuse$2(op, DOCUMENT_OP_REFUSAL_REASONS.STRUCTURE_MISMATCH, "A note needs one matching automatic reference mark.");
|
|
8891
9979
|
note.content = noteContentWithAutomaticMark({
|
|
8892
9980
|
note,
|
|
8893
9981
|
customMark: false
|
|
@@ -8906,7 +9994,7 @@ const editNote = (document, op) => {
|
|
|
8906
9994
|
}
|
|
8907
9995
|
};
|
|
8908
9996
|
const valid = validateOpsDocument(withNote);
|
|
8909
|
-
if (valid.isErr()) return refuse(op, valid.error.reason, valid.error.message);
|
|
9997
|
+
if (valid.isErr()) return refuse$2(op, valid.error.reason, valid.error.message);
|
|
8910
9998
|
const reference = insertContent(withNote, {
|
|
8911
9999
|
type: DOCUMENT_OP_TYPES.INSERT_CONTENT,
|
|
8912
10000
|
at: op.at,
|
|
@@ -8925,13 +10013,13 @@ const editNote = (document, op) => {
|
|
|
8925
10013
|
if (reference.isErr()) return reference;
|
|
8926
10014
|
return storyLifecycleEdit(document, reference.value.document, op);
|
|
8927
10015
|
}
|
|
8928
|
-
if (!collection?.find((candidate) => candidate.id === id)) return refuse(op, DOCUMENT_OP_REFUSAL_REASONS.BLOCK_NOT_FOUND, "The note does not exist.");
|
|
10016
|
+
if (!collection?.find((candidate) => candidate.id === id)) return refuse$2(op, DOCUMENT_OP_REFUSAL_REASONS.BLOCK_NOT_FOUND, "The note does not exist.");
|
|
8929
10017
|
const located = locateRange(document, op, op.at, {
|
|
8930
10018
|
...op.at,
|
|
8931
10019
|
offset: op.at.offset + 1
|
|
8932
10020
|
});
|
|
8933
10021
|
if (located.isErr()) return Result.err(located.error);
|
|
8934
|
-
if (!leafSpans(located.value.location.paragraph.content).some(({ node, before, after }) => before.offset === op.at.offset && after.offset === op.at.offset + 1 && (kind === "footnote" && node.type === "footnoteRef" || kind === "endnote" && node.type === "endnoteRef") && node.id === id)) return refuse(op, DOCUMENT_OP_REFUSAL_REASONS.STRUCTURE_MISMATCH, "The position does not name the note reference.");
|
|
10022
|
+
if (!leafSpans(located.value.location.paragraph.content).some(({ node, before, after }) => before.offset === op.at.offset && after.offset === op.at.offset + 1 && (kind === "footnote" && node.type === "footnoteRef" || kind === "endnote" && node.type === "endnoteRef") && node.id === id)) return refuse$2(op, DOCUMENT_OP_REFUSAL_REASONS.STRUCTURE_MISMATCH, "The position does not name the note reference.");
|
|
8935
10023
|
const removed = deleteRange(document, {
|
|
8936
10024
|
type: DOCUMENT_OP_TYPES.DELETE_RANGE,
|
|
8937
10025
|
from: op.at,
|
|
@@ -8944,7 +10032,7 @@ const editNote = (document, op) => {
|
|
|
8944
10032
|
if (documentStories(removed.value.document).some((story) => {
|
|
8945
10033
|
if (typeof story !== "string" && story.kind === kind && "id" in story && story.id === id) return false;
|
|
8946
10034
|
return storyParagraphs(storyBody(removed.value.document, story)).some(({ paragraph }) => leafSpans(paragraph.content).some(({ node }) => (kind === "footnote" && node.type === "footnoteRef" || kind === "endnote" && node.type === "endnoteRef") && node.id === id));
|
|
8947
|
-
})) return refuse(op, DOCUMENT_OP_REFUSAL_REASONS.STRUCTURE_MISMATCH, "Another reference still owns this note.");
|
|
10035
|
+
})) return refuse$2(op, DOCUMENT_OP_REFUSAL_REASONS.STRUCTURE_MISMATCH, "Another reference still owns this note.");
|
|
8948
10036
|
return storyLifecycleEdit(document, kind === "footnote" ? {
|
|
8949
10037
|
...removed.value.document,
|
|
8950
10038
|
package: {
|
|
@@ -8961,6 +10049,11 @@ const editNote = (document, op) => {
|
|
|
8961
10049
|
};
|
|
8962
10050
|
const dispatch = (document, op) => {
|
|
8963
10051
|
switch (op.type) {
|
|
10052
|
+
case DOCUMENT_OP_TYPES.CREATE_COMMENT:
|
|
10053
|
+
case DOCUMENT_OP_TYPES.UPDATE_COMMENT_CONTENT:
|
|
10054
|
+
case DOCUMENT_OP_TYPES.SET_COMMENT_RESOLUTION:
|
|
10055
|
+
case DOCUMENT_OP_TYPES.DELETE_COMMENT:
|
|
10056
|
+
case DOCUMENT_OP_TYPES.RESTORE_COMMENT_STATE: return applyCommentOp(document, op, applyDocumentOps);
|
|
8964
10057
|
case DOCUMENT_OP_TYPES.CREATE_HEADER_FOOTER:
|
|
8965
10058
|
case DOCUMENT_OP_TYPES.REMOVE_HEADER_FOOTER:
|
|
8966
10059
|
case DOCUMENT_OP_TYPES.SET_SECTION_PROPS:
|
|
@@ -8996,6 +10089,7 @@ const dispatch = (document, op) => {
|
|
|
8996
10089
|
case DOCUMENT_OP_TYPES.INSERT_ROW:
|
|
8997
10090
|
case DOCUMENT_OP_TYPES.DELETE_ROW:
|
|
8998
10091
|
case DOCUMENT_OP_TYPES.SET_TABLE_ROWS: return applyRowOp(document, op);
|
|
10092
|
+
case DOCUMENT_OP_TYPES.SET_PACKAGE_RESOURCES: return applyPackageResourcesOp(document, op);
|
|
8999
10093
|
case DOCUMENT_OP_TYPES.INSERT_COLUMN:
|
|
9000
10094
|
case DOCUMENT_OP_TYPES.DELETE_COLUMN:
|
|
9001
10095
|
case DOCUMENT_OP_TYPES.MERGE_CELLS:
|
|
@@ -9035,6 +10129,11 @@ const stampOf = (op) => {
|
|
|
9035
10129
|
case DOCUMENT_OP_TYPES.SET_CELL_PROPS:
|
|
9036
10130
|
case DOCUMENT_OP_TYPES.SET_ROW_PROPS:
|
|
9037
10131
|
case DOCUMENT_OP_TYPES.SET_TABLE_PROPS: return op.revision;
|
|
10132
|
+
case DOCUMENT_OP_TYPES.CREATE_COMMENT:
|
|
10133
|
+
case DOCUMENT_OP_TYPES.UPDATE_COMMENT_CONTENT:
|
|
10134
|
+
case DOCUMENT_OP_TYPES.SET_COMMENT_RESOLUTION:
|
|
10135
|
+
case DOCUMENT_OP_TYPES.DELETE_COMMENT:
|
|
10136
|
+
case DOCUMENT_OP_TYPES.RESTORE_COMMENT_STATE:
|
|
9038
10137
|
case DOCUMENT_OP_TYPES.CREATE_HEADER_FOOTER:
|
|
9039
10138
|
case DOCUMENT_OP_TYPES.REMOVE_HEADER_FOOTER:
|
|
9040
10139
|
case DOCUMENT_OP_TYPES.ADD_NOTE:
|
|
@@ -9052,7 +10151,8 @@ const stampOf = (op) => {
|
|
|
9052
10151
|
case DOCUMENT_OP_TYPES.SET_CONTAINER_BLOCKS:
|
|
9053
10152
|
case DOCUMENT_OP_TYPES.CREATE_NUMBERING_INSTANCE:
|
|
9054
10153
|
case DOCUMENT_OP_TYPES.DELETE_NUMBERING_INSTANCE:
|
|
9055
|
-
case DOCUMENT_OP_TYPES.SET_SECTION_ENDPOINT:
|
|
10154
|
+
case DOCUMENT_OP_TYPES.SET_SECTION_ENDPOINT:
|
|
10155
|
+
case DOCUMENT_OP_TYPES.SET_PACKAGE_RESOURCES: return;
|
|
9056
10156
|
default: return op;
|
|
9057
10157
|
}
|
|
9058
10158
|
};
|
|
@@ -9133,7 +10233,7 @@ const applyDocumentOp = (document, input) => {
|
|
|
9133
10233
|
};
|
|
9134
10234
|
/** Apply a journal envelope only when its schema matches this reader. */
|
|
9135
10235
|
const applyDocumentOpEnvelope = (document, envelope) => {
|
|
9136
|
-
if (envelope.schema !==
|
|
10236
|
+
if (envelope.schema !== 10) return Result.err(refusal$1(envelope.op, DOCUMENT_OP_REFUSAL_REASONS.UNSUPPORTED_SCHEMA, `Document operation schema ${envelope.schema} is unsupported; this reader accepts 10.`));
|
|
9137
10237
|
return applyDocumentOp(document, envelope.op);
|
|
9138
10238
|
};
|
|
9139
10239
|
/**
|
|
@@ -9158,92 +10258,316 @@ const applyDocumentOps = (document, ops) => {
|
|
|
9158
10258
|
});
|
|
9159
10259
|
};
|
|
9160
10260
|
//#endregion
|
|
9161
|
-
//#region src/ops/
|
|
9162
|
-
const
|
|
9163
|
-
|
|
9164
|
-
|
|
9165
|
-
|
|
10261
|
+
//#region src/ops/tocIntent.ts
|
|
10262
|
+
const refuse$1 = (message, reason = DOCUMENT_OP_REFUSAL_REASONS.UNTRACKABLE) => Result.err(new DocumentOpRefusal({
|
|
10263
|
+
message,
|
|
10264
|
+
reason,
|
|
10265
|
+
opType: DOCUMENT_OP_TYPES.REPLACE_INLINE
|
|
10266
|
+
}));
|
|
10267
|
+
const isPendingOrUnsupported = (content) => content.some((item) => {
|
|
10268
|
+
if (item.type === "bookmarkStart" || item.type === "bookmarkEnd") return false;
|
|
10269
|
+
if (item.type === "run") return (item.propertyChanges?.length ?? 0) > 0;
|
|
10270
|
+
if (item.type === "hyperlink") return isPendingOrUnsupported(item.children);
|
|
10271
|
+
return true;
|
|
10272
|
+
});
|
|
10273
|
+
const takeBookmarkId = (census, reserved) => {
|
|
10274
|
+
for (let id = 1; Number.isSafeInteger(id); id += 1) {
|
|
10275
|
+
if (census.ids.has(id) || reserved.has(id)) continue;
|
|
10276
|
+
reserved.add(id);
|
|
10277
|
+
return id;
|
|
10278
|
+
}
|
|
9166
10279
|
};
|
|
9167
|
-
const
|
|
9168
|
-
const
|
|
9169
|
-
const
|
|
9170
|
-
for (
|
|
9171
|
-
const
|
|
9172
|
-
if (
|
|
9173
|
-
|
|
9174
|
-
|
|
10280
|
+
const takeParagraphIds = ({ document, count, reserved }) => {
|
|
10281
|
+
const occupied = new Set([...packageParagraphIds(document.package), ...reserved ?? []].map(idKey));
|
|
10282
|
+
const ids = [];
|
|
10283
|
+
for (let id = 1; id < 2147483648 && ids.length < count; id += 1) {
|
|
10284
|
+
const candidate = id.toString(16).padStart(8, "0").toUpperCase();
|
|
10285
|
+
if (occupied.has(candidate)) continue;
|
|
10286
|
+
occupied.add(candidate);
|
|
10287
|
+
ids.push(candidate);
|
|
10288
|
+
}
|
|
10289
|
+
return ids.length === count ? ids : void 0;
|
|
10290
|
+
};
|
|
10291
|
+
const takeBookmarkName = (census, reserved) => {
|
|
10292
|
+
for (let suffix = 1;; suffix += 1) {
|
|
10293
|
+
const name = `_Toc${suffix}`;
|
|
10294
|
+
if (census.names.has(name) || reserved.has(name)) continue;
|
|
10295
|
+
reserved.add(name);
|
|
10296
|
+
return name;
|
|
10297
|
+
}
|
|
10298
|
+
};
|
|
10299
|
+
const paragraphFor = (document, position) => storyParagraphs(storyBody(document, position.story)).find(({ paragraph }) => idKey(paragraph.paraId ?? "") === idKey(position.blockId))?.paragraph;
|
|
10300
|
+
const bookmarkAtStart = (paragraph, id, name) => [
|
|
10301
|
+
{
|
|
10302
|
+
type: "bookmarkStart",
|
|
10303
|
+
id,
|
|
10304
|
+
name
|
|
10305
|
+
},
|
|
10306
|
+
...paragraph.content,
|
|
10307
|
+
{
|
|
10308
|
+
type: "bookmarkEnd",
|
|
10309
|
+
id
|
|
10310
|
+
}
|
|
10311
|
+
];
|
|
10312
|
+
/** Map original endpoints through the bookmark wrappers emitted by TOC compilation. */
|
|
10313
|
+
const mapTocBookmarkPosition = (position, ops) => {
|
|
10314
|
+
if (position.offset !== 0) return position;
|
|
10315
|
+
let prepended = 0;
|
|
10316
|
+
for (const op of ops) {
|
|
10317
|
+
if (op.type !== DOCUMENT_OP_TYPES.REPLACE_INLINE || !sameStory(op.story, position.story) || idKey(op.blockId) !== idKey(position.blockId)) continue;
|
|
10318
|
+
prepended += 1;
|
|
9175
10319
|
}
|
|
10320
|
+
return prepended === 0 ? position : {
|
|
10321
|
+
...position,
|
|
10322
|
+
zeroWidthBefore: (position.zeroWidthBefore ?? 0) + prepended
|
|
10323
|
+
};
|
|
9176
10324
|
};
|
|
9177
|
-
const
|
|
9178
|
-
const
|
|
10325
|
+
const paragraphBookmarks = (content) => {
|
|
10326
|
+
const starts = /* @__PURE__ */ new Map();
|
|
10327
|
+
const ends = /* @__PURE__ */ new Set();
|
|
10328
|
+
for (const item of content) if (item.type === "bookmarkStart") starts.set(item.id, item.name);
|
|
10329
|
+
else if (item.type === "bookmarkEnd") ends.add(item.id);
|
|
10330
|
+
else if (item.type === "hyperlink") {
|
|
10331
|
+
const nested = paragraphBookmarks(item.children);
|
|
10332
|
+
for (const [id, name] of nested.starts) starts.set(id, name);
|
|
10333
|
+
for (const id of nested.ends) ends.add(id);
|
|
10334
|
+
}
|
|
9179
10335
|
return {
|
|
9180
|
-
|
|
9181
|
-
|
|
9182
|
-
paragraphs: new Set(paragraphs.keys()),
|
|
9183
|
-
stories: /* @__PURE__ */ new Map()
|
|
10336
|
+
starts,
|
|
10337
|
+
ends
|
|
9184
10338
|
};
|
|
9185
10339
|
};
|
|
9186
|
-
|
|
9187
|
-
const
|
|
9188
|
-
|
|
9189
|
-
|
|
9190
|
-
|
|
9191
|
-
|
|
9192
|
-
|
|
9193
|
-
|
|
9194
|
-
|
|
9195
|
-
|
|
9196
|
-
|
|
9197
|
-
|
|
9198
|
-
|
|
9199
|
-
|
|
9200
|
-
|
|
9201
|
-
};
|
|
9202
|
-
|
|
9203
|
-
|
|
9204
|
-
|
|
9205
|
-
|
|
9206
|
-
|
|
10340
|
+
const tocParagraphs = (intent, links) => {
|
|
10341
|
+
const paragraphs = [{
|
|
10342
|
+
type: "paragraph",
|
|
10343
|
+
formatting: {
|
|
10344
|
+
...intent.titleStyleId === void 0 ? {} : { styleId: intent.titleStyleId },
|
|
10345
|
+
alignment: "center"
|
|
10346
|
+
},
|
|
10347
|
+
content: [{
|
|
10348
|
+
type: "run",
|
|
10349
|
+
formatting: { bold: true },
|
|
10350
|
+
content: [{
|
|
10351
|
+
type: "text",
|
|
10352
|
+
text: intent.title
|
|
10353
|
+
}]
|
|
10354
|
+
}]
|
|
10355
|
+
}];
|
|
10356
|
+
for (const { heading, name } of links) paragraphs.push({
|
|
10357
|
+
type: "paragraph",
|
|
10358
|
+
formatting: {
|
|
10359
|
+
...heading.styleId === void 0 ? {} : { styleId: heading.styleId },
|
|
10360
|
+
...heading.level === 0 ? {} : { indentLeft: heading.level * 720 },
|
|
10361
|
+
tabs: [{
|
|
10362
|
+
position: intent.tabPosition,
|
|
10363
|
+
alignment: "right",
|
|
10364
|
+
leader: "dot"
|
|
10365
|
+
}]
|
|
10366
|
+
},
|
|
10367
|
+
content: [
|
|
10368
|
+
{
|
|
10369
|
+
type: "hyperlink",
|
|
10370
|
+
anchor: name,
|
|
10371
|
+
children: [{
|
|
10372
|
+
type: "run",
|
|
10373
|
+
content: [{
|
|
10374
|
+
type: "text",
|
|
10375
|
+
text: heading.text
|
|
10376
|
+
}]
|
|
10377
|
+
}]
|
|
10378
|
+
},
|
|
10379
|
+
{
|
|
10380
|
+
type: "run",
|
|
10381
|
+
content: [{ type: "tab" }]
|
|
10382
|
+
},
|
|
10383
|
+
{
|
|
10384
|
+
type: "complexField",
|
|
10385
|
+
instruction: `PAGEREF ${name} \\h`,
|
|
10386
|
+
fieldType: "PAGEREF",
|
|
10387
|
+
fieldCode: [{
|
|
10388
|
+
type: "run",
|
|
10389
|
+
content: [{
|
|
10390
|
+
type: "instrText",
|
|
10391
|
+
text: `PAGEREF ${name} \\h`
|
|
10392
|
+
}]
|
|
10393
|
+
}],
|
|
10394
|
+
fieldResult: [{
|
|
10395
|
+
type: "run",
|
|
10396
|
+
content: [{
|
|
10397
|
+
type: "text",
|
|
10398
|
+
text: "1"
|
|
10399
|
+
}]
|
|
10400
|
+
}],
|
|
10401
|
+
dirty: true
|
|
10402
|
+
}
|
|
10403
|
+
]
|
|
10404
|
+
});
|
|
10405
|
+
return paragraphs;
|
|
10406
|
+
};
|
|
10407
|
+
/** These paragraphs are newly constructed for this compile, so assigning their ids is local. */
|
|
10408
|
+
const identifyTocParagraphs = (paragraphs, ids) => {
|
|
10409
|
+
for (const [index, paragraph] of paragraphs.entries()) paragraph.paraId = ids.at(index) ?? panic("TOC allocation must cover every generated paragraph.");
|
|
10410
|
+
return paragraphs;
|
|
10411
|
+
};
|
|
10412
|
+
const compileGenerateTOCIntent = (document, intent, mode) => {
|
|
10413
|
+
if (intent.headings.length === 0) return Result.ok({
|
|
10414
|
+
ops: [],
|
|
10415
|
+
selection: intent.at
|
|
10416
|
+
});
|
|
10417
|
+
if (mode.type === "suggesting") return refuse$1("TOC suggestions require serializable wrapper review provenance.");
|
|
10418
|
+
if (intent.at.story !== OP_STORIES.MAIN) return refuse$1("TOC generation is supported in the main document story only.");
|
|
10419
|
+
if (intent.title === "" || hasIllegalXmlCharacters(intent.title) || intent.headings.some(({ text }) => hasIllegalXmlCharacters(text))) return refuse$1("TOC text is empty or contains characters that XML cannot represent.", DOCUMENT_OP_REFUSAL_REASONS.INVALID_TEXT);
|
|
10420
|
+
if (!Number.isSafeInteger(intent.tabPosition) || intent.tabPosition < 0) return refuse$1("The TOC tab position must be a nonnegative integer.", DOCUMENT_OP_REFUSAL_REASONS.INVALID_OFFSET);
|
|
10421
|
+
const target = paragraphFor(document, intent.at);
|
|
10422
|
+
if (target === void 0) return refuse$1("The TOC insertion paragraph does not exist.", DOCUMENT_OP_REFUSAL_REASONS.BLOCK_NOT_FOUND);
|
|
10423
|
+
if (target.paraId === void 0) return refuse$1("The TOC insertion paragraph has no stable identity.", DOCUMENT_OP_REFUSAL_REASONS.MISSING_BLOCK_ID);
|
|
10424
|
+
const initialGap = resolveGap({
|
|
10425
|
+
paragraph: target,
|
|
10426
|
+
position: intent.at,
|
|
10427
|
+
fallback: "insertion"
|
|
10428
|
+
});
|
|
10429
|
+
if (typeof initialGap !== "object") return refuse$1("The TOC insertion position is invalid.", initialGap);
|
|
10430
|
+
const targetLength = paragraphLength(target);
|
|
10431
|
+
const locations = storyParagraphs(storyBody(document, intent.at.story));
|
|
10432
|
+
const links = [];
|
|
10433
|
+
const bookmarkIds = [];
|
|
10434
|
+
const seenHeadings = /* @__PURE__ */ new Set();
|
|
10435
|
+
const census = packageBookmarkCensus(document.package);
|
|
10436
|
+
const reservedBookmarkIds = /* @__PURE__ */ new Set();
|
|
10437
|
+
const reservedBookmarkNames = /* @__PURE__ */ new Set();
|
|
10438
|
+
for (const heading of intent.headings) {
|
|
10439
|
+
if (!Number.isInteger(heading.level) || heading.level < 0 || heading.level > 8) return refuse$1("TOC heading levels must be between zero and eight.", DOCUMENT_OP_REFUSAL_REASONS.INVALID_OFFSET);
|
|
10440
|
+
const location = locations.find(({ paragraph }) => idKey(paragraph.paraId ?? "") === idKey(heading.blockId));
|
|
10441
|
+
if (location === void 0) return refuse$1("A TOC heading paragraph does not exist.", DOCUMENT_OP_REFUSAL_REASONS.BLOCK_NOT_FOUND);
|
|
10442
|
+
if (paragraphLogicalText(location.paragraph).trim() !== heading.text.trim() || location.paragraph.paraId === void 0 || isPendingOrUnsupported(location.paragraph.content) || (location.paragraph.propertyChanges?.length ?? 0) > 0 || location.paragraph.pPrMark !== void 0 || location.paragraph.reviewCarrier !== void 0) return refuse$1("A TOC heading is stale or contains pending review wrappers.");
|
|
10443
|
+
const headingKey = idKey(heading.blockId);
|
|
10444
|
+
if (seenHeadings.has(headingKey)) return refuse$1("A TOC heading is listed more than once.");
|
|
10445
|
+
seenHeadings.add(headingKey);
|
|
10446
|
+
const local = paragraphBookmarks(location.paragraph.content);
|
|
10447
|
+
const existing = [...local.starts].find(([id, name]) => name.startsWith("_Toc") && local.ends.has(id) && census.pairs.some((pair) => pair.id === id && pair.name === name));
|
|
10448
|
+
if (existing !== void 0) {
|
|
10449
|
+
bookmarkIds.push(existing[0]);
|
|
10450
|
+
links.push({
|
|
10451
|
+
heading,
|
|
10452
|
+
name: existing[1]
|
|
10453
|
+
});
|
|
10454
|
+
continue;
|
|
9207
10455
|
}
|
|
9208
|
-
const
|
|
9209
|
-
|
|
9210
|
-
|
|
9211
|
-
|
|
9212
|
-
|
|
9213
|
-
|
|
9214
|
-
|
|
9215
|
-
|
|
9216
|
-
|
|
9217
|
-
|
|
9218
|
-
|
|
9219
|
-
|
|
9220
|
-
|
|
9221
|
-
|
|
9222
|
-
|
|
9223
|
-
|
|
9224
|
-
|
|
9225
|
-
|
|
9226
|
-
|
|
9227
|
-
|
|
9228
|
-
|
|
9229
|
-
|
|
9230
|
-
|
|
9231
|
-
|
|
10456
|
+
const id = takeBookmarkId(census, reservedBookmarkIds);
|
|
10457
|
+
if (id === void 0) return refuse$1("The bookmark identity space is exhausted.");
|
|
10458
|
+
bookmarkIds.push(id);
|
|
10459
|
+
links.push({
|
|
10460
|
+
heading,
|
|
10461
|
+
name: takeBookmarkName(census, reservedBookmarkNames)
|
|
10462
|
+
});
|
|
10463
|
+
}
|
|
10464
|
+
const ops = [];
|
|
10465
|
+
let insertionAt = {
|
|
10466
|
+
...intent.at,
|
|
10467
|
+
...initialGap
|
|
10468
|
+
};
|
|
10469
|
+
for (const [index, { heading, name }] of links.entries()) {
|
|
10470
|
+
const paragraph = locations.find(({ paragraph: candidate }) => idKey(candidate.paraId ?? "") === idKey(heading.blockId))?.paragraph;
|
|
10471
|
+
if (paragraph === void 0) return refuse$1("A TOC heading paragraph disappeared.");
|
|
10472
|
+
const id = bookmarkIds[index];
|
|
10473
|
+
if (id === void 0) return refuse$1("A TOC bookmark id was not allocated.");
|
|
10474
|
+
if (census.pairs.some((pair) => pair.id === id && pair.name === name)) continue;
|
|
10475
|
+
const op = {
|
|
10476
|
+
type: DOCUMENT_OP_TYPES.REPLACE_INLINE,
|
|
10477
|
+
story: intent.at.story,
|
|
10478
|
+
blockId: paragraph.paraId ?? "",
|
|
10479
|
+
expected: paragraph.content,
|
|
10480
|
+
content: bookmarkAtStart(paragraph, id, name)
|
|
10481
|
+
};
|
|
10482
|
+
ops.push(op);
|
|
10483
|
+
const applied = applyDocumentOp(document, op);
|
|
10484
|
+
if (applied.isErr()) return Result.err(applied.error);
|
|
10485
|
+
document = applied.value.document;
|
|
10486
|
+
insertionAt = mapTocBookmarkPosition(insertionAt, [op]);
|
|
10487
|
+
}
|
|
10488
|
+
const currentTarget = paragraphFor(document, intent.at);
|
|
10489
|
+
if (currentTarget === void 0) return refuse$1("The TOC insertion paragraph disappeared.");
|
|
10490
|
+
const gap = resolveGap({
|
|
10491
|
+
paragraph: currentTarget,
|
|
10492
|
+
position: insertionAt,
|
|
10493
|
+
fallback: "insertion"
|
|
10494
|
+
});
|
|
10495
|
+
if (typeof gap !== "object") return refuse$1("The TOC insertion position is invalid after bookmark updates.", gap);
|
|
10496
|
+
if (gap.offset !== 0 && gap.offset !== paragraphLength(currentTarget) && (isPendingOrUnsupported(currentTarget.content) || (currentTarget.propertyChanges?.length ?? 0) > 0 || currentTarget.pPrMark !== void 0 || currentTarget.reviewCarrier !== void 0)) return refuse$1("TOC insertion cannot split pending review wrappers.");
|
|
10497
|
+
const toc = tocParagraphs(intent, links);
|
|
10498
|
+
const story = intent.at.story;
|
|
10499
|
+
if (gap.offset === 0 || gap.offset === targetLength) {
|
|
10500
|
+
const ids = takeParagraphIds({
|
|
10501
|
+
document,
|
|
10502
|
+
count: toc.length,
|
|
10503
|
+
reserved: mode.reservedBlockIds
|
|
10504
|
+
});
|
|
10505
|
+
if (ids === void 0) return refuse$1("The paragraph identity space is exhausted.");
|
|
10506
|
+
ops.push({
|
|
10507
|
+
type: DOCUMENT_OP_TYPES.INSERT_BLOCKS,
|
|
10508
|
+
story,
|
|
10509
|
+
at: gap.offset === 0 ? {
|
|
10510
|
+
type: "before",
|
|
10511
|
+
blockId: intent.at.blockId
|
|
10512
|
+
} : {
|
|
10513
|
+
type: "after",
|
|
10514
|
+
blockId: intent.at.blockId
|
|
10515
|
+
},
|
|
10516
|
+
blocks: identifyTocParagraphs(toc, ids)
|
|
10517
|
+
});
|
|
10518
|
+
return Result.ok({
|
|
10519
|
+
ops,
|
|
10520
|
+
selection: {
|
|
10521
|
+
...insertionAt,
|
|
10522
|
+
...gap
|
|
9232
10523
|
}
|
|
9233
|
-
}
|
|
9234
|
-
|
|
9235
|
-
|
|
10524
|
+
});
|
|
10525
|
+
}
|
|
10526
|
+
const newBlockId = takeParagraphIds({
|
|
10527
|
+
document,
|
|
10528
|
+
count: 1,
|
|
10529
|
+
reserved: mode.reservedBlockIds
|
|
10530
|
+
})?.at(0);
|
|
10531
|
+
if (newBlockId === void 0) return refuse$1("The paragraph identity space is exhausted.");
|
|
10532
|
+
const split = {
|
|
10533
|
+
type: DOCUMENT_OP_TYPES.SPLIT_BLOCK,
|
|
10534
|
+
at: {
|
|
10535
|
+
...insertionAt,
|
|
10536
|
+
...gap
|
|
10537
|
+
},
|
|
10538
|
+
newBlockId,
|
|
10539
|
+
newHalf: SPLIT_HALVES.FIRST,
|
|
10540
|
+
newParagraph: target.formatting === void 0 ? {} : { formatting: target.formatting }
|
|
10541
|
+
};
|
|
10542
|
+
ops.push(split);
|
|
10543
|
+
const afterSplit = applyDocumentOp(document, split);
|
|
10544
|
+
if (afterSplit.isErr()) return Result.err(afterSplit.error);
|
|
10545
|
+
document = afterSplit.value.document;
|
|
10546
|
+
const tocIds = takeParagraphIds({
|
|
10547
|
+
document,
|
|
10548
|
+
count: toc.length,
|
|
10549
|
+
reserved: mode.reservedBlockIds
|
|
10550
|
+
});
|
|
10551
|
+
if (tocIds === void 0) return refuse$1("The paragraph identity space is exhausted.");
|
|
10552
|
+
const blocks = identifyTocParagraphs(toc, tocIds);
|
|
10553
|
+
ops.push({
|
|
10554
|
+
type: DOCUMENT_OP_TYPES.INSERT_BLOCKS,
|
|
10555
|
+
story,
|
|
10556
|
+
at: {
|
|
10557
|
+
type: "before",
|
|
10558
|
+
blockId: intent.at.blockId
|
|
10559
|
+
},
|
|
10560
|
+
blocks
|
|
10561
|
+
});
|
|
10562
|
+
return Result.ok({
|
|
10563
|
+
ops,
|
|
10564
|
+
selection: {
|
|
10565
|
+
story,
|
|
10566
|
+
blockId: intent.at.blockId,
|
|
10567
|
+
offset: 0,
|
|
10568
|
+
zeroWidthBefore: 0
|
|
9236
10569
|
}
|
|
9237
|
-
|
|
9238
|
-
document,
|
|
9239
|
-
identities,
|
|
9240
|
-
paragraphs,
|
|
9241
|
-
census: censusOf(identities, paragraphs)
|
|
9242
|
-
};
|
|
9243
|
-
versions.set(document, version);
|
|
9244
|
-
latest = version;
|
|
9245
|
-
return version.census;
|
|
9246
|
-
};
|
|
10570
|
+
});
|
|
9247
10571
|
};
|
|
9248
10572
|
//#endregion
|
|
9249
10573
|
//#region src/ops/plan.ts
|
|
@@ -9262,6 +10586,7 @@ const usedIds = (document, space) => new Set(idsIn(packageIdentityKeys(document.
|
|
|
9262
10586
|
/** The same operation with other new ids; one that takes none is returned as it is. */
|
|
9263
10587
|
const withNewIds = (op, newIds) => {
|
|
9264
10588
|
switch (op.type) {
|
|
10589
|
+
case DOCUMENT_OP_TYPES.CREATE_COMMENT:
|
|
9265
10590
|
case DOCUMENT_OP_TYPES.DELETE_BLOCKS:
|
|
9266
10591
|
case DOCUMENT_OP_TYPES.INSERT_BLOCKS:
|
|
9267
10592
|
case DOCUMENT_OP_TYPES.INSERT_TEXT:
|
|
@@ -9286,6 +10611,10 @@ const withNewIds = (op, newIds) => {
|
|
|
9286
10611
|
...op,
|
|
9287
10612
|
newIds
|
|
9288
10613
|
};
|
|
10614
|
+
case DOCUMENT_OP_TYPES.UPDATE_COMMENT_CONTENT:
|
|
10615
|
+
case DOCUMENT_OP_TYPES.SET_COMMENT_RESOLUTION:
|
|
10616
|
+
case DOCUMENT_OP_TYPES.DELETE_COMMENT:
|
|
10617
|
+
case DOCUMENT_OP_TYPES.RESTORE_COMMENT_STATE:
|
|
9289
10618
|
case DOCUMENT_OP_TYPES.CREATE_HEADER_FOOTER:
|
|
9290
10619
|
case DOCUMENT_OP_TYPES.REMOVE_HEADER_FOOTER:
|
|
9291
10620
|
case DOCUMENT_OP_TYPES.ADD_NOTE:
|
|
@@ -9303,7 +10632,8 @@ const withNewIds = (op, newIds) => {
|
|
|
9303
10632
|
case DOCUMENT_OP_TYPES.SET_TABLE:
|
|
9304
10633
|
case DOCUMENT_OP_TYPES.CREATE_NUMBERING_INSTANCE:
|
|
9305
10634
|
case DOCUMENT_OP_TYPES.DELETE_NUMBERING_INSTANCE:
|
|
9306
|
-
case DOCUMENT_OP_TYPES.SET_SECTION_ENDPOINT:
|
|
10635
|
+
case DOCUMENT_OP_TYPES.SET_SECTION_ENDPOINT:
|
|
10636
|
+
case DOCUMENT_OP_TYPES.SET_PACKAGE_RESOURCES: return op;
|
|
9307
10637
|
default: return op;
|
|
9308
10638
|
}
|
|
9309
10639
|
};
|
|
@@ -9329,6 +10659,68 @@ const trimAppliedNewIds = ({ before, op, applied, story }) => {
|
|
|
9329
10659
|
return !known.has(key) && (identities.has(key) || space === IDENTITY_SPACES.REVISION && revisions.has(id));
|
|
9330
10660
|
});
|
|
9331
10661
|
};
|
|
10662
|
+
/** Package-aware explicit pools for every semantic comment anchor cut, including replies. */
|
|
10663
|
+
const allocateCommentAnchorIds = (document, op) => {
|
|
10664
|
+
const keys = [...packageIdentityKeys(document.package), ...identityKeysIn(op)];
|
|
10665
|
+
const firstIn = (space) => {
|
|
10666
|
+
let largest = 0;
|
|
10667
|
+
for (const id of idsIn(keys, space)) largest = Math.max(largest, id);
|
|
10668
|
+
return largest + 1;
|
|
10669
|
+
};
|
|
10670
|
+
const firstRevision = Math.max(firstIn(IDENTITY_SPACES.REVISION), op.comment.id + 1);
|
|
10671
|
+
const firstControl = firstIn(IDENTITY_SPACES.CONTROL);
|
|
10672
|
+
const pool = (first, count) => Array.from({ length: count }, (_, index) => first + index);
|
|
10673
|
+
const attempt = (counts) => applyDocumentOp(document, {
|
|
10674
|
+
...op,
|
|
10675
|
+
newIds: {
|
|
10676
|
+
revision: pool(firstRevision, counts.revision),
|
|
10677
|
+
control: pool(firstControl, counts.control)
|
|
10678
|
+
}
|
|
10679
|
+
});
|
|
10680
|
+
const capacity = {
|
|
10681
|
+
revision: Math.max(0, MAX_REVISION_ID - firstRevision + 1),
|
|
10682
|
+
control: Math.max(0, MAX_REVISION_ID - firstControl + 1)
|
|
10683
|
+
};
|
|
10684
|
+
let count = 0;
|
|
10685
|
+
for (;;) {
|
|
10686
|
+
const trial = attempt({
|
|
10687
|
+
revision: Math.min(count, capacity.revision),
|
|
10688
|
+
control: Math.min(count, capacity.control)
|
|
10689
|
+
});
|
|
10690
|
+
if (trial.isOk()) break;
|
|
10691
|
+
if (trial.error.reason !== DOCUMENT_OP_REFUSAL_REASONS.NEEDS_NEW_IDS) return Result.err(trial.error);
|
|
10692
|
+
const next = count === 0 ? 1 : count * 2;
|
|
10693
|
+
if (next > MAX_DEMAND || count >= Math.max(capacity.revision, capacity.control)) return Result.err(new DocumentOpRefusal({
|
|
10694
|
+
opType: op.type,
|
|
10695
|
+
reason: DOCUMENT_OP_REFUSAL_REASONS.NEEDS_NEW_IDS,
|
|
10696
|
+
message: "Comment anchor cuts exhaust the package identity space."
|
|
10697
|
+
}));
|
|
10698
|
+
count = next;
|
|
10699
|
+
}
|
|
10700
|
+
const counts = {
|
|
10701
|
+
revision: Math.min(count, capacity.revision),
|
|
10702
|
+
control: Math.min(count, capacity.control)
|
|
10703
|
+
};
|
|
10704
|
+
for (const space of [IDENTITY_SPACES.REVISION, IDENTITY_SPACES.CONTROL]) {
|
|
10705
|
+
let low = 0;
|
|
10706
|
+
let high = counts[space];
|
|
10707
|
+
while (low < high) {
|
|
10708
|
+
const middle = Math.floor((low + high) / 2);
|
|
10709
|
+
const trial = attempt({
|
|
10710
|
+
...counts,
|
|
10711
|
+
[space]: middle
|
|
10712
|
+
});
|
|
10713
|
+
if (trial.isOk()) high = middle;
|
|
10714
|
+
else if (trial.error.reason === DOCUMENT_OP_REFUSAL_REASONS.NEEDS_NEW_IDS) low = middle + 1;
|
|
10715
|
+
else return Result.err(trial.error);
|
|
10716
|
+
}
|
|
10717
|
+
counts[space] = low;
|
|
10718
|
+
}
|
|
10719
|
+
return Result.ok({
|
|
10720
|
+
revision: pool(firstRevision, counts.revision),
|
|
10721
|
+
control: pool(firstControl, counts.control)
|
|
10722
|
+
});
|
|
10723
|
+
};
|
|
9332
10724
|
/** The largest id count {@link revisionIdDemand} searches before giving up. */
|
|
9333
10725
|
const MAX_DEMAND = 65536;
|
|
9334
10726
|
/**
|
|
@@ -9394,6 +10786,10 @@ const replacementDeletionSegments = ({ spans, from, to, mode }) => {
|
|
|
9394
10786
|
const start = laterGap(span.before, from);
|
|
9395
10787
|
const end = earlierGap(span.after, to);
|
|
9396
10788
|
if (compareGaps(start, end) >= 0) continue;
|
|
10789
|
+
if (isParagraphContent(span.node) && isRangeBoundary(span.node)) {
|
|
10790
|
+
open = void 0;
|
|
10791
|
+
continue;
|
|
10792
|
+
}
|
|
9397
10793
|
let plan = isCommentAnchor(span.node) ? "anchor" : "direct";
|
|
9398
10794
|
if (mode.type === "suggesting") plan = leafPlan(span, mode.author);
|
|
9399
10795
|
else if (span.ancestors.some(isRemovedRevisionNode)) plan = "untouched";
|
|
@@ -9446,7 +10842,7 @@ const createTrackedPlan = ({ document, revision, newIds }) => {
|
|
|
9446
10842
|
};
|
|
9447
10843
|
const append = (input) => {
|
|
9448
10844
|
let op = input;
|
|
9449
|
-
if (input.revision !== void 0) {
|
|
10845
|
+
if (input.type !== DOCUMENT_OP_TYPES.REPLACE_INLINE && input.revision !== void 0) {
|
|
9450
10846
|
let stampId = revision.id;
|
|
9451
10847
|
if (stampUsed) {
|
|
9452
10848
|
if (remaining <= taken) return outOfIds();
|
|
@@ -9472,7 +10868,7 @@ const createTrackedPlan = ({ document, revision, newIds }) => {
|
|
|
9472
10868
|
});
|
|
9473
10869
|
const applied = applyDocumentOp(current, op);
|
|
9474
10870
|
if (applied.isErr()) return Result.err(applied.error);
|
|
9475
|
-
stampUsed ||= input
|
|
10871
|
+
stampUsed ||= stampOf(input) !== void 0 && applied.value.revisions.length > 0;
|
|
9476
10872
|
const before = current;
|
|
9477
10873
|
current = applied.value.document;
|
|
9478
10874
|
const story = (() => {
|
|
@@ -9482,7 +10878,8 @@ const createTrackedPlan = ({ document, revision, newIds }) => {
|
|
|
9482
10878
|
case DOCUMENT_OP_TYPES.INSERT_CONTENT:
|
|
9483
10879
|
case DOCUMENT_OP_TYPES.SPLIT_BLOCK: return input.at.story;
|
|
9484
10880
|
case DOCUMENT_OP_TYPES.JOIN_BLOCKS:
|
|
9485
|
-
case DOCUMENT_OP_TYPES.SET_PARAGRAPH_PROPS:
|
|
10881
|
+
case DOCUMENT_OP_TYPES.SET_PARAGRAPH_PROPS:
|
|
10882
|
+
case DOCUMENT_OP_TYPES.REPLACE_INLINE: return input.story;
|
|
9486
10883
|
default: return input;
|
|
9487
10884
|
}
|
|
9488
10885
|
})();
|
|
@@ -9656,57 +11053,340 @@ const appendTrackedDeletion = ({ document, options, plan }) => {
|
|
|
9656
11053
|
nextBlockId: next.paraId ?? "",
|
|
9657
11054
|
sectionBoundary: SECTION_BOUNDARY_POLICIES.REMOVE
|
|
9658
11055
|
};
|
|
9659
|
-
const appended = plan.append(ownAddedMark ? join : {
|
|
9660
|
-
...join,
|
|
9661
|
-
revision
|
|
9662
|
-
});
|
|
9663
|
-
if (appended.isErr()) return appended;
|
|
9664
|
-
const directBody = storyBody(direct, from.story);
|
|
9665
|
-
const directLeading = storyParagraphs(directBody).find(({ paragraph }) => idKey(paragraph.paraId ?? "") === idKey(leading.at.blockId));
|
|
9666
|
-
if (directLeading === void 0) return refuse(DOCUMENT_OP_REFUSAL_REASONS.BLOCK_NOT_FOUND, "A simulated join lost its leading paragraph.");
|
|
9667
|
-
const directNext = blockListAt(directBody.content, directLeading.list).at(directLeading.index + 1);
|
|
9668
|
-
if (directNext?.type !== "paragraph") return refuse(DOCUMENT_OP_REFUSAL_REASONS.NOT_ADJACENT, "A simulated join has no following paragraph.");
|
|
9669
|
-
const simulated = applyDocumentOp(direct, {
|
|
9670
|
-
...join,
|
|
9671
|
-
nextBlockId: directNext.paraId ?? ""
|
|
9672
|
-
});
|
|
9673
|
-
if (simulated.isErr()) return Result.err(simulated.error);
|
|
9674
|
-
direct = simulated.value.document;
|
|
9675
|
-
}
|
|
9676
|
-
const survivor = storyParagraphs(storyBody(direct, from.story)).find(({ paragraph }) => idKey(paragraph.paraId ?? "") === idKey(to.blockId));
|
|
9677
|
-
const trackedSurvivor = storyParagraphs(storyBody(plan.document(), from.story)).find(({ paragraph }) => idKey(paragraph.paraId ?? "") === idKey(to.blockId));
|
|
9678
|
-
if (survivor === void 0 || trackedSurvivor === void 0) return refuse(DOCUMENT_OP_REFUSAL_REASONS.BLOCK_NOT_FOUND, "The planned survivor does not exist.");
|
|
9679
|
-
const current = paragraphPropertiesOf(trackedSurvivor.paragraph.formatting);
|
|
9680
|
-
const desired = paragraphPropertiesOf(survivor.paragraph.formatting);
|
|
9681
|
-
if (structurallyEqual(current, desired)) return Result.ok(void 0);
|
|
9682
|
-
const patch = Object.fromEntries([...Object.keys(current ?? {}).map((key) => [key, null]), ...Object.entries(desired ?? {})]);
|
|
9683
|
-
return plan.append({
|
|
9684
|
-
type: DOCUMENT_OP_TYPES.SET_PARAGRAPH_PROPS,
|
|
9685
|
-
story: from.story,
|
|
9686
|
-
blockId: to.blockId,
|
|
9687
|
-
patch,
|
|
9688
|
-
whenEmpty: survivor.paragraph.formatting === void 0 ? EMPTY_PROPERTY_SETS.OMIT : EMPTY_PROPERTY_SETS.KEEP,
|
|
9689
|
-
revision
|
|
9690
|
-
});
|
|
9691
|
-
};
|
|
9692
|
-
/**
|
|
9693
|
-
* Plan a same-list range as inline deletions followed by tracked paragraph
|
|
9694
|
-
* joins. Own insertions are retracted directly; comment anchors remain.
|
|
9695
|
-
* Inline ranges run in reverse order, then joins run from last to first.
|
|
9696
|
-
* All physical revision ids come from one pool for the complete batch.
|
|
9697
|
-
*/
|
|
9698
|
-
const planTrackedDeletion = (document, options) => {
|
|
9699
|
-
const plan = createTrackedPlan({
|
|
9700
|
-
document,
|
|
9701
|
-
revision: options.revision,
|
|
9702
|
-
newIds: options.newIds
|
|
9703
|
-
});
|
|
9704
|
-
const appended = appendTrackedDeletion({
|
|
9705
|
-
document,
|
|
9706
|
-
options,
|
|
9707
|
-
plan
|
|
9708
|
-
});
|
|
9709
|
-
return appended.isErr() ? Result.err(appended.error) : Result.ok(plan.ops);
|
|
11056
|
+
const appended = plan.append(ownAddedMark ? join : {
|
|
11057
|
+
...join,
|
|
11058
|
+
revision
|
|
11059
|
+
});
|
|
11060
|
+
if (appended.isErr()) return appended;
|
|
11061
|
+
const directBody = storyBody(direct, from.story);
|
|
11062
|
+
const directLeading = storyParagraphs(directBody).find(({ paragraph }) => idKey(paragraph.paraId ?? "") === idKey(leading.at.blockId));
|
|
11063
|
+
if (directLeading === void 0) return refuse(DOCUMENT_OP_REFUSAL_REASONS.BLOCK_NOT_FOUND, "A simulated join lost its leading paragraph.");
|
|
11064
|
+
const directNext = blockListAt(directBody.content, directLeading.list).at(directLeading.index + 1);
|
|
11065
|
+
if (directNext?.type !== "paragraph") return refuse(DOCUMENT_OP_REFUSAL_REASONS.NOT_ADJACENT, "A simulated join has no following paragraph.");
|
|
11066
|
+
const simulated = applyDocumentOp(direct, {
|
|
11067
|
+
...join,
|
|
11068
|
+
nextBlockId: directNext.paraId ?? ""
|
|
11069
|
+
});
|
|
11070
|
+
if (simulated.isErr()) return Result.err(simulated.error);
|
|
11071
|
+
direct = simulated.value.document;
|
|
11072
|
+
}
|
|
11073
|
+
const survivor = storyParagraphs(storyBody(direct, from.story)).find(({ paragraph }) => idKey(paragraph.paraId ?? "") === idKey(to.blockId));
|
|
11074
|
+
const trackedSurvivor = storyParagraphs(storyBody(plan.document(), from.story)).find(({ paragraph }) => idKey(paragraph.paraId ?? "") === idKey(to.blockId));
|
|
11075
|
+
if (survivor === void 0 || trackedSurvivor === void 0) return refuse(DOCUMENT_OP_REFUSAL_REASONS.BLOCK_NOT_FOUND, "The planned survivor does not exist.");
|
|
11076
|
+
const current = paragraphPropertiesOf(trackedSurvivor.paragraph.formatting);
|
|
11077
|
+
const desired = paragraphPropertiesOf(survivor.paragraph.formatting);
|
|
11078
|
+
if (structurallyEqual(current, desired)) return Result.ok(void 0);
|
|
11079
|
+
const patch = Object.fromEntries([...Object.keys(current ?? {}).map((key) => [key, null]), ...Object.entries(desired ?? {})]);
|
|
11080
|
+
return plan.append({
|
|
11081
|
+
type: DOCUMENT_OP_TYPES.SET_PARAGRAPH_PROPS,
|
|
11082
|
+
story: from.story,
|
|
11083
|
+
blockId: to.blockId,
|
|
11084
|
+
patch,
|
|
11085
|
+
whenEmpty: survivor.paragraph.formatting === void 0 ? EMPTY_PROPERTY_SETS.OMIT : EMPTY_PROPERTY_SETS.KEEP,
|
|
11086
|
+
revision
|
|
11087
|
+
});
|
|
11088
|
+
};
|
|
11089
|
+
/**
|
|
11090
|
+
* Plan a same-list range as inline deletions followed by tracked paragraph
|
|
11091
|
+
* joins. Own insertions are retracted directly; comment anchors remain.
|
|
11092
|
+
* Inline ranges run in reverse order, then joins run from last to first.
|
|
11093
|
+
* All physical revision ids come from one pool for the complete batch.
|
|
11094
|
+
*/
|
|
11095
|
+
const planTrackedDeletion = (document, options) => {
|
|
11096
|
+
const plan = createTrackedPlan({
|
|
11097
|
+
document,
|
|
11098
|
+
revision: options.revision,
|
|
11099
|
+
newIds: options.newIds
|
|
11100
|
+
});
|
|
11101
|
+
const appended = appendTrackedDeletion({
|
|
11102
|
+
document,
|
|
11103
|
+
options,
|
|
11104
|
+
plan
|
|
11105
|
+
});
|
|
11106
|
+
return appended.isErr() ? Result.err(appended.error) : Result.ok(plan.ops);
|
|
11107
|
+
};
|
|
11108
|
+
//#endregion
|
|
11109
|
+
//#region src/ops/hyperlinkIntent.ts
|
|
11110
|
+
const refuse = (message, reason = DOCUMENT_OP_REFUSAL_REASONS.UNTRACKABLE) => Result.err(new DocumentOpRefusal({
|
|
11111
|
+
message,
|
|
11112
|
+
reason,
|
|
11113
|
+
opType: DOCUMENT_OP_TYPES.REPLACE_INLINE
|
|
11114
|
+
}));
|
|
11115
|
+
/** Cutting these records cannot duplicate review identities or unmodeled wrappers. */
|
|
11116
|
+
const editable = (items) => items.every((item) => {
|
|
11117
|
+
switch (item.type) {
|
|
11118
|
+
case "run": return (item.propertyChanges?.length ?? 0) === 0;
|
|
11119
|
+
case "bookmarkStart":
|
|
11120
|
+
case "bookmarkEnd": return true;
|
|
11121
|
+
case "hyperlink": return editable(item.children);
|
|
11122
|
+
default: return false;
|
|
11123
|
+
}
|
|
11124
|
+
});
|
|
11125
|
+
const HYPERLINK_STYLE_ID = "Hyperlink";
|
|
11126
|
+
const styledRun = (run, intent) => {
|
|
11127
|
+
if (run.formatting === void 0) return run;
|
|
11128
|
+
const formatting = { ...run.formatting };
|
|
11129
|
+
if (intent.type === "setHyperlink") delete formatting.color;
|
|
11130
|
+
else if (formatting.styleId === HYPERLINK_STYLE_ID || intent.hyperlinkStyleId !== void 0 && formatting.styleId === intent.hyperlinkStyleId) delete formatting.styleId;
|
|
11131
|
+
if (structurallyEqual(formatting, run.formatting)) return run;
|
|
11132
|
+
const next = { ...run };
|
|
11133
|
+
if (Object.keys(formatting).length === 0) delete next.formatting;
|
|
11134
|
+
else next.formatting = formatting;
|
|
11135
|
+
return next;
|
|
11136
|
+
};
|
|
11137
|
+
/** Flatten only the edited hyperlinks; unrelated run formatting and zero-width markers stay. */
|
|
11138
|
+
const linkedChildren = (items, intent) => {
|
|
11139
|
+
const children = [];
|
|
11140
|
+
for (const item of items) switch (item.type) {
|
|
11141
|
+
case "run":
|
|
11142
|
+
children.push(intent.type === "setHyperlink" ? styledRun(item, intent) : item);
|
|
11143
|
+
break;
|
|
11144
|
+
case "bookmarkStart":
|
|
11145
|
+
case "bookmarkEnd":
|
|
11146
|
+
children.push(item);
|
|
11147
|
+
break;
|
|
11148
|
+
case "hyperlink":
|
|
11149
|
+
if (item.children.length === 0) {
|
|
11150
|
+
children.push(item);
|
|
11151
|
+
break;
|
|
11152
|
+
}
|
|
11153
|
+
for (const child of item.children) if (child.type === "run") children.push(styledRun(child, intent));
|
|
11154
|
+
else children.push(child);
|
|
11155
|
+
break;
|
|
11156
|
+
default: return panic("Validated hyperlink edit contains an unsupported inline node.");
|
|
11157
|
+
}
|
|
11158
|
+
return children;
|
|
11159
|
+
};
|
|
11160
|
+
const hyperlink = (intent, children) => {
|
|
11161
|
+
const result = {
|
|
11162
|
+
type: "hyperlink",
|
|
11163
|
+
children
|
|
11164
|
+
};
|
|
11165
|
+
if (intent.href.startsWith("#")) result.anchor = intent.href.slice(1);
|
|
11166
|
+
else if (intent.href !== "") result.href = intent.href;
|
|
11167
|
+
if (intent.tooltip !== void 0) result.tooltip = intent.tooltip;
|
|
11168
|
+
return result;
|
|
11169
|
+
};
|
|
11170
|
+
/** Link text segments while structural markers and empty links retain their authored positions. */
|
|
11171
|
+
const wrapLinkedChildren = (intent, children) => {
|
|
11172
|
+
const result = [];
|
|
11173
|
+
let link;
|
|
11174
|
+
for (const child of children) {
|
|
11175
|
+
if (child.type === "run") {
|
|
11176
|
+
if (link === void 0) {
|
|
11177
|
+
link = hyperlink(intent, []);
|
|
11178
|
+
result.push(link);
|
|
11179
|
+
}
|
|
11180
|
+
link.children.push(child);
|
|
11181
|
+
continue;
|
|
11182
|
+
}
|
|
11183
|
+
link = void 0;
|
|
11184
|
+
result.push(child);
|
|
11185
|
+
}
|
|
11186
|
+
return result;
|
|
11187
|
+
};
|
|
11188
|
+
/** Wrapper edits compile to exact-inverse content ops, never to a projection snapshot. */
|
|
11189
|
+
const compileHyperlinkIntent = (document, { intent: sourceIntent, mode, compileEmptyReplacement }) => {
|
|
11190
|
+
let intent = sourceIntent;
|
|
11191
|
+
if (intent.type !== "removeHyperlink" && intent.href !== "" && !intent.href.startsWith("#")) {
|
|
11192
|
+
const href = sanitizeExternalUrl(intent.href);
|
|
11193
|
+
if (href === void 0) return refuse("Hyperlink targets must use a supported URL scheme.", DOCUMENT_OP_REFUSAL_REASONS.INVALID_OPERATION);
|
|
11194
|
+
intent = {
|
|
11195
|
+
...intent,
|
|
11196
|
+
href
|
|
11197
|
+
};
|
|
11198
|
+
}
|
|
11199
|
+
const selected = selectedParagraphRuns(document, intent.from, intent.to);
|
|
11200
|
+
if (selected.isErr()) return Result.err(selected.error);
|
|
11201
|
+
const locations = selected.value.flat();
|
|
11202
|
+
const pieces = [];
|
|
11203
|
+
for (const { paragraph } of locations) {
|
|
11204
|
+
if (!editable(paragraph.content)) return refuse("Hyperlink edits cannot cut pending review identities or unsupported inline wrappers.");
|
|
11205
|
+
const blockId = paragraph.paraId ?? "";
|
|
11206
|
+
const from = idKey(blockId) === idKey(intent.from.blockId) ? intent.from : {
|
|
11207
|
+
story: intent.from.story,
|
|
11208
|
+
blockId,
|
|
11209
|
+
offset: 0
|
|
11210
|
+
};
|
|
11211
|
+
const to = idKey(blockId) === idKey(intent.to.blockId) ? intent.to : {
|
|
11212
|
+
story: intent.to.story,
|
|
11213
|
+
blockId,
|
|
11214
|
+
offset: paragraphLength(paragraph)
|
|
11215
|
+
};
|
|
11216
|
+
const start = resolveGap({
|
|
11217
|
+
paragraph,
|
|
11218
|
+
position: from,
|
|
11219
|
+
fallback: "insertion"
|
|
11220
|
+
});
|
|
11221
|
+
if (typeof start !== "object") return refuse("Invalid hyperlink range start.", start);
|
|
11222
|
+
const end = resolveGap({
|
|
11223
|
+
paragraph,
|
|
11224
|
+
position: to,
|
|
11225
|
+
fallback: "insertion"
|
|
11226
|
+
});
|
|
11227
|
+
if (typeof end !== "object") return refuse("Invalid hyperlink range end.", end);
|
|
11228
|
+
if (start.offset > end.offset || start.offset === end.offset && start.zeroWidthBefore > end.zeroWidthBefore) return refuse("Hyperlink range endpoints are reversed.", DOCUMENT_OP_REFUSAL_REASONS.INVALID_OFFSET);
|
|
11229
|
+
const [before = [], middle = [], after = []] = partitionContent(paragraph.content, [start, end]);
|
|
11230
|
+
pieces.push({
|
|
11231
|
+
paragraph,
|
|
11232
|
+
from,
|
|
11233
|
+
to,
|
|
11234
|
+
before: asParagraphContent(before),
|
|
11235
|
+
middle: asParagraphContent(middle),
|
|
11236
|
+
after: asParagraphContent(after)
|
|
11237
|
+
});
|
|
11238
|
+
}
|
|
11239
|
+
if (mode.type === "suggesting") return refuse("Hyperlink suggestions require serializable wrapper review provenance.");
|
|
11240
|
+
if (intent.type === "insertHyperlink") {
|
|
11241
|
+
if (intent.text === "") return refuse("A hyperlink insertion needs text.", DOCUMENT_OP_REFUSAL_REASONS.EMPTY_CONTENT);
|
|
11242
|
+
const content = [hyperlink(intent, [{
|
|
11243
|
+
type: "run",
|
|
11244
|
+
content: [{
|
|
11245
|
+
type: "text",
|
|
11246
|
+
text: intent.text
|
|
11247
|
+
}]
|
|
11248
|
+
}])];
|
|
11249
|
+
const deletion = compileEmptyReplacement(document, {
|
|
11250
|
+
from: intent.from,
|
|
11251
|
+
to: intent.to
|
|
11252
|
+
});
|
|
11253
|
+
if (deletion.isErr()) return deletion;
|
|
11254
|
+
const applied = applyDocumentOps(document, deletion.value.ops);
|
|
11255
|
+
if (applied.isErr()) return Result.err(applied.error);
|
|
11256
|
+
const at = deletion.value.selection;
|
|
11257
|
+
const target = selectedParagraphRuns(applied.value.document, at, at);
|
|
11258
|
+
if (target.isErr()) return Result.err(target.error);
|
|
11259
|
+
const paragraph = target.value.at(0)?.at(0)?.paragraph;
|
|
11260
|
+
if (!paragraph) return refuse("Hyperlink insertion lost its paragraph.");
|
|
11261
|
+
const gap = resolveGap({
|
|
11262
|
+
paragraph,
|
|
11263
|
+
position: at,
|
|
11264
|
+
fallback: "insertion"
|
|
11265
|
+
});
|
|
11266
|
+
if (typeof gap !== "object") return refuse("Invalid hyperlink insertion gap.", gap);
|
|
11267
|
+
const [before = [], after = []] = partitionContent(paragraph.content, [gap]);
|
|
11268
|
+
const replacement = asParagraphContent(before).concat(content, asParagraphContent(after));
|
|
11269
|
+
return Result.ok({
|
|
11270
|
+
ops: deletion.value.ops.concat([{
|
|
11271
|
+
type: DOCUMENT_OP_TYPES.REPLACE_INLINE,
|
|
11272
|
+
story: at.story,
|
|
11273
|
+
blockId: at.blockId,
|
|
11274
|
+
expected: paragraph.content,
|
|
11275
|
+
content: replacement
|
|
11276
|
+
}]),
|
|
11277
|
+
selection: {
|
|
11278
|
+
...at,
|
|
11279
|
+
offset: at.offset + intent.text.length,
|
|
11280
|
+
zeroWidthBefore: 0
|
|
11281
|
+
}
|
|
11282
|
+
});
|
|
11283
|
+
}
|
|
11284
|
+
const ops = [];
|
|
11285
|
+
for (const piece of pieces) {
|
|
11286
|
+
if (piece.from.offset === piece.to.offset) continue;
|
|
11287
|
+
const children = linkedChildren(piece.middle, intent);
|
|
11288
|
+
const replacement = intent.type === "setHyperlink" ? wrapLinkedChildren(intent, children) : children;
|
|
11289
|
+
if (structurallyEqual(piece.middle, replacement)) continue;
|
|
11290
|
+
ops.push({
|
|
11291
|
+
type: DOCUMENT_OP_TYPES.REPLACE_INLINE,
|
|
11292
|
+
story: intent.from.story,
|
|
11293
|
+
blockId: piece.from.blockId,
|
|
11294
|
+
expected: piece.paragraph.content,
|
|
11295
|
+
content: piece.before.concat(replacement, piece.after)
|
|
11296
|
+
});
|
|
11297
|
+
}
|
|
11298
|
+
return Result.ok({
|
|
11299
|
+
ops,
|
|
11300
|
+
selection: intent.to
|
|
11301
|
+
});
|
|
11302
|
+
};
|
|
11303
|
+
//#endregion
|
|
11304
|
+
//#region src/ops/editorIntentCensus.ts
|
|
11305
|
+
const sameFieldsExcept = (before, after, excluded) => {
|
|
11306
|
+
const beforeFields = Object.entries(before).filter(([key]) => !excluded.has(key));
|
|
11307
|
+
const afterFields = new Map(Object.entries(after).filter(([key]) => !excluded.has(key)));
|
|
11308
|
+
return beforeFields.length === afterFields.size && beforeFields.every(([key, value]) => afterFields.has(key) && afterFields.get(key) === value);
|
|
11309
|
+
};
|
|
11310
|
+
const PACKAGE_CONTENT_FIELDS = /* @__PURE__ */ new Set(["document"]);
|
|
11311
|
+
const DOCUMENT_CONTENT_FIELDS = /* @__PURE__ */ new Set(["content", "sections"]);
|
|
11312
|
+
const adjustCounts = ({ counts, keys, delta }) => {
|
|
11313
|
+
for (const key of keys) {
|
|
11314
|
+
const count = (counts.get(key) ?? 0) + delta;
|
|
11315
|
+
if (count < 0) panic("An incremental identity census cannot have a negative count.");
|
|
11316
|
+
if (count === 0) counts.delete(key);
|
|
11317
|
+
else counts.set(key, count);
|
|
11318
|
+
}
|
|
11319
|
+
};
|
|
11320
|
+
const censusOf = (identities, paragraphs) => {
|
|
11321
|
+
const idsIn = (space) => new Set([...identities.keys()].filter((key) => key.startsWith(`${space}:`)).map((key) => Number(key.slice(space.length + 1))));
|
|
11322
|
+
return {
|
|
11323
|
+
revisions: idsIn(IDENTITY_SPACES.REVISION),
|
|
11324
|
+
controls: idsIn(IDENTITY_SPACES.CONTROL),
|
|
11325
|
+
paragraphs: new Set(paragraphs.keys()),
|
|
11326
|
+
stories: /* @__PURE__ */ new Map()
|
|
11327
|
+
};
|
|
11328
|
+
};
|
|
11329
|
+
/** Cache immutable versions and update plain main stories without walking unchanged leaves. */
|
|
11330
|
+
const createCensusReader = () => {
|
|
11331
|
+
const versions = /* @__PURE__ */ new WeakMap();
|
|
11332
|
+
const paragraphKeys = /* @__PURE__ */ new WeakMap();
|
|
11333
|
+
let latest;
|
|
11334
|
+
const keysOf = (paragraph) => {
|
|
11335
|
+
let keys = paragraphKeys.get(paragraph);
|
|
11336
|
+
if (keys === void 0) {
|
|
11337
|
+
keys = {
|
|
11338
|
+
identities: identityKeysIn(paragraph).concat(reservedIdentityKeysIn(paragraph)),
|
|
11339
|
+
paragraphs: paragraphIdsIn(paragraph).map(idKey)
|
|
11340
|
+
};
|
|
11341
|
+
paragraphKeys.set(paragraph, keys);
|
|
11342
|
+
}
|
|
11343
|
+
return keys;
|
|
11344
|
+
};
|
|
11345
|
+
return (document) => {
|
|
11346
|
+
const cached = versions.get(document);
|
|
11347
|
+
if (cached !== void 0) {
|
|
11348
|
+
latest = cached;
|
|
11349
|
+
return cached.census;
|
|
11350
|
+
}
|
|
11351
|
+
const before = latest;
|
|
11352
|
+
const oldContent = before?.document.package.document.content;
|
|
11353
|
+
const content = document.package.document.content;
|
|
11354
|
+
let identities;
|
|
11355
|
+
let paragraphs;
|
|
11356
|
+
if (before !== void 0 && oldContent !== void 0 && oldContent.every((block) => block.type === "paragraph") && content.every((block) => block.type === "paragraph") && sameFieldsExcept(before.document.package, document.package, PACKAGE_CONTENT_FIELDS) && sameFieldsExcept(before.document.package.document, document.package.document, DOCUMENT_CONTENT_FIELDS)) {
|
|
11357
|
+
identities = new Map(before.identities);
|
|
11358
|
+
paragraphs = new Map(before.paragraphs);
|
|
11359
|
+
const changed = /* @__PURE__ */ new Map();
|
|
11360
|
+
for (const paragraph of oldContent) changed.set(paragraph, (changed.get(paragraph) ?? 0) - 1);
|
|
11361
|
+
for (const paragraph of content) changed.set(paragraph, (changed.get(paragraph) ?? 0) + 1);
|
|
11362
|
+
for (const [paragraph, delta] of changed) {
|
|
11363
|
+
if (delta === 0) continue;
|
|
11364
|
+
const keys = keysOf(paragraph);
|
|
11365
|
+
adjustCounts({
|
|
11366
|
+
counts: identities,
|
|
11367
|
+
keys: keys.identities,
|
|
11368
|
+
delta
|
|
11369
|
+
});
|
|
11370
|
+
adjustCounts({
|
|
11371
|
+
counts: paragraphs,
|
|
11372
|
+
keys: keys.paragraphs,
|
|
11373
|
+
delta
|
|
11374
|
+
});
|
|
11375
|
+
}
|
|
11376
|
+
} else {
|
|
11377
|
+
identities = countKeys(packageIdentityKeys(document.package).concat(reservedIdentityKeysIn(document.package)));
|
|
11378
|
+
paragraphs = countKeys(packageParagraphIds(document.package).map(idKey));
|
|
11379
|
+
}
|
|
11380
|
+
const version = {
|
|
11381
|
+
document,
|
|
11382
|
+
identities,
|
|
11383
|
+
paragraphs,
|
|
11384
|
+
census: censusOf(identities, paragraphs)
|
|
11385
|
+
};
|
|
11386
|
+
versions.set(document, version);
|
|
11387
|
+
latest = version;
|
|
11388
|
+
return version.census;
|
|
11389
|
+
};
|
|
9710
11390
|
};
|
|
9711
11391
|
//#endregion
|
|
9712
11392
|
//#region src/ops/rangeReplacement.ts
|
|
@@ -9740,7 +11420,7 @@ const planTrackedReplace = (document, options) => {
|
|
|
9740
11420
|
opType: DOCUMENT_OP_TYPES.INSERT_CONTENT
|
|
9741
11421
|
}));
|
|
9742
11422
|
if (replacement.tail.openStart !== 0 || replacement.tail.openEnd !== 0) return refuse("Tracked replacement requires a closed tail slice.");
|
|
9743
|
-
if (
|
|
11423
|
+
if (replacement.paragraphs.some(({ sectionProperties }) => sectionProperties !== void 0)) return refuse("Tracked replacement cannot import section-bearing paragraphs.");
|
|
9744
11424
|
const plan = createTrackedPlan({
|
|
9745
11425
|
document,
|
|
9746
11426
|
revision,
|
|
@@ -9752,6 +11432,35 @@ const planTrackedReplace = (document, options) => {
|
|
|
9752
11432
|
plan
|
|
9753
11433
|
});
|
|
9754
11434
|
if (deleted.isErr()) return Result.err(deleted.error);
|
|
11435
|
+
if (options.sourceContainerPolicy === "separate" && (replacement.tail.content.length > 0 || replacement.paragraphs.some((paragraph) => paragraph.content.length > 0))) {
|
|
11436
|
+
const deletionIds = new Set(plan.ops.flatMap((op) => op.type === DOCUMENT_OP_TYPES.DELETE_RANGE && op.revision !== void 0 ? [op.revision.id] : []));
|
|
11437
|
+
const deletionBlocks = new Set(plan.ops.flatMap((op) => {
|
|
11438
|
+
if (op.type === DOCUMENT_OP_TYPES.DELETE_RANGE) return [idKey(op.from.blockId)];
|
|
11439
|
+
if (op.type === DOCUMENT_OP_TYPES.JOIN_BLOCKS) return [idKey(op.nextBlockId)];
|
|
11440
|
+
return [];
|
|
11441
|
+
}));
|
|
11442
|
+
const breakRemovedSeam = (node) => {
|
|
11443
|
+
let next = node;
|
|
11444
|
+
if (node.type === "deletion" && deletionIds.has(node.info.id) && node.resolutionJoins !== void 0 && node.resolutionJoins.remove > 0) next = Object.assign({}, node, { resolutionJoins: Object.assign({}, node.resolutionJoins, { remove: 0 }) });
|
|
11445
|
+
const children = childNodes(next);
|
|
11446
|
+
if (children === void 0) return next;
|
|
11447
|
+
const changed = children.map(breakRemovedSeam);
|
|
11448
|
+
return changed.every((child, index) => child === children[index]) ? next : rebuildNode(next, changed);
|
|
11449
|
+
};
|
|
11450
|
+
for (const { paragraph } of storyParagraphs(storyBody(plan.document(), from.story))) {
|
|
11451
|
+
if (!deletionBlocks.has(idKey(paragraph.paraId ?? ""))) continue;
|
|
11452
|
+
const content = paragraph.content.map(breakRemovedSeam);
|
|
11453
|
+
if (content.every((node, index) => node === paragraph.content[index])) continue;
|
|
11454
|
+
const patched = plan.append({
|
|
11455
|
+
type: DOCUMENT_OP_TYPES.REPLACE_INLINE,
|
|
11456
|
+
story: from.story,
|
|
11457
|
+
blockId: paragraph.paraId ?? "",
|
|
11458
|
+
expected: paragraph.content,
|
|
11459
|
+
content: asParagraphContent(content)
|
|
11460
|
+
});
|
|
11461
|
+
if (patched.isErr()) return Result.err(patched.error);
|
|
11462
|
+
}
|
|
11463
|
+
}
|
|
9755
11464
|
let at = rangeStartAfterDeletion({
|
|
9756
11465
|
before: document,
|
|
9757
11466
|
after: plan.document(),
|
|
@@ -9775,11 +11484,12 @@ const planTrackedReplace = (document, options) => {
|
|
|
9775
11484
|
openStart: 0,
|
|
9776
11485
|
openEnd: 0
|
|
9777
11486
|
},
|
|
11487
|
+
...options.seamPolicy === void 0 ? {} : { seamPolicy: options.seamPolicy },
|
|
9778
11488
|
revision
|
|
9779
11489
|
});
|
|
9780
11490
|
if (inserted.isErr()) return Result.err(inserted.error);
|
|
9781
11491
|
}
|
|
9782
|
-
const { type: _type, paraId, content: _content, sectionProperties: _section, pPrMark: _mark, ...newParagraph } = paragraph;
|
|
11492
|
+
const { type: _type, paraId, content: _content, sectionProperties: _section, pPrMark: _mark, propertyChanges: _changes, ...newParagraph } = paragraph;
|
|
9783
11493
|
const split = plan.append({
|
|
9784
11494
|
type: DOCUMENT_OP_TYPES.SPLIT_BLOCK,
|
|
9785
11495
|
at: {
|
|
@@ -9823,6 +11533,11 @@ const intentEndpoints = (intent) => {
|
|
|
9823
11533
|
fromOffset: 0,
|
|
9824
11534
|
toOffset: 0
|
|
9825
11535
|
};
|
|
11536
|
+
case "setHyperlink":
|
|
11537
|
+
case "removeHyperlink":
|
|
11538
|
+
case "insertHyperlink":
|
|
11539
|
+
case "replaceFragment":
|
|
11540
|
+
case "moveFragment":
|
|
9826
11541
|
case "replaceText":
|
|
9827
11542
|
case "insertAtom":
|
|
9828
11543
|
case "formatRun": return {
|
|
@@ -9839,6 +11554,13 @@ const intentEndpoints = (intent) => {
|
|
|
9839
11554
|
fromOffset: void 0,
|
|
9840
11555
|
toOffset: void 0
|
|
9841
11556
|
};
|
|
11557
|
+
case "generateTOC": return {
|
|
11558
|
+
story: intent.at.story,
|
|
11559
|
+
fromId: intent.at.blockId,
|
|
11560
|
+
toId: intent.at.blockId,
|
|
11561
|
+
fromOffset: intent.at.offset,
|
|
11562
|
+
toOffset: intent.at.offset
|
|
11563
|
+
};
|
|
9842
11564
|
case "setList": {
|
|
9843
11565
|
const first = intent.items.at(0);
|
|
9844
11566
|
const last = intent.items.at(-1);
|
|
@@ -9894,6 +11616,19 @@ const createEditorIntentIdAllocator = () => {
|
|
|
9894
11616
|
demand += leafRevisionDemand(span.ancestors.length);
|
|
9895
11617
|
}
|
|
9896
11618
|
}
|
|
11619
|
+
const incoming = intent.type === "replaceFragment" || intent.type === "moveFragment" ? intent.paragraphs : [];
|
|
11620
|
+
for (const paragraph of incoming) {
|
|
11621
|
+
demand += 4;
|
|
11622
|
+
for (const span of leafSpans(paragraph.content)) demand += 4 * (1 + span.ancestors.length);
|
|
11623
|
+
}
|
|
11624
|
+
if (incoming.length > 0) demand += 8;
|
|
11625
|
+
if (intent.type === "moveFragment") {
|
|
11626
|
+
const target = storyParagraphs(storyBody(document, intent.target.story)).find(({ paragraph }) => idKey(paragraph.paraId ?? "") === idKey(intent.target.blockId));
|
|
11627
|
+
if (target !== void 0) {
|
|
11628
|
+
demand += 3;
|
|
11629
|
+
for (const span of leafSpans(target.paragraph.content)) demand += 4 * (1 + span.ancestors.length);
|
|
11630
|
+
}
|
|
11631
|
+
}
|
|
9897
11632
|
const fresh = (occupied, count) => {
|
|
9898
11633
|
const ids = [];
|
|
9899
11634
|
for (let id = 1; id <= 2147483647 && ids.length < count; id += 1) if (!occupied.has(id)) ids.push(id);
|
|
@@ -10001,16 +11736,18 @@ const editorParagraphGroups = (document, story) => {
|
|
|
10001
11736
|
return groups;
|
|
10002
11737
|
};
|
|
10003
11738
|
const paragraphAt = (document, at) => storyParagraphs(storyBody(document, at.story)).find(({ paragraph }) => idKey(paragraph.paraId ?? "") === idKey(at.blockId))?.paragraph;
|
|
11739
|
+
/** Zero-width markers and empty leaves never supply the formatting of typed content. */
|
|
11740
|
+
const visibleFormattingSpans = (paragraph) => leafSpans(paragraph.content).filter(({ ancestors, before, after }) => after.offset > before.offset && !ancestors.some(isRemovedRevisionNode));
|
|
10004
11741
|
/** Capture authored formatting before deletion, using only visible runs. */
|
|
10005
11742
|
const formattingAt = (paragraph, offset) => {
|
|
10006
|
-
const spans =
|
|
11743
|
+
const spans = visibleFormattingSpans(paragraph);
|
|
10007
11744
|
return (spans.find(({ before, after }) => before.offset <= offset && offset < after.offset) ?? spans.findLast(({ after }) => after.offset <= offset) ?? spans.find(({ before }) => before.offset > offset))?.ancestors.findLast((ancestor) => ancestor.type === "run")?.formatting;
|
|
10008
11745
|
};
|
|
10009
11746
|
const authoredFormatting = (document, from, to) => {
|
|
10010
11747
|
const paragraph = paragraphAt(document, from);
|
|
10011
11748
|
if (paragraph === void 0) panic("A validated input paragraph must exist when reading authored formatting.");
|
|
10012
11749
|
if (!(from.blockId === to.blockId && from.offset === to.offset)) return formattingAt(paragraph, from.offset);
|
|
10013
|
-
if (
|
|
11750
|
+
if (visibleFormattingSpans(paragraph).some(({ before }) => before.offset < from.offset)) return formattingAt(paragraph, from.offset - 1);
|
|
10014
11751
|
const group = editorParagraphGroups(document, from.story).find(({ paragraphs }) => paragraphs.some(({ paraId }) => idKey(paraId ?? "") === idKey(from.blockId)));
|
|
10015
11752
|
const index = group?.paragraphs.findIndex(({ paraId }) => idKey(paraId ?? "") === idKey(from.blockId)) ?? 0;
|
|
10016
11753
|
const previous = group?.paragraphs.slice(0, index).findLast((item) => paragraphVisibleText(item) !== "");
|
|
@@ -10025,6 +11762,144 @@ const splitParagraphFields = (document, at) => {
|
|
|
10025
11762
|
const survivor = editorParagraphGroups(document, at.story).find(({ paragraphs }) => paragraphs.some(({ paraId }) => idKey(paraId ?? "") === idKey(at.blockId)))?.paragraphs.at(-1);
|
|
10026
11763
|
return survivor?.formatting === void 0 ? {} : { formatting: survivor.formatting };
|
|
10027
11764
|
};
|
|
11765
|
+
/** A compound intent consumes one identity pool across its sequential plans. */
|
|
11766
|
+
const modeAfterOps = (document, mode) => {
|
|
11767
|
+
if (mode.newIds === void 0) return mode;
|
|
11768
|
+
const occupied = new Set(packageIdentityKeys(document.package).concat(reservedIdentityKeysIn(document.package)));
|
|
11769
|
+
const revisionIds = mode.newIds.revision?.filter((id) => !occupied.has(`${IDENTITY_SPACES.REVISION}:${id}`));
|
|
11770
|
+
const controlIds = mode.newIds.control?.filter((id) => !occupied.has(`${IDENTITY_SPACES.CONTROL}:${id}`));
|
|
11771
|
+
const newIds = {
|
|
11772
|
+
...revisionIds === void 0 ? {} : { revision: revisionIds },
|
|
11773
|
+
...controlIds === void 0 ? {} : { control: controlIds }
|
|
11774
|
+
};
|
|
11775
|
+
if (mode.type === "editing") return {
|
|
11776
|
+
type: "editing",
|
|
11777
|
+
newIds
|
|
11778
|
+
};
|
|
11779
|
+
if (!occupied.has(`${IDENTITY_SPACES.REVISION}:${mode.revision.id}`)) return {
|
|
11780
|
+
type: "suggesting",
|
|
11781
|
+
revision: mode.revision,
|
|
11782
|
+
newIds
|
|
11783
|
+
};
|
|
11784
|
+
const id = newIds.revision?.at(0) ?? 2147483648;
|
|
11785
|
+
return {
|
|
11786
|
+
type: "suggesting",
|
|
11787
|
+
revision: {
|
|
11788
|
+
...mode.revision,
|
|
11789
|
+
id
|
|
11790
|
+
},
|
|
11791
|
+
newIds: {
|
|
11792
|
+
...newIds,
|
|
11793
|
+
...revisionIds === void 0 ? {} : { revision: revisionIds.slice(1) }
|
|
11794
|
+
}
|
|
11795
|
+
};
|
|
11796
|
+
};
|
|
11797
|
+
/** Imported identities are package-local facts, never destination identities. */
|
|
11798
|
+
const isCopiedRangeStart = (value) => "type" in value && (value.type === "bookmarkStart" || value.type === "moveFromRangeStart" || value.type === "moveToRangeStart");
|
|
11799
|
+
const isCopiedRangeMarker = (value) => isCopiedRangeStart(value) || "type" in value && (value.type === "bookmarkEnd" || value.type === "moveFromRangeEnd" || value.type === "moveToRangeEnd");
|
|
11800
|
+
const isMoveAnchor = (node) => isCommentAnchor(node) || isParagraphContent(node) && isRangeBoundary(node);
|
|
11801
|
+
/** Suggested moves retain source anchors; their inserted copy carries only tracked content. */
|
|
11802
|
+
const withoutMoveAnchors = (nodes) => {
|
|
11803
|
+
const out = [];
|
|
11804
|
+
for (const node of nodes) {
|
|
11805
|
+
if (isMoveAnchor(node)) continue;
|
|
11806
|
+
const children = childNodes(node);
|
|
11807
|
+
const retained = children === void 0 ? node : rebuildNode(node, withoutMoveAnchors(children));
|
|
11808
|
+
if (!isEmptyRecord(retained)) out.push(retained);
|
|
11809
|
+
}
|
|
11810
|
+
return out;
|
|
11811
|
+
};
|
|
11812
|
+
const identifyClipboardParagraphs = ({ document, paragraphs, mode, firstBlockId }) => {
|
|
11813
|
+
const copy = cloneModel([...paragraphs]);
|
|
11814
|
+
const occupied = new Set(packageIdentityKeys(document.package).concat(reservedIdentityKeysIn(document.package)));
|
|
11815
|
+
for (const id of mode.newIds?.revision ?? []) occupied.add(`${IDENTITY_SPACES.REVISION}:${id}`);
|
|
11816
|
+
for (const id of mode.newIds?.control ?? []) occupied.add(`${IDENTITY_SPACES.CONTROL}:${id}`);
|
|
11817
|
+
if (mode.type === "suggesting") occupied.add(`${IDENTITY_SPACES.REVISION}:${mode.revision.id}`);
|
|
11818
|
+
const blockIds = new Set(packageParagraphIds(document.package).map(idKey));
|
|
11819
|
+
const markers = /* @__PURE__ */ new Set();
|
|
11820
|
+
const names = /* @__PURE__ */ new Set();
|
|
11821
|
+
const renamedBookmarks = /* @__PURE__ */ new Map();
|
|
11822
|
+
const visit = (value, callback) => {
|
|
11823
|
+
if (typeof value !== "object" || value === null) return;
|
|
11824
|
+
if (Array.isArray(value)) {
|
|
11825
|
+
for (const item of value) visit(item, callback);
|
|
11826
|
+
return;
|
|
11827
|
+
}
|
|
11828
|
+
callback(value);
|
|
11829
|
+
for (const field of Object.values(value)) visit(field, callback);
|
|
11830
|
+
};
|
|
11831
|
+
visit(document.package, (value) => {
|
|
11832
|
+
if ("type" in value && "id" in value && typeof value.id === "number" && isCopiedRangeMarker(value)) markers.add(value.id);
|
|
11833
|
+
if (isCopiedRangeStart(value) && "name" in value && typeof value.name === "string") names.add(value.name);
|
|
11834
|
+
});
|
|
11835
|
+
visit(copy, (value) => {
|
|
11836
|
+
if (!isCopiedRangeStart(value) || !("name" in value) || typeof value.name !== "string") return;
|
|
11837
|
+
const original = value.name;
|
|
11838
|
+
const renamed = renamedBookmarks.get(original);
|
|
11839
|
+
if (renamed !== void 0) {
|
|
11840
|
+
value.name = renamed;
|
|
11841
|
+
return;
|
|
11842
|
+
}
|
|
11843
|
+
let name = original;
|
|
11844
|
+
let suffix = 1;
|
|
11845
|
+
while (names.has(name)) {
|
|
11846
|
+
name = `${original}_paste${suffix}`;
|
|
11847
|
+
suffix += 1;
|
|
11848
|
+
}
|
|
11849
|
+
names.add(name);
|
|
11850
|
+
renamedBookmarks.set(original, name);
|
|
11851
|
+
value.name = name;
|
|
11852
|
+
});
|
|
11853
|
+
const remapped = /* @__PURE__ */ new Map();
|
|
11854
|
+
const nextInSpace = /* @__PURE__ */ new Map();
|
|
11855
|
+
let nextBlock = firstBlockId ?? 1;
|
|
11856
|
+
const freshBlock = () => {
|
|
11857
|
+
while (nextBlock <= 2147483647 && blockIds.has(nextBlock.toString(16).padStart(8, "0").toUpperCase())) nextBlock += 1;
|
|
11858
|
+
if (nextBlock > 2147483647) return void 0;
|
|
11859
|
+
const id = nextBlock.toString(16).padStart(8, "0").toUpperCase();
|
|
11860
|
+
blockIds.add(id);
|
|
11861
|
+
nextBlock += 1;
|
|
11862
|
+
return id;
|
|
11863
|
+
};
|
|
11864
|
+
const freshId = (space, oldId) => {
|
|
11865
|
+
const key = `${space}:${oldId}`;
|
|
11866
|
+
const existing = remapped.get(key);
|
|
11867
|
+
if (existing !== void 0) return existing;
|
|
11868
|
+
let id = nextInSpace.get(space) ?? 1;
|
|
11869
|
+
while (space === "bookmark" ? markers.has(id) : occupied.has(`${space}:${id}`)) id += 1;
|
|
11870
|
+
if (space === "bookmark") markers.add(id);
|
|
11871
|
+
else occupied.add(`${space}:${id}`);
|
|
11872
|
+
remapped.set(key, id);
|
|
11873
|
+
nextInSpace.set(space, id + 1);
|
|
11874
|
+
return id;
|
|
11875
|
+
};
|
|
11876
|
+
const remintedControls = /* @__PURE__ */ new WeakSet();
|
|
11877
|
+
visit(copy, (value) => {
|
|
11878
|
+
if ("type" in value && value.type === "inlineSdt" && "properties" in value && typeof value.properties === "object" && value.properties !== null && "id" in value.properties && typeof value.properties.id === "number") {
|
|
11879
|
+
value.properties.id = freshId(IDENTITY_SPACES.CONTROL, value.properties.id);
|
|
11880
|
+
remintedControls.add(value.properties);
|
|
11881
|
+
}
|
|
11882
|
+
if ("type" in value && value.type === "hyperlink" && "anchor" in value && typeof value.anchor === "string") value.anchor = renamedBookmarks.get(value.anchor) ?? value.anchor;
|
|
11883
|
+
if ("id" in value && typeof value.id === "number" && !remintedControls.has(value)) {
|
|
11884
|
+
if ("space" in value && (value.space === IDENTITY_SPACES.REVISION || value.space === IDENTITY_SPACES.CONTROL)) value.id = freshId(value.space, value.id);
|
|
11885
|
+
else if ("sdtType" in value) value.id = freshId(IDENTITY_SPACES.CONTROL, value.id);
|
|
11886
|
+
else if ("author" in value && !("type" in value)) value.id = freshId(IDENTITY_SPACES.REVISION, value.id);
|
|
11887
|
+
else if (isCopiedRangeMarker(value)) value.id = freshId("bookmark", value.id);
|
|
11888
|
+
}
|
|
11889
|
+
});
|
|
11890
|
+
for (const [index, paragraph] of copy.entries()) {
|
|
11891
|
+
delete paragraph.paraId;
|
|
11892
|
+
if (index === copy.length - 1) continue;
|
|
11893
|
+
const id = freshBlock();
|
|
11894
|
+
if (id === void 0) return Result.err(new DocumentOpRefusal({
|
|
11895
|
+
reason: DOCUMENT_OP_REFUSAL_REASONS.INVALID_BLOCK_ID,
|
|
11896
|
+
message: "The pasted block identity allocator is exhausted.",
|
|
11897
|
+
opType: DOCUMENT_OP_TYPES.SPLIT_BLOCK
|
|
11898
|
+
}));
|
|
11899
|
+
paragraph.paraId = id;
|
|
11900
|
+
}
|
|
11901
|
+
return Result.ok(copy);
|
|
11902
|
+
};
|
|
10028
11903
|
/** Direct and tracked joins choose paragraph properties from the same editing-view group. */
|
|
10029
11904
|
const joinParagraphPropertyAlignment = ({ document, current, intent, revision }) => {
|
|
10030
11905
|
const groups = editorParagraphGroups(document, intent.story);
|
|
@@ -10064,7 +11939,12 @@ const textSeamDepth = (paragraph, offset) => {
|
|
|
10064
11939
|
return 0;
|
|
10065
11940
|
};
|
|
10066
11941
|
/** Compile one batch; the caller applies it atomically and journals its exact inverse. */
|
|
10067
|
-
const compileEditorIntent = (document, { intent, mode }) => {
|
|
11942
|
+
const compileEditorIntent = (document, { intent, mode, firstBlockId }) => {
|
|
11943
|
+
if (firstBlockId !== void 0 && (!Number.isInteger(firstBlockId) || firstBlockId < 1 || firstBlockId > 2147483648)) return Result.err(new DocumentOpRefusal({
|
|
11944
|
+
reason: DOCUMENT_OP_REFUSAL_REASONS.INVALID_BLOCK_ID,
|
|
11945
|
+
message: "The pasted block identity allocator is exhausted or invalid.",
|
|
11946
|
+
opType: DOCUMENT_OP_TYPES.SPLIT_BLOCK
|
|
11947
|
+
}));
|
|
10068
11948
|
const allocationFields = mode.newIds === void 0 ? {} : { newIds: mode.newIds };
|
|
10069
11949
|
const tracked = mode.type === "suggesting" ? {
|
|
10070
11950
|
revision: mode.revision,
|
|
@@ -10072,14 +11952,486 @@ const compileEditorIntent = (document, { intent, mode }) => {
|
|
|
10072
11952
|
} : allocationFields;
|
|
10073
11953
|
let ops;
|
|
10074
11954
|
let selection;
|
|
11955
|
+
const editedSeams = [];
|
|
10075
11956
|
switch (intent.type) {
|
|
11957
|
+
case "generateTOC": {
|
|
11958
|
+
const compiled = compileGenerateTOCIntent(document, intent, mode);
|
|
11959
|
+
if (compiled.isErr()) return compiled;
|
|
11960
|
+
ops = compiled.value.ops;
|
|
11961
|
+
selection = compiled.value.selection;
|
|
11962
|
+
break;
|
|
11963
|
+
}
|
|
11964
|
+
case "setHyperlink":
|
|
11965
|
+
case "removeHyperlink":
|
|
11966
|
+
case "insertHyperlink": return compileHyperlinkIntent(document, {
|
|
11967
|
+
intent,
|
|
11968
|
+
mode,
|
|
11969
|
+
compileEmptyReplacement: (source, { from, to }) => compileEditorIntent(source, {
|
|
11970
|
+
intent: {
|
|
11971
|
+
type: "replaceText",
|
|
11972
|
+
from,
|
|
11973
|
+
to,
|
|
11974
|
+
text: ""
|
|
11975
|
+
},
|
|
11976
|
+
mode
|
|
11977
|
+
})
|
|
11978
|
+
});
|
|
11979
|
+
case "replaceFragment": {
|
|
11980
|
+
if (intent.paragraphs.length === 0) return compileEditorIntent(document, {
|
|
11981
|
+
intent: {
|
|
11982
|
+
type: "replaceText",
|
|
11983
|
+
from: intent.from,
|
|
11984
|
+
to: intent.to,
|
|
11985
|
+
text: ""
|
|
11986
|
+
},
|
|
11987
|
+
mode
|
|
11988
|
+
});
|
|
11989
|
+
const destination = paragraphAt(document, intent.from);
|
|
11990
|
+
if (destination === void 0) return Result.err(new DocumentOpRefusal({
|
|
11991
|
+
reason: DOCUMENT_OP_REFUSAL_REASONS.BLOCK_NOT_FOUND,
|
|
11992
|
+
message: "The clipboard destination paragraph does not exist.",
|
|
11993
|
+
opType: DOCUMENT_OP_TYPES.INSERT_CONTENT
|
|
11994
|
+
}));
|
|
11995
|
+
const endpoint = paragraphAt(document, intent.to);
|
|
11996
|
+
const destinationFormatting = splitParagraphFields(document, intent.from).formatting;
|
|
11997
|
+
const endpointFormatting = splitParagraphFields(document, intent.to).formatting;
|
|
11998
|
+
const prefixBoundary = intent.openStart === 0 && intent.from.offset > 0;
|
|
11999
|
+
const suffixBoundary = intent.openEnd === 0 && endpoint !== void 0 && intent.to.offset < paragraphLength(endpoint);
|
|
12000
|
+
const normalized = [
|
|
12001
|
+
...prefixBoundary ? [{
|
|
12002
|
+
type: "paragraph",
|
|
12003
|
+
content: [],
|
|
12004
|
+
...destinationFormatting === void 0 ? {} : { formatting: destinationFormatting }
|
|
12005
|
+
}] : [],
|
|
12006
|
+
...intent.paragraphs,
|
|
12007
|
+
...suffixBoundary ? [{
|
|
12008
|
+
type: "paragraph",
|
|
12009
|
+
content: [],
|
|
12010
|
+
...endpointFormatting === void 0 ? {} : { formatting: endpointFormatting }
|
|
12011
|
+
}] : []
|
|
12012
|
+
];
|
|
12013
|
+
const identified = identifyClipboardParagraphs({
|
|
12014
|
+
document,
|
|
12015
|
+
paragraphs: normalized,
|
|
12016
|
+
mode,
|
|
12017
|
+
firstBlockId
|
|
12018
|
+
});
|
|
12019
|
+
if (identified.isErr()) return identified;
|
|
12020
|
+
const paragraphs = identified.value;
|
|
12021
|
+
for (const [index, paragraph] of paragraphs.entries()) {
|
|
12022
|
+
const openStart = index === 0 && intent.openStart === 1;
|
|
12023
|
+
const openEnd = index === normalized.length - 1 && intent.openEnd === 1;
|
|
12024
|
+
if (!openStart && !openEnd) continue;
|
|
12025
|
+
delete paragraph.propertyChanges;
|
|
12026
|
+
delete paragraph.pPrMark;
|
|
12027
|
+
delete paragraph.reviewCarrier;
|
|
12028
|
+
delete paragraph.sectionProperties;
|
|
12029
|
+
delete paragraph.formatting;
|
|
12030
|
+
const inherited = openStart ? destinationFormatting : endpointFormatting;
|
|
12031
|
+
if (inherited !== void 0) paragraph.formatting = inherited;
|
|
12032
|
+
}
|
|
12033
|
+
if (paragraphs.some((paragraph) => paragraph.reviewCarrier !== void 0)) return Result.err(new DocumentOpRefusal({
|
|
12034
|
+
reason: DOCUMENT_OP_REFUSAL_REASONS.UNTRACKABLE,
|
|
12035
|
+
message: "A clipboard paragraph cannot import a private review-resolution carrier.",
|
|
12036
|
+
opType: DOCUMENT_OP_TYPES.INSERT_CONTENT
|
|
12037
|
+
}));
|
|
12038
|
+
if (paragraphs.some((paragraph) => (paragraph.sectionProperties?.headerReferences?.length ?? 0) > 0 || (paragraph.sectionProperties?.footerReferences?.length ?? 0) > 0)) return Result.err(new DocumentOpRefusal({
|
|
12039
|
+
reason: DOCUMENT_OP_REFUSAL_REASONS.SECTION_BOUNDARY,
|
|
12040
|
+
message: "Clipboard section headers and footers require importing their source package parts.",
|
|
12041
|
+
opType: DOCUMENT_OP_TYPES.SET_SECTION_ENDPOINT
|
|
12042
|
+
}));
|
|
12043
|
+
if (mode.type === "suggesting" && paragraphs.some((paragraph) => paragraph.pPrMark !== void 0)) return Result.err(new DocumentOpRefusal({
|
|
12044
|
+
reason: DOCUMENT_OP_REFUSAL_REASONS.UNTRACKABLE,
|
|
12045
|
+
message: "A copied paragraph mark cannot coexist with the tracked paste mark in the paragraph's single mark slot.",
|
|
12046
|
+
opType: DOCUMENT_OP_TYPES.SET_PARAGRAPH_REVIEW
|
|
12047
|
+
}));
|
|
12048
|
+
const tail = paragraphs.at(-1);
|
|
12049
|
+
if (mode.type === "suggesting" && tail !== void 0 && (tail.sectionProperties !== void 0 || tail.pPrMark !== void 0 || (tail.propertyChanges?.length ?? 0) > 0)) return Result.err(new DocumentOpRefusal({
|
|
12050
|
+
reason: DOCUMENT_OP_REFUSAL_REASONS.UNTRACKABLE,
|
|
12051
|
+
message: "A tracked paste cannot attach copied paragraph review or section metadata to a retained destination mark without a separately rejectable mark change.",
|
|
12052
|
+
opType: DOCUMENT_OP_TYPES.SET_PARAGRAPH_REVIEW
|
|
12053
|
+
}));
|
|
12054
|
+
const leading = paragraphs.slice(0, -1);
|
|
12055
|
+
if (mode.type === "suggesting") {
|
|
12056
|
+
const planned = planTrackedReplace(document, {
|
|
12057
|
+
sourceContainerPolicy: "separate",
|
|
12058
|
+
from: intent.from,
|
|
12059
|
+
to: intent.to,
|
|
12060
|
+
revision: mode.revision,
|
|
12061
|
+
newIds: mode.newIds,
|
|
12062
|
+
seamPolicy: INSERTION_SEAM_POLICIES.MERGE_PLAIN_RUNS,
|
|
12063
|
+
replacement: {
|
|
12064
|
+
paragraphs: leading.map(({ sectionProperties: _section, ...paragraph }) => paragraph),
|
|
12065
|
+
tail: {
|
|
12066
|
+
openStart: 0,
|
|
12067
|
+
openEnd: 0,
|
|
12068
|
+
content: tail?.content ?? []
|
|
12069
|
+
}
|
|
12070
|
+
}
|
|
12071
|
+
});
|
|
12072
|
+
if (planned.isErr()) return Result.err(planned.error);
|
|
12073
|
+
ops = planned.value;
|
|
12074
|
+
const applied = applyDocumentOps(document, ops);
|
|
12075
|
+
if (applied.isErr()) return Result.err(applied.error);
|
|
12076
|
+
const at = rangeStartAfterDeletion({
|
|
12077
|
+
before: document,
|
|
12078
|
+
after: applied.value.document,
|
|
12079
|
+
from: intent.from,
|
|
12080
|
+
to: intent.to
|
|
12081
|
+
});
|
|
12082
|
+
const insertionAt = leading.length > 0 ? {
|
|
12083
|
+
...at,
|
|
12084
|
+
offset: 0,
|
|
12085
|
+
zeroWidthBefore: 0
|
|
12086
|
+
} : at;
|
|
12087
|
+
const insertionParagraph = paragraphAt(document, insertionAt);
|
|
12088
|
+
selection = {
|
|
12089
|
+
...insertionAt,
|
|
12090
|
+
...gapAfterInserted({
|
|
12091
|
+
offset: insertionAt.offset,
|
|
12092
|
+
zeroWidthBefore: insertionAt.zeroWidthBefore ?? (insertionParagraph === void 0 ? 0 : defaultInsertionGap(insertionParagraph.content, insertionAt.offset).zeroWidthBefore)
|
|
12093
|
+
}, tail?.content ?? [])
|
|
12094
|
+
};
|
|
12095
|
+
} else {
|
|
12096
|
+
const deleted = compileEditorIntent(document, {
|
|
12097
|
+
intent: {
|
|
12098
|
+
type: "replaceText",
|
|
12099
|
+
from: intent.from,
|
|
12100
|
+
to: intent.to,
|
|
12101
|
+
text: ""
|
|
12102
|
+
},
|
|
12103
|
+
mode
|
|
12104
|
+
});
|
|
12105
|
+
if (deleted.isErr()) return deleted;
|
|
12106
|
+
ops = [...deleted.value.ops];
|
|
12107
|
+
let at = {
|
|
12108
|
+
...deleted.value.selection,
|
|
12109
|
+
zeroWidthBefore: deleted.value.selection.zeroWidthBefore ?? defaultInsertionGap(destination.content, intent.from.offset).zeroWidthBefore
|
|
12110
|
+
};
|
|
12111
|
+
for (const paragraph of leading) {
|
|
12112
|
+
if (paragraph.content.length > 0) ops.push({
|
|
12113
|
+
type: DOCUMENT_OP_TYPES.INSERT_CONTENT,
|
|
12114
|
+
at,
|
|
12115
|
+
slice: {
|
|
12116
|
+
openStart: 0,
|
|
12117
|
+
openEnd: 0,
|
|
12118
|
+
content: paragraph.content
|
|
12119
|
+
},
|
|
12120
|
+
seamPolicy: INSERTION_SEAM_POLICIES.MERGE_PLAIN_RUNS,
|
|
12121
|
+
...allocationFields
|
|
12122
|
+
});
|
|
12123
|
+
const { type: _type, paraId, content: _content, sectionProperties: _section, pPrMark: _mark, ...newParagraph } = paragraph;
|
|
12124
|
+
ops.push({
|
|
12125
|
+
type: DOCUMENT_OP_TYPES.SPLIT_BLOCK,
|
|
12126
|
+
at: {
|
|
12127
|
+
...at,
|
|
12128
|
+
...gapAfterInserted({
|
|
12129
|
+
offset: at.offset,
|
|
12130
|
+
zeroWidthBefore: at.zeroWidthBefore ?? 0
|
|
12131
|
+
}, paragraph.content)
|
|
12132
|
+
},
|
|
12133
|
+
newBlockId: paraId ?? "",
|
|
12134
|
+
newHalf: SPLIT_HALVES.FIRST,
|
|
12135
|
+
newParagraph,
|
|
12136
|
+
...allocationFields
|
|
12137
|
+
});
|
|
12138
|
+
at = {
|
|
12139
|
+
story: at.story,
|
|
12140
|
+
blockId: at.blockId,
|
|
12141
|
+
offset: 0,
|
|
12142
|
+
zeroWidthBefore: 0
|
|
12143
|
+
};
|
|
12144
|
+
}
|
|
12145
|
+
if (tail !== void 0 && tail.content.length > 0) ops.push({
|
|
12146
|
+
type: DOCUMENT_OP_TYPES.INSERT_CONTENT,
|
|
12147
|
+
at,
|
|
12148
|
+
slice: {
|
|
12149
|
+
openStart: 0,
|
|
12150
|
+
openEnd: 0,
|
|
12151
|
+
content: tail.content
|
|
12152
|
+
},
|
|
12153
|
+
...allocationFields
|
|
12154
|
+
});
|
|
12155
|
+
selection = {
|
|
12156
|
+
...at,
|
|
12157
|
+
...gapAfterInserted({
|
|
12158
|
+
offset: at.offset,
|
|
12159
|
+
zeroWidthBefore: at.zeroWidthBefore ?? 0
|
|
12160
|
+
}, tail?.content ?? [])
|
|
12161
|
+
};
|
|
12162
|
+
}
|
|
12163
|
+
if (mode.type === "editing" && intent.openStart === 1 && leading.length > 0) {
|
|
12164
|
+
const first = leading.at(0);
|
|
12165
|
+
if (first?.paraId === void 0) panic("An identified leading clipboard paragraph must have an id.");
|
|
12166
|
+
editedSeams.push({
|
|
12167
|
+
story: intent.from.story,
|
|
12168
|
+
blockId: first.paraId,
|
|
12169
|
+
offset: intent.from.offset
|
|
12170
|
+
});
|
|
12171
|
+
}
|
|
12172
|
+
if (tail !== void 0) {
|
|
12173
|
+
const applied = applyDocumentOps(document, ops);
|
|
12174
|
+
if (applied.isErr()) return Result.err(applied.error);
|
|
12175
|
+
const trailing = editorParagraphGroups(applied.value.document, selection.story).find(({ paragraphs: group }) => group.some(({ paraId }) => idKey(paraId ?? "") === idKey(selection.blockId)))?.paragraphs.at(-1);
|
|
12176
|
+
const trailingMarkAt = trailing?.paraId === void 0 ? selection : {
|
|
12177
|
+
...selection,
|
|
12178
|
+
blockId: trailing.paraId,
|
|
12179
|
+
offset: 0
|
|
12180
|
+
};
|
|
12181
|
+
const current = paragraphAt(applied.value.document, trailingMarkAt);
|
|
12182
|
+
const patch = Object.fromEntries([...Object.keys(current?.formatting ?? {}).map((key) => [key, null]), ...Object.entries(tail.formatting ?? {})]);
|
|
12183
|
+
const formatting = compileEditorIntent(applied.value.document, {
|
|
12184
|
+
intent: {
|
|
12185
|
+
type: "formatParagraph",
|
|
12186
|
+
at: trailingMarkAt,
|
|
12187
|
+
patch
|
|
12188
|
+
},
|
|
12189
|
+
mode: modeAfterOps(applied.value.document, mode)
|
|
12190
|
+
});
|
|
12191
|
+
if (formatting.isErr()) return formatting;
|
|
12192
|
+
ops.push(...formatting.value.ops);
|
|
12193
|
+
}
|
|
12194
|
+
for (const [index, paragraph] of paragraphs.entries()) {
|
|
12195
|
+
if (paragraph.pPrMark === void 0 && (paragraph.propertyChanges?.length ?? 0) === 0 && paragraph.sectionProperties === void 0) continue;
|
|
12196
|
+
const blockId = index === paragraphs.length - 1 ? selection.blockId : paragraph.paraId ?? "";
|
|
12197
|
+
const applied = applyDocumentOps(document, ops);
|
|
12198
|
+
if (applied.isErr()) return Result.err(applied.error);
|
|
12199
|
+
const current = paragraphAt(applied.value.document, {
|
|
12200
|
+
story: selection.story,
|
|
12201
|
+
blockId,
|
|
12202
|
+
offset: 0
|
|
12203
|
+
});
|
|
12204
|
+
if (current === void 0) panic("An allocated clipboard paragraph must remain present.");
|
|
12205
|
+
if (paragraph.pPrMark !== void 0 && current.pPrMark !== void 0) return Result.err(new DocumentOpRefusal({
|
|
12206
|
+
reason: DOCUMENT_OP_REFUSAL_REASONS.REVISION_CONFLICT,
|
|
12207
|
+
message: "A copied paragraph mark cannot replace the destination's pending paragraph mark.",
|
|
12208
|
+
opType: DOCUMENT_OP_TYPES.SET_PARAGRAPH_REVIEW
|
|
12209
|
+
}));
|
|
12210
|
+
const expected = reviewFieldsOf(current);
|
|
12211
|
+
const copiedChange = paragraph.propertyChanges?.at(0);
|
|
12212
|
+
const currentChange = current.propertyChanges?.at(0);
|
|
12213
|
+
if (copiedChange !== void 0 && currentChange !== void 0 && packageIdentityKeys(document.package).includes(`${IDENTITY_SPACES.REVISION}:${currentChange.info.id}`)) return Result.err(new DocumentOpRefusal({
|
|
12214
|
+
reason: DOCUMENT_OP_REFUSAL_REASONS.REVISION_CONFLICT,
|
|
12215
|
+
message: "Copied paragraph review cannot replace a pre-existing destination review.",
|
|
12216
|
+
opType: DOCUMENT_OP_TYPES.SET_PARAGRAPH_REVIEW
|
|
12217
|
+
}));
|
|
12218
|
+
const changes = [...paragraph.propertyChanges ?? current.propertyChanges ?? []];
|
|
12219
|
+
if (paragraph.pPrMark !== void 0 || copiedChange !== void 0) {
|
|
12220
|
+
const review = reviewFieldsOf(current);
|
|
12221
|
+
if (changes.length > 0) review.propertyChanges = changes;
|
|
12222
|
+
if (paragraph.pPrMark !== void 0) review.pPrMark = paragraph.pPrMark;
|
|
12223
|
+
ops.push({
|
|
12224
|
+
type: DOCUMENT_OP_TYPES.SET_PARAGRAPH_REVIEW,
|
|
12225
|
+
story: selection.story,
|
|
12226
|
+
blockId,
|
|
12227
|
+
expected,
|
|
12228
|
+
review
|
|
12229
|
+
});
|
|
12230
|
+
}
|
|
12231
|
+
if (paragraph.sectionProperties !== void 0) ops.push({
|
|
12232
|
+
type: DOCUMENT_OP_TYPES.SET_SECTION_ENDPOINT,
|
|
12233
|
+
endpoint: {
|
|
12234
|
+
type: "paragraph",
|
|
12235
|
+
blockId
|
|
12236
|
+
},
|
|
12237
|
+
expected: current.sectionProperties === void 0 ? { type: Object.hasOwn(current, "sectionProperties") ? "undefined" : "omitted" } : {
|
|
12238
|
+
type: "present",
|
|
12239
|
+
value: current.sectionProperties
|
|
12240
|
+
},
|
|
12241
|
+
properties: {
|
|
12242
|
+
type: "present",
|
|
12243
|
+
value: paragraph.sectionProperties
|
|
12244
|
+
}
|
|
12245
|
+
});
|
|
12246
|
+
}
|
|
12247
|
+
if (mode.type === "editing" && tail !== void 0) {
|
|
12248
|
+
const width = paragraphLength(tail);
|
|
12249
|
+
if (width > 0) {
|
|
12250
|
+
for (const offset of /* @__PURE__ */ new Set([selection.offset, selection.offset - width])) if (offset >= 0) editedSeams.push({
|
|
12251
|
+
...selection,
|
|
12252
|
+
offset
|
|
12253
|
+
});
|
|
12254
|
+
}
|
|
12255
|
+
}
|
|
12256
|
+
if (intent.openEnd === 0 && endpoint !== void 0 && intent.to.offset === paragraphLength(endpoint)) {
|
|
12257
|
+
const bodyParagraphs = storyParagraphs(storyBody(document, intent.to.story));
|
|
12258
|
+
const endLocation = bodyParagraphs.find(({ paragraph }) => idKey(paragraph.paraId ?? "") === idKey(intent.to.blockId));
|
|
12259
|
+
const following = bodyParagraphs.find((location) => endLocation !== void 0 && sameBlockList(location.list, endLocation.list) && location.index === endLocation.index + 1);
|
|
12260
|
+
if (following?.paragraph.paraId !== void 0) selection = {
|
|
12261
|
+
story: intent.to.story,
|
|
12262
|
+
blockId: following.paragraph.paraId,
|
|
12263
|
+
...defaultInsertionGap(following.paragraph.content, 0)
|
|
12264
|
+
};
|
|
12265
|
+
}
|
|
12266
|
+
break;
|
|
12267
|
+
}
|
|
12268
|
+
case "moveFragment": {
|
|
12269
|
+
const range = selectedParagraphRuns(document, intent.from, intent.to);
|
|
12270
|
+
if (range.isErr()) return Result.err(range.error);
|
|
12271
|
+
const sourceParagraph = paragraphAt(document, intent.from);
|
|
12272
|
+
const fromGap = intent.from.zeroWidthBefore ?? (sourceParagraph === void 0 ? 0 : zeroWidthLeavesAt(sourceParagraph.content, intent.from.offset).length);
|
|
12273
|
+
const toGap = intent.to.zeroWidthBefore ?? 0;
|
|
12274
|
+
if (idKey(intent.from.blockId) === idKey(intent.to.blockId) && intent.from.offset === intent.to.offset && fromGap >= toGap) {
|
|
12275
|
+
if (fromGap > toGap && (intent.from.zeroWidthBefore !== void 0 || intent.to.zeroWidthBefore !== void 0)) return Result.err(new DocumentOpRefusal({
|
|
12276
|
+
reason: DOCUMENT_OP_REFUSAL_REASONS.INVALID_OFFSET,
|
|
12277
|
+
message: "The move source marker gaps are reversed.",
|
|
12278
|
+
opType: DOCUMENT_OP_TYPES.DELETE_RANGE
|
|
12279
|
+
}));
|
|
12280
|
+
return Result.ok({
|
|
12281
|
+
ops: [],
|
|
12282
|
+
selection: intent.target
|
|
12283
|
+
});
|
|
12284
|
+
}
|
|
12285
|
+
const locations = storyParagraphs(storyBody(document, intent.from.story));
|
|
12286
|
+
const indexOf = (at) => locations.findIndex(({ paragraph }) => idKey(paragraph.paraId ?? "") === idKey(at.blockId));
|
|
12287
|
+
const first = indexOf(intent.from);
|
|
12288
|
+
const last = indexOf(intent.to);
|
|
12289
|
+
const targetIndex = indexOf(intent.target);
|
|
12290
|
+
const targetGap = intent.target.zeroWidthBefore ?? defaultInsertionGap(paragraphAt(document, intent.target)?.content ?? [], intent.target.offset).zeroWidthBefore;
|
|
12291
|
+
const targetPosition = {
|
|
12292
|
+
offset: intent.target.offset,
|
|
12293
|
+
zeroWidthBefore: targetGap
|
|
12294
|
+
};
|
|
12295
|
+
if (intent.target.story === intent.from.story && targetIndex >= first && targetIndex <= last && (targetIndex !== first || compareGaps(targetPosition, {
|
|
12296
|
+
offset: intent.from.offset,
|
|
12297
|
+
zeroWidthBefore: fromGap
|
|
12298
|
+
}) >= 0) && (targetIndex !== last || compareGaps(targetPosition, {
|
|
12299
|
+
offset: intent.to.offset,
|
|
12300
|
+
zeroWidthBefore: toGap
|
|
12301
|
+
}) <= 0)) return Result.ok({
|
|
12302
|
+
ops: [],
|
|
12303
|
+
selection: intent.target
|
|
12304
|
+
});
|
|
12305
|
+
const anchorDeletions = [];
|
|
12306
|
+
const sourceAnchors = mode.type === "editing" ? range.value.flat() : [];
|
|
12307
|
+
for (const { paragraph } of sourceAnchors) {
|
|
12308
|
+
const blockId = paragraph.paraId ?? "";
|
|
12309
|
+
const from = idKey(blockId) === idKey(intent.from.blockId) ? {
|
|
12310
|
+
offset: intent.from.offset,
|
|
12311
|
+
zeroWidthBefore: fromGap
|
|
12312
|
+
} : {
|
|
12313
|
+
offset: 0,
|
|
12314
|
+
zeroWidthBefore: 0
|
|
12315
|
+
};
|
|
12316
|
+
const to = idKey(blockId) === idKey(intent.to.blockId) ? {
|
|
12317
|
+
offset: intent.to.offset,
|
|
12318
|
+
zeroWidthBefore: toGap
|
|
12319
|
+
} : {
|
|
12320
|
+
offset: paragraphLength(paragraph),
|
|
12321
|
+
zeroWidthBefore: zeroWidthLeavesAt(paragraph.content, paragraphLength(paragraph)).length
|
|
12322
|
+
};
|
|
12323
|
+
for (const span of leafSpans(paragraph.content)) {
|
|
12324
|
+
if (!isMoveAnchor(span.node) || span.ancestors.some(isRemovedRevisionNode) || compareGaps(span.before, from) < 0 || compareGaps(span.after, to) > 0) continue;
|
|
12325
|
+
anchorDeletions.push({
|
|
12326
|
+
type: DOCUMENT_OP_TYPES.DELETE_RANGE,
|
|
12327
|
+
from: {
|
|
12328
|
+
story: intent.from.story,
|
|
12329
|
+
blockId,
|
|
12330
|
+
...span.before
|
|
12331
|
+
},
|
|
12332
|
+
to: {
|
|
12333
|
+
story: intent.from.story,
|
|
12334
|
+
blockId,
|
|
12335
|
+
...span.after
|
|
12336
|
+
}
|
|
12337
|
+
});
|
|
12338
|
+
}
|
|
12339
|
+
}
|
|
12340
|
+
const deleted = compileEditorIntent(document, {
|
|
12341
|
+
intent: {
|
|
12342
|
+
type: "replaceText",
|
|
12343
|
+
from: intent.from,
|
|
12344
|
+
to: intent.to,
|
|
12345
|
+
text: ""
|
|
12346
|
+
},
|
|
12347
|
+
mode
|
|
12348
|
+
});
|
|
12349
|
+
if (deleted.isErr()) return deleted;
|
|
12350
|
+
const logicalDeletions = anchorDeletions.length === 0 ? deleted.value.ops : [...deleted.value.ops.filter((op) => op.type === DOCUMENT_OP_TYPES.DELETE_RANGE).concat(anchorDeletions).sort((left, right) => indexOf(right.from) - indexOf(left.from) || compareGaps({
|
|
12351
|
+
offset: right.from.offset,
|
|
12352
|
+
zeroWidthBefore: right.from.zeroWidthBefore ?? 0
|
|
12353
|
+
}, {
|
|
12354
|
+
offset: left.from.offset,
|
|
12355
|
+
zeroWidthBefore: left.from.zeroWidthBefore ?? 0
|
|
12356
|
+
})), ...deleted.value.ops.filter((op) => op.type !== DOCUMENT_OP_TYPES.DELETE_RANGE)];
|
|
12357
|
+
const anchors = new Set(anchorDeletions);
|
|
12358
|
+
const deletionOps = [];
|
|
12359
|
+
let afterDeletion = document;
|
|
12360
|
+
for (const planned of logicalDeletions) {
|
|
12361
|
+
let op = planned;
|
|
12362
|
+
if (planned.type === DOCUMENT_OP_TYPES.DELETE_RANGE && anchors.has(planned)) {
|
|
12363
|
+
const paragraph = paragraphAt(afterDeletion, planned.from);
|
|
12364
|
+
if (paragraph === void 0) panic("A move anchor lost its selected paragraph.");
|
|
12365
|
+
op = {
|
|
12366
|
+
type: DOCUMENT_OP_TYPES.REPLACE_INLINE,
|
|
12367
|
+
story: planned.from.story,
|
|
12368
|
+
blockId: planned.from.blockId,
|
|
12369
|
+
expected: paragraph.content,
|
|
12370
|
+
content: deleteBetween(paragraph.content, {
|
|
12371
|
+
offset: planned.from.offset,
|
|
12372
|
+
zeroWidthBefore: planned.from.zeroWidthBefore ?? 0
|
|
12373
|
+
}, {
|
|
12374
|
+
offset: planned.to.offset,
|
|
12375
|
+
zeroWidthBefore: planned.to.zeroWidthBefore ?? 0
|
|
12376
|
+
}).content
|
|
12377
|
+
};
|
|
12378
|
+
}
|
|
12379
|
+
const applied = applyDocumentOps(afterDeletion, [op]);
|
|
12380
|
+
if (applied.isErr()) return Result.err(applied.error);
|
|
12381
|
+
afterDeletion = applied.value.document;
|
|
12382
|
+
deletionOps.push(op);
|
|
12383
|
+
}
|
|
12384
|
+
let target = intent.target;
|
|
12385
|
+
for (const op of logicalDeletions) {
|
|
12386
|
+
if (op.type === DOCUMENT_OP_TYPES.DELETE_RANGE && op.revision === void 0 && idKey(target.blockId) === idKey(op.from.blockId) && target.story === op.from.story && target.offset >= op.to.offset) {
|
|
12387
|
+
const source = paragraphAt(document, op.from);
|
|
12388
|
+
const deletionFromGap = op.from.zeroWidthBefore ?? zeroWidthLeavesAt(source?.content ?? [], op.from.offset).length;
|
|
12389
|
+
const deletionToGap = op.to.zeroWidthBefore ?? 0;
|
|
12390
|
+
const deletionTargetGap = target.zeroWidthBefore ?? defaultInsertionGap(source?.content ?? [], target.offset).zeroWidthBefore;
|
|
12391
|
+
const rebased = Object.assign({}, target, { offset: target.offset - (op.to.offset - op.from.offset) });
|
|
12392
|
+
if (target.offset === op.to.offset) rebased.zeroWidthBefore = deletionFromGap + deletionTargetGap - deletionToGap;
|
|
12393
|
+
target = rebased;
|
|
12394
|
+
}
|
|
12395
|
+
if (op.type === DOCUMENT_OP_TYPES.JOIN_BLOCKS && op.revision === void 0 && idKey(target.blockId) === idKey(op.blockId) && target.story === op.story) target = Object.assign({}, target, { blockId: op.nextBlockId });
|
|
12396
|
+
else if (op.type === DOCUMENT_OP_TYPES.JOIN_BLOCKS && op.revision === void 0 && idKey(target.blockId) === idKey(op.nextBlockId) && target.story === op.story) {
|
|
12397
|
+
const prefix = paragraphAt(document, {
|
|
12398
|
+
story: op.story,
|
|
12399
|
+
blockId: op.blockId,
|
|
12400
|
+
offset: 0
|
|
12401
|
+
});
|
|
12402
|
+
const remaining = idKey(op.blockId) === idKey(intent.from.blockId) ? intent.from.offset : 0;
|
|
12403
|
+
if (prefix !== void 0) {
|
|
12404
|
+
const rebased = Object.assign({}, target, { offset: target.offset + remaining });
|
|
12405
|
+
if (target.offset === 0 && idKey(op.blockId) === idKey(intent.from.blockId)) rebased.zeroWidthBefore = fromGap + (target.zeroWidthBefore ?? 0);
|
|
12406
|
+
target = rebased;
|
|
12407
|
+
}
|
|
12408
|
+
}
|
|
12409
|
+
}
|
|
12410
|
+
const paragraphs = mode.type === "suggesting" ? intent.paragraphs.map((paragraph) => Object.assign({}, paragraph, { content: asParagraphContent(withoutMoveAnchors(paragraph.content)) })) : intent.paragraphs;
|
|
12411
|
+
const inserted = compileEditorIntent(afterDeletion, {
|
|
12412
|
+
intent: {
|
|
12413
|
+
type: "replaceFragment",
|
|
12414
|
+
from: target,
|
|
12415
|
+
to: target,
|
|
12416
|
+
paragraphs,
|
|
12417
|
+
openStart: intent.openStart,
|
|
12418
|
+
openEnd: intent.openEnd
|
|
12419
|
+
},
|
|
12420
|
+
mode: modeAfterOps(afterDeletion, mode),
|
|
12421
|
+
...firstBlockId === void 0 ? {} : { firstBlockId }
|
|
12422
|
+
});
|
|
12423
|
+
if (inserted.isErr()) return inserted;
|
|
12424
|
+
ops = [...deletionOps, ...inserted.value.ops];
|
|
12425
|
+
selection = inserted.value.selection;
|
|
12426
|
+
break;
|
|
12427
|
+
}
|
|
10076
12428
|
case "table": {
|
|
10077
12429
|
const operation = intent.operation;
|
|
10078
12430
|
const located = locateTableRow(document, operation);
|
|
10079
12431
|
if (located.isErr()) return Result.err(located.error);
|
|
10080
12432
|
const demand = tableEditParagraphDemand(located.value, operation);
|
|
10081
12433
|
if (demand.isErr()) return Result.err(demand.error);
|
|
10082
|
-
const occupied = new Set(packageParagraphIds(document.package).map(idKey));
|
|
12434
|
+
const occupied = new Set([...packageParagraphIds(document.package), ...mode.reservedBlockIds ?? []].map(idKey));
|
|
10083
12435
|
const newBlockIds = [];
|
|
10084
12436
|
for (let nextId = 1; newBlockIds.length < demand.value; nextId += 1) {
|
|
10085
12437
|
if (nextId >= 2147483648) return Result.err(new DocumentOpRefusal({
|
|
@@ -10252,6 +12604,7 @@ const compileEditorIntent = (document, { intent, mode }) => {
|
|
|
10252
12604
|
const formatting = intentRunFormatting(document, intent);
|
|
10253
12605
|
if (mode.type === "suggesting") {
|
|
10254
12606
|
const planned = planTrackedReplace(document, {
|
|
12607
|
+
sourceContainerPolicy: "join",
|
|
10255
12608
|
from: intent.from,
|
|
10256
12609
|
to: intent.to,
|
|
10257
12610
|
revision: mode.revision,
|
|
@@ -10330,6 +12683,7 @@ const compileEditorIntent = (document, { intent, mode }) => {
|
|
|
10330
12683
|
}];
|
|
10331
12684
|
if (mode.type === "suggesting") {
|
|
10332
12685
|
const planned = planTrackedReplace(document, {
|
|
12686
|
+
sourceContainerPolicy: "join",
|
|
10333
12687
|
from,
|
|
10334
12688
|
to,
|
|
10335
12689
|
revision: mode.revision,
|
|
@@ -10365,7 +12719,7 @@ const compileEditorIntent = (document, { intent, mode }) => {
|
|
|
10365
12719
|
}
|
|
10366
12720
|
ops = [];
|
|
10367
12721
|
let survivorId = from.blockId;
|
|
10368
|
-
if (from.blockId !== to.blockId || from.offset !== to.offset) {
|
|
12722
|
+
if (from.blockId !== to.blockId || from.offset !== to.offset || (from.zeroWidthBefore ?? 0) !== (to.zeroWidthBefore ?? 0)) {
|
|
10369
12723
|
const partitioned = selectedParagraphRuns(document, from, to);
|
|
10370
12724
|
if (partitioned.isErr()) return Result.err(partitioned.error);
|
|
10371
12725
|
survivorId = partitioned.value.at(0)?.at(-1)?.paragraph.paraId ?? from.blockId;
|
|
@@ -10643,14 +12997,17 @@ const compileEditorIntent = (document, { intent, mode }) => {
|
|
|
10643
12997
|
current = aligned.value.document;
|
|
10644
12998
|
}
|
|
10645
12999
|
}
|
|
10646
|
-
|
|
10647
|
-
|
|
13000
|
+
const insertedWidth = intent.type === "replaceText" ? intent.text.length : 0;
|
|
13001
|
+
if (insertedWidth > 0) {
|
|
13002
|
+
for (const offset of /* @__PURE__ */ new Set([selection.offset, selection.offset - insertedWidth])) if (offset >= 0) editedSeams.push({
|
|
10648
13003
|
...selection,
|
|
10649
13004
|
offset
|
|
10650
|
-
};
|
|
13005
|
+
});
|
|
13006
|
+
}
|
|
13007
|
+
for (const at of editedSeams) {
|
|
10651
13008
|
const paragraph = paragraphAt(current, at);
|
|
10652
13009
|
if (paragraph === void 0) panic("An edited paragraph must exist at its seam.");
|
|
10653
|
-
const depth = textSeamDepth(paragraph, offset);
|
|
13010
|
+
const depth = textSeamDepth(paragraph, at.offset);
|
|
10654
13011
|
if (depth === 0) continue;
|
|
10655
13012
|
const join = {
|
|
10656
13013
|
type: DOCUMENT_OP_TYPES.JOIN_INLINE,
|
|
@@ -10955,6 +13312,12 @@ const operationFields = {
|
|
|
10955
13312
|
createNumberingInstance: void 0,
|
|
10956
13313
|
deleteNumberingInstance: void 0,
|
|
10957
13314
|
setSectionEndpoint: void 0,
|
|
13315
|
+
createComment: void 0,
|
|
13316
|
+
updateCommentContent: void 0,
|
|
13317
|
+
setCommentResolution: void 0,
|
|
13318
|
+
deleteComment: void 0,
|
|
13319
|
+
restoreCommentState: void 0,
|
|
13320
|
+
setPackageResources: void 0,
|
|
10958
13321
|
addNote: void 0,
|
|
10959
13322
|
createHeaderFooter: void 0,
|
|
10960
13323
|
removeHeaderFooter: void 0,
|
|
@@ -11011,7 +13374,7 @@ const isJsonData = ({ value, ancestors, depth, budget }) => {
|
|
|
11011
13374
|
return valid;
|
|
11012
13375
|
};
|
|
11013
13376
|
const isDocumentBatch = (value) => object({
|
|
11014
|
-
schema: literal(
|
|
13377
|
+
schema: literal(10),
|
|
11015
13378
|
opId: nonemptyString,
|
|
11016
13379
|
actor: nonemptyString,
|
|
11017
13380
|
baseRev: natural,
|
|
@@ -11068,7 +13431,7 @@ const validateDocumentBatch = (value) => {
|
|
|
11068
13431
|
return decoded;
|
|
11069
13432
|
};
|
|
11070
13433
|
const decodeDocumentBatch = (value) => {
|
|
11071
|
-
if (value["schema"] !==
|
|
13434
|
+
if (value["schema"] !== 10) return Result.err(new BatchRejection({
|
|
11072
13435
|
reason: BATCH_REJECTION_REASONS.UNSUPPORTED_SCHEMA,
|
|
11073
13436
|
message: "Unsupported document operation schema"
|
|
11074
13437
|
}));
|
|
@@ -11170,9 +13533,15 @@ const operationStory = (op) => {
|
|
|
11170
13533
|
case DOCUMENT_OP_TYPES.SET_ROW_PROPS:
|
|
11171
13534
|
case DOCUMENT_OP_TYPES.SET_TABLE_PROPS:
|
|
11172
13535
|
case DOCUMENT_OP_TYPES.SET_TABLE: return op.story;
|
|
13536
|
+
case DOCUMENT_OP_TYPES.CREATE_COMMENT:
|
|
13537
|
+
case DOCUMENT_OP_TYPES.UPDATE_COMMENT_CONTENT:
|
|
13538
|
+
case DOCUMENT_OP_TYPES.SET_COMMENT_RESOLUTION:
|
|
13539
|
+
case DOCUMENT_OP_TYPES.DELETE_COMMENT:
|
|
13540
|
+
case DOCUMENT_OP_TYPES.RESTORE_COMMENT_STATE:
|
|
11173
13541
|
case DOCUMENT_OP_TYPES.CREATE_NUMBERING_INSTANCE:
|
|
11174
13542
|
case DOCUMENT_OP_TYPES.DELETE_NUMBERING_INSTANCE:
|
|
11175
13543
|
case DOCUMENT_OP_TYPES.SET_SECTION_ENDPOINT:
|
|
13544
|
+
case DOCUMENT_OP_TYPES.SET_PACKAGE_RESOURCES:
|
|
11176
13545
|
case DOCUMENT_OP_TYPES.ADD_NOTE:
|
|
11177
13546
|
case DOCUMENT_OP_TYPES.CREATE_HEADER_FOOTER:
|
|
11178
13547
|
case DOCUMENT_OP_TYPES.REMOVE_HEADER_FOOTER:
|
|
@@ -11209,7 +13578,7 @@ const refusal = (op, over, message) => Result.err(new BatchRejection({
|
|
|
11209
13578
|
const transformRestoredOp = ({ op, over, effect, order }) => {
|
|
11210
13579
|
const story = operationStory(op);
|
|
11211
13580
|
const overStory = operationStory(over);
|
|
11212
|
-
if (story === void 0 || overStory === void 0) return refusal(op, over, "
|
|
13581
|
+
if (story === void 0 || overStory === void 0) return refusal(op, over, "Package resources, numbering and section metadata require an exclusive edit.");
|
|
11213
13582
|
if (!sameStory$1(story, overStory)) return Result.ok([op]);
|
|
11214
13583
|
if (independent(op, over)) return Result.ok([op]);
|
|
11215
13584
|
if (effect?.type === "touchedBlocks" && op.type !== DOCUMENT_OP_TYPES.RESOLVE_REVISION) {
|
|
@@ -11906,4 +14275,4 @@ const createClient = (document) => {
|
|
|
11906
14275
|
};
|
|
11907
14276
|
};
|
|
11908
14277
|
//#endregion
|
|
11909
|
-
export { BATCH_REJECTION_REASONS, BATCH_WIRE_OP_TYPES, BatchRejection, DOCUMENT_OP_REFUSAL_REASONS, DOCUMENT_OP_SCHEMA_VERSION, DOCUMENT_OP_TYPES, DocumentOpRefusal, DocumentOpsContractError, EMPTY_PROPERTY_SETS, INHERIT_RUN_PROPS, MAX_BATCH_WIRE_BYTES, OBJECT_REPLACEMENT_CHARACTER, OP_STORIES, PARAGRAPH_MARK_FORMATTING_KEYS, PROPERTY_REVIEW_POLICIES, REVISION_DECISIONS, SPLIT_HALVES, applyDocumentOp, applyDocumentOpEnvelope, applyDocumentOps, captureDocumentOp, combineEdits, compileEditorIntent, createClient, createEditorIntentIdAllocator, createSequencer, documentStories, editorParagraphGroups, findStoryBody, formattingEquals, isTextOnlyRun, mergeRunContent, normalizeForOps, noteUsesCustomMark, packageParagraphIds, paragraphLength, paragraphLogicalText, paragraphVisibleText, parseDocumentBatch, physicalOffsetAtVisibleOffset, planTrackedDeletion, planTrackedReplace, revisionIdDemand, runsMergeable, sameStory, sectionPropertiesAt, storyBody, toOpEnvelope, transformBatch, validateDocumentBatch, validateOpsDocument, validateSequencedBatch };
|
|
14278
|
+
export { BATCH_REJECTION_REASONS, BATCH_WIRE_OP_TYPES, BatchRejection, DOCUMENT_OP_REFUSAL_REASONS, DOCUMENT_OP_SCHEMA_VERSION, DOCUMENT_OP_TYPES, DocumentOpRefusal, DocumentOpsContractError, EMPTY_PROPERTY_SETS, INHERIT_RUN_PROPS, MAX_BATCH_WIRE_BYTES, OBJECT_REPLACEMENT_CHARACTER, OP_STORIES, PARAGRAPH_MARK_FORMATTING_KEYS, PROPERTY_REVIEW_POLICIES, REVISION_DECISIONS, SPLIT_HALVES, allocateCommentAnchorIds, applyDocumentOp, applyDocumentOpEnvelope, applyDocumentOps, captureDocumentOp, combineEdits, commentDocumentIssue, compareGaps, compileEditorIntent, createClient, createEditorIntentIdAllocator, createSequencer, defaultInsertionGap, documentStories, editorParagraphGroups, findStoryBody, formattingEquals, freshCommentId, idKey, inlineLeafSpans, isTextOnlyRun, mapTocBookmarkPosition, mergeRunContent, normalizeForOps, noteUsesCustomMark, packageParagraphIds, packageResourcesOpOf, paragraphLength, paragraphLogicalText, paragraphVisibleText, parseDocumentBatch, physicalOffsetAtVisibleOffset, planTrackedDeletion, planTrackedReplace, revisionIdDemand, runsMergeable, sameStory, sectionPropertiesAt, storyBody, toOpEnvelope, transformBatch, validateDocumentBatch, validateOpsDocument, validateSequencedBatch, zeroWidthLeavesAt };
|