Skip to content

Virtual whiteboard for sketching hand-drawn like diagrams

fix: restore from invalid fixedSegments & type-safer point updates (#9899)

David Luzar531f3e55247a
diff --git a/packages/element/src/elbowArrow.ts b/packages/element/src/elbowArrow.ts
index 0021851645..b988eb25bb 100644
--- a/packages/element/src/elbowArrow.ts
+++ b/packages/element/src/elbowArrow.ts
@@ -359,6 +359,12 @@ const handleSegmentRelease = (
null,
);
+ if (!restoredPoints || restoredPoints.length < 2) {
+ throw new Error(
+ "Property 'points' is required in the update returned by normalizeArrowElementUpdate()",
+ );
+ }
+
const nextPoints: GlobalPoint[] = [];
// First part of the arrow are the old points
@@ -706,7 +712,7 @@ const handleEndpointDrag = (
endGlobalPoint: GlobalPoint,
hoveredStartElement: ExcalidrawBindableElement | null,
hoveredEndElement: ExcalidrawBindableElement | null,
-) => {
+): ElementUpdate<ExcalidrawElbowArrowElement> => {
let startIsSpecial = arrow.startIsSpecial ?? null;
let endIsSpecial = arrow.endIsSpecial ?? null;
const globalUpdatedPoints = updatedPoints.map((p, i) =>
@@ -741,8 +747,15 @@ const handleEndpointDrag = (
// Calculate the moving second point connection and add the start point
{
- const secondPoint = globalUpdatedPoints[startIsSpecial ? 2 : 1];
- const thirdPoint = globalUpdatedPoints[startIsSpecial ? 3 : 2];
+ const secondPoint = globalUpdatedPoints.at(startIsSpecial ? 2 : 1);
+ const thirdPoint = globalUpdatedPoints.at(startIsSpecial ? 3 : 2);
+
+ if (!secondPoint || !thirdPoint) {
+ throw new Error(
+ `Second and third points must exist when handling endpoint drag (${startIsSpecial})`,
+ );
+ }
+
const startIsHorizontal = headingIsHorizontal(startHeading);
const secondIsHorizontal = headingIsHorizontal(
vectorToHeading(vectorFromPoint(secondPoint, thirdPoint)),
@@ -801,10 +814,19 @@ const handleEndpointDrag = (
// Calculate the moving second to last point connection
{
- const secondToLastPoint =
- globalUpdatedPoints[globalUpdatedPoints.length - (endIsSpecial ? 3 : 2)];
- const thirdToLastPoint =
- globalUpdatedPoints[globalUpdatedPoints.length - (endIsSpecial ? 4 : 3)];
+ const secondToLastPoint = globalUpdatedPoints.at(
+ globalUpdatedPoints.length - (endIsSpecial ? 3 : 2),
+ );
+ const thirdToLastPoint = globalUpdatedPoints.at(
+ globalUpdatedPoints.length - (endIsSpecial ? 4 : 3),
+ );
+
+ if (!secondToLastPoint || !thirdToLastPoint) {
+ throw new Error(
+ `Second and third to last points must exist when handling endpoint drag (${endIsSpecial})`,
+ );
+ }
+
const endIsHorizontal = headingIsHorizontal(endHeading);
const secondIsHorizontal = headingForPointIsHorizontal(
thirdToLastPoint,
@@ -2071,16 +2093,7 @@ const normalizeArrowElementUpdate = (
nextFixedSegments: readonly FixedSegment[] | null,
startIsSpecial?: ExcalidrawElbowArrowElement["startIsSpecial"],
endIsSpecial?: ExcalidrawElbowArrowElement["startIsSpecial"],
-): {
- points: LocalPoint[];
- x: number;
- y: number;
- width: number;
- height: number;
- fixedSegments: readonly FixedSegment[] | null;
- startIsSpecial?: ExcalidrawElbowArrowElement["startIsSpecial"];
- endIsSpecial?: ExcalidrawElbowArrowElement["startIsSpecial"];
-} => {
+): ElementUpdate<ExcalidrawElbowArrowElement> => {
const offsetX = global[0][0];
const offsetY = global[0][1];
let points = global.map((p) =>
diff --git a/packages/excalidraw/data/restore.ts b/packages/excalidraw/data/restore.ts
index 40c57b9cf6..34bdc8f57f 100644
--- a/packages/excalidraw/data/restore.ts
+++ b/packages/excalidraw/data/restore.ts
@@ -387,7 +387,10 @@ export const restoreElement = (
elbowed: true,
startBinding: repairBinding(element, element.startBinding),
endBinding: repairBinding(element, element.endBinding),
- fixedSegments: element.fixedSegments,
+ fixedSegments:
+ element.fixedSegments?.length && base.points.length >= 4
+ ? element.fixedSegments
+ : null,
startIsSpecial: element.startIsSpecial,
endIsSpecial: element.endIsSpecial,
})