From f21a99421ecac3316cda6baf4026cad65d84fcbc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juraj=20Nyi=CC=81ri?= Date: Sat, 12 Jun 2021 21:02:42 +0200 Subject: [PATCH] Fix: Bottom scroll boundary for every season and fullscreen infinite loop --- dist/plex-meets-homeassistant.js | 23 ++++++++++++----------- src/modules/utils.ts | 5 +++-- src/plex-meets-homeassistant.ts | 22 +++++++++++++--------- 3 files changed, 28 insertions(+), 22 deletions(-) diff --git a/dist/plex-meets-homeassistant.js b/dist/plex-meets-homeassistant.js index c3bb67a..13bd535 100644 --- a/dist/plex-meets-homeassistant.js +++ b/dist/plex-meets-homeassistant.js @@ -19157,11 +19157,11 @@ const getOffset = (el) => { } return { top: y, left: x }; }; -const getDetailsBottom = (seasonContainers, episodeContainers) => { +const getDetailsBottom = (seasonContainers, episodeContainers, activeElem) => { const lastSeasonContainer = seasonContainers[seasonContainers.length - 1]; const lastEpisodeContainer = episodeContainers[episodeContainers.length - 1]; let detailBottom = false; - if (seasonContainers.length > 0 && parseInt(lastSeasonContainer.style.top, 10) > 0) { + if (seasonContainers.length > 0 && parseInt(activeElem.style.top, 10) > 0) { detailBottom = getHeight(lastSeasonContainer) + parseInt(getOffset(lastSeasonContainer).top, 10) + 10; } else if (episodeContainers.length > 0) { @@ -20040,18 +20040,19 @@ class PlexMeetsHomeAssistant extends HTMLElement { return false; } }); - if (this.getTop() < parseInt(getOffset(activeElem).top, 10) - 70) { + const detailTop = parseInt(getOffset(activeElem).top, 10) - 70; + const detailBottom = getDetailsBottom(seasonContainers, episodeContainers, activeElem); + if (this.getTop() < detailTop) { window.scroll({ - top: getOffset(activeElem).top - 70 + top: detailTop }); } - else { - const detailBottom = getDetailsBottom(seasonContainers, episodeContainers); - if (detailBottom && this.getTop() + window.innerHeight > detailBottom) { - window.scroll({ - top: detailBottom - window.innerHeight - }); - } + else if (detailBottom && + window.innerHeight < detailBottom - detailTop && + this.getTop() + window.innerHeight > detailBottom) { + window.scroll({ + top: detailBottom - window.innerHeight + }); } } this.renderNewElementsIfNeeded(); diff --git a/src/modules/utils.ts b/src/modules/utils.ts index 40a3b5f..2e2ef2d 100644 --- a/src/modules/utils.ts +++ b/src/modules/utils.ts @@ -49,13 +49,14 @@ const getOffset = (el: Element): Record => { const getDetailsBottom = ( seasonContainers: HTMLCollectionOf, - episodeContainers: HTMLCollectionOf + episodeContainers: HTMLCollectionOf, + activeElem: HTMLElement ): number | false => { const lastSeasonContainer = seasonContainers[seasonContainers.length - 1]; const lastEpisodeContainer = episodeContainers[episodeContainers.length - 1]; let detailBottom: number | false = false; - if (seasonContainers.length > 0 && parseInt(lastSeasonContainer.style.top, 10) > 0) { + if (seasonContainers.length > 0 && parseInt(activeElem.style.top, 10) > 0) { detailBottom = getHeight(lastSeasonContainer) + parseInt(getOffset(lastSeasonContainer).top, 10) + 10; } else if (episodeContainers.length > 0) { detailBottom = getHeight(lastEpisodeContainer) + parseInt(getOffset(lastEpisodeContainer).top, 10) + 10; diff --git a/src/plex-meets-homeassistant.ts b/src/plex-meets-homeassistant.ts index f382b66..2f19741 100644 --- a/src/plex-meets-homeassistant.ts +++ b/src/plex-meets-homeassistant.ts @@ -163,17 +163,21 @@ class PlexMeetsHomeAssistant extends HTMLElement { } }); - if (this.getTop() < parseInt(getOffset(activeElem as Element).top, 10) - 70) { + const detailTop = parseInt(getOffset(activeElem as Element).top, 10) - 70; + const detailBottom = getDetailsBottom(seasonContainers, episodeContainers, activeElem); + + if (this.getTop() < detailTop) { window.scroll({ - top: getOffset(activeElem as Element).top - 70 + top: detailTop + }); + } else if ( + detailBottom && + window.innerHeight < detailBottom - detailTop && + this.getTop() + window.innerHeight > detailBottom + ) { + window.scroll({ + top: detailBottom - window.innerHeight }); - } else { - const detailBottom = getDetailsBottom(seasonContainers, episodeContainers); - if (detailBottom && this.getTop() + window.innerHeight > detailBottom) { - window.scroll({ - top: detailBottom - window.innerHeight - }); - } } }