Allow suggestion to be automatically positioned relative to source

This commit is contained in:
Nick O'Leary
2025-06-24 14:10:02 +01:00
parent d95af6acd6
commit 412d47b303

View File

@@ -6536,6 +6536,24 @@ RED.view = (function() {
if (suggestion?.nodes?.length > 0) {
const nodeMap = {}
const links = []
const positionOffset = { x: 0, y: 0 }
if (suggestion.source && suggestion.position === 'relative') {
// If the suggestion is relative to a source node, use its position plus a suitable offset
let targetX = suggestion.source.x + (suggestion.source.w || 120) / 2 + 77
const targetY = suggestion.source.y
// Keep targetY where it is, but ensure targetX is grid aligned
if (snapGrid) {
// This isn't a perfect grid snap, as we don't have the true node width at this point.
// TODO: defer grid snapping until the node is created?
const gridOffset = RED.view.tools.calculateGridSnapOffsets({ x: targetX, y: targetY, w: node_width, h: node_height });
targetX += gridOffset.x
}
positionOffset.x = targetX - (suggestion.nodes[0].x || 0)
positionOffset.y = targetY - (suggestion.nodes[0].y || 0)
}
suggestion.nodes.forEach(nodeConfig => {
if (!nodeConfig.type || nodeConfig.type === 'group' || nodeConfig.type === 'subflow' || nodeConfig.type === 'tab') {
// A node type we don't support previewing
@@ -6543,8 +6561,9 @@ RED.view = (function() {
}
let node
if (nodeConfig.type === 'junction') {
nodeConfig.x = (nodeConfig.x || 0) + positionOffset.x
nodeConfig.y = (nodeConfig.y || 0) + positionOffset.y
node = {
_def: {defaults:{}},
type: 'junction',
@@ -6565,6 +6584,8 @@ RED.view = (function() {
// TODO: unknown node types could happen...
return
}
nodeConfig.x = (nodeConfig.x || 0) + positionOffset.x
nodeConfig.y = (nodeConfig.y || 0) + positionOffset.y
const result = createNode(nodeConfig.type, nodeConfig.x, nodeConfig.y)
if (!result) {
return