From bec77f06e66ec415ca3fbbdd208717ae734a4b25 Mon Sep 17 00:00:00 2001 From: Ibrahim El-bastawisi Date: Sat, 8 Mar 2025 03:37:20 +0200 Subject: [PATCH 1/2] Fix unintended touch table cell selection when scrolling Fixes #7308 --- .../lexical-table/src/LexicalTableObserver.ts | 2 ++ .../src/LexicalTableSelectionHelpers.ts | 36 +++++++++++++++++-- 2 files changed, 35 insertions(+), 3 deletions(-) diff --git a/packages/lexical-table/src/LexicalTableObserver.ts b/packages/lexical-table/src/LexicalTableObserver.ts index dcc26803c3e..f2d99a7f79a 100644 --- a/packages/lexical-table/src/LexicalTableObserver.ts +++ b/packages/lexical-table/src/LexicalTableObserver.ts @@ -103,6 +103,7 @@ export class TableObserver { tableSelection: TableSelection | null; hasHijackedSelectionStyles: boolean; isSelecting: boolean; + pointerType: string | null; shouldCheckSelection: boolean; abortController: AbortController; listenerOptions: {signal: AbortSignal}; @@ -129,6 +130,7 @@ export class TableObserver { this.focusCell = null; this.hasHijackedSelectionStyles = false; this.isSelecting = false; + this.pointerType = null; this.shouldCheckSelection = false; this.abortController = new AbortController(); this.listenerOptions = {signal: this.abortController.signal}; diff --git a/packages/lexical-table/src/LexicalTableSelectionHelpers.ts b/packages/lexical-table/src/LexicalTableSelectionHelpers.ts index c719495efa7..cc9cf734d18 100644 --- a/packages/lexical-table/src/LexicalTableSelectionHelpers.ts +++ b/packages/lexical-table/src/LexicalTableSelectionHelpers.ts @@ -247,6 +247,7 @@ export function applyTableHandlers( }; const onPointerDown = (event: PointerEvent) => { + tableObserver.pointerType = event.pointerType; if (event.button !== 0 || !isDOMNode(event.target) || !editorWindow) { return; } @@ -260,9 +261,8 @@ export function applyTableHandlers( // if we go ahead and make the table selection now it will work // Handle case when tapping on a cell with touch device if ( - ((IS_FIREFOX && event.shiftKey) || - (event.pointerType === 'touch' && - !$isTableSelection(prevSelection))) && + IS_FIREFOX && + event.shiftKey && $isSelectionInTable(prevSelection, tableNode) && ($isRangeSelection(prevSelection) || $isTableSelection(prevSelection)) ) { @@ -999,6 +999,36 @@ export function applyTableHandlers( true, ); } + + // Handle case when the pointer type is touch and the current and + // previous selection are collapsed, and the previous anchor and current + // focus cell nodes are different, then we convert it into table selection + if ( + tableObserver.pointerType === 'touch' && + selection.isCollapsed() && + $isRangeSelection(prevSelection) && + prevSelection.isCollapsed() + ) { + const prevAnchorCellNode = $findCellNode( + prevSelection.anchor.getNode(), + ); + if (prevAnchorCellNode && !prevAnchorCellNode.is(focusCellNode)) { + tableObserver.$setAnchorCellForSelection( + $getObserverCellFromCellNodeOrThrow( + tableObserver, + prevAnchorCellNode, + ), + ); + tableObserver.$setFocusCellForSelection( + $getObserverCellFromCellNodeOrThrow( + tableObserver, + focusCellNode, + ), + true, + ); + tableObserver.pointerType = null; + } + } } } else if ( selection && From afcb47da44a3760c22ae6b5b89e8a4f9318f5715 Mon Sep 17 00:00:00 2001 From: Ibrahim El-bastawisi Date: Sat, 8 Mar 2025 04:02:37 +0200 Subject: [PATCH 2/2] Remove outdated comment regarding touch device cell selection handling --- packages/lexical-table/src/LexicalTableSelectionHelpers.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/packages/lexical-table/src/LexicalTableSelectionHelpers.ts b/packages/lexical-table/src/LexicalTableSelectionHelpers.ts index cc9cf734d18..3bee5764aa3 100644 --- a/packages/lexical-table/src/LexicalTableSelectionHelpers.ts +++ b/packages/lexical-table/src/LexicalTableSelectionHelpers.ts @@ -259,7 +259,6 @@ export function applyTableHandlers( // We can't trust Firefox to do the right thing with the selection and // we don't have a proper state machine to do this "correctly" but // if we go ahead and make the table selection now it will work - // Handle case when tapping on a cell with touch device if ( IS_FIREFOX && event.shiftKey &&