Files
wibosim/server/public/js/theme.js
T
db9979andClaude Opus 5.5 9544fe6911 Client: Hell- und Dunkelmodus, Begründung unter Nachrichten, Probeauswertung im Admin
- Farbschema System/Hell/Dunkel (Knopf in der Kopfleiste und in den Einstellungen), ohne Aufblitzen beim Laden
- Alle Farben als Variablen, Diagramme lesen die Farben beim Zeichnen und folgen dem Schema
- Nachrichten: Themen mit Pfeil, Eilmeldung, Folgemeldung, KI-Analyse und „Warum?“ mit bewegten Werten
- Admin: Schlagzeile mit Regelwerk und Sprachmodell testen, Status der KI-Auswertung

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_018i4GUhQVoPUusUzxgxLWkK
2026-12-03 19:57:29 +00:00

70 lines
3.8 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// Farbschema: „Dunkel“, „Hell“ oder „System“ (folgt prefers-color-scheme).
// Die Wahl liegt in localStorage (wibo.theme); angewendet wird sie als data-theme="light"|"dark" auf <html>.
// Den ersten Stand setzt js/theme-init.js bereits im <head>. Bei jedem Wechsel feuert document das Ereignis
// „wibo:theme“ – die Canvas-Diagramme zeichnen sich daraufhin neu.
import { store, $$ } from './util.js';
export const THEMES = ['system', 'light', 'dark'];
const LABEL = { system: 'System', light: 'Hell', dark: 'Dunkel' };
// Symbole als SVG (◐ System, ☀ Hell, ☾ Dunkel) – Schriftzeichen werden je nach System-Schrift sehr unterschiedlich dargestellt
const svg = (body) => `<svg viewBox="0 0 24 24" width="20" height="20" aria-hidden="true" focusable="false">${body}</svg>`;
const ICON = {
system: svg('<circle cx="12" cy="12" r="8.5" fill="none" stroke="currentColor" stroke-width="2"/><path d="M12 3.5a8.5 8.5 0 0 0 0 17Z" fill="currentColor"/>'),
light: svg('<circle cx="12" cy="12" r="4.5" fill="currentColor"/><g stroke="currentColor" stroke-width="2" stroke-linecap="round"><path d="M12 2v2.5M12 19.5V22M2 12h2.5M19.5 12H22M4.9 4.9l1.8 1.8M17.3 17.3l1.8 1.8M4.9 19.1l1.8-1.8M17.3 6.7l1.8-1.8"/></g>'),
dark: svg('<path d="M20 14.5A8.5 8.5 0 0 1 9.5 4a8.5 8.5 0 1 0 10.5 10.5Z" fill="currentColor"/>'),
};
const mq = matchMedia('(prefers-color-scheme: light)');
/** gespeicherte Wahl: 'system' | 'light' | 'dark' */
export const themePref = () => { const v = store.get('theme', 'system'); return THEMES.includes(v) ? v : 'system'; };
/** tatsächlich angezeigtes Schema: 'light' | 'dark' */
export const resolvedTheme = (pref = themePref()) => (pref === 'system' ? (mq.matches ? 'light' : 'dark') : pref);
function apply() {
const pref = themePref();
const t = resolvedTheme(pref);
const root = document.documentElement;
const changedTheme = root.dataset.theme !== t;
root.dataset.theme = t;
const bg = getComputedStyle(root).getPropertyValue('--bg').trim();
const meta = document.querySelector('meta[name="theme-color"]');
if (meta && bg) meta.setAttribute('content', bg);
syncButtons(pref);
if (changedTheme) document.dispatchEvent(new CustomEvent('wibo:theme', { detail: { theme: t, pref } }));
}
/** Schaltflächen aktualisieren: Kopfzeilen-Umschalter (data-act="theme" ohne data-mode) und Auswahlgruppen (mit data-mode) */
export function syncButtons(pref = themePref()) {
for (const b of $$('[data-act="theme"]')) {
if (b.dataset.mode) { b.setAttribute('aria-pressed', String(b.dataset.mode === pref)); continue; }
const txt = `Farbschema: ${LABEL[pref]}`;
b.setAttribute('aria-label', `${txt} – zum Wechseln klicken`);
b.title = txt;
const ic = b.querySelector('.theme-ic') ?? b;
if (ic.dataset.pref !== pref) { ic.innerHTML = ICON[pref]; ic.dataset.pref = pref; }
}
}
/** Farbschema festlegen; ohne Argument zum nächsten wechseln (System → Hell → Dunkel → System) */
export function setTheme(pref) {
const next = THEMES.includes(pref) ? pref : THEMES[(THEMES.indexOf(themePref()) + 1) % THEMES.length];
if (next === 'system') store.del('theme'); else store.set('theme', next);
apply();
}
/** HTML für die Auswahl in den Einstellungen */
export const themeChooser = () => {
const pref = themePref();
return `<div class="seg w-auto" role="group" aria-label="Farbschema">${THEMES.map((m) => `<button type="button" data-act="theme" data-mode="${m}" aria-pressed="${m === pref}"><span class="theme-ic" aria-hidden="true">${ICON[m]}</span> ${LABEL[m]}</button>`).join('')}</div>`;
};
let inited = false;
export function initTheme() {
if (inited) return;
inited = true;
mq.addEventListener('change', () => { if (themePref() === 'system') apply(); });
// Wechsel in einem anderen Tab übernehmen
addEventListener('storage', (e) => { if (e.key === null || e.key === 'wibo.theme') apply(); });
apply();
}