Github https://github.com/securingsincity/react-ace
在线预览 https://securingsincity.github.io/react-ace
react-ace 是Ace 的react 封装版本
yarn add react-ace
- enableBasicAutocompletion 普通自动完成,可选类型(true/false)
- enableLiveAutocompletion 实时自动完成,可选类型(true/false)
- enableSnippets 代码自动补全,可选类型(true/false)
- onLoad:加载完成后执行的函数,第一个参数是编辑器的实例
- 支持代码 diff对比
AceEditor

import React, { useState, useEffect, forwardRef } from 'react';import { string, bool, number, func } from 'prop-types';import cls from 'classnames';import { isEqual, noop } from 'lodash';import AceEditor from 'react-ace'; // 488.kb gzip 132k// 引入 modeimport "ace-builds/src-noconflict/mode-json";// 引入 themeimport "ace-builds/src-noconflict/theme-github";// 如果要代码提示,这个必须引入!!!import "ace-builds/src-noconflict/ext-language_tools";const EditorRef = forwardRef(Editor);export default EditorRef;EditorRef.propTypes = {onChange: func,mode: string,theme: string,showGutter: bool,tabSize: number,};EditorRef.defaultProps = {onChange: noop,mode: 'json', // 解析和代码突出显示的语言theme: 'github',showGutter: true, // false隐藏行号tabSize: 2,}function Editor(props, ref){const {value,onChange,className,...rest} = props;const [state, setState] = useState();useEffect(() => {if (isEqual(value, state)) return;setState(value ?? undefined);}, [value]);function handleChange(values){setState(values);onChange(values);}return (<AceEditorref={ref}value={state}className={cls('full', className)}onChange={handleChange}wrapEnabled // 换行highlightActiveLine // 突出活动线readOnly // 只读,禁止编辑debounceChangePeriod={60} // onChange事件的抖动延迟style={{ backgroundColor: 'rgba(245,245,245,0.7)'}}editorProps={{ $blockScrolling: true }}// 设置编辑器格式化和代码提示setOptions={{enableBasicAutocompletion: true, // 代码基本提示enableLiveAutocompletion: true, // 代码智能提示enableSnippets: true, // 显示代码片段showLineNumbers: true, // 显示行号useWorker: false,tabSize: 2,}}// annotations={// [{row: 0, column: 2, type: 'error', text: '手动设置错误' }]// }{...rest}/>);}
mode
mode & theme文档 https://github.com/securingsincity/react-ace/blob/master/docs/Modes.md
默认支持常见的 16中语言,常用的 mode
- html,css
- json
- javascript,typescript,jsx
- sql
["csharp","css","elixir","golang","handlebars","html","java","javascript","json","markdown","mysql","python","ruby","sass","typescript","xml"]
theme
默认支持 10种主题,常用的主题有
- github,solarized_light 亮色
- monokai,solarized_dark 暗色
["github","kuroir","monokai","solarized_dark","solarized_light","terminal","textmate","tomorrow","twilight","xcode"]
AceEditor事件
Ace参数及事件 https://github.com/securingsincity/react-ace/blob/master/docs/Ace.md
- onLoad 编辑器加载时调用。第一个参数是编辑器的实例 ace
- onBeforeLoad 编辑器加载之前调用。第一个参数是的实例ace
- onChange 文档改变,有2个参数,每个编辑器的值和事件
- onCopy 编辑器copy事件触发,将文本作为参数传递
- onPaste 编辑器paste事件触发,将文本作为参数传递
- onSelectionChange 编辑器selectionChange事件触发
- 将Selection作为第一个参数传递,事件作为第二个参数传递
- onCursorChange 编辑器changeCursor事件触发
- 将Selection作为第一个参数传递,事件作为第二个参数传递
- onFocus 编辑器focus事件触发
- onBlur 编辑器blur事件触发
- onInput 编辑器input事件触发
- onScroll 编辑器scroll事件触发
onPaste 粘贴
onCopy 复制
getCompletions 自定义代码段
const completers = [{name: 'name',value: 'one',score: 100,meta: '手动添加1'},{name: 'name',value: 'two',score: 100,meta: '手动添加2'}];complete(editor) {const snippet = {getCompletions: (callback) => {callback(null, completers);}}//向编辑器中添加自动补全列表editor.completers.push(snippet);}
DiffEditor 代码对比
https://github.com/securingsincity/react-ace/blob/master/docs/Diff.md
AceEditor 常见问题
webpack-resolver 打包问题
加上 webpack-resolver 打包后, 40.1MB
文件被拆分成一个个小文件
import AceEditor from 'react-ace';// import 'ace-builds/webpack-resolver';import 'ace-builds/src-noconflict/mode-json';
未捕获的DOMException:无法在’WorkerGlobalScope’上执行’importScripts’
‘http://localhost:9000/worker-html.js'上的脚本无法加载
https://github.com/securingsincity/react-ace/issues/725
解决:setOptions={{ useWorker: false }}
react-ace参考:react-ace实现代码编辑
https://www.jianshu.com/p/cc9f5eb87aa2
