From 27becaa9df6212906faf2204fca58e580bdbacd0 Mon Sep 17 00:00:00 2001 From: Eugen Rochko Date: Tue, 23 Jun 2020 12:20:14 +0200 Subject: [PATCH] [Glitch] Change volume control and download buttons in web UI Port ce34c13aff771632e2bf5cd4e37fe7272580f591 to glitch-soc Signed-off-by: Thibaut Girka --- .../flavours/glitch/components/status.js | 3 +- .../flavours/glitch/features/audio/index.js | 81 +++++++++---------- .../status/components/detailed_status.js | 3 +- .../flavours/glitch/features/video/index.js | 46 ++++------- .../glitch/styles/components/media.scss | 63 +++++++++++---- 5 files changed, 105 insertions(+), 91 deletions(-) diff --git a/app/javascript/flavours/glitch/components/status.js b/app/javascript/flavours/glitch/components/status.js index 4ff9359ef0..cda9aee161 100644 --- a/app/javascript/flavours/glitch/components/status.js +++ b/app/javascript/flavours/glitch/components/status.js @@ -593,9 +593,8 @@ class Status extends ImmutablePureComponent { alt={attachment.get('description')} poster={status.getIn(['account', 'avatar_static'])} duration={attachment.getIn(['meta', 'original', 'duration'], 0)} - peaks={[0]} width={this.props.cachedMediaWidth} - height={70} + height={110} cacheWidth={this.props.cacheMediaWidth} /> )} diff --git a/app/javascript/flavours/glitch/features/audio/index.js b/app/javascript/flavours/glitch/features/audio/index.js index c7f807c8ef..995586a24a 100644 --- a/app/javascript/flavours/glitch/features/audio/index.js +++ b/app/javascript/flavours/glitch/features/audio/index.js @@ -6,7 +6,7 @@ import Icon from 'flavours/glitch/components/icon'; import classNames from 'classnames'; import { throttle } from 'lodash'; import { encode, decode } from 'blurhash'; -import { getPointerPosition } from 'mastodon/features/video'; +import { getPointerPosition, fileNameFromURL } from 'flavours/glitch/features/video'; const digitCharacters = [ '0', @@ -140,7 +140,7 @@ const messages = defineMessages({ }); const TICK_SIZE = 10; -const PADDING = 180; +const PADDING = 180; export default @injectIntl class Audio extends React.PureComponent { @@ -150,10 +150,8 @@ class Audio extends React.PureComponent { alt: PropTypes.string, poster: PropTypes.string, duration: PropTypes.number, - peaks: PropTypes.arrayOf(PropTypes.number), width: PropTypes.number, height: PropTypes.number, - preload: PropTypes.bool, editable: PropTypes.bool, intl: PropTypes.object.isRequired, cacheWidth: PropTypes.func, @@ -171,18 +169,6 @@ class Audio extends React.PureComponent { color: { r: 255, g: 255, b: 255 }, }; - // hard coded in components.scss - // any way to get ::before values programatically? - - volWidth = 50; - - volOffset = 70; - - volHandleOffset = v => { - const offset = v * this.volWidth + this.volOffset; - return (offset > 110) ? 110 : offset; - } - setPlayerRef = c => { this.player = c; @@ -359,24 +345,23 @@ class Audio extends React.PureComponent { } handleMouseVolSlide = throttle(e => { - const rect = this.volume.getBoundingClientRect(); - const x = (e.clientX - rect.left) / this.volWidth; // x position within the element. + const { x } = getPointerPosition(this.volume, e); if(!isNaN(x)) { - let slideamt = x; - - if (x > 1) { - slideamt = 1; - } else if(x < 0) { - slideamt = 0; - } - - this.setState({ volume: slideamt }, () => { - this.audio.volume = slideamt; + this.setState({ volume: x }, () => { + this.audio.volume = x; }); } }, 60); + handleMouseEnter = () => { + this.setState({ hovered: true }); + } + + handleMouseLeave = () => { + this.setState({ hovered: false }); + } + _initAudioContext () { const context = new AudioContext(); const analyser = context.createAnalyser(); @@ -412,6 +397,24 @@ class Audio extends React.PureComponent { }); } + handleDownload = () => { + fetch(this.props.src).then(res => res.blob()).then(blob => { + const element = document.createElement('a'); + const objectURL = URL.createObjectURL(blob); + + element.setAttribute('href', objectURL); + element.setAttribute('download', fileNameFromURL(this.props.src)); + + document.body.appendChild(element); + element.click(); + document.body.removeChild(element); + + URL.revokeObjectURL(objectURL); + }).catch(err => { + console.error(err); + }); + } + _renderCanvas () { requestAnimationFrame(() => { this._clear(); @@ -575,13 +578,10 @@ class Audio extends React.PureComponent { render () { const { src, intl, alt, editable } = this.props; const { paused, muted, volume, currentTime, duration, buffer, darkText, dragging } = this.state; - - const volumeWidth = muted ? 0 : volume * this.volWidth; - const volumeHandleLoc = muted ? this.volHandleOffset(0) : this.volHandleOffset(volume); - const progress = (currentTime / duration) * 100; + const progress = (currentTime / duration) * 100; return ( -
+