@@ -28,7 +28,8 @@ import {
28
28
ElementNode ,
29
29
} from 'lexical' ;
30
30
31
- import { COLUMN_WIDTH , PIXEL_VALUE_REG_EXP } from './constants' ;
31
+ import { extractStyleMapFromElement , StyleMap } from "../../utils/dom" ;
32
+ import { CommonBlockAlignment , extractAlignmentFromElement } from "../../nodes/_common" ;
32
33
33
34
export const TableCellHeaderStates = {
34
35
BOTH : 3 ,
@@ -47,6 +48,8 @@ export type SerializedTableCellNode = Spread<
47
48
headerState : TableCellHeaderState ;
48
49
width ?: number ;
49
50
backgroundColor ?: null | string ;
51
+ styles : Record < string , string > ;
52
+ alignment : CommonBlockAlignment ;
50
53
} ,
51
54
SerializedElementNode
52
55
> ;
@@ -63,6 +66,10 @@ export class TableCellNode extends ElementNode {
63
66
__width ?: number ;
64
67
/** @internal */
65
68
__backgroundColor : null | string ;
69
+ /** @internal */
70
+ __styles : StyleMap = new Map ;
71
+ /** @internal */
72
+ __alignment : CommonBlockAlignment = '' ;
66
73
67
74
static getType ( ) : string {
68
75
return 'tablecell' ;
@@ -77,6 +84,8 @@ export class TableCellNode extends ElementNode {
77
84
) ;
78
85
cellNode . __rowSpan = node . __rowSpan ;
79
86
cellNode . __backgroundColor = node . __backgroundColor ;
87
+ cellNode . __styles = new Map ( node . __styles ) ;
88
+ cellNode . __alignment = node . __alignment ;
80
89
return cellNode ;
81
90
}
82
91
@@ -94,16 +103,20 @@ export class TableCellNode extends ElementNode {
94
103
}
95
104
96
105
static importJSON ( serializedNode : SerializedTableCellNode ) : TableCellNode {
97
- const colSpan = serializedNode . colSpan || 1 ;
98
- const rowSpan = serializedNode . rowSpan || 1 ;
99
- const cellNode = $createTableCellNode (
100
- serializedNode . headerState ,
101
- colSpan ,
102
- serializedNode . width || undefined ,
106
+ const node = $createTableCellNode (
107
+ serializedNode . headerState ,
108
+ serializedNode . colSpan ,
109
+ serializedNode . width ,
103
110
) ;
104
- cellNode . __rowSpan = rowSpan ;
105
- cellNode . __backgroundColor = serializedNode . backgroundColor || null ;
106
- return cellNode ;
111
+
112
+ if ( serializedNode . rowSpan ) {
113
+ node . setRowSpan ( serializedNode . rowSpan ) ;
114
+ }
115
+
116
+ node . setStyles ( new Map ( Object . entries ( serializedNode . styles ) ) ) ;
117
+ node . setAlignment ( serializedNode . alignment ) ;
118
+
119
+ return node ;
107
120
}
108
121
109
122
constructor (
@@ -144,34 +157,19 @@ export class TableCellNode extends ElementNode {
144
157
this . hasHeader ( ) && config . theme . tableCellHeader ,
145
158
) ;
146
159
160
+ for ( const [ name , value ] of this . __styles . entries ( ) ) {
161
+ element . style . setProperty ( name , value ) ;
162
+ }
163
+
164
+ if ( this . __alignment ) {
165
+ element . classList . add ( 'align-' + this . __alignment ) ;
166
+ }
167
+
147
168
return element ;
148
169
}
149
170
150
171
exportDOM ( editor : LexicalEditor ) : DOMExportOutput {
151
172
const { element} = super . exportDOM ( editor ) ;
152
-
153
- if ( element ) {
154
- const element_ = element as HTMLTableCellElement ;
155
- element_ . style . border = '1px solid black' ;
156
- if ( this . __colSpan > 1 ) {
157
- element_ . colSpan = this . __colSpan ;
158
- }
159
- if ( this . __rowSpan > 1 ) {
160
- element_ . rowSpan = this . __rowSpan ;
161
- }
162
- element_ . style . width = `${ this . getWidth ( ) || COLUMN_WIDTH } px` ;
163
-
164
- element_ . style . verticalAlign = 'top' ;
165
- element_ . style . textAlign = 'start' ;
166
-
167
- const backgroundColor = this . getBackgroundColor ( ) ;
168
- if ( backgroundColor !== null ) {
169
- element_ . style . backgroundColor = backgroundColor ;
170
- } else if ( this . hasHeader ( ) ) {
171
- element_ . style . backgroundColor = '#f2f3f5' ;
172
- }
173
- }
174
-
175
173
return {
176
174
element,
177
175
} ;
@@ -186,6 +184,8 @@ export class TableCellNode extends ElementNode {
186
184
rowSpan : this . __rowSpan ,
187
185
type : 'tablecell' ,
188
186
width : this . getWidth ( ) ,
187
+ styles : Object . fromEntries ( this . __styles ) ,
188
+ alignment : this . __alignment ,
189
189
} ;
190
190
}
191
191
@@ -231,6 +231,38 @@ export class TableCellNode extends ElementNode {
231
231
return this . getLatest ( ) . __width ;
232
232
}
233
233
234
+ clearWidth ( ) : void {
235
+ const self = this . getWritable ( ) ;
236
+ self . __width = undefined ;
237
+ }
238
+
239
+ getStyles ( ) : StyleMap {
240
+ const self = this . getLatest ( ) ;
241
+ return new Map ( self . __styles ) ;
242
+ }
243
+
244
+ setStyles ( styles : StyleMap ) : void {
245
+ const self = this . getWritable ( ) ;
246
+ self . __styles = new Map ( styles ) ;
247
+ }
248
+
249
+ setAlignment ( alignment : CommonBlockAlignment ) {
250
+ const self = this . getWritable ( ) ;
251
+ self . __alignment = alignment ;
252
+ }
253
+
254
+ getAlignment ( ) : CommonBlockAlignment {
255
+ const self = this . getLatest ( ) ;
256
+ return self . __alignment ;
257
+ }
258
+
259
+ updateTag ( tag : string ) : void {
260
+ const isHeader = tag . toLowerCase ( ) === 'th' ;
261
+ const state = isHeader ? TableCellHeaderStates . ROW : TableCellHeaderStates . NO_STATUS ;
262
+ const self = this . getWritable ( ) ;
263
+ self . __headerState = state ;
264
+ }
265
+
234
266
getBackgroundColor ( ) : null | string {
235
267
return this . getLatest ( ) . __backgroundColor ;
236
268
}
@@ -265,7 +297,9 @@ export class TableCellNode extends ElementNode {
265
297
prevNode . __width !== this . __width ||
266
298
prevNode . __colSpan !== this . __colSpan ||
267
299
prevNode . __rowSpan !== this . __rowSpan ||
268
- prevNode . __backgroundColor !== this . __backgroundColor
300
+ prevNode . __backgroundColor !== this . __backgroundColor ||
301
+ prevNode . __styles !== this . __styles ||
302
+ prevNode . __alignment !== this . __alignment
269
303
) ;
270
304
}
271
305
@@ -287,38 +321,42 @@ export class TableCellNode extends ElementNode {
287
321
}
288
322
289
323
export function $convertTableCellNodeElement (
290
- domNode : Node ,
324
+ domNode : Node ,
291
325
) : DOMConversionOutput {
292
326
const domNode_ = domNode as HTMLTableCellElement ;
293
327
const nodeName = domNode . nodeName . toLowerCase ( ) ;
294
328
295
329
let width : number | undefined = undefined ;
296
330
331
+
332
+ const PIXEL_VALUE_REG_EXP = / ^ ( \d + (?: \. \d + ) ? ) p x $ / ;
297
333
if ( PIXEL_VALUE_REG_EXP . test ( domNode_ . style . width ) ) {
298
334
width = parseFloat ( domNode_ . style . width ) ;
299
335
}
300
336
301
337
const tableCellNode = $createTableCellNode (
302
- nodeName === 'th'
303
- ? TableCellHeaderStates . ROW
304
- : TableCellHeaderStates . NO_STATUS ,
305
- domNode_ . colSpan ,
306
- width ,
338
+ nodeName === 'th'
339
+ ? TableCellHeaderStates . ROW
340
+ : TableCellHeaderStates . NO_STATUS ,
341
+ domNode_ . colSpan ,
342
+ width ,
307
343
) ;
308
344
309
345
tableCellNode . __rowSpan = domNode_ . rowSpan ;
310
- const backgroundColor = domNode_ . style . backgroundColor ;
311
- if ( backgroundColor !== '' ) {
312
- tableCellNode . __backgroundColor = backgroundColor ;
313
- }
314
346
315
347
const style = domNode_ . style ;
316
348
const textDecoration = style . textDecoration . split ( ' ' ) ;
317
349
const hasBoldFontWeight =
318
- style . fontWeight === '700' || style . fontWeight === 'bold' ;
350
+ style . fontWeight === '700' || style . fontWeight === 'bold' ;
319
351
const hasLinethroughTextDecoration = textDecoration . includes ( 'line-through' ) ;
320
352
const hasItalicFontStyle = style . fontStyle === 'italic' ;
321
353
const hasUnderlineTextDecoration = textDecoration . includes ( 'underline' ) ;
354
+
355
+ if ( domNode instanceof HTMLElement ) {
356
+ tableCellNode . setStyles ( extractStyleMapFromElement ( domNode ) ) ;
357
+ tableCellNode . setAlignment ( extractAlignmentFromElement ( domNode ) ) ;
358
+ }
359
+
322
360
return {
323
361
after : ( childLexicalNodes ) => {
324
362
if ( childLexicalNodes . length === 0 ) {
@@ -330,8 +368,8 @@ export function $convertTableCellNodeElement(
330
368
if ( $isTableCellNode ( parentLexicalNode ) && ! $isElementNode ( lexicalNode ) ) {
331
369
const paragraphNode = $createParagraphNode ( ) ;
332
370
if (
333
- $isLineBreakNode ( lexicalNode ) &&
334
- lexicalNode . getTextContent ( ) === '\n'
371
+ $isLineBreakNode ( lexicalNode ) &&
372
+ lexicalNode . getTextContent ( ) === '\n'
335
373
) {
336
374
return null ;
337
375
}
@@ -360,7 +398,7 @@ export function $convertTableCellNodeElement(
360
398
}
361
399
362
400
export function $createTableCellNode (
363
- headerState : TableCellHeaderState ,
401
+ headerState : TableCellHeaderState = TableCellHeaderStates . NO_STATUS ,
364
402
colSpan = 1 ,
365
403
width ?: number ,
366
404
) : TableCellNode {
0 commit comments