mirror of
https://github.com/Comfy-Org/ComfyUI_frontend.git
synced 2026-04-30 03:01:54 +00:00
Convert DOM widgets to Litegraph widget subclass (#3813)
This commit is contained in:
@@ -1,7 +1,6 @@
|
|||||||
import { LGraphNode, LiteGraph } from '@comfyorg/litegraph'
|
import { LGraphNode, LegacyWidget, LiteGraph } from '@comfyorg/litegraph'
|
||||||
import type {
|
import type {
|
||||||
IBaseWidget,
|
IBaseWidget,
|
||||||
ICustomWidget,
|
|
||||||
IWidgetOptions
|
IWidgetOptions
|
||||||
} from '@comfyorg/litegraph/dist/types/widgets'
|
} from '@comfyorg/litegraph/dist/types/widgets'
|
||||||
import _ from 'lodash'
|
import _ from 'lodash'
|
||||||
@@ -13,9 +12,9 @@ import { useDomWidgetStore } from '@/stores/domWidgetStore'
|
|||||||
import { generateUUID } from '@/utils/formatUtil'
|
import { generateUUID } from '@/utils/formatUtil'
|
||||||
|
|
||||||
export interface BaseDOMWidget<V extends object | string>
|
export interface BaseDOMWidget<V extends object | string>
|
||||||
extends ICustomWidget {
|
extends IBaseWidget<V, string, DOMWidgetOptions<V>> {
|
||||||
// ICustomWidget properties
|
// ICustomWidget properties
|
||||||
type: 'custom'
|
type: string
|
||||||
options: DOMWidgetOptions<V>
|
options: DOMWidgetOptions<V>
|
||||||
value: V
|
value: V
|
||||||
callback?: (value: V) => void
|
callback?: (value: V) => void
|
||||||
@@ -89,18 +88,15 @@ export const isComponentWidget = <V extends object | string>(
|
|||||||
): widget is ComponentWidget<V> => 'component' in widget && !!widget.component
|
): widget is ComponentWidget<V> => 'component' in widget && !!widget.component
|
||||||
|
|
||||||
abstract class BaseDOMWidgetImpl<V extends object | string>
|
abstract class BaseDOMWidgetImpl<V extends object | string>
|
||||||
|
extends LegacyWidget<IBaseWidget<V, string, DOMWidgetOptions<V>>>
|
||||||
implements BaseDOMWidget<V>
|
implements BaseDOMWidget<V>
|
||||||
{
|
{
|
||||||
static readonly DEFAULT_MARGIN = 10
|
static readonly DEFAULT_MARGIN = 10
|
||||||
readonly type: 'custom'
|
declare readonly name: string
|
||||||
readonly name: string
|
declare readonly options: DOMWidgetOptions<V>
|
||||||
readonly options: DOMWidgetOptions<V>
|
declare callback?: (value: V) => void
|
||||||
computedHeight?: number
|
|
||||||
y: number = 0
|
|
||||||
callback?: (value: V) => void
|
|
||||||
|
|
||||||
readonly id: string
|
readonly id: string
|
||||||
readonly node: LGraphNode
|
|
||||||
|
|
||||||
constructor(obj: {
|
constructor(obj: {
|
||||||
node: LGraphNode
|
node: LGraphNode
|
||||||
@@ -108,20 +104,17 @@ abstract class BaseDOMWidgetImpl<V extends object | string>
|
|||||||
type: string
|
type: string
|
||||||
options: DOMWidgetOptions<V>
|
options: DOMWidgetOptions<V>
|
||||||
}) {
|
}) {
|
||||||
// @ts-expect-error custom widget type
|
const { node, name, type, options } = obj
|
||||||
this.type = obj.type
|
super({ y: 0, name, type, options }, node)
|
||||||
this.name = obj.name
|
|
||||||
this.options = obj.options
|
|
||||||
|
|
||||||
this.id = generateUUID()
|
this.id = generateUUID()
|
||||||
this.node = obj.node
|
|
||||||
}
|
}
|
||||||
|
|
||||||
get value(): V {
|
override get value(): V {
|
||||||
return this.options.getValue?.() ?? ('' as V)
|
return this.options.getValue?.() ?? ('' as V)
|
||||||
}
|
}
|
||||||
|
|
||||||
set value(v: V) {
|
override set value(v: V) {
|
||||||
this.options.setValue?.(v)
|
this.options.setValue?.(v)
|
||||||
this.callback?.(this.value)
|
this.callback?.(this.value)
|
||||||
}
|
}
|
||||||
@@ -134,7 +127,7 @@ abstract class BaseDOMWidgetImpl<V extends object | string>
|
|||||||
return !['hidden'].includes(this.type) && this.node.isWidgetVisible(this)
|
return !['hidden'].includes(this.type) && this.node.isWidgetVisible(this)
|
||||||
}
|
}
|
||||||
|
|
||||||
draw(
|
override draw(
|
||||||
ctx: CanvasRenderingContext2D,
|
ctx: CanvasRenderingContext2D,
|
||||||
_node: LGraphNode,
|
_node: LGraphNode,
|
||||||
widget_width: number,
|
widget_width: number,
|
||||||
@@ -168,7 +161,7 @@ export class DOMWidgetImpl<T extends HTMLElement, V extends object | string>
|
|||||||
extends BaseDOMWidgetImpl<V>
|
extends BaseDOMWidgetImpl<V>
|
||||||
implements DOMWidget<T, V>
|
implements DOMWidget<T, V>
|
||||||
{
|
{
|
||||||
readonly element: T
|
override readonly element: T
|
||||||
|
|
||||||
constructor(obj: {
|
constructor(obj: {
|
||||||
node: LGraphNode
|
node: LGraphNode
|
||||||
@@ -183,7 +176,6 @@ export class DOMWidgetImpl<T extends HTMLElement, V extends object | string>
|
|||||||
|
|
||||||
/** Extract DOM widget size info */
|
/** Extract DOM widget size info */
|
||||||
computeLayoutSize(node: LGraphNode) {
|
computeLayoutSize(node: LGraphNode) {
|
||||||
// @ts-expect-error custom widget type
|
|
||||||
if (this.type === 'hidden') {
|
if (this.type === 'hidden') {
|
||||||
return {
|
return {
|
||||||
minHeight: 0,
|
minHeight: 0,
|
||||||
@@ -257,7 +249,7 @@ export class ComponentWidgetImpl<V extends object | string>
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
serializeValue(): V {
|
override serializeValue(): V {
|
||||||
return toRaw(this.value)
|
return toRaw(this.value)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user