mirror of
https://github.com/scratchfoundation/scratch-blocks.git
synced 2025-08-28 22:10:31 -04:00
Collapse the five 0/1-based index flags into one option.
This commit is contained in:
parent
e6ec2d6fd9
commit
7928fac3ce
24 changed files with 60 additions and 74 deletions
|
@ -300,9 +300,12 @@ Blockly.Blocks['lists_indexOf'] = {
|
|||
this.appendValueInput('FIND')
|
||||
.appendField(new Blockly.FieldDropdown(OPERATORS), 'END');
|
||||
this.setInputsInline(true);
|
||||
var tooltip = Blockly.Msg.LISTS_INDEX_OF_TOOLTIP
|
||||
.replace('%1', Blockly.Blocks.ONE_BASED_INDEXING ? '0' : '-1');
|
||||
this.setTooltip(tooltip);
|
||||
// Assign 'this' to a variable for use in the tooltip closure below.
|
||||
var thisBlock = this;
|
||||
this.setTooltip(function() {
|
||||
return Blockly.Msg.LISTS_INDEX_OF_TOOLTIP.replace('%1',
|
||||
this.workspace.options.oneBasedIndex ? '0' : '-1');
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
|
@ -390,8 +393,11 @@ Blockly.Blocks['lists_getIndex'] = {
|
|||
break;
|
||||
}
|
||||
if (where == 'FROM_START' || where == 'FROM_END') {
|
||||
tooltip += ' ' + Blockly.Msg.LISTS_INDEX_FROM_START_TOOLTIP
|
||||
.replace('%1', Blockly.Blocks.ONE_BASED_INDEXING ? '#1' : '#0');
|
||||
var msg = (where == 'FROM_START') ?
|
||||
Blockly.Msg.LISTS_INDEX_FROM_START_TOOLTIP :
|
||||
Blockly.Msg.LISTS_INDEX_FROM_END_TOOLTIP;
|
||||
tooltip += ' ' + msg.replace('%1',
|
||||
thisBlock.workspace.options.oneBasedIndex ? '#1' : '#0');
|
||||
}
|
||||
return tooltip;
|
||||
});
|
||||
|
@ -551,7 +557,8 @@ Blockly.Blocks['lists_setIndex'] = {
|
|||
}
|
||||
if (where == 'FROM_START' || where == 'FROM_END') {
|
||||
tooltip += ' ' + Blockly.Msg.LISTS_INDEX_FROM_START_TOOLTIP
|
||||
.replace('%1', Blockly.Blocks.ONE_BASED_INDEXING ? '#1' : '#0');
|
||||
.replace('%1',
|
||||
thisBlock.workspace.options.oneBasedIndex ? '#1' : '#0');
|
||||
}
|
||||
return tooltip;
|
||||
});
|
||||
|
|
|
@ -322,9 +322,12 @@ Blockly.Blocks['text_indexOf'] = {
|
|||
this.appendDummyInput().appendField(Blockly.Msg.TEXT_INDEXOF_TAIL);
|
||||
}
|
||||
this.setInputsInline(true);
|
||||
var tooltip = Blockly.Msg.TEXT_INDEXOF_TOOLTIP
|
||||
.replace('%1', Blockly.Blocks.ONE_BASED_INDEXING ? '0' : '-1');
|
||||
this.setTooltip(tooltip);
|
||||
// Assign 'this' to a variable for use in the tooltip closure below.
|
||||
var thisBlock = this;
|
||||
this.setTooltip(function() {
|
||||
return Blockly.Msg.TEXT_INDEXOF_TOOLTIP.replace('%1',
|
||||
thisBlock.workspace.options.oneBasedIndex ? '0' : '-1');
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
|
@ -355,8 +358,11 @@ Blockly.Blocks['text_charAt'] = {
|
|||
var where = thisBlock.getFieldValue('WHERE');
|
||||
var tooltip = Blockly.Msg.TEXT_CHARAT_TOOLTIP;
|
||||
if (where == 'FROM_START' || where == 'FROM_END') {
|
||||
tooltip += ' ' + Blockly.Msg.LISTS_INDEX_FROM_END_TOOLTIP
|
||||
.replace('%1', Blockly.Blocks.ONE_BASED_INDEXING ? '#1' : '#0');
|
||||
var msg = (where == 'FROM_START') ?
|
||||
Blockly.Msg.LISTS_INDEX_FROM_START_TOOLTIP :
|
||||
Blockly.Msg.LISTS_INDEX_FROM_END_TOOLTIP;
|
||||
tooltip += ' ' + msg.replace('%1',
|
||||
thisBlock.workspace.options.oneBasedIndex ? '#1' : '#0');
|
||||
}
|
||||
return tooltip;
|
||||
});
|
||||
|
|
|
@ -25,9 +25,3 @@
|
|||
'use strict';
|
||||
|
||||
goog.provide('Blockly.Blocks');
|
||||
|
||||
/**
|
||||
* Allow for switching between one and zero based indexing for lists and text,
|
||||
* one based by default.
|
||||
*/
|
||||
Blockly.Blocks.ONE_BASED_INDEXING = true;
|
||||
|
|
|
@ -104,8 +104,10 @@ Blockly.Options = function(options) {
|
|||
// 'path' is a deprecated option which has been replaced by 'media'.
|
||||
pathToMedia = options['path'] + 'media/';
|
||||
}
|
||||
var oneBasedIndex = !options['oneBasedIndex'];
|
||||
|
||||
this.RTL = rtl;
|
||||
this.oneBasedIndex = oneBasedIndex;
|
||||
this.collapse = hasCollapse;
|
||||
this.comments = hasComments;
|
||||
this.disable = hasDisable;
|
||||
|
|
|
@ -170,6 +170,7 @@ Blockly.Toolbox.prototype.init = function() {
|
|||
disabledPatternId: workspace.options.disabledPatternId,
|
||||
parentWorkspace: workspace,
|
||||
RTL: workspace.RTL,
|
||||
oneBasedIndex: workspace.options.oneBasedIndex,
|
||||
horizontalLayout: workspace.horizontalLayout,
|
||||
toolboxPosition: workspace.options.toolboxPosition
|
||||
};
|
||||
|
|
|
@ -359,6 +359,7 @@ Blockly.WorkspaceSvg.prototype.addFlyout_ = function() {
|
|||
disabledPatternId: this.options.disabledPatternId,
|
||||
parentWorkspace: this,
|
||||
RTL: this.RTL,
|
||||
oneBasedIndex: workspace.options.oneBasedIndex,
|
||||
horizontalLayout: this.horizontalLayout,
|
||||
toolboxPosition: this.options.toolboxPosition
|
||||
};
|
||||
|
|
|
@ -84,12 +84,6 @@ Blockly.Dart.ORDER_CASCADE = 15; // ..
|
|||
Blockly.Dart.ORDER_ASSIGNMENT = 16; // = *= /= ~/= %= += -= <<= >>= &= ^= |=
|
||||
Blockly.Dart.ORDER_NONE = 99; // (...)
|
||||
|
||||
/**
|
||||
* Allow for switching between one and zero based indexing for lists and text,
|
||||
* one based by default.
|
||||
*/
|
||||
Blockly.Dart.ONE_BASED_INDEXING = true;
|
||||
|
||||
/**
|
||||
* Initialise the database of variable names.
|
||||
* @param {!Blockly.Workspace} workspace Workspace to generate code from.
|
||||
|
@ -232,10 +226,10 @@ Blockly.Dart.getAdjusted = function(block, atId, opt_delta, opt_negate,
|
|||
opt_order) {
|
||||
var delta = opt_delta || 0;
|
||||
var order = opt_order || Blockly.Dart.ORDER_NONE;
|
||||
if (Blockly.Dart.ONE_BASED_INDEXING) {
|
||||
if (block.workspace.options.oneBasedIndex) {
|
||||
delta--;
|
||||
}
|
||||
var defaultAtIndex = Blockly.Dart.ONE_BASED_INDEXING ? '1' : '0';
|
||||
var defaultAtIndex = block.workspace.options.oneBasedIndex ? '1' : '0';
|
||||
if (delta) {
|
||||
var at = Blockly.Dart.valueToCode(block, atId,
|
||||
Blockly.Dart.ORDER_ADDITIVE) || defaultAtIndex;
|
||||
|
|
|
@ -80,7 +80,7 @@ Blockly.Dart['lists_indexOf'] = function(block) {
|
|||
var list = Blockly.Dart.valueToCode(block, 'VALUE',
|
||||
Blockly.Dart.ORDER_UNARY_POSTFIX) || '[]';
|
||||
var code = list + '.' + operator + '(' + item + ')';
|
||||
if (Blockly.Dart.ONE_BASED_INDEXING) {
|
||||
if (block.workspace.options.oneBasedIndex) {
|
||||
return [code + ' + 1', Blockly.Dart.ORDER_ADDITIVE];
|
||||
}
|
||||
return [code, Blockly.Dart.ORDER_UNARY_POSTFIX];
|
||||
|
|
|
@ -90,7 +90,7 @@ Blockly.Dart['text_indexOf'] = function(block) {
|
|||
var text = Blockly.Dart.valueToCode(block, 'VALUE',
|
||||
Blockly.Dart.ORDER_UNARY_POSTFIX) || '\'\'';
|
||||
var code = text + '.' + operator + '(' + substring + ')';
|
||||
if (Blockly.Dart.ONE_BASED_INDEXING) {
|
||||
if (block.workspace.options.oneBasedIndex) {
|
||||
return [code + ' + 1', Blockly.Dart.ORDER_ADDITIVE];
|
||||
}
|
||||
return [code, Blockly.Dart.ORDER_UNARY_POSTFIX];
|
||||
|
|
|
@ -134,12 +134,6 @@ Blockly.JavaScript.ORDER_OVERRIDES = [
|
|||
[Blockly.JavaScript.ORDER_LOGICAL_OR, Blockly.JavaScript.ORDER_LOGICAL_OR]
|
||||
];
|
||||
|
||||
/**
|
||||
* Allow for switching between one and zero based indexing for lists and text,
|
||||
* one based by default.
|
||||
*/
|
||||
Blockly.JavaScript.ONE_BASED_INDEXING = true;
|
||||
|
||||
/**
|
||||
* Initialise the database of variable names.
|
||||
* @param {!Blockly.Workspace} workspace Workspace to generate code from.
|
||||
|
@ -272,10 +266,10 @@ Blockly.JavaScript.getAdjusted = function(block, atId, opt_delta, opt_negate,
|
|||
opt_order) {
|
||||
var delta = opt_delta || 0;
|
||||
var order = opt_order || Blockly.JavaScript.ORDER_NONE;
|
||||
if (Blockly.JavaScript.ONE_BASED_INDEXING) {
|
||||
if (block.workspace.options.oneBasedIndex) {
|
||||
delta--;
|
||||
}
|
||||
var defaultAtIndex = Blockly.JavaScript.ONE_BASED_INDEXING ? '1' : '0';
|
||||
var defaultAtIndex = block.workspace.options.oneBasedIndex ? '1' : '0';
|
||||
if (delta > 0) {
|
||||
var at = Blockly.JavaScript.valueToCode(block, atId,
|
||||
Blockly.JavaScript.ORDER_ADDITION) || defaultAtIndex;
|
||||
|
|
|
@ -88,7 +88,7 @@ Blockly.JavaScript['lists_indexOf'] = function(block) {
|
|||
var list = Blockly.JavaScript.valueToCode(block, 'VALUE',
|
||||
Blockly.JavaScript.ORDER_MEMBER) || '[]';
|
||||
var code = list + '.' + operator + '(' + item + ')';
|
||||
if (Blockly.JavaScript.ONE_BASED_INDEXING) {
|
||||
if (block.workspace.options.oneBasedIndex) {
|
||||
return [code + ' + 1', Blockly.JavaScript.ORDER_ADDITION];
|
||||
}
|
||||
return [code, Blockly.JavaScript.ORDER_FUNCTION_CALL];
|
||||
|
|
|
@ -96,7 +96,7 @@ Blockly.JavaScript['text_indexOf'] = function(block) {
|
|||
Blockly.JavaScript.ORDER_MEMBER) || '\'\'';
|
||||
var code = text + '.' + operator + '(' + substring + ')';
|
||||
// Adjust index if using one-based indices.
|
||||
if (Blockly.JavaScript.ONE_BASED_INDEXING) {
|
||||
if (block.workspace.options.oneBasedIndex) {
|
||||
return [code + ' + 1', Blockly.JavaScript.ORDER_ADDITION];
|
||||
}
|
||||
return [code, Blockly.JavaScript.ORDER_FUNCTION_CALL];
|
||||
|
|
|
@ -87,9 +87,9 @@ Blockly.Lua.ORDER_OR = 9; // or
|
|||
Blockly.Lua.ORDER_NONE = 99;
|
||||
|
||||
/**
|
||||
* Lua is not supporting zero-indexing since the language itself is one-indexed,
|
||||
* so there is not flag for ONE_BASED_INDEXING to indicate which indexing is
|
||||
* used for lists and text.
|
||||
* Note: Lua is not supporting zero-indexing since the language itself is
|
||||
* one-indexed, so the generator does not repoct the oneBasedIndex configuration
|
||||
* option used for lists and text.
|
||||
*/
|
||||
|
||||
/**
|
||||
|
|
|
@ -131,12 +131,6 @@ Blockly.PHP.ORDER_OVERRIDES = [
|
|||
[Blockly.PHP.ORDER_LOGICAL_OR, Blockly.PHP.ORDER_LOGICAL_OR]
|
||||
];
|
||||
|
||||
/**
|
||||
* Allow for switching between one and zero based indexing for lists and text,
|
||||
* one based by default.
|
||||
*/
|
||||
Blockly.PHP.ONE_BASED_INDEXING = true;
|
||||
|
||||
/**
|
||||
* Initialise the database of variable names.
|
||||
* @param {!Blockly.Workspace} workspace Workspace to generate code from.
|
||||
|
@ -257,10 +251,10 @@ Blockly.PHP.getAdjusted = function(block, atId, opt_delta, opt_negate,
|
|||
opt_order) {
|
||||
var delta = opt_delta || 0;
|
||||
var order = opt_order || Blockly.PHP.ORDER_NONE;
|
||||
if (Blockly.PHP.ONE_BASED_INDEXING) {
|
||||
if (block.workspace.options.oneBasedIndex) {
|
||||
delta--;
|
||||
}
|
||||
var defaultAtIndex = Blockly.PHP.ONE_BASED_INDEXING ? '1' : '0';
|
||||
var defaultAtIndex = block.workspace.options.oneBasedIndex ? '1' : '0';
|
||||
if (delta > 0) {
|
||||
var at = Blockly.PHP.valueToCode(block, atId,
|
||||
Blockly.PHP.ORDER_ADDITION) || defaultAtIndex;
|
||||
|
|
|
@ -105,7 +105,7 @@ Blockly.PHP['lists_indexOf'] = function(block) {
|
|||
Blockly.PHP.ORDER_NONE) || '\'\'';
|
||||
var argument1 = Blockly.PHP.valueToCode(block, 'VALUE',
|
||||
Blockly.PHP.ORDER_MEMBER) || '[]';
|
||||
if (Blockly.PHP.ONE_BASED_INDEXING) {
|
||||
if (block.workspace.options.oneBasedIndex) {
|
||||
var errorIndex = ' 0';
|
||||
var indexAdjustment = ' + 1';
|
||||
} else {
|
||||
|
|
|
@ -102,7 +102,7 @@ Blockly.PHP['text_indexOf'] = function(block) {
|
|||
Blockly.PHP.ORDER_NONE) || '\'\'';
|
||||
var text = Blockly.PHP.valueToCode(block, 'VALUE',
|
||||
Blockly.PHP.ORDER_NONE) || '\'\'';
|
||||
if (Blockly.PHP.ONE_BASED_INDEXING) {
|
||||
if (block.workspace.options.oneBasedIndex) {
|
||||
var errorIndex = ' 0';
|
||||
var indexAdjustment = ' + 1';
|
||||
} else {
|
||||
|
|
|
@ -90,12 +90,6 @@ Blockly.Python.ORDER_CONDITIONAL = 15; // if else
|
|||
Blockly.Python.ORDER_LAMBDA = 16; // lambda
|
||||
Blockly.Python.ORDER_NONE = 99; // (...)
|
||||
|
||||
/**
|
||||
* Allow for switching between one and zero based indexing for lists and text,
|
||||
* one based by default.
|
||||
*/
|
||||
Blockly.Python.ONE_BASED_INDEXING = true;
|
||||
|
||||
/**
|
||||
* List of outer-inner pairings that do NOT require parentheses.
|
||||
* @type {!Array.<!Array.<number>>}
|
||||
|
@ -258,10 +252,10 @@ Blockly.Python.scrub_ = function(block, code) {
|
|||
*/
|
||||
Blockly.Python.getAdjustedInt = function(block, atId, opt_delta, opt_negate) {
|
||||
var delta = opt_delta || 0;
|
||||
if (Blockly.Python.ONE_BASED_INDEXING) {
|
||||
if (block.workspace.options.oneBasedIndex) {
|
||||
delta--;
|
||||
}
|
||||
var defaultAtIndex = Blockly.Python.ONE_BASED_INDEXING ? '1' : '0';
|
||||
var defaultAtIndex = block.workspace.options.oneBasedIndex ? '1' : '0';
|
||||
var atOrder = delta ? Blockly.Python.ORDER_ADDITIVE :
|
||||
Blockly.Python.ORDER_NONE;
|
||||
var at = Blockly.Python.valueToCode(block, atId, atOrder) || defaultAtIndex;
|
||||
|
@ -287,4 +281,3 @@ Blockly.Python.getAdjustedInt = function(block, atId, opt_delta, opt_negate) {
|
|||
}
|
||||
return at;
|
||||
};
|
||||
|
||||
|
|
|
@ -76,7 +76,7 @@ Blockly.Python['lists_indexOf'] = function(block) {
|
|||
Blockly.Python.ORDER_NONE) || '[]';
|
||||
var list = Blockly.Python.valueToCode(block, 'VALUE',
|
||||
Blockly.Python.ORDER_NONE) || '\'\'';
|
||||
if (Blockly.Python.ONE_BASED_INDEXING) {
|
||||
if (block.workspace.options.oneBasedIndex) {
|
||||
var errorIndex = ' 0';
|
||||
var firstIndexAdjustment = ' + 1';
|
||||
var lastIndexAdjustment = '';
|
||||
|
|
|
@ -103,7 +103,7 @@ Blockly.Python['text_indexOf'] = function(block) {
|
|||
var text = Blockly.Python.valueToCode(block, 'VALUE',
|
||||
Blockly.Python.ORDER_MEMBER) || '\'\'';
|
||||
var code = text + '.' + operator + '(' + substring + ')';
|
||||
if (Blockly.Python.ONE_BASED_INDEXING) {
|
||||
if (block.workspace.options.oneBasedIndex) {
|
||||
return [code + ' + 1', Blockly.Python.ORDER_ADDITIVE];
|
||||
}
|
||||
return [code, Blockly.Python.ORDER_FUNCTION_CALL];
|
||||
|
|
|
@ -86,6 +86,7 @@ function start() {
|
|||
toolbox: document.getElementById('toolbox'),
|
||||
zoom: {controls: true, wheel: true}
|
||||
});
|
||||
changeIndex();
|
||||
}
|
||||
|
||||
function loadXml() {
|
||||
|
@ -153,23 +154,17 @@ function toXml() {
|
|||
}
|
||||
|
||||
function toJavaScript() {
|
||||
var oneBasedIndexing = document.getElementById('indexing').checked;
|
||||
Blockly.JavaScript.ONE_BASED_INDEXING = oneBasedIndexing;
|
||||
var code = '\'use strict\';\n\n'
|
||||
code += Blockly.JavaScript.workspaceToCode(workspace);
|
||||
setOutput(code);
|
||||
}
|
||||
|
||||
function toPython() {
|
||||
var oneBasedIndexing = document.getElementById('indexing').checked;
|
||||
Blockly.Python.ONE_BASED_INDEXING = oneBasedIndexing;
|
||||
var code = Blockly.Python.workspaceToCode(workspace);
|
||||
setOutput(code);
|
||||
}
|
||||
|
||||
function toPhp() {
|
||||
var oneBasedIndexing = document.getElementById('indexing').checked;
|
||||
Blockly.PHP.ONE_BASED_INDEXING = oneBasedIndexing;
|
||||
var code = Blockly.PHP.workspaceToCode(workspace);
|
||||
setOutput(code);
|
||||
}
|
||||
|
@ -180,11 +175,15 @@ function toLua() {
|
|||
}
|
||||
|
||||
function toDart() {
|
||||
var oneBasedIndexing = document.getElementById('indexing').checked;
|
||||
Blockly.Dart.ONE_BASED_INDEXING = oneBasedIndexing;
|
||||
var code = Blockly.Dart.workspaceToCode(workspace);
|
||||
setOutput(code);
|
||||
}
|
||||
|
||||
function changeIndex() {
|
||||
var oneBasedIndex = document.getElementById('indexing').checked;
|
||||
workspace.options.oneBasedIndex = oneBasedIndex;
|
||||
workspace.toolbox_.flyout_.workspace_.options.oneBasedIndex = oneBasedIndex;
|
||||
}
|
||||
</script>
|
||||
|
||||
<style>
|
||||
|
@ -326,7 +325,8 @@ h1 {
|
|||
</p>
|
||||
|
||||
<p>
|
||||
<input id="indexing" type="checkbox" checked> Generate with one-based indexing
|
||||
<input id="indexing" type="checkbox" onchange="changeIndex()" checked>
|
||||
<label for="indexing">Generate with one-based indexing</label>
|
||||
</p>
|
||||
|
||||
<p>
|
||||
|
|
|
@ -162,7 +162,7 @@ Blockly.Dart['unittest_adjustindex'] = function(block) {
|
|||
var index = Blockly.Dart.valueToCode(block, 'INDEX',
|
||||
Blockly.Dart.ORDER_ADDITIVE) || '0';
|
||||
// Adjust index if using one-based indexing.
|
||||
if (Blockly.Dart.ONE_BASED_INDEXING) {
|
||||
if (block.workspace.options.oneBasedIndex) {
|
||||
if (Blockly.isNumber(index)) {
|
||||
// If the index is a naked number, adjust it right now.
|
||||
return [parseFloat(index) + 1, Blockly.Dart.ORDER_ATOMIC];
|
||||
|
|
|
@ -166,7 +166,7 @@ Blockly.JavaScript['unittest_adjustindex'] = function(block) {
|
|||
var index = Blockly.JavaScript.valueToCode(block, 'INDEX',
|
||||
Blockly.JavaScript.ORDER_ADDITION) || '0';
|
||||
// Adjust index if using one-based indexing.
|
||||
if (Blockly.JavaScript.ONE_BASED_INDEXING) {
|
||||
if (block.workspace.options.oneBasedIndex) {
|
||||
if (Blockly.isNumber(index)) {
|
||||
// If the index is a naked number, adjust it right now.
|
||||
return [parseFloat(index) + 1, Blockly.JavaScript.ORDER_ATOMIC];
|
||||
|
|
|
@ -172,7 +172,7 @@ Blockly.PHP['unittest_adjustindex'] = function(block) {
|
|||
var index = Blockly.PHP.valueToCode(block, 'INDEX',
|
||||
Blockly.PHP.ORDER_ADDITION) || '0';
|
||||
// Adjust index if using one-based indexing.
|
||||
if (Blockly.PHP.ONE_BASED_INDEXING) {
|
||||
if (block.workspace.options.oneBasedIndex) {
|
||||
if (Blockly.isNumber(index)) {
|
||||
// If the index is a naked number, adjust it right now.
|
||||
return [parseFloat(index) + 1, Blockly.PHP.ORDER_ATOMIC];
|
||||
|
|
|
@ -137,7 +137,7 @@ Blockly.Python['unittest_adjustindex'] = function(block) {
|
|||
var index = Blockly.Python.valueToCode(block, 'INDEX',
|
||||
Blockly.Python.ORDER_ADDITIVE) || '0';
|
||||
// Adjust index if using one-based indexing.
|
||||
if (Blockly.Python.ONE_BASED_INDEXING) {
|
||||
if (block.workspace.options.oneBasedIndex) {
|
||||
if (Blockly.isNumber(index)) {
|
||||
// If the index is a naked number, adjust it right now.
|
||||
return [parseFloat(index) + 1, Blockly.Python.ORDER_ATOMIC];
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue