vue2-ace-editor

vue2-ace-editor

Ace 是一个用 JavaScript 编写的可嵌入代码编辑器。它与 Sublime、Vim 和 TextMate 等原生编辑器的功能和性能相匹配。它可以很容易地嵌入到任何网页和 JavaScript 应用程序中。Ace 被维护为 Cloud9IDE 的主要编辑器,是 Mozilla Skywriter(Bespin)项目的继任者。

如何使用

安装
npm install --save-dev vue2-ace-editor
集成

terminals.vue

<template>
  <Editor v-model="data" :options="defaultOptions" @init="editorInit" lang="json" :theme="theme" width="100%" :height="100%" class="my-editor"></Editor>
</template>
<script>
import Editor from 'vue2-ace-editor'
export default {
  name: 'Terminals',
  props: {
    data: {
      type: null | undefined | String,
      required: true
    },
    isEdit: {
      type: Boolean,
      default: true
    }
  },
  components: { Editor },
  data() {
    return {
      theme: 'tomorrow_night',
      ed: null,
      defaultOptions: {
        fontSize: 14,
        showPrintMargin: false,
        showGutter: true,
        highlightActiveLine: true,
        enableEmmet: true,
        enableLiveAutocompletion: true,
        autoScrollEditorIntoView: true,
        enableSnippets: true,
        tabSize: 2,
        useWorker: false,
        vScrollBarAlwaysVisible: true,
        wrap: true,
      }
    }
  },
  mounted() {
    window.addEventListener("resize", this.$_handleResize);
    this.$once("hook:beforeDestroy", () => {
      window.removeEventListener("resize", this.$_handleResize);
    });
  },
  methods: {
    $_handleResize() {
      this.ed.resize()
    },
    editorInit(ed) {
      this.ed = ed
      require('brace/mode/json')
      require('brace/mode/xml')
      require('brace/theme/' + this.theme)
      require('brace/ext/language_tools') //language extension prerequsite...
      require('brace/snippets/json')
      require('brace/snippets/xml')
      if (!this.isEdit) {
        ed.setReadOnly(true)
      }
      ed.getSession().on('change', () => {
        this.$emit('change', ed.getValue())
      })
    }
  }
}
</script>
<style>
.ace_editor,
.ace_editor * {
  font-family: 'Monaco', 'Menlo', 'Ubuntu Mono', 'Droid Sans Mono', 'Consolas',
    monospace !important;
  font-size: 14px !important;
  font-weight: 400 !important;
  letter-spacing: 0 !important;
}
</style>
配置列表

以下是配置选项的列表。除非另有说明,否则选项值为布尔值。

  • 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--使用弹性制表位
方法
  • 设置主题
editor.setTheme("ace/theme/twilight");
  • 设置语言模式
editor.session.setMode("ace/mode/javascript");
  • 设置和获取内容:
editor.setValue("the new text here"); // 或 session.setValue
editor.getValue(); // 或 session.getValue
  • 获取选定的文本:
editor.session.getTextRange(editor.getSelectionRange());
  • 在光标处插入:
editor.insert("Something cool");
  • 获取当前光标所在的行和列:
editor.selection.getCursor();
  • 转到某一行:
editor.gotoLine(lineNumber);
  • 获取总行数:
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为可编辑
  • 窗口自适应
editor.resize()
  • 在代码中搜索
    以下选项可用于搜索参数:
    • needle: 要查找的字符串或正则表达式
    • backwards: 是否从当前光标所在的位置向后搜索。默认为“false”
    • wrap: 当搜索到达结尾时,是否将搜索返回到开头。默认为“false
    • caseSensitive: 搜索是否应该区分大小写。默认为“false”
    • wholeWord: 搜索是否只匹配整个单词。默认为“false”
    • range: 搜索匹配范围,要搜索整个文档则设置为空
    • regExp: 搜索是否为正则表达式。默认为“false”
    • start: 开始搜索的起始范围或光标位置
    • skipCurrent: 是否在搜索中包含当前行。默认为“false”

下面是一个如何在编辑器对象上设置搜索的示例:

editor.find('needle',{
  backwards: false,
  wrap: true,
  caseSensitive: true,
  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);

selection变化:

editor.getSession().selection.on('changeSelection', callback);

cursor变化:

editor.getSession().selection.on('changeCursor', callback);
  • 添加新的命令和绑定

将指定键绑定并分配给自定义功能:

editor.commands.addCommand({
    name: 'myCommand',
    bindKey: {win: 'Ctrl-M',  mac: 'Command-M'},
    exec: function(editor) {
        //...
    }
});
常见问题

光标错位问题修复:

.ace_editor,
.ace_editor * {
  font-family: 'Monaco', 'Menlo', 'Ubuntu Mono', 'Droid Sans Mono', 'Consolas',
    monospace !important;
  font-size: 14px !important;
  font-weight: 400 !important;
  letter-spacing: 0 !important;
}
  • 8
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值