Skip to content

Commit

Permalink
feat(canvas): allow passing planes descriptors
Browse files Browse the repository at this point in the history
  • Loading branch information
marstamm committed Nov 30, 2021
1 parent 739785a commit eecc65d
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 18 deletions.
39 changes: 22 additions & 17 deletions lib/core/Canvas.js
Original file line number Diff line number Diff line change
Expand Up @@ -369,38 +369,43 @@ Canvas.prototype.getPlane = function(name) {
* Creates a plane that is used to draw elements on it. If no
* root element is provided, an implicit root will be used.
*
* @param {string} name
* @param {string|Object} plane
* @param {Object|djs.model.Root} [rootElement] optional root element
*
* @return {Object} plane descriptor with { layer, rootElement, name }
*/
Canvas.prototype.createPlane = function(name, rootElement) {
if (!name) {
Canvas.prototype.createPlane = function(plane, rootElement) {
if (!plane) {
throw new Error('must specify a name');
}

if (this._planes[name]) {
throw new Error('plane <' + name + '> already exists');
if (this._planes[plane] || this._planes[plane.name]) {
throw new Error('plane <' + plane + '> already exists');
}

if (typeof plane === 'string') {
plane = { name: plane };
}

if (!rootElement) {
rootElement = {
id: '__implicitroot' + name,
if (!plane.layer) {
var svgLayer = this.getLayer(plane.name, PLANE_LAYER_INDEX);
svgAttr(svgLayer, 'display', 'none');

plane.layer = svgLayer;
}

if (!plane.rootElement) {
rootElement = rootElement || {
id: '__implicitroot' + plane.name,
children: [],
isImplicit: true
};
}

var svgLayer = this.getLayer(name, PLANE_LAYER_INDEX);
svgAttr(svgLayer, 'display', 'none');

var plane = this._planes[name] = {
layer: svgLayer,
name: name,
rootElement: null
};
this.setRootElementForPlane(rootElement, plane);
}

this.setRootElementForPlane(rootElement, plane);
this._planes[plane.name] = plane;

return plane;
};
Expand Down
38 changes: 37 additions & 1 deletion test/spec/core/CanvasSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -2413,7 +2413,43 @@ describe('Canvas', function() {

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


it('should accept a plane descriptor', inject(function(canvas) {

// given
var plane = {
name: 'a',
rootElement: { id: 'root' },
layer: canvas.getLayer('a')
};

// when
canvas.createPlane(plane);

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


it('should fill missing plane attributes', inject(function(canvas) {

// given
var plane = {
name: 'a'
};

// when
canvas.createPlane(plane);

// then
expect(canvas.getPlane('a')).to.exist;
expect(canvas.getPlane('a')).to.equal(plane);
expect(plane.rootElement).to.exist;
expect(plane.layer).to.exist;
}));


Expand Down

0 comments on commit eecc65d

Please sign in to comment.