Vue-Codemirror-Lite: 在线代码编辑器 Codemirror 的轻量级 Vue.js 组件

Vue-Codemirror-Lite

项目地点:https://github.com/cnu4/vue-codemirror-lite

CodeMirror Component For Vue.js (support 1.x and 2.x).

Lightweight

为了削减打包后的体积,一切的言语形式和插件在默许的情况下没有加载, 要使用它们,
Using Language Modes and Addons.

Demo

Live Demo: https://cnu4.github.io/vue-codemirror-lite

当地运转实例:

npm install && npm run dev

Installation

npm install vue-codemirror-lite

Usage

// Require in Webpack
var Vue = require('vue')
var VueCodeMirror = require('vue-codemirror-lite')

Vue.use(VueCodeMirror)

// Or use as component (ES6)
import Vue from 'vue'
import { codemirror } from 'vue-codemirror-lite'

export default {
  components: {
    codemirror
  }
}

Usage in component


<!-- simple -->
<codemirror :value="code"></codemirror>

<!-- simple (with bindings in Vue1.x) -->
<codemirror :value.sync="code"></codemirror>

<!-- simple (with bindings in Vue2.x) -->
<codemirror v-model="code"></codemirror>

<!-- advanced -->
<codemirror
  :value="code"
  :options="editorOption"
  ref="myEditor"
  @change="yourCodeChangeMethod">
</codemirror>
export default {
  data () {
    return {
      code: 'const str = "hello world"'
    }
  },
  computed: {
    editor() {
      // get current editor object
      return this.$refs.myEditor.editor
    }
  },
  mounted() {
    // use editor object...
    this.editor.focus()
    console.log('this is current editor object', this.editor)
  }
}

Properties

  • value String 编辑器的值

  • options Object (newValue) 传递给 CodeMirror 实例的设置选项

前去 CodeMirror Configuration 检察更多可用选项

Using Language Modes and Addons

CodeMirror 中包含了很多的言语形式

默许情况下一切的言语形式和插件在默许的情况下没有加载, 启用它们:

  • 装置 vue-codemirror-lite

  • require vue-codemirror-lite 后 require 一个言语形式或许插件

  • 在 options 对象中设置 mode 选项

<template>
  <codemirror :options="{
    mode: 'javascript',
    extraKeys: {'Ctrl-Space': 'autocomplete'}
  }"></codemirror>
</template>

<script>
  import { codemirror } from 'vue-codemirror-lite'
  require('codemirror/mode/javascript/javascript')
  require('codemirror/mode/vue/vue')

  require('codemirror/addon/hint/show-hint.js')
  require('codemirror/addon/hint/show-hint.css')
  require('codemirror/addon/hint/javascript-hint.js')

  export default {
    ...
  }
</script>

demo 目录下的例子完成了 JavaScript 和 vue 语法高亮和 JavaScript 代码提醒

前去 CodeMirror Manual 检察更多的言语形式和插件.

License

MIT

    原文作者:cnu4
    原文地址: https://segmentfault.com/a/1190000008136780
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞