2022-05-09 17:41:42 +02:00
|
|
|
<template>
|
2023-03-29 23:25:39 +02:00
|
|
|
<div style="flex: 0 0 100%">
|
2023-05-27 17:36:15 +02:00
|
|
|
<div style="margin: 0 auto; max-width: 600px" flex justify-center gap-3>
|
2023-03-29 23:25:39 +02:00
|
|
|
<n-form-item label="Sort keys :" label-placement="left" label-width="100">
|
|
|
|
<n-switch v-model:value="sortKeys" />
|
|
|
|
</n-form-item>
|
|
|
|
<n-form-item label="Indent size :" label-placement="left" label-width="100" :show-feedback="false">
|
|
|
|
<n-input-number v-model:value="indentSize" min="0" max="10" style="width: 100px" />
|
|
|
|
</n-form-item>
|
2023-05-27 17:36:15 +02:00
|
|
|
</div>
|
2023-03-29 23:25:39 +02:00
|
|
|
</div>
|
|
|
|
|
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">
|
2022-08-03 12:47:00 +02:00
|
|
|
<textarea-copyable :value="cleanJson" language="json" :follow-height-of="inputElement" />
|
2022-07-24 15:10:38 +02:00
|
|
|
</n-form-item>
|
2022-05-09 17:41:42 +02:00
|
|
|
</template>
|
|
|
|
|
|
|
|
<script setup lang="ts">
|
2022-08-03 12:47:00 +02:00
|
|
|
import TextareaCopyable from '@/components/TextareaCopyable.vue';
|
2022-07-24 15:10:38 +02:00
|
|
|
import { useValidation } from '@/composable/validation';
|
2022-08-04 22:18:15 +02:00
|
|
|
import { withDefaultOnError } from '@/utils/defaults';
|
2022-07-24 15:10:38 +02:00
|
|
|
import { computed, ref } from 'vue';
|
2023-03-29 23:25:39 +02:00
|
|
|
import JSON5 from 'json5';
|
|
|
|
import { useStorage } from '@vueuse/core';
|
|
|
|
import { formatJson } from './json.models';
|
2022-05-09 17:41:42 +02:00
|
|
|
|
2022-07-24 15:10:38 +02:00
|
|
|
const inputElement = ref<HTMLElement>();
|
2022-05-09 17:41:42 +02:00
|
|
|
|
2023-03-29 23:25:39 +02:00
|
|
|
const rawJson = useStorage('json-prettify:raw-json', '{"hello": "world", "foo": "bar"}');
|
|
|
|
const indentSize = useStorage('json-prettify:indent-size', 3);
|
|
|
|
const sortKeys = useStorage('json-prettify:sort-keys', true);
|
|
|
|
const cleanJson = computed(() => withDefaultOnError(() => formatJson({ rawJson, indentSize, sortKeys }), ''));
|
2022-05-09 17:41:42 +02:00
|
|
|
|
|
|
|
const rawJsonValidation = useValidation({
|
|
|
|
source: rawJson,
|
|
|
|
rules: [
|
|
|
|
{
|
2022-08-04 22:18:15 +02:00
|
|
|
validator: (v) => v === '' || JSON5.parse(v),
|
|
|
|
message: 'Provided JSON is not valid.',
|
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>
|