|
1 |
| -import { TD, TH, TR, Table } from '@ag-media/react-pdf-table'; |
2 |
| -import { View } from '@react-pdf/renderer'; |
| 1 | +/** |
| 2 | + * We use mainly the Blocknotes code, mixed with @ag-media/react-pdf-table |
| 3 | + * to have a better Table support. |
| 4 | + * See: |
| 5 | + * https://github.com/TypeCellOS/BlockNote/blob/004c0bf720fe1415c497ad56449015c5f4dd7ba0/packages/xl-pdf-exporter/src/pdf/util/table/Table.tsx |
| 6 | + * |
| 7 | + * We succeded to manage the colspan, but rowspan is not supported yet. |
| 8 | + */ |
| 9 | + |
| 10 | +import { TD, TR, Table } from '@ag-media/react-pdf-table'; |
| 11 | +import { mapTableCell } from '@blocknote/core'; |
| 12 | +import { StyleSheet, Text } from '@react-pdf/renderer'; |
3 | 13 |
|
4 | 14 | import { DocsExporterPDF } from '../types';
|
| 15 | +const PIXELS_PER_POINT = 0.75; |
| 16 | +const styles = StyleSheet.create({ |
| 17 | + tableContainer: { |
| 18 | + border: '1px solid #ddd', |
| 19 | + }, |
| 20 | + row: { |
| 21 | + flexDirection: 'row', |
| 22 | + flexWrap: 'wrap', |
| 23 | + display: 'flex', |
| 24 | + }, |
| 25 | + cell: { |
| 26 | + paddingHorizontal: 5 * PIXELS_PER_POINT, |
| 27 | + paddingTop: 3 * PIXELS_PER_POINT, |
| 28 | + wordWrap: 'break-word', |
| 29 | + whiteSpace: 'pre-wrap', |
| 30 | + }, |
| 31 | + headerCell: { |
| 32 | + fontWeight: 'bold', |
| 33 | + }, |
| 34 | +}); |
5 | 35 |
|
6 | 36 | export const blockMappingTablePDF: DocsExporterPDF['mappings']['blockMapping']['table'] =
|
7 | 37 | (block, exporter) => {
|
| 38 | + const { options } = exporter; |
| 39 | + const blockContent = block.content; |
| 40 | + |
| 41 | + // If headerRows is 1, then the first row is a header row |
| 42 | + const headerRows = new Array(blockContent.headerRows ?? 0).fill( |
| 43 | + true, |
| 44 | + ) as boolean[]; |
| 45 | + // If headerCols is 1, then the first column is a header column |
| 46 | + const headerCols = new Array(blockContent.headerCols ?? 0).fill( |
| 47 | + true, |
| 48 | + ) as boolean[]; |
| 49 | + |
| 50 | + /** |
| 51 | + * Calculate the table scale based on the column widths. |
| 52 | + */ |
| 53 | + const columnWidths = blockContent.columnWidths.map((w) => w || 120); |
| 54 | + const fullWidth = 730; |
| 55 | + const totalWidth = Math.min( |
| 56 | + columnWidths.reduce((sum, w) => sum + w, 0), |
| 57 | + fullWidth, |
| 58 | + ); |
| 59 | + const tableScale = (totalWidth * 100) / fullWidth; |
| 60 | + |
8 | 61 | return (
|
9 |
| - <Table> |
10 |
| - {block.content.rows.map((row, index) => { |
11 |
| - if (index === 0) { |
12 |
| - return ( |
13 |
| - <TH key={index}> |
14 |
| - {row.cells.map((cell, index) => { |
15 |
| - // Make empty cells are rendered. |
16 |
| - if (cell.length === 0) { |
17 |
| - cell.push({ |
18 |
| - styles: {}, |
19 |
| - text: ' ', |
20 |
| - type: 'text', |
21 |
| - }); |
22 |
| - } |
23 |
| - return ( |
24 |
| - <TD key={index}>{exporter.transformInlineContent(cell)}</TD> |
25 |
| - ); |
26 |
| - })} |
27 |
| - </TH> |
28 |
| - ); |
29 |
| - } |
| 62 | + <Table style={[styles.tableContainer, { width: `${tableScale}%` }]}> |
| 63 | + {blockContent.rows.map((row, rowIndex) => { |
| 64 | + const isHeaderRow = headerRows[rowIndex]; |
| 65 | + |
30 | 66 | return (
|
31 |
| - <TR key={index}> |
32 |
| - {row.cells.map((cell, index) => { |
33 |
| - // Make empty cells are rendered. |
34 |
| - if (cell.length === 0) { |
| 67 | + <TR key={rowIndex}> |
| 68 | + {row.cells.map((c, colIndex) => { |
| 69 | + const formatCell = mapTableCell(c); |
| 70 | + |
| 71 | + const isHeaderCol = headerCols[colIndex]; |
| 72 | + |
| 73 | + const cell = formatCell.content; |
| 74 | + const cellProps = formatCell.props; |
| 75 | + |
| 76 | + // Make empty cells rendered. |
| 77 | + if (Array.isArray(cell) && cell.length === 0) { |
35 | 78 | cell.push({
|
36 | 79 | styles: {},
|
37 | 80 | text: ' ',
|
38 | 81 | type: 'text',
|
39 | 82 | });
|
40 | 83 | }
|
| 84 | + |
| 85 | + const weight = columnWidths |
| 86 | + .slice(colIndex, colIndex + (cellProps.colspan || 1)) |
| 87 | + .reduce((sum, w) => sum + w, 0); |
| 88 | + |
| 89 | + const flexCell = { |
| 90 | + flex: `${weight} ${weight} 0%`, |
| 91 | + }; |
| 92 | + |
| 93 | + const arrayStyle = [ |
| 94 | + isHeaderRow || isHeaderCol ? styles.headerCell : {}, |
| 95 | + flexCell, |
| 96 | + { |
| 97 | + color: |
| 98 | + cellProps.textColor === 'default' |
| 99 | + ? undefined |
| 100 | + : options.colors[ |
| 101 | + cellProps.textColor as keyof typeof options.colors |
| 102 | + ].text, |
| 103 | + backgroundColor: |
| 104 | + cellProps.backgroundColor === 'default' |
| 105 | + ? undefined |
| 106 | + : options.colors[ |
| 107 | + cellProps.backgroundColor as keyof typeof options.colors |
| 108 | + ].background, |
| 109 | + textAlign: cellProps.textAlignment, |
| 110 | + }, |
| 111 | + ]; |
| 112 | + |
41 | 113 | return (
|
42 |
| - <TD key={index}> |
43 |
| - <View>{exporter.transformInlineContent(cell)}</View> |
| 114 | + <TD key={colIndex} style={arrayStyle}> |
| 115 | + <Text style={styles.cell}> |
| 116 | + {exporter.transformInlineContent(cell)} |
| 117 | + </Text> |
44 | 118 | </TD>
|
45 | 119 | );
|
46 | 120 | })}
|
|
0 commit comments