-
-
Notifications
You must be signed in to change notification settings - Fork 532
Expand file tree
/
Copy pathMarkdownEditor.vue
More file actions
83 lines (70 loc) · 2.32 KB
/
Copy pathMarkdownEditor.vue
File metadata and controls
83 lines (70 loc) · 2.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
<script setup lang="ts">
import { ref, watch } from "vue";
import Markdown from "@/components/global/Markdown.vue";
import { Checkbox } from "@/components/ui/checkbox";
import { Textarea } from "@/components/ui/textarea";
import { Label } from "@/components/ui/label";
const props = withDefaults(
defineProps<{
modelValue?: string | null;
label?: string | null;
maxLength?: number;
minLength?: number;
}>(),
{
modelValue: null,
label: null,
maxLength: -1,
minLength: -1,
}
);
const emit = defineEmits(["update:modelValue"]);
const local = ref(props.modelValue ?? "");
watch(
() => props.modelValue,
v => {
if (v !== local.value) local.value = v ?? "";
}
);
watch(local, v => emit("update:modelValue", v === "" ? null : v));
const showPreview = ref(false);
const id = useId();
const isLengthInvalid = computed(() => {
if (typeof local.value !== "string") return false;
const len = local.value.length;
const max = props.maxLength ?? -1;
const min = props.minLength ?? -1;
return (max !== -1 && len > max) || (min !== -1 && len < min);
});
const lengthIndicator = computed(() => {
if (typeof local.value !== "string") return "";
const max = props.maxLength ?? -1;
if (max !== -1) {
return `${local.value.length}/${max}`;
}
return "";
});
</script>
<template>
<div class="w-full">
<div class="mb-2 grid grid-cols-1 items-center gap-2 md:grid-cols-4">
<div class="min-w-0">
<Label :for="id" class="flex min-w-0 items-center gap-2 px-1">
<span class="truncate" :title="props.label ?? ''">{{ props.label }}</span>
<span class="grow" />
<span class="ml-2 text-sm" :class="{ 'text-destructive': isLengthInvalid }">{{ lengthIndicator }}</span>
</Label>
</div>
<div class="col-span-1 flex items-center justify-start gap-2 md:col-span-3 md:justify-end">
<label class="text-xs text-foreground/70">{{ $t("global.preview") }}</label>
<Checkbox v-model="showPreview" />
</div>
</div>
<div class="flex w-full flex-col gap-4">
<Textarea :id="id" v-model="local" autosize class="resize-none" />
<div v-if="showPreview">
<Markdown :source="local" />
</div>
</div>
</div>
</template>