@@ -2,8 +2,10 @@ import {
2
2
BlockMapping ,
3
3
DefaultBlockSchema ,
4
4
DefaultProps ,
5
+ mapTableCell ,
5
6
pageBreakSchema ,
6
7
StyledText ,
8
+ TableCell ,
7
9
} from "@blocknote/core" ;
8
10
import { ODTExporter } from "../odtExporter.js" ;
9
11
@@ -81,21 +83,53 @@ const createParagraphStyle = (
81
83
) ) ;
82
84
} ;
83
85
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 ) ;
97
130
98
- return cellStyleName ;
131
+ return styleName ;
132
+ } ;
99
133
} ;
100
134
const createTableStyle = (
101
135
exporter : ODTExporter < any , any , any > ,
@@ -302,7 +336,7 @@ export const odtBlockMappingForDefaultSchema: BlockMapping<
302
336
) || 0 ;
303
337
const tableWidthPT = tableWidthPX * 0.75 ;
304
338
const ex = exporter as ODTExporter < any , any , any > ;
305
- const cellStyleName = createTableCellStyle ( ex ) ;
339
+ const getCellStyleName = createTableCellStyle ( ex ) ;
306
340
const tableStyleName = createTableStyle ( ex , { width : tableWidthPT } ) ;
307
341
308
342
return (
@@ -318,19 +352,27 @@ export const odtBlockMappingForDefaultSchema: BlockMapping<
318
352
/>
319
353
</ style :style >
320
354
) ) ;
321
- return < table :table-column table :style-name = { style } /> ;
355
+ return < table :table-column table :style-name = { style } key = { i } /> ;
322
356
} ) }
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
+ } ) }
334
376
</ table :table-row >
335
377
) ) }
336
378
</ table :table >
0 commit comments