|
1 | 1 | import type { LGraphNode } from '@comfyorg/litegraph' |
2 | 2 | import type { IComboWidget } from '@comfyorg/litegraph/dist/types/widgets' |
3 | 3 |
|
| 4 | +import { transformInputSpecV2ToV1 } from '@/schemas/nodeDef/migration' |
4 | 5 | import { |
| 6 | + ComboInputSpec, |
5 | 7 | type InputSpec, |
6 | | - getComboSpecComboOptions, |
7 | 8 | isComboInputSpec |
8 | | -} from '@/schemas/nodeDefSchema' |
9 | | -import { addValueControlWidgets } from '@/scripts/widgets' |
10 | | -import type { ComfyWidgetConstructor } from '@/scripts/widgets' |
11 | | -import { useWidgetStore } from '@/stores/widgetStore' |
| 9 | +} from '@/schemas/nodeDef/nodeDefSchemaV2' |
| 10 | +import { |
| 11 | + type ComfyWidgetConstructorV2, |
| 12 | + addValueControlWidgets |
| 13 | +} from '@/scripts/widgets' |
12 | 14 |
|
13 | 15 | import { useRemoteWidget } from './useRemoteWidget' |
14 | 16 |
|
| 17 | +const getDefaultValue = (inputSpec: ComboInputSpec) => { |
| 18 | + if (inputSpec.default) return inputSpec.default |
| 19 | + if (inputSpec.options?.length) return inputSpec.options[0] |
| 20 | + if (inputSpec.remote) return 'Loading...' |
| 21 | + return undefined |
| 22 | +} |
| 23 | + |
15 | 24 | export const useComboWidget = () => { |
16 | | - const widgetConstructor: ComfyWidgetConstructor = ( |
| 25 | + const widgetConstructor: ComfyWidgetConstructorV2 = ( |
17 | 26 | node: LGraphNode, |
18 | | - inputName: string, |
19 | | - inputData: InputSpec |
| 27 | + inputSpec: InputSpec |
20 | 28 | ) => { |
21 | | - if (!isComboInputSpec(inputData)) { |
22 | | - throw new Error(`Invalid input data: ${inputData}`) |
| 29 | + if (!isComboInputSpec(inputSpec)) { |
| 30 | + throw new Error(`Invalid input data: ${inputSpec}`) |
23 | 31 | } |
24 | 32 |
|
25 | | - const widgetStore = useWidgetStore() |
26 | | - const inputOptions = inputData[1] ?? {} |
27 | | - const comboOptions = getComboSpecComboOptions(inputData) |
28 | | - |
29 | | - const defaultValue = widgetStore.getDefaultValue(inputData) |
| 33 | + const comboOptions = inputSpec.options ?? [] |
| 34 | + const defaultValue = getDefaultValue(inputSpec) |
30 | 35 |
|
31 | | - const res = { |
32 | | - widget: node.addWidget('combo', inputName, defaultValue, () => {}, { |
| 36 | + const widget = node.addWidget( |
| 37 | + 'combo', |
| 38 | + inputSpec.name, |
| 39 | + defaultValue, |
| 40 | + () => {}, |
| 41 | + { |
33 | 42 | values: comboOptions |
34 | | - }) as IComboWidget |
35 | | - } |
| 43 | + } |
| 44 | + ) as IComboWidget |
36 | 45 |
|
37 | | - if (inputOptions.remote) { |
| 46 | + if (inputSpec.remote) { |
38 | 47 | const remoteWidget = useRemoteWidget({ |
39 | | - inputData, |
| 48 | + remoteConfig: inputSpec.remote, |
40 | 49 | defaultValue, |
41 | 50 | node, |
42 | | - widget: res.widget |
| 51 | + widget |
43 | 52 | }) |
44 | | - if (inputOptions.remote.refresh_button) remoteWidget.addRefreshButton() |
| 53 | + if (inputSpec.remote.refresh_button) remoteWidget.addRefreshButton() |
45 | 54 |
|
46 | | - const origOptions = res.widget.options |
47 | | - res.widget.options = new Proxy( |
48 | | - origOptions as Record<string | symbol, any>, |
49 | | - { |
50 | | - get(target, prop: string | symbol) { |
51 | | - if (prop !== 'values') return target[prop] |
52 | | - return remoteWidget.getValue() |
53 | | - } |
| 55 | + const origOptions = widget.options |
| 56 | + widget.options = new Proxy(origOptions as Record<string | symbol, any>, { |
| 57 | + get(target, prop: string | symbol) { |
| 58 | + if (prop !== 'values') return target[prop] |
| 59 | + return remoteWidget.getValue() |
54 | 60 | } |
55 | | - ) |
| 61 | + }) |
56 | 62 | } |
57 | 63 |
|
58 | | - if (inputOptions.control_after_generate) { |
59 | | - res.widget.linkedWidgets = addValueControlWidgets( |
| 64 | + if (inputSpec.control_after_generate) { |
| 65 | + widget.linkedWidgets = addValueControlWidgets( |
60 | 66 | node, |
61 | | - res.widget, |
| 67 | + widget, |
62 | 68 | undefined, |
63 | 69 | undefined, |
64 | | - inputData |
| 70 | + transformInputSpecV2ToV1(inputSpec) |
65 | 71 | ) |
66 | 72 | } |
67 | | - return res |
| 73 | + return widget |
68 | 74 | } |
69 | 75 |
|
70 | 76 | return widgetConstructor |
|
0 commit comments