• 利用vue-codemirror展示编辑json数据


    安装

    由于项目用的是vue2,因此只能安装vue-codemirror@4.x,依赖codemirror@5.x

    npm install -S codemirror@5.x
    npm install -S vue-codemirror@4.x
    
    • 1
    • 2

    codemirror@5.65.7
    vue-codemirror@4.0.6

    自定义组件

    参考了vue json-viewer codemirror-json格式化

    创建JsonEditor.vue

    代码如下:

    <template>
      <div class="json-editor">
        <textarea ref="textarea"/>
      div>
    template>
    
    <script>
    import CodeMirror from 'codemirror'
    import 'codemirror/addon/lint/lint.css'
    import 'codemirror/lib/codemirror.css'
    import 'codemirror/theme/panda-syntax.css'
    import 'codemirror/mode/javascript/javascript'
    import 'codemirror/addon/lint/json-lint'
    // 折叠代码
    import 'codemirror/addon/fold/foldgutter.css';
    import 'codemirror/addon/fold/foldcode.js';
    import 'codemirror/addon/fold/foldgutter.js';
    import 'codemirror/addon/fold/brace-fold.js';
    import 'codemirror/addon/fold/xml-fold.js';
    import 'codemirror/addon/fold/indent-fold.js';
    import 'codemirror/addon/fold/markdown-fold.js';
    import 'codemirror/addon/fold/comment-fold.js';
    
    export default {
      name: 'JsonEditor',
      props: ['value'],
      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',"CodeMirror-linenumbers","CodeMirror-foldgutter"],
          theme: 'panda-syntax',
          lint: true,
          foldGutter: {
            rangeFinder: new CodeMirror.fold.combine(CodeMirror.fold.indent,CodeMirror.fold.comment)
          },
        })
    
        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>
    
    <style scoped>
    .json-editor {
      height: 100%;
      position: relative;
    }
    .json-editor >>> .CodeMirror {
      height: auto;
      min-height: 180px;
    }
    .json-editor >>> .CodeMirror-scroll {
      min-height: 180px;
    }
    .json-editor >>> .cm-s-rubyblue span.cm-string {
      color: #f08047;
    }
    style>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 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
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81

    主题设置

    参考codemirror主题效果概览选一套喜欢的主题

    • 引入主题样式,例:import 'codemirror/theme/panda-syntax.css'
    • 配置主题,例:
    ...
        this.jsonEditor = CodeMirror.fromTextArea(this.$refs.textarea, {
          ...
          theme: 'panda-syntax',
          ...
        })
    ...
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    代码折叠设置

    • 引入代码折叠js
    import 'codemirror/addon/fold/foldgutter.css';
    import 'codemirror/addon/fold/foldcode.js';
    import 'codemirror/addon/fold/foldgutter.js';
    import 'codemirror/addon/fold/brace-fold.js';
    import 'codemirror/addon/fold/xml-fold.js';
    import 'codemirror/addon/fold/indent-fold.js';
    import 'codemirror/addon/fold/markdown-fold.js';
    import 'codemirror/addon/fold/comment-fold.js';
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 配置代码折叠
        this.jsonEditor = CodeMirror.fromTextArea(this.$refs.textarea, {
          ...
          gutters: [...,"CodeMirror-linenumbers","CodeMirror-foldgutter"],
          foldGutter: {
            rangeFinder: new CodeMirror.fold.combine(CodeMirror.fold.indent,CodeMirror.fold.comment)
          },
        })
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    最终效果

    在这里插入图片描述

    构建模块js

    vue build -t lib -n JsonEditor -d public/dist/json-editor src/components/JsonEditor.vue
    
    • 1

    在这里插入图片描述

  • 相关阅读:
    16.在一行中懒加载图片
    Linux基本指令(二)
    推荐系统-概述:基本架构
    二叉树相关OJ - C++
    什么是单点登录?什么又是 OAuth2.0?
    Xilinx MicroBlaze定时器中断无法返回主函数问题解决
    10.17课上(七段显示器,递归异或与电路)
    playwright自动化项目搭建
    点亮三盏灯
    按图搜索义乌购商品(拍立淘) API
  • 原文地址:https://blog.csdn.net/hbh112233abc/article/details/126006860