Skip to content

Commit 8fc2370

Browse files
authored
fix: do not error on invalid backgroundColor or textColor #2176 (#2179)
1 parent d192b37 commit 8fc2370

File tree

9 files changed

+64
-61
lines changed

9 files changed

+64
-61
lines changed

packages/xl-docx-exporter/src/docx/defaultSchema/blocks.ts

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,16 +35,25 @@ function blockPropsToStyles(
3535
? undefined
3636
: {
3737
type: ShadingType.SOLID,
38-
color:
39-
colors[
40-
props.backgroundColor as keyof typeof colors
41-
].background.slice(1),
38+
color: (() => {
39+
const color = colors[props.backgroundColor]?.background;
40+
if (!color) {
41+
return undefined;
42+
}
43+
return color.slice(1);
44+
})(),
4245
},
4346
run:
4447
props.textColor === "default" || !props.textColor
4548
? undefined
4649
: {
47-
color: colors[props.textColor as keyof typeof colors].text.slice(1),
50+
color: (() => {
51+
const color = colors[props.textColor]?.text;
52+
if (!color) {
53+
return undefined;
54+
}
55+
return color.slice(1);
56+
})(),
4857
},
4958
alignment:
5059
!props.textAlignment || props.textAlignment === "left"

packages/xl-docx-exporter/src/docx/defaultSchema/styles.ts

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -43,23 +43,26 @@ export const docxStyleMappingForDefaultSchema: StyleMapping<
4343
if (!val) {
4444
return {};
4545
}
46+
const color = exporter.options.colors[val]?.background;
47+
if (!color) {
48+
return {};
49+
}
4650
return {
4751
shading: {
48-
fill: exporter.options.colors[
49-
val as keyof typeof exporter.options.colors
50-
].background.slice(1),
52+
fill: color.slice(1),
5153
},
5254
};
5355
},
5456
textColor: (val, exporter) => {
5557
if (!val) {
5658
return {};
5759
}
60+
const color = exporter.options.colors[val]?.text;
61+
if (!color) {
62+
return {};
63+
}
5864
return {
59-
color:
60-
exporter.options.colors[
61-
val as keyof typeof exporter.options.colors
62-
].text.slice(1),
65+
color: color.slice(1),
6366
};
6467
},
6568
code: (val) => {

packages/xl-docx-exporter/src/docx/util/Table.tsx

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -55,11 +55,15 @@ export const Table = (
5555
? undefined
5656
: {
5757
type: ShadingType.SOLID,
58-
color:
59-
t.options.colors[
60-
cell.props
61-
.backgroundColor as keyof typeof t.options.colors
62-
].background.slice(1),
58+
color: (() => {
59+
const color =
60+
t.options.colors[cell.props.backgroundColor]
61+
?.background;
62+
if (!color) {
63+
return undefined;
64+
}
65+
return color.slice(1);
66+
})(),
6367
},
6468
children: [
6569
new Paragraph({
@@ -88,9 +92,14 @@ export const Table = (
8892
color:
8993
cell.props.textColor === "default" || !cell.props.textColor
9094
? undefined
91-
: t.options.colors[
92-
cell.props.textColor as keyof typeof t.options.colors
93-
].text.slice(1),
95+
: (() => {
96+
const color =
97+
t.options.colors[cell.props.textColor]?.text;
98+
if (!color) {
99+
return undefined;
100+
}
101+
return color.slice(1);
102+
})(),
94103
},
95104
}),
96105
],

packages/xl-email-exporter/src/react-email/reactEmailExporter.tsx

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -333,15 +333,11 @@ export class ReactEmailExporter<
333333
backgroundColor:
334334
props.backgroundColor === "default" || !props.backgroundColor
335335
? undefined
336-
: this.options.colors[
337-
props.backgroundColor as keyof typeof this.options.colors
338-
].background,
336+
: this.options.colors[props.backgroundColor]?.background,
339337
color:
340338
props.textColor === "default" || !props.textColor
341339
? undefined
342-
: this.options.colors[
343-
props.textColor as keyof typeof this.options.colors
344-
].text,
340+
: this.options.colors[props.textColor]?.text,
345341
alignItems:
346342
props.textAlignment === "right"
347343
? "flex-end"

packages/xl-odt-exporter/src/odt/defaultSchema/blocks.tsx

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,7 @@ const createParagraphStyle = (
4141

4242
const backgroundColor =
4343
props.backgroundColor && props.backgroundColor !== "default"
44-
? exporter.options.colors[
45-
props.backgroundColor as keyof typeof exporter.options.colors
46-
].background
44+
? exporter.options.colors[props.backgroundColor]?.background
4745
: undefined;
4846

4947
if (backgroundColor) {
@@ -54,7 +52,7 @@ const createParagraphStyle = (
5452
const color =
5553
exporter.options.colors[
5654
props.textColor as keyof typeof exporter.options.colors
57-
].text;
55+
]?.text ?? props.textColor;
5856
textStyles["fo:color"] = color;
5957
}
6058

@@ -115,18 +113,14 @@ const createTableCellStyle = (
115113
fo:background-color={
116114
cell.props.backgroundColor !== "default" &&
117115
cell.props.backgroundColor
118-
? exporter.options.colors[
119-
cell.props
120-
.backgroundColor as keyof typeof exporter.options.colors
121-
].background
116+
? exporter.options.colors?.[cell.props.backgroundColor]
117+
?.background
122118
: undefined
123119
}
124120
// TODO This is not applying because the children set their own colors
125121
fo:color={
126122
cell.props.textColor !== "default" && cell.props.textColor
127-
? exporter.options.colors[
128-
cell.props.textColor as keyof typeof exporter.options.colors
129-
].text
123+
? exporter.options.colors[cell.props.textColor]?.text
130124
: undefined
131125
}
132126
/>

packages/xl-odt-exporter/src/odt/defaultSchema/styles.ts

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,18 +35,21 @@ export const odtStyleMappingForDefaultSchema: StyleMapping<
3535
if (!val) {
3636
return {};
3737
}
38-
const color =
39-
exporter.options.colors[val as keyof typeof exporter.options.colors].text;
38+
const color = exporter.options.colors[val]?.text;
39+
if (!color) {
40+
return {};
41+
}
4042
return { "fo:color": color };
4143
},
4244

4345
backgroundColor: (val, exporter): Record<string, string> => {
4446
if (!val) {
4547
return {};
4648
}
47-
const color =
48-
exporter.options.colors[val as keyof typeof exporter.options.colors]
49-
.background;
49+
const color = exporter.options.colors[val]?.background;
50+
if (!color) {
51+
return {};
52+
}
5053
return { "fo:background-color": color };
5154
},
5255

packages/xl-pdf-exporter/src/pdf/defaultSchema/styles.tsx

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -42,19 +42,15 @@ export const pdfStyleMappingForDefaultSchema: StyleMapping<
4242
return {};
4343
}
4444
return {
45-
backgroundColor:
46-
exporter.options.colors[val as keyof typeof exporter.options.colors]
47-
.background,
45+
backgroundColor: exporter.options.colors[val]?.background,
4846
};
4947
},
5048
textColor: (val, exporter) => {
5149
if (!val) {
5250
return {};
5351
}
5452
return {
55-
color:
56-
exporter.options.colors[val as keyof typeof exporter.options.colors]
57-
.text,
53+
color: exporter.options.colors[val]?.text,
5854
};
5955
},
6056
code: (val) => {

packages/xl-pdf-exporter/src/pdf/pdfExporter.tsx

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -296,15 +296,11 @@ export class PDFExporter<
296296
backgroundColor:
297297
props.backgroundColor === "default" || !props.backgroundColor
298298
? undefined
299-
: this.options.colors[
300-
props.backgroundColor as keyof typeof this.options.colors
301-
].background,
299+
: this.options.colors[props.backgroundColor]?.background,
302300
color:
303301
props.textColor === "default" || !props.textColor
304302
? undefined
305-
: this.options.colors[
306-
props.textColor as keyof typeof this.options.colors
307-
].text,
303+
: this.options.colors[props.textColor]?.text,
308304
alignItems:
309305
props.textAlignment === "right"
310306
? "flex-end"

packages/xl-pdf-exporter/src/pdf/util/table/Table.tsx

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -88,17 +88,14 @@ export const Table = (props: {
8888
color:
8989
cell.props.textColor === "default"
9090
? undefined
91-
: props.transformer.options.colors[
92-
cell.props
93-
.textColor as keyof typeof props.transformer.options.colors
94-
].text,
91+
: props.transformer.options.colors[cell.props.textColor]
92+
?.text,
9593
backgroundColor:
9694
cell.props.backgroundColor === "default"
9795
? undefined
9896
: props.transformer.options.colors[
99-
cell.props
100-
.backgroundColor as keyof typeof props.transformer.options.colors
101-
].background,
97+
cell.props.backgroundColor
98+
]?.background,
10299
textAlign: cell.props.textAlignment,
103100
},
104101
]}

0 commit comments

Comments
 (0)