|
| 1 | +<script setup lang="ts"> |
| 2 | +import { css as cssBeautify, js as jsBeautify } from 'js-beautify'; |
| 3 | +import TextareaCopyable from '@/components/TextareaCopyable.vue'; |
| 4 | +import { useStyleStore } from '@/stores/style.store'; |
| 5 | +import { withDefaultOnError } from '@/utils/defaults'; |
| 6 | +
|
| 7 | +const styleStore = useStyleStore(); |
| 8 | +
|
| 9 | +const languages = [ |
| 10 | + { label: 'JavaScript', value: 'javascript' }, |
| 11 | + { label: 'CSS', value: 'css' }, |
| 12 | +] as { label: string; value: string }[]; |
| 13 | +
|
| 14 | +const selectedLanguage = ref('javascript'); |
| 15 | +const indentSize = ref('2'); |
| 16 | +
|
| 17 | +const prettifyInput = ref(''); |
| 18 | +const minifyInput = ref(''); |
| 19 | +
|
| 20 | +const prettifyOutput = computed(() => |
| 21 | + withDefaultOnError(() => { |
| 22 | + if (!prettifyInput.value) { |
| 23 | + return ''; |
| 24 | + } |
| 25 | + const opts = { indent_size: Number.parseInt(indentSize.value, 10) || 2 }; |
| 26 | + return selectedLanguage.value === 'css' |
| 27 | + ? cssBeautify(prettifyInput.value, opts) |
| 28 | + : jsBeautify(prettifyInput.value, opts); |
| 29 | + }, ''), |
| 30 | +); |
| 31 | +
|
| 32 | +function minifyCss(input: string): string { |
| 33 | + return input |
| 34 | + .replace(/\/\*[\s\S]*?\*\//g, '') |
| 35 | + .replace(/\s+/g, ' ') |
| 36 | + .replace(/ ?([{}:;,>~+]) ?/g, '$1') |
| 37 | + .replaceAll(';}', '}') |
| 38 | + .trim(); |
| 39 | +} |
| 40 | +
|
| 41 | +function minifyJs(input: string): string { |
| 42 | + return input |
| 43 | + .replace(/\/\*[\s\S]*?\*\//g, '') |
| 44 | + .replace(/\/\/[^\n]*/g, '') |
| 45 | + .replace(/\s+/g, ' ') |
| 46 | + .replace(/ ?([{}();,=:+\-*/<>!&|?]) ?/g, '$1') |
| 47 | + .trim(); |
| 48 | +} |
| 49 | +
|
| 50 | +const minifyOutput = computed(() => |
| 51 | + withDefaultOnError(() => { |
| 52 | + if (!minifyInput.value) { |
| 53 | + return ''; |
| 54 | + } |
| 55 | + return selectedLanguage.value === 'css' |
| 56 | + ? minifyCss(minifyInput.value) |
| 57 | + : minifyJs(minifyInput.value); |
| 58 | + }, ''), |
| 59 | +); |
| 60 | +</script> |
| 61 | + |
| 62 | +<template> |
| 63 | + <div style="flex: 0 0 100%"> |
| 64 | + <div style="max-width: 600px" :class="{ 'flex-col': styleStore.isSmallScreen }" mx-auto mb-5 flex gap-2> |
| 65 | + <c-select |
| 66 | + v-model:value="selectedLanguage" |
| 67 | + label="Language" |
| 68 | + style="flex: 1" |
| 69 | + :options="languages" |
| 70 | + /> |
| 71 | + <c-input-text |
| 72 | + v-model:value="indentSize" |
| 73 | + label="Indent size" |
| 74 | + placeholder="2" |
| 75 | + style="flex: 1" |
| 76 | + /> |
| 77 | + </div> |
| 78 | + </div> |
| 79 | + |
| 80 | + <c-card title="Prettify"> |
| 81 | + <n-form-item label="Your code"> |
| 82 | + <c-input-text |
| 83 | + v-model:value="prettifyInput" |
| 84 | + placeholder="Paste your code to prettify..." |
| 85 | + rows="10" |
| 86 | + multiline |
| 87 | + monospace |
| 88 | + raw-text |
| 89 | + /> |
| 90 | + </n-form-item> |
| 91 | + <n-form-item label="Prettified code"> |
| 92 | + <TextareaCopyable :value="prettifyOutput" :language="selectedLanguage" /> |
| 93 | + </n-form-item> |
| 94 | + </c-card> |
| 95 | + |
| 96 | + <c-card title="Minify" mt-5> |
| 97 | + <n-form-item label="Your code"> |
| 98 | + <c-input-text |
| 99 | + v-model:value="minifyInput" |
| 100 | + placeholder="Paste your code to minify..." |
| 101 | + rows="10" |
| 102 | + multiline |
| 103 | + monospace |
| 104 | + raw-text |
| 105 | + /> |
| 106 | + </n-form-item> |
| 107 | + <n-form-item label="Minified code"> |
| 108 | + <TextareaCopyable :value="minifyOutput" :language="selectedLanguage" /> |
| 109 | + </n-form-item> |
| 110 | + </c-card> |
| 111 | +</template> |
0 commit comments