From f708f5091e2182fc88e7cf3e7d23b3d05edc04da Mon Sep 17 00:00:00 2001
From: Tsonglew <tsonglew@gmail.com>
Date: Sat, 4 Feb 2023 16:56:17 +0800
Subject: feat(new-tool): json minify (#265)

Co-authored-by: Corentin THOMASSET <corentin.thomasset74@gmail.com>
---
 src/tools/json-minify/json-minify.vue | 57 +++++++++++++++++++++++++++++++++++
 1 file changed, 57 insertions(+)
 create mode 100644 src/tools/json-minify/json-minify.vue

(limited to 'src/tools/json-minify/json-minify.vue')

diff --git a/src/tools/json-minify/json-minify.vue b/src/tools/json-minify/json-minify.vue
new file mode 100644
index 0000000..92ab7d2
--- /dev/null
+++ b/src/tools/json-minify/json-minify.vue
@@ -0,0 +1,57 @@
+<template>
+  <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="Minify version of your JSON">
+    <textarea-copyable :value="cleanJson" language="json" :follow-height-of="inputElement" />
+  </n-form-item>
+</template>
+
+<script setup lang="ts">
+import TextareaCopyable from '@/components/TextareaCopyable.vue';
+import { useValidation } from '@/composable/validation';
+import { withDefaultOnError } from '@/utils/defaults';
+import JSON5 from 'json5';
+import { computed, ref } from 'vue';
+
+const inputElement = ref<HTMLElement>();
+
+const rawJson = ref('{\n\t"hello": [\n\t\t"world"\n\t]\n}');
+const cleanJson = computed(() => withDefaultOnError(() => JSON.stringify(JSON5.parse(rawJson.value), null, 0), ''));
+
+const rawJsonValidation = useValidation({
+  source: rawJson,
+  rules: [
+    {
+      validator: (v) => v === '' || JSON5.parse(v),
+      message: 'Provided JSON is not valid.',
+    },
+  ],
+});
+</script>
+
+<style lang="less" scoped>
+.result-card {
+  position: relative;
+
+  .copy-button {
+    position: absolute;
+    top: 10px;
+    right: 10px;
+  }
+}
+</style>
-- 
cgit v1.2.3

