vue利用CodeMirrorr+json-lint实现可编辑可自动格式化的Json-Editor组件

news/2024/7/10 1:49:56 标签: jsonEditor, CodeMirror, jsonlint, vue, 编辑json组件

Json-Editor is base on CodeMirrorr. Lint base on json-lint.

老规矩先来张效果图
在这里插入图片描述

下面捋一下实现步骤

CodeMirrorrjsonlint_7">1、安装CodeMirrorr、jsonlint

npm install codemirror
npm install jsonlint

在这里插入图片描述

2、编写Json-Editor组件

在如下位置新建JsonEditor文件夹及index文件
在这里插入图片描述
index文件全部内容如下:

<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/rubyblue.css'
require('script-loader!jsonlint')
import 'codemirror/mode/javascript/javascript'
import 'codemirror/addon/lint/lint'
import 'codemirror/addon/lint/json-lint'

export default {
  name: 'JsonEditor',
  /* eslint-disable vue/require-prop-types */
  props: ['value'],
  data() {
    return {
      jsonEditor: false
    }
  },
  watch: {
    value(value) {
      const editorValue = this.jsonEditor.getValue()
      if (value !== editorValue) {
        this.jsonEditor.setValue(JSON.stringify(this.value, null, 2))
      }
    }
  },
  mounted() {
    // CodeMirror.fromTextArea()中第一个参数是DOM元素,而且必须是textarea元素;第二个参数是可选配置项
    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>

<style scoped>
.json-editor{
  height: 100%;
  position: relative;
}
.json-editor >>> .CodeMirror {
  height: auto;
  min-height: 300px;
}
.json-editor >>> .CodeMirror-scroll{
  min-height: 300px;
}
.json-editor >>> .cm-s-rubyblue span.cm-string {
  color: #F08047;
}
.addbtn{
  margin-bottom: 15px;
  margin-left: 30px;
}
</style>

3、利用json-editor组件实现自己的逻辑,我就不放自己的业务代码了,下面是一个小demo可以参考

<template>
  <div class="components-container">
    <div class="editor-container">
      <json-editor ref="jsonEditor" v-model="value" />
    </div>
  </div>
</template>

<script>
import JsonEditor from '@/components/JsonEditor'

const jsonData = '[{"items":[{"market_type":"forexdata","symbol":"XAUUSD"},{"market_type":"forexdata","symbol":"UKOIL"},{"market_type":"forexdata","symbol":"CORN"}],"name":""},{"items":[{"market_type":"forexdata","symbol":"XAUUSD"},{"market_type":"forexdata","symbol":"XAGUSD"},{"market_type":"forexdata","symbol":"AUTD"},{"market_type":"forexdata","symbol":"AGTD"}],"name":"贵金属"},{"items":[{"market_type":"forexdata","symbol":"CORN"},{"market_type":"forexdata","symbol":"WHEAT"},{"market_type":"forexdata","symbol":"SOYBEAN"},{"market_type":"forexdata","symbol":"SUGAR"}],"name":"农产品"},{"items":[{"market_type":"forexdata","symbol":"UKOIL"},{"market_type":"forexdata","symbol":"USOIL"},{"market_type":"forexdata","symbol":"NGAS"}],"name":"能源化工"}]'

export default {
  name: 'JsonEditorDemo',
  components: { JsonEditor },
  data() {
    return {
      value: JSON.parse(jsonData)
    }
  }
}
</script>

<style scoped>
.editor-container{
  position: relative;
  height: 100%;
}
</style>

https://github.com/PanJiaChen/vue-element-admin/,感谢潘大佬提供给我们这么好的后台集成系统,有需要的可以自己去瞅瞅


http://www.niftyadmin.cn/n/1486373.html

相关文章

android:cliptopadding=quot;falsequot;,android:clipToPadding和android:clipChildren

**android:clipToPadding **设置该控件的绘制范围是否在Padding里面。当clipToPadding为true时&#xff0c;布局不能绘制到padding区域。当clipToPadding为false时&#xff0c;表示布局能够被绘制到padding区域。默认clipToPadding为true。具体举例&#xff1a;当我们为ListVie…

你所不知道的Excel之仿系统查询

之前的毕设是咨询系统&#xff0c;雏形的时候曾经被导师吐槽过我们的系统跟excel表格一样&#xff0c;体现不出来作为一个系统的价值。当时心里十分不服气&#xff0c;现在……咳……真香&#xff0c;excel是真滴强&#x1f44d; 现在来讲讲excel如何可以做到与一个简单的查询系…

html语言考点,计算机一级考试考点:Frontpage操作

计算机一级考试考点&#xff1a;Frontpage操作计算机一级考试分为DOS版和Windows版&#xff0c;考核应试者计算机基本知识和使用微机系统的初步能力。下文是为大家精选的计算机一级考试考点&#xff1a;Frontpage操作&#xff0c;欢迎大家阅读。1、图片的对齐、显示文本等属性设…

BZOJ2631 tree LCT

欢迎访问~原文出处——博客园-zhouzhendong 去博客园看该题解 题目传送门 - BZOJ2631 题意概括 一棵n个节点的树&#xff0c;每一个节点有一个权值&#xff0c;m次操作。 要支持操作有&#xff1a;删边、连边、区间求和、区间加、区间乘。 保证操作过程中不出现环。 n,m<100…

怎么解决Chrome浏览器一打开就显示“喔唷 崩溃啦“

老是显示“喔唷 崩溃啦”都给我整崩溃了&#xff0c; 每次使用前打开属性中的兼容模式就可以继续使用。但是每天都得来几次&#xff0c;未免也太麻烦&#xff01;&#xff01;&#xff01; 找遍全网终于让我找到了解决办法&#xff01;&#xff01; 找到图中位置&#xff0c…

android ums名字,大容量存储(U盘配置)(com.sjj.echo.umsinterface) - 1.4.1 - 应用 - 酷安

后面酷安不再更新, 关于相关问题, 请关注 github.UMSInterface APP后面不再维护. 如果仍然想使用此APP, 可参考 APP Description. 下面会提供一个替代方法来实现同样的功能.基本功能:使用 usb gadget 驱动使指定的镜像或块设备可作为 USB Drive 被PC访问; 同时将此镜像或块设备…

sql语句中对字段自动补足

Oracle,Mysql中 LPAD(需要被补足的字段,总长度,被用来补足的字符)--------------由左边开始补足 Sql Server REPLICATE(被用来补足的字符,总长度-len(需要被补足的字段))需要被补足的字段转载于:https://www.cnblogs.com/pinnasky/archive/2010/09/08/1821476.html

止步不前的 Log4j 将何去何从

提到 Java 的 Logging 机制&#xff0c;我想绝大多数的 Java 开发人员都知道 Log4j 。 早在 Java 提供自己的 Logging API 之前&#xff0c; Log4J 就已经声名在外了&#xff0c;以至于当 Java 自己的 Logging API 出现后并没有赢得广泛的支持。 现在的 Java Logger 已经越来越…