Skip to content

Commit

Permalink
playlist scroll pos (#1120)
Browse files Browse the repository at this point in the history
* first test

* clearner code

* lint fi
  • Loading branch information
mono424 authored Sep 21, 2023
1 parent e1a4fb3 commit ee92bd6
Showing 1 changed file with 19 additions and 1 deletion.
20 changes: 19 additions & 1 deletion web/ts/stream-playlist.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,17 @@
import { DataStore } from "./data-store/data-store";
import { StreamPlaylistEntry } from "./data-store/stream-playlist";

function onVisible(element, callback) {
new IntersectionObserver((entries, observer) => {
entries.forEach((entry) => {
if (entry.intersectionRatio > 0) {
callback(element);
observer.disconnect();
}
});
}).observe(element);
}

export class StreamPlaylist {
private streamId: number;
private elem: HTMLElement;
Expand All @@ -19,11 +30,18 @@ export class StreamPlaylist {
const { prev, next } = this.findNextAndPrev();
this.elem.dispatchEvent(new CustomEvent("update", { detail: { list: this.list, prev, next } }));

// if playlist is hidden and will be visible later
onVisible(this.elem, () => this.scrollSelectedIntoView());

setTimeout(() => {
this.elem.querySelector(".--selected")?.scrollIntoView({ block: "center" });
this.scrollSelectedIntoView();
}, 10);
}

public scrollSelectedIntoView() {
this.elem.querySelector(".--selected").scrollIntoView({ block: "center" });
}

private findNextAndPrev(): { next: StreamPlaylistEntry; prev: StreamPlaylistEntry } {
const streamIndex = this.list.findIndex((e) => e.streamId == this.streamId);
const prevIndex = streamIndex - 1 >= 0 ? streamIndex - 1 : null;
Expand Down

0 comments on commit ee92bd6

Please sign in to comment.