Skip to content

Commit

Permalink
Group filtering (initial)
Browse files Browse the repository at this point in the history
  • Loading branch information
DrJKL committed Apr 19, 2024
1 parent 3097ac9 commit 29d8483
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 8 deletions.
21 changes: 19 additions & 2 deletions src_web/comfyui/fast_node_muter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
} from "typings/litegraph.js";
import { RgthreeToggleNavWidget } from "./utils_widgets.js";
import { filterByColor, filterByTitle, sortBy, type SortType } from "./utils_fast.js";
import { SERVICE as FAST_GROUPS_SERVICE } from "./fast_groups_service.js";

declare const LiteGraph: typeof TLiteGraph;
declare const LGraphCanvas: typeof TLGraphCanvas;
Expand All @@ -19,6 +20,7 @@ const PROPERTY_SORT = "sort";
const PROPERTY_SORT_CUSTOM_ALPHA = "customSortAlphabet";
const PROPERTY_MATCH_COLORS = "matchColors";
const PROPERTY_MATCH_TITLE = "matchTitle";
const PROPERTY_MATCH_TITLE_GROUP = "matchGroupTitle";
const PROPERTY_RESTRICTION = "toggleRestriction";

/**
Expand All @@ -31,19 +33,34 @@ export class FastNodeMuter extends FastGroupsMuter {
override readonly modeOn = LiteGraph.ALWAYS;
override readonly modeOff = LiteGraph.NEVER;

static "@matchTitleGroup" = { type: "string" };

constructor(title = FastNodeMuter.title) {
super(title);
this.properties[PROPERTY_MATCH_TITLE_GROUP] = "";
}

static override setUp<T extends RgthreeBaseNode>(clazz: new (title?: string) => T) {
LiteGraph.registerNodeType((clazz as any).type, clazz);
(clazz as any).category = (clazz as any)._category;
}

private getNodes(sort: SortType): LGraphNode[] {
const matchGroup = this.properties?.[PROPERTY_MATCH_TITLE_GROUP];
if (!matchGroup) {
const graph: LGraph = app.graph;
return [...(graph._nodes ?? [])].filter((n) => !n.isVirtualNode);
}
const pattern = new RegExp(matchGroup, "i");
const allGroups = FAST_GROUPS_SERVICE.getGroups(sort);
const filteredGroups = allGroups.filter((group) => pattern.exec(group.title));
return Array.from(new Set(filteredGroups.map((group) => group._nodes).flat()));
}

override refreshWidgets() {
const graph: LGraph = app.graph;
let sort: SortType = this.properties?.[PROPERTY_SORT] || "position";
const nodes: LGraphNode[] = [...(graph._nodes ?? [])].filter((n) => !n.isVirtualNode);
const nodes = this.getNodes(sort);

// The service will return pre-sorted groups for alphanumeric and position. If this node has a
// custom sort, then we need to sort it manually.
const alphaSorted = sortBy(nodes, {
Expand Down
27 changes: 21 additions & 6 deletions web/comfyui/fast_node_muter.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,36 +3,50 @@ import { NodeTypesString } from "./constants.js";
import { FastGroupsMuter } from "./fast_groups_muter.js";
import { RgthreeToggleNavWidget } from "./utils_widgets.js";
import { filterByColor, filterByTitle, sortBy } from "./utils_fast.js";
import { SERVICE as FAST_GROUPS_SERVICE } from "./fast_groups_service.js";
const PROPERTY_SORT = "sort";
const PROPERTY_SORT_CUSTOM_ALPHA = "customSortAlphabet";
const PROPERTY_MATCH_COLORS = "matchColors";
const PROPERTY_MATCH_TITLE = "matchTitle";
const PROPERTY_MATCH_TITLE_GROUP = "matchGroupTitle";
const PROPERTY_RESTRICTION = "toggleRestriction";
export class FastNodeMuter extends FastGroupsMuter {
constructor(title = FastNodeMuter.title) {
super(title);
this.modeOn = LiteGraph.ALWAYS;
this.modeOff = LiteGraph.NEVER;
this.properties[PROPERTY_MATCH_TITLE_GROUP] = "";
}
static setUp(clazz) {
LiteGraph.registerNodeType(clazz.type, clazz);
clazz.category = clazz._category;
}
getNodes(sort) {
var _a, _b;
const matchGroup = (_a = this.properties) === null || _a === void 0 ? void 0 : _a[PROPERTY_MATCH_TITLE_GROUP];
if (!matchGroup) {
const graph = app.graph;
return [...((_b = graph._nodes) !== null && _b !== void 0 ? _b : [])].filter((n) => !n.isVirtualNode);
}
const pattern = new RegExp(matchGroup, "i");
const allGroups = FAST_GROUPS_SERVICE.getGroups(sort);
const filteredGroups = allGroups.filter((group) => pattern.exec(group.title));
return Array.from(new Set(filteredGroups.map((group) => group._nodes).flat()));
}
refreshWidgets() {
var _a, _b, _c, _d, _e, _f, _g;
const graph = app.graph;
var _a, _b, _c, _d, _e, _f;
let sort = ((_a = this.properties) === null || _a === void 0 ? void 0 : _a[PROPERTY_SORT]) || "position";
const nodes = [...((_b = graph._nodes) !== null && _b !== void 0 ? _b : [])].filter((n) => !n.isVirtualNode);
const nodes = this.getNodes(sort);
const alphaSorted = sortBy(nodes, {
customAlphabet: (_d = (_c = this.properties) === null || _c === void 0 ? void 0 : _c[PROPERTY_SORT_CUSTOM_ALPHA]) === null || _d === void 0 ? void 0 : _d.replace(/\n/g, ""),
customAlphabet: (_c = (_b = this.properties) === null || _b === void 0 ? void 0 : _b[PROPERTY_SORT_CUSTOM_ALPHA]) === null || _c === void 0 ? void 0 : _c.replace(/\n/g, ""),
sort,
});
const colorFiltered = filterByColor(alphaSorted, {
matchColors: (_e = this.properties) === null || _e === void 0 ? void 0 : _e[PROPERTY_MATCH_COLORS],
matchColors: (_d = this.properties) === null || _d === void 0 ? void 0 : _d[PROPERTY_MATCH_COLORS],
nodeColorOption: "color",
});
const titleFiltered = filterByTitle(colorFiltered, {
matchTitle: (_g = (_f = this.properties) === null || _f === void 0 ? void 0 : _f[PROPERTY_MATCH_TITLE]) === null || _g === void 0 ? void 0 : _g.trim(),
matchTitle: (_f = (_e = this.properties) === null || _e === void 0 ? void 0 : _e[PROPERTY_MATCH_TITLE]) === null || _f === void 0 ? void 0 : _f.trim(),
});
let index = 0;
for (const node of titleFiltered) {
Expand Down Expand Up @@ -90,6 +104,7 @@ export class FastNodeMuter extends FastGroupsMuter {
}
FastNodeMuter.type = NodeTypesString.FAST_NODE_MUTER;
FastNodeMuter.title = NodeTypesString.FAST_NODE_MUTER;
FastNodeMuter["@matchTitleGroup"] = { type: "string" };
app.registerExtension({
name: "rgthree.FastNodeMuter",
registerCustomNodes() {
Expand Down

0 comments on commit 29d8483

Please sign in to comment.