mirror of
https://github.com/google/blockly.git
synced 2026-01-11 10:57:07 +01:00
* Adding new minimap demo * Basic code style changes. Adding a few more comments. Return early if disableScrollChange in onScrollChange listener. * Adding horizontal scrolling. Changed scroll change callbacks from onScroll_ to setHandlePosition. onScroll_ is not challed when workspace is dragged. * Registering mousemove and mouseup listener in mousedown event. Mousemove and Mouseup events are now listening over document. * Adding the remove variable modal and functionality to accessible Blockly. (#1011) * Minimap position bug fix for browsers other than chrome. Added touch support. * Adding an add variable modal to accessible Blockly. (#1015) * Adding the remove variable modal and functionality to accessible Blockly. * Adding the add variable modal for accessible Blockly. * Block browser context menu in the toolbox and flyout * Add links to the dev registration form and contributor guidelines * Miscellaneous comment cleanup * Adding the common modal class. (#1017) Centralizes accessible modal behavior. * - Changed error message referencing 'procedure' instead of 'function' (#1019) - Added iOS specific UI messages - Fixed bug with js_to_json.py script where it didn't recognize ' character * - Allows use of Blockly's messaging format for category name, colour,… (#1028) ...in toolbox XML. - Updated code editor demo to use this message format - Re-built blockly_compressed.js * Making text_count use a text color (like text_length, which also returns a number). (#1027) * Enable google/blockly with continuous build on travis ci (#1023) (#1035) * create .travis for ci job * initial checkin for blocky-web travis ci job * rename file to .travis.yaml for typo * remove after_script * added cache * rename .travis.yaml to .travis.yml * Update .travis.yml * include build script * fix yaml file format issue * debug install part * debug build issue * Update .travis.yml * remove cache for now * Update .travis.yml * Update .travis.yml * Update .travis.yml * more debug info * Update .travis.yml * Update .travis.yml * fix typo * installing chrome browser * remove chrome setting config * run build.py as part of npm install * Update .travis.yml * update karma dependency * use karma as test runner * fix typo * remove karma test for now * Update .travis.yml * Update package.json * add npm test target * add browserstack-runner depdendency * update browser support * fix typo for test target * fix chrome typo * added closure dependency * add google-closure-library * include blockly_uncompressed.js and core.js dependency * uncomment out core/*.js files * add kama job as part of install * remove browserstack add on for now * fix karma config typo * add karma-closure * add os support * remove typo config * include more closure files * change os back to linux * use closure-library from node_modules * change log level back to INFO * change npm test target to use open browser command instead of karma * change travis test target to use open command instead of karma * list current directory * find what's in current dir * typo command * Update .travis.yml * typo again * open right index.html * use right path for index.html * xdg-open to open default browser on travis * exit browser after 5s wait * change timeout to 1 min * exit after opening up browser * use browser only * use karma * remove un-needed dependency * clean up script section * fix typo * update build status on readme * initial commit for selenium integration tests * update selenium jar path * fix test_runner.js typo * add more debug info * check java version * add && instead of 9288 * fix java path * add logic to check if selenium is running or not * add some deugging info * initial commit to get chromedriver * add chromedriver flag * add get_chromedriver.sh to package.json and .travel * change browser to chrome for now * fix path issue * update chromdriver path * fix path issue again * more debugging * add debug msg * fix typo * minor fix for getting chromedriver * install latest chrome browser * clean up pakcage.json * use npm target for test run * remove removing trailing comma * fix another trailing comma * updated travis test target * clean up scripts * not sure nmp run preinstall * redirect selenium log to tmp file * revert writing console log to file * update test summary * more clean up * minor clean up before pull request * resolved closure-library conflict 1. add closure-library to dependencies instead of devDependencies. 2. add lint back in scripts block * fix typo (adding comma) in script section * Renames Blockly.workspaceDragSurface to Blockly.WorkspaceDragSurface. Fixes #880. * Ensure useDragSurface is a boolean. Fixed #988 * use pretest instead of preinstall in package.json (#1043) * cherry pick for pretest fix * put pretest target to test_setup.sh * fix conflict * cherry pick for get_chromedriver.sh * add some sleep to wait download to finish * use node.js stable * use npm test target * field_angle renders degree symbol consistently. Fixes #973 * bumpNeighbours_ function moved to block_svg. Fixed #1009 * Update RegEx in js-to-json to match windowi eol (#1050) The current regex only works with the "\n" line endings as it expects no characters after the optional ";" at the end of the line. In windows, if it adds the "\r" it counts as a characters and is not part of the line terminator so it doesn't match. * Fix French translation of "colour with rgb" block (#1053) "colorier", which is currently used, is a verb and proposed "couleur" is a noun: the block in question does not change colour of anything, it creates new colour instead, thus noun is more applicable. Also, noun is used in French translation of "random colour" block: "couleur aléatoire". * Enforcing non-empty names on value inputs and statement inputs. (#1054) * Correcting #1054 (#1056) single quotes. better logic. * Created a variable model with name, id, and type. Created a jsunit test file for variable model. * Change how blockly handles cursors. The old way was quite slow becau… (#1057) * Change how blockly handles cursors. The old way was quite slow because it changed the stylesheet directly. See issue #981 for more details on implementation and tradeoffs. This changes makes the following high level changes: deprecate Blockly.Css.setCursor, use built in open and closed hand cursor instead of custom .cur files, add css to draggable objects to set the open and closed hand cursors. * Rebuild blockly_uncompressed to pick up a testing change to make travis happy. Fix a build warning from a multi-line string in the process. (#1059) * Merge master into develop (#1063) - pick up translation changes - clean up trailing spaces * Rebuild for translations
852 lines
26 KiB
JavaScript
852 lines
26 KiB
JavaScript
/**
|
|
* @license
|
|
* Visual Blocks Editor
|
|
*
|
|
* Copyright 2012 Google Inc.
|
|
* https://developers.google.com/blockly/
|
|
*
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
* you may not use this file except in compliance with the License.
|
|
* You may obtain a copy of the License at
|
|
*
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
*
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
* See the License for the specific language governing permissions and
|
|
* limitations under the License.
|
|
*/
|
|
|
|
/**
|
|
* @fileoverview Text blocks for Blockly.
|
|
* @author fraser@google.com (Neil Fraser)
|
|
*/
|
|
'use strict';
|
|
|
|
goog.provide('Blockly.Blocks.texts'); // Deprecated
|
|
goog.provide('Blockly.Constants.Text');
|
|
|
|
goog.require('Blockly.Blocks');
|
|
|
|
|
|
/**
|
|
* Common HSV hue for all blocks in this category.
|
|
* Should be the same as Blockly.Msg.TEXTS_HUE
|
|
* @readonly
|
|
*/
|
|
Blockly.Constants.Text.HUE = 160;
|
|
/** @deprecated Use Blockly.Constants.Text.HUE */
|
|
Blockly.Blocks.texts.HUE = Blockly.Constants.Text.HUE;
|
|
|
|
Blockly.defineBlocksWithJsonArray([ // BEGIN JSON EXTRACT
|
|
// Block for text value
|
|
{
|
|
"type": "text",
|
|
"message0": "%1",
|
|
"args0": [{
|
|
"type": "field_input",
|
|
"name": "TEXT",
|
|
"text": ""
|
|
}],
|
|
"output": "String",
|
|
"colour": "%{BKY_TEXTS_HUE}",
|
|
"helpUrl": "%{BKY_TEXT_TEXT_HELPURL}",
|
|
"tooltip": "%{BKY_TEXT_TEXT_TOOLTIP}",
|
|
"extensions": [
|
|
"text_quotes",
|
|
"parent_tooltip_when_inline"
|
|
]
|
|
}
|
|
]); // END JSON EXTRACT (Do not delete this comment.)
|
|
|
|
/** Wraps TEXT field with images of double quote characters. */
|
|
Blockly.Constants.Text.textQuotesExtension = function() {
|
|
this.mixin(Blockly.Constants.Text.QUOTE_IMAGE_MIXIN);
|
|
this.quoteField_('TEXT');
|
|
};
|
|
|
|
Blockly.Extensions.register('text_quotes',
|
|
Blockly.Constants.Text.textQuotesExtension);
|
|
|
|
Blockly.Blocks['text_join'] = {
|
|
/**
|
|
* Block for creating a string made up of any number of elements of any type.
|
|
* @this Blockly.Block
|
|
*/
|
|
init: function() {
|
|
this.setHelpUrl(Blockly.Msg.TEXT_JOIN_HELPURL);
|
|
this.setColour(Blockly.Blocks.texts.HUE);
|
|
this.itemCount_ = 2;
|
|
this.mixin(Blockly.Constants.Text.QUOTE_IMAGE_MIXIN);
|
|
this.updateShape_();
|
|
this.setOutput(true, 'String');
|
|
this.setMutator(new Blockly.Mutator(['text_create_join_item']));
|
|
this.setTooltip(Blockly.Msg.TEXT_JOIN_TOOLTIP);
|
|
},
|
|
/**
|
|
* Create XML to represent number of text inputs.
|
|
* @return {!Element} XML storage element.
|
|
* @this Blockly.Block
|
|
*/
|
|
mutationToDom: function() {
|
|
var container = document.createElement('mutation');
|
|
container.setAttribute('items', this.itemCount_);
|
|
return container;
|
|
},
|
|
/**
|
|
* Parse XML to restore the text inputs.
|
|
* @param {!Element} xmlElement XML storage element.
|
|
* @this Blockly.Block
|
|
*/
|
|
domToMutation: function(xmlElement) {
|
|
this.itemCount_ = parseInt(xmlElement.getAttribute('items'), 10);
|
|
this.updateShape_();
|
|
},
|
|
/**
|
|
* Populate the mutator's dialog with this block's components.
|
|
* @param {!Blockly.Workspace} workspace Mutator's workspace.
|
|
* @return {!Blockly.Block} Root block in mutator.
|
|
* @this Blockly.Block
|
|
*/
|
|
decompose: function(workspace) {
|
|
var containerBlock = workspace.newBlock('text_create_join_container');
|
|
containerBlock.initSvg();
|
|
var connection = containerBlock.getInput('STACK').connection;
|
|
for (var i = 0; i < this.itemCount_; i++) {
|
|
var itemBlock = workspace.newBlock('text_create_join_item');
|
|
itemBlock.initSvg();
|
|
connection.connect(itemBlock.previousConnection);
|
|
connection = itemBlock.nextConnection;
|
|
}
|
|
return containerBlock;
|
|
},
|
|
/**
|
|
* Reconfigure this block based on the mutator dialog's components.
|
|
* @param {!Blockly.Block} containerBlock Root block in mutator.
|
|
* @this Blockly.Block
|
|
*/
|
|
compose: function(containerBlock) {
|
|
var itemBlock = containerBlock.getInputTargetBlock('STACK');
|
|
// Count number of inputs.
|
|
var connections = [];
|
|
while (itemBlock) {
|
|
connections.push(itemBlock.valueConnection_);
|
|
itemBlock = itemBlock.nextConnection &&
|
|
itemBlock.nextConnection.targetBlock();
|
|
}
|
|
// Disconnect any children that don't belong.
|
|
for (var i = 0; i < this.itemCount_; i++) {
|
|
var connection = this.getInput('ADD' + i).connection.targetConnection;
|
|
if (connection && connections.indexOf(connection) == -1) {
|
|
connection.disconnect();
|
|
}
|
|
}
|
|
this.itemCount_ = connections.length;
|
|
this.updateShape_();
|
|
// Reconnect any child blocks.
|
|
for (var i = 0; i < this.itemCount_; i++) {
|
|
Blockly.Mutator.reconnect(connections[i], this, 'ADD' + i);
|
|
}
|
|
},
|
|
/**
|
|
* Store pointers to any connected child blocks.
|
|
* @param {!Blockly.Block} containerBlock Root block in mutator.
|
|
* @this Blockly.Block
|
|
*/
|
|
saveConnections: function(containerBlock) {
|
|
var itemBlock = containerBlock.getInputTargetBlock('STACK');
|
|
var i = 0;
|
|
while (itemBlock) {
|
|
var input = this.getInput('ADD' + i);
|
|
itemBlock.valueConnection_ = input && input.connection.targetConnection;
|
|
i++;
|
|
itemBlock = itemBlock.nextConnection &&
|
|
itemBlock.nextConnection.targetBlock();
|
|
}
|
|
},
|
|
/**
|
|
* Modify this block to have the correct number of inputs.
|
|
* @private
|
|
* @this Blockly.Block
|
|
*/
|
|
updateShape_: function() {
|
|
if (this.itemCount_ && this.getInput('EMPTY')) {
|
|
this.removeInput('EMPTY');
|
|
} else if (!this.itemCount_ && !this.getInput('EMPTY')) {
|
|
this.appendDummyInput('EMPTY')
|
|
.appendField(this.newQuote_(true))
|
|
.appendField(this.newQuote_(false));
|
|
}
|
|
// Add new inputs.
|
|
for (var i = 0; i < this.itemCount_; i++) {
|
|
if (!this.getInput('ADD' + i)) {
|
|
var input = this.appendValueInput('ADD' + i);
|
|
if (i == 0) {
|
|
input.appendField(Blockly.Msg.TEXT_JOIN_TITLE_CREATEWITH);
|
|
}
|
|
}
|
|
}
|
|
// Remove deleted inputs.
|
|
while (this.getInput('ADD' + i)) {
|
|
this.removeInput('ADD' + i);
|
|
i++;
|
|
}
|
|
}
|
|
};
|
|
|
|
Blockly.Blocks['text_create_join_container'] = {
|
|
/**
|
|
* Mutator block for container.
|
|
* @this Blockly.Block
|
|
*/
|
|
init: function() {
|
|
this.setColour(Blockly.Blocks.texts.HUE);
|
|
this.appendDummyInput()
|
|
.appendField(Blockly.Msg.TEXT_CREATE_JOIN_TITLE_JOIN);
|
|
this.appendStatementInput('STACK');
|
|
this.setTooltip(Blockly.Msg.TEXT_CREATE_JOIN_TOOLTIP);
|
|
this.contextMenu = false;
|
|
}
|
|
};
|
|
|
|
Blockly.Blocks['text_create_join_item'] = {
|
|
/**
|
|
* Mutator block for add items.
|
|
* @this Blockly.Block
|
|
*/
|
|
init: function() {
|
|
this.setColour(Blockly.Blocks.texts.HUE);
|
|
this.appendDummyInput()
|
|
.appendField(Blockly.Msg.TEXT_CREATE_JOIN_ITEM_TITLE_ITEM);
|
|
this.setPreviousStatement(true);
|
|
this.setNextStatement(true);
|
|
this.setTooltip(Blockly.Msg.TEXT_CREATE_JOIN_ITEM_TOOLTIP);
|
|
this.contextMenu = false;
|
|
}
|
|
};
|
|
|
|
Blockly.Blocks['text_append'] = {
|
|
/**
|
|
* Block for appending to a variable in place.
|
|
* @this Blockly.Block
|
|
*/
|
|
init: function() {
|
|
this.setHelpUrl(Blockly.Msg.TEXT_APPEND_HELPURL);
|
|
this.setColour(Blockly.Blocks.texts.HUE);
|
|
this.appendValueInput('TEXT')
|
|
.appendField(Blockly.Msg.TEXT_APPEND_TO)
|
|
.appendField(new Blockly.FieldVariable(
|
|
Blockly.Msg.TEXT_APPEND_VARIABLE), 'VAR')
|
|
.appendField(Blockly.Msg.TEXT_APPEND_APPENDTEXT);
|
|
this.setPreviousStatement(true);
|
|
this.setNextStatement(true);
|
|
// Assign 'this' to a variable for use in the tooltip closure below.
|
|
var thisBlock = this;
|
|
this.setTooltip(function() {
|
|
return Blockly.Msg.TEXT_APPEND_TOOLTIP.replace('%1',
|
|
thisBlock.getFieldValue('VAR'));
|
|
});
|
|
}
|
|
};
|
|
|
|
Blockly.Blocks['text_length'] = {
|
|
/**
|
|
* Block for string length.
|
|
* @this Blockly.Block
|
|
*/
|
|
init: function() {
|
|
this.jsonInit({
|
|
"message0": Blockly.Msg.TEXT_LENGTH_TITLE,
|
|
"args0": [
|
|
{
|
|
"type": "input_value",
|
|
"name": "VALUE",
|
|
"check": ['String', 'Array']
|
|
}
|
|
],
|
|
"output": 'Number',
|
|
"colour": Blockly.Blocks.texts.HUE,
|
|
"tooltip": Blockly.Msg.TEXT_LENGTH_TOOLTIP,
|
|
"helpUrl": Blockly.Msg.TEXT_LENGTH_HELPURL
|
|
});
|
|
}
|
|
};
|
|
|
|
Blockly.Blocks['text_isEmpty'] = {
|
|
/**
|
|
* Block for is the string null?
|
|
* @this Blockly.Block
|
|
*/
|
|
init: function() {
|
|
this.jsonInit({
|
|
"message0": Blockly.Msg.TEXT_ISEMPTY_TITLE,
|
|
"args0": [
|
|
{
|
|
"type": "input_value",
|
|
"name": "VALUE",
|
|
"check": ['String', 'Array']
|
|
}
|
|
],
|
|
"output": 'Boolean',
|
|
"colour": Blockly.Blocks.texts.HUE,
|
|
"tooltip": Blockly.Msg.TEXT_ISEMPTY_TOOLTIP,
|
|
"helpUrl": Blockly.Msg.TEXT_ISEMPTY_HELPURL
|
|
});
|
|
}
|
|
};
|
|
|
|
Blockly.Blocks['text_indexOf'] = {
|
|
/**
|
|
* Block for finding a substring in the text.
|
|
* @this Blockly.Block
|
|
*/
|
|
init: function() {
|
|
var OPERATORS = [
|
|
[Blockly.Msg.TEXT_INDEXOF_OPERATOR_FIRST, 'FIRST'],
|
|
[Blockly.Msg.TEXT_INDEXOF_OPERATOR_LAST, 'LAST']
|
|
];
|
|
this.setHelpUrl(Blockly.Msg.TEXT_INDEXOF_HELPURL);
|
|
this.setColour(Blockly.Blocks.texts.HUE);
|
|
this.setOutput(true, 'Number');
|
|
this.appendValueInput('VALUE')
|
|
.setCheck('String')
|
|
.appendField(Blockly.Msg.TEXT_INDEXOF_INPUT_INTEXT);
|
|
this.appendValueInput('FIND')
|
|
.setCheck('String')
|
|
.appendField(new Blockly.FieldDropdown(OPERATORS), 'END');
|
|
if (Blockly.Msg.TEXT_INDEXOF_TAIL) {
|
|
this.appendDummyInput().appendField(Blockly.Msg.TEXT_INDEXOF_TAIL);
|
|
}
|
|
this.setInputsInline(true);
|
|
// 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');
|
|
});
|
|
}
|
|
};
|
|
|
|
Blockly.Blocks['text_charAt'] = {
|
|
/**
|
|
* Block for getting a character from the string.
|
|
* @this Blockly.Block
|
|
*/
|
|
init: function() {
|
|
this.WHERE_OPTIONS = [
|
|
[Blockly.Msg.TEXT_CHARAT_FROM_START, 'FROM_START'],
|
|
[Blockly.Msg.TEXT_CHARAT_FROM_END, 'FROM_END'],
|
|
[Blockly.Msg.TEXT_CHARAT_FIRST, 'FIRST'],
|
|
[Blockly.Msg.TEXT_CHARAT_LAST, 'LAST'],
|
|
[Blockly.Msg.TEXT_CHARAT_RANDOM, 'RANDOM']
|
|
];
|
|
this.setHelpUrl(Blockly.Msg.TEXT_CHARAT_HELPURL);
|
|
this.setColour(Blockly.Blocks.texts.HUE);
|
|
this.setOutput(true, 'String');
|
|
this.appendValueInput('VALUE')
|
|
.setCheck('String')
|
|
.appendField(Blockly.Msg.TEXT_CHARAT_INPUT_INTEXT);
|
|
this.appendDummyInput('AT');
|
|
this.setInputsInline(true);
|
|
this.updateAt_(true);
|
|
// Assign 'this' to a variable for use in the tooltip closure below.
|
|
var thisBlock = this;
|
|
this.setTooltip(function() {
|
|
var where = thisBlock.getFieldValue('WHERE');
|
|
var tooltip = Blockly.Msg.TEXT_CHARAT_TOOLTIP;
|
|
if (where == 'FROM_START' || where == 'FROM_END') {
|
|
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;
|
|
});
|
|
},
|
|
/**
|
|
* Create XML to represent whether there is an 'AT' input.
|
|
* @return {!Element} XML storage element.
|
|
* @this Blockly.Block
|
|
*/
|
|
mutationToDom: function() {
|
|
var container = document.createElement('mutation');
|
|
var isAt = this.getInput('AT').type == Blockly.INPUT_VALUE;
|
|
container.setAttribute('at', isAt);
|
|
return container;
|
|
},
|
|
/**
|
|
* Parse XML to restore the 'AT' input.
|
|
* @param {!Element} xmlElement XML storage element.
|
|
* @this Blockly.Block
|
|
*/
|
|
domToMutation: function(xmlElement) {
|
|
// Note: Until January 2013 this block did not have mutations,
|
|
// so 'at' defaults to true.
|
|
var isAt = (xmlElement.getAttribute('at') != 'false');
|
|
this.updateAt_(isAt);
|
|
},
|
|
/**
|
|
* Create or delete an input for the numeric index.
|
|
* @param {boolean} isAt True if the input should exist.
|
|
* @private
|
|
* @this Blockly.Block
|
|
*/
|
|
updateAt_: function(isAt) {
|
|
// Destroy old 'AT' and 'ORDINAL' inputs.
|
|
this.removeInput('AT');
|
|
this.removeInput('ORDINAL', true);
|
|
// Create either a value 'AT' input or a dummy input.
|
|
if (isAt) {
|
|
this.appendValueInput('AT').setCheck('Number');
|
|
if (Blockly.Msg.ORDINAL_NUMBER_SUFFIX) {
|
|
this.appendDummyInput('ORDINAL')
|
|
.appendField(Blockly.Msg.ORDINAL_NUMBER_SUFFIX);
|
|
}
|
|
} else {
|
|
this.appendDummyInput('AT');
|
|
}
|
|
if (Blockly.Msg.TEXT_CHARAT_TAIL) {
|
|
this.removeInput('TAIL', true);
|
|
this.appendDummyInput('TAIL')
|
|
.appendField(Blockly.Msg.TEXT_CHARAT_TAIL);
|
|
}
|
|
var menu = new Blockly.FieldDropdown(this.WHERE_OPTIONS, function(value) {
|
|
var newAt = (value == 'FROM_START') || (value == 'FROM_END');
|
|
// The 'isAt' variable is available due to this function being a closure.
|
|
if (newAt != isAt) {
|
|
var block = this.sourceBlock_;
|
|
block.updateAt_(newAt);
|
|
// This menu has been destroyed and replaced. Update the replacement.
|
|
block.setFieldValue(value, 'WHERE');
|
|
return null;
|
|
}
|
|
return undefined;
|
|
});
|
|
this.getInput('AT').appendField(menu, 'WHERE');
|
|
}
|
|
};
|
|
|
|
Blockly.Blocks['text_getSubstring'] = {
|
|
/**
|
|
* Block for getting substring.
|
|
* @this Blockly.Block
|
|
*/
|
|
init: function() {
|
|
this['WHERE_OPTIONS_1'] = [
|
|
[Blockly.Msg.TEXT_GET_SUBSTRING_START_FROM_START, 'FROM_START'],
|
|
[Blockly.Msg.TEXT_GET_SUBSTRING_START_FROM_END, 'FROM_END'],
|
|
[Blockly.Msg.TEXT_GET_SUBSTRING_START_FIRST, 'FIRST']
|
|
];
|
|
this['WHERE_OPTIONS_2'] = [
|
|
[Blockly.Msg.TEXT_GET_SUBSTRING_END_FROM_START, 'FROM_START'],
|
|
[Blockly.Msg.TEXT_GET_SUBSTRING_END_FROM_END, 'FROM_END'],
|
|
[Blockly.Msg.TEXT_GET_SUBSTRING_END_LAST, 'LAST']
|
|
];
|
|
this.setHelpUrl(Blockly.Msg.TEXT_GET_SUBSTRING_HELPURL);
|
|
this.setColour(Blockly.Blocks.texts.HUE);
|
|
this.appendValueInput('STRING')
|
|
.setCheck('String')
|
|
.appendField(Blockly.Msg.TEXT_GET_SUBSTRING_INPUT_IN_TEXT);
|
|
this.appendDummyInput('AT1');
|
|
this.appendDummyInput('AT2');
|
|
if (Blockly.Msg.TEXT_GET_SUBSTRING_TAIL) {
|
|
this.appendDummyInput('TAIL')
|
|
.appendField(Blockly.Msg.TEXT_GET_SUBSTRING_TAIL);
|
|
}
|
|
this.setInputsInline(true);
|
|
this.setOutput(true, 'String');
|
|
this.updateAt_(1, true);
|
|
this.updateAt_(2, true);
|
|
this.setTooltip(Blockly.Msg.TEXT_GET_SUBSTRING_TOOLTIP);
|
|
},
|
|
/**
|
|
* Create XML to represent whether there are 'AT' inputs.
|
|
* @return {!Element} XML storage element.
|
|
* @this Blockly.Block
|
|
*/
|
|
mutationToDom: function() {
|
|
var container = document.createElement('mutation');
|
|
var isAt1 = this.getInput('AT1').type == Blockly.INPUT_VALUE;
|
|
container.setAttribute('at1', isAt1);
|
|
var isAt2 = this.getInput('AT2').type == Blockly.INPUT_VALUE;
|
|
container.setAttribute('at2', isAt2);
|
|
return container;
|
|
},
|
|
/**
|
|
* Parse XML to restore the 'AT' inputs.
|
|
* @param {!Element} xmlElement XML storage element.
|
|
* @this Blockly.Block
|
|
*/
|
|
domToMutation: function(xmlElement) {
|
|
var isAt1 = (xmlElement.getAttribute('at1') == 'true');
|
|
var isAt2 = (xmlElement.getAttribute('at2') == 'true');
|
|
this.updateAt_(1, isAt1);
|
|
this.updateAt_(2, isAt2);
|
|
},
|
|
/**
|
|
* Create or delete an input for a numeric index.
|
|
* This block has two such inputs, independant of each other.
|
|
* @param {number} n Specify first or second input (1 or 2).
|
|
* @param {boolean} isAt True if the input should exist.
|
|
* @private
|
|
* @this Blockly.Block
|
|
*/
|
|
updateAt_: function(n, isAt) {
|
|
// Create or delete an input for the numeric index.
|
|
// Destroy old 'AT' and 'ORDINAL' inputs.
|
|
this.removeInput('AT' + n);
|
|
this.removeInput('ORDINAL' + n, true);
|
|
// Create either a value 'AT' input or a dummy input.
|
|
if (isAt) {
|
|
this.appendValueInput('AT' + n).setCheck('Number');
|
|
if (Blockly.Msg.ORDINAL_NUMBER_SUFFIX) {
|
|
this.appendDummyInput('ORDINAL' + n)
|
|
.appendField(Blockly.Msg.ORDINAL_NUMBER_SUFFIX);
|
|
}
|
|
} else {
|
|
this.appendDummyInput('AT' + n);
|
|
}
|
|
// Move tail, if present, to end of block.
|
|
if (n == 2 && Blockly.Msg.TEXT_GET_SUBSTRING_TAIL) {
|
|
this.removeInput('TAIL', true);
|
|
this.appendDummyInput('TAIL')
|
|
.appendField(Blockly.Msg.TEXT_GET_SUBSTRING_TAIL);
|
|
}
|
|
var menu = new Blockly.FieldDropdown(this['WHERE_OPTIONS_' + n],
|
|
function(value) {
|
|
var newAt = (value == 'FROM_START') || (value == 'FROM_END');
|
|
// The 'isAt' variable is available due to this function being a
|
|
// closure.
|
|
if (newAt != isAt) {
|
|
var block = this.sourceBlock_;
|
|
block.updateAt_(n, newAt);
|
|
// This menu has been destroyed and replaced.
|
|
// Update the replacement.
|
|
block.setFieldValue(value, 'WHERE' + n);
|
|
return null;
|
|
}
|
|
return undefined;
|
|
});
|
|
|
|
this.getInput('AT' + n)
|
|
.appendField(menu, 'WHERE' + n);
|
|
if (n == 1) {
|
|
this.moveInputBefore('AT1', 'AT2');
|
|
}
|
|
}
|
|
};
|
|
|
|
Blockly.Blocks['text_changeCase'] = {
|
|
/**
|
|
* Block for changing capitalization.
|
|
* @this Blockly.Block
|
|
*/
|
|
init: function() {
|
|
var OPERATORS = [
|
|
[Blockly.Msg.TEXT_CHANGECASE_OPERATOR_UPPERCASE, 'UPPERCASE'],
|
|
[Blockly.Msg.TEXT_CHANGECASE_OPERATOR_LOWERCASE, 'LOWERCASE'],
|
|
[Blockly.Msg.TEXT_CHANGECASE_OPERATOR_TITLECASE, 'TITLECASE']
|
|
];
|
|
this.setHelpUrl(Blockly.Msg.TEXT_CHANGECASE_HELPURL);
|
|
this.setColour(Blockly.Blocks.texts.HUE);
|
|
this.appendValueInput('TEXT')
|
|
.setCheck('String')
|
|
.appendField(new Blockly.FieldDropdown(OPERATORS), 'CASE');
|
|
this.setOutput(true, 'String');
|
|
this.setTooltip(Blockly.Msg.TEXT_CHANGECASE_TOOLTIP);
|
|
}
|
|
};
|
|
|
|
Blockly.Blocks['text_trim'] = {
|
|
/**
|
|
* Block for trimming spaces.
|
|
* @this Blockly.Block
|
|
*/
|
|
init: function() {
|
|
var OPERATORS = [
|
|
[Blockly.Msg.TEXT_TRIM_OPERATOR_BOTH, 'BOTH'],
|
|
[Blockly.Msg.TEXT_TRIM_OPERATOR_LEFT, 'LEFT'],
|
|
[Blockly.Msg.TEXT_TRIM_OPERATOR_RIGHT, 'RIGHT']
|
|
];
|
|
this.setHelpUrl(Blockly.Msg.TEXT_TRIM_HELPURL);
|
|
this.setColour(Blockly.Blocks.texts.HUE);
|
|
this.appendValueInput('TEXT')
|
|
.setCheck('String')
|
|
.appendField(new Blockly.FieldDropdown(OPERATORS), 'MODE');
|
|
this.setOutput(true, 'String');
|
|
this.setTooltip(Blockly.Msg.TEXT_TRIM_TOOLTIP);
|
|
}
|
|
};
|
|
|
|
Blockly.Blocks['text_print'] = {
|
|
/**
|
|
* Block for print statement.
|
|
* @this Blockly.Block
|
|
*/
|
|
init: function() {
|
|
this.jsonInit({
|
|
"message0": Blockly.Msg.TEXT_PRINT_TITLE,
|
|
"args0": [
|
|
{
|
|
"type": "input_value",
|
|
"name": "TEXT"
|
|
}
|
|
],
|
|
"previousStatement": null,
|
|
"nextStatement": null,
|
|
"colour": Blockly.Blocks.texts.HUE,
|
|
"tooltip": Blockly.Msg.TEXT_PRINT_TOOLTIP,
|
|
"helpUrl": Blockly.Msg.TEXT_PRINT_HELPURL
|
|
});
|
|
}
|
|
};
|
|
|
|
Blockly.Blocks['text_prompt_ext'] = {
|
|
/**
|
|
* Block for prompt function (external message).
|
|
* @this Blockly.Block
|
|
*/
|
|
init: function() {
|
|
var TYPES = [
|
|
[Blockly.Msg.TEXT_PROMPT_TYPE_TEXT, 'TEXT'],
|
|
[Blockly.Msg.TEXT_PROMPT_TYPE_NUMBER, 'NUMBER']
|
|
];
|
|
this.setHelpUrl(Blockly.Msg.TEXT_PROMPT_HELPURL);
|
|
this.setColour(Blockly.Blocks.texts.HUE);
|
|
// Assign 'this' to a variable for use in the closures below.
|
|
var thisBlock = this;
|
|
var dropdown = new Blockly.FieldDropdown(TYPES, function(newOp) {
|
|
thisBlock.updateType_(newOp);
|
|
});
|
|
this.appendValueInput('TEXT')
|
|
.appendField(dropdown, 'TYPE');
|
|
this.setOutput(true, 'String');
|
|
this.setTooltip(function() {
|
|
return (thisBlock.getFieldValue('TYPE') == 'TEXT') ?
|
|
Blockly.Msg.TEXT_PROMPT_TOOLTIP_TEXT :
|
|
Blockly.Msg.TEXT_PROMPT_TOOLTIP_NUMBER;
|
|
});
|
|
},
|
|
/**
|
|
* Modify this block to have the correct output type.
|
|
* @param {string} newOp Either 'TEXT' or 'NUMBER'.
|
|
* @private
|
|
* @this Blockly.Block
|
|
*/
|
|
updateType_: function(newOp) {
|
|
this.outputConnection.setCheck(newOp == 'NUMBER' ? 'Number' : 'String');
|
|
},
|
|
/**
|
|
* Create XML to represent the output type.
|
|
* @return {!Element} XML storage element.
|
|
* @this Blockly.Block
|
|
*/
|
|
mutationToDom: function() {
|
|
var container = document.createElement('mutation');
|
|
container.setAttribute('type', this.getFieldValue('TYPE'));
|
|
return container;
|
|
},
|
|
/**
|
|
* Parse XML to restore the output type.
|
|
* @param {!Element} xmlElement XML storage element.
|
|
* @this Blockly.Block
|
|
*/
|
|
domToMutation: function(xmlElement) {
|
|
this.updateType_(xmlElement.getAttribute('type'));
|
|
}
|
|
};
|
|
|
|
Blockly.Blocks['text_prompt'] = {
|
|
/**
|
|
* Block for prompt function (internal message).
|
|
* The 'text_prompt_ext' block is preferred as it is more flexible.
|
|
* @this Blockly.Block
|
|
*/
|
|
init: function() {
|
|
this.mixin(Blockly.Constants.Text.QUOTE_IMAGE_MIXIN);
|
|
var TYPES = [
|
|
[Blockly.Msg.TEXT_PROMPT_TYPE_TEXT, 'TEXT'],
|
|
[Blockly.Msg.TEXT_PROMPT_TYPE_NUMBER, 'NUMBER']
|
|
];
|
|
|
|
// Assign 'this' to a variable for use in the closures below.
|
|
var thisBlock = this;
|
|
this.setHelpUrl(Blockly.Msg.TEXT_PROMPT_HELPURL);
|
|
this.setColour(Blockly.Blocks.texts.HUE);
|
|
var dropdown = new Blockly.FieldDropdown(TYPES, function(newOp) {
|
|
thisBlock.updateType_(newOp);
|
|
});
|
|
this.appendDummyInput()
|
|
.appendField(dropdown, 'TYPE')
|
|
.appendField(this.newQuote_(true))
|
|
.appendField(new Blockly.FieldTextInput(''), 'TEXT')
|
|
.appendField(this.newQuote_(false));
|
|
this.setOutput(true, 'String');
|
|
this.setTooltip(function() {
|
|
return (thisBlock.getFieldValue('TYPE') == 'TEXT') ?
|
|
Blockly.Msg.TEXT_PROMPT_TOOLTIP_TEXT :
|
|
Blockly.Msg.TEXT_PROMPT_TOOLTIP_NUMBER;
|
|
});
|
|
},
|
|
updateType_: Blockly.Blocks['text_prompt_ext'].updateType_,
|
|
mutationToDom: Blockly.Blocks['text_prompt_ext'].mutationToDom,
|
|
domToMutation: Blockly.Blocks['text_prompt_ext'].domToMutation
|
|
};
|
|
|
|
Blockly.Blocks['text_count'] = {
|
|
/**
|
|
* Block for counting how many times one string appears within another string.
|
|
* @this Blockly.Block
|
|
*/
|
|
init: function() {
|
|
this.jsonInit({
|
|
"message0": Blockly.Msg.TEXT_COUNT_MESSAGE0,
|
|
"args0": [
|
|
{
|
|
"type": "input_value",
|
|
"name": "SUB",
|
|
"check": "String"
|
|
},
|
|
{
|
|
"type": "input_value",
|
|
"name": "TEXT",
|
|
"check": "String"
|
|
}
|
|
],
|
|
"output": "Number",
|
|
"inputsInline": true,
|
|
"colour": Blockly.Blocks.texts.HUE,
|
|
"tooltip": Blockly.Msg.TEXT_COUNT_TOOLTIP,
|
|
"helpUrl": Blockly.Msg.TEXT_COUNT_HELPURL
|
|
});
|
|
}
|
|
};
|
|
|
|
Blockly.Blocks['text_replace'] = {
|
|
/**
|
|
* Block for replacing one string with another in the text.
|
|
* @this Blockly.Block
|
|
*/
|
|
init: function() {
|
|
this.jsonInit({
|
|
"message0": Blockly.Msg.TEXT_REPLACE_MESSAGE0,
|
|
"args0": [
|
|
{
|
|
"type": "input_value",
|
|
"name": "FROM",
|
|
"check": "String"
|
|
},
|
|
{
|
|
"type": "input_value",
|
|
"name": "TO",
|
|
"check": "String"
|
|
},
|
|
{
|
|
"type": "input_value",
|
|
"name": "TEXT",
|
|
"check": "String"
|
|
}
|
|
],
|
|
"output": "String",
|
|
"inputsInline": true,
|
|
"colour": Blockly.Blocks.texts.HUE,
|
|
"tooltip": Blockly.Msg.TEXT_REPLACE_TOOLTIP,
|
|
"helpUrl": Blockly.Msg.TEXT_REPLACE_HELPURL
|
|
});
|
|
}
|
|
};
|
|
|
|
Blockly.Blocks['text_reverse'] = {
|
|
/**
|
|
* Block for reversing a string.
|
|
* @this Blockly.Block
|
|
*/
|
|
init: function() {
|
|
this.jsonInit({
|
|
"message0": Blockly.Msg.TEXT_REVERSE_MESSAGE0,
|
|
"args0": [
|
|
{
|
|
"type": "input_value",
|
|
"name": "TEXT",
|
|
"check": "String"
|
|
}
|
|
],
|
|
"output": "String",
|
|
"inputsInline": true,
|
|
"colour": Blockly.Blocks.texts.HUE,
|
|
"tooltip": Blockly.Msg.TEXT_REVERSE_TOOLTIP,
|
|
"helpUrl": Blockly.Msg.TEXT_REVERSE_HELPURL
|
|
});
|
|
}
|
|
};
|
|
|
|
/**
|
|
*
|
|
* @mixin
|
|
* @package
|
|
* @readonly
|
|
*/
|
|
Blockly.Constants.Text.QUOTE_IMAGE_MIXIN = {
|
|
/**
|
|
* Image data URI of an LTR opening double quote (same as RTL closing couble quote).
|
|
* @readonly
|
|
*/
|
|
QUOTE_IMAGE_LEFT_DATAURI:
|
|
'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAwAAAAKCAQAAAAqJXdxAAAAn0lEQVQI1z3OMa5BURSF4f/cQhAKjUQhuQmFNwGJEUi0RKN5rU7FHKhpjEH3TEMtkdBSCY1EIv8r7nFX9e29V7EBAOvu7RPjwmWGH/VuF8CyN9/OAdvqIXYLvtRaNjx9mMTDyo+NjAN1HNcl9ZQ5oQMM3dgDUqDo1l8DzvwmtZN7mnD+PkmLa+4mhrxVA9fRowBWmVBhFy5gYEjKMfz9AylsaRRgGzvZAAAAAElFTkSuQmCC',
|
|
/**
|
|
* Image data URI of an LTR closing double quote (same as RTL opening couble quote).
|
|
* @readonly
|
|
*/
|
|
QUOTE_IMAGE_RIGHT_DATAURI:
|
|
'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAwAAAAKCAQAAAAqJXdxAAAAqUlEQVQI1z3KvUpCcRiA8ef9E4JNHhI0aFEacm1o0BsI0Slx8wa8gLauoDnoBhq7DcfWhggONDmJJgqCPA7neJ7p934EOOKOnM8Q7PDElo/4x4lFb2DmuUjcUzS3URnGib9qaPNbuXvBO3sGPHJDRG6fGVdMSeWDP2q99FQdFrz26Gu5Tq7dFMzUvbXy8KXeAj57cOklgA+u1B5AoslLtGIHQMaCVnwDnADZIFIrXsoXrgAAAABJRU5ErkJggg==',
|
|
/**
|
|
* Pixel width of QUOTE_IMAGE_LEFT_DATAURI and QUOTE_IMAGE_RIGHT_DATAURI.
|
|
* @readonly
|
|
*/
|
|
QUOTE_IMAGE_WIDTH: 12,
|
|
/**
|
|
* Pixel height of QUOTE_IMAGE_LEFT_DATAURI and QUOTE_IMAGE_RIGHT_DATAURI.
|
|
* @readonly
|
|
*/
|
|
QUOTE_IMAGE_HEIGHT: 12,
|
|
|
|
/**
|
|
* Inserts appropriate quote images before and after the named field.
|
|
* @param {string} fieldName The name of the field to wrap with quotes.
|
|
*/
|
|
quoteField_: function(fieldName) {
|
|
for (var i = 0, input; input = this.inputList[i]; i++) {
|
|
for (var j = 0, field; field = input.fieldRow[j]; j++) {
|
|
if (fieldName == field.name) {
|
|
input.insertFieldAt(j, this.newQuote_(true));
|
|
input.insertFieldAt(j + 2, this.newQuote_(false));
|
|
return;
|
|
}
|
|
}
|
|
}
|
|
console.warn('field named "' + fieldName + '" not found in ' + this.toDevString());
|
|
},
|
|
|
|
/**
|
|
* A helper function that generates a FieldImage of an opening or
|
|
* closing double quote. The selected quote will be adapted for RTL blocks.
|
|
* @param {boolean} open If the image should be open quote (“ in LTR).
|
|
* Otherwise, a closing quote is used (” in LTR).
|
|
* @returns {!Blockly.FieldImage} The new field.
|
|
*/
|
|
newQuote_: function(open) {
|
|
var isLeft = this.RTL? !open : open;
|
|
var dataUri = isLeft ?
|
|
this.QUOTE_IMAGE_LEFT_DATAURI :
|
|
this.QUOTE_IMAGE_RIGHT_DATAURI;
|
|
return new Blockly.FieldImage(
|
|
dataUri,
|
|
this.QUOTE_IMAGE_WIDTH,
|
|
this.QUOTE_IMAGE_HEIGHT,
|
|
isLeft ? '\u201C' : '\u201D');
|
|
}
|
|
};
|
|
|