index.vue 1.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. <template>
  2. <div class="json-editor">
  3. <textarea ref="textarea"></textarea>
  4. </div>
  5. </template>
  6. <script>
  7. import CodeMirror from 'codemirror'
  8. import 'codemirror/addon/lint/lint.css'
  9. import 'codemirror/lib/codemirror.css'
  10. import 'codemirror/theme/rubyblue.css'
  11. require('script-loader!jsonlint')
  12. import 'codemirror/mode/javascript/javascript'
  13. import 'codemirror/addon/lint/lint'
  14. import 'codemirror/addon/lint/json-lint'
  15. export default {
  16. name: 'jsonEditor',
  17. data() {
  18. return {
  19. jsonEditor: false
  20. }
  21. },
  22. props: ['value'],
  23. watch: {
  24. value(value) {
  25. const editor_value = this.jsonEditor.getValue()
  26. if (value !== editor_value) {
  27. this.jsonEditor.setValue(JSON.stringify(this.value, null, 2))
  28. }
  29. }
  30. },
  31. mounted() {
  32. this.jsonEditor = CodeMirror.fromTextArea(this.$refs.textarea, {
  33. lineNumbers: true,
  34. mode: 'application/json',
  35. gutters: ['CodeMirror-lint-markers'],
  36. theme: 'rubyblue',
  37. lint: true
  38. })
  39. this.jsonEditor.setValue(JSON.stringify(this.value, null, 2))
  40. this.jsonEditor.on('change', cm => {
  41. this.$emit('changed', cm.getValue())
  42. this.$emit('input', cm.getValue())
  43. })
  44. },
  45. methods: {
  46. getValue() {
  47. return this.jsonEditor.getValue()
  48. }
  49. }
  50. }
  51. </script>
  52. <style scoped>
  53. .json-editor{
  54. height: 100%;
  55. position: relative;
  56. }
  57. .json-editor >>> .CodeMirror {
  58. height: auto;
  59. min-height: 300px;
  60. }
  61. .json-editor >>> .CodeMirror-scroll{
  62. min-height: 300px;
  63. }
  64. .json-editor >>> .cm-s-rubyblue span.cm-string {
  65. color: #F08047;
  66. }
  67. </style>