Add settings store

This commit is contained in:
ZXY101
2023-09-05 14:35:50 +02:00
parent 90796e4ae9
commit b34e5e50ec

45
src/lib/settings/index.ts Normal file
View File

@@ -0,0 +1,45 @@
import { browser } from "$app/environment";
import { writable } from "svelte/store";
type Settings = {
zoomMode: 'keep' | 'something'
rightToLeft: boolean;
singlePageView: boolean;
textEditable: boolean;
textBoxBorders: boolean;
displayOCR: boolean;
};
const defaultSettings: Settings = {
zoomMode: 'keep',
rightToLeft: true,
singlePageView: false,
displayOCR: true,
textEditable: false,
textBoxBorders: false
}
const stored = browser ? window.localStorage.getItem('settings') : undefined
const initialSettings: Settings = stored && browser ? JSON.parse(stored) : defaultSettings
export const settingsStore = writable<Settings>(initialSettings);
export function updateSetting(key: string, value: any) {
settingsStore.update((settings) => {
return {
...settings,
[key]: value
};
});
}
export function resetSettings() {
settingsStore.set(defaultSettings);
}
settingsStore.subscribe((settings) => {
if (browser) {
window.localStorage.setItem('settings', JSON.stringify(settings))
}
})