Skip to content

Commit

Permalink
fix(create): cancel create on <elements.changed>
Browse files Browse the repository at this point in the history
  • Loading branch information
philippfromme authored and fake-join[bot] committed Oct 14, 2019
1 parent fd24592 commit 6ebd3a5
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 1 deletion.
25 changes: 24 additions & 1 deletion lib/features/create/Create.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ import {

import { getBBox } from '../../util/Elements';

var PREFIX = 'create';

var HIGH_PRIORITY = 2000;


/**
* Create new elements through drag and drop.
Expand Down Expand Up @@ -224,6 +228,25 @@ export default function Create(
});
});

function cancel() {
var context = dragging.context();

if (context && context.prefix === PREFIX) {
dragging.cancel();
}
}

// cancel on <elements.changed> that is not result of <drag.end>
eventBus.on('elements.changed', cancel);

eventBus.on('create.end', HIGH_PRIORITY, function() {
eventBus.off('elements.changed', cancel);
});

eventBus.on('create.ended', function() {
eventBus.on('elements.changed', cancel);
});

// API //////////

this.start = function(event, elements, context) {
Expand Down Expand Up @@ -277,7 +300,7 @@ export default function Create(
});
});

dragging.init(event, 'create', {
dragging.init(event, PREFIX, {
cursor: 'grabbing',
autoActivate: true,
data: {
Expand Down
15 changes: 15 additions & 0 deletions test/spec/features/create/CreateSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,21 @@ describe('features/create - Create', function() {
expect(context.shape).to.equal(newShape2);
}));


it('should cancel on <elements.changed>', inject(
function(create, dragging, elementRegistry, eventBus) {

// given
create.start(canvasEvent({ x: 0, y: 0 }), newShape);

// when
eventBus.fire('elements.changed', { elements: [] });

// then
expect(dragging.context()).not.to.exist;
}
));

});


Expand Down

0 comments on commit 6ebd3a5

Please sign in to comment.