Files
2026-08-28 01:01:12 +02:00

83 lines
2.9 KiB
JavaScript

import { api } from '../../../scripts/api.js';
function shortInputName(name) {
const text = String(name || '');
return text.slice(text.lastIndexOf('.') + 1);
}
function inputByName(node, name) {
return (node?.inputs || []).find((input) => shortInputName(input?.name) === name) || null;
}
function sourceNodeFor(node, inputName) {
const input = inputByName(node, inputName);
if (input?.link == null) return null;
const graph = node?.graph;
const link = graph?.links?.[input.link] || graph?.links?.[String(input.link)];
if (!link) return null;
return graph?.getNodeById?.(link.origin_id)
|| graph?._nodes_by_id?.[link.origin_id]
|| graph?._nodes?.find?.((item) => item.id === link.origin_id)
|| null;
}
function widgetValue(node, name) {
const widget = (node?.widgets || []).find((item) => item?.name === name);
if (widget?.value != null && widget.value !== '') return widget.value;
return node?.widgets_values_named?.[name] ?? null;
}
function splitPath(path) {
const text = String(path || '').replace(/^\/+/, '').replace(/\s+\[(?:input|output|temp)\]$/, '');
const index = text.lastIndexOf('/');
return index === -1
? { subfolder: '', filename: text }
: { subfolder: text.slice(0, index), filename: text.slice(index + 1) };
}
export function buildViewUrl(path, type = 'input', explicitSubfolder = '') {
if (!path) return '';
const split = splitPath(path);
const query = new URLSearchParams({ filename: split.filename, type });
const subfolder = explicitSubfolder || split.subfolder;
if (subfolder) query.set('subfolder', subfolder);
return api.apiURL(`/view?${query.toString()}`);
}
function normalizePreview(entry) {
if (!entry) return '';
if (typeof entry === 'string') {
if (/^(blob:|data:|https?:|\/)/.test(entry)) return entry;
return buildViewUrl(entry);
}
if (typeof entry === 'object') {
if (entry.currentSrc) return String(entry.currentSrc);
if (entry.src) return String(entry.src);
if (entry.url) return String(entry.url);
if (entry.filename) return buildViewUrl(entry.filename, entry.type || 'input', entry.subfolder || '');
}
return '';
}
export function hasInputConnection(node, inputName) {
return inputByName(node, inputName)?.link != null;
}
export function resolveInputPreviewUrls(node, inputName) {
const source = sourceNodeFor(node, inputName);
if (!source) return [];
const entries = Array.isArray(source.imgs) ? source.imgs : Array.isArray(source.images) ? source.images : [];
const previews = entries.map(normalizePreview).filter(Boolean);
if (previews.length) return previews;
if (String(source.comfyClass || '') === 'LoadImage') {
const path = widgetValue(source, 'image');
return path ? [buildViewUrl(path)] : [];
}
return [];
}
export function resolveGuideSlotPreviewUrl(node, slot) {
if (!(Number(slot) >= 0)) return '';
return resolveInputPreviewUrls(node, `guide_image_${slot}`)[0] || '';
}