vue3-ace-editor代码编辑器使用

下载

安装 ace-builds 及vue3-ace-editor

引入并封装组件

引入:import { VAceEditor } from "vue3-ace-editor";
常用属性根据父组件传递

const props = defineProps({
  lang: {
    type: String,
    default: "sql",//可以传javaScript或者json,需要有对应文件的引入
  },
  // 是否只读
  readonly: {
    type: Boolean,
    default: false,
  },
  // 是否显示行数
  showLineNumbers: {
    type: Boolean,
    default: true,
  },
  // 是否显示左侧行数那列
  showGutter: {
    type: Boolean,
    default: true,
  },
  hasBorder: {
    type: Boolean,
    default: true,
  },
  // 是否高亮选中行
  highlightActiveLine: {
    type: Boolean,
    default: true,
  },
});

template:

<template>
  <VAceEditor
    class="edit_box"
    v-model:value="editorValue"
    v-bind="attr"
    :lang="lang"
    theme="tomorrow"
    :readonly="readonly"
    ref="aceDom"
    @init="editorInit"
    :class="{ border: hasBorder }"
    :options="{
      fontSize: 14,
      //代码提示及自动补全
      enableSnippets: true,
      enableLiveAutocompletion: true,
      enableBasicAutocompletion: true,
      tabSize: 2,
      placeholder:
        '/* 请使用 SQL 函数或片段表达式 对左侧的属性字段进行计算编辑 */',
      showPrintMargin: false,
      highlightActiveLine: highlightActiveLine,
      showLineNumbers: showLineNumbers,
      showGutter: showGutter,
      wrap: true,//自动换行
    }"
  />
</template>

init函数会接收到组件实例,可以将其抛给父组件,则可以父组件中调用实例,完成光标处插入代码片等功能editor.insert()

const emit = defineEmits(["getEditorInstance"]);

const attr = useAttrs();
const aceDom = ref();
const editor = ref();
const editorValue = ref("");
const editorInit = (editorInstance) => {
  editor.value = editorInstance; //editor实例
};
onMounted(() => {
  emit("getEditorInstance", editor.value);
});

引入mode 和 theme 这里可以选择自己想要的主题和mode

import ace from 'ace-builds';
import modeSqlUrl from "ace-builds/src-noconflict/mode-sql?url";
// import "ace-builds/webpack-resolver"
//sql mode
ace.config.setModuleUrl('ace/mode/sql', modeSqlUrl);

import modeJavascriptUrl from 'ace-builds/src-noconflict/mode-javascript?url';
ace.config.setModuleUrl('ace/mode/javascript', modeJavascriptUrl);

import modeHtmlUrl from 'ace-builds/src-noconflict/mode-html?url';
ace.config.setModuleUrl('ace/mode/html', modeHtmlUrl);

import modeYamlUrl from 'ace-builds/src-noconflict/mode-yaml?url';
ace.config.setModuleUrl('ace/mode/yaml', modeYamlUrl);

import themeGithubUrl from 'ace-builds/src-noconflict/theme-github?url';
ace.config.setModuleUrl('ace/theme/github', themeGithubUrl);

import themeChromeUrl from 'ace-builds/src-noconflict/theme-chrome?url';
ace.config.setModuleUrl('ace/theme/chrome', themeChromeUrl);

import themeMonokaiUrl from 'ace-builds/src-noconflict/theme-monokai?url';
ace.config.setModuleUrl('ace/theme/monokai', themeMonokaiUrl);

import workerBaseUrl from 'ace-builds/src-noconflict/worker-base?url';
ace.config.setModuleUrl('ace/mode/base', workerBaseUrl);

import workerJsonUrl from 'ace-builds/src-noconflict/worker-json?url';
ace.config.setModuleUrl('ace/mode/json_worker', workerJsonUrl);

import workerJavascriptUrl from 'ace-builds/src-noconflict/worker-javascript?url';
ace.config.setModuleUrl('ace/mode/javascript_worker', workerJavascriptUrl);

import workerHtmlUrl from 'ace-builds/src-noconflict/worker-html?url';
ace.config.setModuleUrl('ace/mode/html_worker', workerHtmlUrl);

import workerYamlUrl from 'ace-builds/src-noconflict/worker-yaml?url';
ace.config.setModuleUrl('ace/mode/yaml_worker', workerYamlUrl);

import snippetsHtmlUrl from 'ace-builds/src-noconflict/snippets/html?url';
ace.config.setModuleUrl('ace/snippets/html', snippetsHtmlUrl);
//
import snippetsSqllUrl from "ace-builds/src-noconflict/snippets/sql";
ace.config.setModuleUrl('ace/snippets/sql', snippetsSqllUrl);
//

import snippetsJsUrl from 'ace-builds/src-noconflict/snippets/javascript?url';
ace.config.setModuleUrl('ace/snippets/javascript', snippetsJsUrl);

import snippetsYamlUrl from 'ace-builds/src-noconflict/snippets/yaml?url';
ace.config.setModuleUrl('ace/snippets/javascript', snippetsYamlUrl);

import snippetsJsonUrl from 'ace-builds/src-noconflict/snippets/json?url';
ace.config.setModuleUrl('ace/snippets/json', snippetsJsonUrl);

import 'ace-builds/src-noconflict/ext-language_tools';
ace.require("ace/ext/language_tools");

完整代码

<template>
  <VAceEditor
    class="edit_box"
    v-model:value="editorValue"
    v-bind="attr"
    :lang="lang"
    theme="tomorrow"
    :readonly="readonly"
    ref="aceDom"
    @init="editorInit"
    :class="{ border: hasBorder }"
    :options="{
      fontSize: 14,
      //代码提示及自动补全
      enableSnippets: true,
      enableLiveAutocompletion: true,
      enableBasicAutocompletion: true,
      tabSize: 2,
      placeholder:
        '/* 请使用 SQL 函数或片段表达式 对左侧的属性字段进行计算编辑 */',
      showPrintMargin: false,
      highlightActiveLine: highlightActiveLine,
      showLineNumbers: showLineNumbers,
      showGutter: showGutter,
      wrap: true,//自动换行
    }"
  />
</template>
<script setup lang="ts">
import { onMounted, ref, useAttrs, watch } from "vue";
import { VAceEditor } from "vue3-ace-editor";
import "./vace.config";
const props = defineProps({
  lang: {
    type: String,
    default: "sql",
  },
  // 是否只读
  readonly: {
    type: Boolean,
    default: false,
  },
  // 是否显示行数
  showLineNumbers: {
    type: Boolean,
    default: true,
  },
  // 是否显示左侧行数那列
  showGutter: {
    type: Boolean,
    default: true,
  },
  hasBorder: {
    type: Boolean,
    default: true,
  },
  // 是否高亮选中行
  highlightActiveLine: {
    type: Boolean,
    default: true,
  },
});
const emit = defineEmits(["getEditorInstance"]);

const attr = useAttrs();
const aceDom = ref();
const editor = ref();
const editorValue = ref("");
const editorInit = (editorInstance) => {
  editor.value = editorInstance; //editor实例
};
onMounted(() => {
  emit("getEditorInstance", editor.value);
});
</script>
<style  lang="less">
.edit_box {
  width: 100%;
  height: 100%;
  .ace_scroller .ace_content .ace_placeholder {
    color: var(--grey-text-color1) !important;
  }
}

.border {
  border: 1px solid #c0c4cc;
  border-radius: 5px;
}
</style>


vace.config.js

import ace from 'ace-builds';
import modeSqlUrl from "ace-builds/src-noconflict/mode-sql?url";
// import "ace-builds/webpack-resolver"
ace.config.setModuleUrl('ace/mode/sql', modeSqlUrl);

import modeJavascriptUrl from 'ace-builds/src-noconflict/mode-javascript?url';
ace.config.setModuleUrl('ace/mode/javascript', modeJavascriptUrl);

import modeHtmlUrl from 'ace-builds/src-noconflict/mode-html?url';
ace.config.setModuleUrl('ace/mode/html', modeHtmlUrl);

import modeYamlUrl from 'ace-builds/src-noconflict/mode-yaml?url';
ace.config.setModuleUrl('ace/mode/yaml', modeYamlUrl);

import themeGithubUrl from 'ace-builds/src-noconflict/theme-github?url';
ace.config.setModuleUrl('ace/theme/github', themeGithubUrl);

import themeChromeUrl from 'ace-builds/src-noconflict/theme-chrome?url';
ace.config.setModuleUrl('ace/theme/chrome', themeChromeUrl);

import themeMonokaiUrl from 'ace-builds/src-noconflict/theme-monokai?url';
ace.config.setModuleUrl('ace/theme/monokai', themeMonokaiUrl);

import workerBaseUrl from 'ace-builds/src-noconflict/worker-base?url';
ace.config.setModuleUrl('ace/mode/base', workerBaseUrl);

import workerJsonUrl from 'ace-builds/src-noconflict/worker-json?url';
ace.config.setModuleUrl('ace/mode/json_worker', workerJsonUrl);

import workerJavascriptUrl from 'ace-builds/src-noconflict/worker-javascript?url';
ace.config.setModuleUrl('ace/mode/javascript_worker', workerJavascriptUrl);

import workerHtmlUrl from 'ace-builds/src-noconflict/worker-html?url';
ace.config.setModuleUrl('ace/mode/html_worker', workerHtmlUrl);

import workerYamlUrl from 'ace-builds/src-noconflict/worker-yaml?url';
ace.config.setModuleUrl('ace/mode/yaml_worker', workerYamlUrl);

import snippetsHtmlUrl from 'ace-builds/src-noconflict/snippets/html?url';
ace.config.setModuleUrl('ace/snippets/html', snippetsHtmlUrl);
//
import snippetsSqllUrl from "ace-builds/src-noconflict/snippets/sql";
ace.config.setModuleUrl('ace/snippets/sql', snippetsSqllUrl);
//

import snippetsJsUrl from 'ace-builds/src-noconflict/snippets/javascript?url';
ace.config.setModuleUrl('ace/snippets/javascript', snippetsJsUrl);

import snippetsYamlUrl from 'ace-builds/src-noconflict/snippets/yaml?url';
ace.config.setModuleUrl('ace/snippets/javascript', snippetsYamlUrl);

import snippetsJsonUrl from 'ace-builds/src-noconflict/snippets/json?url';
ace.config.setModuleUrl('ace/snippets/json', snippetsJsonUrl);

import 'ace-builds/src-noconflict/ext-language_tools';
ace.require("ace/ext/language_tools");

后续

要让vue3-ace-editor插件支持Python语法,你需要按照以下步骤进行操作: 1. 安装`vue3-ace-editor`插件。如果你还没有安装这个插件,可以通过npm或yarn来安装它。使用npm的命令如下: ```bash npm install --save vue3-ace-editor ``` 2. 引入`vue3-ace-editor`并在你的组件中使用。在你的Vue组件中,你需要引入`vue3-ace-editor`,并将其添加到组件的`components`选项中。 3. 配置编辑器使用Python模式。`vue3-ace-editor`使用ace-builds作为底层编辑器,所以你需要引入Python模式的语法定义。在你的组件的`mounted`或`onMounted`生命周期钩子中,你可以使用ace-builds的`require`方法来动态加载Python语言的语法文件。例如: ```javascript import { ref, onMounted } from 'vue'; import ace from 'vue3-ace-editor'; import 'ace-builds/src-noconflict/mode-python'; import 'ace-builds/src-noconflict/theme-monokai'; export default { components: { ace }, setup() { const editor = ref(null); onMounted(() => { editor.value.editor.session.setMode("ace/mode/python"); editor.value.editor.setTheme("ace/theme/monokai"); }); return { editor }; } }; ``` 4. 在模板中使用`<ace-editor>`。在你的组件模板中,现在可以添加`<ace-editor>`并使用之前配置好的`editor`引用。例如: ```html <template> <div> <ace-editor ref="editor" width="800px" height="600px" @init="onInit" /> </div> </template> ``` 通过以上步骤,你就可以在vue3项目中使用vue3-ace-editor插件并且配置支持Python语法了。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值