Files
DumasNodes/js/visual_refmod_picker.js
T
chris.dumas c9eb575e2f Squashed 'vendor/dumas_h3_refmod_picker/' content from commit c732f8b
git-subtree-dir: vendor/dumas_h3_refmod_picker
git-subtree-split: c732f8b8893bf4f00c6974c9b9941c763496fe48
2026-10-20 13:43:48 +00:00

1230 lines
48 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import { app } from "../../scripts/app.js";
import { api } from "../../scripts/api.js";
const PLACEHOLDER_IMAGE_PATH = new URL("./placeholder.png", import.meta.url).href;
const DEFAULT_PICKER_NODE_SIZE = [240, 480];
const MIN_PICKER_NODE_WIDTH = 200;
const PREVIEW_PANEL_OVERLAP_PX = 8;
function isTypingTarget(target) {
if (!target) return false;
const tag = String(target.tagName || "").toUpperCase();
return tag === "INPUT" || tag === "TEXTAREA" || tag === "SELECT" || !!target.isContentEditable;
}
let _vrpHoveredNode = null;
let _vrpArrowListenerInstalled = false;
function isVrpNodeInGraph(node) {
if (!node || !app.graph?._nodes) return false;
return app.graph._nodes.includes(node);
}
function getActiveVisualRefModPickerNode() {
if (_vrpHoveredNode && !_vrpHoveredNode.flags?.collapsed && isVrpNodeInGraph(_vrpHoveredNode)) {
return _vrpHoveredNode;
}
if (_vrpHoveredNode && !isVrpNodeInGraph(_vrpHoveredNode)) {
_vrpHoveredNode = null;
}
return null;
}
function installVrpArrowNavigation() {
if (_vrpArrowListenerInstalled) return;
_vrpArrowListenerInstalled = true;
window.addEventListener("keydown", async (event) => {
if (event.defaultPrevented) return;
if (event.key !== "ArrowLeft" && event.key !== "ArrowRight") return;
if (event.ctrlKey || event.altKey || event.metaKey || event.shiftKey) return;
if (isTypingTarget(event.target)) return;
const node = getActiveVisualRefModPickerNode();
if (!node || typeof node._vrpStepModByDelta !== "function") return;
const direction = event.key === "ArrowRight" ? 1 : -1;
const handled = await node._vrpStepModByDelta(direction);
if (!handled) return;
event.preventDefault();
event.stopPropagation();
}, true);
}
function hideWidget(widget) {
if (!widget) return;
widget.hidden = true;
widget.computeSize = () => [0, -4];
if (widget.inputEl) widget.inputEl.style.display = "none";
}
function setDynamicWidgetVisible(widget, visible) {
if (!widget) return;
if (!widget._vrpVisibilityWrapped) {
widget._vrpVisibilityWrapped = true;
widget._vrpBaseComputeSize = widget.computeSize;
widget.computeSize = function (...args) {
if (widget._vrpHidden) {
return [0, -4];
}
if (typeof widget._vrpBaseComputeSize === "function") {
return widget._vrpBaseComputeSize.apply(this, args);
}
return [MIN_PICKER_NODE_WIDTH, Number(LiteGraph?.NODE_WIDGET_HEIGHT || 24)];
};
}
if (typeof widget._vrpOriginalDisplay === "undefined" && widget.inputEl) {
widget._vrpOriginalDisplay = widget.inputEl.style.display;
}
widget._vrpHidden = !visible;
widget.hidden = !visible;
if (widget.inputEl) {
widget.inputEl.style.display = visible ? (widget._vrpOriginalDisplay || "") : "none";
}
}
function refreshPickerLayout(node) {
if (Array.isArray(node.size) && node.size[0] < MIN_PICKER_NODE_WIDTH) {
node.size[0] = MIN_PICKER_NODE_WIDTH;
}
node.setDirtyCanvas(true, true);
app.graph?.setDirtyCanvas(true, true);
}
function estimatePreviewHeight(node, domWidget) {
const nodeHeight = Math.max(Number(node?.size?.[1]) || DEFAULT_PICKER_NODE_SIZE[1], 320);
const titleHeight = Number(LiteGraph?.NODE_TITLE_HEIGHT || 30);
const topBottomPadding = 16;
const widgets = Array.isArray(node?.widgets) ? node.widgets : [];
let occupied = 0;
for (const widget of widgets) {
if (!widget || widget === domWidget) continue;
const size = typeof widget.computeSize === "function"
? widget.computeSize(node.size?.[0] || MIN_PICKER_NODE_WIDTH)
: null;
const height = Array.isArray(size) ? Number(size[1]) || 0 : 0;
if (height > 0) {
occupied += height;
}
}
return Math.max(120, nodeHeight - titleHeight - topBottomPadding - occupied);
}
function basenameForDisplay(value) {
const normalized = String(value || "").replace(/\\/g, "/");
const i = normalized.lastIndexOf("/");
return i >= 0 ? normalized.substring(i + 1) : normalized;
}
function entryDisplayName(entry) {
return String(entry?.name || basenameForDisplay(entry?.path || "") || "");
}
function dirnameForPath(value) {
const normalized = String(value || "").replace(/\\/g, "/").replace(/\/+$/, "");
const i = normalized.lastIndexOf("/");
return i > 0 ? normalized.substring(0, i) : "";
}
function normalizeSelectedModPath(value) {
const path = String(value || "").trim();
return path && path.toLowerCase() !== "(none)" ? path : "";
}
function normalizeFsPath(value) {
return String(value || "").trim().replace(/\\/g, "/").replace(/\/+/g, "/");
}
function isAbsoluteFsPath(value) {
return /^(?:[a-zA-Z]:\/|\/)/.test(normalizeFsPath(value));
}
function relativePathFromRoot(path, root) {
const normalizedPath = normalizeFsPath(path);
const normalizedRoot = normalizeFsPath(root).replace(/\/+$/, "");
if (!normalizedPath) return "";
if (!normalizedRoot || !isAbsoluteFsPath(normalizedPath)) return normalizedPath;
const lowerPath = normalizedPath.toLowerCase();
const lowerRoot = normalizedRoot.toLowerCase();
if (lowerPath === lowerRoot) return "";
if (lowerPath.startsWith(`${lowerRoot}/`)) {
return normalizedPath.substring(normalizedRoot.length + 1);
}
return normalizedPath;
}
function displayPathFromRoot(path, root) {
const relativePath = relativePathFromRoot(path, root);
return relativePath || "/";
}
function resolvePathFromRoot(path, root) {
const normalizedPath = normalizeFsPath(path);
const normalizedRoot = normalizeFsPath(root).replace(/\/+$/, "");
if (!normalizedPath) return "";
if (!normalizedRoot || isAbsoluteFsPath(normalizedPath)) return normalizedPath;
return `${normalizedRoot}/${normalizedPath}`.replace(/\/+/g, "/");
}
function buildPreviewUrl(path) {
if (!path) return PLACEHOLDER_IMAGE_PATH;
return api.apiURL(`/h3refmods/refmod-browser/file?path=${encodeURIComponent(path)}`);
}
function entryHasVisual(entry) {
if (!entry) return true;
if (entry.paired_split) return true;
if (typeof entry.has_visual === "boolean") return entry.has_visual;
return entry.kind !== "audio";
}
function entryHasAudio(entry) {
if (!entry) return true;
if (entry.paired_split) return true;
if (typeof entry.has_audio === "boolean") return entry.has_audio;
return entry.kind === "audio";
}
function installPreviewFallback(img) {
if (!img || img.dataset.vrpFallbackInstalled === "1") return img;
img.dataset.vrpFallbackInstalled = "1";
img.addEventListener("error", () => {
if (img.src === PLACEHOLDER_IMAGE_PATH) return;
img.src = PLACEHOLDER_IMAGE_PATH;
});
return img;
}
function normalizePathKey(value) {
return String(value || "").replace(/\\/g, "/").replace(/\/+/g, "/").toLowerCase();
}
function pathsEqual(a, b) {
return normalizePathKey(a) === normalizePathKey(b);
}
async function getRefModsRoot() {
try {
const resp = await api.fetchApi("/h3refmods/refmod-browser/root");
if (!resp.ok) return "";
const data = await resp.json();
return data.ok ? data.root : "";
} catch (err) {
console.warn("[VisualRefModPicker] Could not get refmods root:", err);
return "";
}
}
async function listBrowserFolder(path) {
const query = path ? `?path=${encodeURIComponent(path)}` : "";
const resp = await api.fetchApi(`/h3refmods/refmod-browser/list${query}`);
if (!resp.ok) {
let msg = `Request failed (${resp.status})`;
try {
const err = await resp.json();
if (err?.error) msg = err.error;
} catch {
// ignore
}
throw new Error(msg);
}
return await resp.json();
}
function makeButton(label, onClick) {
const button = document.createElement("button");
button.textContent = label;
button.style.cssText = `
background: #2e3b4a;
border: 1px solid rgba(255,255,255,0.2);
border-radius: 6px;
color: #dce6f2;
padding: 5px 12px;
cursor: pointer;
font-size: 12px;
`;
button.onmouseenter = () => {
button.style.background = "#3a4a5c";
};
button.onmouseleave = () => {
button.style.background = "#2e3b4a";
};
button.addEventListener("click", onClick);
return button;
}
function createRefModBrowserModal(initialPath, onSelect) {
const overlay = document.createElement("div");
overlay.style.cssText = `
position: fixed;
inset: 0;
background: rgba(0, 0, 0, 0.8);
display: flex;
align-items: center;
justify-content: center;
z-index: 10000;
`;
const modal = document.createElement("div");
modal.style.cssText = `
background: rgba(40, 44, 52, 0.98);
border: 1px solid rgba(255,255,255,0.1);
border-radius: 6px;
width: 95%;
max-width: 1304px;
height: 95%;
max-height: 1185px;
display: flex;
flex-direction: column;
overflow: hidden;
box-shadow: 0 18px 60px rgba(0, 0, 0, 0.45);
`;
const header = document.createElement("div");
header.style.cssText = `
padding: 15px 20px;
border-bottom: 1px solid rgba(255,255,255,0.1);
`;
const topRow = document.createElement("div");
topRow.style.cssText = "display: flex; justify-content: space-between; align-items: center;";
const title = document.createElement("div");
title.textContent = "Select RefMod";
title.style.cssText = "margin: 0; color: #aaa; font-size: 1.17em; font-weight: 600;";
const closeButton = document.createElement("button");
closeButton.textContent = "×";
closeButton.style.cssText = `
background: none;
border: none;
color: #aaa;
font-size: 24px;
cursor: pointer;
padding: 0;
width: 30px;
height: 30px;
`;
closeButton.onclick = () => overlay.remove();
const upButton = makeButton("Up", () => {
if (state.parentPath) void loadFolder(state.parentPath);
});
topRow.append(title, closeButton);
header.append(topRow);
const filterBar = document.createElement("div");
filterBar.style.cssText = `
padding: 10px 20px;
border-bottom: 1px solid rgba(255,255,255,0.1);
display: flex;
align-items: center;
gap: 10px;
`;
filterBar.appendChild(upButton);
const searchWrap = document.createElement("div");
searchWrap.style.cssText = "position: relative; flex: 1; display: flex; align-items: center;";
const searchInput = document.createElement("input");
searchInput.type = "text";
searchInput.placeholder = "Search files...";
searchInput.style.cssText = `
flex: 1;
padding: 8px 36px 8px 12px;
background: rgba(45, 55, 72, 0.7);
border: 1px solid rgba(226, 232, 240, 0.2);
border-radius: 6px;
color: #ccc;
`;
const clearSearchButton = document.createElement("button");
clearSearchButton.type = "button";
clearSearchButton.textContent = "×";
clearSearchButton.title = "Clear search";
clearSearchButton.style.cssText = `
position: absolute;
right: 8px;
top: 50%;
transform: translateY(-50%);
width: 20px;
height: 20px;
padding: 0;
border: none;
background: transparent;
color: #aaa;
cursor: pointer;
font-size: 16px;
line-height: 1;
display: none;
`;
searchWrap.append(searchInput, clearSearchButton);
filterBar.appendChild(searchWrap);
const body = document.createElement("div");
body.style.cssText = `
flex: 1;
overflow-y: auto;
padding: 20px;
display: grid;
grid-template-columns: repeat(auto-fill, minmax(180px, 1fr));
gap: 15px;
align-content: start;
`;
const status = document.createElement("div");
status.style.cssText = "padding: 10px 20px; border-bottom: 1px solid rgba(255,255,255,0.1); display: flex; gap: 12px; align-items: center; font-size: 12px; color: #888;";
const countsLabel = document.createElement("div");
countsLabel.style.cssText = "white-space: nowrap;";
const pathLabel = document.createElement("div");
pathLabel.style.cssText = "min-width: 0; flex: 1; white-space: nowrap; overflow: hidden; text-overflow: ellipsis;";
status.append(countsLabel, pathLabel);
modal.append(header, filterBar, status, body);
overlay.appendChild(modal);
document.body.appendChild(overlay);
overlay.addEventListener("click", (event) => {
if (event.target === overlay) overlay.remove();
});
const state = {
root: "",
currentPath: "",
parentPath: null,
items: [],
};
function setCardSelected(card, selected) {
if (!card) return;
card.dataset.selected = selected ? "1" : "0";
card.style.borderColor = selected ? "rgba(255, 255, 255, 0.95)" : "rgba(226, 232, 240, 0.2)";
card.style.background = selected ? "rgba(50, 112, 163, 0.28)" : "rgba(45, 55, 72, 0.7)";
card.style.transform = "translateY(0)";
}
function updateVisibleItems() {
const search = String(searchInput.value || "").trim().toLowerCase();
for (const entry of state.items) {
const haystack = `${entry.kind === "dir" ? entry.data.name : entry.data.name}`.toLowerCase();
const visible = !search || haystack.includes(search);
entry.element.style.display = visible ? "flex" : "none";
}
}
function updateSearchClearButton() {
clearSearchButton.style.display = searchInput.value ? "block" : "none";
}
function folderCard(entry) {
const card = document.createElement("div");
card.className = "folder-item";
card.style.cssText = `
background: rgba(45, 55, 72, 0.7);
border: 1px solid rgba(226, 232, 240, 0.2);
border-radius: 6px;
padding: 8px;
cursor: pointer;
transition: all 0.15s ease;
display: flex;
flex-direction: column;
gap: 8px;
`;
const icon = document.createElement("div");
icon.style.cssText = `
width: 100%;
height: 250px;
background: rgba(0, 0, 0, 0.5);
border-radius: 4px;
display: flex;
align-items: center;
justify-content: center;
font-size: 64px;
`;
icon.textContent = "📁";
const label = document.createElement("div");
label.textContent = entry.name;
label.style.cssText = `
font-size: 12px;
color: #ccc;
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
text-align: center;
`;
label.title = entry.name;
card.append(icon, label);
card.onmouseenter = () => {
card.style.borderColor = "rgba(66, 153, 225, 0.9)";
card.style.transform = "translateY(-2px)";
card.style.background = "rgba(50, 112, 163, 0.5)";
};
card.onmouseleave = () => {
card.style.borderColor = "rgba(226, 232, 240, 0.2)";
card.style.transform = "translateY(0)";
card.style.background = "rgba(45, 55, 72, 0.7)";
};
card.addEventListener("click", () => void loadFolder(entry.path));
return card;
}
function modCard(entry) {
const card = document.createElement("div");
card.className = "thumbnail-item";
card.style.cssText = `
background: rgba(45, 55, 72, 0.7);
border: 1px solid rgba(226, 232, 240, 0.2);
border-radius: 6px;
padding: 8px;
cursor: pointer;
transition: all 0.15s ease;
display: flex;
flex-direction: column;
gap: 8px;
`;
const imageBox = document.createElement("div");
imageBox.style.cssText = `
width: 100%;
height: 250px;
background: rgba(0, 0, 0, 0.5);
border-radius: 4px;
display: flex;
align-items: center;
justify-content: center;
overflow: hidden;
position: relative;
`;
const img = document.createElement("img");
installPreviewFallback(img);
img.src = entry.preview_path ? `${buildPreviewUrl(entry.preview_path)}&${Date.now()}` : PLACEHOLDER_IMAGE_PATH;
img.style.cssText = `
width: 100%;
height: 100%;
object-fit: cover;
object-position: center center;
display: block;
background: rgba(0, 0, 0, 0.5);
`;
imageBox.appendChild(img);
const title = document.createElement("div");
title.textContent = entryDisplayName(entry);
title.style.cssText = `
font-size: 12px;
color: #ccc;
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
text-align: center;
`;
title.title = entryDisplayName(entry);
card.append(imageBox, title);
const selectedPath = state.selectedPath;
setCardSelected(card, !!selectedPath && pathsEqual(selectedPath, entry.path));
card.onmouseenter = () => {
if (card.dataset.selected === "1") return;
card.style.borderColor = "rgba(66, 153, 225, 0.9)";
card.style.transform = "translateY(-2px)";
card.style.background = "rgba(50, 112, 163, 0.5)";
};
card.onmouseleave = () => {
if (card.dataset.selected === "1") {
setCardSelected(card, true);
return;
}
card.style.borderColor = "rgba(226, 232, 240, 0.2)";
card.style.transform = "translateY(0)";
card.style.background = "rgba(45, 55, 72, 0.7)";
};
card.addEventListener("click", () => {
onSelect(entry);
overlay.remove();
});
return card;
}
async function loadFolder(path) {
const requestedPath = path || state.root;
try {
countsLabel.textContent = "Loading...";
pathLabel.textContent = "";
body.replaceChildren();
state.items = [];
const data = await listBrowserFolder(requestedPath);
state.root = data.root || state.root;
state.currentPath = data.current_path || requestedPath || state.currentPath;
state.parentPath = data.parent_path || null;
state.selectedPath = null;
pathLabel.textContent = displayPathFromRoot(
state.currentPath || state.root,
state.root
);
upButton.disabled = !state.parentPath;
countsLabel.textContent = `${(data.dirs || []).length} folder(s), ${(data.mods || []).length} RefMod(s)`;
for (const dir of data.dirs || []) {
const element = folderCard(dir);
state.items.push({ kind: "dir", data: dir, element });
body.appendChild(element);
}
for (const mod of data.mods || []) {
const element = modCard(mod);
state.items.push({ kind: "mod", data: mod, element });
body.appendChild(element);
}
if (!body.childNodes.length) {
const empty = document.createElement("div");
empty.textContent = "No RefMods or subfolders found here.";
empty.style.cssText = "text-align: center; padding: 40px; color: #aaa;";
body.appendChild(empty);
}
updateVisibleItems();
} catch (err) {
const rootPath = state.root || await getRefModsRoot();
if (rootPath && requestedPath && !pathsEqual(requestedPath, rootPath)) {
console.warn("[VisualRefModPicker] Browser folder missing, falling back to refmods root:", requestedPath, err);
state.root = rootPath;
await loadFolder(rootPath);
return;
}
countsLabel.textContent = err?.message || "Could not load RefMods.";
pathLabel.textContent = "";
}
}
searchInput.addEventListener("input", () => {
updateSearchClearButton();
updateVisibleItems();
});
clearSearchButton.addEventListener("click", () => {
searchInput.value = "";
updateSearchClearButton();
updateVisibleItems();
searchInput.focus();
});
updateSearchClearButton();
void (async () => {
const root = await getRefModsRoot();
state.root = root;
await loadFolder(initialPath || root);
})();
}
app.registerExtension({
name: "H3RefModPicker.VisualRefModPicker",
async beforeRegisterNodeDef(nodeType, nodeData) {
if (nodeData?.name !== "H3RefModVisualPicker") return;
const onNodeCreated = nodeType.prototype.onNodeCreated;
nodeType.prototype.onNodeCreated = function () {
const result = onNodeCreated?.apply(this, arguments);
const node = this;
if (!node.properties) node.properties = {};
node._configuredFromWorkflow = false;
const modPathWidget = node.widgets?.find((w) => w.name === "mod_path");
const strengthWidget = node.widgets?.find((w) => w.name === "video_weight")
|| node.widgets?.find((w) => w.name === "strength");
const audioStrengthWidget = node.widgets?.find((w) => w.name === "audio_weight")
|| node.widgets?.find((w) => w.name === "audio_strength");
const panel = document.createElement("div");
panel.style.cssText = `
display: flex;
flex-direction: column;
width: 100%;
height: 100%;
box-sizing: border-box;
gap: 2px;
overflow: hidden;
background: transparent;
`;
const previewBox = document.createElement("div");
previewBox.style.cssText = `
position: relative;
flex: 1;
min-height: 80px;
width: 100%;
border-radius: 10px;
background: rgba(34, 39, 48, 0.98);
border: 1px solid rgba(78, 90, 108, 0.72);
overflow: hidden;
box-sizing: border-box;
display: flex;
align-items: center;
justify-content: center;
padding: 2px 4px 4px;
`;
const portraitFrame = document.createElement("div");
portraitFrame.style.cssText = `
position: relative;
height: 100%;
width: auto;
max-width: 100%;
max-height: 100%;
aspect-ratio: 3 / 4;
border-radius: 8px;
overflow: hidden;
background: rgba(0, 0, 0, 0.5);
flex: 0 1 auto;
`;
const img = document.createElement("img");
img.draggable = false;
installPreviewFallback(img);
img.src = PLACEHOLDER_IMAGE_PATH;
img.style.cssText = `
position: absolute;
inset: 0;
width: 100%;
height: 100%;
object-fit: cover;
object-position: center center;
display: block;
user-select: none;
pointer-events: none;
-webkit-user-drag: none;
`;
const emptyLabel = document.createElement("div");
emptyLabel.textContent = "No RefMod selected";
emptyLabel.style.cssText = `
position: absolute;
inset: 0;
display: flex;
align-items: center;
justify-content: center;
text-align: center;
font-size: 11px;
color: rgba(178, 191, 208, 0.92);
background: rgba(0, 0, 0, 0.25);
border-radius: 10px;
pointer-events: none;
`;
portraitFrame.appendChild(img);
portraitFrame.appendChild(emptyLabel);
previewBox.appendChild(portraitFrame);
const clickCatcher = document.createElement("div");
clickCatcher.style.cssText = `
position: absolute;
inset: 0;
cursor: pointer;
background: transparent;
`;
previewBox.appendChild(clickCatcher);
previewBox.addEventListener("wheel", (event) => {
const canvas = app.canvas?.canvas || document.querySelector("canvas.lgraphcanvas");
if (!canvas) return;
const forwarded = new WheelEvent("wheel", {
bubbles: true,
cancelable: true,
clientX: event.clientX,
clientY: event.clientY,
deltaX: event.deltaX,
deltaY: event.deltaY,
deltaZ: event.deltaZ,
deltaMode: event.deltaMode,
ctrlKey: event.ctrlKey,
shiftKey: event.shiftKey,
altKey: event.altKey,
metaKey: event.metaKey,
});
canvas.dispatchEvent(forwarded);
}, { passive: true });
panel.appendChild(previewBox);
const domWidget = node.addDOMWidget("vrp_preview", "div", panel, {
serialize: false,
hideOnZoom: false,
});
domWidget.getHeight = () => estimatePreviewHeight(node, domWidget);
const strengthWidgetIndex = node.widgets.indexOf(strengthWidget);
const audioStrengthWidgetIndex = node.widgets.indexOf(audioStrengthWidget);
const numericStrengthWidget = node.addWidget(
"number",
"video_weight",
strengthWidget?.value != null ? Number(strengthWidget.value) : 1.0,
(v) => {
if (strengthWidget) strengthWidget.value = Number(v);
node.setDirtyCanvas(true, true);
},
{
min: strengthWidget?.options?.min != null ? Number(strengthWidget.options.min) : 0,
max: strengthWidget?.options?.max != null ? Number(strengthWidget.options.max) : 10,
step: strengthWidget?.options?.step != null ? Number(strengthWidget.options.step) : 0.01,
precision: 2,
}
);
numericStrengthWidget.serialize = false;
const numericAudioStrengthWidget = node.addWidget(
"number",
"audio_weight",
audioStrengthWidget?.value != null ? Number(audioStrengthWidget.value) : 1.0,
(v) => {
if (audioStrengthWidget) audioStrengthWidget.value = Number(v);
node.setDirtyCanvas(true, true);
},
{
min: audioStrengthWidget?.options?.min != null ? Number(audioStrengthWidget.options.min) : 0,
max: audioStrengthWidget?.options?.max != null ? Number(audioStrengthWidget.options.max) : 10,
step: audioStrengthWidget?.options?.step != null ? Number(audioStrengthWidget.options.step) : 0.01,
precision: 2,
}
);
numericAudioStrengthWidget.serialize = false;
const normalizedStrengthValue = (value, fallback = 1.0) => {
const parsed = Number(value);
return Number.isFinite(parsed) ? parsed : fallback;
};
const syncNumericStrength = () => {
const fallback = normalizedStrengthValue(numericStrengthWidget?.value, 1.0);
const next = normalizedStrengthValue(strengthWidget?.value, fallback);
numericStrengthWidget.value = next;
if (strengthWidget) strengthWidget.value = next;
};
const syncNumericAudioStrength = () => {
const fallback = normalizedStrengthValue(numericAudioStrengthWidget?.value, 1.0);
const next = normalizedStrengthValue(audioStrengthWidget?.value, fallback);
numericAudioStrengthWidget.value = next;
if (audioStrengthWidget) audioStrengthWidget.value = next;
};
if (strengthWidget) {
const originalStrengthCb = strengthWidget.callback;
strengthWidget.callback = function () {
syncNumericStrength();
if (originalStrengthCb) return originalStrengthCb.apply(this, arguments);
};
}
if (audioStrengthWidget) {
const originalAudioStrengthCb = audioStrengthWidget.callback;
audioStrengthWidget.callback = function () {
syncNumericAudioStrength();
if (originalAudioStrengthCb) return originalAudioStrengthCb.apply(this, arguments);
};
}
syncNumericStrength();
syncNumericAudioStrength();
if (strengthWidget) hideWidget(strengthWidget);
if (audioStrengthWidget) hideWidget(audioStrengthWidget);
if (modPathWidget) hideWidget(modPathWidget);
const applyModalityVisibility = (entry) => {
const hasVisual = entryHasVisual(entry);
const hasAudio = entryHasAudio(entry);
setDynamicWidgetVisible(numericStrengthWidget, hasVisual);
setDynamicWidgetVisible(numericAudioStrengthWidget, hasAudio);
node.properties._vrpHasVisual = hasVisual;
node.properties._vrpHasAudio = hasAudio;
refreshPickerLayout(node);
};
const storedPreviewEntry = (path) => {
if (!path) return null;
return {
path,
preview_path: node.properties?._vrpPreviewPath || "",
paired_split: Boolean(node.properties?._vrpPairedSplit),
has_visual: typeof node.properties?._vrpHasVisual === "boolean"
? node.properties._vrpHasVisual
: true,
has_audio: typeof node.properties?._vrpHasAudio === "boolean"
? node.properties._vrpHasAudio
: true,
};
};
applyModalityVisibility(null);
let filePickerWidget = node.addWidget(
"combo",
"mod",
"(none)",
(label) => {
const selected = node._vrpModMap?.[label] || null;
setSelection(selected);
},
{ values: ["(none)"] }
);
filePickerWidget.serialize = false;
node._vrpModMap = { "(none)": null };
node._vrpStepModByDelta = async (delta) => {
const direction = delta >= 0 ? 1 : -1;
const labels = Array.isArray(filePickerWidget?.options?.values)
? filePickerWidget.options.values.filter((label) => label !== "(none)")
: [];
if (labels.length <= 1) return false;
const currentLabel = String(filePickerWidget?.value || "");
const currentIndex = labels.indexOf(currentLabel);
let nextIndex;
if (currentIndex < 0) {
nextIndex = direction > 0 ? 0 : labels.length - 1;
} else {
nextIndex = (currentIndex + direction + labels.length) % labels.length;
}
const nextLabel = labels[nextIndex];
if (!nextLabel) return false;
filePickerWidget.value = nextLabel;
if (typeof filePickerWidget.callback === "function") {
filePickerWidget.callback(nextLabel);
} else {
const nextEntry = node._vrpModMap?.[nextLabel] || null;
setSelection(nextEntry);
}
return true;
};
installVrpArrowNavigation();
const loadPreview = async (entry) => {
applyModalityVisibility(entry);
if (!entry || !entry.path) {
emptyLabel.textContent = "No RefMod selected";
img.removeAttribute("src");
img.style.display = "none";
emptyLabel.style.display = "flex";
return;
}
if (!entryHasVisual(entry)) {
emptyLabel.textContent = "Audio-only RefMod";
img.style.display = "block";
img.src = PLACEHOLDER_IMAGE_PATH;
emptyLabel.style.display = "flex";
return;
}
img.style.display = "block";
emptyLabel.style.display = "none";
img.src = entry.preview_path ? buildPreviewUrl(entry.preview_path) : PLACEHOLDER_IMAGE_PATH;
};
const setSelection = (entry) => {
const selected = entry && entry.path ? entry : null;
const rootPath = node._vrpRoot || "";
const absoluteValue = selected?.path || "";
const storedValue = relativePathFromRoot(absoluteValue, rootPath);
if (modPathWidget) modPathWidget.value = storedValue;
const label = Object.keys(node._vrpModMap || {}).find((key) => pathsEqual(node._vrpModMap[key]?.path, absoluteValue));
if (filePickerWidget) filePickerWidget.value = label || "(none)";
node.properties._vrpModPath = storedValue;
node.properties._vrpModDir = selected?.path ? dirnameForPath(storedValue) : (node.properties._vrpModDir || "");
node.properties._vrpPreviewPath = relativePathFromRoot(selected?.preview_path || "", rootPath);
node.properties._vrpPairedSplit = Boolean(selected?.paired_split);
node.properties._vrpHasVisual = selected ? entryHasVisual(selected) : true;
node.properties._vrpHasAudio = selected ? entryHasAudio(selected) : true;
void loadPreview(selected);
node.setDirtyCanvas(true, true);
};
const refreshPickerOptions = async (folderPath, preferredPath = null) => {
let requestedFolderPath = folderPath || "";
let fallbackRoot = node._vrpRoot || await getRefModsRoot();
if (!requestedFolderPath) {
requestedFolderPath = fallbackRoot;
}
if (!requestedFolderPath) return;
let data;
try {
data = await listBrowserFolder(requestedFolderPath);
} catch (err) {
fallbackRoot = fallbackRoot || await getRefModsRoot();
if (!fallbackRoot || pathsEqual(requestedFolderPath, fallbackRoot)) {
throw err;
}
console.warn("[VisualRefModPicker] Restored folder missing, falling back to refmods root:", requestedFolderPath, err);
requestedFolderPath = fallbackRoot;
data = await listBrowserFolder(requestedFolderPath);
node.properties._vrpModDir = "";
}
node._vrpRoot = data.root || node._vrpRoot || "";
if (pathsEqual(requestedFolderPath, node._vrpRoot || "")) {
node.properties._vrpModDir = "";
} else {
node.properties._vrpModDir = relativePathFromRoot(requestedFolderPath, node._vrpRoot || data.root || "");
}
const labels = ["(none)"];
const map = { "(none)": null };
const used = new Set(labels);
for (const entry of data.mods || []) {
const base = entryDisplayName(entry) || entry.name;
let label = base;
let idx = 2;
while (used.has(label)) {
label = `${base} (${idx++})`;
}
used.add(label);
labels.push(label);
map[label] = entry;
}
node._vrpModMap = map;
if (filePickerWidget) filePickerWidget.options.values = labels;
const desiredPath = preferredPath != null ? preferredPath : (modPathWidget?.value || "");
const desiredAbsolutePath = resolvePathFromRoot(desiredPath, node._vrpRoot || data.root || "");
const desiredLabel = Object.keys(map).find((key) => pathsEqual(map[key]?.path, desiredAbsolutePath));
if (filePickerWidget) filePickerWidget.value = desiredLabel || "(none)";
return desiredLabel ? map[desiredLabel] : null;
};
const openBrowser = async () => {
const root = await getRefModsRoot();
node._vrpRoot = root || node._vrpRoot || "";
const current = node.properties?._vrpModPath || modPathWidget?.value || "";
const initialPath = resolvePathFromRoot(
node.properties?._vrpModDir || dirnameForPath(current),
node._vrpRoot || root
) || root;
createRefModBrowserModal(initialPath, async (selected) => {
node.properties._vrpModDir = dirnameForPath(
relativePathFromRoot(selected.path, node._vrpRoot || root)
);
setSelection(selected);
await refreshPickerOptions(node.properties._vrpModDir, selected.path);
});
};
const browseButton = node.addWidget("button", "📁 Select RefMod", null, openBrowser);
browseButton.serialize = false;
const modPathWidgetIndex = node.widgets.indexOf(modPathWidget);
if (modPathWidgetIndex >= 0 && filePickerWidget) {
const pickerIndex = node.widgets.indexOf(filePickerWidget);
if (pickerIndex >= 0) {
node.widgets.splice(pickerIndex, 1);
node.widgets.splice(modPathWidgetIndex + 1, 0, filePickerWidget);
}
const buttonIndex = node.widgets.indexOf(browseButton);
if (buttonIndex >= 0) {
node.widgets.splice(buttonIndex, 1);
node.widgets.splice(modPathWidgetIndex + 2, 0, browseButton);
}
const previewIndex = node.widgets.indexOf(domWidget);
if (previewIndex >= 0) {
node.widgets.splice(previewIndex, 1);
node.widgets.splice(modPathWidgetIndex + 3, 0, domWidget);
}
}
if (strengthWidgetIndex >= 0 && numericStrengthWidget) {
const numericIndex = node.widgets.indexOf(numericStrengthWidget);
if (numericIndex >= 0) {
node.widgets.splice(numericIndex, 1);
node.widgets.splice(strengthWidgetIndex + 1, 0, numericStrengthWidget);
}
}
if (audioStrengthWidgetIndex >= 0 && numericAudioStrengthWidget) {
const numericAudioIndex = node.widgets.indexOf(numericAudioStrengthWidget);
if (numericAudioIndex >= 0) {
node.widgets.splice(numericAudioIndex, 1);
node.widgets.splice(audioStrengthWidgetIndex + 1, 0, numericAudioStrengthWidget);
}
}
if (filePickerWidget && browseButton) {
const pickerIndex = node.widgets.indexOf(filePickerWidget);
const buttonIndex = node.widgets.indexOf(browseButton);
if (pickerIndex >= 0 && buttonIndex >= 0) {
node.widgets.splice(buttonIndex, 1);
node.widgets.splice(pickerIndex + 1, 0, browseButton);
}
}
clickCatcher.addEventListener("click", async (event) => {
event.stopPropagation();
await openBrowser();
});
const onMouseMove = node.onMouseMove;
node.onMouseMove = function () {
_vrpHoveredNode = this;
return onMouseMove?.apply(this, arguments);
};
const onMouseEnter = node.onMouseEnter;
node.onMouseEnter = function () {
_vrpHoveredNode = this;
return onMouseEnter?.apply(this, arguments);
};
const onMouseLeave = node.onMouseLeave;
node.onMouseLeave = function () {
if (_vrpHoveredNode === this) {
_vrpHoveredNode = null;
}
return onMouseLeave?.apply(this, arguments);
};
if (previewBox) {
previewBox.addEventListener("mouseenter", () => {
_vrpHoveredNode = node;
});
previewBox.addEventListener("mouseleave", () => {
if (_vrpHoveredNode === node) {
_vrpHoveredNode = null;
}
});
}
const originalSerialize = node.serialize;
node.serialize = function () {
const rootPath = node._vrpRoot || "";
const serializedModPath = normalizeSelectedModPath(
relativePathFromRoot(node.properties?._vrpModPath || modPathWidget?.value || "", rootPath)
);
if (modPathWidget) {
modPathWidget.value = serializedModPath;
}
node.properties._vrpModPath = serializedModPath;
if (serializedModPath) {
node.properties._vrpModDir = dirnameForPath(serializedModPath);
}
node.properties._vrpPreviewPath = relativePathFromRoot(node.properties?._vrpPreviewPath || "", rootPath);
if (strengthWidget && numericStrengthWidget) {
strengthWidget.value = normalizedStrengthValue(
numericStrengthWidget.value,
normalizedStrengthValue(strengthWidget.value, 1.0)
);
numericStrengthWidget.value = strengthWidget.value;
}
if (audioStrengthWidget && numericAudioStrengthWidget) {
audioStrengthWidget.value = normalizedStrengthValue(
numericAudioStrengthWidget.value,
normalizedStrengthValue(audioStrengthWidget.value, 1.0)
);
numericAudioStrengthWidget.value = audioStrengthWidget.value;
}
return originalSerialize ? originalSerialize.apply(this, arguments) : undefined;
};
const oldOnResize = node.onResize;
node.onResize = function (size) {
const res = oldOnResize?.apply(this, arguments);
if (size) {
if (size[0] < 200) size[0] = 200;
if (size[1] < 320) size[1] = 320;
}
return res;
};
const origDraw = domWidget.draw;
domWidget.draw = function (ctx, n) {
if (typeof origDraw === "function") origDraw.apply(this, arguments);
if (!panel || n.flags?.collapsed) return;
panel.style.setProperty("width", (n.size[0] - 18) + "px", "important");
panel.style.setProperty("left", "0px", "important");
panel.style.setProperty("margin", "0px", "important");
panel.style.setProperty("padding", "0 4px 4px", "important");
panel.style.setProperty("height", `calc(100% + ${PREVIEW_PANEL_OVERLAP_PX}px)`, "important");
panel.style.setProperty("transform", `translateY(-${PREVIEW_PANEL_OVERLAP_PX}px)`, "important");
panel.style.setProperty("transform-origin", "top left", "important");
panel.style.setProperty("box-sizing", "border-box", "important");
panel.style.setProperty("overflow", "visible", "important");
};
const onConfigure = node.onConfigure;
node.onConfigure = async function (info) {
node._configuredFromWorkflow = true;
node.properties._configuredFromWorkflow = true;
const res = onConfigure?.apply(this, arguments);
if (info && info.widgets_values) {
if (strengthWidget) {
const idx = this.widgets.indexOf(strengthWidget);
if (idx >= 0 && info.widgets_values[idx] !== undefined && info.widgets_values[idx] !== null) {
strengthWidget.value = normalizedStrengthValue(info.widgets_values[idx], 1.0);
}
}
if (audioStrengthWidget) {
const idx = this.widgets.indexOf(audioStrengthWidget);
if (idx >= 0 && info.widgets_values[idx] !== undefined && info.widgets_values[idx] !== null) {
audioStrengthWidget.value = normalizedStrengthValue(info.widgets_values[idx], 1.0);
}
}
}
if (strengthWidget && !Number.isFinite(Number(strengthWidget.value))) {
strengthWidget.value = 1.0;
}
if (audioStrengthWidget && !Number.isFinite(Number(audioStrengthWidget.value))) {
audioStrengthWidget.value = 1.0;
}
syncNumericStrength();
syncNumericAudioStrength();
const restoredPath = normalizeSelectedModPath(
modPathWidget?.value || node.properties?._vrpModPath || ""
);
if (modPathWidget) {
modPathWidget.value = restoredPath;
}
node.properties._vrpModPath = restoredPath;
if (restoredPath) {
node.properties._vrpModDir = dirnameForPath(restoredPath);
}
const restoredDir = node.properties?._vrpModDir || dirnameForPath(restoredPath);
const dir = restoredDir || await getRefModsRoot();
node.properties._vrpModDir = dir;
const restoredEntry = await refreshPickerOptions(dir, restoredPath);
if (restoredEntry) {
setSelection(restoredEntry);
} else {
void loadPreview(storedPreviewEntry(restoredPath));
}
return res;
};
setTimeout(async () => {
syncNumericStrength();
syncNumericAudioStrength();
const initialPath = normalizeSelectedModPath(
modPathWidget?.value || node.properties?._vrpModPath || ""
);
if (modPathWidget) {
modPathWidget.value = initialPath;
}
node.properties._vrpModPath = initialPath;
if (initialPath) {
node.properties._vrpModDir = node.properties?._vrpModDir || dirnameForPath(initialPath);
}
const dir = node.properties?._vrpModDir || dirnameForPath(initialPath) || await getRefModsRoot();
node.properties._vrpModDir = dir;
const initialEntry = await refreshPickerOptions(dir, initialPath || null);
if (initialEntry) {
setSelection(initialEntry);
} else {
void loadPreview(storedPreviewEntry(initialPath));
}
if (!node.properties?._configuredFromWorkflow) {
node.setSize([...DEFAULT_PICKER_NODE_SIZE]);
}
}, 10);
return result;
};
},
});
console.log("[H3RefModPicker] VisualRefModPicker extension loaded");