king
2025-02-19 1aa5654a0b51bb82948fff8bed77b166f25f11ea
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
import React, {Component} from 'react'
import PropTypes from 'prop-types'
import { is, fromJS } from 'immutable'
import { Dropdown, Menu, Modal, notification } from 'antd'
import { FullscreenOutlined, FullscreenExitOutlined, FontSizeOutlined, FormatPainterOutlined, SwapOutlined } from '@ant-design/icons'
 
import {UnControlled as CodeMirror} from 'react-codemirror2'
import sqlFormatter from '@/utils/sqlFormatter.js'
import ReplaceForm from './replaceform'
import 'codemirror/mode/javascript/javascript'
import 'codemirror/mode/sql/sql'
import 'codemirror/mode/xml/xml'
import 'codemirror/mode/css/css'
import 'codemirror/addon/display/fullscreen.js'
 
import 'codemirror/addon/display/fullscreen.css'
import 'codemirror/lib/codemirror.css'
import 'codemirror/theme/cobalt.css'
 
import './index.scss'
 
class CodeMirrorComponent extends Component {
  static propTpyes = {
    value: PropTypes.string, // 内容
    mode: PropTypes.any,     // 可选,语言模式,默认为sql
    theme: PropTypes.any,    // 可选,主题样式
    func: PropTypes.any,     // 编辑存储过程
    onChange: PropTypes.func // 内容变化时回调
  }
 
  state = {
    defaultVal: '', // 初始值
    value: '',      // 实时内容
    options: null,  // mode : text/xml, text/css, text/javascript、text/x-mysql ; theme : cobalt - 黑底
    fullScreen: false,
    style: {fontSize: '18px', lineHeight: '32px'},
    display: true,
    visible: false
  }
 
  editor = null
 
  UNSAFE_componentWillMount () {
    const { func } = this.props
 
    let options = {
      lineNumbers: true,
      lineWrapping: true,
      fullScreen: false
    }
 
    options.mode = this.props.mode || 'text/x-mysql'
    if (this.props.theme) {
      options.theme = this.props.theme
    }
 
    this.setState({
      style: func ? {fontSize: '14px', lineHeight: '25px'} : {fontSize: '18px', lineHeight: '32px'},
      value: this.props.value || '',
      defaultVal: this.props.value || '',
      options
    })
  }
 
  UNSAFE_componentWillReceiveProps (nextProps) {
    const { value } = this.state
 
    if (value !== nextProps.value) {
      this.setState({
        value: nextProps.value || ''
      })
 
      if (this.editor && this.editor.setValue) {
        this.editor.setValue(nextProps.value || '')
      } else {
        this.setState({
          defaultVal: nextProps.value || ''
        })
      }
    }
  }
 
  shouldComponentUpdate (nextProps, nextState) {
    return !is(fromJS({...this.state, value: ''}), fromJS({...nextState, value: ''}))
  }
 
  fullScreenChange = () => {
    const { options, fullScreen } = this.state
 
    this.setState({options: {...options, fullScreen: !fullScreen}, fullScreen: !fullScreen})
  }
 
  changeSize = (size) => {
    let _style = null
    if (size === 14) {
      _style = {fontSize: '14px', lineHeight: '25px'}
    } else if (size === 16) {
      _style = {fontSize: '16px', lineHeight: '28px'}
    } else if (size === 18) {
      _style = {fontSize: '18px', lineHeight: '32px'}
    } else if (size === 20) {
      _style = {fontSize: '20px', lineHeight: '34px'}
    } else if (size === 22) {
      _style = {fontSize: '22px', lineHeight: '36px'}
    } else if (size === 24) {
      _style = {fontSize: '24px', lineHeight: '40px'}
    }
 
    // 切换字体大小,刷新编辑器窗口,解决调整后的选择区域错乱问题
    this.setState({style: _style, display: false, defaultVal: this.state.value}, () => {
      this.setState({display: true})
    })
  }
 
  handleFormat = () => {
    let _sql = this.state.value
    
    if (!_sql) return
 
    let getuuid = () => {
      let uuid = []
      let _options = '0123456789abcdefghigklmnopqrstuv'
      for (let i = 0; i < 19; i++) {
        uuid.push(_options.substr(Math.floor(Math.random() * 0x20), 1))
      }
      return '\'' + uuid.join('') + '\''
    }
 
    let arr = []
    _sql = _sql.replace(/@[0-9a-zA-Z_]+@/g, (word) => {
      let uuid = getuuid()
      arr.push({id: uuid, value: word})
      return uuid
    })
 
    _sql = sqlFormatter.format(_sql.replace(/\s{2,}/g, ' '))
    // _sql = _sql.replace(/case\n\s+when\s/ig, (word) => {
    //   return word.replace(/case/, '').replace(/when/, 'case when')
    // })
    
    _sql = _sql.replace(/\$\s\*\s\//g, '$*/').replace(/\*\s\//g, '*/')
 
    arr.forEach(item => {
      _sql = _sql.replace(item.id, item.value)
    })
 
    _sql = _sql.replace(/,\n\s*/g, ',')
 
    this.setState({display: false, defaultVal: _sql}, () => {
      this.setState({display: true})
    })
    
    this.props.onChange(_sql)
  }
 
  submit = () => {
    let _sql = this.state.value
 
    this.ReplaceRef.handleConfirm().then(res => {
      let reg = new RegExp(res.origin, 'ig')
      let times = _sql.match(reg)
 
      if (!times) {
        notification.warning({
          top: 92,
          message: '未查询到字符《' + res.origin + '》。',
          duration: 5
        })
        return
      }
 
      localStorage.setItem(window.GLOB.sysSign + 'sql_char', JSON.stringify([res.origin, res.value]))
 
      _sql = _sql.replace(reg, res.value)
 
      this.setState({display: false, defaultVal: _sql}, () => {
        this.setState({display: true})
      })
      
      this.props.onChange(_sql)
 
      notification.success({
        top: 92,
        message: `共替换${times.length}处字符《${res.origin}》。`,
        duration: 5
      })
      this.setState({ visible: false })
    })
  }
 
  render() {
    const { mode, func } = this.props
    const { defaultVal, options, fullScreen, style, display, visible } = this.state
    const menu = (
      <Menu>
        <Menu.Item
          style={!style || style.fontSize === '14px' ? {backgroundColor: '#bae7ff'} : ''}
          onClick={() => {this.changeSize(14)}}
        >
          <span style={{padding: '0 10px 0px 5px'}}>14px</span>
        </Menu.Item>
        <Menu.Item
          style={style && style.fontSize === '16px' ? {backgroundColor: '#bae7ff'} : ''}
          onClick={() => {this.changeSize(16)}}
        >
          <span style={{padding: '0 10px 0px 5px'}}>16px</span>
        </Menu.Item>
        <Menu.Item
          style={style && style.fontSize === '18px' ? {backgroundColor: '#bae7ff'} : ''}
          onClick={() => {this.changeSize(18)}}
        >
          <span style={{padding: '0 10px 0px 5px'}}>18px</span>
        </Menu.Item>
        <Menu.Item
          style={style && style.fontSize === '20px' ? {backgroundColor: '#bae7ff'} : ''}
          onClick={() => {this.changeSize(20)}}
        >
          <span style={{padding: '0 10px 0px 5px'}}>20px</span>
        </Menu.Item>
        <Menu.Item
          style={style && style.fontSize === '22px' ? {backgroundColor: '#bae7ff'} : ''}
          onClick={() => {this.changeSize(22)}}
        >
          <span style={{padding: '0 10px 0px 5px'}}>22px</span>
        </Menu.Item>
        <Menu.Item
          style={style && style.fontSize === '24px' ? {backgroundColor: '#bae7ff'} : ''}
          onClick={() => {this.changeSize(24)}}
        >
          <span style={{padding: '0 10px 0px 5px'}}>24px</span>
        </Menu.Item>
      </Menu>
    )
 
    return (
      <>
        <div className={'code-mirror-wrap ' + (fullScreen ? 'mk-fullscreen' : '')} style={fullScreen || func ? style : null}>
          {!mode ? <FormatPainterOutlined title="格式化" onClick={this.handleFormat}/> : null}
          <FullscreenOutlined title="最大化" onClick={this.fullScreenChange}/>
          <FullscreenExitOutlined title="向下还原" onClick={this.fullScreenChange}/>
          {fullScreen || func ? <Dropdown overlayClassName="mk-mirror-font" overlay={menu} placement="bottomRight">
            <FontSizeOutlined />
          </Dropdown> : null}
          {!mode ? <SwapOutlined title="字符替换" onClick={() => this.setState({visible: true})}/> : null}
          {display ? <CodeMirror
            className="code-mirror-area"
            value={defaultVal}
            options={options}
            editorDidMount={editor => { this.editor = editor }}
            onChange={(editor, data, value) => {
              this.setState({value})
              this.props.onChange(value)
            }}
          /> : null}
        </div>
        <Modal
          title="字符替换"
          visible={visible}
          width={500}
          maskClosable={false}
          okText="替换"
          onOk={this.submit}
          onCancel={() => {this.setState({ visible: false })}}
          destroyOnClose
        >
          <ReplaceForm inputSubmit={this.submit} wrappedComponentRef={(inst) => this.ReplaceRef = inst}/>
        </Modal>
      </>
    )
  }
}
 
export default CodeMirrorComponent