From fa33aa08abcc929aa1ba52ad01728ea0d1f0a568 Mon Sep 17 00:00:00 2001 From: David Luzar Date: Wed, 4 Oct 2023 16:18:22 +0200 Subject: [PATCH] refactor: refactor event globals to differentiate from `lastPointerUp` (#7084) --- src/components/App.tsx | 62 +++++++++++++++++++++++++----------------- 1 file changed, 37 insertions(+), 25 deletions(-) diff --git a/src/components/App.tsx b/src/components/App.tsx index 2e7c468eb0b8..2372f0a229e9 100644 --- a/src/components/App.tsx +++ b/src/components/App.tsx @@ -491,8 +491,9 @@ class App extends React.Component { private iFrameRefs = new Map(); hitLinkElement?: NonDeletedExcalidrawElement; - lastPointerDown: React.PointerEvent | null = null; - lastPointerUp: React.PointerEvent | PointerEvent | null = null; + lastPointerDownEvent: React.PointerEvent | null = null; + lastPointerUpEvent: React.PointerEvent | PointerEvent | null = + null; lastViewportPosition = { x: 0, y: 0 }; constructor(props: AppProps) { @@ -3736,10 +3737,10 @@ class App extends React.Component { isTouchScreen: boolean, ) => { const draggedDistance = distance2d( - this.lastPointerDown!.clientX, - this.lastPointerDown!.clientY, - this.lastPointerUp!.clientX, - this.lastPointerUp!.clientY, + this.lastPointerDownEvent!.clientX, + this.lastPointerDownEvent!.clientY, + this.lastPointerUpEvent!.clientX, + this.lastPointerUpEvent!.clientY, ); if ( !this.hitLinkElement || @@ -3750,7 +3751,7 @@ class App extends React.Component { return; } const lastPointerDownCoords = viewportCoordsToSceneCoords( - this.lastPointerDown!, + this.lastPointerDownEvent!, this.state, ); const lastPointerDownHittingLinkIcon = isPointHittingLink( @@ -3760,7 +3761,7 @@ class App extends React.Component { this.device.isMobile, ); const lastPointerUpCoords = viewportCoordsToSceneCoords( - this.lastPointerUp!, + this.lastPointerUpEvent!, this.state, ); const lastPointerUpHittingLinkIcon = isPointHittingLink( @@ -4465,7 +4466,8 @@ class App extends React.Component { return; } - this.lastPointerDown = event; + this.lastPointerDownEvent = event; + this.setState({ lastPointerDownWith: event.pointerType, cursorButton: "down", @@ -4605,14 +4607,14 @@ class App extends React.Component { event: React.PointerEvent, ) => { this.removePointer(event); - this.lastPointerUp = event; + this.lastPointerUpEvent = event; const scenePointer = viewportCoordsToSceneCoords( { clientX: event.clientX, clientY: event.clientY }, this.state, ); const clicklength = - event.timeStamp - (this.lastPointerDown?.timeStamp ?? 0); + event.timeStamp - (this.lastPointerDownEvent?.timeStamp ?? 0); if (this.device.isMobile && clicklength < 300) { const hitElement = this.getElementAtPosition( scenePointer.x, @@ -5366,7 +5368,9 @@ class App extends React.Component { const [gridX, gridY] = getGridPoint( sceneX, sceneY, - this.lastPointerDown?.[KEYS.CTRL_OR_CMD] ? null : this.state.gridSize, + this.lastPointerDownEvent?.[KEYS.CTRL_OR_CMD] + ? null + : this.state.gridSize, ); const embedLink = getEmbedLink(link); @@ -5416,7 +5420,9 @@ class App extends React.Component { const [gridX, gridY] = getGridPoint( sceneX, sceneY, - this.lastPointerDown?.[KEYS.CTRL_OR_CMD] ? null : this.state.gridSize, + this.lastPointerDownEvent?.[KEYS.CTRL_OR_CMD] + ? null + : this.state.gridSize, ); const topLayerFrame = this.getTopLayerFrameAtSceneCoords({ @@ -5593,7 +5599,9 @@ class App extends React.Component { const [gridX, gridY] = getGridPoint( pointerDownState.origin.x, pointerDownState.origin.y, - this.lastPointerDown?.[KEYS.CTRL_OR_CMD] ? null : this.state.gridSize, + this.lastPointerDownEvent?.[KEYS.CTRL_OR_CMD] + ? null + : this.state.gridSize, ); const topLayerFrame = this.getTopLayerFrameAtSceneCoords({ @@ -5651,7 +5659,9 @@ class App extends React.Component { const [gridX, gridY] = getGridPoint( pointerDownState.origin.x, pointerDownState.origin.y, - this.lastPointerDown?.[KEYS.CTRL_OR_CMD] ? null : this.state.gridSize, + this.lastPointerDownEvent?.[KEYS.CTRL_OR_CMD] + ? null + : this.state.gridSize, ); const frame = newFrameElement({ @@ -6773,17 +6783,17 @@ class App extends React.Component { } if (isEraserActive(this.state)) { const draggedDistance = distance2d( - this.lastPointerDown!.clientX, - this.lastPointerDown!.clientY, - this.lastPointerUp!.clientX, - this.lastPointerUp!.clientY, + this.lastPointerDownEvent!.clientX, + this.lastPointerDownEvent!.clientY, + this.lastPointerUpEvent!.clientX, + this.lastPointerUpEvent!.clientY, ); if (draggedDistance === 0) { const scenePointer = viewportCoordsToSceneCoords( { - clientX: this.lastPointerUp!.clientX, - clientY: this.lastPointerUp!.clientY, + clientX: this.lastPointerUpEvent!.clientX, + clientY: this.lastPointerUpEvent!.clientY, }, this.state, ); @@ -7039,14 +7049,16 @@ class App extends React.Component { if ( hitElement && - this.lastPointerUp && - this.lastPointerDown && - this.lastPointerUp.timeStamp - this.lastPointerDown.timeStamp < 300 && + this.lastPointerUpEvent && + this.lastPointerDownEvent && + this.lastPointerUpEvent.timeStamp - + this.lastPointerDownEvent.timeStamp < + 300 && gesture.pointers.size <= 1 && isEmbeddableElement(hitElement) && this.isEmbeddableCenter( hitElement, - this.lastPointerUp, + this.lastPointerUpEvent, pointerDownState.origin.x, pointerDownState.origin.y, )