@asd14/vblocks 5.3.0 → 6.0.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,173 @@
1
+ import { useCallback } from "react"
2
+ import {
3
+ Background,
4
+ Controls,
5
+ Handle,
6
+ MarkerType,
7
+ Position,
8
+ ReactFlow,
9
+ useEdgesState,
10
+ useNodesState,
11
+ type Edge,
12
+ type Node,
13
+ type NodeProps,
14
+ type NodeTypes,
15
+ type OnSelectionChangeFunc,
16
+ } from "@xyflow/react"
17
+
18
+ import { CELL_SIZE, toPixels } from "@self/utils/grid"
19
+
20
+ import { ZoomLevel } from "@self/ui/zoom-level"
21
+
22
+ type PhaseNode = Node<
23
+ { label: string; detail: string; delay?: string },
24
+ "phase"
25
+ >
26
+
27
+ type PhaseEdge = Edge<{ detail: string }>
28
+
29
+ /**
30
+ * The loop. Edit these two lists, this specimen draws the VBlocks serve. A
31
+ * node has a handle on each side, an edge names the two it joins, eg.
32
+ * `right-source` to `left-target`
33
+ */
34
+ const serveNodes = toPixels<PhaseNode>([
35
+ {
36
+ id: "save",
37
+ type: "phase",
38
+ position: { x: 0, y: 0 },
39
+ size: { width: 12, height: 4 },
40
+ data: { label: "You", detail: "Save a file in src/" },
41
+ },
42
+ {
43
+ id: "watcher",
44
+ type: "phase",
45
+ position: { x: 18, y: 0 },
46
+ size: { width: 12, height: 4 },
47
+ data: {
48
+ label: "Watcher",
49
+ detail: "Waits for more saves, eg. of a codemod, then starts one compile",
50
+ delay: "100 ms",
51
+ },
52
+ },
53
+ {
54
+ id: "compile",
55
+ type: "phase",
56
+ position: { x: 18, y: 10 },
57
+ size: { width: 12, height: 4 },
58
+ data: { label: "Webpack", detail: "Compiles the modules the saves touched" },
59
+ },
60
+ {
61
+ id: "browser",
62
+ type: "phase",
63
+ position: { x: 0, y: 10 },
64
+ size: { width: 12, height: 4 },
65
+ data: {
66
+ label: "Browser",
67
+ detail:
68
+ "Hot Module Replacement swaps the modules, React Refresh keeps the state of each component",
69
+ },
70
+ },
71
+ ])
72
+
73
+ const serveEdges: PhaseEdge[] = [
74
+ {
75
+ id: "save-watcher",
76
+ source: "save",
77
+ sourceHandle: "right-source",
78
+ target: "watcher",
79
+ targetHandle: "left-target",
80
+ label: "save",
81
+ animated: true,
82
+ data: { detail: "Your editor writes the file to disk" },
83
+ },
84
+ {
85
+ id: "watcher-compile",
86
+ source: "watcher",
87
+ sourceHandle: "bottom-source",
88
+ target: "compile",
89
+ targetHandle: "top-target",
90
+ label: "changed files",
91
+ animated: true,
92
+ data: { detail: "The files of the last compile that changed or went away" },
93
+ },
94
+ {
95
+ id: "compile-browser",
96
+ source: "compile",
97
+ sourceHandle: "left-source",
98
+ target: "browser",
99
+ targetHandle: "right-target",
100
+ label: "hot update",
101
+ animated: true,
102
+ data: { detail: "The development server sends the new modules over a WebSocket" },
103
+ },
104
+ {
105
+ id: "browser-save",
106
+ source: "browser",
107
+ sourceHandle: "top-source",
108
+ target: "save",
109
+ targetHandle: "bottom-target",
110
+ label: "you see the change",
111
+ animated: true,
112
+ data: { detail: "The page updates without a reload" },
113
+ },
114
+ ]
115
+
116
+ const SIDES = [Position.Top, Position.Right, Position.Bottom, Position.Left]
117
+
118
+ const Phase = ({ data, selected }: NodeProps<PhaseNode>) => (
119
+ <div
120
+ className={`flex h-full w-full flex-col justify-center rounded-full border bg-white px-5 text-center shadow-sm ${selected ? "border-stone-900" : "border-stone-300"}`}>
121
+ {SIDES.map(side => (
122
+ <span key={side}>
123
+ <Handle type="source" position={side} id={`${side}-source`} />
124
+ <Handle type="target" position={side} id={`${side}-target`} />
125
+ </span>
126
+ ))}
127
+ <div className="text-sm font-semibold text-stone-900">{data.label}</div>
128
+ {data.delay && <div className="text-xs text-stone-500">{data.delay}</div>}
129
+ </div>
130
+ )
131
+
132
+ const nodeTypes: NodeTypes = { phase: Phase }
133
+
134
+ type ServeDiagramProps = {
135
+ onSelect: (entityId: string | undefined) => void
136
+ }
137
+
138
+ const ServeDiagram = ({ onSelect }: ServeDiagramProps) => {
139
+ const [nodes, , onNodesChange] = useNodesState(serveNodes)
140
+ const [edges, , onEdgesChange] = useEdgesState(serveEdges)
141
+
142
+ // Memoize it, as React Flow asks for onSelectionChange
143
+ const onSelectionChange: OnSelectionChangeFunc = useCallback(
144
+ selection => {
145
+ onSelect(selection.nodes[0]?.id ?? selection.edges[0]?.id)
146
+ },
147
+ [onSelect]
148
+ )
149
+
150
+ return (
151
+ <ReactFlow
152
+ nodes={nodes}
153
+ edges={edges}
154
+ nodeTypes={nodeTypes}
155
+ onNodesChange={onNodesChange}
156
+ onEdgesChange={onEdgesChange}
157
+ onSelectionChange={onSelectionChange}
158
+ // Snap a dragged node to the dots of <Background>
159
+ snapToGrid
160
+ snapGrid={[CELL_SIZE, CELL_SIZE]}
161
+ defaultEdgeOptions={{ markerEnd: { type: MarkerType.ArrowClosed } }}
162
+ // Center the diagram at 100%, the fit-view button of <Controls> still
163
+ // fits it to the screen
164
+ fitView
165
+ fitViewOptions={{ minZoom: 1, maxZoom: 1 }}>
166
+ <Background gap={CELL_SIZE} />
167
+ <Controls />
168
+ <ZoomLevel />
169
+ </ReactFlow>
170
+ )
171
+ }
172
+
173
+ export { ServeDiagram, serveEdges, serveNodes }
@@ -0,0 +1,29 @@
1
+ import { Panel, useReactFlow, useViewport } from "@xyflow/react"
2
+
3
+ /**
4
+ * The zoom of the diagram, as a percentage, and a button that centers the
5
+ * diagram at 100%. Render it inside `<ReactFlow>`, both hooks read the viewport
6
+ * from there
7
+ */
8
+ const ZoomLevel = () => {
9
+ const { zoom } = useViewport()
10
+ const { fitView } = useReactFlow()
11
+
12
+ return (
13
+ <Panel
14
+ position="top-right"
15
+ className="flex items-center gap-2 rounded border border-stone-200 bg-white px-2 py-1 font-mono text-xs leading-none text-stone-600">
16
+ <span>{Math.round(zoom * 100)}%</span>
17
+ <button
18
+ type="button"
19
+ onClick={() => {
20
+ void fitView({ minZoom: 1, maxZoom: 1, duration: 200 })
21
+ }}
22
+ className="cursor-pointer text-stone-500 hover:text-stone-900">
23
+ Reset
24
+ </button>
25
+ </Panel>
26
+ )
27
+ }
28
+
29
+ export { ZoomLevel }
@@ -0,0 +1,44 @@
1
+ import type { Node } from "@xyflow/react"
2
+
3
+ /**
4
+ * The side of one grid cell, in pixels. `<Background>` draws a dot per cell,
5
+ * and a dragged node snaps to the dots
6
+ */
7
+ const CELL_SIZE = 20
8
+
9
+ /**
10
+ * A node with its `position` and `size` in cells. Give each side an even number
11
+ * of cells, so the handle in its middle sits on a dot
12
+ */
13
+ type CellNode<T extends Node> = T & {
14
+ size: { width: number; height: number }
15
+ }
16
+
17
+ /**
18
+ * Convert the position and the size of each node from cells to pixels
19
+ *
20
+ * @example
21
+ * toPixels<StepNode>([
22
+ * {
23
+ * id: "swc",
24
+ * type: "step",
25
+ * position: { x: 0, y: 7 },
26
+ * size: { width: 12, height: 4 },
27
+ * data: { label: "SWC", detail: "Compiles TypeScript and JSX" },
28
+ * },
29
+ * ])
30
+ * // => [{ id: "swc", position: { x: 0, y: 140 }, width: 240, height: 80, … }]
31
+ */
32
+ const toPixels = <T extends Node>(nodes: CellNode<T>[]): T[] =>
33
+ nodes.map(node => ({
34
+ ...node,
35
+ position: {
36
+ x: node.position.x * CELL_SIZE,
37
+ y: node.position.y * CELL_SIZE,
38
+ },
39
+ width: node.size.width * CELL_SIZE,
40
+ height: node.size.height * CELL_SIZE,
41
+ }))
42
+
43
+ export { CELL_SIZE, toPixels }
44
+ export type { CellNode }
@@ -0,0 +1,16 @@
1
+ #!/bin/sh
2
+
3
+ # Runs in the app folder, after `vblocks init` copies the app files. npm writes
4
+ # the nearest package.json, the project root's
5
+
6
+ set -eu
7
+
8
+ npm pkg set \
9
+ "dependencies.@xyflow/react=^12" \
10
+ "devDependencies.@tailwindcss/postcss=^4" \
11
+ "devDependencies.tailwindcss=^4"
12
+
13
+ # Name the page in the browser tab.
14
+ # `sed -i` differs between GNU and BSD, so write a copy and move it over
15
+ sed 's|<title>.*</title>|<title>xyflow-tabs :: VBlocks</title>|' src/index.html >src/index.html.tmp
16
+ mv -- src/index.html.tmp src/index.html