@asd14/vblocks 5.3.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.
@@ -1,120 +1,35 @@
1
- import { useCallback } from "react"
1
+ import { useState } from "react"
2
2
  import { createRoot } from "react-dom/client"
3
- import {
4
- addEdge,
5
- Background,
6
- Controls,
7
- Handle,
8
- Position,
9
- ReactFlow,
10
- useEdgesState,
11
- useNodesState,
12
- type Edge,
13
- type Node,
14
- type NodeProps,
15
- type NodeTypes,
16
- type OnConnect,
17
- } from "@xyflow/react"
3
+
4
+ import { Details } from "./details"
5
+ import { Diagram } from "./diagram"
18
6
 
19
7
  import "@xyflow/react/dist/style.css"
20
8
  import "./index.css"
21
9
 
22
- type StepNode = Node<{ label: string; detail: string }, "step">
23
-
24
- /**
25
- * The graph. Edit these two lists, this specimen draws the VBlocks build.
26
- */
27
- const initialNodes: StepNode[] = [
28
- {
29
- id: "index.tsx",
30
- type: "step",
31
- position: { x: 0, y: 0 },
32
- data: { label: "src/index.tsx", detail: "The application entry point" },
33
- },
34
- {
35
- id: "index.css",
36
- type: "step",
37
- position: { x: 280, y: 0 },
38
- data: { label: "src/index.css", detail: "The global CSS" },
39
- },
40
- {
41
- id: "swc",
42
- type: "step",
43
- position: { x: 0, y: 140 },
44
- data: { label: "SWC", detail: "Compiles TypeScript and JSX" },
45
- },
46
- {
47
- id: "postcss",
48
- type: "step",
49
- position: { x: 280, y: 140 },
50
- data: { label: "PostCSS", detail: "Runs Tailwind and cssnano" },
51
- },
52
- {
53
- id: "index.html",
54
- type: "step",
55
- position: { x: 560, y: 140 },
56
- data: { label: "src/index.html", detail: "The HTML template" },
57
- },
58
- {
59
- id: "webpack",
60
- type: "step",
61
- position: { x: 280, y: 280 },
62
- data: { label: "Webpack", detail: "Bundles the modules and the assets" },
63
- },
64
- {
65
- id: "dist",
66
- type: "step",
67
- position: { x: 280, y: 420 },
68
- data: { label: "dist/index.html", detail: "The build output" },
69
- },
70
- ]
71
-
72
- const initialEdges: Edge[] = [
73
- { id: "index.tsx-swc", source: "index.tsx", target: "swc" },
74
- { id: "index.css-postcss", source: "index.css", target: "postcss" },
75
- { id: "swc-webpack", source: "swc", target: "webpack" },
76
- { id: "postcss-webpack", source: "postcss", target: "webpack" },
77
- { id: "index.html-webpack", source: "index.html", target: "webpack" },
78
- { id: "webpack-dist", source: "webpack", target: "dist", animated: true },
79
- ]
80
-
81
- const Step = ({ data }: NodeProps<StepNode>) => (
82
- <div className="w-56 rounded-md border border-stone-300 bg-white px-4 py-2 shadow-sm">
83
- <Handle type="target" position={Position.Top} />
84
- <div className="font-mono text-sm font-semibold text-stone-900">
85
- {data.label}
86
- </div>
87
- <div className="text-xs text-stone-500">{data.detail}</div>
88
- <Handle type="source" position={Position.Bottom} />
89
- </div>
90
- )
91
-
92
- const nodeTypes: NodeTypes = { step: Step }
93
-
94
- const Playground = () => {
95
- const [nodes, , onNodesChange] = useNodesState(initialNodes)
96
- const [edges, setEdges, onEdgesChange] = useEdgesState(initialEdges)
97
-
98
- const onConnect: OnConnect = useCallback(
99
- connection => {
100
- setEdges(currentEdges => addEdge(connection, currentEdges))
101
- },
102
- [setEdges]
103
- )
10
+ const App = () => {
11
+ const [selectedId, setSelectedId] = useState<string>()
104
12
 
105
13
  return (
106
- <div className="h-screen w-screen">
107
- <ReactFlow
108
- nodes={nodes}
109
- edges={edges}
110
- nodeTypes={nodeTypes}
111
- onNodesChange={onNodesChange}
112
- onEdgesChange={onEdgesChange}
113
- onConnect={onConnect}
114
- fitView>
115
- <Background />
116
- <Controls />
117
- </ReactFlow>
14
+ <div className="grid h-screen grid-rows-[auto_1fr] bg-stone-50 text-stone-900">
15
+ <header className="border-b border-stone-200 bg-white px-6 py-3">
16
+ <h1 className="text-lg font-semibold">VBlocks</h1>
17
+ <p className="text-sm text-stone-500">How build turns src/ into a page</p>
18
+ </header>
19
+
20
+ <div className="grid min-h-0 grid-cols-[1fr_20rem]">
21
+ <main className="min-h-0">
22
+ <Diagram onSelect={setSelectedId} />
23
+ </main>
24
+
25
+ <aside className="overflow-y-auto border-l border-stone-200 bg-white p-6">
26
+ {selectedId === undefined ? (
27
+ <p className="text-sm text-stone-500">Select a node or an edge</p>
28
+ ) : (
29
+ <Details selectedId={selectedId} />
30
+ )}
31
+ </aside>
32
+ </div>
118
33
  </div>
119
34
  )
120
35
  }
@@ -125,4 +40,4 @@ if (!root) {
125
40
  throw new Error("index.html has no #root element")
126
41
  }
127
42
 
128
- createRoot(root).render(<Playground />)
43
+ createRoot(root).render(<App />)
@@ -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 }
@@ -9,3 +9,8 @@ npm pkg set \
9
9
  "dependencies.@xyflow/react=^12" \
10
10
  "devDependencies.@tailwindcss/postcss=^4" \
11
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 :: VBlocks</title>|' src/index.html >src/index.html.tmp
16
+ mv -- src/index.html.tmp src/index.html
@@ -0,0 +1,13 @@
1
+ /**
2
+ * @param {{ mode: "production" | "development" | "none" }} api
3
+ */
4
+ export default ({ mode }) => ({
5
+ plugins: {
6
+ "@tailwindcss/postcss": {
7
+ optimize: mode === "production",
8
+ },
9
+ "cssnano": mode === "production" && {
10
+ preset: "default",
11
+ },
12
+ },
13
+ })
@@ -0,0 +1,3 @@
1
+ /* Scan only this app. With `--prefix`, you run VBlocks from the project folder,
2
+ where Tailwind would also scan every other app */
3
+ @import "tailwindcss" source(".");
@@ -0,0 +1,90 @@
1
+ import { useState } from "react"
2
+ import { createRoot } from "react-dom/client"
3
+
4
+ import { BuildDetails } from "./pages/build/details"
5
+ import { BuildDiagram } from "./pages/build/diagram"
6
+ import { ServeDetails } from "./pages/serve/details"
7
+ import { ServeDiagram } from "./pages/serve/diagram"
8
+
9
+ import "@xyflow/react/dist/style.css"
10
+ import "./index.css"
11
+
12
+ /**
13
+ * The tabs of the diagram. For a new tab, add a folder in `pages/` with a
14
+ * diagram and its details, then add it here and in each slot of `App`
15
+ */
16
+ const TABS = [
17
+ { id: "build", label: "Build" },
18
+ { id: "serve", label: "Serve" },
19
+ ] as const
20
+
21
+ type TabId = (typeof TABS)[number]["id"]
22
+
23
+ const App = () => {
24
+ const [tabId, setTabId] = useState<TabId>("build")
25
+ const [selectedId, setSelectedId] = useState<string>()
26
+
27
+ const selectTab = (nextTabId: TabId) => {
28
+ setTabId(nextTabId)
29
+ // An id of one tab names nothing in the other
30
+ setSelectedId(undefined)
31
+ }
32
+
33
+ return (
34
+ <div className="grid h-screen grid-rows-[auto_1fr] bg-stone-50 text-stone-900">
35
+ <header className="border-b border-stone-200 bg-white px-6 py-3">
36
+ <h1 className="text-lg font-semibold">VBlocks</h1>
37
+ <p className="text-sm text-stone-500">
38
+ How serve and build turn src/ into a page
39
+ </p>
40
+ </header>
41
+
42
+ <div className="grid min-h-0 grid-cols-[1fr_20rem]">
43
+ <main className="flex min-h-0 flex-col">
44
+ <nav className="flex gap-4 border-b border-stone-200 bg-white px-6">
45
+ {TABS.map(tab => (
46
+ <button
47
+ key={tab.id}
48
+ type="button"
49
+ aria-selected={tab.id === tabId}
50
+ onClick={() => {
51
+ selectTab(tab.id)
52
+ }}
53
+ className={
54
+ tab.id === tabId
55
+ ? "cursor-pointer border-b-2 border-stone-900 py-2 text-sm font-medium"
56
+ : "cursor-pointer border-b-2 border-transparent py-2 text-sm text-stone-500 hover:text-stone-900"
57
+ }>
58
+ {tab.label}
59
+ </button>
60
+ ))}
61
+ </nav>
62
+
63
+ <div className="min-h-0 flex-1">
64
+ {tabId === "build" && <BuildDiagram onSelect={setSelectedId} />}
65
+ {tabId === "serve" && <ServeDiagram onSelect={setSelectedId} />}
66
+ </div>
67
+ </main>
68
+
69
+ <aside className="overflow-y-auto border-l border-stone-200 bg-white p-6">
70
+ {selectedId === undefined ? (
71
+ <p className="text-sm text-stone-500">Select a node or an edge</p>
72
+ ) : (
73
+ <>
74
+ {tabId === "build" && <BuildDetails selectedId={selectedId} />}
75
+ {tabId === "serve" && <ServeDetails selectedId={selectedId} />}
76
+ </>
77
+ )}
78
+ </aside>
79
+ </div>
80
+ </div>
81
+ )
82
+ }
83
+
84
+ const root = document.querySelector("#root")
85
+
86
+ if (!root) {
87
+ throw new Error("index.html has no #root element")
88
+ }
89
+
90
+ createRoot(root).render(<App />)
@@ -0,0 +1,74 @@
1
+ import { buildEdges, buildNodes } from "./diagram"
2
+
3
+ const findLabel = (nodeId: string) =>
4
+ buildNodes.find(node => node.id === nodeId)?.data.label ?? nodeId
5
+
6
+ type LabelListProps = {
7
+ title: string
8
+ labels: string[]
9
+ }
10
+
11
+ const LabelList = ({ title, labels }: LabelListProps) => (
12
+ <div className="mt-4">
13
+ <h3 className="text-xs font-semibold tracking-wide text-stone-500 uppercase">
14
+ {title}
15
+ </h3>
16
+ <ul className="mt-1 font-mono text-sm">
17
+ {labels.length === 0 ? (
18
+ <li className="text-stone-400">none</li>
19
+ ) : (
20
+ labels.map(label => <li key={label}>{label}</li>)
21
+ )}
22
+ </ul>
23
+ </div>
24
+ )
25
+
26
+ type BuildDetailsProps = {
27
+ selectedId: string
28
+ }
29
+
30
+ /**
31
+ * The selected step, with the steps it reads from and writes to, or the
32
+ * selected edge
33
+ */
34
+ const BuildDetails = ({ selectedId }: BuildDetailsProps) => {
35
+ const node = buildNodes.find(item => item.id === selectedId)
36
+
37
+ if (node) {
38
+ return (
39
+ <section>
40
+ <h2 className="font-mono text-sm font-semibold">{node.data.label}</h2>
41
+ <p className="mt-1 text-sm text-stone-600">{node.data.detail}</p>
42
+ <LabelList
43
+ title="In"
44
+ labels={buildEdges
45
+ .filter(edge => edge.target === node.id)
46
+ .map(edge => findLabel(edge.source))}
47
+ />
48
+ <LabelList
49
+ title="Out"
50
+ labels={buildEdges
51
+ .filter(edge => edge.source === node.id)
52
+ .map(edge => findLabel(edge.target))}
53
+ />
54
+ </section>
55
+ )
56
+ }
57
+
58
+ const edge = buildEdges.find(item => item.id === selectedId)
59
+
60
+ return (
61
+ <section>
62
+ <h2 className="font-mono text-sm font-semibold">
63
+ {edge
64
+ ? `${findLabel(edge.source)} → ${findLabel(edge.target)}`
65
+ : selectedId}
66
+ </h2>
67
+ <p className="mt-1 text-sm text-stone-600">
68
+ {edge ? "The output of one step, read by the next" : "A new edge"}
69
+ </p>
70
+ </section>
71
+ )
72
+ }
73
+
74
+ export { BuildDetails }
@@ -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 }