Allow source node to be set in setSuggestedFlow api

This commit is contained in:
Nick O'Leary
2025-06-16 16:54:01 +01:00
parent 407d231cdd
commit 5ec075a49a
2 changed files with 47 additions and 4 deletions

View File

@@ -319,6 +319,9 @@ RED.typeSearch = (function() {
}
visible = true;
} else {
if (suggestCallback) {
suggestCallback(null);
}
dialog.hide();
searchResultsDiv.hide();
}

View File

@@ -6379,10 +6379,10 @@ RED.view = (function() {
nn.w = RED.view.node_width;
nn.h = Math.max(RED.view.node_height, (nn.outputs || 0) * 15);
nn.resize = true;
if (x != null && typeof x == "number" && x >= 0) {
if (x != null && typeof x == "number") {
nn.x = x;
}
if (y != null && typeof y == "number" && y >= 0) {
if (y != null && typeof y == "number") {
nn.y = y;
}
var historyEvent = {
@@ -6472,7 +6472,9 @@ RED.view = (function() {
* x: 0,
* y: 0,
* }
* ]
* ],
* "source": <sourceNode>,
* "sourcePort": <sourcePort>,
* }
* If `nodes` is a single node without an id property, it will be generated
* using its default properties.
@@ -6480,6 +6482,9 @@ RED.view = (function() {
* If `nodes` has multiple, they must all have ids and will be assumed to be 'importable'.
* In other words, a piece of valid flow json.
*
* `source`/`sourcePort` are option and used to indicate a node the suggestion should be connected to.
* If provided, a ghost wire will be added between the source and the first node in the suggestion.
*
* Limitations:
* - does not support groups, subflows or whole tabs
* - does not support config nodes
@@ -6579,6 +6584,15 @@ RED.view = (function() {
suggestedLinks.push(link)
}
})
if (suggestion.source && suggestedNodes[0]?._def?.inputs > 0) {
suggestedLinks.push({
source: suggestion.source,
sourcePort: suggestion.sourcePort || 0,
target: suggestedNodes[0],
targetPort: 0,
__ghost: true
})
}
}
if (ghostNode) {
if (suggestedNodes.length > 0) {
@@ -6599,14 +6613,40 @@ RED.view = (function() {
function applySuggestedFlow () {
if (currentSuggestion && currentSuggestion.nodes) {
const nodesToImport = currentSuggestion.nodes
const sourceNode = currentSuggestion.source
const sourcePort = currentSuggestion.sourcePort || 0
setSuggestedFlow(null)
return importNodes(nodesToImport, {
const result = importNodes(nodesToImport, {
generateIds: true,
touchImport: true,
notify: false,
// Ensure the node gets all of its defaults applied
applyNodeDefaults: true
})
if (sourceNode) {
const firstNode = result.nodeMap[nodesToImport[0].id]
if (firstNode && firstNode._def?.inputs > 0) {
// Connect the source node to the first node in the suggestion
const link = {
source: sourceNode,
target: RED.nodes.node(firstNode.id),
sourcePort: sourcePort,
targetPort: 0
};
RED.nodes.addLink(link)
const historyEvent = RED.history.peek();
if (historyEvent.t === "multi") {
historyEvent = historyEvent.events.find(e => e.t === "add")
}
if (historyEvent) {
historyEvent.links = historyEvent.links || [];
historyEvent.links.push(link);
}
RED.view.redraw(true);
}
}
return result
}
}