@stll/folio-core 0.75.0 → 0.77.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/ai-edits/headless.d.ts +8 -5
- package/dist/ai-edits/headless.js +49 -26
- package/dist/ai-edits/pending-suggestions.d.ts +9 -0
- package/dist/ai-edits/pending-suggestions.js +13 -3
- package/dist/ai-edits/read.js +33 -4
- package/dist/ai-edits/table-geometry.js +12 -4
- package/dist/ai-edits/types.d.ts +9 -4
- package/dist/canonical/batchCommentReferences.js +2 -1
- package/dist/canonical/canonicalBatchReview.js +5 -8
- package/dist/canonical/canonicalComments.js +8 -2
- package/dist/canonical/canonicalCompilerRefusal.d.ts +2 -4
- package/dist/canonical/canonicalCompilerRefusal.js +20 -14
- package/dist/canonical/canonicalInlineProjection.d.ts +2 -2
- package/dist/canonical/canonicalInlineProjection.js +2 -2
- package/dist/canonical/canonicalProjectionBlocks.d.ts +10 -1
- package/dist/canonical/canonicalPublicOperations.js +214 -107
- package/dist/canonical/canonicalSeedContent.d.ts +18 -3
- package/dist/canonical/canonicalSeedContent.js +28 -15
- package/dist/canonical/canonicalSession.d.ts +22 -12
- package/dist/canonical/canonicalSession.js +181 -187
- package/dist/controller/canonicalClipboard.d.ts +3 -2
- package/dist/controller/canonicalClipboard.js +377 -58
- package/dist/controller/canonicalComposition.js +67 -14
- package/dist/controller/canonicalContentControlValue.js +7 -7
- package/dist/controller/canonicalReview.d.ts +21 -0
- package/dist/controller/canonicalReview.js +44 -9
- package/dist/controller/canonicalStoryEditor.js +2 -0
- package/dist/controller/canonicalStructure.d.ts +1 -2
- package/dist/controller/canonicalStructure.js +51 -24
- package/dist/controller/fontReadiness.js +50 -23
- package/dist/controller/hiddenEditorManager.js +7 -20
- package/dist/docx/bulletMarkers.d.ts +8 -0
- package/dist/docx/bulletMarkers.js +9 -1
- package/dist/docx/canonicalSessionInput.js +25 -11
- package/dist/docx/ensureParaIds.d.ts +31 -0
- package/dist/docx/ensureParaIds.js +57 -32
- package/dist/docx/ensureUniqueControlIds.d.ts +13 -0
- package/dist/docx/ensureUniqueControlIds.js +47 -29
- package/dist/docx/ensureUniqueRevisionIds.d.ts +13 -0
- package/dist/docx/ensureUniqueRevisionIds.js +52 -29
- package/dist/docx/importedIdCollisions.js +29 -4
- package/dist/docx/server/templatePlan/apply.js +108 -9
- package/dist/docx/server/templatePlan/blocks.d.ts +77 -5
- package/dist/docx/server/templatePlan/blocks.js +281 -14
- package/dist/docx/server/templatePlan/builders.d.ts +22 -1
- package/dist/docx/server/templatePlan/builders.js +37 -3
- package/dist/docx/server/templatePlan/effects.d.ts +27 -0
- package/dist/docx/server/templatePlan/effects.js +188 -0
- package/dist/docx/server/templatePlan/emit.d.ts +4 -1
- package/dist/docx/server/templatePlan/emit.js +152 -8
- package/dist/docx/server/templatePlan/inspect.js +21 -1
- package/dist/docx/server/templatePlan/labels.js +16 -1
- package/dist/docx/server/templatePlan/plan.d.ts +35 -3
- package/dist/docx/server/templatePlan/plan.js +197 -20
- package/dist/docx/server/templatePlan/planShape.js +58 -22
- package/dist/docx/server/templatePlan/revisions.d.ts +101 -0
- package/dist/docx/server/templatePlan/revisions.js +681 -0
- package/dist/docx/server/templatePlan/story.d.ts +61 -2
- package/dist/docx/server/templatePlan/story.js +175 -13
- package/dist/docx/server/templatePlan/types.d.ts +313 -1
- package/dist/docx/server/templatePlan/verify.js +175 -12
- package/dist/docx/server/templatePlan/xml.d.ts +3 -0
- package/dist/docx/server/templatePlan/xml.js +4 -1
- package/dist/docx/settingsHeaderFooterUpdate.d.ts +1 -1
- package/dist/docx/tableGridXml.d.ts +3 -7
- package/dist/docx/tableGridXml.js +5 -13
- package/dist/layout-bridge/convert/footnoteLayout.js +34 -8
- package/dist/prosemirror/canonicalMarkupDocument.d.ts +20 -0
- package/dist/prosemirror/canonicalMarkupDocument.js +73 -0
- package/dist/prosemirror/canonicalTableCommands.d.ts +1 -1
- package/dist/prosemirror/canonicalTableCommands.js +1 -3
- package/dist/prosemirror/commands/comments.d.ts +6 -234
- package/dist/prosemirror/commands/comments.js +5 -2464
- package/dist/prosemirror/commands/index.d.ts +4 -2
- package/dist/prosemirror/commands/index.js +4 -2
- package/dist/prosemirror/conversion/markupViewSource.d.ts +17 -0
- package/dist/prosemirror/conversion/markupViewSource.js +11 -0
- package/dist/prosemirror/conversion/toProseDoc.d.ts +3 -0
- package/dist/prosemirror/conversion/toProseDoc.js +31 -14
- package/dist/prosemirror/extensions/marks/HyperlinkExtension.js +6 -8
- package/dist/prosemirror/hyperlinkRemoval.d.ts +11 -8
- package/dist/prosemirror/hyperlinkRemoval.js +24 -15
- package/dist/prosemirror/independentMarkupProjection.d.ts +27 -0
- package/dist/prosemirror/independentMarkupProjection.js +147 -0
- package/dist/prosemirror/markupViewNotes.d.ts +1 -1
- package/dist/prosemirror/markupViewNotes.js +24 -27
- package/dist/prosemirror/markupViewPositionMap.d.ts +16 -0
- package/dist/prosemirror/markupViewPositionMap.js +316 -0
- package/dist/prosemirror/markupViewProjection.d.ts +3 -46
- package/dist/prosemirror/markupViewProjection.js +90 -17
- package/dist/prosemirror/paraIds.d.ts +1 -14
- package/dist/prosemirror/paraIds.js +1 -14
- package/dist/prosemirror/paragraphMarkJoin.d.ts +0 -16
- package/dist/prosemirror/paragraphMarkJoin.js +3 -46
- package/dist/prosemirror/projectionReview.d.ts +49 -0
- package/dist/prosemirror/projectionReview.js +292 -0
- package/dist/prosemirror/rebaseParagraphRunFormatting.d.ts +1 -13
- package/dist/prosemirror/rebaseParagraphRunFormatting.js +1 -16
- package/dist/prosemirror/replacedAnnotations.d.ts +6 -83
- package/dist/prosemirror/replacedAnnotations.js +11 -203
- package/dist/prosemirror/revisionCarriers.d.ts +9 -1
- package/dist/prosemirror/revisionCarriers.js +15 -1
- package/dist/prosemirror/revisionRangeQueries.d.ts +32 -0
- package/dist/prosemirror/revisionRangeQueries.js +76 -0
- package/dist/prosemirror/runFormattingMarkNames.d.ts +1 -1
- package/dist/prosemirror/runFormattingMarkNames.js +1 -1
- package/dist/prosemirror/styles/styleResolver.d.ts +1 -0
- package/dist/prosemirror/styles/styleResolver.js +4 -1
- package/dist/prosemirror/suggestionQueries.d.ts +114 -0
- package/dist/prosemirror/suggestionQueries.js +320 -0
- package/dist/prosemirror/tableGridMutation.d.ts +1 -16
- package/dist/prosemirror/tableGridMutation.js +1 -78
- package/dist/prosemirror/tableRowRevisionOwnership.d.ts +18 -0
- package/dist/prosemirror/tableRowRevisionOwnership.js +7 -0
- package/dist/prosemirror/utils/extractTrackedChanges.js +14 -4
- package/dist/server.d.ts +2 -2
- package/dist/types/canonicalCapabilities.d.ts +4 -35
- package/dist/types/canonicalCapabilities.js +4 -33
- package/package.json +2 -2
- package/dist/internal/headlessRevisionResolutionGuard.d.ts +0 -3
- package/dist/internal/headlessRevisionResolutionGuard.js +0 -18
- package/dist/internal/revisionResolutionEdits.d.ts +0 -11
- package/dist/internal/revisionResolutionEdits.js +0 -18
- package/dist/internal/revisionResolutionInline.d.ts +0 -24
- package/dist/internal/revisionResolutionInline.js +0 -302
- package/dist/internal/revisionResolutionStep.d.ts +0 -32
- package/dist/internal/revisionResolutionStep.js +0 -282
- package/dist/internal/revisionResolutionTracking.d.ts +0 -9
- package/dist/internal/revisionResolutionTracking.js +0 -124
- package/dist/internal/wholeStoryRevisionResolution.d.ts +0 -56
- package/dist/internal/wholeStoryRevisionResolution.js +0 -783
- package/dist/prosemirror/commands/resolveAllTableChanges.d.ts +0 -38
- package/dist/prosemirror/commands/resolveAllTableChanges.js +0 -642
- package/dist/prosemirror/commands/resolveNodePropertyChangeAttrs.d.ts +0 -13
- package/dist/prosemirror/commands/resolveNodePropertyChangeAttrs.js +0 -45
- package/dist/prosemirror/commands/tableCellMergeResolution.d.ts +0 -18
- package/dist/prosemirror/commands/tableCellMergeResolution.js +0 -207
- package/dist/prosemirror/commands/tableMergeFoldDecisions.d.ts +0 -60
- package/dist/prosemirror/commands/tableMergeFoldDecisions.js +0 -78
- package/dist/prosemirror/rejoinRunCarriers.d.ts +0 -42
- package/dist/prosemirror/rejoinRunCarriers.js +0 -65
- package/dist/prosemirror/runPropertyChangeResolution.d.ts +0 -15
- package/dist/prosemirror/runPropertyChangeResolution.js +0 -34
|
@@ -0,0 +1,681 @@
|
|
|
1
|
+
import { spliceXml } from "../../selectiveXmlPatch.js";
|
|
2
|
+
import { loadPackage } from "./package.js";
|
|
3
|
+
import { TemplatePlanError } from "./types.js";
|
|
4
|
+
import { elementChildren, escapeXmlAttribute, innerRangeOf, rangeOf, readRawPart, wAttr, wChild, wName } from "./xml.js";
|
|
5
|
+
import { Result } from "better-result";
|
|
6
|
+
import { ZipArchive } from "@stll/docx-core/zip";
|
|
7
|
+
// src/docx/server/templatePlan/revisions.ts
|
|
8
|
+
/**
|
|
9
|
+
* Tracked changes resolved to one view.
|
|
10
|
+
*
|
|
11
|
+
* A document with tracked changes is two texts at once: what it reads with
|
|
12
|
+
* every change accepted, and what it reads with every change rejected. A
|
|
13
|
+
* plan is made for one of them. This module rewrites the story parts of a
|
|
14
|
+
* package into the chosen view: no insertion, deletion, move or recorded
|
|
15
|
+
* formatting change is left, and nothing else of the parts changes.
|
|
16
|
+
*
|
|
17
|
+
* - Accepted: inserted content stays and loses its wrapper, deleted content
|
|
18
|
+
* goes. Rejected: the reverse, and deleted text is ordinary text again.
|
|
19
|
+
* - A paragraph mark that the view removes (deleted and accepted, inserted
|
|
20
|
+
* and rejected) joins its paragraph to the next one of the same container,
|
|
21
|
+
* whose mark, and with it whose properties, the joined paragraph keeps.
|
|
22
|
+
* - A table row that the view removes goes whole; so does a cell.
|
|
23
|
+
* - A recorded formatting change is dropped when accepted. Rejected, the
|
|
24
|
+
* properties it recorded come back.
|
|
25
|
+
*/
|
|
26
|
+
const INSERTED = /* @__PURE__ */ new Set(["ins", "moveTo"]);
|
|
27
|
+
const DELETED = /* @__PURE__ */ new Set(["del", "moveFrom"]);
|
|
28
|
+
const MOVE_MARKS = /* @__PURE__ */ new Set([
|
|
29
|
+
"moveFromRangeStart",
|
|
30
|
+
"moveFromRangeEnd",
|
|
31
|
+
"moveToRangeStart",
|
|
32
|
+
"moveToRangeEnd"
|
|
33
|
+
]);
|
|
34
|
+
/** A recorded change of properties, by the properties element it sits in. */
|
|
35
|
+
const PROPERTY_CHANGES = /* @__PURE__ */ new Map([
|
|
36
|
+
["rPrChange", "rPr"],
|
|
37
|
+
["pPrChange", "pPr"],
|
|
38
|
+
["sectPrChange", "sectPr"],
|
|
39
|
+
["tblPrChange", "tblPr"],
|
|
40
|
+
["tblGridChange", "tblGrid"],
|
|
41
|
+
["tblPrExChange", "tblPrEx"],
|
|
42
|
+
["trPrChange", "trPr"],
|
|
43
|
+
["tcPrChange", "tcPr"]
|
|
44
|
+
]);
|
|
45
|
+
const CELL_MARKS = /* @__PURE__ */ new Set([
|
|
46
|
+
"cellIns",
|
|
47
|
+
"cellDel",
|
|
48
|
+
"cellMerge"
|
|
49
|
+
]);
|
|
50
|
+
const OWN_PROPERTIES = /* @__PURE__ */ new Map([["pPr", /* @__PURE__ */ new Set(["rPr", "sectPr"])], ["sectPr", /* @__PURE__ */ new Set(["headerReference", "footerReference"])]]);
|
|
51
|
+
/** Marks of a range that outlive content a view removes around them: a
|
|
52
|
+
* bookmark or a comment that begins inside a deletion ends outside it. */
|
|
53
|
+
const RANGE_MARKS = /* @__PURE__ */ new Set([
|
|
54
|
+
"bookmarkStart",
|
|
55
|
+
"bookmarkEnd",
|
|
56
|
+
"commentRangeStart",
|
|
57
|
+
"commentRangeEnd",
|
|
58
|
+
"permStart",
|
|
59
|
+
"permEnd"
|
|
60
|
+
]);
|
|
61
|
+
/** Every element name this module acts on, to skip a subtree without one. */
|
|
62
|
+
const REVISION_NAMES = [
|
|
63
|
+
...INSERTED,
|
|
64
|
+
...DELETED,
|
|
65
|
+
...MOVE_MARKS,
|
|
66
|
+
...PROPERTY_CHANGES.keys(),
|
|
67
|
+
...CELL_MARKS,
|
|
68
|
+
"numberingChange",
|
|
69
|
+
"delText",
|
|
70
|
+
"delInstrText"
|
|
71
|
+
];
|
|
72
|
+
const newTally = () => ({
|
|
73
|
+
insertions: 0,
|
|
74
|
+
deletions: 0,
|
|
75
|
+
moves: 0,
|
|
76
|
+
paragraphMarks: 0,
|
|
77
|
+
tableRows: 0,
|
|
78
|
+
formattingChanges: 0,
|
|
79
|
+
authors: /* @__PURE__ */ new Set(),
|
|
80
|
+
first: null,
|
|
81
|
+
last: null
|
|
82
|
+
});
|
|
83
|
+
/**
|
|
84
|
+
* What a part tracks, whatever view is taken of it: every insertion,
|
|
85
|
+
* deletion, move and recorded formatting change, with who made it and
|
|
86
|
+
* when. A change inside content the view removes is counted all the same.
|
|
87
|
+
*/
|
|
88
|
+
const tallyPart = (root, tally) => {
|
|
89
|
+
const stack = [root];
|
|
90
|
+
for (let element = stack.pop(); element !== void 0; element = stack.pop()) {
|
|
91
|
+
const name = wName(element);
|
|
92
|
+
let counted = true;
|
|
93
|
+
if (name === "ins") tally.insertions += 1;
|
|
94
|
+
else if (name === "del") tally.deletions += 1;
|
|
95
|
+
else if (name === "moveFrom" || name === "moveTo") tally.moves += 1;
|
|
96
|
+
else if (name !== null && (PROPERTY_CHANGES.has(name) || CELL_MARKS.has(name) || name === "numberingChange")) tally.formattingChanges += 1;
|
|
97
|
+
else counted = false;
|
|
98
|
+
if (counted) {
|
|
99
|
+
const author = wAttr(element, "author");
|
|
100
|
+
const date = wAttr(element, "date");
|
|
101
|
+
if (author !== null && author !== "") tally.authors.add(author);
|
|
102
|
+
if (date !== null && date !== "") {
|
|
103
|
+
tally.first = tally.first === null || date < tally.first ? date : tally.first;
|
|
104
|
+
tally.last = tally.last === null || date > tally.last ? date : tally.last;
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
stack.push(...elementChildren(element));
|
|
108
|
+
}
|
|
109
|
+
};
|
|
110
|
+
const mayHoldRevision = (text) => REVISION_NAMES.some((name) => text.includes(`:${name}`));
|
|
111
|
+
/** The element's source text; the walk is unsound without its range. */
|
|
112
|
+
const sourceOf = (walk, element) => {
|
|
113
|
+
const range = rangeOf(element);
|
|
114
|
+
if (range === null) {
|
|
115
|
+
walk.sound = false;
|
|
116
|
+
return "";
|
|
117
|
+
}
|
|
118
|
+
return walk.xml.slice(range.start, range.end);
|
|
119
|
+
};
|
|
120
|
+
/** The range marks inside content a view removes, as they stand. */
|
|
121
|
+
const rangeMarksIn = (walk, element) => {
|
|
122
|
+
let marks = "";
|
|
123
|
+
const stack = [...elementChildren(element)].toReversed();
|
|
124
|
+
for (let next = stack.pop(); next !== void 0; next = stack.pop()) if (RANGE_MARKS.has(wName(next) ?? "")) marks += sourceOf(walk, next);
|
|
125
|
+
else stack.push(...elementChildren(next).toReversed());
|
|
126
|
+
return marks;
|
|
127
|
+
};
|
|
128
|
+
/** Whether the chosen view keeps content marked `name`. */
|
|
129
|
+
const keeps = (walk, name) => INSERTED.has(name) ? walk.view === "accept" : walk.view === "reject";
|
|
130
|
+
/** Deleted text read as text again: the names of its elements. */
|
|
131
|
+
const asLiveText = (xml) => xml.replaceAll(/<(\/?)([A-Za-z0-9]+:)delText\b/gu, "<$1$2t").replaceAll(/<(\/?)([A-Za-z0-9]+:)delInstrText\b/gu, "<$1$2instrText");
|
|
132
|
+
/** Whether the view removes the paragraph's mark. */
|
|
133
|
+
const losesMark = (walk, paragraph) => {
|
|
134
|
+
const mark = wChild(wChild(paragraph, "pPr"), "rPr");
|
|
135
|
+
return (walk.view === "accept" ? ["del", "moveFrom"] : ["ins", "moveTo"]).some((name) => wChild(mark, name) !== null);
|
|
136
|
+
};
|
|
137
|
+
/** Whether the view removes the row: the marker of `w:trPr`. */
|
|
138
|
+
const losesRow = (walk, row) => {
|
|
139
|
+
const properties = wChild(row, "trPr");
|
|
140
|
+
return wChild(properties, walk.view === "accept" ? "del" : "ins") !== null;
|
|
141
|
+
};
|
|
142
|
+
const losesCell = (walk, cell) => wChild(wChild(cell, "tcPr"), walk.view === "accept" ? "cellDel" : "cellIns") !== null;
|
|
143
|
+
/**
|
|
144
|
+
* The children of `container` in the chosen view, with the text between
|
|
145
|
+
* them as it stands. A paragraph whose mark goes hands its content to the
|
|
146
|
+
* next paragraph of the container.
|
|
147
|
+
*/
|
|
148
|
+
const resolveChildren = (walk, container) => {
|
|
149
|
+
const inner = innerRangeOf(walk.xml, container);
|
|
150
|
+
if (inner === null) return "";
|
|
151
|
+
const pieces = [];
|
|
152
|
+
let cursor = inner.start;
|
|
153
|
+
const visit = (element) => {
|
|
154
|
+
const range = rangeOf(element);
|
|
155
|
+
if (range === null) {
|
|
156
|
+
walk.sound = false;
|
|
157
|
+
return;
|
|
158
|
+
}
|
|
159
|
+
pieces.push(walk.xml.slice(cursor, range.start));
|
|
160
|
+
cursor = range.end;
|
|
161
|
+
const name = wName(element);
|
|
162
|
+
if (name !== null && (INSERTED.has(name) || DELETED.has(name))) {
|
|
163
|
+
if (keeps(walk, name)) {
|
|
164
|
+
const kept = resolveChildren(walk, element);
|
|
165
|
+
pieces.push(DELETED.has(name) ? asLiveText(kept) : kept);
|
|
166
|
+
} else pieces.push(rangeMarksIn(walk, element));
|
|
167
|
+
return;
|
|
168
|
+
}
|
|
169
|
+
if (name !== null && MOVE_MARKS.has(name)) return;
|
|
170
|
+
if (name === "numberingChange" || name !== null && CELL_MARKS.has(name)) return;
|
|
171
|
+
if (name !== null && PROPERTY_CHANGES.has(name)) return;
|
|
172
|
+
if (name === "tr" && losesRow(walk, element)) {
|
|
173
|
+
walk.tally.tableRows += 1;
|
|
174
|
+
pieces.push(rangeMarksIn(walk, element));
|
|
175
|
+
return;
|
|
176
|
+
}
|
|
177
|
+
if (name === "tc" && losesCell(walk, element)) {
|
|
178
|
+
pieces.push(rangeMarksIn(walk, element));
|
|
179
|
+
return;
|
|
180
|
+
}
|
|
181
|
+
if (name === "p") {
|
|
182
|
+
pieces.push({
|
|
183
|
+
xml: resolveElement(walk, element),
|
|
184
|
+
paragraph: element,
|
|
185
|
+
carried: losesMark(walk, element)
|
|
186
|
+
});
|
|
187
|
+
return;
|
|
188
|
+
}
|
|
189
|
+
pieces.push(resolveElement(walk, element));
|
|
190
|
+
};
|
|
191
|
+
for (const child of elementChildren(container)) visit(child);
|
|
192
|
+
pieces.push(walk.xml.slice(cursor, inner.end));
|
|
193
|
+
return joinParagraphs(walk, pieces);
|
|
194
|
+
};
|
|
195
|
+
/** The content of a resolved paragraph without its properties. */
|
|
196
|
+
const contentOf = (paragraphXml) => {
|
|
197
|
+
const open = paragraphXml.indexOf(">") + 1;
|
|
198
|
+
const close = paragraphXml.lastIndexOf("</");
|
|
199
|
+
if (paragraphXml.endsWith("/>") || close < open) return "";
|
|
200
|
+
const body = paragraphXml.slice(open, close);
|
|
201
|
+
if (/^\s*<([A-Za-z0-9]+:)?pPr\b/u.exec(body) === null) return body;
|
|
202
|
+
if (/^\s*<([A-Za-z0-9]+:)?pPr\b[^>]*\/>/u.test(body)) return body.slice(body.indexOf("/>") + 2);
|
|
203
|
+
const end = /<\/([A-Za-z0-9]+:)?pPr>/u.exec(body);
|
|
204
|
+
return end === null ? body : body.slice(end.index + end[0].length);
|
|
205
|
+
};
|
|
206
|
+
/** `content` put in front of what a resolved paragraph holds. */
|
|
207
|
+
const withLeadingContent = (paragraphXml, content) => {
|
|
208
|
+
if (content === "") return paragraphXml;
|
|
209
|
+
if (paragraphXml.endsWith("/>")) {
|
|
210
|
+
const name = /^<([^\s/>]+)/u.exec(paragraphXml)?.[1] ?? "w:p";
|
|
211
|
+
return `${paragraphXml.slice(0, -2)}>${content}</${name}>`;
|
|
212
|
+
}
|
|
213
|
+
const open = paragraphXml.indexOf(">") + 1;
|
|
214
|
+
const body = paragraphXml.slice(open);
|
|
215
|
+
const selfClosed = /^\s*<([A-Za-z0-9]+:)?pPr\b[^>]*\/>/u.exec(body);
|
|
216
|
+
if (selfClosed !== null) {
|
|
217
|
+
const at = open + selfClosed[0].length;
|
|
218
|
+
return `${paragraphXml.slice(0, at)}${content}${paragraphXml.slice(at)}`;
|
|
219
|
+
}
|
|
220
|
+
if (/^\s*<([A-Za-z0-9]+:)?pPr\b/u.test(body)) {
|
|
221
|
+
const end = /<\/([A-Za-z0-9]+:)?pPr>/u.exec(body);
|
|
222
|
+
if (end !== null) {
|
|
223
|
+
const at = open + end.index + end[0].length;
|
|
224
|
+
return `${paragraphXml.slice(0, at)}${content}${paragraphXml.slice(at)}`;
|
|
225
|
+
}
|
|
226
|
+
}
|
|
227
|
+
return `${paragraphXml.slice(0, open)}${content}${body}`;
|
|
228
|
+
};
|
|
229
|
+
const joinParagraphs = (walk, pieces) => {
|
|
230
|
+
let out = "";
|
|
231
|
+
/** Content of paragraphs whose mark went, waiting for the next one. */
|
|
232
|
+
let carried = "";
|
|
233
|
+
/** The last such paragraph, kept whole when no paragraph follows. */
|
|
234
|
+
let held = null;
|
|
235
|
+
const flush = () => {
|
|
236
|
+
if (held !== null) {
|
|
237
|
+
out += withLeadingContent(held, carried);
|
|
238
|
+
carried = "";
|
|
239
|
+
held = null;
|
|
240
|
+
}
|
|
241
|
+
};
|
|
242
|
+
for (const piece of pieces) {
|
|
243
|
+
if (typeof piece === "string") {
|
|
244
|
+
out += piece;
|
|
245
|
+
continue;
|
|
246
|
+
}
|
|
247
|
+
if (piece.paragraph === null) {
|
|
248
|
+
flush();
|
|
249
|
+
out += piece.xml;
|
|
250
|
+
continue;
|
|
251
|
+
}
|
|
252
|
+
if (piece.carried) {
|
|
253
|
+
walk.tally.paragraphMarks += 1;
|
|
254
|
+
if (held !== null) carried += contentOf(held);
|
|
255
|
+
held = piece.xml;
|
|
256
|
+
continue;
|
|
257
|
+
}
|
|
258
|
+
if (held !== null) {
|
|
259
|
+
carried += contentOf(held);
|
|
260
|
+
held = null;
|
|
261
|
+
}
|
|
262
|
+
out += withLeadingContent(piece.xml, carried);
|
|
263
|
+
carried = "";
|
|
264
|
+
}
|
|
265
|
+
flush();
|
|
266
|
+
return out;
|
|
267
|
+
};
|
|
268
|
+
/** The properties a recorded change gives back, inside `properties`. */
|
|
269
|
+
const restoredProperties = (walk, properties, name) => {
|
|
270
|
+
const changeName = [...PROPERTY_CHANGES].find(([, owner]) => owner === name)?.[0];
|
|
271
|
+
const change = changeName === void 0 ? null : wChild(properties, changeName);
|
|
272
|
+
if (change === null || walk.view !== "reject") return null;
|
|
273
|
+
const recorded = wChild(change, name);
|
|
274
|
+
const outer = rangeOf(properties);
|
|
275
|
+
const inner = innerRangeOf(walk.xml, properties);
|
|
276
|
+
if (outer === null || inner === null) return null;
|
|
277
|
+
const recordedInner = recorded === null ? null : innerRangeOf(walk.xml, recorded);
|
|
278
|
+
const old = recordedInner === null ? "" : walk.xml.slice(recordedInner.start, recordedInner.end);
|
|
279
|
+
const own = OWN_PROPERTIES.get(name) ?? null;
|
|
280
|
+
const kept = own === null ? "" : elementChildren(properties).filter((child) => own.has(wName(child) ?? "")).map((child) => resolveElement(walk, child)).join("");
|
|
281
|
+
const head = walk.xml.slice(outer.start, inner.start);
|
|
282
|
+
const tail = walk.xml.slice(inner.end, outer.end);
|
|
283
|
+
return name === "sectPr" ? `${head}${kept}${old}${tail}` : `${head}${old}${kept}${tail}`;
|
|
284
|
+
};
|
|
285
|
+
/** `element` in the chosen view; its own text when it holds no revision. */
|
|
286
|
+
const resolveElement = (walk, element) => {
|
|
287
|
+
const source = sourceOf(walk, element);
|
|
288
|
+
if (!mayHoldRevision(source)) return source;
|
|
289
|
+
const name = wName(element);
|
|
290
|
+
if (name !== null && [...PROPERTY_CHANGES.values()].includes(name)) {
|
|
291
|
+
const restored = restoredProperties(walk, element, name);
|
|
292
|
+
if (restored !== null) return restored;
|
|
293
|
+
}
|
|
294
|
+
const outer = rangeOf(element);
|
|
295
|
+
const inner = innerRangeOf(walk.xml, element);
|
|
296
|
+
if (outer === null) {
|
|
297
|
+
walk.sound = false;
|
|
298
|
+
return source;
|
|
299
|
+
}
|
|
300
|
+
if (inner === null) return source;
|
|
301
|
+
const content = resolveChildren(walk, element);
|
|
302
|
+
return `${walk.xml.slice(outer.start, inner.start)}${content}${walk.xml.slice(inner.end, outer.end)}`;
|
|
303
|
+
};
|
|
304
|
+
const summaryOf = (tally) => ({
|
|
305
|
+
insertions: tally.insertions,
|
|
306
|
+
deletions: tally.deletions,
|
|
307
|
+
moves: tally.moves,
|
|
308
|
+
paragraphMarks: tally.paragraphMarks,
|
|
309
|
+
tableRows: tally.tableRows,
|
|
310
|
+
formattingChanges: tally.formattingChanges,
|
|
311
|
+
authors: [...tally.authors].toSorted(),
|
|
312
|
+
first: tally.first,
|
|
313
|
+
last: tally.last
|
|
314
|
+
});
|
|
315
|
+
/**
|
|
316
|
+
* The text a deleted paragraph mark is carried as, inside a deletion of
|
|
317
|
+
* its own, by a reading that keeps tracked changes: a private-use character
|
|
318
|
+
* no document's text holds.
|
|
319
|
+
*/
|
|
320
|
+
const DELETED_PARAGRAPH_MARK = "";
|
|
321
|
+
/**
|
|
322
|
+
* The element a deleted table row is kept in by a reading that keeps
|
|
323
|
+
* tracked changes. The accepted view has no such row, so it may be no row
|
|
324
|
+
* of the table to the readers of blocks; in a namespace of its own it is
|
|
325
|
+
* none, and the writer and verification find it by this name.
|
|
326
|
+
*/
|
|
327
|
+
const KEPT_DELETED_ROW_NAMESPACE = "urn:stll:folio:kept-deleted-row";
|
|
328
|
+
const KEPT_DELETED_ROW = "keptDeletedRow";
|
|
329
|
+
/** The row a kept-deleted-row element holds, or null for any other element. */
|
|
330
|
+
const keptDeletedRowOf = (element) => element.namespaceUri === "urn:stll:folio:kept-deleted-row" ? elementChildren(element).find((child) => wName(child) === "tr") ?? null : null;
|
|
331
|
+
/** The deletion a row's properties record, or null. */
|
|
332
|
+
const deletedRowMarkOf = (row) => wChild(wChild(row, "trPr"), "del");
|
|
333
|
+
/** The insertion a row's properties record, or null. */
|
|
334
|
+
const insertedRowMarkOf = (row) => wChild(wChild(row, "trPr"), "ins");
|
|
335
|
+
/** What a deleted row held: its cells' text, deleted text included, a tab
|
|
336
|
+
* between cells and a line break between paragraphs. */
|
|
337
|
+
const rowTextOf = (row) => {
|
|
338
|
+
const paragraphText = (paragraph) => {
|
|
339
|
+
let text = "";
|
|
340
|
+
const stack = [...elementChildren(paragraph)].toReversed();
|
|
341
|
+
for (let next = stack.pop(); next !== void 0; next = stack.pop()) {
|
|
342
|
+
const name = wName(next);
|
|
343
|
+
if (name === "t" || name === "delText") text += (next.elements ?? []).map((node) => node.type === "text" ? String(node.text ?? "") : "").join("");
|
|
344
|
+
else stack.push(...elementChildren(next).toReversed());
|
|
345
|
+
}
|
|
346
|
+
return text;
|
|
347
|
+
};
|
|
348
|
+
const cellText = (cell) => {
|
|
349
|
+
const lines = [];
|
|
350
|
+
const stack = [...elementChildren(cell)].toReversed();
|
|
351
|
+
for (let next = stack.pop(); next !== void 0; next = stack.pop()) if (wName(next) === "p") lines.push(paragraphText(next));
|
|
352
|
+
else stack.push(...elementChildren(next).toReversed());
|
|
353
|
+
return lines.join("\n");
|
|
354
|
+
};
|
|
355
|
+
const cells = [];
|
|
356
|
+
const stack = [...elementChildren(row)].toReversed();
|
|
357
|
+
for (let next = stack.pop(); next !== void 0; next = stack.pop()) if (wName(next) === "tc") cells.push(cellText(next));
|
|
358
|
+
else if (wName(next) !== "trPr") stack.push(...elementChildren(next).toReversed());
|
|
359
|
+
return cells.join(" ");
|
|
360
|
+
};
|
|
361
|
+
/**
|
|
362
|
+
* The row revisions a table carries in the kept form, in order: an inserted
|
|
363
|
+
* row by the place it has among the table's rows, a deleted one by the
|
|
364
|
+
* place it was taken from and by its text; each with who made the change.
|
|
365
|
+
* Rows of a table nested in a cell are that table's.
|
|
366
|
+
*/
|
|
367
|
+
const tableRowRevisions = (table) => {
|
|
368
|
+
const out = [];
|
|
369
|
+
let rows = 0;
|
|
370
|
+
const by = (mark) => `${wAttr(mark, "author") ?? ""}:${wAttr(mark, "date") ?? ""}`;
|
|
371
|
+
const visit = (container) => {
|
|
372
|
+
for (const child of elementChildren(container)) {
|
|
373
|
+
const deleted = keptDeletedRowOf(child);
|
|
374
|
+
if (deleted !== null) out.push(`${String(rows)}:del:${by(deletedRowMarkOf(deleted))}:${rowTextOf(deleted)}`);
|
|
375
|
+
else if (wName(child) === "tr") {
|
|
376
|
+
const inserted = insertedRowMarkOf(child);
|
|
377
|
+
if (inserted !== null) out.push(`${String(rows)}:ins:${by(inserted)}`);
|
|
378
|
+
rows += 1;
|
|
379
|
+
} else if (wName(child) === "sdt") {
|
|
380
|
+
const content = wChild(child, "sdtContent");
|
|
381
|
+
if (content !== null) visit(content);
|
|
382
|
+
} else if (wName(child) === "customXml") visit(child);
|
|
383
|
+
}
|
|
384
|
+
};
|
|
385
|
+
visit(table);
|
|
386
|
+
return out;
|
|
387
|
+
};
|
|
388
|
+
const deletedMarkOf = (paragraph) => wChild(wChild(wChild(paragraph, "pPr"), "rPr"), "del");
|
|
389
|
+
/** Whether `xml` may hold a paragraph mark recorded as deleted: an empty
|
|
390
|
+
* deletion, in either spelling of an empty element. */
|
|
391
|
+
const mayHoldDeletedMark = (xml) => /<[A-Za-z0-9]+:del\b[^>]*(?:\/>|><\/)/u.test(xml);
|
|
392
|
+
/** Whether the opening tag of an element declares a namespace of its own. */
|
|
393
|
+
const declaresNamespace = (source) => /^<[^>]*\sxmlns[:=]/u.test(source);
|
|
394
|
+
/**
|
|
395
|
+
* The deletion that stands for a deleted paragraph mark, by who deleted it.
|
|
396
|
+
* It is written with the prefix of the paragraph it came from, which is
|
|
397
|
+
* bound wherever that paragraph's content may stand.
|
|
398
|
+
*/
|
|
399
|
+
const markDeletionXml = (paragraph, mark) => {
|
|
400
|
+
const prefix = (paragraph.name ?? "w:p").split(":").at(0) ?? "w";
|
|
401
|
+
const attribute = (name) => {
|
|
402
|
+
const value = wAttr(mark, name);
|
|
403
|
+
return value === null ? "" : ` ${prefix}:${name}="${escapeXmlAttribute(value)}"`;
|
|
404
|
+
};
|
|
405
|
+
return `<${prefix}:del ${prefix}:id="0"${attribute("author")}${attribute("date")}><${prefix}:r><${prefix}:delText></${prefix}:delText></${prefix}:r></${prefix}:del>`;
|
|
406
|
+
};
|
|
407
|
+
/**
|
|
408
|
+
* The children of `container` with every paragraph whose mark is deleted
|
|
409
|
+
* joined to the paragraph after it: its content, then a deletion that
|
|
410
|
+
* stands for the mark, then that paragraph's own content under that
|
|
411
|
+
* paragraph's properties. This is what the document reads with its changes
|
|
412
|
+
* accepted, with the deleted mark still on record. A paragraph that has no
|
|
413
|
+
* paragraph after it in its container keeps its mark, and so does one whose
|
|
414
|
+
* own tag declares a namespace: its content would lose the binding in
|
|
415
|
+
* another paragraph.
|
|
416
|
+
*/
|
|
417
|
+
const joinChildren = (walk, container) => {
|
|
418
|
+
const inner = innerRangeOf(walk.xml, container);
|
|
419
|
+
if (inner === null) return "";
|
|
420
|
+
let out = "";
|
|
421
|
+
let cursor = inner.start;
|
|
422
|
+
/** A paragraph whose mark is deleted, waiting for the next paragraph. */
|
|
423
|
+
let held = null;
|
|
424
|
+
const release = () => {
|
|
425
|
+
if (held !== null) {
|
|
426
|
+
out += held.xml;
|
|
427
|
+
held = null;
|
|
428
|
+
}
|
|
429
|
+
};
|
|
430
|
+
for (const child of elementChildren(container)) {
|
|
431
|
+
const range = rangeOf(child);
|
|
432
|
+
if (range === null) {
|
|
433
|
+
walk.sound = false;
|
|
434
|
+
return "";
|
|
435
|
+
}
|
|
436
|
+
const between = walk.xml.slice(cursor, range.start);
|
|
437
|
+
cursor = range.end;
|
|
438
|
+
const source = walk.xml.slice(range.start, range.end);
|
|
439
|
+
if (wName(child) === "tr" && deletedRowMarkOf(child) !== null) {
|
|
440
|
+
release();
|
|
441
|
+
out += `${between}<kept:${KEPT_DELETED_ROW} xmlns:kept="${KEPT_DELETED_ROW_NAMESPACE}">${source}</kept:${KEPT_DELETED_ROW}>`;
|
|
442
|
+
continue;
|
|
443
|
+
}
|
|
444
|
+
if (wName(child) !== "p") {
|
|
445
|
+
release();
|
|
446
|
+
out += between + joinElement(walk, child, source);
|
|
447
|
+
continue;
|
|
448
|
+
}
|
|
449
|
+
const carried = held === null ? "" : held.content;
|
|
450
|
+
const joined = withLeadingContent(source, carried);
|
|
451
|
+
const mark = declaresNamespace(source) ? null : deletedMarkOf(child);
|
|
452
|
+
if (mark === null) {
|
|
453
|
+
out += between + joined;
|
|
454
|
+
held = null;
|
|
455
|
+
continue;
|
|
456
|
+
}
|
|
457
|
+
held = {
|
|
458
|
+
xml: between + joined,
|
|
459
|
+
content: contentOf(joined) + markDeletionXml(child, mark)
|
|
460
|
+
};
|
|
461
|
+
}
|
|
462
|
+
release();
|
|
463
|
+
return out + walk.xml.slice(cursor, inner.end);
|
|
464
|
+
};
|
|
465
|
+
const joinElement = (walk, element, source) => {
|
|
466
|
+
const outer = rangeOf(element);
|
|
467
|
+
const inner = innerRangeOf(walk.xml, element);
|
|
468
|
+
if (!mayHoldDeletedMark(source) || outer === null || inner === null) return source;
|
|
469
|
+
return `${walk.xml.slice(outer.start, inner.start)}${joinChildren(walk, element)}${walk.xml.slice(inner.end, outer.end)}`;
|
|
470
|
+
};
|
|
471
|
+
/** One part with its deleted paragraph marks carried as deletions; null
|
|
472
|
+
* when it cannot be read whole. */
|
|
473
|
+
const joinDeletedMarksInPart = (path, xml) => {
|
|
474
|
+
if (!mayHoldDeletedMark(xml)) return xml;
|
|
475
|
+
const part = readRawPart(path, xml);
|
|
476
|
+
const outer = part === null ? null : rangeOf(part.root);
|
|
477
|
+
if (part === null || outer === null) return null;
|
|
478
|
+
const walk = {
|
|
479
|
+
xml,
|
|
480
|
+
sound: true
|
|
481
|
+
};
|
|
482
|
+
const joined = joinElement(walk, part.root, xml.slice(outer.start, outer.end));
|
|
483
|
+
return walk.sound ? spliceXml(xml, [{
|
|
484
|
+
...outer,
|
|
485
|
+
newXml: joined
|
|
486
|
+
}]) : null;
|
|
487
|
+
};
|
|
488
|
+
/**
|
|
489
|
+
* A package in the form a reading that keeps tracked changes takes it in:
|
|
490
|
+
* the main document and the notes with their deleted paragraph marks
|
|
491
|
+
* carried as deletions. The bytes are the document's own when it has none.
|
|
492
|
+
*/
|
|
493
|
+
const withDeletedMarksJoined = async (docx) => {
|
|
494
|
+
const loaded = await loadPackage(docx);
|
|
495
|
+
if (loaded.isErr()) return Result.err(loaded.error);
|
|
496
|
+
const pkg = loaded.value;
|
|
497
|
+
const paths = [
|
|
498
|
+
pkg.documentPath,
|
|
499
|
+
pkg.partPath("footnotes"),
|
|
500
|
+
pkg.partPath("endnotes")
|
|
501
|
+
].filter((path) => path !== null);
|
|
502
|
+
const rewritten = /* @__PURE__ */ new Map();
|
|
503
|
+
for (const path of paths) {
|
|
504
|
+
const xml = path === pkg.documentPath ? pkg.documentXml : await pkg.readText(path);
|
|
505
|
+
if (xml === null) continue;
|
|
506
|
+
const joined = joinDeletedMarksInPart(path, xml);
|
|
507
|
+
if (joined === null) return Result.err(new TemplatePlanError({
|
|
508
|
+
code: "xml-unreadable",
|
|
509
|
+
message: `The deleted paragraph marks of ${path} could not be read.`
|
|
510
|
+
}));
|
|
511
|
+
if (joined !== xml) rewritten.set(path, joined);
|
|
512
|
+
}
|
|
513
|
+
if (rewritten.size === 0) return Result.ok(docx);
|
|
514
|
+
return await Result.tryPromise({
|
|
515
|
+
try: async () => {
|
|
516
|
+
const zip = await ZipArchive.read(docx);
|
|
517
|
+
for (const [path, content] of rewritten) {
|
|
518
|
+
const current = zip.entry(path);
|
|
519
|
+
zip.set(path, content, current === void 0 ? {} : { modified: current.modified });
|
|
520
|
+
}
|
|
521
|
+
return await zip.generate();
|
|
522
|
+
},
|
|
523
|
+
catch: (cause) => new TemplatePlanError({
|
|
524
|
+
code: "archive-unreadable",
|
|
525
|
+
message: "The package could not be rewritten with its marks joined.",
|
|
526
|
+
cause
|
|
527
|
+
})
|
|
528
|
+
});
|
|
529
|
+
};
|
|
530
|
+
const REVISION_WRAPPERS = /* @__PURE__ */ new Set([
|
|
531
|
+
"ins",
|
|
532
|
+
"del",
|
|
533
|
+
"moveFrom",
|
|
534
|
+
"moveTo"
|
|
535
|
+
]);
|
|
536
|
+
const countObstacles = (root, into) => {
|
|
537
|
+
const visit = (element, parent, grandparent) => {
|
|
538
|
+
const name = wName(element);
|
|
539
|
+
if (name !== null && REVISION_WRAPPERS.has(name)) {
|
|
540
|
+
if (parent === "rPr" && grandparent === "pPr") {
|
|
541
|
+
into.paragraphMarks += 1;
|
|
542
|
+
into.moves += name === "moveFrom" || name === "moveTo" ? 1 : 0;
|
|
543
|
+
} else if (parent === "trPr") into.tableRows += 1;
|
|
544
|
+
else if (name === "moveFrom" || name === "moveTo") into.moves += 1;
|
|
545
|
+
} else if (name !== null && CELL_MARKS.has(name)) into.tableCells += 1;
|
|
546
|
+
for (const child of elementChildren(element)) visit(child, name, parent);
|
|
547
|
+
};
|
|
548
|
+
visit(root, null, null);
|
|
549
|
+
};
|
|
550
|
+
/**
|
|
551
|
+
* What a document tracks, and which of it a plan that keeps tracked changes
|
|
552
|
+
* cannot carry: a change that makes the paragraphs of the accepted and the
|
|
553
|
+
* rejected view differ.
|
|
554
|
+
*/
|
|
555
|
+
const keptRevisionObstacles = async (docx) => {
|
|
556
|
+
const loaded = await loadPackage(docx);
|
|
557
|
+
if (loaded.isErr()) return Result.err(loaded.error);
|
|
558
|
+
const pkg = loaded.value;
|
|
559
|
+
const tally = newTally();
|
|
560
|
+
const obstacles = {
|
|
561
|
+
paragraphMarks: 0,
|
|
562
|
+
tableRows: 0,
|
|
563
|
+
tableCells: 0,
|
|
564
|
+
moves: 0
|
|
565
|
+
};
|
|
566
|
+
const paths = [
|
|
567
|
+
pkg.documentPath,
|
|
568
|
+
pkg.partPath("footnotes"),
|
|
569
|
+
pkg.partPath("endnotes")
|
|
570
|
+
].filter((path) => path !== null);
|
|
571
|
+
for (const path of paths) {
|
|
572
|
+
const xml = path === pkg.documentPath ? pkg.documentXml : await pkg.readText(path);
|
|
573
|
+
if (xml === null || !mayHoldRevision(xml)) continue;
|
|
574
|
+
const part = readRawPart(path, xml);
|
|
575
|
+
if (part !== null) {
|
|
576
|
+
tallyPart(part.root, tally);
|
|
577
|
+
countObstacles(part.root, obstacles);
|
|
578
|
+
}
|
|
579
|
+
}
|
|
580
|
+
return Result.ok({
|
|
581
|
+
tracked: {
|
|
582
|
+
...summaryOf(tally),
|
|
583
|
+
paragraphMarks: obstacles.paragraphMarks,
|
|
584
|
+
tableRows: obstacles.tableRows
|
|
585
|
+
},
|
|
586
|
+
obstacles
|
|
587
|
+
});
|
|
588
|
+
};
|
|
589
|
+
/** One part in the chosen view, or null when it cannot be read whole. */
|
|
590
|
+
const resolvePartRevisions = (path, xml, view) => {
|
|
591
|
+
const part = readRawPart(path, xml);
|
|
592
|
+
if (part === null) return null;
|
|
593
|
+
const walk = {
|
|
594
|
+
xml,
|
|
595
|
+
view,
|
|
596
|
+
tally: newTally(),
|
|
597
|
+
sound: true
|
|
598
|
+
};
|
|
599
|
+
const outer = rangeOf(part.root);
|
|
600
|
+
if (outer === null) return null;
|
|
601
|
+
tallyPart(part.root, walk.tally);
|
|
602
|
+
const resolved = resolveElement(walk, part.root);
|
|
603
|
+
if (!walk.sound) return null;
|
|
604
|
+
const rewritten = spliceXml(xml, [{
|
|
605
|
+
...outer,
|
|
606
|
+
newXml: resolved
|
|
607
|
+
}]);
|
|
608
|
+
return rewritten === null ? null : {
|
|
609
|
+
xml: rewritten,
|
|
610
|
+
tally: walk.tally
|
|
611
|
+
};
|
|
612
|
+
};
|
|
613
|
+
const mergeTallies = (tallies) => {
|
|
614
|
+
const total = newTally();
|
|
615
|
+
for (const tally of tallies) {
|
|
616
|
+
total.insertions += tally.insertions;
|
|
617
|
+
total.deletions += tally.deletions;
|
|
618
|
+
total.moves += tally.moves;
|
|
619
|
+
total.paragraphMarks += tally.paragraphMarks;
|
|
620
|
+
total.tableRows += tally.tableRows;
|
|
621
|
+
total.formattingChanges += tally.formattingChanges;
|
|
622
|
+
for (const author of tally.authors) total.authors.add(author);
|
|
623
|
+
for (const date of [tally.first, tally.last]) if (date !== null) {
|
|
624
|
+
total.first = total.first === null || date < total.first ? date : total.first;
|
|
625
|
+
total.last = total.last === null || date > total.last ? date : total.last;
|
|
626
|
+
}
|
|
627
|
+
}
|
|
628
|
+
return total;
|
|
629
|
+
};
|
|
630
|
+
/**
|
|
631
|
+
* A package with the tracked changes of its main document and its notes
|
|
632
|
+
* resolved to `view`, and what was resolved. The bytes are the source's own
|
|
633
|
+
* when it tracks nothing.
|
|
634
|
+
*/
|
|
635
|
+
const resolveTrackedChanges = async (docx, view) => {
|
|
636
|
+
const loaded = await loadPackage(docx);
|
|
637
|
+
if (loaded.isErr()) return Result.err(loaded.error);
|
|
638
|
+
const pkg = loaded.value;
|
|
639
|
+
const paths = [
|
|
640
|
+
pkg.documentPath,
|
|
641
|
+
pkg.partPath("footnotes"),
|
|
642
|
+
pkg.partPath("endnotes")
|
|
643
|
+
].filter((path) => path !== null);
|
|
644
|
+
const rewritten = /* @__PURE__ */ new Map();
|
|
645
|
+
const tallies = [];
|
|
646
|
+
for (const path of paths) {
|
|
647
|
+
const xml = path === pkg.documentPath ? pkg.documentXml : await pkg.readText(path);
|
|
648
|
+
if (xml === null || !mayHoldRevision(xml)) continue;
|
|
649
|
+
const resolved = resolvePartRevisions(path, xml, view);
|
|
650
|
+
if (resolved === null) return Result.err(new TemplatePlanError({
|
|
651
|
+
code: "xml-unreadable",
|
|
652
|
+
message: `The tracked changes of ${path} could not be resolved.`
|
|
653
|
+
}));
|
|
654
|
+
tallies.push(resolved.tally);
|
|
655
|
+
if (resolved.xml !== xml) rewritten.set(path, resolved.xml);
|
|
656
|
+
}
|
|
657
|
+
const resolved = summaryOf(mergeTallies(tallies));
|
|
658
|
+
if (rewritten.size === 0) return Result.ok({
|
|
659
|
+
docx,
|
|
660
|
+
resolved
|
|
661
|
+
});
|
|
662
|
+
return await Result.tryPromise({
|
|
663
|
+
try: async () => {
|
|
664
|
+
const zip = await ZipArchive.read(docx);
|
|
665
|
+
for (const [path, content] of rewritten) {
|
|
666
|
+
const current = zip.entry(path);
|
|
667
|
+
zip.set(path, content, current === void 0 ? {} : { modified: current.modified });
|
|
668
|
+
}
|
|
669
|
+
return {
|
|
670
|
+
docx: await zip.generate(),
|
|
671
|
+
resolved
|
|
672
|
+
};
|
|
673
|
+
},
|
|
674
|
+
catch: (cause) => new TemplatePlanError({
|
|
675
|
+
code: "archive-unreadable",
|
|
676
|
+
message: "The package could not be rewritten without its tracked changes.",
|
|
677
|
+
cause
|
|
678
|
+
})
|
|
679
|
+
});
|
|
680
|
+
};
|
|
681
|
+
export { DELETED_PARAGRAPH_MARK, KEPT_DELETED_ROW_NAMESPACE, deletedRowMarkOf, insertedRowMarkOf, joinDeletedMarksInPart, keptDeletedRowOf, keptRevisionObstacles, resolvePartRevisions, resolveTrackedChanges, rowTextOf, tableRowRevisions, withDeletedMarksJoined };
|