Line data Source code
1 16 : <template>
2 11 : <Textarea
3 : ref="textareaRef"
4 : v-bind="attributes()"
5 : :model-value="readText()"
6 : :aria-label="label"
7 : :readonly="readOnly()"
8 : class="h-150 min-h-40 flex-auto resize-none rounded-t-none rounded-b-2xl border-t border-solid border-border/50 bg-transparent px-[max(--spacing(4),calc((100%-34em)/2))] py-10 font-inter text-[15px] leading-6 focus-visible:ring-0"
9 : data-testid="text-editor-dialog-textarea"
10 : @compositionstart="isComposing = true"
11 : @compositionend="isComposing = false"
12 : @update:model-value="(value) => onEdit(String(value))"
13 : />
14 : </template>
15 :
16 : <script setup lang="ts">
17 : import type { TextareaHTMLAttributes } from 'vue'
18 : import { nextTick, onMounted, ref, useTemplateRef, watch } from 'vue'
19 :
20 : import Textarea from '@comfyorg/design-system/textarea/Textarea'
21 :
22 : const {
23 : readText,
24 : label,
25 : readOnly,
26 : onEdit,
27 : attributes = () => ({})
28 : } = defineProps<{
29 : readText: () => string
30 : label: string
31 : readOnly: () => boolean
32 : onEdit: (text: string) => void
33 : attributes?: () => TextareaHTMLAttributes
34 : }>()
35 :
36 2 : const textareaRef = useTemplateRef('textareaRef')
37 2 : const isComposing = ref(false)
38 :
39 11 : function textareaElement(): HTMLTextAreaElement | undefined {
40 11 : const element: unknown = textareaRef.value?.$el
41 11 : return element instanceof HTMLTextAreaElement ? element : undefined
42 : }
43 :
44 2 : watch(
45 : () => readText(),
46 : async (text) => {
47 9 : const element = textareaElement()
48 9 : if (!element || isComposing.value || text === element.value) return
49 9 : const view = {
50 : start: element.selectionStart,
51 : end: element.selectionEnd,
52 : direction: element.selectionDirection ?? undefined,
53 : scrollTop: element.scrollTop,
54 : selectedThrough: element.value.slice(0, element.selectionEnd)
55 : }
56 9 : await nextTick()
57 0 : const start = Math.min(view.start, text.length)
58 0 : const isSelectionIntact = text.startsWith(view.selectedThrough)
59 9 : element.setSelectionRange(
60 : start,
61 : isSelectionIntact ? view.end : start,
62 : view.direction
63 : )
64 9 : element.scrollTop = view.scrollTop
65 : }
66 : )
67 :
68 2 : onMounted(() => {
69 2 : const element = textareaElement()
70 2 : if (!element) return
71 2 : element.setSelectionRange(0, 0)
72 2 : element.focus()
73 : })
74 : </script>
|