Files
loki/widget/dom.ts
CatoandClaude Opus 5.5 a506d80d78 feat(brand): the rotorelief disc replaces the stroked spiral (#1472)
* feat(brand): the rotorelief disc replaces the stroked spiral

After Duchamp's discs in Anémic Cinéma (1926): the coil is cut out of a
disc, its inner turns drift off centre and thin toward the middle, so a
plain rotation reads as a tunnel breathing in and out.

- src/config/brand-mark.ts: markPathD() draws the whole mark as one
  evenodd path (disc, coil cut, pupil cut); MARK_TURNS gives optical
  sizes so the disc still reads at 16px
- LokiMark renders it everywhere in-app (header, consult avatar,
  feedback launcher, voice) in currentColor
- voice: the disc fills the orb; clockwise (pouring inward) while
  listening and thinking, reversed while speaking; loudness sets depth
- generate:brand now also writes the PWA install icons, replacing
  generate-app-icons.mjs; favicon, desktop, tray and widget regenerated

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_019MDtmreRmsJkmf3AcYy6DS

* chore(desktop): 0.8.60 for the new app and tray icon

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_019MDtmreRmsJkmf3AcYy6DS

---------

Co-authored-by: Claude <noreply@anthropic.com>
2026-10-30 09:53:59 +01:00

94 lines
4.0 KiB
TypeScript

import { MARK_D, MARK_VIEWBOX } from "./brand-mark.generated";
/**
* The widget's tiny DOM toolkit: one element helper, the inline icons, and the
* client-side image downscale. Zero dependencies — this bundle mounts on any
* stranger's site.
*/
export function h<K extends keyof HTMLElementTagNameMap>(
tag: K,
cls?: string,
text?: string,
): HTMLElementTagNameMap[K] {
const el = document.createElement(tag);
if (cls) el.className = cls;
if (text) el.textContent = text;
return el;
}
export const PENCIL_SVG =
'<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M17 3a2.85 2.83 0 1 1 4 4L7.5 20.5 2 22l1.5-5.5Z"/></svg>';
const MIC_SVG =
'<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><path d="M12 2a3 3 0 0 0-3 3v7a3 3 0 0 0 6 0V5a3 3 0 0 0-3-3Z"/><path d="M19 10v2a7 7 0 0 1-14 0v-2"/><path d="M12 19v3"/></svg>';
/** "Show Loki this": a target. */
export const TARGET_SVG =
'<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><circle cx="12" cy="12" r="9"/><circle cx="12" cy="12" r="4"/><path d="M12 1v3M12 20v3M1 12h3M20 12h3"/></svg>';
export const CAMERA_SVG =
'<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M14.5 4h-5L7 7H4a2 2 0 0 0-2 2v9a2 2 0 0 0 2 2h16a2 2 0 0 0 2-2V9a2 2 0 0 0-2-2h-3l-2.5-3z"/><circle cx="12" cy="13" r="3"/></svg>';
/**
* Loki's mark — the same disc as the logo (generated from the brand SSOT) —
* used as the widget's status light instead of a generic dot. The CSS turns it
* slowly while Loki watches, fast while it thinks, and stops it when paused,
* so the one shape that says "Loki" also says what Loki is doing.
*/
export function spiralMark(cls = ""): HTMLSpanElement {
const span = h("span", `dot spiral${cls ? ` ${cls}` : ""}`);
span.setAttribute("aria-hidden", "true");
span.innerHTML = `<svg viewBox="0 0 ${MARK_VIEWBOX} ${MARK_VIEWBOX}"><path d="${MARK_D}" fill="currentColor" fill-rule="evenodd"/></svg>`;
return span;
}
/** A fresh <span> each call — one node cannot sit in two places, and the label
* is rebuilt on every state change. */
export function micIcon(): HTMLSpanElement {
const span = h("span");
span.innerHTML = MIC_SVG;
return span;
}
/** Client-side downscale so a phone photo never ships megabytes: longest edge
* ≤1280px, JPEG, quality stepped down until it fits the ingest cap. */
const MAX_SHOT_CHARS = 590_000; // ingest caps the data URL at 600k chars
export function downscaleImage(file: Blob): Promise<string | null> {
return new Promise((resolve) => {
const url = URL.createObjectURL(file);
const img = new Image();
img.onload = () => {
URL.revokeObjectURL(url);
const scale = Math.min(1, 1280 / Math.max(img.width, img.height));
const canvas = document.createElement("canvas");
canvas.width = Math.max(1, Math.round(img.width * scale));
canvas.height = Math.max(1, Math.round(img.height * scale));
const ctx = canvas.getContext("2d");
if (!ctx) return resolve(null);
ctx.drawImage(img, 0, 0, canvas.width, canvas.height);
for (const quality of [0.8, 0.6, 0.4]) {
const out = canvas.toDataURL("image/jpeg", quality);
if (out.length <= MAX_SHOT_CHARS) return resolve(out);
}
resolve(null);
};
img.onerror = () => {
URL.revokeObjectURL(url);
resolve(null);
};
img.src = url;
});
}
/**
* On a phone the keyboard covers the bottom of the sheet, and the box being
* typed into sat half behind it (2026-10-09). Once the keyboard has settled,
* bring the box back into view.
*/
export function keepInViewOnFocus(el: HTMLElement, settleMs = 350): void {
el.addEventListener("focus", () => {
window.setTimeout(() => el.scrollIntoView({ block: "nearest" }), settleMs);
});
}