fix(modeling): properly cascade label deletion

Ensure we execute the following nicely:

* unset name -> remove label shape
* remove label shape -> unset name
This commit is contained in:
Nico Rehwaldt 2018-06-08 16:01:40 +02:00 committed by Philipp Fromme
parent 8bb34dacb2
commit a7a1743df0
4 changed files with 54 additions and 21 deletions

View File

@ -54,7 +54,7 @@ export default function UpdateLabelHandler(modeling, textRenderer) {
if (!isLabel(element) if (!isLabel(element)
&& isLabelExternal(element) && isLabelExternal(element)
&& !hasExternalLabel(element) && !hasExternalLabel(element)
&& newLabel !== '') { && !isEmptyText(newLabel)) {
// create label // create label
var paddingTop = 7; var paddingTop = 7;
@ -86,10 +86,14 @@ export default function UpdateLabelHandler(modeling, textRenderer) {
var element = ctx.element, var element = ctx.element,
label = element.label || element, label = element.label || element,
newLabel = ctx.newLabel, newLabel = ctx.newLabel,
newBounds = ctx.newBounds; newBounds = ctx.newBounds,
hints = ctx.hints || {};
if (isLabel(label) && newLabel.trim() === '') { if (isLabel(label) && isEmptyText(newLabel)) {
modeling.removeShape(label);
if (hints.removeShape !== false) {
modeling.removeShape(label, { unsetLabel: false });
}
return; return;
} }
@ -131,4 +135,11 @@ export default function UpdateLabelHandler(modeling, textRenderer) {
UpdateLabelHandler.$inject = [ UpdateLabelHandler.$inject = [
'modeling', 'modeling',
'textRenderer' 'textRenderer'
]; ];
// helpers ///////////////////////
function isEmptyText(label) {
return !label || !label.trim();
}

View File

@ -58,11 +58,12 @@ Modeling.prototype.getHandlers = function() {
}; };
Modeling.prototype.updateLabel = function(element, newLabel, newBounds) { Modeling.prototype.updateLabel = function(element, newLabel, newBounds, hints) {
this._commandStack.execute('element.updateLabel', { this._commandStack.execute('element.updateLabel', {
element: element, element: element,
newLabel: newLabel, newLabel: newLabel,
newBounds: newBounds newBounds: newBounds,
hints: hints || {}
}); });
}; };

View File

@ -89,11 +89,12 @@ export default function LabelBehavior(
// update label after label shape was deleted // update label after label shape was deleted
this.postExecute('shape.delete', function(event) { this.postExecute('shape.delete', function(event) {
var context = event.context, var context = event.context,
shape = context; labelTarget = context.labelTarget,
hints = context.hints || {};
// check if label // check if label
if (shape.labelTarget) { if (labelTarget && hints.unsetLabel !== false) {
modeling.updateLabel(shape.labelTarget, ''); modeling.updateLabel(labelTarget, '', null, { removeShape: false });
} }
}); });

View File

@ -50,20 +50,40 @@ describe('features/modeling - update label', function() {
)); ));
it('should delete label when setting empty string', inject( describe('should delete label', function() {
function(modeling, elementRegistry) {
// given it('when setting null', inject(
var startEvent_1 = elementRegistry.get('StartEvent_1'); function(modeling, elementRegistry) {
// when // given
modeling.updateLabel(startEvent_1, ''); var startEvent_1 = elementRegistry.get('StartEvent_1');
// then // when
expect(startEvent_1.businessObject.name).to.equal(''); modeling.updateLabel(startEvent_1, null);
expect(startEvent_1.label).not.to.exist;
} // then
)); expect(startEvent_1.businessObject.name).not.to.exist;
expect(startEvent_1.label).not.to.exist;
}
));
it('when setting empty string', inject(
function(modeling, elementRegistry) {
// given
var startEvent_1 = elementRegistry.get('StartEvent_1');
// when
modeling.updateLabel(startEvent_1, '');
// then
expect(startEvent_1.businessObject.name).to.equal('');
expect(startEvent_1.label).not.to.exist;
}
));
});
it('should change name of start event when editing label', inject( it('should change name of start event when editing label', inject(