Skip to content

Bug: TableRowNode and TableCellNode's importDom methods don't take into account the element's height and width respectively #4518

Closed

Description

@GMchris

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

  1. Create a lexical editor with the TablePlugin and the Table, TableRow and TableCell nodes
  2. Implement some functionality for resizing TableCells or TableRows (I more or less replicated the logic provided in the playground.
  3. Using $generateNodesFromDOM on some DOM that features a table, with rows that have style="height: xx" or cells that have style="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

  1. GMchris commented on May 19, 2023

    @GMchris
    ContributorAuthor

    If the proposed solution works for you, I'd be willings to push a PR with the changes.

  2. fantactuka commented on May 19, 2023

    @fantactuka
    Contributor

    Makes sense, would appreciate PR for this one!

  3. added
    good first issue
    tables
    on May 19, 2023
  4. utkarsh-shrivastav77 commented on Jun 6, 2023

    @utkarsh-shrivastav77

    Hi is this issue still open

  5. alhridoy commented on Oct 5, 2023

    @alhridoy

    Hi @GMchris I would love to work on it. Please assign me.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    good first issuetables

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions