ACE编辑器ace-editor笔记

<template>
	<div class="ace-editor" ref="ace"></div>
</template>
<script>
import ace from 'ace-builds'
import 'ace-builds/webpack-resolver' // 在 webpack 环境中使用必须要导入
import 'ace-builds/src-noconflict/theme-chrome' // 默认设置的主题
import 'ace-builds/src-noconflict/mode-html' // 默认设置的语言模式
import 'ace-builds/src-noconflict/ext-language_tools'
export default {
...
	mounted () {
		this.initEditor()
	}
	methods: {
        initEditor() {
           	this.aceEditor = ace.edit(this.$refs.ace, {
		      maxLines: this.maxLines, // 最大行数,超过会自动出现滚动条
		      minLines: this.minLines, // 最小行数,还未到最大行数时,编辑器会自动伸缩大小
		      fontSize: this.fontSize, // 编辑器内字体大小
		      theme: this.themePath, // 默认设置的主题
		      mode: this.modePath + this.mode, // 默认设置的语言模式
		      tabSize: 4 // 制表符设置为 4 个空格大小
		    })
		    this.aceEditor.setOptions({
		      enableSnippets: true,
		      enableLiveAutocompletion: true,
		      enableBasicAutocompletion: true
		    })
        }
    }
}

一、常用配置

  1. 设置主题
this.aceEditor.setTheme("ace/theme/eclipse");
  1. 设置编辑的语言,这里是要编辑xml
this.aceEditor.getSession().setMode("ace/mode/xml"); 
  1. 给编辑框赋值
this.aceEditor.setValue('hello world');
  1. 获取编辑框的值
this.aceEditor.getValue(); 
  1. 获取选择内容
this.aceEditor.session.getTextRange(editor.getSelectionRange()); 
  1. 设置只读
this.aceEditor.setReadOnly(true); 
  1. 查找
this.aceEditor.find('needle',{ //needle,要查找的字符串或正则表达式
   backwards: false,//是否反向搜索,默认为false
   wrap: false,//搜索到文档底部是否回到顶端,默认为false
   caseSensitive: false,//是否匹配大小写搜索,默认为false
   wholeWord: false,//是否匹配整个单词搜素,默认为false
   regExp: false//搜索内容是否是正则表达式,默认为false
});
this.aceEditor.findNext(); //查找下一个
this.aceEditor.findPrevious(); //查找上一个
this.aceEditor.find('world');
  1. 替换
this.aceEditor.replace('bar');
this.aceEditor.replaceAll('bar');
  1. 监听
this.aceEditor.getSession().on('change', function(e) {});// 监听改变事件
this.aceEditor.getSession().selection.on('changeSelection', function(e) {});// 监听选择事件
this.aceEditor.getSession().selection.on('changeCursor', function(e) {});// 监听光标移动
  1. 其他
this.aceEditor.insert("Something cool");//在光标处插入:
this.aceEditor.selection.getCursor(); //获取光标所在行或列:
this.aceEditor.gotoLine(2);//跳转到行
this.aceEditor.session.getLength();//获取行数
  1. editor.setOption来设置配置项

– editor选项

选项名值类型默认值可选值备注
selectionStyleStringtextline|text选中样式
highlightActiveLineBooleantrue-高亮当前行
highlightSelectedWordBooleantrue-高亮选中文本
readOnlyBooleanfalse-是否只读
cursorStyleStringaceace|slim|smooth|wide光标样式
mergeUndoDeltasString|Booleanfalsealways合并撤销
behavioursEnabledBooleantrue-启用行为
wrapBehavioursEnabledBooleantrue-启用换行
autoScrollEditorIntoViewBooleanfalse-启用滚动
copyWithEmptySelectionBooleantrue-复制空格
useSoftTabsBooleanfalse-使用软标签
navigateWithinSoftTabsBooleanfalse-软标签跳转
enableMultiselectBooleanfalse-选中多处

– renderer选项

选项名值类型默认值可选值备注
hScrollBarAlwaysVisibleBooleanfalse-纵向滚动条始终可见
vScrollBarAlwaysVisibleBooleanfalse-横向滚动条始终可见
highlightGutterLineBooleantrue-高亮边线
animatedScrollBooleanfalse-滚动动画
showInvisiblesBooleanfalse-显示不可见字符
showPrintMarginBooleantrue-显示打印边距
printMarginColumnNumber80-设置页边距
printMarginBoolean|Numberfalse-显示并设置页边距
fadeFoldWidgetsBooleanfalse-淡入折叠部件
showFoldWidgetsBooleantrue-显示折叠部件
showLineNumbersBooleantrue-显示行号
showGutterBooleantrue-显示行号区域
displayIndentGuidesBooleantrue-显示参考线
fontSizeNumber|Stringinherit-设置字号
fontFamilyStringinherit 设置字体
maxLinesNumber--至多行数
minLinesNumber--至少行数
scrollPastEndBoolean|Number0-滚动位置
fixedWidthGutterBooleanfalse-固定行号区域宽度
themeString--主题引用路径,例如"ace/theme/textmate"

– mouseHandler选项

选项名值类型默认值可选值备注
scrollSpeedNumber--滚动速度
dragDelayNumber--拖拽延时
dragEnabledBooleantrue-是否启用拖动
focusTimoutNumber--聚焦超时
tooltipFollowsMouseBooleanfalse-鼠标提示

– session选项

选项名值类型默认值可选值备注
firstLineNumberNumber1-起始行号
overwriteBoolean--重做
newLineModeStringautoauto|unix|windows新开行模式
useWorkerBoolean--使用辅助对象
useSoftTabsBoolean--使用软标签
tabSizeNumber--标签大小
wrapBoolean--换行
foldStyleString-markbegin|markbeginend|manual折叠样式
modeString--代码匹配模式,例如“ace/mode/text"

– 扩展选项

选项名值类型默认值可选值备注
enableBasicAutocompletionBoolean--启用基本自动完成
enableLiveAutocompletionBoolean--启用实时自动完成
enableSnippetsBoolean--启用代码段
enableEmmetBoolean--启用Emmet
useElasticTabstopsBoolean--使用弹性制表位

二、禁用特定的ACE编辑器CSS模式警告规则

const INFO_RULES = [
  // Disable Heading (h2) has already been defined. warning
  'unique-headings',
  // Disable Heading (h2) should not be qualified. warning
  'qualified-headings',
  'fallback-colors'
];
const DISABLED_RULES = [
  // Disable Don't use adjoining classes. warning
  'adjoining-classes',
  // Disable Rule doesn't have all its properties in alphabetical ordered. warning
  'order-alphabetical'
];
// 调用
this.aceEditor.session.$worker.call('setInfoRules', [INFO_RULES.join('|')]) ;
this.aceEditor.session.$worker.call('setDisabledRules', [DISABLED_RULES.join('|')]) ;
或者直接填写以‘|’分隔
this.aceEditor.session.$worker.call('setDisabledRules', ['box-sizing|ids|order-alphabetical|universal-selector|fallback-colors|box-model|adjoining-classes|order-alphabetical|unique-headings|qualified-headings'])

Ace Editor官网文档

  • 1
    点赞
  • 24
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值