@asd14/vblocks 5.2.0 → 5.4.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,151 @@
1
+ import { useCallback } from "react"
2
+ import {
3
+ addEdge,
4
+ Background,
5
+ Controls,
6
+ Handle,
7
+ MarkerType,
8
+ Position,
9
+ ReactFlow,
10
+ useEdgesState,
11
+ useNodesState,
12
+ type Edge,
13
+ type Node,
14
+ type NodeProps,
15
+ type NodeTypes,
16
+ type OnConnect,
17
+ type OnSelectionChangeFunc,
18
+ } from "@xyflow/react"
19
+
20
+ import { CELL_SIZE, toPixels } from "@self/utils/grid"
21
+
22
+ import { ZoomLevel } from "@self/ui/zoom-level"
23
+
24
+ type StepNode = Node<{ label: string; detail: string }, "step">
25
+
26
+ /**
27
+ * The graph. Edit these two lists, this specimen draws the VBlocks build
28
+ */
29
+ const buildNodes = toPixels<StepNode>([
30
+ {
31
+ id: "index.tsx",
32
+ type: "step",
33
+ position: { x: 0, y: 0 },
34
+ size: { width: 12, height: 4 },
35
+ data: { label: "src/index.tsx", detail: "The application entry point" },
36
+ },
37
+ {
38
+ id: "index.css",
39
+ type: "step",
40
+ position: { x: 14, y: 0 },
41
+ size: { width: 12, height: 4 },
42
+ data: { label: "src/index.css", detail: "The global CSS" },
43
+ },
44
+ {
45
+ id: "swc",
46
+ type: "step",
47
+ position: { x: 0, y: 7 },
48
+ size: { width: 12, height: 4 },
49
+ data: { label: "SWC", detail: "Compiles TypeScript and JSX" },
50
+ },
51
+ {
52
+ id: "postcss",
53
+ type: "step",
54
+ position: { x: 14, y: 7 },
55
+ size: { width: 12, height: 4 },
56
+ data: { label: "PostCSS", detail: "Runs Tailwind and cssnano" },
57
+ },
58
+ {
59
+ id: "index.html",
60
+ type: "step",
61
+ position: { x: 28, y: 7 },
62
+ size: { width: 12, height: 4 },
63
+ data: { label: "src/index.html", detail: "The HTML template" },
64
+ },
65
+ {
66
+ id: "webpack",
67
+ type: "step",
68
+ position: { x: 14, y: 14 },
69
+ size: { width: 12, height: 4 },
70
+ data: { label: "Webpack", detail: "Bundles the modules and the assets" },
71
+ },
72
+ {
73
+ id: "dist",
74
+ type: "step",
75
+ position: { x: 14, y: 21 },
76
+ size: { width: 12, height: 4 },
77
+ data: { label: "dist/index.html", detail: "The bundle" },
78
+ },
79
+ ])
80
+
81
+ const buildEdges: Edge[] = [
82
+ { id: "index.tsx-swc", source: "index.tsx", target: "swc" },
83
+ { id: "index.css-postcss", source: "index.css", target: "postcss" },
84
+ { id: "swc-webpack", source: "swc", target: "webpack" },
85
+ { id: "postcss-webpack", source: "postcss", target: "webpack" },
86
+ { id: "index.html-webpack", source: "index.html", target: "webpack" },
87
+ { id: "webpack-dist", source: "webpack", target: "dist", animated: true },
88
+ ]
89
+
90
+ const Step = ({ data, selected }: NodeProps<StepNode>) => (
91
+ <div
92
+ className={`h-full w-full overflow-hidden rounded-md border bg-white px-4 py-2 shadow-sm ${selected ? "border-stone-900" : "border-stone-300"}`}>
93
+ <Handle type="target" position={Position.Top} />
94
+ <div className="font-mono text-sm font-semibold text-stone-900">
95
+ {data.label}
96
+ </div>
97
+ <div className="text-xs text-stone-500">{data.detail}</div>
98
+ <Handle type="source" position={Position.Bottom} />
99
+ </div>
100
+ )
101
+
102
+ const nodeTypes: NodeTypes = { step: Step }
103
+
104
+ type BuildDiagramProps = {
105
+ onSelect: (entityId: string | undefined) => void
106
+ }
107
+
108
+ const BuildDiagram = ({ onSelect }: BuildDiagramProps) => {
109
+ const [nodes, , onNodesChange] = useNodesState(buildNodes)
110
+ const [edges, setEdges, onEdgesChange] = useEdgesState(buildEdges)
111
+
112
+ const onConnect: OnConnect = useCallback(
113
+ connection => {
114
+ setEdges(currentEdges => addEdge(connection, currentEdges))
115
+ },
116
+ [setEdges]
117
+ )
118
+
119
+ // Memoize it, as React Flow asks for onSelectionChange
120
+ const onSelectionChange: OnSelectionChangeFunc = useCallback(
121
+ selection => {
122
+ onSelect(selection.nodes[0]?.id ?? selection.edges[0]?.id)
123
+ },
124
+ [onSelect]
125
+ )
126
+
127
+ return (
128
+ <ReactFlow
129
+ nodes={nodes}
130
+ edges={edges}
131
+ nodeTypes={nodeTypes}
132
+ onNodesChange={onNodesChange}
133
+ onEdgesChange={onEdgesChange}
134
+ onConnect={onConnect}
135
+ onSelectionChange={onSelectionChange}
136
+ // Snap a dragged node to the dots of <Background>
137
+ snapToGrid
138
+ snapGrid={[CELL_SIZE, CELL_SIZE]}
139
+ defaultEdgeOptions={{ markerEnd: { type: MarkerType.ArrowClosed } }}
140
+ // Center the diagram at 100%, the fit-view button of <Controls> still
141
+ // fits it to the screen
142
+ fitView
143
+ fitViewOptions={{ minZoom: 1, maxZoom: 1 }}>
144
+ <Background gap={CELL_SIZE} />
145
+ <Controls />
146
+ <ZoomLevel />
147
+ </ReactFlow>
148
+ )
149
+ }
150
+
151
+ export { BuildDiagram, buildEdges, buildNodes }
@@ -0,0 +1,33 @@
1
+ import { serveEdges, serveNodes } from "./diagram"
2
+
3
+ type ServeDetailsProps = {
4
+ selectedId: string
5
+ }
6
+
7
+ /**
8
+ * The selected phase of the loop, with how long it waits, or the selected
9
+ * hand-off between two phases
10
+ */
11
+ const ServeDetails = ({ selectedId }: ServeDetailsProps) => {
12
+ const node = serveNodes.find(item => item.id === selectedId)
13
+ const edge = serveEdges.find(item => item.id === selectedId)
14
+
15
+ return (
16
+ <section>
17
+ <h2 className="text-sm font-semibold">
18
+ {node?.data.label ?? edge?.label ?? selectedId}
19
+ </h2>
20
+ <p className="mt-1 text-sm text-stone-600">
21
+ {node?.data.detail ?? edge?.data?.detail}
22
+ </p>
23
+ {node?.data.delay && (
24
+ <p className="mt-4 text-sm">
25
+ <span className="text-stone-500">Waits </span>
26
+ <span className="font-mono">{node.data.delay}</span>
27
+ </p>
28
+ )}
29
+ </section>
30
+ )
31
+ }
32
+
33
+ export { ServeDetails }
@@ -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