Better subgraph cut/paste handling & tests

This commit is contained in:
space-nuko
2023-05-17 10:18:49 -05:00
parent 68895443f7
commit 51d8e17f49
7 changed files with 147 additions and 9 deletions

View File

@@ -1,4 +1,4 @@
import { LConnectionKind, LGraph, LGraphNode, type INodeSlot, type SlotIndex, LiteGraph, getStaticProperty, type LGraphAddNodeOptions, LGraphCanvas, type LGraphRemoveNodeOptions } from "@litegraph-ts/core";
import { LConnectionKind, LGraph, LGraphNode, type INodeSlot, type SlotIndex, LiteGraph, getStaticProperty, type LGraphAddNodeOptions, LGraphCanvas, type LGraphRemoveNodeOptions, Subgraph, type LGraphAddNodeMode } from "@litegraph-ts/core";
import GraphSync from "./GraphSync";
import EventEmitter from "events";
import type TypedEmitter from "typed-emitter";
@@ -41,8 +41,9 @@ export default class ComfyGraph extends LGraph {
}
override onNodeAdded(node: LGraphNode, options: LGraphAddNodeOptions) {
if (options.subgraphs && options.subgraphs.length > 0)
return
// Don't add detached subgraphs
if (node.getRootGraph() == null || this._is_subgraph)
return;
layoutState.nodeAdded(node, options)
@@ -112,7 +113,19 @@ export default class ComfyGraph extends LGraph {
const dragItemIDs = widgetNodesAdded.map(wn => get(layoutState).allItemsByNode[wn.id]?.dragItem?.id).filter(Boolean)
console.debug("[ComfyGraph] Group new widgets", dragItemIDs)
layoutState.groupItems(dragItemIDs, { title: node.title })
// Use the default node title instead of custom node title, in
// case node was cloned
const reg = LiteGraph.registered_node_types[node.type]
layoutState.groupItems(dragItemIDs, { title: reg.title })
}
}
// Handle subgraphs being attached
if (node.is(Subgraph)) {
console.error("ISSUBGRAPH")
for (const child of node.subgraph.iterateNodesInOrder()) {
this.onNodeAdded(child, options)
}
}
@@ -123,6 +136,13 @@ export default class ComfyGraph extends LGraph {
override onNodeRemoved(node: LGraphNode, options: LGraphRemoveNodeOptions) {
layoutState.nodeRemoved(node, options);
// Handle subgraphs being removed
if (node.is(Subgraph)) {
for (const child of node.subgraph.iterateNodesInOrder()) {
this.onNodeRemoved(child, options)
}
}
// console.debug("Removed", node);
this.eventBus.emit("nodeRemoved", node);
}

View File

@@ -291,7 +291,7 @@ const setNodeTitle = (arg: IDragItem, value: any) => {
if (reg == null)
return
if (value)
if (value && value !== reg.title)
widget.node.title = `${reg.title} (${value})`
else
widget.node.title = reg.title
@@ -699,6 +699,20 @@ const store: Writable<LayoutState> = writable({
}
})
function clear() {
store.set({
root: null,
allItems: {},
allItemsByNode: {},
isMenuOpen: false,
isConfiguring: true,
refreshPropsPanel: writable(0),
attrs: {
...defaultWorkflowAttributes
}
})
}
function findDefaultContainerForInsertion(): ContainerLayout | null {
const state = get(store);
@@ -741,6 +755,7 @@ function addContainer(parent: ContainerLayout | null, attrs: Partial<Attributes>
...attrs
}
}
const entry: DragItemEntry = { dragItem, children: [], parent: null };
if (state.allItemsByNode[dragItem.id] != null)
@@ -750,6 +765,7 @@ function addContainer(parent: ContainerLayout | null, attrs: Partial<Attributes>
if (parent) {
moveItem(dragItem, parent, index)
}
console.debug("[layoutState] addContainer", state)
store.set(state)
runOnChangedForWidgetDefaults(dragItem)
@@ -758,7 +774,7 @@ function addContainer(parent: ContainerLayout | null, attrs: Partial<Attributes>
function addWidget(parent: ContainerLayout, node: ComfyWidgetNode, attrs: Partial<Attributes> = {}, index?: number): WidgetLayout {
const state = get(store);
const widgetName = "Widget"
const widgetName = node.title || "Widget"
const dragItem: WidgetLayout = {
type: "widget",
id: uuidv4(),
@@ -771,7 +787,7 @@ function addWidget(parent: ContainerLayout, node: ComfyWidgetNode, attrs: Partia
...attrs
}
}
const parentEntry = state.allItems[parent.id]
const entry: DragItemEntry = { dragItem, children: [], parent: null };
if (state.allItems[dragItem.id] != null)
@@ -821,6 +837,7 @@ function removeEntry(state: LayoutState, id: DragItemID) {
function nodeAdded(node: LGraphNode, options: LGraphAddNodeOptions) {
const state = get(store)
console.error("NODEADDED", node.type, (node as any).svelteComponentType)
if (state.isConfiguring)
return;
@@ -834,7 +851,7 @@ function nodeAdded(node: LGraphNode, options: LGraphAddNodeOptions) {
const parent = findDefaultContainerForInsertion();
console.debug("[layoutState] nodeAdded", node)
console.debug("[layoutState] nodeAdded", node.id)
if ("svelteComponentType" in node) {
addWidget(parent, node as ComfyWidgetNode);
}

View File

@@ -8,6 +8,9 @@ export function isNodeDisabled(node: LGraphNode): boolean {
if (node.mode !== NodeMode.ALWAYS) {
return true;
}
if (node.graph == null) {
return true
}
node = node.graph._subgraph_node;
}
return false;