Fix regression in legacy widget-values-as-function (#990)

Resolves issue with KJNodes get/set nodes.

![image](https://github.com/user-attachments/assets/ecbf99d4-b2d3-48bf-a34c-65d0bf451b4c)

Downstream consumer:

c3dc82108a/web/js/setgetnodes.js (L401-L404)
This commit is contained in:
filtered
2025-05-02 02:50:26 +10:00
committed by GitHub
parent cfabeb0821
commit 7d38f6bdc7

View File

@@ -1,3 +1,4 @@
import type { LGraphNode } from "@/LGraphNode"
import type { IComboWidget, IWidgetOptions } from "@/types/widgets"
import { clamp, LiteGraph } from "@/litegraph"
@@ -5,7 +6,13 @@ import { clamp, LiteGraph } from "@/litegraph"
import { BaseSteppedWidget } from "./BaseSteppedWidget"
import { BaseWidget, type DrawWidgetOptions, type WidgetEventOptions } from "./BaseWidget"
type Values = string[] | Record<string, string>
/**
* This is used as an (invalid) assertion to resolve issues with legacy duck-typed values.
*
* Function style in use by:
* https://github.com/kijai/ComfyUI-KJNodes/blob/c3dc82108a2a86c17094107ead61d63f8c76200e/web/js/setgetnodes.js#L401-L404
*/
type Values = string[] | Record<string, string> | ((widget: ComboWidget, node: LGraphNode) => string[])
function toArray(values: Values): string[] {
return Array.isArray(values) ? values : Object.keys(values)
@@ -24,12 +31,11 @@ export class ComboWidget extends BaseSteppedWidget implements IComboWidget {
this.value = widget.value
}
#getValues(): Values {
#getValues(node: LGraphNode): Values {
const { values } = this.options
if (values == null) throw new Error("[ComboWidget]: values is required")
return typeof values === "function"
// @ts-expect-error handle () => string[] type that is not typed in IWidgetOptions
? values(this, node)
: values
}
@@ -65,7 +71,7 @@ export class ComboWidget extends BaseSteppedWidget implements IComboWidget {
}
#tryChangeValue(delta: number, options: WidgetEventOptions): void {
const values = this.#getValues()
const values = this.#getValues(options.node)
const indexedValues = toArray(values)
// avoids double click event
@@ -195,7 +201,7 @@ export class ComboWidget extends BaseSteppedWidget implements IComboWidget {
if (x > width - 40) return this.incrementValue({ e, node, canvas })
// Otherwise, show dropdown menu
const values = this.#getValues()
const values = this.#getValues(node)
const values_list = toArray(values)
// Handle center click - show dropdown menu