react中使用Ace Editor編輯器

安裝插件

"ace-builds": "^1.4.12",
"react-ace": "^9.4.0",
"sql-formatter": "^4.0.2"

使用

import React, { Component } from 'react'
import { Button } from 'antd';
import AceEditor from 'react-ace';
// js編輯器插件中實現(xiàn)sql格式化 sql-formatter
import { format } from 'sql-formatter';
import 'ace-builds/src-noconflict/mode-sql'; // sql模式的包
import 'ace-builds/src-noconflict/mode-mysql';// mysql模式的包
import 'ace-builds/src-noconflict/theme-xcode';// xcode,(亮白)的主題樣式
import "ace-builds/src-noconflict/theme-twilight";// twilight,(暗黑)的主題樣式
//以下import的是風(fēng)格,還有好多種,可以根據(jù)自己需求導(dǎo)入
// github、tomorrow、kuioir、twilight、xcode、textmeta、terminal、solarized-light、solarized-dark
import 'ace-builds/src-noconflict/ext-language_tools'; //(編譯代碼的文件)


// console.log(format('SELECT * FROM tbl'));

export class index extends Component {
    constructor(props) {
        super(props)
    
        this.state = {
        }
    }
    
    // 美化代碼
    beautify = () => {
        this.setState({
            TextAceEditor: format(this.state.TextAceEditor)
        })
    }

        render() {
            // 增加需要自定義的代碼提示
            const completers = [
                {
                    name: 'name',
                    value: 'one',
                    score: 100,
                    meta: '阿Sir,看這里'
                },
                {
                    name: 'name',
                    value: 'two',
                    score: 100,
                    meta: '阿Sir,看這里'
                },
                {
                    name: 'name',
                    value: 'three',
                    score: 100,
                    meta: '阿Sir,看這里'
                }
            ];


            const complete = editor => {
                editor.completers.push({
                    getCompletions: function (editors, session, pos, prefix, callback) {
                        callback(null, completers);
                    }
                });
            };

            // ————————————————
            // 版權(quán)聲明:本文為CSDN博主「IT和尚」的原創(chuàng)文章,遵循CC 4.0 BY-SA版權(quán)協(xié)議,轉(zhuǎn)載請附上原文出處鏈接及本聲明。
            // 原文鏈接:https://blog.csdn.net/u013040887/article/details/103310440/



            return (
                <div>
                    <AceEditor
                        mode='mysql' // 設(shè)置編輯語言 
                        theme="xcode" // 設(shè)置主題  cobalt monokai,twilight,(暗黑),xcode,(亮白)
                        name="app_code_editor"
                        fontSize={20} // 設(shè)置字號
                        onChange={ (value) => this.setState({TextAceEditor: value}) } // 獲取輸入框的 代碼
                        value={this.state.TextAceEditor} // 
                        style={{ width: '100%', height: 500 }}
                        setOptions={{
                            enableBasicAutocompletion: true,   //啟用基本自動完成功能 不推薦使用
                            enableLiveAutocompletion: true,   //啟用實時自動完成功能 (比如:智能代碼提示)
                            enableSnippets: true,  //啟用代碼段
                            showLineNumbers: true,
                            tabSize: 2,
                            wrap: true, // 換行
                            autoScrollEditorIntoView: true, // 自動滾動編輯器視圖
                        }}
                        onLoad={complete}
                    />
                    <Button type="primary" onClick={() => this.beautify()} >美化代碼</Button>
                </div>
            )
        }
    }

    export default index
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容