diff --git a/src/widgets/ComboWidget.ts b/src/widgets/ComboWidget.ts index 4de0a816c..a19910ad2 100644 --- a/src/widgets/ComboWidget.ts +++ b/src/widgets/ComboWidget.ts @@ -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 +/** + * 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 | ((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