1
- import { BlockMapping , DefaultBlockSchema } from "@blocknote/core" ;
1
+ import {
2
+ BlockMapping ,
3
+ DefaultBlockSchema ,
4
+ DefaultProps ,
5
+ } from "@blocknote/core" ;
2
6
import { ODTExporter } from "../odtExporter.js" ;
3
7
import {
4
8
DrawFrame ,
5
9
DrawImage ,
6
10
StyleBackgroundFill ,
7
11
StyleParagraphProperties ,
8
12
StyleStyle ,
13
+ StyleTableCellProperties ,
9
14
Table ,
10
15
TableCell ,
11
16
TableRow ,
@@ -23,13 +28,21 @@ export const getTabs = (nestingLevel: number) => {
23
28
24
29
const createParagraphStyle = (
25
30
exporter : ODTExporter < any , any , any > ,
26
- props : Record < string , any >
31
+ props : Partial < DefaultProps > ,
32
+ parentStyleName ?: string
27
33
) => {
28
34
const styles : Record < string , string > = { } ;
29
35
const children : React . ReactNode [ ] = [ ] ;
30
36
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 ] ;
33
46
}
34
47
35
48
if ( props . backgroundColor && props . backgroundColor !== "default" ) {
@@ -40,19 +53,47 @@ const createParagraphStyle = (
40
53
children . push ( < StyleBackgroundFill color = { color } /> ) ;
41
54
}
42
55
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
+
43
64
if ( Object . keys ( styles ) . length === 0 && children . length === 0 ) {
44
- return undefined ;
65
+ return parentStyleName ;
45
66
}
46
67
47
68
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 } >
49
73
< StyleParagraphProperties { ...styles } >
50
74
{ children }
51
75
</ StyleParagraphProperties >
52
76
</ StyleStyle >
53
77
) ) ;
54
78
} ;
55
79
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
+
56
97
export const odtBlockMappingForDefaultSchema : BlockMapping <
57
98
DefaultBlockSchema ,
58
99
any ,
@@ -76,7 +117,8 @@ export const odtBlockMappingForDefaultSchema: BlockMapping<
76
117
heading : ( block , exporter , nestingLevel ) => {
77
118
const customStyleName = createParagraphStyle (
78
119
exporter as ODTExporter < any , any , any > ,
79
- block . props
120
+ block . props ,
121
+ "Heading" + block . props . level
80
122
) ;
81
123
const styleName = customStyleName ;
82
124
@@ -120,19 +162,23 @@ export const odtBlockMappingForDefaultSchema: BlockMapping<
120
162
) ;
121
163
} ,
122
164
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
+ } ,
136
182
137
183
codeBlock : ( block , exporter ) => (
138
184
< TextP >
0 commit comments