Line data Source code
1 : <template>
2 21706 : <div class="editable-text inline">
3 : <component :is="labelType" v-if="!isEditing" :class="labelClass">
4 : {{ modelValue }}
5 : </component>
6 : <Input
7 : v-else
8 : ref="inputRef"
9 : v-model="inputValue"
10 : v-focus
11 : type="text"
12 : class="h-full rounded-none p-0 focus-visible:ring-0"
13 : v-bind="inputAttrs"
14 : @blur="finishEditing"
15 : @keydown.enter.capture.stop="inputRef?.blur()"
16 : @keydown.escape.capture.stop="cancelEditing"
17 : @click.stop
18 : @contextmenu.stop
19 : @pointerdown.stop.capture
20 : @pointermove.stop.capture
21 : />
22 : </div>
23 : </template>
24 :
25 : <script setup lang="ts">
26 : import { nextTick, ref, watch } from 'vue'
27 :
28 : import Input from '@comfyorg/design-system/input/Input'
29 :
30 : const {
31 : modelValue,
32 : isEditing = false,
33 : inputAttrs = {},
34 : labelType = 'span'
35 : } = defineProps<{
36 : modelValue: string
37 : isEditing?: boolean
38 : inputAttrs?: Record<string, string>
39 : labelClass?: string
40 : labelType?: string
41 : }>()
42 :
43 21526 : const emit = defineEmits(['edit', 'cancel'])
44 21526 : const inputValue = ref<string>(modelValue)
45 21526 : const inputRef = ref<InstanceType<typeof Input>>()
46 21526 : const isCanceling = ref(false)
47 :
48 33 : function finishEditing() {
49 33 : if (!isCanceling.value) {
50 30 : emit('edit', inputValue.value)
51 : }
52 33 : isCanceling.value = false
53 : }
54 :
55 3 : function cancelEditing() {
56 3 : isCanceling.value = true
57 3 : inputValue.value = modelValue
58 3 : emit('cancel')
59 3 : inputRef.value?.blur()
60 : }
61 :
62 21526 : watch(
63 : () => isEditing,
64 : async (newVal) => {
65 21553 : if (newVal) {
66 43 : inputValue.value = modelValue
67 43 : await nextTick(() => {
68 43 : if (!inputRef.value) return
69 43 : const fileName = inputValue.value.includes('.')
70 : ? inputValue.value.split('.').slice(0, -1).join('.')
71 : : inputValue.value
72 43 : inputRef.value.setSelectionRange(0, fileName.length)
73 : })
74 : }
75 : },
76 : { immediate: true }
77 : )
78 :
79 21526 : const vFocus = {
80 : mounted: (el: HTMLElement) => el.focus()
81 : }
82 : </script>
|