34 lines
1.2 KiB
JavaScript
34 lines
1.2 KiB
JavaScript
|
|
(function initCanvasInputs(root) {
|
||
|
|
function uniqueNonEmpty(values) {
|
||
|
|
const seen = new Set();
|
||
|
|
return (Array.isArray(values) ? values : []).filter((value) => {
|
||
|
|
const normalized = typeof value === "string" ? value.trim() : "";
|
||
|
|
if (!normalized || seen.has(normalized)) return false;
|
||
|
|
seen.add(normalized);
|
||
|
|
return true;
|
||
|
|
});
|
||
|
|
}
|
||
|
|
|
||
|
|
function collectUpstreamOutputs(node, readNodeOutput) {
|
||
|
|
const out = { text: "", image: "", images: [] };
|
||
|
|
if (!node || !node.inputs || typeof readNodeOutput !== "function") return out;
|
||
|
|
|
||
|
|
for (const key of Object.keys(node.inputs)) {
|
||
|
|
const connections = (node.inputs[key] && node.inputs[key].connections) || [];
|
||
|
|
for (const connection of connections) {
|
||
|
|
const source = readNodeOutput(connection.node) || {};
|
||
|
|
if (source.text && !out.text) out.text = String(source.text).trim();
|
||
|
|
if (source.image) out.images.push(source.image);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
out.images = uniqueNonEmpty(out.images);
|
||
|
|
out.image = out.images[0] || "";
|
||
|
|
return out;
|
||
|
|
}
|
||
|
|
|
||
|
|
const api = { uniqueNonEmpty, collectUpstreamOutputs };
|
||
|
|
if (typeof module !== "undefined" && module.exports) module.exports = api;
|
||
|
|
root.CanvasInputs = api;
|
||
|
|
})(typeof window !== "undefined" ? window : globalThis);
|