Symbols and Palette Implementation in Vanilla-Diagram

Symbols

A symbol says how a node is drawn: { name, label?, defaultSize, minSize?, lockAspect?, path | render, ports?, textBox?, container? }. Two forms, exactly one per symbol:

A declarative symbol's path is also its outline for the snap guides, which start and stop where it meets their line; a drawn symbol counts as its box there (04-pointer-and-touch).

Either form may paint with the theme's tokens (fill: var(--vn-diagram-node-fill)): the shape's root element gets the vn-diagram-node-shape class, which the base stylesheet paints.

container: true makes a container: other nodes may name its nodes as their parent; they draw in the frames layer under the connectors, are picked by their border and their text box (the label band), and move, resize and are removed with what they hold (13-containers). Default false.

textBox is the box the text fills, { x, y, width, height } in fractions of the node's box; default the whole box. An optional maxHeight, in px, caps its height: a label band along the top of a container keeps its height when the container grows tall, as a Tidy up around a long column of children makes it (16-auto-layout). A path that draws a line under the band caps it the same way, as it gets only the size.

The registry (VanillaDiagramSymbols) is page-wide, as theme names are. registerSymbol() checks the contract and returns false, registering nothing, with a warning, when it fails: a non-empty name not already taken (the built-ins' included), a positive default size, a positive minimum size when given, exactly one of path and render, ports with unique ids inside 0 to 1 and a side, a text box in fractions with a positive maxHeight when given, a boolean container when given. The stored symbol is a frozen copy with its defaults filled in (minSize 16 x 16, the four default ports, the whole box as text box). Registering a symbol notifies the diagrams, which redraw the nodes drawn as rectangles until then, and the palettes.

Sets work like themes: a name, a label (its own, else the symbolSet.<name> message, else the name) and its symbols, in order. registerSymbolSet(name, symbols) registers the definitions it lists, takes the names it lists when they are registered, and skips any other entry with a warning; a definition whose name is taken by another symbol warns and uses the registered one. Registering a set name again replaces the set, except basic.

The basic set

Name Default size Outline Ports
rectangle 120 x 60 the box the side midpoints
rounded-rectangle 120 x 60 the box, corner radius min(10, w/4, h/4) as rectangle
ellipse 120 x 72 two arcs as rectangle (on the ellipse)
circle 72 x 72 as ellipse, lockAspect as rectangle
diamond 100 x 80 the four side midpoints of the box the vertices; text box 0.15 to 0.85 across
triangle 100 x 80 apex at the top middle, base along the bottom (0.5, 0) top, (0.75, 0.5) right, (0.5, 1) bottom, (0.25, 0.5) left
text 120 x 32 the box, painted transparent: it still takes the pointer as rectangle
group 320 x 200, minimum 80 x 60 a rounded frame, painted with the group tokens; container: true; its text box a band along the top (15% of its height, up to 30 px, its height at the default size) for its label as rectangle

Labels come from the symbol.<name> messages ("Rectangle", ..., "Group"); the text symbol shows the textPlaceholder message ("Text") until it has text.

<vn-diagram-palette diagram="<id>"> links through the diagram's link helper (_VnDiagramLink, in vanilla-diagram-element.js), now the palette's alone: <vn-diagram-toolbar>, a component of its own, links with code of its own on the diagram's public API (Diagram Toolbar). It resolves the id with document.getElementById(), as the Gantt toolbar resolves gantt="<id>", and waits for it with the grid toolbar's technique: a MutationObserver on the document (child lists and id attributes) retries the link until a <vn-diagram> with that id is initialized. The diagram property takes the element or an id and reads the linked element, or null. A second observer on the linked diagram's connector-type, read-only and class attributes keeps the palette's states current (the class carries vn-diagram--busy). Several palettes can serve one diagram; a palette serves one at a time.

The tiles come from the registry, not from the diagram, so they draw before any diagram is linked. While unlinked, or while the diagram is read-only or busy (a load, a layout or an export runs), the tiles are aria-disabled and make no ghost; the diagram refuses a palette add while busy anyway. The connector kinds follow only the link and busy (Groups and tiles).

Groups and tiles

The palette draws one group per name in sets (default basic), headed by the set's label, then, unless connectors="false", the three connector kinds. A set not registered yet is skipped and shows as soon as it is registered; when it still isn't once the page has loaded, the palette warns once (a host registers its sets after the scripts are ready, which is after the palette first draws).

Each tile is a <button> with the symbol drawn at tile size (the symbol's path of the fitted size, or its render element), its aria-label and title the symbol's label. The buttons are one role="toolbar" with one tab stop: the arrow keys move between them across groups (left and right swap under dir="rtl"), Home and End go to the first and the last; a redraw keeps the focused button.

The connector kinds are toggle buttons (aria-pressed): a press sets the diagram's connector-type, and the current kind is pressed. Read-only leaves them on (the type is a setting, not an edit); while unlinked or busy they are aria-disabled and a press does nothing.

Adding

Both fire vn-diagram-node-adding first, cancelable, with { node, source: 'palette' }; the host may set node.id, node.text, node.data, node.swatch and node.style. Then an add command adds it, source palette. A drop snaps the node's top-left corner to the diagram's grid (04-pointer-and-touch), and a node added with its centre in a container joins it: the detail's node.parent names it.

The swatch map

swatches="rectangle:col1 diamond:col2" gives each listed shape a colour slot (09-themes):