当前位置: 首页 > 工具软件 > ACE Editor > 使用案例 >

【实战】React 必会第三方插件 —— Ace editor 中 删除部分内容后,代码提示依旧显示(不用重输)

钱均
2023-12-01


  • 文档: [ace-builds/README.md]:(https://github.com/ajaxorg/ace-builds/blob/master/README.md)

一、问题

Ace editor 在输入关键字时,每次键入都会有提示变动,一旦删除一个字母,提示即刻消失,必须再次输入后才会有提示

二、解决

editor 保存到 state 中,监听onChange事件,执行 startAutocomplete 命令即可:

handleOnChange() {
	editor.execCommand('startAutocomplete')
}

这样不管是输入还是删除都会自动触发提示(自动完成)


over


三、拓展学习

1.AceEditor

示例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:当在文本框内输入内容时,是否需要滚动条进行响应的滚动定位。

全部属性:

PropDefaultTypeDescription
name‘ace-editor’StringUnique Id to be used for the editor
placeholdernullStringPlaceholder text to be displayed when editor is empty
mode‘’StringLanguage for parsing and code highlighting
theme‘’Stringtheme to use
value‘’Stringvalue you want to populate in the code highlighter
defaultValue‘’StringDefault value of the editor
height‘500px’StringCSS value for height
width‘500px’StringCSS value for width
classNameStringcustom className
fontSize12Numberpixel value for font-size
showGuttertrueBooleanshow gutter
showPrintMargintrueBooleanshow print margin
highlightActiveLinetrueBooleanhighlight active line
focusfalseBooleanwhether to focus
cursorStart1Numberthe location of the cursor
wrapEnabledfalseBooleanWrapping lines
readOnlyfalseBooleanmake the editor read only
minLinesNumberMinimum number of lines to be displayed
maxLinesNumberMaximum number of lines to be displayed
enableBasicAutocompletionfalseBooleanEnable basic autocompletion
enableLiveAutocompletionfalseBooleanEnable live autocompletion
enableSnippetsfalseBooleanEnable snippets
tabSize4NumbertabSize
debounceChangePeriodnullNumberA debounce delay period for the onChange event
onLoadFunctioncalled on editor load. The first argument is the instance of the editor
onBeforeLoadFunctioncalled before editor load. the first argument is an instance of ace
onChangeFunctionoccurs on document change it has 2 arguments the value and the event.
onCopyFunctiontriggered by editor copy event, and passes text as argument
onPasteFunctionTriggered by editor paste event, and passes text as argument
onSelectionChangeFunctiontriggered by editor selectionChange event, and passes a Selection as it’s first argument and the event as the second
onCursorChangeFunctiontriggered by editor changeCursor event, and passes a Selection as it’s first argument and the event as the second
onFocusFunctiontriggered by editor focus event
onBlurFunctiontriggered by editor blur event.It has two arguments event and editor
onInputFunctiontriggered by editor input event
onScrollFunctiontriggered by editor scroll event
onValidateFunctiontriggered, when annotations are changed
editorPropsObjectproperties to apply directly to the Ace editor instance
setOptionsObjectoptions to apply directly to the Ace editor instance
keyboardHandlerStringcorresponding to the keybinding mode to set (such as vim or emacs)
commandsArraynew commands to add to the editor
annotationsArrayannotations to show in the editor i.e. [{ row: 0, column: 2, type: 'error', text: 'Some error.'}], displayed in the gutter.(type:‘error’,‘info’,‘warning’)
markersArraymarkers 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.
styleObjectcamelCased 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';

2.其他代码编辑器

CodeMirror

  • 官网:https://codemirror.net/
  • github:https://github.com/codemirror/CodeMirror
  • 示例代码:https://uiwjs.github.io/react-codemirror/

Monaco

  • github:https://github.com/microsoft/monaco-editor

diff2html

  • 官网:https://diff2html.xyz/
  • gitbub:https://github.com/rtfpessoa/diff2html

ace中的命令

主要内容说完,结尾列举一些 ace.js 中定义的命令:

命令描述快捷键
addCursorAboveAdd cursor above:Ctrl-Alt-Up
addCursorBelowAdd cursor belowCtrl-Alt-Down
addCursorAboveSkipCurrentAdd cursor above (skip current)Ctrl-Alt-Shift-Up
addCursorBelowSkipCurrentAdd cursor below (skip current)Ctrl-Alt-Shift-Down
selectMoreBeforeSelect more beforeCtrl-Alt-Left
selectMoreAfterSelect more afterCtrl-Alt-Right
selectNextBeforeSelect next beforeCtrl-Alt-Shift-Left
selectNextAfterSelect next afterCtrl-Alt-Shift-Right
toggleSplitSelectionIntoLinesSplit into linesCtrl-Alt-L
splitSelectionIntoLinesSplit into lines
alignCursorsAlign cursorsCtrl-Alt-A
findAllFind allCtrl-Alt-K
singleSelectionSingle selection
startAutocompleteStart auto completeCtrl-Space / Ctrl-Shift-Space / Alt-Space

其他还有些命令是根据主题定制的,这里就不再赘述

其他命令来源(可以自定义命令来处理一组自定义操作):

  • https://github.com/ajaxorg/ace-builds/blob/master/src/keybinding-vscode.js
  • https://github.com/ajaxorg/ace-builds/blob/master/src/keybinding-sublime.js
  • https://github.com/ajaxorg/ace-builds/blob/master/src/keybinding-emacs.js
 类似资料: