@beyondwork/docx-react-component 1.0.17 → 1.0.19

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.
Files changed (74) hide show
  1. package/README.md +8 -2
  2. package/package.json +32 -34
  3. package/src/api/README.md +5 -1
  4. package/src/api/public-types.ts +374 -4
  5. package/src/api/session-state.ts +58 -0
  6. package/src/core/commands/formatting-commands.ts +1 -0
  7. package/src/core/commands/image-commands.ts +147 -0
  8. package/src/core/commands/index.ts +5 -1
  9. package/src/core/commands/list-commands.ts +231 -36
  10. package/src/core/commands/paragraph-layout-commands.ts +339 -0
  11. package/src/core/commands/section-layout-commands.ts +680 -0
  12. package/src/core/commands/style-commands.ts +262 -0
  13. package/src/core/search/search-text.ts +329 -0
  14. package/src/core/selection/mapping.ts +41 -0
  15. package/src/core/state/editor-state.ts +1 -1
  16. package/src/index.ts +30 -0
  17. package/src/io/docx-session.ts +260 -39
  18. package/src/io/export/serialize-main-document.ts +202 -5
  19. package/src/io/export/serialize-numbering.ts +28 -7
  20. package/src/io/normalize/normalize-text.ts +63 -25
  21. package/src/io/ooxml/numbering-sentinels.ts +44 -0
  22. package/src/io/ooxml/parse-footnotes.ts +212 -20
  23. package/src/io/ooxml/parse-headers-footers.ts +229 -25
  24. package/src/io/ooxml/parse-inline-media.ts +16 -0
  25. package/src/io/ooxml/parse-main-document.ts +411 -6
  26. package/src/io/ooxml/parse-numbering.ts +7 -0
  27. package/src/io/ooxml/parse-settings.ts +184 -0
  28. package/src/io/ooxml/parse-shapes.ts +25 -0
  29. package/src/io/ooxml/parse-styles.ts +463 -0
  30. package/src/io/ooxml/parse-theme.ts +32 -0
  31. package/src/model/canonical-document.ts +133 -3
  32. package/src/model/cds-1.0.0.ts +13 -0
  33. package/src/model/snapshot.ts +2 -1
  34. package/src/runtime/document-layout.ts +332 -0
  35. package/src/runtime/document-navigation.ts +564 -0
  36. package/src/runtime/document-runtime.ts +265 -35
  37. package/src/runtime/document-search.ts +145 -0
  38. package/src/runtime/numbering-prefix.ts +47 -26
  39. package/src/runtime/page-layout-estimation.ts +212 -0
  40. package/src/runtime/read-only-diagnostics-runtime.ts +1 -0
  41. package/src/runtime/session-capabilities.ts +2 -0
  42. package/src/runtime/story-context.ts +164 -0
  43. package/src/runtime/story-targeting.ts +162 -0
  44. package/src/runtime/surface-projection.ts +239 -12
  45. package/src/runtime/table-schema.ts +87 -5
  46. package/src/runtime/view-state.ts +459 -0
  47. package/src/ui/WordReviewEditor.tsx +1902 -312
  48. package/src/ui/browser-export.ts +52 -0
  49. package/src/ui/headless/preserve-editor-selection.ts +5 -0
  50. package/src/ui/headless/selection-helpers.ts +20 -0
  51. package/src/ui/headless/selection-toolbar-model.ts +22 -0
  52. package/src/ui/headless/use-editor-keyboard.ts +6 -1
  53. package/src/ui-tailwind/chrome/tw-page-ruler.tsx +386 -0
  54. package/src/ui-tailwind/chrome/tw-selection-toolbar.tsx +125 -14
  55. package/src/ui-tailwind/editor-surface/perf-probe.ts +107 -0
  56. package/src/ui-tailwind/editor-surface/pm-command-bridge.ts +45 -6
  57. package/src/ui-tailwind/editor-surface/pm-contextual-ui.ts +31 -0
  58. package/src/ui-tailwind/editor-surface/pm-position-map.ts +2 -2
  59. package/src/ui-tailwind/editor-surface/pm-schema.ts +47 -5
  60. package/src/ui-tailwind/editor-surface/pm-state-from-snapshot.ts +95 -22
  61. package/src/ui-tailwind/editor-surface/search-plugin.ts +19 -68
  62. package/src/ui-tailwind/editor-surface/tw-inline-token.tsx +11 -0
  63. package/src/ui-tailwind/editor-surface/tw-prosemirror-surface.tsx +394 -77
  64. package/src/ui-tailwind/editor-surface/tw-table-node-view.tsx +0 -1
  65. package/src/ui-tailwind/index.ts +2 -1
  66. package/src/ui-tailwind/review/tw-comment-sidebar.tsx +277 -147
  67. package/src/ui-tailwind/review/tw-review-rail.tsx +6 -6
  68. package/src/ui-tailwind/theme/editor-theme.css +123 -0
  69. package/src/ui-tailwind/toolbar/tw-toolbar-icon-button.tsx +4 -0
  70. package/src/ui-tailwind/toolbar/tw-toolbar.tsx +291 -12
  71. package/src/ui-tailwind/tw-review-workspace.tsx +926 -27
  72. package/src/validation/compatibility-engine.ts +92 -20
  73. package/src/validation/diagnostics.ts +1 -0
  74. package/src/validation/docx-comment-proof.ts +487 -0
@@ -0,0 +1,680 @@
1
+ /**
2
+ * Section layout authoring commands.
3
+ *
4
+ * Supports inserting and deleting section breaks, bounded section layout
5
+ * updates (page size, margins, gutter, orientation, columns, first-page /
6
+ * odd-even variants, page-numbering restart / format), and section break
7
+ * deletion with following-section merge semantics.
8
+ *
9
+ * All mutations are command-backed and export-safe.
10
+ */
11
+ import type {
12
+ RuntimeRenderSnapshot,
13
+ SelectionSnapshot,
14
+ } from "../../api/public-types";
15
+ import type {
16
+ CanonicalDocument as CanonicalDocumentEnvelope,
17
+ ColumnProperties,
18
+ PageMargins,
19
+ PageNumbering,
20
+ PageSize,
21
+ SectionBreakNode,
22
+ SectionProperties,
23
+ } from "../../model/canonical-document.ts";
24
+
25
+ // ---- Public types ----
26
+
27
+ export interface SectionLayoutCommandContext {
28
+ timestamp: string;
29
+ }
30
+
31
+ export interface SectionLayoutMutationResult {
32
+ document: CanonicalDocumentEnvelope;
33
+ selection: SelectionSnapshot;
34
+ changed: boolean;
35
+ }
36
+
37
+ export type SectionBreakType =
38
+ | "nextPage"
39
+ | "continuous"
40
+ | "evenPage"
41
+ | "oddPage"
42
+ | "nextColumn";
43
+
44
+ export interface SectionLayoutPatch {
45
+ pageSize?: Partial<PageSize>;
46
+ pageMargins?: Partial<PageMargins>;
47
+ columns?: Partial<ColumnProperties>;
48
+ pageNumbering?: Partial<PageNumbering>;
49
+ titlePage?: boolean;
50
+ sectionType?: SectionBreakType;
51
+ }
52
+
53
+ export interface SectionLinkPatch {
54
+ kind: "header" | "footer";
55
+ variant: "default" | "first" | "even";
56
+ linkToPrevious: boolean;
57
+ relationshipId?: string | null;
58
+ }
59
+
60
+ // ---- Insert section break ----
61
+
62
+ /**
63
+ * Insert a section break at the current selection position.
64
+ * The new section inherits properties from the section that contained the
65
+ * insertion point (the *following* section keeps its own properties).
66
+ */
67
+ export function insertSectionBreak(
68
+ document: CanonicalDocumentEnvelope,
69
+ snapshot: RuntimeRenderSnapshot,
70
+ breakType: SectionBreakType,
71
+ _context: SectionLayoutCommandContext,
72
+ ): SectionLayoutMutationResult {
73
+ const cloned = structuredClone(document) as CanonicalDocumentEnvelope;
74
+ const surface = snapshot.surface;
75
+
76
+ if (!surface) {
77
+ return noopResult(document, snapshot.selection);
78
+ }
79
+
80
+ const position = snapshot.selection.head;
81
+ const blockIndex = resolveBlockIndexForPosition(surface.blocks, position);
82
+
83
+ if (blockIndex < 0) {
84
+ return noopResult(document, snapshot.selection);
85
+ }
86
+
87
+ const sectionTarget = resolveSectionTarget(cloned, surface.blocks, position);
88
+ const inheritedProperties = cloneSectionProperties(sectionTarget?.properties);
89
+ const sectionBreak: SectionBreakNode = {
90
+ type: "section_break",
91
+ sectionProperties: {
92
+ ...inheritedProperties,
93
+ sectionType: breakType,
94
+ },
95
+ };
96
+
97
+ cloned.content.children.splice(blockIndex + 1, 0, sectionBreak);
98
+ cloned.updatedAt = _context.timestamp;
99
+
100
+ return {
101
+ document: cloned,
102
+ selection: snapshot.selection,
103
+ changed: true,
104
+ };
105
+ }
106
+
107
+ export function insertSectionBreakAfterSectionIndex(
108
+ document: CanonicalDocumentEnvelope,
109
+ sectionIndex: number,
110
+ breakType: SectionBreakType,
111
+ context: SectionLayoutCommandContext,
112
+ ): SectionLayoutMutationResult {
113
+ const cloned = structuredClone(document) as CanonicalDocumentEnvelope;
114
+ const insertionIndex = findSectionInsertionIndex(cloned, sectionIndex);
115
+ if (insertionIndex < 0) {
116
+ return noopResult(document, createSelectionSnapshot(0, 0));
117
+ }
118
+
119
+ const inheritedProperties = cloneSectionProperties(
120
+ getSectionPropertiesAtIndex(cloned, sectionIndex),
121
+ );
122
+ const sectionBreak: SectionBreakNode = {
123
+ type: "section_break",
124
+ sectionProperties: {
125
+ ...inheritedProperties,
126
+ sectionType: breakType,
127
+ },
128
+ };
129
+
130
+ cloned.content.children.splice(insertionIndex, 0, sectionBreak);
131
+ cloned.updatedAt = context.timestamp;
132
+
133
+ return {
134
+ document: cloned,
135
+ selection: createSelectionSnapshot(0, 0),
136
+ changed: true,
137
+ };
138
+ }
139
+
140
+ // ---- Delete section break ----
141
+
142
+ /**
143
+ * Delete the section break at or nearest to the current selection position.
144
+ * On deletion the *following* section's formatting and header/footer linkage
145
+ * are adopted (Word merge semantics).
146
+ */
147
+ export function deleteSectionBreak(
148
+ document: CanonicalDocumentEnvelope,
149
+ snapshot: RuntimeRenderSnapshot,
150
+ _context: SectionLayoutCommandContext,
151
+ ): SectionLayoutMutationResult {
152
+ const cloned = structuredClone(document) as CanonicalDocumentEnvelope;
153
+
154
+ const breakIndex = findNearestSectionBreak(cloned, snapshot);
155
+ if (breakIndex < 0) {
156
+ return noopResult(document, snapshot.selection);
157
+ }
158
+
159
+ const breakNode = cloned.content.children[breakIndex];
160
+ if (!breakNode || breakNode.type !== "section_break") {
161
+ return noopResult(document, snapshot.selection);
162
+ }
163
+
164
+ // Remove the section break node
165
+ cloned.content.children.splice(breakIndex, 1);
166
+
167
+ cloned.updatedAt = _context.timestamp;
168
+
169
+ return {
170
+ document: cloned,
171
+ selection: snapshot.selection,
172
+ changed: true,
173
+ };
174
+ }
175
+
176
+ export function deleteSectionBreakAtSectionIndex(
177
+ document: CanonicalDocumentEnvelope,
178
+ sectionIndex: number,
179
+ context: SectionLayoutCommandContext,
180
+ ): SectionLayoutMutationResult {
181
+ if (sectionIndex <= 0) {
182
+ return noopResult(document, createSelectionSnapshot(0, 0));
183
+ }
184
+
185
+ const cloned = structuredClone(document) as CanonicalDocumentEnvelope;
186
+ const breakIndex = findSectionBreakNodeIndexForSection(cloned, sectionIndex);
187
+ if (breakIndex < 0) {
188
+ return noopResult(document, createSelectionSnapshot(0, 0));
189
+ }
190
+
191
+ cloned.content.children.splice(breakIndex, 1);
192
+ cloned.updatedAt = context.timestamp;
193
+
194
+ return {
195
+ document: cloned,
196
+ selection: createSelectionSnapshot(0, 0),
197
+ changed: true,
198
+ };
199
+ }
200
+
201
+ // ---- Update section layout ----
202
+
203
+ /**
204
+ * Apply a bounded layout patch to the section containing the current selection.
205
+ * Supports: page size, margins, gutter, orientation, columns,
206
+ * first-page / odd-even variants, page-numbering restart / format.
207
+ */
208
+ export function updateSectionLayout(
209
+ document: CanonicalDocumentEnvelope,
210
+ snapshot: RuntimeRenderSnapshot,
211
+ patch: SectionLayoutPatch,
212
+ _context: SectionLayoutCommandContext,
213
+ ): SectionLayoutMutationResult {
214
+ const cloned = structuredClone(document) as CanonicalDocumentEnvelope;
215
+ const surface = snapshot.surface;
216
+
217
+ if (!surface) {
218
+ return noopResult(document, snapshot.selection);
219
+ }
220
+
221
+ const position = snapshot.selection.head;
222
+ const sectionTarget = resolveSectionTarget(cloned, surface.blocks, position);
223
+
224
+ if (!sectionTarget) {
225
+ return noopResult(document, snapshot.selection);
226
+ }
227
+
228
+ const existing = sectionTarget.properties ?? {};
229
+ const updated = applySectionLayoutPatch(existing, patch);
230
+
231
+ if (sectionTarget.kind === "inline") {
232
+ const breakNode = cloned.content.children[sectionTarget.blockIndex];
233
+ if (breakNode && breakNode.type === "section_break") {
234
+ breakNode.sectionProperties = updated;
235
+ // Clear raw XML so the structured properties are used on export
236
+ delete breakNode.sectionPropertiesXml;
237
+ delete breakNode.propertiesXml;
238
+ }
239
+ } else {
240
+ // Final section: update subParts.finalSectionProperties
241
+ if (!cloned.subParts) {
242
+ return noopResult(document, snapshot.selection);
243
+ }
244
+ cloned.subParts.finalSectionProperties = updated;
245
+ }
246
+
247
+ cloned.updatedAt = _context.timestamp;
248
+
249
+ return {
250
+ document: cloned,
251
+ selection: snapshot.selection,
252
+ changed: true,
253
+ };
254
+ }
255
+
256
+ export function updateSectionLayoutAtSectionIndex(
257
+ document: CanonicalDocumentEnvelope,
258
+ sectionIndex: number,
259
+ patch: SectionLayoutPatch,
260
+ context: SectionLayoutCommandContext,
261
+ ): SectionLayoutMutationResult {
262
+ const cloned = structuredClone(document) as CanonicalDocumentEnvelope;
263
+ const target = resolveSectionPropertiesOwner(cloned, sectionIndex);
264
+ if (!target) {
265
+ return noopResult(document, createSelectionSnapshot(0, 0));
266
+ }
267
+
268
+ const updated = applySectionLayoutPatch(target.properties ?? {}, patch);
269
+ if (target.kind === "inline") {
270
+ const breakNode = cloned.content.children[target.blockIndex];
271
+ if (breakNode && breakNode.type === "section_break") {
272
+ breakNode.sectionProperties = updated;
273
+ delete breakNode.sectionPropertiesXml;
274
+ delete breakNode.propertiesXml;
275
+ }
276
+ } else {
277
+ cloned.subParts = {
278
+ ...(cloned.subParts ?? {
279
+ headers: [],
280
+ footers: [],
281
+ }),
282
+ finalSectionProperties: updated,
283
+ };
284
+ }
285
+
286
+ cloned.updatedAt = context.timestamp;
287
+ return {
288
+ document: cloned,
289
+ selection: createSelectionSnapshot(0, 0),
290
+ changed: true,
291
+ };
292
+ }
293
+
294
+ export function setSectionPageNumberingAtSectionIndex(
295
+ document: CanonicalDocumentEnvelope,
296
+ sectionIndex: number,
297
+ pageNumbering: Partial<PageNumbering> | null,
298
+ context: SectionLayoutCommandContext,
299
+ ): SectionLayoutMutationResult {
300
+ const cloned = structuredClone(document) as CanonicalDocumentEnvelope;
301
+ const target = resolveSectionPropertiesOwner(cloned, sectionIndex);
302
+ if (!target) {
303
+ return noopResult(document, createSelectionSnapshot(0, 0));
304
+ }
305
+
306
+ const nextProperties = cloneSectionProperties(target.properties);
307
+ if (pageNumbering === null) {
308
+ delete nextProperties.pageNumbering;
309
+ } else {
310
+ nextProperties.pageNumbering = {
311
+ ...(target.properties?.pageNumbering ?? {}),
312
+ ...pageNumbering,
313
+ };
314
+ }
315
+
316
+ if (target.kind === "inline") {
317
+ const breakNode = cloned.content.children[target.blockIndex];
318
+ if (breakNode && breakNode.type === "section_break") {
319
+ breakNode.sectionProperties = nextProperties;
320
+ delete breakNode.sectionPropertiesXml;
321
+ delete breakNode.propertiesXml;
322
+ }
323
+ } else {
324
+ cloned.subParts = {
325
+ ...(cloned.subParts ?? {
326
+ headers: [],
327
+ footers: [],
328
+ }),
329
+ finalSectionProperties: nextProperties,
330
+ };
331
+ }
332
+
333
+ cloned.updatedAt = context.timestamp;
334
+ return {
335
+ document: cloned,
336
+ selection: createSelectionSnapshot(0, 0),
337
+ changed: true,
338
+ };
339
+ }
340
+
341
+ export function setHeaderFooterLinkAtSectionIndex(
342
+ document: CanonicalDocumentEnvelope,
343
+ sectionIndex: number,
344
+ patch: SectionLinkPatch,
345
+ context: SectionLayoutCommandContext,
346
+ ): SectionLayoutMutationResult {
347
+ const cloned = structuredClone(document) as CanonicalDocumentEnvelope;
348
+ const target = resolveSectionPropertiesOwner(cloned, sectionIndex);
349
+ if (!target) {
350
+ return noopResult(document, createSelectionSnapshot(0, 0));
351
+ }
352
+
353
+ const referencesKey =
354
+ patch.kind === "header" ? "headerReferences" : "footerReferences";
355
+ const currentProperties = cloneSectionProperties(target.properties);
356
+ const currentReferences = [...(currentProperties?.[referencesKey] ?? [])];
357
+ const nextReferences = currentReferences.filter((entry) => entry.variant !== patch.variant);
358
+
359
+ if (!patch.linkToPrevious) {
360
+ const relationshipId =
361
+ normalizeRelationshipId(patch.relationshipId) ??
362
+ resolveInheritedRelationshipId(cloned, sectionIndex, patch.kind, patch.variant);
363
+ if (!relationshipId) {
364
+ return noopResult(document, createSelectionSnapshot(0, 0));
365
+ }
366
+
367
+ nextReferences.push({
368
+ variant: patch.variant,
369
+ relationshipId,
370
+ });
371
+ }
372
+
373
+ const nextProperties = {
374
+ ...(currentProperties ?? {}),
375
+ [referencesKey]: nextReferences.length > 0 ? nextReferences : undefined,
376
+ };
377
+
378
+ if (target.kind === "inline") {
379
+ const breakNode = cloned.content.children[target.blockIndex];
380
+ if (breakNode && breakNode.type === "section_break") {
381
+ breakNode.sectionProperties = nextProperties;
382
+ delete breakNode.sectionPropertiesXml;
383
+ delete breakNode.propertiesXml;
384
+ }
385
+ } else {
386
+ cloned.subParts = {
387
+ ...(cloned.subParts ?? {
388
+ headers: [],
389
+ footers: [],
390
+ }),
391
+ finalSectionProperties: nextProperties,
392
+ };
393
+ }
394
+
395
+ cloned.updatedAt = context.timestamp;
396
+ return {
397
+ document: cloned,
398
+ selection: createSelectionSnapshot(0, 0),
399
+ changed: true,
400
+ };
401
+ }
402
+
403
+ // ---- Helpers ----
404
+
405
+ function noopResult(
406
+ document: CanonicalDocumentEnvelope,
407
+ selection: SelectionSnapshot,
408
+ ): SectionLayoutMutationResult {
409
+ return { document, selection, changed: false };
410
+ }
411
+
412
+ function createSelectionSnapshot(anchor: number, head: number): SelectionSnapshot {
413
+ return {
414
+ anchor,
415
+ head,
416
+ isCollapsed: anchor === head,
417
+ activeRange: {
418
+ kind: "range",
419
+ from: Math.min(anchor, head),
420
+ to: Math.max(anchor, head),
421
+ assoc: {
422
+ start: -1,
423
+ end: 1,
424
+ },
425
+ },
426
+ };
427
+ }
428
+
429
+ function resolveBlockIndexForPosition(
430
+ blocks: ReadonlyArray<{ from: number; to: number }>,
431
+ position: number,
432
+ ): number {
433
+ for (let i = blocks.length - 1; i >= 0; i--) {
434
+ const block = blocks[i];
435
+ if (block && position >= block.from) {
436
+ return i;
437
+ }
438
+ }
439
+ return blocks.length > 0 ? 0 : -1;
440
+ }
441
+
442
+ interface SectionTarget {
443
+ kind: "inline" | "final";
444
+ blockIndex: number;
445
+ properties?: SectionProperties;
446
+ }
447
+
448
+ function findSectionBreakNodeIndexForSection(
449
+ document: CanonicalDocumentEnvelope,
450
+ sectionIndex: number,
451
+ ): number {
452
+ if (sectionIndex <= 0) {
453
+ return -1;
454
+ }
455
+
456
+ let currentSection = 0;
457
+ for (let index = 0; index < document.content.children.length; index += 1) {
458
+ const block = document.content.children[index];
459
+ if (block?.type !== "section_break") {
460
+ continue;
461
+ }
462
+ currentSection += 1;
463
+ if (currentSection === sectionIndex) {
464
+ return index;
465
+ }
466
+ }
467
+
468
+ return -1;
469
+ }
470
+
471
+ function findSectionInsertionIndex(
472
+ document: CanonicalDocumentEnvelope,
473
+ sectionIndex: number,
474
+ ): number {
475
+ let currentSection = 0;
476
+ for (let index = 0; index < document.content.children.length; index += 1) {
477
+ const block = document.content.children[index];
478
+ if (block?.type !== "section_break") {
479
+ continue;
480
+ }
481
+ if (currentSection === sectionIndex) {
482
+ return index;
483
+ }
484
+ currentSection += 1;
485
+ }
486
+
487
+ return sectionIndex === currentSection ? document.content.children.length : -1;
488
+ }
489
+
490
+ function getSectionPropertiesAtIndex(
491
+ document: CanonicalDocumentEnvelope,
492
+ sectionIndex: number,
493
+ ): SectionProperties | undefined {
494
+ const target = resolveSectionPropertiesOwner(document, sectionIndex);
495
+ return target?.properties;
496
+ }
497
+
498
+ function resolveSectionPropertiesOwner(
499
+ document: CanonicalDocumentEnvelope,
500
+ sectionIndex: number,
501
+ ): SectionTarget | null {
502
+ if (sectionIndex < 0) {
503
+ return null;
504
+ }
505
+
506
+ let currentSection = 0;
507
+ for (let index = 0; index < document.content.children.length; index += 1) {
508
+ const block = document.content.children[index];
509
+ if (block?.type !== "section_break") {
510
+ continue;
511
+ }
512
+ if (currentSection === sectionIndex) {
513
+ return {
514
+ kind: "inline",
515
+ blockIndex: index,
516
+ properties: block.sectionProperties,
517
+ };
518
+ }
519
+ currentSection += 1;
520
+ }
521
+
522
+ return currentSection === sectionIndex
523
+ ? {
524
+ kind: "final",
525
+ blockIndex: document.content.children.length - 1,
526
+ properties: document.subParts?.finalSectionProperties,
527
+ }
528
+ : null;
529
+ }
530
+
531
+ function normalizeRelationshipId(value: string | null | undefined): string | undefined {
532
+ if (typeof value !== "string") {
533
+ return undefined;
534
+ }
535
+ const trimmed = value.trim();
536
+ return trimmed.length > 0 ? trimmed : undefined;
537
+ }
538
+
539
+ function resolveInheritedRelationshipId(
540
+ document: CanonicalDocumentEnvelope,
541
+ sectionIndex: number,
542
+ kind: "header" | "footer",
543
+ variant: "default" | "first" | "even",
544
+ ): string | undefined {
545
+ for (let candidateIndex = sectionIndex - 1; candidateIndex >= 0; candidateIndex -= 1) {
546
+ const properties = getSectionPropertiesAtIndex(document, candidateIndex);
547
+ const references =
548
+ kind === "header" ? properties?.headerReferences : properties?.footerReferences;
549
+ const directMatch = references?.find((entry) => entry.variant === variant);
550
+ if (directMatch?.relationshipId) {
551
+ return directMatch.relationshipId;
552
+ }
553
+ }
554
+
555
+ const documents = kind === "header"
556
+ ? document.subParts?.headers ?? []
557
+ : document.subParts?.footers ?? [];
558
+ return documents.find((entry) => entry.variant === variant)?.relationshipId;
559
+ }
560
+
561
+ function resolveSectionTarget(
562
+ document: CanonicalDocumentEnvelope,
563
+ blocks: ReadonlyArray<{ from: number; to: number }>,
564
+ position: number,
565
+ ): SectionTarget | null {
566
+ const blockIndex = resolveBlockIndexForPosition(blocks, position);
567
+ if (blockIndex < 0) {
568
+ return null;
569
+ }
570
+
571
+ // Walk forward from the resolved block to find the next section break
572
+ for (let i = blockIndex; i < document.content.children.length; i++) {
573
+ const block = document.content.children[i];
574
+ if (block && block.type === "section_break") {
575
+ return {
576
+ kind: "inline",
577
+ blockIndex: i,
578
+ properties: block.sectionProperties,
579
+ };
580
+ }
581
+ }
582
+
583
+ // No section break found; this is the final section
584
+ return {
585
+ kind: "final",
586
+ blockIndex: document.content.children.length - 1,
587
+ properties: document.subParts?.finalSectionProperties,
588
+ };
589
+ }
590
+
591
+ function findNearestSectionBreak(
592
+ document: CanonicalDocumentEnvelope,
593
+ snapshot: RuntimeRenderSnapshot,
594
+ ): number {
595
+ const surface = snapshot.surface;
596
+ if (!surface) {
597
+ return -1;
598
+ }
599
+
600
+ const position = snapshot.selection.head;
601
+ let bestIndex = -1;
602
+ let bestDistance = Number.POSITIVE_INFINITY;
603
+
604
+ for (let i = 0; i < document.content.children.length; i++) {
605
+ const block = document.content.children[i];
606
+ if (!block || block.type !== "section_break") {
607
+ continue;
608
+ }
609
+ const surfaceBlock = surface.blocks[i];
610
+ if (!surfaceBlock) {
611
+ continue;
612
+ }
613
+ const distance = Math.min(
614
+ Math.abs(position - surfaceBlock.from),
615
+ Math.abs(position - surfaceBlock.to),
616
+ );
617
+ if (distance < bestDistance) {
618
+ bestDistance = distance;
619
+ bestIndex = i;
620
+ }
621
+ }
622
+
623
+ return bestIndex;
624
+ }
625
+
626
+ function applySectionLayoutPatch(
627
+ existing: SectionProperties,
628
+ patch: SectionLayoutPatch,
629
+ ): SectionProperties {
630
+ const result: SectionProperties = { ...existing };
631
+
632
+ if (patch.pageSize) {
633
+ result.pageSize = {
634
+ ...(existing.pageSize ?? { width: 12240, height: 15840 }),
635
+ ...patch.pageSize,
636
+ };
637
+ }
638
+
639
+ if (patch.pageMargins) {
640
+ result.pageMargins = {
641
+ ...(existing.pageMargins ?? {
642
+ top: 1440,
643
+ right: 1440,
644
+ bottom: 1440,
645
+ left: 1440,
646
+ }),
647
+ ...patch.pageMargins,
648
+ };
649
+ }
650
+
651
+ if (patch.columns) {
652
+ result.columns = {
653
+ ...(existing.columns ?? {}),
654
+ ...patch.columns,
655
+ };
656
+ }
657
+
658
+ if (patch.pageNumbering) {
659
+ result.pageNumbering = {
660
+ ...(existing.pageNumbering ?? {}),
661
+ ...patch.pageNumbering,
662
+ };
663
+ }
664
+
665
+ if (patch.titlePage !== undefined) {
666
+ result.titlePage = patch.titlePage;
667
+ }
668
+
669
+ if (patch.sectionType !== undefined) {
670
+ result.sectionType = patch.sectionType;
671
+ }
672
+
673
+ return result;
674
+ }
675
+
676
+ function cloneSectionProperties(
677
+ properties: SectionProperties | undefined,
678
+ ): SectionProperties {
679
+ return properties ? structuredClone(properties) : {};
680
+ }