This commit is contained in:
Sida Liu
2024-04-06 19:51:22 +08:00
parent 139718dec3
commit 3fdd04de16
3 changed files with 73 additions and 49 deletions
+43
View File
@@ -0,0 +1,43 @@
import { api } from '../../scripts/api.js';
import { app } from '../../scripts/app.js'; // Assuming `app` exports the current application instance
// Function to update STRING widget's content based on server response
function updateStringWidgetContent(nodeId, widgetName, newContent) {
const node = app.graph._nodes.find(n => n.id === nodeId);
if (!node) {
console.error('Node not found:', nodeId);
return;
}
const widget = node.widgets.find(w => w.name === widgetName && w.type === 'customtext');
if (!widget) {
console.error('STRING widget not found:', widgetName);
return;
}
widget.value = newContent;
// Optionally, if there's a callback function defined for the widget, call it
if (widget.callback) {
widget.callback(newContent);
}
}
// Listen for a specific event from the server indicating a script execution is completed
api.addEventListener('executed', event => {
const detail = event.detail;
// Check if `output` exists and has a `output`
if (detail.output && Array.isArray(detail.output.output)) {
// Join the array elements into a string
const content = detail.output.output.join('');
// Example node ID and widget name, replace with actual ones
const nodeId = parseInt(app.runningNodeId); // Replace with actual node ID
const widgetName = 'output'; // Replace with actual widget name
updateStringWidgetContent(nodeId, widgetName, content);
} else {
console.error('Unexpected detail format:', detail);
}
});