Skip to content

feat(table): paste bounded clipboard regions into existing cells - #996

Open
huyanxius wants to merge 8 commits into
mdx-editor:mainfrom
huyanxius:fix/784-table-region-paste
Open

huyanxius wants to merge 8 commits into
mdx-editor:mainfrom
huyanxius:fix/784-table-region-paste

Conversation

@huyanxius

@huyanxius huyanxius commented Oct 15, 2026 •

Copy link
Copy Markdown
Contributor

Refs #784.

Behavior

  • Paste a standalone HTML table or TSV region of at least 2×2 cells into a table cell, using it as the top-left anchor. TSV whose first column is empty in every row is not treated as a region.
  • Overwrite the rectangle, preserve empty cells, and grow the table right/down within the 10,000-cell destination bound.
  • Prefer a valid HTML region, otherwise use valid TSV. Support quoted TSV fields, embedded line breaks, escaped quotes, and trailing empty fields. HTML formatting is reduced to text; merged-cell coverage is empty.
  • Only a command sent by the cell editor itself can replace a region. Editable nested JSX retains its own paste handling.
  • Rejected/oversized table-shaped or tabbed data uses plain text at the caret, not a silent no-op or nested table. Ordinary prose retains the existing handler.

History and scope

The region is one parent history entry. Pending anchor text is merged into its baseline using a public Lexical history tag, so one Undo restores that text without an extra pending-text undo step. After region paste, focus moves to the parent so keyboard and toolbar Undo work immediately. Untouched cell histories remain local.

This version is based on the landed, table-scoped #1004 fix. There are no additional core/global history changes or direct history-stack writes in this PR. Region replacement is limited to tables in the root editor. Tables inside directive/JSX containers keep caret text paste instead of introducing cross-editor history routing.

Verification

Current head: afa47d43a41ade74e3b5473c3bceef91e03dd658.

CI for this head passes all four jobs. The PR merge-test tree matches the published head's tree.

  • Full lint and typecheck pass.
  • Unit suite: 436 passed; the existing 1 skip and 1 todo are unchanged.
  • Chromium, Firefox, and WebKit: 207/207 browser cases passed, including all 36 table-region paste cases.
  • JS/declaration and docs builds, the Lexical package graph, packed React 18/19 consumers, and cross-version Markdown checks pass.
  • Local affected suites: 60 passed. Prettier and git diff --check pass.

Local full lint/typecheck were resource-killed, and local browser startup was blocked before page creation; the full results above are from CI.

Coverage uses synthetic DOM clipboard payloads with real browser keyboard/focus. The nested JSX fixture explicitly focuses its editing host and prepares its DOM caret before paste. Native OS clipboard permissions and external spreadsheet application interoperability are not certified.

@huyanxius
huyanxius marked this pull request as ready for review October 16, 2026 06:03
huyanxius added a commit to huyanxius/editor that referenced this pull request Oct 16, 2026
Reuse the fixture-only hunk from f867088 (mdx-editor#996). Keep all existing Markdown, image, and focus assertions.
@petyosi

petyosi commented Oct 27, 2026

Copy link
Copy Markdown
Contributor

Thanks for this. Pasting a spreadsheet region into a table is useful, but this version changes too much default behaviour for me to merge.

  • Any paste that contains a tab is now treated as TSV. Pasting "\tindented" at the end of a cell empties that cell and overwrites the next one. Pasting a code snippet splits it across rows and columns.
  • Custom undo routing. Every cell editor gets critical-priority UNDO, REDO, CAN_UNDO and PASTE handlers, and the paste writes into @lexical/history internals. This has edge cases:
    • Paste, type in another cell A, undo that typing, then undo the paste. Redo from A now replays A's stale local redo, and the paste can no longer be redone.
    • For a table inside a directive or JSX editor, Undo after a paste looks unreachable, because the top history entry belongs to the outer editor.
  • Unrelated changes. The save() and reconcile changes apply to every cell edit, not only to pastes. They look like a real fix for an existing bug: after a cell commit, Undo in the root restores the markdown, but the cell keeps showing the old text. That fix needs its own PR.

I would accept a much smaller version:

  1. Trigger only on an HTML table, or on TSV with at least 2×2 cells.
  2. Record the paste as one parent history entry. No routing between cell and parent histories, and no CONTRIBUTING section.
  3. Send the undo/reconcile fix as a separate PR with its own test.
  4. Leave out the Firefox fixture hunk. It is on main now.

Copy link
Copy Markdown
Contributor Author

Updated normally to 97d02ce75854399958df4519c13d7a8f56acf7d6 after narrowing the paste delta and splitting the core dependency into draft #1004.

The paste-specific changes now:

  • Trigger only for a standalone HTML table, or TSV with at least two parsed records containing at least two fields each. Single-row/tab-indented/code snippets keep the normal paste path.
  • Keep a region as one parent-history operation. Table-plugin Undo/Redo routing and direct history-stack writes are removed; untouched cells retain their local history.
  • Preserve the original rectangular overwrite/growth, empty cells, pending anchor text, bounded parsing/destination size, and stale-blur protections, including real directive, JSX, and multi-level nesting.
  • Remove the CONTRIBUTING addition and the already-landed Firefox fixture delta.

The ordinary edit/blur reconciliation and shared export/history repair are reviewed separately in #1004. They were reproduced on current main before repair; this branch includes that dependency, so those files remain in this PR's main-relative diff until the core repair lands. Relative to the core head, this PR has only the nine table-paste source/example/test files.

Verification on the exact published tree: 318 unit tests passed (existing 1 skip/1 todo retained), full lint, typecheck, library/declaration build, and the 24-package Lexical version check passed. Independent review reran 114 focused tests successfully. The old remote head and the standalone core head are both ancestors; no history rewrite was used.

Exact-head CI is running. Local browser startup was environment-blocked before a page opened, so the native keyboard/focus, packaged-consumer, and cross-version CI gates are still pending for this head. This update supersedes the previous PR body's custom history-routing contract and readiness statement; I am not claiming the revised head is fully verified yet.

Copy link
Copy Markdown
Contributor Author

Final verification for b84f36b450a8e790cf88241ca8f019e08000112c: exact-head CI now passes all four jobs. Chromium, Firefox, and WebKit pass 180/180 browser cases, including all 24 table-region paste cases. Lint, typecheck, unit tests, JS/declaration and docs builds, the Lexical version graph, packed React 18/19 consumers, and cross-version Markdown checks also pass.

The first revised CI run exposed five failures in the shared core dependency, not the paste contract. They are fixed in #1004 (dd014f8a): parent Undo/Redo no longer commits or discards an untouched sibling's pending cell text during native focus changes, and cell-local history resets no longer disable the unrelated root toolbar. Existing assertions were retained, with stronger final Redo focus/caret coverage; no tests were skipped to obtain this result. The standalone core CI passes 156/156 browser cases.

The exact integrated tree also passes 321 local unit tests (the existing 1 skip/1 todo retained), full lint/typecheck/build, and independent re-review of the corrective changes. This was a normal update preserving the old branch and core ancestry. The narrowed paste delta remains nine source/example/test files relative to the standalone core repair; no custom table Undo/Redo routing or direct stack writes were reintroduced.

Coverage uses real browser keyboard/focus plus synthetic clipboard events; native OS clipboard and external spreadsheet application integration were not separately tested. No upstream merge or deployment has been performed.

@petyosi petyosi left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for narrowing this. Dropping the history routing and the CONTRIBUTING section is what I asked for. A few things are still needed:

1. Indented code is still pasted as a grid. The TSV check accepts lines that start with a tab, so "\tfoo()\n\tbar()" (2×2), "\tA\tint\n\tB\tstring" (2×3) or a Makefile recipe still empties the cell and overwrites the neighbouring cell. That was the original complaint. Please reject TSV whose first column is empty on every row. Any HTML table also counts, even 1×1, so a single copied spreadsheet cell replaces the whole cell content instead of inserting at the caret. Please apply the 2×2 rule to HTML tables too.

2. A paste inside a nested editor in a cell overwrites the table. The PASTE_COMMAND handler (TableEditor.tsx:568-570) ignores the editor that sent the command. Lexical sends commands up through parent editors by priority, so this critical-priority handler runs before an inline JSX editor's own paste handler. Pasting a\tb\nc\td inside a JSX component in a cell replaces that cell, including the component, and its neighbours. Please return false when the source editor is not the cell editor, and add a test with an editable inline JSX component in a cell.

3. Undo right after the paste does nothing. The destination cell remounts with an empty local history, so Ctrl/Cmd+Z and the toolbar Undo do nothing until focus leaves the table. The browser test asserts this. Without routing, one option is to focus outside the cell after the paste. In any case, Undo should reverse the paste the user just made.

4. Pending anchor text adds a second history step. If the anchor cell has unsaved text, save() commits it before the paste, so Undo first removes the paste and then the text. Please either record both as one entry or state it plainly in the docs.

5. It depends on #1004. This branch includes #1004 and uses ensureEditorHistoryBaseline and persistedContents. I've asked for #1004 to be cut down to the table-cell fix. Please rebase onto that once it lands.

Smaller: rejected or oversized regions, and any HTML over 1 MB that contains <table, are dropped silently via preventDefault. Please fall back to the normal paste instead of doing nothing.

Copy link
Copy Markdown
Contributor Author

Updated to b941070633f9d54bb65cce4527d78520929107de for this review, with current main integrated normally; no history rewrite.

  • TSV with an empty first column in every row now stays in the cell. HTML also needs at least 2×2 cells. Regression cases include both indented examples, Makefile recipes, quoted empty first fields, and one-dimensional HTML.
  • The region handler first checks sourceEditor === editor. An editable inline JSX component inside a cell retains its own paste handling; its component and neighboring cells are not replaced.
  • Region paste now moves focus to the parent, so immediate keyboard and toolbar Undo reach the paste entry. Pending anchor text is merged into that entry's baseline using Lexical's public history tag: one Undo restores the pending text, without an additional pending-text undo step. Repeated pastes remain separate steps.
  • The branch now uses the landed, narrowed fix(core): reconcile nested history and mounted table cells #1004 implementation. The main-relative diff contains only nine table-paste source/example/test files, with no global history-helper or core-export delta. To keep that boundary honest, region paste is limited to tables in the root editor; tables inside directive/JSX containers use caret text paste rather than reviving cross-editor history coordination.
  • Rejected or oversized data is no longer silently consumed. Table-shaped/tabbed fallback content is inserted as plain text at the caret, with inline line breaks, rather than overwriting cells. This also fixes a reproduced fallback trap: the default HTML importer created a nested TableNode from a single copied cell, which the existing cell save could drop. Tests now verify no nested table and preservation after blur and Markdown reload, including HTML-only empty cells.

Local verification: the full unit suite passed 433 tests before the final empty-HTML cases; the final affected suites pass 60/60. Prettier, diff checks, and the 24-package Lexical version check pass. Local lint/typecheck were resource-killed and browser startup was environment-blocked before page creation, so I am not claiming those passed locally.

Exact-head CI is running, including the native keyboard/focus cases in Chromium, Firefox, and WebKit. The PR description has been corrected to remove the obsolete routing and dropped-paste contract.

Copy link
Copy Markdown
Contributor Author

Verification update for afa47d43a41ade74e3b5473c3bceef91e03dd658: CI now passes all four jobs. The PR merge-test tree matches the published head. Full lint/typecheck, 436 unit tests (existing 1 skip/1 todo unchanged), JS/declaration and docs builds, the Lexical graph, packed React 18/19 consumers, and cross-version Markdown checks pass. Chromium, Firefox, and WebKit pass 207/207 browser cases, including all 36 table-region paste cases.

The initial CI failures are addressed: the parser's closure-mutated first-column flag was made explicit for typed lint, and the new nested-JSX browser fixture now activates the cell, focuses the nested editing host, and prepares its DOM caret before paste. The failed runs showed the caret was outside that editor, including Firefox failing the focus precondition before paste. The fixture now asserts both focus and caret location; component, pasted-content, and neighboring-cell assertions are retained. No tests were skipped or paste assertions removed to get this result.

The fixes for the review remain confined to nine table-paste source/example/test files relative to current main. There is no global history-helper/core-export delta or direct history-stack manipulation. As documented above, region replacement is root-table-only; tables inside directive/JSX containers retain safe caret text paste. Browser coverage uses synthetic clipboard payloads with real keyboard/focus, not native OS clipboard or external spreadsheet interoperability.

This branch has not been deployed

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants