Make index optional

This commit is contained in:
DD 2018-02-23 16:24:18 -05:00
parent 10789cd779
commit 9a65df4c12
2 changed files with 16 additions and 28 deletions

View file

@ -401,22 +401,16 @@ class RenderedTarget extends Target {
/** /**
* Add a costume, taking care to avoid duplicate names. * Add a costume, taking care to avoid duplicate names.
* @param {!object} costumeObject Object representing the costume. * @param {!object} costumeObject Object representing the costume.
* @param {?int} index Index at which to add costume
*/ */
addCostume (costumeObject) { addCostume (costumeObject, index) {
const usedNames = this.sprite.costumes.map(costume => costume.name); const usedNames = this.sprite.costumes.map(costume => costume.name);
costumeObject.name = StringUtil.unusedName(costumeObject.name, usedNames); costumeObject.name = StringUtil.unusedName(costumeObject.name, usedNames);
if (index) {
this.sprite.costumes.splice(index, 0, costumeObject);
} else {
this.sprite.costumes.push(costumeObject); this.sprite.costumes.push(costumeObject);
} }
/**
* Add a costume at the given index, taking care to avoid duplicate names.
* @param {!object} costumeObject Object representing the costume.
* @param {!int} index Index at which to add costume
*/
addCostumeAt (costumeObject, index) {
const usedNames = this.sprite.costumes.map(costume => costume.name);
costumeObject.name = StringUtil.unusedName(costumeObject.name, usedNames);
this.sprite.costumes.splice(index, 0, costumeObject);
} }
/** /**
@ -472,23 +466,17 @@ class RenderedTarget extends Target {
/** /**
* Add a sound, taking care to avoid duplicate names. * Add a sound, taking care to avoid duplicate names.
* @param {!object} soundObject Object representing the sound. * @param {!object} soundObject Object representing the sound.
* @param {!int} index Index at which to add costume * @param {?int} index Index at which to add costume
*/ */
addSoundAt (soundObject, index) { addSound (soundObject, index) {
const usedNames = this.sprite.sounds.map(sound => sound.name); const usedNames = this.sprite.sounds.map(sound => sound.name);
soundObject.name = StringUtil.unusedName(soundObject.name, usedNames); soundObject.name = StringUtil.unusedName(soundObject.name, usedNames);
if (index) {
this.sprite.sounds.splice(index, 0, soundObject); this.sprite.sounds.splice(index, 0, soundObject);
} } else {
/**
* Add a sound, taking care to avoid duplicate names.
* @param {!object} soundObject Object representing the sound.
*/
addSound (soundObject) {
const usedNames = this.sprite.sounds.map(sound => sound.name);
soundObject.name = StringUtil.unusedName(soundObject.name, usedNames);
this.sprite.sounds.push(soundObject); this.sprite.sounds.push(soundObject);
} }
}
/** /**
* Rename a sound, taking care to avoid duplicate names. * Rename a sound, taking care to avoid duplicate names.

View file

@ -337,7 +337,7 @@ class VirtualMachine extends EventEmitter {
const clone = Object.assign({}, originalCostume); const clone = Object.assign({}, originalCostume);
const md5ext = `${clone.assetId}.${clone.dataFormat}`; const md5ext = `${clone.assetId}.${clone.dataFormat}`;
return loadCostume(md5ext, clone, this.runtime).then(() => { return loadCostume(md5ext, clone, this.runtime).then(() => {
this.editingTarget.addCostumeAt(clone, costumeIndex + 1); this.editingTarget.addCostume(clone, costumeIndex + 1);
this.editingTarget.setCostume(costumeIndex + 1); this.editingTarget.setCostume(costumeIndex + 1);
this.emitTargetsUpdate(); this.emitTargetsUpdate();
}); });
@ -352,7 +352,7 @@ class VirtualMachine extends EventEmitter {
const originalSound = this.editingTarget.getSounds()[soundIndex]; const originalSound = this.editingTarget.getSounds()[soundIndex];
const clone = Object.assign({}, originalSound); const clone = Object.assign({}, originalSound);
return loadSound(clone, this.runtime).then(() => { return loadSound(clone, this.runtime).then(() => {
this.editingTarget.addSoundAt(clone, soundIndex + 1); this.editingTarget.addSound(clone, soundIndex + 1);
this.emitTargetsUpdate(); this.emitTargetsUpdate();
}); });
} }