@whzxc/agent-design-gallery 0.0.0-stage → 0.7.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/LICENSE +69 -0
- package/README.md +218 -2
- package/package.json +43 -4
- package/public/access.css +10 -0
- package/public/app.js +1674 -0
- package/public/arrangement-worker.js +11 -0
- package/public/arrangement.js +49 -0
- package/public/assets.js +356 -0
- package/public/auth.js +69 -0
- package/public/favicon.svg +6 -0
- package/public/hub.css +67 -0
- package/public/hub.html +33 -0
- package/public/hub.js +165 -0
- package/public/icons.js +50 -0
- package/public/index.html +70 -0
- package/public/instance-actions.js +27 -0
- package/public/labels.js +122 -0
- package/public/layout.js +119 -0
- package/public/relationships.js +28 -0
- package/public/selection.js +20 -0
- package/public/style.css +194 -0
- package/public/switcher.css +37 -0
- package/public/switcher.js +176 -0
- package/public/token-catalog.js +129 -0
- package/public/tokens.css +61 -0
- package/public/view-route.js +66 -0
- package/src/cli-output.mjs +236 -0
- package/src/cli.mjs +176 -0
- package/src/http.mjs +38 -0
- package/src/hub/auth.mjs +41 -0
- package/src/hub/instances.mjs +99 -0
- package/src/hub/proxy.mjs +30 -0
- package/src/hub/registry.mjs +181 -0
- package/src/hub.mjs +128 -0
- package/src/lifecycle.mjs +109 -0
- package/src/link.mjs +55 -0
- package/src/open.mjs +61 -0
- package/src/perf.mjs +12 -0
- package/src/server.mjs +679 -0
- package/src/service.mjs +184 -0
- package/src/toolkit/arch-browser.mjs +40 -0
- package/src/toolkit/arch-git.mjs +55 -0
- package/src/toolkit/arch-overlay.mjs +225 -0
- package/src/toolkit/arch.mjs +221 -0
- package/src/toolkit/asset-rules.mjs +14 -0
- package/src/toolkit/browser.mjs +33 -0
- package/src/toolkit/build-store.mjs +266 -0
- package/src/toolkit/build-worker.mjs +35 -0
- package/src/toolkit/build.mjs +147 -0
- package/src/toolkit/commands.mjs +345 -0
- package/src/toolkit/compare.mjs +222 -0
- package/src/toolkit/config.mjs +136 -0
- package/src/toolkit/document.mjs +85 -0
- package/src/toolkit/migrate.mjs +85 -0
- package/src/toolkit/project.mjs +172 -0
- package/src/toolkit/results.mjs +44 -0
- package/src/toolkit/runtime.d.ts +14 -0
- package/src/toolkit/runtime.mjs +54 -0
- package/src/toolkit/text.mjs +12 -0
- package/src/toolkit/tokens.mjs +37 -0
- package/src/viewer-cache.mjs +85 -0
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { layoutWorld, packingSnapshot, restorePacking } from './layout.js';
|
|
2
|
+
|
|
3
|
+
self.onmessage = ({data:{id,input,packing}}) => {
|
|
4
|
+
try {
|
|
5
|
+
restorePacking(packing);
|
|
6
|
+
const start=performance.now(),placed=layoutWorld(input);
|
|
7
|
+
self.postMessage({id,placed,packing:packingSnapshot(),durationMs:performance.now()-start});
|
|
8
|
+
} catch(error) {
|
|
9
|
+
self.postMessage({id,error:error.message});
|
|
10
|
+
}
|
|
11
|
+
};
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import { arrangementInput } from './assets.js';
|
|
2
|
+
import { packingSnapshot, restorePacking, layoutWorld } from './layout.js';
|
|
3
|
+
|
|
4
|
+
const aborted = () => new DOMException('Arrangement superseded', 'AbortError');
|
|
5
|
+
|
|
6
|
+
export function createArranger() {
|
|
7
|
+
let worker, nextId=0;
|
|
8
|
+
const pending=new Map();
|
|
9
|
+
function close(failure=aborted()) {
|
|
10
|
+
worker?.terminate();worker=undefined;
|
|
11
|
+
for(const task of pending.values())task.finish(failure);
|
|
12
|
+
pending.clear();
|
|
13
|
+
}
|
|
14
|
+
function start() {
|
|
15
|
+
if(worker)return;
|
|
16
|
+
worker=new Worker(new URL('./arrangement-worker.js',import.meta.url),{type:'module'});
|
|
17
|
+
worker.onmessage=({data})=>{
|
|
18
|
+
const task=pending.get(data.id);
|
|
19
|
+
if(!task)return;
|
|
20
|
+
if(data.error)task.finish(Error(`Cannot arrange designs: ${data.error}`));
|
|
21
|
+
else {restorePacking(data.packing);task.finish(null,data);}
|
|
22
|
+
};
|
|
23
|
+
worker.onerror=event=>close(Error(`Cannot start arrangement worker: ${event.message}`));
|
|
24
|
+
}
|
|
25
|
+
return {
|
|
26
|
+
close,
|
|
27
|
+
get pending() {return pending.size;},
|
|
28
|
+
arrange(data,{signal,onPending}={}) {
|
|
29
|
+
signal?.throwIfAborted();
|
|
30
|
+
const endProbe=globalThis.__adgTiming?.span('arrangement-cache-probe');
|
|
31
|
+
const input=arrangementInput(data),cached=layoutWorld(input,{cachedOnly:true});
|
|
32
|
+
endProbe?.({hit:!!cached});
|
|
33
|
+
if(cached)return Promise.resolve(cached);
|
|
34
|
+
onPending?.();
|
|
35
|
+
start();
|
|
36
|
+
const id=++nextId,end=globalThis.__adgTiming?.span('arrangement-wait');
|
|
37
|
+
return new Promise((resolve,reject)=>{
|
|
38
|
+
const abort=()=>close();
|
|
39
|
+
pending.set(id,{finish(failure,result){
|
|
40
|
+
pending.delete(id);signal?.removeEventListener('abort',abort);
|
|
41
|
+
end?.({workerMs:result?.durationMs});
|
|
42
|
+
failure?reject(failure):resolve(result.placed);
|
|
43
|
+
}});
|
|
44
|
+
signal?.addEventListener('abort',abort,{once:true});
|
|
45
|
+
worker.postMessage({id,input,packing:packingSnapshot()});
|
|
46
|
+
});
|
|
47
|
+
},
|
|
48
|
+
};
|
|
49
|
+
}
|
package/public/assets.js
ADDED
|
@@ -0,0 +1,356 @@
|
|
|
1
|
+
import { dataFetch } from './auth.js';
|
|
2
|
+
import { blockForAsset, componentWrapWidth, layoutWorld } from "./layout.js";
|
|
3
|
+
// Measure declared SVG content in an isolated, script-disabled document.
|
|
4
|
+
export const metadataContext = () => `adaptive-wrap-v1|${navigator.userAgent}|${devicePixelRatio}`;
|
|
5
|
+
export function measuredMetadata(asset) {
|
|
6
|
+
const {format,full,component,parts,background,imports,frameRanges}=asset;
|
|
7
|
+
return {format,full,component,parts,background,imports,frameRanges};
|
|
8
|
+
}
|
|
9
|
+
export function previewAddress(asset,theme,crop) {
|
|
10
|
+
const query=new URLSearchParams({theme,context:metadataContext(),crop});
|
|
11
|
+
return `preview/${asset.hash}?${query}`;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export async function prepare(asset, theme, {signal} = {}) {
|
|
15
|
+
const frameQuery = asset.onlyFrame && /\.(tsx|mmd)$/.test(asset.path) ? `&frame=${encodeURIComponent(asset.onlyFrame)}` : '';
|
|
16
|
+
const response = await dataFetch(
|
|
17
|
+
`asset/${encodeURIComponent(asset.path)}?theme=${theme}&v=${encodeURIComponent(asset.version)}${frameQuery}`,
|
|
18
|
+
{signal},
|
|
19
|
+
);
|
|
20
|
+
if(response.status===409)throw Object.assign(Error('Source rebuild superseded'),{name:'AbortError'});
|
|
21
|
+
if (!response.ok) {
|
|
22
|
+
const problem = frameQuery && (await response.json().catch(() => null))?.error;
|
|
23
|
+
throw new Error(problem || `Cannot load ${asset.path}`);
|
|
24
|
+
}
|
|
25
|
+
const source = await response.text();
|
|
26
|
+
if (/\.(tsx|mmd)$/.test(asset.path)) {
|
|
27
|
+
const built=JSON.parse(source);
|
|
28
|
+
return prepareSource({...asset,hash:built.hash??asset.hash,version:built.hash??asset.version},built,theme,signal);
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
const frame = document.createElement("iframe");
|
|
32
|
+
frame.setAttribute("sandbox", "allow-same-origin");
|
|
33
|
+
frame.style.cssText =
|
|
34
|
+
"position:fixed;left:-100000px;top:0;width:2000px;height:2000px;visibility:hidden";
|
|
35
|
+
try {
|
|
36
|
+
await new Promise((resolve, reject) => {
|
|
37
|
+
const timer = setTimeout(
|
|
38
|
+
() => reject(new Error(`SVG measurement timed out: ${asset.path}`)),
|
|
39
|
+
10000,
|
|
40
|
+
);
|
|
41
|
+
frame.onload = () => {
|
|
42
|
+
clearTimeout(timer);
|
|
43
|
+
resolve();
|
|
44
|
+
};
|
|
45
|
+
frame.onerror = () => {
|
|
46
|
+
clearTimeout(timer);
|
|
47
|
+
reject(new Error(`Cannot measure ${asset.path}`));
|
|
48
|
+
};
|
|
49
|
+
frame.srcdoc = `<!doctype html><html data-theme="${theme}"><body style="margin:0">${source}</body></html>`;
|
|
50
|
+
document.body.append(frame);
|
|
51
|
+
});
|
|
52
|
+
const root = frame.contentDocument.querySelector("svg");
|
|
53
|
+
if (!root) throw new Error(`Invalid SVG: ${asset.path}`);
|
|
54
|
+
const vb = root.viewBox.baseVal;
|
|
55
|
+
const full = {
|
|
56
|
+
x: vb.x,
|
|
57
|
+
y: vb.y,
|
|
58
|
+
width: vb.width || root.width.baseVal.value || 300,
|
|
59
|
+
height: vb.height || root.height.baseVal.value || 150,
|
|
60
|
+
};
|
|
61
|
+
const component = root.dataset.design === "component";
|
|
62
|
+
const selector = component
|
|
63
|
+
? "g[data-component][data-form]"
|
|
64
|
+
: 'g[data-part^="frame-"]';
|
|
65
|
+
const nodes = [...root.querySelectorAll(selector)].filter(
|
|
66
|
+
(e) => !e.parentElement.closest(selector),
|
|
67
|
+
);
|
|
68
|
+
const parts = nodes
|
|
69
|
+
.map((e, index) => {
|
|
70
|
+
const b = e.getBBox(),
|
|
71
|
+
m = root.getScreenCTM().inverse().multiply(e.getScreenCTM());
|
|
72
|
+
const points = [
|
|
73
|
+
[b.x, b.y],
|
|
74
|
+
[b.x + b.width, b.y],
|
|
75
|
+
[b.x, b.y + b.height],
|
|
76
|
+
[b.x + b.width, b.y + b.height],
|
|
77
|
+
].map(([x, y]) => new DOMPoint(x, y).matrixTransform(m));
|
|
78
|
+
const x = Math.min(...points.map((p) => p.x)),
|
|
79
|
+
y = Math.min(...points.map((p) => p.y));
|
|
80
|
+
return {
|
|
81
|
+
id: String(index),
|
|
82
|
+
name: e.dataset.form || e.dataset.part.replace(/^frame-/, ""),
|
|
83
|
+
x,
|
|
84
|
+
y,
|
|
85
|
+
width: Math.max(...points.map((p) => p.x)) - x,
|
|
86
|
+
height: Math.max(...points.map((p) => p.y)) - y,
|
|
87
|
+
};
|
|
88
|
+
})
|
|
89
|
+
.filter((p) => p.width > 0 && p.height > 0);
|
|
90
|
+
return {
|
|
91
|
+
...asset,
|
|
92
|
+
source,
|
|
93
|
+
full,
|
|
94
|
+
component,
|
|
95
|
+
parts,
|
|
96
|
+
background: frame.contentWindow
|
|
97
|
+
.getComputedStyle(root)
|
|
98
|
+
.getPropertyValue("--canvas")
|
|
99
|
+
.trim(),
|
|
100
|
+
};
|
|
101
|
+
} finally {
|
|
102
|
+
frame.remove();
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
// Retain one stylesheet in the bounded in-memory source cache. Individual
|
|
106
|
+
// crops restore that exact stylesheet before XML parsing, so their bytes match.
|
|
107
|
+
export function compactPreparedSource(asset) {
|
|
108
|
+
if(asset.format!=='tsx'||!asset.frameRanges?.length||asset.frameRanges.length<2)return asset;
|
|
109
|
+
const frames=asset.frameRanges.map(([start,end])=>asset.source.slice(start,end));
|
|
110
|
+
const sheets=frames.map(frame=>[...frame.matchAll(/<style>[\s\S]*?<\/style>/g)]);
|
|
111
|
+
if(sheets.some(styles=>styles.length!==1)||sheets.some(styles=>styles[0][0]!==sheets[0][0][0]))return asset;
|
|
112
|
+
const shared=sheets[0][0][0],comment='<style>/* shared stylesheet */</style>';
|
|
113
|
+
const pieces=[asset.source.slice(0,asset.frameRanges[0][0])],frameRanges=[];
|
|
114
|
+
let offset=pieces[0].length;
|
|
115
|
+
const sharedStyleRange=[offset+sheets[0][0].index,offset+sheets[0][0].index+shared.length];
|
|
116
|
+
for(const [index,frame] of frames.entries()) {
|
|
117
|
+
const content=index?frame.replace(shared,comment):frame;
|
|
118
|
+
frameRanges.push([offset,offset+content.length]);pieces.push(content);offset+=content.length;
|
|
119
|
+
}
|
|
120
|
+
pieces.push(asset.source.slice(asset.frameRanges.at(-1)[1]));
|
|
121
|
+
return {...asset,source:pieces.join(''),frameRanges,sharedStyleRange};
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
export function imageURL(asset, crop, onSource) {
|
|
125
|
+
const endPreview = globalThis.__adgTiming?.span('preview-generation', asset.path, {crop:crop?.id});
|
|
126
|
+
if (!crop)
|
|
127
|
+
return URL.createObjectURL(
|
|
128
|
+
new Blob([asset.source], { type: "image/svg+xml" }),
|
|
129
|
+
);
|
|
130
|
+
// TSX preparation records the exact character range of each top-level frame.
|
|
131
|
+
// Parse only the requested frame instead of reparsing all its siblings.
|
|
132
|
+
const range = typeof crop.id === 'string' && /^\d+$/.test(crop.id) && asset.frameRanges?.[crop.id];
|
|
133
|
+
let fragment=range?asset.source.slice(...range):null;
|
|
134
|
+
if(range&&asset.sharedStyleRange)
|
|
135
|
+
fragment=fragment.replace('<style>/* shared stylesheet */</style>',asset.source.slice(...asset.sharedStyleRange));
|
|
136
|
+
const source = range
|
|
137
|
+
? asset.source.slice(0, asset.frameRanges[0][0]) + fragment + '</svg>'
|
|
138
|
+
: asset.source;
|
|
139
|
+
const doc = new DOMParser().parseFromString(source, "image/svg+xml"),
|
|
140
|
+
root = doc.documentElement;
|
|
141
|
+
if (asset.format === 'tsx' && ['overview','full'].includes(crop.id)) {
|
|
142
|
+
const styles = [...root.querySelectorAll('style')];
|
|
143
|
+
// Prepared TSX sets repeat the same generated stylesheet in every frame.
|
|
144
|
+
// Keep the elements and nonempty text for structural selectors, but submit
|
|
145
|
+
// its CSS rules only once. Authored or differing styles keep their cascade.
|
|
146
|
+
if (styles.length > 1 && styles.length === asset.parts.length &&
|
|
147
|
+
styles.every(style => !style.attributes.length && style.textContent === styles[0].textContent)) {
|
|
148
|
+
for (const style of styles.slice(1)) style.textContent = '/* shared stylesheet */';
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
if (crop.id !== "full")
|
|
152
|
+
for (const note of root.querySelectorAll(":scope > text.note"))
|
|
153
|
+
note.remove();
|
|
154
|
+
if (!range && /^\d+$/.test(crop.id)) {
|
|
155
|
+
const selector = asset.component
|
|
156
|
+
? "g[data-component][data-form]"
|
|
157
|
+
: 'g[data-part^="frame-"]';
|
|
158
|
+
const nodes = [...root.querySelectorAll(selector)].filter(
|
|
159
|
+
(e) => !e.parentElement.closest(selector),
|
|
160
|
+
);
|
|
161
|
+
nodes.forEach((node, index) => {
|
|
162
|
+
if (String(index) !== crop.id) node.remove();
|
|
163
|
+
});
|
|
164
|
+
}
|
|
165
|
+
root.setAttribute(
|
|
166
|
+
"viewBox",
|
|
167
|
+
`${crop.x} ${crop.y} ${crop.width} ${crop.height}`,
|
|
168
|
+
);
|
|
169
|
+
root.setAttribute("width", crop.width);
|
|
170
|
+
root.setAttribute("height", crop.height);
|
|
171
|
+
const rendered=new XMLSerializer().serializeToString(doc);
|
|
172
|
+
onSource?.(rendered);
|
|
173
|
+
const url = URL.createObjectURL(new Blob([rendered], {type:"image/svg+xml"}));
|
|
174
|
+
endPreview?.();
|
|
175
|
+
return url;
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
// Keep current visible previews and a small LRU of offscreen previews. Identity
|
|
179
|
+
// includes theme and source version so visible paths cannot pin stale generations.
|
|
180
|
+
export function evictPreviews(cache, activeKeys) {
|
|
181
|
+
if (cache.size <= 100) return;
|
|
182
|
+
for (const entry of [...cache.values()].sort((a, b) => a.used - b.used)) {
|
|
183
|
+
if (cache.size <= 80) break;
|
|
184
|
+
if (!activeKeys.has(entry.key)) cache.delete(entry.key);
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
export function groupAssets(data) {
|
|
189
|
+
const byPath = new Map(data.assets.map(asset => [asset.path, asset]));
|
|
190
|
+
const used = new Set(),
|
|
191
|
+
grouped = [];
|
|
192
|
+
for (const g of data.groups) {
|
|
193
|
+
const list = g.assets.flatMap((p) => {
|
|
194
|
+
const a = byPath.get(p);
|
|
195
|
+
if (!a || used.has(p)) return [];
|
|
196
|
+
used.add(p);
|
|
197
|
+
return [a];
|
|
198
|
+
});
|
|
199
|
+
if (list.length) grouped.push({ ...g, assets: list });
|
|
200
|
+
}
|
|
201
|
+
const rest = new Map();
|
|
202
|
+
for (const a of data.assets) {
|
|
203
|
+
if (used.has(a.path)) continue;
|
|
204
|
+
const parent = a.path.split("/").slice(0, -1).join("/") || "Design";
|
|
205
|
+
if (!rest.has(parent)) rest.set(parent, []);
|
|
206
|
+
rest.get(parent).push(a);
|
|
207
|
+
}
|
|
208
|
+
for (const [title, assets] of rest) grouped.push({ title, assets });
|
|
209
|
+
return grouped;
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
export function scopeKind(scope) {
|
|
213
|
+
return scope?.split(':', 1)[0];
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
export function scopeTarget(scope) {
|
|
217
|
+
const separator = scope?.indexOf(':') ?? -1;
|
|
218
|
+
return separator < 0 ? undefined : scope.slice(separator + 1);
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
export function resolveScope(data, layers, requested) {
|
|
222
|
+
const layerOf = asset => asset.layer ?? (asset.path.includes('/') ? asset.path.split('/')[0] : 'Design');
|
|
223
|
+
const layerAssets = new Map(layers.map(layer => [layer, []]));
|
|
224
|
+
for (const asset of data.assets) {
|
|
225
|
+
const layer = layerOf(asset);
|
|
226
|
+
if (!layerAssets.has(layer)) layerAssets.set(layer, []);
|
|
227
|
+
layerAssets.get(layer).push(asset);
|
|
228
|
+
}
|
|
229
|
+
const available = [...layerAssets.keys()];
|
|
230
|
+
const result = (scope, assets) => ({ scope, assets, layers: available });
|
|
231
|
+
if (requested === 'all') return result('all', data.assets);
|
|
232
|
+
const key = scopeTarget(requested);
|
|
233
|
+
switch (scopeKind(requested)) {
|
|
234
|
+
case 'layer':
|
|
235
|
+
if (layerAssets.has(key)) return result(requested, layerAssets.get(key));
|
|
236
|
+
break;
|
|
237
|
+
case 'group': {
|
|
238
|
+
const group = groupAssets(data).find(item => item.title === key);
|
|
239
|
+
if (group) return result(requested, group.assets);
|
|
240
|
+
break;
|
|
241
|
+
}
|
|
242
|
+
case 'asset': {
|
|
243
|
+
const asset = data.assets.find(item => item.path === key);
|
|
244
|
+
if (asset) return result(requested, [asset]);
|
|
245
|
+
break;
|
|
246
|
+
}
|
|
247
|
+
}
|
|
248
|
+
const first = available.find(layer => layerAssets.get(layer).length) ?? available[0];
|
|
249
|
+
return first ? result(`layer:${first}`, layerAssets.get(first)) : result('all', data.assets);
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
|
|
253
|
+
export function arrangementInput(data) {
|
|
254
|
+
return groupAssets(data).map(group => ({blocks:group.assets.map(blockForAsset)}));
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
export function arrange(data, { provisional = false, placed } = {}) {
|
|
258
|
+
const grouped = groupAssets(data);
|
|
259
|
+
const sections = placed
|
|
260
|
+
? placed.map((section,index) => ({...grouped[index],...section}))
|
|
261
|
+
: layoutWorld(grouped.map(g => ({ ...g, blocks: g.assets.map(blockForAsset) })), {provisional});
|
|
262
|
+
const groups = sections.map(s => ({ ...s, width: s.w, height: s.h }));
|
|
263
|
+
const items = [];
|
|
264
|
+
for (const [group, section] of groups.entries()) {
|
|
265
|
+
for (const block of section.blocks) {
|
|
266
|
+
const asset = section.assets.find(a => a.path === block.asset);
|
|
267
|
+
const frames = block.kind === 'set' ? [{ id: 'overview', name: 'All forms', x: block.x, y: block.y, crop: block.crop }] : block.frames;
|
|
268
|
+
for (const frame of frames) {
|
|
269
|
+
const crop = { ...frame.crop, width: frame.crop.w, height: frame.crop.h, id: frame.id, name: frame.name };
|
|
270
|
+
items.push({ ...asset, id: block.kind === 'set' ? asset.path : `${asset.path}#${frame.id}`, kind: block.kind,
|
|
271
|
+
group, x: frame.x, y: frame.y, width: crop.width, height: crop.height, shown: [crop] });
|
|
272
|
+
}
|
|
273
|
+
}
|
|
274
|
+
}
|
|
275
|
+
return { groups, items, world: { x: 0, y: 0, width: Math.max(1, ...groups.map(g => g.x + g.width)), height: Math.max(1, ...groups.map(g => g.y + g.height)) } };
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
function formDocument(form,css,theme) {
|
|
279
|
+
if(form.document)return form.document;
|
|
280
|
+
const escape=value=>String(value).replace(/[&<>"']/g,c=>({'&':'&','<':'<','>':'>','"':'"',"'":'''}[c]));
|
|
281
|
+
return `<!doctype html><html data-theme="${theme}"><head><meta charset="utf-8"><style>${css.replace(/<\/style/gi,'<\\/style')}</style></head><body><div data-adg-frame="${escape(form.name)}" style="${form.width ? `width:${form.width}px;` : 'width:max-content;'}${form.height ? `height:${form.height}px;` : ''}">${form.html}</div></body></html>`;
|
|
282
|
+
}
|
|
283
|
+
async function prepareSource(asset, built, theme, signal) {
|
|
284
|
+
const endMeasurement = globalThis.__adgTiming?.span('measurement', asset.path, {frames:built.frames.length});
|
|
285
|
+
const parts = [];
|
|
286
|
+
const frame = document.createElement('iframe');
|
|
287
|
+
frame.setAttribute('sandbox','allow-same-origin');
|
|
288
|
+
frame.style.cssText='position:fixed;left:-100000px;top:0;visibility:hidden';
|
|
289
|
+
document.body.append(frame);
|
|
290
|
+
try {
|
|
291
|
+
let sliceStart=performance.now();
|
|
292
|
+
for (const [index, form] of built.frames.entries()) {
|
|
293
|
+
const endFrame = globalThis.__adgTiming?.span('measurement-frame', asset.path, {frame:form.name});
|
|
294
|
+
signal?.throwIfAborted();
|
|
295
|
+
if(performance.now()-sliceStart>8) {
|
|
296
|
+
await (globalThis.scheduler?.yield?.()??new Promise(resolve=>setTimeout(resolve,0)));
|
|
297
|
+
sliceStart=performance.now();
|
|
298
|
+
}
|
|
299
|
+
const documentSource=formDocument(form,built.css,theme);
|
|
300
|
+
frame.style.width=(form.width ?? 1600)+'px'; frame.style.height=(form.height ?? 1200)+'px';
|
|
301
|
+
if (index === 0) {
|
|
302
|
+
await new Promise(resolve=>{frame.onload=resolve;frame.srcdoc=documentSource;});
|
|
303
|
+
} else {
|
|
304
|
+
// Frames from one source share CSS. Keep its sandbox document and viewport,
|
|
305
|
+
// replacing only the specimen instead of navigating for every form.
|
|
306
|
+
const parsed = new DOMParser().parseFromString(documentSource, 'text/html');
|
|
307
|
+
frame.contentDocument.body.replaceChildren(frame.contentDocument.importNode(parsed.querySelector('[data-adg-frame]'), true));
|
|
308
|
+
await Promise.all([...frame.contentDocument.images].map(image => image.decode().catch(() => {})));
|
|
309
|
+
}
|
|
310
|
+
await frame.contentDocument.fonts.ready;
|
|
311
|
+
const shell=frame.contentDocument.querySelector('[data-adg-frame]');
|
|
312
|
+
const box=shell.getBoundingClientRect();
|
|
313
|
+
const styles=new Map(),rects=new Map();
|
|
314
|
+
const styleFor=node=>{if(!styles.has(node))styles.set(node,frame.contentWindow.getComputedStyle(node));return styles.get(node)};
|
|
315
|
+
const rectFor=node=>{if(!rects.has(node))rects.set(node,node.getBoundingClientRect());return rects.get(node)};
|
|
316
|
+
const elements = [...shell.querySelectorAll('[data-adg-owners]')].flatMap((node,index) => {
|
|
317
|
+
const rect = rectFor(node);
|
|
318
|
+
let left=Math.max(rect.left,box.left),top=Math.max(rect.top,box.top),right=Math.min(rect.right,box.right),bottom=Math.min(rect.bottom,box.bottom);
|
|
319
|
+
for (let parent=node.parentElement;parent && parent!==shell;parent=parent.parentElement) {
|
|
320
|
+
const style=styleFor(parent);
|
|
321
|
+
if (!/(hidden|clip|auto|scroll)/.test(style.overflowX+' '+style.overflowY)) continue;
|
|
322
|
+
const clip=rectFor(parent);
|
|
323
|
+
if (/(hidden|clip|auto|scroll)/.test(style.overflowX)) {left=Math.max(left,clip.left);right=Math.min(right,clip.right)}
|
|
324
|
+
if (/(hidden|clip|auto|scroll)/.test(style.overflowY)) {top=Math.max(top,clip.top);bottom=Math.min(bottom,clip.bottom)}
|
|
325
|
+
}
|
|
326
|
+
if (right<=left || bottom<=top || styleFor(node).visibility==='hidden') return [];
|
|
327
|
+
return [{id:String(index),owners:JSON.parse(node.dataset.adgOwners),x:left-box.x,y:top-box.y,width:right-left,height:bottom-top}];
|
|
328
|
+
});
|
|
329
|
+
const content=frame.contentDocument.createElement('div');
|
|
330
|
+
content.setAttribute('xmlns','http://www.w3.org/1999/xhtml');
|
|
331
|
+
content.setAttribute('style','font:16px/1.5 var(--font-ui);color:var(--text);');
|
|
332
|
+
content.append(frame.contentDocument.querySelector('style').cloneNode(true),shell.cloneNode(true));
|
|
333
|
+
parts.push({id:String(index),name:form.name,note:form.note,width:box.width,height:box.height,elements,markup:new XMLSerializer().serializeToString(content)});
|
|
334
|
+
endFrame?.();
|
|
335
|
+
}
|
|
336
|
+
const wrapWidth = built.component ? componentWrapWidth(parts) : Infinity;
|
|
337
|
+
let x=0,y=0,rowHeight=0;
|
|
338
|
+
for (const part of parts) {
|
|
339
|
+
if (x && x+part.width>wrapWidth) { x=0;y+=rowHeight+48;rowHeight=0; }
|
|
340
|
+
Object.assign(part,{x,y});
|
|
341
|
+
x+=part.width+32;rowHeight=Math.max(rowHeight,part.height);
|
|
342
|
+
}
|
|
343
|
+
const width=Math.max(1,...parts.map(p=>p.x+p.width)),height=Math.max(1,...parts.map(p=>p.y+p.height));
|
|
344
|
+
const xml = value => String(value).replace(/[&<>"']/g,c=>({'&':'&','<':'<','>':'>','"':'"',"'":'''}[c]));
|
|
345
|
+
const header=`<svg xmlns="http://www.w3.org/2000/svg" data-theme="${theme}" width="${width}" height="${height}" viewBox="0 0 ${width} ${height}">`;
|
|
346
|
+
let offset=header.length;
|
|
347
|
+
const frameRanges=[];
|
|
348
|
+
const markup=parts.map(p=>{
|
|
349
|
+
const value=`<g ${built.component ? 'data-component="source" data-form="'+xml(p.name)+'"' : 'data-part="frame-'+xml(p.name)+'"'}><foreignObject overflow="visible" x="${p.x}" y="${p.y}" width="${p.width}" height="${p.height}">${p.markup}</foreignObject></g>`;
|
|
350
|
+
frameRanges.push([offset,offset+value.length]);offset+=value.length;
|
|
351
|
+
return value;
|
|
352
|
+
});
|
|
353
|
+
const source=header+markup.join('')+'</svg>';
|
|
354
|
+
return {...asset,imports:built.imports,format:'tsx',source,frameRanges,full:{x:0,y:0,width,height},parts:parts.map(({markup,...p})=>p),component:built.component,background:frame.contentWindow.getComputedStyle(frame.contentDocument.documentElement).getPropertyValue('--canvas').trim()};
|
|
355
|
+
} finally { frame.remove(); endMeasurement?.(); }
|
|
356
|
+
}
|
package/public/auth.js
ADDED
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
import { icon } from './icons.js';
|
|
2
|
+
const storageKey = 'adg-key';
|
|
3
|
+
let unauthorized = false;
|
|
4
|
+
|
|
5
|
+
export function captureKey() {
|
|
6
|
+
if (typeof window === 'undefined') return;
|
|
7
|
+
const url = new URL(window.location.href);
|
|
8
|
+
if (!url.searchParams.has('key')) return;
|
|
9
|
+
const key = url.searchParams.get('key');
|
|
10
|
+
window.localStorage.setItem(storageKey, key);
|
|
11
|
+
url.searchParams.delete('key');
|
|
12
|
+
window.history.replaceState(window.history.state, '', url.pathname + url.search + url.hash);
|
|
13
|
+
}
|
|
14
|
+
export function isUnauthorized() { return unauthorized; }
|
|
15
|
+
export function showUnauthorized() {
|
|
16
|
+
if (unauthorized) return;
|
|
17
|
+
unauthorized = true;
|
|
18
|
+
const theme = window.localStorage.getItem('adg-theme');
|
|
19
|
+
document.documentElement.dataset.theme = theme === 'light' || theme === 'dark'
|
|
20
|
+
? theme : window.matchMedia('(prefers-color-scheme: dark)').matches ? 'dark' : 'light';
|
|
21
|
+
document.documentElement.dataset.adgUnauthorized = 'true';
|
|
22
|
+
const gallery = document.querySelector('.gallery');
|
|
23
|
+
if (gallery) gallery.hidden = true;
|
|
24
|
+
const hub = document.querySelector('#hub');
|
|
25
|
+
if (hub) hub.hidden = true;
|
|
26
|
+
for (const popover of document.querySelectorAll(':popover-open')) popover.hidePopover();
|
|
27
|
+
for (const dialog of document.querySelectorAll('dialog[open]')) dialog.close();
|
|
28
|
+
const shell = document.createElement('main');
|
|
29
|
+
shell.className = 'access-shell';
|
|
30
|
+
shell.setAttribute('aria-label', 'Hub access');
|
|
31
|
+
shell.innerHTML = `<div class="access-brand">ADG</div><div class="access-center"><section class="access-card" role="alert" aria-labelledby="access-title"><div class="access-code">${icon('key-round', 20)}<span>401</span></div><div class="access-copy"><h1 id="access-title">Key required</h1><p></p></div></section></div>`;
|
|
32
|
+
shell.querySelector('p').textContent = storedKey()
|
|
33
|
+
? 'The key saved in this browser was rejected.' : 'This browser needs an access key.';
|
|
34
|
+
document.body.append(shell);
|
|
35
|
+
window.dispatchEvent(new Event('adg-unauthorized'));
|
|
36
|
+
}
|
|
37
|
+
function storedKey() {
|
|
38
|
+
return typeof window === 'undefined' ? null : window.localStorage.getItem(storageKey);
|
|
39
|
+
}
|
|
40
|
+
function withBase(input) {
|
|
41
|
+
if(typeof window==='undefined')return input;
|
|
42
|
+
const base=new URL(window.location.href).searchParams.get('base');
|
|
43
|
+
if(base===null)return input;
|
|
44
|
+
const url=new URL(input,document.baseURI);
|
|
45
|
+
if(!url.searchParams.has('base'))url.searchParams.set('base',base);
|
|
46
|
+
return url.href;
|
|
47
|
+
}
|
|
48
|
+
export function keyURL(input) {
|
|
49
|
+
const url = new URL(withBase(input), document.baseURI), key = storedKey();
|
|
50
|
+
if (url.origin !== window.location.origin) throw new Error('ADG data requests must use the same origin.');
|
|
51
|
+
if (key) url.searchParams.set('key', key);
|
|
52
|
+
return url.href;
|
|
53
|
+
}
|
|
54
|
+
export async function dataFetch(input, init = {}) {
|
|
55
|
+
input=withBase(input);
|
|
56
|
+
if (typeof window !== 'undefined' && new URL(input, document.baseURI).origin !== window.location.origin)
|
|
57
|
+
throw new Error('ADG data requests must use the same origin.');
|
|
58
|
+
const headers = new Headers(init.headers), key = storedKey();
|
|
59
|
+
if (key) headers.set('Authorization', `Bearer ${key}`);
|
|
60
|
+
const response = await fetch(input, { ...init, headers });
|
|
61
|
+
if (response.status === 401) {
|
|
62
|
+
showUnauthorized();
|
|
63
|
+
const error = new Error('401 Unauthorized');
|
|
64
|
+
error.status = 401;
|
|
65
|
+
throw error;
|
|
66
|
+
}
|
|
67
|
+
return response;
|
|
68
|
+
}
|
|
69
|
+
captureKey();
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 32 32">
|
|
2
|
+
<rect width="32" height="32" rx="8" fill="#5743AE"/>
|
|
3
|
+
<path d="M24 8H12a4 4 0 0 0 0 8h8a4 4 0 0 1 0 8H8"
|
|
4
|
+
fill="none" stroke="#FAF9FF" stroke-width="4" stroke-linecap="round"/>
|
|
5
|
+
<rect x="21" y="5" width="6" height="6" rx="1" fill="#DDD3FF"/>
|
|
6
|
+
</svg>
|
package/public/hub.css
ADDED
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
:root { color-scheme:light; font:var(--text-body)/1.5 var(--font-ui); color:var(--text); background:var(--canvas); }
|
|
2
|
+
:root[data-theme="dark"] { color-scheme:dark; }
|
|
3
|
+
* { box-sizing:border-box; }
|
|
4
|
+
body { margin:0; }
|
|
5
|
+
[hidden] { display:none!important; }
|
|
6
|
+
h1,h2,p { margin:0; }
|
|
7
|
+
a { color:inherit; text-decoration:none; }
|
|
8
|
+
a:hover { text-decoration:underline; }
|
|
9
|
+
a:focus-visible,button:focus-visible { outline:1px solid var(--accent); outline-offset:2px; }
|
|
10
|
+
button { font:inherit; color:inherit; border:0; cursor:pointer; }
|
|
11
|
+
.icon { display:block; flex:none; pointer-events:none; }
|
|
12
|
+
[data-icon] { display:inline-flex; flex:none; }
|
|
13
|
+
#hub { min-height:100dvh; padding:12px; display:flex; flex-direction:column; gap:32px; }
|
|
14
|
+
.hub-bar { display:inline-flex; align-self:flex-start; align-items:center; height:44px; padding:6px; border-radius:var(--radius-panel); background:var(--surface); box-shadow:var(--shadow-float); }
|
|
15
|
+
.hub-bar span { padding:0 8px; font-size:var(--text-section); font-weight:650; }
|
|
16
|
+
#hub main { display:flex; flex-direction:column; gap:32px; width:100%; max-width:1080px; margin:0 auto; padding-top:24px; }
|
|
17
|
+
#hub main>section { display:flex; flex-direction:column; gap:16px; }
|
|
18
|
+
#hub main>.live-section { gap:12px; }
|
|
19
|
+
.hub-heading { display:flex; align-items:center; justify-content:space-between; gap:16px; }
|
|
20
|
+
h1 { font-size:var(--text-title); font-weight:650; }
|
|
21
|
+
h2 { font-size:var(--text-section); font-weight:600; }
|
|
22
|
+
#rescan { height:32px; display:inline-flex; align-items:center; gap:10px; padding:0 12px; flex:none; border-radius:var(--radius-field); background:var(--surface-sunken); font-weight:550; line-height:16px; }
|
|
23
|
+
#rescan .icon { color:var(--text-secondary); }
|
|
24
|
+
#rescan:disabled { opacity:.6; cursor:default; }
|
|
25
|
+
.hub-list { overflow:hidden; border-radius:var(--radius-panel); background:var(--surface); box-shadow:var(--shadow-float); }
|
|
26
|
+
.project-columns { display:grid; grid-template-columns:minmax(0,1fr) 144px 200px 148px; gap:24px; align-items:center; }
|
|
27
|
+
.project-header { padding:12px 20px; border-bottom:1px solid var(--border); font-size:var(--text-caption); color:var(--text-tertiary); }
|
|
28
|
+
.project-row { padding:20px; }
|
|
29
|
+
.project-row+.project-row,.instance-row+.instance-row { border-top:1px solid var(--border); }
|
|
30
|
+
.project-identity { min-width:0; }
|
|
31
|
+
.hub-name { display:flex; align-items:center; gap:8px; }
|
|
32
|
+
.hub-name>.icon { color:var(--text-secondary); }
|
|
33
|
+
.hub-name>.icon:last-child:not(:first-child) { color:var(--text-tertiary); }
|
|
34
|
+
.hub-name a,.hub-name>.hub-title { font-weight:600; }
|
|
35
|
+
.hub-path { margin-top:4px; overflow:hidden; text-overflow:ellipsis; white-space:nowrap; font-size:var(--text-meta); color:var(--text-secondary); }
|
|
36
|
+
.hub-change,.hub-git,.hub-counts { display:flex; align-items:center; gap:8px; min-width:0; font-size:var(--text-meta); color:var(--text-secondary); }
|
|
37
|
+
.hub-git>span:first-of-type { overflow:hidden; text-overflow:ellipsis; white-space:nowrap; }
|
|
38
|
+
.hub-git code { font:var(--text-caption)/1.5 var(--font-mono); color:var(--text-tertiary); flex:none; }
|
|
39
|
+
.hub-dirty { width:5px; height:5px; flex:none; border-radius:50%; background:var(--text-secondary); }
|
|
40
|
+
.phone-change { display:none; }
|
|
41
|
+
.hub-name .phone-change { font-weight:400; }
|
|
42
|
+
.hub-warning { display:flex; align-items:center; flex-wrap:wrap; gap:4px 8px; margin-top:12px; font-size:var(--text-meta); color:var(--danger); }
|
|
43
|
+
.hub-warning-detail { color:var(--text-secondary); }
|
|
44
|
+
.instance-row { padding:16px 20px; display:flex; align-items:center; justify-content:space-between; gap:24px; }
|
|
45
|
+
.instance-identity { display:flex; min-width:0; flex-direction:column; gap:4px; }
|
|
46
|
+
.instance-row .hub-name a,.instance-row .hub-name>span { overflow:hidden; text-overflow:ellipsis; white-space:nowrap; }
|
|
47
|
+
.instance-start { flex:none; color:var(--text-tertiary); }
|
|
48
|
+
.instance-warning { display:flex; align-items:center; flex:none; gap:8px; font-size:var(--text-meta); color:var(--danger); }
|
|
49
|
+
.instance-close { display:grid; place-items:center; flex:none; width:var(--control-size); height:var(--control-size); padding:0;
|
|
50
|
+
border-radius:var(--radius-row); background:none; color:var(--text-secondary); }
|
|
51
|
+
.instance-close:hover:not(:disabled) { background:var(--surface-sunken); }
|
|
52
|
+
.instance-close:disabled { opacity:.45; cursor:default; }
|
|
53
|
+
.hub-empty { display:flex; flex-direction:column; align-items:center; gap:12px; padding:48px 24px; text-align:center; }
|
|
54
|
+
.hub-empty>.icon { color:var(--text-tertiary); }
|
|
55
|
+
.hub-empty p { margin-top:4px; color:var(--text-secondary); }
|
|
56
|
+
#error { color:var(--danger); }
|
|
57
|
+
@media (max-width:700px) {
|
|
58
|
+
#hub main { padding-top:0; }
|
|
59
|
+
.project-header.project-columns,.desktop-change { display:none; }
|
|
60
|
+
.project-row { padding:16px; }
|
|
61
|
+
.project-columns { display:flex; flex-direction:column; align-items:stretch; gap:12px; }
|
|
62
|
+
.phone-change { display:flex; margin-left:auto; gap:4px; flex:none; font-size:var(--text-meta); color:var(--text-secondary); font-weight:400; align-items:center; }
|
|
63
|
+
.project-identity .hub-name>a,.project-identity .hub-name>.hub-title { min-width:0; overflow:hidden; text-overflow:ellipsis; white-space:nowrap; }
|
|
64
|
+
.project-identity .hub-name>.hub-external { display:none; }
|
|
65
|
+
.instance-row { padding:16px; flex-direction:column; align-items:stretch; gap:8px; }
|
|
66
|
+
.instance-row-closeable { flex-direction:row; align-items:center; gap:16px; }
|
|
67
|
+
}
|
package/public/hub.html
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
<!doctype html>
|
|
2
|
+
<html lang="en">
|
|
3
|
+
<head>
|
|
4
|
+
<meta charset="utf-8">
|
|
5
|
+
<meta name="viewport" content="width=device-width, initial-scale=1">
|
|
6
|
+
<meta name="referrer" content="same-origin">
|
|
7
|
+
<title>ADG</title>
|
|
8
|
+
<link rel="icon" type="image/svg+xml" href="/favicon.svg">
|
|
9
|
+
<link rel="stylesheet" href="tokens.css">
|
|
10
|
+
<link rel="stylesheet" href="hub.css">
|
|
11
|
+
<link rel="stylesheet" href="access.css">
|
|
12
|
+
<script type="module" src="hub.js"></script>
|
|
13
|
+
</head>
|
|
14
|
+
<body>
|
|
15
|
+
<div id="hub">
|
|
16
|
+
<header class="hub-bar"><span>ADG</span></header>
|
|
17
|
+
<main>
|
|
18
|
+
<section aria-labelledby="projects-title">
|
|
19
|
+
<div class="hub-heading">
|
|
20
|
+
<h1 id="projects-title">Projects</h1>
|
|
21
|
+
<button id="rescan" type="button"><span data-icon="refresh-cw" data-size="14"></span><span>Rescan</span></button>
|
|
22
|
+
</div>
|
|
23
|
+
<p id="error" role="alert" hidden></p>
|
|
24
|
+
<div id="projects"></div>
|
|
25
|
+
</section>
|
|
26
|
+
<section class="live-section" aria-labelledby="instances-title">
|
|
27
|
+
<h2 id="instances-title">Live instances</h2>
|
|
28
|
+
<div id="instances"></div>
|
|
29
|
+
</section>
|
|
30
|
+
</main>
|
|
31
|
+
</div>
|
|
32
|
+
</body>
|
|
33
|
+
</html>
|