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
Show file tree
Hide file tree
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
41 changes: 37 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,35 @@ 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 firstCell = tableNode
.getFirstChildOrThrow()
.getFirstChild();

const lastCell = tableNode.getLastChildOrThrow().getLastChild();

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

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

return true;
}
}
}
}

Expand Down
1 change: 1 addition & 0 deletions packages/lexical/flow/Lexical.js.flow
Original file line number Diff line number Diff line change
Expand Up @@ -681,6 +681,7 @@ declare export class ElementNode extends LexicalNode {
getFirstChild<T: LexicalNode>(): null | T;
getFirstChildOrThrow<T: LexicalNode>(): T;
getLastChild<T: LexicalNode>(): null | T;
getLastChildOrThrow<T: LexicalNode>(): T;
getChildAtIndex<T: LexicalNode>(index: number): null | T;
getTextContent(): string;
getDirection(): 'ltr' | 'rtl' | null;
Expand Down
7 changes: 7 additions & 0 deletions packages/lexical/src/nodes/LexicalElementNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,13 @@ export class ElementNode extends LexicalNode {
}
return $getNodeByKey<T>(children[childrenLength - 1]);
}
getLastChildOrThrow<T extends LexicalNode>(): T {
const lastChild = this.getLastChild<T>();
if (lastChild === null) {
invariant(false, 'Expected node %s to have a last child.', this.__key);
}
return lastChild;
}
getChildAtIndex<T extends LexicalNode>(index: number): null | T {
const self = this.getLatest();
const children = self.__children;
Expand Down