scratch-gui
Version:
GraphicaL User Interface for creating and running Scratch 3.0 projects
34 lines (27 loc) • 1.29 kB
JavaScript
import OpcodeLabels from './opcode-labels.js';
const isUndefined = a => typeof a === 'undefined';
/**
* Convert monitors from VM format to what the GUI needs to render.
* - Convert opcode to a label and a category
* @param {string} block.id - The id of the monitor block
* @param {string} block.spriteName - Present only if the monitor applies only to the sprite
* with given target ID. The name of the target sprite when the monitor was created
* @param {string} block.opcode - The opcode of the monitor
* @param {object} block.params - Extra params to the monitor block
* @param {string|number|Array} block.value - The monitor value
* @return {object} The adapted monitor with label and category
*/
export default function ({id, spriteName, opcode, params, value}) {
let {label, category, labelFn} = OpcodeLabels(opcode);
// Use labelFn if provided for dynamic labelling (e.g. variables)
if (!isUndefined(labelFn)) label = labelFn(params);
// Append sprite name for sprite-specific monitors
if (spriteName) {
label = `${spriteName}: ${label}`;
}
// If value is a number, round it to six decimal places
if (typeof value === 'number') {
value = Number(value.toFixed(6));
}
return {id, label, category, value};
}