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

Merge pull request #36 from web-bee-ru/feature/copy-button-slot

feat: customize copy button with slot and timeout (updated README)
This commit is contained in:
陈峰
2020-03-20 16:58:31 +08:00
committed by GitHub
3 changed files with 49 additions and 9 deletions
+15 -1
View File
@@ -13,6 +13,8 @@ Support for incremental update components
- [Installing](#installing) - [Installing](#installing)
- [Example](#example) - [Example](#example)
- [Options](#options) - [Options](#options)
- [Listeners](#listeners)
- [Slots](#slots)
- [Theming](#theming) - [Theming](#theming)
## Installing ## Installing
@@ -145,11 +147,23 @@ import 'vue-json-viewer/style.css'
| ----------- |:------------- | ----------- | | ----------- |:------------- | ----------- |
| `value` | JSON data (can be used with `v-model`) | **Required** | | `value` | JSON data (can be used with `v-model`) | **Required** |
| `expand-depth` | Collapse blocs under this depth | `1` | | `expand-depth` | Collapse blocs under this depth | `1` |
| `copyable` | Display the copy button, you can customize copy text just set `{copyText: 'copy', copiedText: 'copied'}` or set `true` use default copytext | `false` | | `copyable` | Display the copy button, you can customize copy text just set `{copyText: 'copy', copiedText: 'copied', timeout: 2000}` or set `true` use default copytext | `false` |
| `sort` | Sort keys before displaying | `false` | | `sort` | Sort keys before displaying | `false` |
| `boxed` | Add a fancy "boxed" style to component | `false` | | `boxed` | Add a fancy "boxed" style to component | `false` |
| `theme` | Add a custom CSS class for theming purposes | `jv-light` | | `theme` | Add a custom CSS class for theming purposes | `jv-light` |
## Listeners
| Listener | Description | Value |
| ----------- |:------------- | ----------- |
| `copied` | Emits copyEvent after text copied | Clipboard success event |
## Slots
| Name | Description | Scope |
| ----------- |:------------- | ----------- |
| `copy` | Custom content for copy button | `{copied: boolean}` |
## 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:
+18
View File
@@ -6,6 +6,15 @@ Vue.use(JsonViewer)
new Vue({ new Vue({
el: '#app', el: '#app',
render() { render() {
const scopedSlots = {
copy: ({ copied }) => {
if (copied) return <button disabled>Copied!</button>
return <button>Copy me!</button>
},
}
const onCopied = (copyEvent) => {
alert(`Text successfully copied!\n${copyEvent.text}`);
}
return ( return (
<div> <div>
<json-viewer value={this.jsonData}></json-viewer> <json-viewer value={this.jsonData}></json-viewer>
@@ -19,6 +28,15 @@ new Vue({
}} }}
boxed boxed
sort></json-viewer> sort></json-viewer>
<hr />
<json-viewer
value={this.jsonData}
expand-depth={1}
copyable={{
timeout: 4000
}}
scopedSlots={scopedSlots}
onCopied={onCopied}></json-viewer>
</div> </div>
) )
}, },
+16 -8
View File
@@ -8,7 +8,14 @@
ref="clip" ref="clip"
class="jv-button" class="jv-button"
:class="{copied}" :class="{copied}"
>{{ copied ? copyText.copiedText : copyText.copyText }}</span> >
<slot
name="copy"
:copied="copied"
>
{{ copied ? copyText.copiedText : copyText.copyText }}
</slot>
</span>
</div> </div>
<div <div
class="jv-code" class="jv-code"
@@ -92,11 +99,12 @@ export default {
return 'jv-container ' + this.theme + (this.boxed ? ' boxed' : '') return 'jv-container ' + this.theme + (this.boxed ? ' boxed' : '')
}, },
copyText() { copyText() {
const { copyText, copiedText } = this.copyable const { copyText, copiedText, timeout } = this.copyable
return { return {
copyText: copyText || 'copy', copyText: copyText || 'copy',
copiedText: copiedText || 'copied!' copiedText: copiedText || 'copied!',
timeout: timeout || 2000
} }
} }
}, },
@@ -112,8 +120,8 @@ export default {
return JSON.stringify(this.value, null, 2) return JSON.stringify(this.value, null, 2)
} }
}); });
clipBoard.on('success', () => { clipBoard.on('success', (e) => {
this.onCopied() this.onCopied(e)
}) })
} }
}, },
@@ -131,15 +139,15 @@ export default {
} }
}) })
}, },
onCopied() { onCopied(copyEvent) {
if (this.copied) { if (this.copied) {
return; return;
} }
this.copied = true this.copied = true
setTimeout(() => { setTimeout(() => {
this.copied = false this.copied = false
}, 2000) }, this.copyText.timeout)
this.$emit('copied') this.$emit('copied', copyEvent)
}, },
toggleExpandCode () { toggleExpandCode () {
this.expandCode = !this.expandCode this.expandCode = !this.expandCode