mirror of
https://github.com/scratchfoundation/scratch-blocks.git
synced 2025-08-28 22:10:31 -04:00
Remove global workspace references from variable and procedure utilities.
This commit is contained in:
parent
6cc26f07ac
commit
315a5fc762
8 changed files with 85 additions and 63 deletions
|
@ -1133,15 +1133,16 @@ Blockly.FieldImage.prototype.init=function(a){if(!this.sourceBlock_){this.source
|
|||
a=this.rectElement_||this.imageElement_;a.tooltip=this.sourceBlock_;Blockly.Tooltip.bindMouseEvents(a)}};Blockly.FieldImage.prototype.dispose=function(){goog.dom.removeNode(this.fieldGroup_);this.rectElement_=this.imageElement_=this.fieldGroup_=null};Blockly.FieldImage.prototype.setTooltip=function(a){(this.rectElement_||this.imageElement_).tooltip=a};Blockly.FieldImage.prototype.getValue=function(){return this.src_};
|
||||
Blockly.FieldImage.prototype.setValue=function(a){null!==a&&(this.src_=a,this.imageElement_&&this.imageElement_.setAttributeNS("http://www.w3.org/1999/xlink","xlink:href",goog.isString(a)?a:""))};Blockly.FieldImage.prototype.setText=function(a){null!==a&&(this.text_=a)};Blockly.FieldImage.prototype.render_=function(){};
|
||||
// Copyright 2012 Google Inc. Apache License 2.0
|
||||
Blockly.Variables={};Blockly.Variables.NAME_TYPE="VARIABLE";Blockly.Variables.allVariables=function(a){a=a||Blockly.mainWorkspace;var b;if(a.getDescendants)b=a.getDescendants();else if(a.getAllBlocks)b=a.getAllBlocks();else throw"Not Block or Workspace: "+a;a=Object.create(null);for(var c=0;c<b.length;c++){var d=b[c].getVars;if(d)for(var d=d.call(b[c]),e=0;e<d.length;e++){var f=d[e];f&&(a[f.toLowerCase()]=f)}}b=[];for(var g in a)b.push(a[g]);return b};
|
||||
Blockly.Variables.renameVariable=function(a,b,c){c=(c||Blockly.mainWorkspace).getAllBlocks();for(var d=0;d<c.length;d++){var e=c[d].renameVar;e&&e.call(c[d],a,b)}};
|
||||
Blockly.Variables.flyoutCategory=function(a,b,c,d){var e=Blockly.Variables.allVariables();e.sort(goog.string.caseInsensitiveCompare);e.unshift(null);for(var f=void 0,g=0;g<e.length;g++)if(e[g]!==f){var h=Blockly.Blocks.variables_get?Blockly.Block.obtain(d,"variables_get"):null;h&&h.initSvg();var k=Blockly.Blocks.variables_set?Blockly.Block.obtain(d,"variables_set"):null;k&&k.initSvg();null===e[g]?f=(h||k).getVars()[0]:(h&&h.setFieldValue(e[g],"VAR"),k&&k.setFieldValue(e[g],"VAR"));k&&a.push(k);h&&
|
||||
a.push(h);h&&k?b.push(c,3*c):b.push(2*c)}};Blockly.Variables.generateUniqueName=function(){var a=Blockly.Variables.allVariables(),b="";if(a.length){a.sort(goog.string.caseInsensitiveCompare);for(var c=0,d="i",e=0,f=!1;!b;){e=0;for(f=!1;e<a.length&&!f;)a[e].toLowerCase()==d&&(f=!0),e++;f?("z"===d[0]?(c++,d="a"):(d=String.fromCharCode(d.charCodeAt(0)+1),"l"==d[0]&&(d=String.fromCharCode(d.charCodeAt(0)+1))),0<c&&(d+=c)):b=d}}else b="i";return b};
|
||||
Blockly.Variables={};Blockly.Variables.NAME_TYPE="VARIABLE";Blockly.Variables.allVariables=function(a){var b;if(a.getDescendants)b=a.getDescendants();else if(a.getAllBlocks)b=a.getAllBlocks();else throw"Not Block or Workspace: "+a;a=Object.create(null);for(var c=0;c<b.length;c++){var d=b[c].getVars;if(d)for(var d=d.call(b[c]),e=0;e<d.length;e++){var f=d[e];f&&(a[f.toLowerCase()]=f)}}b=[];for(var g in a)b.push(a[g]);return b};
|
||||
Blockly.Variables.renameVariable=function(a,b,c){c=c.getAllBlocks();for(var d=0;d<c.length;d++){var e=c[d].renameVar;e&&e.call(c[d],a,b)}};
|
||||
Blockly.Variables.flyoutCategory=function(a,b,c,d){var e=Blockly.Variables.allVariables(d.targetWorkspace);e.sort(goog.string.caseInsensitiveCompare);e.unshift(null);for(var f=void 0,g=0;g<e.length;g++)if(e[g]!==f){var h=Blockly.Blocks.variables_get?Blockly.Block.obtain(d,"variables_get"):null;h&&h.initSvg();var k=Blockly.Blocks.variables_set?Blockly.Block.obtain(d,"variables_set"):null;k&&k.initSvg();null===e[g]?f=(h||k).getVars()[0]:(h&&h.setFieldValue(e[g],"VAR"),k&&k.setFieldValue(e[g],"VAR"));
|
||||
k&&a.push(k);h&&a.push(h);h&&k?b.push(c,3*c):b.push(2*c)}};Blockly.Variables.generateUniqueName=function(a){a=Blockly.Variables.allVariables(a);var b="";if(a.length)for(var c=1,d=0,e="ijkmnopqrstuvwxyzabcdefgh".charAt(d);!b;){for(var f=!1,g=0;g<a.length;g++)if(a[g].toLowerCase()==e){f=!0;break}f?(d++,25==d&&(d=0,c++),e="ijkmnopqrstuvwxyzabcdefgh".charAt(d),1<c&&(e+=c)):b=e}else b="i";return b};
|
||||
// Copyright 2012 Google Inc. Apache License 2.0
|
||||
Blockly.FieldVariable=function(a,b){var c;if(b){var d=this;c=function(a){var c=Blockly.FieldVariable.dropdownChange.call(d,a);a=void 0===c?a:null===c?d.getValue():c;b.call(d,a);return c}}else c=Blockly.FieldVariable.dropdownChange;Blockly.FieldVariable.superClass_.constructor.call(this,Blockly.FieldVariable.dropdownCreate,c);a?this.setValue(a):this.setValue(Blockly.Variables.generateUniqueName())};goog.inherits(Blockly.FieldVariable,Blockly.FieldDropdown);
|
||||
Blockly.FieldVariable.prototype.clone=function(){return new Blockly.FieldVariable(this.getValue(),this.changeHandler_)};Blockly.FieldVariable.prototype.getValue=function(){return this.getText()};Blockly.FieldVariable.prototype.setValue=function(a){this.value_=a;this.setText(a)};
|
||||
Blockly.FieldVariable.dropdownCreate=function(){var a=this.sourceBlock_&&this.sourceBlock_.workspace?Blockly.Variables.allVariables(this.sourceBlock_.workspace):[],b=this.getText();b&&-1==a.indexOf(b)&&a.push(b);a.sort(goog.string.caseInsensitiveCompare);a.push(Blockly.Msg.RENAME_VARIABLE);a.push(Blockly.Msg.NEW_VARIABLE);for(var b=[],c=0;c<a.length;c++)b[c]=[a[c],a[c]];return b};
|
||||
Blockly.FieldVariable.dropdownChange=function(a){function b(a,b){Blockly.hideChaff();var c=window.prompt(a,b);return c&&c.replace(/[\s\xa0]+/g," ").replace(/^ | $/g,"")}if(a==Blockly.Msg.RENAME_VARIABLE){var c=this.getText();(a=b(Blockly.Msg.RENAME_VARIABLE_TITLE.replace("%1",c),c))&&Blockly.Variables.renameVariable(c,a);return null}if(a==Blockly.Msg.NEW_VARIABLE)return(a=b(Blockly.Msg.NEW_VARIABLE_TITLE,""))?(Blockly.Variables.renameVariable(a,a),a):null};
|
||||
Blockly.FieldVariable=function(a,b){var c;if(b){var d=this;c=function(a){var c=Blockly.FieldVariable.dropdownChange.call(d,a);a=void 0===c?a:null===c?d.getValue():c;b.call(d,a);return c}}else c=Blockly.FieldVariable.dropdownChange;Blockly.FieldVariable.superClass_.constructor.call(this,Blockly.FieldVariable.dropdownCreate,c);this.setValue(a||"")};goog.inherits(Blockly.FieldVariable,Blockly.FieldDropdown);
|
||||
Blockly.FieldVariable.prototype.init=function(a){this.sourceBlock_||(this.getValue()||this.setValue(Blockly.Variables.generateUniqueName(a.isInFlyout?a.workspace.targetWorkspace:a.workspace)),Blockly.FieldVariable.superClass_.init.call(this,a))};Blockly.FieldVariable.prototype.clone=function(){return new Blockly.FieldVariable(this.getValue(),this.changeHandler_)};Blockly.FieldVariable.prototype.getValue=function(){return this.getText()};
|
||||
Blockly.FieldVariable.prototype.setValue=function(a){this.value_=a;this.setText(a)};Blockly.FieldVariable.dropdownCreate=function(){var a=this.sourceBlock_&&this.sourceBlock_.workspace?Blockly.Variables.allVariables(this.sourceBlock_.workspace):[],b=this.getText();b&&-1==a.indexOf(b)&&a.push(b);a.sort(goog.string.caseInsensitiveCompare);a.push(Blockly.Msg.RENAME_VARIABLE);a.push(Blockly.Msg.NEW_VARIABLE);for(var b=[],c=0;c<a.length;c++)b[c]=[a[c],a[c]];return b};
|
||||
Blockly.FieldVariable.dropdownChange=function(a){function b(a,b){Blockly.hideChaff();var c=window.prompt(a,b);c&&(c=c.replace(/[\s\xa0]+/g," ").replace(/^ | $/g,""),c==Blockly.Msg.RENAME_VARIABLE||c==Blockly.Msg.NEW_VARIABLE)&&(c=null);return c}var c=this.sourceBlock_.workspace;if(a==Blockly.Msg.RENAME_VARIABLE){var d=this.getText();(a=b(Blockly.Msg.RENAME_VARIABLE_TITLE.replace("%1",d),d))&&Blockly.Variables.renameVariable(d,a,c);return null}if(a==Blockly.Msg.NEW_VARIABLE)return(a=b(Blockly.Msg.NEW_VARIABLE_TITLE,
|
||||
""))?(Blockly.Variables.renameVariable(a,a,c),a):null};
|
||||
// Copyright 2012 Google Inc. Apache License 2.0
|
||||
Blockly.Generator=function(a){this.name_=a;this.RESERVED_WORDS_="";this.FUNCTION_NAME_PLACEHOLDER_REGEXP_=new RegExp(this.FUNCTION_NAME_PLACEHOLDER_,"g")};Blockly.Generator.NAME_TYPE="generated_function";Blockly.Generator.prototype.INFINITE_LOOP_TRAP=null;Blockly.Generator.prototype.STATEMENT_PREFIX=null;
|
||||
Blockly.Generator.prototype.workspaceToCode=function(a){var b=a||Blockly.mainWorkspace;a=[];this.init(b);for(var b=b.getTopBlocks(!0),c=0,d;d=b[c];c++){var e=this.blockToCode(d);goog.isArray(e)&&(e=e[0]);e&&(d.outputConnection&&this.scrubNakedValue&&(e=this.scrubNakedValue(e)),a.push(e))}a=a.join("\n");a=this.finish(a);a=a.replace(/^\s+\n/,"");a=a.replace(/\n\s+$/,"\n");return a=a.replace(/[ \t]+\n/g,"\n")};Blockly.Generator.prototype.prefixLines=function(a,b){return b+a.replace(/\n(.)/g,"\n"+b+"$1")};
|
||||
|
@ -1154,11 +1155,11 @@ Blockly.Generator.prototype.INDENT=" ";Blockly.Generator.prototype.addReservedW
|
|||
Blockly.Names=function(a){this.reservedDict_=Object.create(null);if(a){a=a.split(",");for(var b=0;b<a.length;b++)this.reservedDict_[a[b]]=!0}this.reset()};Blockly.Names.prototype.reset=function(){this.db_=Object.create(null);this.dbReverse_=Object.create(null)};Blockly.Names.prototype.getName=function(a,b){var c=a.toLowerCase()+"_"+b;if(c in this.db_)return this.db_[c];var d=this.getDistinctName(a,b);return this.db_[c]=d};
|
||||
Blockly.Names.prototype.getDistinctName=function(a,b){for(var c=this.safeName_(a),d="";this.dbReverse_[c+d]||c+d in this.reservedDict_;)d=d?d+1:2;c+=d;this.dbReverse_[c]=!0;return c};Blockly.Names.prototype.safeName_=function(a){a?(a=encodeURI(a.replace(/ /g,"_")).replace(/[^\w]/g,"_"),-1!="0123456789".indexOf(a[0])&&(a="my_"+a)):a="unnamed";return a};Blockly.Names.equals=function(a,b){return a.toLowerCase()==b.toLowerCase()};
|
||||
// Copyright 2012 Google Inc. Apache License 2.0
|
||||
Blockly.Procedures={};Blockly.Procedures.NAME_TYPE="PROCEDURE";Blockly.Procedures.allProcedures=function(){for(var a=Blockly.mainWorkspace.getAllBlocks(),b=[],c=[],d=0;d<a.length;d++){var e=a[d].getProcedureDef;e&&(e=e.call(a[d]))&&(e[2]?b.push(e):c.push(e))}c.sort(Blockly.Procedures.procTupleComparator_);b.sort(Blockly.Procedures.procTupleComparator_);return[c,b]};Blockly.Procedures.procTupleComparator_=function(a,b){var c=a[0].toLowerCase(),d=b[0].toLowerCase();return c>d?1:c<d?-1:0};
|
||||
Blockly.Procedures={};Blockly.Procedures.NAME_TYPE="PROCEDURE";Blockly.Procedures.allProcedures=function(a){a=a.getAllBlocks();for(var b=[],c=[],d=0;d<a.length;d++){var e=a[d].getProcedureDef;e&&(e=e.call(a[d]))&&(e[2]?b.push(e):c.push(e))}c.sort(Blockly.Procedures.procTupleComparator_);b.sort(Blockly.Procedures.procTupleComparator_);return[c,b]};Blockly.Procedures.procTupleComparator_=function(a,b){var c=a[0].toLowerCase(),d=b[0].toLowerCase();return c>d?1:c<d?-1:0};
|
||||
Blockly.Procedures.findLegalName=function(a,b){if(b.isInFlyout)return a;for(;!Blockly.Procedures.isLegalName(a,b.workspace,b);){var c=a.match(/^(.*?)(\d+)$/);a=c?c[1]+(parseInt(c[2],10)+1):a+"2"}return a};Blockly.Procedures.isLegalName=function(a,b,c){b=b.getAllBlocks();for(var d=0;d<b.length;d++)if(b[d]!=c){var e=b[d].getProcedureDef;if(e&&(e=e.call(b[d]),Blockly.Names.equals(e[0],a)))return!1}return!0};
|
||||
Blockly.Procedures.rename=function(a){a=a.replace(/^[\s\xa0]+|[\s\xa0]+$/g,"");a=Blockly.Procedures.findLegalName(a,this.sourceBlock_);for(var b=this.sourceBlock_.workspace.getAllBlocks(),c=0;c<b.length;c++){var d=b[c].renameProcedure;d&&d.call(b[c],this.text_,a)}return a};
|
||||
Blockly.Procedures.flyoutCategory=function(a,b,c,d){function e(e,f){for(var k=0;k<e.length;k++){var l=Blockly.Block.obtain(d,f);l.setFieldValue(e[k][0],"NAME");for(var p=[],n=0;n<e[k][1].length;n++)p[n]="ARG"+n;l.setProcedureParameters(e[k][1],p);l.initSvg();a.push(l);b.push(2*c)}}if(Blockly.Blocks.procedures_defnoreturn){var f=Blockly.Block.obtain(d,"procedures_defnoreturn");f.initSvg();a.push(f);b.push(2*c)}Blockly.Blocks.procedures_defreturn&&(f=Blockly.Block.obtain(d,"procedures_defreturn"),f.initSvg(),
|
||||
a.push(f),b.push(2*c));Blockly.Blocks.procedures_ifreturn&&(f=Blockly.Block.obtain(d,"procedures_ifreturn"),f.initSvg(),a.push(f),b.push(2*c));b.length&&(b[b.length-1]=3*c);f=Blockly.Procedures.allProcedures();e(f[0],"procedures_callnoreturn");e(f[1],"procedures_callreturn")};Blockly.Procedures.getCallers=function(a,b){for(var c=[],d=b.getAllBlocks(),e=0;e<d.length;e++){var f=d[e].getProcedureCall;f&&(f=f.call(d[e]))&&Blockly.Names.equals(f,a)&&c.push(d[e])}return c};
|
||||
a.push(f),b.push(2*c));Blockly.Blocks.procedures_ifreturn&&(f=Blockly.Block.obtain(d,"procedures_ifreturn"),f.initSvg(),a.push(f),b.push(2*c));b.length&&(b[b.length-1]=3*c);f=Blockly.Procedures.allProcedures(d.targetWorkspace);e(f[0],"procedures_callnoreturn");e(f[1],"procedures_callreturn")};Blockly.Procedures.getCallers=function(a,b){for(var c=[],d=b.getAllBlocks(),e=0;e<d.length;e++){var f=d[e].getProcedureCall;f&&(f=f.call(d[e]))&&Blockly.Names.equals(f,a)&&c.push(d[e])}return c};
|
||||
Blockly.Procedures.disposeCallers=function(a,b){for(var c=Blockly.Procedures.getCallers(a,b),d=0;d<c.length;d++)c[d].dispose(!0,!1)};Blockly.Procedures.mutateCallers=function(a,b,c,d){a=Blockly.Procedures.getCallers(a,b);for(b=0;b<a.length;b++)a[b].setProcedureParameters(c,d)};Blockly.Procedures.getDefinition=function(a,b){for(var c=b.getAllBlocks(),d=0;d<c.length;d++){var e=c[d].getProcedureDef;if(e&&(e=e.call(c[d]))&&Blockly.Names.equals(e[0],a))return c[d]}return null};
|
||||
// Copyright 2013 Google Inc. Apache License 2.0
|
||||
var rtclient={INSTALL_SCOPE:"https://www.googleapis.com/auth/drive.install",FILE_SCOPE:"https://www.googleapis.com/auth/drive.file",APPDATA_SCOPE:"https://www.googleapis.com/auth/drive.appdata",OPENID_SCOPE:"openid",REALTIME_MIMETYPE:"application/vnd.google-apps.drive-sdk",FOLDER_KEY:"folderId",getParams:function(){function a(a){a=a.slice(1).split("&");for(var b=0;b<a.length;b++){var d=a[b].split("=");c[decodeURIComponent(d[0])]=decodeURIComponent(d[1])}}var b=goog.global.location||{},c={},d=b.hash;
|
||||
|
@ -1205,7 +1206,7 @@ Blockly.Flyout.prototype.createDom=function(){this.svgGroup_=Blockly.createSvgEl
|
|||
Blockly.Flyout.prototype.dispose=function(){this.hide();Blockly.unbindEvent_(this.eventWrappers_);this.eventWrappers_.length=0;this.scrollbar_&&(this.scrollbar_.dispose(),this.scrollbar_=null);this.workspace_=null;this.svgGroup_&&(goog.dom.removeNode(this.svgGroup_),this.svgGroup_=null);this.targetWorkspace_=this.svgBackground_=null};
|
||||
Blockly.Flyout.prototype.getMetrics_=function(){if(!this.isVisible())return null;var a=this.height_-2*this.CORNER_RADIUS,b=this.width_;try{var c=this.workspace_.getCanvas().getBBox()}catch(d){c={height:0,y:0}}return{viewHeight:a,viewWidth:b,contentHeight:c.height+c.y,viewTop:-this.workspace_.scrollY,contentTop:0,absoluteTop:this.CORNER_RADIUS,absoluteLeft:0}};
|
||||
Blockly.Flyout.prototype.setMetrics_=function(a){var b=this.getMetrics_();b&&(goog.isNumber(a.y)&&(this.workspace_.scrollY=-b.contentHeight*a.y-b.contentTop),a=this.workspace_.scrollY+b.absoluteTop,this.workspace_.getCanvas().setAttribute("transform","translate(0,"+a+")"))};
|
||||
Blockly.Flyout.prototype.init=function(a){this.targetWorkspace_=a;this.scrollbar_=new Blockly.Scrollbar(this.workspace_,!1,!1);this.hide();this.eventWrappers_.concat(Blockly.bindEvent_(window,goog.events.EventType.RESIZE,this,this.position_));this.position_();this.eventWrappers_.concat(Blockly.bindEvent_(this.svgGroup_,"wheel",this,this.wheel_));this.eventWrappers_.concat(Blockly.bindEvent_(this.svgGroup_,"mousewheel",this,this.wheel_));this.eventWrappers_.concat(Blockly.bindEvent_(this.targetWorkspace_.getCanvas(),
|
||||
Blockly.Flyout.prototype.init=function(a){this.targetWorkspace_=a;this.workspace_.targetWorkspace=a;this.scrollbar_=new Blockly.Scrollbar(this.workspace_,!1,!1);this.hide();this.eventWrappers_.concat(Blockly.bindEvent_(window,goog.events.EventType.RESIZE,this,this.position_));this.position_();this.eventWrappers_.concat(Blockly.bindEvent_(this.svgGroup_,"wheel",this,this.wheel_));this.eventWrappers_.concat(Blockly.bindEvent_(this.svgGroup_,"mousewheel",this,this.wheel_));this.eventWrappers_.concat(Blockly.bindEvent_(this.targetWorkspace_.getCanvas(),
|
||||
"blocklyWorkspaceChange",this,this.filterForCapacity_));this.eventWrappers_.concat(Blockly.bindEvent_(this.svgGroup_,"mousedown",this,this.onMouseDown_))};
|
||||
Blockly.Flyout.prototype.position_=function(){if(this.isVisible()){var a=this.targetWorkspace_.getMetrics();if(a){var b=this.width_-this.CORNER_RADIUS;Blockly.RTL&&(b*=-1);var c=["M "+(Blockly.RTL?this.width_:0)+",0"];c.push("h",b);c.push("a",this.CORNER_RADIUS,this.CORNER_RADIUS,0,0,Blockly.RTL?0:1,Blockly.RTL?-this.CORNER_RADIUS:this.CORNER_RADIUS,this.CORNER_RADIUS);c.push("v",Math.max(0,a.viewHeight-2*this.CORNER_RADIUS));c.push("a",this.CORNER_RADIUS,this.CORNER_RADIUS,0,0,Blockly.RTL?0:1,Blockly.RTL?
|
||||
this.CORNER_RADIUS:-this.CORNER_RADIUS,this.CORNER_RADIUS);c.push("h",-b);c.push("z");this.svgBackground_.setAttribute("d",c.join(" "));b=a.absoluteLeft;Blockly.RTL&&(b+=a.viewWidth,b-=this.width_);this.svgGroup_.setAttribute("transform","translate("+b+","+a.absoluteTop+")");this.height_=a.viewHeight;this.scrollbar_&&this.scrollbar_.resize()}}};
|
||||
|
|
|
@ -66,14 +66,32 @@ Blockly.FieldVariable = function(varname, opt_changeHandler) {
|
|||
Blockly.FieldVariable.superClass_.constructor.call(this,
|
||||
Blockly.FieldVariable.dropdownCreate, changeHandler);
|
||||
|
||||
if (varname) {
|
||||
this.setValue(varname);
|
||||
} else {
|
||||
this.setValue(Blockly.Variables.generateUniqueName());
|
||||
}
|
||||
this.setValue(varname || '');
|
||||
};
|
||||
goog.inherits(Blockly.FieldVariable, Blockly.FieldDropdown);
|
||||
|
||||
/**
|
||||
* Install this dropdown on a block.
|
||||
* @param {!Blockly.Block} block The block containing this text.
|
||||
*/
|
||||
Blockly.FieldVariable.prototype.init = function(block) {
|
||||
if (this.sourceBlock_) {
|
||||
// Dropdown has already been initialized once.
|
||||
return;
|
||||
}
|
||||
|
||||
if (!this.getValue()) {
|
||||
// Variables without names get uniquely named for this workspace.
|
||||
if (block.isInFlyout) {
|
||||
var workspace = block.workspace.targetWorkspace;
|
||||
} else {
|
||||
var workspace = block.workspace;
|
||||
}
|
||||
this.setValue(Blockly.Variables.generateUniqueName(workspace));
|
||||
}
|
||||
Blockly.FieldVariable.superClass_.init.call(this, block);
|
||||
};
|
||||
|
||||
/**
|
||||
* Clone this FieldVariable.
|
||||
* @return {!Blockly.FieldVariable} The result of calling the constructor again
|
||||
|
@ -147,14 +165,23 @@ Blockly.FieldVariable.dropdownChange = function(text) {
|
|||
var newVar = window.prompt(promptText, defaultText);
|
||||
// Merge runs of whitespace. Strip leading and trailing whitespace.
|
||||
// Beyond this, all names are legal.
|
||||
return newVar && newVar.replace(/[\s\xa0]+/g, ' ').replace(/^ | $/g, '');
|
||||
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;
|
||||
}
|
||||
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);
|
||||
Blockly.Variables.renameVariable(oldVar, text, workspace);
|
||||
}
|
||||
return null;
|
||||
} else if (text == Blockly.Msg.NEW_VARIABLE) {
|
||||
|
@ -162,7 +189,7 @@ Blockly.FieldVariable.dropdownChange = 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);
|
||||
Blockly.Variables.renameVariable(text, text, workspace);
|
||||
return text;
|
||||
}
|
||||
return null;
|
||||
|
|
|
@ -201,6 +201,7 @@ Blockly.Flyout.prototype.setMetrics_ = function(yRatio) {
|
|||
*/
|
||||
Blockly.Flyout.prototype.init = function(workspace) {
|
||||
this.targetWorkspace_ = workspace;
|
||||
this.workspace_.targetWorkspace = workspace;
|
||||
// Add scrollbar.
|
||||
this.scrollbar_ = new Blockly.Scrollbar(this.workspace_, false, false);
|
||||
|
||||
|
|
|
@ -39,14 +39,15 @@ goog.require('Blockly.Workspace');
|
|||
Blockly.Procedures.NAME_TYPE = 'PROCEDURE';
|
||||
|
||||
/**
|
||||
* Find all user-created procedure definitions.
|
||||
* Find all user-created procedure definitions in a workspace.
|
||||
* @param {!Blockly.Workspace} root Root workspace.
|
||||
* @return {!Array.<!Array.<!Array>>} Pair of arrays, the
|
||||
* first contains procedures without return variables, the second with.
|
||||
* Each procedure is defined by a three-element list of name, parameter
|
||||
* list, and return value boolean.
|
||||
*/
|
||||
Blockly.Procedures.allProcedures = function() {
|
||||
var blocks = Blockly.mainWorkspace.getAllBlocks();
|
||||
Blockly.Procedures.allProcedures = function(root) {
|
||||
var blocks = root.getAllBlocks();
|
||||
var proceduresReturn = [];
|
||||
var proceduresNoReturn = [];
|
||||
for (var x = 0; x < blocks.length; x++) {
|
||||
|
@ -207,7 +208,7 @@ Blockly.Procedures.flyoutCategory = function(blocks, gaps, margin, workspace) {
|
|||
}
|
||||
}
|
||||
|
||||
var tuple = Blockly.Procedures.allProcedures();
|
||||
var tuple = Blockly.Procedures.allProcedures(workspace.targetWorkspace);
|
||||
populateProcedures(tuple[0], 'procedures_callnoreturn');
|
||||
populateProcedures(tuple[1], 'procedures_callreturn');
|
||||
};
|
||||
|
|
|
@ -38,12 +38,10 @@ Blockly.Variables.NAME_TYPE = 'VARIABLE';
|
|||
|
||||
/**
|
||||
* Find all user-created variables.
|
||||
* @param {Blockly.Block|Blockly.Workspace|undefined} opt_root Optional root
|
||||
* block or workspace. Defaults to main workspace.
|
||||
* @param {!Blockly.Block|!Blockly.Workspace} root Root block or workspace.
|
||||
* @return {!Array.<string>} Array of variable names.
|
||||
*/
|
||||
Blockly.Variables.allVariables = function(opt_root) {
|
||||
var root = opt_root || Blockly.mainWorkspace;
|
||||
Blockly.Variables.allVariables = function(root) {
|
||||
var blocks;
|
||||
if (root.getDescendants) {
|
||||
// Root is Block.
|
||||
|
@ -81,11 +79,9 @@ Blockly.Variables.allVariables = function(opt_root) {
|
|||
* Find all instances of the specified variable and rename them.
|
||||
* @param {string} oldName Variable to rename.
|
||||
* @param {string} newName New variable name.
|
||||
* @param {Blockly.Workspace=} opt_workspace Workspace rename variables in.
|
||||
* Defaults to main workspace.
|
||||
* @param {!Blockly.Workspace} workspace Workspace rename variables in.
|
||||
*/
|
||||
Blockly.Variables.renameVariable = function(oldName, newName, opt_workspace) {
|
||||
var workspace = opt_workspace || Blockly.mainWorkspace;
|
||||
Blockly.Variables.renameVariable = function(oldName, newName, workspace) {
|
||||
var blocks = workspace.getAllBlocks();
|
||||
// Iterate through every block.
|
||||
for (var x = 0; x < blocks.length; x++) {
|
||||
|
@ -104,7 +100,7 @@ Blockly.Variables.renameVariable = function(oldName, newName, opt_workspace) {
|
|||
* @param {!Blockly.Workspace} workspace The flyout's workspace.
|
||||
*/
|
||||
Blockly.Variables.flyoutCategory = function(blocks, gaps, margin, workspace) {
|
||||
var variableList = Blockly.Variables.allVariables();
|
||||
var variableList = Blockly.Variables.allVariables(workspace.targetWorkspace);
|
||||
variableList.sort(goog.string.caseInsensitiveCompare);
|
||||
// In addition to the user's variables, we also want to display the default
|
||||
// variable name at the top. We also don't want this duplicated if the
|
||||
|
@ -140,40 +136,39 @@ Blockly.Variables.flyoutCategory = function(blocks, gaps, margin, workspace) {
|
|||
/**
|
||||
* Return a new variable name that is not yet being used. This will try to
|
||||
* generate single letter variable names in the range 'i' to 'z' to start with.
|
||||
* If no unique name is located it will try 'i1' to 'z1', then 'i2' to 'z2' etc.
|
||||
* If no unique name is located it will try 'i' to 'z', 'a' to 'h',
|
||||
* then 'i2' to 'z2' etc. Skip 'l'.
|
||||
* @param {!Blockly.Workspace} workspace The workspace to be unique in.
|
||||
* @return {string} New variable name.
|
||||
*/
|
||||
Blockly.Variables.generateUniqueName = function() {
|
||||
var variableList = Blockly.Variables.allVariables();
|
||||
Blockly.Variables.generateUniqueName = function(workspace) {
|
||||
var variableList = Blockly.Variables.allVariables(workspace);
|
||||
var newName = '';
|
||||
if (variableList.length) {
|
||||
variableList.sort(goog.string.caseInsensitiveCompare);
|
||||
var nameSuffix = 0, potName = 'i', i = 0, inUse = false;
|
||||
var nameSuffix = 1;
|
||||
var letters = 'ijkmnopqrstuvwxyzabcdefgh'; // No 'l'.
|
||||
var letterIndex = 0;
|
||||
var potName = letters.charAt(letterIndex);
|
||||
while (!newName) {
|
||||
i = 0;
|
||||
inUse = false;
|
||||
while (i < variableList.length && !inUse) {
|
||||
var inUse = false;
|
||||
for (var i = 0; i < variableList.length; i++) {
|
||||
if (variableList[i].toLowerCase() == potName) {
|
||||
// This potential name is already used.
|
||||
inUse = true;
|
||||
break;
|
||||
}
|
||||
i++;
|
||||
}
|
||||
if (inUse) {
|
||||
// Try the next potential name.
|
||||
if (potName[0] === 'z') {
|
||||
// Reached the end of the character sequence so back to 'a' but with
|
||||
letterIndex++;
|
||||
if (letterIndex == letters.length) {
|
||||
// Reached the end of the character sequence so back to 'i'.
|
||||
// a new suffix.
|
||||
letterIndex = 0;
|
||||
nameSuffix++;
|
||||
potName = 'a';
|
||||
} else {
|
||||
potName = String.fromCharCode(potName.charCodeAt(0) + 1);
|
||||
if (potName[0] == 'l') {
|
||||
// Avoid using variable 'l' because of ambiguity with '1'.
|
||||
potName = String.fromCharCode(potName.charCodeAt(0) + 1);
|
||||
}
|
||||
}
|
||||
if (nameSuffix > 0) {
|
||||
potName = letters.charAt(letterIndex);
|
||||
if (nameSuffix > 1) {
|
||||
potName += nameSuffix;
|
||||
}
|
||||
} else {
|
||||
|
|
|
@ -73,10 +73,9 @@ Blockly.Dart.ORDER_NONE = 99; // (...)
|
|||
|
||||
/**
|
||||
* Initialise the database of variable names.
|
||||
* @param {Blockly.Workspace=} opt_workspace Workspace to generate code from.
|
||||
* Defaults to main workspace.
|
||||
* @param {!Blockly.Workspace} workspace Workspace to generate code from.
|
||||
*/
|
||||
Blockly.Dart.init = function(opt_workspace) {
|
||||
Blockly.Dart.init = function(workspace) {
|
||||
// Create a dictionary of definitions to be printed before the code.
|
||||
Blockly.Dart.definitions_ = Object.create(null);
|
||||
// Create a dictionary mapping desired function names in definitions_
|
||||
|
@ -91,7 +90,7 @@ Blockly.Dart.init = function(opt_workspace) {
|
|||
}
|
||||
|
||||
var defvars = [];
|
||||
var variables = Blockly.Variables.allVariables(opt_workspace);
|
||||
var variables = Blockly.Variables.allVariables(workspace);
|
||||
for (var x = 0; x < variables.length; x++) {
|
||||
defvars[x] = 'var ' +
|
||||
Blockly.Dart.variableDB_.getName(variables[x],
|
||||
|
|
|
@ -105,10 +105,9 @@ Blockly.JavaScript.ORDER_NONE = 99; // (...)
|
|||
|
||||
/**
|
||||
* Initialise the database of variable names.
|
||||
* @param {Blockly.Workspace=} opt_workspace Workspace to generate code from.
|
||||
* Defaults to main workspace.
|
||||
* @param {!Blockly.Workspace} workspace Workspace to generate code from.
|
||||
*/
|
||||
Blockly.JavaScript.init = function(opt_workspace) {
|
||||
Blockly.JavaScript.init = function(workspace) {
|
||||
// Create a dictionary of definitions to be printed before the code.
|
||||
Blockly.JavaScript.definitions_ = Object.create(null);
|
||||
// Create a dictionary mapping desired function names in definitions_
|
||||
|
@ -123,7 +122,7 @@ Blockly.JavaScript.init = function(opt_workspace) {
|
|||
}
|
||||
|
||||
var defvars = [];
|
||||
var variables = Blockly.Variables.allVariables(opt_workspace);
|
||||
var variables = Blockly.Variables.allVariables(workspace);
|
||||
for (var x = 0; x < variables.length; x++) {
|
||||
defvars[x] = 'var ' +
|
||||
Blockly.JavaScript.variableDB_.getName(variables[x],
|
||||
|
|
|
@ -81,10 +81,9 @@ Blockly.Python.ORDER_NONE = 99; // (...)
|
|||
|
||||
/**
|
||||
* Initialise the database of variable names.
|
||||
* @param {Blockly.Workspace=} opt_workspace Workspace to generate code from.
|
||||
* Defaults to main workspace.
|
||||
* @param {!Blockly.Workspace} workspace Workspace to generate code from.
|
||||
*/
|
||||
Blockly.Python.init = function(opt_workspace) {
|
||||
Blockly.Python.init = function(workspace) {
|
||||
// Create a dictionary of definitions to be printed before the code.
|
||||
Blockly.Python.definitions_ = Object.create(null);
|
||||
// Create a dictionary mapping desired function names in definitions_
|
||||
|
@ -99,7 +98,7 @@ Blockly.Python.init = function(opt_workspace) {
|
|||
}
|
||||
|
||||
var defvars = [];
|
||||
var variables = Blockly.Variables.allVariables(opt_workspace);
|
||||
var variables = Blockly.Variables.allVariables(workspace);
|
||||
for (var x = 0; x < variables.length; x++) {
|
||||
defvars[x] = Blockly.Python.variableDB_.getName(variables[x],
|
||||
Blockly.Variables.NAME_TYPE) + ' = None';
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue