2022-05-09 17:41:42 +02:00
|
|
|
<template>
|
|
|
|
<n-card>
|
|
|
|
<n-form-item
|
|
|
|
label="Your raw json:"
|
|
|
|
:feedback="rawJsonValidation.message"
|
|
|
|
:validation-status="rawJsonValidation.status"
|
|
|
|
>
|
2022-05-14 16:29:50 +02:00
|
|
|
<n-input
|
|
|
|
v-model:value="rawJson"
|
|
|
|
class="json-input"
|
|
|
|
type="textarea"
|
|
|
|
placeholder="Paste your raw json here..."
|
|
|
|
autocomplete="off"
|
|
|
|
autocorrect="off"
|
|
|
|
autocapitalize="off"
|
|
|
|
spellcheck="false"
|
|
|
|
/>
|
2022-05-09 17:41:42 +02:00
|
|
|
</n-form-item>
|
2022-05-14 16:29:50 +02:00
|
|
|
|
|
|
|
<n-space justify="center">
|
|
|
|
<n-button secondary @click="rawJson = ''">Clear</n-button>
|
|
|
|
</n-space>
|
2022-05-09 17:41:42 +02:00
|
|
|
</n-card>
|
|
|
|
|
|
|
|
<n-card v-if="cleanJson.length > 0">
|
|
|
|
<n-scrollbar :x-scrollable="true">
|
|
|
|
<n-config-provider :hljs="hljs">
|
|
|
|
<n-code :code="cleanJson" language="json" />
|
|
|
|
</n-config-provider>
|
|
|
|
</n-scrollbar>
|
|
|
|
</n-card>
|
|
|
|
</template>
|
|
|
|
|
|
|
|
<script setup lang="ts">
|
|
|
|
import { ref, computed } from 'vue';
|
|
|
|
import hljs from 'highlight.js/lib/core';
|
|
|
|
import json from 'highlight.js/lib/languages/json';
|
|
|
|
import { useValidation } from '@/composable/validation';
|
|
|
|
|
|
|
|
hljs.registerLanguage('json', json);
|
|
|
|
|
|
|
|
const rawJson = ref('');
|
|
|
|
const cleanJson = computed(() => {
|
|
|
|
try {
|
|
|
|
return JSON.stringify(JSON.parse(rawJson.value), null, 3);
|
|
|
|
} catch (_) {
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
const rawJsonValidation = useValidation({
|
|
|
|
source: rawJson,
|
|
|
|
rules: [
|
|
|
|
{
|
2022-05-14 16:29:50 +02:00
|
|
|
validator: (v) => v === '' || JSON.parse(v),
|
2022-05-09 17:41:42 +02:00
|
|
|
message: 'Invalid json string',
|
|
|
|
},
|
|
|
|
],
|
|
|
|
});
|
|
|
|
</script>
|
|
|
|
|
|
|
|
<style lang="less" scoped>
|
|
|
|
.json-input ::v-deep(.n-input-wrapper) {
|
|
|
|
resize: both !important;
|
|
|
|
}
|
|
|
|
</style>
|