This commit is contained in:
2025-10-15 01:23:05 +02:00
parent 8ac5ac0770
commit e492c03f15
15 changed files with 484 additions and 330 deletions

View File

@ -18,8 +18,15 @@ const defaultSettings: EditorSettings = {
vimMode: false
};
function createEditorSettings() {
const stored = localStorage.getItem('editorSettings');
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('editorSettings') : null;
const initial = stored ? { ...defaultSettings, ...JSON.parse(stored) } : defaultSettings;
const { subscribe, set, update } = writable<EditorSettings>(initial);
@ -27,24 +34,28 @@ function createEditorSettings() {
return {
subscribe,
set: (value: EditorSettings) => {
localStorage.setItem('editorSettings', JSON.stringify(value));
if (typeof localStorage !== 'undefined') {
localStorage.setItem('editorSettings', JSON.stringify(value));
}
set(value);
},
update: (updater: (value: EditorSettings) => EditorSettings) => {
update((current) => {
const newValue = updater(current);
localStorage.setItem('editorSettings', JSON.stringify(newValue));
if (typeof localStorage !== 'undefined') {
localStorage.setItem('editorSettings', JSON.stringify(newValue));
}
return newValue;
});
},
updatePartial: (partial: Partial<EditorSettings>) => {
update((current) => {
const newValue = { ...current, ...partial };
localStorage.setItem('editorSettings', JSON.stringify(newValue));
if (typeof localStorage !== 'undefined') {
localStorage.setItem('editorSettings', JSON.stringify(newValue));
}
return newValue;
});
}
};
}
export const editorSettings = createEditorSettings();