Skip to content

Virtual whiteboard for sketching hand-drawn like diagrams

fix(editor): Diamond hit test shortcut (#11631)

Márk Tolmácse6ae6bf05755
diff --git a/packages/element/src/distance.ts b/packages/element/src/distance.ts
index c94652a1aa..1b6960fbec 100644
--- a/packages/element/src/distance.ts
+++ b/packages/element/src/distance.ts
@@ -75,9 +75,7 @@ const distanceToRectanguloidElement = (
return Math.min(
...sides.map((s) => distanceToLineSegment(rotatedPoint, s)),
- ...corners
- .map((a) => curvePointDistance(a, rotatedPoint))
- .filter((d): d is number => d !== null),
+ ...corners.map((a) => curvePointDistance(a, rotatedPoint)),
);
};
@@ -104,9 +102,7 @@ const distanceToDiamondElement = (
return Math.min(
...sides.map((s) => distanceToLineSegment(rotatedPoint, s)),
- ...curves
- .map((a) => curvePointDistance(a, rotatedPoint))
- .filter((d): d is number => d !== null),
+ ...curves.map((a) => curvePointDistance(a, rotatedPoint)),
);
};
diff --git a/packages/element/src/utils.ts b/packages/element/src/utils.ts
index 819cad562f..d3a3831700 100644
--- a/packages/element/src/utils.ts
+++ b/packages/element/src/utils.ts
@@ -431,10 +431,20 @@ export function deconstructDiamondElement(
const baseCorners = getDiamondBaseCorners(element, offset);
- const corners = baseCorners.map(
- (corner) =>
- curveCatmullRomCubicApproxPoints(curveOffsetPoints(corner, offset))!,
- );
+ const corners =
+ offset > 0
+ ? baseCorners.map(
+ (corner) =>
+ curveCatmullRomCubicApproxPoints(
+ curveOffsetPoints(corner, offset),
+ )!,
+ )
+ : [
+ [baseCorners[0]],
+ [baseCorners[1]],
+ [baseCorners[2]],
+ [baseCorners[3]],
+ ];
const sides = [
lineSegment<GlobalPoint>(
diff --git a/packages/math/src/curve.ts b/packages/math/src/curve.ts
index 8e53727f84..5d17ab79a9 100644
--- a/packages/math/src/curve.ts
+++ b/packages/math/src/curve.ts
@@ -267,7 +267,10 @@ export function curveClosestPoint<Point extends GlobalPoint | LocalPoint>(
pointDistance(p, bezierEquation(c, t)),
);
- if (!solution) {
+ // `solution` is only nullish when the search window is narrower than the
+ // tolerance (e.g. a caller-supplied tolerance larger than the window). A
+ // legitimate solution of `t = 0` must not be treated as a failure.
+ if (solution == null) {
return null;
}
@@ -287,8 +290,10 @@ export function curvePointDistance<Point extends GlobalPoint | LocalPoint>(
) {
const closest = curveClosestPoint(c, p);
+ // No closest point found: report an "infinitely far" distance so this curve
+ // is never mistaken for the nearest component when fed into `Math.min`.
if (!closest) {
- return 0;
+ return Infinity;
}
return pointDistance(p, closest);