Skip to content

Commit 4c34462

Browse files
YousefEDareknawo
authored andcommitted
table styles
1 parent c843810 commit 4c34462

File tree

4 files changed

+84
-38
lines changed

4 files changed

+84
-38
lines changed

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

Lines changed: 66 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,16 @@
1-
import { BlockMapping, DefaultBlockSchema } from "@blocknote/core";
1+
import {
2+
BlockMapping,
3+
DefaultBlockSchema,
4+
DefaultProps,
5+
} from "@blocknote/core";
26
import { ODTExporter } from "../odtExporter.js";
37
import {
48
DrawFrame,
59
DrawImage,
610
StyleBackgroundFill,
711
StyleParagraphProperties,
812
StyleStyle,
13+
StyleTableCellProperties,
914
Table,
1015
TableCell,
1116
TableRow,
@@ -23,13 +28,21 @@ export const getTabs = (nestingLevel: number) => {
2328

2429
const createParagraphStyle = (
2530
exporter: ODTExporter<any, any, any>,
26-
props: Record<string, any>
31+
props: Partial<DefaultProps>,
32+
parentStyleName?: string
2733
) => {
2834
const styles: Record<string, string> = {};
2935
const children: React.ReactNode[] = [];
3036

31-
if (props.textAlignment !== "default") {
32-
styles["fo:text-align"] = props.textAlignment;
37+
if (props.textAlignment && props.textAlignment !== "left") {
38+
const alignmentMap = {
39+
left: "start",
40+
center: "center",
41+
right: "end",
42+
justify: "justify",
43+
};
44+
styles["fo:text-align"] =
45+
alignmentMap[props.textAlignment as keyof typeof alignmentMap];
3346
}
3447

3548
if (props.backgroundColor && props.backgroundColor !== "default") {
@@ -40,19 +53,47 @@ const createParagraphStyle = (
4053
children.push(<StyleBackgroundFill color={color} />);
4154
}
4255

56+
if (props.textColor && props.textColor !== "default") {
57+
const color =
58+
exporter.options.colors[
59+
props.textColor as keyof typeof exporter.options.colors
60+
].text;
61+
styles["fo:color"] = color;
62+
}
63+
4364
if (Object.keys(styles).length === 0 && children.length === 0) {
44-
return undefined;
65+
return parentStyleName;
4566
}
4667

4768
return exporter.registerStyle((name) => (
48-
<StyleStyle style:family="paragraph" style:name={name}>
69+
<StyleStyle
70+
style:family="paragraph"
71+
style:name={name}
72+
style:parent-style-name={parentStyleName}>
4973
<StyleParagraphProperties {...styles}>
5074
{children}
5175
</StyleParagraphProperties>
5276
</StyleStyle>
5377
));
5478
};
5579

80+
const createTableStyle = (exporter: ODTExporter<any, any, any>) => {
81+
const cellName = exporter.registerStyle((name) => (
82+
<StyleStyle style:family="table-cell" style:name={name}>
83+
<StyleTableCellProperties
84+
fo:border="0.0069in solid #000000"
85+
style:writing-mode="lr-tb"
86+
fo:padding-top="0in"
87+
fo:padding-left="0.075in"
88+
fo:padding-bottom="0in"
89+
fo:padding-right="0.075in"
90+
/>
91+
</StyleStyle>
92+
));
93+
94+
return cellName;
95+
};
96+
5697
export const odtBlockMappingForDefaultSchema: BlockMapping<
5798
DefaultBlockSchema,
5899
any,
@@ -76,7 +117,8 @@ export const odtBlockMappingForDefaultSchema: BlockMapping<
76117
heading: (block, exporter, nestingLevel) => {
77118
const customStyleName = createParagraphStyle(
78119
exporter as ODTExporter<any, any, any>,
79-
block.props
120+
block.props,
121+
"Heading" + block.props.level
80122
);
81123
const styleName = customStyleName;
82124

@@ -120,19 +162,23 @@ export const odtBlockMappingForDefaultSchema: BlockMapping<
120162
);
121163
},
122164

123-
table: (block, exporter) => (
124-
<Table>
125-
{block.content.rows.map((row) => (
126-
<TableRow>
127-
{row.cells.map((cell) => (
128-
<TableCell>
129-
<TextP>{exporter.transformInlineContent(cell)}</TextP>
130-
</TableCell>
131-
))}
132-
</TableRow>
133-
))}
134-
</Table>
135-
),
165+
table: (block, exporter) => {
166+
const styleName = createTableStyle(exporter as ODTExporter<any, any, any>);
167+
168+
return (
169+
<Table>
170+
{block.content.rows.map((row) => (
171+
<TableRow>
172+
{row.cells.map((cell) => (
173+
<TableCell table:style-name={styleName}>
174+
<TextP>{exporter.transformInlineContent(cell)}</TextP>
175+
</TableCell>
176+
))}
177+
</TableRow>
178+
))}
179+
</Table>
180+
);
181+
},
136182

137183
codeBlock: (block, exporter) => (
138184
<TextP>

packages/xl-odt-exporter/src/odt/odtExporter.tsx

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -82,24 +82,6 @@ export class ODTExporter<
8282
return styles;
8383
}
8484

85-
private getBlockStyleName(props: Record<string, any>): string | undefined {
86-
const styles = this.blockPropsToStyles(props);
87-
if (Object.keys(styles).length === 0) {
88-
return undefined;
89-
}
90-
91-
const styleName = `P${++this.styleCounter}`;
92-
93-
this.automaticStyles.set(
94-
styleName,
95-
<StyleStyle style:name={styleName} style:family="paragraph">
96-
<style:paragraph-properties {...styles} />
97-
</StyleStyle>
98-
);
99-
100-
return styleName;
101-
}
102-
10385
public transformStyledText(styledText: StyledText<S>): React.ReactNode {
10486
const stylesArray = this.mapStyles(styledText.styles);
10587
const styles = Object.assign({}, ...stylesArray);

packages/xl-odt-exporter/src/odt/util/components.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,3 +116,15 @@ export const StyleBackgroundFill = ({
116116

117117
export const StyleParagraphProperties = ({ children, ...props }: Props) =>
118118
createElement("style:paragraph-properties", props, children);
119+
120+
export const StyleTableProperties = ({ children, ...props }: Props) =>
121+
createElement("style:table-properties", props, children);
122+
123+
export const StyleTableCellProperties = ({ children, ...props }: Props) =>
124+
createElement("style:table-cell-properties", props, children);
125+
126+
export const StyleTableColumnProperties = ({ children, ...props }: Props) =>
127+
createElement("style:table-column-properties", props, children);
128+
129+
export const StyleTableRowProperties = ({ children, ...props }: Props) =>
130+
createElement("style:table-row-properties", props, children);

packages/xl-odt-exporter/src/odt/util/jsx.d.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@ declare namespace JSX {
2121
"manifest:file-entry": any;
2222
"style:paragraph-properties": any;
2323
"style:background-fill": any;
24+
"style:table-properties": any;
25+
"style:table-cell-properties": any;
26+
"style:table-column-properties": any;
27+
"style:table-row-properties": any;
2428
}
2529

2630
interface IntrinsicAttributes {
@@ -31,5 +35,7 @@ declare namespace JSX {
3135
"style:background-fill"?: string;
3236
"draw:fill"?: string;
3337
"draw:fill-color"?: string;
38+
"fo:border"?: string;
39+
"fo:padding"?: string;
3440
}
3541
}

0 commit comments

Comments
 (0)