diff --git a/packages/excalidraw/actions/actionFinalize.tsx b/packages/excalidraw/actions/actionFinalize.tsx index 9730c022f9..0d258f5bf4 100644 --- a/packages/excalidraw/actions/actionFinalize.tsx +++ b/packages/excalidraw/actions/actionFinalize.tsx @@ -32,6 +32,7 @@ import type { ExcalidrawElement, ExcalidrawLinearElement, NonDeleted, + PointsPositionUpdates, } from "@excalidraw/element/types"; import { t } from "../i18n"; @@ -55,14 +56,10 @@ export const actionFinalize = register({ perform: (elements, appState, data, app) => { let newElements = elements; const { interactiveCanvas, focusContainer, scene } = app; - const { event, sceneCoords } = - (data as { - event?: PointerEvent; - sceneCoords?: { x: number; y: number }; - }) ?? {}; const elementsMap = scene.getNonDeletedElementsMap(); - if (event && appState.selectedLinearElement) { + if (data && appState.selectedLinearElement) { + const { event, sceneCoords } = data; const element = LinearElementEditor.getElement( appState.selectedLinearElement.elementId, elementsMap, @@ -73,6 +70,11 @@ export const actionFinalize = register({ "Arrow element should exist if selectedLinearElement is set", ); + invariant( + sceneCoords, + "sceneCoords should be defined if actionFinalize is called with event", + ); + const linearElementEditor = LinearElementEditor.handlePointerUp( event, appState.selectedLinearElement, @@ -85,23 +87,15 @@ export const actionFinalize = register({ const selectedPointsIndices = newArrow ? [element.points.length - 1] // New arrow creation : appState.selectedLinearElement.selectedPointsIndices; - const draggedPoints = + + const draggedPoints: PointsPositionUpdates = selectedPointsIndices.reduce((map, index) => { map.set(index, { - point: newArrow - ? LinearElementEditor.pointFromAbsoluteCoords( - element, - sceneCoords - ? pointFrom(sceneCoords.x, sceneCoords.y) - : LinearElementEditor.getPointAtIndexGlobalCoordinates( - element, - -1, - arrayToMap(newElements), - ), - elementsMap, - ) - : element.points[index], - draggedPoints: !newArrow, + point: LinearElementEditor.pointFromAbsoluteCoords( + element, + pointFrom(sceneCoords.x, sceneCoords.y), + elementsMap, + ), }); return map;