From 06309129be80c549aacf19a01e9b36bdcd47f9f2 Mon Sep 17 00:00:00 2001 From: Eugen Rochko Date: Wed, 24 Jun 2020 10:25:32 +0200 Subject: [Glitch] Fix audio/video/images/cards not reacting to window resizes in web UI Port bb9ca8a587ee5a3ec8778e72828aca0ba8871327 to glitch-soc Co-authored-by: Yamagishi Kazutoshi Co-authored-by: Yamagishi Kazutoshi Signed-off-by: Thibaut Girka --- .../flavours/glitch/features/video/index.js | 31 +++++++++++++++++++--- 1 file changed, 27 insertions(+), 4 deletions(-) (limited to 'app/javascript/flavours/glitch/features/video/index.js') diff --git a/app/javascript/flavours/glitch/features/video/index.js b/app/javascript/flavours/glitch/features/video/index.js index 747d03f84..c7cb5bc43 100644 --- a/app/javascript/flavours/glitch/features/video/index.js +++ b/app/javascript/flavours/glitch/features/video/index.js @@ -2,7 +2,7 @@ import React from 'react'; import PropTypes from 'prop-types'; import { defineMessages, injectIntl, FormattedMessage } from 'react-intl'; import { fromJS, is } from 'immutable'; -import { throttle } from 'lodash'; +import { throttle, debounce } from 'lodash'; import classNames from 'classnames'; import { isFullscreen, requestFullscreen, exitFullscreen } from 'flavours/glitch/util/fullscreen'; import { displayMedia, useBlurhash } from 'flavours/glitch/util/initial_state'; @@ -144,10 +144,21 @@ class Video extends React.PureComponent { setPlayerRef = c => { this.player = c; - if (c && c.offsetWidth && c.offsetWidth != this.state.containerWidth) { - if (this.props.cacheWidth) this.props.cacheWidth(this.player.offsetWidth); + if (this.player) { + this._setDimensions(); + } + } + + _setDimensions () { + const width = this.player.offsetWidth; + + if (width && width != this.state.containerWidth) { + if (this.props.cacheWidth) { + this.props.cacheWidth(width); + } + this.setState({ - containerWidth: c.offsetWidth, + containerWidth: width, }); } } @@ -293,12 +304,16 @@ class Video extends React.PureComponent { document.addEventListener('mozfullscreenchange', this.handleFullscreenChange, true); document.addEventListener('MSFullscreenChange', this.handleFullscreenChange, true); + window.addEventListener('resize', this.handleResize, { passive: true }); + if (this.props.blurhash) { this._decode(); } } componentWillUnmount () { + window.removeEventListener('resize', this.handleResize); + document.removeEventListener('fullscreenchange', this.handleFullscreenChange, true); document.removeEventListener('webkitfullscreenchange', this.handleFullscreenChange, true); document.removeEventListener('mozfullscreenchange', this.handleFullscreenChange, true); @@ -334,6 +349,14 @@ class Video extends React.PureComponent { } } + handleResize = debounce(() => { + if (this.player) { + this._setDimensions(); + } + }, 250, { + trailing: true, + }); + handleFullscreenChange = () => { this.setState({ fullscreen: isFullscreen() }); } -- cgit