From 0bd9ece0e6f6daa7826d5b93d00b72095086fa42 Mon Sep 17 00:00:00 2001 From: Weidi Deng Date: Thu, 7 Jan 2021 11:58:59 +0800 Subject: [PATCH] store multiple navigation info --- frontend/src/store/getters.js | 9 +++++++ frontend/src/store/index.js | 5 ++-- frontend/src/store/mutations.js | 9 ++++++- frontend/src/views/Files.vue | 43 +++++++++++++++------------------ 4 files changed, 38 insertions(+), 28 deletions(-) diff --git a/frontend/src/store/getters.js b/frontend/src/store/getters.js index 70c9cd0e..56bdd7f4 100644 --- a/frontend/src/store/getters.js +++ b/frontend/src/store/getters.js @@ -13,6 +13,15 @@ const getters = { let sum = state.upload.progress.reduce((acc, val) => acc + val) return Math.ceil(sum / state.upload.size * 100); + }, + getLastViewedDetail: state => (path) => { + if (state.lastViewed.details.has(path)) { + let i = state.lastViewed.paths.indexOf(path) + state.lastViewed.paths.splice(i, 1) + state.lastViewed.paths.push(path) + return state.lastViewed.details.get(path) + } + return null } } diff --git a/frontend/src/store/index.js b/frontend/src/store/index.js index 6fd164e8..36fbf3b9 100644 --- a/frontend/src/store/index.js +++ b/frontend/src/store/index.js @@ -27,9 +27,8 @@ const state = { previewMode: false, hash: '', lastViewed: { - path: '', - clicked: '', - pageOffset: 0 + paths: [], + details: new Map() } } diff --git a/frontend/src/store/mutations.js b/frontend/src/store/mutations.js index 9165ed4d..d8c43606 100644 --- a/frontend/src/store/mutations.js +++ b/frontend/src/store/mutations.js @@ -88,7 +88,14 @@ const mutations = { state.previewMode = value }, setHash: (state, value) => (state.hash = value), - setLastViewed: (state, value) => (state.lastViewed = value) + addLastViewed: (state, value) => { + let paths = state.lastViewed.paths + if (!state.lastViewed.details.has(value.path)) paths.push(value.path) + state.lastViewed.details.set(value.path, value.detail) + if (paths.length > 10) { + state.lastViewed.details.delete(paths.shift()) + } + } } export default mutations diff --git a/frontend/src/views/Files.vue b/frontend/src/views/Files.vue index 981194d7..2aae9e85 100644 --- a/frontend/src/views/Files.vue +++ b/frontend/src/views/Files.vue @@ -55,7 +55,8 @@ export default { 'selectedCount', 'isListing', 'isEditor', - 'isFiles' + 'isFiles', + 'getLastViewedDetail' ]), ...mapState([ 'req', @@ -63,8 +64,7 @@ export default { 'reload', 'multiple', 'loading', - 'show', - 'lastViewed' + 'show' ]), isPreview () { return !this.loading && !this.isListing && !this.isEditor || this.loading && this.$store.state.previewMode @@ -131,40 +131,35 @@ export default { methods: { ...mapMutations([ 'setLoading' ]), async nav() { - if (!this.isPreview && !this.isEditor && clean(`/${this.$route.params.pathMatch}`) !== clean(this.lastViewed.path) && clean(`/${this.$route.params.pathMatch}`).startsWith(clean(this.lastViewed.path))) { + if (!this.isPreview && !this.isEditor && clean(`/${this.$route.params.pathMatch}`).startsWith(clean(this.req.path))) { let dirs = clean(this.$route.fullPath).split("/") - this.$store.commit('setLastViewed', { - path: this.req.path, - clicked: decodeURIComponent(dirs[dirs.length - 1]), - pageOffset: window.pageYOffset + this.$store.commit('addLastViewed', { + path: clean(this.req.path), + detail: { + clicked: decodeURIComponent(dirs[dirs.length - 1]), + pageOffset: window.pageYOffset + } }) - this.fetchData() - } else if (clean(this.lastViewed.path) === clean(`/${this.$route.params.pathMatch}`)) { - await this.fetchData() - - let offset = 1000, oldPageOffset = 0, pageOffset = window.pageYOffset, _this = this - let int = setInterval(function() { + } + await this.fetchData() + let detail = this.getLastViewedDetail(clean(`/${this.$route.params.pathMatch}`)) + if (detail !== null) { + let offset = Math.min(1000, detail.pageOffset), oldPageOffset = 0, pageOffset = window.pageYOffset + let int = setInterval(function () { window.scrollTo(0, offset) oldPageOffset = pageOffset pageOffset = window.pageYOffset - if (offset >= _this.lastViewed.pageOffset || oldPageOffset === pageOffset) clearInterval(int); - offset += Math.min(1000, _this.lastViewed.pageOffset - offset) + if (offset >= detail.pageOffset || oldPageOffset === pageOffset) clearInterval(int); + offset += Math.min(1000, detail.pageOffset - offset) }, 20); for (let i = 0; i < this.req.items.length; i++) { - if (this.req.items[i].name === this.lastViewed.clicked) { + if (this.req.items[i].name === detail.clicked) { this.$store.commit('addSelected', i) break } } - } else { - this.$store.commit('setLastViewed', { - path: '', - clicked: '', - pageOffset: 0 - }) - this.fetchData() } }, async fetchData () {