@beyondwork/docx-react-component 1.0.18 → 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 +24 -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,564 @@
1
+ /**
2
+ * Runtime-owned document navigation model.
3
+ *
4
+ * Derives the page stack, heading outline, and navigation context entirely
5
+ * from canonical document state and section properties. DOM scroll position,
6
+ * CSS layout, and shell-local state are never consulted — the runtime is the
7
+ * source of pagination truth.
8
+ *
9
+ * This module owns read models only. Section authoring commands live in
10
+ * `src/core/commands/section-layout-commands.ts` (owned by A3).
11
+ */
12
+
13
+ import type {
14
+ DocumentHeadingSnapshot,
15
+ DocumentNavigationSnapshot,
16
+ DocumentPageSnapshot,
17
+ EditorStoryTarget,
18
+ EditorSurfaceSnapshot,
19
+ SurfaceBlockSnapshot,
20
+ SurfaceInlineSegment,
21
+ } from "../api/public-types";
22
+ import type {
23
+ FootnoteCollection,
24
+ } from "../model/canonical-document.ts";
25
+ import type { CanonicalDocumentEnvelope } from "../core/state/editor-state.ts";
26
+ import { createSelectionSnapshot } from "../core/state/editor-state.ts";
27
+ import { MAIN_STORY_TARGET } from "../core/selection/mapping.ts";
28
+ import { createEditorSurfaceSnapshot } from "./surface-projection.ts";
29
+ import {
30
+ estimateBlockHeight,
31
+ estimateParagraphHeight,
32
+ getUsableColumnMetrics,
33
+ getUsableColumnWidth,
34
+ getUsablePageHeight,
35
+ } from "./page-layout-estimation.ts";
36
+ import {
37
+ buildPageLayoutSnapshot,
38
+ buildResolvedSections,
39
+ findSectionForPosition as resolveSectionForPosition,
40
+ resolveSectionForStoryTarget,
41
+ type ResolvedDocumentSection,
42
+ } from "./document-layout.ts";
43
+ import { findNoteReferencePosition } from "./view-state.ts";
44
+
45
+ const MIN_BLOCK_HEIGHT_TWIPS = 240;
46
+ const FOOTNOTE_REFERENCE_RESERVATION_TWIPS = 180;
47
+
48
+ // ---------------------------------------------------------------------------
49
+ // Public API
50
+ // ---------------------------------------------------------------------------
51
+
52
+ /**
53
+ * Build the full document navigation snapshot from canonical state.
54
+ *
55
+ * The page stack is derived from structured block layout, section geometry,
56
+ * and bounded pagination rules. DOM scroll position and CSS layout are never
57
+ * consulted.
58
+ */
59
+ export function createDocumentNavigationSnapshot(
60
+ document: CanonicalDocumentEnvelope,
61
+ selectionHead: number,
62
+ activeStory: EditorStoryTarget,
63
+ ): DocumentNavigationSnapshot {
64
+ const mainSurface = createEditorSurfaceSnapshot(
65
+ document,
66
+ createSelectionSnapshot(0, 0),
67
+ MAIN_STORY_TARGET,
68
+ );
69
+
70
+ const sections = buildResolvedSections(document);
71
+ const pages = buildPageStack(document, sections, mainSurface);
72
+ const headings = buildHeadingOutline(document, mainSurface, sections, pages);
73
+ const navigationContext = resolveActiveNavigationContext(
74
+ document,
75
+ pages,
76
+ sections,
77
+ mainSurface,
78
+ selectionHead,
79
+ activeStory,
80
+ );
81
+
82
+ return {
83
+ pageCount: pages.length,
84
+ pages,
85
+ headings,
86
+ activePageIndex: navigationContext.activePageIndex,
87
+ activeSectionIndex: navigationContext.activeSectionIndex,
88
+ };
89
+ }
90
+
91
+ /**
92
+ * Find the page index that contains a given main-story character offset.
93
+ */
94
+ export function findPageForOffset(
95
+ pages: readonly DocumentPageSnapshot[],
96
+ offset: number,
97
+ ): number {
98
+ for (let i = 0; i < pages.length; i++) {
99
+ if (offset < pages[i]!.endOffset) {
100
+ return i;
101
+ }
102
+ }
103
+ return Math.max(0, pages.length - 1);
104
+ }
105
+
106
+ /**
107
+ * Find the section index that contains a given main-story character offset.
108
+ */
109
+ export function findSectionForOffset(
110
+ sections: readonly ResolvedDocumentSection[],
111
+ offset: number,
112
+ ): number {
113
+ return sections.length > 0 ? resolveSectionForPosition(sections, offset).index : 0;
114
+ }
115
+
116
+ function buildPageStack(
117
+ document: CanonicalDocumentEnvelope,
118
+ sections: ResolvedDocumentSection[],
119
+ mainSurface: EditorSurfaceSnapshot,
120
+ ): DocumentPageSnapshot[] {
121
+ const pages: DocumentPageSnapshot[] = [];
122
+ let globalPageIndex = 0;
123
+
124
+ for (const section of sections) {
125
+ const layout = buildPageLayoutSnapshot(
126
+ section.index,
127
+ section.properties ?? document.subParts?.finalSectionProperties,
128
+ document.subParts,
129
+ );
130
+ const sectionBlocks = collectSectionBlocks(mainSurface.blocks, section.start, section.end);
131
+ const paginated = paginateSectionBlocks(
132
+ section,
133
+ sectionBlocks,
134
+ layout,
135
+ document.subParts?.footnoteCollection,
136
+ );
137
+
138
+ for (const page of paginated) {
139
+ pages.push({
140
+ ...page,
141
+ pageIndex: globalPageIndex,
142
+ });
143
+ globalPageIndex += 1;
144
+ }
145
+ }
146
+
147
+ // Guarantee at least one page
148
+ if (pages.length === 0) {
149
+ pages.push({
150
+ pageIndex: 0,
151
+ sectionIndex: 0,
152
+ pageInSection: 0,
153
+ startOffset: 0,
154
+ endOffset: mainSurface.storySize,
155
+ layout: buildPageLayoutSnapshot(
156
+ 0,
157
+ document.subParts?.finalSectionProperties,
158
+ document.subParts,
159
+ ),
160
+ });
161
+ }
162
+
163
+ return pages;
164
+ }
165
+
166
+ function collectSectionBlocks(
167
+ blocks: readonly SurfaceBlockSnapshot[],
168
+ start: number,
169
+ end: number,
170
+ ): SurfaceBlockSnapshot[] {
171
+ return blocks.filter((block) => block.to > start && block.from < end);
172
+ }
173
+
174
+ function paginateSectionBlocks(
175
+ section: ResolvedDocumentSection,
176
+ blocks: readonly SurfaceBlockSnapshot[],
177
+ layout: DocumentPageSnapshot["layout"],
178
+ footnotes: FootnoteCollection | undefined,
179
+ ): Omit<DocumentPageSnapshot, "pageIndex">[] {
180
+ if (blocks.length === 0) {
181
+ return [
182
+ {
183
+ sectionIndex: section.index,
184
+ pageInSection: 0,
185
+ startOffset: section.start,
186
+ endOffset: section.end,
187
+ layout,
188
+ },
189
+ ];
190
+ }
191
+
192
+ const pages: Omit<DocumentPageSnapshot, "pageIndex">[] = [];
193
+ const usableHeight = getUsablePageHeight(layout);
194
+ const columnMetrics = getUsableColumnMetrics(layout);
195
+ const maxColumns = Math.max(1, columnMetrics.length);
196
+ let pageStart = section.start;
197
+ let columnHeight = 0;
198
+ let columnIndex = 0;
199
+ let pageInSection = 0;
200
+ let reservedNoteHeight = 0;
201
+ const reservedNotes = new Set<string>();
202
+
203
+ const pushPage = (endOffset: number): void => {
204
+ const boundedEnd = Math.max(pageStart, Math.min(endOffset, section.end));
205
+ if (boundedEnd === pageStart && pages.length > 0) {
206
+ return;
207
+ }
208
+ pages.push({
209
+ sectionIndex: section.index,
210
+ pageInSection,
211
+ startOffset: pageStart,
212
+ endOffset: boundedEnd,
213
+ layout,
214
+ });
215
+ pageInSection += 1;
216
+ pageStart = boundedEnd;
217
+ columnHeight = 0;
218
+ columnIndex = 0;
219
+ reservedNoteHeight = 0;
220
+ reservedNotes.clear();
221
+ };
222
+
223
+ for (let index = 0; index < blocks.length; index += 1) {
224
+ const block = blocks[index]!;
225
+ const nextBoundary = blocks[index + 1]?.from ?? section.end;
226
+ while (true) {
227
+ const columnWidth =
228
+ columnMetrics[Math.min(columnIndex, columnMetrics.length - 1)]?.width ??
229
+ getUsableColumnWidth(layout);
230
+ const baseHeight = estimateBlockHeight(block, columnWidth);
231
+ const keepWithNextHeight =
232
+ block.kind === "paragraph" && block.keepNext
233
+ ? baseHeight + estimateBlockHeight(blocks[index + 1], columnWidth)
234
+ : baseHeight;
235
+ const noteHeight = estimateFootnoteReservation(block, footnotes, columnWidth, reservedNotes);
236
+ const projectedHeight = columnHeight + keepWithNextHeight + reservedNoteHeight + noteHeight;
237
+
238
+ if (block.kind === "paragraph" && block.pageBreakBefore && pageStart < block.from) {
239
+ pushPage(block.from);
240
+ continue;
241
+ }
242
+ if (projectedHeight > usableHeight && pageStart < block.from) {
243
+ if (columnIndex < maxColumns - 1) {
244
+ columnIndex += 1;
245
+ columnHeight = 0;
246
+ reservedNoteHeight = 0;
247
+ reservedNotes.clear();
248
+ continue;
249
+ }
250
+ pushPage(block.from);
251
+ continue;
252
+ }
253
+
254
+ const effectiveNoteHeight = estimateFootnoteReservation(
255
+ block,
256
+ footnotes,
257
+ columnWidth,
258
+ reservedNotes,
259
+ );
260
+ columnHeight += baseHeight;
261
+ reservedNoteHeight += effectiveNoteHeight;
262
+ currentPageNoteIds(block).forEach((noteKey) => reservedNotes.add(noteKey));
263
+
264
+ if (hasColumnBreak(block)) {
265
+ if (columnIndex < maxColumns - 1) {
266
+ columnIndex += 1;
267
+ columnHeight = 0;
268
+ reservedNoteHeight = 0;
269
+ reservedNotes.clear();
270
+ } else {
271
+ pushPage(nextBoundary);
272
+ }
273
+ break;
274
+ }
275
+
276
+ if (index === blocks.length - 1) {
277
+ pushPage(section.end);
278
+ }
279
+ break;
280
+ }
281
+ }
282
+
283
+ return pages.length > 0
284
+ ? pages
285
+ : [
286
+ {
287
+ sectionIndex: section.index,
288
+ pageInSection: 0,
289
+ startOffset: section.start,
290
+ endOffset: section.end,
291
+ layout,
292
+ },
293
+ ];
294
+ }
295
+
296
+ function resolveActiveNavigationContext(
297
+ document: CanonicalDocumentEnvelope,
298
+ pages: readonly DocumentPageSnapshot[],
299
+ sections: readonly ResolvedDocumentSection[],
300
+ mainSurface: EditorSurfaceSnapshot,
301
+ selectionHead: number,
302
+ activeStory: EditorStoryTarget,
303
+ ): { activePageIndex: number; activeSectionIndex: number } {
304
+ if (activeStory.kind === "main") {
305
+ const activePageIndex = findPageForOffset(pages, selectionHead);
306
+ return {
307
+ activePageIndex,
308
+ activeSectionIndex: pages[activePageIndex]?.sectionIndex ?? 0,
309
+ };
310
+ }
311
+
312
+ if (activeStory.kind === "header" || activeStory.kind === "footer") {
313
+ const section = resolveSectionForStoryTarget(document, sections, activeStory);
314
+ if (section) {
315
+ return {
316
+ activePageIndex: findFirstPageIndexForSection(pages, section.index),
317
+ activeSectionIndex: section.index,
318
+ };
319
+ }
320
+ }
321
+
322
+ if (activeStory.kind === "footnote" || activeStory.kind === "endnote") {
323
+ const referencePosition = findNoteReferencePosition(mainSurface, activeStory);
324
+ const activePageIndex = findPageForOffset(pages, referencePosition);
325
+ return {
326
+ activePageIndex,
327
+ activeSectionIndex:
328
+ pages[activePageIndex]?.sectionIndex ??
329
+ findSectionForOffset(sections, referencePosition),
330
+ };
331
+ }
332
+
333
+ return {
334
+ activePageIndex: 0,
335
+ activeSectionIndex: 0,
336
+ };
337
+ }
338
+
339
+ function findFirstPageIndexForSection(
340
+ pages: readonly DocumentPageSnapshot[],
341
+ sectionIndex: number,
342
+ ): number {
343
+ const match = pages.findIndex((page) => page.sectionIndex === sectionIndex);
344
+ return match >= 0 ? match : 0;
345
+ }
346
+
347
+ function estimateFootnoteReservation(
348
+ block: SurfaceBlockSnapshot,
349
+ footnotes: FootnoteCollection | undefined,
350
+ columnWidth: number,
351
+ reservedNotes: ReadonlySet<string>,
352
+ ): number {
353
+ if (!footnotes || block.kind !== "paragraph") {
354
+ return 0;
355
+ }
356
+
357
+ let reservation = 0;
358
+ for (const noteKey of currentPageNoteIds(block)) {
359
+ if (reservedNotes.has(noteKey)) {
360
+ continue;
361
+ }
362
+
363
+ const [noteKind, noteId] = noteKey.split(":");
364
+ const noteCollection =
365
+ noteKind === "endnote" ? footnotes.endnotes : footnotes.footnotes;
366
+ const note = noteCollection[noteId];
367
+ reservation += FOOTNOTE_REFERENCE_RESERVATION_TWIPS;
368
+ if (note) {
369
+ reservation += note.blocks.reduce(
370
+ (total, child) => total + estimateCanonicalNoteBlockHeight(child, columnWidth),
371
+ 0,
372
+ );
373
+ }
374
+ }
375
+
376
+ return reservation;
377
+ }
378
+
379
+ function estimateCanonicalNoteBlockHeight(
380
+ block: FootnoteCollection["footnotes"][string]["blocks"][number],
381
+ columnWidth: number,
382
+ ): number {
383
+ switch (block.type) {
384
+ case "paragraph":
385
+ return estimateParagraphHeight(
386
+ {
387
+ blockId: "note",
388
+ kind: "paragraph",
389
+ from: 0,
390
+ to: 0,
391
+ ...(block.styleId ? { styleId: block.styleId } : {}),
392
+ segments: createEstimatedNoteSegments(block.children),
393
+ },
394
+ columnWidth,
395
+ );
396
+ case "table":
397
+ return MIN_BLOCK_HEIGHT_TWIPS * Math.max(1, block.rows.length);
398
+ default:
399
+ return MIN_BLOCK_HEIGHT_TWIPS;
400
+ }
401
+ }
402
+
403
+ function createEstimatedNoteSegments(
404
+ children: Extract<FootnoteCollection["footnotes"][string]["blocks"][number], { type: "paragraph" }>["children"],
405
+ ): SurfaceInlineSegment[] {
406
+ const segments: SurfaceInlineSegment[] = [];
407
+
408
+ children.forEach((child, index) => {
409
+ if (child.type === "text") {
410
+ segments.push({
411
+ segmentId: `note-${index}`,
412
+ kind: "text",
413
+ from: 0,
414
+ to: Array.from(child.text).length,
415
+ text: child.text,
416
+ });
417
+ return;
418
+ }
419
+
420
+ if (child.type === "hard_break" || child.type === "tab") {
421
+ segments.push({
422
+ segmentId: `note-${index}`,
423
+ kind: child.type,
424
+ from: 0,
425
+ to: 1,
426
+ });
427
+ }
428
+ });
429
+
430
+ return segments;
431
+ }
432
+
433
+ function currentPageNoteIds(
434
+ block: SurfaceBlockSnapshot,
435
+ ): Set<string> {
436
+ const notes = new Set<string>();
437
+ if (block.kind !== "paragraph") {
438
+ return notes;
439
+ }
440
+
441
+ for (const segment of block.segments) {
442
+ if (segment.kind === "note_ref" && segment.noteId) {
443
+ notes.add(`${segment.noteKind}:${segment.noteId}`);
444
+ }
445
+ }
446
+ return notes;
447
+ }
448
+
449
+ function hasColumnBreak(block: SurfaceBlockSnapshot): boolean {
450
+ return block.kind === "paragraph" && block.segments.some(
451
+ (segment) =>
452
+ segment.kind === "opaque_inline" &&
453
+ segment.label === "Column break",
454
+ );
455
+ }
456
+
457
+ // ---------------------------------------------------------------------------
458
+ // Heading outline
459
+ // ---------------------------------------------------------------------------
460
+
461
+ /** Style IDs that map to heading levels by convention. */
462
+ const HEADING_STYLE_PATTERN = /^heading\s*(\d+)$/i;
463
+
464
+ function headingLevelFromStyleId(styleId?: string): number | null {
465
+ if (!styleId) return null;
466
+ const match = HEADING_STYLE_PATTERN.exec(styleId);
467
+ if (match) {
468
+ const level = parseInt(match[1]!, 10);
469
+ return level >= 1 && level <= 9 ? level : null;
470
+ }
471
+ return null;
472
+ }
473
+
474
+ function buildHeadingOutline(
475
+ document: CanonicalDocumentEnvelope,
476
+ mainSurface: EditorSurfaceSnapshot,
477
+ sections: ResolvedDocumentSection[],
478
+ pages: DocumentPageSnapshot[],
479
+ ): DocumentHeadingSnapshot[] {
480
+ const headings: DocumentHeadingSnapshot[] = [];
481
+
482
+ for (const block of mainSurface.blocks) {
483
+ if (block.kind !== "paragraph") continue;
484
+
485
+ // Check explicit outlineLevel first, then style-based heading detection
486
+ const outlineLevel = block.outlineLevel;
487
+ const styleLevel = resolveHeadingLevelFromStyle(document, block.styleId);
488
+ const level =
489
+ outlineLevel !== undefined && outlineLevel >= 0
490
+ ? outlineLevel + 1
491
+ : styleLevel;
492
+
493
+ if (level === null) continue;
494
+
495
+ const text = extractParagraphText(block);
496
+ if (!text.trim()) continue;
497
+
498
+ const offset = block.from;
499
+ const pageIndex = findPageForOffset(pages, offset);
500
+ const sectionIndex = findSectionForOffset(sections, offset);
501
+
502
+ headings.push({
503
+ headingId: `heading-${block.blockId}-${offset}`,
504
+ level,
505
+ text: text.trim(),
506
+ offset,
507
+ pageIndex,
508
+ sectionIndex,
509
+ });
510
+ }
511
+
512
+ return headings;
513
+ }
514
+
515
+ function resolveHeadingLevelFromStyle(
516
+ document: CanonicalDocumentEnvelope,
517
+ styleId?: string,
518
+ ): number | null {
519
+ if (!styleId) {
520
+ return null;
521
+ }
522
+
523
+ const visited = new Set<string>();
524
+ let currentStyleId: string | undefined = styleId;
525
+ while (currentStyleId && !visited.has(currentStyleId)) {
526
+ visited.add(currentStyleId);
527
+ const style:
528
+ | { outlineLevel?: number; basedOn?: string }
529
+ | undefined = document.styles.paragraphs[currentStyleId];
530
+ if (!style) {
531
+ break;
532
+ }
533
+ if (
534
+ typeof style.outlineLevel === "number" &&
535
+ style.outlineLevel >= 0 &&
536
+ style.outlineLevel <= 8
537
+ ) {
538
+ return style.outlineLevel + 1;
539
+ }
540
+ currentStyleId = style.basedOn;
541
+ }
542
+
543
+ return headingLevelFromStyleId(styleId);
544
+ }
545
+
546
+ function extractParagraphText(
547
+ block: Extract<SurfaceBlockSnapshot, { kind: "paragraph" }>,
548
+ ): string {
549
+ let text = "";
550
+ for (const segment of block.segments) {
551
+ switch (segment.kind) {
552
+ case "text":
553
+ text += segment.text;
554
+ break;
555
+ case "tab":
556
+ text += "\t";
557
+ break;
558
+ case "hard_break":
559
+ text += "\n";
560
+ break;
561
+ }
562
+ }
563
+ return text;
564
+ }