From 458ba29ccf9af4a2f2ef1057206ee5f0f979beda Mon Sep 17 00:00:00 2001 From: Denis Bykhov Date: Sun, 16 Apr 2023 17:11:40 +0600 Subject: [PATCH] Load active viewlet (#2994) Signed-off-by: Denis Bykhov --- plugins/view-resources/src/utils.ts | 13 ++++++++++++- .../src/components/SpecialView.svelte | 11 +++++++++-- 2 files changed, 21 insertions(+), 3 deletions(-) diff --git a/plugins/view-resources/src/utils.ts b/plugins/view-resources/src/utils.ts index 34d4b80dfa..f5b9cd8a38 100644 --- a/plugins/view-resources/src/utils.ts +++ b/plugins/view-resources/src/utils.ts @@ -481,7 +481,18 @@ export function makeViewletKey (loc?: Location): string { return 'viewlet' + locationToUrl(loc) } -export const activeViewlet = writable | null>>({}) +function getSavedViewlets (): Record | null> { + const res: Record | null> = {} + const keys = Object.keys(localStorage) + for (const key of keys) { + if (!key.startsWith('viewlet')) continue + const item = localStorage.getItem(key) as Ref | null + res[key] = item + } + return res +} + +export const activeViewlet = writable | null>>(getSavedViewlets()) export function setActiveViewletId (viewletId: Ref | null, loc?: Location): void { const key = makeViewletKey(loc) diff --git a/plugins/workbench-resources/src/components/SpecialView.svelte b/plugins/workbench-resources/src/components/SpecialView.svelte index 99b05e1d0c..91311423a8 100644 --- a/plugins/workbench-resources/src/components/SpecialView.svelte +++ b/plugins/workbench-resources/src/components/SpecialView.svelte @@ -88,8 +88,15 @@ }) ) - $: { - const newViewlet = viewlets.find((viewlet) => viewlet?._id === $activeViewlet[key]) ?? viewlets[0] + $: getActiveViewlet(viewlets, $activeViewlet, key) + + function getActiveViewlet ( + viewlets: WithLookup[], + activeViewlet: Record | null>, + key: string + ) { + if (viewlets.length === 0) return + const newViewlet = viewlets.find((viewlet) => viewlet?._id === activeViewlet[key]) ?? viewlets[0] if (viewlet?._id !== newViewlet?._id) { preference = undefined }