基于vue2-ace-editor的代码编辑器

基于vue2-ace-editor的代码编辑器

一、AceEditor组件

1.组件定义

<template>
  <editor ref="codeEditor" v-model="currentContent" @init="editorInit" :options="options" :lang="lang" :theme="codeStyle" :width="width" :height="height"/>
</template>

<script>
import { mapState } from 'vuex'

export default {
  name: 'AceEditor',
  components: {
    editor: require('vue2-ace-editor')
  },
  props: {
    width: {
      type: String,
      default: '100%'
    },
    height: {
      type: String,
      default: '500px'
    },
    content: {
      type: String,
      required: true,
      default: () => null
    },
    lang: {
      type: String,
      default: 'java'
    },
    readOnly: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      options: {
        autoScrollEditorIntoView: true,
        enableBasicAutocompletion: true,
        enableLiveAutocompletion: true,
        enableSnippets: false,
        // 只读
        readOnly: this.readOnly,
        // 显示打印边距线
        showPrintMargin: false,
        // 字体大小
        fontSize: this.$store.state.settings.codeSize
      }
    }
  },
  computed: {
    ...mapState({
      codeStyle: state => state.settings.codeStyle,
      codeSize: state => state.settings.codeSize
    }),
    currentContent: {
      get() {
        return this.content
      },
      set(val) {
        this.$emit('update:content', val)
      }
    }
  },
  watch: {
    codeSize: {
      handler: function(value) {
        this.$refs.codeEditor.editor.setOptions({
          fontSize: value
        })
      },
      deep: true
    }
  },
  created() {

  },
  mounted() {

  },
  methods: {
    editorInit(editor) {
      require('brace/ext/language_tools')
      require('brace/mode/java')
      require('brace/mode/mysql')
      require('brace/mode/json')
      require('brace/mode/xml')
      require('brace/theme/chrome')
      require('brace/theme/github')
      require('brace/theme/eclipse')
      require('brace/theme/dracula')
      require('brace/snippets/java')
      require('brace/snippets/mysql')
      require('brace/ext/beautify')
    },
    format() {
      const ace = require('brace')
      const editor = this.$refs.codeEditor.editor
      const beautify = ace.acequire('ace/ext/beautify')
      beautify.beautify(editor.session)
    }
  }
}
</script>

<style scoped>

</style>

2.组件引用

<AceEditor ref="codeEditor" :content.sync="sqlText" lang="mysql" :read-only="false" width="100%" height="600px"></AceEditor>

ace-editor beautify扩展格式化效果极差,建议结合vkbeautify第三方js插件使用。

二、vkbeautify格式化插件

vkBeautify:javascript plugin to pretty-print or minify
text in XML, JSON, CSS and SQL formats.

1.package.json

"dependencies": {
    "vkbeautify": "^0.99.3",
}

2.使用说明

Pretty print

    vkbeautify.xml(text [,indent_pattern]);
    vkbeautify.json(text [,indent_pattern]);
    vkbeautify.css(text [,indent_pattern]);
    vkbeautify.sql(text [,indent_pattern]);

    @text - String; text to beatufy;
    @indent_pattern - Integer | String;
            Integer:  number of white spaces;
            String:   character string to visualize indentation ( can also be a set of white spaces )

Minify

    vkbeautify.xmlmin(text [,preserve_comments]);
    vkbeautify.jsonmin(text);
    vkbeautify.cssmin(text [,preserve_comments]);
    vkbeautify.sqlmin(text);

    @text - String; text to minify;
    @preserve_comments - Bool; [optional];
            Set this flag to true to prevent removing comments from @text ( minxml and mincss functions only. )

Examples

    vkbeautify.xml(text); // pretty print XML
    vkbeautify.json(text, 4 ); // pretty print JSON
    vkbeautify.css(text, '. . . .'); // pretty print CSS
    vkbeautify.sql(text, '----'); // pretty print SQL

    vkbeautify.xmlmin(text, true);// minify XML, preserve comments
    vkbeautify.jsonmin(text);// minify JSON
    vkbeautify.cssmin(text);// minify CSS, remove comments ( default )
    vkbeautify.sqlmin(text);// minify SQL

参考:
Ace editor中文文档
ACE Editor 常规使用方法及注意事项
vkbeautify说明文档
vue中 实现xml的格式化和高亮显示

Vue2-AceEditor是一个用于Vue.js应用的 Ace 编辑器插件,它允许你在组件中嵌入一个富文本编辑器。要在Vue2项目中完成代码编写并运行,你可以按照以下步骤操作: 1. **安装插件**: 首先,在你的Vue项目中安装`vue2-ace-editor`。如果你使用的是npm,可以在`package.json`文件里通过命令行输入: ``` npm install vue2-ace-editor --save ``` 或者使用yarn: ``` yarn add vue2-ace-editor ``` 2. **导入并在模板中使用**: 在`main.js`或其他需要的地方,引入编辑器组件,并在你的Vue组件模板中使用它: ```html <template> <div> <vue2-ace-editor :mode="editorMode" :value="codeValue" @change="handleCodeChange"></vue2-ace-editor> </div> </template> ``` 3. **设置初始化选项**: 定义`editorMode`和`codeValue`的数据属性,例如设置初始模式(如javascript)和默认代码: ```js import { Vue2AceEditor } from 'vue2-ace-editor'; export default { components: { Vue2AceEditor, }, data() { return { editorMode: 'javascript', // 指定语言模式 codeValue: '', // 初始代码内容 }; }, methods: { handleCodeChange(newValue) { console.log('代码更改:', newValue); // 这里可以将新代码值保存到服务器或本地进行实时预览、测试等操作 }, }, } ``` 4. **添加事件监听**: `@change`事件会在用户修改代码后触发,你可以在`handleCodeChange`方法中处理用户的代码。 5. **运行代码**: 为了运行代码,你需要有相应的环境,比如在浏览器中打开包含这个Vue组件的页面即可。如果你想在浏览器之外运行,可以考虑构建成Web Worker或者其他运行环境,这取决于你的实际需求。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

搬山境KL攻城狮

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值