104 lines
4.0 KiB
JavaScript
104 lines
4.0 KiB
JavaScript
import { app } from "../../../scripts/app.js";
|
|
|
|
app.registerExtension({
|
|
name: "io.SimpleStringRepository",
|
|
|
|
async beforeRegisterNodeDef(nodeType, nodeData, app) {
|
|
if (!nodeData?.name?.startsWith("SimpleStringRepository")) return;
|
|
|
|
const isCompact = nodeData.name.includes("Compact");
|
|
|
|
const origOnNodeCreated = nodeType.prototype.onNodeCreated;
|
|
nodeType.prototype.onNodeCreated = function () {
|
|
const r = origOnNodeCreated?.apply(this, arguments);
|
|
|
|
const extractParsedStrings = () => {
|
|
const w = this.widgets?.find(w => w.name === "string") || this.widgets?.[0];
|
|
return (w?.value || "").split("\n");
|
|
};
|
|
|
|
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;
|
|
};
|
|
},
|
|
});
|