新增SetNodeInput功能

This commit is contained in:
puke
2025-06-02 18:03:38 +08:00
parent 8bce2f8c18
commit 1344a2fcfd
3 changed files with 115 additions and 0 deletions
+1
View File
@@ -17,6 +17,7 @@ def init_oneapi():
print(f"ComfyUI-OneAPI failed to load: {str(e)}")
# This function will be automatically called when ComfyUI loads the plugin
WEB_DIRECTORY = "./web"
NODE_CLASS_MAPPINGS = {}
NODE_DISPLAY_NAME_MAPPINGS = {}
+108
View File
@@ -0,0 +1,108 @@
import { app } from "../../scripts/app.js";
import { api } from "../../scripts/api.js";
const Swal = window.Sweetalert2;
function addCanvasMenuOptions() {
const original_getCanvasMenuOptions = LGraphCanvas.prototype.getCanvasMenuOptions;
LGraphCanvas.prototype.getCanvasMenuOptions = function () {
// get the basic options
const originOptions = original_getCanvasMenuOptions.apply(this, arguments);
const addedOptions = [
{
content: "T Load Default Workflow",
callback: async () => {
Swal.fire({
title: 'Hello',
text: 'Hello',
icon: 'success',
confirmButtonText: 'OK'
});
}
},
null
]
return [...addedOptions, ...originOptions];
// return originOptions;
}
}
function addExtraMenuOptions(nodeType, nodeData, app) {
const nodeTypeClass = nodeType?.comfyClass;
const original_getExtraMenuOptions = nodeType.prototype.getExtraMenuOptions;
nodeType.prototype.getExtraMenuOptions = function (_, options) {
original_getExtraMenuOptions?.apply(this, arguments);
options.splice(0, 0, {
content: "Set Node Input",
callback: async () => {
const selectedNodes = app.canvas.selected_nodes;
const selectedNodeIds = Object.keys(selectedNodes);
if (selectedNodeIds.length > 1) {
Swal.fire({
title: 'Error',
text: 'Please select only one node',
icon: 'error',
confirmButtonText: 'OK'
});
return;
}
const nodeId = selectedNodeIds[0];
const node = selectedNodes[nodeId];
const fieldNames = node.widgets.map(e => e.name);
const selectedFieldName = await Swal.fire({
title: 'Select Input Field',
input: 'select',
inputOptions: fieldNames,
inputPlaceholder: 'Select Input Field',
confirmButtonText: 'OK'
});
const fieldIndex = selectedFieldName.value;
if (fieldIndex !== "") {
const fieldName = fieldNames[fieldIndex];
const varName = await Swal.fire({
title: `Select Variable Name for ${fieldName}`,
input: 'text',
inputPlaceholder: 'Select Variable Name',
inputValidator: (value) => {
if (!value) {
return 'Please enter a variable name';
}
},
confirmButtonText: 'OK'
});
addVar2Node(node, varName.value, fieldName);
}
}
})
}
}
function addVar2Node(node, varName, fieldName) {
const newPart = `$${varName}.${fieldName}`;
// Split and remove existing items with the same fieldName
let parts = node.title.split(',').map(item => item.trim());
parts = parts.filter(item => {
// Match $xxx.fieldName
const match = item.match(/^\$[^.]+\.(.+)$/);
// Keep items that are not the current fieldName, or not in $xxx.fieldName format
return !(match && match[1] === fieldName);
});
// Add the new one
parts.push(newPart);
// Rejoin
node.title = parts.filter(Boolean).join(',');
}
app.registerExtension({
name: "ComfyUI-OneAPI-ConvertUIToAPI",
async setup() {
// Custom canvas background right-click menu
// addCanvasMenuOptions();
},
async beforeRegisterNodeDef(nodeType, nodeData, app) {
// Custom node right-click menu
addExtraMenuOptions(nodeType, nodeData, app);
},
})
File diff suppressed because one or more lines are too long