Fragiola

API: core (@fragiola/flow)

Features

The eight opt-in features, from selection to the minimap - each factory, its options and defaults, what it adds to the model and the engine.

The core holds the document, nodes, edges, handles, the viewport, measurement, edge paths and fit-view. Everything else is an opt-in feature: a factory on its own entry, installed on the model once, when it is created. A flow that never imports a feature ships none of its code (the build's bundle guard checks the main entry imports none), and a command, key or document field of a feature that is not installed is a type error; at run time its command returns not_found and a document field is a validation issue, both naming the feature, while its get and is keys answer undefined and false. The concept is on Features.

import { Flow } from "@fragiola/flow-react";
import { connection } from "@fragiola/flow-react/connection";
import { drag } from "@fragiola/flow-react/drag";
import { keyboard } from "@fragiola/flow-react/keyboard";
import { selection } from "@fragiola/flow-react/selection";

// once, outside the component: the features are fixed when the model is created
const features = [selection({ boxMode: "partial" }), drag({ snapGrid: [20, 20] }), connection(), keyboard()] as const;

export function Editor() {
    return (
        <Flow.Root<Types, typeof features> features={features} defaultNodes={nodes} className="size-full">
            …
        </Flow.Root>
    );
}

Each React entry re-exports the core's (@fragiola/flow-react/drag is @fragiola/flow/drag), so a React app imports everything from the one package it installs; /minimap and /resize add their primitives. Pass typeof features as the type argument so the verbs know the keys. Each factory returns a frozen object with its resolved options.

Read-only diagram
Gallery

selection()

selection(options?) from @fragiola/flow-react/selection returns a SelectionFeature: clicking selects a node or an edge (replacing the selection), a modifier click toggles it in the selection, a plain click on the pane clears it, and Shift + drag on the pane draws a selection box. It owns the selected field of nodes and edges, the commands selection.set, selection.clear and selection.remove, the keys "selected-nodes", "selected-edges", "node-selected", "edge-selected", and on the engine "selection-box", "selection-rect" and "box-selecting".

SelectionOptions:

optiontypedefaultdescription
boxModeBoxSelectionMode"full"a box takes the nodes it holds whole ("full") or those it touches ("partial"), with the edges between them
multiplebooleantruea click with the multi-selection key (multiKey) toggles the item in the selection (adds it, or removes a selected one)
boxOnDragbooleanfalseany drag on the pane draws a box (instead of panning); otherwise the box key + drag
multiKeySelectionKey["Meta", "Control", "Shift"]held, a click toggles a node or an edge in the selection (with multiple); null for none
boxKeySelectionKey"Shift"held, a drag on the pane draws a selection box; null for none (only boxOnDrag then)
rectSelectionRectMode"box"when the rectangle around the selected nodes (selection-rect, the nodes-selection part) shows: after a box selected two or more, until the selection changes otherwise ("box"), or whenever two or more are selected ("always")

SelectionKey is a key (KeyboardEvent.key or .code; Meta, Control, Alt and Shift read from the pointer event), several (any of them held), or null.

drag()

drag(options?) from @fragiola/flow-react/drag returns a DragFeature: a press on a node drags it (or the selection it belongs to), with transient node.move steps and a final one; Escape cancels and puts them back. A node holding a DRAG_HANDLE_ATTRIBUTE (data-flow-drag-handle) element drags only from it, and a dragged node's box stays inside its extent and the engine's nodeExtent. It adds no command (node.move is the core's), and on the engine "dragging-node-ids", "dragging" and "node-dragging".

DragOptions:

optiontypedefaultdescription
snapGrid[number, number]nonethe grid a dragged node snaps to, in flow units (the others move by the same offset)
thresholdnumber1how far, in pixels, a press moves before it is a drag
selectOnDragbooleantruea drag of a node that is not selected selects it first (it is then dragged alone); false drags it unselected and keeps the selection

connection()

connection(options?) from @fragiola/flow-react/connection returns a ConnectionFeature: a press on a handle draws a connection; the drop on another handle runs edge.add. While it is drawn, the engine asks model.can("edge.add", …), so a middleware's veto marks the target data-invalid and refuses the drop (see Connection rules). It adds "connection" and "connecting" to the engine.

ConnectionOptions:

optiontypedefaultdescription
radiusnumber20how near, in screen pixels, a handle must be for the drop to snap to it
modeConnectionMode"strict""strict": a source ends at a target (or the reverse); "loose": at any other handle
clickToConnectbooleanfalsea tap on a handle starts a connection that follows the pointer (connection, data-connecting); a tap on another handle ends it like a drop (the same validity and events), a tap elsewhere, Escape or cancel-gesture cancels it
thresholdnumber1how far, in pixels, a press on a handle moves before it draws a connection (with clickToConnect, a press within the engine's nodeClickDistance stays a tap)
lineTypeBuiltInEdgeType"bezier"the connection line's path ("bezier", "simplebezier", "smoothstep", "step", "straight"), with the engine's pathOptions

reconnection()

reconnection(options?) from @fragiola/flow-react/reconnection returns a ReconnectionFeature; it requires connection() (installing it alone throws). A press on an edge's end (a Flow.ReconnectAnchor, or the stroke within radius of the end) moves that end to another handle with edge.reconnect, its command (refused for an edge whose reconnectable flag is false). It adds "edge-reconnecting" to the engine.

ReconnectionOptions:

optiontypedefaultdescription
radiusnumber10how near, in pixels, to an edge's end a press grabs it (give touch apps more)

resize()

resize(options?) from @fragiola/flow-react/resize returns a ResizeFeature: a press on a ResizeHandle resizes its node from that corner or side, with transient node.resize steps (the core's command) and a final one. The entry adds NodeResizer and ResizeHandle (see NodeResizer), the engine "resizing" and "node-resizing". A NodeResizer's own limits override these for its node, and its own direction (a ResizeDirection: "horizontal" moves only widths and x, "vertical" only heights and y, from a corner handle as from a side one, with no kept ratio; none: both) is given the same way (engine.adapter.setPartOptions("resizer", nodeId, { direction })).

ResizeOptions:

optiontypedefaultdescription
minWidthnumber10the smallest width, in flow units
minHeightnumber10the smallest height
maxWidthnumbernonethe largest width
maxHeightnumbernonethe largest height
keepAspectRatiobooleanfalsea resize keeps the node's width-to-height ratio
snap{ x, y }nonethe grid, in flow units, the sides a pointer moves snap to (sizes and positions land on it); the drag feature's snapGrid is its own (an arrow key's step is never snapped)

subflows()

subflows() from @fragiola/flow-react/subflows returns a SubflowsFeature (its SubflowsOptions are empty): a node may live in another (parentId), its position relative to its parent; with extent: "parent" it stays inside its parent's box, with a rectangle extent ({ x, y, width, height }, in its position's coordinates) inside that rectangle (kept by the gestures: drags, the arrows and resizes), and with expandParent the parent grows to keep it. Removing a parent removes its children. It owns those fields, the command node.set-parent and the keys "node-children-by" and "node-parent-by". See Sub-flows.

keyboard()

keyboard(options?) from @fragiola/flow-react/keyboard returns a KeyboardFeature: nodes and edges take DOM focus (tabindex="0", the engine's "node-focusable" and "edge-focusable"); the arrows move the selection (or the focused node), the delete keys (deleteKeys: Delete and Backspace by default) remove it (selection.remove with selection()), Enter and Space select, Escape clears the selection. See Keyboard and accessibility.

With connection() installed too, handles take focus (tabindex="0" in a connectable handle's part attributes) and Enter or Space on one starts a keyboard connection: Tab, Shift + Tab or the arrows walk the target handles model.can("edge.add") accepts, nearest first (the one pointed at is the connection's to, data-valid, announced by a "connect-target" event), Enter or Space connect, Escape cancels (input: "keyboard" on the connection events). With resize() installed too, resize handles take focus and the arrows move a focused handle's sides by step (fastStep with Shift) in one node.resize step, within the resizer's limits (a "resize" event with source: "keyboard"). Without both, nothing changes in the tab order.

KeyboardOptions:

optiontypedefaultdescription
stepnumber5how far an arrow moves a node (with resize(), a focused resize handle's sides), in flow units
fastStepnumber20how far Shift with an arrow moves it (or the sides)
deleteKeysstring[]["Delete", "Backspace"]the keys (KeyboardEvent.key or .code) that remove the selection; [] for none
panOnFocusbooleantruea node that takes focus is panned into view when it is not wholly in it
beforeDeleteBeforeDeletenoneasked before a delete key removes anything, with the nodes and the edges it would remove (a removed node's edges among them): true removes them, false nothing, a { nodes, edges } subset only those (never more); a promise of one waits (the key is taken at once, another delete key meanwhile does nothing) and, when it resolves, removes in one step what is still there (DeleteDecision)

minimap()

minimap(options?) from @fragiola/flow-react/minimap returns a MinimapFeature: the engine's "minimap-view" (what a minimap of a size draws), "minimap-point-to-flow" (a client point on it as a flow point: a click's), and pan and zoom from the minimap. The entry adds MiniMap, MiniMapNode and MiniMapMask (see MiniMap).

MinimapOptions:

optiontypedefaultdescription
pannablebooleantruea press or a drag on the minimap pans the viewport
zoomablebooleantruethe wheel over the minimap zooms the viewport
paddingnumber0.1the share of the minimap's size kept free around the nodes and the view, on every side
inversePanbooleanfalsea drag on the minimap moves the view the other way from the pointer (as grabbing the flow); a press does not centre the view then
zoomStepnumber10how fast the wheel over the minimap zooms: 10 is the flow's own wheel, 20 twice as fast; a value that is not a finite number above 0 counts as the default (a part's as none)

Who owns what

FEATURE_KEYS is the table the core reads: for each FeatureName, its commands, its model get and is keys, its engine keys (engineRun, engineGet, engineIs) and its node and edge fields. FeatureKeyKind is one of those kinds, OwnedBy<K, N> the names of a kind the features N own.

import { FEATURE_KEYS } from "@fragiola/flow-react";

FEATURE_KEYS.subflows.commands; // ["node.set-parent"]
FEATURE_KEYS.selection.nodeFields; // ["selected"]

The feature contract

A feature is a FlowFeature<N, O> (any one: AnyFlowFeature; a list: FlowFeatures, whose wide form as a type argument means "any features"). Its parts are all optional: requires, options, commands (each a CommandDefinition), model get and is keys reading a FlowReader (the document's nodes and edges, in order and by id), document rules (documentIssues, checkNode, checkEdge, removing, constrainPosition, nodeChanged, flagsChanged, absolutePosition) and engine(host), its part of each engine.

typeis
FeatureEnginewhat a feature adds to one engine: run, get and is keys, and the hooks pointerdown, tap, wheel, keydown, focus, attributes, attached, detached, destroy, walked in install order
FeatureEngineHostwhat the engine lends its features: the model (as a FeatureModel), a shared slot, the root, the viewport, measured rects and handles, edge paths, absolutePosition, positionFor, extentOf, keepInExtent, held, interactive, allows (a kind's set-interactive switch), tapDistance, focusable, toClient, path (a built-in edge path with the engine's pathOptions), partOptions, changed and emit
FeatureGesturewhat a pointerdown that takes the press returns: move, end, cancel and active, and autoPan (true, or the AutoPanGesture it is: "node", "connect", "selection", "resize"); the engine owns the capture, Escape, a cancel and a missed release
FeatureModelthe model's verbs as a feature's engine side calls them, typed loosely
FlowReadernodes(), edges(), node(id), edge(id) of a document
FeatureNamesOf<F>the names of a list's features
AllowedKeys<Kind, All, F>the names of a kind a model with the features F has: the core's and theirs
CommandNameOf<F>its commands
ModelGetKeyOf<F>its model.get keys
ModelIsKeyOf<F>its model.is keys
EngineActionKeyOf<F>its engine's actions
EngineGetKeyOf<F>its engine's reads
EngineIsKeyOf<F>its engine's questions

Features never import each other: they talk through keys and the host's shared slot (the connection under way is shared by connection() and reconnection() that way).