diff --git a/src/sprites/rendered-target.js b/src/sprites/rendered-target.js index 370dacd63..ebfe80a6d 100644 --- a/src/sprites/rendered-target.js +++ b/src/sprites/rendered-target.js @@ -534,12 +534,19 @@ class RenderedTarget extends Target { /** * Delete a costume by index. * @param {number} index Costume index to be deleted + * @return {?object} The costume that was deleted or null + * if the index was out of bounds of the costumes list or + * this target only has one costume. */ deleteCostume (index) { const originalCostumeCount = this.sprite.costumes.length; - if (originalCostumeCount === 1) return; + if (originalCostumeCount === 1) return null; - this.sprite.deleteCostumeAt(index); + if (index < 0 || index >= originalCostumeCount) { + return null; + } + + const deletedCostume = this.sprite.deleteCostumeAt(index); if (index === this.currentCostume && index === originalCostumeCount - 1) { this.setCostume(index - 1); @@ -550,6 +557,7 @@ class RenderedTarget extends Target { } this.runtime.requestTargetsUpdate(this); + return deletedCostume; } /** diff --git a/src/sprites/sprite.js b/src/sprites/sprite.js index fa24f8d77..f02f41a0e 100644 --- a/src/sprites/sprite.js +++ b/src/sprites/sprite.js @@ -93,11 +93,10 @@ class Sprite { /** * Delete a costume by index. * @param {number} index Costume index to be deleted + * @return {?object} The deleted costume */ deleteCostumeAt (index) { - this.costumes_ = this.costumes_ - .slice(0, index) - .concat(this.costumes_.slice(index + 1)); + return this.costumes.splice(index, 1)[0]; } /** diff --git a/src/virtual-machine.js b/src/virtual-machine.js index d23c85773..618711121 100644 --- a/src/virtual-machine.js +++ b/src/virtual-machine.js @@ -589,9 +589,19 @@ class VirtualMachine extends EventEmitter { /** * Delete a costume from the current editing target. * @param {int} costumeIndex - the index of the costume to be removed. + * @return {?function} A function to restore the deleted costume, or null, + * if no costume was deleted. */ deleteCostume (costumeIndex) { - this.editingTarget.deleteCostume(costumeIndex); + const deletedCostume = this.editingTarget.deleteCostume(costumeIndex); + if (deletedCostume) { + const target = this.editingTarget; + return () => { + target.addCostume(deletedCostume); + this.emitTargetsUpdate(); + }; + } + return null; } /**