From cf62796bdc550a1fd35b15dd7bd39ba58cac82aa Mon Sep 17 00:00:00 2001 From: king <18310653075@163.com> Date: 星期二, 01 十一月 2022 18:19:37 +0800 Subject: [PATCH] 2022-11-01 --- src/templates/zshare/codemirror/index.jsx | 42 +++++++++++++++++++++++++++++++++--------- 1 files changed, 33 insertions(+), 9 deletions(-) diff --git a/src/templates/zshare/codemirror/index.jsx b/src/templates/zshare/codemirror/index.jsx index a7ee8d3..853e055 100644 --- a/src/templates/zshare/codemirror/index.jsx +++ b/src/templates/zshare/codemirror/index.jsx @@ -27,7 +27,6 @@ } state = { - editor: null, // code瀵硅薄 defaultVal: '', // 鍒濆鍊� value: '', // 瀹炴椂鍐呭 options: null, // mode : text/xml, text/css, text/javascript銆乼ext/x-mysql ; theme : cobalt - 榛戝簳 @@ -35,6 +34,8 @@ style: {fontSize: '18px', lineHeight: '32px'}, display: true } + + editor = null UNSAFE_componentWillMount () { let options = { @@ -56,15 +57,15 @@ } UNSAFE_componentWillReceiveProps (nextProps) { - const { value, editor } = this.state + const { value } = this.state if (value !== nextProps.value) { this.setState({ value: nextProps.value || '' }) - if (editor && editor.setValue) { - editor.setValue(nextProps.value || '') + if (this.editor && this.editor.setValue) { + this.editor.setValue(nextProps.value || '') } else { this.setState({ defaultVal: nextProps.value || '' @@ -74,7 +75,7 @@ } shouldComponentUpdate (nextProps, nextState) { - return !is(fromJS({...this.state, value: '', editor: ''}), fromJS({...nextState, value: '', editor: ''})) + return !is(fromJS({...this.state, value: ''}), fromJS({...nextState, value: ''})) } fullScreenChange = () => { @@ -100,7 +101,7 @@ } // 鍒囨崲瀛椾綋澶у皬锛屽埛鏂扮紪杈戝櫒绐楀彛锛岃В鍐宠皟鏁村悗鐨勯�夋嫨鍖哄煙閿欎贡闂 - this.setState({style: _style, display: false, editor: null, defaultVal: this.state.value}, () => { + this.setState({style: _style, display: false, defaultVal: this.state.value}, () => { this.setState({display: true}) }) } @@ -109,13 +110,35 @@ 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, '*/') - this.setState({display: false, editor: null, defaultVal: _sql}, () => { + arr.forEach(item => { + _sql = _sql.replace(item.id, item.value) + }) + + this.setState({display: false, defaultVal: _sql}, () => { this.setState({display: true}) }) @@ -178,8 +201,9 @@ className="code-mirror-area" value={defaultVal} options={options} + ref={(ref) => { this.editor = ref }} onChange={(editor, data, value) => { - this.setState({editor, value}) + this.setState({value}) this.props.onChange(value) }} /> : null} -- Gitblit v1.8.0