Line data Source code
1 16 : <script setup lang="ts">
2 : import { computed, useId } from 'vue'
3 : import { useI18n } from 'vue-i18n'
4 :
5 : import ScrubableNumberInput from '@/components/common/ScrubableNumberInput.vue'
6 : import { evaluateInput } from '@/lib/litegraph/src/utils/widget'
7 : import { useNumberStepCalculation } from '@/renderer/extensions/vueNodes/widgets/composables/useNumberStepCalculation'
8 : import type { SimplifiedWidget } from '@/types/simplifiedWidget'
9 : import { cn } from '@comfyorg/tailwind-utils'
10 : import {
11 : INPUT_EXCLUDED_PROPS,
12 : filterWidgetProps
13 : } from '@/utils/widgetPropFilter'
14 :
15 : import WidgetLayoutFieldLabel from './layout/WidgetLayoutFieldLabel.vue'
16 :
17 8415 : const { locale } = useI18n()
18 8415 : const inputId = useId()
19 :
20 8415 : const props = defineProps<{
21 : widget: SimplifiedWidget<number>
22 : rootClass?: string
23 : }>()
24 :
25 8574 : function formatNumber(value: number, options?: Intl.NumberFormatOptions) {
26 8574 : return new Intl.NumberFormat(locale.value, options).format(value)
27 : }
28 :
29 8415 : const decimalSeparator = computed(() =>
30 : formatNumber(1.1).replace(/\p{Number}/gu, '')
31 : )
32 8415 : const groupSeparator = computed(() =>
33 : formatNumber(11111).replace(/\p{Number}/gu, '')
34 : )
35 31 : function unformatValue(value: string) {
36 31 : return value
37 : .replaceAll(groupSeparator.value, '')
38 : .replaceAll(decimalSeparator.value, '.')
39 : }
40 :
41 8415 : const modelValue = defineModel<number>({ default: 0 })
42 :
43 8415 : const displayValue = computed(() => {
44 8518 : const value = modelValue.value
45 8518 : if ((value as unknown) === '' || !isFinite(value)) return `${value}`
46 :
47 8516 : const options: Intl.NumberFormatOptions = {
48 : useGrouping: useGrouping.value
49 : }
50 8516 : if (precision.value !== undefined) {
51 8491 : options.minimumFractionDigits = precision.value
52 8491 : options.maximumFractionDigits = precision.value
53 : }
54 8516 : return formatNumber(value, options)
55 : })
56 :
57 31 : function parseValue(raw: string): number | undefined {
58 31 : return evaluateInput(unformatValue(raw))
59 : }
60 :
61 : interface NumericWidgetOptions {
62 : min: number
63 : max: number
64 : step?: number
65 : step2?: number
66 : precision?: number
67 : disabled?: boolean
68 : useGrouping?: boolean
69 : }
70 :
71 8415 : const filteredProps = computed(() => {
72 : const filtered = filterWidgetProps(props.widget.options, INPUT_EXCLUDED_PROPS)
73 12028 : return filtered as Partial<NumericWidgetOptions>
74 : })
75 :
76 8415 : const disabled = computed(() => props.widget.options?.disabled ?? false)
77 :
78 8415 : const precision = computed(() => {
79 12028 : const p = props.widget.options?.precision
80 12028 : return typeof p === 'number' && p >= 0 ? p : undefined
81 : })
82 :
83 8415 : const step = useNumberStepCalculation(() => props.widget.options, precision)
84 :
85 8415 : const useGrouping = computed(() => {
86 12026 : return props.widget.options?.useGrouping === true
87 : })
88 :
89 8415 : const sliderWidth = computed(() => {
90 12028 : const { max, min, step } = filteredProps.value
91 12028 : if (
92 : min === undefined ||
93 : max === undefined ||
94 : step === undefined ||
95 : (max - min) / step >= 100
96 : )
97 10052 : return 0
98 : const ratio = (modelValue.value - min) / (max - min)
99 1976 : return (ratio * 100).toFixed(0)
100 : })
101 :
102 8415 : const inputAttrs = computed(() => ({
103 : id: inputId,
104 : 'aria-label': props.widget.label || props.widget.name,
105 : 'aria-valuenow': modelValue.value,
106 : 'aria-valuemin': filteredProps.value.min,
107 : 'aria-valuemax': filteredProps.value.max,
108 : role: 'spinbutton',
109 : tabindex: 0
110 : }))
111 : </script>
112 :
113 : <template>
114 : <ScrubableNumberInput
115 : v-model="modelValue"
116 : data-testid="widget-number-row"
117 : :min="filteredProps.min"
118 : :max="filteredProps.max"
119 : :step
120 : :display-value
121 : :disabled
122 : :parse-value
123 : :input-attrs
124 : :class="
125 : cn('cursor-ew-resize touch-pan-y', widget.borderStyle, props.rootClass)
126 : "
127 : @pointerdown.stop
128 : @pointermove.stop
129 : @pointerup.stop
130 : >
131 : <template #label>
132 : <WidgetLayoutFieldLabel :widget :input-id class="pl-2" />
133 : </template>
134 : <template #between><slot name="between" /></template>
135 : <template #background>
136 : <div
137 : class="pointer-events-none absolute inset-0 -z-10 overflow-clip rounded-md"
138 : >
139 : <div
140 : class="size-full bg-foreground/10"
141 : :style="{ width: `${sliderWidth}%` }"
142 : />
143 : </div>
144 : </template>
145 : <slot />
146 : </ScrubableNumberInput>
147 : </template>
|