import React from 'react'
import * as Icons from 'components/Icons'
import * as antd from 'antd'
import themeSettings from 'globals/theme_settings'
import {connect} from 'umi'
import styles from './index.less'
import { SketchPicker } from 'react-color';
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'
class BackgroundColor extends React.Component{
state = {
selected: "#fff"
}
sendChanges(){
this.props.changeColor(this.state.selected)
}
selectColor = (color) =>{
this.setState({selected: color.hex})
}
render(){
return <>
this.sendChanges()}> Change
>
}
}
@connect(({ app }) => ({ app }))
class DarkMode extends React.Component{
state = {
model: { active: false, autoTime: '' }
}
handleUpdate(payload){
if (!payload) {
payload = this.state.model
}
this.setState({ model: payload, processing: false })
this.props.dispatch({
type: 'app/updateTheme',
payload: {
key: 'darkmode',
value: payload
}
});
}
render(){
const promiseState = async state => new Promise(resolve => this.setState(state, resolve));
return <>
Dark Mode
Enabled
{promiseState(prevState => ({ model: { ...prevState.model, active: e }})).then(() => this.handleUpdate())}}
checked={this.state.model.active}
/>
>
}
}
@connect(({ app }) => ({ app }))
class BackgroundImage extends React.Component{
state = {
customURL: '',
fileURL: null,
processing: null,
model: { active: false, opacity: null, src: 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)
})
}
handleUpdate(payload){
if (!payload) {
payload = this.state.model
}
this.setState({ model: payload, processing: false })
this.props.dispatch({
type: 'app/updateTheme',
payload: {
key: 'backgroundImage',
value: payload
}
});
}
handleErase(){
this.handleUpdate({})
}
handleExport(){
exportDataAsFile({data: JSON.stringify(this.state.model), type: 'text/json'})
}
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 || { 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]}
}
componentDidMount(){
const storaged = theme.get()
if(storaged){
this.setState({ model: storaged["backgroundImage"] })
}
let textColor = this.rgbToScheme(get_style_rule_value('#root', 'color'))
let overlayColor = this.rgbToScheme(get_style_rule_value('#root', 'backgroundColor'))
this.setState({
textColor: textColor,
overlayColor: overlayColor
})
}
render(){
const promiseState = async state => new Promise(resolve => this.setState(state, resolve));
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
{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_visible: false,
helper_fragment: null,
}
helper = {
open: (e) => {
this.setState({ helper_visible: true, helper_fragment: e })
},
close: () => {
this.setState({ helper_visible: false, helper_fragment: null })
}
}
render(){
const settingClick = {
backgroundImage: () => this.helper.open(),
overlay: () => this.helper.open() ,
darkmode: () => this.helper.open()
}
const isActive = (key) => {
return key? key.active : false
}
return(
Theme
(
{item.icon}{item.title} {isActive(arrayToObject(this.props.app.app_theme)[item.id])? "Enabled" : "Disabled"}
{item.description}
)}
/>
{this.state.helper_fragment}
)
}
}