import React from 'react'
import * as Icons from 'components/Icons'
import * as antd from 'antd'
import {connect} from 'umi'
import styles from './index.less'
import { onError } from 'core/libs/errorhandler'
import { theme, getOptimalOpacityFromIMG, get_style_rule_value } from 'core/libs/style'
import { urlToBase64, imageToBase64, arrayToObject } from 'core'
import exportDataAsFile from 'core/libs/interface/export_data'
import ThemeSettingsList from 'globals/theme_settings.js'
class ThemeConfigurator extends React.Component{
componentDidMount(){
this.applyStoraged()
}
applyStoraged(){
const storaged = theme.get()
if(storaged && this.state){
storaged[this.state.key]? this.setState({ model: storaged[this.state.key]}) : onError.internal_proccess(`"Config key" or "Dispatcher" is missing`)
}
}
promiseState = async state => new Promise(resolve => this.setState(state, resolve));
handleUpdate(payload){
if(!this.state.key || !this.props.dispatch) {
return onError.internal_proccess(`"Config key" or "App/Dispatcher" is missing`)
}
if (!payload) {
payload = this.state.model
}
this.setState({ model: payload, processing: false })
this.props.dispatch({
type: 'app/updateTheme',
payload: {
key: this.state.key,
value: payload
}
});
}
handleErase(){
this.handleUpdate({})
}
handleExport(){
exportDataAsFile({data: JSON.stringify(this.state.model), type: 'text/json'})
}
}
@connect(({ app }) => ({ app }))
class DarkMode extends ThemeConfigurator{
constructor(props){
super(props),
this.state = {
key: "darkmode",
model: { active: false }
}
}
render(){
return <>
Dark Mode
Enabled
{this.promiseState(prevState => ({ model: { ...prevState.model, active: e }})).then(() => this.handleUpdate())}}
checked={this.state.model.active}
/>
>
}
}
@connect(({ app }) => ({ app }))
class Colors extends ThemeConfigurator{
constructor(props){
super(props),
this.state = {
key: "darkmode",
model: { active: false }
}
}
render(){
return <>
Dark Mode
Enabled
{this.promiseState(prevState => ({ model: { ...prevState.model, active: e }})).then(() => this.handleUpdate())}}
checked={this.state.model.active}
/>
>
}
}
@connect(({ app }) => ({ app }))
class BackgroundImage extends ThemeConfigurator{
constructor(props){
super(props),
this.state = {
key: "backgroundImage",
model: { active: false, opacity: null, src: null },
textColor: this.rgbToScheme(getComputedStyle(document.getElementById("appWrapper")).color),
overlayColor: this.rgbToScheme(getComputedStyle(document.getElementById("appWrapper")).backgroundColor),
processing: null,
customURL: '',
fileURL: null,
}
}
handleFileUpload = info => {
if (info.file.status === 'uploading') {
return this.setState({ processing: false })
}
if (info.file.status === 'done') {
this.setState({ processing: true })
imageToBase64(info.file.originFileObj, fileURL => {
this.setState({ fileURL: fileURL })
this.proccessBackground(fileURL)
})
}
}
handleCustomURL(url){
this.setState({ processing: true, fileURL: url })
urlToBase64(url, fileURL => {
this.proccessBackground(fileURL)
})
}
proccessBackground(data){
getOptimalOpacityFromIMG({textColor: this.state.textColor, overlayColor: this.state.overlayColor, img: data}, (res) => {
this.handleUpdate({active: true, src: this.state.fileURL, opacity: res})
})
}
schemeToRGB(values){
const scheme = values? values : { r: '0', g: '0', b: '0' }
const r = scheme.r || '0'
const g = scheme.g || '0'
const b = scheme.b || '0'
return `rgb(${r}, ${g}, ${b})`
}
rgbToScheme(rgb){
const values = rgb.replace(/[^\d,]/g, '').split(',');
return {r: values[0], g: values[1], b: values[2]}
}
render(){
const PreviewModel = () => {
return(
Preview
{ this.state.model.src?
Sample text
Sample text
Sample text
Sample text
Some text here
Some text here
:
No Background
}
)
}
return (
<>
Background Image
Enabled
{this.promiseState(prevState => ({ model: { ...prevState.model, active: e }})).then(() => this.handleUpdate())}}
checked={this.state.model.active}
/>
Opacity
{this.setState(prevState => ({model: {...prevState.model, opacity: e/100}}))}} onAfterChange={() => this.handleUpdate()} value={this.state.model.opacity*100} />
Export Code
this.handleExport()}> Export
Import Code
null}> Import
Erase
this.handleErase()} okText="Yes" cancelText="No">
Delete
Upload
Upload from your files
} />
Or
Upload from URL
this.handleCustomURL(this.state.customURL)} onChange={e => this.setState({ customURL: e.target.value })} value={this.state.customURL} placeholder="http://example.com/my_coolest_image.jpg" />
{this.state.processing?
Processing image ...
: null}
{this.state.params? JSON.stringify(this.state.params) : null}
{/* Unsplash
this.search(value)} />
(
this.returnString(item.urls.full)} src={item.urls.small} /> ) }/> */}
>
)
}
}
@connect(({ app }) => ({ app }))
export default class ThemeSettings extends React.Component{
state = {
helper_fragment: null
}
render(){
const idToComponent = {
backgroundImage: ,
darkmode: ,
color: ,
}
const handleClick = (key) => key? this.setState({helper_fragment: idToComponent[key]}) : null
const isActive = (key) => { return key? key.active : false }
return(
(
handleClick(item.id)}>
{item.icon}{item.title} {isActive(arrayToObject(this.props.app.app_theme)[item.id])? "Enabled" : "Disabled"}
{item.description}
)}
/>
this.setState({ helper_fragment: null })}
visible={this.state.helper_fragment? true : false}
>
{this.state.helper_fragment}
)
}
}