|
|
|
@ -1,3 +1,27 @@ |
|
|
|
// see https://github.com/vuejs/vue-router-next/blob/master/playground/scrollWaiter.ts
|
|
|
|
import type { RouteLocationNormalized } from 'vue-router'; |
|
|
|
// class ScrollQueue {
|
|
|
|
// private resolve: (() => void) | null = null;
|
|
|
|
// private promise: Promise<any> | null = null;
|
|
|
|
|
|
|
|
// add() {
|
|
|
|
// this.promise = new Promise((resolve) => {
|
|
|
|
// this.resolve = resolve as () => void;
|
|
|
|
// });
|
|
|
|
// }
|
|
|
|
|
|
|
|
// flush() {
|
|
|
|
// this.resolve && this.resolve();
|
|
|
|
// this.resolve = null;
|
|
|
|
// this.promise = null;
|
|
|
|
// }
|
|
|
|
|
|
|
|
// async wait() {
|
|
|
|
// await this.promise;
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
// const scrollWaiter = new ScrollQueue();
|
|
|
|
|
|
|
|
/** |
|
|
|
* Handles the scroll behavior on route navigation |
|
|
|
* |
|
|
|
@ -8,10 +32,9 @@ |
|
|
|
*/ |
|
|
|
// @ts-ignore
|
|
|
|
export async function scrollBehavior(to, from, savedPosition) { |
|
|
|
await scrollWaiter.wait(); |
|
|
|
// await scrollWaiter.wait();
|
|
|
|
// Use predefined scroll behavior if defined, defaults to no scroll behavior
|
|
|
|
const behavior = document.documentElement.style.scrollBehavior || 'auto'; |
|
|
|
|
|
|
|
const behavior = 'smooth'; |
|
|
|
// Returning the `savedPosition` (if available) will result in a native-like
|
|
|
|
// behavior when navigating with back/forward buttons
|
|
|
|
if (savedPosition) { |
|
|
|
@ -24,7 +47,7 @@ export async function scrollBehavior(to, from, savedPosition) { |
|
|
|
} |
|
|
|
|
|
|
|
// Check if any matched route config has meta that discourages scrolling to top
|
|
|
|
if (to.matched.some((m: any) => m.meta.scrollToTop === false)) { |
|
|
|
if (to.matched.some((m: RouteLocationNormalized) => m.meta.scrollToTop === false)) { |
|
|
|
// Leave scroll as it is
|
|
|
|
return false; |
|
|
|
} |
|
|
|
@ -32,27 +55,3 @@ export async function scrollBehavior(to, from, savedPosition) { |
|
|
|
// Always scroll to top
|
|
|
|
return { left: 0, top: 0, behavior }; |
|
|
|
} |
|
|
|
|
|
|
|
// see https://github.com/vuejs/vue-router-next/blob/master/playground/scrollWaiter.ts
|
|
|
|
class ScrollQueue { |
|
|
|
private resolve: (() => void) | null = null; |
|
|
|
private promise: Promise<any> | null = null; |
|
|
|
|
|
|
|
add() { |
|
|
|
this.promise = new Promise((resolve) => { |
|
|
|
this.resolve = resolve as () => void; |
|
|
|
}); |
|
|
|
} |
|
|
|
|
|
|
|
flush() { |
|
|
|
this.resolve && this.resolve(); |
|
|
|
this.resolve = null; |
|
|
|
this.promise = null; |
|
|
|
} |
|
|
|
|
|
|
|
async wait() { |
|
|
|
await this.promise; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
export const scrollWaiter = new ScrollQueue(); |
|
|
|
|