Skip to content

Commit 89613ce

Browse files
committed
1 parent f6787d6 commit 89613ce

File tree

1 file changed

+128
-58
lines changed

1 file changed

+128
-58
lines changed

packages/project-editor/flow/editor/mouse-handler.tsx

+128-58
Original file line numberDiff line numberDiff line change
@@ -371,57 +371,98 @@ export class DragMouseHandler extends MouseHandlerWithSnapLines {
371371
});
372372
}
373373

374-
getConnectionLineAlignedY(context: IFlowContext, event: IPointerEvent) {
374+
getConnectionLineAlignedY(
375+
context: IFlowContext,
376+
event: IPointerEvent,
377+
left: number,
378+
top: number
379+
) {
375380
if (!event.ctrlKey) {
376381
return undefined;
377382
}
378383

384+
const dxMouseDrag = left - this.selectionBoundingRectAtDown.left;
385+
const dyMouseDrag = top - this.selectionBoundingRectAtDown.top;
386+
379387
let foundConnectionLine;
380-
let foundDx = 0;
388+
let foundLength = 0;
381389

382390
for (const connectionLine of (context.document.flow.object as Flow)
383391
.connectionLines) {
392+
const sourceComponentIndex = this.selectedObjects.findIndex(
393+
selectedObject =>
394+
selectedObject.object == connectionLine.sourceComponent
395+
);
396+
397+
const targetComponentIndex = this.selectedObjects.findIndex(
398+
selectedObject =>
399+
selectedObject.object == connectionLine.targetComponent
400+
);
401+
384402
if (
385-
this.selectedObjects.find(
386-
selectedObject =>
387-
selectedObject.object == connectionLine.sourceComponent
388-
)
403+
(sourceComponentIndex != -1 && targetComponentIndex != -1) ||
404+
(sourceComponentIndex == -1 && targetComponentIndex == -1)
389405
) {
390-
// skip connection lines where source is among selected objects
391406
continue;
392407
}
393408

394-
const selectedObject = this.selectedObjects.find(
395-
selectedObject =>
396-
selectedObject.object == connectionLine.targetComponent
397-
);
398-
if (selectedObject) {
399-
const sourceOutputX =
400-
connectionLine.sourceComponent.geometry.left +
401-
connectionLine.sourceComponent.geometry.outputs[
402-
connectionLine.output
403-
].position.x;
404-
405-
const targetInputX =
406-
this.rects[
407-
this.selectedObjects.findIndex(
408-
selectedObject =>
409-
selectedObject.object ==
410-
connectionLine.targetComponent
411-
)
412-
].left +
413-
connectionLine.targetComponent.geometry.inputs[
414-
connectionLine.input
415-
].position.x;
409+
let sourceOutputX;
410+
let sourceOutputY;
411+
let targetInputX;
412+
let targetInputY;
413+
414+
const outputPos =
415+
connectionLine.sourceComponent.geometry.outputs[
416+
connectionLine.output
417+
].position;
418+
419+
const inputPos =
420+
connectionLine.targetComponent.geometry.inputs[
421+
connectionLine.input
422+
].position;
423+
424+
if (sourceComponentIndex != -1) {
425+
sourceOutputX =
426+
this.objectPositionsAtDown[sourceComponentIndex].x +
427+
dxMouseDrag +
428+
outputPos.x;
429+
430+
sourceOutputY =
431+
this.objectPositionsAtDown[sourceComponentIndex].y +
432+
dyMouseDrag +
433+
outputPos.y;
434+
435+
targetInputX =
436+
connectionLine.targetComponent.geometry.left + inputPos.x;
437+
438+
targetInputY =
439+
connectionLine.targetComponent.geometry.top + inputPos.y;
440+
} else {
441+
sourceOutputX =
442+
connectionLine.sourceComponent.geometry.left + outputPos.x;
443+
444+
sourceOutputY =
445+
connectionLine.sourceComponent.geometry.top + outputPos.y;
446+
447+
targetInputX =
448+
this.objectPositionsAtDown[targetComponentIndex].x +
449+
dxMouseDrag +
450+
inputPos.x;
451+
452+
targetInputY =
453+
this.objectPositionsAtDown[targetComponentIndex].y +
454+
dyMouseDrag +
455+
inputPos.y;
456+
}
416457

458+
if (sourceOutputX < targetInputX) {
417459
const dx = targetInputX - sourceOutputX;
460+
const dy = targetInputY - sourceOutputY;
461+
const length = dx * dx + dy * dy;
418462

419-
if (
420-
dx > 0 &&
421-
(!foundConnectionLine || Math.abs(dx) < Math.abs(foundDx))
422-
) {
463+
if (!foundConnectionLine || length < foundLength) {
423464
foundConnectionLine = connectionLine;
424-
foundDx = dx;
465+
foundLength = length;
425466
}
426467
}
427468
}
@@ -430,33 +471,60 @@ export class DragMouseHandler extends MouseHandlerWithSnapLines {
430471
return undefined;
431472
}
432473

433-
const topSource = foundConnectionLine.sourceComponent.top;
474+
const sourceComponentIndex = this.selectedObjects.findIndex(
475+
selectedObject =>
476+
selectedObject.object == foundConnectionLine.sourceComponent
477+
);
434478

435-
const topBounding = this.selectionBoundingRectAtDown.top;
436-
const topTarget =
437-
this.objectPositionsAtDown[
438-
this.selectedObjects.findIndex(
439-
selectedObject =>
440-
selectedObject.object ==
441-
foundConnectionLine.targetComponent
442-
)
443-
].y;
479+
if (sourceComponentIndex != -1) {
480+
const topTarget = foundConnectionLine.targetComponent.top;
481+
const topBounding = this.selectionBoundingRectAtDown.top;
482+
const topSource =
483+
this.objectPositionsAtDown[sourceComponentIndex].y;
484+
485+
const sourceOutputY =
486+
foundConnectionLine.sourceComponent.geometry.outputs[
487+
foundConnectionLine.output
488+
].position.y;
489+
490+
const targetInputY =
491+
foundConnectionLine.targetComponent.geometry.inputs[
492+
foundConnectionLine.input
493+
].position.y;
494+
495+
return (
496+
topTarget +
497+
(topBounding - topSource) -
498+
(sourceOutputY - targetInputY)
499+
);
500+
} else {
501+
const targetComponentIndex = this.selectedObjects.findIndex(
502+
selectedObject =>
503+
selectedObject.object == foundConnectionLine.targetComponent
504+
);
444505

445-
const sourceOutputY =
446-
foundConnectionLine.sourceComponent.geometry.outputs[
447-
foundConnectionLine.output
448-
].position.y;
506+
const topSource = foundConnectionLine.sourceComponent.top;
449507

450-
const targetInputY =
451-
foundConnectionLine.targetComponent.geometry.inputs[
452-
foundConnectionLine.input
453-
].position.y;
508+
const topBounding = this.selectionBoundingRectAtDown.top;
509+
const topTarget =
510+
this.objectPositionsAtDown[targetComponentIndex].y;
454511

455-
return (
456-
topSource +
457-
(topBounding - topTarget) -
458-
(targetInputY - sourceOutputY)
459-
);
512+
const sourceOutputY =
513+
foundConnectionLine.sourceComponent.geometry.outputs[
514+
foundConnectionLine.output
515+
].position.y;
516+
517+
const targetInputY =
518+
foundConnectionLine.targetComponent.geometry.inputs[
519+
foundConnectionLine.input
520+
].position.y;
521+
522+
return (
523+
topSource +
524+
(topBounding - topTarget) -
525+
(targetInputY - sourceOutputY)
526+
);
527+
}
460528
}
461529

462530
down(context: IFlowContext, event: IPointerEvent) {
@@ -500,7 +568,9 @@ export class DragMouseHandler extends MouseHandlerWithSnapLines {
500568

501569
const connectionLineAlignedY = this.getConnectionLineAlignedY(
502570
context,
503-
event
571+
event,
572+
left,
573+
top
504574
);
505575
if (connectionLineAlignedY) {
506576
top = connectionLineAlignedY;

0 commit comments

Comments
 (0)