From 62d7746e81e8b61eff31b20daeb516834f07afd8 Mon Sep 17 00:00:00 2001 From: Philipp Fromme Date: Fri, 10 May 2019 11:28:15 +0200 Subject: [PATCH] feat(bpmn-snapping): snap shape to itself Closes #993 --- lib/features/snapping/BpmnSnapping.js | 8 +++- .../snapping/BpmnSnapping.general.bpmn | 38 +++++++++++++++ .../features/snapping/BpmnSnappingSpec.js | 46 +++++++++++++++++++ 3 files changed, 91 insertions(+), 1 deletion(-) create mode 100644 test/spec/features/snapping/BpmnSnapping.general.bpmn diff --git a/lib/features/snapping/BpmnSnapping.js b/lib/features/snapping/BpmnSnapping.js index 14c1e1a6..4920678d 100644 --- a/lib/features/snapping/BpmnSnapping.js +++ b/lib/features/snapping/BpmnSnapping.js @@ -1,7 +1,8 @@ import inherits from 'inherits'; import { - forEach + forEach, + isNumber } from 'min-dash'; import { @@ -316,6 +317,11 @@ BpmnSnapping.prototype.initSnap = function(event) { BpmnSnapping.prototype.addTargetSnaps = function(snapPoints, shape, target) { + // snap shape to itself + if (isNumber(shape.x) && isNumber(shape.y)) { + snapPoints.add('mid', mid(shape)); + } + // use target parent as snap target if (is(shape, 'bpmn:BoundaryEvent') && shape.type !== 'label') { target = target.parent; diff --git a/test/spec/features/snapping/BpmnSnapping.general.bpmn b/test/spec/features/snapping/BpmnSnapping.general.bpmn new file mode 100644 index 00000000..0f48812e --- /dev/null +++ b/test/spec/features/snapping/BpmnSnapping.general.bpmn @@ -0,0 +1,38 @@ + + + + + SequenceFlow_1 + + + SequenceFlow_1 + SequenceFlow_2 + + + + SequenceFlow_2 + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/test/spec/features/snapping/BpmnSnappingSpec.js b/test/spec/features/snapping/BpmnSnappingSpec.js index 14fab963..1fd79ca4 100644 --- a/test/spec/features/snapping/BpmnSnappingSpec.js +++ b/test/spec/features/snapping/BpmnSnappingSpec.js @@ -29,6 +29,52 @@ describe('features/snapping - BpmnSnapping', function() { connectModule ]; + describe('general', function() { + + var diagramXML = require('./BpmnSnapping.general.bpmn'); + + beforeEach(bootstrapModeler(diagramXML, { + modules: testModules + })); + + var task; + + beforeEach(inject(function(dragging, elementRegistry) { + task = elementRegistry.get('Task_1'); + + dragging.setOptions({ manual: true }); + })); + + + it('should snap to itself', inject(function(canvas, dragging, move) { + + // given + var originalPosition = { + x: task.x, + y: task.y + }; + + var taskGfx = canvas.getGraphics(task); + + // when + move.start(canvasEvent(originalPosition), task); + + dragging.hover({ element: task, gfx: taskGfx }); + + dragging.move(canvasEvent({ + x: originalPosition.x + 5, + y: originalPosition.y + 5 + })); + + dragging.end(); + + // then + expect(task.x).to.eql(originalPosition.x); + expect(task.y).to.eql(originalPosition.y); + })); + + }); + describe('on Boundary Events', function() { var diagramXML = require('../../../fixtures/bpmn/collaboration/process.bpmn');