@dudousxd/nestjs-catalog-react 0.21.0 → 0.23.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.
@@ -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
+ }
@@ -0,0 +1,27 @@
1
+ import { type WorkflowEdge, type WorkflowNode } from './model';
2
+ /**
3
+ * The graph without those nodes, and what that cost, in one sentence.
4
+ *
5
+ * The edges are counted before the nodes are dropped, because an edge goes for
6
+ * touching a removed node *at either end* — and a caller working from the
7
+ * filtered graph could no longer tell which ones those were.
8
+ *
9
+ * The nodes are named and the wires are counted, deliberately in that
10
+ * proportion. "1 node removed" sends somebody looking at the canvas to work out
11
+ * which; a node with six wires named one at a time is a sentence nobody
12
+ * finishes. The count is what answers the question this sentence exists for:
13
+ * *did anything else just change?*
14
+ */
15
+ export declare function removeNodes(nodes: WorkflowNode[], edges: WorkflowEdge[], ids: readonly string[]): {
16
+ nodes: WorkflowNode[];
17
+ edges: WorkflowEdge[];
18
+ said: string;
19
+ };
20
+ /**
21
+ * How many wires a removal would take, for a warning written before the click.
22
+ *
23
+ * The same count {@link removeNodes} reports afterwards, asked in advance —
24
+ * which is where it does the most good: on the menu item, and on the tooltip of
25
+ * a delete button that does not stop to ask.
26
+ */
27
+ export declare function wiresOn(edges: WorkflowEdge[], ids: readonly string[]): number;
@@ -0,0 +1,81 @@
1
+ import { nodeName } from './model';
2
+ /**
3
+ * Taking nodes out, and saying what went with them.
4
+ *
5
+ * ## Why this is not an undo
6
+ *
7
+ * It was, for about an hour. Deleting a node became a one-click control on the
8
+ * node's hover toolbar and an item on its right-click menu — both asked for,
9
+ * both right — and a one-click delete with no way back is a bad trade on a
10
+ * canvas where the unsaved draft is *everything* somebody has done since their
11
+ * last save. So this file grew a ten-second "removed · undo" window.
12
+ *
13
+ * A real per-action undo stack landed while that was being written, and it is a
14
+ * better answer to the same problem in every respect: it covers every gesture
15
+ * rather than the destructive two, it has a Reset beside it, and it guards the
16
+ * tab against being closed on unsaved work. Shipping a second, narrower
17
+ * mechanism next to it would give the canvas two things called undo that behave
18
+ * differently — which is precisely the failure this whole change exists to fix,
19
+ * since the reason a context menu was needed at all is that the canvas had two
20
+ * things called "wire". So the window went, and `history.tsx` is the way back.
21
+ *
22
+ * ## What is left, and why it is worth a module
23
+ *
24
+ * One fact that the undo stack does not state and nothing else on the screen
25
+ * does either: **a node takes its wires with it.** Removing a box silently
26
+ * unwires up to two other boxes, and a canvas that reports "node removed" leaves
27
+ * somebody to discover half an hour later that their graph no longer reaches its
28
+ * sink. Undo makes that recoverable; it does not make it visible.
29
+ *
30
+ * So this is the filter and the sentence, together, so that every gesture that
31
+ * removes a node says the same thing about what it cost — and so the menu item
32
+ * and the delete button can say it *before* the click as well as after.
33
+ */
34
+ /** "1 connection" / "3 connections", with the plural not left to the reader. */
35
+ function count(n, noun) {
36
+ return `${n} ${noun}${n === 1 ? '' : 's'}`;
37
+ }
38
+ /**
39
+ * The graph without those nodes, and what that cost, in one sentence.
40
+ *
41
+ * The edges are counted before the nodes are dropped, because an edge goes for
42
+ * touching a removed node *at either end* — and a caller working from the
43
+ * filtered graph could no longer tell which ones those were.
44
+ *
45
+ * The nodes are named and the wires are counted, deliberately in that
46
+ * proportion. "1 node removed" sends somebody looking at the canvas to work out
47
+ * which; a node with six wires named one at a time is a sentence nobody
48
+ * finishes. The count is what answers the question this sentence exists for:
49
+ * *did anything else just change?*
50
+ */
51
+ export function removeNodes(nodes, edges, ids) {
52
+ const going = new Set(ids);
53
+ const removed = nodes.filter((node) => going.has(node.id));
54
+ const cut = wiresOn(edges, ids);
55
+ return {
56
+ nodes: nodes.filter((node) => !going.has(node.id)),
57
+ edges: edges.filter((edge) => !going.has(edge.from) && !going.has(edge.to)),
58
+ said: describeRemoval(removed, cut),
59
+ };
60
+ }
61
+ function describeRemoval(removed, wires) {
62
+ if (removed.length === 0)
63
+ return 'Nothing was removed.';
64
+ const named = removed.length <= 3
65
+ ? removed.map((node) => `"${nodeName(node)}"`).join(', ')
66
+ : count(removed.length, 'node');
67
+ if (wires === 0)
68
+ return `${named} removed. Nothing was wired to it.`;
69
+ return `${named} removed, and the ${count(wires, 'connection')} it was part of went with it.`;
70
+ }
71
+ /**
72
+ * How many wires a removal would take, for a warning written before the click.
73
+ *
74
+ * The same count {@link removeNodes} reports afterwards, asked in advance —
75
+ * which is where it does the most good: on the menu item, and on the tooltip of
76
+ * a delete button that does not stop to ask.
77
+ */
78
+ export function wiresOn(edges, ids) {
79
+ const going = new Set(ids);
80
+ return edges.filter((edge) => going.has(edge.from) || going.has(edge.to)).length;
81
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dudousxd/nestjs-catalog-react",
3
- "version": "0.21.0",
3
+ "version": "0.23.0",
4
4
  "description": "Ready-made React screens for @dudousxd/nestjs-catalog — a model manager and a generic object explorer you can drop into your own app shell.",
5
5
  "license": "MIT",
6
6
  "author": "Davide Carvalho",
@@ -110,7 +110,7 @@
110
110
  "tailwind-merge": "3.4.1",
111
111
  "tiptap-markdown": "0.9.0",
112
112
  "typescript": "5.9.3",
113
- "@dudousxd/nestjs-catalog": "0.18.0"
113
+ "@dudousxd/nestjs-catalog": "0.19.0"
114
114
  },
115
115
  "dependencies": {
116
116
  "clsx": "2.1.1",