AceEditor使用

1. 安装步骤

安装 react-ace

react-ace 是一个 React 组件库,允许你在 React 项目中使用 Ace Editor。

js 复制代码
npm install react-ace --save
yarn add react-ace --save

安装 ace-builds

ace-builds 包含 Ace Editor 的核心文件和模块。需要它来提供编辑功能。

js 复制代码
npm install ace-builds --save
yarn add ace-builds --save

2.使用 AceEditor

安装完这些包后,你可以在项目中进行引入并使用。

js 复制代码
import React, { useEffect, useRef } from 'react'
import AceEditor from 'react-ace'
//支持json格式
import 'ace-builds/src-noconflict/mode-json'
//支持java格式
import 'ace-builds/src-noconflict/mode-java'
////支持各种样式
import 'ace-builds/src-noconflict/theme-monokai'
// import 'ace-builds/src-noconflict/theme-twilight'
// import 'ace-builds/src-noconflict/theme-solarized_dark'
// import 'ace-builds/src-noconflict/theme-dracula'
// import 'ace-builds/src-noconflict/theme-chaos'
// import 'ace-builds/src-noconflict/theme-terminal'
// import 'ace-builds/src-noconflict/theme-gruvbox'
// 导入搜索框扩展
import 'ace-builds/src-noconflict/ext-searchbox'
//引入自定义样式
import '@/pages/lark/styles/aceEditor.scss'
const CustomeCanvas = observer((props: any) => {

    const aceEditorRef = useRef<any>()
    useEffect(() => {
    if (aceEditorRef && aceEditorRef.current) {
      // 加载aceEditor搜索
      const editor = aceEditorRef.current.editor
      editor.commands.addCommand({
        name: 'showSearchBox',
        bindKey: { win: 'Ctrl-F', mac: 'Command-F' },
        exec: () => {
          editor.execCommand('find')
        },
      })
    }
    }, [aceEditorRef, aceEditorRef.current])
    return (
        <AceEditor
          ref={aceEditorRef}
          mode="json"
          theme="monokai"
          name="code_editor"
          onChange={onChangeJsonParams}
          fontSize={14}
          showPrintMargin
          showGutter
          highlightActiveLine={false}
          value={jsonParams}
          wrapEnabled // 启用代码换行
          // readOnly    //是否支持编辑
          setOptions={{
            enableBasicAutocompletion: true,
            enableLiveAutocompletion: true,
            enableSnippets: true,
            showLineNumbers: true,
            tabSize: 2, // 空格
            printMargin: false, // 隐藏中间的边距线
          }}
          style={{ width: '100%', height: '830px' }}
        />
    )
})
export default CustomeCanvas

自定义样式

js 复制代码
.ace_editor .ace_line {
  line-height: 1.5; /* 设置行间距为1.5倍 */
}
//左侧序列号模块
.ace_gutter {
  background: #282c34;
  color: #7d8799;
}
.ace_layer .ace_gutter-layer .ace_folding-enabled {
  background: #282c34 !important;
  color: #7d8799 !important;
}
.ace_gutter-active-line .ace_gutter-cell {
  background: #282c34;
  color: #7d8799;
}
.ace-monokai .ace_gutter-active-line {
  background: #282c34;
  color: #7d8799;
}
.ace-monokai .ace_gutter {
  background: #282c34;
  color: #7d8799;
}
//右侧JSON数据展示模块
//整个模块背景色
.ace_scroller {
  background: #282c34;
  color: #7d8799;
  font-family: monospace;
}
//key
.ace_variable {
  color: #e06c75 !important;
}
//字符类型
.ace_string {
  color: #98c379 !important;
}
//Boolean类型
.ace-monokai .ace_constant.ace_character,
.ace-monokai .ace_constant.ace_language,
.ace-monokai .ace_constant.ace_numeric,
.ace-monokai .ace_constant.ace_other {
  color: #d19a66 !important;
}
//{}和[]的颜色
.ace_line_group {
  color: rgba(255, 255, 255, 0.6) !important; /* 白色,50%透明度 */
}
//选中数据的样式
.ace-monokai .ace_marker-layer .ace_selection {
  background-color: #72a1ff59;
  outline: 1px solid #457dff;
  border-radius: 0 !important;
}
.ace-monokai .ace_marker-layer .ace_selected-word {
  border: 1px solid #457dff;
  border-radius: 0 !important;
}
相关推荐
前端小巷子4 分钟前
跨域问题解决方案:JSONP
前端·javascript·面试
eric*168811 分钟前
尚硅谷张天禹老师课程配套笔记
前端·vue.js·笔记·vue·尚硅谷·张天禹·尚硅谷张天禹
程序员爱钓鱼27 分钟前
Go语言中的反射机制 — 元编程技巧与注意事项
前端·后端·go
GIS之路1 小时前
GeoTools 结合 OpenLayers 实现属性查询(二)
前端·信息可视化
烛阴1 小时前
一文搞懂 Python 闭包:让你的代码瞬间“高级”起来!
前端·python
AA-代码批发V哥1 小时前
HTML之表单结构全解析
前端·html
聪聪的学习笔记1 小时前
【1】确认安装 Node.js 和 npm版本号
前端·npm·node.js
小磊哥er1 小时前
【前端工程化】你知道前端编码规范包含哪些内容吗
前端
菌菇汤2 小时前
uni-app实现单选,多选也能搜索,勾选,选择,回显
前端·javascript·vue.js·微信小程序·uni-app·app
Ramos丶2 小时前
【ABAP】 从无到有 新建一个Webdynpro程序
java·前端·javascript