@auscera/design-labs 0.0.0-stage → 0.1.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/apps/workspace/.skills/design-labs/SKILL.md +63 -0
- package/apps/workspace/.skills/design-labs/references/commands.md +38 -0
- package/apps/workspace/AGENTS.md +9 -0
- package/apps/workspace/app/(lab)/compare/page.tsx +2 -0
- package/apps/workspace/app/(lab)/lab.css +412 -0
- package/apps/workspace/app/(lab)/layout.tsx +21 -0
- package/apps/workspace/app/(lab)/page.tsx +2 -0
- package/apps/workspace/app/api/annotations/route.ts +94 -0
- package/apps/workspace/app/api/versions/events/route.ts +63 -0
- package/apps/workspace/app/api/versions/route.ts +7 -0
- package/apps/workspace/components/animated-icons.tsx +72 -0
- package/apps/workspace/components/dotmatrix-loader.css +1228 -0
- package/apps/workspace/components/glass-controls.tsx +47 -0
- package/apps/workspace/components/lab-providers.tsx +17 -0
- package/apps/workspace/components/molecules/brand-lockup.tsx +13 -0
- package/apps/workspace/components/molecules/pane-url-popover.css +92 -0
- package/apps/workspace/components/molecules/pane-url-popover.tsx +191 -0
- package/apps/workspace/components/pane-preferences.tsx +75 -0
- package/apps/workspace/components/pane-viewport.css +34 -0
- package/apps/workspace/components/pane-viewport.tsx +199 -0
- package/apps/workspace/components/preview-pane.css +293 -0
- package/apps/workspace/components/preview-pane.tsx +662 -0
- package/apps/workspace/components/ui/dotm-triangle-11.tsx +170 -0
- package/apps/workspace/components/ui/dotmatrix-core.tsx +1649 -0
- package/apps/workspace/components/ui/dotmatrix-hooks.ts +224 -0
- package/apps/workspace/components/version-select.tsx +220 -0
- package/apps/workspace/components/workspace.tsx +313 -0
- package/apps/workspace/eslint.config.mjs +3 -0
- package/apps/workspace/lab.config.json +1 -0
- package/apps/workspace/lib/annotation-export.ts +28 -0
- package/apps/workspace/lib/annotation-types.ts +38 -0
- package/apps/workspace/lib/device-presets.ts +103 -0
- package/apps/workspace/lib/use-annotation-bridge.ts +130 -0
- package/apps/workspace/lib/version-types.ts +12 -0
- package/apps/workspace/lib/website-url.ts +78 -0
- package/apps/workspace/next-env.d.ts +6 -0
- package/apps/workspace/next.config.mjs +1 -0
- package/apps/workspace/package.json +35 -0
- package/apps/workspace/public/apple-touch-icon.png +0 -0
- package/apps/workspace/public/brand/design-labs-script.LICENSE.txt +93 -0
- package/apps/workspace/public/brand/design-labs-script.woff2 +0 -0
- package/apps/workspace/public/brand/logo.webp +0 -0
- package/apps/workspace/public/brand/wordmark.css +42 -0
- package/apps/workspace/public/favicon.ico +0 -0
- package/apps/workspace/public/icon-192.png +0 -0
- package/apps/workspace/public/icon-512.png +0 -0
- package/apps/workspace/public/preview-bridge.js +411 -0
- package/apps/workspace/public/site.webmanifest +1 -0
- package/apps/workspace/scripts/annotation-store.mjs +308 -0
- package/apps/workspace/scripts/lab.mjs +414 -0
- package/apps/workspace/scripts/migrate.mjs +2 -0
- package/apps/workspace/scripts/prepare-preview.mjs +203 -0
- package/apps/workspace/tsconfig.json +24 -0
- package/package.json +9 -3
- package/scripts/install.mjs +67 -0
- package/scripts/update.mjs +204 -0
- package/README.md +0 -3
|
@@ -0,0 +1,313 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
import Link from "next/link";
|
|
3
|
+
import { ArrowUpRight, ArrowLeftRight } from "./animated-icons";
|
|
4
|
+
import { BrandLockup } from "./molecules/brand-lockup";
|
|
5
|
+
import { GlassToggle, EmptyState } from "./glass-controls";
|
|
6
|
+
import { useClonePanePreference } from "./pane-preferences";
|
|
7
|
+
import { PreviewPane } from "./preview-pane";
|
|
8
|
+
import { useEffect, useState } from "react";
|
|
9
|
+
import { useQuery, useQueryClient } from "@tanstack/react-query";
|
|
10
|
+
import type { LabVersion as Version } from "../lib/version-types";
|
|
11
|
+
type Pane = { id: string; version: string };
|
|
12
|
+
type Tree =
|
|
13
|
+
| Pane
|
|
14
|
+
| { id: string; direction: "row" | "column"; children: [Tree, Tree] };
|
|
15
|
+
const isPane = (t: Tree): t is Pane => "version" in t;
|
|
16
|
+
function validTree(value: unknown, depth = 0): boolean {
|
|
17
|
+
if (value === null) return true;
|
|
18
|
+
if (!value || typeof value !== "object" || Array.isArray(value) || depth > 20) return false;
|
|
19
|
+
const node = value as Record<string, unknown>;
|
|
20
|
+
if (typeof node.id !== "string" || !node.id || node.id.length > 200) return false;
|
|
21
|
+
if ("version" in node) return typeof node.version === "string" && node.version.length <= 300;
|
|
22
|
+
return (node.direction === "row" || node.direction === "column") && Array.isArray(node.children) && node.children.length === 2 && node.children.every((child) => child !== null && validTree(child, depth + 1));
|
|
23
|
+
}
|
|
24
|
+
function replace(t: Tree, id: string, value: Tree | null): Tree | null {
|
|
25
|
+
if (t.id === id) return value;
|
|
26
|
+
if (isPane(t)) return t;
|
|
27
|
+
const a = replace(t.children[0], id, value),
|
|
28
|
+
b = replace(t.children[1], id, value);
|
|
29
|
+
return a && b ? { ...t, children: [a, b] } : a || b;
|
|
30
|
+
}
|
|
31
|
+
function find(t: Tree, id: string): Pane | undefined {
|
|
32
|
+
if (isPane(t)) return t.id === id ? t : undefined;
|
|
33
|
+
return find(t.children[0], id) || find(t.children[1], id);
|
|
34
|
+
}
|
|
35
|
+
function useVersions() {
|
|
36
|
+
const client = useQueryClient();
|
|
37
|
+
const [streamError, setStreamError] = useState(false);
|
|
38
|
+
const [, tick] = useState(0);
|
|
39
|
+
const query = useQuery<Version[]>({
|
|
40
|
+
queryKey: ["design-labs", "versions"],
|
|
41
|
+
queryFn: async () => {
|
|
42
|
+
const response = await fetch("/api/versions");
|
|
43
|
+
if (!response.ok) throw Error("Could not load versions");
|
|
44
|
+
return response.json();
|
|
45
|
+
},
|
|
46
|
+
staleTime: 60000,
|
|
47
|
+
gcTime: 300000,
|
|
48
|
+
});
|
|
49
|
+
useEffect(() => {
|
|
50
|
+
let events: EventSource | undefined;
|
|
51
|
+
const connect = () => {
|
|
52
|
+
events?.close();
|
|
53
|
+
if (document.hidden) return;
|
|
54
|
+
events = new EventSource("/api/versions/events");
|
|
55
|
+
events.onmessage = (event) => {
|
|
56
|
+
try {
|
|
57
|
+
client.setQueryData(
|
|
58
|
+
["design-labs", "versions"],
|
|
59
|
+
JSON.parse(event.data),
|
|
60
|
+
);
|
|
61
|
+
setStreamError(false);
|
|
62
|
+
} catch {
|
|
63
|
+
setStreamError(true);
|
|
64
|
+
}
|
|
65
|
+
};
|
|
66
|
+
events.onerror = () => setStreamError(true);
|
|
67
|
+
};
|
|
68
|
+
connect();
|
|
69
|
+
document.addEventListener("visibilitychange", connect);
|
|
70
|
+
const clock = setInterval(() => {
|
|
71
|
+
if (!document.hidden) tick((n) => n + 1);
|
|
72
|
+
}, 60000);
|
|
73
|
+
return () => {
|
|
74
|
+
events?.close();
|
|
75
|
+
clearInterval(clock);
|
|
76
|
+
document.removeEventListener("visibilitychange", connect);
|
|
77
|
+
};
|
|
78
|
+
}, [client]);
|
|
79
|
+
return { versions: query.data || [], error: query.isError || streamError };
|
|
80
|
+
}
|
|
81
|
+
function Changed({ version: v }: { version: Version }) {
|
|
82
|
+
if (!v.updatedAt)
|
|
83
|
+
return <p className="changed">Previous change time unknown</p>;
|
|
84
|
+
const minutes = Math.max(
|
|
85
|
+
0,
|
|
86
|
+
Math.floor((Date.now() - Date.parse(v.updatedAt)) / 60000),
|
|
87
|
+
);
|
|
88
|
+
const relative =
|
|
89
|
+
minutes < 1
|
|
90
|
+
? "just now"
|
|
91
|
+
: minutes < 60
|
|
92
|
+
? `${minutes} minutes ago`
|
|
93
|
+
: minutes < 1440
|
|
94
|
+
? `${Math.floor(minutes / 60)} hours ago`
|
|
95
|
+
: `${Math.floor(minutes / 1440)} days ago`;
|
|
96
|
+
return (
|
|
97
|
+
<p className="changed" title={new Date(v.updatedAt).toLocaleString()}>
|
|
98
|
+
{v.changeTimeKind === "detected" ? "Changes detected" : "Last changed"}:{" "}
|
|
99
|
+
<time dateTime={v.updatedAt}>{relative}</time>
|
|
100
|
+
<span className="exact-time">
|
|
101
|
+
{" "}
|
|
102
|
+
· {new Date(v.updatedAt).toLocaleString()}
|
|
103
|
+
</span>
|
|
104
|
+
</p>
|
|
105
|
+
);
|
|
106
|
+
}
|
|
107
|
+
export default function Workspace({ compare = false }: { compare?: boolean }) {
|
|
108
|
+
const { versions, error } = useVersions();
|
|
109
|
+
const clonePanePreference = useClonePanePreference();
|
|
110
|
+
const [category, setCategory] = useState<"landing" | "dashboard">("landing");
|
|
111
|
+
const [tree, setTree] = useState<Tree | null>(null),
|
|
112
|
+
[max, setMax] = useState<string | null>(null),
|
|
113
|
+
[initialized, setInitialized] = useState(false);
|
|
114
|
+
const [storedTrees, setStoredTrees] = useState<Partial<Record<"landing" | "dashboard", Tree | null>>>({});
|
|
115
|
+
const [storedMax, setStoredMax] = useState<Partial<Record<"landing" | "dashboard", string | null>>>({});
|
|
116
|
+
const [hydrated, setHydrated] = useState(false);
|
|
117
|
+
const available = versions.filter((v) => v.category === category);
|
|
118
|
+
useEffect(() => {
|
|
119
|
+
try {
|
|
120
|
+
const raw = localStorage.getItem("design-labs:compare-layout:v1");
|
|
121
|
+
if (raw) {
|
|
122
|
+
const saved = JSON.parse(raw);
|
|
123
|
+
if (saved && saved.schemaVersion === 1 && saved.trees && typeof saved.trees === "object" && !Array.isArray(saved.trees) && Object.values(saved.trees).every((tree) => validTree(tree)) && (saved.category === "landing" || saved.category === "dashboard")) {
|
|
124
|
+
const current = saved.category as "landing" | "dashboard";
|
|
125
|
+
setCategory(current); setStoredTrees(saved.trees); setStoredMax(saved.max || {});
|
|
126
|
+
setTree(saved.trees[current] ?? null); setMax(saved.max?.[current] ?? null); setInitialized(true);
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
} catch { /* Invalid or unavailable storage: initialize normally. */ }
|
|
130
|
+
setHydrated(true);
|
|
131
|
+
}, []);
|
|
132
|
+
useEffect(() => {
|
|
133
|
+
if (!hydrated || !initialized || !compare) return;
|
|
134
|
+
try { localStorage.setItem("design-labs:compare-layout:v1", JSON.stringify({schemaVersion:1,category,trees:{...storedTrees,[category]:tree},max:{...storedMax,[category]:max}})); } catch { /* Continue without persistence when storage is unavailable. */ }
|
|
135
|
+
}, [hydrated, initialized, compare, category, tree, max, storedTrees, storedMax]);
|
|
136
|
+
useEffect(() => {
|
|
137
|
+
if (hydrated && !initialized && versions.length) {
|
|
138
|
+
const q = new URLSearchParams(location.search);
|
|
139
|
+
const selected = versions.find((v) => v.key === q.get("left"));
|
|
140
|
+
if (selected) setCategory(selected.category);
|
|
141
|
+
const list = selected
|
|
142
|
+
? versions.filter((v) => v.category === selected.category)
|
|
143
|
+
: versions.filter((v) => v.category === "landing");
|
|
144
|
+
const left = selected || list[0],
|
|
145
|
+
right = list.find((v) => v.key === q.get("right")) || list[1] || left;
|
|
146
|
+
if (left)
|
|
147
|
+
setTree({
|
|
148
|
+
id: "initial",
|
|
149
|
+
direction: "row",
|
|
150
|
+
children: [
|
|
151
|
+
{ id: "left", version: left.key },
|
|
152
|
+
{ id: "right", version: right.key },
|
|
153
|
+
],
|
|
154
|
+
});
|
|
155
|
+
setInitialized(true);
|
|
156
|
+
}
|
|
157
|
+
}, [versions, initialized, hydrated]);
|
|
158
|
+
function changeCategory(c: "landing" | "dashboard") {
|
|
159
|
+
if (c === category) return;
|
|
160
|
+
setStoredTrees((previous) => ({ ...previous, [category]: tree }));
|
|
161
|
+
setStoredMax((previous) => ({ ...previous, [category]: max }));
|
|
162
|
+
setCategory(c);
|
|
163
|
+
const v = versions.find((v) => v.category === c);
|
|
164
|
+
setTree(Object.hasOwn(storedTrees, c) ? storedTrees[c] ?? null : v ? { id: crypto.randomUUID(), version: v.key } : null);
|
|
165
|
+
setMax(storedMax[c] ?? null);
|
|
166
|
+
setInitialized(true);
|
|
167
|
+
}
|
|
168
|
+
function split(p: Pane, direction: "row" | "column") {
|
|
169
|
+
const nextId = crypto.randomUUID();
|
|
170
|
+
clonePanePreference(p.id, nextId);
|
|
171
|
+
setTree((t) =>
|
|
172
|
+
t
|
|
173
|
+
? replace(t, p.id, {
|
|
174
|
+
id: crypto.randomUUID(),
|
|
175
|
+
direction,
|
|
176
|
+
children: [p, { id: nextId, version: p.version }],
|
|
177
|
+
})
|
|
178
|
+
: t,
|
|
179
|
+
);
|
|
180
|
+
setMax(null);
|
|
181
|
+
}
|
|
182
|
+
function render(t: Tree): React.ReactNode {
|
|
183
|
+
if (!isPane(t))
|
|
184
|
+
return (
|
|
185
|
+
<div key={t.id} className={`pane-split split-${t.direction}`}>
|
|
186
|
+
{t.children.map(render)}
|
|
187
|
+
</div>
|
|
188
|
+
);
|
|
189
|
+
const v = versions.find((v) => v.key === t.version);
|
|
190
|
+
return (
|
|
191
|
+
<PreviewPane
|
|
192
|
+
key={t.id}
|
|
193
|
+
id={t.id}
|
|
194
|
+
version={v}
|
|
195
|
+
options={available}
|
|
196
|
+
maximized={max === t.id}
|
|
197
|
+
onVersionChange={(value) =>
|
|
198
|
+
setTree((root) =>
|
|
199
|
+
root ? replace(root, t.id, { ...t, version: value }) : null,
|
|
200
|
+
)
|
|
201
|
+
}
|
|
202
|
+
onSplit={(direction) => split(t, direction)}
|
|
203
|
+
onMaximize={() => setMax(max === t.id ? null : t.id)}
|
|
204
|
+
onClose={() => {
|
|
205
|
+
setTree((root) => (root ? replace(root, t.id, null) : null));
|
|
206
|
+
setMax(null);
|
|
207
|
+
}}
|
|
208
|
+
/>
|
|
209
|
+
);
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
const maximized = tree && max ? find(tree, max) : undefined;
|
|
213
|
+
return (
|
|
214
|
+
<main className={`lab-shell ${compare ? "compare-shell" : ""}`}>
|
|
215
|
+
<header className="lab-header">
|
|
216
|
+
<BrandLockup />
|
|
217
|
+
<GlassToggle value={category} onChange={changeCategory} />
|
|
218
|
+
<Link className="button" href={compare ? "/" : "/compare"}>
|
|
219
|
+
{compare ? "All versions" : <>Compare versions <ArrowUpRight size={14} aria-hidden /></>}
|
|
220
|
+
</Link>
|
|
221
|
+
</header>
|
|
222
|
+
{error && (
|
|
223
|
+
<p role="alert">Could not refresh version metadata. Retrying…</p>
|
|
224
|
+
)}
|
|
225
|
+
{compare ? (
|
|
226
|
+
<div
|
|
227
|
+
className={maximized ? "pane-workspace maximized" : "pane-workspace"}
|
|
228
|
+
>
|
|
229
|
+
{tree ? (
|
|
230
|
+
render(maximized || tree)
|
|
231
|
+
) : (
|
|
232
|
+
<EmptyState>
|
|
233
|
+
<p>No panes open.</p>
|
|
234
|
+
<button
|
|
235
|
+
className="button"
|
|
236
|
+
onClick={() => {
|
|
237
|
+
setInitialized(true);
|
|
238
|
+
setTree({
|
|
239
|
+
id: crypto.randomUUID(),
|
|
240
|
+
version: available[0]?.key || "",
|
|
241
|
+
});
|
|
242
|
+
}}
|
|
243
|
+
>
|
|
244
|
+
Add pane
|
|
245
|
+
</button>
|
|
246
|
+
</EmptyState>
|
|
247
|
+
)}
|
|
248
|
+
</div>
|
|
249
|
+
) : (
|
|
250
|
+
<>
|
|
251
|
+
<section className="lab-intro">
|
|
252
|
+
<h1>Your websites, in every direction.</h1>
|
|
253
|
+
<p>
|
|
254
|
+
Explore a version. Try its interactions. Compare what changes.
|
|
255
|
+
</p>
|
|
256
|
+
</section>
|
|
257
|
+
<section className="version-grid" aria-label="Website versions">
|
|
258
|
+
{available.map((v) => (
|
|
259
|
+
<article className="version-card" key={v.key}>
|
|
260
|
+
{v.path ? (
|
|
261
|
+
<Link className="website-preview" href={v.path}>
|
|
262
|
+
<div className="preview-canvas">
|
|
263
|
+
<iframe
|
|
264
|
+
src={v.path}
|
|
265
|
+
title={`${v.label} thumbnail`}
|
|
266
|
+
tabIndex={-1}
|
|
267
|
+
/>
|
|
268
|
+
</div>
|
|
269
|
+
<span className="preview-open">Open website <ArrowUpRight size={14} aria-hidden /></span>
|
|
270
|
+
</Link>
|
|
271
|
+
) : (
|
|
272
|
+
<div className="preview-placeholder">
|
|
273
|
+
Preview awaiting setup
|
|
274
|
+
</div>
|
|
275
|
+
)}
|
|
276
|
+
<div className="version-details">
|
|
277
|
+
<div className="version-meta">
|
|
278
|
+
<span>{v.project}</span>
|
|
279
|
+
<span>{v.production ? "Production" : v.status}</span>
|
|
280
|
+
</div>
|
|
281
|
+
<h2>{v.label}</h2>
|
|
282
|
+
<p>{v.summary}</p>
|
|
283
|
+
<Changed version={v} />
|
|
284
|
+
<div className="card-actions">
|
|
285
|
+
{v.path && (
|
|
286
|
+
<a className="button" href={v.path}>
|
|
287
|
+
Open website <ArrowUpRight size={14} aria-hidden />
|
|
288
|
+
</a>
|
|
289
|
+
)}
|
|
290
|
+
<Link
|
|
291
|
+
className="button"
|
|
292
|
+
href={`/compare?left=${encodeURIComponent(v.key)}`}
|
|
293
|
+
>
|
|
294
|
+
Compare <ArrowLeftRight size={14} aria-hidden />
|
|
295
|
+
</Link>
|
|
296
|
+
</div>
|
|
297
|
+
</div>
|
|
298
|
+
</article>
|
|
299
|
+
))}
|
|
300
|
+
</section>
|
|
301
|
+
{!available.length && (
|
|
302
|
+
<EmptyState>
|
|
303
|
+
<p>
|
|
304
|
+
No {category === "landing" ? "landing pages" : "dashboards"}{" "}
|
|
305
|
+
yet. Ask your agent to snapshot a Next.js project.
|
|
306
|
+
</p>
|
|
307
|
+
</EmptyState>
|
|
308
|
+
)}
|
|
309
|
+
</>
|
|
310
|
+
)}
|
|
311
|
+
</main>
|
|
312
|
+
);
|
|
313
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"schemaVersion":1,"versions":[]}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import type { Annotation } from "./annotation-types";
|
|
2
|
+
export function annotationMarkdown(items: Annotation[], versionLabel: string) {
|
|
3
|
+
return `# Design Labs annotations — ${versionLabel}\n\n${items.map((a, index) => `## ${index + 1}. ${a.comment}\n\n- Version: ${a.versionKey}\n- Page: ${a.target.pathname}\n- Viewport: ${a.target.viewport.width} × ${a.target.viewport.height}\n- Selector: \`${a.target.selector.replaceAll("`", "\\`")}\`\n- Element: ${a.target.tag} — ${a.target.text}\n${a.target.component ? `- Component: ${a.target.component}\n` : ""}${a.target.sourceFile ? `- Source hint: ${a.target.sourceFile}\n` : ""}\n<details><summary>Rendered element context</summary>\n\n\`\`\`html\n${a.target.html.replaceAll("```", "```")}\n\`\`\`\n\nStyles: ${JSON.stringify(a.target.styles)}\n\n</details>\n\nCreated: ${a.createdAt}\n`).join("\n")}`;
|
|
4
|
+
}
|
|
5
|
+
export function downloadReview(
|
|
6
|
+
items: Annotation[],
|
|
7
|
+
label: string,
|
|
8
|
+
format: "md" | "json",
|
|
9
|
+
) {
|
|
10
|
+
const body =
|
|
11
|
+
format === "md"
|
|
12
|
+
? annotationMarkdown(items, label)
|
|
13
|
+
: JSON.stringify(
|
|
14
|
+
{ schemaVersion: 1, version: label, annotations: items },
|
|
15
|
+
null,
|
|
16
|
+
2,
|
|
17
|
+
);
|
|
18
|
+
const url = URL.createObjectURL(
|
|
19
|
+
new Blob([body], {
|
|
20
|
+
type: format === "md" ? "text/markdown" : "application/json",
|
|
21
|
+
}),
|
|
22
|
+
);
|
|
23
|
+
const link = document.createElement("a");
|
|
24
|
+
link.href = url;
|
|
25
|
+
link.download = `design-labs-review.${format}`;
|
|
26
|
+
link.click();
|
|
27
|
+
setTimeout(() => URL.revokeObjectURL(url), 1000);
|
|
28
|
+
}
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
export type ElementBounds = {
|
|
2
|
+
x: number;
|
|
3
|
+
y: number;
|
|
4
|
+
width: number;
|
|
5
|
+
height: number;
|
|
6
|
+
};
|
|
7
|
+
export type TargetContext = {
|
|
8
|
+
selector: string;
|
|
9
|
+
tag: string;
|
|
10
|
+
text: string;
|
|
11
|
+
html: string;
|
|
12
|
+
styles: Record<string, string>;
|
|
13
|
+
bounds: ElementBounds;
|
|
14
|
+
viewport: { width: number; height: number };
|
|
15
|
+
pathname: string;
|
|
16
|
+
component?: string;
|
|
17
|
+
sourceFile?: string;
|
|
18
|
+
};
|
|
19
|
+
export type Annotation = {
|
|
20
|
+
id: string;
|
|
21
|
+
versionKey: string;
|
|
22
|
+
comment: string;
|
|
23
|
+
target: TargetContext;
|
|
24
|
+
createdAt: string;
|
|
25
|
+
updatedAt: string;
|
|
26
|
+
};
|
|
27
|
+
export type AnnotationPin = {
|
|
28
|
+
id: string;
|
|
29
|
+
selector: string;
|
|
30
|
+
tag?: string;
|
|
31
|
+
text?: string;
|
|
32
|
+
};
|
|
33
|
+
export type ResolvedPin = {
|
|
34
|
+
id: string;
|
|
35
|
+
selector: string;
|
|
36
|
+
found: boolean;
|
|
37
|
+
bounds?: ElementBounds;
|
|
38
|
+
};
|
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
export type ViewportSize = { width: number; height: number };
|
|
2
|
+
export type DevicePreset = ViewportSize & { id: string; label: string };
|
|
3
|
+
export type DeviceGroup = { label: string; devices: DevicePreset[] };
|
|
4
|
+
|
|
5
|
+
export const DEFAULT_VIEWPORT: ViewportSize = { width: 390, height: 844 };
|
|
6
|
+
export const VIEWPORT_LIMITS = {
|
|
7
|
+
minWidth: 240,
|
|
8
|
+
maxWidth: 3840,
|
|
9
|
+
minHeight: 240,
|
|
10
|
+
maxHeight: 2160,
|
|
11
|
+
} as const;
|
|
12
|
+
|
|
13
|
+
export function clampViewport(size: ViewportSize): ViewportSize {
|
|
14
|
+
const finite = (value: number, fallback: number) =>
|
|
15
|
+
Number.isFinite(value) ? Math.round(value) : fallback;
|
|
16
|
+
return {
|
|
17
|
+
width: Math.max(
|
|
18
|
+
VIEWPORT_LIMITS.minWidth,
|
|
19
|
+
Math.min(
|
|
20
|
+
VIEWPORT_LIMITS.maxWidth,
|
|
21
|
+
finite(size.width, DEFAULT_VIEWPORT.width),
|
|
22
|
+
),
|
|
23
|
+
),
|
|
24
|
+
height: Math.max(
|
|
25
|
+
VIEWPORT_LIMITS.minHeight,
|
|
26
|
+
Math.min(
|
|
27
|
+
VIEWPORT_LIMITS.maxHeight,
|
|
28
|
+
finite(size.height, DEFAULT_VIEWPORT.height),
|
|
29
|
+
),
|
|
30
|
+
),
|
|
31
|
+
};
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
// CSS viewport sizes, not physical panel pixels. Verified against Chromium DevTools
|
|
35
|
+
// EmulatedDevices.ts on 2026-11-17 (foldable open sizes use vertical-spanned).
|
|
36
|
+
// https://github.com/ChromeDevTools/devtools-frontend/blob/main/front_end/models/emulation/EmulatedDevices.ts
|
|
37
|
+
export const DEVICE_GROUPS: DeviceGroup[] = [
|
|
38
|
+
{
|
|
39
|
+
label: "Phones",
|
|
40
|
+
devices: [
|
|
41
|
+
{ id: "iphone-se", label: "iPhone SE", width: 375, height: 667 },
|
|
42
|
+
{ id: "iphone-16", label: "iPhone 16", width: 393, height: 852 },
|
|
43
|
+
{
|
|
44
|
+
id: "iphone-16-pro-max",
|
|
45
|
+
label: "iPhone 16 Pro Max",
|
|
46
|
+
width: 440,
|
|
47
|
+
height: 956,
|
|
48
|
+
},
|
|
49
|
+
{ id: "pixel-9", label: "Pixel 9", width: 412, height: 924 },
|
|
50
|
+
{ id: "pixel-9-pro", label: "Pixel 9 Pro", width: 427, height: 952 },
|
|
51
|
+
{ id: "pixel-10", label: "Pixel 10", width: 412, height: 924 },
|
|
52
|
+
{
|
|
53
|
+
id: "galaxy-a55",
|
|
54
|
+
label: "Samsung Galaxy A55",
|
|
55
|
+
width: 360,
|
|
56
|
+
height: 800,
|
|
57
|
+
},
|
|
58
|
+
],
|
|
59
|
+
},
|
|
60
|
+
{
|
|
61
|
+
label: "Foldables",
|
|
62
|
+
devices: [
|
|
63
|
+
{ id: "pixel-fold", label: "Pixel 9 Pro Fold", width: 412, height: 922 },
|
|
64
|
+
{
|
|
65
|
+
id: "pixel-fold-open",
|
|
66
|
+
label: "Pixel 9 Pro Fold · open",
|
|
67
|
+
width: 836,
|
|
68
|
+
height: 842,
|
|
69
|
+
},
|
|
70
|
+
{ id: "galaxy-fold", label: "Galaxy Z Fold 6", width: 412, height: 968 },
|
|
71
|
+
{
|
|
72
|
+
id: "galaxy-fold-open",
|
|
73
|
+
label: "Galaxy Z Fold 6 · open",
|
|
74
|
+
width: 744,
|
|
75
|
+
height: 860,
|
|
76
|
+
},
|
|
77
|
+
],
|
|
78
|
+
},
|
|
79
|
+
{
|
|
80
|
+
label: "Tablets & desktops",
|
|
81
|
+
devices: [
|
|
82
|
+
{ id: "ipad-mini", label: "iPad Mini", width: 768, height: 1024 },
|
|
83
|
+
{ id: "ipad-pro", label: "iPad Pro 13", width: 1032, height: 1376 },
|
|
84
|
+
{ id: "surface-pro", label: "Surface Pro 10", width: 960, height: 1440 },
|
|
85
|
+
{ id: "desktop-hd", label: "Desktop · 1280", width: 1280, height: 800 },
|
|
86
|
+
{ id: "desktop-wide", label: "Desktop · 1440", width: 1440, height: 900 },
|
|
87
|
+
{
|
|
88
|
+
id: "desktop-full-hd",
|
|
89
|
+
label: "Desktop · 1920",
|
|
90
|
+
width: 1920,
|
|
91
|
+
height: 1080,
|
|
92
|
+
},
|
|
93
|
+
],
|
|
94
|
+
},
|
|
95
|
+
{
|
|
96
|
+
label: "Smart displays",
|
|
97
|
+
devices: [
|
|
98
|
+
{ id: "nest-hub-max", label: "Nest Hub Max", width: 1280, height: 800 },
|
|
99
|
+
],
|
|
100
|
+
},
|
|
101
|
+
];
|
|
102
|
+
|
|
103
|
+
export const DEVICE_PRESETS = DEVICE_GROUPS.flatMap((group) => group.devices);
|
|
@@ -0,0 +1,130 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
import {
|
|
3
|
+
useCallback,
|
|
4
|
+
useEffect,
|
|
5
|
+
useRef,
|
|
6
|
+
useState,
|
|
7
|
+
type RefObject,
|
|
8
|
+
} from "react";
|
|
9
|
+
import type {
|
|
10
|
+
TargetContext,
|
|
11
|
+
ResolvedPin,
|
|
12
|
+
Annotation,
|
|
13
|
+
} from "./annotation-types";
|
|
14
|
+
export function useAnnotationBridge(
|
|
15
|
+
frame: RefObject<HTMLIFrameElement | null>,
|
|
16
|
+
url: string | undefined,
|
|
17
|
+
enabled: boolean,
|
|
18
|
+
onTarget: (target: TargetContext) => void,
|
|
19
|
+
onCancel: () => void,
|
|
20
|
+
) {
|
|
21
|
+
const [ready, setReady] = useState(false),
|
|
22
|
+
[pathname, setPathname] = useState(""),
|
|
23
|
+
[pins, setPins] = useState<ResolvedPin[]>([]);
|
|
24
|
+
const session = useRef("");
|
|
25
|
+
const callback = useRef({ onTarget, onCancel });
|
|
26
|
+
callback.current = { onTarget, onCancel };
|
|
27
|
+
const timer = useRef<ReturnType<typeof setInterval> | undefined>(undefined);
|
|
28
|
+
const origin =
|
|
29
|
+
typeof window !== "undefined" && url
|
|
30
|
+
? new URL(url, window.location.href).origin
|
|
31
|
+
: "";
|
|
32
|
+
const send = useCallback(
|
|
33
|
+
(message: Record<string, unknown>) => {
|
|
34
|
+
if (frame.current?.contentWindow && origin && session.current)
|
|
35
|
+
frame.current.contentWindow.postMessage(
|
|
36
|
+
{ channel: "design-labs", session: session.current, ...message },
|
|
37
|
+
origin,
|
|
38
|
+
);
|
|
39
|
+
},
|
|
40
|
+
[frame, origin],
|
|
41
|
+
);
|
|
42
|
+
const connect = useCallback(() => {
|
|
43
|
+
setReady(false);
|
|
44
|
+
setPins([]);
|
|
45
|
+
if (timer.current) clearInterval(timer.current);
|
|
46
|
+
if (!enabled || !frame.current || !url) return;
|
|
47
|
+
session.current = crypto.randomUUID();
|
|
48
|
+
try {
|
|
49
|
+
const doc = frame.current.contentDocument;
|
|
50
|
+
if (doc) {
|
|
51
|
+
doc.querySelector("script[data-design-labs-parent]")?.remove();
|
|
52
|
+
const script = doc.createElement("script");
|
|
53
|
+
script.src = new URL("/preview-bridge.js", window.location.href).href;
|
|
54
|
+
script.dataset.labOrigin = window.location.origin;
|
|
55
|
+
script.dataset.session = session.current;
|
|
56
|
+
script.dataset.designLabsParent = "true";
|
|
57
|
+
const existingNonce =
|
|
58
|
+
doc.querySelector<HTMLScriptElement>("script[nonce]")?.nonce;
|
|
59
|
+
if (existingNonce) script.nonce = existingNonce;
|
|
60
|
+
script.onload = () => send({ type: "init" });
|
|
61
|
+
doc.head.append(script);
|
|
62
|
+
}
|
|
63
|
+
} catch {
|
|
64
|
+
/* Cross-origin preview uses its prepared adapter. */
|
|
65
|
+
}
|
|
66
|
+
let tries = 0;
|
|
67
|
+
send({ type: "init" });
|
|
68
|
+
timer.current = setInterval(() => {
|
|
69
|
+
send({ type: "init" });
|
|
70
|
+
if (++tries >= 20 && timer.current) clearInterval(timer.current);
|
|
71
|
+
}, 500);
|
|
72
|
+
}, [enabled, frame, send, url]);
|
|
73
|
+
useEffect(() => {
|
|
74
|
+
if (!enabled) return;
|
|
75
|
+
const receive = (e: MessageEvent) => {
|
|
76
|
+
if (
|
|
77
|
+
e.source !== frame.current?.contentWindow ||
|
|
78
|
+
e.origin !== origin ||
|
|
79
|
+
!e.data ||
|
|
80
|
+
typeof e.data !== "object"
|
|
81
|
+
)
|
|
82
|
+
return;
|
|
83
|
+
const data = e.data as Record<string, unknown>;
|
|
84
|
+
if (data.channel !== "design-labs" || data.session !== session.current)
|
|
85
|
+
return;
|
|
86
|
+
if (data.type === "ready") {
|
|
87
|
+
setReady(true);
|
|
88
|
+
if (timer.current) clearInterval(timer.current);
|
|
89
|
+
}
|
|
90
|
+
if (data.type === "location" && typeof data.pathname === "string")
|
|
91
|
+
setPathname(data.pathname);
|
|
92
|
+
if (data.type === "pins" && Array.isArray(data.items))
|
|
93
|
+
setPins(
|
|
94
|
+
data.items.filter(
|
|
95
|
+
(p): p is ResolvedPin =>
|
|
96
|
+
p && typeof p.id === "string" && typeof p.found === "boolean",
|
|
97
|
+
),
|
|
98
|
+
);
|
|
99
|
+
if (
|
|
100
|
+
data.type === "target" &&
|
|
101
|
+
data.target &&
|
|
102
|
+
typeof data.target === "object"
|
|
103
|
+
)
|
|
104
|
+
callback.current.onTarget(data.target as TargetContext);
|
|
105
|
+
if (data.type === "cancel") callback.current.onCancel();
|
|
106
|
+
};
|
|
107
|
+
window.addEventListener("message", receive);
|
|
108
|
+
connect();
|
|
109
|
+
return () => {
|
|
110
|
+
send({ type: "inspect", enabled: false });
|
|
111
|
+
send({ type: "pins", items: [] });
|
|
112
|
+
if (timer.current) clearInterval(timer.current);
|
|
113
|
+
window.removeEventListener("message", receive);
|
|
114
|
+
};
|
|
115
|
+
}, [connect, enabled, frame, origin, send]);
|
|
116
|
+
const resolve = useCallback(
|
|
117
|
+
(items: Annotation[]) =>
|
|
118
|
+
send({
|
|
119
|
+
type: "pins",
|
|
120
|
+
items: items.map((a) => ({
|
|
121
|
+
id: a.id,
|
|
122
|
+
selector: a.target.selector,
|
|
123
|
+
tag: a.target.tag,
|
|
124
|
+
text: a.target.text,
|
|
125
|
+
})),
|
|
126
|
+
}),
|
|
127
|
+
[send],
|
|
128
|
+
);
|
|
129
|
+
return { ready: enabled && ready, pathname, pins, send, connect, resolve };
|
|
130
|
+
}
|