Skip to content

Commit

Permalink
Add listeners only when required
Browse files Browse the repository at this point in the history
  • Loading branch information
areznik10 committed Apr 2, 2024
1 parent c7ef4ae commit a64352c
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 17 deletions.
2 changes: 2 additions & 0 deletions packages/lexical-table/src/LexicalTableObserver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ export class TableObserver {
editor: LexicalEditor;
tableSelection: TableSelection | null;
hasHijackedSelectionStyles: boolean;
inSelectionProgress: boolean;

constructor(editor: LexicalEditor, tableNodeKey: string) {
this.isHighlightingCells = false;
Expand All @@ -94,6 +95,7 @@ export class TableObserver {
this.focusCell = null;
this.hasHijackedSelectionStyles = false;
this.trackTable();
this.inSelectionProgress = false;
}

getTable(): TableDOMTable {
Expand Down
46 changes: 29 additions & 17 deletions packages/lexical-table/src/LexicalTableSelectionHelpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,21 +88,25 @@ export function applyTableHandlers(

attachTableObserverToTableElement(tableElement, tableObserver);

const onMouseUp = () => {
editorWindow.removeEventListener('mouseup', onMouseUp);
editorWindow.removeEventListener('mousemove', onMouseMove);
};

const onMouseMove = (moveEvent: MouseEvent) => {
const focusCell = getDOMCellFromTarget(moveEvent.target as Node);
if (
focusCell !== null &&
(tableObserver.anchorX !== focusCell.x ||
tableObserver.anchorY !== focusCell.y)
) {
moveEvent.preventDefault();
tableObserver.setFocusCellForSelection(focusCell);
}
const createMouseHandlers = () => {
const onMouseUp = () => {
tableObserver.inSelectionProgress = false;
editorWindow.removeEventListener('mouseup', onMouseUp);
editorWindow.removeEventListener('mousemove', onMouseMove);
};

const onMouseMove = (moveEvent: MouseEvent) => {
const focusCell = getDOMCellFromTarget(moveEvent.target as Node);
if (
focusCell !== null &&
(tableObserver.anchorX !== focusCell.x ||
tableObserver.anchorY !== focusCell.y)
) {
moveEvent.preventDefault();
tableObserver.setFocusCellForSelection(focusCell);
}
};
return {onMouseMove: onMouseMove, onMouseUp: onMouseUp};
};

tableElement.addEventListener('mousedown', (event: MouseEvent) => {
Expand All @@ -121,6 +125,8 @@ export function applyTableHandlers(
tableObserver.setAnchorCellForSelection(anchorCell);
}

const {onMouseUp, onMouseMove} = createMouseHandlers();
tableObserver.inSelectionProgress = true;
editorWindow.addEventListener('mouseup', onMouseUp);
editorWindow.addEventListener('mousemove', onMouseMove);
}, 0);
Expand Down Expand Up @@ -717,8 +723,14 @@ export function applyTableHandlers(
getObserverCellFromCellNode(focusCellNode),
true,
);
editorWindow.addEventListener('mouseup', onMouseUp);
editorWindow.addEventListener('mousemove', onMouseMove);
if (!tableObserver.inSelectionProgress) {
setTimeout(() => {
const {onMouseUp, onMouseMove} = createMouseHandlers();
tableObserver.inSelectionProgress = true;
editorWindow.addEventListener('mouseup', onMouseUp);
editorWindow.addEventListener('mousemove', onMouseMove);
}, 0);
}
}
}
} else if (
Expand Down

0 comments on commit a64352c

Please sign in to comment.