Add interface languages and improve themes and workspace controls for 0.1.36
This commit is contained in:
@@ -1,13 +1,16 @@
|
||||
export const CUSTOM_THEMES_KEY = "runestone.custom-themes";
|
||||
export const COLOR_TOKENS = ["bg", "bg-elevated", "bg-active", "border", "text", "text-dim", "text-strong", "accent", "accent-2", "selection", "error", "warn", "warn-hot", "ok", "special", "syntax-parameter", "syntax-keyword", "syntax-local", "syntax-static", "syntax-instance", "syntax-global", "syntax-string", "syntax-number", "syntax-comment", "syntax-type", "syntax-function"] as const;
|
||||
export type ThemeColors = Partial<Record<typeof COLOR_TOKENS[number], string>>;
|
||||
export type CustomTheme = { schema: 1; id: string; name: string; light: ThemeColors; dark: ThemeColors };
|
||||
export const FONT_ROLES = ['ui', 'prose', 'heading', 'mono'] as const;
|
||||
export type ThemeFonts = Partial<Record<typeof FONT_ROLES[number], string>>;
|
||||
export type CustomTheme = { schema: 2; id: string; name: string; colors: ThemeColors; fonts?: ThemeFonts };
|
||||
export type LegacyCustomTheme = { schema: 1; id: string; name: string; light: ThemeColors; dark: ThemeColors };
|
||||
|
||||
export function parseCustomTheme(input: unknown): CustomTheme {
|
||||
export function parseCustomTheme(input: unknown, legacyMode: "light" | "dark" = "dark"): CustomTheme {
|
||||
if (!input || typeof input !== "object") throw new Error("Choose a RuneStone theme JSON file.");
|
||||
const value = input as Record<string, unknown>;
|
||||
if (value.schema !== 1 || typeof value.id !== "string" || !/^[a-z][a-z0-9-]{0,63}$/.test(value.id)
|
||||
|| typeof value.name !== "string" || !value.name.trim() || value.name.length > 80) throw new Error("The theme needs schema 1, an ID, and a name.");
|
||||
if ((value.schema !== 1 && value.schema !== 2) || typeof value.id !== "string" || !/^[a-z][a-z0-9-]{0,63}$/.test(value.id)
|
||||
|| typeof value.name !== "string" || !value.name.trim() || value.name.length > 80) throw new Error("The theme needs schema 2, an ID, and a name.");
|
||||
const colors = (mode: string): ThemeColors => {
|
||||
const source = value[mode];
|
||||
if (!source || typeof source !== "object" || Array.isArray(source)) throw new Error(`The theme needs ${mode} colors.`);
|
||||
@@ -20,13 +23,55 @@ export function parseCustomTheme(input: unknown): CustomTheme {
|
||||
if (!result.bg || !result.text || !result.accent) throw new Error(`The ${mode} palette needs bg, text, and accent.`);
|
||||
return result;
|
||||
};
|
||||
return { schema: 1, id: value.id, name: value.name.trim(), light: colors("light"), dark: colors("dark") };
|
||||
if (value.schema === 1) { colors("light"); colors("dark"); }
|
||||
let fonts: ThemeFonts | undefined;
|
||||
if (value.fonts !== undefined) {
|
||||
if (!value.fonts || typeof value.fonts !== 'object' || Array.isArray(value.fonts)) throw new Error('Use an object for theme fonts.');
|
||||
fonts = {};
|
||||
for (const [role, stack] of Object.entries(value.fonts)) {
|
||||
if (!(FONT_ROLES as readonly string[]).includes(role)) throw new Error(`Unknown theme font: ${role}`);
|
||||
if (typeof stack !== 'string' || stack.length > 512 || !/^[\p{L}\p{N} _,'"-]+$/u.test(stack) || !stack.trim()
|
||||
|| stack.split(',').some(family => !/^(?:"[^"\n]+"|'[^'\n]+'|[\p{L}\p{N}_ -]+)$/u.test(family.trim()))) throw new Error(`Use a font family stack for ${role}.`);
|
||||
fonts[role as keyof ThemeFonts] = stack.trim();
|
||||
}
|
||||
}
|
||||
return { schema: 2, id: value.id, name: value.name.trim(), colors: colors(value.schema === 1 ? legacyMode : "colors"), ...(fonts ? { fonts } : {}) };
|
||||
}
|
||||
|
||||
export function customThemes(): CustomTheme[] {
|
||||
const pendingLegacy = new Map<string, CustomTheme[]>();
|
||||
|
||||
export function customThemes(legacyMode?: "light" | "dark"): CustomTheme[] {
|
||||
try {
|
||||
const stored = JSON.parse(localStorage.getItem(CUSTOM_THEMES_KEY) || "[]");
|
||||
return Array.isArray(stored) ? stored.flatMap(value => { try { return [parseCustomTheme(value)]; } catch { return []; } }) : [];
|
||||
const raw = localStorage.getItem(CUSTOM_THEMES_KEY) || "[]";
|
||||
const pending = pendingLegacy.get(raw);
|
||||
if (pending) return pending.map(theme => ({ ...theme, colors: { ...theme.colors } }));
|
||||
const stored = JSON.parse(raw);
|
||||
if (!Array.isArray(stored)) return [];
|
||||
if (!legacyMode) {
|
||||
try { legacyMode = JSON.parse(localStorage.getItem('runestone.appearance') || 'null')?.mode === 'light' ? 'light' : 'dark'; }
|
||||
catch { legacyMode = 'dark'; }
|
||||
}
|
||||
const parsed = stored.flatMap(value => { try { return [parseCustomTheme(value, legacyMode)]; } catch { return []; } });
|
||||
if (stored.some(value => value?.schema === 1)) {
|
||||
// Retain both original palettes before converting the saved selection to one palette.
|
||||
try {
|
||||
const backupKey = `${CUSTOM_THEMES_KEY}.legacy`;
|
||||
const backup = JSON.parse(localStorage.getItem(backupKey) || '[]');
|
||||
if (!Array.isArray(backup)) throw new Error('Invalid legacy backup');
|
||||
const originals = backup;
|
||||
for (const value of stored.filter(value => value?.schema === 1)) {
|
||||
if (!originals.some(item => JSON.stringify(item) === JSON.stringify(value))) originals.push(value);
|
||||
}
|
||||
localStorage.setItem(backupKey, JSON.stringify(originals));
|
||||
localStorage.setItem(CUSTOM_THEMES_KEY, JSON.stringify(stored.map(value => {
|
||||
try { return parseCustomTheme(value, legacyMode); } catch { return value; }
|
||||
})));
|
||||
} catch {
|
||||
// Storage failures must not hide readable themes or change their chosen palette.
|
||||
pendingLegacy.set(raw, parsed);
|
||||
}
|
||||
}
|
||||
return parsed;
|
||||
} catch { return []; }
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user