-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.vue
74 lines (62 loc) · 1.9 KB
/
app.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
<template>
<div>
<NuxtPage />
</div>
</template>
<!--
<script setup>
onMounted(() => {
// Source; https://github.com/johanholmerin/popstate-direction
// Keep track of current position
let currentIndex = (history.state && history.state.index) || 0;
// Set initial index, before replacing setters
if (!history.state || !('index' in history.state)) {
history.replaceState(
{ index: currentIndex, state: history.state },
document.title
);
}
// Native functions
const getState = Object.getOwnPropertyDescriptor(History.prototype, 'state')
.get;
const { pushState, replaceState } = history;
// Detect forward and back changes
function onPopstate() {
const state = getState.call(history);
// State is unset when `location.hash` is set. Update with incremented index
if (!state) {
replaceState.call(history, { index: currentIndex + 1 }, document.title);
}
const index = state ? state.index : currentIndex + 1;
const direction = index > currentIndex ? 'forward' : 'back';
window.dispatchEvent(new Event(direction));
currentIndex = index;
}
// Create functions which modify index
function modifyStateFunction(func, n) {
return (state, ...args) => {
func.call(history, { index: currentIndex + n, state }, ...args);
// Only update currentIndex if call succeeded
currentIndex += n;
};
}
// Override getter to only return the real state
function modifyStateGetter(cls) {
const { get } = Object.getOwnPropertyDescriptor(cls.prototype, 'state');
Object.defineProperty(cls.prototype, 'state', {
configurable: true,
enumerable: true,
get() {
return get.call(this).state;
},
set: undefined
});
}
modifyStateGetter(History);
modifyStateGetter(PopStateEvent);
history.pushState = modifyStateFunction(pushState, 1);
history.replaceState = modifyStateFunction(replaceState, 0);
window.addEventListener('popstate', onPopstate);
console.log("asdsad")
})
</script> -->