Skip to content

Commit 71152f4

Browse files
committed
Revert "Revert "Change audio/video playback to stop playback when out of view (mastodon#12486)""
This reverts commit 2568dab.
1 parent 3175365 commit 71152f4

2 files changed

Lines changed: 34 additions & 0 deletions

File tree

app/javascript/mastodon/features/audio/index.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,8 @@ class Audio extends React.PureComponent {
6060
if (this.waveform) {
6161
this._updateWaveform();
6262
}
63+
64+
window.addEventListener('scroll', this.handleScroll);
6365
}
6466

6567
componentDidUpdate (prevProps) {
@@ -69,6 +71,8 @@ class Audio extends React.PureComponent {
6971
}
7072

7173
componentWillUnmount () {
74+
window.removeEventListener('scroll', this.handleScroll);
75+
7276
if (this.wavesurfer) {
7377
this.wavesurfer.destroy();
7478
this.wavesurfer = null;
@@ -174,6 +178,19 @@ class Audio extends React.PureComponent {
174178
}
175179
}, 60);
176180

181+
handleScroll = throttle(() => {
182+
if (!this.waveform || !this.wavesurfer) {
183+
return;
184+
}
185+
186+
const { top, height } = this.waveform.getBoundingClientRect();
187+
const inView = (top <= (window.innerHeight || document.documentElement.clientHeight)) && (top + height >= 0);
188+
189+
if (!this.state.paused && !inView) {
190+
this.setState({ paused: true }, () => this.wavesurfer.pause());
191+
}
192+
}, 150, { trailing: true })
193+
177194
render () {
178195
const { height, intl, alt, editable } = this.props;
179196
const { paused, muted, volume, currentTime } = this.state;

app/javascript/mastodon/features/video/index.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -279,12 +279,16 @@ class Video extends React.PureComponent {
279279
document.addEventListener('mozfullscreenchange', this.handleFullscreenChange, true);
280280
document.addEventListener('MSFullscreenChange', this.handleFullscreenChange, true);
281281

282+
window.addEventListener('scroll', this.handleScroll);
283+
282284
if (this.props.blurhash) {
283285
this._decode();
284286
}
285287
}
286288

287289
componentWillUnmount () {
290+
window.removeEventListener('scroll', this.handleScroll);
291+
288292
document.removeEventListener('fullscreenchange', this.handleFullscreenChange, true);
289293
document.removeEventListener('webkitfullscreenchange', this.handleFullscreenChange, true);
290294
document.removeEventListener('mozfullscreenchange', this.handleFullscreenChange, true);
@@ -321,6 +325,19 @@ class Video extends React.PureComponent {
321325
}
322326
}
323327

328+
handleScroll = throttle(() => {
329+
if (!this.video) {
330+
return;
331+
}
332+
333+
const { top, height } = this.video.getBoundingClientRect();
334+
const inView = (top <= (window.innerHeight || document.documentElement.clientHeight)) && (top + height >= 0);
335+
336+
if (!this.state.paused && !inView) {
337+
this.setState({ paused: true }, () => this.video.pause());
338+
}
339+
}, 150, { trailing: true })
340+
324341
handleFullscreenChange = () => {
325342
this.setState({ fullscreen: isFullscreen() });
326343
}

0 commit comments

Comments
 (0)