Toggle nodes/containers on and off by tags
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
import { LiteGraph, type ContextMenuItem, type LGraphNode, type Vector2, LConnectionKind, LLink, LGraphCanvas, type SlotType, TitleMode, type SlotLayout, BuiltInSlotType, type ITextWidget, type SerializedLGraphNode } from "@litegraph-ts/core";
|
||||
import { LiteGraph, type ContextMenuItem, type LGraphNode, type Vector2, LConnectionKind, LLink, LGraphCanvas, type SlotType, TitleMode, type SlotLayout, BuiltInSlotType, type ITextWidget, type SerializedLGraphNode, NodeMode, type IToggleWidget } from "@litegraph-ts/core";
|
||||
import ComfyGraphNode from "./ComfyGraphNode";
|
||||
import { Watch } from "@litegraph-ts/nodes-basic";
|
||||
import type { SerializedPrompt } from "$lib/components/ComfyApp";
|
||||
@@ -7,6 +7,7 @@ import type { GalleryOutput } from "./ComfyWidgetNodes";
|
||||
import { get } from "svelte/store";
|
||||
import queueState from "$lib/stores/queueState";
|
||||
import notify from "$lib/notify";
|
||||
import layoutState from "$lib/stores/layoutState";
|
||||
|
||||
export interface ComfyQueueEventsProperties extends Record<any, any> {
|
||||
}
|
||||
@@ -251,3 +252,86 @@ LiteGraph.registerNodeType({
|
||||
desc: "Runs a part of the graph based on a tag",
|
||||
type: "actions/execute_subgraph"
|
||||
})
|
||||
|
||||
export interface ComfySetNodeModeActionProperties extends Record<any, any> {
|
||||
targetTags: string,
|
||||
enable: boolean,
|
||||
}
|
||||
|
||||
export class ComfySetNodeModeAction extends ComfyGraphNode {
|
||||
override properties: ComfySetNodeModeActionProperties = {
|
||||
targetTags: "",
|
||||
enable: false
|
||||
}
|
||||
|
||||
static slotLayout: SlotLayout = {
|
||||
inputs: [
|
||||
{ name: "enabled", type: "boolean" },
|
||||
{ name: "set", type: BuiltInSlotType.ACTION },
|
||||
],
|
||||
}
|
||||
|
||||
displayWidget: ITextWidget;
|
||||
enableWidget: IToggleWidget;
|
||||
|
||||
constructor(title?: string) {
|
||||
super(title)
|
||||
this.displayWidget = this.addWidget("text", "Tags", this.properties.targetTags, "targetTags")
|
||||
this.enableWidget = this.addWidget("toggle", "Enable", this.properties.enable, "enable")
|
||||
}
|
||||
|
||||
override onPropertyChanged(property: any, value: any) {
|
||||
if (property === "enabled") {
|
||||
this.enableWidget.value = value
|
||||
}
|
||||
}
|
||||
|
||||
override onExecute() {
|
||||
const enabled = this.getInputData(0)
|
||||
if (typeof enabled === "boolean")
|
||||
this.setProperty("enabled", enabled)
|
||||
}
|
||||
|
||||
override onAction(action: any, param: any) {
|
||||
let enabled = this.properties.enabled
|
||||
if (typeof param === "object" && "enabled" in param)
|
||||
enabled = param["enabled"]
|
||||
|
||||
const tags = this.properties.targetTags.split(",").map(s => s.trim());
|
||||
|
||||
for (const node of this.graph._nodes) {
|
||||
if ("tags" in node.properties) {
|
||||
const comfyNode = node as ComfyGraphNode;
|
||||
const hasTag = tags.some(t => comfyNode.properties.tags.indexOf(t) != -1);
|
||||
if (hasTag) {
|
||||
let newMode: NodeMode;
|
||||
if (enabled) {
|
||||
newMode = NodeMode.ALWAYS;
|
||||
} else {
|
||||
newMode = NodeMode.NEVER;
|
||||
}
|
||||
node.changeMode(newMode);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for (const entry of Object.values(get(layoutState).allItems)) {
|
||||
if (entry.dragItem.type === "container") {
|
||||
const container = entry.dragItem;
|
||||
const hasTag = tags.some(t => container.attrs.tags.indexOf(t) != -1);
|
||||
if (hasTag) {
|
||||
container.attrs.hidden = !enabled;
|
||||
console.warn("Cont", container.attrs.tags, tags, hasTag, container.attrs, enabled)
|
||||
}
|
||||
container.attrsChanged.set(get(container.attrsChanged) + 1)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
LiteGraph.registerNodeType({
|
||||
class: ComfySetNodeModeAction,
|
||||
title: "Comfy.SetNodeModeAction",
|
||||
desc: "Sets a group of nodes/UI containers as enabled/disabled based on their tags (comma-separated)",
|
||||
type: "actions/set_node_mode"
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user