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

Merge pull request #85 from chenfengjw163/develop

Develop
This commit is contained in:
陈峰
2021-12-18 16:21:28 +08:00
committed by GitHub
8 changed files with 114 additions and 26 deletions
+7
View File
@@ -160,6 +160,8 @@ import 'vue-json-viewer/style.css'
| `expanded` | Default expand the view | `false` | | `expanded` | Default expand the view | `false` |
| `timeformat` | custom time format function | time => time.toLocaleString() | | `timeformat` | custom time format function | time => time.toLocaleString() |
| `preview-mode` | no expand mode | `false` | | `preview-mode` | no expand mode | `false` |
| `show-array-index` | array show index | `true` |
## Listeners ## Listeners
@@ -173,6 +175,11 @@ import 'vue-json-viewer/style.css'
| ----------- |:------------- | ----------- | | ----------- |:------------- | ----------- |
| `copy` | Custom content for copy button | `{copied: boolean}` | | `copy` | Custom content for copy button | `{copied: boolean}` |
## shortcut keys
| Name | Description | Scope |
| ----------- |:------------- | ----------- |
| `alt` + click | expand all node | |
## Theming ## Theming
To create custom theme, (e.g. `my-awesome-json-theme`), in two easy steps: To create custom theme, (e.g. `my-awesome-json-theme`), in two easy steps:
+19
View File
@@ -154,6 +154,25 @@ import 'vue-json-viewer/style.css'
| `expanded` | 默认展开视图 | `false` | | `expanded` | 默认展开视图 | `false` |
| `timeformat` | 自定义时间格式函数 | time => time.toLocaleString() | | `timeformat` | 自定义时间格式函数 | time => time.toLocaleString() |
| `preview-mode` | 不可折叠模式,默认全部展开 | `false` | | `preview-mode` | 不可折叠模式,默认全部展开 | `false` |
| `show-array-index` | 是否显示数组索引 | `true` |
## 事件
| 事件 | 描述 | 值 |
| ----------- |:------------- | ----------- |
| `copied` | 复制文本后的事件 | |
## Slots
| 名称 | 描述 | Scope |
| ----------- |:------------- | ----------- |
| `copy` | 自定义拷贝按钮 | `{copied: boolean}` |
## 快捷键
| 名称 | 描述 |
| ----------- |:------------- |
| `alt` + click | 展开当前节点下的所有节点 |
## 主题 ## 主题
+1
View File
@@ -40,6 +40,7 @@ new Vue({
align: 'left' align: 'left'
}} }}
boxed boxed
show-array-index={false}
timeformat={time => new Date(time)} timeformat={time => new Date(time)}
sort></json-viewer> sort></json-viewer>
<hr /> <hr />
+28 -4
View File
@@ -26,19 +26,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) {
@@ -64,7 +76,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 {
open: !!this.expand open: !!this.expand
}, },
on: { on: {
click: this.toggle click: (event) => {
if (event.altKey) {
this.toggleAll()
} else {
this.toggle()
}
}
} }
})) }))
} }
@@ -117,10 +135,16 @@ 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,
}, },
on: { on: {
'update:expand': value => { 'update:expand': value => {
this.expand = value this.expand = value
},
'update: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 () {
+27 -12
View File
@@ -18,7 +18,9 @@ export default {
}, },
sort: Boolean, sort: Boolean,
expand: Boolean, expand: Boolean,
forceExpand: Boolean,
previewMode: Boolean, previewMode: Boolean,
showArrayIndex: Boolean,
}, },
data() { data() {
return { return {
@@ -47,7 +49,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) {
@@ -56,8 +64,7 @@ export default {
evt.initEvent('resized', true, false) evt.initEvent('resized', true, false)
this.$el.dispatchEvent(evt) this.$el.dispatchEvent(evt)
} }
}, }
}, },
render (h) { render (h) {
let elements = [] let elements = []
@@ -69,7 +76,13 @@ export default {
'open': !!this.expand, 'open': !!this.expand,
}, },
on: { on: {
click: this.toggle click: (event) => {
if (event.altKey) {
this.toggleAll()
} else {
this.toggle()
}
}
} }
})) }))
} }
@@ -87,15 +100,14 @@ export default {
this.value.forEach((value, key) => { this.value.forEach((value, key) => {
elements.push(h(JsonBox, { elements.push(h(JsonBox, {
key, key,
style: {
display: this.expand ? undefined : 'none'
},
props: { props: {
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,
} }
})) }))
}) })
@@ -103,14 +115,17 @@ export default {
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,
}, },
on: { on: {
click: this.toggle click: (event) => {
if (event.altKey) {
this.toggleAll()
} else {
this.toggle()
}
}
}, },
attrs: { attrs: {
title: `click to reveal ${this.value.length} hidden items` title: `click to reveal ${this.value.length} hidden items`
+22 -8
View File
@@ -17,8 +17,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 {
@@ -56,6 +58,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 {
'open': !!this.expand, 'open': !!this.expand,
}, },
on: { on: {
click: this.toggle click: (event) => {
if (event.altKey) {
this.toggleAll()
} else {
this.toggle()
}
}
} }
})) }))
} }
@@ -99,15 +111,14 @@ export default {
elements.push(h(JsonBox, { elements.push(h(JsonBox, {
key, key,
style: {
display: !this.expand ? 'none' : undefined
},
props: { props: {
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,
} }
})) }))
} }
@@ -116,14 +127,17 @@ 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,
}, },
on: { on: {
click: this.toggle click: (event) => {
if (event.altKey) {
this.toggleAll()
} else {
this.toggle()
}
}
}, },
attrs: { attrs: {
title: `click to reveal object content (keys: ${Object.keys(this.ordered).join(', ')})` title: `click to reveal object content (keys: ${Object.keys(this.ordered).join(', ')})`
+1 -1
View File
@@ -1,6 +1,6 @@
{ {
"name": "vue-json-viewer", "name": "vue-json-viewer",
"version": "2.2.20", "version": "2.2.21",
"description": "vuejs展示json的组件", "description": "vuejs展示json的组件",
"main": "vue-json-viewer.js", "main": "vue-json-viewer.js",
"files": ["vue-json-viewer.js", "ssr.js", "style.css"], "files": ["vue-json-viewer.js", "ssr.js", "style.css"],