47 lines
2.4 KiB
JavaScript
47 lines
2.4 KiB
JavaScript
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;
|
|
}
|