mirror of
https://github.com/excalidraw/excalidraw.git
synced 2026-11-20 21:23:22 +03:00
fix(element): decide skeleton arrow end bindings before moving either end
When placing skeleton arrows, each end bound beyond its element's binding
area is either extended onto the element (if it just falls short) or
unbound. 61255ad73 did this one end at a time: start first, then end. For
elbow arrows, extending the start goes through `movePoints`, which reroutes
the whole arrow from both its bindings. That moved the still-bound end onto
its element before it was checked, so it was kept bound. The result
depended on which end was the start.
Reproduction: rectangles A (0,0 100x20) and B (500,0 100x20), and an elbow
arrow (`elbowed: true`) [0,0]→[400,0]→[400,490] at (150,10), bound from A
to B. Its start falls 50px short of A, and its end is at (550,500), far
below B.
Failure case:
A→B: start extended onto A and bound, end rerouted to about (550,26)
just under B, and left bound to B
B→A (same arrow reversed): end extended onto A and bound, start left at
(550,500) and unbound
Expected: both directions as B→A: the A end extended and bound, the B end
left where given and unbound.
Fix: `extendBoundEndOntoElement` becomes `getBoundEndExtension`, which only
returns where the end would be extended to and moves nothing. The skeleton
conversion now checks both ends against the arrow as given. It unbinds the
unreachable ends first, so the reroute doesn't take them along, then
applies every extension in one `movePoints` call, then rebinds as before.
Only elbow arrows were affected: moving one end of a non-elbow arrow leaves
its other end in place. Mermaid's converter doesn't produce elbow arrows,
so this needs hand-written or LLM-generated skeletons.
Signed-off-by: Mark Tolmacs <mark@lazycat.hu>
This commit is contained in:
2 files changed
+55
-52
No files matched your search
@@ -1217,23 +1217,22 @@ export const isEndBoundBeyondBindingArea = (
|
||||
};
|
||||
|
||||
// how far at least to extend an arrow's end segment looking for what it's
|
||||
// bound to (see `extendBoundEndOntoElement`)
|
||||
// bound to (see `getBoundEndExtension`)
|
||||
const MIN_BOUND_END_EXTENSION = 100;
|
||||
|
||||
/**
|
||||
* Extends the arrow's bound end along its end segment, by the segment's length
|
||||
* (at least MIN_BOUND_END_EXTENSION), and if that reaches the element it's
|
||||
* bound to, moves the end there, off the element by the binding gap. This
|
||||
* (at least MIN_BOUND_END_EXTENSION), to find where it would reach the element
|
||||
* it's bound to, off the element by the binding gap. Moving the end there
|
||||
* restores bindings given to arrows that just fall short of their elements.
|
||||
*
|
||||
* @returns whether the end was moved onto the element
|
||||
* @returns where to move the end to, or null if it doesn't reach the element
|
||||
*/
|
||||
export const extendBoundEndOntoElement = (
|
||||
arrow: NonDeleted<ExcalidrawArrowElement>,
|
||||
export const getBoundEndExtension = (
|
||||
arrow: ExcalidrawArrowElement,
|
||||
startOrEnd: "start" | "end",
|
||||
scene: Scene,
|
||||
): boolean => {
|
||||
const elementsMap = scene.getNonDeletedElementsMap();
|
||||
elementsMap: NonDeletedSceneElementsMap,
|
||||
): GlobalPoint | null => {
|
||||
const binding =
|
||||
startOrEnd === "start" ? arrow.startBinding : arrow.endBinding;
|
||||
const bindableElement = binding && elementsMap.get(binding.elementId);
|
||||
@@ -1242,13 +1241,12 @@ export const extendBoundEndOntoElement = (
|
||||
!isBindableElement(bindableElement) ||
|
||||
arrow.points.length < 2
|
||||
) {
|
||||
return false;
|
||||
return null;
|
||||
}
|
||||
|
||||
const index = startOrEnd === "start" ? 0 : arrow.points.length - 1;
|
||||
const end = LinearElementEditor.getPointAtIndexGlobalCoordinates(
|
||||
arrow,
|
||||
index,
|
||||
startOrEnd === "start" ? 0 : -1,
|
||||
elementsMap,
|
||||
);
|
||||
const adjacent = LinearElementEditor.getPointAtIndexGlobalCoordinates(
|
||||
@@ -1258,7 +1256,7 @@ export const extendBoundEndOntoElement = (
|
||||
);
|
||||
const segmentLength = pointDistance(adjacent, end);
|
||||
if (segmentLength < PRECISION) {
|
||||
return false;
|
||||
return null;
|
||||
}
|
||||
|
||||
const extension = lineSegment(
|
||||
@@ -1271,34 +1269,15 @@ export const extendBoundEndOntoElement = (
|
||||
end,
|
||||
),
|
||||
);
|
||||
const nextEnd = intersectElementWithLineSegment(
|
||||
bindableElement,
|
||||
elementsMap,
|
||||
extension,
|
||||
getBindingGap(bindableElement),
|
||||
).sort((a, b) => pointDistanceSq(a, end) - pointDistanceSq(b, end))[0];
|
||||
if (!nextEnd) {
|
||||
return false;
|
||||
}
|
||||
|
||||
LinearElementEditor.movePoints(
|
||||
arrow,
|
||||
scene,
|
||||
new Map([
|
||||
[
|
||||
index,
|
||||
{
|
||||
point: LinearElementEditor.pointFromAbsoluteCoords(
|
||||
arrow,
|
||||
nextEnd,
|
||||
elementsMap,
|
||||
),
|
||||
},
|
||||
],
|
||||
]),
|
||||
return (
|
||||
intersectElementWithLineSegment(
|
||||
bindableElement,
|
||||
elementsMap,
|
||||
extension,
|
||||
getBindingGap(bindableElement),
|
||||
).sort((a, b) => pointDistanceSq(a, end) - pointDistanceSq(b, end))[0] ??
|
||||
null
|
||||
);
|
||||
|
||||
return true;
|
||||
};
|
||||
|
||||
export const unbindBindingElement = (
|
||||
|
||||
@@ -33,7 +33,7 @@ import type { Zoom } from "@excalidraw/excalidraw/types";
|
||||
|
||||
import {
|
||||
bindBindingElement,
|
||||
extendBoundEndOntoElement,
|
||||
getBoundEndExtension,
|
||||
isEndBoundBeyondBindingArea,
|
||||
unbindBindingElement,
|
||||
updateBoundArrow,
|
||||
@@ -98,6 +98,7 @@ import type {
|
||||
NonDeletedExcalidrawElement,
|
||||
NonDeletedSceneElementsMap,
|
||||
Ordered,
|
||||
PointsPositionUpdates,
|
||||
TextAlign,
|
||||
VerticalAlign,
|
||||
ExcalidrawStickyNoteElement,
|
||||
@@ -968,26 +969,49 @@ export const convertToExcalidrawElements = (
|
||||
}
|
||||
// users can't (re)bind an end given (e.g. by a layout engine) beyond the
|
||||
// binding area of what it's bound to, so don't leave it bound there,
|
||||
// unless it just falls short of it: then extend it onto it
|
||||
// unless it just falls short of it: then extend it onto it. Decide for
|
||||
// both ends as given, as moving either end of an elbow arrow reroutes it
|
||||
// from both its bindings, which could move the other end onto its element
|
||||
const extendedEnds: PointsPositionUpdates = new Map();
|
||||
for (const startOrEnd of ["start", "end"] as const) {
|
||||
if (
|
||||
isEndBoundBeyondBindingArea(
|
||||
!isEndBoundBeyondBindingArea(
|
||||
arrow,
|
||||
startOrEnd,
|
||||
placedElementsMap,
|
||||
DEFAULT_ZOOM,
|
||||
) &&
|
||||
!extendBoundEndOntoElement(arrow, startOrEnd, scene)
|
||||
)
|
||||
) {
|
||||
const elementId = unbindBindingElement(arrow, startOrEnd, scene);
|
||||
if (elementId) {
|
||||
unboundArrowIds.set(elementId, [
|
||||
...(unboundArrowIds.get(elementId) ?? []),
|
||||
arrow.id,
|
||||
]);
|
||||
}
|
||||
continue;
|
||||
}
|
||||
const extendedEnd = getBoundEndExtension(
|
||||
arrow,
|
||||
startOrEnd,
|
||||
placedElementsMap,
|
||||
);
|
||||
if (extendedEnd) {
|
||||
extendedEnds.set(startOrEnd === "start" ? 0 : arrow.points.length - 1, {
|
||||
point: LinearElementEditor.pointFromAbsoluteCoords(
|
||||
arrow,
|
||||
extendedEnd,
|
||||
placedElementsMap,
|
||||
),
|
||||
});
|
||||
continue;
|
||||
}
|
||||
const elementId = unbindBindingElement(arrow, startOrEnd, scene);
|
||||
if (elementId) {
|
||||
unboundArrowIds.set(elementId, [
|
||||
...(unboundArrowIds.get(elementId) ?? []),
|
||||
arrow.id,
|
||||
]);
|
||||
}
|
||||
}
|
||||
// once unbound, so elbow arrows aren't rerouted to what they were bound to
|
||||
if (extendedEnds.size) {
|
||||
LinearElementEditor.movePoints(arrow, scene, extendedEnds);
|
||||
}
|
||||
for (const startOrEnd of ["start", "end"] as const) {
|
||||
// it was bound before what it's bound to was sized if that came later
|
||||
// (e.g. to fit its label), so its fixed point may be off (e.g. the
|
||||
// center of a 0x0 element): rebind it against the element as sized
|
||||
|
||||
Reference in new issue
Block a user