Files
oldboy/src/lib/stores/editorSettings.ts
2025-11-13 18:08:38 +01:00

69 lines
2.1 KiB
TypeScript

import { writable } from 'svelte/store';
import type { ThemeName } from '../themes';
const STORAGE_KEY = 'editorSettings';
export interface EditorSettings {
fontSize: number;
fontFamily: string;
showLineNumbers: boolean;
enableLineWrapping: boolean;
tabSize: number;
vimMode: boolean;
enableHoverTooltips: boolean;
theme: ThemeName;
}
const defaultSettings: EditorSettings = {
fontSize: 14,
fontFamily: "'Roboto Mono', Monaco, Consolas, 'Liberation Mono', 'Courier New', monospace",
showLineNumbers: true,
enableLineWrapping: false,
tabSize: 2,
vimMode: false,
enableHoverTooltips: true,
theme: 'monodark'
};
export interface EditorSettingsStore {
subscribe: (run: (value: EditorSettings) => void) => () => void;
set: (value: EditorSettings) => void;
update: (updater: (value: EditorSettings) => EditorSettings) => void;
updatePartial: (partial: Partial<EditorSettings>) => void;
}
export function createEditorSettingsStore(): EditorSettingsStore {
const stored = typeof localStorage !== 'undefined' ? localStorage.getItem(STORAGE_KEY) : null;
const initial = stored ? { ...defaultSettings, ...JSON.parse(stored) } : defaultSettings;
const { subscribe, set, update } = writable<EditorSettings>(initial);
return {
subscribe,
set: (value: EditorSettings) => {
if (typeof localStorage !== 'undefined') {
localStorage.setItem(STORAGE_KEY, JSON.stringify(value));
}
set(value);
},
update: (updater: (value: EditorSettings) => EditorSettings) => {
update((current) => {
const newValue = updater(current);
if (typeof localStorage !== 'undefined') {
localStorage.setItem(STORAGE_KEY, JSON.stringify(newValue));
}
return newValue;
});
},
updatePartial: (partial: Partial<EditorSettings>) => {
update((current) => {
const newValue = { ...current, ...partial };
if (typeof localStorage !== 'undefined') {
localStorage.setItem(STORAGE_KEY, JSON.stringify(newValue));
}
return newValue;
});
}
};
}