2022-05-09 17:41:42 +02:00
|
|
|
<template>
|
2022-07-24 15:10:38 +02:00
|
|
|
<n-form-item
|
|
|
|
label="Your raw json"
|
|
|
|
:feedback="rawJsonValidation.message"
|
|
|
|
:validation-status="rawJsonValidation.status"
|
|
|
|
>
|
|
|
|
<n-input
|
|
|
|
ref="inputElement"
|
|
|
|
v-model:value="rawJson"
|
|
|
|
placeholder="Paste your raw json here..."
|
|
|
|
type="textarea"
|
|
|
|
rows="20"
|
|
|
|
autocomplete="off"
|
|
|
|
autocorrect="off"
|
|
|
|
autocapitalize="off"
|
|
|
|
spellcheck="false"
|
|
|
|
/>
|
|
|
|
</n-form-item>
|
|
|
|
<n-form-item label="Prettify version of your json">
|
|
|
|
<n-card class="result-card" :style="`min-height: ${inputElementHeight ?? 400}px`">
|
2022-05-09 17:41:42 +02:00
|
|
|
<n-config-provider :hljs="hljs">
|
2022-07-24 15:10:38 +02:00
|
|
|
<n-code :code="cleanJson" language="json" :trim="false" />
|
2022-05-09 17:41:42 +02:00
|
|
|
</n-config-provider>
|
2022-07-24 15:10:38 +02:00
|
|
|
<n-button v-if="cleanJson" class="copy-button" secondary @click="copy">Copy</n-button>
|
|
|
|
</n-card>
|
|
|
|
</n-form-item>
|
2022-05-09 17:41:42 +02:00
|
|
|
</template>
|
|
|
|
|
|
|
|
<script setup lang="ts">
|
2022-07-24 15:10:38 +02:00
|
|
|
import { useCopy } from '@/composable/copy';
|
|
|
|
import { useValidation } from '@/composable/validation';
|
|
|
|
import { useElementSize } from '@vueuse/core';
|
2022-05-09 17:41:42 +02:00
|
|
|
import hljs from 'highlight.js/lib/core';
|
|
|
|
import json from 'highlight.js/lib/languages/json';
|
2022-07-24 15:10:38 +02:00
|
|
|
import { computed, ref } from 'vue';
|
2022-05-09 17:41:42 +02:00
|
|
|
|
|
|
|
hljs.registerLanguage('json', json);
|
2022-07-24 15:10:38 +02:00
|
|
|
const inputElement = ref<HTMLElement>();
|
|
|
|
const { height: inputElementHeight } = useElementSize(inputElement);
|
2022-05-09 17:41:42 +02:00
|
|
|
|
2022-07-24 15:10:38 +02:00
|
|
|
const rawJson = ref('{"hello": "world"}');
|
2022-05-09 17:41:42 +02:00
|
|
|
const cleanJson = computed(() => {
|
|
|
|
try {
|
|
|
|
return JSON.stringify(JSON.parse(rawJson.value), null, 3);
|
|
|
|
} catch (_) {
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2022-07-24 15:10:38 +02:00
|
|
|
const { copy } = useCopy({ source: cleanJson });
|
|
|
|
|
2022-05-09 17:41:42 +02:00
|
|
|
const rawJsonValidation = useValidation({
|
|
|
|
source: rawJson,
|
|
|
|
rules: [
|
|
|
|
{
|
2022-05-14 16:29:50 +02:00
|
|
|
validator: (v) => v === '' || JSON.parse(v),
|
2022-07-24 15:10:38 +02:00
|
|
|
message: 'Invalid json',
|
2022-05-09 17:41:42 +02:00
|
|
|
},
|
|
|
|
],
|
|
|
|
});
|
|
|
|
</script>
|
|
|
|
|
|
|
|
<style lang="less" scoped>
|
2022-07-24 15:10:38 +02:00
|
|
|
.result-card {
|
|
|
|
position: relative;
|
|
|
|
.copy-button {
|
|
|
|
position: absolute;
|
|
|
|
top: 10px;
|
|
|
|
right: 10px;
|
|
|
|
}
|
2022-05-09 17:41:42 +02:00
|
|
|
}
|
|
|
|
</style>
|