diff --git a/src/document/Document.js b/src/document/Document.js index e6079dc1..83ae4ed2 100644 --- a/src/document/Document.js +++ b/src/document/Document.js @@ -37,7 +37,8 @@ var Document = this.Document = Base.extend({ this.symbols = []; this.views = [new DocumentView(this)]; this.activeView = this.views[0]; - this._selectedItems = []; + this._selectedItems = {}; + this._selectedItemCount = 0; }, getCurrentStyle: function() { @@ -60,34 +61,50 @@ var Document = this.Document = Base.extend({ getSelectedItems: function() { // TODO: return groups if their children are all selected, // and filter out their children from the list. - return this._selectedItems; + var items = []; + Base.each(items, function(item) { + items.push(item); + }); + return items; }, - + // TODO: implement setSelectedItems? + _selectItem: function(item, select) { + if (select) { + this.selectedItemCount++; + this._selectedItems[item.getId()] = item; + } else { + this._selectedItemCount--; + delete this._selectedItems[item.getId()]; + } + }, + draw: function() { if (this.canvas) { + var context = this.context; // Initial tests conclude that clearing the canvas using clearRect // is always faster than setting canvas.width = canvas.width // http://jsperf.com/clearrect-vs-setting-width/7 - this.context.clearRect(0, 0, + context.clearRect(0, 0, this.size.width + 1, this.size.height + 1); - this.context.save(); + context.save(); var param = { offset: new Point(0, 0) }; for (var i = 0, l = this.layers.length; i < l; i++) - Item.draw(this.layers[i], this.context, param); - this.context.restore(); + Item.draw(this.layers[i], context, param); + context.restore(); // Draw the selection of the selected items in the document: - var selectedItems = this._selectedItems, - length = selectedItems.length; - if (length) { - this.context.strokeWidth = 1; + if (this._selectedItemCount > 0) { + context.save(); + context.strokeWidth = 1; // Todo: use Layer#color - this.context.strokeStyle = this.context.fillStyle = '#4f7aff'; + context.strokeStyle = context.fillStyle = '#4f7aff'; param = { selection: true }; - for (var i = 0; i < length; i++) - selectedItems[i].draw(this.context, param); + Base.each(this.selectedItems, function(item) { + item.draw(context, param); + }); + context.restore(); } } }, diff --git a/src/item/Item.js b/src/item/Item.js index c1ae7530..b2169462 100644 --- a/src/item/Item.js +++ b/src/item/Item.js @@ -64,15 +64,7 @@ var Item = this.Item = Base.extend({ // TODO: when an item is removed or moved to another // document, it needs to be removed from _selectedItems this._selected = selected; - var selectedItems = this._document._selectedItems; - if (selected) { - selectedItems.push(this); - } else { - // TODO: is there a faster way? - var index = selectedItems.indexOf(this); - if (index != -1) - selectedItems.splice(index, 1); - } + this._document._selectItem(this, selected); } } }, diff --git a/src/path/Path.js b/src/path/Path.js index ba84e7fd..766a87cb 100644 --- a/src/path/Path.js +++ b/src/path/Path.js @@ -136,17 +136,8 @@ var Path = this.Path = PathItem.extend({ setSelected: function(selected) { var wasSelected = this.isSelected(); var length = this._segments.length; - if (!wasSelected != !selected && length) { - var selectedItems = this._document._selectedItems; - if (selected) { - selectedItems.push(this); - } else { - // TODO: is there a faster way? - var index = selectedItems.indexOf(this); - if (index != -1) - selectedItems.splice(index, 1); - } - } + if (!wasSelected != !selected && length) + this._document._selectItem(this, selected); this._selectedSegmentCount = selected ? length : 0; for (var i = 0; i < length; i++) this._segments[i]._selectionState = selected diff --git a/src/path/Segment.js b/src/path/Segment.js index e2fa2747..bee56923 100644 --- a/src/path/Segment.js +++ b/src/path/Segment.js @@ -221,15 +221,12 @@ var Segment = this.Segment = Base.extend({ selectedItems = path._document._selectedItems; if (!this._selectionState) { path._selectedSegmentCount--; - if (path._selectedSegmentCount == 0) { - var index = selectedItems.indexOf(this); - selectedItems.slice(index, 1); - } + if (path._selectedSegmentCount == 0) + path._document._selectItem(path, true); } else { path._selectedSegmentCount++; - if (path._selectedSegmentCount == 1) { - selectedItems.push(path); - } + if (path._selectedSegmentCount == 1) + path._document._selectItem(path, false); } } },