Browser host, screenshots, docs, CI, README, LICENSE
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
e0bde1d150
commit
de8abfaa26
17 files changed
+1019
-13
No files matched your search
@@ -0,0 +1,30 @@
|
||||
#!/usr/bin/env node
|
||||
// Minimal static server for the browser host (serves host/, config/, build/ only).
|
||||
// node host/serve.mjs [port] (default: $PORT or 8080)
|
||||
import http from 'node:http';
|
||||
import { readFile } from 'node:fs/promises';
|
||||
import { join, extname, normalize } from 'node:path';
|
||||
import { fileURLToPath } from 'node:url';
|
||||
|
||||
const ROOT = join(fileURLToPath(import.meta.url), '..', '..');
|
||||
const MIME = { '.html': 'text/html', '.mjs': 'text/javascript', '.js': 'text/javascript', '.json': 'application/json', '.wasm': 'application/wasm' };
|
||||
const ALLOWED = ['host/', 'config/', 'build/'];
|
||||
|
||||
export function serve(port = 0) {
|
||||
const srv = http.createServer(async (req, res) => {
|
||||
try {
|
||||
let p = decodeURIComponent(new URL(req.url, 'http://x').pathname);
|
||||
if (p === '/') p = '/host/web/index.html';
|
||||
const rel = normalize(p).replace(/^\/+/, '');
|
||||
if (rel.includes('..') || !ALLOWED.some((a) => rel.startsWith(a)) || rel.startsWith('host/node_modules')) { res.writeHead(404).end('not found'); return; }
|
||||
const body = await readFile(join(ROOT, rel));
|
||||
res.writeHead(200, { 'content-type': MIME[extname(rel)] ?? 'application/octet-stream', 'cache-control': 'no-store' }).end(body);
|
||||
} catch { res.writeHead(404).end('not found'); }
|
||||
});
|
||||
return new Promise((resolve) => srv.listen(port, '127.0.0.1', () => resolve(srv)));
|
||||
}
|
||||
|
||||
if (process.argv[1] === fileURLToPath(import.meta.url)) {
|
||||
const srv = await serve(Number(process.argv[2] ?? process.env.PORT ?? 8080));
|
||||
console.log(`moon-plasma host: http://127.0.0.1:${srv.address().port}/`);
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
/** Loader for browsers: fetches the jco output under <base>/build/. */
|
||||
export function webLoader(base) {
|
||||
return async (name) => {
|
||||
const r = await fetch(`${base}/build/components/${name}.meta.json`);
|
||||
if (!r.ok) throw new Error(`cannot load ${name}.meta.json: HTTP ${r.status}`);
|
||||
const meta = await r.json();
|
||||
const dir = `${base}/build/jco/${name}`;
|
||||
const mod = await import(`${dir}/${name}.js`);
|
||||
const getCoreModule = async (p) => {
|
||||
const res = await fetch(`${dir}/${p}`);
|
||||
if (!res.ok) throw new Error(`cannot fetch ${p}: HTTP ${res.status}`);
|
||||
return WebAssembly.compile(await res.arrayBuffer());
|
||||
};
|
||||
return { meta, instantiate: (imports) => mod.instantiate(getCoreModule, imports) };
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
// Canvas2D backend: rasterises a render list (draw-ops in screen coordinates).
|
||||
import { colorCss } from '../src/scene.mjs';
|
||||
|
||||
function roundRectPath(ctx, r, radius) {
|
||||
ctx.beginPath();
|
||||
const rad = Math.min(radius, r.w / 2, r.h / 2);
|
||||
if (rad > 0) ctx.roundRect(r.x, r.y, r.w, r.h, rad); else ctx.rect(r.x, r.y, r.w, r.h);
|
||||
}
|
||||
|
||||
export function paint(ctx, ops, screen) {
|
||||
ctx.clearRect(0, 0, screen.w, screen.h);
|
||||
let depth = 0;
|
||||
for (const op of ops) {
|
||||
const v = op.val;
|
||||
switch (op.tag) {
|
||||
case 'push-clip':
|
||||
ctx.save(); depth++;
|
||||
ctx.beginPath(); ctx.rect(v.x, v.y, v.w, v.h); ctx.clip();
|
||||
break;
|
||||
case 'pop-clip':
|
||||
if (depth === 0) throw new Error('pop-clip without push-clip');
|
||||
ctx.restore(); depth--;
|
||||
break;
|
||||
case 'fill-rect':
|
||||
ctx.fillStyle = colorCss(v.color); roundRectPath(ctx, v.rect, v.radius); ctx.fill();
|
||||
break;
|
||||
case 'stroke-rect':
|
||||
ctx.strokeStyle = colorCss(v.color); ctx.lineWidth = v.width;
|
||||
roundRectPath(ctx, { x: v.rect.x + 0.5, y: v.rect.y + 0.5, w: v.rect.w - 1, h: v.rect.h - 1 }, v.radius); ctx.stroke();
|
||||
break;
|
||||
case 'line':
|
||||
ctx.strokeStyle = colorCss(v.color); ctx.lineWidth = v.width;
|
||||
ctx.beginPath(); ctx.moveTo(v.start.x + 0.5, v.start.y + 0.5); ctx.lineTo(v.end.x + 0.5, v.end.y + 0.5); ctx.stroke();
|
||||
break;
|
||||
case 'circle':
|
||||
ctx.beginPath(); ctx.arc(v.center.x, v.center.y, v.radius, 0, Math.PI * 2);
|
||||
if (v.filled) { ctx.fillStyle = colorCss(v.color); ctx.fill(); }
|
||||
else { ctx.strokeStyle = colorCss(v.color); ctx.lineWidth = 1.5; ctx.stroke(); }
|
||||
break;
|
||||
case 'text':
|
||||
ctx.font = `${v.style.bold ? '600 ' : ''}${v.style.size}px system-ui, "DejaVu Sans", sans-serif`;
|
||||
ctx.fillStyle = colorCss(v.style.color);
|
||||
ctx.textAlign = v.style.align; ctx.textBaseline = 'middle';
|
||||
ctx.fillText(v.text, v.origin.x, v.origin.y);
|
||||
break;
|
||||
default: throw new Error(`unknown draw-op ${op.tag}`);
|
||||
}
|
||||
}
|
||||
if (depth !== 0) throw new Error(`unbalanced clip stack (${depth})`);
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
<!doctype html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="utf-8">
|
||||
<title>moon-plasma</title>
|
||||
<style>
|
||||
html, body { margin: 0; background: #05070d; color: #aab; font: 12px/1.4 system-ui, sans-serif; }
|
||||
#screen { display: block; margin: 0 auto; max-width: 100vw; }
|
||||
#status { text-align: center; padding: 4px; font-family: monospace; }
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<canvas id="screen" width="1280" height="720"></canvas>
|
||||
<div id="status">loading components…</div>
|
||||
<script type="module" src="./main.mjs"></script>
|
||||
</body>
|
||||
</html>
|
||||
@@ -0,0 +1,46 @@
|
||||
import { Desktop } from '../src/desktop.mjs';
|
||||
import { createPlatform } from '../src/platform.mjs';
|
||||
import { tokensFromJson } from '../src/theme.mjs';
|
||||
import { webLoader } from '../src/loader-web.mjs';
|
||||
import { paint } from './canvas.mjs';
|
||||
|
||||
const base = new URL('../../', import.meta.url).href.replace(/\/$/, '');
|
||||
const q = new URLSearchParams(location.search);
|
||||
const getJson = async (p) => {
|
||||
const r = await fetch(`${base}/${p}`);
|
||||
if (!r.ok) throw new Error(`GET ${p}: HTTP ${r.status}`);
|
||||
return r.json();
|
||||
};
|
||||
|
||||
const status = document.getElementById('status');
|
||||
try {
|
||||
const config = await getJson(`config/${q.get('config') ?? 'desktop.json'}`);
|
||||
const apps = await getJson('config/apps.json');
|
||||
const tokens = tokensFromJson(await getJson(`config/themes/${q.get('theme') ?? config.theme}.json`));
|
||||
const fixed = q.get('time') ? new Date(q.get('time')) : null;
|
||||
const platform = createPlatform({ apps, tokens, now: fixed ? () => fixed : undefined });
|
||||
const desktop = await new Desktop({ config, loader: webLoader(base), platform }).start();
|
||||
|
||||
const canvas = document.getElementById('screen');
|
||||
canvas.width = desktop.screen.w; canvas.height = desktop.screen.h;
|
||||
const ctx = canvas.getContext('2d');
|
||||
const redraw = () => paint(ctx, desktop.frame().ops, desktop.screen);
|
||||
const pos = (e) => { const r = canvas.getBoundingClientRect(); return [Math.round((e.clientX - r.left) * canvas.width / r.width), Math.round((e.clientY - r.top) * canvas.height / r.height)]; };
|
||||
for (const [ev, kind] of [['pointermove', 'move'], ['pointerdown', 'down'], ['pointerup', 'up']]) {
|
||||
canvas.addEventListener(ev, (e) => { const [x, y] = pos(e); if (desktop.pointer(kind, x, y)) redraw(); });
|
||||
}
|
||||
window.addEventListener('keydown', (e) => {
|
||||
const k = e.key.length === 1 || ['Backspace', 'Enter', 'Escape'].includes(e.key) ? e.key : null;
|
||||
if (k && desktop.key(k)) { e.preventDefault(); redraw(); }
|
||||
});
|
||||
redraw();
|
||||
if (!fixed) setInterval(redraw, 1000);
|
||||
status.textContent = `${desktop.widgets.map((w) => `${w.id}=${w.componentName}`).join(' ')} | theme: ${platform.tokens.name}`;
|
||||
// Handle for automation / the browser console.
|
||||
window.moonPlasma = { desktop, platform, redraw, swap: async (id, c) => { await desktop.swap(id, c); redraw(); }, ready: true };
|
||||
} catch (e) {
|
||||
status.textContent = `ERROR: ${e.message}`;
|
||||
status.style.color = '#f66';
|
||||
window.moonPlasmaError = String(e.stack ?? e);
|
||||
throw e;
|
||||
}
|
||||
Reference in new issue
Block a user