Skip to content

Commit fe22afe

Browse files
authored
Merge pull request node-red#4974 from node-red/4969-fix-junction-insert-location
Fix junction insert position via context menu
2 parents 69753a9 + e8d81d8 commit fe22afe

3 files changed

Lines changed: 35 additions & 28 deletions

File tree

packages/node_modules/@node-red/editor-client/src/js/ui/contextMenu.js

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -54,15 +54,15 @@ RED.contextMenu = (function () {
5454
}
5555
}
5656

57+
const scale = RED.view.scale()
5758
const offset = $("#red-ui-workspace-chart").offset()
58-
59-
let addX = options.x - offset.left + $("#red-ui-workspace-chart").scrollLeft()
60-
let addY = options.y - offset.top + $("#red-ui-workspace-chart").scrollTop()
59+
let addX = (options.x - offset.left + $("#red-ui-workspace-chart").scrollLeft()) / scale
60+
let addY = (options.y - offset.top + $("#red-ui-workspace-chart").scrollTop()) / scale
6161

6262
if (RED.view.snapGrid) {
6363
const gridSize = RED.view.gridSize()
64-
addX = gridSize * Math.floor(addX / gridSize)
65-
addY = gridSize * Math.floor(addY / gridSize)
64+
addX = gridSize * Math.round(addX / gridSize)
65+
addY = gridSize * Math.round(addY / gridSize)
6666
}
6767

6868
if (RED.settings.theme("menu.menu-item-action-list", true)) {
@@ -87,7 +87,9 @@ RED.contextMenu = (function () {
8787
},
8888
(hasLinks) ? { // has least 1 wire selected
8989
label: RED._("contextMenu.junction"),
90-
onselect: 'core:split-wires-with-junctions',
90+
onselect: function () {
91+
RED.actions.invoke('core:split-wires-with-junctions', { x: addX, y: addY })
92+
},
9193
disabled: !canEdit || !hasLinks
9294
} : {
9395
label: RED._("contextMenu.junction"),

packages/node_modules/@node-red/editor-client/src/js/ui/view-tools.js

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1154,11 +1154,11 @@ RED.view.tools = (function() {
11541154
}
11551155
}
11561156

1157-
function addJunctionsToWires(wires) {
1157+
function addJunctionsToWires(options = {}) {
11581158
if (RED.workspaces.isLocked()) {
11591159
return
11601160
}
1161-
let wiresToSplit = wires || (RED.view.selection().links && RED.view.selection().links.filter(e => !e.link));
1161+
let wiresToSplit = options.wires || (RED.view.selection().links && RED.view.selection().links.filter(e => !e.link));
11621162
if (!wiresToSplit) {
11631163
return
11641164
}
@@ -1206,21 +1206,26 @@ RED.view.tools = (function() {
12061206
if (links.length === 0) {
12071207
return
12081208
}
1209-
let pointCount = 0
1210-
links.forEach(function(l) {
1211-
if (l._sliceLocation) {
1212-
junction.x += l._sliceLocation.x
1213-
junction.y += l._sliceLocation.y
1214-
delete l._sliceLocation
1215-
pointCount++
1216-
} else {
1217-
junction.x += l.source.x + l.source.w/2 + l.target.x - l.target.w/2
1218-
junction.y += l.source.y + l.target.y
1219-
pointCount += 2
1220-
}
1221-
})
1222-
junction.x = Math.round(junction.x/pointCount)
1223-
junction.y = Math.round(junction.y/pointCount)
1209+
if (addedJunctions.length === 0 && Object.hasOwn(options, 'x') && Object.hasOwn(options, 'y')) {
1210+
junction.x = options.x
1211+
junction.y = options.y
1212+
} else {
1213+
let pointCount = 0
1214+
links.forEach(function(l) {
1215+
if (l._sliceLocation) {
1216+
junction.x += l._sliceLocation.x
1217+
junction.y += l._sliceLocation.y
1218+
delete l._sliceLocation
1219+
pointCount++
1220+
} else {
1221+
junction.x += l.source.x + l.source.w/2 + l.target.x - l.target.w/2
1222+
junction.y += l.source.y + l.target.y
1223+
pointCount += 2
1224+
}
1225+
})
1226+
junction.x = Math.round(junction.x/pointCount)
1227+
junction.y = Math.round(junction.y/pointCount)
1228+
}
12241229
if (RED.view.snapGrid) {
12251230
let gridSize = RED.view.gridSize()
12261231
junction.x = (gridSize*Math.round(junction.x/gridSize));
@@ -1410,7 +1415,7 @@ RED.view.tools = (function() {
14101415
RED.actions.add("core:wire-multiple-to-node", function() { wireMultipleToNode() })
14111416

14121417
RED.actions.add("core:split-wire-with-link-nodes", function () { splitWiresWithLinkNodes() });
1413-
RED.actions.add("core:split-wires-with-junctions", function () { addJunctionsToWires() });
1418+
RED.actions.add("core:split-wires-with-junctions", function (options) { addJunctionsToWires(options) });
14141419

14151420
RED.actions.add("core:generate-node-names", generateNodeNames )
14161421

packages/node_modules/@node-red/editor-client/src/js/ui/view.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -321,8 +321,8 @@ RED.view = (function() {
321321
evt.stopPropagation()
322322
RED.contextMenu.show({
323323
type: 'workspace',
324-
x:evt.clientX-5,
325-
y:evt.clientY-5
324+
x: evt.clientX,
325+
y: evt.clientY
326326
})
327327
return false
328328
})
@@ -5174,8 +5174,8 @@ RED.view = (function() {
51745174
var delta = Infinity;
51755175
for (var i = 0; i < lineLength; i++) {
51765176
var linePos = pathLine.getPointAtLength(i);
5177-
var posDeltaX = Math.abs(linePos.x-d3.event.offsetX)
5178-
var posDeltaY = Math.abs(linePos.y-d3.event.offsetY)
5177+
var posDeltaX = Math.abs(linePos.x-(d3.event.offsetX / scaleFactor))
5178+
var posDeltaY = Math.abs(linePos.y-(d3.event.offsetY / scaleFactor))
51795179
var posDelta = posDeltaX*posDeltaX + posDeltaY*posDeltaY
51805180
if (posDelta < delta) {
51815181
pos = linePos

0 commit comments

Comments
 (0)