Ace editor 在输入关键字时,每次键入都会有提示变动,一旦删除一个字母,提示即刻消失,必须再次输入后才会有提示
editor 保存到 state 中,监听onChange事件,执行 startAutocomplete 命令即可:
handleOnChange() {
editor.execCommand('startAutocomplete')
}
这样不管是输入还是删除都会自动触发提示(自动完成)
over
示例demo:https://securingsincity.github.io/react-ace/
在 react 中使用 AceEditor 需要引入以下两个依赖:
npm install react-ace ace-builds
或
yarn add react-ace ace-builds
截止2023.02.20,最新版本号为:
- react-ace:10.1.0
- ace-builds:1.15.2
代码仓库地址:
- react-ace:https://github.com/securingsincity/react-ace
- ace-builds:https://github.com/ajaxorg/ace
使用样例:
// 核心组件
import AceEditor from 'react-ace'
// 引入对应的语言
import 'ace-builds/src-noconflict/mode-javascript'
//以下import的是风格
import 'ace-builds/src-noconflict/theme-eclipse'
// 代码提示
import 'ace-builds/src-noconflict/ext-language_tools'
import 'ace-builds/webpack-resolver'
import './code.less'
const CodeView = () => {
return (
<AceEditor
// name类似于id和key需要保持唯一性
name='myCodeView'
// 字体大小
fontSize={16}
style={{ width: '80%', height: '80%', minHeight: 80 }}
mode='javascript'
theme='eclipse'
/>
)
}
export default CodeView
常用属性:
mode:编辑器的整体模式或样式,这里取值为 markdown,表明需要用这个编辑器来输入 markdown文本,这样编辑器就会进行相应的初始设置。
theme:编辑器主题,这里使用了 github这个主题。
wrapEnabled:当输入的一句文本比一行的长度要长时,是否允许换行。
tabSize:使用几个空格来表示表示一次 Tab按键。
fontSize:文本的字体大小
height:编辑器的高度,单位为 px。
width:编辑器的宽度,单位为 px。
debounceChangePeriod:多长时间对输入响应一次,单位为 ms,类似于节流。
onChange:文本框内容发生变化时的回调函数。
onScroll:文本框内容发生滚动时的回调函数。
name:编辑器的 id。
editorProps:当在文本框内输入内容时,是否需要滚动条进行响应的滚动定位。
全部属性:
Prop | Default | Type | Description |
---|---|---|---|
name | ‘ace-editor’ | String | Unique Id to be used for the editor |
placeholder | null | String | Placeholder text to be displayed when editor is empty |
mode | ‘’ | String | Language for parsing and code highlighting |
theme | ‘’ | String | theme to use |
value | ‘’ | String | value you want to populate in the code highlighter |
defaultValue | ‘’ | String | Default value of the editor |
height | ‘500px’ | String | CSS value for height |
width | ‘500px’ | String | CSS value for width |
className | String | custom className | |
fontSize | 12 | Number | pixel value for font-size |
showGutter | true | Boolean | show gutter |
showPrintMargin | true | Boolean | show print margin |
highlightActiveLine | true | Boolean | highlight active line |
focus | false | Boolean | whether to focus |
cursorStart | 1 | Number | the location of the cursor |
wrapEnabled | false | Boolean | Wrapping lines |
readOnly | false | Boolean | make the editor read only |
minLines | Number | Minimum number of lines to be displayed | |
maxLines | Number | Maximum number of lines to be displayed | |
enableBasicAutocompletion | false | Boolean | Enable basic autocompletion |
enableLiveAutocompletion | false | Boolean | Enable live autocompletion |
enableSnippets | false | Boolean | Enable snippets |
tabSize | 4 | Number | tabSize |
debounceChangePeriod | null | Number | A debounce delay period for the onChange event |
onLoad | Function | called on editor load. The first argument is the instance of the editor | |
onBeforeLoad | Function | called before editor load. the first argument is an instance of ace | |
onChange | Function | occurs on document change it has 2 arguments the value and the event. | |
onCopy | Function | triggered by editor copy event, and passes text as argument | |
onPaste | Function | Triggered by editor paste event, and passes text as argument | |
onSelectionChange | Function | triggered by editor selectionChange event, and passes a Selection as it’s first argument and the event as the second | |
onCursorChange | Function | triggered by editor changeCursor event, and passes a Selection as it’s first argument and the event as the second | |
onFocus | Function | triggered by editor focus event | |
onBlur | Function | triggered by editor blur event.It has two arguments event and editor | |
onInput | Function | triggered by editor input event | |
onScroll | Function | triggered by editor scroll event | |
onValidate | Function | triggered, when annotations are changed | |
editorProps | Object | properties to apply directly to the Ace editor instance | |
setOptions | Object | options to apply directly to the Ace editor instance | |
keyboardHandler | String | corresponding to the keybinding mode to set (such as vim or emacs) | |
commands | Array | new commands to add to the editor | |
annotations | Array | annotations to show in the editor i.e. [{ row: 0, column: 2, type: 'error', text: 'Some error.'}] , displayed in the gutter.(type:‘error’,‘info’,‘warning’) | |
markers | Array | markers to show in the editor, i.e. [{ startRow: 0, startCol: 2, endRow: 1, endCol: 20, className: 'error-marker', type: 'background' }] . Make sure to define the class (eg. “.error-marker”) and set position: absolute for it. | |
style | Object | camelCased properties |
不同文件中可以引入不同的语言支持包:
import 'brace/mode/javascript';//
import 'brace/mode/html';//
import 'brace/mode/java';//
import 'brace/mode/python';//
import 'brace/mode/lua';//
import 'brace/mode/xml';//
import 'brace/mode/ruby';//
import 'brace/mode/sass';
import 'brace/mode/markdown';//
import 'brace/mode/mysql';
import 'brace/mode/json';//
import 'brace/mode/css';//
import 'brace/mode/typescript';
主要内容说完,结尾列举一些 ace.js 中定义的命令:
命令 | 描述 | 快捷键 | |
---|---|---|---|
addCursorAbove | Add cursor above | :Ctrl-Alt-Up | |
addCursorBelow | Add cursor below | Ctrl-Alt-Down | |
addCursorAboveSkipCurrent | Add cursor above (skip current) | Ctrl-Alt-Shift-Up | |
addCursorBelowSkipCurrent | Add cursor below (skip current) | Ctrl-Alt-Shift-Down | |
selectMoreBefore | Select more before | Ctrl-Alt-Left | |
selectMoreAfter | Select more after | Ctrl-Alt-Right | |
selectNextBefore | Select next before | Ctrl-Alt-Shift-Left | |
selectNextAfter | Select next after | Ctrl-Alt-Shift-Right | |
toggleSplitSelectionIntoLines | Split into lines | Ctrl-Alt-L | |
splitSelectionIntoLines | Split into lines | ||
alignCursors | Align cursors | Ctrl-Alt-A | |
findAll | Find all | Ctrl-Alt-K | |
singleSelection | Single selection | ||
startAutocomplete | Start auto complete | Ctrl-Space / Ctrl-Shift-Space / Alt-Space |
其他还有些命令是根据主题定制的,这里就不再赘述
其他命令来源(可以自定义命令来处理一组自定义操作):