mirror of
https://github.com/scratchfoundation/scratch-vm.git
synced 2025-08-28 22:30:40 -04:00
Update blocks after sound/costume rename.
This commit is contained in:
parent
ef60b2a771
commit
7faeed7128
2 changed files with 98 additions and 2 deletions
|
@ -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;
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue