Keep preferences in one settings file and move tab transfer to the host
Keep the user preferences in settings.json in the app config folder. WebView storage can be lost, and the host cannot read it. The file has a schema number, a revision number, and one value for each section: general, appearance, customThemes, customThemesLegacy, and language. - settings_read gives all sections, the revision, and a one-time notice. - settings_write replaces one section, writes the file atomically, and sends settings-changed to all windows. The limit is 8 MiB. - settings_migrate writes the first file only when no file exists. - The host moves a damaged file to settings.corrupt-<seconds>.json, starts from the defaults, and reports this one time. Each window loads the sections before its first render. One store in src/lib/settings/store.ts replaces the storage listeners. When no file exists, the window normalizes the old localStorage values and migrates them. The old keys stay in place for one release. The window layout and the open tabs stay in localStorage on purpose. The browser preview keeps all preferences in localStorage. Keep tab transfer offers in host memory, not in localStorage, because an offer can hold unsaved text. A window takes an offer once. The host checks the workspace root and a 120 second expiry. When the receiver accepts the tab, the host sends tab-transfer-accepted to the source window only. A refused offer stays open, and the source tab stays open. A new window and a drop on the tab ribbon use one check. The smoke harness serves the new commands from one shared state for each browser context. Tests read saved values from the mock settings file. Rust, unit, and smoke tests cover the changes.
This commit is contained in:
@@ -1,6 +1,8 @@
|
||||
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';
|
||||
import { readSection, writeSection } from '../settings/store.ts';
|
||||
/** The old localStorage key. The appearance section of the settings file replaces it. */
|
||||
export const APPEARANCE_KEY = 'runestone.appearance';
|
||||
export const THEMES = ['default', 'claude', 'sakura', 'harvest-moon', 'crimson-pilot', 'silver-serpent'] as const;
|
||||
export const BUILTIN_THEMES = [
|
||||
@@ -18,10 +20,11 @@ export const BACKGROUND_POSITIONS = ['center', 'top left', 'top', 'top right', '
|
||||
export const BACKGROUND_SIZES = ['cover', 'contain', 'auto'] as const;
|
||||
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; transparency?: unknown } | null): Appearance {
|
||||
/** `themes` is the list of custom themes. By default, it is the saved list. */
|
||||
export function normalizeAppearance(value: { theme?: unknown; mode?: unknown; newTabBackground?: unknown; transparency?: unknown } | null, themes?: CustomTheme[]): 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 selectedCustom = theme.startsWith('custom:') ? customThemes(value?.mode === 'light' ? 'light' : 'dark').find(item => `custom:${item.id}` === theme) : undefined;
|
||||
const selectedCustom = theme.startsWith('custom:') ? (themes ?? 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 };
|
||||
@@ -43,6 +46,11 @@ export function parseAppearance(raw: string | null): Appearance {
|
||||
try { return normalizeAppearance(JSON.parse(raw || 'null')); }
|
||||
catch { return { ...DEFAULT_APPEARANCE }; }
|
||||
}
|
||||
/** The saved appearance, normalized. */
|
||||
export function readAppearance(): Appearance {
|
||||
const value = readSection('appearance');
|
||||
return normalizeAppearance(value && typeof value === 'object' && !Array.isArray(value) ? value : null);
|
||||
}
|
||||
const keys = ['bg', 'bg-elevated', 'bg-active', 'border', 'text-dim', 'text', 'text-strong', 'accent', 'selection'] as const;
|
||||
const palettes = {
|
||||
default: {
|
||||
@@ -203,9 +211,11 @@ export function applyAppearance(value: Appearance, root = document.documentEleme
|
||||
}
|
||||
window.dispatchEvent(new CustomEvent('runestone-theme-changed'));
|
||||
}
|
||||
export function saveAppearance(value: Appearance) {
|
||||
/** Save and apply an appearance. Throws at once if browser storage refuses it. The promise reports a failed save. */
|
||||
export function saveAppearance(value: Appearance): Promise<void> {
|
||||
const appearance = normalizeAppearance(value);
|
||||
localStorage.setItem(APPEARANCE_KEY, JSON.stringify(appearance));
|
||||
const saved = writeSection('appearance', appearance);
|
||||
applyAppearance(appearance);
|
||||
return saved;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user