Line data Source code
1 : import type { CurveData } from '@comfyorg/graph-model/curve'
2 : import type { LGraphNode } from '@/lib/litegraph/src/litegraph'
3 : import type { ICurveWidget } from '@comfyorg/graph-model/widgets'
4 : import type {
5 : CurveInputSpec,
6 : InputSpec as InputSpecV2
7 : } from '@comfyorg/object-info-parser/schemas/nodeDefSchemaV2'
8 : import type { ComfyWidgetConstructorV2 } from '@/types/widgetConstructor'
9 :
10 2356 : const DEFAULT_CURVE_DATA: CurveData = {
11 : points: [
12 : [0, 0],
13 : [1, 1]
14 : ],
15 : interpolation: 'monotone_cubic'
16 : }
17 :
18 2356 : export const useCurveWidget = (): ComfyWidgetConstructorV2 => {
19 2356 : return (node: LGraphNode, inputSpec: InputSpecV2): ICurveWidget => {
20 3 : const spec = inputSpec as CurveInputSpec
21 3 : const defaultValue: CurveData = spec.default
22 : ? { ...spec.default, points: [...spec.default.points] }
23 : : { ...DEFAULT_CURVE_DATA, points: [...DEFAULT_CURVE_DATA.points] }
24 :
25 3 : const rawWidget = node.addWidget(
26 : 'curve',
27 : spec.name,
28 : defaultValue,
29 : () => {},
30 : { hideInPanel: true }
31 : )
32 :
33 3 : if (rawWidget.type !== 'curve') {
34 0 : throw new Error(`Unexpected widget type: ${rawWidget.type}`)
35 : }
36 :
37 3 : return rawWidget as ICurveWidget
38 : }
39 : }
|