Update simpleStringRepository.js

Updated frontend buttons for Comfy 2.0 (via Grok)
This commit is contained in:
Igal Ogonov
2026-10-21 14:11:44 +03:00
committed by GitHub
parent f5317295b0
commit 4d66da840f
+94 -85
View File
@@ -1,94 +1,103 @@
import { app } from "../../../scripts/app.js";
import { ComfyWidgets } from "../../../scripts/widgets.js";
// Displays input text on a node
app.registerExtension({
name: "io.SimpleStringRepository",
async beforeRegisterNodeDef(nodeType, nodeData, app) {
if(nodeType?.comfyClass.startsWith('SimpleStringRepository')){
//TODO Perhaps add stuff
}
},
async nodeCreated(node, app) {
let nodeClass= node?.comfyClass;
if(nodeClass.startsWith('SimpleStringRepository')){
let isCompact = node?.comfyClass.endsWith('Compact');
name: "io.SimpleStringRepository",
let extractParsedStrings = ()=>{
let str_widget = node.widgets[0];
let str = str_widget.value;
return str.split('\n');
}
async beforeRegisterNodeDef(nodeType, nodeData, app) {
if (!nodeData?.name?.startsWith("SimpleStringRepository")) return;
//Adds numerical indexes to strings, does not destroy or change existing ones.
node.addWidget("button", "✍ Add Missing Indexes", null, () => {
let currentStrings = extractParsedStrings();
let map = {}
let orphans = {};
for(let i in currentStrings){
let index = null;
let str = currentStrings[i];
if(str.includes('.')){
index = str.split('.')[0];
str = str.split('.')[1];
}
if(!index || map[index])
orphans[i] = str;
else
map[index] = str;
}
let newIndex = 1;
for(let i in orphans){
while(map[newIndex] !== undefined)
newIndex++;
currentStrings[i] = newIndex + '.' + orphans[i];
map[newIndex] = orphans[i];
}
node.widgets[0].value = currentStrings.join('\n');
}, { serialize: false });
const isCompact = nodeData.name.includes("Compact");
//Regenerates all string indexes, potentially changing existing ones.
node.addWidget("button", "⚠ Regenerate All Indexes", null, () => {
let currentStrings = extractParsedStrings();
for(let i in currentStrings){
if(currentStrings[i].includes('.'))
currentStrings[i] = currentStrings[i].split('.')[1];
currentStrings[i] = (i-0+1) +'.'+ currentStrings[i];
}
node.widgets[0].value = currentStrings.join('\n');
}, { serialize: false });
const origOnNodeCreated = nodeType.prototype.onNodeCreated;
nodeType.prototype.onNodeCreated = function () {
const r = origOnNodeCreated?.apply(this, arguments);
//Strips all strings of their indexes
node.addWidget("button", "❌ Remove All Indexes", null, () => {
let currentStrings = extractParsedStrings();
for(let i in currentStrings){
if(currentStrings[i].includes('.'))
currentStrings[i] = currentStrings[i].split('.')[1];
}
node.widgets[0].value = currentStrings.join('\n');
}, { serialize: false });
const extractParsedStrings = () => {
const w = this.widgets?.find(w => w.name === "string") || this.widgets?.[0];
return (w?.value || "").split("\n");
};
//Resets Parameters (Target/Strength/Randomness)
node.addWidget("button", "↺ Reset Target Parameters", null, () => {
for(let i in node.widgets){
let widget = node.widgets[i];
switch(isCompact){
case true:
if(widget.name.match(/^target\d+$/))
widget.value = '0,1,-1';
break;
case false:
if(widget.name.match(/^target\d+$/))
widget.value = 0;
if(widget.name.match(/^strength\d+$/))
widget.value = 1;
if(widget.name.match(/^random\d+$/))
widget.value = -1;
break;
}
}
}, { serialize: false });
}
},
const setStringValue = (newValue) => {
const w = this.widgets?.find(w => w.name === "string") || this.widgets?.[0];
if (w) w.value = newValue;
};
// Add Missing Indexes
this.addWidget("button", "🟢 Add Missing Indexes", null, () => {
const lines = extractParsedStrings();
const map = {};
const orphans = {};
for (let i = 0; i < lines.length; i++) {
let line = lines[i];
let index = null;
let str = line;
if (line.includes(".")) {
const parts = line.split(".");
index = parts[0].trim();
str = parts.slice(1).join(".").trim();
}
if (!index || map[index] !== undefined) {
orphans[i] = str;
} else {
map[index] = str;
}
}
let newIndex = 1;
for (const i in orphans) {
while (map[newIndex] !== undefined) newIndex++;
lines[i] = newIndex + "." + orphans[i];
map[newIndex] = orphans[i];
newIndex++;
}
setStringValue(lines.join("\n"));
}, { serialize: false });
// Regenerate All Indexes
this.addWidget("button", "🟡 Regenerate All Indexes", null, () => {
const lines = extractParsedStrings();
for (let i = 0; i < lines.length; i++) {
let line = lines[i];
if (line.includes(".")) {
line = line.split(".").slice(1).join(".").trim();
}
lines[i] = (i + 1) + "." + line;
}
setStringValue(lines.join("\n"));
}, { serialize: false });
// Remove All Indexes
this.addWidget("button", "🔴 Remove All Indexes", null, () => {
const lines = extractParsedStrings();
for (let i = 0; i < lines.length; i++) {
let line = lines[i];
if (line.includes(".")) {
line = line.split(".").slice(1).join(".").trim();
}
lines[i] = line;
}
setStringValue(lines.join("\n"));
}, { serialize: false });
// Reset Target Parameters
this.addWidget("button", "🗘 Reset Target Parameters", null, () => {
if (!this.widgets) return;
for (const widget of this.widgets) {
const name = widget.name || "";
if (isCompact) {
if (/^target\d+$/.test(name)) widget.value = "0,1,-1";
} else {
if (/^target\d+$/.test(name)) widget.value = 0;
if (/^strength\d+$/.test(name)) widget.value = 1;
if (/^random\d+$/.test(name)) widget.value = -1;
}
}
}, { serialize: false });
return r;
};
},
});