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

[lexical-table] Bug Fix: Fix unintended touch table cell selection when scrolling #7309

Merged
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
2 changes: 2 additions & 0 deletions packages/lexical-table/src/LexicalTableObserver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ export class TableObserver {
tableSelection: TableSelection | null;
hasHijackedSelectionStyles: boolean;
isSelecting: boolean;
pointerType: string | null;
shouldCheckSelection: boolean;
abortController: AbortController;
listenerOptions: {signal: AbortSignal};
Expand All @@ -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};
Expand Down
37 changes: 33 additions & 4 deletions packages/lexical-table/src/LexicalTableSelectionHelpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,7 @@ export function applyTableHandlers(
};

const onPointerDown = (event: PointerEvent) => {
tableObserver.pointerType = event.pointerType;
if (event.button !== 0 || !isDOMNode(event.target) || !editorWindow) {
return;
}
Expand All @@ -258,11 +259,9 @@ 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) ||
(event.pointerType === 'touch' &&
!$isTableSelection(prevSelection))) &&
IS_FIREFOX &&
event.shiftKey &&
$isSelectionInTable(prevSelection, tableNode) &&
($isRangeSelection(prevSelection) || $isTableSelection(prevSelection))
) {
Expand Down Expand Up @@ -999,6 +998,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;
Copy link
Contributor Author

@ibastawisi ibastawisi Mar 8, 2025

Choose a reason for hiding this comment

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

Maybe we should reset the flag regardless of anchor and focus being different, this would only trigger table selection on consecutive tabs on different cells without selection changes between them..

The downside is inconsistent behavior and maybe the flag state gets lost if multiple discrete selection changes happen following a single tab.

}
}
}
} else if (
selection &&
Expand Down
Loading