From 52beedcef04625607796acaeb739d838decd28fa Mon Sep 17 00:00:00 2001 From: Leo Vasanko Date: Mon, 20 Nov 2023 14:49:15 -0800 Subject: [PATCH] Preserve cursor and selection across reloads. --- frontend/src/components/FileExplorer.vue | 2 +- frontend/src/components/Gallery.vue | 7 +------ frontend/src/stores/main.ts | 17 ++++++++++++++--- 3 files changed, 16 insertions(+), 10 deletions(-) diff --git a/frontend/src/components/FileExplorer.vue b/frontend/src/components/FileExplorer.vue index 4348a34..c990510 100644 --- a/frontend/src/components/FileExplorer.vue +++ b/frontend/src/components/FileExplorer.vue @@ -221,7 +221,7 @@ onMounted(() => { updateModified(); modifiedTimer = setInterval(updateModified, 1000) const active = document.querySelector('.cursor') as HTMLElement | null if (active) { - active.scrollIntoView({ block: 'center', behavior: 'smooth' }) + active.scrollIntoView({ block: 'center', behavior: 'instant' }) active.focus() } }) diff --git a/frontend/src/components/Gallery.vue b/frontend/src/components/Gallery.vue index f4928a5..08c9062 100644 --- a/frontend/src/components/Gallery.vue +++ b/frontend/src/components/Gallery.vue @@ -164,15 +164,10 @@ watchEffect(() => { focusBreadcrumb() } }) -let nowkey = ref(0) -let modifiedTimer: any = null -const updateModified = () => { - nowkey.value = Math.floor(Date.now() / 1000) -} onMounted(() => { const active = document.querySelector('.cursor') as HTMLElement | null if (active) { - active.scrollIntoView({ block: 'center', behavior: 'smooth' }) + active.scrollIntoView({ block: 'center', behavior: 'instant' }) active.focus() } }) diff --git a/frontend/src/stores/main.ts b/frontend/src/stores/main.ts index b26cece..7541105 100644 --- a/frontend/src/stores/main.ts +++ b/frontend/src/stores/main.ts @@ -1,6 +1,6 @@ import type { FileEntry, FUID, SelectedItems } from '@/repositories/Document' import { Doc } from '@/repositories/Document' -import { defineStore } from 'pinia' +import { defineStore, type StateTree } from 'pinia' import { collator } from '@/utils' import { logoutUser } from '@/repositories/User' import { watchConnect } from '@/repositories/WS' @@ -11,7 +11,7 @@ export const useMainStore = defineStore({ id: 'main', state: () => ({ document: shallowRef([]), - selected: new Set(), + selected: new Set([]), query: '' as string, fileExplorer: null as any, error: '' as string, @@ -31,7 +31,18 @@ export const useMainStore = defineStore({ } }), persist: { - paths: ['prefs'], + paths: ['prefs', 'cursor', 'selected'], + serializer: { + deserialize: (data: string): StateTree => { + const ret = JSON.parse(data) + ret.selected = new Set(ret.selected) + return ret + }, + serialize: (tree: StateTree): string => { + tree.selected = Array.from(tree.selected) + return JSON.stringify(tree) + } + }, }, actions: { updateRoot(root: FileEntry[]) {