forked from dirkliu/vue-json-editor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvue-json-editor.vue
100 lines (87 loc) · 1.9 KB
/
vue-json-editor.vue
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
<template>
<div>
<div class="jsoneditor-vue"></div>
<div class="jsoneditor-btns" v-if="showBtns!==false"><button class="json-save-btn" type="button" @click="onSave()" :disabled="error">保存</button></div>
</div>
</template>
<script>
import './assets/jsoneditor.css'
import JsonEditor from './assets/jsoneditor'
export default {
props: ['value', 'showBtns'],
data () {
return {
editor: null,
error: false,
json: this.value
}
},
mounted () {
var self = this
var options = {
mode: 'tree',
modes: ['tree', 'code', 'form', 'text', 'view'], // allowed modes
onChange () {
try {
var json = self.editor.get()
self.error = false
} catch (e) {
console.log('e:', e)
self.error = true
}
if (!self.error) {
self.json = json
self.$emit('json-change', json)
}
}
}
this.editor = new JsonEditor(this.$el.querySelector('.jsoneditor-vue'), options, this.json)
},
methods: {
onSave () {
this.$emit('json-save', this.json)
}
}
}
</script>
<style>
.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;
}
.json-save-btn:focus{
outline: none;
}
.json-save-btn[disabled]{
background-color: #1D8CE0;
}
code {
background-color: #f5f5f5;
}
</style>