From 8e4fd83f5cd2d5b3a2e36ccc52da85c399c70a3f Mon Sep 17 00:00:00 2001 From: Mark Tolmacs Date: Sun, 2 Mar 2025 17:55:37 +0100 Subject: [PATCH] Refactors --- packages/element/src/linearElementEditor.ts | 85 +++++++++++---------- packages/excalidraw/data/transform.test.ts | 2 +- 2 files changed, 46 insertions(+), 41 deletions(-) diff --git a/packages/element/src/linearElementEditor.ts b/packages/element/src/linearElementEditor.ts index a7627bcdf8..df6125dd8e 100644 --- a/packages/element/src/linearElementEditor.ts +++ b/packages/element/src/linearElementEditor.ts @@ -335,55 +335,60 @@ export class LinearElementEditor { LinearElementEditor.movePoints( element, selectedPointsIndices.map((pointIndex) => { - let p = pointFrom( - element.x + element.points[pointIndex][0] + deltaX, - element.y + element.points[pointIndex][1] + deltaY, + let newPointPosition = pointFrom( + element.points[pointIndex][0] + deltaX, + element.points[pointIndex][1] + deltaY, ); - if ( - pointIndex === lastClickedPoint && - (pointIndex === 0 || pointIndex === element.points.length - 1) - ) { - const hoveredElement = getHoveredElementForBinding( - { - x: scenePointerX, - y: scenePointerY, - }, - app.scene.getNonDeletedElements(), - app.scene.getNonDeletedElementsMap(), - app.state.zoom, - elbowed, - elbowed, + // Check if point dragging is happening + if (pointIndex === lastClickedPoint) { + let globalNewPointPosition = pointFrom( + scenePointerX - linearElementEditor.pointerOffset.x, + scenePointerY - linearElementEditor.pointerOffset.y, ); - if (hoveredElement) { - const newPoints = Array.from(element.points); - newPoints[pointIndex] = pointFrom( - element.points[pointIndex][0] + deltaX, - element.points[pointIndex][1] + deltaY, - ); - p = bindPointToSnapToElementOutline( + + if ( + pointIndex === 0 || + pointIndex === element.points.length - 1 + ) { + const hoveredElement = getHoveredElementForBinding( { - ...element, - points: newPoints, + x: scenePointerX, + y: scenePointerY, }, - hoveredElement, - pointIndex === 0 ? "start" : "end", + app.scene.getNonDeletedElements(), + app.scene.getNonDeletedElementsMap(), + app.state.zoom, + elbowed, + elbowed, ); + + if (hoveredElement) { + const newPoints = Array.from(element.points); + newPoints[pointIndex] = pointFrom( + element.points[pointIndex][0] + deltaX, + element.points[pointIndex][1] + deltaY, + ); + globalNewPointPosition = bindPointToSnapToElementOutline( + { + ...element, + points: newPoints, + }, + hoveredElement, + pointIndex === 0 ? "start" : "end", + ); + } } + + newPointPosition = LinearElementEditor.createPointAt( + element, + elementsMap, + globalNewPointPosition[0], + globalNewPointPosition[1], + event[KEYS.CTRL_OR_CMD] ? null : app.getEffectiveGridSize(), + ); } - const newPointPosition: LocalPoint = - pointIndex === lastClickedPoint - ? LinearElementEditor.createPointAt( - element, - elementsMap, - p[0], - p[1], - // p[0] - linearElementEditor.pointerOffset.x, - // p[1] - linearElementEditor.pointerOffset.y, - event[KEYS.CTRL_OR_CMD] ? null : app.getEffectiveGridSize(), - ) - : pointFrom(p[0] - element.x, p[1] - element.y); return { index: pointIndex, point: newPointPosition, diff --git a/packages/excalidraw/data/transform.test.ts b/packages/excalidraw/data/transform.test.ts index 12bd284b73..18bf557d30 100644 --- a/packages/excalidraw/data/transform.test.ts +++ b/packages/excalidraw/data/transform.test.ts @@ -781,7 +781,7 @@ describe("Test Transform", () => { expect((arrow as ExcalidrawArrowElement).endBinding).toStrictEqual({ elementId: "rect-1", focus: -0, - gap: 5, + gap: FIXED_BINDING_DISTANCE, }); expect(rect.boundElements).toStrictEqual([ {