Vanilla Diagram Toolbar

<vn-diagram-toolbar diagram="<id>">: the command bar of a <vn-diagram>, written in markup. Choose the standard commands and their order, add commands of your own, and give each button a style. It needs a diagram on the page (vanilla-diagram) and reads it through the diagram's public API only.

Features

Installation

Direct File Include

<script src="path/to/vanilla-diagram/vanilla-diagram.js"></script>
<script src="path/to/vanilla-diagram-toolbar/vanilla-diagram-toolbar.js"></script>

The two scripts load in either order: the toolbar needs the diagram only when it links. It injects its own stylesheet, vanilla-diagram-toolbar.css, from its script's folder, and loads its colours from the diagram's theme file.

In dist/ the script ships as vanilla-diagram-toolbar.bundle.js, with vanilla-diagram-toolbar.css and vanilla-diagram-toolbar.d.ts beside it.

Usage

New to the toolbar? Getting started builds one step by step, with a live example at each step.

<vn-diagram-toolbar diagram="flow">
  <vn-diagram-toolbar-status></vn-diagram-toolbar-status>
  <vn-diagram-toolbar-command command="save" label="Save" icon="save" variant="primary"
    disabled-when="!canUndo"></vn-diagram-toolbar-command>
  <vn-diagram-toolbar-separator></vn-diagram-toolbar-separator>
  <vn-diagram-toolbar-command command="undo"></vn-diagram-toolbar-command>
  <vn-diagram-toolbar-command command="redo"></vn-diagram-toolbar-command>
  <vn-diagram-toolbar-command command="zoomToFit"></vn-diagram-toolbar-command>
  <vn-diagram-toolbar-swatches></vn-diagram-toolbar-swatches>
  <vn-diagram-toolbar-command command="delete" label="Delete" variant="danger"></vn-diagram-toolbar-command>
</vn-diagram-toolbar>
<vn-diagram id="flow" style="height: 600px"></vn-diagram>
const toolbar = document.querySelector('vn-diagram-toolbar');
VanillaDiagramToolbarElement.registerIcon('save', ['M3 2.5h8l2.5 2.5v8.5h-10.5Z', 'M5 13.5v-4h6v4']);
toolbar.registerCommand('save', (diagram) => {
  myApi.save(diagram.toJSON()).then(() => diagram.clearHistory());
});
toolbar.addEventListener('vn-diagram-toolbar-command', (e) => {
  if (e.detail.command === 'delete' && !confirm('Delete?')) e.preventDefault();
});
toolbar.setStatusMessage('Ready.');

Elements and attributes

<vn-diagram-toolbar> attribute Property
diagram diagram A <vn-diagram>'s id. The property takes the element or an id and reads the linked element, null while unlinked. It links once that diagram is initialized, and unlinks when it is destroyed
theme theme A diagram theme name; unset (null), it follows the linked diagram's theme
<vn-diagram-toolbar-command> attribute Property
command command A standard command (below) or a name of your own
arg commandArg JSON, parsed on each click; the property takes any value and wins over the attribute
label, label-key Text beside the icon; label-key reads the toolbar's messages. Without a label the button is icon-only
title-key The tooltip and, icon-only, the accessible name; a standard command defaults to its own label
icon, icon-position icon, iconPosition A built-in or registered icon (a standard command defaults to its own), or an inline <svg> child; left (default) or right of the label
variant variant primary, secondary, ghost (default) or danger
disabled-when, show-when, hide-when A state field, ! to negate (disabled-when="!hasSelectedNode")

The three menu buttons take variant too: <vn-diagram-toolbar-swatches> (the colour menu), <vn-diagram-toolbar-connector-type> and <vn-diagram-toolbar-arrows>. Each shows the selection's common value (a colour box, or the type's or the arrows' icon; a dashed line when the selected connectors differ). <vn-diagram-toolbar-status> and <vn-diagram-toolbar-separator> take none.

Standard commands

command Runs Enabled
undo, redo undo(), redo() canUndo, canRedo, not busy
zoomIn, zoomOut zoomBy(1.25), zoomBy(1 / 1.25) Not at the zoom limit, not busy
zoomToFit zoomToFit() Linked, not busy
delete deleteSelection() A selection, not read-only, not busy
bringToFront, sendToBack bringToFront(), sendToBack() A selected node, not read-only, not busy
swatch (the colour menu; arg a slot, or null) setSwatch(arg) A selected node, not read-only, not busy
connectorType (the connector type menu; arg straight, bezier or elbow) updateConnector(id, { type }) on each selected connector whose type differs, in one transaction() (one undo step); an edited route is cleared, as by any type change A selected connector, not read-only, not busy
arrows (the arrows menu; arg none, end, start or both) updateConnector(id, { arrows }) likewise: { end: false }, no field (the default end arrow), { start: true, end: false }, { start: true } A selected connector, not read-only, not busy
exportSvg, exportPng Await toSVG(), toPNG(), then download diagram.svg, diagram.png (both buttons off while the export runs) At least one node, not busy
layout (Tidy up; not in the default bar) layout() with its defaults (layered, down), then zoomToFit() when a node moved At least one node, not read-only, not busy

The default bar is status undo redo | zoomIn zoomOut zoomToFit | delete | bringToFront sendToBack | swatches connectorType arrows | exportSvg exportPng. The connector menus change only what is selected: the type a user draws next is the palette's (connector-type), with an end arrow. On a standard command disabled-when can only add to the rule; on a custom one it replaces it (a custom command is enabled while linked, read-only included). A disabled button is aria-disabled, still reachable by the arrow keys. The menus show the value every selected item has; above 1,000 selected items they show it as mixed without reading each one.

State

toolbar.state (frozen, recomputed on every diagram change; a pan or zoom recomputes only canZoomIn and canZoomOut, and leaves the menus alone) is what disabled-when, show-when and hide-when read: linked, readOnly, canUndo, canRedo, canZoomIn, canZoomOut, hasNodes, hasSelection (nodes or connectors), hasSelectedNode, hasSelectedConnector, selectedCount and busy (the diagram's isBusy(): a load, a layout or an export runs). Every field is false, and the count 0, while unlinked. While busy, undo, redo, the zoom buttons, delete, the z-order, colour, connector type and arrows commands, export and Tidy up are off; a custom command can follow it with disabled-when="busy".

Methods

Method
dispatchCommand(command, arg?) Fires vn-diagram-toolbar-command, then runs the registerCommand() handler, else the standard action, else nothing. False while unlinked, for a disabled standard command, or when cancelled
registerCommand(name, fn) fn(diagram, arg, toolbar) runs the command; a standard name replaces its action. A handler that throws is logged
setMessages(map), messages Override labels; messages is the effective set (a copy)
setStatusMessage(text, severity?), getStatusMessage() The status text; severity info, success or error (only error tints it); '' or null clears it
VanillaDiagramToolbarElement.registerIcon(name, paths) An icon for icon="<name>": SVG path data in a 16 x 16 box, stroked with the button's colour

Events

Event Detail
vn-diagram-toolbar-command { command, arg, diagram, toolbar, state, source }, on the toolbar, cancelable, before any command runs; source is the item whose button was pressed, null from script

The name is in window.VanillaDiagramToolbarEvents.COMMAND. The changes the standard commands make report source api in the diagram's vn-diagram-changed.

Keyboard

The buttons are one tab stop: left and right arrows (swapped under dir="rtl"), Home and End move between the visible buttons. Down, Enter or Space on a menu button (colours, connector type, arrows) opens its menu, where up and down move, Home and End jump, Enter picks, Escape closes back to the button and Tab moves on.

Messages

English is built in. Keys: toolbarLabel (the bar's accessible name), toolbar.undo, toolbar.redo, toolbar.zoom-in, toolbar.zoom-out, toolbar.zoom-to-fit, toolbar.delete, toolbar.bring-to-front, toolbar.send-to-back, toolbar.export-svg, toolbar.export-png, toolbar.layout, swatchesLabel, swatch.col1 to swatch.col10, swatch.none, connectorTypeLabel, connectorType.straight, connectorType.bezier, connectorType.elbow, arrowsLabel, arrows.none, arrows.end, arrows.start, arrows.both, command.connectorType and command.arrows (the connector menus' undo steps, which vn-diagram-changed carries as its label), and any key you add for label-key and title-key.

Theming

The diagram's theme files colour the toolbar: they put .vn-diagram-toolbar in their token selector, so it paints with the palette's tokens and five of its own:

Token
--vn-diagram-toolbar-primary-bg, --vn-diagram-toolbar-primary-color A primary button: the theme's accent
--vn-diagram-toolbar-danger-bg, --vn-diagram-toolbar-danger-color A danger button: the theme's error red
--vn-diagram-toolbar-border A secondary button's outline

A custom diagram theme (VanillaDiagramElement.registerTheme()) defines them too. Under forced-colors: active the buttons use ButtonFace and ButtonText, primary and danger with a thicker border.

Architecture

One file: the toolbar and its six items, its own link (on <vn-diagram>'s public isInitialized(), getSelectedIds(), getNodeCount(), deleteSelection(), setSwatch(), transaction(), updateConnector(), ...), its own stylesheet links, labels and event registry. See docs/vanilla-diagram-toolbar/00-index.md.

File Structure

vanilla-diagram-toolbar/
├── vanilla-diagram-toolbar.js     # <vn-diagram-toolbar> and its items
├── vanilla-diagram-toolbar.css    # structure and the button variants
├── vanilla-diagram-toolbar.d.ts   # hand-authored TypeScript declarations
└── README.md

Changelog

Version 1.1.1

Version 1.1.0

Version 1.0.0

License

MIT