it-tools/src/tools/url-encoder/url-encoder.vue

122 lines
2.8 KiB
Vue
Raw Normal View History

2022-04-13 13:29:44 +02:00
<template>
<n-card title="Encode">
<n-form-item
label="Your string :"
:feedback="encodedValidation.message"
:validation-status="encodedValidation.status"
>
<n-input
v-model:value="encodeInput"
type="textarea"
placeholder="The string to encode"
:autosize="{ minRows: 2 }"
/>
</n-form-item>
2022-04-13 13:29:44 +02:00
<n-form-item label="Your string encoded :">
<n-input
:value="encodeOutput"
type="textarea"
readonly
placeholder="Your string encoded"
:autosize="{ minRows: 2 }"
/>
</n-form-item>
2022-04-13 13:29:44 +02:00
<n-space justify="center">
<n-button secondary @click="copyEncoded"> Copy </n-button>
</n-space>
</n-card>
<n-card title="Decode">
<n-form-item
label="Your encoded string :"
:feedback="decodeValidation.message"
:validation-status="decodeValidation.status"
>
<n-input
v-model:value="decodeInput"
type="textarea"
placeholder="The string to decode"
:autosize="{ minRows: 2 }"
/>
</n-form-item>
2022-04-13 13:29:44 +02:00
<n-form-item label="Your string decoded :">
<n-input
:value="decodeOutput"
type="textarea"
readonly
placeholder="Your string decoded"
:autosize="{ minRows: 2 }"
/>
</n-form-item>
2022-04-13 13:29:44 +02:00
<n-space justify="center">
<n-button secondary @click="copyDecoded"> Copy </n-button>
</n-space>
</n-card>
2022-04-13 13:29:44 +02:00
</template>
<script setup lang="ts">
import { useCopy } from '@/composable/copy';
import { useValidation } from '@/composable/validation';
2022-04-22 23:31:40 +02:00
import { computed, ref } from 'vue';
2022-04-13 13:29:44 +02:00
2022-04-22 23:31:40 +02:00
const encodeInput = ref('Hello world :)');
2022-04-13 13:29:44 +02:00
const encodeOutput = computed(() => {
2022-04-16 16:16:00 +02:00
try {
2022-04-22 23:31:40 +02:00
return encodeURIComponent(encodeInput.value);
2022-04-16 16:16:00 +02:00
} catch (_) {
2022-04-22 23:31:40 +02:00
return '';
2022-04-16 16:16:00 +02:00
}
2022-04-22 23:31:40 +02:00
});
2022-04-13 13:29:44 +02:00
const encodedValidation = useValidation({
2022-04-22 23:31:40 +02:00
source: encodeInput,
rules: [
{
validator: (value) => {
try {
encodeURIComponent(value);
return true;
} catch (_) {
return false;
}
},
message: 'Impossible to parse this string',
2022-04-16 16:16:00 +02:00
},
2022-04-22 23:31:40 +02:00
],
});
2022-04-13 13:29:44 +02:00
2022-04-22 23:31:40 +02:00
const { copy: copyEncoded } = useCopy({ source: encodeOutput, text: 'Encoded string copied to the clipboard' });
2022-04-13 13:29:44 +02:00
2022-04-22 23:31:40 +02:00
const decodeInput = ref('Hello%20world%20%3A)');
2022-04-13 13:29:44 +02:00
const decodeOutput = computed(() => {
2022-04-16 16:16:00 +02:00
try {
2022-04-22 23:31:40 +02:00
return decodeURIComponent(decodeInput.value);
2022-04-16 16:16:00 +02:00
} catch (_) {
2022-04-22 23:31:40 +02:00
return '';
2022-04-16 16:16:00 +02:00
}
2022-04-22 23:31:40 +02:00
});
2022-04-13 13:29:44 +02:00
const decodeValidation = useValidation({
2022-04-22 23:31:40 +02:00
source: encodeInput,
rules: [
{
validator: (value) => {
try {
decodeURIComponent(value);
return true;
} catch (_) {
return false;
}
},
message: 'Impossible to parse this string',
2022-04-16 16:16:00 +02:00
},
2022-04-22 23:31:40 +02:00
],
});
2022-04-13 13:29:44 +02:00
2022-04-22 23:31:40 +02:00
const { copy: copyDecoded } = useCopy({ source: decodeOutput, text: 'Decoded string copied to the clipboard' });
2022-04-13 13:29:44 +02:00
</script>