fix: Yet more patching of intersect code (#8352)

* Yet more patching of intersect code
This commit is contained in:
Márk Tolmács 2024-08-09 17:33:12 +02:00 committed by GitHub
parent 1ea5b26f25
commit 99b91c46f7
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 37 additions and 11 deletions

View File

@ -41,6 +41,7 @@ import {
isElbowArrow,
isFrameLikeElement,
isLinearElement,
isRectangularElement,
isTextElement,
} from "./typeChecks";
import type { ElementUpdate } from "./mutateElement";
@ -751,7 +752,7 @@ export const bindPointToSnapToElementOutline = (
const aabb = bindableElement && aabbForElement(bindableElement);
if (bindableElement && aabb) {
// TODO: Dirty hack until tangents are properly calculated
// TODO: Dirty hacks until tangents are properly calculated
const intersections = [
...intersectElementWithLine(
bindableElement,
@ -759,24 +760,32 @@ export const bindPointToSnapToElementOutline = (
[point[0], point[1] + 2 * bindableElement.height],
FIXED_BINDING_DISTANCE,
elementsMap,
).map((i) =>
distanceToBindableElement(bindableElement, i, elementsMap) >=
bindableElement.height / 2
).map((i) => {
if (!isRectangularElement(bindableElement)) {
return i;
}
const d = distanceToBindableElement(bindableElement, i, elementsMap);
return d >= bindableElement.height / 2 || d < FIXED_BINDING_DISTANCE
? ([point[0], -1 * i[1]] as Point)
: ([point[0], i[1]] as Point),
),
: ([point[0], i[1]] as Point);
}),
...intersectElementWithLine(
bindableElement,
[point[0] - 2 * bindableElement.width, point[1]],
[point[0] + 2 * bindableElement.width, point[1]],
FIXED_BINDING_DISTANCE,
elementsMap,
).map((i) =>
distanceToBindableElement(bindableElement, i, elementsMap) >=
bindableElement.width / 2
).map((i) => {
if (!isRectangularElement(bindableElement)) {
return i;
}
const d = distanceToBindableElement(bindableElement, i, elementsMap);
return d >= bindableElement.width / 2 || d < FIXED_BINDING_DISTANCE
? ([-1 * i[0], point[1]] as Point)
: ([i[0], point[1]] as Point),
),
: ([i[0], point[1]] as Point);
}),
];
const heading = headingForPointFromElement(bindableElement, aabb, point);

View File

@ -176,6 +176,23 @@ export const isRectanguloidElement = (
);
};
// TODO: Remove this when proper distance calculation is introduced
// @see binding.ts:distanceToBindableElement()
export const isRectangularElement = (
element?: ExcalidrawElement | null,
): element is ExcalidrawBindableElement => {
return (
element != null &&
(element.type === "rectangle" ||
element.type === "image" ||
element.type === "text" ||
element.type === "iframe" ||
element.type === "embeddable" ||
element.type === "frame" ||
element.type === "magicframe")
);
};
export const isTextBindableContainer = (
element: ExcalidrawElement | null,
includeLocked = true,