@beyondwork/docx-react-component 1.0.14 → 1.0.16
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/package.json +43 -24
- package/src/api/public-types.ts +15 -0
- package/src/compare/diff-engine.ts +84 -7
- package/src/compare/index.ts +25 -0
- package/src/compare/snapshot.ts +31 -0
- package/src/core/commands/formatting-commands.ts +225 -0
- package/src/formats/xlsx/io/serialize-shared-strings.ts +72 -0
- package/src/formats/xlsx/io/serialize-sheet.ts +333 -0
- package/src/formats/xlsx/io/serialize-styles.ts +98 -0
- package/src/formats/xlsx/io/serialize-workbook.ts +429 -0
- package/src/formats/xlsx/runtime/cell-commands.ts +567 -0
- package/src/formats/xlsx/runtime/sheet-commands.ts +206 -0
- package/src/formats/xlsx/runtime/workbook-runtime.ts +177 -0
- package/src/formats/xlsx/runtime/workbook-transaction.ts +822 -0
- package/src/io/ooxml/parse-main-document.ts +6 -6
- package/src/io/ooxml/parse-revisions.ts +18 -24
- package/src/legal/bookmarks.ts +35 -0
- package/src/legal/index.ts +32 -0
- package/src/legal/signature-blocks.ts +259 -0
- package/src/runtime/document-runtime.ts +43 -0
- package/src/runtime/numbering-prefix.ts +195 -0
- package/src/runtime/surface-projection.ts +292 -9
- package/src/ui/WordReviewEditor.tsx +107 -4
- package/src/ui-tailwind/editor-surface/pm-schema.ts +148 -13
- package/src/ui-tailwind/editor-surface/pm-state-from-snapshot.ts +15 -29
|
@@ -0,0 +1,195 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
NumberingCatalog,
|
|
3
|
+
NumberingLevelDefinition,
|
|
4
|
+
ParagraphNode,
|
|
5
|
+
} from "../model/canonical-document.ts";
|
|
6
|
+
|
|
7
|
+
interface NumberingSequenceState {
|
|
8
|
+
counters: Array<number | undefined>;
|
|
9
|
+
lastLevel: number | null;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export interface NumberingPrefixResolver {
|
|
13
|
+
resolve(numbering: ParagraphNode["numbering"] | undefined): string | null;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
const DEFAULT_START_AT = 1;
|
|
17
|
+
|
|
18
|
+
export function createNumberingPrefixResolver(
|
|
19
|
+
catalog: NumberingCatalog,
|
|
20
|
+
): NumberingPrefixResolver {
|
|
21
|
+
const sequenceStates = new Map<string, NumberingSequenceState>();
|
|
22
|
+
|
|
23
|
+
return {
|
|
24
|
+
resolve(numbering) {
|
|
25
|
+
if (!numbering) {
|
|
26
|
+
return null;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
const instance = catalog.instances[numbering.numberingInstanceId];
|
|
30
|
+
if (!instance) {
|
|
31
|
+
return null;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
const definition = catalog.abstractDefinitions[instance.abstractNumberingId];
|
|
35
|
+
if (!definition) {
|
|
36
|
+
return null;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
const levelDefinitions = new Map(
|
|
40
|
+
definition.levels.map((level) => [level.level, level] as const),
|
|
41
|
+
);
|
|
42
|
+
const levelDefinition = levelDefinitions.get(numbering.level);
|
|
43
|
+
if (!levelDefinition || levelDefinition.format === "none") {
|
|
44
|
+
return null;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
const sequenceState = getSequenceState(sequenceStates, numbering.numberingInstanceId);
|
|
48
|
+
advanceSequence(sequenceState, numbering.level, levelDefinitions, instance.overrides);
|
|
49
|
+
|
|
50
|
+
return renderLevelText(levelDefinition.text, sequenceState.counters, levelDefinitions);
|
|
51
|
+
},
|
|
52
|
+
};
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
function getSequenceState(
|
|
56
|
+
states: Map<string, NumberingSequenceState>,
|
|
57
|
+
numberingInstanceId: string,
|
|
58
|
+
): NumberingSequenceState {
|
|
59
|
+
const existing = states.get(numberingInstanceId);
|
|
60
|
+
if (existing) {
|
|
61
|
+
return existing;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
const created: NumberingSequenceState = {
|
|
65
|
+
counters: [],
|
|
66
|
+
lastLevel: null,
|
|
67
|
+
};
|
|
68
|
+
states.set(numberingInstanceId, created);
|
|
69
|
+
return created;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
function advanceSequence(
|
|
73
|
+
state: NumberingSequenceState,
|
|
74
|
+
currentLevel: number,
|
|
75
|
+
levelDefinitions: Map<number, NumberingLevelDefinition>,
|
|
76
|
+
overrides: NumberingCatalog["instances"][string]["overrides"],
|
|
77
|
+
): void {
|
|
78
|
+
if (state.lastLevel !== null && currentLevel <= state.lastLevel) {
|
|
79
|
+
state.counters.length = currentLevel + 1;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
const startAt = getLevelStartAt(currentLevel, levelDefinitions, overrides);
|
|
83
|
+
const currentValue = state.counters[currentLevel];
|
|
84
|
+
state.counters[currentLevel] =
|
|
85
|
+
currentValue === undefined ? startAt : currentValue + 1;
|
|
86
|
+
state.lastLevel = currentLevel;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
function getLevelStartAt(
|
|
90
|
+
level: number,
|
|
91
|
+
levelDefinitions: Map<number, NumberingLevelDefinition>,
|
|
92
|
+
overrides: NumberingCatalog["instances"][string]["overrides"],
|
|
93
|
+
): number {
|
|
94
|
+
const override = overrides.find((entry) => entry.level === level);
|
|
95
|
+
if (override?.startAt !== undefined) {
|
|
96
|
+
return override.startAt;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
return levelDefinitions.get(level)?.startAt ?? DEFAULT_START_AT;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
function renderLevelText(
|
|
103
|
+
text: string,
|
|
104
|
+
counters: Array<number | undefined>,
|
|
105
|
+
levelDefinitions: Map<number, NumberingLevelDefinition>,
|
|
106
|
+
): string | null {
|
|
107
|
+
if (!text) {
|
|
108
|
+
return null;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
if (!text.includes("%")) {
|
|
112
|
+
return text;
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
const rendered = text.replace(/%([1-9])/g, (_match, token) => {
|
|
116
|
+
const level = Number.parseInt(token, 10) - 1;
|
|
117
|
+
const counter = counters[level];
|
|
118
|
+
if (counter === undefined) {
|
|
119
|
+
return "";
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
const format = levelDefinitions.get(level)?.format ?? "decimal";
|
|
123
|
+
return formatCounter(counter, format);
|
|
124
|
+
});
|
|
125
|
+
|
|
126
|
+
return rendered.trim().length > 0 ? rendered : null;
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
function formatCounter(value: number, format: string): string {
|
|
130
|
+
switch (format) {
|
|
131
|
+
case "decimal":
|
|
132
|
+
return String(value);
|
|
133
|
+
case "decimalZero":
|
|
134
|
+
return String(value).padStart(2, "0");
|
|
135
|
+
case "upperLetter":
|
|
136
|
+
return toAlphabetic(value).toUpperCase();
|
|
137
|
+
case "lowerLetter":
|
|
138
|
+
return toAlphabetic(value).toLowerCase();
|
|
139
|
+
case "upperRoman":
|
|
140
|
+
return toRoman(value).toUpperCase();
|
|
141
|
+
case "lowerRoman":
|
|
142
|
+
return toRoman(value).toLowerCase();
|
|
143
|
+
case "none":
|
|
144
|
+
return "";
|
|
145
|
+
default:
|
|
146
|
+
return String(value);
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
function toAlphabetic(value: number): string {
|
|
151
|
+
if (value <= 0) {
|
|
152
|
+
return String(value);
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
let remainder = value;
|
|
156
|
+
let result = "";
|
|
157
|
+
while (remainder > 0) {
|
|
158
|
+
remainder -= 1;
|
|
159
|
+
result = String.fromCharCode(65 + (remainder % 26)) + result;
|
|
160
|
+
remainder = Math.floor(remainder / 26);
|
|
161
|
+
}
|
|
162
|
+
return result;
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
function toRoman(value: number): string {
|
|
166
|
+
if (value <= 0 || value >= 4000) {
|
|
167
|
+
return String(value);
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
const numerals: Array<[number, string]> = [
|
|
171
|
+
[1000, "M"],
|
|
172
|
+
[900, "CM"],
|
|
173
|
+
[500, "D"],
|
|
174
|
+
[400, "CD"],
|
|
175
|
+
[100, "C"],
|
|
176
|
+
[90, "XC"],
|
|
177
|
+
[50, "L"],
|
|
178
|
+
[40, "XL"],
|
|
179
|
+
[10, "X"],
|
|
180
|
+
[9, "IX"],
|
|
181
|
+
[5, "V"],
|
|
182
|
+
[4, "IV"],
|
|
183
|
+
[1, "I"],
|
|
184
|
+
];
|
|
185
|
+
|
|
186
|
+
let remaining = value;
|
|
187
|
+
let result = "";
|
|
188
|
+
for (const [amount, numeral] of numerals) {
|
|
189
|
+
while (remaining >= amount) {
|
|
190
|
+
remaining -= amount;
|
|
191
|
+
result += numeral;
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
return result;
|
|
195
|
+
}
|
|
@@ -29,6 +29,10 @@ import {
|
|
|
29
29
|
describeOpaqueFragment,
|
|
30
30
|
getOpaqueFragment,
|
|
31
31
|
} from "../preservation/store.ts";
|
|
32
|
+
import {
|
|
33
|
+
createNumberingPrefixResolver,
|
|
34
|
+
type NumberingPrefixResolver,
|
|
35
|
+
} from "./numbering-prefix.ts";
|
|
32
36
|
|
|
33
37
|
interface ParagraphAccumulator {
|
|
34
38
|
blockId: string;
|
|
@@ -37,6 +41,7 @@ interface ParagraphAccumulator {
|
|
|
37
41
|
to: number;
|
|
38
42
|
styleId?: string;
|
|
39
43
|
numbering?: ParagraphNode["numbering"];
|
|
44
|
+
numberingPrefix?: string;
|
|
40
45
|
segments: SurfaceInlineSegment[];
|
|
41
46
|
}
|
|
42
47
|
|
|
@@ -47,6 +52,7 @@ export function createEditorSurfaceSnapshot(
|
|
|
47
52
|
const root = normalizeDocumentRoot(document.content);
|
|
48
53
|
const blocks: SurfaceBlockSnapshot[] = [];
|
|
49
54
|
const lockedFragmentIds: string[] = [];
|
|
55
|
+
const numberingPrefixResolver = createNumberingPrefixResolver(document.numbering);
|
|
50
56
|
let cursor = 0;
|
|
51
57
|
const counters = {
|
|
52
58
|
paragraph: 0,
|
|
@@ -58,7 +64,13 @@ export function createEditorSurfaceSnapshot(
|
|
|
58
64
|
};
|
|
59
65
|
|
|
60
66
|
for (let index = 0; index < root.children.length; index += 1) {
|
|
61
|
-
const surfaceBlock = createSurfaceBlock(
|
|
67
|
+
const surfaceBlock = createSurfaceBlock(
|
|
68
|
+
root.children[index],
|
|
69
|
+
document,
|
|
70
|
+
cursor,
|
|
71
|
+
counters,
|
|
72
|
+
numberingPrefixResolver,
|
|
73
|
+
);
|
|
62
74
|
blocks.push(surfaceBlock.block);
|
|
63
75
|
lockedFragmentIds.push(...surfaceBlock.lockedFragmentIds);
|
|
64
76
|
cursor = surfaceBlock.nextCursor;
|
|
@@ -67,6 +79,8 @@ export function createEditorSurfaceSnapshot(
|
|
|
67
79
|
}
|
|
68
80
|
}
|
|
69
81
|
|
|
82
|
+
blocks.push(...createSecondaryStoryPreviewBlocks(document, cursor));
|
|
83
|
+
|
|
70
84
|
return {
|
|
71
85
|
storySize: cursor,
|
|
72
86
|
plainText: createPlainText(blocks),
|
|
@@ -87,6 +101,7 @@ function createSurfaceBlock(
|
|
|
87
101
|
customXml: number;
|
|
88
102
|
altChunk: number;
|
|
89
103
|
},
|
|
104
|
+
numberingPrefixResolver: NumberingPrefixResolver,
|
|
90
105
|
): { block: SurfaceBlockSnapshot; lockedFragmentIds: string[]; nextCursor: number } {
|
|
91
106
|
if (block.type === "opaque_block") {
|
|
92
107
|
const fragment = getOpaqueFragment(document.preservation as never, block.fragmentId);
|
|
@@ -115,13 +130,27 @@ function createSurfaceBlock(
|
|
|
115
130
|
if (block.type === "table") {
|
|
116
131
|
const tableIndex = counters.table;
|
|
117
132
|
counters.table += 1;
|
|
118
|
-
return createTableBlock(
|
|
133
|
+
return createTableBlock(
|
|
134
|
+
tableIndex,
|
|
135
|
+
block,
|
|
136
|
+
document,
|
|
137
|
+
cursor,
|
|
138
|
+
counters,
|
|
139
|
+
numberingPrefixResolver,
|
|
140
|
+
);
|
|
119
141
|
}
|
|
120
142
|
|
|
121
143
|
if (block.type === "sdt") {
|
|
122
144
|
const sdtIndex = counters.sdt;
|
|
123
145
|
counters.sdt += 1;
|
|
124
|
-
return createSdtBlock(
|
|
146
|
+
return createSdtBlock(
|
|
147
|
+
sdtIndex,
|
|
148
|
+
block,
|
|
149
|
+
document,
|
|
150
|
+
cursor,
|
|
151
|
+
counters,
|
|
152
|
+
numberingPrefixResolver,
|
|
153
|
+
);
|
|
125
154
|
}
|
|
126
155
|
|
|
127
156
|
if (block.type === "custom_xml") {
|
|
@@ -189,7 +218,13 @@ function createSurfaceBlock(
|
|
|
189
218
|
|
|
190
219
|
const paragraphIndex = counters.paragraph;
|
|
191
220
|
counters.paragraph += 1;
|
|
192
|
-
return createParagraphBlock(
|
|
221
|
+
return createParagraphBlock(
|
|
222
|
+
paragraphIndex,
|
|
223
|
+
block,
|
|
224
|
+
document,
|
|
225
|
+
cursor,
|
|
226
|
+
numberingPrefixResolver,
|
|
227
|
+
);
|
|
193
228
|
}
|
|
194
229
|
|
|
195
230
|
function createTableBlock(
|
|
@@ -205,6 +240,7 @@ function createTableBlock(
|
|
|
205
240
|
customXml: number;
|
|
206
241
|
altChunk: number;
|
|
207
242
|
},
|
|
243
|
+
numberingPrefixResolver: NumberingPrefixResolver,
|
|
208
244
|
): { block: SurfaceBlockSnapshot; lockedFragmentIds: string[]; nextCursor: number } {
|
|
209
245
|
const lockedFragmentIds: string[] = [];
|
|
210
246
|
let innerCursor = cursor;
|
|
@@ -216,7 +252,13 @@ function createTableBlock(
|
|
|
216
252
|
for (const [cellIndex, cell] of row.cells.entries()) {
|
|
217
253
|
const cellContent: SurfaceBlockSnapshot[] = [];
|
|
218
254
|
for (const child of cell.children) {
|
|
219
|
-
const result = createSurfaceBlock(
|
|
255
|
+
const result = createSurfaceBlock(
|
|
256
|
+
child,
|
|
257
|
+
document,
|
|
258
|
+
innerCursor,
|
|
259
|
+
counters,
|
|
260
|
+
numberingPrefixResolver,
|
|
261
|
+
);
|
|
220
262
|
cellContent.push(result.block);
|
|
221
263
|
lockedFragmentIds.push(...result.lockedFragmentIds);
|
|
222
264
|
innerCursor = result.nextCursor;
|
|
@@ -310,13 +352,20 @@ function createSdtBlock(
|
|
|
310
352
|
customXml: number;
|
|
311
353
|
altChunk: number;
|
|
312
354
|
},
|
|
355
|
+
numberingPrefixResolver: NumberingPrefixResolver,
|
|
313
356
|
): { block: SurfaceBlockSnapshot; lockedFragmentIds: string[]; nextCursor: number } {
|
|
314
357
|
const children: SurfaceBlockSnapshot[] = [];
|
|
315
358
|
const lockedFragmentIds: string[] = [];
|
|
316
359
|
let innerCursor = cursor;
|
|
317
360
|
|
|
318
361
|
for (const child of block.children) {
|
|
319
|
-
const result = createSurfaceBlock(
|
|
362
|
+
const result = createSurfaceBlock(
|
|
363
|
+
child,
|
|
364
|
+
document,
|
|
365
|
+
innerCursor,
|
|
366
|
+
counters,
|
|
367
|
+
numberingPrefixResolver,
|
|
368
|
+
);
|
|
320
369
|
children.push(result.block);
|
|
321
370
|
lockedFragmentIds.push(...result.lockedFragmentIds);
|
|
322
371
|
innerCursor = result.nextCursor;
|
|
@@ -344,6 +393,7 @@ function createParagraphBlock(
|
|
|
344
393
|
paragraph: ParagraphNode,
|
|
345
394
|
document: CanonicalDocumentEnvelope,
|
|
346
395
|
start: number,
|
|
396
|
+
numberingPrefixResolver: NumberingPrefixResolver,
|
|
347
397
|
): {
|
|
348
398
|
block: SurfaceBlockSnapshot;
|
|
349
399
|
nextCursor: number;
|
|
@@ -356,12 +406,20 @@ function createParagraphBlock(
|
|
|
356
406
|
to: start,
|
|
357
407
|
...(paragraph.styleId ? { styleId: paragraph.styleId } : {}),
|
|
358
408
|
...(paragraph.numbering ? { numbering: paragraph.numbering } : {}),
|
|
409
|
+
...(paragraph.numbering
|
|
410
|
+
? {
|
|
411
|
+
numberingPrefix:
|
|
412
|
+
numberingPrefixResolver.resolve(paragraph.numbering) ?? undefined,
|
|
413
|
+
}
|
|
414
|
+
: {}),
|
|
359
415
|
...(paragraph.alignment ? { alignment: paragraph.alignment } : {}),
|
|
360
416
|
...(paragraph.spacing ? { spacing: paragraph.spacing } : {}),
|
|
361
417
|
...(paragraph.indentation ? { indentation: paragraph.indentation } : {}),
|
|
362
418
|
...(paragraph.borders ? { borders: paragraph.borders } : {}),
|
|
363
419
|
...(paragraph.shading ? { shading: paragraph.shading } : {}),
|
|
364
|
-
...(paragraph.tabStops && paragraph.tabStops.length > 0
|
|
420
|
+
...(paragraph.tabStops && paragraph.tabStops.length > 0
|
|
421
|
+
? { tabStops: paragraph.tabStops.map((tabStop) => toSurfaceTabStop(tabStop)) }
|
|
422
|
+
: {}),
|
|
365
423
|
...(paragraph.keepNext ? { keepNext: true } : {}),
|
|
366
424
|
...(paragraph.keepLines ? { keepLines: true } : {}),
|
|
367
425
|
...(paragraph.pageBreakBefore ? { pageBreakBefore: true } : {}),
|
|
@@ -459,6 +517,7 @@ function appendInlineSegments(
|
|
|
459
517
|
case "opaque_inline": {
|
|
460
518
|
const fragment = getOpaqueFragment(document.preservation as never, node.fragmentId);
|
|
461
519
|
const descriptor = fragment ? describeOpaqueFragment(fragment) : null;
|
|
520
|
+
const preview = fragment ? describePreservedInlinePreview(fragment.payloadReference) : null;
|
|
462
521
|
paragraph.segments.push({
|
|
463
522
|
segmentId: `${paragraph.blockId}-segment-${paragraph.segments.length}`,
|
|
464
523
|
kind: "opaque_inline",
|
|
@@ -466,8 +525,9 @@ function appendInlineSegments(
|
|
|
466
525
|
to: start + 1,
|
|
467
526
|
fragmentId: node.fragmentId,
|
|
468
527
|
warningId: node.warningId,
|
|
469
|
-
label: descriptor?.label ?? "Unsupported inline OOXML",
|
|
528
|
+
label: preview?.label ?? descriptor?.label ?? "Unsupported inline OOXML",
|
|
470
529
|
detail:
|
|
530
|
+
preview?.detail ??
|
|
471
531
|
descriptor?.detail ??
|
|
472
532
|
"Locked whole-unit to keep unsupported inline OOXML intact through export.",
|
|
473
533
|
state: "locked-preserve-only",
|
|
@@ -612,12 +672,19 @@ function createPlainText(
|
|
|
612
672
|
const text: string[] = [];
|
|
613
673
|
for (const block of blocks) {
|
|
614
674
|
if (block.kind === "opaque_block") {
|
|
675
|
+
if (block.fragmentId.startsWith("preview:")) {
|
|
676
|
+
continue;
|
|
677
|
+
}
|
|
615
678
|
text.push("\uFFFA");
|
|
616
679
|
continue;
|
|
617
680
|
}
|
|
618
681
|
|
|
619
682
|
if (block.kind === "table") {
|
|
620
|
-
|
|
683
|
+
for (const row of block.rows) {
|
|
684
|
+
for (const cell of row.cells) {
|
|
685
|
+
text.push(createPlainText(cell.content));
|
|
686
|
+
}
|
|
687
|
+
}
|
|
621
688
|
continue;
|
|
622
689
|
}
|
|
623
690
|
|
|
@@ -650,6 +717,222 @@ function createPlainText(
|
|
|
650
717
|
return text.join("");
|
|
651
718
|
}
|
|
652
719
|
|
|
720
|
+
function createSecondaryStoryPreviewBlocks(
|
|
721
|
+
document: CanonicalDocumentEnvelope,
|
|
722
|
+
cursor: number,
|
|
723
|
+
): SurfaceBlockSnapshot[] {
|
|
724
|
+
const previews: SurfaceBlockSnapshot[] = [];
|
|
725
|
+
const subParts = document.subParts;
|
|
726
|
+
if (!subParts) {
|
|
727
|
+
return previews;
|
|
728
|
+
}
|
|
729
|
+
|
|
730
|
+
for (const header of subParts.headers ?? []) {
|
|
731
|
+
previews.push(
|
|
732
|
+
createSecondaryStoryPreviewBlock(
|
|
733
|
+
`Header · ${header.variant}`,
|
|
734
|
+
`header:${header.relationshipId}`,
|
|
735
|
+
cursor,
|
|
736
|
+
createSecondaryStoryPreviewDetail(header.partPath, header.blocks),
|
|
737
|
+
),
|
|
738
|
+
);
|
|
739
|
+
}
|
|
740
|
+
|
|
741
|
+
for (const footer of subParts.footers ?? []) {
|
|
742
|
+
previews.push(
|
|
743
|
+
createSecondaryStoryPreviewBlock(
|
|
744
|
+
`Footer · ${footer.variant}`,
|
|
745
|
+
`footer:${footer.relationshipId}`,
|
|
746
|
+
cursor,
|
|
747
|
+
createSecondaryStoryPreviewDetail(footer.partPath, footer.blocks),
|
|
748
|
+
),
|
|
749
|
+
);
|
|
750
|
+
}
|
|
751
|
+
|
|
752
|
+
const footnotes = Object.values(subParts.footnoteCollection?.footnotes ?? {}).sort(compareNoteIds);
|
|
753
|
+
for (const note of footnotes) {
|
|
754
|
+
previews.push(
|
|
755
|
+
createSecondaryStoryPreviewBlock(
|
|
756
|
+
`Footnote ${note.noteId}`,
|
|
757
|
+
`footnote:${note.noteId}`,
|
|
758
|
+
cursor,
|
|
759
|
+
createSecondaryStoryPreviewDetail(`/word/footnotes.xml#${note.noteId}`, note.blocks),
|
|
760
|
+
),
|
|
761
|
+
);
|
|
762
|
+
}
|
|
763
|
+
|
|
764
|
+
const endnotes = Object.values(subParts.footnoteCollection?.endnotes ?? {}).sort(compareNoteIds);
|
|
765
|
+
for (const note of endnotes) {
|
|
766
|
+
previews.push(
|
|
767
|
+
createSecondaryStoryPreviewBlock(
|
|
768
|
+
`Endnote ${note.noteId}`,
|
|
769
|
+
`endnote:${note.noteId}`,
|
|
770
|
+
cursor,
|
|
771
|
+
createSecondaryStoryPreviewDetail(`/word/endnotes.xml#${note.noteId}`, note.blocks),
|
|
772
|
+
),
|
|
773
|
+
);
|
|
774
|
+
}
|
|
775
|
+
|
|
776
|
+
return previews;
|
|
777
|
+
}
|
|
778
|
+
|
|
779
|
+
function createSecondaryStoryPreviewBlock(
|
|
780
|
+
label: string,
|
|
781
|
+
previewId: string,
|
|
782
|
+
cursor: number,
|
|
783
|
+
detail: string,
|
|
784
|
+
): Extract<SurfaceBlockSnapshot, { kind: "opaque_block" }> {
|
|
785
|
+
return {
|
|
786
|
+
blockId: `secondary-story-${previewId.replace(/[^a-z0-9._:-]+/gi, "-")}`,
|
|
787
|
+
kind: "opaque_block",
|
|
788
|
+
from: cursor,
|
|
789
|
+
to: cursor,
|
|
790
|
+
fragmentId: `preview:${previewId}`,
|
|
791
|
+
warningId: `preview:${previewId}`,
|
|
792
|
+
label,
|
|
793
|
+
detail,
|
|
794
|
+
state: "locked-preserve-only",
|
|
795
|
+
};
|
|
796
|
+
}
|
|
797
|
+
|
|
798
|
+
function createSecondaryStoryPreviewDetail(
|
|
799
|
+
sourceLabel: string,
|
|
800
|
+
blocks: readonly BlockNode[],
|
|
801
|
+
): string {
|
|
802
|
+
const previewLines = blocks
|
|
803
|
+
.map((block) => summarizePreviewBlock(block))
|
|
804
|
+
.filter((line) => line.trim().length > 0);
|
|
805
|
+
const previewText = previewLines.length > 0 ? previewLines.join("\n\n") : "Empty story.";
|
|
806
|
+
return `Read-only preview from ${sourceLabel}.\n${previewText}`;
|
|
807
|
+
}
|
|
808
|
+
|
|
809
|
+
function summarizePreviewBlock(block: BlockNode): string {
|
|
810
|
+
switch (block.type) {
|
|
811
|
+
case "paragraph": {
|
|
812
|
+
const text = block.children.map((child) => summarizePreviewInline(child)).join("");
|
|
813
|
+
return text.trim().length > 0 ? text : "¶";
|
|
814
|
+
}
|
|
815
|
+
case "table":
|
|
816
|
+
return "[Table preserved in secondary story]";
|
|
817
|
+
case "opaque_block":
|
|
818
|
+
return "[Locked block preserved for export]";
|
|
819
|
+
case "sdt":
|
|
820
|
+
return "[Content control preview]";
|
|
821
|
+
case "custom_xml":
|
|
822
|
+
return "[Custom XML preview]";
|
|
823
|
+
case "alt_chunk":
|
|
824
|
+
return "[AltChunk preview]";
|
|
825
|
+
case "section_break":
|
|
826
|
+
return "[Section boundary]";
|
|
827
|
+
}
|
|
828
|
+
}
|
|
829
|
+
|
|
830
|
+
function summarizePreviewInline(node: InlineNode): string {
|
|
831
|
+
switch (node.type) {
|
|
832
|
+
case "text":
|
|
833
|
+
return node.text;
|
|
834
|
+
case "tab":
|
|
835
|
+
return "\t";
|
|
836
|
+
case "hard_break":
|
|
837
|
+
return "\n";
|
|
838
|
+
case "hyperlink":
|
|
839
|
+
return node.children.map((child) => summarizePreviewInline(child)).join("");
|
|
840
|
+
case "footnote_ref":
|
|
841
|
+
return node.noteKind === "footnote" ? `[fn ${node.noteId}]` : `[en ${node.noteId}]`;
|
|
842
|
+
case "field": {
|
|
843
|
+
const instruction = node.instruction.trim();
|
|
844
|
+
return instruction.length > 0 ? `[Field: ${instruction}]` : "[Field]";
|
|
845
|
+
}
|
|
846
|
+
case "bookmark_start":
|
|
847
|
+
return node.name ? `[Bookmark: ${node.name}]` : "[Bookmark]";
|
|
848
|
+
case "bookmark_end":
|
|
849
|
+
return "";
|
|
850
|
+
case "image":
|
|
851
|
+
return node.altText ? `[Image: ${node.altText}]` : "[Image]";
|
|
852
|
+
case "opaque_inline":
|
|
853
|
+
return "[Locked inline content]";
|
|
854
|
+
case "symbol":
|
|
855
|
+
return node.char ? String.fromCodePoint(parseInt(node.char, 16)) : "\uFFFD";
|
|
856
|
+
case "column_break":
|
|
857
|
+
return "[Column break]";
|
|
858
|
+
case "chart_preview":
|
|
859
|
+
return "[Chart]";
|
|
860
|
+
case "smartart_preview":
|
|
861
|
+
return "[SmartArt]";
|
|
862
|
+
case "shape":
|
|
863
|
+
return node.text ? `[Shape: ${node.text}]` : "[Shape]";
|
|
864
|
+
case "wordart":
|
|
865
|
+
return node.text ? `[WordArt: ${node.text}]` : "[WordArt]";
|
|
866
|
+
case "vml_shape":
|
|
867
|
+
return node.text ? `[VML: ${node.text}]` : "[VML shape]";
|
|
868
|
+
}
|
|
869
|
+
}
|
|
870
|
+
|
|
871
|
+
function compareNoteIds(
|
|
872
|
+
left: { noteId: string },
|
|
873
|
+
right: { noteId: string },
|
|
874
|
+
): number {
|
|
875
|
+
return Number.parseInt(left.noteId, 10) - Number.parseInt(right.noteId, 10);
|
|
876
|
+
}
|
|
877
|
+
|
|
878
|
+
function toSurfaceTabStop(
|
|
879
|
+
tabStop: NonNullable<ParagraphNode["tabStops"]>[number],
|
|
880
|
+
): { pos: number; val?: string; leader?: string } {
|
|
881
|
+
return {
|
|
882
|
+
pos: tabStop.position,
|
|
883
|
+
...(tabStop.align ? { val: tabStop.align } : {}),
|
|
884
|
+
...(tabStop.leader ? { leader: tabStop.leader } : {}),
|
|
885
|
+
};
|
|
886
|
+
}
|
|
887
|
+
|
|
888
|
+
function describePreservedInlinePreview(
|
|
889
|
+
payloadReference: string,
|
|
890
|
+
): { label: string; detail: string } | null {
|
|
891
|
+
if (/\b(?:w:)?bookmarkStart\b/u.test(payloadReference)) {
|
|
892
|
+
const name = /\bw:name="([^"]+)"/u.exec(payloadReference)?.[1];
|
|
893
|
+
return {
|
|
894
|
+
label: name ? `Bookmark · ${name}` : "Bookmark",
|
|
895
|
+
detail: name
|
|
896
|
+
? `Bookmark anchor \"${name}\" is preserved as a read-only inline token for export safety.`
|
|
897
|
+
: "Bookmark anchor is preserved as a read-only inline token for export safety.",
|
|
898
|
+
};
|
|
899
|
+
}
|
|
900
|
+
|
|
901
|
+
if (/\b(?:w:)?bookmarkEnd\b/u.test(payloadReference)) {
|
|
902
|
+
return {
|
|
903
|
+
label: "Bookmark end",
|
|
904
|
+
detail: "Bookmark end marker is preserved as a read-only inline token for export safety.",
|
|
905
|
+
};
|
|
906
|
+
}
|
|
907
|
+
|
|
908
|
+
if (/\b(?:w:)?fldSimple\b|\b(?:w:)?fldChar\b|\b(?:w:)?instrText\b/u.test(payloadReference)) {
|
|
909
|
+
const simpleInstruction = /\bw:instr="([^"]*)"/u.exec(payloadReference)?.[1];
|
|
910
|
+
const complexInstruction = [...payloadReference.matchAll(/<(?:\w+:)?instrText\b[^>]*>([\s\S]*?)<\/(?:\w+:)?instrText>/gu)]
|
|
911
|
+
.map((match) => decodeXmlEntities(match[1] ?? ""))
|
|
912
|
+
.join("")
|
|
913
|
+
.trim();
|
|
914
|
+
const instruction = (simpleInstruction ?? complexInstruction ?? "").trim();
|
|
915
|
+
return {
|
|
916
|
+
label: "Field",
|
|
917
|
+
detail:
|
|
918
|
+
instruction.length > 0
|
|
919
|
+
? `Read-only field preserved for export safety. Instruction: ${instruction}.`
|
|
920
|
+
: "Read-only field preserved for export safety.",
|
|
921
|
+
};
|
|
922
|
+
}
|
|
923
|
+
|
|
924
|
+
return null;
|
|
925
|
+
}
|
|
926
|
+
|
|
927
|
+
function decodeXmlEntities(text: string): string {
|
|
928
|
+
return text
|
|
929
|
+
.replace(/</g, "<")
|
|
930
|
+
.replace(/>/g, ">")
|
|
931
|
+
.replace(/"/g, "\"")
|
|
932
|
+
.replace(/'/g, "'")
|
|
933
|
+
.replace(/&/g, "&");
|
|
934
|
+
}
|
|
935
|
+
|
|
653
936
|
function cloneMarks(marks: TextMark[]): {
|
|
654
937
|
marks: SurfaceTextMark[];
|
|
655
938
|
markAttrs?: {
|