From 3b056e20793ba0651cecbbc4446a9ebe56d66c35 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=A3=8E=E6=A3=8B?= Date: Fri, 13 Nov 2020 11:40:05 +0800 Subject: [PATCH] feat: support long string expand --- History.md | 6 ++- lib/types/json-string.vue | 88 +++++++++++++++++++++++++++++---------- package.json | 2 +- 3 files changed, 73 insertions(+), 23 deletions(-) diff --git a/History.md b/History.md index 55fa98d..084caa7 100644 --- a/History.md +++ b/History.md @@ -1,4 +1,8 @@ -## 2.2.14 2020-10-13 +## 2.2.16 2020-11-13 + +- support long string expand [issues](https://github.com/chenfengjw163/vue-json-viewer/issues/60) + +## 2.2.15 2020-10-13 - Add Preview Mode diff --git a/lib/types/json-string.vue b/lib/types/json-string.vue index c4143c6..7b21736 100644 --- a/lib/types/json-string.vue +++ b/lib/types/json-string.vue @@ -3,38 +3,84 @@ const REG_LINK = /^\w+:\/\//; export default { name: 'JsonString', - functional: true, props: { jsonValue: { type: String, required: true } }, - render (h, { props }) { - let value = props.jsonValue; + data() { + return { + expand: false, + canExtend: false, + } + }, + mounted() { + if (this.$refs.itemRef.offsetHeight > this.$refs.holderRef.offsetHeight) { + this.canExtend = true; + } + }, + methods: { + toggle() { + this.expand = !this.expand; + } + }, + render (h) { + let value = this.jsonValue; const islink = REG_LINK.test(value) - let domProps - - if (islink) { - value = `${value}`; - domProps = { - innerHTML: `"${value.toString()}"` - } + let domItem + + if (this.expand) { + domItem = { + class: { + 'jv-ellipsis': true, + }, + on: { + click: this.toggle + }, + domProps: { + innerText: '...' + } + }; } else { - domProps = { - innerText: `"${value.toString()}"` + domItem = { + class: { + 'jv-item': true, + 'jv-string': true, + }, + ref: 'itemRef', + } + if (islink) { + value = `${value}`; + domItem.domProps = { + innerHTML: `"${value.toString()}"` + } + } else { + domItem.domProps = { + innerText: `"${value.toString()}"` + } } } + - return h('span', { - class: { - 'jv-item': true, - 'jv-string': true, - }, - domProps: { - ...domProps - } - }) + return h('span', {}, [ + this.canExtend && h('span', { + class: { + 'jv-toggle': true, + open: this.expand, + }, + on: { + click: this.toggle, + } + }), + h('span', { + class: { + 'jv-holder-node': true, + }, + ref: 'holderRef' + }), + h('span', domItem) + ]) } } diff --git a/package.json b/package.json index 6807d4d..ecffac9 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "vue-json-viewer", - "version": "2.2.15", + "version": "2.2.16", "description": "vuejs展示json的组件", "main": "vue-json-viewer.js", "files": [