Skip to content

Commit

Permalink
[lexical-table][lexical-playground] Bug Fix: Table selection stuck (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexanderReznik authored May 8, 2024
1 parent 89a9701 commit 7e3a79c
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
10 changes: 10 additions & 0 deletions packages/lexical-playground/__tests__/utils/index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -620,6 +620,7 @@ export async function dragMouse(
positionStart = 'middle',
positionEnd = 'middle',
mouseUp = true,
slow = false,
) {
let fromX = fromBoundingBox.x;
let fromY = fromBoundingBox.y;
Expand All @@ -643,6 +644,11 @@ export async function dragMouse(
toY += toBoundingBox.height;
}

if (slow) {
//simulate more than 1 mouse move event to replicate human slow dragging
await page.mouse.move((fromX + toX) / 2, (fromY + toY) / 2);
}

await page.mouse.move(toX, toY);

if (mouseUp) {
Expand Down Expand Up @@ -801,6 +807,10 @@ export async function selectCellsFromTableCords(
page,
await firstRowFirstColumnCell.boundingBox(),
await secondRowSecondCell.boundingBox(),
'middle',
'middle',
true,
true,
);
}

Expand Down
10 changes: 10 additions & 0 deletions packages/lexical-table/src/LexicalTableSelectionHelpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,10 @@ export const getDOMSelection = (
): Selection | null =>
CAN_USE_DOM ? (targetWindow || window).getSelection() : null;

const isMouseDownOnEvent = (event: MouseEvent) => {
return (event.buttons & 1) === 1;
};

export function applyTableHandlers(
tableNode: TableNode,
tableElement: HTMLTableElementWithWithTableSelectionState,
Expand Down Expand Up @@ -104,6 +108,12 @@ export function applyTableHandlers(
const onMouseMove = (moveEvent: MouseEvent) => {
// delaying mousemove handler to allow selectionchange handler from LexicalEvents.ts to be executed first
setTimeout(() => {
if (!isMouseDownOnEvent(moveEvent) && tableObserver.isSelecting) {
tableObserver.isSelecting = false;
editorWindow.removeEventListener('mouseup', onMouseUp);
editorWindow.removeEventListener('mousemove', onMouseMove);
return;
}
const focusCell = getDOMCellFromTarget(moveEvent.target as Node);
if (
focusCell !== null &&
Expand Down

0 comments on commit 7e3a79c

Please sign in to comment.