monitor id getter should use block fields instead of a list of strings

This commit is contained in:
Karishma Chadha 2018-11-08 01:33:26 -05:00
parent 7d1226458f
commit 5db3db3a92
7 changed files with 17 additions and 17 deletions

6
package-lock.json generated
View file

@ -11899,9 +11899,9 @@
}
},
"scratch-render": {
"version": "0.1.0-prerelease.20181024220305",
"resolved": "https://registry.npmjs.org/scratch-render/-/scratch-render-0.1.0-prerelease.20181024220305.tgz",
"integrity": "sha512-wbCVQW6qNJUsuUv6YDTfsKfmXCwI2r0W6wWqstNS2HojNigpYeBVG2SHFPQp/zDOzp/6q/uQkTN/pBOuDoEJpw==",
"version": "0.1.0-prerelease.20181102130522",
"resolved": "https://registry.npmjs.org/scratch-render/-/scratch-render-0.1.0-prerelease.20181102130522.tgz",
"integrity": "sha512-r6V3zzwXalhWR5xWN4miNht7FbYqF6On+p3n2USK4r2D6oaE5vh2aMwKSjt9OfEf/hfFJK63mJd+qeuJ1slKVQ==",
"dev": true,
"requires": {
"grapheme-breaker": "0.3.2",

View file

@ -273,10 +273,10 @@ class Scratch3LooksBlocks {
},
looks_costumenumbername: {
isSpriteSpecific: true,
getId: (targetId, params) => getMonitorIdForBlockWithArgs(`${targetId}_costumenumbername`, params)
getId: (targetId, fields) => getMonitorIdForBlockWithArgs(`${targetId}_costumenumbername`, fields)
},
looks_backdropnumbername: {
getId: (_, params) => getMonitorIdForBlockWithArgs('backdropnumbername', params)
getId: (_, fields) => getMonitorIdForBlockWithArgs('backdropnumbername', fields)
}
};
}

View file

@ -90,7 +90,7 @@ class Scratch3SensingBlocks {
// This is different from the default toolbox xml id in order to support
// importing multiple monitors from the same opcode from sb2 files,
// something that is not currently supported in scratch 3.
getId: (_, params) => getMonitorIdForBlockWithArgs('current', params) // _${param}`
getId: (_, fields) => getMonitorIdForBlockWithArgs('current', fields) // _${param}`
}
};
}

View file

@ -579,9 +579,9 @@ class Blocks {
if (block.fields && Object.keys(block.fields).length > 0 &&
block.opcode !== 'data_variable' && block.opcode !== 'data_listcontents') {
// This block has an argument which needs to get separated out into multiple monitor blocks
const params = Object.keys(block.fields).map(k => block.fields[k].value);
const newId = getMonitorIdForBlockWithArgs(block.id, params);
// This block has an argument which needs to get separated out into
// multiple monitor blocks with ids based on the selected argument
const newId = getMonitorIdForBlockWithArgs(block.id, block.fields);
// Note: we're not just constantly creating a longer and longer id everytime we check
// the checkbox because we're using the id of the block in the flyout as the base

View file

@ -297,8 +297,7 @@ const parseMonitorObject = (object, runtime, targets, extensions) => {
} else if (object.cmd === 'contentsOfList:') {
block.id = getVariableId(object.param, Variable.LIST_TYPE);
} else if (runtime.monitorBlockInfo.hasOwnProperty(block.opcode)) {
if (object.param === 'day of week') object.param = 'DAYOFWEEK';
block.id = runtime.monitorBlockInfo[block.opcode].getId(target.id, [object.param]);
block.id = runtime.monitorBlockInfo[block.opcode].getId(target.id, block.fields);
} else {
// If the opcode can't be found in the runtime monitorBlockInfo,
// then default to using the block opcode as the id instead.

View file

@ -4,16 +4,15 @@
* (and therefore more than one monitor block) associated
* with it (e.g. when reporter blocks have inputs).
* @param {string} baseId The base id to use for the different monitor blocks
* @param {string[]} params A list of strings representing selected
* parameters on the block.
* @param {object} fields The monitor block's fields object.
*/
// TODO this function should eventually be the single place where all monitor
// IDs are obtained given an opcode for the reporter block and the list of
// selected parameters.
const getMonitorIdForBlockWithArgs = function (id, params) {
const getMonitorIdForBlockWithArgs = function (id, fields) {
let fieldString = '';
for (const param of params) {
fieldString += `_${param}`;
for (const fieldKey in fields) {
fieldString += `_${fields[fieldKey].value}`;
}
return `${id}${fieldString}`;
};

View file

@ -60,7 +60,9 @@ test('importing sb2 project with monitors', t => {
t.equal(monitorRecord.height, 202);
// Backdrop name monitor is visible, not sprite specific
monitorRecord = vm.runtime._monitorState.get('backdropnumbername');
// should get imported with id that references the name parameter
// via '_name' at the end since the 3.0 block has a dropdown.
monitorRecord = vm.runtime._monitorState.get('backdropnumbername_name');
t.equal(monitorRecord.opcode, 'looks_backdropnumbername');
t.equal(monitorRecord.mode, 'default');
t.equal(monitorRecord.visible, true);