@folijs/core 0.4.0 → 0.6.0

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 (153) hide show
  1. package/CHANGELOG.md +49 -0
  2. package/dist/model/schema.d.ts +9 -1
  3. package/dist/model/schema.d.ts.map +1 -1
  4. package/dist/model/schema.js +13 -3
  5. package/dist/model/schema.js.map +1 -1
  6. package/dist/state/commands.d.ts +10 -1
  7. package/dist/state/commands.d.ts.map +1 -1
  8. package/dist/state/commands.js +125 -40
  9. package/dist/state/commands.js.map +1 -1
  10. package/dist/state/comments.d.ts.map +1 -1
  11. package/dist/state/comments.js +15 -6
  12. package/dist/state/comments.js.map +1 -1
  13. package/dist/state/controls.d.ts +128 -0
  14. package/dist/state/controls.d.ts.map +1 -0
  15. package/dist/state/controls.js +1459 -0
  16. package/dist/state/controls.js.map +1 -0
  17. package/dist/state/editor.d.ts +86 -1
  18. package/dist/state/editor.d.ts.map +1 -1
  19. package/dist/state/editor.js +169 -41
  20. package/dist/state/editor.js.map +1 -1
  21. package/dist/state/fields.d.ts +79 -0
  22. package/dist/state/fields.d.ts.map +1 -0
  23. package/dist/state/fields.js +398 -0
  24. package/dist/state/fields.js.map +1 -0
  25. package/dist/state/fieldupdate.d.ts +178 -0
  26. package/dist/state/fieldupdate.d.ts.map +1 -0
  27. package/dist/state/fieldupdate.js +1574 -0
  28. package/dist/state/fieldupdate.js.map +1 -0
  29. package/dist/state/find.d.ts +68 -0
  30. package/dist/state/find.d.ts.map +1 -0
  31. package/dist/state/find.js +397 -0
  32. package/dist/state/find.js.map +1 -0
  33. package/dist/state/history.d.ts +12 -1
  34. package/dist/state/history.d.ts.map +1 -1
  35. package/dist/state/history.js +43 -7
  36. package/dist/state/history.js.map +1 -1
  37. package/dist/state/index.d.ts +15 -5
  38. package/dist/state/index.d.ts.map +1 -1
  39. package/dist/state/index.js +13 -3
  40. package/dist/state/index.js.map +1 -1
  41. package/dist/state/links.d.ts.map +1 -1
  42. package/dist/state/links.js +30 -19
  43. package/dist/state/links.js.map +1 -1
  44. package/dist/state/mark.d.ts.map +1 -1
  45. package/dist/state/mark.js +8 -1
  46. package/dist/state/mark.js.map +1 -1
  47. package/dist/state/navigation.d.ts +70 -0
  48. package/dist/state/navigation.d.ts.map +1 -0
  49. package/dist/state/navigation.js +226 -0
  50. package/dist/state/navigation.js.map +1 -0
  51. package/dist/state/notes.d.ts +94 -0
  52. package/dist/state/notes.d.ts.map +1 -0
  53. package/dist/state/notes.js +444 -0
  54. package/dist/state/notes.js.map +1 -0
  55. package/dist/state/pictures.d.ts +51 -0
  56. package/dist/state/pictures.d.ts.map +1 -0
  57. package/dist/state/pictures.js +140 -0
  58. package/dist/state/pictures.js.map +1 -0
  59. package/dist/state/review.d.ts +13 -4
  60. package/dist/state/review.d.ts.map +1 -1
  61. package/dist/state/review.js +223 -29
  62. package/dist/state/review.js.map +1 -1
  63. package/dist/state/sections.d.ts +101 -0
  64. package/dist/state/sections.d.ts.map +1 -0
  65. package/dist/state/sections.js +472 -0
  66. package/dist/state/sections.js.map +1 -0
  67. package/dist/state/selection.d.ts +12 -1
  68. package/dist/state/selection.d.ts.map +1 -1
  69. package/dist/state/selection.js +12 -0
  70. package/dist/state/selection.js.map +1 -1
  71. package/dist/state/state.d.ts +2 -0
  72. package/dist/state/state.d.ts.map +1 -1
  73. package/dist/state/state.js +4 -0
  74. package/dist/state/state.js.map +1 -1
  75. package/dist/state/stories.d.ts +41 -0
  76. package/dist/state/stories.d.ts.map +1 -0
  77. package/dist/state/stories.js +243 -0
  78. package/dist/state/stories.js.map +1 -0
  79. package/dist/state/tablecommands.d.ts +77 -0
  80. package/dist/state/tablecommands.d.ts.map +1 -0
  81. package/dist/state/tablecommands.js +675 -0
  82. package/dist/state/tablecommands.js.map +1 -0
  83. package/dist/state/tables.d.ts +110 -0
  84. package/dist/state/tables.d.ts.map +1 -0
  85. package/dist/state/tables.js +730 -0
  86. package/dist/state/tables.js.map +1 -0
  87. package/dist/state/track.d.ts +23 -0
  88. package/dist/state/track.d.ts.map +1 -1
  89. package/dist/state/track.js +113 -15
  90. package/dist/state/track.js.map +1 -1
  91. package/dist/state/word.d.ts +78 -16
  92. package/dist/state/word.d.ts.map +1 -1
  93. package/dist/state/word.js +231 -11
  94. package/dist/state/word.js.map +1 -1
  95. package/dist/state/wordclip.d.ts +15 -6
  96. package/dist/state/wordclip.d.ts.map +1 -1
  97. package/dist/state/wordclip.js +213 -18
  98. package/dist/state/wordclip.js.map +1 -1
  99. package/dist/state/wordformat.d.ts +10 -2
  100. package/dist/state/wordformat.d.ts.map +1 -1
  101. package/dist/state/wordformat.js +98 -17
  102. package/dist/state/wordformat.js.map +1 -1
  103. package/dist/v3/document.d.ts +237 -7
  104. package/dist/v3/document.d.ts.map +1 -1
  105. package/dist/v3/document.js +314 -15
  106. package/dist/v3/document.js.map +1 -1
  107. package/dist/v3/index.d.ts +2 -2
  108. package/dist/v3/index.d.ts.map +1 -1
  109. package/dist/v3/index.js +2 -2
  110. package/dist/v3/index.js.map +1 -1
  111. package/dist/v3/migrate.d.ts +2 -2
  112. package/dist/v3/migrate.d.ts.map +1 -1
  113. package/dist/v3/resolve.d.ts +57 -5
  114. package/dist/v3/resolve.d.ts.map +1 -1
  115. package/dist/v3/resolve.js +224 -16
  116. package/dist/v3/resolve.js.map +1 -1
  117. package/dist/v3/schema.d.ts +6 -0
  118. package/dist/v3/schema.d.ts.map +1 -1
  119. package/dist/v3/schema.js +254 -8
  120. package/dist/v3/schema.js.map +1 -1
  121. package/package.json +1 -1
  122. package/src/model/schema.ts +18 -2
  123. package/src/state/commands.ts +121 -32
  124. package/src/state/comments.ts +14 -6
  125. package/src/state/controls.ts +1596 -0
  126. package/src/state/editor.ts +292 -46
  127. package/src/state/fields.ts +432 -0
  128. package/src/state/fieldupdate.ts +1770 -0
  129. package/src/state/find.ts +510 -0
  130. package/src/state/history.ts +51 -7
  131. package/src/state/index.ts +157 -0
  132. package/src/state/links.ts +27 -17
  133. package/src/state/mark.ts +8 -1
  134. package/src/state/navigation.ts +305 -0
  135. package/src/state/notes.ts +519 -0
  136. package/src/state/numeric-pictures.word.json +172 -0
  137. package/src/state/pictures.ts +168 -0
  138. package/src/state/review.ts +265 -31
  139. package/src/state/sections.ts +571 -0
  140. package/src/state/selection.ts +15 -1
  141. package/src/state/state.ts +5 -0
  142. package/src/state/stories.ts +285 -0
  143. package/src/state/tablecommands.ts +741 -0
  144. package/src/state/tables.ts +761 -0
  145. package/src/state/track.ts +128 -16
  146. package/src/state/word.ts +276 -13
  147. package/src/state/wordclip.ts +231 -32
  148. package/src/state/wordformat.ts +96 -14
  149. package/src/v3/document.ts +572 -16
  150. package/src/v3/index.ts +21 -0
  151. package/src/v3/migrate.ts +2 -2
  152. package/src/v3/resolve.ts +289 -21
  153. package/src/v3/schema.ts +278 -9
package/src/v3/index.ts CHANGED
@@ -1,26 +1,47 @@
1
1
  export {
2
2
  type AbstractNumberingJSON,
3
3
  type BorderJSON,
4
+ type CellPropertiesJSON,
4
5
  type CommentJSON,
6
+ type ControlJSON,
7
+ checkControl,
5
8
  createDocument,
6
9
  type FoliDocument,
10
+ type FoliDocumentV3,
11
+ type FoliDocumentV4,
12
+ type MeasureJSON,
13
+ migrateToV4,
14
+ migrateToV5,
15
+ type NotePropertiesJSON,
7
16
  type NumberingInstanceJSON,
8
17
  type NumberingLevelJSON,
9
18
  type ParagraphPropertiesJSON,
10
19
  parseContent,
11
20
  parseDocument,
21
+ type ResourceJSON,
22
+ type RowPropertiesJSON,
12
23
  SCHEMA_VERSION,
13
24
  type SectionJSON,
25
+ type StoryJSON,
14
26
  type StyleJSON,
27
+ TABLE_CONDITIONS,
28
+ type TableCondition,
29
+ type TableConditionJSON,
30
+ type TablePropertiesJSON,
15
31
  type ThemeJSON,
32
+ type WordSettingsJSON,
16
33
  } from "./document.js";
17
34
  export { migrateToV3 } from "./migrate.js";
18
35
  export {
36
+ type CellFormat,
37
+ type CellPlace,
19
38
  formatNumber,
20
39
  type ListMarker,
21
40
  type ParagraphFormat,
22
41
  Resolver,
23
42
  type RunFormat,
43
+ type TableFormat,
44
+ tableConditions,
24
45
  } from "./resolve.js";
25
46
  export {
26
47
  checkSection,
package/src/v3/migrate.ts CHANGED
@@ -3,7 +3,7 @@ import type { MarkJSON, NodeJSON } from "../model/index.js";
3
3
  import type { BlockJSON, InlineJSON, MarkNameJSON } from "../schema.js";
4
4
  import type {
5
5
  AbstractNumberingJSON,
6
- FoliDocument,
6
+ FoliDocumentV3,
7
7
  NumberingInstanceJSON,
8
8
  SectionJSON,
9
9
  StyleJSON,
@@ -25,7 +25,7 @@ import { mmToTwips } from "./units.js";
25
25
  * - `settings` becomes the section, in twips (rounded to the twip: under 0.02 mm).
26
26
  * The styles and numbering definitions the result references are added.
27
27
  */
28
- export function migrateToV3(doc: FoliDocumentV2): FoliDocument {
28
+ export function migrateToV3(doc: FoliDocumentV2): FoliDocumentV3 {
29
29
  const instances: NumberingInstanceJSON[] = [];
30
30
  const content = blocks(doc.content.content, { instances });
31
31
  return {
package/src/v3/resolve.ts CHANGED
@@ -7,11 +7,16 @@
7
7
  import type { MarkJSON } from "../model/index.js";
8
8
  import type {
9
9
  AbstractNumberingJSON,
10
+ BorderJSON,
11
+ CellPropertiesJSON,
10
12
  FoliDocument,
13
+ MeasureJSON,
11
14
  NumberingInstanceJSON,
12
15
  NumberingLevelJSON,
13
16
  ParagraphPropertiesJSON,
14
17
  StyleJSON,
18
+ TableCondition,
19
+ TablePropertiesJSON,
15
20
  ThemeJSON,
16
21
  } from "./document.js";
17
22
 
@@ -79,12 +84,16 @@ export interface ParagraphFormat {
79
84
  export interface ListMarker {
80
85
  /** As Word shows it ("1.", "1.1", "(a)", or a bullet's character as the file has it). */
81
86
  text: string;
87
+ /** The same without the words of its format: "Section 1." is "1." (`REF \\t`). */
88
+ plain: string;
82
89
  /** What follows the marker. */
83
90
  suffix: "tab" | "space" | "nothing";
84
91
  justification: string;
85
92
  /** The marker's formatting: the paragraph's, its mark's, then the level's run properties. */
86
93
  run: RunFormat;
87
94
  instance: number;
95
+ /** The definition counted: instances sharing one go on from each other. */
96
+ abstract: number;
88
97
  level: number;
89
98
  }
90
99
 
@@ -366,11 +375,22 @@ export function formatNumber(n: number, format: string | undefined): string {
366
375
  return letters(n).toLowerCase();
367
376
  case "ordinal":
368
377
  return ordinal(n);
378
+ case "chicago":
379
+ return chicago(n);
380
+ case "numberInDash":
381
+ return `- ${n} -`;
369
382
  default:
370
383
  return String(n);
371
384
  }
372
385
  }
373
386
 
387
+ /** Word's `chicago` marks: `*`, `†`, `‡`, `§`, then the same doubled, tripled... */
388
+ function chicago(n: number): string {
389
+ const marks = ["*", "†", "‡", "§"];
390
+ if (n < 1) return String(n);
391
+ return (marks[(n - 1) % 4] as string).repeat(Math.ceil(n / 4));
392
+ }
393
+
374
394
  const ROMAN: [number, string][] = [
375
395
  [1000, "M"],
376
396
  [900, "CM"],
@@ -411,7 +431,123 @@ function ordinal(n: number): string {
411
431
  return `${n}${suffix}`;
412
432
  }
413
433
 
414
- type Envelope = Pick<FoliDocument, "defaults" | "styles" | "numbering" | "theme">;
434
+ /** A table's effective properties: its style's (the default table style's when it names none), then its own. */
435
+ export type TableFormat = Omit<TablePropertiesJSON, "style" | "ext"> & {
436
+ /** The table style that applies. */
437
+ style: string | null;
438
+ };
439
+
440
+ /** Where a cell is: its row among the table's rows, and its index among the row's cells. */
441
+ export interface CellPlace {
442
+ row: number;
443
+ rows: number;
444
+ cell: number;
445
+ cells: number;
446
+ }
447
+
448
+ type Side = "top" | "bottom" | "left" | "right";
449
+
450
+ /** A cell's effective formatting from the table style's parts that apply to it and its own properties. */
451
+ export interface CellFormat {
452
+ /** The table style that applies. */
453
+ style: string | null;
454
+ /** The table style's parts that apply, in the order they apply (later wins). */
455
+ conditions: TableCondition[];
456
+ /** Fill `RRGGBB`; null for none. */
457
+ shading: string | null;
458
+ /** The cell's own edges before conflicts with its neighbours are resolved (left-to-right text). */
459
+ borders: Partial<Record<Side, BorderJSON>>;
460
+ /** Twips. */
461
+ margins: Record<Side, number>;
462
+ verticalAlign: "top" | "center" | "bottom" | "both";
463
+ }
464
+
465
+ /** A table style's parts for a set of conditions, flattened in the order they apply. */
466
+ interface TableLayer {
467
+ key: string;
468
+ paragraph: ParagraphPropertiesJSON[];
469
+ run: MarkJSON[][];
470
+ cell: CellPropertiesJSON[];
471
+ }
472
+
473
+ const TABLE_KEYS = new Set([
474
+ "width",
475
+ "justification",
476
+ "indent",
477
+ "layout",
478
+ "borders",
479
+ "cellMargins",
480
+ "cellSpacing",
481
+ "shading",
482
+ "look",
483
+ "rowBandSize",
484
+ "columnBandSize",
485
+ ]);
486
+ const PER_SIDE = new Set(["borders", "cellMargins", "look"]);
487
+
488
+ /** Table properties merged in order, later winning (borders and margins side by side). */
489
+ function mergeTable(target: Json, props: Json | undefined | null): void {
490
+ if (!props) return;
491
+ for (const [key, value] of Object.entries(props)) {
492
+ if (value == null || !TABLE_KEYS.has(key)) continue;
493
+ target[key] = PER_SIDE.has(key) ? { ...(target[key] as Json), ...(value as Json) } : value;
494
+ }
495
+ }
496
+
497
+ /** A cell margin in twips: Word measures margins in `dxa` (other types count as none). */
498
+ function twipsOf(measure: MeasureJSON | undefined): number | undefined {
499
+ if (!measure) return undefined;
500
+ return measure.type === "dxa" ? measure.value : 0;
501
+ }
502
+
503
+ /** The last value `pick` gives among `layers` (later layers win). */
504
+ function last<T, V>(layers: readonly T[], pick: (layer: T) => V | undefined | null): V | undefined {
505
+ for (let i = layers.length - 1; i >= 0; i--) {
506
+ const value = pick(layers[i] as T);
507
+ if (value != null) return value;
508
+ }
509
+ return undefined;
510
+ }
511
+
512
+ /**
513
+ * The table style's parts that apply to a cell, in Word's order: the whole table, banded
514
+ * columns, banded rows, first and last column, first and last row, then the corners. The first
515
+ * and last row and column (when `look` turns them on) are left out of banding.
516
+ *
517
+ * shortcut: banding and the first and last column count cells, not grid columns; not checked
518
+ * against Word's drawing of a table with merged cells or rows starting late (`gridBefore`).
519
+ */
520
+ export function tableConditions(
521
+ look: NonNullable<TablePropertiesJSON["look"]>,
522
+ place: CellPlace,
523
+ rowBand = 1,
524
+ columnBand = 1,
525
+ ): TableCondition[] {
526
+ const out: TableCondition[] = ["wholeTable"];
527
+ const firstRow = !!look.firstRow && place.row === 0;
528
+ const lastRow = !!look.lastRow && place.row === place.rows - 1;
529
+ const firstCol = !!look.firstColumn && place.cell === 0;
530
+ const lastCol = !!look.lastColumn && place.cell === place.cells - 1;
531
+ if (!look.noVBand && !firstCol && !lastCol) {
532
+ const i = place.cell - (look.firstColumn ? 1 : 0);
533
+ out.push(Math.floor(i / columnBand) % 2 === 0 ? "band1Vert" : "band2Vert");
534
+ }
535
+ if (!look.noHBand && !firstRow && !lastRow) {
536
+ const i = place.row - (look.firstRow ? 1 : 0);
537
+ out.push(Math.floor(i / rowBand) % 2 === 0 ? "band1Horz" : "band2Horz");
538
+ }
539
+ if (firstCol) out.push("firstCol");
540
+ if (lastCol) out.push("lastCol");
541
+ if (firstRow) out.push("firstRow");
542
+ if (lastRow) out.push("lastRow");
543
+ if (firstRow && firstCol) out.push("nwCell");
544
+ if (firstRow && lastCol) out.push("neCell");
545
+ if (lastRow && firstCol) out.push("swCell");
546
+ if (lastRow && lastCol) out.push("seCell");
547
+ return out;
548
+ }
549
+
550
+ type Envelope = Pick<FoliDocument, "defaults" | "styles" | "numbering" | "theme" | "settings">;
415
551
 
416
552
  interface Level {
417
553
  instance: NumberingInstanceJSON;
@@ -435,14 +571,23 @@ export class Resolver {
435
571
  readonly #characterStyles = new Map<string, MarkJSON[][]>();
436
572
  readonly #instances = new Map<number, NumberingInstanceJSON>();
437
573
  readonly #abstracts = new Map<number, AbstractNumberingJSON>();
574
+ #defaultTableStyle: string | undefined;
575
+ readonly #layers = new Map<string, TableLayer>();
576
+
577
+ /** Whether a paragraph's space after and the next one's space before add up (else: the larger). */
578
+ get addsParagraphSpacing(): boolean {
579
+ return this.#envelope.settings?.doNotUseHTMLParagraphAutoSpacing === true;
580
+ }
438
581
 
439
582
  constructor(envelope: Envelope) {
440
583
  this.#envelope = envelope;
441
584
  // With duplicate ids, the first wins, as in Word.
442
585
  for (const s of envelope.styles) if (!this.#styles.has(s.id)) this.#styles.set(s.id, s);
443
- // With several default paragraph styles, the last one applies (ECMA-376 17.7.4.17).
444
- for (const s of envelope.styles)
586
+ // With several default styles of a type, the last one applies (ECMA-376 17.7.4.17).
587
+ for (const s of envelope.styles) {
445
588
  if (s.type === "paragraph" && s.isDefault) this.#defaultParagraphStyle = s.id;
589
+ if (s.type === "table" && s.isDefault) this.#defaultTableStyle = s.id;
590
+ }
446
591
  for (const n of envelope.numbering.instances)
447
592
  if (!this.#instances.has(n.id)) this.#instances.set(n.id, n);
448
593
  for (const a of envelope.numbering.abstracts)
@@ -463,16 +608,120 @@ export class Resolver {
463
608
  return out;
464
609
  }
465
610
 
466
- /** A paragraph style's formatting over the defaults, memoized. */
467
- #paragraphStyle(id: string | undefined) {
468
- const key = id ?? "";
611
+ /** A table style and those it is based on (the default table style for a missing one). */
612
+ #tableChain(id: string | null | undefined): StyleJSON[] {
613
+ const chain = id ? this.#chain(id, "table") : [];
614
+ return chain.length ? chain : this.#chain(this.#defaultTableStyle, "table");
615
+ }
616
+
617
+ /** The table style's parts for `conditions`, memoized. */
618
+ #layer(style: string | null, conditions: readonly TableCondition[]): TableLayer {
619
+ // JSON: no two style ids and condition lists give the same key.
620
+ const key = JSON.stringify([style, conditions]);
621
+ const known = this.#layers.get(key);
622
+ if (known) return known;
623
+ const layer: TableLayer = { key, paragraph: [], run: [], cell: [] };
624
+ const chain = this.#tableChain(style);
625
+ for (const condition of conditions) {
626
+ for (const s of chain) {
627
+ const part =
628
+ condition === "wholeTable" ? s : s.conditions?.find((c) => c.type === condition);
629
+ if (part?.paragraph) layer.paragraph.push(part.paragraph);
630
+ if (part?.run) layer.run.push(part.run);
631
+ if (part?.cell) layer.cell.push(part.cell);
632
+ }
633
+ }
634
+ this.#layers.set(key, layer);
635
+ return layer;
636
+ }
637
+
638
+ /** A table's effective properties (`attrs`: the table node's attributes); the caller's to change. */
639
+ table(attrs: Json): TableFormat {
640
+ const chain = this.#tableChain(attrs.style as string | null | undefined);
641
+ const out: Json = {};
642
+ for (const s of chain) mergeTable(out, s.table as Json | undefined);
643
+ mergeTable(out, attrs);
644
+ // A copy: the merged values are the styles' and the node's own objects.
645
+ return structuredClone({
646
+ ...(out as Omit<TableFormat, "style">),
647
+ style: chain.at(-1)?.id ?? null,
648
+ });
649
+ }
650
+
651
+ /**
652
+ * A cell's effective formatting (ECMA-376 17.7.6): the table style's parts that apply where
653
+ * the cell is (`tableConditions`), the row's exceptions over the table's properties, and the
654
+ * cell's own properties (`table`, `row`, `cell`: the nodes' attributes). Pass the result to
655
+ * `paragraph` for the paragraphs in the cell. The result is the caller's to change.
656
+ */
657
+ cell(table: Json, row: Json, cell: Json, place: CellPlace): CellFormat {
658
+ const props = this.table(table) as Json;
659
+ mergeTable(props, row.exceptions as Json | null | undefined);
660
+ const format = props as TableFormat;
661
+ const conditions = tableConditions(
662
+ format.look ?? {},
663
+ place,
664
+ format.rowBandSize,
665
+ format.columnBandSize,
666
+ );
667
+ const layers = [...this.#layer(format.style, conditions).cell, cell as CellPropertiesJSON];
668
+ const fill = last(layers, (l) => l.shading)?.fill ?? format.shading?.fill;
669
+ const tableBorders = format.borders ?? {};
670
+ const edge = (
671
+ side: Side,
672
+ start: "start" | "end" | null,
673
+ inside: BorderJSON | undefined,
674
+ outer: boolean,
675
+ ) =>
676
+ last(layers, (l) => l.borders?.[side] ?? (start ? l.borders?.[start] : undefined)) ??
677
+ (outer ? (tableBorders[side] ?? (start ? tableBorders[start] : undefined)) : inside);
678
+ const borders: Partial<Record<Side, BorderJSON>> = {};
679
+ const sides: [Side, "start" | "end" | null, BorderJSON | undefined, boolean][] = [
680
+ ["top", null, tableBorders.insideH, place.row === 0],
681
+ ["bottom", null, tableBorders.insideH, place.row === place.rows - 1],
682
+ ["left", "start", tableBorders.insideV, place.cell === 0],
683
+ ["right", "end", tableBorders.insideV, place.cell === place.cells - 1],
684
+ ];
685
+ for (const [side, start, inside, outer] of sides) {
686
+ const border = edge(side, start, inside, outer);
687
+ // A copy: the border may be a style's or the node's own object.
688
+ if (border) borders[side] = { ...border };
689
+ }
690
+ const margin = (side: Side, start: "start" | "end" | null) =>
691
+ twipsOf(
692
+ last(layers, (l) => l.margins?.[side] ?? (start ? l.margins?.[start] : undefined)) ??
693
+ format.cellMargins?.[side] ??
694
+ (start ? format.cellMargins?.[start] : undefined),
695
+ ) ?? 0;
696
+ return {
697
+ style: format.style,
698
+ conditions,
699
+ shading: fill === undefined || fill === "auto" ? null : fill.toUpperCase(),
700
+ borders,
701
+ margins: {
702
+ top: margin("top", null),
703
+ bottom: margin("bottom", null),
704
+ left: margin("left", "start"),
705
+ right: margin("right", "end"),
706
+ },
707
+ verticalAlign: last(layers, (l) => l.verticalAlign) ?? "top",
708
+ };
709
+ }
710
+
711
+ /**
712
+ * A paragraph style's formatting over the defaults (and, in a table, the table style's parts
713
+ * for the cell, which come between them as ECMA-376 17.7.2 orders them), memoized.
714
+ *
715
+ * shortcut: Word lets a table style's properties win over the default paragraph style's
716
+ * (Normal) in some cases; not modeled (docs/limitations.md).
717
+ */
718
+ #paragraphStyle(id: string | undefined, layer?: TableLayer) {
719
+ const key = JSON.stringify([layer?.key ?? null, id ?? null]);
469
720
  const known = this.#paragraphStyles.get(key);
470
721
  if (known) return known;
471
- const { defaults } = this.#envelope;
472
722
  const paragraph = cloneParagraph(DEFAULT_PARAGRAPH);
473
723
  const run = cloneRun(DEFAULT_RUN);
474
- mergeParagraph(paragraph, defaults.paragraph);
475
- applyDirect(run, defaults.run, this.#envelope.theme);
724
+ this.#base(paragraph, run, layer);
476
725
  // A style that does not exist (or is not a paragraph style) is the default paragraph style.
477
726
  let chain = id === undefined ? [] : this.#chain(id, "paragraph");
478
727
  if (!chain.length) chain = this.#chain(this.#defaultParagraphStyle, "paragraph");
@@ -488,19 +737,34 @@ export class Resolver {
488
737
  return result;
489
738
  }
490
739
 
740
+ /** The document defaults, then a table style's parts for a cell. */
741
+ #base(paragraph: Merging, run: RunFormat | null, layer: TableLayer | undefined): void {
742
+ const { defaults, theme } = this.#envelope;
743
+ mergeParagraph(paragraph, defaults.paragraph);
744
+ if (run) applyDirect(run, defaults.run, theme);
745
+ for (const p of layer?.paragraph ?? []) mergeParagraph(paragraph, p);
746
+ // The table style's run properties are set, as the defaults are (not toggled).
747
+ if (run) for (const r of layer?.run ?? []) applyDirect(run, r, theme);
748
+ }
749
+
491
750
  /**
492
751
  * A paragraph's effective properties, and the formatting of its text before a run's own marks
493
- * (`attrs`: the paragraph node's attributes, as JSON or a live node has them). The results are
494
- * the caller's: changing them changes nothing else.
752
+ * (`attrs`: the paragraph node's attributes, as JSON or a live node has them; `cell`: the
753
+ * format of the table cell it is in, from `cell`). The results are the caller's: changing
754
+ * them changes nothing else.
495
755
  */
496
- paragraph(attrs: Json): { paragraph: ParagraphFormat; run: RunFormat } {
497
- const { paragraph, run } = this.#resolve(attrs);
756
+ paragraph(attrs: Json, cell?: CellFormat): { paragraph: ParagraphFormat; run: RunFormat } {
757
+ const { paragraph, run } = this.#resolve(attrs, cell);
498
758
  return { paragraph, run };
499
759
  }
500
760
 
501
- #resolve(attrs: Json): { paragraph: ParagraphFormat; run: RunFormat; level: Level | undefined } {
761
+ #resolve(
762
+ attrs: Json,
763
+ cell?: CellFormat,
764
+ ): { paragraph: ParagraphFormat; run: RunFormat; level: Level | undefined } {
502
765
  const props = attrs as ParagraphPropertiesJSON;
503
- const base = this.#paragraphStyle(props.style ?? undefined);
766
+ const layer = cell ? this.#layer(cell.style, cell.conditions) : undefined;
767
+ const base = this.#paragraphStyle(props.style ?? undefined, layer);
504
768
  let paragraph = cloneParagraph(base.paragraph);
505
769
  const directNumbering = props.numbering?.id != null;
506
770
  if (props.numbering) mergeParagraph(paragraph, { numbering: props.numbering });
@@ -512,7 +776,7 @@ export class Resolver {
512
776
  } else {
513
777
  // Numbering given by the style: the style's own indents win over the level's.
514
778
  const stacked = cloneParagraph(DEFAULT_PARAGRAPH);
515
- mergeParagraph(stacked, this.#envelope.defaults.paragraph);
779
+ this.#base(stacked, null, layer);
516
780
  mergeParagraph(stacked, level.level.paragraph);
517
781
  for (const style of base.chain) mergeParagraph(stacked, style.paragraph);
518
782
  stacked.style = paragraph.style;
@@ -625,20 +889,24 @@ export class Resolver {
625
889
  const restart = this.#definition(instance, abstract, deeper)?.restart;
626
890
  if (restart === undefined || (restart !== 0 && index < restart)) levels[deeper] = undefined;
627
891
  }
628
- const text = (level.text ?? "").replace(/%([1-9])/g, (_, d: string) => {
629
- const i = Number(d) - 1;
630
- const format = level.legal ? "decimal" : this.#definition(instance, abstract, i)?.format;
631
- return formatNumber(levels[i] ?? startOf(i), format);
632
- });
892
+ const written = (template: string) =>
893
+ template.replace(/%([1-9])/g, (_, d: string) => {
894
+ const i = Number(d) - 1;
895
+ const format = level.legal ? "decimal" : this.#definition(instance, abstract, i)?.format;
896
+ return formatNumber(levels[i] ?? startOf(i), format);
897
+ });
898
+ const text = written(level.text ?? "");
633
899
  const marker = cloneRun(run);
634
900
  applyDirect(marker, attrs.markFormat as MarkJSON[] | undefined, this.#envelope.theme);
635
901
  applyDirect(marker, level.run, this.#envelope.theme);
636
902
  return {
637
903
  text,
904
+ plain: written((level.text ?? "").replace(/\p{L}+/gu, "")).trim(),
638
905
  suffix: level.suffix ?? "tab",
639
906
  justification: level.justification ?? "left",
640
907
  run: marker,
641
908
  instance: instance.id,
909
+ abstract: abstract.id,
642
910
  level: index,
643
911
  };
644
912
  });