king
2022-12-09 cb9ade2afd2a367ad767bc605ab7086c695dd010
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
150
151
152
import React, {Component} from 'react'
import PropTypes from 'prop-types'
import { is, fromJS } from 'immutable'
import { Modal, Row, Col, Button, Tabs, notification } from 'antd'
import { CloseCircleFilled, RedoOutlined } from '@ant-design/icons'
 
import Api from '@/api'
import { minkeIconSystem } from '@/utils/option.js'
import MkIcon from '@/components/mk-icon'
import './index.scss'
 
const { TabPane } = Tabs
// ['direction', 'edit', 'normal', 'data', 'hint']
 
class MkEditIcon extends Component {
  static propTpyes = {
    onChange: PropTypes.func
  }
 
  state = {
    selectIcon: '',
    allowClear: false,
    icons: [],
    cusicons: [],
    visible: false
  }
 
  UNSAFE_componentWillMount () {
    const { options } = this.props
    let val = ''
    if (this.props['data-__meta']) {
      val = this.props['data-__meta'].initialValue || ''
    }
 
    let icons = []
 
    if (options) {
      options.forEach(item => {
        icons.push(...minkeIconSystem[item])
      })
    } else {
      icons = [...minkeIconSystem.direction, ...minkeIconSystem.edit, ...minkeIconSystem.normal, ...minkeIconSystem.data, ...minkeIconSystem.hint]
    }
 
    this.setState({selectIcon: val, allowClear: this.props.allowClear === true, icons})
  }
 
  componentDidMount() {
    if (!window.GLOB.designView) {
      if (sessionStorage.getItem('systemIcons')) {
        this.setState({cusicons: JSON.parse(sessionStorage.getItem('systemIcons'))})
      } else {
        this.getIcons()
      }
    }
  }
 
  shouldComponentUpdate (nextProps, nextState) {
    return !is(fromJS(this.state), fromJS(nextState))
  }
 
  checkIcon = (val) => {
    this.setState({selectIcon: val, visible: false})
    this.props.onChange(val)
  }
 
  getIcons = () => {
    Api.getSystemConfig({ func: 's_get_icons' }).then(res => {
      if (!res.status) {
        notification.warning({
          top: 92,
          message: res.message,
          duration: 5
        })
        sessionStorage.setItem('systemIcons', JSON.stringify([]))
        return
      } else if (!res.data) {
        return
      }
 
      let icons = res.data.map(item => {
        item.icon_svg = window.decodeURIComponent(window.atob(item.icon_svg))
        return item
      })
 
      sessionStorage.setItem('systemIcons', JSON.stringify(icons))
 
      this.setState({cusicons: icons})
    })
  }
 
  trigger = () => {
    const { selectIcon, cusicons } = this.state
 
    this.setState({visible: true})
 
    if (cusicons.length > 0 && selectIcon && /<svg/.test(selectIcon)) {
      setTimeout(() => {
        let node = document.getElementById('mk-custom-tab')
        node && node.click()
      }, 200)
    }
  }
 
  render() {
    const { selectIcon, visible, icons, allowClear, cusicons } = this.state
 
    return (
      <div className="mk-icon-box">
        {selectIcon ? <MkIcon type={selectIcon}/> : <span style={{color: '#bcbcbc'}}>请选择</span>}
        <MkIcon className="trigger" onClick={this.trigger} type="swap"/>
        {allowClear && selectIcon ? <CloseCircleFilled className="close" onClick={() => this.checkIcon('')}/> : null}
        <Modal
          wrapClassName="mk-pop-modal mk-icon-wrap"
          // title={'图标选择'}
          visible={visible}
          width={800}
          maskClosable={false}
          onCancel={() => { this.setState({ visible: false }) }}
          footer={[
            <Button key="close" onClick={() => { this.setState({ visible: false }) }}>关闭</Button>
          ]}
          destroyOnClose
        >
          <Tabs className={cusicons.length > 0 ? 'dubble-tabs' : ''}>
            <TabPane tab="系统" key="setting">
              <Row>
                {icons.map(icon => <Col className={icon === selectIcon ? 'active' : ''} key={icon} span={4}>
                  <MkIcon onClick={() => this.checkIcon(icon)} type={icon} />
                </Col>)}
              </Row>
            </TabPane>
            <TabPane tab={
              <span className="tab-control" id="mk-custom-tab">
                <RedoOutlined onClick={(e) => {this.getIcons()}}/>
                自定义
              </span>
            } key="scripts">
              <Row>
                {cusicons.map(icon => <Col className={icon.icon_svg === selectIcon ? 'active' : ''} key={icon.id} span={4} onClick={() => this.checkIcon(icon.icon_svg)}>
                  <MkIcon type={icon.icon_svg} />
                </Col>)}
              </Row>
            </TabPane>
          </Tabs>
        </Modal>
      </div>
    )
  }
}
 
export default MkEditIcon