fix: restore scroll position after closing the preview

This commit is contained in:
Sweet Palma 2023-12-10 18:09:27 +02:00
parent cfafefa35a
commit 39c9ada2b0
5 changed files with 45 additions and 14 deletions

View File

@ -22,6 +22,8 @@ const state = {
multiple: false, multiple: false,
prompts: [], prompts: [],
showShell: false, showShell: false,
scrollPosition: 0,
showLimit: 50,
}; };
export default new Vuex.Store({ export default new Vuex.Store({

View File

@ -104,6 +104,12 @@ const mutations = {
setETA(state, value) { setETA(state, value) {
state.upload.eta = value; state.upload.eta = value;
}, },
setShowLimit(state, value) {
state.showLimit = value;
},
setScrollPosition(state, value) {
state.scrollPosition = value;
},
resetUpload(state) { resetUpload(state) {
state.upload.uploads = {}; state.upload.uploads = {};
state.upload.queue = []; state.upload.queue = [];

View File

@ -50,7 +50,7 @@ export default {
}; };
}, },
computed: { computed: {
...mapState(["req", "reload", "loading"]), ...mapState(["req", "scrollPosition", "reload", "loading"]),
currentView() { currentView() {
if (this.req.type == undefined) { if (this.req.type == undefined) {
return null; return null;
@ -72,7 +72,22 @@ export default {
this.fetchData(); this.fetchData();
}, },
watch: { watch: {
$route: "fetchData", $route: async function (value) {
// Do not fetch data if "cache" query parameter is set
if (!this.$route.query.cache) {
await this.fetchData();
return;
}
// Load previous listing view parameters
if (this.currentView === "listing") {
const cachedShowLimit = this.$store.state.showLimit;
this.$nextTick(() => {
this.$store.commit('setShowLimit', cachedShowLimit);
this.$nextTick(() => window.scrollTo(0, this.scrollPosition));
});
}
},
reload: function (value) { reload: function (value) {
if (value === true) { if (value === true) {
this.fetchData(); this.fetchData();

View File

@ -290,7 +290,6 @@ export default {
}, },
data: function () { data: function () {
return { return {
showLimit: 50,
columnWidth: 280, columnWidth: 280,
dragCounter: 0, dragCounter: 0,
width: window.innerWidth, width: window.innerWidth,
@ -298,7 +297,7 @@ export default {
}; };
}, },
computed: { computed: {
...mapState(["req", "selected", "user", "multiple", "selected", "loading"]), ...mapState(["req", "selected", "showLimit", "user", "multiple", "selected", "loading"]),
...mapGetters(["selectedCount", "currentPrompt"]), ...mapGetters(["selectedCount", "currentPrompt"]),
nameSorted() { nameSorted() {
return this.req.sorting.by === "name"; return this.req.sorting.by === "name";
@ -383,9 +382,7 @@ export default {
}, },
watch: { watch: {
req: function () { req: function () {
// Reset the show value this.$store.commit("setShowLimit", 50);
this.showLimit = 50;
// Ensures that the listing is displayed // Ensures that the listing is displayed
Vue.nextTick(() => { Vue.nextTick(() => {
// How much every listing item affects the window height // How much every listing item affects the window height
@ -611,9 +608,14 @@ export default {
scrollEvent: throttle(function () { scrollEvent: throttle(function () {
const totalItems = this.req.numDirs + this.req.numFiles; const totalItems = this.req.numDirs + this.req.numFiles;
// Stop if listing is loading
if (this.loading) return;
// Update store scroll position
this.$store.commit("setScrollPosition", window.scrollY);
// All items are displayed // All items are displayed
if (this.showLimit >= totalItems) return; if (this.showLimit >= totalItems) return;
const currentPos = window.innerHeight + window.scrollY; const currentPos = window.innerHeight + window.scrollY;
// Trigger at the 75% of the window height // Trigger at the 75% of the window height
@ -626,7 +628,7 @@ export default {
); );
// Increase the number of displayed items // Increase the number of displayed items
this.showLimit += showQuantity; this.$store.commit('setShowLimit', this.showLimit + showQuantity);
} }
}, 100), }, 100),
dragEnter() { dragEnter() {
@ -884,7 +886,9 @@ export default {
if (this.showLimit > showQuantity && !fit) return; if (this.showLimit > showQuantity && !fit) return;
// Set the number of displayed items // Set the number of displayed items
this.showLimit = showQuantity > totalItems ? totalItems : showQuantity; const newShowLimit = showQuantity > totalItems ? totalItems : showQuantity;
this.$store.commit('setShowLimit', newShowLimit);
}, },
}, },
}; };

View File

@ -342,10 +342,14 @@ export default {
}, 1500); }, 1500);
}, 500), }, 500),
close() { close() {
this.$store.commit("updateRequest", {}); const uri = url.removeLastDir(this.$route.path) + "/";
const hasCachedListing = !!this.oldReq.items;
let uri = url.removeLastDir(this.$route.path) + "/"; this.$store.commit("updateRequest", this.oldReq);
this.$router.push({ path: uri }); if (hasCachedListing) {
this.$router.push({ path: uri, query: { cache: true } });
} else {
this.$router.push({ path: uri });
}
}, },
download() { download() {
window.open(this.downloadUrl); window.open(this.downloadUrl);