Line data Source code
1 : import type { LGraphNode } from '@/lib/litegraph/src/litegraph'
2 : import type {
3 : IRangeWidget,
4 : IWidgetRangeOptions
5 : } from '@comfyorg/graph-model/widgets'
6 : import type { RangeInputSpec } from '@comfyorg/object-info-parser/schemas/nodeDefSchemaV2'
7 : import type { ComfyWidgetConstructorV2 } from '@/types/widgetConstructor'
8 :
9 2356 : export const useRangeWidget = (): ComfyWidgetConstructorV2 => {
10 2356 : return (node: LGraphNode, inputSpec): IRangeWidget => {
11 0 : const spec = inputSpec as RangeInputSpec
12 0 : const defaultValue = spec.default ?? { min: 0.0, max: 1.0 }
13 :
14 0 : const options: IWidgetRangeOptions = {
15 : display: spec.display,
16 : gradient_stops: spec.gradient_stops,
17 : show_midpoint: spec.show_midpoint,
18 : midpoint_scale: spec.midpoint_scale,
19 : value_min: spec.value_min,
20 : value_max: spec.value_max,
21 : hideInPanel: true
22 : }
23 :
24 0 : const rawWidget = node.addWidget(
25 : 'range',
26 : spec.name,
27 : { ...defaultValue },
28 : () => {},
29 : options
30 : )
31 :
32 0 : if (rawWidget.type !== 'range') {
33 0 : throw new Error(`Unexpected widget type: ${rawWidget.type}`)
34 : }
35 :
36 0 : return rawWidget as IRangeWidget
37 : }
38 : }
|