Cleaning
This commit is contained in:
@ -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();
|
||||
|
||||
Reference in New Issue
Block a user