|
import { app } from "../../../scripts/app.js";
|
|
import { api } from "../../../scripts/api.js";
|
|
|
|
|
|
const style = document.createElement('style');
|
|
style.textContent = `
|
|
.if-button {
|
|
background: var(--comfy-input-bg);
|
|
border: 1px solid var(--border-color);
|
|
color: var(--input-text);
|
|
padding: 4px 12px;
|
|
border-radius: 4px;
|
|
cursor: pointer;
|
|
transition: all 0.2s ease;
|
|
margin-right: 5px;
|
|
}
|
|
|
|
.if-button:hover {
|
|
background: var(--comfy-input-bg-hover);
|
|
}
|
|
`;
|
|
document.head.appendChild(style);
|
|
|
|
app.registerExtension({
|
|
name: "Comfy.IF_MemoAvatar",
|
|
async beforeRegisterNodeDef(nodeType, nodeData) {
|
|
if (nodeData.name !== "IF_MemoAvatar") return;
|
|
|
|
const origOnNodeCreated = nodeType.prototype.onNodeCreated;
|
|
|
|
nodeType.prototype.onNodeCreated = function() {
|
|
const result = origOnNodeCreated?.apply(this, arguments);
|
|
|
|
|
|
const audioWidget = this.widgets?.find(w => w.name === "audio");
|
|
if (audioWidget) {
|
|
|
|
const origSetValue = audioWidget.setValue;
|
|
const origCallback = audioWidget.callback;
|
|
|
|
audioWidget.setValue = function(v, skip_callback) {
|
|
const result = origSetValue?.call(this, v, skip_callback);
|
|
return result;
|
|
};
|
|
|
|
|
|
audioWidget.callback = function(value) {
|
|
if (origCallback) {
|
|
origCallback.call(this, value);
|
|
}
|
|
};
|
|
}
|
|
|
|
|
|
const selectFileBtn = this.addWidget("button", "select_audio", "Select WAV File 🎵", async () => {
|
|
const input = document.createElement("input");
|
|
input.type = "file";
|
|
input.accept = ".wav";
|
|
input.style.display = "none";
|
|
document.body.appendChild(input);
|
|
|
|
input.onchange = async (e) => {
|
|
try {
|
|
if (!e.target.files.length) return;
|
|
const file = e.target.files[0];
|
|
|
|
|
|
const formData = new FormData();
|
|
formData.append("files[]", file);
|
|
formData.append("subfolder", "");
|
|
formData.append("type", "input");
|
|
|
|
|
|
const uploadResp = await api.fetchApi("/upload/audio", {
|
|
method: "POST",
|
|
body: formData
|
|
});
|
|
|
|
if (uploadResp.ok) {
|
|
const data = await uploadResp.json();
|
|
|
|
const audioWidget = this.widgets.find(w => w.name === "audio");
|
|
if (audioWidget) {
|
|
audioWidget.value = data.name;
|
|
if (audioWidget.callback) {
|
|
audioWidget.callback(audioWidget.value);
|
|
}
|
|
console.log("Audio file uploaded successfully:", data.name);
|
|
}
|
|
} else {
|
|
throw new Error(await uploadResp.text());
|
|
}
|
|
} catch (error) {
|
|
console.error("Error handling file:", error);
|
|
alert("Error uploading audio file: " + error.message);
|
|
} finally {
|
|
document.body.removeChild(input);
|
|
}
|
|
};
|
|
|
|
input.click();
|
|
});
|
|
|
|
|
|
this.addWidget("preview", "preview", {
|
|
serialize: false,
|
|
size: [256, 256]
|
|
});
|
|
|
|
|
|
if (this.widgets) {
|
|
this.widgets.forEach(w => w.parent = this);
|
|
}
|
|
|
|
return result;
|
|
};
|
|
|
|
|
|
nodeType.prototype.onResize = function(size) {
|
|
const minWidth = 400;
|
|
const minHeight = 200;
|
|
size[0] = Math.max(size[0], minWidth);
|
|
size[1] = Math.max(size[1], minHeight);
|
|
};
|
|
|
|
|
|
nodeType.prototype.onExecuted = function(message) {
|
|
if (message.preview) {
|
|
const previewWidget = this.widgets.find(w => w.name === "preview");
|
|
if (previewWidget) {
|
|
previewWidget.value = message.preview;
|
|
}
|
|
}
|
|
};
|
|
}
|
|
}); |