Extensions: fix Boolean inputs

This commit is contained in:
Christopher Willis-Ford 2017-10-06 23:24:41 -07:00
parent c1681e54d5
commit 67c788ec8d

View file

@ -48,9 +48,8 @@ const ArgumentTypeMap = (() => {
shadowType: 'text', shadowType: 'text',
fieldType: 'TEXT' fieldType: 'TEXT'
}; };
// @TODO: talk to Rachel & co. to figure out what goes here. Make it OK to not have a field. Add `check` support.
map[ArgumentType.BOOLEAN] = { map[ArgumentType.BOOLEAN] = {
shadowType: '' check: 'Boolean'
}; };
return map; return map;
})(); })();
@ -400,8 +399,7 @@ class Runtime extends EventEmitter {
* @private * @private
*/ */
_makeExtensionMenuId (menuName, extensionId) { _makeExtensionMenuId (menuName, extensionId) {
/** @TODO: protect against XML characters in menu name */ return `${extensionId}.menu.${escapeHtml(menuName)}`;
return `${extensionId}.menu.${menuName}`;
} }
/** /**
@ -515,36 +513,47 @@ class Runtime extends EventEmitter {
// Sanitize the placeholder to ensure valid XML // Sanitize the placeholder to ensure valid XML
placeholder = placeholder.replace(/[<"&]/, '_'); placeholder = placeholder.replace(/[<"&]/, '_');
blockJSON.args0.push({ const argJSON = {
type: 'input_value', type: 'input_value',
name: placeholder name: placeholder
}); };
// scratch-blocks uses 1-based argument indexing
const argNum = blockJSON.args0.length;
argsMap[placeholder] = argNum;
const argInfo = blockInfo.arguments[placeholder] || {}; const argInfo = blockInfo.arguments[placeholder] || {};
const argTypeInfo = ArgumentTypeMap[argInfo.type] || {}; const argTypeInfo = ArgumentTypeMap[argInfo.type] || {};
const defaultValue = (typeof argInfo.defaultValue === 'undefined' ? '' : argInfo.defaultValue.toString()); const defaultValue = (typeof argInfo.defaultValue === 'undefined' ? '' : argInfo.defaultValue.toString());
if (argTypeInfo.check) {
argJSON.check = argTypeInfo.check;
}
const shadowType = (argInfo.menu ? const shadowType = (argInfo.menu ?
this._makeExtensionMenuId(argInfo.menu, categoryInfo.id) : this._makeExtensionMenuId(argInfo.menu, categoryInfo.id) :
argTypeInfo.shadowType); argTypeInfo.shadowType);
const fieldType = argInfo.menu || argTypeInfo.fieldType; const fieldType = argInfo.menu || argTypeInfo.fieldType;
// <value> is the ScratchBlocks name for a block input. // <value> is the ScratchBlocks name for a block input.
// The <shadow> is a placeholder for a reporter and is visible when there's no reporter in this input. inputList.push(`<value name="${placeholder}">`);
inputList.push(`<value name="${placeholder}"><shadow type="${shadowType}">`);
// <field> is a text field that the user can type into. Some shadows, like the color picker, don't allow // The <shadow> is a placeholder for a reporter and is visible when there's no reporter in this input.
// text input and therefore don't need a field element. // Boolean inputs don't need to specify a shadow in the XML.
if (fieldType) { if (shadowType) {
inputList.push(`<field name="${fieldType}">${defaultValue}</field>`); inputList.push(`<shadow type="${shadowType}">`);
// <field> is a text field that the user can type into. Some shadows, like the color picker, don't allow
// text input and therefore don't need a field element.
if (fieldType) {
inputList.push(`<field name="${fieldType}">${defaultValue}</field>`);
}
inputList.push('</shadow>');
} }
inputList.push('</shadow></value>'); inputList.push('</value>');
// scratch-blocks uses 1-based argument indexing
blockJSON.args0.push(argJSON);
const argNum = blockJSON.args0.length;
argsMap[placeholder] = argNum;
return `%${argNum}`; return `%${argNum}`;
}); });