@@ -30,12 +30,13 @@ import {TableDOMCell, TableDOMTable} from './LexicalTableObserver';
3030import { getTable } from './LexicalTableSelectionHelpers' ;
3131import { CommonBlockNode , copyCommonBlockProperties , SerializedCommonBlockNode } from "lexical/nodes/CommonBlockNode" ;
3232import {
33+ applyCommonPropertyChanges ,
3334 commonPropertiesDifferent , deserializeCommonBlockNode ,
3435 setCommonBlockPropsFromElement ,
3536 updateElementWithCommonBlockProps
3637} from "lexical/nodes/common" ;
3738import { el , extractStyleMapFromElement , StyleMap } from "../../utils/dom" ;
38- import { getTableColumnWidths } from "../../utils/tables" ;
39+ import { buildColgroupFromTableWidths , getTableColumnWidths } from "../../utils/tables" ;
3940
4041export type SerializedTableNode = Spread < {
4142 colWidths : string [ ] ;
@@ -54,7 +55,7 @@ export class TableNode extends CommonBlockNode {
5455 static clone ( node : TableNode ) : TableNode {
5556 const newNode = new TableNode ( node . __key ) ;
5657 copyCommonBlockProperties ( node , newNode ) ;
57- newNode . __colWidths = node . __colWidths ;
58+ newNode . __colWidths = [ ... node . __colWidths ] ;
5859 newNode . __styles = new Map ( node . __styles ) ;
5960 return newNode ;
6061 }
@@ -98,15 +99,8 @@ export class TableNode extends CommonBlockNode {
9899 updateElementWithCommonBlockProps ( tableElement , this ) ;
99100
100101 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 ) {
110104 tableElement . append ( colgroup ) ;
111105 }
112106
@@ -117,11 +111,29 @@ export class TableNode extends CommonBlockNode {
117111 return tableElement ;
118112 }
119113
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 ;
125137 }
126138
127139 exportDOM ( editor : LexicalEditor ) : DOMExportOutput {
@@ -169,7 +181,7 @@ export class TableNode extends CommonBlockNode {
169181
170182 getColWidths ( ) : string [ ] {
171183 const self = this . getLatest ( ) ;
172- return self . __colWidths ;
184+ return [ ... self . __colWidths ] ;
173185 }
174186
175187 getStyles ( ) : StyleMap {
0 commit comments