首页 > 其他分享 >vue3集成jsoneditor

vue3集成jsoneditor

时间:2023-09-06 15:13:38浏览次数:48  
标签:集成 props state json editor vue3 line jsoneditor

一、背景

之前在做录制回放平台的时候,需要前端展示子调用信息,子调用是一个请求列表数组结构,jsoneditor对数组的默认展示结构是[0].[1].[2]..的方式,为了达到如下的效果,必须用到 onNodeName的钩子函数,因此深入调研了下vue3如何集成jsoneditor

最后做出来的效果图

onNodeName的参考文档 https://github.com/josdejong/jsoneditor/blob/master/docs/api.md

二、参考方案

json-editor-vue3
感谢这位老哥的方案,我看了下源码,没有满足我的需要,核心就是属性需要自己加,因此我拿着他的代码改了下

三、代码实现

  • 安装依赖 jsoneditor
npm install --save jsoneditor

jsoneditor是个开源的js的组件,参考文档 https://github.com/josdejong/jsoneditor

  • 编写组件

目录结构如下

vue3-json-editor.tsx:
其中options的定义是完全参考jsoneditor的api文档的,具体需要什么功能,自己去实现对应的options即可!

import { ComponentPublicInstance, defineComponent, getCurrentInstance, onMounted, reactive, watch } from 'vue'
// @ts-ignore
// eslint-disable-next-line import/extensions
import JsonEditor from 'jsoneditor';
import 'jsoneditor/dist/jsoneditor.min.css';
// eslint-disable-next-line import/prefer-default-export
export const Vue3JsonEditor = defineComponent({
  props: {
    modelValue: [String, Boolean, Object, Array],
    showBtns: [Boolean],
    expandedOnStart: {
      type: Boolean,
      default: false
    },
    navigationBar: {
      type: Boolean,
      default: true
    },
    mode: {
      type: String,
      default: 'tree'
    },
    modes: {
      type: Array,
      default () {
        return ['tree', 'code', 'form', 'text', 'view']
      }
    },
    lang: {
      type: String,
      default: 'en'
    },
    onNodeName: {
      type: Function,
      default: ()=>{}
    }
  },
  setup (props: any, { emit }) {
    const root = getCurrentInstance()?.root.proxy as ComponentPublicInstance

    const state = reactive({
      editor: null as any,
      error: false,
      json: {},
      internalChange: false,
      expandedModes: ['tree', 'view', 'form'],

      uid: `jsoneditor-vue-${getCurrentInstance()?.uid}`
    })

    watch(
      () => props.modelValue as unknown as any,
      async (val) => {
        if (!state.internalChange) {
          state.json = val
          // eslint-disable-next-line no-use-before-define
          await setEditor(val)
          state.error = false
          // eslint-disable-next-line no-use-before-define
          expandAll()
        }
      }, { immediate: true })

    onMounted(() => {
      //这个options的定义是完全参考jsoneditor的api文档的
      const options = {
        mode: props.mode,
        modes: props.modes,
        onChange () {
          try {
            const json = state.editor.get()
            state.json = json
            state.error = false
            // eslint-disable-next-line vue/custom-event-name-casing
            emit('json-change', json)
            state.internalChange = true
            emit('input', json)
            root.$nextTick(function () {
              state.internalChange = false
            })
          } catch (e) {
            state.error = true
            // eslint-disable-next-line vue/custom-event-name-casing
            emit('has-error', e)
          }
        },
        onNodeName(v: object) {
          // eslint-disable-next-line vue/custom-event-name-casing
            return props.onNodeName(v);
        },

        onModeChange () {
          // eslint-disable-next-line no-use-before-define
          expandAll()
        },
        navigationBar: props.navigationBar
      }
      state.editor = new JsonEditor(
        document.querySelector(`#${state.uid}`),
        options,
        state.json
      )

      // eslint-disable-next-line vue/custom-event-name-casing
      emit('provide-editor', state.editor)
    })

    function expandAll () {
      if (props.expandedOnStart && state.expandedModes.includes(props.mode)) {
        (state.editor as any).expandAll()
      }
    }

    function onSave () {
      // eslint-disable-next-line vue/custom-event-name-casing
      emit('json-save', state.json)
    }

    function setEditor (value: any): void {
      if (state.editor) state.editor.set(value)
    }

    return () => {
      // @ts-ignore
      // @ts-ignore
      return (
        <div>
          <div id={state.uid} class={'jsoneditor-vue'}></div>
        </div>
      )
    }
  }
})

style.css

.ace_line_group {
  text-align: left;
}
.json-editor-container {
  display: flex;
  width: 100%;
}
.json-editor-container .tree-mode {
  width: 50%;
}
.json-editor-container .code-mode {
  flex-grow: 1;
}
.jsoneditor-btns {
  text-align: center;
  margin-top: 10px;
}
.jsoneditor-vue .jsoneditor-outer {
  min-height: 150px;
}
.jsoneditor-vue div.jsoneditor-tree {
  min-height: 350px;
}
.json-save-btn {
  background-color: #20a0ff;
  border: none;
  color: #fff;
  padding: 5px 10px;
  border-radius: 5px;
  cursor: pointer;
}
.json-save-btn:focus {
  outline: none;
}
.json-save-btn[disabled] {
  background-color: #1d8ce0;
  cursor: not-allowed;
}
code {
  background-color: #f5f5f5;
}

index.ts

export * from './vue3-json-editor';

四、如何使用

<template>
  <div class="container">
    <Vue3JsonEditor
        v-model="json"
        mode='view'
        :show-btns="true"
        :on-node-name="onNodeName"
    />
  </div>
</template>

<script lang="ts" setup>
import {computed,} from 'vue'
import {Vue3JsonEditor} from "@/components/json-editor";


const props = defineProps({
  record: {
    type: Object,
    default() {
      return {
        request: undefined,
      };
    },
  },
});

const json=computed(()=>{
  const {record} = props;
  return record.subInvocations;
});

// eslint-disable-next-line consistent-return
const onNodeName = (node: {
    value: any; type: any
})=>{
  if (node.type==='object' && node.value.identity) {
    return node.value.identity;
  }
  return undefined;
}

</script>

<script lang="ts">
export default {
  name: 'Invocations',
};
</script>


<style scoped lang="less">
.container {
  padding: 0 20px 20px 20px;
}
</style>

本文由mdnice多平台发布

标签:集成,props,state,json,editor,vue3,line,jsoneditor
From: https://www.cnblogs.com/viogs/p/17682325.html

相关文章

  • vue3+typescript +uniapp中select标签
    <select:value="state.year"@change="handleSelectChange($event.target)"> <option:value="i"v-for="iinstate.yearrange">{{i}}</option> </select> ts的代码:``相当于v-model<se......
  • DSP集成麦克风阵列声源定位模组AR-1105​
    麦克风阵列声源定位模组AR-1105是采用DSP音频处理器集成麦克风阵列声源定位技术进行研发,模组具有全硬件集成.体积小巧,外围电路简洁,无需软件调试,易上手等优点的情况下同时保持反应灵敏,定位准确等特性.模组分为:声音定位核心主板麦克风阵列板声源定位LED显示板声音定位核心主板......
  • 在线CAD的SDK如何集成到网页系统-关于MxCad项目二次开发插件集成
    前言1.很多单位拥有着数量庞大的DWG图纸,部门之间传统的图纸管理和沟通,效率较为低下,而将CAD图纸的浏览,编辑,审图,签章等工作集成到自己的网页系统,将会显著提高工作效率和企业的竞争力。集成到网页系统需要我们在后台服务器部署webcadsdk,部署后的操作界面效果如下: 2.在线CAD功......
  • Vue3 setup 如何添加name
    Vue3setup如何添加name小满zs2022-11-2915:5810778 开启掘金成长之旅!这是我参与「掘金日新计划·12月更文挑战」的第2天,点击查看活动详情Vue3中name有什么用呢?1.在递归组件的时候需要定义name2.配合keep-aliveincludeexclude可以缓存组件3.在Vue有报错或......
  • vue3探索——使用ref与$parent实现父子组件间通信
    在vue3中,可以使用vue3的APIdefineExpose()函数结合ref或者$parent,实现父子组件数据的传递。子组件向父组件传递数据defineExpose()和ref子组件:通过defineExpose()函数,向外暴露响应式数据或者方法//src/components/son.vue<template><div><h1>儿子有${{so......
  • Vue3实现批量打印二维码与条形码
    (二维码与条形码在vue3中的使用)欢迎阅览本篇文章这篇文章是我在工作途中对批量生成二维码的一些见解,例如vue-qr(二维码)与jsbarcode(条形码)在vxe-table表格中的使用,二维码与条形码的批量生成与打印(打印时一页一个码)等。注意!本篇文章的所有代码均使用setup语法糖与TypeScript,请确保......
  • vue3如何监听 props 的变化?
    背景实际开发过程中,当需要通过watch 监听传入的props的某个值的变化,来动态改变组件内部的样式,实现方式如下:exportdefault{name:'countdown',props:{showBox:{type:Boolean,required:true,default:false},},setup(prop......
  • vue3.0 el-table 动态合并单元格
    <el-tablev-resize:34style="margin:10px010px":data="tableData":header-cell-style="{background:'#F6F6F6',height:'10px','text-align':'center'}":......
  • IU5200集成30V的OVP功能,支持I2C接口,3A充电电流,1~4节锂电池升降压充电芯片
    IU5200D是一款自动申请快充输入,开关模式升降压充电管理IC,用于1~4节锂离子电池和锂聚合物电池,以及1~5节磷酸铁锂电池。芯片集成包括4开关MOSFET、输入和充电电流感应电路、电池以及升降压转换器的环路补偿。芯片具有3A的充电电流能力,充电电流可以通过外部电阻灵活可调。IU5200D内置......
  • antd限制开始时间与结束时间范围是30天,并不能选择当前日期之后的日期 vue3(默认展示近7
    <a-range-picker:value="hackValue||dateArr":disabled-date="disabledDate"style="width:240px"separator="~":allow-cl......