Line data Source code
1 16 : <template>
2 : <Tooltip :disabled="canStep">
3 : <TooltipTrigger as-child>
4 : <div
5 : ref="scrubElement"
6 : v-bind="$attrs"
7 : :class="
8 : cn(
9 : 'relative isolate flex min-w-0 items-center overflow-visible rounded-(--node-widget-radius) border-(length:--node-widget-border-width) border-node-border bg-input text-xs text-foreground focus-within:ring focus-within:ring-ring',
10 : useWidgetHeight(),
11 : className
12 : )
13 : "
14 : >
15 : <slot name="background" />
16 : <Button
17 : v-if="!hideButtons"
18 : :aria-label="t('g.decrement')"
19 : data-testid="decrement"
20 : class="h-full w-6 shrink-0 rounded-(--node-widget-radius)"
21 : variant="muted-textonly"
22 : size="icon-sm"
23 : :disabled="!canDecrement"
24 : tabindex="-1"
25 : @click="modelValue = clamp(modelValue - step)"
26 : >
27 : <i
28 : class="icon-[lucide--minus] triangle-icons:icon-[comfy--play] triangle-icons:size-2.5 triangle-icons:rotate-180"
29 : />
30 : </Button>
31 : <slot name="label" />
32 : <slot name="between" />
33 : <div
34 : class="relative my-0.25 ml-auto min-h-0 min-w-[4ch] self-stretch py-1.5"
35 : >
36 : <span aria-hidden="true" class="invisible block truncate px-2">
37 : {{ draft ?? displayValue ?? modelValue }}
38 : </span>
39 : <input
40 : ref="inputField"
41 : v-bind="inputAttrs"
42 : :value="draft ?? displayValue ?? modelValue"
43 : :disabled
44 : class="peer absolute inset-0 truncate border-0 bg-transparent p-1 pr-2 text-right text-xs focus:outline-0"
45 : inputmode="decimal"
46 : autocomplete="off"
47 : autocorrect="off"
48 : spellcheck="false"
49 : @input="handleInput"
50 : @focus="valueBeforeEditing = modelValue"
51 : @blur="commitDraft"
52 : @keyup.enter="commitDraft"
53 : @keydown.esc.prevent.stop="cancelTextEdit"
54 : @keydown.up.prevent="updateValueBy(step)"
55 : @keydown.down.prevent="updateValueBy(-step)"
56 : @keydown.page-up.prevent="updateValueBy(10 * step)"
57 : @keydown.page-down.prevent="updateValueBy(-10 * step)"
58 : />
59 : <div
60 : class="absolute inset-0 z-10 cursor-ew-resize touch-pan-y peer-focus:hidden"
61 : />
62 : </div>
63 : <slot />
64 : <Button
65 : v-if="!hideButtons"
66 : :aria-label="t('g.increment')"
67 : data-testid="increment"
68 : class="h-full w-6 shrink-0 rounded-(--node-widget-radius)"
69 : variant="muted-textonly"
70 : size="icon-sm"
71 : :disabled="!canIncrement"
72 : tabindex="-1"
73 : @click="modelValue = clamp(modelValue + step)"
74 : >
75 : <i
76 : class="icon-[lucide--plus] triangle-icons:icon-[comfy--play] triangle-icons:size-2.5"
77 : />
78 : </Button>
79 : </div>
80 : </TooltipTrigger>
81 : <TooltipContent v-if="!canStep">{{
82 : t('widgets.precisionLimit')
83 : }}</TooltipContent>
84 : </Tooltip>
85 : </template>
86 :
87 : <script setup lang="ts">
88 : import { useFocus } from '@vueuse/core'
89 : import { computed, ref, useTemplateRef } from 'vue'
90 : import type { HTMLAttributes } from 'vue'
91 : import { useI18n } from 'vue-i18n'
92 :
93 : import Button from '@comfyorg/design-system/button/Button'
94 : import Tooltip from '@comfyorg/design-system/tooltip/Tooltip'
95 : import TooltipContent from '@comfyorg/design-system/tooltip/TooltipContent'
96 : import TooltipTrigger from '@comfyorg/design-system/tooltip/TooltipTrigger'
97 : import { useNumberScrub } from '@/composables/useNumberScrub'
98 : import { useWidgetHeight } from '@/types/widgetTypes'
99 : import { cn } from '@comfyorg/tailwind-utils'
100 :
101 : const {
102 : class: className,
103 : min = -Number.MAX_VALUE,
104 : max = Number.MAX_VALUE,
105 : step = 1,
106 : disabled = false,
107 : hideButtons = false,
108 : displayValue,
109 : parseValue
110 : } = defineProps<{
111 : class?: HTMLAttributes['class']
112 : min?: number
113 : max?: number
114 : step?: number
115 : disabled?: boolean
116 : hideButtons?: boolean
117 : displayValue?: string
118 : parseValue?: (raw: string) => number | undefined
119 : inputAttrs?: Record<string, unknown>
120 : }>()
121 :
122 : defineOptions({ inheritAttrs: false })
123 :
124 8682 : const { t } = useI18n()
125 8682 : const modelValue = defineModel<number>({ default: 0 })
126 8682 : const draft = ref<string>()
127 8682 : let valueBeforeEditing = modelValue.value
128 :
129 8682 : const scrubElement = useTemplateRef<HTMLDivElement>('scrubElement')
130 8682 : const inputField = useTemplateRef<HTMLInputElement>('inputField')
131 8682 : const { focused } = useFocus(inputField)
132 :
133 63 : function clamp(value: number): number {
134 63 : return Math.min(max, Math.max(min, value))
135 : }
136 :
137 8682 : const canDecrement = computed(
138 : () => modelValue.value > min && !disabled && canStep.value
139 : )
140 8682 : const canIncrement = computed(
141 : () => modelValue.value < max && !disabled && canStep.value
142 : )
143 8682 : const canStep = computed(
144 : () =>
145 : Number.isFinite(modelValue.value) &&
146 : Math.abs(modelValue.value) <= Number.MAX_SAFE_INTEGER
147 : )
148 :
149 45 : function handleInput(event: Event) {
150 45 : if (event.target instanceof HTMLInputElement) draft.value = event.target.value
151 : }
152 :
153 56 : function commitDraft(): number {
154 56 : if (draft.value === undefined) return modelValue.value
155 44 : const raw = draft.value.trim()
156 56 : const parsed = parseValue
157 : ? parseValue(raw)
158 : : raw === ''
159 : ? undefined
160 : : Number(raw)
161 56 : draft.value = undefined
162 56 : if (parsed != null && !isNaN(parsed)) {
163 43 : const value = clamp(parsed)
164 43 : modelValue.value = value
165 43 : return value
166 : }
167 1 : return modelValue.value
168 : }
169 :
170 0 : function cancelTextEdit() {
171 0 : draft.value = undefined
172 0 : modelValue.value = valueBeforeEditing
173 0 : inputField.value?.blur()
174 : }
175 :
176 0 : function startTextEdit() {
177 0 : inputField.value?.focus()
178 0 : inputField.value?.select()
179 : }
180 :
181 8682 : useNumberScrub(scrubElement, {
182 : value: modelValue,
183 : min: () => Math.max(min, -Number.MAX_SAFE_INTEGER),
184 : max: () => Math.min(max, Number.MAX_SAFE_INTEGER),
185 : step: () => step,
186 : disabled: () => disabled || focused.value,
187 : onScrub: (value) => {
188 10 : modelValue.value = value
189 : },
190 : onClick: startTextEdit
191 : })
192 :
193 6 : function updateValueBy(delta: number) {
194 6 : modelValue.value = Math.min(max, Math.max(min, modelValue.value + delta))
195 6 : draft.value = undefined
196 : }
197 : </script>
|