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

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 }.

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.

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).