jsonEditor.vue
2.38 KB
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
101
102
<template>
<div class="flex flex-col justify-between">
<div ref="jsoneditorRef" style="height: 100%"></div>
<div class="ml-2 -mt-1.5">
<Button v-if="showBtn" @click="onHandleCopy" class="mt-8 -ml-2">
<template #icon>
<CopyOutlined />
</template>
copy
</Button>
</div>
</div>
</template>
<script lang="ts" setup>
import { ref, onMounted, nextTick, unref } from 'vue';
import jsoneditor from 'jsoneditor';
import 'jsoneditor/dist/jsoneditor.min.css';
import { Button } from 'ant-design-vue';
import { useMessage } from '/@/hooks/web/useMessage';
import { CopyOutlined } from '@ant-design/icons-vue';
const props = defineProps({
showBtn: {
type: Boolean,
default: false,
},
});
const { createMessage } = useMessage();
// json 以及初始化JSON
const jsoneditorRef = ref();
const jsonValue = ref({});
const jsonInstance = ref();
onMounted(() => {
nextTick(() => {
let options = {
mode: 'code',
mainMenuBar: false,
statusBar: false,
onFocus: () => {},
onBlur: () => {},
onChangeText: (e) => {
if (props.showBtn) {
if (e.length) return setJsonValue('测试结果为');
}
},
} as object;
let editor = new jsoneditor(jsoneditorRef.value, options);
editor.set(jsonValue.value);
jsonInstance.value = editor;
});
});
const getJsonValue = () => unref(jsonInstance).get();
const setJsonMode = (mode) => {
nextTick(() => {
unref(jsonInstance).setMode(mode);
});
};
const setJsonValue = (Json) => {
nextTick(() => {
unref(jsonInstance).set(Json);
});
};
function copyToClip(content, message) {
var aux = document.createElement('input');
aux.setAttribute('value', content);
document.body.appendChild(aux);
aux.select();
document.execCommand('copy');
document.body.removeChild(aux);
if (message == null) {
createMessage.success('复制成功!');
} else {
}
}
const onHandleCopy = () => {
copyToClip(JSON.stringify(getJsonValue()), null);
};
defineExpose({
getJsonValue,
setJsonValue,
jsonInstance,
setJsonMode,
});
</script>
<style>
.jsoneditor {
border: none;
}
</style>