mirror of
https://github.com/scratchfoundation/paper.js.git
synced 2025-06-05 17:54:11 -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);
|
return _remove.base.call(this, notify);
|
||||||
if (this._index != null) {
|
if (this._index != null) {
|
||||||
var project = this._project;
|
var project = this._project;
|
||||||
if (project.activeLayer === this)
|
if (project._activeLayer === this)
|
||||||
project.activeLayer = this.getNextSibling()
|
project._activeLayer = this.getNextSibling()
|
||||||
|| this.getPreviousSibling();
|
|| this.getPreviousSibling();
|
||||||
Base.splice(project.layers, null, this._index, 1);
|
Base.splice(project.layers, null, this._index, 1);
|
||||||
this._installEvents(false);
|
this._installEvents(false);
|
||||||
|
@ -124,7 +124,7 @@ var Layer = Group.extend(/** @lends Layer# */{
|
||||||
* console.log(project.activeLayer == firstLayer); // true
|
* console.log(project.activeLayer == firstLayer); // true
|
||||||
*/
|
*/
|
||||||
activate: function() {
|
activate: function() {
|
||||||
this._project.activeLayer = this;
|
this._project._activeLayer = this;
|
||||||
},
|
},
|
||||||
|
|
||||||
// Private helper for #insertAbove() / #insertBelow()
|
// Private helper for #insertAbove() / #insertBelow()
|
||||||
|
|
|
@ -53,9 +53,9 @@ var Project = PaperScopeItem.extend(/** @lends Project# */{
|
||||||
// constructors.
|
// constructors.
|
||||||
PaperScopeItem.call(this, true);
|
PaperScopeItem.call(this, true);
|
||||||
this.layers = [];
|
this.layers = [];
|
||||||
|
this._activeLayer = null;
|
||||||
this.symbols = [];
|
this.symbols = [];
|
||||||
this._currentStyle = new Style(null, null, this);
|
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
|
// If no view is provided, we create a 1x1 px canvas view just so we
|
||||||
// have something to do size calculations with.
|
// have something to do size calculations with.
|
||||||
// (e.g. PointText#_getBounds)
|
// (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
|
* Checks whether the project has any content or not.
|
||||||
* projects by default are created with one empty layer, this returns also
|
|
||||||
* {@code true} if that layer exists but is itself empty.
|
|
||||||
*
|
*
|
||||||
* @return Boolean
|
* @return Boolean
|
||||||
*/
|
*/
|
||||||
isEmpty: function() {
|
isEmpty: function() {
|
||||||
return this.layers.length <= 1
|
return this.layers.length === 0;
|
||||||
&& (!this.activeLayer || this.activeLayer.isEmpty());
|
|
||||||
},
|
},
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -183,11 +180,11 @@ var Project = PaperScopeItem.extend(/** @lends Project# */{
|
||||||
if (child instanceof Layer) {
|
if (child instanceof Layer) {
|
||||||
Base.splice(this.layers, [child]);
|
Base.splice(this.layers, [child]);
|
||||||
// Also activate this layer if there was none before
|
// Also activate this layer if there was none before
|
||||||
if (!this.activeLayer)
|
if (!this._activeLayer)
|
||||||
this.activeLayer = child;
|
this._activeLayer = child;
|
||||||
} else if (child instanceof Item) {
|
} else if (child instanceof Item) {
|
||||||
// Anything else than layers needs to be added to a layer first
|
// 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
|
// NOTE: If there is no layer and this project is not the active
|
||||||
// one, passing insert: false and calling addChild on the
|
// one, passing insert: false and calling addChild on the
|
||||||
// project will handle it correctly.
|
// project will handle it correctly.
|
||||||
|
@ -772,7 +769,7 @@ var Project = PaperScopeItem.extend(/** @lends Project# */{
|
||||||
this.activate();
|
this.activate();
|
||||||
// Provide the activeLayer as a possible target for layers, but only if
|
// Provide the activeLayer as a possible target for layers, but only if
|
||||||
// it's empty.
|
// it's empty.
|
||||||
var layer = this.activeLayer;
|
var layer = this._activeLayer;
|
||||||
return Base.importJSON(json, layer && layer.isEmpty() && layer);
|
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.
|
* 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
|
* The layer which is currently active. New items will be created on this
|
||||||
* layer by default.
|
* layer by default.
|
||||||
*
|
*
|
||||||
* @name Project#activeLayer
|
|
||||||
* @type Layer
|
* @type Layer
|
||||||
|
* @bean
|
||||||
*/
|
*/
|
||||||
|
getActiveLayer: function() {
|
||||||
|
return this._activeLayer || new Layer({ project: this });
|
||||||
|
},
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The symbols contained within the project.
|
* The symbols contained within the project.
|
||||||
|
|
|
@ -45,18 +45,20 @@ test('Item#matches()', function() {
|
||||||
});
|
});
|
||||||
|
|
||||||
test('Project#getItems()', function() {
|
test('Project#getItems()', function() {
|
||||||
|
var layer = new Layer();
|
||||||
|
|
||||||
equals(function() {
|
equals(function() {
|
||||||
var matches = paper.project.getItems({
|
var matches = paper.project.getItems({
|
||||||
type: 'layer'
|
type: 'layer'
|
||||||
});
|
});
|
||||||
return matches.length == 1 && matches[0] == paper.project.activeLayer;
|
return matches.length == 1 && matches[0] == layer;
|
||||||
}, true);
|
}, true);
|
||||||
|
|
||||||
equals(function() {
|
equals(function() {
|
||||||
var matches = paper.project.getItems({
|
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);
|
}, true);
|
||||||
|
|
||||||
var path = new Path();
|
var path = new Path();
|
||||||
|
@ -69,12 +71,11 @@ test('Project#getItems()', function() {
|
||||||
|
|
||||||
equals(function() {
|
equals(function() {
|
||||||
var matches = paper.project.getItems({
|
var matches = paper.project.getItems({
|
||||||
'constructor': Path
|
constructor: Path
|
||||||
});
|
});
|
||||||
return matches.length == 1 && matches[0] === path;
|
return matches.length == 1 && matches[0] === path;
|
||||||
}, true);
|
}, true);
|
||||||
|
|
||||||
|
|
||||||
var group = new Group();
|
var group = new Group();
|
||||||
equals(function() {
|
equals(function() {
|
||||||
var matches = paper.project.getItems({
|
var matches = paper.project.getItems({
|
||||||
|
|
|
@ -29,6 +29,7 @@ test('clear()', function() {
|
||||||
var project = new Project();
|
var project = new Project();
|
||||||
new Layer();
|
new Layer();
|
||||||
new Layer();
|
new Layer();
|
||||||
|
new Layer();
|
||||||
equals(project.layers.length, 3);
|
equals(project.layers.length, 3);
|
||||||
project.clear();
|
project.clear();
|
||||||
equals(project.layers.length, 0);
|
equals(project.layers.length, 0);
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue