|
1 | 1 | <script setup lang="ts">
|
2 |
| -import yaml from 'yaml'; |
3 | 2 | import { useStorage } from '@vueuse/core';
|
4 | 3 | import { formatYaml } from './yaml-models';
|
5 |
| -import { withDefaultOnError } from '@/utils/defaults'; |
6 |
| -import { useValidation } from '@/composable/validation'; |
7 | 4 | import TextareaCopyable from '@/components/TextareaCopyable.vue';
|
8 | 5 |
|
9 |
| -const inputElement = ref<HTMLElement>(); |
10 |
| -
|
11 | 6 | const rawYaml = useStorage('yaml-prettify:raw-yaml', '');
|
12 | 7 | const indentSize = useStorage('yaml-prettify:indent-size', 2);
|
13 | 8 | const sortKeys = useStorage('yaml-prettify:sort-keys', false);
|
14 | 9 |
|
15 |
| -const cleanYaml = computed(() => withDefaultOnError(() => formatYaml({ rawYaml, indentSize, sortKeys }), '')); |
16 |
| -
|
17 |
| -const rawYamlValidation = useValidation({ |
18 |
| - source: rawYaml, |
19 |
| - rules: [ |
20 |
| - { |
21 |
| - validator: v => v === '' || yaml.parse(v), |
22 |
| - message: 'Provided YAML is not valid.', |
23 |
| - }, |
24 |
| - ], |
| 10 | +const yamlFormattingResult = computed(() => { |
| 11 | + try { |
| 12 | + return { yaml: formatYaml({ rawYaml, indentSize, sortKeys }), errors: [] }; |
| 13 | + } |
| 14 | + catch (e: any) { |
| 15 | + return { yaml: '#see error messages', errors: e.toString().split('\n') }; |
| 16 | + } |
25 | 17 | });
|
| 18 | +const errors = computed(() => yamlFormattingResult.value.errors); |
| 19 | +const cleanYaml = computed(() => yamlFormattingResult.value.yaml); |
| 20 | +
|
| 21 | +const MONACO_EDITOR_OPTIONS = { |
| 22 | + automaticLayout: true, |
| 23 | + formatOnType: true, |
| 24 | + formatOnPaste: true, |
| 25 | +}; |
26 | 26 | </script>
|
27 | 27 |
|
28 | 28 | <template>
|
29 |
| - <div style="flex: 0 0 100%"> |
30 |
| - <div style="margin: 0 auto; max-width: 600px" flex justify-center gap-3> |
31 |
| - <n-form-item label="Sort keys :" label-placement="left" label-width="100"> |
32 |
| - <n-switch v-model:value="sortKeys" /> |
33 |
| - </n-form-item> |
34 |
| - <n-form-item label="Indent size :" label-placement="left" label-width="100" :show-feedback="false"> |
35 |
| - <n-input-number v-model:value="indentSize" min="1" max="10" style="width: 100px" /> |
36 |
| - </n-form-item> |
| 29 | + <div max-w-600> |
| 30 | + <div style="flex: 0 0 100%"> |
| 31 | + <div style="margin: 0 auto; max-width: 600px" flex justify-center gap-3> |
| 32 | + <n-form-item label="Sort keys :" label-placement="left" label-width="100"> |
| 33 | + <n-switch v-model:value="sortKeys" /> |
| 34 | + </n-form-item> |
| 35 | + <n-form-item label="Indent size :" label-placement="left" label-width="100" :show-feedback="false"> |
| 36 | + <n-input-number v-model:value="indentSize" min="1" max="10" style="width: 100px" /> |
| 37 | + </n-form-item> |
| 38 | + </div> |
37 | 39 | </div>
|
38 |
| - </div> |
39 | 40 |
|
40 |
| - <n-form-item |
41 |
| - label="Your raw YAML" |
42 |
| - :feedback="rawYamlValidation.message" |
43 |
| - :validation-status="rawYamlValidation.status" |
44 |
| - > |
45 |
| - <c-input-text |
46 |
| - ref="inputElement" |
47 |
| - v-model:value="rawYaml" |
48 |
| - placeholder="Paste your raw YAML here..." |
49 |
| - rows="20" |
50 |
| - multiline |
51 |
| - autocomplete="off" |
52 |
| - autocorrect="off" |
53 |
| - autocapitalize="off" |
54 |
| - spellcheck="false" |
55 |
| - monospace |
56 |
| - /> |
57 |
| - </n-form-item> |
58 |
| - <n-form-item label="Prettified version of your YAML"> |
59 |
| - <TextareaCopyable :value="cleanYaml" language="yaml" :follow-height-of="inputElement" /> |
60 |
| - </n-form-item> |
| 41 | + <c-label label="Your raw YAML:"> |
| 42 | + <div relative w-full> |
| 43 | + <c-monaco-editor |
| 44 | + v-model:value="rawYaml" |
| 45 | + theme="vs-dark" |
| 46 | + language="yaml" |
| 47 | + height="250px" |
| 48 | + :options="MONACO_EDITOR_OPTIONS" |
| 49 | + /> |
| 50 | + </div> |
| 51 | + </c-label> |
| 52 | + |
| 53 | + <div v-if="errors.length > 0"> |
| 54 | + <n-alert title="The following errors occured" type="error" mt-5> |
| 55 | + <ul> |
| 56 | + <li v-for="(message, index) of errors" :key="index"> |
| 57 | + {{ message }} |
| 58 | + </li> |
| 59 | + </ul> |
| 60 | + </n-alert> |
| 61 | + </div> |
| 62 | + |
| 63 | + <n-divider /> |
| 64 | + |
| 65 | + <n-form-item label="Prettified version of your YAML"> |
| 66 | + <TextareaCopyable :value="cleanYaml" language="yaml" /> |
| 67 | + </n-form-item> |
| 68 | + </div> |
61 | 69 | </template>
|
62 | 70 |
|
63 |
| -<style lang="less" scoped> |
64 |
| -.result-card { |
65 |
| - position: relative; |
66 |
| - .copy-button { |
| 71 | + <style lang="less" scoped> |
| 72 | + .result-card { |
| 73 | + position: relative; |
| 74 | + .copy-button { |
67 | 75 | position: absolute;
|
68 | 76 | top: 10px;
|
69 | 77 | right: 10px;
|
70 |
| - } |
71 |
| -} |
72 |
| -</style> |
| 78 | + } |
| 79 | + } |
| 80 | + </style> |
0 commit comments