From 13441add242845329baab904c033bccb9c528d80 Mon Sep 17 00:00:00 2001 From: filtered <176114999+webfiltered@users.noreply.github.com> Date: Thu, 8 May 2025 10:25:54 +1000 Subject: [PATCH] Convert DOM widgets to Litegraph widget subclass (#3813) --- src/scripts/domWidget.ts | 36 ++++++++++++++---------------------- 1 file changed, 14 insertions(+), 22 deletions(-) diff --git a/src/scripts/domWidget.ts b/src/scripts/domWidget.ts index 6fa5ff9cb..7b94eeab7 100644 --- a/src/scripts/domWidget.ts +++ b/src/scripts/domWidget.ts @@ -1,7 +1,6 @@ -import { LGraphNode, LiteGraph } from '@comfyorg/litegraph' +import { LGraphNode, LegacyWidget, LiteGraph } from '@comfyorg/litegraph' import type { IBaseWidget, - ICustomWidget, IWidgetOptions } from '@comfyorg/litegraph/dist/types/widgets' import _ from 'lodash' @@ -13,9 +12,9 @@ import { useDomWidgetStore } from '@/stores/domWidgetStore' import { generateUUID } from '@/utils/formatUtil' export interface BaseDOMWidget - extends ICustomWidget { + extends IBaseWidget> { // ICustomWidget properties - type: 'custom' + type: string options: DOMWidgetOptions value: V callback?: (value: V) => void @@ -89,18 +88,15 @@ export const isComponentWidget = ( ): widget is ComponentWidget => 'component' in widget && !!widget.component abstract class BaseDOMWidgetImpl + extends LegacyWidget>> implements BaseDOMWidget { static readonly DEFAULT_MARGIN = 10 - readonly type: 'custom' - readonly name: string - readonly options: DOMWidgetOptions - computedHeight?: number - y: number = 0 - callback?: (value: V) => void + declare readonly name: string + declare readonly options: DOMWidgetOptions + declare callback?: (value: V) => void readonly id: string - readonly node: LGraphNode constructor(obj: { node: LGraphNode @@ -108,20 +104,17 @@ abstract class BaseDOMWidgetImpl type: string options: DOMWidgetOptions }) { - // @ts-expect-error custom widget type - this.type = obj.type - this.name = obj.name - this.options = obj.options + const { node, name, type, options } = obj + super({ y: 0, name, type, options }, node) this.id = generateUUID() - this.node = obj.node } - get value(): V { + override get value(): V { return this.options.getValue?.() ?? ('' as V) } - set value(v: V) { + override set value(v: V) { this.options.setValue?.(v) this.callback?.(this.value) } @@ -134,7 +127,7 @@ abstract class BaseDOMWidgetImpl return !['hidden'].includes(this.type) && this.node.isWidgetVisible(this) } - draw( + override draw( ctx: CanvasRenderingContext2D, _node: LGraphNode, widget_width: number, @@ -168,7 +161,7 @@ export class DOMWidgetImpl extends BaseDOMWidgetImpl implements DOMWidget { - readonly element: T + override readonly element: T constructor(obj: { node: LGraphNode @@ -183,7 +176,6 @@ export class DOMWidgetImpl /** Extract DOM widget size info */ computeLayoutSize(node: LGraphNode) { - // @ts-expect-error custom widget type if (this.type === 'hidden') { return { minHeight: 0, @@ -257,7 +249,7 @@ export class ComponentWidgetImpl } } - serializeValue(): V { + override serializeValue(): V { return toRaw(this.value) } }