Commit 544cdb94 authored by carlosperate's avatar carlosperate

Update Blockly to allow easy replacement of window.prompt by async version.

parent b26b7884
......@@ -15,6 +15,11 @@ It adds the following features:
* Procedures core class modified to include the Arduino setup() and loop() functions
* Minor visual changes to the zoom icons positioning
The following features are planned to be push upstream (list will be updated as PR get accepted):
* Fix toolbox XML nodes injected into blockly under IE (works on Chrome and Firefox)
* Replaces window.prompt uses to a local version that can easily be replaced by an asynchronous HTML version
All other changes and fixes have been submitted to the original Blockly repository for inclusion into the upstream master branch.
This fork gets frequent upstream pulls to maintain it up to date.
......
......@@ -113,7 +113,7 @@ Blockly.FieldTextInput.prototype.showEditor_ = function(opt_quietInput) {
if (!quietInput && (goog.userAgent.MOBILE || goog.userAgent.ANDROID ||
goog.userAgent.IPAD)) {
// Mobile browsers have issues with in-line textareas (focus & keyboards).
var newValue = window.prompt(Blockly.Msg.CHANGE_VALUE_TITLE, this.text_);
var newValue = Blockly.prompt(Blockly.Msg.CHANGE_VALUE_TITLE,this.text_);
if (this.sourceBlock_ && this.changeHandler_) {
var override = this.changeHandler_(newValue);
if (override !== undefined) {
......
......@@ -29,6 +29,7 @@ goog.provide('Blockly.FieldVariable');
goog.require('Blockly.FieldDropdown');
goog.require('Blockly.Msg');
goog.require('Blockly.Variables');
goog.require('Blockly.utils');
goog.require('goog.string');
......@@ -159,39 +160,43 @@ Blockly.FieldVariable.dropdownCreate = function() {
* @this {!Blockly.FieldVariable}
*/
Blockly.FieldVariable.dropdownChange = function(text) {
function promptName(promptText, defaultText) {
function promptName(promptText, defaultText, callback) {
Blockly.hideChaff();
var newVar = window.prompt(promptText, defaultText);
// Merge runs of whitespace. Strip leading and trailing whitespace.
// Beyond this, all names are legal.
if (newVar) {
newVar = newVar.replace(/[\s\xa0]+/g, ' ').replace(/^ | $/g, '');
if (newVar == Blockly.Msg.RENAME_VARIABLE ||
newVar == Blockly.Msg.NEW_VARIABLE) {
// Ok, not ALL names are legal...
newVar = null;
var newVar = Blockly.prompt(promptText, defaultText, function(newVar) {
// Merge runs of whitespace. Strip leading and trailing whitespace.
// Beyond this, all names are legal.
if (newVar) {
newVar = newVar.replace(/[\s\xa0]+/g, ' ').replace(/^ | $/g, '');
if (newVar == Blockly.Msg.RENAME_VARIABLE ||
newVar == Blockly.Msg.NEW_VARIABLE) {
// Ok, not ALL names are legal...
newVar = null;
}
}
}
return newVar;
callback(newVar);
});
}
var workspace = this.sourceBlock_.workspace;
if (text == Blockly.Msg.RENAME_VARIABLE) {
var oldVar = this.getText();
text = promptName(Blockly.Msg.RENAME_VARIABLE_TITLE.replace('%1', oldVar),
oldVar);
if (text) {
Blockly.Variables.renameVariable(oldVar, text, workspace);
}
promptName(Blockly.Msg.RENAME_VARIABLE_TITLE.replace('%1', oldVar),
oldVar, function(text) {
if (text) {
Blockly.Variables.renameVariable(oldVar, text, workspace);
}
});
return null;
} else if (text == Blockly.Msg.NEW_VARIABLE) {
text = promptName(Blockly.Msg.NEW_VARIABLE_TITLE, '');
// Since variables are case-insensitive, ensure that if the new variable
// matches with an existing variable, the new case prevails throughout.
if (text) {
Blockly.Variables.renameVariable(text, text, workspace);
return text;
}
return null;
/*promptName(Blockly.Msg.NEW_VARIABLE_TITLE, '', function(text) {
// Since variables are case-insensitive, ensure that if the new variable
// matches with an existing variable, the new case prevails throughout.
if (text) {
Blockly.Variables.renameVariable(text, text, workspace);
//TODO: need to add here what too do with the newly created variable
}
});*/
//TODO: this return variable needs to be made redundant
return Blockly.Variables.generateUniqueName(workspace);
}
return undefined;
};
......@@ -591,3 +591,31 @@ Blockly.genUid.crypto_ = this.crypto;
*/
Blockly.genUid.soup_ = '!#$%()*+,-./:;=?@[]^_`{|}~' +
'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
/**
* Local prompt function created to allow blockly developers to overwrite it
* with a customised version. This version uses the default window.prompt
* functionality, but it has been designed to be easily replaced by an
* asynchronous HTML based prompt.
* @param {string} message Main text message for the window prompt.
* @param {string=} opt_defaultInput Input string to be displayed by default.
* @param {function=} opt_callback Optional function callback to process the
* user input.
* @return {undefined|null|string} If no callback is provided it returns the
* value directly from window.prompt (null or string), otherwise it
* returns undefined.
*/
Blockly.prompt = function(message, opt_defaultInput, opt_callback) {
if (opt_callback === undefined) {
// If no callback provided to revert back to the normal blockly prompt
return window.prompt(message, opt_defaultInput);
} else {
// window.prompt still a blocking function, but returns value via callback
if (typeof opt_callback == 'function') {
opt_callback(window.prompt(message, opt_defaultInput));
} else {
console.log('Blocky prompt callback needs to be a callable function.');
}
}
return undefined;
};
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