1 import {SerializedTableNode, TableNode, TableRowNode} from "@lexical/table";
2 import {DOMConversion, DOMConversionMap, DOMConversionOutput, LexicalNode, Spread} from "lexical";
3 import {EditorConfig} from "lexical/LexicalEditor";
4 import {el} from "../helpers";
6 export type SerializedCustomTableNode = Spread<{
9 }, SerializedTableNode>
11 export class CustomTableNode extends TableNode {
13 __colWidths: string[] = [];
16 return 'custom-table';
20 const self = this.getWritable();
25 const self = this.getLatest();
29 setColWidths(widths: string[]) {
30 const self = this.getWritable();
31 self.__colWidths = widths;
34 getColWidths(): string[] {
35 const self = this.getLatest();
36 return self.__colWidths;
39 static clone(node: CustomTableNode) {
40 const newNode = new CustomTableNode(node.__key);
41 newNode.__id = node.__id;
42 newNode.__colWidths = node.__colWidths;
46 createDOM(config: EditorConfig): HTMLElement {
47 const dom = super.createDOM(config);
48 const id = this.getId();
50 dom.setAttribute('id', id);
53 const colWidths = this.getColWidths();
54 if (colWidths.length > 0) {
55 const colgroup = el('colgroup');
56 for (const width of colWidths) {
57 const col = el('col');
59 col.style.width = width;
69 updateDOM(): boolean {
73 exportJSON(): SerializedCustomTableNode {
75 ...super.exportJSON(),
79 colWidths: this.__colWidths,
83 static importJSON(serializedNode: SerializedCustomTableNode): CustomTableNode {
84 const node = $createCustomTableNode();
85 node.setId(serializedNode.id);
86 node.setColWidths(serializedNode.colWidths);
90 static importDOM(): DOMConversionMap|null {
92 table(node: HTMLElement): DOMConversion|null {
94 conversion: (element: HTMLElement): DOMConversionOutput|null => {
95 const node = $createCustomTableNode();
98 node.setId(element.id);
101 const colWidths = getTableColumnWidths(element as HTMLTableElement);
102 node.setColWidths(colWidths);
113 function getTableColumnWidths(table: HTMLTableElement): string[] {
114 const rows = table.querySelectorAll('tr');
115 let maxColCount: number = 0;
116 let maxColRow: HTMLTableRowElement|null = null;
118 for (const row of rows) {
119 if (row.childElementCount > maxColCount) {
121 maxColCount = row.childElementCount;
125 const colGroup = table.querySelector('colgroup');
126 let widths: string[] = [];
127 if (colGroup && colGroup.childElementCount === maxColCount) {
128 widths = extractWidthsFromRow(colGroup);
130 if (widths.filter(Boolean).length === 0 && maxColRow) {
131 widths = extractWidthsFromRow(maxColRow);
137 function extractWidthsFromRow(row: HTMLTableRowElement|HTMLTableColElement) {
138 return [...row.children].map(child => extractWidthFromElement(child as HTMLElement))
141 function extractWidthFromElement(element: HTMLElement): string {
142 let width = element.style.width || element.getAttribute('width');
143 if (!Number.isNaN(Number(width))) {
144 width = width + 'px';
150 export function $createCustomTableNode(): CustomTableNode {
151 return new CustomTableNode();
154 export function $isCustomTableNode(node: LexicalNode | null | undefined): boolean {
155 return node instanceof CustomTableNode;
158 export function $setTableColumnWidth(node: CustomTableNode, columnIndex: number, width: number): void {
159 const rows = node.getChildren() as TableRowNode[];
161 for (const row of rows) {
162 const cellCount = row.getChildren().length;
163 if (cellCount > maxCols) {
168 let colWidths = node.getColWidths();
169 if (colWidths.length === 0 || colWidths.length < maxCols) {
170 colWidths = Array(maxCols).fill('');
173 if (columnIndex + 1 > colWidths.length) {
174 console.error(`Attempted to set table column width for column [${columnIndex}] but only ${colWidths.length} columns found`);
177 colWidths[columnIndex] = width + 'px';
178 node.setColWidths(colWidths);
179 console.log('setting col widths', node, colWidths);