Unverified Commit cd31f22a authored by Beka Westberg's avatar Beka Westberg Committed by GitHub

Reorg context menu opts. Add yail opt to events (#2099)

Refactors the context menu options into individual functions. Adds the
"Generate Yail" option to component event blocks.
parent 9d8db547
...@@ -120,83 +120,100 @@ function unboundVariableHandler(myBlock, yailText) { ...@@ -120,83 +120,100 @@ function unboundVariableHandler(myBlock, yailText) {
var code = "(let ("; var code = "(let (";
for (var i in unbound_vars) { for (var i in unbound_vars) {
code += '($' + unbound_vars[i] + ' ' + Blockly.Yail.quotifyForREPL(document.querySelector('input[name="' + unbound_vars[i] + '"]').value) + ') '; code += '($' + unbound_vars[i] + ' ' + Blockly.Yail.quotifyForREPL(document.querySelector('input[name="' + unbound_vars[i] + '"]').value) + ') ';
}; }
code += ")" + yailText + ")"; code += ")" + yailText + ")";
Blockly.ReplMgr.putYail(code, myBlock); Blockly.ReplMgr.putYail(code, myBlock);
} }
dialog.hide(); dialog.hide();
}); });
}; }
} }
/**
* Adds an option to the block's context menu to export it to a PNG.
* @param {!Blockly.BlockSvg} myBlock The block to export to PNG.
* @param {!Array<!Object>} options The option list to add to.
*/
Blockly.BlocklyEditor.addPngExportOption = function(myBlock, options) { Blockly.BlocklyEditor.addPngExportOption = function(myBlock, options) {
var downloadBlockOption = { var downloadBlockOption = {
enabled: true, enabled: true,
text: Blockly.BlocklyEditor.makeMenuItemWithHelp(Blockly.Msg.DOWNLOAD_BLOCKS_AS_PNG, text: Blockly.BlocklyEditor.makeMenuItemWithHelp(
Blockly.Msg.DOWNLOAD_BLOCKS_AS_PNG,
'/reference/other/download-pngs.html'), '/reference/other/download-pngs.html'),
callback: function() { callback: function() {
Blockly.exportBlockAsPng(myBlock); Blockly.exportBlockAsPng(myBlock);
} }
}; };
// Add it above the help option.
options.splice(options.length - 1, 0, downloadBlockOption); options.splice(options.length - 1, 0, downloadBlockOption);
}; };
/** /**
* Add a "Do It" option to the context menu for every block. If the user is an admin also * Adds an option to the block's context menu to generate its yail.
* add a "Generate Yail" option to the context menu for every block. The generated yail will go in * @param {!Blockly.BlockSvg} myBlock The block to generate yail for.
* the block's comment (if it has one) for now. * @param {!Array<!Object>} options The option list to add to.
* TODO: eventually create a separate kind of bubble for the generated yail, which can morph into
* the bubble for "do it" output once we hook up to the REPL.
*/ */
Blockly.Block.prototype.customContextMenu = function(options) { Blockly.BlocklyEditor.addGenerateYailOption = function(myBlock, options) {
var myBlock = this; if (!window.parent.BlocklyPanel_checkIsAdmin()) {
Blockly.BlocklyEditor.addPngExportOption(myBlock, options); return;
if (window.parent.BlocklyPanel_checkIsAdmin()) { }
// TODO: eventually create a separate kind of bubble for the generated yail,
// which can morph into the bubble for "do it" output once we hook
// up to the REPL.
var yailOption = {enabled: !this.disabled}; var yailOption = {enabled: !this.disabled};
yailOption.text = Blockly.Msg.GENERATE_YAIL; yailOption.text = Blockly.Msg.GENERATE_YAIL;
yailOption.callback = function() { yailOption.callback = function() {
var yailText; // Blockly.Yail.blockToCode1 returns a string if the block is a statement
//Blockly.Yail.blockToCode1 returns a string if the block is a statement // and an array if the block is a value
//and an array if the block is a value var yail = Blockly.Yail.blockToCode1(myBlock);
var yailTextOrArray = Blockly.Yail.blockToCode1(myBlock); myBlock.setCommentText((yail instanceof Array) ? yail[0] : yail);
if(yailTextOrArray instanceof Array){
yailText = yailTextOrArray[0];
} else {
yailText = yailTextOrArray;
}
myBlock.setCommentText(yailText);
}; };
options.push(yailOption); options.push(yailOption);
} };
var connectedToRepl = top.ReplState.state === Blockly.ReplMgr.rsState.CONNECTED;
/**
* Adds an option to the block's context menu to execute the block.
* @param {!Blockly.BlockSvg} myBlock The block to execute.
* @param {!Array<!Object>} options The option list to add to.
*/
Blockly.BlocklyEditor.addDoItOption = function(myBlock, options) {
var connectedToRepl =
top.ReplState.state === Blockly.ReplMgr.rsState.CONNECTED;
var doitOption = { enabled: !this.disabled && connectedToRepl}; var doitOption = { enabled: !this.disabled && connectedToRepl};
doitOption.text = Blockly.Msg.DO_IT; doitOption.text = Blockly.Msg.DO_IT;
doitOption.callback = function() { doitOption.callback = function() {
var yailText;
//Blockly.Yail.blockToCode1 returns a string if the block is a statement
//and an array if the block is a value
var yailTextOrArray = Blockly.Yail.blockToCode1(myBlock);
var dialog;
if (!connectedToRepl) { if (!connectedToRepl) {
dialog = new goog.ui.Dialog(null, true, new goog.dom.DomHelper(top.document)); var dialog = new goog.ui.Dialog(
null, true, new goog.dom.DomHelper(top.document));
dialog.setTitle(Blockly.Msg.CAN_NOT_DO_IT); dialog.setTitle(Blockly.Msg.CAN_NOT_DO_IT);
dialog.setTextContent(Blockly.Msg.CONNECT_TO_DO_IT); dialog.setTextContent(Blockly.Msg.CONNECT_TO_DO_IT);
dialog.setButtonSet(new goog.ui.Dialog.ButtonSet(). dialog.setButtonSet(new goog.ui.Dialog.ButtonSet()
addButton(goog.ui.Dialog.ButtonSet.DefaultButtons.OK, .addButton(goog.ui.Dialog.ButtonSet.DefaultButtons.OK,
false, true)); false, true));
dialog.setVisible(true); dialog.setVisible(true);
} else { } else {
if(yailTextOrArray instanceof Array){ // Blockly.Yail.blockToCode1 returns a string if the block is a statement
yailText = yailTextOrArray[0]; // and an array if the block is a value
} else { var yail = Blockly.Yail.blockToCode1(myBlock);
yailText = yailTextOrArray; unboundVariableHandler(myBlock, (yail instanceof Array) ? yail[0] : yail);
}
unboundVariableHandler(myBlock, yailText);
} }
}; };
options.push(doitOption); options.push(doitOption);
//Option to clear error generated by Do It };
if(myBlock.replError){
/**
* Adds an option to the block's context menu to clear the result of a "Do It"
* operation, if the result exists.
* @param {!Blockly.BlockSvg} myBlock The block clean up.
* @param {!Array<!Object>} options The option list to add to.
*/
Blockly.BlocklyEditor.addClearDoItOption = function(myBlock, options) {
if (!myBlock.replError) {
return;
}
var clearDoitOption = {enabled: true}; var clearDoitOption = {enabled: true};
clearDoitOption.text = Blockly.Msg.CLEAR_DO_IT_ERROR; clearDoitOption.text = Blockly.Msg.CLEAR_DO_IT_ERROR;
clearDoitOption.callback = function() { clearDoitOption.callback = function() {
...@@ -204,9 +221,24 @@ Blockly.Block.prototype.customContextMenu = function(options) { ...@@ -204,9 +221,24 @@ Blockly.Block.prototype.customContextMenu = function(options) {
Blockly.getMainWorkspace().getWarningHandler().checkErrors(myBlock); Blockly.getMainWorkspace().getWarningHandler().checkErrors(myBlock);
}; };
options.push(clearDoitOption); options.push(clearDoitOption);
} };
if(myBlock.procCustomContextMenu){
myBlock.procCustomContextMenu(options); /**
* Adds extra context menu options to all blocks. Current options include:
* - Png Export
* - Generate Yail (admin only)
* - Do It
* - Clear Do It (only if Do It is appended)
* @this {!Blockly.BlockSvg}
*/
Blockly.Block.prototype.customContextMenu = function(options) {
Blockly.BlocklyEditor.addPngExportOption(this, options);
Blockly.BlocklyEditor.addGenerateYailOption(this, options);
Blockly.BlocklyEditor.addDoItOption(this, options);
Blockly.BlocklyEditor.addClearDoItOption(this, options);
if(this.procCustomContextMenu){
this.procCustomContextMenu(options);
} }
}; };
......
...@@ -529,6 +529,7 @@ Blockly.Blocks.component_event = { ...@@ -529,6 +529,7 @@ Blockly.Blocks.component_event = {
Blockly.FieldParameterFlydown.addHorizontalVerticalOption(this, options); Blockly.FieldParameterFlydown.addHorizontalVerticalOption(this, options);
Blockly.ComponentBlock.addGenericOption(this, options); Blockly.ComponentBlock.addGenericOption(this, options);
Blockly.BlocklyEditor.addPngExportOption(this, options); Blockly.BlocklyEditor.addPngExportOption(this, options);
Blockly.BlocklyEditor.addGenerateYailOption(this, options);
}, },
// check if the block corresponds to an event inside componentTypes[typeName].eventDictionary // check if the block corresponds to an event inside componentTypes[typeName].eventDictionary
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment