From 91d36e9b81a5087b0000b8abc1bcdb3fadeb807f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?M=C3=A1rk=20Tolm=C3=A1cs?= Date: Thu, 22 May 2025 12:34:15 +0200 Subject: [PATCH] fix: Linear to elbow conversion crash (#9556) * Fix linear to elbow conversion * Add invariant check * Add dev invariant fix * Add arrowhead --- packages/element/src/elbowArrow.ts | 19 +++++++++++++++++++ .../components/ConvertElementTypePopup.tsx | 3 ++- 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/packages/element/src/elbowArrow.ts b/packages/element/src/elbowArrow.ts index 95a2aa8ef..73c82a898 100644 --- a/packages/element/src/elbowArrow.ts +++ b/packages/element/src/elbowArrow.ts @@ -974,6 +974,25 @@ export const updateElbowArrowPoints = ( ), "Elbow arrow segments must be either horizontal or vertical", ); + + invariant( + updates.fixedSegments?.find( + (segment) => + segment.index === 1 && + pointsEqual(segment.start, (updates.points ?? arrow.points)[0]), + ) == null && + updates.fixedSegments?.find( + (segment) => + segment.index === (updates.points ?? arrow.points).length - 1 && + pointsEqual( + segment.end, + (updates.points ?? arrow.points)[ + (updates.points ?? arrow.points).length - 1 + ], + ), + ) == null, + "The first and last segments cannot be fixed", + ); } const fixedSegments = updates.fixedSegments ?? arrow.fixedSegments ?? []; diff --git a/packages/excalidraw/components/ConvertElementTypePopup.tsx b/packages/excalidraw/components/ConvertElementTypePopup.tsx index b1dd11cb4..8e527d549 100644 --- a/packages/excalidraw/components/ConvertElementTypePopup.tsx +++ b/packages/excalidraw/components/ConvertElementTypePopup.tsx @@ -564,7 +564,7 @@ export const convertElementTypes = ( continue; } const fixedSegments: FixedSegment[] = []; - for (let i = 0; i < nextPoints.length - 1; i++) { + for (let i = 1; i < nextPoints.length - 2; i++) { fixedSegments.push({ start: nextPoints[i], end: nextPoints[i + 1], @@ -581,6 +581,7 @@ export const convertElementTypes = ( ); mutateElement(element, app.scene.getNonDeletedElementsMap(), { ...updates, + endArrowhead: "arrow", }); } else { // if we're converting to non-elbow linear element, check if