From abc7b1b654b5c43108cfbd231073e225cdd4af9a Mon Sep 17 00:00:00 2001 From: Frank Noirot Date: Fri, 26 Jan 2024 14:27:16 -0500 Subject: [PATCH] Add persisted setting for model auto-rotate behavior --- src/components/ModelViewer.svelte | 3 +- src/lib/stores.ts | 41 ++++++++++++++++++- .../view/[modelId]/+page.svelte | 25 +++++++++-- 3 files changed, 63 insertions(+), 6 deletions(-) diff --git a/src/components/ModelViewer.svelte b/src/components/ModelViewer.svelte index 98be7bd..ecbf1d9 100644 --- a/src/components/ModelViewer.svelte +++ b/src/components/ModelViewer.svelte @@ -7,6 +7,7 @@ export let dataUrl: string export let pausable = true export let enableZoom = true + export let enableAutoRotate = true let readyToRender = false const { size: threlteSize } = useThrelte() @@ -78,7 +79,7 @@ > (undefined) + +type UserSettings = { + autoRotateModels: boolean +} + +const SETTINGS_KEY = 'userSettings' +export const userSettingsInitial = fromLocalStorage(SETTINGS_KEY, { + autoRotateModels: true +}) +export const userSettings = writable(userSettingsInitial) +toLocalStorage(userSettings, SETTINGS_KEY) + +function toLocalStorage(store: Readable, storageKey: string) { + if (browser) { + store.subscribe((value) => { + const storageValue = + typeof value === 'object' + ? JSON.stringify(value) + : typeof value === 'string' + ? value + : String(value) + + window.localStorage.setItem(storageKey, storageValue) + }) + } +} + +// Get value from localStorage if in browser and the value is stored, otherwise fallback +function fromLocalStorage(storageKey: string, fallbackValue: T) { + if (browser) { + const storedValue = window.localStorage.getItem(storageKey) + + if (storedValue !== 'undefined' && storedValue !== null) { + return typeof fallbackValue === 'object' ? JSON.parse(storedValue) : storedValue + } + } + + return fallbackValue +} diff --git a/src/routes/(sidebarLayout)/view/[modelId]/+page.svelte b/src/routes/(sidebarLayout)/view/[modelId]/+page.svelte index d116429..55c787c 100644 --- a/src/routes/(sidebarLayout)/view/[modelId]/+page.svelte +++ b/src/routes/(sidebarLayout)/view/[modelId]/+page.svelte @@ -7,9 +7,10 @@ import Spinner from 'components/Icons/Spinner.svelte' import { browser } from '$app/environment' import ErrorCard from 'components/ErrorCard.svelte' - import { combinedGenerations, unreadGenerations } from '$lib/stores' + import { combinedGenerations, unreadGenerations, userSettings } from '$lib/stores' import { invalidateAll, onNavigate } from '$app/navigation' import { navigating } from '$app/stores' + import Checkmark from 'components/Icons/Checkmark.svelte' export let data: Models['TextToCad_type'] $: status = $combinedGenerations.find((g) => g.id === data.id)?.status ?? data.status @@ -75,6 +76,7 @@ isSceneEmpty = true }} dataUrl={gltfUrl} + enableAutoRotate={$userSettings.autoRotateModels} /> @@ -92,10 +94,19 @@ {/if} -