This commit is contained in:
justumen
2024-11-22 12:30:00 +01:00
parent 840e62d00c
commit 0673c134d5
46 changed files with 1629 additions and 410 deletions

View File

@@ -1,65 +1,171 @@
import { app } from "../../../scripts/app.js";
import { api } from "../../../scripts/api.js";
// Add CSS style for the black background button class
const style = document.createElement("style");
style.textContent = `
.reset-button-exceeded {
background-color: black !important;
color: white !important;
}
`;
app.registerExtension({
name: "Bjornulf.LoopIntegerSequential",
async nodeCreated(node) {
if (node.comfyClass !== "Bjornulf_LoopIntegerSequential") return;
name: "Bjornulf.LoopIntegerSequential",
async nodeCreated(node) {
if (node.comfyClass !== "Bjornulf_LoopIntegerSequential") return;
// Hide seed widget
const seedWidget = node.widgets.find(w => w.name === "seed");
if (seedWidget) {
seedWidget.visible = false;
}
// Add get value button
// const getValueButton = node.addWidget("button", "Get Counter Value", null, () => {
// fetch('/get_counter_value')
// .then(response => response.json())
// .then(data => {
// if (data.success) {
// app.ui.toast(`Current counter value: ${data.value}`, {'duration': 5000});
// } else {
// app.ui.toast(`Failed to get counter value: ${data.error || "Unknown error"}`, {'type': 'error', 'duration': 5000});
// }
// })
// .catch((error) => {
// console.error('Error:', error);
// app.ui.toast("An error occurred while getting the counter value.", {'type': 'error', 'duration': 5000});
// });
// });
// Add reset button
const resetButton = node.addWidget("button", "Reset Counter", null, () => {
fetch('/reset_counter', {
method: 'POST'
})
.then(response => response.json())
.then(data => {
if (data.success) {
app.ui.toast("Counter reset successfully!", {'duration': 5000});
} else {
app.ui.toast(`Failed to reset counter: ${data.error || "Unknown error"}`, {'type': 'error', 'duration': 5000});
}
})
.catch((error) => {
console.error('Error:', error);
app.ui.toast("An error occurred while resetting the counter.", {'type': 'error', 'duration': 5000});
});
});
// Override the original execute function
const originalExecute = node.execute;
node.execute = function() {
const result = originalExecute.apply(this, arguments);
if (result instanceof Promise) {
return result.catch(error => {
if (error.message.includes("Counter has reached its limit")) {
app.ui.toast(`Execution blocked: ${error.message}`, {'type': 'error', 'duration': 5000});
}
throw error; // Re-throw the error to stop further execution
});
}
return result;
};
// Hide seed widget
const seedWidget = node.widgets.find((w) => w.name === "seed");
if (seedWidget) {
seedWidget.visible = false;
}
});
// Function to update the Reset Button text
const updateResetButtonTextNode = () => {
fetch("/get_counter_value", {
method: "POST",
})
.then((response) => response.json())
.then((data) => {
if (data.success) {
const jumpWidget = node.widgets.find((w) => w.name === "jump");
const fromThisWidget = node.widgets.find(
(w) => w.name === "from_this"
);
if (data.value === 0) {
resetButton.name =
"Reset Counter (Empty, next: " + fromThisWidget.value + ")";
} else {
const toThatWidget = node.widgets.find(
(w) => w.name === "to_that"
);
let next_value = data.value + jumpWidget.value - 1;
if (next_value > toThatWidget.value) {
resetButton.name = `Reset Counter (ABOVE MAX: ${next_value} > ${toThatWidget.value})`;
console.log("resetButton", resetButton);
} else {
resetButton.name = `Reset Counter (next: ${next_value})`; // - ${toThatWidget.value}
}
}
} else {
console.error("Error in context size:", data.error);
resetButton.name = "Reset Counter (Error)";
}
})
.catch((error) => {
console.error("Error fetching context size:", error);
resetButton.name = "Reset Counter (Error)";
});
};
// Add reset button
const resetButton = node.addWidget("button", "Reset Counter", null, () => {
fetch("/reset_counter", {
method: "POST",
})
.then((response) => response.json())
.then((data) => {
if (data.success) {
// updateLineNumber();
updateResetButtonTextNode();
app.ui.toast("Counter reset successfully!", { duration: 5000 });
} else {
app.ui.toast(
`Failed to reset counter: ${data.error || "Unknown error"}`,
{ type: "error", duration: 5000 }
);
}
})
.catch((error) => {
console.error("Error:", error);
app.ui.toast("An error occurred while resetting the counter.", {
type: "error",
duration: 5000,
});
});
});
// Override the original execute function
const originalExecute = node.execute;
node.execute = function () {
const result = originalExecute.apply(this, arguments);
if (result instanceof Promise) {
return result.catch((error) => {
if (error.message.includes("Counter has reached its limit")) {
app.ui.toast(`Execution blocked: ${error.message}`, {
type: "error",
duration: 5000,
});
}
throw error; // Re-throw the error to stop further execution
});
}
return result;
};
// Initial update of showing counter number
setTimeout(updateResetButtonTextNode, 0);
// Listen for node execution events (update value when node executed)
api.addEventListener("executed", async () => {
updateResetButtonTextNode();
});
// Add a handler for the jump widget (update value reset on change)
const jumpWidget = node.widgets.find((w) => w.name === "jump");
if (jumpWidget) {
const originalOnChange = jumpWidget.callback;
jumpWidget.callback = function (v) {
if (originalOnChange) {
originalOnChange.call(this, v);
}
updateResetButtonTextNode();
};
}
// Add a handler for the to_that widget (update value reset on change)
const toThatWidget = node.widgets.find((w) => w.name === "to_that");
if (toThatWidget) {
const originalOnChange = toThatWidget.callback;
toThatWidget.callback = function (v) {
if (originalOnChange) {
originalOnChange.call(this, v);
}
updateResetButtonTextNode();
};
}
// Add a handler for the to_that widget (on change from_this => reset button)
const fromThisWidget = node.widgets.find((w) => w.name === "from_this");
if (fromThisWidget) {
const originalOnChange = fromThisWidget.callback;
fromThisWidget.callback = function (v) {
if (originalOnChange) {
originalOnChange.call(this, v);
}
fetch("/reset_counter", {
method: "POST",
})
.then((response) => response.json())
.then((data) => {
if (data.success) {
// updateLineNumber();
updateResetButtonTextNode();
app.ui.toast("Counter reset successfully!", { duration: 5000 });
} else {
app.ui.toast(
`Failed to reset counter: ${data.error || "Unknown error"}`,
{ type: "error", duration: 5000 }
);
}
})
.catch((error) => {
console.error("Error:", error);
app.ui.toast("An error occurred while resetting the counter.", {
type: "error",
duration: 5000,
});
});
};
}
},
});