1 import {SerializedTableNode, TableNode} from "@lexical/table";
2 import {DOMConversion, DOMConversionMap, DOMConversionOutput, LexicalNode, Spread} from "lexical";
3 import {EditorConfig} from "lexical/LexicalEditor";
5 import {el} from "../utils/dom";
6 import {getTableColumnWidths} from "../utils/tables";
8 export type SerializedCustomTableNode = Spread<{
11 }, SerializedTableNode>
13 export class CustomTableNode extends TableNode {
15 __colWidths: string[] = [];
18 return 'custom-table';
22 const self = this.getWritable();
27 const self = this.getLatest();
31 setColWidths(widths: string[]) {
32 const self = this.getWritable();
33 self.__colWidths = widths;
36 getColWidths(): string[] {
37 const self = this.getLatest();
38 return self.__colWidths;
41 static clone(node: CustomTableNode) {
42 const newNode = new CustomTableNode(node.__key);
43 newNode.__id = node.__id;
44 newNode.__colWidths = node.__colWidths;
48 createDOM(config: EditorConfig): HTMLElement {
49 const dom = super.createDOM(config);
50 const id = this.getId();
52 dom.setAttribute('id', id);
55 const colWidths = this.getColWidths();
56 if (colWidths.length > 0) {
57 const colgroup = el('colgroup');
58 for (const width of colWidths) {
59 const col = el('col');
61 col.style.width = width;
71 updateDOM(): boolean {
75 exportJSON(): SerializedCustomTableNode {
77 ...super.exportJSON(),
81 colWidths: this.__colWidths,
85 static importJSON(serializedNode: SerializedCustomTableNode): CustomTableNode {
86 const node = $createCustomTableNode();
87 node.setId(serializedNode.id);
88 node.setColWidths(serializedNode.colWidths);
92 static importDOM(): DOMConversionMap|null {
94 table(node: HTMLElement): DOMConversion|null {
96 conversion: (element: HTMLElement): DOMConversionOutput|null => {
97 const node = $createCustomTableNode();
100 node.setId(element.id);
103 const colWidths = getTableColumnWidths(element as HTMLTableElement);
104 node.setColWidths(colWidths);
115 export function $createCustomTableNode(): CustomTableNode {
116 return new CustomTableNode();
119 export function $isCustomTableNode(node: LexicalNode | null | undefined): node is CustomTableNode {
120 return node instanceof CustomTableNode;