File
Blob: src/shared/editor/schema/table.ts
| 1 | import { Extension } from "@tiptap/core"; |
| 2 | import { Table, TableCell, TableHeader, TableRow } from "@tiptap/extension-table"; |
| 3 | |
| 4 | export const TABLE_CELL_MIN_WIDTH = 80; |
| 5 | |
| 6 | function parseHeightStyle(style: string): number | null { |
| 7 | const match = /(?:^|;)\s*height\s*:\s*([0-9.]+)px\s*(?:;|$)/i.exec(style); |
| 8 | if (!match) return null; |
| 9 | const value = Number.parseFloat(match[1] ?? ""); |
| 10 | return Number.isFinite(value) && value > 0 ? value : null; |
| 11 | } |
| 12 | |
| 13 | export const SharedTable = Table.configure({ |
| 14 | resizable: true, |
| 15 | allowTableNodeSelection: true, |
| 16 | cellMinWidth: TABLE_CELL_MIN_WIDTH, |
| 17 | lastColumnResizable: true, |
| 18 | renderWrapper: true, |
| 19 | View: null, |
| 20 | }); |
| 21 | |
| 22 | export const SharedTableRow = TableRow; |
| 23 | export const SharedTableCell = TableCell; |
| 24 | export const SharedTableHeader = TableHeader; |
| 25 | |
| 26 | export const SharedTableRowHeightAttribute = Extension.create({ |
| 27 | name: "tableRowHeight", |
| 28 | |
| 29 | addGlobalAttributes() { |
| 30 | return [ |
| 31 | { |
| 32 | types: ["tableRow"], |
| 33 | attributes: { |
| 34 | height: { |
| 35 | default: null, |
| 36 | parseHTML: (element) => parseHeightStyle(element.getAttribute("style") ?? ""), |
| 37 | renderHTML: (attributes: { height?: number | null }) => { |
| 38 | if (!attributes.height) return {}; |
| 39 | return { style: `height: ${attributes.height}px` }; |
| 40 | }, |
| 41 | }, |
| 42 | }, |
| 43 | }, |
| 44 | ]; |
| 45 | }, |
| 46 | }); |
| 47 | |
| 48 | export function createSharedTableExtensions() { |
| 49 | return [SharedTable, SharedTableRow, SharedTableCell, SharedTableHeader, SharedTableRowHeightAttribute] as const; |
| 50 | } |