From 1467755a198af4b2c8456bce02eed6be5dcccc19 Mon Sep 17 00:00:00 2001 From: cwstra Date: Mon, 14 Oct 2024 21:40:45 +0000 Subject: [PATCH] Read colgroups when deserializing LexicalTableNode --- packages/lexical-table/src/LexicalTableNode.ts | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/packages/lexical-table/src/LexicalTableNode.ts b/packages/lexical-table/src/LexicalTableNode.ts index 90031636d26..23a249dd0ba 100644 --- a/packages/lexical-table/src/LexicalTableNode.ts +++ b/packages/lexical-table/src/LexicalTableNode.ts @@ -33,6 +33,7 @@ import {$isTableCellNode, TableCellNode} from './LexicalTableCellNode'; import {TableDOMCell, TableDOMTable} from './LexicalTableObserver'; import {TableRowNode} from './LexicalTableRowNode'; import {getTable} from './LexicalTableSelectionHelpers'; +import {PIXEL_VALUE_REG_EXP} from './constants' export type SerializedTableNode = Spread< { @@ -358,6 +359,19 @@ export function $convertTableElement( if (domNode.hasAttribute('data-lexical-row-striping')) { tableNode.setRowStriping(true); } + const colGroup = domNode.querySelector(":scope > colgroup"); + if (colGroup) { + let columns: number[] | undefined = []; + for (const col of colGroup.querySelectorAll(":scope > col")) { + const width = (col as HTMLElement).style.width; + if (!width || !PIXEL_VALUE_REG_EXP.test(width)) { + columns = undefined; + break; + } + columns.push(parseFloat(width)) + } + if (columns) tableNode.setColWidths(columns); + } return {node: tableNode}; }