Ports and Connectors Implementation in Vanilla-Diagram
Ports
A symbol's ports are points in fractions of its box, each with the side a
connector leaves it by; by default the four side midpoints, ids top,
right, bottom, left. On a rotated node a port's point turns with the
node and its side is turned to the nearest of the four directions.
Ports are screen-space adorners: a dot and a larger transparent hit circle
(8px radius, 12px under (pointer: coarse)). They show on the hovered node
(port-visibility="hover"), or on every node in view ("always"), and on
every node in view while a connector is being drawn or a port is armed. Not
in read-only mode.
A connector keeps its ports until layout(), which gives each connector of a
moved node the ports that face each other at the new places
(16-auto-layout).
Connecting
- Drag. A press on a port starts a connect gesture at once: a press on a
port is never a pan. Past 3px, a dashed preview of the current
connector-typefollows the pointer. The target is the port within 16 screen px of the pointer; else, over a node's body, that node's nearest port; the target port is highlighted and the preview snaps to it. Release on a target connects; anywhere else draws nothing. - Click. A press and release on a port without moving arms it. A click on another port connects the two, as a release would; a click on the empty canvas, a click on a node, or Escape disarms it, and so does a change that removes its node (the API, an undo, a failed save's revert): the other nodes' ports hide at once.
- The source port can't be its own target; another port of the same node can. Two connectors between the same ports are allowed; a host that minds cancels the event below.
Either way, vn-diagram-connecting fires first, cancelable, with
{ connector, from, to, type, reconnect: false }; the host may set
connector.id and connector.data. Then a connect command adds it, source
pointer (keyboard from connect mode,
06-keyboard-and-focus). A change
that removes the source node during the drag (the API, an undo) ends the
drag and its preview at once. One that removes the node of the port the drag
is snapped to looks for the target again at once, at the pointer (another
port, or none): the preview never ends at a port that is gone, and the
release never names a missing node. A reconnect drag does the same.
One connection mode at a time. A connect or reconnect drag and keyboard
connect mode share the one dashed preview, so they never run together: a
press on a port or on a connector's end leaves connect mode before its drag
starts, and C does nothing while a pointer gesture runs. The preview on
screen is always the one the next commit (the release, or Enter) uses.
Reconnect. A selected connector shows a handle at each end. Dragging one
onto another port (found as for a connect, never the connector's other end)
fires vn-diagram-connecting with reconnect: true and the new ends, then a
reconnect command. Past 3px the connector is in flight, as a new one is:
it hides (class vn-diagram-connector--in-flight), and the dashed preview
draws it instead, with its own type, arrows and edited route, from its fixed
end to the dragged end (snapped to the target port, else at the pointer). A
release, Escape or any cancel ends the flight: the connector is drawn again,
at its new ends or where it was. A change that removes the connector during
the drag (Delete, removeItems(), an undo) cancels the drag at once, in that
change, so its preview never outlives it (_endInputOnRemoval(), from
_applyEffectToView() and a failed save's revert). The connector in flight
is tracked apart from the preview (_inFlightId), which marks it to be
drawn again when the drag ends.
Routes
VanillaDiagramRouting.route(type, a, b) is pure: each end is a port's world
point and side, and it returns { d, points }.
- Straight: one segment.
- Bezier: one cubic; each control point lies along its port's side at
max(40, distance / 3)world px. - Elbow: leaves the source along its side by a 16px stub, enters the
target against its side by 16px, and joins the stubs with at most three
axis-aligned segments. Rather than a table of the 16 pairs of sides, it
tries a fixed list of joins (the middle segment at the midpoint, then the
two L shapes, then along the outer edge, then a 32px detour) and keeps the
one with the fewest segments, then the shortest, that has no segment
turning straight back and that leaves and enters in the right directions.
Ports facing each other on one level give one straight segment. Facing
ports closer than two stubs (32px) get stubs that meet on one line across
the gap, so the elbow crosses it straight, or with one jog, rather than
looping back: halfway, or, in a gap under 20px, with the target keeping up
to 10px, the arrowhead's length, and the source at least 2px
(
facingTargetStub()). A gap of 0 or less keeps the 32px detour. It generalizes the Gantt'sroutePath(), which routes left and right anchors on rows. This plain elbow doesn't avoid shapes; whileavoid-shapesis on (the default), the diagram keeps it only when nothing is in its way, and else routes the elbow around the nodes in its way, its own end nodes included (12-routing-around-shapes).avoid-shapes="false"draws the plain elbow everywhere.
The route is drawn twice: the visible path, and a transparent 12 screen-px
copy (vector-effect: non-scaling-stroke, pointer-events: stroke) that
takes the pointer.
Edited routes
A connector's points hold its edited route; without them the route is
automatic. VanillaDiagramRouting.route(type, a, b, points) draws either.
- Elbow. A selected elbow shows a handle on each segment but the first
and the last (the ones leaving and entering the ports). Dragging one moves
that segment across its own axis (a horizontal segment up or down); its
neighbours stretch. The first edit copies the automatic route's bend
points into
points, in world px; the release commits oneupdateofpoints, labelledcommand.route. When an end node moves, its stub is re-joined to the nearest stored point with one or two axis-aligned segments (straight on first, across first when the point lies behind the stub), and the middle stays where it was. When that join would run past the stub end and turn back (the port moved back across the stored end segment, by a move or a rotation), the stored end point slides along its segment to the stub end's level instead. When the stored end segment runs along the stub's axis instead (a rotation turned the port, so that its stub runs the way the stored route reaches it), and the stub end lies within that segment, the end point slides along it to the stub end, and the route reaches the port without a hook. A join that would still turn back (a rotation turned the port to another side, the stub end passed the stored end segment, or a segment was dragged past its stub end) is replaced by the plain elbow into, or out of, the stored end segment, in the drag preview as after it. A stored end point that slides onto its neighbour merges with it. A stored end point that lies on its port (the end moved onto it; within 0.01px, since stored points keep 2 decimals) is dropped before all this, and the route is built from the stored points left, joined to the next one; it stays inpoints, and the route goes through it again once the port moves off it. A single stored point (an L) has no end segment: when its route would turn back, the elbow is drawn as the plain elbow between its ends, and through its point again once that fits; the point stays inpoints. The route then never turns back at an end. The repaired join knows no shapes and may cross the end node. Collinear points and zero-length segments are dropped when stored. An elbow with stored points doesn't move them when both its nodes move. - Bezier. A selected bezier shows a handle on each control point, with a
dashed tangent to its end.
pointsholds the two control points as offsets from their own end, so the curve keeps its shape when a node moves. - Straight connectors have no handles. Changing a connector's
typeclearspoints. - Back to automatic:
updateConnector(id, { points: null }), or a double-click on a route handle. - A change that removes the connector during a handle drag (the API, an undo) ends the drag at once, its preview with it; the release commits nothing, even when the connector is back by then.
- An avoiding route is edited as drawn: its handles sit on the route
around the shapes, and a segment drag stores that route's bends as
points. From then on it is an edited route, kept as it is even when a shape moves across it; back to automatic, it routes around the shapes again.
Free ends
An end is a port { node, port } or a free point { x, y } in world px. A
free end has no side: it faces the other end along the larger of the two
deltas (freeSide()). With free-ends on, a connect drag released on the
empty canvas keeps a free to end at the release point, and a selected
connector's end handle dragged onto the empty canvas becomes free;
vn-diagram-connecting carries the free end. The attribute only gates the
user making free ends: the data and the API always take them, and a free end
can be dragged back onto a port. Without it, a release on the empty canvas
draws nothing, as in phase 1.
Labels
A connector's label is drawn at the route's midpoint by length and edited in
place (10-text-editing).
Arrowheads. Each diagram has two <marker>s, normal and selected, whose
ids carry a per-diagram sequence number, as the Gantt's link markers do, so
two diagrams never share one. The end arrow is on by default
(arrows.end: false removes it); arrows.start: true adds one at the start
(the marker's orient="auto-start-reverse" turns it).