From 373067f784dee77fa98b593992f8e324f9cde46d Mon Sep 17 00:00:00 2001
From: king <18310653075@163.com>
Date: 星期二, 25 十月 2022 15:15:01 +0800
Subject: [PATCH] 2022-10-25

---
 src/templates/zshare/codemirror/index.jsx |   19 ++++++++++++++++++-
 1 files changed, 18 insertions(+), 1 deletions(-)

diff --git a/src/templates/zshare/codemirror/index.jsx b/src/templates/zshare/codemirror/index.jsx
index 98b324a..149a15d 100644
--- a/src/templates/zshare/codemirror/index.jsx
+++ b/src/templates/zshare/codemirror/index.jsx
@@ -2,9 +2,10 @@
 import PropTypes from 'prop-types'
 import { is, fromJS } from 'immutable'
 import { Dropdown, Menu } from 'antd'
-import { FullscreenOutlined, FullscreenExitOutlined, FontSizeOutlined } from '@ant-design/icons'
+import { FullscreenOutlined, FullscreenExitOutlined, FontSizeOutlined, FormatPainterOutlined } from '@ant-design/icons'
 
 import {UnControlled as CodeMirror} from 'react-codemirror2'
+import sqlFormatter from '@/utils/sqlFormatter.js'
 import 'codemirror/mode/javascript/javascript'
 import 'codemirror/mode/sql/sql'
 import 'codemirror/mode/xml/xml'
@@ -104,7 +105,22 @@
     })
   }
 
+  handleFormat = () => {
+    let _sql = this.state.value
+    
+    if (!_sql) return
+    
+    _sql = sqlFormatter.format(_sql.replace(/\s{2,}/g, ' '))
+
+    this.setState({display: false, editor: null, defaultVal: _sql}, () => {
+      this.setState({display: true})
+    })
+    
+    this.props.onChange(_sql)
+  }
+
   render() {
+    const { mode } = this.props
     const { defaultVal, options, fullScreen, style, display } = this.state
     const menu = (
       <Menu>
@@ -149,6 +165,7 @@
 
     return (
       <div className="code-mirror-wrap" style={fullScreen ? style : null}>
+        {!mode && !fullScreen ? <FormatPainterOutlined onClick={this.handleFormat}/> : null}
         {!fullScreen ? <FullscreenOutlined onClick={this.fullScreenChange}/> : null}
         {fullScreen ? <FullscreenExitOutlined onClick={this.fullScreenChange}/> : null}
         {fullScreen ? <Dropdown overlay={menu} placement="bottomRight">

--
Gitblit v1.8.0