Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP][lexical-table] Feature: Use custom selection behavior for table cell triple click #7005

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions packages/lexical-table/src/LexicalTablePluginHelpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,21 @@
*/

import {
$findMatchingParent,
$insertFirst,
$insertNodeToNearestRoot,
$unwrapAndFilterDescendants,
mergeRegister,
} from '@lexical/utils';
import {
$createParagraphNode,
$getNearestNodeFromDOMNode,
$isElementNode,
$isTextNode,
CLICK_COMMAND,
COMMAND_PRIORITY_EDITOR,
ElementNode,
isDOMNode,
LexicalEditor,
NodeKey,
} from 'lexical';
Expand Down Expand Up @@ -250,6 +256,29 @@ export function registerTableSelectionObserver(
};
}

function $tableClickCommand(event: MouseEvent): boolean {
if (event.detail !== 3 || !isDOMNode(event.target)) {
return false;
}
const startNode = $getNearestNodeFromDOMNode(event.target);
if (startNode === null) {
return false;
}
const blockNode = $findMatchingParent(
startNode,
(node): node is ElementNode => $isElementNode(node) && !node.isInline(),
);
if (blockNode === null) {
return false;
}
const rootNode = blockNode.getParent();
if (!$isTableCellNode(rootNode)) {
return false;
}
blockNode.select(0);
return true;
}

/**
* Register the INSERT_TABLE_COMMAND listener and the table integrity transforms. The
* table selection observer should be registered separately after this with
Expand All @@ -271,5 +300,10 @@ export function registerTablePlugin(editor: LexicalEditor): () => void {
editor.registerNodeTransform(TableNode, $tableTransform),
editor.registerNodeTransform(TableRowNode, $tableRowTransform),
editor.registerNodeTransform(TableCellNode, $tableCellTransform),
editor.registerCommand(
CLICK_COMMAND,
$tableClickCommand,
COMMAND_PRIORITY_EDITOR,
),
);
}
Loading