Bug: [lexical-table] Once a document has a table, every cut goes through the table's handler, even one nowhere near the table
Lexical version: 0.52.0 (the current latest), Chromium 153 on Linux. It reproduces on playground.lexical.dev (0.52.0+dev.esm), and the code is unchanged on main at f0fefed.
Steps To Reproduce
- In the playground, clear the editor, type
# Title(the markdown shortcut makes it a heading) and press Enter. - Insert a table: Insert → Table → Confirm.
- Press Ctrl+End to go to the paragraph after the table, and type
x y. - Select everything (Ctrl+A twice: the playground's first Ctrl+A selects the current block) and press Ctrl+X.
Without step 2, the same steps leave one empty paragraph.
Link to code example: stock packages, no React. It prints ["heading"], and the LOW-priority listener never runs. With the $createTableNodeWithDimensions line removed, it prints LOW cut listener ran, then ["paragraph"].
import {$createHeadingNode, HeadingNode, registerRichText} from '@lexical/rich-text';
import {
$createTableNodeWithDimensions,
registerTablePlugin,
registerTableSelectionObserver,
TableCellNode,
TableNode,
TableRowNode,
} from '@lexical/table';
import {
$createParagraphNode,
$createTextNode,
$getRoot,
$selectAll,
COMMAND_PRIORITY_LOW,
createEditor,
CUT_COMMAND,
} from 'lexical';
const root = document.createElement('div');
root.contentEditable = 'true';
document.body.append(root);
const editor = createEditor({
nodes: [HeadingNode, TableNode, TableRowNode, TableCellNode],
onError: (error) => {
throw error;
},
});
editor.setRootElement(root);
registerRichText(editor);
registerTablePlugin(editor);
registerTableSelectionObserver(editor);
editor.registerCommand(
CUT_COMMAND,
() => {
console.log('LOW cut listener ran');
return false;
},
COMMAND_PRIORITY_LOW,
);
editor.update(
() => {
$getRoot().append(
$createHeadingNode('h1').append($createTextNode('Title')),
$createTableNodeWithDimensions(1, 2, false),
$createParagraphNode().append($createTextNode('x y')),
);
},
{discrete: true},
);
editor.update(() => $selectAll(), {discrete: true});
editor.dispatchCommand(
CUT_COMMAND,
new ClipboardEvent('cut', {clipboardData: new DataTransfer()}),
);
setTimeout(() => {
console.log(editor.read(() => $getRoot().getChildren().map((node) => node.getType())));
}, 100);The current behavior
An empty heading is left. Without the table, rich text's cut widens the whole-document range to the blocks themselves and leaves an empty paragraph (#5835).
The table took the cut. applyTableHandlers registers a CUT_COMMAND listener for each table at COMMAND_PRIORITY_HIGH (LexicalTableSelectionHelpers.ts#L893-L920). It never checks whether the range is in its table. For any RangeSelection in the document, it:
- copies the range;
- calls
$deleteCellHandler, which returnsfalsefor a range outside the table; - calls
selection.removeText()and returnstrue.
The return true came with #6596, the fix for #6590, where a cut went wrong once the document held a table. Since the handler still answers every range, once a document holds a table no cut listener below COMMAND_PRIORITY_HIGH runs for a range selection, wherever the range is:
- Rich text's own cut, at
COMMAND_PRIORITY_EDITOR(index.ts#L579-L617, registered at L1982-L1989). So a whole-document cut isn't widened, and neither of its updates is taggedCUT_TAG(#8649). An update listener sees no cut. - Any other listener at
COMMAND_PRIORITY_NORMALorCOMMAND_PRIORITY_LOW. #7933 lowered the table's handlers from CRITICAL to HIGH so that a plugin can guard cuts, but only a CRITICAL one can.
I checked this in Chromium with real Ctrl+X, on the document from the example:
| cut | without a table | with a table |
|---|---|---|
| select all | paragraph[] left; NORMAL and LOW listeners run; 2 CUT_TAG updates |
heading[] left; no listener runs; no CUT_TAG update |
x in the last paragraph |
x removed and copied; listeners run; 1 CUT_TAG update |
x removed and copied; no listener runs; no CUT_TAG update |
The expected behavior
A table answers the cuts that concern it: a table selection, or a range with an end in the table, which $deleteCellHandler widens around the table first. Any other range goes on to the editor's own cut and to other listeners, as it would with no table in the document.
This does that:
event => {
const selection = $getSelection();
if (selection) {
if (!($isTableSelection(selection) || $isRangeSelection(selection))) {
return false;
}
+ if (
+ $isRangeSelection(selection) &&
+ !tableNode.isParentOf(selection.anchor.getNode()) &&
+ !tableNode.isParentOf(selection.focus.getNode())
+ ) {
+ return false;
+ }
// Copying to the clipboard is async so we must capture the data
// before we delete itI checked it against a patched copy of 0.52.0's build, in Chromium with real Ctrl+X:
- Select all with a table: an empty paragraph is left, as with no table. The NORMAL and LOW listeners run, and both of rich text's updates carry
CUT_TAG. - Text outside the table (#6590's case): the same text is copied and removed once, and the listeners run.
- A range from before the table to after it: the same nodes are removed and the same text copied as today.
- What stays the table's: unchanged. That's a range in a cell's text, a table selection of the whole table, and a range from outside the table into a cell.
One existing test changes: "Cut table in the middle of a range selection" (LexicalTableNode.test.tsx#L857-L897). It mounts only the TablePlugin, so it relies on the table's handler cutting a range whose ends are both outside the table. With the fix, that range is the text extension's, and the test mounts none. With rich text registered it gives the same <p><br></p>, but only once rich text's cut has finished, since it removes after its copy resolves. The test needs RichTextPlugin and an await.
Impact of fix
Every editor with tables and cut handling of its own. Once a document has a table anywhere, a plugin that guards or rewrites cuts below CRITICAL priority never runs. A read-only region, like #7933's use case, can then be cut out. A whole-document cut also keeps the first block's type. We hit it in an editor with inline math built on Lexical: its cut rules for formulas never ran in a document that held a table.
Source: facebook/lexical