@@ -30,12 +30,13 @@ import {TableDOMCell, TableDOMTable} from './LexicalTableObserver';
|
30 | 30 | import {getTable} from './LexicalTableSelectionHelpers';
|
31 | 31 | import {CommonBlockNode, copyCommonBlockProperties, SerializedCommonBlockNode} from "lexical/nodes/CommonBlockNode";
|
32 | 32 | import {
|
| 33 | +applyCommonPropertyChanges, |
33 | 34 | commonPropertiesDifferent, deserializeCommonBlockNode,
|
34 | 35 | setCommonBlockPropsFromElement,
|
35 | 36 | updateElementWithCommonBlockProps
|
36 | 37 | } from "lexical/nodes/common";
|
37 | 38 | import {el, extractStyleMapFromElement, StyleMap} from "../../utils/dom";
|
38 |
| -import {getTableColumnWidths} from "../../utils/tables"; |
| 39 | +import {buildColgroupFromTableWidths, getTableColumnWidths} from "../../utils/tables"; |
39 | 40 |
|
40 | 41 | export type SerializedTableNode = Spread<{
|
41 | 42 | colWidths: string[];
|
@@ -54,7 +55,7 @@ export class TableNode extends CommonBlockNode {
|
54 | 55 | static clone(node: TableNode): TableNode {
|
55 | 56 | const newNode = new TableNode(node.__key);
|
56 | 57 | copyCommonBlockProperties(node, newNode);
|
57 |
| -newNode.__colWidths = node.__colWidths; |
| 58 | +newNode.__colWidths = [...node.__colWidths]; |
58 | 59 | newNode.__styles = new Map(node.__styles);
|
59 | 60 | return newNode;
|
60 | 61 | }
|
@@ -98,15 +99,8 @@ export class TableNode extends CommonBlockNode {
|
98 | 99 | updateElementWithCommonBlockProps(tableElement, this);
|
99 | 100 |
|
100 | 101 | const colWidths = this.getColWidths();
|
101 |
| -if (colWidths.length > 0) { |
102 |
| -const colgroup = el('colgroup'); |
103 |
| -for (const width of colWidths) { |
104 |
| -const col = el('col'); |
105 |
| -if (width) { |
106 |
| -col.style.width = width; |
107 |
| -} |
108 |
| -colgroup.append(col); |
109 |
| -} |
| 102 | +const colgroup = buildColgroupFromTableWidths(colWidths); |
| 103 | +if (colgroup) { |
110 | 104 | tableElement.append(colgroup);
|
111 | 105 | }
|
112 | 106 |
|
@@ -117,11 +111,29 @@ export class TableNode extends CommonBlockNode {
|
117 | 111 | return tableElement;
|
118 | 112 | }
|
119 | 113 |
|
120 |
| -updateDOM(_prevNode: TableNode): boolean { |
121 |
| -return commonPropertiesDifferent(_prevNode, this) |
122 |
| -|| this.__colWidths.join(':') !== _prevNode.__colWidths.join(':') |
123 |
| -|| this.__styles.size !== _prevNode.__styles.size |
124 |
| -|| (Array.from(this.__styles.values()).join(':') !== (Array.from(_prevNode.__styles.values()).join(':'))); |
| 114 | +updateDOM(_prevNode: TableNode, dom: HTMLElement): boolean { |
| 115 | +applyCommonPropertyChanges(_prevNode, this, dom); |
| 116 | + |
| 117 | +if (this.__colWidths.join(':') !== _prevNode.__colWidths.join(':')) { |
| 118 | +const existingColGroup = Array.from(dom.children).find(child => child.nodeName === 'COLGROUP'); |
| 119 | +const newColGroup = buildColgroupFromTableWidths(this.__colWidths); |
| 120 | +if (existingColGroup) { |
| 121 | +existingColGroup.remove(); |
| 122 | +} |
| 123 | + |
| 124 | +if (newColGroup) { |
| 125 | +dom.prepend(newColGroup); |
| 126 | +} |
| 127 | +} |
| 128 | + |
| 129 | +if (Array.from(this.__styles.values()).join(':') !== Array.from(_prevNode.__styles.values()).join(':')) { |
| 130 | +dom.style.cssText = ''; |
| 131 | +for (const [name, value] of this.__styles.entries()) { |
| 132 | +dom.style.setProperty(name, value); |
| 133 | +} |
| 134 | +} |
| 135 | + |
| 136 | +return false; |
125 | 137 | }
|
126 | 138 |
|
127 | 139 | exportDOM(editor: LexicalEditor): DOMExportOutput {
|
@@ -169,7 +181,7 @@ export class TableNode extends CommonBlockNode {
|
169 | 181 |
|
170 | 182 | getColWidths(): string[] {
|
171 | 183 | const self = this.getLatest();
|
172 |
| -return self.__colWidths; |
| 184 | +return [...self.__colWidths]; |
173 | 185 | }
|
174 | 186 |
|
175 | 187 | getStyles(): StyleMap {
|
|
0 commit comments