swarm-ui: edit a notification flow on a canvas
A custom flow opens at /notifications/<id>: its nodes drawn left to right (source, then matches, then todos), and a panel to edit the picked node: the source's events, a match's conditions, a todo's recipients, op, key and text. Add a match or todo below a node, move a branch under another match, or remove it; an edit the controller would refuse (a todo with children, a second parent, a cycle, over the node or depth cap) is turned away with the reason. Edits stay in a draft until save, which sends the whole document at the revision read. A stale revision is told apart from a refused document: the editor offers to reload or to keep the draft on top of the latest. The form choices come from the controller's schema when it sends one. The list page gains edit and duplicate on custom flows, duplicate on the built-ins, and a new-flow box; a new flow's id avoids the built-ins' too.
This commit is contained in:
parent
c00fd99112
commit
f28e1e9063
1 changed files with 1209 additions and 6 deletions
|
|
@ -0,0 +1,95 @@
|
|||
// Where the flow editor's canvas puts each node: the tree runs left to
|
||||
// right, one column per level, a leaf per row, and a parent sits at the
|
||||
// middle of its children. Computed from the flow on every edit, so nothing
|
||||
// about the layout is stored.
|
||||
import type { Flow } from "./FlowTypes.js";
|
||||
|
||||
export const NODE_WIDTH = 190;
|
||||
export const NODE_HEIGHT = 46;
|
||||
const GAP_X = 56;
|
||||
const GAP_Y = 14;
|
||||
const PAD = 12;
|
||||
|
||||
export interface PlacedNode {
|
||||
id: string;
|
||||
depth: number;
|
||||
// Top-left corner.
|
||||
x: number;
|
||||
y: number;
|
||||
}
|
||||
|
||||
export interface PlacedEdge {
|
||||
from: string;
|
||||
to: string;
|
||||
// Right-middle of the parent, left-middle of the child.
|
||||
x1: number;
|
||||
y1: number;
|
||||
x2: number;
|
||||
y2: number;
|
||||
}
|
||||
|
||||
export interface Layout {
|
||||
nodes: PlacedNode[];
|
||||
edges: PlacedEdge[];
|
||||
width: number;
|
||||
height: number;
|
||||
}
|
||||
|
||||
export function layoutFlow(flow: Flow): Layout {
|
||||
const children = new Map<string, string[]>();
|
||||
for (const e of flow.edges) {
|
||||
children.set(e.from, [...(children.get(e.from) ?? []), e.to]);
|
||||
}
|
||||
const known = new Set(flow.nodes.map((n) => n.id));
|
||||
const placed = new Map<string, PlacedNode>();
|
||||
let row = 0;
|
||||
|
||||
// Depth first, so a leaf takes the next row and a parent the middle of its
|
||||
// first and last child. `placed` also stops a cycle from looping.
|
||||
const place = (id: string, depth: number): number => {
|
||||
const kids = (children.get(id) ?? []).filter(
|
||||
(k) => known.has(k) && !placed.has(k),
|
||||
);
|
||||
const at = (r: number): number => PAD + r * (NODE_HEIGHT + GAP_Y);
|
||||
const x = PAD + depth * (NODE_WIDTH + GAP_X);
|
||||
// Reserve the spot first so a node cannot be reached twice.
|
||||
placed.set(id, { id, depth, x, y: 0 });
|
||||
// A node with two parents (a draft the save would refuse) is placed under
|
||||
// the first that reaches it.
|
||||
const ys: number[] = [];
|
||||
for (const k of kids) if (!placed.has(k)) ys.push(place(k, depth + 1));
|
||||
const y = ys.length === 0 ? at(row++) : (ys[0] + ys[ys.length - 1]) / 2;
|
||||
placed.set(id, { id, depth, x, y });
|
||||
return y;
|
||||
};
|
||||
|
||||
const root = flow.nodes.find((n) => n.kind === "source");
|
||||
if (root) place(root.id, 0);
|
||||
// A draft being rewired can hold a node nothing reaches yet: show it below
|
||||
// rather than lose it, until it is attached.
|
||||
for (const n of flow.nodes) {
|
||||
if (!placed.has(n.id)) place(n.id, 0);
|
||||
}
|
||||
|
||||
const nodes = flow.nodes.map((n) => placed.get(n.id) as PlacedNode);
|
||||
const edges = flow.edges
|
||||
.filter((e) => placed.has(e.from) && placed.has(e.to))
|
||||
.map((e) => {
|
||||
const a = placed.get(e.from) as PlacedNode;
|
||||
const b = placed.get(e.to) as PlacedNode;
|
||||
return {
|
||||
from: e.from,
|
||||
to: e.to,
|
||||
x1: a.x + NODE_WIDTH,
|
||||
y1: a.y + NODE_HEIGHT / 2,
|
||||
x2: b.x,
|
||||
y2: b.y + NODE_HEIGHT / 2,
|
||||
};
|
||||
});
|
||||
return {
|
||||
nodes,
|
||||
edges,
|
||||
width: Math.max(0, ...nodes.map((n) => n.x + NODE_WIDTH)) + PAD,
|
||||
height: Math.max(0, ...nodes.map((n) => n.y + NODE_HEIGHT)) + PAD,
|
||||
};
|
||||
}
|
||||
Loading…
Reference in a new issue