Ace编辑器中文文档
时间:2023-04-02 11:40:42
HTML
Ace是一个用JavaScript编写的可嵌入代码编辑器。它与Sublime、Vim和TextMate等原生编辑器的功能和性能相匹配。它可以很容易地嵌入到任何网页和JavaScript应用程序中。官网地址:Ace-TheHighPerformanceCodeEditorfortheWebGithub:GitHub-ajaxorg/ace:Ace(Ajax.orgCloud9Editor)vue版本:GitHub-chairuosen/vue2-ace-editor快速上手易用sometext
设置主题和语言模式要更改主题,请使用要使用的主题路径配置编辑器。主题文件将按需加载:editor.setTheme("ace/theme/twilight");默认情况下,编辑器处于纯文本模式。但是,所有其他语言模式都可以作为单独的模块使用。语言模式也会按需加载:editor.session.setMode("ace/mode/javascript");编辑器状态Ace保留所有编辑器状态(选择、滚动位置等)切换编辑器非常有用:varEditSession=require("ace/edit_session").EditSessionvarjs=newEditSession("somejscode")varcss=newEditSession(["some","css","codehere"])//要将文档加载到编辑器中,只需像这样调用editor.setSession(js)在项目中配置Ace//配置代码模式为aceoptionace.edit(element,{mode:"ace/mode/javascript",selectionStyle:"text"})//使用setOptions方法一次设置多个选项editor.setOptions({autoScrollEditorIntoView:true,copyWithEmptySelection:true,});//单独设置setOptions方法editor.setOption("mergeUndoDeltas","always");//有些选项也是直接设置的,例如:editor.setTheme(...)//获取选项设置valueeditor.getOption("optionName");//核心Ace组件包括(editor,session,renderer,mouseHandler)setOption(optionName,optionValue)setOptions({optionName:optionValue})getOption(optionName)getOptions()设置和获取内容:`editor.setValue("这里的新文本");//或session.setValueeditor。获取值();//或session.getValue以获取选定的文本:editor。session.getTextRange(editor.getSelectionRange());在光标处插入:editor.insert("Somethingcool");获取当前光标所在的行和列:editor.selection.getCursor();转到一行:编辑器。转到行(行号);获取总行数:editor.session.getLength();设置默认标签大小:editor.getSession().setTabSize(4);使用软标签:editor.getSession().setUseSoftTabs(true);设置字体大小:document.getElementById('editor').style.fontSize='12px';切换自动换行:editor.getSession().setUseWrapMode(true);设置行高亮:editor.setHighlightActiveLine(false);设置打印边距可见性:editor.setShowPrintMargin(false);设置编辑器为只读:editor.setReadOnly(true);//false是可编辑窗口自适应编辑器仅在触发调整大小事件时调整自身大小。如果你想用其他方式来调整编辑器div的大小,并且你需要调整编辑器的大小,使用下面的命令:editor.resize()在代码中搜索ACE编辑器的主要搜索功能是在search.js中定义的。以下选项可用于搜索参数:needle:要查找的字符串或正则表达式backwards:是否从当前光标位置向后搜索。默认为“false”wrap:搜索到末尾时是否返回到开头。默认为"falsecaseSensitive:搜索是否区分大小写。默认为"false"wholeWord:搜索是否只匹配整个单词。默认为"false"range:搜索匹配范围,并设置为空如果要搜索整个文档regExp:搜索是否为正则表达式,默认为“false”start:开始搜索的起始范围或光标位置skipCurrent:是否在搜索中包括当前行。默认为“false”以下是如何在编辑器对象上设置搜索的示例:editor.find('needle',{backwards:false,wrap:false,caseSensitive:false,wholeWord:false,regExp:false});editor.findNext();editor.findPrevious();以下是如何进行替换:editor.find('foo');editor.replace('bar');这是替换所有:editor.replaceAll('bar');editor.replaceAll在使用之前需要调用editor.find('needle',...)事件监听器。onchange:editor.getSession().on('change',callback);选择更改:editor.getSession().selection.on('changeSelection',callback);光标变化:editor.getSession().selection.on('changeCursor',callback);添加新命令和绑定将指定键绑定并分配给自定义函数:editor.commands.addCommand({name:'myCommand',bindKey:{win:'Ctrl-M',mac:'Command-M'},exec:function(编辑器){//...}});主要配置下面是当前支持的主要选项列表。除非另有说明,选项值都是布尔值,可以通过editor.setOption设置。-editoroptionoptionnamevaluetypedefaultvalueoptionalvaluenote-启用行为wrapBehavioursEnabledBooleantrue-启用换行autoScrollEditorIntoViewBooleanfalse-启用滚动copyWithEmptySelectionBooleantrue-复制空格useSoftTabsBooleanfalse-使用软标签navigateWithinSoftTabsBooleanfalse-软标签跳转enableMultiselectBooleanfalse-选中多处-renderer选项选项名值类型默认值可选值备注hScrollBarAlwaysVisibleBooleanfalse-纵向滚动条始终VisiblevScrollBarAlwaysVisibleBooleanfalse-horizontalscrollbarisalwaysvisiblehighlightGutterLineBooleantrue-highlightededgeanimatedScrollBooleanfalse-scrollanimationshowInvisiblesBooleanfalse-displayinvisiblecharactersshowPrintMarginBooleanoldtrue-displayprintmarginprintMarginColumnNumber80-setmarginprintMarginBoolean|Numberfalse-displayandsetpagemarginshowFoldWidgetsBooleantrue-displayfoldedwidgetsshowLineNumbersBooleantrue-displaylinenumbersshowGutterBooleantrue-displaylinenumberareadisplayIndentGuidesBooleantrue-显示参考行fontSizeNumber|Stringinherit-设置字体大小fontFamilyStringinherit-设置字体maxLinesNumber--最多行数minLinesNumber--最少行数scrollPastEndBoolean|Number0-滚动位置fixedWidthGutterBooleanfalse-固定行号区域宽度themeString--主题参考路径,如"ace/theme/textmate"-mouseHandleroptionoptionnamevaluetypedefaultvalueoptionalvalueremarksscrollSpeedNumber--滚动速度dragDelayNumber--dragEnabledBooleantrue--是否启用拖动focusTimoutNumber--焦点超时tooltipFollowsMo??useBooleanfalse-mouseprompt-sessionoptionoptionnamevaluetypedefaultvalueoptionalvalueremarkfirstLineNumberNumber1-起始行号overwriteBoolean--redonewLineModeStringautoauto|unix|windows换行模式useWorkerBoolean--使用辅助对象useSoftTabsBoolean--使用软标签tabSizeNumber--tabsizewrapBoolean--换行foldStyleString-markbegin|markbeginend|手动折叠样式modeString--代码匹配模式,如"ace/mode/text"--扩展选项optionnamevaluetypedefaultvalueoptionalvalueremarkenableBasicAutocompletionBoolean--启用基本自动补全enableLiveAutocompletionBoolean--启用实时自动补全enableSnippetsBoolean--启用代码片段enableEmmetBoolean--启用EmmetuseElasticTabstopsBoolean--使用弹性制表位