From 0a5d7c21003f325ff88e252b9e32b34090640659 Mon Sep 17 00:00:00 2001 From: Nick O'Leary Date: Fri, 1 Jul 2022 10:31:50 +0100 Subject: [PATCH] Update add-junction menu to work in more cases --- .../editor-client/src/js/ui/contextMenu.js | 41 +++++++++++++++++-- 1 file changed, 37 insertions(+), 4 deletions(-) diff --git a/packages/node_modules/@node-red/editor-client/src/js/ui/contextMenu.js b/packages/node_modules/@node-red/editor-client/src/js/ui/contextMenu.js index 0b388aff3..a8b787ba5 100644 --- a/packages/node_modules/@node-red/editor-client/src/js/ui/contextMenu.js +++ b/packages/node_modules/@node-red/editor-client/src/js/ui/contextMenu.js @@ -43,7 +43,16 @@ RED.contextMenu = (function() { const isGroup = hasSelection && selection.nodes.length === 1 && selection.nodes[0].type === 'group' const canRemoveFromGroup = hasSelection && !!selection.nodes[0].g + const offset = $("#red-ui-workspace-chart").offset() + let addX = options.x - offset.left + $("#red-ui-workspace-chart").scrollLeft() + let addY = options.y - offset.top + $("#red-ui-workspace-chart").scrollTop() + + if (RED.view.snapGrid) { + const gridSize = RED.view.gridSize() + addX = gridSize*Math.floor(addX/gridSize) + addY = gridSize*Math.floor(addY/gridSize) + } const menuItems = [ { onselect: 'core:show-action-list', onpostselect: function() {} }, @@ -54,17 +63,42 @@ RED.contextMenu = (function() { label: RED._("contextMenu.node"), onselect: function() { RED.view.showQuickAddDialog({ - position: [ options.x - offset.left, options.y - offset.top ], + position: [ addX, addY ], touchTrigger: true, splice: isSingleLink?selection.links[0]:undefined, // spliceMultiple: isMultipleLinks }) } }, - { + ( hasSelection || hasLinks ) ? { label: RED._("contextMenu.junction"), onselect: 'core:split-wires-with-junctions', - disabled: hasSelection || !hasLinks + disabled: !hasLinks + } : { + label: RED._("contextMenu.junction"), + onselect: function() { + const nn = { + _def: {defaults:{}}, + type: 'junction', + z: RED.workspaces.active(), + id: RED.nodes.id(), + x: addX, + y: addY, + w: 0, h: 0, + outputs: 1, + inputs: 1, + dirty: true + } + const historyEvent = { + dirty: RED.nodes.dirty(), + t:'add', + junctions:[nn] + } + RED.nodes.addJunction(nn); + RED.history.push(historyEvent); + RED.nodes.dirty(true); + RED.view.redraw(true) + } }, { label: RED._("contextMenu.linkNodes"), @@ -118,7 +152,6 @@ RED.contextMenu = (function() { } } - const offset = $("#red-ui-workspace-chart").offset() menu = RED.menu.init({ direction: 'right', onpreselect: function() {