mirror of
https://github.com/scratchfoundation/scratch-vm.git
synced 2024-12-23 06:23:37 -05:00
Instrument blocks.js with getters for fields and inputs
This commit is contained in:
parent
b1e913b99a
commit
3898fe1c45
1 changed files with 36 additions and 1 deletions
|
@ -22,6 +22,13 @@ function Blocks () {
|
|||
this._stacks = [];
|
||||
}
|
||||
|
||||
/**
|
||||
* Blockly inputs that represent statements/substacks
|
||||
* are prefixed with this string.
|
||||
* @const{string}
|
||||
*/
|
||||
Blocks.SUBSTACK_INPUT_PREFIX = 'SUBSTACK';
|
||||
|
||||
/**
|
||||
* Provide an object with metadata for the requested block ID.
|
||||
* @param {!string} blockId ID of block we have stored.
|
||||
|
@ -60,7 +67,7 @@ Blocks.prototype.getSubstack = function (id, substackNum) {
|
|||
if (typeof block === 'undefined') return null;
|
||||
if (!substackNum) substackNum = 1;
|
||||
|
||||
var inputName = 'SUBSTACK';
|
||||
var inputName = Blocks.SUBSTACK_INPUT_PREFIX;
|
||||
if (substackNum > 1) {
|
||||
inputName += substackNum;
|
||||
}
|
||||
|
@ -80,6 +87,34 @@ Blocks.prototype.getOpcode = function (id) {
|
|||
return this._blocks[id].opcode;
|
||||
};
|
||||
|
||||
/**
|
||||
* Get all fields and their values for a block.
|
||||
* @param {?string} id ID of block to query.
|
||||
* @return {!Object} All fields and their values.
|
||||
*/
|
||||
Blocks.prototype.getFields = function (id) {
|
||||
if (typeof this._blocks[id] === 'undefined') return null;
|
||||
return this._blocks[id].fields;
|
||||
};
|
||||
|
||||
/**
|
||||
* Get all non-substack inputs for a block.
|
||||
* @param {?string} id ID of block to query.
|
||||
* @return {!Object} All non-substack inputs and their associated blocks.
|
||||
*/
|
||||
Blocks.prototype.getInputs = function (id) {
|
||||
if (typeof this._blocks[id] === 'undefined') return null;
|
||||
var inputs = {};
|
||||
for (var input in this._blocks[id].inputs) {
|
||||
// Ignore blocks prefixed with substack prefix.
|
||||
if (input.substring(0, Blocks.SUBSTACK_INPUT_PREFIX.length)
|
||||
!= Blocks.SUBSTACK_INPUT_PREFIX) {
|
||||
inputs[input] = this._blocks[id].inputs[input];
|
||||
}
|
||||
}
|
||||
return inputs;
|
||||
};
|
||||
|
||||
// ---------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
|
|
Loading…
Reference in a new issue