Skip to content

Commit 19ef51b

Browse files
committed
fix: stop-gap for tables not exporting
1 parent 4c57877 commit 19ef51b

File tree

2 files changed

+74
-27
lines changed
  • examples/05-interoperability/07-converting-blocks-to-odt
  • packages/xl-odt-exporter/src/odt/defaultSchema

2 files changed

+74
-27
lines changed

examples/05-interoperability/07-converting-blocks-to-odt/App.tsx

+5
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,11 @@ export default function App() {
2525
// Creates a new editor instance with some initial content.
2626
const editor = useCreateBlockNote({
2727
schema: withPageBreak(BlockNoteSchema.create()),
28+
tables: {
29+
splitCells: true,
30+
cellTextColor: true,
31+
cellBackgroundColor: true,
32+
},
2833
initialContent: [
2934
{
3035
type: "paragraph",

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

+69-27
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@ import {
22
BlockMapping,
33
DefaultBlockSchema,
44
DefaultProps,
5+
mapTableCell,
56
pageBreakSchema,
67
StyledText,
8+
TableCell,
79
} from "@blocknote/core";
810
import { ODTExporter } from "../odtExporter.js";
911

@@ -81,21 +83,53 @@ const createParagraphStyle = (
8183
));
8284
};
8385

84-
const createTableCellStyle = (exporter: ODTExporter<any, any, any>) => {
85-
const cellStyleName = exporter.registerStyle((name) => (
86-
<style:style style:family="table-cell" style:name={name}>
87-
<style:table-cell-properties
88-
fo:border="0.5pt solid #000000"
89-
style:writing-mode="lr-tb"
90-
fo:padding-top="0in"
91-
fo:padding-left="0.075in"
92-
fo:padding-bottom="0in"
93-
fo:padding-right="0.075in"
94-
/>
95-
</style:style>
96-
));
86+
const createTableCellStyle = (
87+
exporter: ODTExporter<any, any, any>
88+
): ((cell: TableCell<any, any>) => string) => {
89+
// To not create a new style for each cell within a table, we cache the styles based on unique cell properties
90+
const cellStyleCache = new Map<string, string>();
91+
92+
return (cell: TableCell<any, any>) => {
93+
const key = `${cell.props.backgroundColor}-${cell.props.textColor}-${cell.props.textAlignment}`;
94+
95+
if (cellStyleCache.has(key)) {
96+
return cellStyleCache.get(key)!;
97+
}
98+
99+
const styleName = exporter.registerStyle((name) => (
100+
<style:style style:family="table-cell" style:name={name}>
101+
<style:table-cell-properties
102+
fo:border="0.5pt solid #000000"
103+
style:writing-mode="lr-tb"
104+
fo:padding-top="0in"
105+
fo:padding-left="0.075in"
106+
fo:padding-bottom="0in"
107+
fo:padding-right="0.075in"
108+
fo:background-color={
109+
cell.props.backgroundColor !== "default" &&
110+
cell.props.backgroundColor
111+
? exporter.options.colors[
112+
cell.props
113+
.backgroundColor as keyof typeof exporter.options.colors
114+
].background
115+
: undefined
116+
}
117+
// TODO This is not applying because the children set their own colors
118+
fo:color={
119+
cell.props.textColor !== "default" && cell.props.textColor
120+
? exporter.options.colors[
121+
cell.props.textColor as keyof typeof exporter.options.colors
122+
].text
123+
: undefined
124+
}
125+
/>
126+
</style:style>
127+
));
128+
129+
cellStyleCache.set(key, styleName);
97130

98-
return cellStyleName;
131+
return styleName;
132+
};
99133
};
100134
const createTableStyle = (
101135
exporter: ODTExporter<any, any, any>,
@@ -302,7 +336,7 @@ export const odtBlockMappingForDefaultSchema: BlockMapping<
302336
) || 0;
303337
const tableWidthPT = tableWidthPX * 0.75;
304338
const ex = exporter as ODTExporter<any, any, any>;
305-
const cellStyleName = createTableCellStyle(ex);
339+
const getCellStyleName = createTableCellStyle(ex);
306340
const tableStyleName = createTableStyle(ex, { width: tableWidthPT });
307341

308342
return (
@@ -318,19 +352,27 @@ export const odtBlockMappingForDefaultSchema: BlockMapping<
318352
/>
319353
</style:style>
320354
));
321-
return <table:table-column table:style-name={style} />;
355+
return <table:table-column table:style-name={style} key={i} />;
322356
})}
323-
{block.content.rows.map((row) => (
324-
<table:table-row>
325-
{row.cells.map((cell) => (
326-
<table:table-cell
327-
table:style-name={cellStyleName}
328-
office:value-type="string">
329-
<text:p text:style-name="Standard">
330-
{exporter.transformInlineContent(cell)}
331-
</text:p>
332-
</table:table-cell>
333-
))}
357+
{block.content.rows.map((row, rowIndex) => (
358+
<table:table-row key={rowIndex}>
359+
{row.cells.map((c, colIndex) => {
360+
const cell = mapTableCell(c);
361+
return (
362+
<table:table-cell
363+
key={`${rowIndex}-${colIndex}`}
364+
table:style-name={getCellStyleName(cell)}
365+
office:value-type="string"
366+
style:text-align-source="fix"
367+
style:paragraph-properties-text-align={
368+
cell.props.textAlignment
369+
}>
370+
<text:p text:style-name="Standard">
371+
{exporter.transformInlineContent(cell.content)}
372+
</text:p>
373+
</table:table-cell>
374+
);
375+
})}
334376
</table:table-row>
335377
))}
336378
</table:table>

0 commit comments

Comments
 (0)