-
Notifications
You must be signed in to change notification settings - Fork 0
/
default.vue
57 lines (53 loc) · 1.4 KB
/
default.vue
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
<template>
<Nuxt />
</template>
<script>
import { debounce } from "throttle-debounce";
// create key for each page
const getKey = () => {
return "%%live-preview-pos" + "|" + window.location.pathname;
};
// memoise scroll position
const holdPosition = debounce(300, () => {
window.sessionStorage.setItem(
getKey(),
JSON.stringify({ scrollX: window.scrollX, scrollY: window.scrollY })
);
});
/**
* set scroll position
*/
const scrollSetPosition = () => {
const currentPosition = JSON.parse(window.sessionStorage.getItem(getKey()));
if (currentPosition && currentPosition.scrollY !== undefined) {
window.scrollTo(currentPosition.scrollX, currentPosition.scrollY);
}
};
export default {
mounted() {
const { query } = this.$route;
if (
query.preview ||
query["x-craft-live-preview"] ||
query["x-craft-preview"]
) {
// wait all resources loaded for set position scroll
// window.onload = scrollSetPosition;
// don't work on prod :(
window.addEventListener("load", scrollSetPosition);
window.addEventListener("scroll", holdPosition);
}
},
unmounted() {
const { query } = this.$route;
if (
query.preview ||
query["x-craft-live-preview"] ||
query["x-craft-preview"]
) {
window.removeEventListener("scroll", holdPosition);
window.removeEventListener("load", scrollSetPosition);
}
},
};
</script>