perf: support view as base64 text for the key with type string

This commit is contained in:
tiny-craft 2023-09-08 10:54:28 +08:00
parent 9152a32fd3
commit dbb5969e34
1 changed files with 25 additions and 6 deletions

View File

@ -48,23 +48,42 @@ const viewOption = [
]
const viewAs = ref(types.PLAIN_TEXT)
const jsonValue = computed(() => {
const getJsonValue = () => {
try {
const jsonObj = JSON.parse(props.value)
return JSON.stringify(jsonObj, null, 2)
} catch (e) {
return props.value
}
})
}
const getBase64Value = () => {
try {
return atob(props.value)
} catch (e) {
return props.value
}
}
const getBase64JsonValue = () => {
try {
const text = atob(props.value)
const jsonObj = JSON.parse(text)
return JSON.stringify(jsonObj, null, 2)
} catch (e) {
return props.value
}
}
const autoDetectFormat = () => {
// auto check format when loaded
if (IsJson(jsonValue.value)) {
if (IsJson(props.value)) {
viewAs.value = types.JSON
} else {
viewAs.value = types.PLAIN_TEXT
}
}
onMounted(() => {
autoDetectFormat()
})
@ -85,11 +104,11 @@ const viewValue = computed(() => {
case types.PLAIN_TEXT:
return props.value
case types.JSON:
return jsonValue.value
return getJsonValue()
case types.BASE64_TO_TEXT:
return props.value
return getBase64Value()
case types.BASE64_TO_JSON:
return props.value
return getBase64JsonValue()
default:
return props.value
}