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

Improve table selection handling when there are no siblings #3051

Merged
merged 2 commits into from
Sep 22, 2022
Merged
Changes from 1 commit
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
45 changes: 41 additions & 4 deletions packages/lexical-table/src/LexicalTableSelectionHelpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import {
DELETE_CHARACTER_COMMAND,
DELETE_LINE_COMMAND,
DELETE_WORD_COMMAND,
DEPRECATED_$createGridSelection,
DEPRECATED_$isGridSelection,
FOCUS_COMMAND,
FORMAT_TEXT_COMMAND,
Expand Down Expand Up @@ -629,11 +630,11 @@ export function applyTableHandlers(
const isAnchorInside = tableNode.isParentOf(anchorNode);
const isFocusInside = tableNode.isParentOf(focusNode);

const containsPartialTable =
const selectionContainsPartialTable =
(isAnchorInside && !isFocusInside) ||
(isFocusInside && !isAnchorInside);

if (containsPartialTable) {
if (selectionContainsPartialTable) {
tableSelection.clearText();
return true;
}
Expand Down Expand Up @@ -890,11 +891,14 @@ export function applyTableHandlers(
const isAnchorInside = tableNode.isParentOf(anchorNode);
const isFocusInside = tableNode.isParentOf(focusNode);

const containsPartialTable =
const selectionContainsPartialTable =
(isAnchorInside && !isFocusInside) ||
(isFocusInside && !isAnchorInside);

if (containsPartialTable) {
const selectionIsInsideTable =
isAnchorInside && isFocusInside && !tableNode.isSelected();

if (selectionContainsPartialTable) {
const isBackward = selection.isBackward();
const modifiedSelection = $createRangeSelection();
const tableKey = tableNode.getKey();
Expand All @@ -916,6 +920,39 @@ export function applyTableHandlers(
$addHighlightStyleToTable(tableSelection);

return true;
} else if (selectionIsInsideTable) {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This basically just checks if it's a range selection located inside of a table and if all of the cells are selected. If so, it converts the range selection to a grid selection.

const {grid} = tableSelection;

if (
selection.getNodes().filter($isTableCellNode).length ===
grid.rows * grid.columns
) {
const gridSelection = DEPRECATED_$createGridSelection();
const tableKey = tableNode.getKey();
const tableFirstDescendant = tableNode.getFirstDescendant();
const tableLastDescendant = tableNode.getLastDescendant();

const firstCell =
tableFirstDescendant &&
$findMatchingParent(tableFirstDescendant, $isTableCellNode);

const lastCell =
tableLastDescendant &&
$findMatchingParent(tableLastDescendant, $isTableCellNode);
tylerjbainbridge marked this conversation as resolved.
Show resolved Hide resolved

if (firstCell != null && lastCell != null) {
gridSelection.set(
tableKey,
firstCell.getKey(),
lastCell.getKey(),
);

$setSelection(gridSelection);
tableSelection.updateTableGridSelection(gridSelection);

return true;
}
}
}
}

Expand Down