Return function to restore a costume when it gets deleted.

This commit is contained in:
Karishma Chadha 2018-08-21 15:12:42 -04:00
parent 6f9da87abe
commit e5ca208a41
3 changed files with 23 additions and 6 deletions

View file

@ -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;
}
/**

View file

@ -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];
}
/**

View file

@ -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;
}
/**