mirror of
https://github.com/excalidraw/excalidraw.git
synced 2026-12-06 03:05:18 +03:00
`convertToExcalidrawElements` binds skeleton arrows to their `start`/`end`
elements while it is still creating elements, and the placement pass
(e0f198c15) then places every bound end where its binding puts it. Two
kinds of bindings made at conversion time are wrong, and placing the
arrows exposes them.
1. Ends given beyond the binding area of their element
A layout engine (e.g. mermaid's sequence diagrams, whose messages are
bound to the participants' headers but drawn far below them) can bind an
end the user could never bind in the editor. Its fixedPoint lies well
outside the element, and `normalizeFixedPoint` clamps it, so the binding
can't hold the end where it was given.
Reproduction: rectangles A (0,0 100x20) and B (500,0 100x20).
- "half-far": an arrow at (100,10) with points [0,0]->[450,490], start A,
end B, so its end is given at (550,500), far below B.
- "beyond": an arrow at (50,40) with points [0,0]->[500,0], start A, end B,
20px below both, so beyond binding distance.
Failure case (HEAD):
- "half-far": end bound to B at fixedPoint [0.5001,10]. Placing moves the
end from the given (550,500) to (550,200).
- "beyond": both ends bound at fixedPoint [0.5001,2]. That's a binding the
editor can't create, and it moves the arrow onto the headers the first
time A or B is moved.
Expected: ends beyond the binding area are left unbound where given.
Ends within it stay bound.
Fix: new `isPointInBindingArea` (collision.ts) checks whether a point is
inside an element or within binding distance of its outline.
`isEndBoundBeyondBindingArea` (binding.ts) applies it to an arrow's bound
end. Before placing, the pass unbinds every end beyond its element's
binding area (`unbindBindingElement`, which also drops the arrow from
the element's `boundElements`). An arrow with no binding left isn't
placed and keeps its geometry as given.
The check ignores frame clipping (`getBindingCandidates` still skips
the clipped parts of frame children): frames are sized around their
children only at the end of the conversion, so an element given with a
`frameId` would be checked against a 0x0 frame, and every end bound to
it would be beyond reach. Arrows unbound from an element are still
framed with it, as the frame pass frames what is bound to a frame's
children (through their `boundElements`, which no longer list them).
An end that just falls short of its element is extended onto it instead
(`extendBoundEndOntoElement`, binding.ts): its end segment is extended
past the end by the segment's length, at least 100px, and if that
reaches the element, the end is moved to where it does, off the element
by the binding gap, and stays bound. This keeps bindings a generator
meant to make, but drew arrows slightly too short for.
Reproduction: rectangle B (500,0 100x20), an arrow at (150,10) with
points [0,0]->[250,0], end B, so its end is given 100px short of B.
Expected: end bound to B, moved to (500 - binding gap, 10).
2. Ends bound before their element is sized
A rectangle/ellipse/diamond with a label and no width/height is created
0x0 and sized to fit its label only when its label is bound. That happens
in the same pass that binds arrows, in skeleton order. An arrow listed
before the element therefore binds to a 0x0 element, and
`calculateFixedPointForNonElbowArrowBinding` binds ends on elements
smaller than MIN_BINDABLE_SIZE to their center. Nothing recomputes the
binding once the element is sized, so the placement pass re-aims the end
at the center, and the result depends on element order.
Reproduction: an arrow at (100,50) with points [0,0]->[300,0], end bound
to B, where B is a rectangle at (400,25) with label "B" and no size
(sized to 20x35).
Failure case (HEAD):
arrow listed first: endBinding.fixedPoint [0.5001,0.5001] (center),
points [[0,0],[294,-7.11]] (tilted)
B listed first: endBinding.fixedPoint [0,0.714] (left edge),
points [[0,0],[294,0]] (straight)
Expected: the same in either order, the "B listed first" result.
Fix: the pass rebinds each end that is still bound
(`bindBindingElement`, keeping its mode) against its element as sized.
For elements whose size didn't change after binding, this gives the
same binding as before.
Signed-off-by: Mark Tolmacs <mark@lazycat.hu>
@excalidraw/element
Install
npm install @excalidraw/element
If you prefer Yarn over npm, use this command to install the Excalidraw utils package:
yarn add @excalidraw/element
With PNPM, similarly install the package with this command:
pnpm add @excalidraw/element