From 0f39cfe403d7567554127acbb97e0291b700a382 Mon Sep 17 00:00:00 2001 From: Sammy Franklin Date: Fri, 6 Oct 2023 20:29:48 -0700 Subject: [PATCH] created Export option for nodes --- web/lib/litegraph.core.js | 68 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 68 insertions(+) diff --git a/web/lib/litegraph.core.js b/web/lib/litegraph.core.js index f81c83a8a..df07cc220 100644 --- a/web/lib/litegraph.core.js +++ b/web/lib/litegraph.core.js @@ -12949,6 +12949,69 @@ LGraphNode.prototype.executeAction = function(action) return false; }; + LGraphCanvas.onMenuNodeExport = function(value, options, e, menu, node) { + if (!node) { + throw "no node passed"; + } + + const getOption = ({ name, type }, pinType) => { + const color = LGraphCanvas.link_type_colors[type]; + const innerHtml = `${name}`; + return { + name, + type: pinType, + content: innerHtml + }; + }; + + let inputs = node.inputs ?? []; + let outputs = node.outputs ?? []; + if (node.properties?.exports?.inputs) { + inputs = inputs.filter(({name}) => !node.properties.exports.inputs.includes(name)); + } + if (node.properties?.exports?.outputs) { + outputs = outputs.filter(({name}) => !node.properties.exports.outputs.includes(name)); + } + + const exportableVars = [ + ...inputs.map((node) => getOption(node, "input")), + null, + ...outputs.map((node) => getOption(node, "output")), + ]; + new LiteGraph.ContextMenu(exportableVars, { + event: e, + callback: inner_clicked, + parentMenu: menu, + node: node + }); + + function inner_clicked(v) { + if (!node) { + return; + } + node.graph.beforeChange(/*?*/); //node + + const exportVar = () => { + if (!node.properties.exports) { + node.properties.exports = { inputs: [], outputs: [] }; + } + + console.log(v); + if (v.type == "input") { + node.properties.exports.inputs.push(v.name); + } else if (v.type == "output") { + node.properties.exports.outputs.push(v.name); + } + }; + exportVar(); + + node.graph.afterChange(/*?*/); //node + node.setDirtyCanvas(true); + } + + return false; + }; + LGraphCanvas.onMenuNodeRemove = function(value, options, e, menu, node) { if (!node) { throw "no node passed"; @@ -13158,6 +13221,11 @@ LGraphNode.prototype.executeAction = function(action) has_submenu: true, callback: LGraphCanvas.onMenuNodeShapes }, + { + content: "Export", + has_submenu: true, + callback: LGraphCanvas.onMenuNodeExport + }, null ); }