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

Refactor table resizer rendering logic #5957

Merged
merged 1 commit into from
Apr 25, 2024
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,11 @@ import {
$getTableRowIndexFromTableCellNode,
$isTableCellNode,
$isTableRowNode,
$isTableSelection,
getDOMCellFromTarget,
TableCellNode,
} from '@lexical/table';
import {calculateZoomLevel} from '@lexical/utils';
import {
$getNearestNodeFromDOMNode,
$getSelection,
COMMAND_PRIORITY_HIGH,
SELECTION_CHANGE_COMMAND,
} from 'lexical';
import {$getNearestNodeFromDOMNode} from 'lexical';
import * as React from 'react';
import {
MouseEventHandler,
Expand Down Expand Up @@ -61,27 +55,10 @@ function TableCellResizer({editor}: {editor: LexicalEditor}): JSX.Element {
useState<MousePosition | null>(null);

const [activeCell, updateActiveCell] = useState<TableDOMCell | null>(null);
const [isSelectingGrid, updateIsSelectingGrid] = useState<boolean>(false);
const [isMouseDown, updateIsMouseDown] = useState<boolean>(false);
const [draggingDirection, updateDraggingDirection] =
useState<MouseDraggingDirection | null>(null);

useEffect(() => {
return editor.registerCommand(
SELECTION_CHANGE_COMMAND,
(payload) => {
const selection = $getSelection();
const isTableSelection = $isTableSelection(selection);

if (isSelectingGrid !== isTableSelection) {
updateIsSelectingGrid(isTableSelection);
}

return false;
},
COMMAND_PRIORITY_HIGH,
);
});

const resetState = useCallback(() => {
updateActiveCell(null);
targetRef.current = null;
Expand All @@ -90,6 +67,10 @@ function TableCellResizer({editor}: {editor: LexicalEditor}): JSX.Element {
tableRectRef.current = null;
}, []);

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

useEffect(() => {
const onMouseMove = (event: MouseEvent) => {
setTimeout(() => {
Expand All @@ -102,7 +83,7 @@ function TableCellResizer({editor}: {editor: LexicalEditor}): JSX.Element {
});
return;
}

updateIsMouseDown(isMouseDownOnEvent(event));
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 is a safety measure in case mouseup is not registered by the browser. I'm not sure if this can ever happen and I'll be happy to hear a second opinion, if this check is required

if (resizerRef.current && resizerRef.current.contains(target as Node)) {
return;
}
Expand Down Expand Up @@ -137,10 +118,26 @@ function TableCellResizer({editor}: {editor: LexicalEditor}): JSX.Element {
}, 0);
};

const onMouseDown = (event: MouseEvent) => {
setTimeout(() => {
updateIsMouseDown(true);
}, 0);
};

const onMouseUp = (event: MouseEvent) => {
setTimeout(() => {
updateIsMouseDown(false);
}, 0);
};

document.addEventListener('mousemove', onMouseMove);
document.addEventListener('mousedown', onMouseDown);
document.addEventListener('mouseup', onMouseUp);

return () => {
document.removeEventListener('mousemove', onMouseMove);
document.removeEventListener('mousedown', onMouseDown);
document.removeEventListener('mouseup', onMouseUp);
};
}, [activeCell, draggingDirection, editor, resetState]);

Expand Down Expand Up @@ -390,7 +387,7 @@ function TableCellResizer({editor}: {editor: LexicalEditor}): JSX.Element {

return (
<div ref={resizerRef}>
{activeCell != null && !isSelectingGrid && (
{activeCell != null && !isMouseDown && (
<>
<div
className="TableCellResizer__resizer TableCellResizer__ui"
Expand Down
Loading