mirror of
https://github.com/scratchfoundation/paper.js.git
synced 2025-05-23 19:40:43 -04:00
Implement new strategy regarding Project#activeLayer:
- By default, no layer is created for any project. - Project#activeLayer is a getter for the hidden #_activeLayer property. When it is called and no layer exists, one is created on the fly. - Creating any other items creates a layer if none exists (was already the case).
This commit is contained in:
parent
ead2e8e49b
commit
6450430b68
4 changed files with 22 additions and 20 deletions
|
@ -86,8 +86,8 @@ var Layer = Group.extend(/** @lends Layer# */{
|
|||
return _remove.base.call(this, notify);
|
||||
if (this._index != null) {
|
||||
var project = this._project;
|
||||
if (project.activeLayer === this)
|
||||
project.activeLayer = this.getNextSibling()
|
||||
if (project._activeLayer === this)
|
||||
project._activeLayer = this.getNextSibling()
|
||||
|| this.getPreviousSibling();
|
||||
Base.splice(project.layers, null, this._index, 1);
|
||||
this._installEvents(false);
|
||||
|
@ -124,7 +124,7 @@ var Layer = Group.extend(/** @lends Layer# */{
|
|||
* console.log(project.activeLayer == firstLayer); // true
|
||||
*/
|
||||
activate: function() {
|
||||
this._project.activeLayer = this;
|
||||
this._project._activeLayer = this;
|
||||
},
|
||||
|
||||
// Private helper for #insertAbove() / #insertBelow()
|
||||
|
|
|
@ -53,9 +53,9 @@ var Project = PaperScopeItem.extend(/** @lends Project# */{
|
|||
// constructors.
|
||||
PaperScopeItem.call(this, true);
|
||||
this.layers = [];
|
||||
this._activeLayer = null;
|
||||
this.symbols = [];
|
||||
this._currentStyle = new Style(null, null, this);
|
||||
this.activeLayer = new Layer();
|
||||
// If no view is provided, we create a 1x1 px canvas view just so we
|
||||
// have something to do size calculations with.
|
||||
// (e.g. PointText#_getBounds)
|
||||
|
@ -98,15 +98,12 @@ var Project = PaperScopeItem.extend(/** @lends Project# */{
|
|||
},
|
||||
|
||||
/**
|
||||
* Checks whether the project has any content or not. Note that since
|
||||
* projects by default are created with one empty layer, this returns also
|
||||
* {@code true} if that layer exists but is itself empty.
|
||||
* Checks whether the project has any content or not.
|
||||
*
|
||||
* @return Boolean
|
||||
*/
|
||||
isEmpty: function() {
|
||||
return this.layers.length <= 1
|
||||
&& (!this.activeLayer || this.activeLayer.isEmpty());
|
||||
return this.layers.length === 0;
|
||||
},
|
||||
|
||||
/**
|
||||
|
@ -183,11 +180,11 @@ var Project = PaperScopeItem.extend(/** @lends Project# */{
|
|||
if (child instanceof Layer) {
|
||||
Base.splice(this.layers, [child]);
|
||||
// Also activate this layer if there was none before
|
||||
if (!this.activeLayer)
|
||||
this.activeLayer = child;
|
||||
if (!this._activeLayer)
|
||||
this._activeLayer = child;
|
||||
} else if (child instanceof Item) {
|
||||
// Anything else than layers needs to be added to a layer first
|
||||
(this.activeLayer
|
||||
(this._activeLayer
|
||||
// NOTE: If there is no layer and this project is not the active
|
||||
// one, passing insert: false and calling addChild on the
|
||||
// project will handle it correctly.
|
||||
|
@ -772,7 +769,7 @@ var Project = PaperScopeItem.extend(/** @lends Project# */{
|
|||
this.activate();
|
||||
// Provide the activeLayer as a possible target for layers, but only if
|
||||
// it's empty.
|
||||
var layer = this.activeLayer;
|
||||
var layer = this._activeLayer;
|
||||
return Base.importJSON(json, layer && layer.isEmpty() && layer);
|
||||
},
|
||||
|
||||
|
@ -815,7 +812,7 @@ var Project = PaperScopeItem.extend(/** @lends Project# */{
|
|||
*/
|
||||
|
||||
/**
|
||||
* {@grouptitle Project Hierarchy}
|
||||
* {@grouptitle Project Content}
|
||||
*
|
||||
* The layers contained within the project.
|
||||
*
|
||||
|
@ -827,9 +824,12 @@ var Project = PaperScopeItem.extend(/** @lends Project# */{
|
|||
* The layer which is currently active. New items will be created on this
|
||||
* layer by default.
|
||||
*
|
||||
* @name Project#activeLayer
|
||||
* @type Layer
|
||||
* @bean
|
||||
*/
|
||||
getActiveLayer: function() {
|
||||
return this._activeLayer || new Layer({ project: this });
|
||||
},
|
||||
|
||||
/**
|
||||
* The symbols contained within the project.
|
||||
|
|
|
@ -45,18 +45,20 @@ test('Item#matches()', function() {
|
|||
});
|
||||
|
||||
test('Project#getItems()', function() {
|
||||
var layer = new Layer();
|
||||
|
||||
equals(function() {
|
||||
var matches = paper.project.getItems({
|
||||
type: 'layer'
|
||||
});
|
||||
return matches.length == 1 && matches[0] == paper.project.activeLayer;
|
||||
return matches.length == 1 && matches[0] == layer;
|
||||
}, true);
|
||||
|
||||
equals(function() {
|
||||
var matches = paper.project.getItems({
|
||||
'class': Item
|
||||
class: Item
|
||||
});
|
||||
return matches.length == 1 && matches[0] == paper.project.activeLayer;
|
||||
return matches.length == 1 && matches[0] == layer;
|
||||
}, true);
|
||||
|
||||
var path = new Path();
|
||||
|
@ -69,12 +71,11 @@ test('Project#getItems()', function() {
|
|||
|
||||
equals(function() {
|
||||
var matches = paper.project.getItems({
|
||||
'constructor': Path
|
||||
constructor: Path
|
||||
});
|
||||
return matches.length == 1 && matches[0] === path;
|
||||
}, true);
|
||||
|
||||
|
||||
var group = new Group();
|
||||
equals(function() {
|
||||
var matches = paper.project.getItems({
|
||||
|
|
|
@ -29,6 +29,7 @@ test('clear()', function() {
|
|||
var project = new Project();
|
||||
new Layer();
|
||||
new Layer();
|
||||
new Layer();
|
||||
equals(project.layers.length, 3);
|
||||
project.clear();
|
||||
equals(project.layers.length, 0);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue