Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
A
ardublockly
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Analytics
Analytics
Repository
Value Stream
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Commits
Open sidebar
xpstem
ardublockly
Commits
315a5fc7
Commit
315a5fc7
authored
Jan 12, 2015
by
Neil Fraser
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Remove global workspace references from variable and procedure utilities.
parent
6cc26f07
Changes
8
Expand all
Hide whitespace changes
Inline
Side-by-side
Showing
8 changed files
with
85 additions
and
63 deletions
+85
-63
blockly_compressed.js
blockly_compressed.js
+12
-11
core/field_variable.js
core/field_variable.js
+35
-8
core/flyout.js
core/flyout.js
+1
-0
core/procedures.js
core/procedures.js
+5
-4
core/variables.js
core/variables.js
+23
-28
generators/dart.js
generators/dart.js
+3
-4
generators/javascript.js
generators/javascript.js
+3
-4
generators/python.js
generators/python.js
+3
-4
No files found.
blockly_compressed.js
View file @
315a5fc7
This diff is collapsed.
Click to expand it.
core/field_variable.js
View file @
315a5fc7
...
...
@@ -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\x
a0
]
+/g
,
'
'
).
replace
(
/^ | $/g
,
''
);
if
(
newVar
)
{
newVar
=
newVar
.
replace
(
/
[\s\x
a0
]
+/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
;
...
...
core/flyout.js
View file @
315a5fc7
...
...
@@ -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
);
...
...
core/procedures.js
View file @
315a5fc7
...
...
@@ -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
'
);
};
...
...
core/variables.js
View file @
315a5fc7
...
...
@@ -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
{
...
...
generators/dart.js
View file @
315a5fc7
...
...
@@ -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
],
...
...
generators/javascript.js
View file @
315a5fc7
...
...
@@ -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
],
...
...
generators/python.js
View file @
315a5fc7
...
...
@@ -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
'
;
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment