-
Notifications
You must be signed in to change notification settings - Fork 132
Expand file tree
/
Copy pathindex.ts
More file actions
1939 lines (1776 loc) · 54.6 KB
/
index.ts
File metadata and controls
1939 lines (1776 loc) · 54.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import type { TabStop } from './engines/tabs.js';
export { computeTabStops, layoutWithTabs, calculateTabWidth } from './engines/tabs.js';
// Re-export TabStop for external consumers
export type { TabStop };
// Export table contracts
export { OOXML_PCT_DIVISOR, type TableWidthAttr, type TableColumnSpec } from './engines/tables.js';
export { effectiveTableCellSpacing } from './table-cell-spacing.js';
// Table column rescaling (moved from layout-engine for cross-stage use)
export { rescaleColumnWidths } from './table-column-rescale.js';
// Cell spacing resolution (moved from measuring-dom for cross-stage use)
export { getCellSpacingPx } from './cell-spacing.js';
// OOXML z-index normalization (moved from pm-adapter for cross-stage use)
export {
normalizeZIndex,
coerceRelativeHeight,
isPlainObject,
OOXML_Z_INDEX_BASE,
resolveFloatingZIndex,
getFragmentZIndex,
} from './ooxml-z-index.js';
// Export justify utilities
export {
shouldApplyJustify,
calculateJustifySpacing,
SPACE_CHARS,
type ShouldApplyJustifyParams,
type CalculateJustifySpacingParams,
} from './justify-utils.js';
export {
parseInsetClipPathForScale,
formatInsetClipPathTransform,
type InsetClipPathScale,
} from './clip-path-inset.js';
export {
SUBSCRIPT_SUPERSCRIPT_SCALE,
normalizeBaselineShift,
hasExplicitBaselineShift,
isSuperscriptOrSubscript,
usesDefaultScriptLayout,
scaleFontSizeForVerticalText,
resolveBaseFontSizeForVerticalText,
type VerticalTextAlign,
} from './vertical-text.js';
export { computeFragmentPmRange, computeLinePmRange, type LinePmRange } from './pm-range.js';
export { cloneColumnLayout, normalizeColumnLayout, widthsEqual } from './column-layout.js';
export type { NormalizedColumnLayout } from './column-layout.js';
/** Inline field annotation metadata extracted from w:sdt nodes. */
export type FieldAnnotationMetadata = {
type: 'fieldAnnotation';
variant?: 'text' | 'image' | 'signature' | 'checkbox' | 'html' | 'link';
fieldId: string;
fieldType?: string;
displayLabel?: string;
defaultDisplayLabel?: string;
alias?: string;
fieldColor?: string;
borderColor?: string;
highlighted?: boolean;
fontFamily?: string | null;
fontSize?: string | number | null;
textColor?: string | null;
textHighlight?: string | null;
linkUrl?: string | null;
imageSrc?: string | null;
rawHtml?: unknown;
size?: {
width?: number;
height?: number;
} | null;
extras?: Record<string, unknown> | null;
multipleImage?: boolean;
hash?: string | null;
generatorIndex?: number | null;
sdtId?: string | null;
hidden?: boolean;
visibility?: 'visible' | 'hidden';
isLocked?: boolean;
formatting?: {
bold?: boolean;
italic?: boolean;
underline?: boolean;
};
marks?: Record<string, unknown>;
};
export type StructuredContentLockMode = 'unlocked' | 'sdtLocked' | 'contentLocked' | 'sdtContentLocked';
export type StructuredContentMetadata = {
type: 'structuredContent';
scope: 'inline' | 'block';
id?: string | null;
tag?: string | null;
alias?: string | null;
lockMode?: StructuredContentLockMode;
sdtPr?: unknown;
};
export type DocumentSectionMetadata = {
type: 'documentSection';
id?: string | null;
title?: string | null;
description?: string | null;
sectionType?: string | null;
isLocked?: boolean;
sdBlockId?: string | null;
};
export type DocPartMetadata = {
type: 'docPartObject';
gallery?: string | null;
uniqueId?: string | null;
alias?: string | null;
instruction?: string | null;
};
/**
* Union of all SDT (Structured Document Tag) metadata variants.
*
* Word SDTs are flexible containers that can represent:
* - Field annotations: inline placeholders for user input
* - Structured content: containers with semantic tags (inline or block-level)
* - Document sections: locked or conditional regions with titles
* - Doc parts: special objects like tables of contents
*/
export type SdtMetadata =
| FieldAnnotationMetadata
| StructuredContentMetadata
| DocumentSectionMetadata
| DocPartMetadata;
export const CONTRACTS_VERSION = '1.0.0';
/** Unique identifier for a block in the document. Format: `${pos}-${type}`. */
export type BlockId = string;
/** Tab leader type for filling space before tab stops. */
export type LeaderType = 'dot' | 'heavy' | 'hyphen' | 'middleDot' | 'underscore';
export type TrackedChangeKind = 'insert' | 'delete' | 'format';
export type TrackedChangesMode = 'review' | 'original' | 'final' | 'off';
/** Formatting mark for track-format metadata. */
export type RunMark = {
type: string;
attrs?: Record<string, unknown> | null;
};
export type TrackedChangeMeta = {
kind: TrackedChangeKind;
id: string;
author?: string;
authorEmail?: string;
authorImage?: string;
date?: string;
before?: RunMark[];
after?: RunMark[];
};
export type FlowRunLinkTarget = '_blank' | '_self' | '_parent' | '_top';
export type FlowRunLink = {
version?: 1 | 2;
href?: string;
title?: string;
target?: FlowRunLinkTarget;
rel?: string;
tooltip?: string;
anchor?: string;
docLocation?: string;
rId?: string;
name?: string;
history?: boolean;
};
/**
* Common formatting marks that can be applied to any run type.
* Used by TextRun, TabRun, and other run types that support inline formatting.
*/
export type RunMarks = {
/** Bold text styling. */
bold?: boolean;
/** Italic text styling. */
italic?: boolean;
/** Additional letter spacing in pixels (positive for expanded, negative for condensed). */
letterSpacing?: number;
/** Text color as hex string (e.g., "#FF0000"). */
color?: string;
/** Underline decoration with optional style and color. */
underline?: {
/** Underline style (defaults to 'single'). */
style?: 'single' | 'double' | 'dotted' | 'dashed' | 'wavy';
/** Underline color as hex string (defaults to text color). */
color?: string;
} | null;
/** Strikethrough text decoration. */
strike?: boolean;
/** Highlight (background) color as hex string. */
highlight?: string;
/** Text transformation (case modification). */
textTransform?: 'uppercase' | 'lowercase' | 'capitalize' | 'none';
/** Vertical alignment for superscript/subscript text. */
vertAlign?: 'superscript' | 'subscript' | 'baseline';
/**
* Explicit baseline shift in points (positive = raise, negative = lower).
* Rendering normalizes a shift of zero to "no explicit shift".
*/
baselineShift?: number;
};
export type TextRun = RunMarks & {
kind?: 'text';
text: string;
fontFamily: string;
fontSize: number;
/** Comment annotations applied to this run (supports overlapping comments). */
comments?: Array<{
commentId: string;
importedId?: string;
internal?: boolean;
trackedChange?: boolean;
}>;
/**
* Custom data attributes propagated from ProseMirror marks (keys must be data-*).
*/
dataAttrs?: Record<string, string>;
sdt?: SdtMetadata;
link?: FlowRunLink;
/** Token annotations for dynamic content (page numbers, etc.). */
token?: 'pageNumber' | 'totalPageCount' | 'pageReference';
/** Absolute ProseMirror position (inclusive) of first character in this run. */
pmStart?: number;
/** Absolute ProseMirror position (exclusive) after the last character. */
pmEnd?: number;
/** Metadata for page reference tokens (only when token === 'pageReference'). */
pageRefMetadata?: {
bookmarkId: string;
instruction: string;
};
/** Tracked-change metadata from ProseMirror marks. */
trackedChange?: TrackedChangeMeta;
};
export type TabRun = RunMarks & {
kind: 'tab';
text: '\t';
/** Width in pixels (assigned by measurer/resolver). */
width?: number;
tabStops?: TabStop[];
tabIndex?: number;
leader?: LeaderType | null;
decimalChar?: string;
indent?: ParagraphIndent;
pmStart?: number;
pmEnd?: number;
/** SDT metadata if tab is inside a structured document tag. */
sdt?: SdtMetadata;
};
export type LineBreakRun = {
kind: 'lineBreak';
/**
* Optional attributes carried through from the source document.
* Mirrors OOXML <w:br> attributes (type/clear) to preserve fidelity.
*/
attrs?: {
lineBreakType?: string;
clear?: string;
};
pmStart?: number;
pmEnd?: number;
};
export type ImageLuminanceAdjustment = {
/** OOXML a:lum/@bright in raw units (-100000..100000). */
bright?: number;
/** OOXML a:lum/@contrast in raw units (-100000..100000). */
contrast?: number;
};
/**
* Inline image run for images that flow with text on the same line.
* Unlike ImageBlock (anchored/floating images), ImageRun is part of the paragraph's run array
* and participates in line breaking alongside text.
*
* Corresponds to Microsoft Word's inline images (<wp:inline> in DOCX).
*
* @example
* // A paragraph with text and inline image:
* {
* kind: 'paragraph',
* runs: [
* { kind: 'text', text: 'Here is an image: ', ... },
* { kind: 'image', src: 'data:...', width: 100, height: 50, ... },
* { kind: 'text', text: ' within text.', ... }
* ]
* }
*/
export type ImageRun = {
kind: 'image';
/** Image source URL (data URI or external URL). */
src: string;
/** Image width in pixels. */
width: number;
/** Image height in pixels. */
height: number;
/** Alternative text for accessibility. */
alt?: string;
/** Image title (tooltip). */
title?: string;
/** Clip-path value for cropped images. */
clipPath?: string;
/**
* Spacing around the image (from DOCX distT/distB/distL/distR attributes).
* Applied as CSS margins in the DOM painter.
* All values in pixels.
*/
distTop?: number;
distBottom?: number;
distLeft?: number;
distRight?: number;
/**
* Vertical alignment of image relative to text baseline.
* Currently only 'bottom' is supported (image sits on baseline).
* Future: 'top', 'middle', 'baseline', 'text-top', 'text-bottom'.
*/
verticalAlign?: 'bottom';
/** Absolute ProseMirror position (inclusive) of this image run. */
pmStart?: number;
/** Absolute ProseMirror position (exclusive) after this image run. */
pmEnd?: number;
/** SDT metadata if image is wrapped in a structured document tag. */
sdt?: SdtMetadata;
/**
* Custom data attributes propagated from ProseMirror marks (keys must be data-*).
*/
dataAttrs?: Record<string, string>;
// Image transformations from OOXML a:xfrm (applies to inline images)
rotation?: number; // Rotation angle in degrees
flipH?: boolean; // Horizontal flip
flipV?: boolean; // Vertical flip
// VML image adjustments for watermark effects
gain?: string | number; // Brightness/washout (VML hex string or number)
blacklevel?: string | number; // Contrast adjustment (VML hex string or number)
// OOXML image effects
grayscale?: boolean; // Apply grayscale filter to image
lum?: ImageLuminanceAdjustment; // DrawingML luminance adjustment from a:lum
};
export type BreakRun = {
kind: 'break';
/** Optional break type (e.g., 'line', 'page', 'column') */
breakType?: 'line' | 'page' | 'column' | string;
pmStart?: number;
pmEnd?: number;
sdt?: SdtMetadata;
};
/**
* Inline field annotation run for interactive form fields displayed as styled "pills".
* Renders as a bordered, rounded inline element with displayLabel or type-specific content.
*
* Corresponds to super-editor's FieldAnnotation node which renders via FieldAnnotationView.
*
* @example
* // A paragraph with text and field annotation:
* {
* kind: 'paragraph',
* runs: [
* { kind: 'text', text: 'Enter name: ', ... },
* { kind: 'fieldAnnotation', variant: 'text', displayLabel: 'Full Name', fieldColor: '#980043', ... },
* ]
* }
*/
export type FieldAnnotationRun = {
kind: 'fieldAnnotation';
/** The variant/type of field annotation. */
variant: 'text' | 'image' | 'signature' | 'checkbox' | 'html' | 'link';
/** Display text shown inside the pill (fallback for all types). */
displayLabel: string;
/** Unique field identifier. */
fieldId?: string;
/** Field type identifier (e.g., 'TEXTINPUT', 'SIGNATURE'). */
fieldType?: string;
/** Background color as hex string (e.g., "#980043"). Applied with alpha. */
fieldColor?: string;
/** Border color as hex string (e.g., "#b015b3"). */
borderColor?: string;
/** Whether to show the pill styling (border, background). Defaults to true. */
highlighted?: boolean;
/** Whether the field is hidden (display: none). */
hidden?: boolean;
/** CSS visibility value. */
visibility?: 'visible' | 'hidden';
// Type-specific content
/** Image source URL for image/signature variants. */
imageSrc?: string | null;
/** Link URL for link variant. */
linkUrl?: string | null;
/** Raw HTML content for html variant. */
rawHtml?: string | null;
// Sizing
/** Explicit size for the annotation (used for images). */
size?: {
width?: number;
height?: number;
} | null;
// Typography (applied to the displayLabel text)
/** Font family for the label text. */
fontFamily?: string | null;
/** Font size in points or pixels (e.g., "12pt", 14). */
fontSize?: string | number | null;
/** Text color as hex string. */
textColor?: string | null;
/** Text highlight/background color (overrides fieldColor). */
textHighlight?: string | null;
/** Bold text styling. */
bold?: boolean;
/** Italic text styling. */
italic?: boolean;
/** Underline text styling. */
underline?: boolean;
/** Absolute ProseMirror position (inclusive) of this run. */
pmStart?: number;
/** Absolute ProseMirror position (exclusive) after this run. */
pmEnd?: number;
/** Full SDT metadata if available. */
sdt?: SdtMetadata;
};
export type MathRun = {
kind: 'math';
/** OMML XML as JSON (xml2json format) for the renderer to convert to MathML. */
ommlJson: unknown;
/** Plain text content for measurement fallback and accessibility. */
textContent: string;
/** Estimated width in pixels. */
width: number;
/** Estimated height in pixels. */
height: number;
/** Absolute ProseMirror position (inclusive) of this math run. */
pmStart?: number;
/** Absolute ProseMirror position (exclusive) after this math run. */
pmEnd?: number;
/** SDT metadata if math is wrapped in a structured document tag. */
sdt?: SdtMetadata;
};
export type Run = TextRun | TabRun | ImageRun | LineBreakRun | BreakRun | FieldAnnotationRun | MathRun;
export type ParagraphBlock = {
kind: 'paragraph';
id: BlockId;
runs: Run[];
attrs?: ParagraphAttrs;
};
/** Border style (subset of OOXML ST_Border). */
export type BorderStyle =
| 'none'
| 'single'
| 'double'
| 'dashed'
| 'dotted'
| 'thick'
| 'triple'
| 'dotDash'
| 'dotDotDash'
| 'wave'
| 'doubleWave';
/** Border specification for table and cell borders. */
export type BorderSpec = {
style?: BorderStyle;
width?: number;
color?: string;
space?: number;
};
/**
* Three-state border value for table borders.
* - `null`: inherit from table style
* - `{ none: true }`: explicit "no border"
* - `BorderSpec`: explicit border
*/
export type TableBorderValue = null | { none: true } | BorderSpec;
/** Table-level border configuration (outer + inner borders). */
export type TableBorders = {
top?: TableBorderValue;
right?: TableBorderValue;
bottom?: TableBorderValue;
left?: TableBorderValue;
insideH?: TableBorderValue;
insideV?: TableBorderValue;
};
/** Cell-level border configuration (overrides table-level borders). */
export type CellBorders = {
top?: BorderSpec;
right?: BorderSpec;
bottom?: BorderSpec;
left?: BorderSpec;
};
export type TableCellAttrs = {
borders?: CellBorders;
padding?: BoxSpacing;
verticalAlign?: 'top' | 'middle' | 'center' | 'bottom';
background?: string;
tableCellProperties?: Record<string, unknown>;
};
export type TableAttrs = {
borders?: TableBorders;
borderCollapse?: 'collapse' | 'separate';
cellSpacing?: CellSpacing;
sdt?: SdtMetadata;
containerSdt?: SdtMetadata;
[key: string]: unknown;
};
export type TableCell = {
id: BlockId;
/** Multi-block cell content (new feature) */
blocks?: (ParagraphBlock | ImageBlock | DrawingBlock | TableBlock)[];
/** Single paragraph (backward compatibility) */
paragraph?: ParagraphBlock;
rowSpan?: number;
colSpan?: number;
attrs?: TableCellAttrs;
};
export type TableRowProperties = {
repeatHeader?: boolean;
cantSplit?: boolean;
[key: string]: unknown;
};
export type TableRowAttrs = {
tableRowProperties?: TableRowProperties;
rowHeight?: {
value: number;
rule?: 'auto' | 'atLeast' | 'exact' | string;
};
};
export type TableRow = {
id: BlockId;
cells: TableCell[];
attrs?: TableRowAttrs;
};
export type TableBlock = {
kind: 'table';
id: BlockId;
rows: TableRow[];
attrs?: TableAttrs;
/** Column widths in pixels from OOXML w:tblGrid. */
columnWidths?: number[];
/** Anchor positioning for floating tables (from w:tblpPr). */
anchor?: TableAnchor;
/** Text wrapping for floating tables (from w:tblpPr distances). */
wrap?: TableWrap;
};
export type BoxSpacing = {
top?: number;
right?: number;
bottom?: number;
left?: number;
};
export type PageMargins = {
top?: number;
right?: number;
bottom?: number;
left?: number;
header?: number;
footer?: number;
gutter?: number;
};
export type ImageBlockAttrs = {
sdt?: SdtMetadata;
containerSdt?: SdtMetadata;
[key: string]: unknown;
};
export type ImageBlock = {
kind: 'image';
id: BlockId;
src: string;
width?: number;
height?: number;
alt?: string;
title?: string;
objectFit?: 'contain' | 'cover' | 'fill' | 'scale-down';
display?: 'inline' | 'block';
padding?: BoxSpacing;
margin?: BoxSpacing;
anchor?: ImageAnchor;
wrap?: ImageWrap;
/** Stacking order from OOXML relativeHeight (same formula as editor: Math.max(0, relativeHeight - OOXML_Z_INDEX_BASE)) */
zIndex?: number;
attrs?: ImageBlockAttrs;
// VML image adjustments for watermark effects
gain?: string | number; // Brightness/washout (VML hex string or number)
blacklevel?: string | number; // Contrast adjustment (VML hex string or number)
// OOXML image effects
grayscale?: boolean; // Apply grayscale filter to image
lum?: ImageLuminanceAdjustment; // DrawingML luminance adjustment from a:lum
// Image transformations from OOXML a:xfrm (applies to both inline and anchored images)
rotation?: number; // Rotation angle in degrees
flipH?: boolean; // Horizontal flip
flipV?: boolean; // Vertical flip
};
export type DrawingKind = 'image' | 'vectorShape' | 'shapeGroup' | 'chart';
export type DrawingContentSnapshot = {
name: string;
attributes?: Record<string, unknown>;
elements?: unknown[];
};
export type DrawingGeometry = {
width: number;
height: number;
rotation?: number;
flipH?: boolean;
flipV?: boolean;
};
export type PositionedDrawingGeometry = DrawingGeometry & {
x?: number;
y?: number;
};
/** Gradient stop for gradient fills. Defines a color at a specific position along the gradient. */
export type GradientStop = {
/** Position along the gradient (0-1 range, where 0 is start and 1 is end). */
position: number;
/** Hex color code (e.g., "#FF0000"). */
color: string;
/** Optional alpha/opacity value (0-1 range). */
alpha?: number;
};
/** Gradient fill configuration for linear or radial gradients. */
export type GradientFill = {
type: 'gradient';
/** Type of gradient: linear (directional) or radial (circular). */
gradientType: 'linear' | 'radial';
/** Array of color stops defining the gradient. */
stops: GradientStop[];
/** Angle in degrees for linear gradients (0 = left to right, 90 = bottom to top). */
angle: number;
/** Path descriptor for radial gradients (e.g., 'circle'). */
path?: string;
};
/** Solid fill with alpha transparency. */
export type SolidFillWithAlpha = {
type: 'solidWithAlpha';
/** Hex color code. */
color: string;
/** Alpha/opacity value (0-1 range, where 0 is fully transparent and 1 is fully opaque). */
alpha: number;
};
/**
* Fill color for shapes. Can be:
* - string: Simple hex color (e.g., "#FF0000") for backward compatibility
* - GradientFill: Linear or radial gradient
* - SolidFillWithAlpha: Solid color with transparency
* - null: No fill
*/
export type FillColor = string | GradientFill | SolidFillWithAlpha | null;
/**
* Stroke color for shapes. Can be:
* - string: Hex color (e.g., "#000000")
* - null: Explicitly no border/stroke
*/
export type StrokeColor = string | null;
/** Text formatting options for shape text content. */
export type TextFormatting = {
bold?: boolean;
italic?: boolean;
color?: string;
fontSize?: number;
fontFamily?: string;
letterSpacing?: number;
};
/** A single text part with optional formatting. */
export type TextPart = {
text: string;
formatting?: TextFormatting;
/** Optional field token (e.g., PAGE/NUMPAGES) resolved at render time. */
fieldType?: 'PAGE' | 'NUMPAGES';
/** Indicates this part represents a line break between paragraphs. */
isLineBreak?: boolean;
/** Indicates this line break follows an empty paragraph (creates extra spacing). */
isEmptyParagraph?: boolean;
};
/** Text content configuration for shapes. */
export type ShapeTextContent = {
/** Array of text parts with individual formatting. */
parts: TextPart[];
/** Horizontal text alignment within the shape. */
horizontalAlign?: 'left' | 'center' | 'right';
};
export type LineEnd = {
type?: string;
width?: string;
length?: string;
};
export type LineEnds = {
head?: LineEnd;
tail?: LineEnd;
};
export type EffectExtent = {
left: number;
top: number;
right: number;
bottom: number;
};
export type VectorShapeStyle = {
fillColor?: FillColor;
strokeColor?: StrokeColor;
strokeWidth?: number;
lineEnds?: LineEnds;
textContent?: ShapeTextContent;
textAlign?: string;
textVerticalAlign?: 'top' | 'center' | 'bottom';
textInsets?: {
top: number;
right: number;
bottom: number;
left: number;
};
};
export type ShapeGroupTransform = {
x?: number;
y?: number;
width?: number;
height?: number;
childX?: number;
childY?: number;
childWidth?: number;
childHeight?: number;
childOriginXEmu?: number;
childOriginYEmu?: number;
};
export type ShapeGroupVectorChild = {
shapeType: 'vectorShape';
attrs: PositionedDrawingGeometry &
VectorShapeStyle & {
kind?: string;
customGeometry?: CustomGeometryData;
shapeId?: string;
shapeName?: string;
};
};
export type ShapeGroupImageChild = {
shapeType: 'image';
attrs: PositionedDrawingGeometry & {
src: string;
alt?: string;
clipPath?: string;
imageId?: string;
imageName?: string;
};
};
export type ShapeGroupUnknownChild = {
shapeType: string;
attrs: Record<string, unknown>;
};
export type ShapeGroupChild = ShapeGroupVectorChild | ShapeGroupImageChild | ShapeGroupUnknownChild;
export type DrawingBlockBase = {
kind: 'drawing';
id: BlockId;
drawingKind: DrawingKind;
margin?: BoxSpacing;
padding?: BoxSpacing;
anchor?: ImageAnchor;
wrap?: ImageWrap;
zIndex?: number;
drawingContentId?: string;
drawingContent?: DrawingContentSnapshot;
attrs?: Record<string, unknown>;
};
/**
* Custom geometry path data extracted from a:custGeom/a:pathLst.
* Each path has an SVG `d` attribute and its own coordinate space (w × h).
*/
export type CustomGeometryData = {
paths: Array<{
/** SVG path d attribute (M, L, C, Q, Z commands) */
d: string;
/** Coordinate space width for this path */
w: number;
/** Coordinate space height for this path */
h: number;
}>;
};
export type VectorShapeDrawing = DrawingBlockBase & {
drawingKind: 'vectorShape';
geometry: DrawingGeometry;
shapeKind?: string;
customGeometry?: CustomGeometryData;
fillColor?: FillColor;
strokeColor?: StrokeColor;
strokeWidth?: number;
lineEnds?: LineEnds;
effectExtent?: EffectExtent;
textContent?: ShapeTextContent;
textAlign?: string;
textVerticalAlign?: 'top' | 'center' | 'bottom';
textInsets?: {
top: number;
right: number;
bottom: number;
left: number;
};
};
export type ShapeGroupDrawing = DrawingBlockBase & {
drawingKind: 'shapeGroup';
geometry: DrawingGeometry;
groupTransform?: ShapeGroupTransform;
shapes: ShapeGroupChild[];
size?: {
width?: number;
height?: number;
};
};
export type ImageDrawing = DrawingBlockBase &
Omit<ImageBlock, 'kind' | 'id' | 'margin' | 'padding' | 'anchor' | 'wrap'> & {
drawingKind: 'image';
};
// ============================================================================
// Chart Drawing Types
// ============================================================================
/** A single data series in a chart (e.g., one set of bars in a bar chart). */
export type ChartSeriesData = {
/** Display name for the series (from c:tx). */
name: string;
/** Category labels (from c:cat / c:strCache). */
categories: string[];
/** Numeric values (from c:val / c:numCache). */
values: number[];
/** Optional X-axis values for XY charts (scatter/bubble). */
xValues?: number[];
/** Optional bubble radius/size values for bubble charts. */
bubbleSizes?: number[];
};
/** Axis configuration extracted from c:catAx / c:valAx. */
export type ChartAxisConfig = {
title?: string;
orientation?: 'minMax' | 'maxMin';
};
/** Normalized chart data model parsed from OOXML chart XML. */
export type ChartModel = {
/** OOXML chart element name (e.g., 'barChart', 'lineChart', 'pieChart'). */
chartType: string;
/** Sub-type qualifier (e.g., 'clustered', 'stacked', 'percentStacked'). */
subType?: string;
/** Bar direction — 'col' for vertical columns, 'bar' for horizontal bars. */
barDirection?: 'col' | 'bar';
/** Data series in the chart. */
series: ChartSeriesData[];
/** Category axis config. */
categoryAxis?: ChartAxisConfig;
/** Value axis config. */
valueAxis?: ChartAxisConfig;
/** Legend position (e.g., 'r', 'b', 't', 'l'). */
legendPosition?: string;
/** OOXML chart style ID. */
styleId?: number;
};
/** Chart drawing block. */
export type ChartDrawing = DrawingBlockBase & {
drawingKind: 'chart';
geometry: DrawingGeometry;
/** Parsed chart data for rendering. */
chartData: ChartModel;
/** Relationship ID for the chart part in the docx package. */
chartRelId?: string;
/** Path to the chart XML part (e.g., 'word/charts/chart1.xml'). */
chartPartPath?: string;
};
export type DrawingBlock = VectorShapeDrawing | ShapeGroupDrawing | ImageDrawing | ChartDrawing;
/**
* Vertical alignment of content within a section/page.
* Maps to OOXML w:vAlign values in sectPr.
*/
export type SectionVerticalAlign = 'top' | 'center' | 'bottom' | 'both';
export type SectionBreakBlock = {
kind: 'sectionBreak';
id: BlockId;
type?: 'continuous' | 'nextPage' | 'evenPage' | 'oddPage';
pageSize?: { w: number; h: number };
orientation?: 'portrait' | 'landscape';
margins: {
/** Header margin (distance from top of page to header content) */
header?: number;
/** Footer margin (distance from bottom of page to footer content) */
footer?: number;
/** Top page margin (distance from top of page to body content) */
top?: number;
/** Right page margin */
right?: number;
/** Bottom page margin */
bottom?: number;
/** Left page margin */
left?: number;
};
numbering?: {
format?: 'decimal' | 'lowerLetter' | 'upperLetter' | 'lowerRoman' | 'upperRoman' | 'numberInDash';
start?: number;
};
headerRefs?: {
default?: string;
first?: string;
even?: string;
odd?: string;
};
footerRefs?: {
default?: string;
first?: string;
even?: string;
odd?: string;
};
columns?: {
count: number;
gap: number;
widths?: number[];
equalWidth?: boolean;
};
/**
* Vertical alignment of content within the section's pages.
* - 'top': Content starts at top margin (default behavior)
* - 'center': Content is vertically centered between margins
* - 'bottom': Content is aligned to bottom margin
* - 'both': Content is vertically justified (distributed)
*/
vAlign?: SectionVerticalAlign;
attrs?: {
source?: string;
requirePageBoundary?: boolean;
[key: string]: unknown;
};
};