Files
mayfly-go/frontend/src/views/ops/redis/ViewerJson.vue
2024-10-31 17:24:56 +08:00

56 lines
1.2 KiB
Vue
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<template>
<div class="text-formated-container">
<monaco-editor ref="monacoEditorRef" :canChangeMode="false" v-model="state.modelValue" language="json" />
</div>
</template>
<script lang="ts" setup>
import { ref, watch, reactive, onMounted } from 'vue';
import MonacoEditor from '@/components/monaco/MonacoEditor.vue';
const props = defineProps({
content: {
type: String,
default: '',
},
});
const monacoEditorRef = ref(null) as any;
const state = reactive({
modelValue: '',
content: null as any,
});
// 因为默认从Text viewer开始暂时不watch保存时会触发重新格式化
watch(
() => props.content,
(val: any) => {
setContent(val);
}
);
onMounted(() => {
setContent(props.content);
});
const setContent = (val: any) => {
state.modelValue = val;
setTimeout(() => {
monacoEditorRef.value.format();
}, 100);
};
const getContent = () => {
// 尝试压缩json
try {
state.content = JSON.stringify(JSON.parse(state.modelValue));
return state.content;
} catch (e) {
return state.modelValue;
}
};
defineExpose({ getContent });
</script>
<style lang="scss"></style>