Skip to content

Commit

Permalink
TableSelection: Fix backward range selection hijack logic (facebook#1765
Browse files Browse the repository at this point in the history
)

* Add tests for special Table selection behavior

* Fix backward range selection hijacking logic
  • Loading branch information
tylerjbainbridge authored Apr 15, 2022
1 parent 92c6eac commit 7aa228d
Showing 1 changed file with 7 additions and 14 deletions.
21 changes: 7 additions & 14 deletions packages/lexical-table/src/LexicalTableSelectionHelpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ import {
$isGridSelection,
$isParagraphNode,
$isRangeSelection,
$isTextNode,
$setSelection,
DELETE_CHARACTER_COMMAND,
FORMAT_TEXT_COMMAND,
Expand Down Expand Up @@ -799,27 +798,21 @@ export function applyTableHandlers(
(isFocusInside && !isAnchorInside);
if (containsPartialTable) {
const isBackward = selection.isBackward();
const startNode = isBackward ? focusNode : anchorNode;
const modifiedSelection = $createRangeSelection();
const tableIndex = tableNode.getIndexWithinParent();
const parentKey = tableNode.getParentOrThrow().getKey();
isRangeSelectionHijacked = true;
(isBackward
? modifiedSelection.focus
: modifiedSelection.anchor
).set(
startNode.getKey(),
(isBackward ? selection.focus : selection.anchor).offset,
$isTextNode(startNode) ? 'text' : 'element',
modifiedSelection.anchor.set(
selection.anchor.key,
selection.anchor.offset,
selection.anchor.type,
);
(isBackward
? modifiedSelection.anchor
: modifiedSelection.focus
).set(
// Set selection to before or after table on the root node.
modifiedSelection.focus.set(
parentKey,
isBackward ? tableIndex - 1 : tableIndex + 1,
'element',
);
isRangeSelectionHijacked = true;
$setSelection(modifiedSelection);
$addHighlightStyleToTable(tableSelection);
return true;
Expand Down

0 comments on commit 7aa228d

Please sign in to comment.