Fragiola

API: core (@fragiola/flow)

Commands

Every command the model runs, with its payload and result types and how to run it from code or as JSON. Generated from the registry.

The model runs 18 commands: the core's, and those of the features that bring some (each says which). Each one takes a JSON payload, checked against its JSON Schema, and returns a CommandResult: { ok: true, value }, or { ok: false, error }. Run one with model.run(name, payload) (typed by your registry and your features), or give it as untrusted JSON to model.dispatch({ command, payload }), which validates it first. model.get("commands") returns the same list for the commands of the installed features, with the schemas, ready to become tool definitions for an assistant.

Every command can return invalid_payload (the payload does not match its schema, or names a field of a feature that is not installed, with the path of each problem), vetoed (a middleware refused it), queued (a middleware ran it while another command was running) and middleware_error; a command of a feature that is not installed returns not_found, and an unknown name unknown_command. Each command lists the codes its own rules add: not_found (a node or edge it names is missing) and refused (a rule of the flow refuses the change: a permission flag, a duplicate edge, a parent cycle).

Positions are in flow coordinates (x grows to the right, y grows down); with sub-flows, a node's position is relative to its parent. The payload types below use T, your registry of node and edge types.

Nodes

node.add

Adds a node at the end of the drawing order: its type, position and data, optionally its id (generated when missing), size and flags. Refused when the id is taken.

Payload (FlowNodeInput<T>)

fieldtyperequireddescription
idstringnoits id; generated when missing
typestringyesits type, from the app's registry
positionobjectyesits top-left corner, in flow coordinates (relative to its parent with sub-flows)
dataobjectyesthe app's data, a JSON object typed by the node or edge type
widthnumbernoits width, when the document gives one
heightnumbernoits height, when the document gives one
initialWidthnumbernoits width until it is measured; width wins
initialHeightnumbernoits height until it is measured; height wins
handlesobject[]noits handles, declared for its edges to be drawn before it is measured
sourcePosition"top" | "right" | "bottom" | "left"nothe side its edges leave by when no handle gives one; default bottom
targetPosition"top" | "right" | "bottom" | "left"nothe side its edges enter by when no handle gives one; default top
originobjectnothe point of its box its position stands at, as shares of its size, over the engine's nodeOrigin
zIndexintegernoits place in the stacking order of its layer (nodes among nodes, edges among edges): a greater one is drawn above; default 0
draggablebooleannoit may be moved (node.move); default true
selectablebooleannoit may be selected (selection.set); default true
connectablebooleannoedges may start or end at it (edge.add, edge.reconnect); default true
deletablebooleannoit may be removed (node.remove, selection.remove); default true
hiddenbooleannoit is not shown; default false
focusablebooleannoit takes DOM focus (with the keyboard feature); default true
selectedbooleannoit is selected (requires the selection feature)
parentIdstringnothe node it lives in, its position relative to it (requires the subflows feature)
extent"parent" | objectno"parent": it stays inside its parent's box; a rectangle (x, y, width, height): it stays inside it, in the coordinates of its position (requires the subflows feature)
expandParentbooleannoits parent grows to keep it inside (requires the subflows feature)

Result ({ nodeId: string })

fieldtyperequireddescription
nodeIdstringyesthe node's id

Errors: refused, besides those every command can return.

model.run("node.add", {
    id: "start",
    type: "task",
    position: {
        x: 0,
        y: 0
    },
    data: {
        title: "Start"
    }
});
{ "command": "node.add", "payload": {"id":"start","type":"task","position":{"x":0,"y":0},"data":{"title":"Start"}} }

node.remove

Removes a node and every edge that starts or ends at it, in one step; with sub-flows, its children go too. Refused when the node (or a child) is not deletable.

Payload ({ nodeId: string })

fieldtyperequireddescription
nodeIdstringyesthe node's id

Result (RemovedResult)

fieldtyperequireddescription
removedNodeIdsstring[]yesthe nodes removed, in drawing order
removedEdgeIdsstring[]yesthe edges removed: those that touched a removed node, and those removed themselves

Errors: not_found, refused, besides those every command can return.

model.run("node.remove", {
    nodeId: "start"
});
{ "command": "node.remove", "payload": {"nodeId":"start"} }

node.move

Moves a node to a position (relative to its parent with sub-flows), kept inside its parent with extent: "parent" (a rectangle extent is kept by the gestures, not here). Refused when the node is not draggable. A drag runs it as transient steps, then once more for the drop.

It may run as a step of a continuous gesture: model.run(…, { transient: true }).

Payload ({ nodeId: string; position: XY })

fieldtyperequireddescription
nodeIdstringyesthe node's id
positionobjectyesits new top-left corner, in flow coordinates (relative to its parent with sub-flows)

Result ({ nodeId: string; position: XY })

fieldtyperequireddescription
nodeIdstringyesthe node's id
positionobjectyesthe position it got, kept inside its parent with extent: "parent"

Errors: not_found, refused, besides those every command can return.

model.run("node.move", {
    nodeId: "start",
    position: {
        x: 120,
        y: 40
    }
});
{ "command": "node.move", "payload": {"nodeId":"start","position":{"x":120,"y":40}} }

node.resize

Sets a node's width and height, and optionally its position (a resize from a top or left edge moves it), kept inside its parent with extent: "parent" (a rectangle extent is kept by the gestures, not here). A resize gesture runs it as transient steps, then once more at the end.

It may run as a step of a continuous gesture: model.run(…, { transient: true }).

Payload ({ nodeId: string; width: number; height: number; position?: XY; })

fieldtyperequireddescription
nodeIdstringyesthe node's id
widthnumberyesits new width
heightnumberyesits new height
positionobjectnoits new top-left corner; none: it stays

Result ({ nodeId: string; width: number; height: number; position: XY })

fieldtyperequireddescription
nodeIdstringyesthe node's id
widthnumberyesits width
heightnumberyesits height
positionobjectyesthe position it got, kept inside its parent with extent: "parent"

Errors: not_found, besides those every command can return.

model.run("node.resize", {
    nodeId: "start",
    width: 180,
    height: 60
});
{ "command": "node.resize", "payload": {"nodeId":"start","width":180,"height":60} }

node.set-data

Merges a shallow patch into a node's data: each top-level key given replaces the node's own.

Payload ({ nodeId: string; data: NodeDataPatchOf<T> })

fieldtyperequireddescription
nodeIdstringyesthe node's id
dataobjectyesthe keys to set in the node's data

Result ({ nodeId: string })

fieldtyperequireddescription
nodeIdstringyesthe node's id

Errors: not_found, besides those every command can return.

model.run("node.set-data", {
    nodeId: "start",
    data: {
        title: "Begin"
    }
});
{ "command": "node.set-data", "payload": {"nodeId":"start","data":{"title":"Begin"}} }

node.set-flags

Sets some of a node's flags (draggable, selectable, connectable, deletable, hidden, focusable); the others stay. A node made unselectable is deselected.

Payload (NodeFlagsPayload)

fieldtyperequireddescription
nodeIdstringyesthe node's id
draggablebooleannoit may be moved (node.move); default true
selectablebooleannoit may be selected (selection.set); default true
connectablebooleannoedges may start or end at it (edge.add, edge.reconnect); default true
deletablebooleannoit may be removed (node.remove, selection.remove); default true
hiddenbooleannoit is not shown; default false
focusablebooleannoit takes DOM focus (with the keyboard feature); default true

Result ({ nodeId: string })

fieldtyperequireddescription
nodeIdstringyesthe node's id

Errors: not_found, besides those every command can return.

model.run("node.set-flags", {
    nodeId: "start",
    draggable: false
});
{ "command": "node.set-flags", "payload": {"nodeId":"start","draggable":false} }

node.set-z-index

Sets a node's zIndex, its place in the nodes' stacking order (null: the default, 0), in place: its edges and measurements stay.

Payload ({ nodeId: string; zIndex: number \| null })

fieldtyperequireddescription
nodeIdstringyesthe node's id
zIndexinteger | nullyesits place in its layer's stacking order (an integer); null: back to the default, 0

Result ({ nodeId: string })

fieldtyperequireddescription
nodeIdstringyesthe node's id

Errors: not_found, besides those every command can return.

model.run("node.set-z-index", {
    nodeId: "start",
    zIndex: 2
});
{ "command": "node.set-z-index", "payload": {"nodeId":"start","zIndex":2} }

node.set-parent

Puts a node into a parent node (parentId), or back on the flow (null), keeping its place on screen: its position becomes relative to the new parent. extent and expandParent set how it stays in its parent. Refused when the parent is the node itself or one of its descendants.

It comes with the subflows feature: install subflows() from @fragiola/flow/subflows (without it, the command is not_found).

Payload ({ nodeId: string; parentId: string \| null; extent?: "parent" \| Rect \| null; expandParent?: boolean; })

fieldtyperequireddescription
nodeIdstringyesthe node's id
parentIdstring | nullyesthe new parent's id, or null for none
extent"parent" | object | nullno"parent": it stays inside its parent; a rectangle: it stays inside it (in the coordinates of its position); null: no extent; none: as it was
expandParentbooleannoits parent grows to keep it inside; none: as it was

Result ({ nodeId: string; position: XY })

fieldtyperequireddescription
nodeIdstringyesthe node's id
positionobjectyesits position, relative to its new parent

Errors: not_found, refused, besides those every command can return.

model.run("node.set-parent", {
    nodeId: "start",
    parentId: "group",
    extent: "parent"
});
{ "command": "node.set-parent", "payload": {"nodeId":"start","parentId":"group","extent":"parent"} }

Edges

edge.add

Adds an edge from a source node to a target node, optionally between two of their handles, with its type, data and flags (its id generated when missing). Refused when a node is not connectable, when an edge already joins the same handles, or when the id is taken.

Payload (FlowEdgeInput<T>)

fieldtyperequireddescription
idstringnoits id; generated when missing
sourcestringyesthe id of the node it starts at
targetstringyesthe id of the node it ends at
sourceHandlestringnothe source node's handle it starts at; none: the node's default source handle
targetHandlestringnothe target node's handle it ends at; none: the node's default target handle
typestringnohow it is drawn: "bezier" (the default), "simplebezier", "smoothstep", "step", "straight" or an app type
dataobjectnothe app's data, a JSON object typed by the node or edge type
zIndexintegernoits place in the stacking order of its layer (nodes among nodes, edges among edges): a greater one is drawn above; default 0
pathOptionsobjectnothe shape of its path, over the engine's path options: each built-in type reads its own keys (bezier: curvature; smoothstep: borderRadius, offset, stepPosition, centerX, centerY; step: the same but borderRadius)
selectablebooleannoit may be selected (selection.set); default true
deletablebooleannoit may be removed (edge.remove, selection.remove); default true
hiddenbooleannoit is not shown; default false
reconnectableboolean | "source" | "target"noits ends may be moved to other nodes or handles (edge.reconnect): both (true), none (false), or only its "source" or its "target" end; default true
focusablebooleannoit takes DOM focus (with the keyboard feature); default true
animatedbooleannoit is drawn animated (the app's CSS animates its data-animated parts); default false
selectedbooleannoit is selected (requires the selection feature)

Result ({ edgeId: string })

fieldtyperequireddescription
edgeIdstringyesthe edge's id

Errors: not_found, refused, besides those every command can return.

model.run("edge.add", {
    source: "start",
    target: "end",
    type: "smoothstep"
});
{ "command": "edge.add", "payload": {"source":"start","target":"end","type":"smoothstep"} }

edge.remove

Removes an edge. Refused when it is not deletable.

Payload ({ edgeId: string })

fieldtyperequireddescription
edgeIdstringyesthe edge's id

Result ({ edgeId: string })

fieldtyperequireddescription
edgeIdstringyesthe edge's id

Errors: not_found, refused, besides those every command can return.

model.run("edge.remove", {
    edgeId: "edge-1"
});
{ "command": "edge.remove", "payload": {"edgeId":"edge-1"} }

edge.set-data

Merges a shallow patch into an edge's data: each top-level key given replaces the edge's own.

Payload ({ edgeId: string; data: EdgeDataPatchOf<T> })

fieldtyperequireddescription
edgeIdstringyesthe edge's id
dataobjectyesthe keys to set in the edge's data

Result ({ edgeId: string })

fieldtyperequireddescription
edgeIdstringyesthe edge's id

Errors: not_found, besides those every command can return.

model.run("edge.set-data", {
    edgeId: "edge-1",
    data: {
        weight: 2
    }
});
{ "command": "edge.set-data", "payload": {"edgeId":"edge-1","data":{"weight":2}} }

edge.set-flags

Sets some of an edge's flags (selectable, deletable, hidden, reconnectable, focusable, animated); the others stay. An edge made unselectable is deselected.

Payload (EdgeFlagsPayload)

fieldtyperequireddescription
edgeIdstringyesthe edge's id
selectablebooleannoit may be selected (selection.set); default true
deletablebooleannoit may be removed (edge.remove, selection.remove); default true
hiddenbooleannoit is not shown; default false
reconnectableboolean | "source" | "target"noits ends may be moved to other nodes or handles (edge.reconnect): both (true), none (false), or only its "source" or its "target" end; default true
focusablebooleannoit takes DOM focus (with the keyboard feature); default true
animatedbooleannoit is drawn animated (the app's CSS animates its data-animated parts); default false

Result ({ edgeId: string })

fieldtyperequireddescription
edgeIdstringyesthe edge's id

Errors: not_found, besides those every command can return.

model.run("edge.set-flags", {
    edgeId: "edge-1",
    deletable: false
});
{ "command": "edge.set-flags", "payload": {"edgeId":"edge-1","deletable":false} }

edge.set-z-index

Sets an edge's zIndex, its place in the edges' drawing order (null: the default, 0), in place.

Payload ({ edgeId: string; zIndex: number \| null })

fieldtyperequireddescription
edgeIdstringyesthe edge's id
zIndexinteger | nullyesits place in its layer's stacking order (an integer); null: back to the default, 0

Result ({ edgeId: string })

fieldtyperequireddescription
edgeIdstringyesthe edge's id

Errors: not_found, besides those every command can return.

model.run("edge.set-z-index", {
    edgeId: "edge-1",
    zIndex: 1
});
{ "command": "edge.set-z-index", "payload": {"edgeId":"edge-1","zIndex":1} }

edge.reconnect

Moves an edge's ends: its new source and target nodes and, optionally, handles (a handle left out is none). Its id, type and data stay. Refused when the edge is not reconnectable (or an end it moves is not: reconnectable "source" or "target"), a node is not connectable, or another edge already joins the same handles.

It comes with the reconnection feature: install reconnection() from @fragiola/flow/reconnection (without it, the command is not_found).

Payload ({ edgeId: string } & ConnectionPayload)

fieldtyperequireddescription
edgeIdstringyesthe edge's id
sourcestringyesthe id of the node it starts at
targetstringyesthe id of the node it ends at
sourceHandlestringnothe source node's handle it starts at; none: the node's default source handle
targetHandlestringnothe target node's handle it ends at; none: the node's default target handle

Result ({ edgeId: string })

fieldtyperequireddescription
edgeIdstringyesthe edge's id

Errors: not_found, refused, besides those every command can return.

model.run("edge.reconnect", {
    edgeId: "edge-1",
    source: "start",
    target: "review"
});
{ "command": "edge.reconnect", "payload": {"edgeId":"edge-1","source":"start","target":"review"} }

Selection

selection.set

Selects nodes and edges: "replace" (the default) makes them the whole selection, "add" adds them, "toggle" flips each one. Refused when one is not selectable.

It comes with the selection feature: install selection() from @fragiola/flow/selection (without it, the command is not_found).

It may run as a step of a continuous gesture: model.run(…, { transient: true }).

Payload ({ nodeIds?: string[]; edgeIds?: string[]; mode?: SelectionMode; })

fieldtyperequireddescription
nodeIdsstring[]nothe nodes; none: no node
edgeIdsstring[]nothe edges; none: no edge
mode"replace" | "add" | "toggle"nohow they combine with the selection: "replace" (default), "add" or "toggle"

Result (SelectionResult)

fieldtyperequireddescription
nodeIdsstring[]yesthe nodes, in drawing order
edgeIdsstring[]yesthe edges, in drawing order

Errors: not_found, refused, besides those every command can return.

model.run("selection.set", {
    nodeIds: [
        "start",
        "end"
    ],
    mode: "add"
});
{ "command": "selection.set", "payload": {"nodeIds":["start","end"],"mode":"add"} }

selection.clear

Deselects every node and edge.

It comes with the selection feature: install selection() from @fragiola/flow/selection (without it, the command is not_found).

Payload (Record<never, never>)

No fields.

Result (SelectionResult)

fieldtyperequireddescription
nodeIdsstring[]yesthe nodes, in drawing order
edgeIdsstring[]yesthe edges, in drawing order

Errors: only those every command can return.

model.run("selection.clear", {});
{ "command": "selection.clear", "payload": {} }

selection.remove

Removes the selected nodes and edges that are deletable, in one step: a node takes its edges with it and, with sub-flows, its children (a node holding one that is not deletable stays).

It comes with the selection feature: install selection() from @fragiola/flow/selection (without it, the command is not_found).

Payload (Record<never, never>)

No fields.

Result (RemovedResult)

fieldtyperequireddescription
removedNodeIdsstring[]yesthe nodes removed, in drawing order
removedEdgeIdsstring[]yesthe edges removed: the selected ones and those that touched a removed node

Errors: only those every command can return.

model.run("selection.remove", {});
{ "command": "selection.remove", "payload": {} }

Batches

batch

Runs several commands as one atomic step: in order, all or nothing, with one event. The first failure is the batch's result, its path pointing into commands.

It may run as a step of a continuous gesture: model.run(…, { transient: true }).

Payload ({ commands: BatchEntry<T>[] })

fieldtyperequireddescription
commandsobject[]yesthe commands, in order: each a { command, payload }

Result ({ results: unknown[] })

fieldtyperequireddescription
resultsJSON[]yeseach command's value, in order

Errors: those of the commands it runs (the first failure, at its path in commands); nothing applies unless all succeed.

model.run("batch", {
    commands: [
        {
            command: "node.add",
            payload: {
                id: "a",
                type: "task",
                position: {
                    x: 0,
                    y: 0
                },
                data: {}
            }
        },
        {
            command: "node.add",
            payload: {
                id: "b",
                type: "task",
                position: {
                    x: 200,
                    y: 0
                },
                data: {}
            }
        },
        {
            command: "edge.add",
            payload: {
                source: "a",
                target: "b"
            }
        }
    ]
});
{ "command": "batch", "payload": {"commands":[{"command":"node.add","payload":{"id":"a","type":"task","position":{"x":0,"y":0},"data":{}}},{"command":"node.add","payload":{"id":"b","type":"task","position":{"x":200,"y":0},"data":{}}},{"command":"edge.add","payload":{"source":"a","target":"b"}}]} }