Repository navigation
Description
Every transformation (exportDOM, importJSON, exportJSON, createDOM) take into account a TableRowNode or TableCellNode's height or width, however trying to parse HTML (even if it was outputted by Lexical itself) doesn't set the height/width of the node. This seems like a mistake, however it's specifically only these two things that don't get imported.
Lexical version: 0.10.0
Steps To Reproduce
- Create a lexical editor with the
TablePluginand theTable,TableRowandTableCellnodes - Implement some functionality for resizing
TableCells orTableRows (I more or less replicated the logic provided in the playground. - Using
$generateNodesFromDOMon some DOM that features a table, with rows that havestyle="height: xx"or cells that havestyle="width: xx"
The current behavior
The width/height in the DOM is ignored. Cell/Row nodes are added without these properties set
The expected behavior
These properties should get set, especially because they're already part of their corresponding classes.
Proposed solution
The constructors of these nodes already support receiving this sizing property, let's look at some code within the cell node. This code is ran to turn a dom node into a lexical table cell node.
export function convertTableCellNodeElement(
domNode: Node,
): DOMConversionOutput {
const domNode_ = domNode as HTMLTableCellElement;
const nodeName = domNode.nodeName.toLowerCase();
const tableCellNode = $createTableCellNode(
nodeName === 'th'
? TableCellHeaderStates.ROW
: TableCellHeaderStates.NO_STATUS,
);
tableCellNode.__colSpan = domNode_.colSpan;
tableCellNode.__rowSpan = domNode_.rowSpan;
const backgroundColor = domNode_.style.backgroundColor;
if (backgroundColor !== '') {
tableCellNode.__backgroundColor = backgroundColor;
}
return {
forChild: (lexicalNode, parentLexicalNode) => {
if ($isTableCellNode(parentLexicalNode) && !$isElementNode(lexicalNode)) {
const paragraphNode = $createParagraphNode();
if (
$isLineBreakNode(lexicalNode) &&
lexicalNode.getTextContent() === '\n'
) {
return null;
}
paragraphNode.append(lexicalNode);
return paragraphNode;
}
return lexicalNode;
},
node: tableCellNode,
};
}I believe the only necessary fix here is to include an additional line which reads the "width/height"
const width = domNode._style.width;
const tableCellNode = $createTableCellNode(
nodeName === 'th'
? TableCellHeaderStates.ROW
: TableCellHeaderStates.NO_STATUS,
domNode_.colSpan,
width || null,
);
tableCellNode.__colSpan =
tableCellNode.__rowSpan = domNode_.rowSpan;
const backgroundColor = domNode_.style.backgroundColor;
if (backgroundColor !== '') {
tableCellNode.__backgroundColor = backgroundColor;
}
Activity
GMchris commentedon May 19, 2023
If the proposed solution works for you, I'd be willings to push a PR with the changes.
fantactuka commentedon May 19, 2023
Makes sense, would appreciate PR for this one!
utkarsh-shrivastav77 commentedon Jun 6, 2023
Hi is this issue still open
alhridoy commentedon Oct 5, 2023
Hi @GMchris I would love to work on it. Please assign me.