Skip to content

[lexical-table] Bug Fix: Table alignment fails when selecting in non-TL->BR direction - #8883

Merged
etrepum merged 2 commits into
meta:mainfrom
sahiee-dev:fix/table-alignment-selection-direction
Jul 27, 2026
Merged

etrepum merged 2 commits into
meta:mainfrom
sahiee-dev:fix/table-alignment-selection-direction

Conversation

@sahiee-dev

Copy link
Copy Markdown
Contributor

Description

Current behavior

When a TableSelection covers every cell in a table, FORMAT_ELEMENT_COMMAND is expected to align the table itself instead of aligning the text inside each cell.

However, this only worked when the selection started at the top-left cell and ended at the bottom-right cell. The $isFullTableSelection helper compared the anchor and focus node keys directly against map[0][0] and map[last][last], so it only recognised selections made in that specific direction.

Since $computeTableMap always builds the table grid in document order, regardless of where the drag starts, full-table selections made in the other three directions (bottom-right → top-left, top-right → bottom-left, and bottom-left → top-right) failed this check.

As a result, those selections incorrectly fell back to per-cell alignment. The text inside each cell was aligned, but the table itself never moved.

Changes in this PR

This PR replaces the corner-based key comparison with a direction-independent bounding rectangle check. It reuses the existing min/max row and column span calculation that the per-cell fallback path already performs.

A selection is now treated as a full-table selection whenever its bounding rectangle spans from [0,0] to [lastRow,lastCol], regardless of which corner the user starts or ends the drag from.

Since this logic makes $isFullTableSelection unnecessary, the helper has been removed along with its extra $computeTableMap call.

Closes #8880

Test plan

Before

  1. Open the playground and insert a table (Insert → Table).
  2. Drag-select the entire table starting from the bottom-right cell and ending at the top-left cell. The same issue also occurs for top-right → bottom-left and bottom-left → top-right.
  3. Click an alignment button such as Center.

Result: The text inside each cell is centred, but the table itself is not centred on the page.

Screen.Recording.2026-07-27.at.4.54.27.PM.mov

After

Repeat the same steps.

Result: The table itself is centred on the page, while the text inside the cells remains unchanged. Alignment now behaves the same regardless of which corner the selection starts from.

Screen.Recording.2026-07-27.at.4.55.33.PM.mov

Regression coverage has been added at both levels:

  • Unit (packages/lexical-table/src/__tests__/unit/LexicalTableExtension.test.ts)

    • Verifies all four drag directions (TL→BR, BR→TL, TR→BL, BL→TR) correctly align the table while leaving cell paragraphs unchanged.
    • Verifies a full-table selection containing a merged cell, selected in reverse, still aligns the table.
    • Verifies a partial selection made in reverse still falls back to per-cell alignment.
  • E2E (packages/lexical-playground/__tests__/e2e/Tables.spec.mjs)

    • Selects a full table from bottom-right to top-left using real pointer and DOM interactions.
    • Verifies that the <table> receives the alignment class and that no cell paragraph receives an inline text-align style.

The full unit test suite (226 files, 4,923 tests) and the complete Tables.spec.mjs E2E suite (88 tests) pass without any regressions.

…en selecting in non-TL->BR direction

Current behavior: $isFullTableSelection required selection.anchor to be
exactly the top-left cell and selection.focus to be exactly the
bottom-right cell. Since $computeTableMap always builds the grid in
document order regardless of drag direction, any full-table selection
started from a different corner (bottom-right, top-right, or
bottom-left) failed this check and silently fell through to per-cell
alignment instead of aligning the table itself.

Changes in this PR: replaces the corner-key equality check with a
direction-agnostic bounding-rectangle check, reusing the min/max span
computation the per-cell fallback already had. This also removes the
now-redundant $isFullTableSelection helper and its duplicate
$computeTableMap call.

Closes meta#8880

Verified against current main (82cdae1) both by reverting the fix
locally (unit + e2e tests fail exactly on the 3 previously-broken
directions) and by reproducing the bug live on playground.lexical.dev.
Removed comments that explained the change relative to the fix/PR
(referencing "the bug", "meta#8880", "before the fix") rather than
documenting durable behavior for future readers — that context belongs
in the commit message, not in code that outlives it.
@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Jul 27, 2026
@vercel

vercel Bot commented Jul 27, 2026 •

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
lexical Ready Ready Preview Jul 27, 2026 11:29am
lexical-playground Ready Ready Preview Jul 27, 2026 11:29am

Request Review

@etrepum
etrepum added this pull request to the merge queue Jul 27, 2026
Merged via the queue into meta:main with commit c754c13 Jul 27, 2026
48 checks passed
@etrepum etrepum mentioned this pull request Jul 29, 2026

This branch was successfully deployed

2 active deployments
Preview – lexical — 6435da6d Deployed Jul 27, 2026 by vercel[bot]
Preview – lexical-playground — 6435da6d Deployed Jul 27, 2026 by vercel[bot]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. extended-tests Run extended e2e tests on a PR

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Bug: Table alignment fails when selecting in non-TL→BR direction

2 participants