mirror of
https://github.com/tcsenpai/swingmusic.git
synced 2025-06-07 11:45:35 +00:00
search tracks page scroll to top on search
+ add composable to wait for scroll
This commit is contained in:
parent
3d37cd59b3
commit
68f990aada
41
src/composables/useWaitForScroll.ts
Normal file
41
src/composables/useWaitForScroll.ts
Normal file
@ -0,0 +1,41 @@
|
||||
// CREDITS: https://stackoverflow.com/a/66664192
|
||||
|
||||
/**
|
||||
* Scrolls and waits for the scroll to finish. Returns a promise that resolves when the scroll is finished.
|
||||
* @param elem The element to scroll and wait for
|
||||
* @param pos The position to scroll to
|
||||
* @param delay The delay in seconds to wait for
|
||||
* @returns A promise that resolves when the element has been scrolled to the position
|
||||
*/
|
||||
export default function waitForScrollEnd(
|
||||
elem: HTMLElement,
|
||||
pos = 0,
|
||||
delay = 100
|
||||
): Promise<void> {
|
||||
elem.scroll({
|
||||
top: pos,
|
||||
behavior: "smooth",
|
||||
});
|
||||
const frame_limit = 20;
|
||||
let last_changed_frame = 0;
|
||||
let last_y = elem.scrollTop;
|
||||
|
||||
return new Promise((resolve) => {
|
||||
function tick(frames: number) {
|
||||
// We requestAnimationFrame either for 500 frames or until 20 frames with
|
||||
// no change have been observed.
|
||||
if (frames >= 500 || frames - last_changed_frame > frame_limit) {
|
||||
setTimeout(() => {
|
||||
resolve();
|
||||
}, delay);
|
||||
} else {
|
||||
if (window.scrollY != last_y) {
|
||||
last_changed_frame = frames;
|
||||
last_y = window.scrollY;
|
||||
}
|
||||
requestAnimationFrame(tick.bind(null, frames + 1));
|
||||
}
|
||||
}
|
||||
tick(0);
|
||||
});
|
||||
}
|
@ -3,15 +3,20 @@ import { useDebounce } from "@vueuse/core";
|
||||
import { defineStore } from "pinia";
|
||||
import { watch } from "vue";
|
||||
import { useRoute } from "vue-router";
|
||||
import { Routes } from "@/router/routes";
|
||||
|
||||
import {
|
||||
loadMoreAlbums,
|
||||
loadMoreArtists, loadMoreTracks, searchAlbums,
|
||||
searchArtists, searchTracks
|
||||
loadMoreArtists,
|
||||
loadMoreTracks,
|
||||
searchAlbums,
|
||||
searchArtists,
|
||||
searchTracks,
|
||||
} from "../composables/fetch/searchMusic";
|
||||
import { Album, Artist, Playlist, Track } from "../interfaces";
|
||||
import { Routes } from "@/router/routes";
|
||||
import useLoaderStore from "./loader";
|
||||
import useTabStore from "./tabs";
|
||||
import waitForScrollEnd from "@/composables/useWaitForScroll";
|
||||
import { Album, Artist, Playlist, Track } from "../interfaces";
|
||||
/**
|
||||
*
|
||||
* Scrolls on clicking the loadmore button
|
||||
@ -76,11 +81,17 @@ export default defineStore("search", () => {
|
||||
function fetchTracks(query: string) {
|
||||
if (!query) return;
|
||||
|
||||
searchTracks(query).then((res) => {
|
||||
tracks.value = res.tracks;
|
||||
tracks.more = res.more;
|
||||
searchTracks(query).then((data) => {
|
||||
const scrollable = document.getElementById(
|
||||
"songlist-scroller"
|
||||
) as HTMLElement;
|
||||
|
||||
waitForScrollEnd(scrollable, 0).then(() => {
|
||||
tracks.value = data.tracks;
|
||||
tracks.more = data.more;
|
||||
tracks.query = query;
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
function fetchAlbums(query: string) {
|
||||
|
Loading…
x
Reference in New Issue
Block a user