Update blocks after sound/costume rename.
This commit is contained in:
1 parent
ef60b2a771
commit
7faeed7128
2 files changed
+98
-2
No files matched your search
@@ -577,6 +577,80 @@ class Blocks {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Update blocks after a sound, costume, or backdrop gets renamed.
|
||||
* Any block referring to the old name of the asset should get updated
|
||||
* to refer to the new name.
|
||||
* @param {string} oldName The old name of the asset that was renamed.
|
||||
* @param {string} newName The new name of the asset that was renamed.
|
||||
* @param {string} assetType String representation of the kind of asset
|
||||
* that was renamed. This can be one of 'costume', 'sound', or 'backdrop'.
|
||||
*/
|
||||
updateAssetName (oldName, newName, assetType) {
|
||||
let getAssetField;
|
||||
if (assetType === 'costume') {
|
||||
getAssetField = this._getCostumeField.bind(this);
|
||||
} else if (assetType === 'sound') {
|
||||
getAssetField = this._getSoundField.bind(this);
|
||||
} else if (assetType === 'backdrop') {
|
||||
getAssetField = this._getBackdropField.bind(this);
|
||||
} else {
|
||||
return;
|
||||
}
|
||||
const blocks = this._blocks;
|
||||
for (const blockId in blocks) {
|
||||
const assetField = getAssetField(blockId);
|
||||
if (assetField && assetField.value === oldName) {
|
||||
assetField.value = newName;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Helper function to retrieve a costume menu field from a block given its id.
|
||||
* @param {string} blockId A unique identifier for a block
|
||||
* @return {?object} The costume menu field of the block with the given block id.
|
||||
* Null if either a block with the given id doesn't exist or if a costume menu field
|
||||
* does not exist on the block with the given id.
|
||||
*/
|
||||
_getCostumeField (blockId) {
|
||||
const block = this.getBlock(blockId);
|
||||
if (block && block.fields.hasOwnProperty('COSTUME')) {
|
||||
return block.fields.COSTUME;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Helper function to retrieve a sound menu field from a block given its id.
|
||||
* @param {string} blockId A unique identifier for a block
|
||||
* @return {?object} The sound menu field of the block with the given block id.
|
||||
* Null, if either a block with the given id doesn't exist or if a sound menu field
|
||||
* does not exist on the block with the given id.
|
||||
*/
|
||||
_getSoundField (blockId) {
|
||||
const block = this.getBlock(blockId);
|
||||
if (block && block.fields.hasOwnProperty('SOUND_MENU')) {
|
||||
return block.fields.SOUND_MENU;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Helper function to retrieve a backdrop menu field from a block given its id.
|
||||
* @param {string} blockId A unique identifier for a block
|
||||
* @return {?object} The backdrop menu field of the block with the given block id.
|
||||
* Null, if either a block with the given id doesn't exist or if a backdrop menu field
|
||||
* does not exist on the block with the given id.
|
||||
*/
|
||||
_getBackdropField (blockId) {
|
||||
const block = this.getBlock(blockId);
|
||||
if (block && block.fields.hasOwnProperty('BACKDROP')) {
|
||||
return block.fields.BACKDROP;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
|
||||
@@ -417,7 +417,26 @@ class RenderedTarget extends Target {
|
||||
const usedNames = this.sprite.costumes
|
||||
.filter((costume, index) => costumeIndex !== index)
|
||||
.map(costume => costume.name);
|
||||
this.sprite.costumes[costumeIndex].name = StringUtil.unusedName(newName, usedNames);
|
||||
const oldName = this.sprite.costumes[costumeIndex].name;
|
||||
const newUnusedName = StringUtil.unusedName(newName, usedNames);
|
||||
this.sprite.costumes[costumeIndex].name = newUnusedName;
|
||||
|
||||
if (this.isStage) {
|
||||
this.blocks.updateAssetName(oldName, newUnusedName, 'backdrop');
|
||||
// Since this is a backdrop, run through the blocks of the other targets
|
||||
// and update the backdrop name there as well
|
||||
const targets = this.runtime.targets;
|
||||
for (let i = 0; i < targets.length; i++) {
|
||||
const currTarget = targets[i];
|
||||
if (currTarget.id === this.id) {
|
||||
continue;
|
||||
}
|
||||
currTarget.blocks.updateAssetName(oldName, newUnusedName, 'backdrop');
|
||||
}
|
||||
} else {
|
||||
this.blocks.updateAssetName(oldName, newUnusedName, 'costume');
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -462,7 +481,10 @@ class RenderedTarget extends Target {
|
||||
const usedNames = this.sprite.sounds
|
||||
.filter((sound, index) => soundIndex !== index)
|
||||
.map(sound => sound.name);
|
||||
this.sprite.sounds[soundIndex].name = StringUtil.unusedName(newName, usedNames);
|
||||
const oldName = this.sprite.sounds[soundIndex].name;
|
||||
const newUnusedName = StringUtil.unusedName(newName, usedNames);
|
||||
this.sprite.sounds[soundIndex].name = newUnusedName;
|
||||
this.blocks.updateAssetName(oldName, newUnusedName, 'sound');
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in new issue
Block a user