@beyondwork/docx-react-component 1.0.1 → 1.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +44 -104
- package/package.json +76 -46
- package/src/README.md +85 -0
- package/src/api/README.md +22 -0
- package/src/api/public-types.ts +525 -0
- package/src/compare/diff-engine.ts +530 -0
- package/src/compare/export-redlines.ts +162 -0
- package/src/compare/snapshot.ts +37 -0
- package/src/component-inventory.md +99 -0
- package/src/core/README.md +10 -0
- package/src/core/commands/README.md +3 -0
- package/src/core/commands/formatting-commands.ts +161 -0
- package/src/core/commands/image-commands.ts +144 -0
- package/src/core/commands/index.ts +1013 -0
- package/src/core/commands/list-commands.ts +370 -0
- package/src/core/commands/review-commands.ts +108 -0
- package/src/core/commands/text-commands.ts +119 -0
- package/src/core/schema/README.md +3 -0
- package/src/core/schema/text-schema.ts +512 -0
- package/src/core/selection/README.md +3 -0
- package/src/core/selection/mapping.ts +238 -0
- package/src/core/selection/review-anchors.ts +94 -0
- package/src/core/state/README.md +3 -0
- package/src/core/state/editor-state.ts +580 -0
- package/src/core/state/text-transaction.ts +276 -0
- package/src/formats/xlsx/io/parse-shared-strings.ts +41 -0
- package/src/formats/xlsx/io/parse-sheet.ts +289 -0
- package/src/formats/xlsx/io/parse-styles.ts +57 -0
- package/src/formats/xlsx/io/parse-workbook.ts +75 -0
- package/src/formats/xlsx/io/xlsx-session.ts +306 -0
- package/src/formats/xlsx/model/cell.ts +189 -0
- package/src/formats/xlsx/model/sheet.ts +244 -0
- package/src/formats/xlsx/model/styles.ts +118 -0
- package/src/formats/xlsx/model/workbook.ts +449 -0
- package/src/index.ts +45 -0
- package/src/io/README.md +10 -0
- package/src/io/docx-session.ts +1763 -0
- package/src/io/export/README.md +3 -0
- package/src/io/export/export-session.ts +165 -0
- package/src/io/export/minimal-docx.ts +115 -0
- package/src/io/export/reattach-preserved-parts.ts +54 -0
- package/src/io/export/serialize-comments.ts +876 -0
- package/src/io/export/serialize-footnotes.ts +217 -0
- package/src/io/export/serialize-headers-footers.ts +200 -0
- package/src/io/export/serialize-main-document.ts +982 -0
- package/src/io/export/serialize-numbering.ts +97 -0
- package/src/io/export/serialize-revisions.ts +389 -0
- package/src/io/export/serialize-runtime-revisions.ts +265 -0
- package/src/io/export/serialize-tables.ts +147 -0
- package/src/io/export/split-review-boundaries.ts +194 -0
- package/src/io/normalize/README.md +3 -0
- package/src/io/normalize/normalize-text.ts +437 -0
- package/src/io/ooxml/README.md +3 -0
- package/src/io/ooxml/parse-comments.ts +779 -0
- package/src/io/ooxml/parse-complex-content.ts +287 -0
- package/src/io/ooxml/parse-fields.ts +438 -0
- package/src/io/ooxml/parse-footnotes.ts +403 -0
- package/src/io/ooxml/parse-headers-footers.ts +483 -0
- package/src/io/ooxml/parse-inline-media.ts +431 -0
- package/src/io/ooxml/parse-main-document.ts +1846 -0
- package/src/io/ooxml/parse-numbering.ts +425 -0
- package/src/io/ooxml/parse-revisions.ts +658 -0
- package/src/io/ooxml/parse-shapes.ts +271 -0
- package/src/io/ooxml/parse-tables.ts +568 -0
- package/src/io/ooxml/parse-theme.ts +314 -0
- package/src/io/ooxml/part-manifest.ts +136 -0
- package/src/io/ooxml/revision-boundaries.ts +351 -0
- package/src/io/opc/README.md +3 -0
- package/src/io/opc/corrupt-package.ts +166 -0
- package/src/io/opc/docx-package.ts +74 -0
- package/src/io/opc/package-reader.ts +320 -0
- package/src/io/opc/package-writer.ts +273 -0
- package/src/legal/bookmarks.ts +196 -0
- package/src/legal/cross-references.ts +356 -0
- package/src/legal/defined-terms.ts +203 -0
- package/src/model/README.md +3 -0
- package/src/model/canonical-document.ts +1911 -0
- package/src/model/cds-1.0.0.ts +196 -0
- package/src/model/snapshot.ts +393 -0
- package/src/preservation/README.md +3 -0
- package/src/preservation/markup-compatibility.ts +48 -0
- package/src/preservation/opaque-fragment-store.ts +89 -0
- package/src/preservation/opaque-region.ts +233 -0
- package/src/preservation/package-preservation.ts +120 -0
- package/src/preservation/preserved-part-manifest.ts +56 -0
- package/src/preservation/relationship-retention.ts +57 -0
- package/src/preservation/store.ts +185 -0
- package/src/review/README.md +16 -0
- package/src/review/store/README.md +3 -0
- package/src/review/store/comment-anchors.ts +70 -0
- package/src/review/store/comment-remapping.ts +154 -0
- package/src/review/store/comment-store.ts +331 -0
- package/src/review/store/comment-thread.ts +109 -0
- package/src/review/store/revision-actions.ts +394 -0
- package/src/review/store/revision-store.ts +303 -0
- package/src/review/store/revision-types.ts +168 -0
- package/src/review/store/runtime-comment-store.ts +43 -0
- package/src/runtime/README.md +3 -0
- package/src/runtime/ai-action-policy.ts +764 -0
- package/src/runtime/document-runtime.ts +967 -0
- package/src/runtime/read-only-diagnostics-runtime.ts +232 -0
- package/src/runtime/review-runtime.ts +44 -0
- package/src/runtime/revision-runtime.ts +107 -0
- package/src/runtime/session-capabilities.ts +138 -0
- package/src/runtime/surface-projection.ts +570 -0
- package/src/runtime/table-commands.ts +87 -0
- package/src/runtime/table-schema.ts +140 -0
- package/src/runtime/virtualized-rendering.ts +258 -0
- package/src/ui/README.md +30 -0
- package/src/ui/WordReviewEditor.tsx +1504 -0
- package/src/ui/comments/README.md +3 -0
- package/src/ui/compatibility/README.md +3 -0
- package/src/ui/editor-surface/README.md +3 -0
- package/src/ui/headless/comment-decoration-model.ts +124 -0
- package/src/ui/headless/revision-decoration-model.ts +128 -0
- package/src/ui/headless/selection-helpers.ts +34 -0
- package/src/ui/headless/use-editor-keyboard.ts +98 -0
- package/src/ui/review/README.md +3 -0
- package/src/ui/shared/revision-filters.ts +31 -0
- package/src/ui/status/README.md +3 -0
- package/src/ui/theme/README.md +3 -0
- package/src/ui/toolbar/README.md +3 -0
- package/src/ui-tailwind/chrome/tw-alert-banner.tsx +48 -0
- package/src/ui-tailwind/chrome/tw-selection-toolbar.tsx +44 -0
- package/src/ui-tailwind/chrome/tw-unsaved-modal.tsx +58 -0
- package/src/ui-tailwind/chrome/use-before-unload.ts +20 -0
- package/src/ui-tailwind/editor-surface/pm-command-bridge.ts +139 -0
- package/src/ui-tailwind/editor-surface/pm-decorations.ts +98 -0
- package/src/ui-tailwind/editor-surface/pm-position-map.ts +123 -0
- package/src/ui-tailwind/editor-surface/pm-schema.ts +452 -0
- package/src/ui-tailwind/editor-surface/pm-state-from-snapshot.ts +327 -0
- package/src/ui-tailwind/editor-surface/search-plugin.ts +157 -0
- package/src/ui-tailwind/editor-surface/tw-caret.tsx +12 -0
- package/src/ui-tailwind/editor-surface/tw-editor-surface.tsx +150 -0
- package/src/ui-tailwind/editor-surface/tw-inline-token.tsx +118 -0
- package/src/ui-tailwind/editor-surface/tw-opaque-block.tsx +52 -0
- package/src/ui-tailwind/editor-surface/tw-paragraph-block.tsx +151 -0
- package/src/ui-tailwind/editor-surface/tw-prosemirror-surface.tsx +215 -0
- package/src/ui-tailwind/editor-surface/tw-segment-view.tsx +111 -0
- package/src/ui-tailwind/editor-surface/tw-table-node-view.tsx +122 -0
- package/src/ui-tailwind/index.ts +61 -0
- package/src/ui-tailwind/review/tw-comment-sidebar.tsx +276 -0
- package/src/ui-tailwind/review/tw-health-panel.tsx +120 -0
- package/src/ui-tailwind/review/tw-review-rail.tsx +120 -0
- package/src/ui-tailwind/review/tw-revision-sidebar.tsx +164 -0
- package/src/ui-tailwind/status/tw-status-bar.tsx +58 -0
- package/src/ui-tailwind/theme/editor-theme.css +190 -0
- package/src/ui-tailwind/toolbar/tw-toolbar-icon-button.tsx +48 -0
- package/src/ui-tailwind/toolbar/tw-toolbar.tsx +231 -0
- package/src/ui-tailwind/tw-review-workspace.tsx +140 -0
- package/src/validation/README.md +3 -0
- package/src/validation/compatibility-engine.ts +317 -0
- package/src/validation/compatibility-report.ts +160 -0
- package/src/validation/diagnostics.ts +203 -0
- package/src/validation/import-diagnostics.ts +128 -0
- package/src/validation/low-priority-word-surfaces.ts +373 -0
- package/dist/chunk-32W6IVQE.js +0 -7725
- package/dist/chunk-32W6IVQE.js.map +0 -1
- package/dist/index.cjs +0 -23722
- package/dist/index.cjs.map +0 -1
- package/dist/index.d.cts +0 -7
- package/dist/index.d.ts +0 -7
- package/dist/index.js +0 -16011
- package/dist/index.js.map +0 -1
- package/dist/public-types-DqCURAz8.d.cts +0 -1152
- package/dist/public-types-DqCURAz8.d.ts +0 -1152
- package/dist/tailwind.cjs +0 -8295
- package/dist/tailwind.cjs.map +0 -1
- package/dist/tailwind.d.cts +0 -323
- package/dist/tailwind.d.ts +0 -323
- package/dist/tailwind.js +0 -553
- package/dist/tailwind.js.map +0 -1
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
import type { NumberingCatalog, ParagraphNode } from "../../model/canonical-document.ts";
|
|
2
|
+
|
|
3
|
+
export const WORD_NUMBERING_CONTENT_TYPE =
|
|
4
|
+
"application/vnd.openxmlformats-officedocument.wordprocessingml.numbering+xml";
|
|
5
|
+
|
|
6
|
+
export function serializeNumberingXml(catalog: NumberingCatalog): string {
|
|
7
|
+
const abstractDefinitions = Object.values(catalog.abstractDefinitions).sort((left, right) =>
|
|
8
|
+
compareSerializedIds(left.abstractNumberingId, right.abstractNumberingId),
|
|
9
|
+
);
|
|
10
|
+
const instances = Object.values(catalog.instances).sort((left, right) =>
|
|
11
|
+
compareSerializedIds(left.numberingInstanceId, right.numberingInstanceId),
|
|
12
|
+
);
|
|
13
|
+
|
|
14
|
+
const body = [
|
|
15
|
+
...abstractDefinitions.map((definition) => serializeAbstractDefinition(definition)),
|
|
16
|
+
...instances.map((instance) => serializeInstance(instance)),
|
|
17
|
+
].join("");
|
|
18
|
+
|
|
19
|
+
return [
|
|
20
|
+
`<?xml version="1.0" encoding="UTF-8" standalone="yes"?>`,
|
|
21
|
+
`<w:numbering xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main">${body}</w:numbering>`,
|
|
22
|
+
].join("\n");
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export function serializeParagraphNumberingProperties(
|
|
26
|
+
numbering: ParagraphNode["numbering"],
|
|
27
|
+
): string {
|
|
28
|
+
if (!numbering) {
|
|
29
|
+
return "";
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
return `<w:numPr><w:ilvl w:val="${numbering.level}"/><w:numId w:val="${escapeAttribute(
|
|
33
|
+
stripCanonicalPrefix(numbering.numberingInstanceId, "num:"),
|
|
34
|
+
)}"/></w:numPr>`;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
function serializeAbstractDefinition(definition: NumberingCatalog["abstractDefinitions"][string]): string {
|
|
38
|
+
const abstractNumId = escapeAttribute(
|
|
39
|
+
stripCanonicalPrefix(definition.abstractNumberingId, "abstract-num:"),
|
|
40
|
+
);
|
|
41
|
+
const levels = [...definition.levels]
|
|
42
|
+
.sort((left, right) => left.level - right.level)
|
|
43
|
+
.map((level) => serializeLevel(level))
|
|
44
|
+
.join("");
|
|
45
|
+
|
|
46
|
+
return `<w:abstractNum w:abstractNumId="${abstractNumId}">${levels}</w:abstractNum>`;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
function serializeLevel(level: NumberingCatalog["abstractDefinitions"][string]["levels"][number]): string {
|
|
50
|
+
const start = level.startAt !== undefined ? `<w:start w:val="${level.startAt}"/>` : "";
|
|
51
|
+
const paragraphStyle = level.paragraphStyleId
|
|
52
|
+
? `<w:pStyle w:val="${escapeAttribute(level.paragraphStyleId)}"/>`
|
|
53
|
+
: "";
|
|
54
|
+
|
|
55
|
+
return `<w:lvl w:ilvl="${level.level}">${start}<w:numFmt w:val="${escapeAttribute(
|
|
56
|
+
level.format,
|
|
57
|
+
)}"/><w:lvlText w:val="${escapeAttribute(level.text)}"/>${paragraphStyle}</w:lvl>`;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
function serializeInstance(instance: NumberingCatalog["instances"][string]): string {
|
|
61
|
+
const numId = escapeAttribute(stripCanonicalPrefix(instance.numberingInstanceId, "num:"));
|
|
62
|
+
const abstractNumId = escapeAttribute(
|
|
63
|
+
stripCanonicalPrefix(instance.abstractNumberingId, "abstract-num:"),
|
|
64
|
+
);
|
|
65
|
+
const overrides = [...instance.overrides]
|
|
66
|
+
.sort((left, right) => left.level - right.level)
|
|
67
|
+
.map((override) => serializeOverride(override))
|
|
68
|
+
.join("");
|
|
69
|
+
|
|
70
|
+
return `<w:num w:numId="${numId}"><w:abstractNumId w:val="${abstractNumId}"/>${overrides}</w:num>`;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
function serializeOverride(override: NumberingCatalog["instances"][string]["overrides"][number]): string {
|
|
74
|
+
const startOverride =
|
|
75
|
+
override.startAt !== undefined ? `<w:startOverride w:val="${override.startAt}"/>` : "";
|
|
76
|
+
return `<w:lvlOverride w:ilvl="${override.level}">${startOverride}</w:lvlOverride>`;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
function compareSerializedIds(left: string, right: string): number {
|
|
80
|
+
return stripKnownPrefix(left).localeCompare(stripKnownPrefix(right), "en", { numeric: true });
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
function stripKnownPrefix(value: string): string {
|
|
84
|
+
return value.replace(/^abstract-num:|^num:/, "");
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
function stripCanonicalPrefix(value: string, prefix: "abstract-num:" | "num:"): string {
|
|
88
|
+
return value.startsWith(prefix) ? value.slice(prefix.length) : value;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
function escapeAttribute(value: string): string {
|
|
92
|
+
return value
|
|
93
|
+
.replace(/&/g, "&")
|
|
94
|
+
.replace(/"/g, """)
|
|
95
|
+
.replace(/</g, "<")
|
|
96
|
+
.replace(/>/g, ">");
|
|
97
|
+
}
|
|
@@ -0,0 +1,389 @@
|
|
|
1
|
+
import type { RevisionRecord } from "../../review/store/revision-types.ts";
|
|
2
|
+
import type {
|
|
3
|
+
ParsedRevisionsResult,
|
|
4
|
+
PreservedRevisionMarkup,
|
|
5
|
+
} from "../ooxml/parse-revisions.ts";
|
|
6
|
+
import {
|
|
7
|
+
mapRevisionBoundaries,
|
|
8
|
+
type RevisionParagraphBoundary,
|
|
9
|
+
} from "../ooxml/revision-boundaries.ts";
|
|
10
|
+
|
|
11
|
+
export interface SerializeRevisionsOptions {
|
|
12
|
+
revisions: readonly RevisionRecord[];
|
|
13
|
+
preservedMarkup: readonly PreservedRevisionMarkup[];
|
|
14
|
+
boundaries?: readonly RevisionParagraphBoundary[];
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
interface XmlReplacement {
|
|
18
|
+
start: number;
|
|
19
|
+
end: number;
|
|
20
|
+
replacement: string;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
interface ParagraphMarkupDecision {
|
|
24
|
+
revisionId: string;
|
|
25
|
+
paragraphIndex: number;
|
|
26
|
+
rawMarkup: string;
|
|
27
|
+
mergeFromParagraphIndex?: number;
|
|
28
|
+
mergeToParagraphIndex?: number;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export function serializeRevisionsIntoDocumentXml(
|
|
32
|
+
documentXml: string,
|
|
33
|
+
options: SerializeRevisionsOptions | ParsedRevisionsResult,
|
|
34
|
+
): string {
|
|
35
|
+
const revisions = options.revisions;
|
|
36
|
+
const preservedMarkup = options.preservedMarkup
|
|
37
|
+
.slice()
|
|
38
|
+
.sort((left, right) => left.xmlStart - right.xmlStart);
|
|
39
|
+
const revisionById = new Map(revisions.map((revision) => [revision.revisionId, revision]));
|
|
40
|
+
const boundaries = options.boundaries ?? mapRevisionBoundaries(documentXml);
|
|
41
|
+
const replacements: XmlReplacement[] = [];
|
|
42
|
+
const consumedRevisionIds = new Set<string>();
|
|
43
|
+
const paragraphDecisions = collectParagraphMarkupDecisions(
|
|
44
|
+
preservedMarkup,
|
|
45
|
+
revisionById,
|
|
46
|
+
boundaries,
|
|
47
|
+
);
|
|
48
|
+
|
|
49
|
+
replacements.push(
|
|
50
|
+
...createParagraphStructuralReplacements(
|
|
51
|
+
documentXml,
|
|
52
|
+
boundaries,
|
|
53
|
+
paragraphDecisions,
|
|
54
|
+
consumedRevisionIds,
|
|
55
|
+
),
|
|
56
|
+
);
|
|
57
|
+
|
|
58
|
+
for (const markup of preservedMarkup) {
|
|
59
|
+
if (consumedRevisionIds.has(markup.revisionId)) {
|
|
60
|
+
continue;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
const revision = revisionById.get(markup.revisionId);
|
|
64
|
+
|
|
65
|
+
if (
|
|
66
|
+
revision?.kind === "property-change" &&
|
|
67
|
+
revision.status === "rejected" &&
|
|
68
|
+
markup.containerXmlStart !== undefined &&
|
|
69
|
+
markup.containerXmlEnd !== undefined &&
|
|
70
|
+
markup.beforeContainerXml !== undefined
|
|
71
|
+
) {
|
|
72
|
+
replacements.push({
|
|
73
|
+
start: markup.containerXmlStart,
|
|
74
|
+
end: markup.containerXmlEnd,
|
|
75
|
+
replacement: markup.beforeContainerXml,
|
|
76
|
+
});
|
|
77
|
+
continue;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
replacements.push({
|
|
81
|
+
start: markup.xmlStart,
|
|
82
|
+
end: markup.xmlEnd,
|
|
83
|
+
replacement: serializeMarkup(markup, revision),
|
|
84
|
+
});
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
return applyReplacements(documentXml, replacements);
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
function serializeMarkup(
|
|
91
|
+
markup: PreservedRevisionMarkup,
|
|
92
|
+
revision: RevisionRecord | undefined,
|
|
93
|
+
): string {
|
|
94
|
+
if (!revision) {
|
|
95
|
+
return markup.rawXml;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
switch (revision.kind) {
|
|
99
|
+
case "move":
|
|
100
|
+
case "formatting":
|
|
101
|
+
return markup.rawXml;
|
|
102
|
+
case "property-change":
|
|
103
|
+
return serializePropertyChangeMarkup(markup.rawXml, revision.status);
|
|
104
|
+
case "insertion":
|
|
105
|
+
return serializeInsertionMarkup(markup.rawXml, revision.status);
|
|
106
|
+
case "deletion":
|
|
107
|
+
return serializeDeletionMarkup(markup.rawXml, revision.status);
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
function collectParagraphMarkupDecisions(
|
|
112
|
+
preservedMarkup: readonly PreservedRevisionMarkup[],
|
|
113
|
+
revisionById: ReadonlyMap<string, RevisionRecord>,
|
|
114
|
+
boundaries: readonly RevisionParagraphBoundary[],
|
|
115
|
+
): ParagraphMarkupDecision[] {
|
|
116
|
+
const decisions: ParagraphMarkupDecision[] = [];
|
|
117
|
+
|
|
118
|
+
for (const markup of preservedMarkup) {
|
|
119
|
+
if (!markup.originalRevisionType.startsWith("paragraph-")) {
|
|
120
|
+
continue;
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
const revision = revisionById.get(markup.revisionId);
|
|
124
|
+
const paragraphIndex = markup.paragraphIndex;
|
|
125
|
+
if (!revision || paragraphIndex === undefined) {
|
|
126
|
+
continue;
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
if (revision.status === "active" || revision.status === "detached") {
|
|
130
|
+
continue;
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
if (markup.originalRevisionType === "paragraph-del") {
|
|
134
|
+
decisions.push({
|
|
135
|
+
revisionId: markup.revisionId,
|
|
136
|
+
paragraphIndex,
|
|
137
|
+
rawMarkup: markup.rawXml,
|
|
138
|
+
...(revision.status === "accepted" && boundaries[paragraphIndex + 1]
|
|
139
|
+
? { mergeFromParagraphIndex: paragraphIndex, mergeToParagraphIndex: paragraphIndex + 1 }
|
|
140
|
+
: {}),
|
|
141
|
+
});
|
|
142
|
+
continue;
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
if (markup.originalRevisionType === "paragraph-ins") {
|
|
146
|
+
decisions.push({
|
|
147
|
+
revisionId: markup.revisionId,
|
|
148
|
+
paragraphIndex,
|
|
149
|
+
rawMarkup: markup.rawXml,
|
|
150
|
+
...(revision.status === "rejected" && boundaries[paragraphIndex - 1]
|
|
151
|
+
? { mergeFromParagraphIndex: paragraphIndex - 1, mergeToParagraphIndex: paragraphIndex }
|
|
152
|
+
: {}),
|
|
153
|
+
});
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
return decisions;
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
function createParagraphStructuralReplacements(
|
|
161
|
+
documentXml: string,
|
|
162
|
+
boundaries: readonly RevisionParagraphBoundary[],
|
|
163
|
+
decisions: readonly ParagraphMarkupDecision[],
|
|
164
|
+
consumedRevisionIds: Set<string>,
|
|
165
|
+
): XmlReplacement[] {
|
|
166
|
+
const replacements: XmlReplacement[] = [];
|
|
167
|
+
const removedMarkupByParagraph = new Map<number, string[]>();
|
|
168
|
+
const mergeEdgeStarts = new Set<number>();
|
|
169
|
+
|
|
170
|
+
for (const decision of decisions) {
|
|
171
|
+
consumedRevisionIds.add(decision.revisionId);
|
|
172
|
+
const existing = removedMarkupByParagraph.get(decision.paragraphIndex) ?? [];
|
|
173
|
+
existing.push(decision.rawMarkup);
|
|
174
|
+
removedMarkupByParagraph.set(decision.paragraphIndex, existing);
|
|
175
|
+
|
|
176
|
+
if (
|
|
177
|
+
decision.mergeFromParagraphIndex !== undefined &&
|
|
178
|
+
decision.mergeToParagraphIndex !== undefined
|
|
179
|
+
) {
|
|
180
|
+
mergeEdgeStarts.add(decision.mergeFromParagraphIndex);
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
const mergedParagraphIndices = new Set<number>();
|
|
185
|
+
|
|
186
|
+
for (let paragraphIndex = 0; paragraphIndex < boundaries.length; paragraphIndex += 1) {
|
|
187
|
+
if (mergedParagraphIndices.has(paragraphIndex) || !mergeEdgeStarts.has(paragraphIndex)) {
|
|
188
|
+
continue;
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
let groupEnd = paragraphIndex;
|
|
192
|
+
while (mergeEdgeStarts.has(groupEnd)) {
|
|
193
|
+
groupEnd += 1;
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
const startBoundary = boundaries[paragraphIndex];
|
|
197
|
+
const endBoundary = boundaries[groupEnd];
|
|
198
|
+
if (!startBoundary || !endBoundary) {
|
|
199
|
+
continue;
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
let targetInnerXml = removeParagraphRevisionMarkup(
|
|
203
|
+
getParagraphInnerXml(documentXml, startBoundary),
|
|
204
|
+
removedMarkupByParagraph.get(paragraphIndex) ?? [],
|
|
205
|
+
);
|
|
206
|
+
let appendedStoryXml = "";
|
|
207
|
+
|
|
208
|
+
for (let sourceIndex = paragraphIndex + 1; sourceIndex <= groupEnd; sourceIndex += 1) {
|
|
209
|
+
const sourceBoundary = boundaries[sourceIndex];
|
|
210
|
+
if (!sourceBoundary) {
|
|
211
|
+
continue;
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
appendedStoryXml += getParagraphStoryXml(documentXml, sourceBoundary);
|
|
215
|
+
mergedParagraphIndices.add(sourceIndex);
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
replacements.push({
|
|
219
|
+
start: startBoundary.paragraphStart,
|
|
220
|
+
end: endBoundary.paragraphEnd,
|
|
221
|
+
replacement: serializeMergedParagraph(
|
|
222
|
+
documentXml,
|
|
223
|
+
startBoundary,
|
|
224
|
+
targetInnerXml,
|
|
225
|
+
appendedStoryXml,
|
|
226
|
+
),
|
|
227
|
+
});
|
|
228
|
+
mergedParagraphIndices.add(paragraphIndex);
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
for (const [paragraphIndex, rawMarkup] of removedMarkupByParagraph.entries()) {
|
|
232
|
+
if (mergedParagraphIndices.has(paragraphIndex)) {
|
|
233
|
+
continue;
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
const boundary = boundaries[paragraphIndex];
|
|
237
|
+
if (!boundary) {
|
|
238
|
+
continue;
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
replacements.push({
|
|
242
|
+
start: boundary.paragraphStart,
|
|
243
|
+
end: boundary.paragraphEnd,
|
|
244
|
+
replacement: serializeMergedParagraph(
|
|
245
|
+
documentXml,
|
|
246
|
+
boundary,
|
|
247
|
+
removeParagraphRevisionMarkup(getParagraphInnerXml(documentXml, boundary), rawMarkup),
|
|
248
|
+
"",
|
|
249
|
+
),
|
|
250
|
+
});
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
return replacements;
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
function serializeMergedParagraph(
|
|
257
|
+
documentXml: string,
|
|
258
|
+
targetBoundary: RevisionParagraphBoundary,
|
|
259
|
+
targetInnerXml: string,
|
|
260
|
+
appendedStoryXml: string,
|
|
261
|
+
): string {
|
|
262
|
+
return [
|
|
263
|
+
documentXml.slice(targetBoundary.paragraphStart, targetBoundary.paragraphStartTagEnd),
|
|
264
|
+
targetInnerXml,
|
|
265
|
+
appendedStoryXml,
|
|
266
|
+
documentXml.slice(targetBoundary.paragraphEndTagStart, targetBoundary.paragraphEnd),
|
|
267
|
+
].join("");
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
function getParagraphInnerXml(
|
|
271
|
+
documentXml: string,
|
|
272
|
+
boundary: RevisionParagraphBoundary,
|
|
273
|
+
): string {
|
|
274
|
+
return documentXml.slice(boundary.paragraphStartTagEnd, boundary.paragraphEndTagStart);
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
function getParagraphStoryXml(
|
|
278
|
+
documentXml: string,
|
|
279
|
+
boundary: RevisionParagraphBoundary,
|
|
280
|
+
): string {
|
|
281
|
+
const innerXml = getParagraphInnerXml(documentXml, boundary);
|
|
282
|
+
if (
|
|
283
|
+
boundary.paragraphPropertiesStart === undefined ||
|
|
284
|
+
boundary.paragraphPropertiesEnd === undefined
|
|
285
|
+
) {
|
|
286
|
+
return innerXml;
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
const relativeStart = boundary.paragraphPropertiesStart - boundary.paragraphStartTagEnd;
|
|
290
|
+
const relativeEnd = boundary.paragraphPropertiesEnd - boundary.paragraphStartTagEnd;
|
|
291
|
+
|
|
292
|
+
return innerXml.slice(0, relativeStart) + innerXml.slice(relativeEnd);
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
function removeParagraphRevisionMarkup(
|
|
296
|
+
innerXml: string,
|
|
297
|
+
rawMarkup: string | readonly string[],
|
|
298
|
+
): string {
|
|
299
|
+
const markups = Array.isArray(rawMarkup) ? rawMarkup : [rawMarkup];
|
|
300
|
+
return cleanupEmptyParagraphPropertyContainers(
|
|
301
|
+
markups.reduce((result, markup) => result.replace(markup, ""), innerXml),
|
|
302
|
+
);
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
function cleanupEmptyParagraphPropertyContainers(xml: string): string {
|
|
306
|
+
let next = xml;
|
|
307
|
+
let previous = "";
|
|
308
|
+
|
|
309
|
+
while (next !== previous) {
|
|
310
|
+
previous = next;
|
|
311
|
+
next = next
|
|
312
|
+
.replace(/<w:rPr>\s*<\/w:rPr>/g, "")
|
|
313
|
+
.replace(/<w:pPr>\s*<\/w:pPr>/g, "");
|
|
314
|
+
}
|
|
315
|
+
|
|
316
|
+
return next;
|
|
317
|
+
}
|
|
318
|
+
|
|
319
|
+
function applyReplacements(documentXml: string, replacements: XmlReplacement[]): string {
|
|
320
|
+
const sorted = replacements
|
|
321
|
+
.slice()
|
|
322
|
+
.sort((left, right) => right.start - left.start || right.end - left.end);
|
|
323
|
+
let output = documentXml;
|
|
324
|
+
|
|
325
|
+
for (const replacement of sorted) {
|
|
326
|
+
output =
|
|
327
|
+
output.slice(0, replacement.start) +
|
|
328
|
+
replacement.replacement +
|
|
329
|
+
output.slice(replacement.end);
|
|
330
|
+
}
|
|
331
|
+
|
|
332
|
+
return output;
|
|
333
|
+
}
|
|
334
|
+
|
|
335
|
+
function serializePropertyChangeMarkup(
|
|
336
|
+
rawXml: string,
|
|
337
|
+
status: RevisionRecord["status"],
|
|
338
|
+
): string {
|
|
339
|
+
switch (status) {
|
|
340
|
+
case "accepted":
|
|
341
|
+
return "";
|
|
342
|
+
case "rejected":
|
|
343
|
+
case "active":
|
|
344
|
+
case "detached":
|
|
345
|
+
return rawXml;
|
|
346
|
+
}
|
|
347
|
+
}
|
|
348
|
+
|
|
349
|
+
function serializeInsertionMarkup(
|
|
350
|
+
rawXml: string,
|
|
351
|
+
status: RevisionRecord["status"],
|
|
352
|
+
): string {
|
|
353
|
+
switch (status) {
|
|
354
|
+
case "accepted":
|
|
355
|
+
return unwrapRevisionContainer(rawXml);
|
|
356
|
+
case "rejected":
|
|
357
|
+
return "";
|
|
358
|
+
case "active":
|
|
359
|
+
case "detached":
|
|
360
|
+
return rawXml;
|
|
361
|
+
}
|
|
362
|
+
}
|
|
363
|
+
|
|
364
|
+
function serializeDeletionMarkup(
|
|
365
|
+
rawXml: string,
|
|
366
|
+
status: RevisionRecord["status"],
|
|
367
|
+
): string {
|
|
368
|
+
switch (status) {
|
|
369
|
+
case "accepted":
|
|
370
|
+
return "";
|
|
371
|
+
case "rejected":
|
|
372
|
+
return convertDeletedContentToRuns(unwrapRevisionContainer(rawXml));
|
|
373
|
+
case "active":
|
|
374
|
+
case "detached":
|
|
375
|
+
return rawXml;
|
|
376
|
+
}
|
|
377
|
+
}
|
|
378
|
+
|
|
379
|
+
function unwrapRevisionContainer(rawXml: string): string {
|
|
380
|
+
return rawXml
|
|
381
|
+
.replace(/^<[^>]+>/, "")
|
|
382
|
+
.replace(/<\/[^>]+>\s*$/, "");
|
|
383
|
+
}
|
|
384
|
+
|
|
385
|
+
function convertDeletedContentToRuns(xml: string): string {
|
|
386
|
+
return xml
|
|
387
|
+
.replace(/<(\/?)w:delText\b/g, "<$1w:t")
|
|
388
|
+
.replace(/<(\/?)w:delInstrText\b/g, "<$1w:instrText");
|
|
389
|
+
}
|