diff --git a/src/item/Item.js b/src/item/Item.js index c0cb9643..285fafc5 100644 --- a/src/item/Item.js +++ b/src/item/Item.js @@ -2244,6 +2244,19 @@ var Item = Base.extend(Callback, /** @lends Item# */{ return this._remove(true, true); }, + /** + * Replaces this item with the provided new item which will takes its place + * in the project hierarchy instead. + * + * @return {Boolean} {@true if the item was replaced} + */ + replaceWith: function(item) { + var ok = item && item.insertBelow(this); + if (ok) + this.remove(); + return ok; + }, + /** * Removes all of the item's {@link #children} (if any). * diff --git a/test/tests/Item.js b/test/tests/Item.js index fff5e18d..e1396299 100644 --- a/test/tests/Item.js +++ b/test/tests/Item.js @@ -183,6 +183,29 @@ test('item.nextSibling / item.previousSibling', function() { }, true); }); +test('item.replaceWith(other)', function() { + var project = paper.project; + var path = new Path(); + var secondPath = new Path(); + var thirdPath = new Path(); + equals(function() { + return project.activeLayer.children.length; + }, 3); + path.replaceWith(secondPath); + equals(function() { + return project.activeLayer.children.length; + }, 2); + equals(function() { + return path.parent == null; + }, true); + equals(function() { + return secondPath.previousSibling == null; + }, true); + equals(function() { + return secondPath.nextSibling == thirdPath; + }, true); +}); + test('item.insertChild(0, child)', function() { var project = paper.project; var path = new Path();