Update the browser, editor, settings, and installer

This commit is contained in:
Knotty Wyvern
2026-10-12 14:46:21 -07:00
parent 909894b9c8
commit f6eaa52949
163 changed files with 146260 additions and 534 deletions
+75 -5
View File
@@ -1,12 +1,26 @@
import { customThemes } from './customThemes.ts';
import { customThemes, type CustomTheme } from './customThemes.ts';
import { readBackgroundImage } from './backgroundImages.ts';
export const APPEARANCE_KEY = 'runestone.appearance';
export const THEMES = ['default', 'claude', 'sakura'] as const;
export type ThemeName = typeof THEMES[number] | `custom:${string}`;
export type Appearance = { theme: ThemeName; mode: 'light' | 'dark' };
export const BACKGROUND_POSITIONS = ['center', 'top left', 'top', 'top right', 'left', 'right', 'bottom left', 'bottom', 'bottom right'] as const;
export const BACKGROUND_SIZES = ['cover', 'contain', 'auto'] as const;
export type Appearance = { theme: ThemeName; mode: 'light' | 'dark'; newTabBackground?: { image?: string; imageId?: string; overlay?: number; position?: typeof BACKGROUND_POSITIONS[number]; size?: typeof BACKGROUND_SIZES[number] } };
export const DEFAULT_APPEARANCE: Appearance = { theme: 'default', mode: 'dark' };
export function normalizeAppearance(value: { theme?: unknown; mode?: unknown } | null): Appearance {
export function normalizeAppearance(value: { theme?: unknown; mode?: unknown; newTabBackground?: unknown } | null): Appearance {
const theme: ThemeName = typeof value?.theme === 'string' && (/^custom:[a-z][a-z0-9-]{0,63}$/.test(value.theme) || (THEMES as readonly string[]).includes(value.theme)) ? value.theme as ThemeName : 'default';
return { theme, mode: theme === 'sakura' || value?.mode === 'light' ? 'light' : 'dark' };
const result: Appearance = { theme, mode: theme === 'sakura' || value?.mode === 'light' ? 'light' : 'dark' };
const background = value?.newTabBackground;
if (background && typeof background === 'object') {
const source = background as { image?: unknown; imageId?: unknown; overlay?: unknown; position?: unknown; size?: unknown };
const image = typeof source.image === 'string' && source.image.length <= 3_000_000 && /^data:image\/(?:png|jpeg|webp|gif);base64,[a-z0-9+/=]+$/i.test(source.image) ? source.image : undefined;
const imageId = typeof source.imageId === 'string' && /^[0-9a-f-]{36}$/i.test(source.imageId) ? source.imageId : undefined;
const overlay = typeof source.overlay === 'number' && Number.isFinite(source.overlay) ? Math.max(-1, Math.min(1, source.overlay)) : 0;
const position = BACKGROUND_POSITIONS.includes(source.position as typeof BACKGROUND_POSITIONS[number]) ? source.position as typeof BACKGROUND_POSITIONS[number] : 'center';
const size = BACKGROUND_SIZES.includes(source.size as typeof BACKGROUND_SIZES[number]) ? source.size as typeof BACKGROUND_SIZES[number] : 'cover';
if (image || imageId) result.newTabBackground = { ...(image ? { image } : {}), ...(imageId ? { imageId } : {}), overlay, position, size };
}
return result;
}
export function parseAppearance(raw: string | null): Appearance {
try { return normalizeAppearance(JSON.parse(raw || 'null')); }
@@ -28,8 +42,64 @@ const palettes = {
light: ['#fefcfc','#fefcfc','#dbeacb','#f8dbe6','#687788','#513346','#321c2a','#98556c','#ffc9dc'],
},
};
export function exportAppearanceTheme(theme: ThemeName): CustomTheme {
const custom = customThemes().find(item => `custom:${item.id}` === theme);
if (custom) return custom;
const palette = theme === 'sakura' ? { light: palettes.sakura.light, dark: palettes.sakura.light }
: palettes[theme === 'claude' ? 'claude' : 'default'];
return { schema: 1, id: 'my-theme', name: 'My theme',
light: Object.fromEntries(keys.map((key, index) => [key, palette.light[index]])),
dark: Object.fromEntries(keys.map((key, index) => [key, palette.dark[index]])) };
}
const backgroundUrls = new WeakMap<HTMLElement, { image: string; url: string }>();
const backgroundRequests = new WeakMap<HTMLElement, string | undefined>();
async function applySavedBackground(root: HTMLElement, background?: Appearance['newTabBackground']) {
const id = background?.imageId;
backgroundRequests.set(root, id);
if (!id) { applyBackgroundImage(root, background?.image); return; }
const previous = backgroundUrls.get(root);
if (previous?.image === id) { root.style.setProperty('--rs-new-tab-image', `url("${previous.url}")`); return; }
applyBackgroundImage(root);
try {
const file = await readBackgroundImage(id);
if (!file || backgroundRequests.get(root) !== id) return;
const url = URL.createObjectURL(file);
const old = backgroundUrls.get(root);
backgroundUrls.set(root, { image: id, url });
root.style.setProperty('--rs-new-tab-image', `url("${url}")`);
if (old) URL.revokeObjectURL(old.url);
} catch { /* Keep the theme usable if image storage is unavailable. */ }
}
function applyBackgroundImage(root: HTMLElement, image?: string) {
const previous = backgroundUrls.get(root);
if (image && previous?.image === image) {
root.style.setProperty('--rs-new-tab-image', `url("${previous.url}")`);
return;
}
let url: string | undefined;
if (image) {
try {
const [header, data] = image.split(',');
const bytes = Uint8Array.from(atob(data), character => character.charCodeAt(0));
url = URL.createObjectURL(new Blob([bytes], { type: header.slice(5, header.indexOf(';')) }));
} catch { /* Invalid saved image data must not stop the theme update. */ }
}
// Large data URLs exceed Chromium's CSS variable limit. Keep only a short blob URL in CSS.
root.style.setProperty('--rs-new-tab-image', url ? `url("${url}")` : 'none');
if (image && url) backgroundUrls.set(root, { image, url });
else backgroundUrls.delete(root);
if (previous) URL.revokeObjectURL(previous.url);
}
export function applyAppearance(value: Appearance, root = document.documentElement) {
const { theme, mode } = normalizeAppearance(value);
const { theme, mode, newTabBackground } = normalizeAppearance(value);
root.style.setProperty('--rs-new-tab-bg', 'var(--rs-bg)');
const overlay = newTabBackground?.overlay ?? 0;
const shade = overlay < 0 ? '255,255,255' : '0,0,0';
root.style.setProperty('--rs-background-overlay', `linear-gradient(rgba(${shade},${Math.abs(overlay)}),rgba(${shade},${Math.abs(overlay)}))`);
root.style.setProperty('--rs-background-image', newTabBackground ? 'var(--rs-background-overlay), var(--rs-new-tab-image, none)' : 'none');
root.style.setProperty('--rs-background-position', newTabBackground?.position ?? 'center');
root.style.setProperty('--rs-background-size', newTabBackground?.size ?? 'cover');
void applySavedBackground(root, newTabBackground);
root.dataset.theme = mode;
root.dataset.palette = theme;
root.style.colorScheme = mode;