diff options
author | Sasha Sorokin <dafri.nochiterov8@gmail.com> | 2020-07-09 18:01:30 +0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-07-09 13:01:30 +0200 |
commit | 61c07c37317f01c1ab4981826704750fe9937fe7 (patch) | |
tree | abecace8339a034b8615c4a11eabf541c178bdb5 /app/javascript/mastodon/features/video/index.js | |
parent | 5b7a93b02cfa4916bd7a99d876b0e6b567e639c3 (diff) |
Replace repetitive blurhash code with component (#14267)
This commit replaces all unnecessarily repeated code for decoding and embedding blurhash canvases with separate component - <Blurhash>. Under the hood Blurhash component will use effect dependent on its props. This gives a few benefits: it will only be re-rendered whenever the hash or width/height/dummy props update, and will not render if canvas won't get to the final DOM, because then effect won't fire, which prevents weird bugs like #14257.
Diffstat (limited to 'app/javascript/mastodon/features/video/index.js')
-rw-r--r-- | app/javascript/mastodon/features/video/index.js | 38 |
1 files changed, 9 insertions, 29 deletions
diff --git a/app/javascript/mastodon/features/video/index.js b/app/javascript/mastodon/features/video/index.js index 135200a3d..fb12567f0 100644 --- a/app/javascript/mastodon/features/video/index.js +++ b/app/javascript/mastodon/features/video/index.js @@ -7,7 +7,7 @@ import classNames from 'classnames'; import { isFullscreen, requestFullscreen, exitFullscreen } from '../ui/util/fullscreen'; import { displayMedia, useBlurhash } from '../../initial_state'; import Icon from 'mastodon/components/icon'; -import { decode } from 'blurhash'; +import Blurhash from 'mastodon/components/blurhash'; const messages = defineMessages({ play: { id: 'video.play', defaultMessage: 'Play' }, @@ -169,10 +169,6 @@ class Video extends React.PureComponent { this.volume = c; } - setCanvasRef = c => { - this.canvas = c; - } - handleClickRoot = e => e.stopPropagation(); handlePlay = () => { @@ -289,10 +285,6 @@ class Video extends React.PureComponent { window.addEventListener('scroll', this.handleScroll); window.addEventListener('resize', this.handleResize, { passive: true }); - - if (this.props.blurhash) { - this._decode(); - } } componentWillUnmount () { @@ -315,24 +307,6 @@ class Video extends React.PureComponent { if (prevState.revealed && !this.state.revealed && this.video) { this.video.pause(); } - - if (prevProps.blurhash !== this.props.blurhash && this.props.blurhash) { - this._decode(); - } - } - - _decode () { - if (!useBlurhash) return; - - const hash = this.props.blurhash; - const pixels = decode(hash, 32, 32); - - if (pixels) { - const ctx = this.canvas.getContext('2d'); - const imageData = new ImageData(pixels, 32, 32); - - ctx.putImageData(imageData, 0, 0); - } } handleResize = debounce(() => { @@ -438,7 +412,7 @@ class Video extends React.PureComponent { } render () { - const { preview, src, inline, startTime, onOpenVideo, onCloseVideo, intl, alt, detailed, sensitive, link, editable } = this.props; + const { preview, src, inline, startTime, onOpenVideo, onCloseVideo, intl, alt, detailed, sensitive, link, editable, blurhash } = this.props; const { containerWidth, currentTime, duration, volume, buffer, dragging, paused, fullscreen, hovered, muted, revealed } = this.state; const progress = (currentTime / duration) * 100; const playerStyle = {}; @@ -481,7 +455,13 @@ class Video extends React.PureComponent { onClick={this.handleClickRoot} tabIndex={0} > - <canvas width={32} height={32} ref={this.setCanvasRef} className={classNames('media-gallery__preview', { 'media-gallery__preview--hidden': revealed })} /> + <Blurhash + hash={blurhash} + className={classNames('media-gallery__preview', { + 'media-gallery__preview--hidden': revealed, + })} + dummy={!useBlurhash} + /> {(revealed || editable) && <video ref={this.setVideoRef} |