From 44cceb5da287a0ad01d9389f475284c88eda7f7b Mon Sep 17 00:00:00 2001 From: Philipp Fromme Date: Fri, 11 Oct 2019 11:22:51 +0200 Subject: [PATCH] fix(adaptive-label-positioning): ignore labels that are being created Closes #1211 --- .../AdaptiveLabelPositioningBehavior.js | 6 +- .../AdaptiveLabelPositioningBehaviorSpec.js | 57 ++++++++++++++++++- 2 files changed, 59 insertions(+), 4 deletions(-) diff --git a/lib/features/modeling/behavior/AdaptiveLabelPositioningBehavior.js b/lib/features/modeling/behavior/AdaptiveLabelPositioningBehavior.js index cd8b64bab6..adaadf96c8 100644 --- a/lib/features/modeling/behavior/AdaptiveLabelPositioningBehavior.js +++ b/lib/features/modeling/behavior/AdaptiveLabelPositioningBehavior.js @@ -84,6 +84,11 @@ export default function AdaptiveLabelPositioningBehavior(eventBus, modeling) { label = element.label, labelMid = getMid(label); + // ignore labels that are being created + if (!label.parent) { + return; + } + var elementTrbl = asTRBL(element); var newLabelMid; @@ -125,7 +130,6 @@ export default function AdaptiveLabelPositioningBehavior(eventBus, modeling) { break; } - var delta = substract(newLabelMid, labelMid); modeling.moveShape(label, delta); diff --git a/test/spec/features/modeling/behavior/AdaptiveLabelPositioningBehaviorSpec.js b/test/spec/features/modeling/behavior/AdaptiveLabelPositioningBehaviorSpec.js index e972d736b7..cea217aab0 100644 --- a/test/spec/features/modeling/behavior/AdaptiveLabelPositioningBehaviorSpec.js +++ b/test/spec/features/modeling/behavior/AdaptiveLabelPositioningBehaviorSpec.js @@ -3,13 +3,16 @@ import { inject } from 'test/TestHelper'; -import { - getOrientation -} from 'diagram-js/lib/layout/LayoutUtil'; +import { getOrientation } from 'diagram-js/lib/layout/LayoutUtil'; import modelingModule from 'lib/features/modeling'; import coreModule from 'lib/core'; +import { + DEFAULT_LABEL_SIZE, + getExternalLabelMid +} from 'lib/util/LabelUtil'; + var testModules = [ modelingModule, coreModule @@ -281,6 +284,45 @@ describe('modeling/behavior - AdaptiveLabelPositioningBehavior', function() { } )); + + it('should not adjust position', inject(function(bpmnFactory, elementFactory, elementRegistry, modeling, textRenderer) { + + // given + var sequenceFlow = elementRegistry.get('SequenceFlow_1'); + + var intermediateThrowEvent = elementFactory.createShape({ + businessObject: bpmnFactory.create('bpmn:IntermediateThrowEvent', { + name: 'Foo' + }), + type: 'bpmn:IntermediateThrowEvent', + x: 0, + y: 0 + }); + + var externalLabelMid = getExternalLabelMid(intermediateThrowEvent); + + var externalLabelBounds = textRenderer.getExternalLabelBounds(DEFAULT_LABEL_SIZE, 'Foo'); + + var label = elementFactory.createLabel({ + labelTarget: intermediateThrowEvent, + x: externalLabelMid.x - externalLabelBounds.width / 2, + y: externalLabelMid.y - externalLabelBounds.height / 2, + width: externalLabelBounds.width, + height: externalLabelBounds.height + }); + + var sequenceFlowMid = getConnectionMid(sequenceFlow.waypoints[0], sequenceFlow.waypoints[1]); + + // when + modeling.createElements([ intermediateThrowEvent, label ], sequenceFlowMid, sequenceFlow); + + // then + expect(label.x).to.be.closeTo(325, 1); + expect(label.y).to.be.closeTo(335, 1); + expect(label.width).to.be.closeTo(19, 1); + expect(label.height).to.be.closeTo(14, 1); + })); + }); }); @@ -407,3 +449,12 @@ describe('modeling/behavior - AdaptiveLabelPositioningBehavior', function() { }); }); + +// helpers ////////// + +function getConnectionMid(a, b) { + return { + x: (a.x + b.x) / 2, + y: (a.y + b.y) / 2 + }; +} \ No newline at end of file