Skip to content

Commit

Permalink
feat(canvas): allow removing planes
Browse files Browse the repository at this point in the history
  • Loading branch information
marstamm authored and fake-join[bot] committed Nov 30, 2021
1 parent 06f4745 commit 3244df4
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 7 deletions.
36 changes: 33 additions & 3 deletions lib/core/Canvas.js
Original file line number Diff line number Diff line change
Expand Up @@ -467,9 +467,13 @@ Canvas.prototype.getActiveLayer = function() {
*/
Canvas.prototype.getActivePlane = function() {
var plane = this._activePlane;

if (!plane) {
plane = this.createPlane(BASE_LAYER);
this.setActivePlane(BASE_LAYER);
if (!this.getPlane(BASE_LAYER)) {
this.createPlane(BASE_LAYER);
}

plane = this.setActivePlane(BASE_LAYER);
}

return plane;
Expand All @@ -496,7 +500,33 @@ Canvas.prototype.findPlane = function(element) {


/**
* Renames the given plane
* Removes the given plane.
*
* @param {string|djs.model.Base} plane
*
* @returns {Object} removed plane
*/
Canvas.prototype.removePlane = function(plane) {
if (typeof plane === 'string') {
plane = this.getPlane(plane);
}

if (!plane) {
throw new Error('must specify a plane');
}

delete this._planes[plane.name];

if (plane === this._activePlane) {
this._activePlane = null;
}

return plane;
};


/**
* Renames the given plane.
*
* @param {string|djs.model.Base} plane
* @param {string} newName
Expand Down
63 changes: 59 additions & 4 deletions test/spec/core/CanvasSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -2337,6 +2337,61 @@ describe('Canvas', function() {
});


describe('#removePlane', function() {

it('should expect a name', inject(function(canvas) {

expect(function() {

// when
canvas.removePlane();
}).to.throw('must specify a plane');
}));


it('should accept names', inject(function(canvas) {

// given
canvas.createPlane('a');

// when
canvas.removePlane('a');

// then
expect(canvas.getPlane('a')).not.to.exist;
}));


it('should remove active plane', inject(function(canvas) {

// given
canvas.createPlane('a');
canvas.setActivePlane('a');

// when
canvas.removePlane('a');

// then
expect(canvas._activePlane).to.not.exist;
}));


it('should default to base plane after active plane is removed', inject(function(canvas) {

// given
canvas.createPlane('a');
canvas.setActivePlane('a');

// when
canvas.removePlane('a');

// then
expect(canvas.getActivePlane().name).to.equal('base');
}));

});


describe('#renamePlane', function() {

it('should expect a plane', inject(function(canvas) {
Expand Down Expand Up @@ -2407,7 +2462,7 @@ describe('Canvas', function() {
}));


it('should not remove canvas data-element-id', inject(function(canvas) {
it('should not remove canvas data-element-id', inject(function(canvas, elementRegistry) {

// given
var rootA = { id: 'A' };
Expand All @@ -2423,6 +2478,7 @@ describe('Canvas', function() {

// then
expect(svgAttr(canvas._svg, 'data-element-id')).to.equal('A');
expect(elementRegistry.get(canvas._svg)).to.equal(rootA);
}));


Expand Down Expand Up @@ -2530,15 +2586,15 @@ describe('Canvas', function() {
// assume
expect(elementRegistry.getGraphics('A', true)).to.exist;
expect(elementRegistry.getGraphics('B', true)).to.not.exist;
expect(svgAttr(canvas._svg, 'data-element-id')).to.equal('A');
expect(elementRegistry.get(canvas._svg)).to.equal(rootA);

// when
canvas.setActivePlane('b');

// then
expect(elementRegistry.getGraphics('A', true)).to.not.exist;
expect(elementRegistry.getGraphics('B', true)).to.exist;
expect(svgAttr(canvas._svg, 'data-element-id')).to.equal('B');
expect(elementRegistry.get(canvas._svg)).to.equal(rootB);
}));


Expand Down Expand Up @@ -2628,7 +2684,6 @@ describe('Canvas', function() {
});



describe('#findPlane', function() {

it('should return the correct plane', inject(function(canvas) {
Expand Down

0 comments on commit 3244df4

Please sign in to comment.