king
2023-06-22 79e4981aa6cc9354276fc54cdf6d14eb08ab7fee
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
import React, {Component} from 'react'
import PropTypes from 'prop-types'
import { SketchPicker } from 'react-color'
import { Popover } from 'antd'
import { CloseCircleFilled } from '@ant-design/icons'
 
import './index.scss'
 
const presetColors = [
  '#1890ff', '#f5222d', '#fa541c', '#fa8c16', '#faad14', '#fadb14', '#a0d911', '#52c41a', '#13c2c2', '#2f54eb', '#722ed1',
  '#eb2f96', '#aeb303', '#c32539', '#1d3661', '#ffd591', '#ffe58f', '#fffb8f', '#eaff8f', '#b7eb8f', '#87e8de', '#91d5ff',
  '#adc6ff', '#EBE9E9', '#d9d9d9', 'rgba(0, 0, 0, 0.65)', 'rgba(0, 0, 0, 0.85)', '#000000', '#ffffff', 'transparent'
]
const _href = window.location.href.split('#')[0]
 
class ColorSketch extends Component {
  static propTpyes = {
    defaultValue: PropTypes.any,
    value: PropTypes.any,
    onChange: PropTypes.func
  }
  state = {
    color: '',
    initVal: '',
    colors: [],
    allowClear: false
  }
 
  UNSAFE_componentWillMount () {
    const { defaultValue, value, allowClear } = this.props
    let initVal = ''
 
    if (this.props['data-__meta']) {
      initVal = this.props['data-__meta'].initialValue
    } else if (defaultValue) {
      initVal = defaultValue
    } else if (value) {
      initVal = value
    }
 
    let _colors = sessionStorage.getItem('app_colors')
    let colors = JSON.parse(JSON.stringify(presetColors))
    let normal_colors = localStorage.getItem(_href + 'normal_colors')
 
    if (normal_colors) {
      try {
        normal_colors = JSON.parse(normal_colors)
      } catch (e) {
        normal_colors = []
      }
      
      normal_colors.forEach(item => {
        colors.push({color: item, title: '常用色:' + item})
      })
    } else {
      normal_colors = []
    }
 
    if (_colors) {
      try {
        _colors = JSON.parse(_colors)
      } catch (e) {
        _colors = []
      }
      
      _colors.forEach(item => {
        if (normal_colors.includes(item.linkurl)) return
        
        colors.push({color: item.linkurl, title: '系统色:' + item.linkurl})
      })
    }
 
    if (colors.length < 40) {
      for (let i = colors.length; i < 40; i++) {
        colors.push({color: 'transparent', title: '' + i})
      }
    }
 
    this.setState({color: initVal, initVal, allowClear: allowClear === true, colors})
  }
 
  handleChange = (color) => {
    let _color = `rgba(${ color.rgb.r }, ${ color.rgb.g }, ${ color.rgb.b }, ${ color.rgb.a })`
    let _hex = color.hex === 'transparent' ? '#ffffff' : color.hex
 
    this.setState({ color: _color }, () => {
      this.props.onChange && this.props.onChange(_color, _hex)
    })
  }
 
  clear = () => {
    this.setState({ color: '' }, () => {
      this.props.onChange && this.props.onChange('')
    })
  }
 
  UNSAFE_componentWillReceiveProps(nextProps) {
    if (nextProps.value !== undefined && nextProps.value !== this.state.color) {
      this.setState({ color: nextProps.value })
    }
  }
 
  onVisibleChange = (status) => {
    const { initVal, color } = this.state
    
    if (!status && color && color !== initVal && color !== 'rgba(0, 0, 0, 0)' && color !== 'transparent' && !/rgba\(\d+,\s*\d+,\s*\d+,\s*0\)/.test(color)) {
      let normal_colors = localStorage.getItem(_href + 'normal_colors')
 
      if (normal_colors) {
        try {
          normal_colors = JSON.parse(normal_colors)
        } catch (e) {
          normal_colors = []
        }
      } else {
        normal_colors = []
      }
 
      normal_colors.unshift(color)
      normal_colors = Array.from(new Set(normal_colors))
 
      if (normal_colors.length > 10) {
        normal_colors.length = 10
      }
 
      localStorage.setItem(_href + 'normal_colors', JSON.stringify(normal_colors))
    }
  }
 
  
 
  render() {
    const { color, allowClear, colors } = this.state
    return (
      <div className="color-sketch-block">
        <Popover content={
          <SketchPicker color={ color } presetColors={colors} onChange={ this.handleChange } />
        } overlayClassName="color-sketch-popover" placement="bottomRight" title="" trigger="click" onVisibleChange={this.onVisibleChange}>
          <div className="color-sketch-block-box">
            <div className="color-sketch-block-inner" style={ {background: color} }></div>
          </div>
        </Popover>
        <div className="color-sketch-value">{color || <span style={{color: '#ff4d4f'}}>无</span>}{allowClear && color ? <CloseCircleFilled onClick={this.clear}/> : null}</div>
      </div>
    )
  }
}
 
export default ColorSketch