index.vue 1.6 KB
Newer Older
P
Pan 已提交
1
<template>
P
Pan 已提交
2
  <div class="json-editor">
J
Jere 已提交
3
    <textarea ref="textarea" />
P
Pan 已提交
4 5 6 7 8 9 10 11 12 13 14 15 16 17
  </div>
</template>

<script>
import CodeMirror from 'codemirror'
import 'codemirror/addon/lint/lint.css'
import 'codemirror/lib/codemirror.css'
import 'codemirror/theme/rubyblue.css'
require('script-loader!jsonlint')
import 'codemirror/mode/javascript/javascript'
import 'codemirror/addon/lint/lint'
import 'codemirror/addon/lint/json-lint'

export default {
18 19 20
  name: 'JsonEditor',
  /* eslint-disable vue/require-prop-types */
  props: ['value'],
P
Pan 已提交
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
  data() {
    return {
      jsonEditor: false
    }
  },
  watch: {
    value(value) {
      const editor_value = this.jsonEditor.getValue()
      if (value !== editor_value) {
        this.jsonEditor.setValue(JSON.stringify(this.value, null, 2))
      }
    }
  },
  mounted() {
    this.jsonEditor = CodeMirror.fromTextArea(this.$refs.textarea, {
      lineNumbers: true,
      mode: 'application/json',
      gutters: ['CodeMirror-lint-markers'],
      theme: 'rubyblue',
      lint: true
    })

    this.jsonEditor.setValue(JSON.stringify(this.value, null, 2))
    this.jsonEditor.on('change', cm => {
      this.$emit('changed', cm.getValue())
      this.$emit('input', cm.getValue())
    })
  },
  methods: {
    getValue() {
      return this.jsonEditor.getValue()
    }
  }
}
</script>

P
Pan 已提交
57 58
<style scoped>
.json-editor{
P
Pan 已提交
59
  height: 100%;
P
Pan 已提交
60
  position: relative;
P
Pan 已提交
61
}
P
Pan 已提交
62 63 64 65
.json-editor >>> .CodeMirror {
  height: auto;
  min-height: 300px;
}
P
Pan 已提交
66 67 68
.json-editor >>> .CodeMirror-scroll{
  min-height: 300px;
}
P
Pan 已提交
69
.json-editor >>> .cm-s-rubyblue span.cm-string {
P
Pan 已提交
70 71 72
  color: #F08047;
}
</style>