Refactoring
This commit is contained in:
@ -1,5 +1,7 @@
|
||||
import { writable } from 'svelte/store';
|
||||
|
||||
const STORAGE_KEY = 'editorSettings';
|
||||
|
||||
export interface EditorSettings {
|
||||
fontSize: number;
|
||||
fontFamily: string;
|
||||
@ -26,7 +28,7 @@ export interface EditorSettingsStore {
|
||||
}
|
||||
|
||||
export function createEditorSettingsStore(): EditorSettingsStore {
|
||||
const stored = typeof localStorage !== 'undefined' ? localStorage.getItem('editorSettings') : null;
|
||||
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);
|
||||
@ -35,7 +37,7 @@ export function createEditorSettingsStore(): EditorSettingsStore {
|
||||
subscribe,
|
||||
set: (value: EditorSettings) => {
|
||||
if (typeof localStorage !== 'undefined') {
|
||||
localStorage.setItem('editorSettings', JSON.stringify(value));
|
||||
localStorage.setItem(STORAGE_KEY, JSON.stringify(value));
|
||||
}
|
||||
set(value);
|
||||
},
|
||||
@ -43,7 +45,7 @@ export function createEditorSettingsStore(): EditorSettingsStore {
|
||||
update((current) => {
|
||||
const newValue = updater(current);
|
||||
if (typeof localStorage !== 'undefined') {
|
||||
localStorage.setItem('editorSettings', JSON.stringify(newValue));
|
||||
localStorage.setItem(STORAGE_KEY, JSON.stringify(newValue));
|
||||
}
|
||||
return newValue;
|
||||
});
|
||||
@ -52,7 +54,7 @@ export function createEditorSettingsStore(): EditorSettingsStore {
|
||||
update((current) => {
|
||||
const newValue = { ...current, ...partial };
|
||||
if (typeof localStorage !== 'undefined') {
|
||||
localStorage.setItem('editorSettings', JSON.stringify(newValue));
|
||||
localStorage.setItem(STORAGE_KEY, JSON.stringify(newValue));
|
||||
}
|
||||
return newValue;
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user