Keyboard and Focus Implementation in Vanilla-Diagram
The canvas (<svg tabindex="0" role="application">, labelled by
diagramLabel and described by the keyboardHint) is one tab stop, as the
Gantt's timeline is. The keys act only while it has focus, never on the
document, so they don't fire while the user types elsewhere, and a host's own
shortcuts on N, P or J keep working outside it. The node and connector
groups are aria-hidden: the announcer says what the keys did and the
outline lists the nodes
(15-screen-readers).
While the canvas has focus, the focus node (the node selected last) carries a dashed focus ring; in connect mode, the target does.
Keys
| Key | Nothing selected | A selection |
|---|---|---|
| Arrows | Pan 40 screen px | Select the nearest node in that direction from the focus node's centre (from the anchor while a walked connector is selected), centred in view when it isn't fully in it; the announcer says it |
| Shift + arrow | Pan 40 screen px | Move the selected nodes, a selected container's descendants with them, 10 world px (a nudge); with a grid, the selection's box to the next grid line in that direction |
| Shift + Alt + arrow | Move them 1 world px, unsnapped | |
| Ctrl/Cmd + Shift + arrow | Resize the one selected node by 10 px: the right (left / right arrows) or bottom (up / down) edge of its own frame, keeping the ratio for a lockAspect symbol; a container's frame stops at its children |
|
| Delete, Backspace | Remove, a container's descendants too, after vn-diagram-removing (cancelable) |
|
| Ctrl/Cmd + Z; Ctrl/Cmd + Shift + Z, Ctrl + Y | Undo; redo | Undo; redo |
| Ctrl/Cmd + A | Select every node | Select every node |
| Enter, F2 | Edit the focus node's text, else the one selected connector's label (10-text-editing) | |
| Ctrl/Cmd + C, X, V | Copy, cut, paste, through the copy, cut and paste events (11-clipboard-and-export); V also with nothing selected |
|
| Ctrl/Cmd + D | Duplicate | |
Ctrl/Cmd + Shift + ], [ |
Bring the selected nodes to the front, send them to the back | |
+ (or =), - |
Zoom in, out by 1.25 about the centre | The same |
0, 1 |
Zoom to fit; zoom to 100% | The same |
C |
Connect mode from the focus node | |
N; P (any case) |
Select the focus node's next or previous connector, clockwise from 12 o'clock, wrapping; the node becomes the anchor and the announcer says Connector {index} of {count}. With a walked connector selected, the anchor's next or previous one |
|
J |
With a walked connector selected, select its other end (a free end: nothing), revealed and announced | |
R; Shift + R |
Turn each selected node 15 degrees clockwise about its own centre; counter-clockwise. A container doesn't turn. One batch of update commands per press, labelled command.rotate, stored in [0, 360) |
|
| Escape | Cancel a gesture, else leave connect mode, else disarm a port, else select a walked connector's anchor again, else clear the selection |
Nearest in a direction (VanillaDiagramGeometry.nearestInDirection()):
among the nodes whose centre lies within 45 degrees of the direction, the
closest centre; when there is none, the closest anywhere on that side. Ties
go to the first in z order.
A bare modifier key (Shift, Ctrl, Alt, Meta) does nothing on its own: it is
part of the next key. The bracket keys are matched by KeyboardEvent.code
(BracketRight, BracketLeft): with Shift held, key reads } and { on a
US layout. Plain C stays connect mode. R and Shift + R turn: Ctrl/Cmd +
Alt + arrows are taken by some desktops and graphics drivers, Alt + arrows
are the browser's Back and Forward on Windows and Linux. N, P and J
are matched by key, any case, with no Ctrl, Cmd or Alt: ] and [ by
code would be the + key (zoom in) on Italian and German layouts, and by
key they need AltGr on some. Ctrl/Cmd + A selects in arrival order, the
topmost node last (O(n)). Keys typed in the text editor never reach these
handlers.
Walking connections
N and P select connectors, which the arrows can't: a connector can be
selected, deleted (Delete) and its label edited (Enter, F2) from the
keyboard. Their order is clockwise by the direction from the node's centre to
the connector's other end, from 12 o'clock, ties by id. A node without
connectors: they say 0 connections and select nothing. Any selection the
walk didn't make (a pointer, the API) drops the anchor
(15-screen-readers).
The nudge burst
Shift + arrows preview like a drag: the first nudge sets
_preview = { kind: 'move', ids, dx, dy } on the selected nodes and a
selected container's descendants, each nudge
adds to the offset and re-routes their connectors, and the model is
untouched, so the getters and toJSON() return the positions from before the
burst. One move of the summed offset commits 500ms after the last nudge, or
at once before anything else reaches the diagram: another key (Escape
included), a pointer press, a wheel, the canvas losing focus or leaving the
DOM, an API mutation, undo() or redo(), a load, whenSaved(), turning
read-only on, or setDataManager(). A burst is therefore one undo step and
one change set; a nudged node that lands in or out of a container joins or
leaves it in that same step, as at a drag's release. A failed save and
destroy() cancel it, as they cancel a drag, and the nudged nodes go back.
Connect mode
C with a focus node enters connect mode from it. The arrow keys pick the
target, the nearest node in that direction from the current target (from the
source at first), and show the connector as a preview between the pair of
ports nearest each other. Enter connects that pair with the current
connector-type, after vn-diagram-connecting, and leaves the mode; Escape
leaves it without connecting.
Connect mode and a pointer connect or reconnect drag never run together
(they share the one preview): C does nothing while a pointer gesture runs,
and a press on a port or a connector's end leaves the mode before its drag
starts
(05-ports-and-connectors § Connecting).
A change from elsewhere (the API, an undo, a failed save's revert) that
removes the source ends the mode; one that removes the target drops it, and
the mode goes on from the source with no target and no preview; one that
moves either redraws the preview. A transaction that removes a node and adds
it back changes nothing. Neither ending is announced; the removal itself is.
Every drag has an alternative
WCAG 2.2's 2.5.7 asks for a way to do without dragging what a drag does: a click on a palette tile (or Enter, Space) adds its symbol; a click on a port and then another connects them; the keys above move, resize, turn and connect.
Read-only and busy
Selecting, panning, zooming, copying and walking connections (N, P, J)
work; moving, resizing, turning,
removing, connecting, editing text, cut, paste, duplicate, the z-order keys,
undo and redo do nothing. The API still turns nodes.
While the diagram is busy (a load, a layout or an export runs,
01-web-component § The busy state),
no key does anything: those above, and selecting, panning, zooming, copying
and walking connections too (Ctrl+Z, Ctrl+Shift+Z and Ctrl+Y included); Tab
still leaves the canvas. The keys the idle canvas owns keep their default
prevented, as idle and read-only: Escape, the arrows, the z-order keys,
Ctrl/Cmd+D, Ctrl/Cmd+Z, Ctrl+Y, Ctrl/Cmd+A, and + = - _ 0 1 N P J (so
Escape doesn't close a dialog around the diagram, nor N or J open Firefox's
quick find); every other key keeps the browser's default. The API still
works, undo() and redo() too.