TslContent.vue
3.88 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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
<template>
<div>
<div>
<Typography>
<TypographyParagraph>
<blockquote class="bg-gray-50">{{ useBlockContent }}</blockquote>
</TypographyParagraph>
</Typography>
</div>
<div>
<Tabs type="card" v-model:active-key="activeKey" @change="handleSwitchTsl">
<Tabs.TabPane :key="FunctionType.PROPERTIES" tab="属性" />
<Tabs.TabPane :key="FunctionType.SERVICE" tab="服务" />
<!-- <Tabs.TabPane :key="FunctionType.EVENTS" tab="事件" /> -->
<template #tabBarExtraContent>
<Button @click="handlePremitter">
<template #icon>
<SortAscendingOutlined />
</template>
格式化
</Button>
<Button class="ml-2" @click="handleCopy">
<template #icon>
<CopyOutlined />
</template>
copy
</Button>
</template>
</Tabs>
</div>
<div class="relative">
<Spin :spinning="loading">
<div id="jsoneditor" ref="jsoneditorEl"></div>
<div class="absolute top-0 left-0 w-full h-full"></div>
</Spin>
</div>
</div>
</template>
<script lang="ts" setup>
import { ref, unref, onMounted, nextTick } from 'vue';
import { CopyOutlined, SortAscendingOutlined } from '@ant-design/icons-vue';
import { useCopyToClipboard } from '/@/hooks/web/useCopyToClipboard';
import { useMessage } from '/@/hooks/web/useMessage';
import jsoneditor from 'jsoneditor';
import 'jsoneditor/dist/jsoneditor.min.css';
import { Button, Typography, TypographyParagraph, Tabs, Spin } from 'ant-design-vue';
import { FunctionType } from './config';
import useCommon from '../hook/useCommon';
import { DeviceRecord } from '/@/api/device/model/deviceModel';
import { getModelTsl } from '/@/api/device/modelOfMatter';
const props = defineProps<{
record: DeviceRecord;
}>();
const loading = ref(false);
const { createMessage } = useMessage();
const { useBlockContent } = useCommon();
const jsonValue = ref();
const jsonInstance = ref<{
set: (value?: Recordable) => void;
get: () => Recordable;
}>();
const jsoneditorEl = ref<HTMLDivElement>();
const activeKey = ref(FunctionType.PROPERTIES);
onMounted(() => {
nextTick(() => {
let options = {
mode: 'code',
mainMenuBar: false,
statusBar: false,
};
let editor = new jsoneditor(jsoneditorEl.value, options);
editor.set(jsonValue.value);
jsonInstance.value = editor;
});
});
const { clipboardRef, copiedRef } = useCopyToClipboard();
const handleCopy = () => {
try {
const valueRef = unref(jsonInstance)?.get();
const value = JSON.stringify(unref(valueRef));
if (!value) {
createMessage.warning('请输入要拷贝的内容!');
return;
}
clipboardRef.value = value;
if (unref(copiedRef)) {
createMessage.success('复制成功!');
}
} catch (e) {
console.log(e);
}
};
const getFormData = () => {
const value = unref(jsonInstance)?.get();
if (!value) return;
return value;
};
const resetFormData = () => {
unref(jsonInstance)?.set();
};
const handlePremitter = () => {
const value = unref(jsonInstance)?.get();
return unref(jsonInstance)?.set(value);
};
const handleSwitchTsl = async (functionType: FunctionType) => {
try {
loading.value = true;
const record = await getModelTsl({ deviceProfileId: props.record.id, functionType });
jsonInstance.value?.set(record);
} catch (error) {
} finally {
loading.value = false;
}
};
onMounted(() => {
activeKey.value = FunctionType.PROPERTIES;
handleSwitchTsl(FunctionType.PROPERTIES);
});
defineExpose({
getFormData,
resetFormData,
});
</script>
<style lang="less" scope>
#jsoneditor {
width: 100%;
height: 450px;
}
</style>