From 5cbd8cab4cd7849496ec5d4f97b1bbde3f794d71 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juraj=20Nyi=CC=81ri?= Date: Tue, 31 Jan 2023 00:04:21 +0100 Subject: [PATCH] Fix: Incorrect card width --- dist/plex-meets-homeassistant.js | 23 +++++++++++++++++++++-- src/plex-meets-homeassistant.ts | 27 ++++++++++++++++++++++++--- 2 files changed, 45 insertions(+), 5 deletions(-) diff --git a/dist/plex-meets-homeassistant.js b/dist/plex-meets-homeassistant.js index af83d1a..7c14d58 100644 --- a/dist/plex-meets-homeassistant.js +++ b/dist/plex-meets-homeassistant.js @@ -22499,8 +22499,17 @@ class PlexMeetsHomeAssistant extends HTMLElement { this.searchInputElem.style.display = 'none'; } if (this.card) { - const marginRight = 10; // needs to be equal to .container margin right - const areaSize = this.card.offsetWidth - parseInt(this.card.style.paddingRight, 10) - parseInt(this.card.style.paddingLeft, 10); + // Todo: figure why 10.3 works + const marginRight = 10.3; // needs to be equal to .container margin right + const getAreaSize = () => { + if (this.card) { + return (this.card.getBoundingClientRect().width - + parseInt(this.card.style.paddingRight, 10) - + parseInt(this.card.style.paddingLeft, 10)); + } + return 0; + }; + const areaSize = getAreaSize(); const postersInRow = Math.floor(areaSize / this.minWidth); if (areaSize > 0) { CSS_STYLE.width = areaSize / postersInRow - marginRight; @@ -22508,6 +22517,15 @@ class PlexMeetsHomeAssistant extends HTMLElement { const episodesInRow = Math.floor(areaSize / this.minEpisodeWidth); CSS_STYLE.episodeWidth = Math.floor(areaSize / episodesInRow - marginRight); CSS_STYLE.episodeHeight = Math.round(CSS_STYLE.episodeWidth * CSS_STYLE.episodeRatio); + // hack to make sure cards width is always calculated properly, todo solve better in the future + setTimeout(() => { + if (this.card) { + const newAreaSize = getAreaSize(); + if (newAreaSize !== areaSize) { + this.renderPage(); + } + } + }, 1); } else if (this.renderPageRetries < 10) { // sometimes it loop forever, todo: properly fix! @@ -23656,6 +23674,7 @@ class PlexMeetsHomeAssistant extends HTMLElement { } const container = document.createElement('div'); container.className = 'plexMeetsContainer'; + // console.log(CSS_STYLE); container.style.width = `${CSS_STYLE.width}px`; if (this.displayTitleMain || this.displaySubtitleMain) { container.style.marginBottom = '10px'; diff --git a/src/plex-meets-homeassistant.ts b/src/plex-meets-homeassistant.ts index 00c9871..050e5fe 100644 --- a/src/plex-meets-homeassistant.ts +++ b/src/plex-meets-homeassistant.ts @@ -756,9 +756,20 @@ class PlexMeetsHomeAssistant extends HTMLElement { } if (this.card) { - const marginRight = 10; // needs to be equal to .container margin right - const areaSize = - this.card.offsetWidth - parseInt(this.card.style.paddingRight, 10) - parseInt(this.card.style.paddingLeft, 10); + // Todo: figure why 10.3 works + const marginRight = 10.3; // needs to be equal to .container margin right + + const getAreaSize = (): number => { + if (this.card) { + return ( + this.card.getBoundingClientRect().width - + parseInt(this.card.style.paddingRight, 10) - + parseInt(this.card.style.paddingLeft, 10) + ); + } + return 0; + }; + const areaSize = getAreaSize(); const postersInRow = Math.floor(areaSize / this.minWidth); if (areaSize > 0) { CSS_STYLE.width = areaSize / postersInRow - marginRight; @@ -767,6 +778,15 @@ class PlexMeetsHomeAssistant extends HTMLElement { CSS_STYLE.episodeWidth = Math.floor(areaSize / episodesInRow - marginRight); CSS_STYLE.episodeHeight = Math.round(CSS_STYLE.episodeWidth * CSS_STYLE.episodeRatio); + // hack to make sure cards width is always calculated properly, todo solve better in the future + setTimeout(() => { + if (this.card) { + const newAreaSize = getAreaSize(); + if (newAreaSize !== areaSize) { + this.renderPage(); + } + } + }, 1); } else if (this.renderPageRetries < 10) { // sometimes it loop forever, todo: properly fix! setTimeout(() => { @@ -2034,6 +2054,7 @@ class PlexMeetsHomeAssistant extends HTMLElement { const container = document.createElement('div'); container.className = 'plexMeetsContainer'; + // console.log(CSS_STYLE); container.style.width = `${CSS_STYLE.width}px`; if (this.displayTitleMain || this.displaySubtitleMain) {