@dudousxd/nestjs-catalog-react 0.20.0 → 0.22.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/WorkflowCanvas.js +895 -287
- package/dist/context.d.ts +123 -1
- package/dist/context.js +17 -0
- package/dist/index.d.ts +1 -1
- package/dist/routes.d.ts +48 -0
- package/dist/routes.js +5 -0
- package/dist/workflow/canvas-menu.d.ts +161 -0
- package/dist/workflow/canvas-menu.js +359 -0
- package/dist/workflow/create.d.ts +127 -0
- package/dist/workflow/create.js +238 -0
- package/dist/workflow/menu.d.ts +73 -0
- package/dist/workflow/menu.js +174 -0
- package/dist/workflow/place.d.ts +79 -0
- package/dist/workflow/place.js +89 -0
- package/dist/workflow/removal.d.ts +27 -0
- package/dist/workflow/removal.js +81 -0
- package/package.json +2 -2
|
@@ -0,0 +1,238 @@
|
|
|
1
|
+
import { defaultLabel } from './graph';
|
|
2
|
+
import { WORKFLOW_BRANCH_LABELS, WORKFLOW_NODE_KINDS, newLocalId, nodeName, unreachableNodeKind, } from './model';
|
|
3
|
+
import { placeBetween } from './place';
|
|
4
|
+
import { canConnect, edgeId } from './validate';
|
|
5
|
+
/**
|
|
6
|
+
* Making nodes and wires, and deciding which of those the graph would accept.
|
|
7
|
+
*
|
|
8
|
+
* Lifted out of the canvas screen when a second surface needed it. The wiring
|
|
9
|
+
* menu on a node, the inspector's picker and now the right-click menu all have
|
|
10
|
+
* to offer *exactly* what `canConnect` allows and nothing else — and the way
|
|
11
|
+
* that goes wrong is never a bug in one of them, it is a second copy of the rule
|
|
12
|
+
* written down so a menu can be "smart". Everything here asks; nothing restates.
|
|
13
|
+
*
|
|
14
|
+
* The other reason it is its own module: `defaultLabel` is over in `graph.ts`,
|
|
15
|
+
* `canConnect` in `validate.ts`, and these functions are the join between them.
|
|
16
|
+
* They were sitting in the middle of a six-thousand-line component, which is
|
|
17
|
+
* where a rule goes to be quietly duplicated by the next person who cannot find
|
|
18
|
+
* it.
|
|
19
|
+
*/
|
|
20
|
+
/**
|
|
21
|
+
* A fresh node of one kind, carrying only the fields that kind has.
|
|
22
|
+
*
|
|
23
|
+
* Built per kind rather than as one shape with optional fields, because the
|
|
24
|
+
* executable model is a discriminated union: a source carries a source kind and
|
|
25
|
+
* a config, a transform carries a transform, a sink carries the type it commits,
|
|
26
|
+
* and nothing carries a field belonging to another kind.
|
|
27
|
+
*/
|
|
28
|
+
export function newNodeOfKind(kind, id, position, name) {
|
|
29
|
+
if (kind === 'source') {
|
|
30
|
+
return { id, name, kind: 'source', sourceKind: 'http', config: {}, position };
|
|
31
|
+
}
|
|
32
|
+
if (kind === 'transform') {
|
|
33
|
+
return { id, name, kind: 'transform', transformId: '', position };
|
|
34
|
+
}
|
|
35
|
+
if (kind === 'call') {
|
|
36
|
+
// Both empty, and the graph is invalid until they are not — deliberately.
|
|
37
|
+
// There is no list of workflows to default from (see `CallableWorkflowRef`
|
|
38
|
+
// in core), and defaulting a *version* to "1" would be the one guess that
|
|
39
|
+
// matters: it would silently pin whichever code happens to be registered as
|
|
40
|
+
// version 1 in whatever deployment this graph is promoted into.
|
|
41
|
+
return { id, name, kind: 'call', callName: '', callVersion: '', config: {}, position };
|
|
42
|
+
}
|
|
43
|
+
if (kind === 'if') {
|
|
44
|
+
// No variable, so the graph is invalid until somebody names one. There is
|
|
45
|
+
// nothing to guess: which variable tells this deployment apart from another
|
|
46
|
+
// is the entire content of the node, and a default would be a decision the
|
|
47
|
+
// graph appears to make and nobody authored.
|
|
48
|
+
//
|
|
49
|
+
// The *kind* of test does get a default, and it is the deployment one,
|
|
50
|
+
// because a predicate has to be one of them and an empty variable name is a
|
|
51
|
+
// gate that visibly refuses to publish. A row-count gate with its default
|
|
52
|
+
// threshold would publish happily while testing something nobody chose.
|
|
53
|
+
return { id, name, kind: 'if', predicate: { kind: 'env', envVar: '' }, position };
|
|
54
|
+
}
|
|
55
|
+
if (kind === 'filter') {
|
|
56
|
+
// One empty comparison rather than an empty `all`, and rather than nothing.
|
|
57
|
+
//
|
|
58
|
+
// Nothing is not available: the model has no "no predicate yet" state, on
|
|
59
|
+
// purpose, because a filter whose test is absent has to be given one by
|
|
60
|
+
// somebody and every default is a rule about rows nobody wrote. An empty
|
|
61
|
+
// `all` *is* representable and is refused by the validator, which is exactly
|
|
62
|
+
// why it is not the starting point — a group with no conditions keeps every
|
|
63
|
+
// row, so a filter that started that way would draw as a working node that
|
|
64
|
+
// does nothing.
|
|
65
|
+
//
|
|
66
|
+
// So it starts as one comparison with no column, which the validator refuses
|
|
67
|
+
// by name: the node says "needs a column" on the canvas from the moment it
|
|
68
|
+
// is dropped. The operator defaults to `equals` because it is the only one
|
|
69
|
+
// that is a guess about *form* rather than about data — every other choice
|
|
70
|
+
// implies something about the column's type before a column is chosen.
|
|
71
|
+
return {
|
|
72
|
+
id,
|
|
73
|
+
name,
|
|
74
|
+
kind: 'filter',
|
|
75
|
+
predicate: { kind: 'compare', column: '', operator: 'equals', value: '' },
|
|
76
|
+
position,
|
|
77
|
+
};
|
|
78
|
+
}
|
|
79
|
+
if (kind === 'sink') {
|
|
80
|
+
return { id, name, kind: 'sink', targetType: '', position };
|
|
81
|
+
}
|
|
82
|
+
return unreachableNodeKind(kind, 'newNodeOfKind');
|
|
83
|
+
}
|
|
84
|
+
/**
|
|
85
|
+
* A wire, with its branch already decided when it leaves an `if`.
|
|
86
|
+
*
|
|
87
|
+
* Assigned here rather than left blank for somebody to fill in, because a blank
|
|
88
|
+
* one is refused by `validateWorkflow` and the refusal would fire on the very
|
|
89
|
+
* first wire out of a node somebody just created — the premature-error problem
|
|
90
|
+
* `partitionProblems` exists to describe, arrived at from a different direction.
|
|
91
|
+
*
|
|
92
|
+
* The first wire out of a gate is the `then`, the second is the `else`, and
|
|
93
|
+
* after that it is `then` again. That ordering is not arbitrary: those are the
|
|
94
|
+
* two somebody is drawing when they draw a gate, in that order, and a third wire
|
|
95
|
+
* is fan-out on a side they then choose in the inspector. Every one of them is
|
|
96
|
+
* editable there, so this is a default and never a decision.
|
|
97
|
+
*/
|
|
98
|
+
export function newEdge(nodes, edges, from, to) {
|
|
99
|
+
const source = nodes.find((node) => node.id === from);
|
|
100
|
+
if (source?.kind !== 'if')
|
|
101
|
+
return { from, to };
|
|
102
|
+
const taken = edges.filter((edge) => edge.from === from).map((edge) => edge.branch);
|
|
103
|
+
const free = WORKFLOW_BRANCH_LABELS.find((label) => !taken.includes(label));
|
|
104
|
+
return { from, to, branch: free ?? 'then' };
|
|
105
|
+
}
|
|
106
|
+
/**
|
|
107
|
+
* A name nobody has used yet, so the fourth transform is not also "Transform".
|
|
108
|
+
*
|
|
109
|
+
* Every node used to be born called exactly `defaultLabel(kind)`, which is how
|
|
110
|
+
* a graph ends up with three boxes called "Transform" and a problem message
|
|
111
|
+
* that has to fall back to naming the id — `Sink (sink_3b5a…)` — because the
|
|
112
|
+
* name it was given identifies nothing. A message that names an id is a message
|
|
113
|
+
* whose reader has to go hunting for which box it means.
|
|
114
|
+
*
|
|
115
|
+
* Compared against what nodes are *called* rather than against a counter, so
|
|
116
|
+
* renaming "Transform 2" to "Join" frees the number again, and so a name typed
|
|
117
|
+
* by hand is never duplicated by one generated afterwards. `nodeName` is what
|
|
118
|
+
* the rest of the screen displays, so it is what has to be unique.
|
|
119
|
+
*/
|
|
120
|
+
export function uniqueName(nodes, kind) {
|
|
121
|
+
const base = defaultLabel(kind);
|
|
122
|
+
const taken = new Set(nodes.map((node) => nodeName(node)));
|
|
123
|
+
if (!taken.has(base))
|
|
124
|
+
return base;
|
|
125
|
+
// `taken.size + 1` candidates for `taken.size` names: one of them is free, so
|
|
126
|
+
// this always returns from inside the loop.
|
|
127
|
+
for (let n = 2; n <= taken.size + 1; n += 1) {
|
|
128
|
+
const candidate = `${base} ${n}`;
|
|
129
|
+
if (!taken.has(candidate))
|
|
130
|
+
return candidate;
|
|
131
|
+
}
|
|
132
|
+
return `${base} ${taken.size + 2}`;
|
|
133
|
+
}
|
|
134
|
+
/**
|
|
135
|
+
* A probe id no real node can hold.
|
|
136
|
+
*
|
|
137
|
+
* Node ids are minted by `newLocalId`, which always contains an underscore and a
|
|
138
|
+
* UUID; this cannot collide with one, and it never leaves the function that
|
|
139
|
+
* makes it.
|
|
140
|
+
*/
|
|
141
|
+
const PROBE = '__probe__';
|
|
142
|
+
/**
|
|
143
|
+
* Which kinds of new node this one could legally feed — asked, never restated.
|
|
144
|
+
*
|
|
145
|
+
* The menu must offer only edges the graph allows, and the temptation is to
|
|
146
|
+
* write that down: "a source may feed a transform or a sink; nothing follows a
|
|
147
|
+
* sink". Writing it down is how the canvas ends up with a second copy of rules
|
|
148
|
+
* that live in `canConnect`, and the first time the two disagree the menu either
|
|
149
|
+
* offers an edge that is then refused or hides one that was always fine.
|
|
150
|
+
*
|
|
151
|
+
* So this builds a throwaway node of each kind, drops it into a copy of the
|
|
152
|
+
* graph, and asks `canConnect` — the same function the drag uses, the same one
|
|
153
|
+
* the "send its output to" picker filters with. The probe is never stored and
|
|
154
|
+
* its id never leaves this function. When a rule changes, this follows.
|
|
155
|
+
*
|
|
156
|
+
* The list itself is {@link WORKFLOW_NODE_KINDS} and never a hand-written row:
|
|
157
|
+
* `filter` shipped complete — model, validator, executor, inspector, its own
|
|
158
|
+
* colour on the canvas — and could not be added from this screen at all, because
|
|
159
|
+
* the palette was six JSX elements somebody had typed out and only five of them
|
|
160
|
+
* had been typed.
|
|
161
|
+
*/
|
|
162
|
+
export function newKindsFrom(from, nodes, edges) {
|
|
163
|
+
return WORKFLOW_NODE_KINDS.filter((kind) => {
|
|
164
|
+
const probe = newNodeOfKind(kind, PROBE, { x: 0, y: 0 }, PROBE);
|
|
165
|
+
return canConnect([...nodes, probe], edges, from.id, PROBE).ok;
|
|
166
|
+
});
|
|
167
|
+
}
|
|
168
|
+
/**
|
|
169
|
+
* Which kinds could be **spliced into** an existing wire.
|
|
170
|
+
*
|
|
171
|
+
* Asked exactly like {@link newKindsFrom}, and asked twice, because splicing is
|
|
172
|
+
* two connections and not one: the upstream node has to be able to feed the new
|
|
173
|
+
* kind, *and* the new kind has to be able to feed the downstream node. A menu
|
|
174
|
+
* that checked only the first half would happily offer to insert a sink into the
|
|
175
|
+
* middle of a graph and then produce a graph in which nothing runs after it.
|
|
176
|
+
*
|
|
177
|
+
* The original wire is taken out of the graph before either question is asked.
|
|
178
|
+
* Leaving it in means the second question is answered against a graph where the
|
|
179
|
+
* two ends are still directly connected, and every candidate would be refused
|
|
180
|
+
* for closing a loop that is about to stop existing.
|
|
181
|
+
*/
|
|
182
|
+
export function kindsBetween(edge, nodes, edges) {
|
|
183
|
+
const id = edgeId(edge);
|
|
184
|
+
const rest = edges.filter((candidate) => edgeId(candidate) !== id);
|
|
185
|
+
return WORKFLOW_NODE_KINDS.filter((kind) => {
|
|
186
|
+
const probe = newNodeOfKind(kind, PROBE, { x: 0, y: 0 }, PROBE);
|
|
187
|
+
const all = [...nodes, probe];
|
|
188
|
+
if (!canConnect(all, rest, edge.from, PROBE).ok)
|
|
189
|
+
return false;
|
|
190
|
+
const half = [...rest, { from: edge.from, to: PROBE }];
|
|
191
|
+
return canConnect(all, half, PROBE, edge.to).ok;
|
|
192
|
+
});
|
|
193
|
+
}
|
|
194
|
+
/**
|
|
195
|
+
* The graph with a new node standing in the middle of an existing wire.
|
|
196
|
+
*
|
|
197
|
+
* `A → B` becomes `A → new → B`, and the two new wires go in **at the index the
|
|
198
|
+
* old one held**. That is not cosmetic: a node with several inbound edges
|
|
199
|
+
* receives its inputs in the order the edges appear in this array, and that
|
|
200
|
+
* order is part of what the graph produces. Appending would silently reorder the
|
|
201
|
+
* feeds into `B` — a join would see its two inputs the other way round — which
|
|
202
|
+
* is exactly the kind of change nobody would look for, because on screen the
|
|
203
|
+
* picture is identical.
|
|
204
|
+
*
|
|
205
|
+
* The branch label travels with the first half. A wire leaving an `if` node is
|
|
206
|
+
* on a side, the side decides which half of the pipeline runs, and dropping it
|
|
207
|
+
* here would move the whole downstream branch onto the gate's default and pass
|
|
208
|
+
* validation while doing it. The second half is built by {@link newEdge}, which
|
|
209
|
+
* gives it a branch only if the *inserted* node is itself a gate.
|
|
210
|
+
*/
|
|
211
|
+
export function nodeBetween(edge, kind, nodes) {
|
|
212
|
+
return newNodeOfKind(kind, newLocalId(kind), placeBetween(nodes.find((candidate) => candidate.id === edge.from), nodes.find((candidate) => candidate.id === edge.to), nodes), uniqueName(nodes, kind));
|
|
213
|
+
}
|
|
214
|
+
/**
|
|
215
|
+
* The splice itself, taking the node rather than making one.
|
|
216
|
+
*
|
|
217
|
+
* Separate from {@link nodeBetween} for a reason that has bitten this file's
|
|
218
|
+
* neighbours: the id comes from `newLocalId`, so calling one function twice —
|
|
219
|
+
* once to find out what was created and once inside the state updater — mints
|
|
220
|
+
* two different nodes. The node is therefore made once, outside, and this is
|
|
221
|
+
* pure with respect to it.
|
|
222
|
+
*/
|
|
223
|
+
export function graphWithNodeBetween(edge, node, nodes, edges) {
|
|
224
|
+
const id = edgeId(edge);
|
|
225
|
+
const at = edges.findIndex((candidate) => edgeId(candidate) === id);
|
|
226
|
+
const rest = edges.filter((candidate) => edgeId(candidate) !== id);
|
|
227
|
+
const first = edge.branch === undefined
|
|
228
|
+
? { from: edge.from, to: node.id }
|
|
229
|
+
: { from: edge.from, to: node.id, branch: edge.branch };
|
|
230
|
+
const second = newEdge([...nodes, node], [...rest, first], node.id, edge.to);
|
|
231
|
+
const next = [...rest];
|
|
232
|
+
next.splice(at < 0 ? next.length : at, 0, first, second);
|
|
233
|
+
return { nodes: [...nodes, node], edges: next };
|
|
234
|
+
}
|
|
235
|
+
/** The branch a wire is not on, when it is on one at all. */
|
|
236
|
+
export function otherBranch(branch) {
|
|
237
|
+
return WORKFLOW_BRANCH_LABELS.find((label) => label !== branch) ?? branch;
|
|
238
|
+
}
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
import type { CanvasMenuSection } from './canvas-menu';
|
|
2
|
+
import { type WorkflowNode } from './model';
|
|
3
|
+
/** Where a context menu is anchored, in viewport coordinates. */
|
|
4
|
+
export interface MenuAnchorRect {
|
|
5
|
+
x: number;
|
|
6
|
+
y: number;
|
|
7
|
+
width: number;
|
|
8
|
+
height: number;
|
|
9
|
+
}
|
|
10
|
+
/**
|
|
11
|
+
* The menu itself, anchored to a point rather than to a control.
|
|
12
|
+
*
|
|
13
|
+
* Base UI ships a `ContextMenu` whose `Trigger` owns the `contextmenu` event on
|
|
14
|
+
* a wrapping element, and it is not what this canvas needs: the trigger takes
|
|
15
|
+
* over right-click for its whole subtree unconditionally, and this canvas has to
|
|
16
|
+
* decide *per event* whether it has something better to offer than the browser's
|
|
17
|
+
* menu — and, when it does, which of four different things was clicked. React
|
|
18
|
+
* Flow already answers that question (`onNodeContextMenu`, `onEdgeContextMenu`,
|
|
19
|
+
* `onPaneContextMenu`, `onSelectionContextMenu`), so the event stays the
|
|
20
|
+
* canvas's and only the anchor comes here.
|
|
21
|
+
*
|
|
22
|
+
* `positionMethod="fixed"` because the rect is in viewport coordinates. The
|
|
23
|
+
* collision behaviour is Base UI's default and is the reason it is not
|
|
24
|
+
* configured here: near the right edge the popup flips to the other side of the
|
|
25
|
+
* point, near the bottom it shifts up, and it never renders off-screen — which
|
|
26
|
+
* is the one thing about a context menu that cannot be checked in jsdom and had
|
|
27
|
+
* to be checked in a browser.
|
|
28
|
+
*/
|
|
29
|
+
export declare function CanvasContextMenu({ opening, sections, label, onClose, }: {
|
|
30
|
+
/** Null before anything has been right-clicked, which is most of the time. */
|
|
31
|
+
opening: {
|
|
32
|
+
anchor: MenuAnchorRect;
|
|
33
|
+
} | null;
|
|
34
|
+
sections: CanvasMenuSection[];
|
|
35
|
+
/** What this menu is about, for a screen reader that gets no pointer context. */
|
|
36
|
+
label: string;
|
|
37
|
+
onClose: () => void;
|
|
38
|
+
}): import("react/jsx-runtime").JSX.Element | null;
|
|
39
|
+
/**
|
|
40
|
+
* The controls that appear on the node under the pointer.
|
|
41
|
+
*
|
|
42
|
+
* Two of them, and the second is the one that was asked for: *"tem que ter uma
|
|
43
|
+
* forma mais rápida de deletar o nó… talvez no hover tipo o wire"*. Removing a
|
|
44
|
+
* node meant opening its inspector and scrolling to the bottom of it, which is
|
|
45
|
+
* four gestures for the most ordinary edit there is.
|
|
46
|
+
*
|
|
47
|
+
* It is a separate button rather than a menu item **and** a menu item, because
|
|
48
|
+
* they answer different needs: the button is for somebody who already knows what
|
|
49
|
+
* they want, and the item is for somebody reading the menu to find out what is
|
|
50
|
+
* possible. What keeps that safe is that neither of them asks first and both of
|
|
51
|
+
* them are undoable — see `removal.ts` for why that trade was made in that
|
|
52
|
+
* direction.
|
|
53
|
+
*
|
|
54
|
+
* `NodeToolbar` with an explicit `nodeId` is React Flow's documented way of
|
|
55
|
+
* rendering a toolbar for a node from outside that node's own component, so the
|
|
56
|
+
* placement comes from the same measurements the canvas draws with and this file
|
|
57
|
+
* does no arithmetic on a viewport transform. `offset={0}` is load-bearing: the
|
|
58
|
+
* pointer has to travel from the node to the toolbar, and any gap is canvas —
|
|
59
|
+
* `onNodeMouseLeave` would fire with nothing to catch it and the control would
|
|
60
|
+
* vanish on the way to itself.
|
|
61
|
+
*/
|
|
62
|
+
export declare function NodeActionsToolbar({ node, canEdit, open, sections, deleteHint, onOpenChange, onHoverChange, onDelete, }: {
|
|
63
|
+
/** Null whenever nothing is hovered or selected, which is most of the time. */
|
|
64
|
+
node: WorkflowNode | null;
|
|
65
|
+
canEdit: boolean;
|
|
66
|
+
open: boolean;
|
|
67
|
+
sections: CanvasMenuSection[];
|
|
68
|
+
/** What deleting this node costs, spelled out on the button's tooltip. */
|
|
69
|
+
deleteHint: string;
|
|
70
|
+
onOpenChange: (open: boolean) => void;
|
|
71
|
+
onHoverChange: (hovering: boolean) => void;
|
|
72
|
+
onDelete: () => void;
|
|
73
|
+
}): import("react/jsx-runtime").JSX.Element | null;
|
|
@@ -0,0 +1,174 @@
|
|
|
1
|
+
import { jsx as _jsx, jsxs as _jsxs, Fragment as _Fragment } from "react/jsx-runtime";
|
|
2
|
+
import { Menu as BaseMenu } from '@base-ui/react/menu';
|
|
3
|
+
import { NodeToolbar, Position } from '@xyflow/react';
|
|
4
|
+
import { ArrowRight, Code2, GitBranch, LayoutGrid, Maximize2, MoreHorizontal, PanelRight, Plus, Trash2, Unplug, Waypoints, } from 'lucide-react';
|
|
5
|
+
import { cn } from '../cn';
|
|
6
|
+
import { Tooltip } from '../ui/tooltip';
|
|
7
|
+
import { nodeName } from './model';
|
|
8
|
+
/**
|
|
9
|
+
* The two surfaces that render {@link CanvasMenuSection}, and the way back from
|
|
10
|
+
* the destructive items they carry.
|
|
11
|
+
*
|
|
12
|
+
* ## One list, two ways in
|
|
13
|
+
*
|
|
14
|
+
* The right-click menu and the pill on a node's hover toolbar draw **the same
|
|
15
|
+
* sections from the same builder**. That is the point rather than an economy:
|
|
16
|
+
* the hover pill used to have a bespoke dropdown of its own, and the moment a
|
|
17
|
+
* second menu existed the two would have started answering "what can this node
|
|
18
|
+
* do" differently — which is precisely the failure the pill was already an
|
|
19
|
+
* example of, since it was called "Wire" while the handle beside it was also
|
|
20
|
+
* called wiring.
|
|
21
|
+
*
|
|
22
|
+
* The pill is therefore **renamed**. It says "Actions", it is `aria-label`ed as
|
|
23
|
+
* the actions for the node it belongs to, and its tooltip says the same list is
|
|
24
|
+
* on right-click — because the discovery problem this whole change exists for
|
|
25
|
+
* was never that the menu was missing, it was that nobody could tell the two
|
|
26
|
+
* "wire" things apart. It survives rather than being deleted for one reason
|
|
27
|
+
* worth stating: right-click is not discoverable either. A control that appears
|
|
28
|
+
* under the pointer is how somebody finds out the menu exists at all, and the
|
|
29
|
+
* menu is how they find out what it contains.
|
|
30
|
+
*
|
|
31
|
+
* ## Where the browser's own menu is left alone
|
|
32
|
+
*
|
|
33
|
+
* These take over `contextmenu` on nodes, wires and the canvas pane, and nowhere
|
|
34
|
+
* else. The chrome floating over the canvas — the header card, the action row,
|
|
35
|
+
* the details rail, the inspector sheet, every text field in all of them — is a
|
|
36
|
+
* sibling DOM subtree and is untouched, so right-clicking a name field still
|
|
37
|
+
* gets cut/copy/paste/spell-check, which is the only menu that is any use there.
|
|
38
|
+
*
|
|
39
|
+
* ## Keyboard
|
|
40
|
+
*
|
|
41
|
+
* Nothing extra is bound, and that is deliberate: pressing the context-menu key
|
|
42
|
+
* or Shift+F10 makes the browser dispatch a real `contextmenu` event at whatever
|
|
43
|
+
* has focus, so a focused node or wire opens this menu through the same path a
|
|
44
|
+
* right-click does. What the canvas has to add is the anchor — a keyboard-fired
|
|
45
|
+
* event has no pointer, so it is anchored to the focused element instead of to
|
|
46
|
+
* (0, 0), which is a corner of the screen nowhere near the node. Base UI's menu
|
|
47
|
+
* supplies the rest of the contract: arrow keys move the highlight, Escape
|
|
48
|
+
* closes, focus goes back where it came from.
|
|
49
|
+
*/
|
|
50
|
+
const MUTED = 'text-zinc-400 dark:text-zinc-500';
|
|
51
|
+
const RULE = 'border-zinc-200 dark:border-zinc-800';
|
|
52
|
+
const PANEL = 'bg-white dark:bg-zinc-900';
|
|
53
|
+
const ICONS = {
|
|
54
|
+
open: PanelRight,
|
|
55
|
+
code: Code2,
|
|
56
|
+
arrow: ArrowRight,
|
|
57
|
+
plus: Plus,
|
|
58
|
+
// A wire with a stop on it — the one item on these menus that changes the
|
|
59
|
+
// shape of an existing connection rather than making or breaking one.
|
|
60
|
+
insert: Waypoints,
|
|
61
|
+
branch: GitBranch,
|
|
62
|
+
unplug: Unplug,
|
|
63
|
+
trash: Trash2,
|
|
64
|
+
tidy: LayoutGrid,
|
|
65
|
+
fit: Maximize2,
|
|
66
|
+
};
|
|
67
|
+
/**
|
|
68
|
+
* How a popup looks. Shared, so the two entry points cannot drift visually
|
|
69
|
+
* either — a right-click menu that looked different from the pill's would read
|
|
70
|
+
* as a different feature.
|
|
71
|
+
*
|
|
72
|
+
* The transitions are `motion-safe:` throughout. Base UI drives its enter and
|
|
73
|
+
* exit from `data-starting-style` / `data-ending-style`, which is CSS, so
|
|
74
|
+
* `prefers-reduced-motion` is honoured by the variant rather than by a hook —
|
|
75
|
+
* and the popup still appears, instantly, which is the test for whether an
|
|
76
|
+
* animation was carrying information.
|
|
77
|
+
*/
|
|
78
|
+
const POPUP = cn('z-50 max-h-[min(28rem,var(--available-height))] w-64 overflow-y-auto rounded-md border p-1 shadow-lg outline-none', RULE, PANEL, 'origin-[var(--transform-origin)]', 'motion-safe:transition-[opacity,transform] motion-safe:duration-100 motion-safe:ease-out', 'motion-safe:data-[starting-style]:scale-95 motion-safe:data-[starting-style]:opacity-0', 'motion-safe:data-[ending-style]:scale-95 motion-safe:data-[ending-style]:opacity-0');
|
|
79
|
+
const ITEM = cn('flex w-full cursor-default select-none items-start gap-1.5 rounded px-1.5 py-1 text-left text-[11px] outline-none',
|
|
80
|
+
// `data-highlighted` is Base UI's, and it is set by the keyboard as well as by
|
|
81
|
+
// the pointer — so arrowing down a menu looks like moving a mouse down it,
|
|
82
|
+
// which is the whole reason not to style `:hover` here.
|
|
83
|
+
'data-[highlighted]:bg-zinc-100 dark:data-[highlighted]:bg-zinc-800');
|
|
84
|
+
/**
|
|
85
|
+
* The destructive tone.
|
|
86
|
+
*
|
|
87
|
+
* Red before the pointer arrives rather than on hover, for the reason the
|
|
88
|
+
* `destructive` button variant gives: an action you cannot undo by looking at it
|
|
89
|
+
* should be identifiable from across the menu, not on contact.
|
|
90
|
+
*/
|
|
91
|
+
const DESTRUCTIVE = cn('text-rose-600 dark:text-rose-400', 'data-[highlighted]:bg-rose-50 dark:data-[highlighted]:bg-rose-950/40');
|
|
92
|
+
function MenuItemRow({ item }) {
|
|
93
|
+
const Icon = ICONS[item.icon];
|
|
94
|
+
return (_jsxs(BaseMenu.Item, { className: cn(ITEM, item.destructive && DESTRUCTIVE), onClick: () => item.run(), children: [_jsx(Icon, { size: 11, className: cn('mt-[3px] shrink-0', item.destructive ? '' : MUTED) }), _jsxs("span", { className: "min-w-0 flex-1", children: [_jsxs("span", { className: "flex items-baseline gap-1.5", children: [_jsx("span", { className: "min-w-0 flex-1 truncate", children: item.label }), item.hint && (_jsx("span", { className: cn('shrink-0 font-mono text-[9px]', MUTED), children: item.hint }))] }), item.detail && (_jsx("span", { className: cn('mt-0.5 block text-[10px] leading-snug', MUTED), children: item.detail }))] })] }));
|
|
95
|
+
}
|
|
96
|
+
function MenuSectionBlock({ section }) {
|
|
97
|
+
return (_jsxs(_Fragment, { children: [section.separated && _jsx(BaseMenu.Separator, { className: cn('my-1 h-px border-t', RULE) }), _jsxs(BaseMenu.Group, { className: "py-0.5", children: [section.title && (_jsx(BaseMenu.GroupLabel, { className: cn('px-1.5 pb-0.5 font-mono text-[9px] uppercase tracking-[0.14em]', MUTED), children: section.title })), section.note && (_jsx("p", { className: cn('px-1.5 py-0.5 text-[11px] leading-relaxed', MUTED), children: section.note })), section.items.map((item) => (_jsx(MenuItemRow, { item: item }, item.key)))] })] }));
|
|
98
|
+
}
|
|
99
|
+
function MenuBody({ sections }) {
|
|
100
|
+
return (_jsx(_Fragment, { children: sections.map((section) => (_jsx(MenuSectionBlock, { section: section }, section.key))) }));
|
|
101
|
+
}
|
|
102
|
+
/**
|
|
103
|
+
* The menu itself, anchored to a point rather than to a control.
|
|
104
|
+
*
|
|
105
|
+
* Base UI ships a `ContextMenu` whose `Trigger` owns the `contextmenu` event on
|
|
106
|
+
* a wrapping element, and it is not what this canvas needs: the trigger takes
|
|
107
|
+
* over right-click for its whole subtree unconditionally, and this canvas has to
|
|
108
|
+
* decide *per event* whether it has something better to offer than the browser's
|
|
109
|
+
* menu — and, when it does, which of four different things was clicked. React
|
|
110
|
+
* Flow already answers that question (`onNodeContextMenu`, `onEdgeContextMenu`,
|
|
111
|
+
* `onPaneContextMenu`, `onSelectionContextMenu`), so the event stays the
|
|
112
|
+
* canvas's and only the anchor comes here.
|
|
113
|
+
*
|
|
114
|
+
* `positionMethod="fixed"` because the rect is in viewport coordinates. The
|
|
115
|
+
* collision behaviour is Base UI's default and is the reason it is not
|
|
116
|
+
* configured here: near the right edge the popup flips to the other side of the
|
|
117
|
+
* point, near the bottom it shifts up, and it never renders off-screen — which
|
|
118
|
+
* is the one thing about a context menu that cannot be checked in jsdom and had
|
|
119
|
+
* to be checked in a browser.
|
|
120
|
+
*/
|
|
121
|
+
export function CanvasContextMenu({ opening, sections, label, onClose, }) {
|
|
122
|
+
if (!opening)
|
|
123
|
+
return null;
|
|
124
|
+
const anchor = opening.anchor;
|
|
125
|
+
return (_jsx(BaseMenu.Root, { open: true,
|
|
126
|
+
// Opening is the canvas's decision and is made by a `contextmenu` event, so
|
|
127
|
+
// the only thing this menu's own controls can ask for is to be closed —
|
|
128
|
+
// Escape, an outside press, or an item that has just run.
|
|
129
|
+
onOpenChange: (open) => {
|
|
130
|
+
if (!open)
|
|
131
|
+
onClose();
|
|
132
|
+
},
|
|
133
|
+
// Not modal: a modal menu locks the host page's scroll and disables
|
|
134
|
+
// pointer interaction everywhere outside it, which on a canvas embedded in
|
|
135
|
+
// somebody else's app shell is a heavier promise than a context menu has
|
|
136
|
+
// any business making. Dismiss-on-outside-press and Escape are unaffected.
|
|
137
|
+
modal: false, children: _jsx(BaseMenu.Portal, { children: _jsx(BaseMenu.Positioner, { positionMethod: "fixed", side: "right", align: "start", sideOffset: 2, collisionPadding: 8, anchor: {
|
|
138
|
+
getBoundingClientRect: () => DOMRect.fromRect({
|
|
139
|
+
x: anchor.x,
|
|
140
|
+
y: anchor.y,
|
|
141
|
+
width: anchor.width,
|
|
142
|
+
height: anchor.height,
|
|
143
|
+
}),
|
|
144
|
+
}, className: "z-50", children: _jsx(BaseMenu.Popup, { className: POPUP, "aria-label": label, children: _jsx(MenuBody, { sections: sections }) }) }) }) }));
|
|
145
|
+
}
|
|
146
|
+
/**
|
|
147
|
+
* The controls that appear on the node under the pointer.
|
|
148
|
+
*
|
|
149
|
+
* Two of them, and the second is the one that was asked for: *"tem que ter uma
|
|
150
|
+
* forma mais rápida de deletar o nó… talvez no hover tipo o wire"*. Removing a
|
|
151
|
+
* node meant opening its inspector and scrolling to the bottom of it, which is
|
|
152
|
+
* four gestures for the most ordinary edit there is.
|
|
153
|
+
*
|
|
154
|
+
* It is a separate button rather than a menu item **and** a menu item, because
|
|
155
|
+
* they answer different needs: the button is for somebody who already knows what
|
|
156
|
+
* they want, and the item is for somebody reading the menu to find out what is
|
|
157
|
+
* possible. What keeps that safe is that neither of them asks first and both of
|
|
158
|
+
* them are undoable — see `removal.ts` for why that trade was made in that
|
|
159
|
+
* direction.
|
|
160
|
+
*
|
|
161
|
+
* `NodeToolbar` with an explicit `nodeId` is React Flow's documented way of
|
|
162
|
+
* rendering a toolbar for a node from outside that node's own component, so the
|
|
163
|
+
* placement comes from the same measurements the canvas draws with and this file
|
|
164
|
+
* does no arithmetic on a viewport transform. `offset={0}` is load-bearing: the
|
|
165
|
+
* pointer has to travel from the node to the toolbar, and any gap is canvas —
|
|
166
|
+
* `onNodeMouseLeave` would fire with nothing to catch it and the control would
|
|
167
|
+
* vanish on the way to itself.
|
|
168
|
+
*/
|
|
169
|
+
export function NodeActionsToolbar({ node, canEdit, open, sections, deleteHint, onOpenChange, onHoverChange, onDelete, }) {
|
|
170
|
+
if (!node || !canEdit)
|
|
171
|
+
return null;
|
|
172
|
+
const name = nodeName(node);
|
|
173
|
+
return (_jsxs(NodeToolbar, { nodeId: node.id, isVisible: true, position: Position.Top, align: "end", offset: 0, onMouseEnter: () => onHoverChange(true), onMouseLeave: () => onHoverChange(false), className: "flex items-center gap-1", children: [_jsxs(BaseMenu.Root, { open: open, onOpenChange: onOpenChange, modal: false, children: [_jsxs(BaseMenu.Trigger, { "aria-label": `Actions for ${name}`, className: cn('flex items-center gap-1 rounded-md border px-1.5 py-1 text-[10px] shadow-sm outline-none', RULE, PANEL, 'hover:bg-zinc-50 dark:hover:bg-zinc-800', 'focus-visible:ring-2 focus-visible:ring-sky-500/40'), children: [_jsx(MoreHorizontal, { size: 11 }), "Actions"] }), _jsx(BaseMenu.Portal, { children: _jsx(BaseMenu.Positioner, { side: "bottom", align: "end", sideOffset: 4, collisionPadding: 8, className: "z-50", children: _jsx(BaseMenu.Popup, { className: POPUP, "aria-label": `Actions for ${name}`, children: _jsx(MenuBody, { sections: sections }) }) }) })] }), _jsx(Tooltip, { content: deleteHint, children: _jsx("button", { type: "button", onClick: onDelete, "aria-label": `Delete ${name}`, className: cn('flex items-center rounded-md border px-1.5 py-1 shadow-sm outline-none', RULE, PANEL, 'text-rose-600 hover:bg-rose-50 dark:text-rose-400 dark:hover:bg-rose-950/40', 'focus-visible:ring-2 focus-visible:ring-rose-500/40'), children: _jsx(Trash2, { size: 11 }) }) })] }));
|
|
174
|
+
}
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
import type { WorkflowNode } from './model';
|
|
2
|
+
/**
|
|
3
|
+
* Where a node goes when something other than the toolbar creates it.
|
|
4
|
+
*
|
|
5
|
+
* Three callers now, and they were one: wiring a new node off an existing one,
|
|
6
|
+
* inserting one into a wire that already exists, and dropping one where somebody
|
|
7
|
+
* right-clicked. They share a rule — *land on the grid `layout` would have used,
|
|
8
|
+
* and never on top of something* — and that rule was written out once, inside
|
|
9
|
+
* the canvas, next to seven hundred lines that have nothing to do with geometry.
|
|
10
|
+
*
|
|
11
|
+
* Moved out rather than copied, because the failure of a second copy is silent:
|
|
12
|
+
* two placements that disagree by 32 pixels look correct in isolation and shuffle
|
|
13
|
+
* the whole graph the first time somebody presses Tidy.
|
|
14
|
+
*/
|
|
15
|
+
/**
|
|
16
|
+
* The grid `layout` uses, restated because it keeps its gaps to itself.
|
|
17
|
+
*
|
|
18
|
+
* `NODE_WIDTH` and `NODE_HEIGHT` come from core — the server spaces its own
|
|
19
|
+
* adopted graphs by them — but the column and row *gaps* are private to
|
|
20
|
+
* `layout`. They are repeated here so a node created by any of the gestures
|
|
21
|
+
* below lands where Tidy would have put it. If they ever drift, the consequence
|
|
22
|
+
* is spacing that looks slightly off and never a node in the wrong place: every
|
|
23
|
+
* function here is defined by what is *occupied*, not by these numbers.
|
|
24
|
+
*/
|
|
25
|
+
export declare const COLUMN_STEP: number;
|
|
26
|
+
export declare const ROW_STEP: number;
|
|
27
|
+
/**
|
|
28
|
+
* The first free spot at or below a point, stepping down one row at a time.
|
|
29
|
+
*
|
|
30
|
+
* Down rather than right, in every caller, because down is the axis `layout`
|
|
31
|
+
* treats as "same depth, different branch": moving right would claim a column
|
|
32
|
+
* that means "runs after this", which is a lie about the graph told in the one
|
|
33
|
+
* language the canvas has for saying it.
|
|
34
|
+
*/
|
|
35
|
+
export declare function freeSpot(nodes: WorkflowNode[], at: {
|
|
36
|
+
x: number;
|
|
37
|
+
y: number;
|
|
38
|
+
}): {
|
|
39
|
+
x: number;
|
|
40
|
+
y: number;
|
|
41
|
+
};
|
|
42
|
+
/**
|
|
43
|
+
* Where a node created *by wiring from another node* goes.
|
|
44
|
+
*
|
|
45
|
+
* One column to the right of the node that spawned it, because that is exactly
|
|
46
|
+
* what the canvas's arrangement means: `layout` puts a node one column past the
|
|
47
|
+
* deepest thing feeding it, and a node created by this action is fed by that
|
|
48
|
+
* node and nothing else. So the position that matches the picture is not a
|
|
49
|
+
* guess — it is the position the layout would have chosen anyway.
|
|
50
|
+
*
|
|
51
|
+
* The three obvious alternatives are each wrong in their own way: under the
|
|
52
|
+
* cursor puts a node wherever a menu happened to be dismissed, on top of an
|
|
53
|
+
* existing node reads as "the button did nothing", and off-screen — which is
|
|
54
|
+
* what `nextPosition` does, correctly, for the toolbar's add buttons, since
|
|
55
|
+
* those have no parent to sit beside — leaves somebody looking at an unchanged
|
|
56
|
+
* canvas. Sitting beside its parent means it is on screen whenever its parent
|
|
57
|
+
* is, which is the only guarantee available without measuring the viewport.
|
|
58
|
+
*/
|
|
59
|
+
export declare function placeNextTo(from: WorkflowNode, nodes: WorkflowNode[]): {
|
|
60
|
+
x: number;
|
|
61
|
+
y: number;
|
|
62
|
+
};
|
|
63
|
+
/**
|
|
64
|
+
* Where a node **spliced into an existing wire** goes: between its two ends.
|
|
65
|
+
*
|
|
66
|
+
* Deliberately not `placeNextTo(from)`. A node inserted into `A → B` runs after
|
|
67
|
+
* A and before B, and the whole reason somebody can read this canvas is that
|
|
68
|
+
* left-to-right is run order — so dropping it in A's next column, which is
|
|
69
|
+
* frequently B's column, would draw the new node level with the node it now
|
|
70
|
+
* feeds. Halfway is the only position that states the relationship correctly
|
|
71
|
+
* before Tidy is ever pressed.
|
|
72
|
+
*
|
|
73
|
+
* Rounded to whole pixels because these end up in a saved graph and
|
|
74
|
+
* `{x: 383.5}` in stored JSON is noise that shows up in every diff of it.
|
|
75
|
+
*/
|
|
76
|
+
export declare function placeBetween(from: WorkflowNode | undefined, to: WorkflowNode | undefined, nodes: WorkflowNode[]): {
|
|
77
|
+
x: number;
|
|
78
|
+
y: number;
|
|
79
|
+
};
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
import { NODE_HEIGHT, NODE_WIDTH } from './graph';
|
|
2
|
+
/**
|
|
3
|
+
* Where a node goes when something other than the toolbar creates it.
|
|
4
|
+
*
|
|
5
|
+
* Three callers now, and they were one: wiring a new node off an existing one,
|
|
6
|
+
* inserting one into a wire that already exists, and dropping one where somebody
|
|
7
|
+
* right-clicked. They share a rule — *land on the grid `layout` would have used,
|
|
8
|
+
* and never on top of something* — and that rule was written out once, inside
|
|
9
|
+
* the canvas, next to seven hundred lines that have nothing to do with geometry.
|
|
10
|
+
*
|
|
11
|
+
* Moved out rather than copied, because the failure of a second copy is silent:
|
|
12
|
+
* two placements that disagree by 32 pixels look correct in isolation and shuffle
|
|
13
|
+
* the whole graph the first time somebody presses Tidy.
|
|
14
|
+
*/
|
|
15
|
+
/**
|
|
16
|
+
* The grid `layout` uses, restated because it keeps its gaps to itself.
|
|
17
|
+
*
|
|
18
|
+
* `NODE_WIDTH` and `NODE_HEIGHT` come from core — the server spaces its own
|
|
19
|
+
* adopted graphs by them — but the column and row *gaps* are private to
|
|
20
|
+
* `layout`. They are repeated here so a node created by any of the gestures
|
|
21
|
+
* below lands where Tidy would have put it. If they ever drift, the consequence
|
|
22
|
+
* is spacing that looks slightly off and never a node in the wrong place: every
|
|
23
|
+
* function here is defined by what is *occupied*, not by these numbers.
|
|
24
|
+
*/
|
|
25
|
+
export const COLUMN_STEP = NODE_WIDTH + 96;
|
|
26
|
+
export const ROW_STEP = NODE_HEIGHT + 32;
|
|
27
|
+
/** Every position a node currently sits on, as the key the search below tests. */
|
|
28
|
+
function occupied(nodes) {
|
|
29
|
+
return new Set(nodes.map((node) => `${node.position?.x ?? 0},${node.position?.y ?? 0}`));
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* The first free spot at or below a point, stepping down one row at a time.
|
|
33
|
+
*
|
|
34
|
+
* Down rather than right, in every caller, because down is the axis `layout`
|
|
35
|
+
* treats as "same depth, different branch": moving right would claim a column
|
|
36
|
+
* that means "runs after this", which is a lie about the graph told in the one
|
|
37
|
+
* language the canvas has for saying it.
|
|
38
|
+
*/
|
|
39
|
+
export function freeSpot(nodes, at) {
|
|
40
|
+
const taken = occupied(nodes);
|
|
41
|
+
let y = at.y;
|
|
42
|
+
while (taken.has(`${at.x},${y}`))
|
|
43
|
+
y += ROW_STEP;
|
|
44
|
+
return { x: at.x, y };
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* Where a node created *by wiring from another node* goes.
|
|
48
|
+
*
|
|
49
|
+
* One column to the right of the node that spawned it, because that is exactly
|
|
50
|
+
* what the canvas's arrangement means: `layout` puts a node one column past the
|
|
51
|
+
* deepest thing feeding it, and a node created by this action is fed by that
|
|
52
|
+
* node and nothing else. So the position that matches the picture is not a
|
|
53
|
+
* guess — it is the position the layout would have chosen anyway.
|
|
54
|
+
*
|
|
55
|
+
* The three obvious alternatives are each wrong in their own way: under the
|
|
56
|
+
* cursor puts a node wherever a menu happened to be dismissed, on top of an
|
|
57
|
+
* existing node reads as "the button did nothing", and off-screen — which is
|
|
58
|
+
* what `nextPosition` does, correctly, for the toolbar's add buttons, since
|
|
59
|
+
* those have no parent to sit beside — leaves somebody looking at an unchanged
|
|
60
|
+
* canvas. Sitting beside its parent means it is on screen whenever its parent
|
|
61
|
+
* is, which is the only guarantee available without measuring the viewport.
|
|
62
|
+
*/
|
|
63
|
+
export function placeNextTo(from, nodes) {
|
|
64
|
+
return freeSpot(nodes, {
|
|
65
|
+
x: (from.position?.x ?? 0) + COLUMN_STEP,
|
|
66
|
+
y: from.position?.y ?? 0,
|
|
67
|
+
});
|
|
68
|
+
}
|
|
69
|
+
/**
|
|
70
|
+
* Where a node **spliced into an existing wire** goes: between its two ends.
|
|
71
|
+
*
|
|
72
|
+
* Deliberately not `placeNextTo(from)`. A node inserted into `A → B` runs after
|
|
73
|
+
* A and before B, and the whole reason somebody can read this canvas is that
|
|
74
|
+
* left-to-right is run order — so dropping it in A's next column, which is
|
|
75
|
+
* frequently B's column, would draw the new node level with the node it now
|
|
76
|
+
* feeds. Halfway is the only position that states the relationship correctly
|
|
77
|
+
* before Tidy is ever pressed.
|
|
78
|
+
*
|
|
79
|
+
* Rounded to whole pixels because these end up in a saved graph and
|
|
80
|
+
* `{x: 383.5}` in stored JSON is noise that shows up in every diff of it.
|
|
81
|
+
*/
|
|
82
|
+
export function placeBetween(from, to, nodes) {
|
|
83
|
+
const a = from?.position ?? { x: 0, y: 0 };
|
|
84
|
+
const b = to?.position ?? { x: a.x + COLUMN_STEP * 2, y: a.y };
|
|
85
|
+
return freeSpot(nodes, {
|
|
86
|
+
x: Math.round((a.x + b.x) / 2),
|
|
87
|
+
y: Math.round((a.y + b.y) / 2),
|
|
88
|
+
});
|
|
89
|
+
}
|