2
0
mirror of https://github.com/tenrok/vue-json-viewer.git synced 2026-06-23 20:40:38 +03:00

feat: merge develop

This commit is contained in:
fengqi.cf
2021-12-18 16:08:06 +08:00
7 changed files with 109 additions and 26 deletions
+9 -3
View File
@@ -2,7 +2,7 @@
[中文版](https://github.com/chenfengjw163/vue-json-viewer/blob/master/README_CN.md) [中文版](https://github.com/chenfengjw163/vue-json-viewer/blob/master/README_CN.md)
Simple JSON viewer component, for Vue.js 2. Simple JSON viewer component, for Vue.js 2 or 3.
Support for incremental update components Support for incremental update components
@@ -20,12 +20,18 @@ Support for incremental update components
## Installing ## Installing
Using npm: Using npm:
``` ```
$ npm install vue-json-viewer --save $ npm install vue-json-viewer@2 --save
// Vue2
$ npm install vue-json-viewer@3 --save
// Vue3
``` ```
Using yarn: Using yarn:
``` ```
$ yarn add vue-json-viewer $ yarn add vue-json-viewer@2
// Vue2
$ yarn add vue-json-viewer@3
// Vue3
``` ```
## Example ## Example
+9 -3
View File
@@ -1,6 +1,6 @@
# vue-json-viewer # vue-json-viewer
简单易用的json内容展示组件,支持vue@2.0+,支持SSR,组件支持增量渲染即使大文件json也可以快速渲染。 简单易用的json内容展示组件,支持vue@2和3,支持SSR,组件支持增量渲染即使大文件json也可以快速渲染。
[![Travis](https://img.shields.io/travis/chenfengjw163/vue-json-viewer/master.svg?style=flat-square)](https://travis-ci.org/chenfengjw163/vue-json-viewer) [![Travis](https://img.shields.io/travis/chenfengjw163/vue-json-viewer/master.svg?style=flat-square)](https://travis-ci.org/chenfengjw163/vue-json-viewer)
[![npm](https://img.shields.io/npm/v/vue-json-viewer.svg?style=flat-square)](https://www.npmjs.com/package/vue-json-viewer) [![npm](https://img.shields.io/npm/v/vue-json-viewer.svg?style=flat-square)](https://www.npmjs.com/package/vue-json-viewer)
@@ -14,12 +14,18 @@
## 安装 ## 安装
使用 npm: 使用 npm:
``` ```
$ npm install vue-json-viewer --save $ npm install vue-json-viewer@2 --save
// Vue2
$ npm install vue-json-viewer@3 --save
// Vue3
``` ```
使用 yarn: 使用 yarn:
``` ```
$ yarn add vue-json-viewer $ yarn add vue-json-viewer@2
// Vue2
$ yarn add vue-json-viewer@3
// Vue3
``` ```
## 示例 ## 示例
+5
View File
@@ -0,0 +1,5 @@
{
"dependencies": {
"vue-json-viewer": "^3.0.1"
}
}
+28 -4
View File
@@ -27,19 +27,31 @@ export default {
default: 0 default: 0
}, },
previewMode: Boolean, previewMode: Boolean,
forceExpand: Boolean,
showArrayIndex: Boolean,
}, },
data() { data() {
return { return {
expand: true expand: true,
forceExpandMe: this.forceExpand,
} }
}, },
mounted() { mounted() {
this.expand = this.previewMode || (this.depth >= this.expandDepth ? false : true) this.expand = this.previewMode || (this.depth >= this.expandDepth ? false : true) || this.forceExpandMe
}, },
methods: { methods: {
toggle() { toggle() {
this.expand = !this.expand this.expand = !this.expand
this.dispatchEvent()
},
toggleAll() {
this.expand = !this.expand
this.forceExpandMe = this.expand
this.dispatchEvent()
},
dispatchEvent() {
try { try {
this.$el.dispatchEvent(new Event('resized')) this.$el.dispatchEvent(new Event('resized'))
} catch (e) { } catch (e) {
@@ -65,7 +77,7 @@ export default {
let elements = [] let elements = []
let dataType let dataType
if (this.value === null || this.value === undefined) { if (this.value === null || this.value === undefined) {
dataType = JsonUndefined dataType = JsonUndefined
} else if (Array.isArray(this.value)) { } else if (Array.isArray(this.value)) {
dataType = JsonArray dataType = JsonArray
@@ -90,7 +102,13 @@ export default {
'jv-toggle': true, 'jv-toggle': true,
open: !!this.expand open: !!this.expand
}, },
onClick: this.toggle onClick: (event) => {
if (event.altKey) {
this.toggleAll()
} else {
this.toggle()
}
}
})) }))
} }
@@ -113,8 +131,14 @@ export default {
depth: this.depth, depth: this.depth,
expand: this.expand, expand: this.expand,
previewMode: this.previewMode, previewMode: this.previewMode,
forceExpand: this.forceExpandMe,
showArrayIndex: this.showArrayIndex,
'onUpdate:expand': value => { 'onUpdate:expand': value => {
this.expand = value this.expand = value
},
'onUpdate:expandAll': value => {
this.expand = value
this.forceExpandMe = this.expand
} }
})) }))
+9 -1
View File
@@ -1,5 +1,8 @@
<template> <template>
<div ref="viewer" :class="jvClass"> <div
ref="viewer"
:class="jvClass"
>
<div <div
v-if="copyable" v-if="copyable"
:class="`jv-tooltip ${copyText.align || 'right'}`" :class="`jv-tooltip ${copyText.align || 'right'}`"
@@ -26,6 +29,7 @@
:value="value" :value="value"
:sort="sort" :sort="sort"
:preview-mode="previewMode" :preview-mode="previewMode"
:show-array-index="showArrayIndex"
/> />
</div> </div>
<div <div
@@ -88,6 +92,10 @@ export default {
previewMode: { previewMode: {
type: Boolean, type: Boolean,
default: false, default: false,
},
showArrayIndex: {
type: Boolean,
default: true,
} }
}, },
provide () { provide () {
+26 -8
View File
@@ -19,7 +19,9 @@ export default {
}, },
sort: Boolean, sort: Boolean,
expand: Boolean, expand: Boolean,
forceExpand: Boolean,
previewMode: Boolean, previewMode: Boolean,
showArrayIndex: Boolean,
}, },
data() { data() {
return { return {
@@ -48,7 +50,13 @@ export default {
}, },
toggle() { toggle() {
this.$emit('update:expand', !this.expand) this.$emit('update:expand', !this.expand)
this.dispatchEvent();
},
toggleAll() {
this.$emit('update:expandAll', !this.expand)
this.dispatchEvent();
},
dispatchEvent() {
try { try {
this.$el.dispatchEvent(new Event('resized')) this.$el.dispatchEvent(new Event('resized'))
} catch (e) { } catch (e) {
@@ -58,7 +66,6 @@ export default {
this.$el.dispatchEvent(evt) this.$el.dispatchEvent(evt)
} }
}, },
}, },
render () { render () {
let elements = [] let elements = []
@@ -69,7 +76,13 @@ export default {
'jv-toggle': true, 'jv-toggle': true,
'open': !!this.expand, 'open': !!this.expand,
}, },
onClick: this.toggle onClick: (event) => {
if (event.altKey) {
this.toggleAll()
} else {
this.toggle()
}
}
})) }))
} }
@@ -88,23 +101,28 @@ export default {
display: this.expand ? undefined : 'none' display: this.expand ? undefined : 'none'
}, },
sort: this.sort, sort: this.sort,
keyName: `${key}`, keyName: this.showArrayIndex ? `${key}`: '',
depth: this.depth + 1, depth: this.depth + 1,
value, value,
previewMode: this.previewMode, previewMode: this.previewMode,
forceExpand: this.forceExpand,
showArrayIndex: this.showArrayIndex,
})) }))
}) })
} }
if (!this.expand && this.value.length) { if (!this.expand && this.value.length) {
elements.push(h('span', { elements.push(h('span', {
style: {
display: undefined
},
class: { class: {
'jv-ellipsis': true, 'jv-ellipsis': true,
}, },
onClick: this.toggle, onClick: (event) => {
if (event.altKey) {
this.toggleAll()
} else {
this.toggle()
}
},
title: `click to reveal ${this.value.length} hidden items`, title: `click to reveal ${this.value.length} hidden items`,
innerText: '...' innerText: '...'
})) }))
+22 -6
View File
@@ -18,8 +18,10 @@ export default {
default: 0 default: 0
}, },
expand: Boolean, expand: Boolean,
forceExpand: Boolean,
sort: Boolean, sort: Boolean,
previewMode: Boolean, previewMode: Boolean,
showArrayIndex: Boolean,
}, },
data() { data() {
return { return {
@@ -57,6 +59,10 @@ export default {
this.$emit('update:expand', !this.expand) this.$emit('update:expand', !this.expand)
this.dispatchEvent(); this.dispatchEvent();
}, },
toggleAll() {
this.$emit('update:expandAll', !this.expand)
this.dispatchEvent();
},
dispatchEvent() { dispatchEvent() {
try { try {
this.$el.dispatchEvent(new Event('resized')) this.$el.dispatchEvent(new Event('resized'))
@@ -77,7 +83,13 @@ export default {
'jv-toggle': true, 'jv-toggle': true,
'open': !!this.expand, 'open': !!this.expand,
}, },
onClick: this.toggle onClick: (event) => {
if (event.altKey) {
this.toggleAll()
} else {
this.toggle()
}
}
})) }))
} }
@@ -99,12 +111,13 @@ export default {
style: { style: {
display: !this.expand ? 'none' : undefined display: !this.expand ? 'none' : undefined
}, },
sort: this.sort, sort: this.sort,
keyName: key, keyName: key,
depth: this.depth + 1, depth: this.depth + 1,
value, value,
previewMode: this.previewMode, previewMode: this.previewMode,
forceExpand: this.forceExpand,
showArrayIndex: this.showArrayIndex,
})) }))
} }
} }
@@ -112,13 +125,16 @@ export default {
if (!this.expand && Object.keys(this.value).length) { if (!this.expand && Object.keys(this.value).length) {
elements.push(h('span', { elements.push(h('span', {
style: {
display: this.expand ? 'none' : undefined
},
class: { class: {
'jv-ellipsis': true, 'jv-ellipsis': true,
}, },
onClick: this.toggle, onClick: (event) => {
if (event.altKey) {
this.toggleAll()
} else {
this.toggle()
}
},
title: `click to reveal object content (keys: ${Object.keys(this.ordered).join(', ')})`, title: `click to reveal object content (keys: ${Object.keys(this.ordered).join(', ')})`,
innerText: '...' innerText: '...'
})) }))