Line data Source code
1 16 : <template>
2 : <div
3 : :class="
4 : cn(
5 : 'group relative rounded-lg transition-all focus-within:ring focus-within:ring-ring',
6 : !isReadOnly && 'hover:bg-muted',
7 : widget.borderStyle
8 : )
9 : "
10 : >
11 : <div
12 : :class="
13 : cn(
14 : 'flex size-full flex-col overflow-hidden rounded-md border-(length:--node-widget-border-width) border-node-border',
15 : !widget.options?.disabled && (isReadOnly ? 'bg-input/50' : 'bg-input')
16 : )
17 : "
18 : >
19 : <div class="flex h-6 shrink-0 items-center gap-2 px-3">
20 : <label
21 : v-if="!hideLayoutField"
22 : :for="id"
23 : class="min-w-0 truncate text-2xs text-node-foreground"
24 : @pointerdown.stop
25 : @pointermove.stop
26 : @pointerup.stop
27 : >
28 : {{ displayName }}
29 : </label>
30 : <div
31 : role="group"
32 : :aria-label="$t('g.actions')"
33 : class="ml-auto flex shrink-0 items-center gap-0.5 opacity-0 group-hover:opacity-100 focus-within:opacity-100 pointer-coarse:opacity-100"
34 : >
35 : <span class="pr-1 text-2xs text-muted-foreground tabular-nums">
36 : {{ characterCount }}
37 : </span>
38 : <span aria-hidden="true" class="mr-1 h-3 w-px bg-border" />
39 : <Button
40 : variant="textonly"
41 : size="icon-sm"
42 : :class="actionButtonClass"
43 : :title="isCopied ? $t('g.copied') : $t('g.copyToClipboard')"
44 : :aria-label="isCopied ? $t('g.copied') : $t('g.copyToClipboard')"
45 : @click="handleCopy"
46 : @pointerdown.capture.stop
47 : >
48 : <i
49 : :class="
50 : cn(
51 : 'size-3',
52 : isCopied ? 'icon-[lucide--check]' : 'icon-[lucide--copy]'
53 : )
54 : "
55 : />
56 : </Button>
57 : <Button
58 : v-if="!widget.linkedUpstream"
59 : variant="textonly"
60 : size="icon-sm"
61 : :class="actionButtonClass"
62 : :title="$t('textEditor.expand')"
63 : :aria-label="$t('textEditor.expand')"
64 : @click="openTextEditor"
65 : @pointerdown.capture.stop
66 : >
67 : <i class="icon-[lucide--maximize-2] size-3" />
68 : </Button>
69 : </div>
70 : </div>
71 : <Textarea
72 : v-bind="filteredProps"
73 : :id
74 : ref="textAreaRef"
75 : :model-value="text"
76 : class="min-h-0 flex-1 resize-none overflow-hidden rounded-none bg-transparent pt-0 pr-4 font-mono text-(length:--comfy-textarea-font-size) leading-normal not-disabled:text-foreground hover:overflow-auto focus:overflow-auto focus-visible:ring-0"
77 : :placeholder
78 : :readonly="isReadOnly"
79 : data-capture-wheel="true"
80 : @pointerdown.capture.stop="trackFocus"
81 : @pointermove.capture.stop
82 : @pointerup.capture.stop
83 : @contextmenu.capture="handleContextMenu"
84 : @update:model-value="(value) => (modelValue = String(value))"
85 : />
86 : </div>
87 : </div>
88 : </template>
89 :
90 : <script setup lang="ts">
91 : import { refAutoReset } from '@vueuse/core'
92 : import { omit } from 'es-toolkit'
93 : import { computed, onBeforeUnmount, ref, useId, useTemplateRef } from 'vue'
94 : import { useI18n } from 'vue-i18n'
95 :
96 : import Button from '@comfyorg/design-system/button/Button'
97 : import Textarea from '@comfyorg/design-system/textarea/Textarea'
98 : import { useCopyToClipboard } from '@/composables/useCopyToClipboard'
99 : import { isNodeOptionsOpen } from '@/composables/graph/useMoreOptionsMenu'
100 : import { showTextEditor } from '@/renderer/extensions/textEditor/textEditorDialog'
101 : import type { SimplifiedWidget } from '@/types/simplifiedWidget'
102 : import { useHideLayoutField } from '@/types/widgetTypes'
103 : import { cn } from '@comfyorg/tailwind-utils'
104 : import {
105 : INPUT_EXCLUDED_PROPS,
106 : filterWidgetProps
107 : } from '@/utils/widgetPropFilter'
108 :
109 795 : const actionButtonClass =
110 : 'pointer-events-none rounded-sm hover:bg-muted group-hover:pointer-events-auto pointer-coarse:pointer-events-auto'
111 :
112 : const {
113 : widget,
114 : nodeTitle,
115 : placeholder = ''
116 : } = defineProps<{
117 : widget: SimplifiedWidget<string>
118 : nodeTitle?: string
119 : placeholder?: string
120 : }>()
121 :
122 2246 : const textAreaRef = useTemplateRef('textAreaRef')
123 :
124 2246 : const modelValue = defineModel<string | number | null>({ default: '' })
125 2246 : const text = computed(() => String(modelValue.value ?? ''))
126 :
127 2246 : const isFocused = ref(false)
128 7 : function trackFocus() {
129 7 : isFocused.value = document.activeElement === textAreaRef.value?.$el
130 : }
131 :
132 2246 : const hideLayoutField = useHideLayoutField()
133 2246 : const { copyToClipboard } = useCopyToClipboard()
134 2246 : const isCopied = refAutoReset(false, 2000)
135 :
136 2246 : const filteredProps = computed(() =>
137 : filterWidgetProps(widget.options, INPUT_EXCLUDED_PROPS)
138 : )
139 :
140 2246 : const displayName = computed(() => widget.label || widget.name)
141 2246 : const { t, n } = useI18n()
142 2246 : const characterCount = computed(() =>
143 : t(
144 : 'textEditor.characterCount',
145 : { count: n(text.value.length) },
146 : text.value.length
147 : )
148 : )
149 2246 : const id = useId()
150 :
151 2246 : const isReadOnly = computed(() =>
152 : Boolean(widget.options?.read_only || widget.options?.disabled)
153 : )
154 :
155 4 : function handleContextMenu(e: MouseEvent) {
156 4 : if (isNodeOptionsOpen() || isFocused.value) {
157 1 : e.stopPropagation()
158 1 : return
159 : }
160 3 : e.preventDefault()
161 : }
162 :
163 0 : async function handleCopy() {
164 0 : isCopied.value = await copyToClipboard(text.value, {
165 : toastOnSuccess: false
166 : })
167 : }
168 :
169 2246 : let closeTextEditor: (() => void) | undefined
170 :
171 2 : function openTextEditor() {
172 2 : closeTextEditor = showTextEditor({
173 : readText: () => text.value,
174 : widgetLabel: displayName.value,
175 : nodeTitle,
176 : readOnly: () => isReadOnly.value,
177 : onEdit: (text) => (modelValue.value = text),
178 : textareaAttributes: () => ({
179 : ...omit(filteredProps.value, ['disabled']),
180 : placeholder
181 : })
182 : })
183 : }
184 :
185 2246 : onBeforeUnmount(() => closeTextEditor?.())
186 : </script>
|