Skip to content
File

Blob: src/shared/editor/schema/table.ts

typescript51 lines
1import { Extension } from "@tiptap/core";
2import { Table, TableCell, TableHeader, TableRow } from "@tiptap/extension-table";
3 
4export const TABLE_CELL_MIN_WIDTH = 80;
5 
6function 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 
13export 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 
22export const SharedTableRow = TableRow;
23export const SharedTableCell = TableCell;
24export const SharedTableHeader = TableHeader;
25 
26export 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 
48export function createSharedTableExtensions() {
49 return [SharedTable, SharedTableRow, SharedTableCell, SharedTableHeader, SharedTableRowHeightAttribute] as const;
50}