From db7b242c187aec21e6db9cfbfea94d8adf9fcf8a Mon Sep 17 00:00:00 2001 From: Alex Reznik Date: Thu, 25 Apr 2024 11:17:55 +0100 Subject: [PATCH] Refactor table resizer rendering logic (#5957) --- .../src/plugins/TableCellResizer/index.tsx | 51 +++++++++---------- 1 file changed, 24 insertions(+), 27 deletions(-) diff --git a/packages/lexical-playground/src/plugins/TableCellResizer/index.tsx b/packages/lexical-playground/src/plugins/TableCellResizer/index.tsx index a678b205e01..484338f8919 100644 --- a/packages/lexical-playground/src/plugins/TableCellResizer/index.tsx +++ b/packages/lexical-playground/src/plugins/TableCellResizer/index.tsx @@ -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, @@ -61,27 +55,10 @@ function TableCellResizer({editor}: {editor: LexicalEditor}): JSX.Element { useState(null); const [activeCell, updateActiveCell] = useState(null); - const [isSelectingGrid, updateIsSelectingGrid] = useState(false); + const [isMouseDown, updateIsMouseDown] = useState(false); const [draggingDirection, updateDraggingDirection] = useState(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; @@ -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(() => { @@ -102,7 +83,7 @@ function TableCellResizer({editor}: {editor: LexicalEditor}): JSX.Element { }); return; } - + updateIsMouseDown(isMouseDownOnEvent(event)); if (resizerRef.current && resizerRef.current.contains(target as Node)) { return; } @@ -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]); @@ -390,7 +387,7 @@ function TableCellResizer({editor}: {editor: LexicalEditor}): JSX.Element { return (
- {activeCell != null && !isSelectingGrid && ( + {activeCell != null && !isMouseDown && ( <>