Add interface languages and improve themes and workspace controls for 0.1.36
This commit is contained in:
+62
-19
@@ -1,4 +1,4 @@
|
||||
import { customThemes, type CustomTheme } from './customThemes.ts';
|
||||
import { customThemes, FONT_ROLES, type ThemeFonts, type ThemeColors, type CustomTheme } from './customThemes.ts';
|
||||
import { readBackgroundImage } from './backgroundImages.ts';
|
||||
import { characterTheme, characterSyntax, SYNTAX_ROLES } from './characterThemes.ts';
|
||||
export const APPEARANCE_KEY = 'runestone.appearance';
|
||||
@@ -11,17 +11,22 @@ export const BUILTIN_THEMES = [
|
||||
const LEGACY_THEME_IDS: Record<string, typeof THEMES[number]> = { holo: 'harvest-moon', asuka: 'crimson-pilot', malfoid: 'silver-serpent' };
|
||||
export function themeModes(theme: string): readonly ('light' | 'dark')[] {
|
||||
const character = characterTheme(theme);
|
||||
return character ? [character.mode] : theme === 'sakura' ? ['light'] : ['light', 'dark'];
|
||||
return theme.startsWith('custom:') ? [customThemeMode(customThemes().find(item => `custom:${item.id}` === theme)?.colors)] : character ? [character.mode] : theme === 'sakura' ? ['light'] : ['light', 'dark'];
|
||||
}
|
||||
export type ThemeName = typeof THEMES[number] | `custom:${string}`;
|
||||
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 type Appearance = { transparency?: { enabled: boolean; amount: number }; 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; newTabBackground?: unknown } | null): Appearance {
|
||||
export function normalizeAppearance(value: { theme?: unknown; mode?: unknown; newTabBackground?: unknown; transparency?: unknown } | null): Appearance {
|
||||
const savedTheme = typeof value?.theme === 'string' && Object.hasOwn(LEGACY_THEME_IDS, value.theme) ? LEGACY_THEME_IDS[value.theme] : value?.theme;
|
||||
const theme: ThemeName = typeof savedTheme === 'string' && (/^custom:[a-z][a-z0-9-]{0,63}$/.test(savedTheme) || (THEMES as readonly string[]).includes(savedTheme)) ? savedTheme as ThemeName : 'default';
|
||||
const result: Appearance = { theme, mode: characterTheme(theme)?.mode ?? (theme === 'sakura' || value?.mode === 'light' ? 'light' : 'dark') };
|
||||
const selectedCustom = theme.startsWith('custom:') ? customThemes(value?.mode === 'light' ? 'light' : 'dark').find(item => `custom:${item.id}` === theme) : undefined;
|
||||
const result: Appearance = { theme, mode: selectedCustom ? customThemeMode(selectedCustom.colors) : characterTheme(theme)?.mode ?? (theme === 'sakura' || value?.mode === 'light' ? 'light' : 'dark') };
|
||||
if (value?.transparency && typeof value.transparency === 'object') {
|
||||
const source = value.transparency as { enabled?: unknown; amount?: unknown };
|
||||
result.transparency = { enabled: source.enabled !== false, amount: typeof source.amount === 'number' && Number.isFinite(source.amount) ? Math.max(0, Math.min(100, Math.round(source.amount))) : 18 };
|
||||
}
|
||||
const background = value?.newTabBackground;
|
||||
if (background && typeof background === 'object') {
|
||||
const source = background as { image?: unknown; imageId?: unknown; overlay?: unknown; position?: unknown; size?: unknown };
|
||||
@@ -61,23 +66,56 @@ function builtinPalette(theme: ThemeName) {
|
||||
if (character) return { light: character.surfaces, dark: character.surfaces };
|
||||
return palettes[theme === 'claude' ? 'claude' : 'default'];
|
||||
}
|
||||
export function exportAppearanceTheme(theme: ThemeName): CustomTheme {
|
||||
const custom = customThemes().find(item => `custom:${item.id}` === theme);
|
||||
if (custom) return custom;
|
||||
const palette = builtinPalette(theme);
|
||||
const syntax = characterSyntax(theme);
|
||||
const extra = characterTheme(theme) ? { ...syntax, 'accent-2': characterTheme(theme)!.secondary } : {};
|
||||
return { schema: 1, id: 'my-theme', name: 'My theme',
|
||||
light: { ...Object.fromEntries(keys.map((key, index) => [key, palette.light[index]])), ...extra },
|
||||
dark: { ...Object.fromEntries(keys.map((key, index) => [key, palette.dark[index]])), ...extra } };
|
||||
export function customThemeMode(colors?: ThemeColors): 'light' | 'dark' {
|
||||
const hex = colors?.bg?.slice(1) ?? '141414';
|
||||
const full = hex.length <= 4 ? [...hex.slice(0, 3)].map(value => value + value).join('') : hex.slice(0, 6);
|
||||
const rgb = [0, 2, 4].map(index => parseInt(full.slice(index, index + 2), 16) / 255)
|
||||
.map(value => value <= .04045 ? value / 12.92 : ((value + .055) / 1.055) ** 2.4);
|
||||
return rgb[0] * .2126 + rgb[1] * .7152 + rgb[2] * .0722 > .179 ? 'light' : 'dark';
|
||||
}
|
||||
export function exportAppearanceTheme(theme: ThemeName, mode: 'light' | 'dark' = 'dark'): CustomTheme {
|
||||
const custom = customThemes(mode).find(item => `custom:${item.id}` === theme);
|
||||
if (custom) mode = customThemeMode(custom.colors);
|
||||
mode = characterTheme(theme)?.mode ?? (theme === 'sakura' ? 'light' : mode);
|
||||
const palette = builtinPalette(theme)[mode];
|
||||
const colors: ThemeColors = Object.fromEntries(keys.map((key, index) => [key, palette[index]]));
|
||||
colors['accent-2'] = characterTheme(theme)?.secondary ?? (theme === 'claude' ? '#6a9bcc' : theme === 'sakura' ? '#607fa9' : colors.accent);
|
||||
const semantic = mode === 'light' ? ['#b42323', '#8a5700', '#806600', '#287b29', '#7740a5'] : ['#f44747', '#d68a00', '#cca700', '#89d185', '#c792ea'];
|
||||
if (theme === 'claude') { semantic[3] = '#788c5d'; semantic[4] = '#6a9bcc'; }
|
||||
if (theme === 'sakura') { semantic[3] = '#618c71'; semantic[4] = '#cb6b91'; }
|
||||
(['error', 'warn-hot', 'warn', 'ok', 'special'] as const).forEach((key, index) => colors[key] = semantic[index]);
|
||||
const syntax = theme === 'sakura' ? ['#cb6b91', '#98556c', '#618c71', '#69a2bd', '#607fa9', '#687788'] : [colors.special, colors.accent, colors.text, colors['accent-2'], colors['accent-2'], colors['text-dim']];
|
||||
(['parameter', 'keyword', 'local', 'static', 'instance', 'global'] as const).forEach((key, index) => colors[`syntax-${key}`] = syntax[index]);
|
||||
Object.assign(colors, { 'syntax-string': colors.ok, 'syntax-number': colors.special, 'syntax-comment': colors['text-dim'], 'syntax-type': colors['accent-2'], 'syntax-function': colors['accent-2'] }, characterSyntax(theme));
|
||||
return { schema: 2, id: custom?.id ?? 'my-theme', name: custom?.name ?? 'My theme', colors: { ...colors, ...custom?.colors }, fonts: { ...themeFonts(theme), ...custom?.fonts } };
|
||||
}
|
||||
export function themeFonts(theme: ThemeName): Required<ThemeFonts> {
|
||||
const ui = '"Segoe UI Variable Text", "Segoe UI", ui-sans-serif, system-ui, sans-serif';
|
||||
const prose = '"Segoe UI Variable Display", "Segoe UI", "Helvetica Neue", ui-sans-serif, system-ui, sans-serif';
|
||||
const mono = '"Cascadia Mono", "Cascadia Code", "JetBrains Mono", "Fira Code", Consolas, ui-monospace, monospace';
|
||||
const fonts = { ui, prose, heading: prose, mono };
|
||||
if (theme === 'claude') return { ...fonts, ui: '"Lora", Georgia, serif', prose: '"Lora", Georgia, serif', heading: '"Poppins", Arial, sans-serif' };
|
||||
if (theme === 'harvest-moon') return { ...fonts, ui: 'Candara, "Trebuchet MS", "Segoe UI", sans-serif', prose: 'Constantia, Georgia, "Times New Roman", serif', heading: 'Constantia, Georgia, "Times New Roman", serif' };
|
||||
if (theme === 'crimson-pilot') return { ...fonts, ui: 'Bahnschrift, "Trebuchet MS", Arial, sans-serif', prose: '"Segoe UI", "Helvetica Neue", Arial, sans-serif', heading: 'Bahnschrift, "Trebuchet MS", Arial, sans-serif' };
|
||||
if (theme === 'silver-serpent') return { ...fonts, ui: 'Corbel, "Segoe UI", "Helvetica Neue", Arial, sans-serif', prose: '"Palatino Linotype", "Book Antiqua", Palatino, Georgia, serif', heading: '"Palatino Linotype", "Book Antiqua", Palatino, Georgia, serif' };
|
||||
return fonts;
|
||||
}
|
||||
export function applyTransparency(value: Appearance['transparency'], root: HTMLElement) {
|
||||
const supplied = value?.amount ?? 18;
|
||||
const amount = value?.enabled === false ? 0 : Number.isFinite(supplied) ? Math.max(0, Math.min(100, supplied)) : 18;
|
||||
root.dataset.transparency = amount ? 'on' : 'off';
|
||||
root.style.setProperty('--rs-surface-opacity', `${100 - amount}%`);
|
||||
root.style.setProperty('--rs-editor-surface-opacity', `${100 - amount * 10 / 18}%`);
|
||||
root.style.setProperty('--rs-hover-surface-opacity', `${100 - amount * 8 / 18}%`);
|
||||
root.style.setProperty('--rs-transparent-plugin-bg', amount ? 'transparent' : 'var(--rs-bg)');
|
||||
}
|
||||
const backgroundUrls = new WeakMap<HTMLElement, { image: string; url: string }>();
|
||||
const backgroundRequests = new WeakMap<HTMLElement, string | undefined>();
|
||||
function applyBackgroundSurfaces(root: HTMLElement, hasImage: boolean) {
|
||||
root.style.setProperty('--rs-workarea-sidebar-bg', hasImage ? 'var(--rs-image-sidebar-bg)' : 'var(--rs-bg-elevated)');
|
||||
root.style.setProperty('--rs-workarea-editor-bg', hasImage ? 'var(--rs-image-editor-bg)' : 'var(--rs-bg)');
|
||||
root.style.setProperty('--rs-workarea-surface-opacity', hasImage ? '82%' : '100%');
|
||||
root.style.setProperty('--rs-workarea-plugin-bg', hasImage ? 'transparent' : 'var(--rs-bg)');
|
||||
root.style.setProperty('--rs-workarea-surface-opacity', hasImage ? 'var(--rs-surface-opacity)' : '100%');
|
||||
root.style.setProperty('--rs-workarea-plugin-bg', hasImage ? 'var(--rs-transparent-plugin-bg)' : 'var(--rs-bg)');
|
||||
}
|
||||
async function applySavedBackground(root: HTMLElement, background?: Appearance['newTabBackground']) {
|
||||
const id = background?.imageId;
|
||||
@@ -120,7 +158,11 @@ function applyBackgroundImage(root: HTMLElement, image?: string) {
|
||||
if (previous) URL.revokeObjectURL(previous.url);
|
||||
}
|
||||
export function applyAppearance(value: Appearance, root = document.documentElement) {
|
||||
const { theme, mode, newTabBackground } = normalizeAppearance(value);
|
||||
const normalized = normalizeAppearance(value);
|
||||
const { theme, newTabBackground } = normalized;
|
||||
const custom = theme.startsWith('custom:') ? customThemes(normalized.mode).find(item => item.id === theme.slice(7)) : undefined;
|
||||
const mode = custom ? customThemeMode(custom.colors) : normalized.mode;
|
||||
applyTransparency(normalized.transparency, root);
|
||||
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';
|
||||
@@ -132,6 +174,8 @@ export function applyAppearance(value: Appearance, root = document.documentEleme
|
||||
root.dataset.theme = mode;
|
||||
root.dataset.palette = theme;
|
||||
root.style.colorScheme = mode;
|
||||
const fonts = { ...themeFonts(theme), ...custom?.fonts };
|
||||
FONT_ROLES.forEach(role => root.style.setProperty(`--rs-font-${role}`, fonts[role]));
|
||||
const palette = builtinPalette(theme)[mode];
|
||||
keys.forEach((key, index) => root.style.setProperty(`--rs-${key}`, palette[index]));
|
||||
root.style.setProperty('--rs-accent-2', characterTheme(theme)?.secondary ?? (theme === 'claude' ? '#6a9bcc' : theme === 'sakura' ? '#607fa9' : 'var(--rs-accent)'));
|
||||
@@ -149,8 +193,7 @@ export function applyAppearance(value: Appearance, root = document.documentEleme
|
||||
['parameter', 'keyword', 'local', 'static', 'instance', 'global'].forEach((key, index) => root.style.setProperty(`--rs-syntax-${key}`, syntax[index]));
|
||||
for (const [key, color] of Object.entries(characterSyntax(theme))) root.style.setProperty(`--rs-${key}`, color);
|
||||
root.style.setProperty('--rs-danger', 'var(--rs-error)');
|
||||
const custom = theme.startsWith('custom:') ? customThemes().find(item => item.id === theme.slice(7)) : undefined;
|
||||
if (custom) for (const [key, color] of Object.entries(custom[mode])) root.style.setProperty(`--rs-${key}`, color);
|
||||
if (custom) for (const [key, color] of Object.entries(custom.colors)) root.style.setProperty(`--rs-${key}`, color);
|
||||
window.dispatchEvent(new CustomEvent('runestone-theme-changed'));
|
||||
}
|
||||
export function saveAppearance(value: Appearance) {
|
||||
|
||||
Reference in New Issue
Block a user