@blocknote/xl-docx-exporter 0.24.2 → 0.25.1

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.
@@ -1,8 +1,15 @@
1
- import { Exporter, InlineContentSchema, TableContent } from "@blocknote/core";
1
+ import {
2
+ Exporter,
3
+ InlineContentSchema,
4
+ mapTableCell,
5
+ TableContent,
6
+ UnreachableCaseError,
7
+ } from "@blocknote/core";
2
8
  import {
3
9
  Table as DocxTable,
4
10
  Paragraph,
5
11
  ParagraphChild,
12
+ ShadingType,
6
13
  TableCell,
7
14
  TableRow,
8
15
  } from "docx";
@@ -12,32 +19,84 @@ export const Table = (
12
19
  t: Exporter<any, any, any, any, ParagraphChild, any, any>
13
20
  ) => {
14
21
  const DEFAULT_COLUMN_WIDTH = 120;
22
+
23
+ // If headerRows is 1, then the first row is a header row
24
+ const headerRows = new Array(data.headerRows ?? 0).fill(true);
25
+ // If headerCols is 1, then the first column is a header column
26
+ const headerCols = new Array(data.headerCols ?? 0).fill(true);
27
+
15
28
  return new DocxTable({
16
29
  layout: "autofit",
17
30
  columnWidths: data.columnWidths.map(
18
31
  (w) =>
19
32
  (w ?? DEFAULT_COLUMN_WIDTH) * /* to points */ 0.75 * /* to twips */ 20
20
33
  ),
21
- rows: data.rows.map(
22
- (row) =>
23
- new TableRow({
24
- children: row.cells.map((cell, i) => {
25
- const width = data.columnWidths?.[i];
26
- return new TableCell({
27
- width: width
28
- ? {
29
- size: `${width * 0.75}pt`,
30
- type: "dxa",
31
- }
32
- : undefined,
33
- children: [
34
- new Paragraph({
35
- children: t.transformInlineContent(cell),
36
- }),
37
- ],
38
- });
39
- }),
40
- })
41
- ),
34
+ rows: data.rows.map((row, rowIndex) => {
35
+ const isHeaderRow = headerRows[rowIndex];
36
+ return new TableRow({
37
+ tableHeader: isHeaderRow,
38
+ children: row.cells.map((c, colIndex) => {
39
+ const width = data.columnWidths?.[colIndex];
40
+ const cell = mapTableCell(c);
41
+ const isHeaderColumn = headerCols[colIndex];
42
+
43
+ return new TableCell({
44
+ width: width
45
+ ? {
46
+ size: `${width * 0.75}pt`,
47
+ type: "dxa",
48
+ }
49
+ : undefined,
50
+ columnSpan: cell.props.colspan,
51
+ rowSpan: cell.props.rowspan,
52
+ shading:
53
+ cell.props.backgroundColor === "default" ||
54
+ !cell.props.backgroundColor
55
+ ? undefined
56
+ : {
57
+ type: ShadingType.SOLID,
58
+ color:
59
+ t.options.colors[
60
+ cell.props
61
+ .backgroundColor as keyof typeof t.options.colors
62
+ ].background.slice(1),
63
+ },
64
+ children: [
65
+ new Paragraph({
66
+ children: t.transformInlineContent(cell.content),
67
+
68
+ alignment:
69
+ !cell.props.textAlignment ||
70
+ cell.props.textAlignment === "left"
71
+ ? undefined
72
+ : cell.props.textAlignment === "center"
73
+ ? "center"
74
+ : cell.props.textAlignment === "right"
75
+ ? "right"
76
+ : cell.props.textAlignment === "justify"
77
+ ? "distribute"
78
+ : (() => {
79
+ throw new UnreachableCaseError(
80
+ cell.props.textAlignment
81
+ );
82
+ })(),
83
+ run: {
84
+ // TODO add support for table headers exporting, bolding seems to not be working at the moment
85
+ bold: isHeaderRow || isHeaderColumn,
86
+ // TODO table paragraph color seems to not be working at the moment
87
+ // Probably because the runs are setting their own color
88
+ color:
89
+ cell.props.textColor === "default" || !cell.props.textColor
90
+ ? undefined
91
+ : t.options.colors[
92
+ cell.props.textColor as keyof typeof t.options.colors
93
+ ].text.slice(1),
94
+ },
95
+ }),
96
+ ],
97
+ });
98
+ }),
99
+ });
100
+ }),
42
101
  });
43
102
  };