swarm-ui: rewire a notification flow by drag or keyboard
The flow editor can now change a flow's shape on the canvas, not only through the inspector's fields: - drag a box onto another to move it and its branch there; a drop the save would refuse shows the target dashed red and gives the reason; - a palette of node kinds adds a node below the picked box on click, or below any box it is dragged onto; it offers what the controller lists; - arrow keys walk the tree, M picks a box up and Enter drops it under the focused one, Alt+Up/Down reorder a node among its siblings, Delete removes a branch; focus follows the edit; - each box with siblings says when it runs, and the inspector can move it earlier or later, since the router runs children in edge order; - request_reviewer is a second leaf kind beside todo, with a reviewer field; - leaving with unsaved edits asks first, also for in-app links and the back button (a router hook runs navigation past a page's guard).
This commit is contained in:
parent
e01770c056
commit
68e289fff5
1 changed files with 1072 additions and 133 deletions
|
|
@ -7,6 +7,11 @@
|
|||
// would reject. The tree rules mirror `Flow::validate` in swarm-wire.
|
||||
import type { Flow, FlowNode, FlowSchema, FlowsView } from "./FlowTypes.js";
|
||||
|
||||
// Kinds that end a branch: nothing hangs below them.
|
||||
export function isLeafKind(kind: string): boolean {
|
||||
return kind === "todo" || kind === "request_reviewer";
|
||||
}
|
||||
|
||||
export interface Limits {
|
||||
maxNodes: number;
|
||||
maxDepth: number;
|
||||
|
|
@ -105,15 +110,47 @@ export function newNodeId(flow: Flow, prefix: string): string {
|
|||
}
|
||||
}
|
||||
|
||||
// The kinds a user can add; the source is only ever the root.
|
||||
export type AddableKind = "match" | "todo" | "request_reviewer";
|
||||
|
||||
export const ADDABLE_KINDS: AddableKind[] = [
|
||||
"match",
|
||||
"todo",
|
||||
"request_reviewer",
|
||||
];
|
||||
|
||||
// The kinds the palette offers, each with the controller's description of it.
|
||||
// A controller that sends no kinds predates `request_reviewer`, so only the
|
||||
// two it always had are offered.
|
||||
export function paletteKinds(
|
||||
schema: FlowSchema,
|
||||
): { kind: AddableKind; description: string }[] {
|
||||
const listed = new Map(schema.node_kinds.map((k) => [k.kind, k.description]));
|
||||
return ADDABLE_KINDS.filter((kind) =>
|
||||
listed.size === 0 ? kind !== "request_reviewer" : listed.has(kind),
|
||||
).map((kind) => ({ kind, description: listed.get(kind) ?? "" }));
|
||||
}
|
||||
|
||||
// The word a new node's id starts with, such as `reviewer` for `reviewer1`.
|
||||
const ID_PREFIX: Record<AddableKind, string> = {
|
||||
match: "match",
|
||||
todo: "todo",
|
||||
request_reviewer: "reviewer",
|
||||
};
|
||||
|
||||
// A node of `kind` with the fields a user is asked to fill in left empty.
|
||||
export function blankNode(
|
||||
flow: Flow,
|
||||
kind: "match" | "todo",
|
||||
): Extract<FlowNode, { kind: "match" | "todo" }> {
|
||||
const id = newNodeId(flow, kind);
|
||||
return kind === "match"
|
||||
? { id, kind, when: [] }
|
||||
: {
|
||||
kind: AddableKind,
|
||||
): Extract<FlowNode, { kind: AddableKind }> {
|
||||
const id = newNodeId(flow, ID_PREFIX[kind]);
|
||||
switch (kind) {
|
||||
case "match":
|
||||
return { id, kind, when: [] };
|
||||
case "request_reviewer":
|
||||
return { id, kind, reviewer: "" };
|
||||
case "todo":
|
||||
return {
|
||||
id,
|
||||
kind,
|
||||
to: { field: "assignees" },
|
||||
|
|
@ -122,6 +159,7 @@ export function blankNode(
|
|||
prefix: false,
|
||||
text: "",
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
// `added` under `parentId`.
|
||||
|
|
@ -133,7 +171,9 @@ export function addChild(
|
|||
): Edit {
|
||||
const parent = node(flow, parentId);
|
||||
if (!parent) return refuse("that parent is not in the flow");
|
||||
if (parent.kind === "todo") return refuse("a todo ends a branch");
|
||||
if (isLeafKind(parent.kind)) {
|
||||
return refuse(`a ${parent.kind} ends a branch`);
|
||||
}
|
||||
if (added.kind === "source") return refuse("a flow has one source");
|
||||
if (!isId(added.id) || node(flow, added.id)) {
|
||||
return refuse(`${added.id} is not a free node id`);
|
||||
|
|
@ -181,10 +221,15 @@ export function reparent(
|
|||
const parent = node(flow, parentId);
|
||||
if (!target || !parent) return refuse("that node is not in the flow");
|
||||
if (target.kind === "source") return refuse("the source stays the root");
|
||||
if (parent.kind === "todo") return refuse("a todo ends a branch");
|
||||
if (isLeafKind(parent.kind)) {
|
||||
return refuse(`a ${parent.kind} ends a branch`);
|
||||
}
|
||||
if (subtreeIds(flow, id).includes(parentId)) {
|
||||
return refuse("a node cannot go under itself or its own branch");
|
||||
}
|
||||
if (parentOf(flow, id) === parentId) {
|
||||
return refuse(`${id} is already under ${parentId}`);
|
||||
}
|
||||
if (depthOf(flow, parentId) + 1 + heightBelow(flow, id) > limits.maxDepth) {
|
||||
return refuse(`a flow is at most ${limits.maxDepth} levels deep`);
|
||||
}
|
||||
|
|
@ -200,6 +245,40 @@ export function reparent(
|
|||
};
|
||||
}
|
||||
|
||||
// Where `id` runs among the nodes sharing its parent: `index` from 0 of
|
||||
// `count`. A node with no parent (the source) is the only one of its kind.
|
||||
export function siblingPosition(
|
||||
flow: Flow,
|
||||
id: string,
|
||||
): { index: number; count: number } {
|
||||
const parent = parentOf(flow, id);
|
||||
if (parent === undefined) return { index: 0, count: 1 };
|
||||
const siblings = childrenOf(flow, parent);
|
||||
return { index: Math.max(0, siblings.indexOf(id)), count: siblings.length };
|
||||
}
|
||||
|
||||
// `id` swapped with its neighbour that runs `delta` places earlier (-1) or
|
||||
// later (+1). The router runs a node's children in edge order, so this changes
|
||||
// which branch acts first; other edges keep their places.
|
||||
export function moveAmongSiblings(flow: Flow, id: string, delta: -1 | 1): Edit {
|
||||
const parent = parentOf(flow, id);
|
||||
if (parent === undefined) return refuse("only a branch can be reordered");
|
||||
const at: number[] = [];
|
||||
flow.edges.forEach((e, i) => {
|
||||
if (e.from === parent) at.push(i);
|
||||
});
|
||||
const here = at.findIndex((i) => flow.edges[i].to === id);
|
||||
const there = here + delta;
|
||||
if (there < 0 || there >= at.length) {
|
||||
return refuse(
|
||||
delta < 0 ? `${id} already runs first` : `${id} already runs last`,
|
||||
);
|
||||
}
|
||||
const edges = [...flow.edges];
|
||||
[edges[at[here]], edges[at[there]]] = [edges[at[there]], edges[at[here]]];
|
||||
return { ok: true, flow: { ...flow, edges } };
|
||||
}
|
||||
|
||||
// A todo with its op switched. A clear carries no text, and only a clear may
|
||||
// be a prefix, so each is reset where the new op has no place for it.
|
||||
export function withOp(
|
||||
|
|
@ -225,8 +304,8 @@ export function replaceNode(flow: Flow, next: FlowNode): Edit {
|
|||
"the source cannot change kind, and no other node can become one",
|
||||
);
|
||||
}
|
||||
if (next.kind === "todo" && childrenOf(flow, next.id).length > 0) {
|
||||
return refuse("a node with children cannot become a todo");
|
||||
if (isLeafKind(next.kind) && childrenOf(flow, next.id).length > 0) {
|
||||
return refuse(`a node with children cannot become a ${next.kind}`);
|
||||
}
|
||||
return {
|
||||
ok: true,
|
||||
|
|
|
|||
Loading…
Reference in a new issue