Skip to content

Commit

Permalink
Revert "Add support for LiteGraph to convert to classes (#334)" (#386)
Browse files Browse the repository at this point in the history
This reverts commit e2141a8.
  • Loading branch information
huchenlei authored Aug 12, 2024
1 parent d9df032 commit d607f6c
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 49 deletions.
26 changes: 13 additions & 13 deletions src/extensions/core/contextMenuFilter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,19 @@ const ext = {
init() {
const ctxMenu = LiteGraph.ContextMenu
// @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) {
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 (options?.className === 'dark' && values?.length > 4) {
if (options?.className === 'dark' && values?.length > 10) {
const filter = document.createElement('input')
filter.classList.add('comfy-context-menu-filter')
filter.placeholder = 'Filter list'
ctx.root.prepend(filter)
this.root.prepend(filter)

const items = Array.from(
ctx.root.querySelectorAll('.litemenu-entry')
this.root.querySelectorAll('.litemenu-entry')
) as HTMLElement[]
let displayedItems = [...items]
let itemCount = displayedItems.length
Expand Down Expand Up @@ -61,16 +61,16 @@ const ext = {
}

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 (rect.top < 0) {
const scale =
1 -
ctx.root.getBoundingClientRect().height /
ctx.root.clientHeight
const shift = (ctx.root.clientHeight * scale) / 2
ctx.root.style.top = -shift + 'px'
this.root.getBoundingClientRect().height /
this.root.clientHeight
const shift = (this.root.clientHeight * scale) / 2
this.root.style.top = -shift + 'px'
}
}

Expand Down Expand Up @@ -109,7 +109,7 @@ const ext = {
selectedItem?.click()
break
case 'Escape':
ctx.close()
this.close()
break
}
})
Expand Down Expand Up @@ -140,15 +140,15 @@ const ext = {
let top = options.event.clientY - 10

const bodyRect = document.body.getBoundingClientRect()
const rootRect = ctx.root.getBoundingClientRect()
const rootRect = this.root.getBoundingClientRect()
if (
bodyRect.height &&
top > 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()
}
})
Expand Down
13 changes: 7 additions & 6 deletions src/extensions/core/noteNode.ts
Original file line number Diff line number Diff line change
@@ -1,32 +1,32 @@
import { LiteGraph, LGraphCanvas } from '@comfyorg/litegraph'
import { app } from '../../scripts/app'
import { ComfyWidgets } from '../../scripts/widgets'
import { LGraphNode } from '@comfyorg/litegraph'
// Node that add notes to your project

app.registerExtension({
name: 'Comfy.NoteNode',
registerCustomNodes() {
class NoteNode extends LGraphNode {
class NoteNode {
static category: string

color = LGraphCanvas.node_colors.yellow.color
bgcolor = LGraphCanvas.node_colors.yellow.bgcolor
groupcolor = LGraphCanvas.node_colors.yellow.groupcolor
properties: { text: string }
serialize_widgets: boolean
isVirtualNode: boolean
collapsable: boolean
title_mode: number

constructor(title?: string) {
super(title)
constructor() {
if (!this.properties) {
this.properties = { text: '' }
}
ComfyWidgets.STRING(
// Should we extends LGraphNode? Yesss
// @ts-expect-error
// Should we extends LGraphNode?
this,
'',
// @ts-expect-error
['', { default: this.properties.text, multiline: true }],
app
)
Expand All @@ -40,6 +40,7 @@ app.registerExtension({

LiteGraph.registerNodeType(
'Note',
// @ts-expect-error
Object.assign(NoteNode, {
title_mode: LiteGraph.NORMAL_TITLE,
title: 'Note',
Expand Down
5 changes: 2 additions & 3 deletions src/extensions/core/rerouteNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,11 @@ app.registerExtension({
__outputType?: string
}

class RerouteNode extends LGraphNode {
class RerouteNode {
static category: string | undefined
static defaultVisibility = false

constructor(title?: string) {
super(title)
constructor() {
if (!this.properties) {
this.properties = {}
}
Expand Down
9 changes: 4 additions & 5 deletions src/extensions/core/widgetInputs.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { ComfyWidgets, addValueControlWidgets } from '../../scripts/widgets'
import { app } from '../../scripts/app'
import { applyTextReplacements } from '../../scripts/utils'
import { LiteGraph, LGraphNode } from '@comfyorg/litegraph'
import type { INodeInputSlot, IWidget } from '@comfyorg/litegraph'
import { LiteGraph } from '@comfyorg/litegraph'
import type { LGraphNode, INodeInputSlot, IWidget } from '@comfyorg/litegraph'

const CONVERTED_TYPE = 'converted-widget'
const VALID_TYPES = ['STRING', 'combo', 'number', 'toggle', 'BOOLEAN']
Expand All @@ -13,12 +13,11 @@ const TARGET = Symbol() // Used for reroutes to specify the real target widget
interface PrimitiveNode extends LGraphNode {}

const replacePropertyName = 'Run widget replace on values'
class PrimitiveNode extends LGraphNode {
class PrimitiveNode {
controlValues: any[]
lastType: string
static category: string
constructor(title?: string) {
super(title)
constructor() {
this.addOutput('connect to widget input', '*')
this.serialize_widgets = true
this.isVirtualNode = true
Expand Down
21 changes: 10 additions & 11 deletions src/scripts/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1986,15 +1986,8 @@ export class ComfyApp {

async registerNodeDef(nodeId: string, nodeData: ComfyNodeDef) {
const self = this
const node = class ComfyNode extends LGraphNode {
static comfyClass? = nodeData.name
// 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)
const node = Object.assign(
function ComfyNode() {
var inputs = nodeData['input']['required']
if (nodeData['input']['optional'] != undefined) {
inputs = Object.assign(
Expand Down Expand Up @@ -2060,17 +2053,23 @@ export class ComfyApp {
this.serialize_widgets = true

app.#invokeExtensionsAsync('nodeCreated', this)
},
{
title: nodeData.display_name || nodeData.name,
comfyClass: nodeData.name,
nodeData
}
}
// @ts-expect-error
)
node.prototype.comfyClass = nodeData.name

this.#addNodeContextMenuHandler(node)
this.#addDrawBackgroundHandler(node)
this.#addNodeKeyHandler(node)

await this.#invokeExtensionsAsync('beforeRegisterNodeDef', node, nodeData)
// @ts-expect-error
LiteGraph.registerNodeType(nodeId, node)
// @ts-expect-error
node.category = nodeData.category
}

Expand Down
1 change: 1 addition & 0 deletions src/scripts/domWidget.ts
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,7 @@ export function addDomClippingSetting(): void {
})
}

//@ts-ignore
LGraphNode.prototype.addDOMWidget = function (
name: string,
type: string,
Expand Down
11 changes: 0 additions & 11 deletions src/types/litegraph-augmentation.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,6 @@ declare module '@comfyorg/litegraph' {
* If the node is a frontend only node and should not be serialized into the prompt.
*/
isVirtualNode?: boolean

addDOMWidget(
name: string,
type: string,
element: HTMLElement,
options: Record<string, any>
): DOMWidget
}

interface IWidget<TValue = any, TOptions = any> {
Expand Down Expand Up @@ -74,8 +67,4 @@ declare module '@comfyorg/litegraph' {
slotPos: Vector2
): number
}

interface ContextMenu {
root?: HTMLDivElement
}
}

0 comments on commit d607f6c

Please sign in to comment.