diff --git a/src/components/modals/settings/sections/BackgroundSettings.jsx b/src/components/modals/settings/sections/BackgroundSettings.jsx index 67a37c7d..9a234c51 100644 --- a/src/components/modals/settings/sections/BackgroundSettings.jsx +++ b/src/components/modals/settings/sections/BackgroundSettings.jsx @@ -6,7 +6,8 @@ export default class BackgroundSettings extends React.PureComponent { super(...args); this.state = { blur: 0, - brightness: 100 + brightness: 100, + gradientSettings: '' }; } @@ -14,7 +15,7 @@ export default class BackgroundSettings extends React.PureComponent { switch (key) { case 'customBackgroundColour': localStorage.setItem('customBackgroundColour', ''); - document.getElementById('customBackgroundHex').textContent = 'Disabled'; + this.setState({ gradientSettings: '' }); break; case 'customBackground': document.getElementById('customBackground').value = ''; break; case 'blur': @@ -30,72 +31,159 @@ export default class BackgroundSettings extends React.PureComponent { toast(this.props.toastLanguage.reset); } - componentDidMount() { - document.getElementById('bg-input').onchange = (e) => { - const reader = new FileReader(); - const file = e.target.files[0]; + componentDidMount() { + document.getElementById('bg-input').onchange = (e) => { + const reader = new FileReader(); + const file = e.target.files[0]; - if (file.size > 2000000) return toast('File is over 2MB', '#ff0000', '#ffffff'); + if (file.size > 2000000) return toast('File is over 2MB', '#ff0000', '#ffffff'); - reader.addEventListener('load', (e) => { - localStorage.setItem('customBackground', e.target.result); - document.getElementById('customBackground').src = e.target.result; - document.getElementById('customBackground').value = e.target.result; - }); + reader.addEventListener('load', (e) => { + localStorage.setItem('customBackground', e.target.result); + document.getElementById('customBackground').src = e.target.result; + document.getElementById('customBackground').value = e.target.result; + }); - reader.readAsDataURL(file); - }; + reader.readAsDataURL(file); + }; - const hex = localStorage.getItem('customBackgroundColour'); - if (hex !== '') { - document.getElementById('customBackgroundColour').value = hex; - document.getElementById('customBackgroundHex').innerText = hex; - } else document.getElementById('customBackgroundHex').innerText = 'Disabled'; - - this.setState({ - blur: localStorage.getItem('blur'), - brightness: localStorage.getItem('brightness') - }) - - document.getElementById('customBackground').value = localStorage.getItem('customBackground'); - document.getElementById('backgroundAPI').value = localStorage.getItem('backgroundAPI'); + const hex = localStorage.getItem('customBackgroundColour'); + let gradientSettings = ''; + if (hex !== '') { + try { + gradientSettings = JSON.parse(hex); + } catch (e) { + //Disregard exception. + } } + this.setState({ + blur: localStorage.getItem('blur'), + brightness: localStorage.getItem('brightness'), + gradientSettings + }) + + document.getElementById('customBackground').value = localStorage.getItem('customBackground'); + document.getElementById('backgroundAPI').value = localStorage.getItem('backgroundAPI'); + } + + onGradientChange = (event, index) => { + const newValue = event.target.value; + const name = event.target.name; + this.setState((s) => { + const newState = { + gradientSettings: { + ...s.gradientSettings, + gradient: s.gradientSettings.gradient.map((g, i) => i === index ? { ...g, [name]: newValue } : g) + } + }; + return newState; + }); + } + + pickFirstColour = (event) => { + const value = event.target.value; + this.setState({ gradientSettings: { "angle": "180", "gradient": [{ "colour": value, "stop": 0 }], "type": "linear" } }); + } + + addColour = () => { + this.setState((s) => { + const [lastGradient, ...initGradients] = s.gradientSettings.gradient.reverse(); + const newState = { + gradientSettings: { + ...s.gradientSettings, + gradient: [...initGradients, lastGradient, { ...lastGradient, stop: 100 }].sort((a, b) => (a.stop > b.stop) ? 1 : -1) + } + }; + return newState; + }); + } + + removeColour = (index) => { + this.setState((s) => { + const newState = { + gradientSettings: { + ...s.gradientSettings, + gradient: s.gradientSettings.gradient.filter((g, i) => i !== index) + } + }; + return newState; + }); + } + + currentGradientSettings = () => { + if (typeof this.state.gradientSettings === 'object') { + const clampNumber = (num, a, b) => Math.max(Math.min(num, Math.max(a, b)), Math.min(a, b)); + return JSON.stringify({ + ...this.state.gradientSettings, + gradient: [...this.state.gradientSettings.gradient.map(g => { return { ...g, stop: clampNumber(+g.stop, 0, 100) } })].sort((a, b) => (a.stop > b.stop) ? 1 : -1) + }); + } + return "Disabled"; + } + render() { - return ( + let colourSettings; + if (typeof this.state.gradientSettings === 'object') { + const gradientInputs = this.state.gradientSettings.gradient.map((g, i) => { + const gradientHasMoreThanOneColour = this.state.gradientSettings.gradient.length > 1; + return ( +
{this.props.language.background.blur} ({this.state.blur}%) this.resetItem('blur')}>{this.props.language.reset}
- this.setState({ blur: event.target.value })} /> -{this.props.language.background.brightness} ({this.state.brightness}%) this.resetItem('brightness')}>{this.props.language.reset}
- this.setState({ brightness: event.target.value })} /> -{this.props.language.background.customURL} this.resetItem('customBackground')}>{this.props.language.reset}
- -{this.props.language.background.custombackground} this.resetItem('customBackground')}>{this.props.language.reset}
- - -{this.props.language.background.customcolour} this.resetItem('customBackgroundColour')}>{this.props.language.reset}
- document.getElementById('customBackgroundHex').innerText = document.getElementById('customBackgroundColour').value}> - -{this.props.language.background.blur} ({this.state.blur}%) this.resetItem('blur')}>{this.props.language.reset}
+ this.setState({ blur: event.target.value })} /> +{this.props.language.background.brightness} ({this.state.brightness}%) this.resetItem('brightness')}>{this.props.language.reset}
+ this.setState({ brightness: event.target.value })} /> +{this.props.language.background.customURL} this.resetItem('customBackground')}>{this.props.language.reset}
+ +{this.props.language.background.custombackground} this.resetItem('customBackground')}>{this.props.language.reset}
+ + +{this.props.language.background.customcolour} this.resetItem('customBackgroundColour')}>{this.props.language.reset}
+ + {colourSettings} +