Revert "Add support for LiteGraph to convert to classes (#334)" (#386)

This reverts commit e2141a81e2.
This commit is contained in:
Chenlei Hu
2024-08-12 09:19:10 -04:00
committed by GitHub
parent d9df0328c5
commit d607f6c7f7
7 changed files with 37 additions and 49 deletions

View File

@@ -8,19 +8,19 @@ const ext = {
init() { init() {
const ctxMenu = LiteGraph.ContextMenu const ctxMenu = LiteGraph.ContextMenu
// @ts-expect-error // @ts-expect-error
// TODO Very hacky way to modify Litegraph behaviour. Fix ctx later. // TODO Very hacky way to modify Litegraph behaviour. Fix this later.
LiteGraph.ContextMenu = function (values, options) { LiteGraph.ContextMenu = function (values, options) {
const ctx = new ctxMenu(values, options) const ctx = ctxMenu.call(this, values, options)
// If we are a dark menu (only used for combo boxes) then add a filter input // If we are a dark menu (only used for combo boxes) then add a filter input
if (options?.className === 'dark' && values?.length > 4) { if (options?.className === 'dark' && values?.length > 10) {
const filter = document.createElement('input') const filter = document.createElement('input')
filter.classList.add('comfy-context-menu-filter') filter.classList.add('comfy-context-menu-filter')
filter.placeholder = 'Filter list' filter.placeholder = 'Filter list'
ctx.root.prepend(filter) this.root.prepend(filter)
const items = Array.from( const items = Array.from(
ctx.root.querySelectorAll('.litemenu-entry') this.root.querySelectorAll('.litemenu-entry')
) as HTMLElement[] ) as HTMLElement[]
let displayedItems = [...items] let displayedItems = [...items]
let itemCount = displayedItems.length let itemCount = displayedItems.length
@@ -61,16 +61,16 @@ const ext = {
} }
const positionList = () => { const positionList = () => {
const rect = ctx.root.getBoundingClientRect() const rect = this.root.getBoundingClientRect()
// If the top is off-screen then shift the element with scaling applied // If the top is off-screen then shift the element with scaling applied
if (rect.top < 0) { if (rect.top < 0) {
const scale = const scale =
1 - 1 -
ctx.root.getBoundingClientRect().height / this.root.getBoundingClientRect().height /
ctx.root.clientHeight this.root.clientHeight
const shift = (ctx.root.clientHeight * scale) / 2 const shift = (this.root.clientHeight * scale) / 2
ctx.root.style.top = -shift + 'px' this.root.style.top = -shift + 'px'
} }
} }
@@ -109,7 +109,7 @@ const ext = {
selectedItem?.click() selectedItem?.click()
break break
case 'Escape': case 'Escape':
ctx.close() this.close()
break break
} }
}) })
@@ -140,7 +140,7 @@ const ext = {
let top = options.event.clientY - 10 let top = options.event.clientY - 10
const bodyRect = document.body.getBoundingClientRect() const bodyRect = document.body.getBoundingClientRect()
const rootRect = ctx.root.getBoundingClientRect() const rootRect = this.root.getBoundingClientRect()
if ( if (
bodyRect.height && bodyRect.height &&
top > bodyRect.height - rootRect.height - 10 top > bodyRect.height - rootRect.height - 10
@@ -148,7 +148,7 @@ const ext = {
top = Math.max(0, bodyRect.height - rootRect.height - 10) top = Math.max(0, bodyRect.height - rootRect.height - 10)
} }
ctx.root.style.top = top + 'px' this.root.style.top = top + 'px'
positionList() positionList()
} }
}) })

View File

@@ -1,32 +1,32 @@
import { LiteGraph, LGraphCanvas } from '@comfyorg/litegraph' import { LiteGraph, LGraphCanvas } from '@comfyorg/litegraph'
import { app } from '../../scripts/app' import { app } from '../../scripts/app'
import { ComfyWidgets } from '../../scripts/widgets' import { ComfyWidgets } from '../../scripts/widgets'
import { LGraphNode } from '@comfyorg/litegraph'
// Node that add notes to your project // Node that add notes to your project
app.registerExtension({ app.registerExtension({
name: 'Comfy.NoteNode', name: 'Comfy.NoteNode',
registerCustomNodes() { registerCustomNodes() {
class NoteNode extends LGraphNode { class NoteNode {
static category: string static category: string
color = LGraphCanvas.node_colors.yellow.color color = LGraphCanvas.node_colors.yellow.color
bgcolor = LGraphCanvas.node_colors.yellow.bgcolor bgcolor = LGraphCanvas.node_colors.yellow.bgcolor
groupcolor = LGraphCanvas.node_colors.yellow.groupcolor groupcolor = LGraphCanvas.node_colors.yellow.groupcolor
properties: { text: string }
serialize_widgets: boolean
isVirtualNode: boolean isVirtualNode: boolean
collapsable: boolean collapsable: boolean
title_mode: number title_mode: number
constructor(title?: string) { constructor() {
super(title)
if (!this.properties) { if (!this.properties) {
this.properties = { text: '' } this.properties = { text: '' }
} }
ComfyWidgets.STRING( ComfyWidgets.STRING(
// Should we extends LGraphNode? Yesss // @ts-expect-error
// Should we extends LGraphNode?
this, this,
'', '',
// @ts-expect-error
['', { default: this.properties.text, multiline: true }], ['', { default: this.properties.text, multiline: true }],
app app
) )
@@ -40,6 +40,7 @@ app.registerExtension({
LiteGraph.registerNodeType( LiteGraph.registerNodeType(
'Note', 'Note',
// @ts-expect-error
Object.assign(NoteNode, { Object.assign(NoteNode, {
title_mode: LiteGraph.NORMAL_TITLE, title_mode: LiteGraph.NORMAL_TITLE,
title: 'Note', title: 'Note',

View File

@@ -11,12 +11,11 @@ app.registerExtension({
__outputType?: string __outputType?: string
} }
class RerouteNode extends LGraphNode { class RerouteNode {
static category: string | undefined static category: string | undefined
static defaultVisibility = false static defaultVisibility = false
constructor(title?: string) { constructor() {
super(title)
if (!this.properties) { if (!this.properties) {
this.properties = {} this.properties = {}
} }

View File

@@ -1,8 +1,8 @@
import { ComfyWidgets, addValueControlWidgets } from '../../scripts/widgets' import { ComfyWidgets, addValueControlWidgets } from '../../scripts/widgets'
import { app } from '../../scripts/app' import { app } from '../../scripts/app'
import { applyTextReplacements } from '../../scripts/utils' import { applyTextReplacements } from '../../scripts/utils'
import { LiteGraph, LGraphNode } from '@comfyorg/litegraph' import { LiteGraph } from '@comfyorg/litegraph'
import type { INodeInputSlot, IWidget } from '@comfyorg/litegraph' import type { LGraphNode, INodeInputSlot, IWidget } from '@comfyorg/litegraph'
const CONVERTED_TYPE = 'converted-widget' const CONVERTED_TYPE = 'converted-widget'
const VALID_TYPES = ['STRING', 'combo', 'number', 'toggle', 'BOOLEAN'] const VALID_TYPES = ['STRING', 'combo', 'number', 'toggle', 'BOOLEAN']
@@ -13,12 +13,11 @@ const TARGET = Symbol() // Used for reroutes to specify the real target widget
interface PrimitiveNode extends LGraphNode {} interface PrimitiveNode extends LGraphNode {}
const replacePropertyName = 'Run widget replace on values' const replacePropertyName = 'Run widget replace on values'
class PrimitiveNode extends LGraphNode { class PrimitiveNode {
controlValues: any[] controlValues: any[]
lastType: string lastType: string
static category: string static category: string
constructor(title?: string) { constructor() {
super(title)
this.addOutput('connect to widget input', '*') this.addOutput('connect to widget input', '*')
this.serialize_widgets = true this.serialize_widgets = true
this.isVirtualNode = true this.isVirtualNode = true

View File

@@ -1986,15 +1986,8 @@ export class ComfyApp {
async registerNodeDef(nodeId: string, nodeData: ComfyNodeDef) { async registerNodeDef(nodeId: string, nodeData: ComfyNodeDef) {
const self = this const self = this
const node = class ComfyNode extends LGraphNode { const node = Object.assign(
static comfyClass? = nodeData.name function ComfyNode() {
// TODO: change to "title?" once litegraph.d.ts has been updated
static title = nodeData.display_name || nodeData.name
static nodeData? = nodeData
static category?: string
constructor(title?: string) {
super(title)
var inputs = nodeData['input']['required'] var inputs = nodeData['input']['required']
if (nodeData['input']['optional'] != undefined) { if (nodeData['input']['optional'] != undefined) {
inputs = Object.assign( inputs = Object.assign(
@@ -2060,9 +2053,13 @@ export class ComfyApp {
this.serialize_widgets = true this.serialize_widgets = true
app.#invokeExtensionsAsync('nodeCreated', this) app.#invokeExtensionsAsync('nodeCreated', this)
},
{
title: nodeData.display_name || nodeData.name,
comfyClass: nodeData.name,
nodeData
} }
} )
// @ts-expect-error
node.prototype.comfyClass = nodeData.name node.prototype.comfyClass = nodeData.name
this.#addNodeContextMenuHandler(node) this.#addNodeContextMenuHandler(node)
@@ -2070,7 +2067,9 @@ export class ComfyApp {
this.#addNodeKeyHandler(node) this.#addNodeKeyHandler(node)
await this.#invokeExtensionsAsync('beforeRegisterNodeDef', node, nodeData) await this.#invokeExtensionsAsync('beforeRegisterNodeDef', node, nodeData)
// @ts-expect-error
LiteGraph.registerNodeType(nodeId, node) LiteGraph.registerNodeType(nodeId, node)
// @ts-expect-error
node.category = nodeData.category node.category = nodeData.category
} }

View File

@@ -254,6 +254,7 @@ export function addDomClippingSetting(): void {
}) })
} }
//@ts-ignore
LGraphNode.prototype.addDOMWidget = function ( LGraphNode.prototype.addDOMWidget = function (
name: string, name: string,
type: string, type: string,

View File

@@ -14,13 +14,6 @@ declare module '@comfyorg/litegraph' {
* If the node is a frontend only node and should not be serialized into the prompt. * If the node is a frontend only node and should not be serialized into the prompt.
*/ */
isVirtualNode?: boolean isVirtualNode?: boolean
addDOMWidget(
name: string,
type: string,
element: HTMLElement,
options: Record<string, any>
): DOMWidget
} }
interface IWidget<TValue = any, TOptions = any> { interface IWidget<TValue = any, TOptions = any> {
@@ -74,8 +67,4 @@ declare module '@comfyorg/litegraph' {
slotPos: Vector2 slotPos: Vector2
): number ): number
} }
interface ContextMenu {
root?: HTMLDivElement
}
} }