mirror of
https://github.com/scratchfoundation/scratch-blocks.git
synced 2025-08-28 22:10:31 -04:00
Feature/merge feb 2017 (#791)
* Revert "Rebuild nov 3 16" * Move injected css to start of head * simplification * lint * Remove copy/paste buttons. * Localisation updates from https://translatewiki.net. * Don't split dropdown text if there is an image. * Unblock push to master. * Revert "Revert "Rebuild nov 3 16"" This reverts commitc8ca24a000
. * rebuild * Remove ifelse block and messages' * Remove obsolete Gecko image hack. Apparently this has been fixed in Gecko. * Add correct focus behavior for the modal. Update boundary sounds. * Disallow clicks on disabled buttons. * add back metadata tag to qqq * revert qqq.json * Improve performance of block dragging. This is a backport of the blo… (#732) Improve performance of block dragging. This is a backport of the block drag surface from scratch-blocks. At the beginning of a block drag, blocks get moved to a drag surface which then translates using translate3d to avoid repainting the entire svg on every mouse move. At the end of the drag, the blocks are dropped back in the svg in their new position. * API-breaking cleanup. But doubtful anyone will be affected. (#748) * Make add/removeClass return whether they did anything. * Move more functions onto utils. * Move bind functions to Blockly. * Routine recompile. * String reference in JSON string messages (#741) * Adds message references to message string interpolation, in the form of %{BKY_STRING}. * Re-adding CONTROLS_IFELSE block using the new syntax, referencing to CONTROL_IF equivalents. * Fix compiler errors. * Break the sidebar out into its own individual component. * Hide notification messages after a short time interval. * Fix selection border on blocks that have been highlighted. * controls_ifelse: Remove right-align. Remove Boolean check on statements. (#749) * Move away from using a common modal service, since the block options and the toolbox modals are going to end up behaving fairly differently. * Fix conflict between 'utils' and 'image dropdown' merges. * Add a contextual modal for the toolbox. * Fix some bugs arising in the toolbox modal for the no-categories case. * Allow attaching blocks to a marked spot from the toolbox modal. This is the last prerequisite for removal of the existing on-screen toolbox. * Delete the on-screen toolbox. * Add warning sounds when the user reaches a boundary of the workspace. * Stop some blocks from throwing errors in headless workspaces. * Lint * Fix speling. * Fix broken highlighting when highlighted block is deleted. Issue 752. * When the workspace is empty, make it easy for the user to add a new group of blocks to it. * Handle the finer points for setting focus correctly after deleting blocks from the workspace. * When user edits text in a field, set text, not value. Existing text-editable fields don’t care (dropdown care, but are not text-editable). But a note picker needs to set its value to 60 if text is set to ‘C4’. * Set the text not the value when closing a text editor. Also rename variables for clarity. * Localisation updates from https://translatewiki.net. * Streamline the logic for block selection callbacks in the toolbox modal. * Do not show disabled actions in the block options modal. * Set focus correctly when toolbox modal is dismissed. * Add information regarding target screen reader and browser. * Rebuild Blockly. * Remove unavailable blocks from toolbox modal. Hide unnecessary category name in a toolbox without categories. * Do some refactoring and tidy-up. Pull some hardcoded strings out for i18n purposes; remove unused strings. * Update config options for sidebar buttons. * Minor refactoring. Remove unused dependencies. * Improve styling of sidebar buttons. * Remove clipboard functionality. * Refactor and simplify marked spot logic. * Change dropdowns to select fields instead of lists of buttons. * Add ability to specify a css class for labels and buttons * Don't make labels clickable * console.log -> console.warn * change 'class' to 'web-style' * createSvgElement is now in utils. fix two calls. * Improve comments. * lint * fix missing semicolon * When adding a new block group from the toolbox modal, only show blocks with no output connections. * Clean up the sidebar file and remove unneeded code. * Remove some functions from utilsService and consolidate code in workspace-tree.component.js. * Standardize indentation. * Remove premature focus on buttons in modal dialogs, since this prevents readout of the dialog text. * Localisation updates from https://translatewiki.net. * Don't get Toolbox element unless needed. * Associate flyout button callbacks directly with workspaces * Add colour block to the block factory base block initial state * Start getting helpurl and tooltip in * Generate helpURL and tooltip for Javascript block definition * Use Tab keys instead of arrow keys for dialog boxes. Set role=alertdialog and read out the header/text automatically. Ensure that Esc key actually closes dialogs and that all keystrokes are captured. * Add an aria-describedby to the 'create new block group...' button in the workspace to give more context. * Fix issue with aria-liveregion not speaking. Allow sufficient time for alert noise to play before speaking the notification. * Make zoom speed independent of event granularity Before, touchpads would give "smoother" scrolling by delivering lots of mousewheel events with small distance changes. Because the code only looked at the sign of deltaY, ten 5px scrolls would zoom 10x more than one 50px scroll. This change makes zooming with a touchpad more like zooming with a mousewheel. On my laptop, a full-scale zoom (fully out to fully in) was about a 5mm finger movement before, and is now about 3cm. Fixes #758. * Split the scrollbar and flyout out into their own SVG elements. They (#771) * Split the scrollbar and flyout out into their own SVG elements. They are siblings of the workpsace SVG. This paves the way to make performance improvements to workspace dragging. * remove overflow-y on the block exporter labels so scroll bars do not show upin firefox. Also fix up the styles on the labels so that they display better in firefox. (#699) * Fix #698 by adjusting the regex to not have \. Still not 100% sure w… (#700) * Fix #698 by adjusting the regex to not have \. Still not 100% sure why that was there. Also replaces bad names on input. There are probably more invalid names but this is a start. * update generator comments * Move the call to disable resize before placeNewBlock so that it is of… (#777) * Move the call to disable resize before placeNewBlock so that it is off when workspace resizeContents gets triggered by placeNewBlock. This fixes a bug in rtl mode where the workspace was being resized between when the block was added to the workspace and when it was moved to the proper location. * Disable workspace resizing while loading the flyout from XML * Localisation updates from https://translatewiki.net. * Add a workspace drag surface that blocks and bubble get moved to duri… (#778) * Add a workspace drag surface that blocks and bubble get moved to during a workspace drag. The surface is translated using translate3d instead of svg's translate attribute so that the browser does not have to repaint the entire workspace on every mouse move. This is very similar to the block drag surface. * Address code review comments * add back hasClass_ utility removed in #748 and stop using contains since it is not supported in IE * Fixes #786 by checking if getComputedStyle is null in is3dSupported. We do not cache the value in this case and try again later. is3dSupported is only called while users are interacting with blockly which they cannot do while hidden so the performance implications of running the check again are minimal. (#787) * Localisation updates from https://translatewiki.net. * Change the Python codegen for string quoting to match the behaviour of `repr` on a string in CPython. * Localisation updates from https://translatewiki.net. * Add an `allInputsConnected` method to `Block` and `Workspace` to test whether all trees in the block forest have their inputs filled. An optional argument controls whether or not shadow blocks are counted as being filled. Recommitting changes off `develop` instead of `master` as per discussion in PR #791. * Localisation updates from https://translatewiki.net. * Localisation updates from https://translatewiki.net. * Use the drag surface when scrolling using the scrollbars. #783 (#789) * End event groups when you finish editing a field * Fix #794 and make the workspace grid drag along with the workspace. (#801) There was some IE specific code that also applies to Edge so just updated a conditional to include Edge. * Now that text input's setText skips setValue, it needs to explicitly create a change event * Check if the text has changed before firing an event * Init procedure blocks with empty name, and set default name in xml in Blockly.Procedures.flyoutCategory * Routine rebuild * Move createDom call into the constructor of block drag surface. (#790) * Make cursor stay as a closed hand when dragging blocks around in the drag surface. Do this by applying the same style to text elements in the drag surface that we do in the main svg. (#805) * Don't connect to blocks under the flyout. * recompile again. (#806) * Fix german translation * Fix german translation of 'delete x blocks' * Adding unit tests for ifelse block. * Improvements to the generator test framework. * <field>, <value> reorder due to load/save. * Use the npm closure library instead of the same library installed at a parallel directory * Fix undo/redo for FieldCheckbox Thanks to PR #813 by ademenev * PR #818: Adding support for string table lookups in dropdown field labels Adding support for string table lookups in dropdown field labels specified in JSON. Adds Blockly.utils.replaceMessageReferences() method to handle string replacement without interpolation tokens. Effectively uses the same old code, now moved into tokenizeInterpolation_(), which takes a parseInterpolationTokens option. Replaces the direct JavaScript references (not pure JSON, and thus not portable). Demonstrating this behavior in the logic_boolean dropdown. * Integrating qqq.json changes into messages.json. (#820) From commitsb77f8cbebc
and4ecdedec9f
* Naming changes in mirror demo * Adding support for untranslated messages. (#819) This will be used to define constants accessible in JSON block definitions. Messages with descriptions that include `{{Notranslate}}` will not be included in the translation files sent to TranslateWiki. Instead, they are written to `msg/json/constants.json`, and later merged back into the `.js` files, similar to synonyms. Template details: https://translatewiki.net/wiki/Template:Notranslate * JSON support for message lookup in colour, tooltip, and help URL. (#825) String replacement for the colour, tooltip text and help URL attributes of JSON defined blocks. Demonstrated in logic_boolean. * Fixes as per code review on PR. * Reduce number of Closure files in App Engine upload. * Python false is False. Issue #828. * Replace 'const' with 'var'. This unbreaks IE10 and advanced compiled apps such as Blockly Games. * Fix bug in audioService where attached event callbacks were not being cleared properly. * Rename workspace-tree to workspace-block. * Minor refactoring of the modal code (add comments, guard against invalid keystrokes, etc.). * FieldNumber & FieldAngle: Default value "0" (#832) FieldNumber and FieldAngle previously accepted "undefined" as values, if not defined in JSON. This catches these and uses "0" for any NaN value. The constructor value parameter is now optional. Includes tests. * Remove unnecessary check when attaching a new block to a marked connection. * Remove debug info. * Refactor and simplify field-segment.component.js. * Replace single quotes with double. (#836) Fixes commits in #832. * Adding extensions for JSON support of dynamic blocks. (#834) Adding support for extensions, functions that can assist with loading blocks, much like init functions, but that can be referenced from JSON definitions. This allows JSON definitions to define dynamic blocks such as onchange handlers and mutators. Rewrote math_number as an example pure JSON block. * Add ability to add a class to a scrollbar so that different types of … (#837) * Add ability to add a class to a scrollbar so that different types of scrollbars can be distinguished from each other. You used to be able to do this by looking at the parent element but now all the scrollbars are siblings in the dom. Also, use this new class to fix #816 so that layering of the flyout and workspace scrollbars are done correctly. * JSON definitions for colour blocks (#838) Replaces old colour block definitions with a Blockly.defineBlocksWithJsonArray(..) call. Generator unit tests continue to load and pass, signifying compatibility with prior block definitions. Replaces extension 'math_number_tooltip' with the reusable 'parent_tooltip_when_inline' extension, also used by colour_picker. Includes tests. * Rewrite tree.service.js. - Remove unnecessary code and functions. - Add documentation where needed. - Fix a bug arising when a block on the workspace is attached to an existing link. * Use setValue in fieldTextInput so that procedure renaming works * Further cleanup and removal of unnecessary functions. Pull some strings out for i18n. * Use bindEvent_ instead of bindEventWithChecks_ for longStop * Clean up workspace.component.js. When moving a block from one place to another, move all blocks after it too, and adjust the active descs accordingly. * Unit tests for JSON block definitions (just the start) (#850) * Beginnings of a JSON block definition unit test set. * Dispose of unit test workspaces and blocks in finally blocks. * Clarify JSON error message by echoing arg notation. * New blocks text_count, text_replace, and text_reverse (#830) Includes generators for all languages and units tests on those generators. * Fixing combo boxes getting out-of-sync with NVDA. Combo boxes need to be special cased like text input. Also, Escape is a reserved button in NVDA, so I added Enter as a way to "submit and move up a level" in addition to escape, so these boxes can be edited while NVDA is on. * Add a block to reverse a list (#844) * Porting math.js blocks to JSON (#846) Moving all `math.js` definitions into a single JSON array, complete with i18n syntax for all messages, dropdowns, and tooltips. Adding Blockly.Extensions.buildTooltipForDropdown(..) to facilitate the creation and error-checking of tooltips that update based on the value of a dropdown. Now warn on raw string in JSON 'extensions'. * Fixing JSON support for images in dropdowns. Adding tests. (#851) Fixes #848. * Update README.md Add a link to our forum. * Correcting math_change color * Enable custom flyout categories. * Add some safety * Update the set of reserved words in Python to reflect the current state of Python (2.7 and 3.6). (#861) * .getOptions_() to .getOptions() (#869) Fixes #867. * Blockly.Extensions.buildTooltipForDropdown(..): Deferred validation. (#870) Defer tooltip message string check until after load, when all Blockly.Msg should be loaded. Avoids validation in headless mode, due to lack of document.readyState. * annotation updates * jsdoc corrections (#874) * Remove use of Array.prototype.includes which is not implemented in IE or Edge < 14. Fixes google/blockly#876. * Attempt to work around the IE/Edge bug where `getComputedTextLength()` throws an exception when the SVG node is not visible. This workaround forces a re-render, which in turn, forces a re-calculation of the node width once a block is inserted into the workspace SVG. This workaround is only executed on IE and Edge. See https://groups.google.com/forum/#!topic/blockly/T8IR4t4xAIY for the initial discussion of this issue. * Change CSS transforms to work with older browsers (#879) * Change the setting of the CSS transform properties on SVG nodes to set both the unprefixed version and the `-webkit-` prefixed version so that Blockly correctly renders in order browsers, such as Safari < 9 and iOS Safari < 9.2. For discussion of this issue, see https://groups.google.com/forum/#!topic/blockly/o3pERaRQhSg * Correct the separation between the CSS transform property and the rest of the CSS that was in the variable misleadingly called "transform". * Don't try to get block position in a headless workspace * Stop bumping neighbours in headless blockly * Place context menu correctly on touch * Clear all active desc ids when the 'Erase Workspace' button is pressed. * Fix a bug where splicing a block between two linked blocks disconnects the group and messes up the focus. * Deleting a top-level block does not cause blocks after it to be deleted. Properly handle the active desc for this case. * Use the empty field placeholder for dropdowns that do not have a value selected. * Bugfix for #892. I incorrectly converted one CSS transform setting to use the cross-browser setting function in 40a063763c74b3f712c3057565966c25d5cfdb10. (#895) * Adding @namespace annotations for JSDoc. (#900) * Fix typo causing TypeError (#901) * Pinning the angular2 dependency, and including licenses. (#893) * Add skeleton for tests on rendered workspaces * Fix some lint errors * Correct changedState in setWarningText() (#908) When clearing warnings on blocks with IDs, the changedState variable should be true if the text changed. This will trigger the block being reshaped and remove the space for the notification icon (this.bumpNeighbours_). * Adds Block.prototype.mixin() and Blockly.Extensions.registerMixin(). (#907) Adds Block.prototype.mixin() and Blockly.Extensions.registerMixin(). This adds support for a common use pattern in extensions, and adds error checking to avoid future incompatibilities. * Porting Logic blocks to JSON (#913) Extensions, mixins, mutators and constants now grouped under the new namespace Blockly.Constants.Logic. * Improving errors/warnings with Block.toDevString() and Connection.toString(). (#911) * Add isEditable to field, and add tests * Separate tests * Blockly.Constants.Math and Blockly.Constants.Colour extension constants (#916) Also, correcting quotes in logic.js. * Correction to logic_ternary type check (#920) * Porting Loop blocks to JSON (#919) * Improved documentation on `Blockly.Extensions.buildTooltipForDropdown` * Replaced incorrect uses of `@mixes` JSDoc annotation (on mixin extensions) with `@augments Blockly.Block`. * Added Blockly.Extensions.buildTooltipWithFieldValue() extension helper. * Workspace isDraggable * JSONify simple list blocks * JSONify variable blocks * Initial text block, with a mixin to generate quote image fields. (#923) Text block now uses the extension "text_quotes", supported by Blockly.Constants.Text.QUOTE_IMAGE_MIXIN.quoteField_(fieldName), so that each platform can use the best platform appropriate image (size, density, etc.) for the quotes. * Add no-op stub .neighbors() for headless Connection. * Adding tests for logic_ternary block in a new jsunit test framework. * Correcting output of the logic_null block. * extension controls_if => controls_if_mutator. * Renamed extension function constant, and moved variables into the mixin. * Dereference string table references when loading variable fields from JSON. * Moving FieldImage string dereferencing back into Block.interpolate_() (part of jsonInit()). This sets a clear boundary of where dereferencing should happen. Towards this, I've added message dereferencing for other field types here, as well. I've used a pattern of field-type specific helper functions. * Addressing comments. * .utils.replaceMessageReferences(..) now gracefully returns non-string arguments. * Fix a few small errors and rebuild * Call dynamic toolbox generators correctly * cleanup * Fix unit tests, and delete a few that relied on completely undefined behaviour * Fix RTL text inputs * eslintignore more tests * Fix insertion marker highlighting, I think * Make getFlyout public
This commit is contained in:
parent
a4b0e2d803
commit
57c3666198
60 changed files with 3251 additions and 358 deletions
|
@ -3,6 +3,8 @@
|
|||
/msg/*
|
||||
/core/css.js
|
||||
/tests/jsunit/*
|
||||
/tests/workspace_svg/*
|
||||
/tests/blocks/*
|
||||
/tests/generators/*
|
||||
/generators/*
|
||||
/demos/*
|
||||
|
|
|
@ -21,4 +21,4 @@ Scratch Blocks brings together two different programming "grammars" that the Scr
|
|||
The "getting started" guide including [FAQ](https://scratch.mit.edu/developers#faq) and [design documentation](https://github.com/LLK/scratch-blocks/wiki/Design) can be found in the [wiki](https://github.com/LLK/scratch-blocks/wiki).
|
||||
|
||||
## Donate
|
||||
We provide [Scratch](https://scratch.mit.edu) free of charge, and want to keep it that way! Please consider making a [donation](https://secure.donationpay.org/scratchfoundation/) to support our continued engineering, design, community, and resource development efforts. Donations of any size are appreciated. Thank you!
|
||||
We provide [Scratch](https://scratch.mit.edu) free of charge, and want to keep it that way! Please consider making a [donation](https://secure.donationpay.org/scratchfoundation/) to support our continued engineering, design, community, and resource development efforts. Donations of any size are appreciated. Thank you!
|
5
build.py
5
build.py
|
@ -87,7 +87,7 @@ var isNodeJS = !!(typeof module !== 'undefined' && module.exports &&
|
|||
|
||||
if (isNodeJS) {
|
||||
var window = {};
|
||||
require('../closure-library/closure/goog/bootstrap/nodejs');
|
||||
require('closure-library');
|
||||
}
|
||||
|
||||
window.BLOCKLY_DIR = (function() {
|
||||
|
@ -109,7 +109,7 @@ window.BLOCKLY_DIR = (function() {
|
|||
window.BLOCKLY_BOOT = function() {
|
||||
var dir = '';
|
||||
if (isNodeJS) {
|
||||
require('../closure-library/closure/goog/bootstrap/nodejs');
|
||||
require('closure-library');
|
||||
dir = 'blockly';
|
||||
} else {
|
||||
// Execute after Closure has loaded.
|
||||
|
@ -432,6 +432,7 @@ class Gen_langfiles(threading.Thread):
|
|||
os.path.join("i18n", "create_messages.py"),
|
||||
"--source_lang_file", os.path.join("msg", "json", "en.json"),
|
||||
"--source_synonym_file", os.path.join("msg", "json", "synonyms.json"),
|
||||
"--source_constants_file", os.path.join("msg", "json", "constants.json"),
|
||||
"--key_file", os.path.join("msg", "json", "keys.json"),
|
||||
"--output_dir", os.path.join("msg", "js"),
|
||||
"--quiet"]
|
||||
|
|
248
core/block.js
248
core/block.js
|
@ -30,6 +30,7 @@ goog.require('Blockly.Blocks');
|
|||
goog.require('Blockly.Colours');
|
||||
goog.require('Blockly.Comment');
|
||||
goog.require('Blockly.Connection');
|
||||
goog.require('Blockly.Extensions');
|
||||
goog.require('Blockly.Input');
|
||||
goog.require('Blockly.Mutator');
|
||||
goog.require('Blockly.Warning');
|
||||
|
@ -164,7 +165,7 @@ Blockly.Block = function(workspace, prototypeName, opt_id) {
|
|||
this.type = prototypeName;
|
||||
var prototype = Blockly.Blocks[prototypeName];
|
||||
goog.asserts.assertObject(prototype,
|
||||
'Error: "%s" is an unknown language block.', prototypeName);
|
||||
'Error: Unknown block type "%s".', prototypeName);
|
||||
goog.mixin(this, prototype);
|
||||
}
|
||||
|
||||
|
@ -182,8 +183,7 @@ Blockly.Block = function(workspace, prototypeName, opt_id) {
|
|||
}
|
||||
// Bind an onchange function, if it exists.
|
||||
if (goog.isFunction(this.onchange)) {
|
||||
this.onchangeWrapper_ = this.onchange.bind(this);
|
||||
this.workspace.addChangeListener(this.onchangeWrapper_);
|
||||
this.setOnChange(this.onchange);
|
||||
}
|
||||
};
|
||||
|
||||
|
@ -359,6 +359,7 @@ Blockly.Block.prototype.lastConnectionInStack = function() {
|
|||
* connected should not coincidentally line up on screen.
|
||||
* @private
|
||||
*/
|
||||
// TODO: Refactor to return early in headless mode.
|
||||
Blockly.Block.prototype.bumpNeighbours_ = function() {
|
||||
if (!this.workspace) {
|
||||
return; // Deleted block.
|
||||
|
@ -757,7 +758,7 @@ Blockly.Block.prototype.getColourTertiary = function() {
|
|||
* @private
|
||||
*/
|
||||
Blockly.Block.prototype.makeColour_ = function(colour) {
|
||||
var hue = parseFloat(colour);
|
||||
var hue = Number(colour);
|
||||
if (!isNaN(hue)) {
|
||||
return Blockly.hueToRgb(hue);
|
||||
} else if (goog.isString(colour) && colour.match(/^#[0-9a-fA-F]{6}$/)) {
|
||||
|
@ -778,20 +779,45 @@ Blockly.Block.prototype.setColour = function(colour, colourSecondary, colourTert
|
|||
if (colourSecondary !== undefined) {
|
||||
this.colourSecondary_ = this.makeColour_(colourSecondary);
|
||||
} else {
|
||||
this.colourSecondary_ = goog.color.darken(goog.color.hexToRgb(this.colour_),
|
||||
0.1);
|
||||
this.colourSecondary_ = goog.color.rgbArrayToHex(
|
||||
goog.color.darken(goog.color.hexToRgb(this.colour_),
|
||||
0.1));
|
||||
}
|
||||
if (colourTertiary !== undefined) {
|
||||
this.colourTertiary_ = this.makeColour_(colourTertiary);
|
||||
} else {
|
||||
this.colourTertiary_ = goog.color.darken(goog.color.hexToRgb(this.colour_),
|
||||
0.2);
|
||||
this.colourTertiary_ = goog.color.rgbArrayToHex(
|
||||
goog.color.darken(goog.color.hexToRgb(this.colour_),
|
||||
0.2));
|
||||
}
|
||||
if (this.rendered) {
|
||||
this.updateColour();
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Sets a callback function to use whenever the block's parent workspace
|
||||
* changes, replacing any prior onchange handler. This is usually only called
|
||||
* from the constructor, the block type initializer function, or an extension
|
||||
* initializer function.
|
||||
* @param {function(Blockly.Events.Abstract)} onchangeFn The callback to call
|
||||
* when the block's workspace changes.
|
||||
* @throws {Error} if onchangeFn is not falsey or a function.
|
||||
*/
|
||||
Blockly.Block.prototype.setOnChange = function(onchangeFn) {
|
||||
if (onchangeFn && !goog.isFunction(onchangeFn)) {
|
||||
throw new Error("onchange must be a function.");
|
||||
}
|
||||
if (this.onchangeWrapper_) {
|
||||
this.workspace.removeChangeListener(this.onchangeWrapper_);
|
||||
}
|
||||
this.onchange = onchangeFn;
|
||||
if (this.onchange) {
|
||||
this.onchangeWrapper_ = onchangeFn.bind(this);
|
||||
this.workspace.addChangeListener(this.onchangeWrapper_);
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Returns the named field from a block.
|
||||
* @param {string} name The name of the field.
|
||||
|
@ -1050,7 +1076,11 @@ Blockly.Block.prototype.toString = function(opt_maxLength, opt_emptyToken) {
|
|||
} else {
|
||||
for (var i = 0, input; input = this.inputList[i]; i++) {
|
||||
for (var j = 0, field; field = input.fieldRow[j]; j++) {
|
||||
text.push(field.getText());
|
||||
if (field instanceof Blockly.FieldDropdown && !field.getValue()) {
|
||||
text.push(emptyFieldPlaceholder);
|
||||
} else {
|
||||
text.push(field.getText());
|
||||
}
|
||||
}
|
||||
if (input.connection) {
|
||||
var child = input.connection.targetBlock();
|
||||
|
@ -1115,7 +1145,18 @@ Blockly.Block.prototype.jsonInit = function(json) {
|
|||
|
||||
// Set basic properties of block.
|
||||
if (json['colour'] !== undefined) {
|
||||
this.setColour(json['colour'], json['colourSecondary'], json['colourTertiary']);
|
||||
// TODO: Consider a helper function here.
|
||||
var rawValue = json['colour'];
|
||||
var primary = goog.isString(rawValue) ?
|
||||
Blockly.utils.replaceMessageReferences(rawValue) : rawValue;
|
||||
rawValue = json['colourSecondary'];
|
||||
var secondary = goog.isString(rawValue) ?
|
||||
Blockly.utils.replaceMessageReferences(rawValue) : rawValue;
|
||||
rawValue = json['colourTertiary'];
|
||||
var tertiary = goog.isString(rawValue) ?
|
||||
Blockly.utils.replaceMessageReferences(rawValue) : rawValue;
|
||||
|
||||
this.setColour(primary, secondary, tertiary);
|
||||
}
|
||||
|
||||
// Interpolate the message blocks.
|
||||
|
@ -1140,10 +1181,30 @@ Blockly.Block.prototype.jsonInit = function(json) {
|
|||
this.setNextStatement(true, json['nextStatement']);
|
||||
}
|
||||
if (json['tooltip'] !== undefined) {
|
||||
this.setTooltip(json['tooltip']);
|
||||
var rawValue = json['tooltip'];
|
||||
var localizedText = Blockly.utils.replaceMessageReferences(rawValue);
|
||||
this.setTooltip(localizedText);
|
||||
}
|
||||
if (json['enableContextMenu'] !== undefined) {
|
||||
var rawValue = json['enableContextMenu'];
|
||||
this.contextMenu = !!rawValue;
|
||||
}
|
||||
if (json['helpUrl'] !== undefined) {
|
||||
this.setHelpUrl(json['helpUrl']);
|
||||
var rawValue = json['helpUrl'];
|
||||
var localizedValue = Blockly.utils.replaceMessageReferences(rawValue);
|
||||
this.setHelpUrl(localizedValue);
|
||||
}
|
||||
if (goog.isString(json['extensions'])) {
|
||||
console.warn('JSON attribute \'extensions\' should be an array of ' +
|
||||
'strings. Found raw string in JSON for \'' + json['type'] + '\' block.');
|
||||
json['extensions'] = [json['extensions']]; // Correct and continue.
|
||||
}
|
||||
if (Array.isArray(json['extensions'])) {
|
||||
var extensionNames = json['extensions'];
|
||||
for (var i = 0; i < extensionNames.length; ++i) {
|
||||
var extensionName = extensionNames[i];
|
||||
Blockly.Extensions.apply(extensionName, this);
|
||||
}
|
||||
}
|
||||
if (json['outputShape'] !== undefined) {
|
||||
this.setOutputShape(json['outputShape']);
|
||||
|
@ -1156,6 +1217,34 @@ Blockly.Block.prototype.jsonInit = function(json) {
|
|||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Add key/values from mixinObj to this block object. By default, this method
|
||||
* will check that the keys in mixinObj will not overwrite existing values in
|
||||
* the block, including prototype values. This provides some insurance against
|
||||
* mixin / extension incompatibilities with future block features. This check
|
||||
* can be disabled by passing true as the second argument.
|
||||
* @param {!Object} mixinObj The key/values pairs to add to this block object.
|
||||
* @param {boolean=} opt_disableCheck Option flag to disable overwrite checks.
|
||||
*/
|
||||
Blockly.Block.prototype.mixin = function(mixinObj, opt_disableCheck) {
|
||||
if (goog.isDef(opt_disableCheck) && !goog.isBoolean(opt_disableCheck)) {
|
||||
throw new Error("opt_disableCheck must be a boolean if provided");
|
||||
}
|
||||
if (!opt_disableCheck) {
|
||||
var overwrites = [];
|
||||
for (var key in mixinObj) {
|
||||
if (this[key] !== undefined) {
|
||||
overwrites.push(key);
|
||||
}
|
||||
}
|
||||
if (overwrites.length) {
|
||||
throw new Error('Mixin will overwrite block members: ' +
|
||||
JSON.stringify(overwrites));
|
||||
}
|
||||
}
|
||||
goog.mixin(this, mixinObj);
|
||||
};
|
||||
|
||||
/**
|
||||
* Interpolate a message description onto the block.
|
||||
* @param {string} message Text contains interpolation tokens (%1, %2, ...)
|
||||
|
@ -1175,9 +1264,9 @@ Blockly.Block.prototype.interpolate_ = function(message, args, lastDummyAlign) {
|
|||
var token = tokens[i];
|
||||
if (typeof token == 'number') {
|
||||
goog.asserts.assert(token > 0 && token <= args.length,
|
||||
'Message index "%s" out of range.', token);
|
||||
'Message index %%s out of range.', token);
|
||||
goog.asserts.assert(!indexDup[token],
|
||||
'Message index "%s" duplicated.', token);
|
||||
'Message index %%s duplicated.', token);
|
||||
indexDup[token] = true;
|
||||
indexCount++;
|
||||
elements.push(args[token - 1]);
|
||||
|
@ -1189,7 +1278,7 @@ Blockly.Block.prototype.interpolate_ = function(message, args, lastDummyAlign) {
|
|||
}
|
||||
}
|
||||
goog.asserts.assert(indexCount == args.length,
|
||||
'Message does not reference all %s arg(s).', args.length);
|
||||
'block "%s": Message does not reference all %s arg(s).', this.type, args.length);
|
||||
// Add last dummy input if needed.
|
||||
if (elements.length && (typeof elements[elements.length - 1] == 'string' ||
|
||||
goog.string.startsWith(elements[elements.length - 1]['type'],
|
||||
|
@ -1231,13 +1320,10 @@ Blockly.Block.prototype.interpolate_ = function(message, args, lastDummyAlign) {
|
|||
input = this.appendDummyInput(element['name']);
|
||||
break;
|
||||
case 'field_label':
|
||||
field = new Blockly.FieldLabel(element['text'], element['class']);
|
||||
field = Blockly.Block.newFieldLabelFromJson_(element);
|
||||
break;
|
||||
case 'field_input':
|
||||
field = new Blockly.FieldTextInput(element['text']);
|
||||
if (typeof element['spellcheck'] == 'boolean') {
|
||||
field.setSpellcheck(element['spellcheck']);
|
||||
}
|
||||
field = Blockly.Block.newFieldTextInputFromJson_(element);
|
||||
break;
|
||||
case 'field_textdropdown':
|
||||
field = new Blockly.FieldTextDropdown(element['text'], element['options']);
|
||||
|
@ -1262,7 +1348,7 @@ Blockly.Block.prototype.interpolate_ = function(message, args, lastDummyAlign) {
|
|||
field = new Blockly.FieldColour(element['colour']);
|
||||
break;
|
||||
case 'field_variable':
|
||||
field = new Blockly.FieldVariable(element['variable']);
|
||||
field = Blockly.Block.newFieldVariableFromJson_(element);
|
||||
break;
|
||||
case 'field_dropdown':
|
||||
field = new Blockly.FieldDropdown(element['options']);
|
||||
|
@ -1271,9 +1357,7 @@ Blockly.Block.prototype.interpolate_ = function(message, args, lastDummyAlign) {
|
|||
field = new Blockly.FieldIconMenu(element['options']);
|
||||
break;
|
||||
case 'field_image':
|
||||
field = new Blockly.FieldImage(element['src'],
|
||||
element['width'], element['height'], element['alt'],
|
||||
element['flip_rtl']);
|
||||
field = Blockly.Block.newFieldImageFromJson_(element);
|
||||
break;
|
||||
case 'field_number':
|
||||
field = new Blockly.FieldNumber(element['value'],
|
||||
|
@ -1312,6 +1396,65 @@ Blockly.Block.prototype.interpolate_ = function(message, args, lastDummyAlign) {
|
|||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Helper function to construct a FieldImage from a JSON arg object,
|
||||
* dereferencing any string table references.
|
||||
* @param {!Object} options A JSON object with options (src, width, height, and alt).
|
||||
* @returns {!Blockly.FieldImage} The new image.
|
||||
* @private
|
||||
*/
|
||||
Blockly.Block.newFieldImageFromJson_ = function(options) {
|
||||
var src = Blockly.utils.replaceMessageReferences(options['src']);
|
||||
var width = Number(Blockly.utils.replaceMessageReferences(options['width']));
|
||||
var height =
|
||||
Number(Blockly.utils.replaceMessageReferences(options['height']));
|
||||
var alt = Blockly.utils.replaceMessageReferences(options['alt']);
|
||||
var flip_rtl = Blockly.utils.replaceMessageReferences(options['flip_rtl']);
|
||||
return new Blockly.FieldImage(src, width, height, alt, flip_rtl);
|
||||
};
|
||||
|
||||
/**
|
||||
* Helper function to construct a FieldLabel from a JSON arg object,
|
||||
* dereferencing any string table references.
|
||||
* @param {!Object} options A JSON object with options (text, and class).
|
||||
* @returns {!Blockly.FieldImage} The new image.
|
||||
* @private
|
||||
*/
|
||||
Blockly.Block.newFieldLabelFromJson_ = function(options) {
|
||||
var text = Blockly.utils.replaceMessageReferences(options['text']);
|
||||
return new Blockly.FieldLabel(text, options['class']);
|
||||
};
|
||||
|
||||
/**
|
||||
* Helper function to construct a FieldTextInput from a JSON arg object,
|
||||
* dereferencing any string table references.
|
||||
* @param {!Object} options A JSON object with options (text, class, and
|
||||
* spellcheck).
|
||||
* @returns {!Blockly.FieldImage} The new image.
|
||||
* @private
|
||||
*/
|
||||
Blockly.Block.newFieldTextInputFromJson_ = function(options) {
|
||||
var text = Blockly.utils.replaceMessageReferences(options['text']);
|
||||
var field = new Blockly.FieldTextInput(text, options['class']);
|
||||
if (typeof options['spellcheck'] == 'boolean') {
|
||||
field.setSpellcheck(options['spellcheck']);
|
||||
}
|
||||
return field;
|
||||
};
|
||||
|
||||
/**
|
||||
* Helper function to construct a FieldVariable from a JSON arg object,
|
||||
* dereferencing any string table references.
|
||||
* @param {!Object} options A JSON object with options (variable).
|
||||
* @returns {!Blockly.FieldImage} The new image.
|
||||
* @private
|
||||
*/
|
||||
Blockly.Block.newFieldVariableFromJson_ = function(options) {
|
||||
var varname = Blockly.utils.replaceMessageReferences(options['variable']);
|
||||
return new Blockly.FieldVariable(varname);
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* Add a value input, statement input or local variable to this block.
|
||||
* @param {number} type Either Blockly.INPUT_VALUE or Blockly.NEXT_STATEMENT or
|
||||
|
@ -1519,7 +1662,7 @@ Blockly.Block.prototype.hasCheckboxInFlyout = function() {
|
|||
* @param {?string} text The text, or null to delete.
|
||||
* @abstract
|
||||
*/
|
||||
Blockly.Block.prototype.setWarningText = function(/*text*/) {
|
||||
Blockly.Block.prototype.setWarningText = function(/* text */) {
|
||||
// NOP.
|
||||
};
|
||||
|
||||
|
@ -1528,7 +1671,7 @@ Blockly.Block.prototype.setWarningText = function(/*text*/) {
|
|||
* @param {Blockly.Mutator} mutator A mutator dialog instance or null to remove.
|
||||
* @abstract
|
||||
*/
|
||||
Blockly.Block.prototype.setMutator = function(/*mutator*/) {
|
||||
Blockly.Block.prototype.setMutator = function(/* mutator */) {
|
||||
// NOP.
|
||||
};
|
||||
|
||||
|
@ -1563,3 +1706,56 @@ Blockly.Block.prototype.moveBy = function(dx, dy) {
|
|||
Blockly.Block.prototype.makeConnection_ = function(type) {
|
||||
return new Blockly.Connection(this, type);
|
||||
};
|
||||
|
||||
/**
|
||||
* Recursively checks whether all statement and value inputs are filled with
|
||||
* blocks. Also checks all following statement blocks in this stack.
|
||||
* @param {boolean=} opt_shadowBlocksAreFilled An optional argument controlling
|
||||
* whether shadow blocks are counted as filled. Defaults to true.
|
||||
* @return {boolean} True if all inputs are filled, false otherwise.
|
||||
*/
|
||||
Blockly.Block.prototype.allInputsFilled = function(opt_shadowBlocksAreFilled) {
|
||||
// Account for the shadow block filledness toggle.
|
||||
if (opt_shadowBlocksAreFilled === undefined) {
|
||||
opt_shadowBlocksAreFilled = true;
|
||||
}
|
||||
if (!opt_shadowBlocksAreFilled && this.isShadow()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// Recursively check each input block of the current block.
|
||||
for (var i = 0, input; input = this.inputList[i]; i++) {
|
||||
if (!input.connection) {
|
||||
continue;
|
||||
}
|
||||
var target = input.connection.targetBlock();
|
||||
if (!target || !target.allInputsFilled(opt_shadowBlocksAreFilled)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
// Recursively check the next block after the current block.
|
||||
var next = this.getNextBlock();
|
||||
if (next) {
|
||||
return next.allInputsFilled(opt_shadowBlocksAreFilled);
|
||||
}
|
||||
|
||||
return true;
|
||||
};
|
||||
|
||||
/**
|
||||
* This method returns a string describing this Block in developer terms (type
|
||||
* name and ID; English only).
|
||||
*
|
||||
* Intended to on be used in console logs and errors. If you need a string that
|
||||
* uses the user's native language (including block text, field values, and
|
||||
* child blocks), use [toString()]{@link Blockly.Block#toString}.
|
||||
* @return {string} The description.
|
||||
*/
|
||||
Blockly.Block.prototype.toDevString = function() {
|
||||
var msg = this.type ? '"' + this.type + '" block' : 'Block';
|
||||
if (this.id) {
|
||||
msg += ' (id="' + this.id + '")';
|
||||
}
|
||||
return msg;
|
||||
};
|
||||
|
|
|
@ -143,16 +143,15 @@ Blockly.BlockDragSurfaceSvg.prototype.translateAndScaleGroup = function(x, y, sc
|
|||
* @param {number} y Y translation for the entire surface.
|
||||
*/
|
||||
Blockly.BlockDragSurfaceSvg.prototype.translateSurface = function(x, y) {
|
||||
var transform;
|
||||
x *= this.scale_;
|
||||
y *= this.scale_;
|
||||
// This is a work-around to prevent a the blocks from rendering
|
||||
// fuzzy while they are being dragged on the drag surface.
|
||||
x = x.toFixed(0);
|
||||
y = y.toFixed(0);
|
||||
transform =
|
||||
'transform: translate3d(' + x + 'px, ' + y + 'px, 0px); display: block;';
|
||||
this.SVG_.setAttribute('style', transform);
|
||||
this.SVG_.style.display = 'block';
|
||||
Blockly.utils.setCssTransform(this.SVG_,
|
||||
'translate3d(' + x + 'px, ' + y + 'px, 0px)');
|
||||
};
|
||||
|
||||
/**
|
||||
|
|
|
@ -336,11 +336,11 @@ Blockly.BlockSvg.prototype.updateColour = function() {
|
|||
Blockly.BlockSvg.prototype.highlightForReplacement = function(add) {
|
||||
if (add) {
|
||||
this.svgPath_.setAttribute('filter', 'url(#blocklyReplacementGlowFilter)');
|
||||
Blockly.utils.addClass_(/** @type {!Element} */ (this.svgGroup_),
|
||||
Blockly.utils.addClass(/** @type {!Element} */ (this.svgGroup_),
|
||||
'blocklyReplaceable');
|
||||
} else {
|
||||
this.svgPath_.removeAttribute('filter');
|
||||
Blockly.utils.removeClass_(/** @type {!Element} */ (this.svgGroup_),
|
||||
Blockly.utils.removeClass(/** @type {!Element} */ (this.svgGroup_),
|
||||
'blocklyReplaceable');
|
||||
}
|
||||
};
|
||||
|
|
|
@ -534,6 +534,63 @@ Blockly.BlockSvg.prototype.clearTransformAttributes_ = function() {
|
|||
this.getSvgRoot().removeAttribute('transform');
|
||||
};
|
||||
|
||||
/**
|
||||
* Transforms a block by setting the translation on the transform attribute
|
||||
* of the block's SVG.
|
||||
* @param {number} x The x coordinate of the translation.
|
||||
* @param {number} y The y coordinate of the translation.
|
||||
*/
|
||||
Blockly.BlockSvg.prototype.translate = function(x, y) {
|
||||
this.getSvgRoot().setAttribute('transform',
|
||||
'translate(' + x + ',' + y + ')');
|
||||
};
|
||||
|
||||
/**
|
||||
* Move this block to its workspace's drag surface, accounting for positioning.
|
||||
* Generally should be called at the same time as setDragging_(true).
|
||||
* Does nothing if useDragSurface_ is false.
|
||||
* @private
|
||||
*/
|
||||
Blockly.BlockSvg.prototype.moveToDragSurface_ = function() {
|
||||
if (!this.useDragSurface_) {
|
||||
return;
|
||||
}
|
||||
// The translation for drag surface blocks,
|
||||
// is equal to the current relative-to-surface position,
|
||||
// to keep the position in sync as it move on/off the surface.
|
||||
var xy = this.getRelativeToSurfaceXY();
|
||||
this.clearTransformAttributes_();
|
||||
this.workspace.blockDragSurface_.translateSurface(xy.x, xy.y);
|
||||
// Execute the move on the top-level SVG component
|
||||
this.workspace.blockDragSurface_.setBlocksAndShow(this.getSvgRoot());
|
||||
};
|
||||
|
||||
/**
|
||||
* Move this block back to the workspace block canvas.
|
||||
* Generally should be called at the same time as setDragging_(false).
|
||||
* Does nothing if useDragSurface_ is false.
|
||||
* @private
|
||||
*/
|
||||
Blockly.BlockSvg.prototype.moveOffDragSurface_ = function() {
|
||||
if (!this.useDragSurface_) {
|
||||
return;
|
||||
}
|
||||
// Translate to current position, turning off 3d.
|
||||
var xy = this.getRelativeToSurfaceXY();
|
||||
this.clearTransformAttributes_();
|
||||
this.translate(xy.x, xy.y);
|
||||
this.workspace.blockDragSurface_.clearAndHide(this.workspace.getCanvas());
|
||||
};
|
||||
|
||||
/**
|
||||
* Clear the block of transform="..." attributes.
|
||||
* Used when the block is switching from 3d to 2d transform or vice versa.
|
||||
* @private
|
||||
*/
|
||||
Blockly.BlockSvg.prototype.clearTransformAttributes_ = function() {
|
||||
this.getSvgRoot().removeAttribute('transform');
|
||||
};
|
||||
|
||||
/**
|
||||
* Snap this block to the nearest grid point.
|
||||
*/
|
||||
|
@ -793,18 +850,12 @@ Blockly.BlockSvg.prototype.onMouseUp_ = function(e) {
|
|||
new Blockly.Events.Ui(rootBlock, 'stackclick', undefined, undefined));
|
||||
}
|
||||
Blockly.terminateDrag_();
|
||||
// If we're over a delete area, delete the block even if it could be connected
|
||||
// to another block. Thi sis different from blockly.
|
||||
if (!this.getParent() && Blockly.selected.isDeletable() &&
|
||||
this.workspace.isDeleteArea(e)) {
|
||||
var trashcan = this.workspace.trashcan;
|
||||
if (trashcan) {
|
||||
setTimeout(trashcan.close.bind(trashcan), 100);
|
||||
}
|
||||
Blockly.selected.dispose(false, true);
|
||||
} else if (Blockly.selected && Blockly.highlightedConnection_) {
|
||||
this.positionNewBlock(Blockly.selected,
|
||||
Blockly.localConnection_, Blockly.highlightedConnection_);
|
||||
|
||||
var deleteArea = this.workspace.isDeleteArea(e);
|
||||
|
||||
// Connect to a nearby block, but not if it's over the toolbox.
|
||||
if (Blockly.selected && Blockly.highlightedConnection_ &&
|
||||
deleteArea != Blockly.DELETE_AREA_TOOLBOX) {
|
||||
// Connect two blocks together.
|
||||
Blockly.localConnection_.connect(Blockly.highlightedConnection_);
|
||||
if (this.rendered) {
|
||||
|
@ -818,7 +869,15 @@ Blockly.BlockSvg.prototype.onMouseUp_ = function(e) {
|
|||
// Don't throw an object in the trash can if it just got connected.
|
||||
this.workspace.trashcan.close();
|
||||
}
|
||||
} //else
|
||||
} else if (deleteArea && !this.getParent() && Blockly.selected.isDeletable()) {
|
||||
// We didn't connect the block, and it was over the trash can or the
|
||||
// toolbox. Delete it.
|
||||
var trashcan = this.workspace.trashcan;
|
||||
if (trashcan) {
|
||||
goog.Timer.callOnce(trashcan.close, 100, trashcan);
|
||||
}
|
||||
Blockly.selected.dispose(false, true);
|
||||
}
|
||||
if (Blockly.highlightedConnection_) {
|
||||
Blockly.highlightedConnection_ = null;
|
||||
}
|
||||
|
@ -1362,6 +1421,41 @@ Blockly.BlockSvg.prototype.updateCursor_ = function(e, closestConnection) {
|
|||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Provide visual indication of whether the block will be deleted if
|
||||
* dropped here.
|
||||
* Prefer connecting over dropping into the trash can, but prefer dragging to
|
||||
* the toolbox over connecting to other blocks.
|
||||
* @param {!Event} e Mouse move event.
|
||||
* @param {Blockly.Connection} closestConnection The connection this block would
|
||||
* potentially connect to if dropped here, or null.
|
||||
* @return {boolean} True if the block would be deleted if dropped here,
|
||||
* otherwise false.
|
||||
* @private
|
||||
*/
|
||||
Blockly.BlockSvg.prototype.updateCursor_ = function(e, closestConnection) {
|
||||
var deleteArea = this.workspace.isDeleteArea(e);
|
||||
var wouldConnect = Blockly.selected && closestConnection &&
|
||||
deleteArea != Blockly.DELETE_AREA_TOOLBOX;
|
||||
var wouldDelete = deleteArea && !this.getParent() &&
|
||||
Blockly.selected.isDeletable();
|
||||
var showDeleteCursor = wouldDelete && !wouldConnect;
|
||||
|
||||
if (showDeleteCursor) {
|
||||
Blockly.Css.setCursor(Blockly.Css.Cursor.DELETE);
|
||||
if (deleteArea == Blockly.DELETE_AREA_TRASH && this.workspace.trashcan) {
|
||||
this.workspace.trashcan.setOpen_(true);
|
||||
}
|
||||
return true;
|
||||
} else {
|
||||
Blockly.Css.setCursor(Blockly.Css.Cursor.CLOSED);
|
||||
if (this.workspace.trashcan) {
|
||||
this.workspace.trashcan.setOpen_(false);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Add or remove the UI indicating if this block is movable or not.
|
||||
*/
|
||||
|
@ -1637,7 +1731,7 @@ Blockly.BlockSvg.prototype.setWarningText = function(text, opt_id) {
|
|||
if (!newText) {
|
||||
this.warning.dispose();
|
||||
}
|
||||
changedState = oldText == newText;
|
||||
changedState = oldText != newText;
|
||||
}
|
||||
}
|
||||
if (changedState && this.rendered) {
|
||||
|
|
|
@ -401,7 +401,7 @@ Blockly.prompt = function(message, defaultValue, callback) {
|
|||
* Helper function for defining a block from JSON. The resulting function has
|
||||
* the correct value of jsonDef at the point in code where jsonInit is called.
|
||||
* @param {!Object} jsonDef The JSON definition of a block.
|
||||
* @return {function} A function that calls jsonInit with the correct value
|
||||
* @return {function()} A function that calls jsonInit with the correct value
|
||||
* of jsonDef.
|
||||
* @private
|
||||
*/
|
||||
|
@ -418,9 +418,15 @@ Blockly.jsonInitFactory_ = function(jsonDef) {
|
|||
*/
|
||||
Blockly.defineBlocksWithJsonArray = function(jsonArray) {
|
||||
for (var i = 0, elem; elem = jsonArray[i]; i++) {
|
||||
Blockly.Blocks[elem.type] = {
|
||||
init: Blockly.jsonInitFactory_(elem)
|
||||
};
|
||||
var typename = elem.type;
|
||||
if (typename == null || typename === '') {
|
||||
console.warn('Block definition #' + i +
|
||||
' in JSON array is missing a type attribute. Skipping.');
|
||||
} else {
|
||||
Blockly.Blocks[typename] = {
|
||||
init: Blockly.jsonInitFactory_(elem)
|
||||
};
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
|
|
|
@ -19,9 +19,14 @@
|
|||
*/
|
||||
|
||||
/**
|
||||
* @fileoverview Empty name space for the Blocks singleton.
|
||||
* @fileoverview A mapping of block type names to block prototype objects.
|
||||
* @author spertus@google.com (Ellen Spertus)
|
||||
*/
|
||||
'use strict';
|
||||
|
||||
/**
|
||||
* A mapping of block type names to block prototype objects.
|
||||
* @name Blockly.Blocks
|
||||
* @type {!Object<string,Object>}
|
||||
*/
|
||||
goog.provide('Blockly.Blocks');
|
||||
|
|
|
@ -337,7 +337,9 @@ Blockly.Connection.prototype.checkConnection_ = function(target) {
|
|||
case Blockly.Connection.REASON_TARGET_NULL:
|
||||
throw 'Target connection is null.';
|
||||
case Blockly.Connection.REASON_CHECKS_FAILED:
|
||||
throw 'Connection checks failed.';
|
||||
var msg = 'Connection checks failed. ';
|
||||
msg += this + ' expected ' + this.check_ + ', found ' + target.check_;
|
||||
throw msg;
|
||||
case Blockly.Connection.REASON_SHADOW_PARENT:
|
||||
throw 'Connecting non-shadow to shadow block.';
|
||||
default:
|
||||
|
@ -479,7 +481,7 @@ Blockly.Connection.prototype.connect = function(otherConnection) {
|
|||
/**
|
||||
* Update two connections to target each other.
|
||||
* @param {Blockly.Connection} first The first connection to update.
|
||||
* @param {Blockly.Connection} second The second conneciton to update.
|
||||
* @param {Blockly.Connection} second The second connection to update.
|
||||
* @private
|
||||
*/
|
||||
Blockly.Connection.connectReciprocally_ = function(first, second) {
|
||||
|
@ -612,6 +614,18 @@ Blockly.Connection.prototype.checkType_ = function(otherConnection) {
|
|||
return false;
|
||||
};
|
||||
|
||||
/**
|
||||
* Function to be called when this connection's compatible types have changed.
|
||||
* @private
|
||||
*/
|
||||
Blockly.Connection.prototype.onCheckChanged_ = function() {
|
||||
// The new value type may not be compatible with the existing connection.
|
||||
if (this.isConnected() && !this.checkType_(this.targetConnection)) {
|
||||
var child = this.isSuperior() ? this.targetBlock() : this.sourceBlock_;
|
||||
child.unplug();
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Change a connection's compatibility.
|
||||
* @param {*} check Compatible value type or list of value types.
|
||||
|
@ -626,13 +640,7 @@ Blockly.Connection.prototype.setCheck = function(check) {
|
|||
check = [check];
|
||||
}
|
||||
this.check_ = check;
|
||||
// The new value type may not be compatible with the existing connection.
|
||||
if (this.isConnected() && !this.checkType_(this.targetConnection)) {
|
||||
var child = this.isSuperior() ? this.targetBlock() : this.sourceBlock_;
|
||||
child.unplug();
|
||||
// Bump away.
|
||||
this.sourceBlock_.bumpNeighbours_();
|
||||
}
|
||||
this.onCheckChanged_();
|
||||
} else {
|
||||
this.check_ = null;
|
||||
}
|
||||
|
@ -673,3 +681,49 @@ Blockly.Connection.prototype.setShadowDom = function(shadow) {
|
|||
Blockly.Connection.prototype.getShadowDom = function() {
|
||||
return this.shadowDom_;
|
||||
};
|
||||
|
||||
/**
|
||||
* Find all nearby compatible connections to this connection.
|
||||
* Type checking does not apply, since this function is used for bumping.
|
||||
*
|
||||
* Headless configurations (the default) do not have neighboring connection,
|
||||
* and always return an empty list (the default).
|
||||
* {@link Blockly.RenderedConnection} overrides this behavior with a list
|
||||
* computed from the rendered positioning.
|
||||
* @param {number} maxLimit The maximum radius to another connection.
|
||||
* @return {!Array.<!Blockly.Connection>} List of connections.
|
||||
* @private
|
||||
*/
|
||||
Blockly.Connection.prototype.neighbours_ = function(/* maxLimit */) {
|
||||
return [];
|
||||
};
|
||||
|
||||
/**
|
||||
* This method returns a string describing this Connection in developer terms
|
||||
* (English only). Intended to on be used in console logs and errors.
|
||||
* @return {string} The description.
|
||||
*/
|
||||
Blockly.Connection.prototype.toString = function() {
|
||||
var msg;
|
||||
var block = this.sourceBlock_;
|
||||
if (!block) {
|
||||
return 'Orphan Connection';
|
||||
} else if (block.outputConnection == this) {
|
||||
msg = 'Output Connection of ';
|
||||
} else if (block.previousConnection == this) {
|
||||
msg = 'Previous Connection of ';
|
||||
} else if (block.nextConnection == this) {
|
||||
msg = 'Next Connection of ';
|
||||
} else {
|
||||
var parentInput = goog.array.find(block.inputList, function(input) {
|
||||
return input.connection == this;
|
||||
}, this);
|
||||
if (parentInput) {
|
||||
msg = 'Input "' + parentInput.name + '" connection on ';
|
||||
} else {
|
||||
console.warn('Connection not actually connected to sourceBlock_');
|
||||
return 'Orphan Connection';
|
||||
}
|
||||
}
|
||||
return msg + block.toDevString();
|
||||
};
|
||||
|
|
|
@ -274,3 +274,19 @@ Blockly.DELETE_AREA_TRASH = 1;
|
|||
* @const
|
||||
*/
|
||||
Blockly.DELETE_AREA_TOOLBOX = 2;
|
||||
|
||||
/**
|
||||
* String for use in the "custom" attribute of a category in toolbox xml.
|
||||
* This string indicates that the category should be dynamically populated with
|
||||
* variable blocks.
|
||||
* @const {string}
|
||||
*/
|
||||
Blockly.VARIABLE_CATEGORY_NAME = 'VARIABLE';
|
||||
|
||||
/**
|
||||
* String for use in the "custom" attribute of a category in toolbox xml.
|
||||
* This string indicates that the category should be dynamically populated with
|
||||
* procedure blocks.
|
||||
* @const {string}
|
||||
*/
|
||||
Blockly.PROCEDURE_CATEGORY_NAME = 'PROCEDURE';
|
||||
|
|
|
@ -24,6 +24,10 @@
|
|||
*/
|
||||
'use strict';
|
||||
|
||||
/**
|
||||
* @name Blockly.ContextMenu
|
||||
* @namespace
|
||||
*/
|
||||
goog.provide('Blockly.ContextMenu');
|
||||
|
||||
goog.require('goog.dom');
|
||||
|
|
19
core/css.js
19
core/css.js
|
@ -24,6 +24,10 @@
|
|||
*/
|
||||
'use strict';
|
||||
|
||||
/**
|
||||
* @name Blockly.Css
|
||||
* @namespace
|
||||
*/
|
||||
goog.provide('Blockly.Css');
|
||||
|
||||
goog.require('Blockly.Colours');
|
||||
|
@ -229,7 +233,7 @@ Blockly.Css.CONTENT = [
|
|||
'right: 0;',
|
||||
'bottom: 0;',
|
||||
'overflow: visible !important;',
|
||||
'z-index: 5000;',
|
||||
'z-index: 50;', /* Display below toolbox, but above everything else. */
|
||||
'}',
|
||||
|
||||
'.blocklyTooltipDiv {',
|
||||
|
@ -401,6 +405,10 @@ Blockly.Css.CONTENT = [
|
|||
'stroke-opacity: .5;',
|
||||
'}',
|
||||
|
||||
'.blocklyInsertionMarker>.blocklyPath {',
|
||||
'stroke: none;',
|
||||
'}',
|
||||
|
||||
'.blocklyText {',
|
||||
'fill: #fff;',
|
||||
'font-family: "Helvetica Neue", Helvetica, sans-serif;',
|
||||
|
@ -538,10 +546,17 @@ Blockly.Css.CONTENT = [
|
|||
'fill-opacity: .8;',
|
||||
'}',
|
||||
|
||||
'.blocklyMainWorkspaceScrollbar {',
|
||||
'z-index: 20;',
|
||||
'}',
|
||||
|
||||
'.blocklyFlyoutScrollbar {',
|
||||
'z-index: 30;',
|
||||
'}',
|
||||
|
||||
'.blocklyScrollbarHorizontal, .blocklyScrollbarVertical {',
|
||||
'position: absolute;',
|
||||
'outline: none;',
|
||||
'z-index: 30;',
|
||||
'}',
|
||||
|
||||
'.blocklyScrollbarBackground {',
|
||||
|
|
|
@ -355,7 +355,12 @@ Blockly.Events.Create = function(block) {
|
|||
return; // Blank event to be populated by fromJson.
|
||||
}
|
||||
Blockly.Events.Create.superClass_.constructor.call(this, block);
|
||||
this.xml = Blockly.Xml.blockToDomWithXY(block);
|
||||
|
||||
if (block.workspace.rendered) {
|
||||
this.xml = Blockly.Xml.blockToDomWithXY(block);
|
||||
} else {
|
||||
this.xml = Blockly.Xml.blockToDom(block);
|
||||
}
|
||||
this.ids = Blockly.Events.getDescendantIds_(block);
|
||||
};
|
||||
goog.inherits(Blockly.Events.Create, Blockly.Events.Abstract);
|
||||
|
@ -424,7 +429,12 @@ Blockly.Events.Delete = function(block) {
|
|||
throw 'Connected blocks cannot be deleted.';
|
||||
}
|
||||
Blockly.Events.Delete.superClass_.constructor.call(this, block);
|
||||
this.oldXml = Blockly.Xml.blockToDomWithXY(block);
|
||||
|
||||
if (block.workspace.rendered) {
|
||||
this.oldXml = Blockly.Xml.blockToDomWithXY(block);
|
||||
} else {
|
||||
this.oldXml = Blockly.Xml.blockToDom(block);
|
||||
}
|
||||
this.ids = Blockly.Events.getDescendantIds_(block);
|
||||
};
|
||||
goog.inherits(Blockly.Events.Delete, Blockly.Events.Abstract);
|
||||
|
|
241
core/extensions.js
Normal file
241
core/extensions.js
Normal file
|
@ -0,0 +1,241 @@
|
|||
/**
|
||||
* @license
|
||||
* Visual Blocks Editor
|
||||
*
|
||||
* Copyright 2017 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 Extensions are functions that help initialize blocks, usually
|
||||
* adding dynamic behavior such as onchange handlers and mutators. These
|
||||
* are applied using Block.applyExtension(), or the JSON "extensions"
|
||||
* array attribute.
|
||||
* @author Anm@anm.me (Andrew n marshall)
|
||||
*/
|
||||
'use strict';
|
||||
|
||||
/**
|
||||
* @name Blockly.Extensions
|
||||
* @namespace
|
||||
**/
|
||||
goog.provide('Blockly.Extensions');
|
||||
|
||||
|
||||
/**
|
||||
* The set of all registered extensions, keyed by extension name/id.
|
||||
* @private
|
||||
*/
|
||||
Blockly.Extensions.ALL_ = {};
|
||||
|
||||
/**
|
||||
* Registers a new extension function. Extensions are functions that help
|
||||
* initialize blocks, usually adding dynamic behavior such as onchange
|
||||
* handlers and mutators. These are applied using Block.applyExtension(), or
|
||||
* the JSON "extensions" array attribute.
|
||||
* @param {string} name The name of this extension.
|
||||
* @param {function} initFn The function to initialize an extended block.
|
||||
* @throws {Error} if the extension name is empty, the extension is already
|
||||
* registered, or extensionFn is not a function.
|
||||
*/
|
||||
Blockly.Extensions.register = function(name, initFn) {
|
||||
if (!goog.isString(name) || goog.string.isEmptyOrWhitespace(name)) {
|
||||
throw new Error('Error: Invalid extension name "' + name + '"');
|
||||
}
|
||||
if (Blockly.Extensions.ALL_[name]) {
|
||||
throw new Error('Error: Extension "' + name + '" is already registered.');
|
||||
}
|
||||
if (!goog.isFunction(initFn)) {
|
||||
throw new Error('Error: Extension "' + name + '" must be a function');
|
||||
}
|
||||
Blockly.Extensions.ALL_[name] = initFn;
|
||||
};
|
||||
|
||||
/**
|
||||
* Registers a new extension function that adds all key/value of mixinObj.
|
||||
* @param {string} name The name of this extension.
|
||||
* @param {!Object} mixinObj The values to mix in.
|
||||
* @throws {Error} if the extension name is empty or the extension is already
|
||||
* registered.
|
||||
*/
|
||||
Blockly.Extensions.registerMixin = function(name, mixinObj) {
|
||||
Blockly.Extensions.register(name, function() {
|
||||
this.mixin(mixinObj);
|
||||
});
|
||||
};
|
||||
|
||||
/**
|
||||
* Applies an extension method to a block. This should only be called during
|
||||
* block construction.
|
||||
* @param {string} name The name of the extension.
|
||||
* @param {!Blockly.Block} block The block to apply the named extension to.
|
||||
* @throws {Error} if the extension is not found.
|
||||
*/
|
||||
Blockly.Extensions.apply = function(name, block) {
|
||||
var extensionFn = Blockly.Extensions.ALL_[name];
|
||||
if (!goog.isFunction(extensionFn)) {
|
||||
throw new Error('Error: Extension "' + name + '" not found.');
|
||||
}
|
||||
extensionFn.apply(block);
|
||||
};
|
||||
|
||||
/**
|
||||
* Builds an extension function that will map a dropdown value to a tooltip
|
||||
* string.
|
||||
*
|
||||
* This method includes multiple checks to ensure tooltips, dropdown options,
|
||||
* and message references are aligned. This aims to catch errors as early as
|
||||
* possible, without requiring developers to manually test tooltips under each
|
||||
* option. After the page is loaded, each tooltip text string will be checked
|
||||
* for matching message keys in the internationalized string table. Deferring
|
||||
* this until the page is loaded decouples loading dependencies. Later, upon
|
||||
* loading the first block of any given type, the extension will validate every
|
||||
* dropdown option has a matching tooltip in the lookupTable. Errors are
|
||||
* reported as warnings in the console, and are never fatal.
|
||||
* @param {string} dropdownName The name of the field whose value is the key
|
||||
* to the lookup table.
|
||||
* @param {!Object<string, string>} lookupTable The table of field values to
|
||||
* tooltip text.
|
||||
* @return {Function} The extension function.
|
||||
*/
|
||||
Blockly.Extensions.buildTooltipForDropdown = function(dropdownName, lookupTable) {
|
||||
// List of block types already validated, to minimize duplicate warnings.
|
||||
var blockTypesChecked = [];
|
||||
|
||||
// Check the tooltip string messages for invalid references.
|
||||
// Wait for load, in case Blockly.Msg is not yet populated.
|
||||
// runAfterPageLoad() does not run in a Node.js environment due to lack of
|
||||
// document object, in which case skip the validation.
|
||||
if (document) { // Relies on document.readyState
|
||||
Blockly.utils.runAfterPageLoad(function() {
|
||||
for (var key in lookupTable) {
|
||||
// Will print warnings is reference is missing.
|
||||
Blockly.utils.checkMessageReferences(lookupTable[key]);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* The actual extension.
|
||||
* @this {Blockly.Block}
|
||||
*/
|
||||
var extensionFn = function() {
|
||||
if (this.type && blockTypesChecked.indexOf(this.type) === -1) {
|
||||
Blockly.Extensions.checkDropdownOptionsInTable_(
|
||||
this, dropdownName, lookupTable);
|
||||
blockTypesChecked.push(this.type);
|
||||
}
|
||||
|
||||
this.setTooltip(function() {
|
||||
var value = this.getFieldValue(dropdownName);
|
||||
var tooltip = lookupTable[value];
|
||||
if (tooltip == null) {
|
||||
if (blockTypesChecked.indexOf(this.type) === -1) {
|
||||
// Warn for missing values on generated tooltips
|
||||
var warning = 'No tooltip mapping for value ' + value +
|
||||
' of field ' + dropdownName;
|
||||
if (this.type != null) {
|
||||
warning += (' of block type ' + this.type);
|
||||
}
|
||||
console.warn(warning + '.');
|
||||
}
|
||||
} else {
|
||||
tooltip = Blockly.utils.replaceMessageReferences(tooltip);
|
||||
}
|
||||
return tooltip;
|
||||
}.bind(this));
|
||||
};
|
||||
return extensionFn;
|
||||
};
|
||||
|
||||
/**
|
||||
* Checks all options keys are present in the provided string lookup table.
|
||||
* Emits console warnings when they are not.
|
||||
* @param {!Blockly.Block} block The block containing the dropdown
|
||||
* @param {string} dropdownName The name of the dropdown
|
||||
* @param {!Object<string, string>} lookupTable The string lookup table
|
||||
* @private
|
||||
*/
|
||||
Blockly.Extensions.checkDropdownOptionsInTable_ =
|
||||
function(block, dropdownName, lookupTable) {
|
||||
// Validate all dropdown options have values.
|
||||
var dropdown = block.getField(dropdownName);
|
||||
if (!dropdown.isOptionListDynamic()) {
|
||||
var options = dropdown.getOptions();
|
||||
for (var i = 0; i < options.length; ++i) {
|
||||
var optionKey = options[i][1]; // label, then value
|
||||
if (lookupTable[optionKey] == null) {
|
||||
console.warn('No tooltip mapping for value ' + optionKey +
|
||||
' of field ' + dropdownName + ' of block type ' + block.type);
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Builds an extension function that will install a dynamic tooltip. The
|
||||
* tooltip message should include the string '%1' and that string will be
|
||||
* replaced with the value of the named field.
|
||||
* @param {string} msgTemplate The template form to of the message text, with
|
||||
* %1 placeholder.
|
||||
* @param {string} fieldName The field with the replacement value.
|
||||
* @returns {Function} The extension function.
|
||||
*/
|
||||
Blockly.Extensions.buildTooltipWithFieldValue =
|
||||
function(msgTemplate, fieldName) {
|
||||
// Check the tooltip string messages for invalid references.
|
||||
// Wait for load, in case Blockly.Msg is not yet populated.
|
||||
// runAfterPageLoad() does not run in a Node.js environment due to lack of
|
||||
// document object, in which case skip the validation.
|
||||
if (document) { // Relies on document.readyState
|
||||
Blockly.utils.runAfterPageLoad(function() {
|
||||
// Will print warnings is reference is missing.
|
||||
Blockly.utils.checkMessageReferences(msgTemplate);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* The actual extension.
|
||||
* @this {Blockly.Block}
|
||||
*/
|
||||
var extensionFn = function() {
|
||||
this.setTooltip(function() {
|
||||
return Blockly.utils.replaceMessageReferences(msgTemplate)
|
||||
.replace('%1', this.getFieldValue(fieldName));
|
||||
}.bind(this));
|
||||
};
|
||||
return extensionFn;
|
||||
};
|
||||
|
||||
/**
|
||||
* Configures the tooltip to mimic the parent block when connected. Otherwise,
|
||||
* uses the tooltip text at the time this extension is initialized. This takes
|
||||
* advantage of the fact that all other values from JSON are initialized before
|
||||
* extensions.
|
||||
* @this {Blockly.Block}
|
||||
* @private
|
||||
*/
|
||||
Blockly.Extensions.extensionParentTooltip_ = function() {
|
||||
this.tooltipWhenNotConnected_ = this.tooltip;
|
||||
this.setTooltip(function() {
|
||||
var parent = this.getParent();
|
||||
return (parent &&
|
||||
parent.getInputsInline() &&
|
||||
parent.tooltip) ||
|
||||
this.tooltipWhenNotConnected_;
|
||||
}.bind(this));
|
||||
};
|
||||
Blockly.Extensions.register('parent_tooltip_when_inline',
|
||||
Blockly.Extensions.extensionParentTooltip_);
|
136
core/field.js
136
core/field.js
|
@ -77,7 +77,7 @@ Blockly.Field.cacheReference_ = 0;
|
|||
/**
|
||||
* Name of field. Unique within each block.
|
||||
* Static labels are usually unnamed.
|
||||
* @type {string=}
|
||||
* @type {string|undefined}
|
||||
*/
|
||||
Blockly.Field.prototype.name = undefined;
|
||||
|
||||
|
@ -215,6 +215,17 @@ Blockly.Field.prototype.updateEditable = function() {
|
|||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Check whether this field is currently editable. Some fields are never
|
||||
* editable (e.g. text labels). Those fields are not serialized to XML. Other
|
||||
* fields may be editable, and therefore serialized, but may exist on
|
||||
* non-editable blocks.
|
||||
* @return {boolean} whether this field is editable and on an editable block
|
||||
*/
|
||||
Blockly.Field.prototype.isCurrentlyEditable = function() {
|
||||
return this.EDITABLE && !!this.sourceBlock_ && this.sourceBlock_.isEditable();
|
||||
};
|
||||
|
||||
/**
|
||||
* Gets whether this editable field is visible or not.
|
||||
* @return {boolean} True if visible.
|
||||
|
@ -329,38 +340,17 @@ Blockly.Field.prototype.getSvgRoot = function() {
|
|||
* @private
|
||||
*/
|
||||
Blockly.Field.prototype.render_ = function() {
|
||||
var width = 0;
|
||||
|
||||
if (this.visible_ && this.textElement_) {
|
||||
// Replace the text.
|
||||
goog.dom.removeChildren(/** @type {!Element} */ (this.textElement_));
|
||||
var textNode = document.createTextNode(this.getDisplayText_());
|
||||
this.textElement_.appendChild(textNode);
|
||||
|
||||
// Calculate width of field
|
||||
width = Blockly.Field.getCachedWidth(this.textElement_);
|
||||
|
||||
// Add padding to left and right of text.
|
||||
if (this.EDITABLE) {
|
||||
width += Blockly.BlockSvg.EDITABLE_FIELD_PADDING;
|
||||
}
|
||||
|
||||
// Adjust width for drop-down arrows.
|
||||
var arrowWidth = 0;
|
||||
if (this.positionArrow) {
|
||||
arrowWidth = this.positionArrow(width);
|
||||
width += arrowWidth;
|
||||
}
|
||||
|
||||
// Add padding to any drawn box.
|
||||
if (this.box_) {
|
||||
width += 2 * Blockly.BlockSvg.BOX_FIELD_PADDING;
|
||||
}
|
||||
this.updateWidth();
|
||||
|
||||
// Update text centering, based on newly calculated width.
|
||||
var centerTextX = (width - arrowWidth) / 2;
|
||||
var centerTextX = (this.size_.width - this.arrowWidth_) / 2;
|
||||
if (this.sourceBlock_.RTL) {
|
||||
centerTextX += arrowWidth;
|
||||
centerTextX += this.arrowWidth_;
|
||||
}
|
||||
|
||||
// In a text-editing shadow block's field,
|
||||
|
@ -373,7 +363,7 @@ Blockly.Field.prototype.render_ = function() {
|
|||
// X position starts at the left edge of the block, in both RTL and LTR.
|
||||
// First offset by the width of the block to move to the right edge,
|
||||
// and then subtract to move to the same position as LTR.
|
||||
var minCenter = width - minOffset;
|
||||
var minCenter = this.size_.width - minOffset;
|
||||
centerTextX = Math.min(minCenter, centerTextX);
|
||||
} else {
|
||||
// (width / 2) should exceed Blockly.BlockSvg.FIELD_WIDTH / 2
|
||||
|
@ -386,9 +376,6 @@ Blockly.Field.prototype.render_ = function() {
|
|||
this.textElement_.setAttribute('x', centerTextX);
|
||||
}
|
||||
|
||||
// Set width of the field.
|
||||
this.size_.width = width;
|
||||
|
||||
// Update any drawn box to the correct width and height.
|
||||
if (this.box_) {
|
||||
this.box_.setAttribute('width', this.size_.width);
|
||||
|
@ -396,6 +383,37 @@ Blockly.Field.prototype.render_ = function() {
|
|||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Updates thw width of the field. This calls getCachedWidth which won't cache
|
||||
* the approximated width on IE/Edge when `getComputedTextLength` fails. Once
|
||||
* it eventually does succeed, the result will be cached.
|
||||
**/
|
||||
Blockly.Field.prototype.updateWidth = function() {
|
||||
var width = Blockly.Field.getCachedWidth(this.textElement_);
|
||||
// Calculate width of field
|
||||
width = Blockly.Field.getCachedWidth(this.textElement_);
|
||||
|
||||
// Add padding to left and right of text.
|
||||
if (this.EDITABLE) {
|
||||
width += Blockly.BlockSvg.EDITABLE_FIELD_PADDING;
|
||||
}
|
||||
|
||||
// Adjust width for drop-down arrows.
|
||||
this.arrowWidth_ = 0;
|
||||
if (this.positionArrow) {
|
||||
this.arrowWidth_ = this.positionArrow(width);
|
||||
width += this.arrowWidth_;
|
||||
}
|
||||
|
||||
// Add padding to any drawn box.
|
||||
if (this.box_) {
|
||||
width += 2 * Blockly.BlockSvg.BOX_FIELD_PADDING;
|
||||
}
|
||||
|
||||
// Set width of the field.
|
||||
this.size_.width = width;
|
||||
};
|
||||
|
||||
/**
|
||||
* Gets the width of a text element, caching it in the process.
|
||||
* @param {!Element} textElement An SVG 'text' element.
|
||||
|
@ -403,21 +421,32 @@ Blockly.Field.prototype.render_ = function() {
|
|||
*/
|
||||
Blockly.Field.getCachedWidth = function(textElement) {
|
||||
var key = textElement.textContent + '\n' + textElement.className.baseVal;
|
||||
if (Blockly.Field.cacheWidths_ && Blockly.Field.cacheWidths_[key]) {
|
||||
var width = Blockly.Field.cacheWidths_[key];
|
||||
} else {
|
||||
try {
|
||||
var width = textElement.getComputedTextLength();
|
||||
} catch (e) {
|
||||
// MSIE 11 is known to throw "Unexpected call to method or property
|
||||
// access." if Blockly is hidden.
|
||||
var width = textElement.textContent.length * 8;
|
||||
}
|
||||
var width;
|
||||
|
||||
if (Blockly.Field.cacheWidths_) {
|
||||
Blockly.Field.cacheWidths_[key] = width;
|
||||
// Return the cached width if it exists.
|
||||
if (Blockly.Field.cacheWidths_) {
|
||||
width = Blockly.Field.cacheWidths_[key];
|
||||
if (width) {
|
||||
return width;
|
||||
}
|
||||
}
|
||||
|
||||
// Attempt to compute fetch the width of the SVG text element.
|
||||
try {
|
||||
width = textElement.getComputedTextLength();
|
||||
} catch (e) {
|
||||
// MSIE 11 and Edge are known to throw "Unexpected call to method or
|
||||
// property access." if the block is hidden. Instead, use an
|
||||
// approximation and do not cache the result. At some later point in time
|
||||
// when the block is inserted into the visible DOM, this method will be
|
||||
// called again and, at that point in time, will not throw an exception.
|
||||
return textElement.textContent.length * 8;
|
||||
}
|
||||
|
||||
// Cache the computed width and return.
|
||||
if (Blockly.Field.cacheWidths_) {
|
||||
Blockly.Field.cacheWidths_[key] = width;
|
||||
}
|
||||
return width;
|
||||
};
|
||||
|
||||
|
@ -492,6 +521,31 @@ Blockly.Field.prototype.getDisplayText_ = function() {
|
|||
return text;
|
||||
};
|
||||
|
||||
/**
|
||||
* Get the text from this field as displayed on screen. May differ from getText
|
||||
* due to ellipsis, and other formatting.
|
||||
* @return {string} Currently displayed text.
|
||||
* @private
|
||||
*/
|
||||
Blockly.Field.prototype.getDisplayText_ = function() {
|
||||
var text = this.text_;
|
||||
if (!text) {
|
||||
// Prevent the field from disappearing if empty.
|
||||
return Blockly.Field.NBSP;
|
||||
}
|
||||
if (text.length > this.maxDisplayLength) {
|
||||
// Truncate displayed string and add an ellipsis ('...').
|
||||
text = text.substring(0, this.maxDisplayLength - 2) + '\u2026';
|
||||
}
|
||||
// Replace whitespace with non-breaking spaces so the text doesn't collapse.
|
||||
text = text.replace(/\s/g, Blockly.Field.NBSP);
|
||||
if (this.sourceBlock_.RTL) {
|
||||
// The SVG is LTR, force text to be RTL.
|
||||
text += '\u200F';
|
||||
}
|
||||
return text;
|
||||
};
|
||||
|
||||
/**
|
||||
* Get the text from this field.
|
||||
* @return {string} Current text.
|
||||
|
|
|
@ -34,7 +34,8 @@ goog.require('goog.userAgent');
|
|||
|
||||
/**
|
||||
* Class for an editable angle field.
|
||||
* @param {string} text The initial content of the field.
|
||||
* @param {(string|number)=} opt_value The initial content of the field. The
|
||||
* value should cast to a number, and if it does not, '0' will be used.
|
||||
* @param {Function=} opt_validator An optional function that is called
|
||||
* to validate any constraints on what the user entered. Takes the new
|
||||
* text as an argument and returns the accepted text or null to abort
|
||||
|
@ -42,12 +43,14 @@ goog.require('goog.userAgent');
|
|||
* @extends {Blockly.FieldTextInput}
|
||||
* @constructor
|
||||
*/
|
||||
Blockly.FieldAngle = function(text, opt_validator) {
|
||||
// Add degree symbol: "360°" (LTR) or "°360" (RTL)
|
||||
Blockly.FieldAngle = function(opt_value, opt_validator) {
|
||||
// Add degree symbol: '360°' (LTR) or '°360' (RTL)
|
||||
this.symbol_ = Blockly.utils.createSvgElement('tspan', {}, null);
|
||||
this.symbol_.appendChild(document.createTextNode('\u00B0'));
|
||||
|
||||
Blockly.FieldAngle.superClass_.constructor.call(this, text, opt_validator);
|
||||
opt_value = (opt_value && !isNaN(opt_value)) ? String(opt_value) : '0';
|
||||
Blockly.FieldAngle.superClass_.constructor.call(
|
||||
this, opt_value, opt_validator);
|
||||
this.addArgType('angle');
|
||||
};
|
||||
goog.inherits(Blockly.FieldAngle, Blockly.FieldTextInput);
|
||||
|
|
|
@ -40,8 +40,8 @@ goog.require('goog.userAgent');
|
|||
|
||||
/**
|
||||
* Class for an editable dropdown field.
|
||||
* @param {(!Array.<!Array.<string>>|!Function)} menuGenerator An array of
|
||||
* options for a dropdown list, or a function which generates these options.
|
||||
* @param {(!Array.<!Array>|!Function)} menuGenerator An array of options
|
||||
* for a dropdown list, or a function which generates these options.
|
||||
* @param {Function=} opt_validator A function that is executed when a new
|
||||
* option is selected, with the newly selected value as its sole argument.
|
||||
* If it returns a value, that value (which must be one of the options) will
|
||||
|
@ -53,7 +53,7 @@ goog.require('goog.userAgent');
|
|||
Blockly.FieldDropdown = function(menuGenerator, opt_validator) {
|
||||
this.menuGenerator_ = menuGenerator;
|
||||
this.trimOptions_();
|
||||
var firstTuple = this.getOptions_()[0];
|
||||
var firstTuple = this.getOptions()[0];
|
||||
|
||||
// Call parent's constructor.
|
||||
Blockly.FieldDropdown.superClass_.constructor.call(this, firstTuple[1],
|
||||
|
@ -63,7 +63,7 @@ Blockly.FieldDropdown = function(menuGenerator, opt_validator) {
|
|||
goog.inherits(Blockly.FieldDropdown, Blockly.Field);
|
||||
|
||||
/**
|
||||
* Horizontal distance that a checkmark ovehangs the dropdown.
|
||||
* Horizontal distance that a checkmark overhangs the dropdown.
|
||||
*/
|
||||
Blockly.FieldDropdown.CHECKMARK_OVERHANG = 25;
|
||||
|
||||
|
@ -78,6 +78,28 @@ Blockly.FieldDropdown.prototype.CURSOR = 'default';
|
|||
*/
|
||||
Blockly.FieldDropdown.prototype.selectedItem = null;
|
||||
|
||||
/**
|
||||
* Language-neutral currently selected string or image object.
|
||||
* @type {string|!Object}
|
||||
* @private
|
||||
*/
|
||||
Blockly.FieldDropdown.prototype.value_ = '';
|
||||
|
||||
/**
|
||||
* SVG image element if currently selected option is an image, or null.
|
||||
* @type {SVGElement}
|
||||
* @private
|
||||
*/
|
||||
Blockly.FieldDropdown.prototype.imageElement_ = null;
|
||||
|
||||
/**
|
||||
* Object with src, height, width, and alt attributes if currently selected
|
||||
* option is an image, or null.
|
||||
* @type {Object}
|
||||
* @private
|
||||
*/
|
||||
Blockly.FieldDropdown.prototype.imageJson_ = null;
|
||||
|
||||
/**
|
||||
* Install this dropdown on a block.
|
||||
*/
|
||||
|
@ -145,15 +167,23 @@ Blockly.FieldDropdown.prototype.showEditor_ = function() {
|
|||
thisField.onItemSelected(menu, menuItem);
|
||||
}
|
||||
Blockly.DropDownDiv.hide();
|
||||
Blockly.Events.setGroup(false);
|
||||
}
|
||||
|
||||
var menu = new goog.ui.Menu();
|
||||
menu.setRightToLeft(this.sourceBlock_.RTL);
|
||||
var options = this.getOptions_();
|
||||
var options = this.getOptions();
|
||||
for (var i = 0; i < options.length; i++) {
|
||||
var text = options[i][0]; // Human-readable text.
|
||||
var value = options[i][1]; // Language-neutral value.
|
||||
var menuItem = new goog.ui.MenuItem(text);
|
||||
var content = options[i][0]; // Human-readable text or image.
|
||||
var value = options[i][1]; // Language-neutral value.
|
||||
if (typeof content == 'object') {
|
||||
// An image, not text.
|
||||
var image = new Image(content['width'], content['height']);
|
||||
image.src = content['src'];
|
||||
image.alt = content['alt'] || '';
|
||||
content = image;
|
||||
}
|
||||
var menuItem = new goog.ui.MenuItem(content);
|
||||
menuItem.setRightToLeft(this.sourceBlock_.RTL);
|
||||
menuItem.setValue(value);
|
||||
menuItem.setCheckable(true);
|
||||
|
@ -248,8 +278,8 @@ Blockly.FieldDropdown.prototype.onHide = function() {
|
|||
|
||||
/**
|
||||
* Handle the selection of an item in the dropdown menu.
|
||||
* @param {goog.ui.Menu} menu The Menu component clicked.
|
||||
* @param {goog.ui.MenuItem} menuItem The MenuItem selected within menu.
|
||||
* @param {!goog.ui.Menu} menu The Menu component clicked.
|
||||
* @param {!goog.ui.MenuItem} menuItem The MenuItem selected within menu.
|
||||
*/
|
||||
Blockly.FieldDropdown.prototype.onItemSelected = function(menu, menuItem) {
|
||||
var value = menuItem.getValue();
|
||||
|
@ -271,10 +301,30 @@ Blockly.FieldDropdown.prototype.trimOptions_ = function() {
|
|||
this.prefixField = null;
|
||||
this.suffixField = null;
|
||||
var options = this.menuGenerator_;
|
||||
if (!goog.isArray(options) || options.length < 2) {
|
||||
if (!goog.isArray(options)) {
|
||||
return;
|
||||
}
|
||||
var strings = options.map(function(t) {return t[0];});
|
||||
var hasImages = false;
|
||||
|
||||
// Localize label text and image alt text.
|
||||
for (var i = 0; i < options.length; i++) {
|
||||
var label = options[i][0];
|
||||
if (typeof label == 'string') {
|
||||
options[i][0] = Blockly.utils.replaceMessageReferences(label);
|
||||
} else {
|
||||
if (label.alt != null) {
|
||||
options[i][0].alt = Blockly.utils.replaceMessageReferences(label.alt);
|
||||
}
|
||||
hasImages = true;
|
||||
}
|
||||
}
|
||||
if (hasImages || options.length < 2) {
|
||||
return; // Do nothing if too few items or at least one label is an image.
|
||||
}
|
||||
var strings = [];
|
||||
for (var i = 0; i < options.length; i++) {
|
||||
strings.push(options[i][0]);
|
||||
}
|
||||
var shortest = Blockly.utils.shortestStringLength(strings);
|
||||
var prefixLength = Blockly.utils.commonWordPrefix(strings, shortest);
|
||||
var suffixLength = Blockly.utils.commonWordSuffix(strings, shortest);
|
||||
|
@ -303,12 +353,18 @@ Blockly.FieldDropdown.prototype.trimOptions_ = function() {
|
|||
};
|
||||
|
||||
/**
|
||||
* Return a list of the options for this dropdown.
|
||||
* @return {!Array.<!Array.<string>>} Array of option tuples:
|
||||
* (human-readable text, language-neutral name).
|
||||
* @private
|
||||
* @return {boolean} True if the option list is generated by a function. Otherwise false.
|
||||
*/
|
||||
Blockly.FieldDropdown.prototype.getOptions_ = function() {
|
||||
Blockly.FieldDropdown.prototype.isOptionListDynamic = function() {
|
||||
return goog.isFunction(this.menuGenerator_);
|
||||
};
|
||||
|
||||
/**
|
||||
* Return a list of the options for this dropdown.
|
||||
* @return {!Array.<!Array>} Array of option tuples:
|
||||
* (human-readable text or image, language-neutral name).
|
||||
*/
|
||||
Blockly.FieldDropdown.prototype.getOptions = function() {
|
||||
if (goog.isFunction(this.menuGenerator_)) {
|
||||
return this.menuGenerator_.call(this);
|
||||
}
|
||||
|
@ -342,11 +398,18 @@ Blockly.FieldDropdown.prototype.setValue = function(newValue) {
|
|||
}
|
||||
this.value_ = newValue;
|
||||
// Look up and display the human-readable text.
|
||||
var options = this.getOptions_();
|
||||
var options = this.getOptions();
|
||||
for (var i = 0; i < options.length; i++) {
|
||||
// Options are tuples of human-readable text and language-neutral values.
|
||||
if (options[i][1] == newValue) {
|
||||
this.setText(options[i][0]);
|
||||
var content = options[i][0];
|
||||
if (typeof content == 'object') {
|
||||
this.imageJson_ = content;
|
||||
this.setText(content.alt);
|
||||
} else {
|
||||
this.imageJson_ = null;
|
||||
this.setText(content);
|
||||
}
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
@ -356,7 +419,7 @@ Blockly.FieldDropdown.prototype.setValue = function(newValue) {
|
|||
};
|
||||
|
||||
/**
|
||||
* Set the text in this field. Trigger a rerender of the source block.
|
||||
* Sets the text in this field. Trigger a rerender of the source block.
|
||||
* @param {?string} text New text.
|
||||
*/
|
||||
Blockly.FieldDropdown.prototype.setText = function(text) {
|
||||
|
@ -375,7 +438,6 @@ Blockly.FieldDropdown.prototype.setText = function(text) {
|
|||
);
|
||||
this.textElement_.parentNode.appendChild(this.arrow_);
|
||||
}
|
||||
|
||||
if (this.sourceBlock_ && this.sourceBlock_.rendered) {
|
||||
this.sourceBlock_.render();
|
||||
this.sourceBlock_.bumpNeighbours_();
|
||||
|
|
|
@ -33,7 +33,7 @@ goog.require('goog.userAgent');
|
|||
|
||||
|
||||
/**
|
||||
* Class for an image.
|
||||
* Class for an image on a block.
|
||||
* @param {string} src The URL of the image.
|
||||
* @param {number} width Width of the image.
|
||||
* @param {number} height Height of the image.
|
||||
|
@ -44,6 +44,7 @@ goog.require('goog.userAgent');
|
|||
*/
|
||||
Blockly.FieldImage = function(src, width, height, opt_alt, flip_rtl) {
|
||||
this.sourceBlock_ = null;
|
||||
|
||||
// Ensure height and width are numbers. Strings are bad at math.
|
||||
this.height_ = Number(height);
|
||||
this.width_ = Number(width);
|
||||
|
@ -74,9 +75,13 @@ Blockly.FieldImage.prototype.init = function() {
|
|||
this.fieldGroup_.style.display = 'none';
|
||||
}
|
||||
/** @type {SVGElement} */
|
||||
this.imageElement_ = Blockly.utils.createSvgElement('image',
|
||||
{'height': this.height_ + 'px',
|
||||
'width': this.width_ + 'px'}, this.fieldGroup_);
|
||||
this.imageElement_ = Blockly.utils.createSvgElement(
|
||||
'image',
|
||||
{
|
||||
'height': this.height_ + 'px',
|
||||
'width': this.width_ + 'px'
|
||||
},
|
||||
this.fieldGroup_);
|
||||
this.setValue(this.src_);
|
||||
this.sourceBlock_.getSvgRoot().appendChild(this.fieldGroup_);
|
||||
|
||||
|
|
|
@ -39,10 +39,11 @@ goog.require('goog.userAgent');
|
|||
* These properties are included here (i.e. instead of just accepting a
|
||||
* decimalAllowed, negativeAllowed) to maintain API compatibility with Blockly
|
||||
* and Blockly for Android.
|
||||
* @param {number|string} value The initial content of the field.
|
||||
* @param {number|string|undefined} opt_min Minimum value.
|
||||
* @param {number|string|undefined} opt_max Maximum value.
|
||||
* @param {number|string|undefined} opt_precision Precision for value.
|
||||
* @param {(string|number)=} opt_value The initial content of the field. The value
|
||||
* should cast to a number, and if it does not, '0' will be used.
|
||||
* @param {(string|number)=} opt_min Minimum value.
|
||||
* @param {(string|number)=} opt_max Maximum value.
|
||||
* @param {(string|number)=} opt_precision Precision for value.
|
||||
* @param {Function=} opt_validator An optional function that is called
|
||||
* to validate any constraints on what the user entered. Takes the new
|
||||
* text as an argument and returns the accepted text or null to abort
|
||||
|
@ -50,11 +51,12 @@ goog.require('goog.userAgent');
|
|||
* @extends {Blockly.FieldTextInput}
|
||||
* @constructor
|
||||
*/
|
||||
Blockly.FieldNumber = function(value, opt_min, opt_max, opt_precision,
|
||||
Blockly.FieldNumber = function(opt_value, opt_min, opt_max, opt_precision,
|
||||
opt_validator) {
|
||||
var numRestrictor = this.getNumRestrictor(opt_min, opt_max, opt_precision);
|
||||
Blockly.FieldNumber.superClass_.constructor.call(this, value, opt_validator,
|
||||
numRestrictor);
|
||||
opt_value = (opt_value && !isNaN(opt_value)) ? String(opt_value) : '0';
|
||||
Blockly.FieldNumber.superClass_.constructor.call(
|
||||
this, opt_value, opt_validator, numRestrictor);
|
||||
this.addArgType('number');
|
||||
};
|
||||
goog.inherits(Blockly.FieldNumber, Blockly.FieldTextInput);
|
||||
|
|
|
@ -488,7 +488,7 @@ Blockly.FieldTextInput.prototype.widgetDispose_ = function() {
|
|||
}
|
||||
}
|
||||
}
|
||||
thisField.setValue(text);
|
||||
thisField.setText(text);
|
||||
thisField.sourceBlock_.rendered && thisField.sourceBlock_.render();
|
||||
Blockly.unbindEvent_(htmlInput.onKeyDownWrapper_);
|
||||
Blockly.unbindEvent_(htmlInput.onKeyUpWrapper_);
|
||||
|
|
|
@ -29,6 +29,7 @@ goog.provide('Blockly.FieldVariable');
|
|||
goog.require('Blockly.FieldDropdown');
|
||||
goog.require('Blockly.Msg');
|
||||
goog.require('Blockly.Variables');
|
||||
goog.require('goog.asserts');
|
||||
goog.require('goog.string');
|
||||
|
||||
|
||||
|
@ -52,12 +53,14 @@ goog.inherits(Blockly.FieldVariable, Blockly.FieldDropdown);
|
|||
/**
|
||||
* The menu item index for the rename variable option.
|
||||
* @type {number}
|
||||
* @private
|
||||
*/
|
||||
Blockly.FieldVariable.prototype.renameVarItemIndex_ = -1;
|
||||
|
||||
/**
|
||||
* The menu item index for the delete variable option.
|
||||
* @type {number}
|
||||
* @private
|
||||
*/
|
||||
Blockly.FieldVariable.prototype.deleteVarItemIndex_ = -1;
|
||||
|
||||
|
@ -87,6 +90,16 @@ Blockly.FieldVariable.prototype.init = function() {
|
|||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Attach this field to a block.
|
||||
* @param {!Blockly.Block} block The block containing this field.
|
||||
*/
|
||||
Blockly.FieldVariable.prototype.setSourceBlock = function(block) {
|
||||
goog.asserts.assert(!block.isShadow(),
|
||||
'Variable fields are not allowed to exist on shadow blocks.');
|
||||
Blockly.FieldVariable.superClass_.setSourceBlock.call(this, block);
|
||||
};
|
||||
|
||||
/**
|
||||
* Get the variable's name (use a variableDB to convert into a real name).
|
||||
* Unline a regular dropdown, variables are literal and have no neutral value.
|
||||
|
@ -113,7 +126,7 @@ Blockly.FieldVariable.prototype.setValue = function(newValue) {
|
|||
* Return a sorted list of variable names for variable dropdown menus.
|
||||
* Include a special option at the end for creating a new variable name.
|
||||
* @return {!Array.<string>} Array of variable names.
|
||||
* @this Blockly.FieldVariable
|
||||
* @this {Blockly.FieldVariable}
|
||||
*/
|
||||
Blockly.FieldVariable.dropdownCreate = function() {
|
||||
if (this.sourceBlock_ && this.sourceBlock_.workspace) {
|
||||
|
|
|
@ -307,8 +307,8 @@ Blockly.Flyout.prototype.dragMode_ = Blockly.DRAG_NONE;
|
|||
|
||||
/**
|
||||
* Creates the flyout's DOM. Only needs to be called once. The flyout can
|
||||
* either exist as its own <svg> element or be a <g> nested inside a separate
|
||||
* <svg> element.
|
||||
* either exist as its own svg element or be a g element nested inside a
|
||||
* separate svg element.
|
||||
* @param {string} tagName The type of tag to put the flyout in. This
|
||||
* should be <svg> or <g>.
|
||||
* @return {!Element} The flyout's SVG group.
|
||||
|
@ -340,7 +340,7 @@ Blockly.Flyout.prototype.init = function(targetWorkspace) {
|
|||
this.workspace_.targetWorkspace = targetWorkspace;
|
||||
// Add scrollbar.
|
||||
this.scrollbar_ = new Blockly.Scrollbar(this.workspace_,
|
||||
this.horizontalLayout_, false);
|
||||
this.horizontalLayout_, false, 'blocklyFlyoutScrollbar');
|
||||
|
||||
this.position();
|
||||
|
||||
|
@ -492,14 +492,17 @@ Blockly.Flyout.prototype.show = function(xmlList) {
|
|||
this.hide();
|
||||
this.clearOldBlocks_();
|
||||
|
||||
if (xmlList == Blockly.Variables.NAME_TYPE) {
|
||||
// Special category for variables.
|
||||
xmlList =
|
||||
Blockly.Variables.flyoutCategory(this.workspace_.targetWorkspace);
|
||||
} else if (xmlList == Blockly.Procedures.NAME_TYPE) {
|
||||
// Special category for procedures.
|
||||
xmlList =
|
||||
Blockly.Procedures.flyoutCategory(this.workspace_.targetWorkspace);
|
||||
// Handle dynamic categories, represented by a name instead of a list of XML.
|
||||
// Look up the correct category generation function and call that to get a
|
||||
// valid XML list.
|
||||
if (typeof xmlList == 'string') {
|
||||
var fnToApply = this.workspace_.targetWorkspace.getToolboxCategoryCallback(
|
||||
xmlList);
|
||||
goog.asserts.assert(goog.isFunction(fnToApply),
|
||||
'Couldn\'t find a callback function when opening a toolbox category.');
|
||||
xmlList = fnToApply(this.workspace_.targetWorkspace);
|
||||
goog.asserts.assert(goog.isArray(xmlList),
|
||||
'The result of a toolbox category callback must be an array.');
|
||||
}
|
||||
|
||||
this.setVisible(true);
|
||||
|
@ -560,6 +563,7 @@ Blockly.Flyout.prototype.show = function(xmlList) {
|
|||
this.listeners_.push(Blockly.bindEvent_(this.svgBackground_, 'mouseover',
|
||||
this, deselectAll));
|
||||
|
||||
this.workspace_.setResizesEnabled(true);
|
||||
this.reflow();
|
||||
|
||||
// Correctly position the flyout's scrollbar when it opens.
|
||||
|
|
|
@ -180,7 +180,7 @@ Blockly.HorizontalFlyout.prototype.position = function() {
|
|||
this.svgGroup_.setAttribute("width", this.width_);
|
||||
this.svgGroup_.setAttribute("height", this.height_);
|
||||
var transform = 'translate(' + x + 'px,' + y + 'px)';
|
||||
this.svgGroup_.style.transform = transform;
|
||||
Blockly.utils.setCssTransform(this.svgGroup_, transform);
|
||||
|
||||
// Update the scrollbar (if one exists).
|
||||
if (this.scrollbar_) {
|
||||
|
|
|
@ -31,6 +31,7 @@ goog.require('Blockly.Comment');
|
|||
goog.require('Blockly.Events');
|
||||
goog.require('Blockly.Flyout');
|
||||
goog.require('Blockly.FlyoutButton');
|
||||
goog.require('Blockly.utils');
|
||||
goog.require('Blockly.WorkspaceSvg');
|
||||
goog.require('goog.dom');
|
||||
goog.require('goog.events');
|
||||
|
@ -255,7 +256,7 @@ Blockly.VerticalFlyout.prototype.position = function() {
|
|||
this.svgGroup_.setAttribute("width", this.width_);
|
||||
this.svgGroup_.setAttribute("height", this.height_);
|
||||
var transform = 'translate(' + x + 'px,' + y + 'px)';
|
||||
this.svgGroup_.style.transform = transform;
|
||||
Blockly.utils.setCssTransform(this.svgGroup_, transform);
|
||||
|
||||
// Update the scrollbar (if one exists).
|
||||
if (this.scrollbar_) {
|
||||
|
@ -510,9 +511,9 @@ Blockly.VerticalFlyout.prototype.checkboxClicked_ = function(checkboxObj) {
|
|||
return function(e) {
|
||||
checkboxObj.clicked = !checkboxObj.clicked;
|
||||
if (checkboxObj.clicked) {
|
||||
Blockly.addClass_((checkboxObj.svgRoot), 'checked');
|
||||
Blockly.utils.addClass((checkboxObj.svgRoot), 'checked');
|
||||
} else {
|
||||
Blockly.removeClass_((checkboxObj.svgRoot), 'checked');
|
||||
Blockly.utils.removeClass((checkboxObj.svgRoot), 'checked');
|
||||
}
|
||||
// This event has been handled. No need to bubble up to the document.
|
||||
e.stopPropagation();
|
||||
|
|
|
@ -29,9 +29,9 @@ goog.provide('Blockly.inject');
|
|||
goog.require('Blockly.BlockDragSurfaceSvg');
|
||||
goog.require('Blockly.Css');
|
||||
goog.require('Blockly.constants');
|
||||
goog.require('Blockly.DropDownDiv');
|
||||
goog.require('Blockly.Options');
|
||||
goog.require('Blockly.WorkspaceSvg');
|
||||
goog.require('Blockly.DropDownDiv');
|
||||
goog.require('Blockly.WorkspaceDragSurfaceSvg');
|
||||
goog.require('goog.dom');
|
||||
goog.require('goog.ui.Component');
|
||||
|
@ -58,6 +58,7 @@ Blockly.inject = function(container, opt_options) {
|
|||
container.appendChild(subContainer);
|
||||
|
||||
var svg = Blockly.createDom_(subContainer, options);
|
||||
|
||||
// Create surfaces for dragging things. These are optimizations
|
||||
// so that the broowser does not repaint during the drag.
|
||||
var blockDragSurface = new Blockly.BlockDragSurfaceSvg(subContainer);
|
||||
|
@ -366,9 +367,10 @@ Blockly.init_ = function(mainWorkspace) {
|
|||
Blockly.inject.bindDocumentEvents_ = function() {
|
||||
if (!Blockly.documentEventsBound_) {
|
||||
Blockly.bindEventWithChecks_(document, 'keydown', null, Blockly.onKeyDown_);
|
||||
// longStop needs to run to stop the context menu from showing up. It
|
||||
// should run regardless of what other touch event handlers have run.
|
||||
Blockly.bindEvent_(document, 'touchend', null, Blockly.longStop_);
|
||||
Blockly.bindEvent_(document, 'touchcancel', null,
|
||||
Blockly.longStop_);
|
||||
Blockly.bindEvent_(document, 'touchcancel', null, Blockly.longStop_);
|
||||
// Don't use bindEvent_ for document's mouseup since that would create a
|
||||
// corresponding touch handler that would squelch the ability to interact
|
||||
// with non-Blockly elements.
|
||||
|
|
|
@ -70,13 +70,32 @@ Blockly.Input.prototype.align = Blockly.ALIGN_LEFT;
|
|||
Blockly.Input.prototype.visible_ = true;
|
||||
|
||||
/**
|
||||
* Add an item to the end of the input's field row.
|
||||
* Add a field (or label from string), and all prefix and suffix fields, to the
|
||||
* end of the input's field row.
|
||||
* @param {string|!Blockly.Field} field Something to add as a field.
|
||||
* @param {string=} opt_name Language-neutral identifier which may used to find
|
||||
* this field again. Should be unique to the host block.
|
||||
* @return {!Blockly.Input} The input being append to (to allow chaining).
|
||||
*/
|
||||
Blockly.Input.prototype.appendField = function(field, opt_name) {
|
||||
this.insertFieldAt(this.fieldRow.length, field, opt_name);
|
||||
return this;
|
||||
};
|
||||
|
||||
/**
|
||||
* Inserts a field (or label from string), and all prefix and suffix fields, at
|
||||
* the location of the input's field row.
|
||||
* @param {number} index The index at which to insert field.
|
||||
* @param {string|!Blockly.Field} field Something to add as a field.
|
||||
* @param {string=} opt_name Language-neutral identifier which may used to find
|
||||
* this field again. Should be unique to the host block.
|
||||
* @return {number} The index following the last inserted field.
|
||||
*/
|
||||
Blockly.Input.prototype.insertFieldAt = function(index, field, opt_name) {
|
||||
if (index < 0 || index > this.fieldRow.length) {
|
||||
throw new Error('index ' + index + ' out of bounds.');
|
||||
}
|
||||
|
||||
// Empty string, Null or undefined generates no field, unless field is named.
|
||||
if (!field && !opt_name) {
|
||||
return this;
|
||||
|
@ -93,13 +112,14 @@ Blockly.Input.prototype.appendField = function(field, opt_name) {
|
|||
|
||||
if (field.prefixField) {
|
||||
// Add any prefix.
|
||||
this.appendField(field.prefixField);
|
||||
index = this.insertFieldAt(index, field.prefixField);
|
||||
}
|
||||
// Add the field to the field row.
|
||||
this.fieldRow.push(field);
|
||||
this.fieldRow.splice(index, 0, field);
|
||||
++index;
|
||||
if (field.suffixField) {
|
||||
// Add any suffix.
|
||||
this.appendField(field.suffixField);
|
||||
index = this.insertFieldAt(index, field.suffixField);
|
||||
}
|
||||
|
||||
if (this.sourceBlock_.rendered) {
|
||||
|
@ -107,7 +127,7 @@ Blockly.Input.prototype.appendField = function(field, opt_name) {
|
|||
// Adding a field will cause the block to change shape.
|
||||
this.sourceBlock_.bumpNeighbours_();
|
||||
}
|
||||
return this;
|
||||
return index;
|
||||
};
|
||||
|
||||
/**
|
||||
|
|
|
@ -24,19 +24,19 @@
|
|||
*/
|
||||
'use strict';
|
||||
|
||||
/**
|
||||
* @name Blockly.Procedures
|
||||
* @namespace
|
||||
**/
|
||||
goog.provide('Blockly.Procedures');
|
||||
|
||||
goog.require('Blockly.Blocks');
|
||||
goog.require('Blockly.constants');
|
||||
goog.require('Blockly.Field');
|
||||
goog.require('Blockly.Names');
|
||||
goog.require('Blockly.Workspace');
|
||||
|
||||
|
||||
/**
|
||||
* Category to separate procedure names from variables and generated functions.
|
||||
*/
|
||||
Blockly.Procedures.NAME_TYPE = 'PROCEDURE';
|
||||
|
||||
/**
|
||||
* Find all user-created procedure definitions in a workspace.
|
||||
* @param {!Blockly.Workspace} root Root workspace.
|
||||
|
@ -132,7 +132,7 @@ Blockly.Procedures.isLegalName_ = function(name, workspace, opt_exclude) {
|
|||
* Rename a procedure. Called by the editable field.
|
||||
* @param {string} name The proposed new name.
|
||||
* @return {string} The accepted name.
|
||||
* @this Blockly.Field
|
||||
* @this {Blockly.Field}
|
||||
*/
|
||||
Blockly.Procedures.rename = function(name) {
|
||||
// Strip leading and trailing whitespace. Beyond this, all names are legal.
|
||||
|
|
|
@ -387,3 +387,17 @@ Blockly.RenderedConnection.prototype.connect_ = function(childConnection) {
|
|||
}
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Function to be called when this connection's compatible types have changed.
|
||||
* @private
|
||||
*/
|
||||
Blockly.RenderedConnection.prototype.onCheckChanged_ = function() {
|
||||
// The new value type may not be compatible with the existing connection.
|
||||
if (this.isConnected() && !this.checkType_(this.targetConnection)) {
|
||||
var child = this.isSuperior() ? this.targetBlock() : this.sourceBlock_;
|
||||
child.unplug();
|
||||
// Bump away.
|
||||
this.sourceBlock_.bumpNeighbours_();
|
||||
}
|
||||
};
|
||||
|
|
|
@ -38,8 +38,10 @@ goog.require('goog.events');
|
|||
*/
|
||||
Blockly.ScrollbarPair = function(workspace) {
|
||||
this.workspace_ = workspace;
|
||||
this.hScroll = new Blockly.Scrollbar(workspace, true, true);
|
||||
this.vScroll = new Blockly.Scrollbar(workspace, false, true);
|
||||
this.hScroll = new Blockly.Scrollbar(workspace, true, true,
|
||||
'blocklyMainWorkspaceScrollbar');
|
||||
this.vScroll = new Blockly.Scrollbar(workspace, false, true,
|
||||
'blocklyMainWorkspaceScrollbar');
|
||||
this.corner_ = Blockly.utils.createSvgElement('rect',
|
||||
{'height': Blockly.Scrollbar.scrollbarThickness,
|
||||
'width': Blockly.Scrollbar.scrollbarThickness,
|
||||
|
@ -182,15 +184,16 @@ Blockly.ScrollbarPair.prototype.getRatio_ = function(handlePosition, viewSize) {
|
|||
* @param {!Blockly.Workspace} workspace Workspace to bind the scrollbar to.
|
||||
* @param {boolean} horizontal True if horizontal, false if vertical.
|
||||
* @param {boolean=} opt_pair True if scrollbar is part of a horiz/vert pair.
|
||||
* @param {string} opt_class A class to be applied to this scrollbar.
|
||||
* @constructor
|
||||
*/
|
||||
Blockly.Scrollbar = function(workspace, horizontal, opt_pair) {
|
||||
Blockly.Scrollbar = function(workspace, horizontal, opt_pair, opt_class) {
|
||||
this.workspace_ = workspace;
|
||||
this.pair_ = opt_pair || false;
|
||||
this.horizontal_ = horizontal;
|
||||
this.oldHostMetrics_ = null;
|
||||
|
||||
this.createDom_();
|
||||
this.createDom_(opt_class);
|
||||
|
||||
/**
|
||||
* The upper left corner of the scrollbar's svg group.
|
||||
|
@ -369,9 +372,9 @@ Blockly.Scrollbar.prototype.setScrollViewSize_ = function(newSize) {
|
|||
};
|
||||
|
||||
/**
|
||||
* Set whether this scrollbar's container is visible.
|
||||
* @param {boolean} visible Whether the container is visible.
|
||||
*/
|
||||
* Set whether this scrollbar's container is visible.
|
||||
* @param {boolean} visible Whether the container is visible.
|
||||
*/
|
||||
Blockly.ScrollbarPair.prototype.setContainerVisible = function(visible) {
|
||||
this.hScroll.setContainerVisible(visible);
|
||||
this.vScroll.setContainerVisible(visible);
|
||||
|
@ -389,7 +392,7 @@ Blockly.Scrollbar.prototype.setPosition = function(x, y) {
|
|||
var tempX = this.position_.x + this.origin_.x;
|
||||
var tempY = this.position_.y + this.origin_.y;
|
||||
var transform = 'translate(' + tempX + 'px,' + tempY + 'px)';
|
||||
this.outerSvg_.style.transform = transform;
|
||||
Blockly.utils.setCssTransform(this.outerSvg_, transform);
|
||||
};
|
||||
|
||||
/**
|
||||
|
@ -577,11 +580,12 @@ Blockly.Scrollbar.prototype.resizeContentVertical = function(hostMetrics) {
|
|||
/**
|
||||
* Create all the DOM elements required for a scrollbar.
|
||||
* The resulting widget is not sized.
|
||||
* @param {string} opt_class A class to be applied to this scrollbar.
|
||||
* @private
|
||||
*/
|
||||
Blockly.Scrollbar.prototype.createDom_ = function() {
|
||||
Blockly.Scrollbar.prototype.createDom_ = function(opt_class) {
|
||||
/* Create the following DOM:
|
||||
<svg class="blocklyScrollbarHorizontal">
|
||||
<svg class="blocklyScrollbarHorizontal optionalClass">
|
||||
<g>
|
||||
<rect class="blocklyScrollbarBackground" />
|
||||
<rect class="blocklyScrollbarHandle" rx="8" ry="8" />
|
||||
|
@ -590,6 +594,9 @@ Blockly.Scrollbar.prototype.createDom_ = function() {
|
|||
*/
|
||||
var className = 'blocklyScrollbar' +
|
||||
(this.horizontal_ ? 'Horizontal' : 'Vertical');
|
||||
if (opt_class) {
|
||||
className += ' ' + opt_class;
|
||||
}
|
||||
this.outerSvg_ = Blockly.utils.createSvgElement('svg', {'class': className},
|
||||
null);
|
||||
this.svgGroup_ = Blockly.utils.createSvgElement('g', {}, this.outerSvg_);
|
||||
|
|
|
@ -29,6 +29,10 @@
|
|||
*/
|
||||
'use strict';
|
||||
|
||||
/**
|
||||
* @name Blockly.Tooltip
|
||||
* @namespace
|
||||
**/
|
||||
goog.provide('Blockly.Tooltip');
|
||||
|
||||
goog.require('goog.dom');
|
||||
|
|
|
@ -24,6 +24,10 @@
|
|||
*/
|
||||
'use strict';
|
||||
|
||||
/**
|
||||
* @name Blockly.Touch
|
||||
* @namespace
|
||||
**/
|
||||
goog.provide('Blockly.Touch');
|
||||
|
||||
goog.require('goog.events');
|
||||
|
@ -77,8 +81,15 @@ Blockly.longPid_ = 0;
|
|||
*/
|
||||
Blockly.longStart_ = function(e, uiObject) {
|
||||
Blockly.longStop_();
|
||||
// Punt on multitouch events.
|
||||
if (e.changedTouches.length != 1) {
|
||||
return;
|
||||
}
|
||||
Blockly.longPid_ = setTimeout(function() {
|
||||
e.button = 2; // Simulate a right button click.
|
||||
// e was a touch event. It needs to pretend to be a mouse event.
|
||||
e.clientX = e.changedTouches[0].clientX;
|
||||
e.clientY = e.changedTouches[0].clientY;
|
||||
uiObject.onMouseDown_(e);
|
||||
}, Blockly.LONGPRESS);
|
||||
};
|
||||
|
|
|
@ -26,6 +26,10 @@
|
|||
*/
|
||||
'use strict';
|
||||
|
||||
/**
|
||||
* @name Blockly.utils
|
||||
* @namespace
|
||||
**/
|
||||
goog.provide('Blockly.utils');
|
||||
|
||||
goog.require('Blockly.Touch');
|
||||
|
@ -206,7 +210,7 @@ Blockly.utils.getScale_ = function(element) {
|
|||
var transform = element.getAttribute('transform');
|
||||
if (transform) {
|
||||
var transformComponents =
|
||||
transform.match(Blockly.utils.getScale_.REGEXP_);
|
||||
transform.match(Blockly.utils.getScale_.REGEXP_);
|
||||
if (transformComponents && transformComponents[0]) {
|
||||
scale = parseFloat(transformComponents[0]);
|
||||
}
|
||||
|
@ -410,19 +414,50 @@ Blockly.utils.tokenizeInterpolation = function(message) {
|
|||
};
|
||||
|
||||
/**
|
||||
* Replaces string table references in a message string. For example,
|
||||
* %{bky_my_msg} and %{BKY_MY_MSG} will both be replaced with the value in
|
||||
* Blockly.Msg['MY_MSG'].
|
||||
* @param {string} message Text which might contain string table references.
|
||||
* Replaces string table references in a message, if the message is a string.
|
||||
* For example, "%{bky_my_msg}" and "%{BKY_MY_MSG}" will both be replaced with
|
||||
* the value in Blockly.Msg['MY_MSG'].
|
||||
* @param {string|?} message Message, which may be a string that contains
|
||||
* string table references.
|
||||
* @return {!string} String with message references replaced.
|
||||
*/
|
||||
Blockly.utils.replaceMessageReferences = function(message) {
|
||||
if (!goog.isString(message)) {
|
||||
return message;
|
||||
}
|
||||
var interpolatedResult = Blockly.utils.tokenizeInterpolation_(message, false);
|
||||
// When parseInterpolationTokens == false, interpolatedResult should be at
|
||||
// most length 1.
|
||||
return interpolatedResult.length ? interpolatedResult[0] : "";
|
||||
};
|
||||
|
||||
/**
|
||||
* Validates that any %{BKY_...} references in the message refer to keys of
|
||||
* the Blockly.Msg string table.
|
||||
* @param {string} message Text which might contain string table references.
|
||||
* @return {boolean} True if all message references have matching values.
|
||||
* Otherwise, false.
|
||||
*/
|
||||
Blockly.utils.checkMessageReferences = function(message) {
|
||||
var isValid = true; // True until a bad reference is found
|
||||
|
||||
var regex = /%{BKY_([a-zA-Z][a-zA-Z0-9_]*)}/g;
|
||||
var match = regex.exec(message);
|
||||
while (match != null) {
|
||||
var msgKey = match[1];
|
||||
if (Blockly.Msg[msgKey] == null) {
|
||||
console.log('WARNING: No message string for %{BKY_' + msgKey + '}.');
|
||||
isValid = false;
|
||||
}
|
||||
|
||||
// Re-run on remainder of sting.
|
||||
message = message.substring(match.index + msgKey.length + 1);
|
||||
match = regex.exec(message);
|
||||
}
|
||||
|
||||
return isValid;
|
||||
};
|
||||
|
||||
/**
|
||||
* Internal implemention of the message reference and interpolation token
|
||||
* parsing used by tokenizeInterpolation() and replaceMessageReferences().
|
||||
|
@ -505,7 +540,9 @@ Blockly.utils.tokenizeInterpolation_ = function(message, parseInterpolationToken
|
|||
keyUpper.substring(4) : null;
|
||||
if (bklyKey && bklyKey in Blockly.Msg) {
|
||||
var rawValue = Blockly.Msg[bklyKey];
|
||||
var subTokens = Blockly.utils.tokenizeInterpolation(rawValue);
|
||||
var subTokens = goog.isString(rawValue) ?
|
||||
Blockly.utils.tokenizeInterpolation(rawValue) :
|
||||
parseInterpolationTokens ? String(rawValue) : rawValue;
|
||||
tokens = tokens.concat(subTokens);
|
||||
} else {
|
||||
// No entry found in the string table. Pass reference as string.
|
||||
|
@ -840,3 +877,37 @@ Blockly.utils.insertAfter_ = function(newNode, refNode) {
|
|||
parentNode.appendChild(newNode);
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Calls a function after the page has loaded, possibly immediately.
|
||||
* @param {function()} fn Function to run.
|
||||
* @throws Error Will throw if no global document can be found (e.g., Node.js).
|
||||
*/
|
||||
Blockly.utils.runAfterPageLoad = function(fn) {
|
||||
if (!document) {
|
||||
throw new Error('Blockly.utils.runAfterPageLoad() requires browser document.');
|
||||
}
|
||||
if (document.readyState === 'complete') {
|
||||
fn(); // Page has already loaded. Call immediately.
|
||||
} else {
|
||||
// Poll readyState.
|
||||
var readyStateCheckInterval = setInterval(function() {
|
||||
if (document.readyState === 'complete') {
|
||||
clearInterval(readyStateCheckInterval);
|
||||
fn();
|
||||
}
|
||||
}, 10);
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Sets the CSS transform property on an element. This function sets the
|
||||
* non-vendor-prefixed and vendor-prefixed versions for backwards compatibility
|
||||
* with older browsers. See http://caniuse.com/#feat=transforms2d
|
||||
* @param {!Element} node The node which the CSS transform should be applied.
|
||||
* @param {string} transform The value of the CSS `transform` property.
|
||||
*/
|
||||
Blockly.utils.setCssTransform = function(node, transform) {
|
||||
node.style['transform'] = transform;
|
||||
node.style['-webkit-transform'] = transform;
|
||||
};
|
||||
|
|
|
@ -24,18 +24,18 @@
|
|||
*/
|
||||
'use strict';
|
||||
|
||||
/**
|
||||
* @name Blockly.Variables
|
||||
* @namespace
|
||||
**/
|
||||
goog.provide('Blockly.Variables');
|
||||
|
||||
goog.require('Blockly.Blocks');
|
||||
goog.require('Blockly.constants');
|
||||
goog.require('Blockly.Workspace');
|
||||
goog.require('goog.string');
|
||||
|
||||
|
||||
/**
|
||||
* Category to separate variable names from procedures and generated functions.
|
||||
*/
|
||||
Blockly.Variables.NAME_TYPE = 'VARIABLE';
|
||||
|
||||
/**
|
||||
* Find all user-created variables that are in use in the workspace.
|
||||
* For use by generators.
|
||||
|
@ -329,9 +329,9 @@ Blockly.Variables.generateUniqueName = function(workspace) {
|
|||
* Create a new variable on the given workspace.
|
||||
* @param {!Blockly.Workspace} workspace The workspace on which to create the
|
||||
* variable.
|
||||
* @param {function(?string)=} opt_callback A callback. It
|
||||
* will be passed a new variable name, or null if the change is to be
|
||||
* aborted (cancel button).
|
||||
* @param {function(?string=)=} opt_callback A callback. It will
|
||||
* be passed an acceptable new variable name, or null if change is to be
|
||||
* aborted (cancel button), or undefined if an existing variable was chosen.
|
||||
*/
|
||||
Blockly.Variables.createVariable = function(workspace, opt_callback) {
|
||||
var promptAndCheckWithAlert = function(defaultName) {
|
||||
|
|
|
@ -26,6 +26,10 @@
|
|||
*/
|
||||
'use strict';
|
||||
|
||||
/**
|
||||
* @name Blockly.WidgetDiv
|
||||
* @namespace
|
||||
**/
|
||||
goog.provide('Blockly.WidgetDiv');
|
||||
|
||||
goog.require('Blockly.Css');
|
||||
|
|
|
@ -484,6 +484,32 @@ Blockly.Workspace.prototype.getBlockById = function(id) {
|
|||
return block || null;
|
||||
};
|
||||
|
||||
/**
|
||||
* Checks whether all value and statement inputs in the workspace are filled
|
||||
* with blocks.
|
||||
* @param {boolean=} opt_shadowBlocksAreFilled An optional argument controlling
|
||||
* whether shadow blocks are counted as filled. Defaults to true.
|
||||
* @return {boolean} True if all inputs are filled, false otherwise.
|
||||
*/
|
||||
Blockly.Workspace.prototype.allInputsFilled = function(opt_shadowBlocksAreFilled) {
|
||||
var blocks = this.getTopBlocks(false);
|
||||
for (var i = 0, block; block = blocks[i]; i++) {
|
||||
if (!block.allInputsFilled(opt_shadowBlocksAreFilled)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
};
|
||||
|
||||
/**
|
||||
* Getter for the flyout associated with this workspace. This is null in a
|
||||
* non-rendered workspace, but may be overriden by subclasses.
|
||||
* @return {Blockly.Flyout} The flyout on this workspace.
|
||||
*/
|
||||
Blockly.Workspace.prototype.getFlyout = function() {
|
||||
return null;
|
||||
};
|
||||
|
||||
/**
|
||||
* Database of all workspaces.
|
||||
* @private
|
||||
|
|
|
@ -113,9 +113,9 @@ Blockly.workspaceDragSurfaceSvg.prototype.translateSurface = function(x, y) {
|
|||
x = x.toFixed(0);
|
||||
y = y.toFixed(0);
|
||||
|
||||
var transform =
|
||||
'transform: translate3d(' + x + 'px, ' + y + 'px, 0px); display: block;';
|
||||
this.SVG_.setAttribute('style', transform);
|
||||
this.SVG_.style.display = 'block';
|
||||
Blockly.utils.setCssTransform(this.SVG_,
|
||||
'translate3d(' + x + 'px, ' + y + 'px, 0px)');
|
||||
};
|
||||
|
||||
/**
|
||||
|
@ -158,7 +158,7 @@ Blockly.workspaceDragSurfaceSvg.prototype.clearAndHide = function(newSurface) {
|
|||
this.SVG_.style.display = 'none';
|
||||
goog.asserts.assert(this.SVG_.childNodes.length == 0,
|
||||
'Drag surface was not cleared.');
|
||||
this.SVG_.style.transform = '';
|
||||
Blockly.utils.setCssTransform(this.SVG_, '');
|
||||
this.previousSibling_ = null;
|
||||
};
|
||||
|
||||
|
|
|
@ -77,6 +77,17 @@ Blockly.WorkspaceSvg = function(options, opt_blockDragSurface, opt_wsDragSurface
|
|||
this.workspaceDragSurface_ = opt_wsDragSurface;
|
||||
}
|
||||
|
||||
this.useWorkspaceDragSurface_ =
|
||||
this.workspaceDragSurface_ && Blockly.utils.is3dSupported();
|
||||
|
||||
if (opt_blockDragSurface) {
|
||||
this.blockDragSurface_ = opt_blockDragSurface;
|
||||
}
|
||||
|
||||
if (opt_wsDragSurface) {
|
||||
this.workspaceDragSurface_ = opt_wsDragSurface;
|
||||
}
|
||||
|
||||
this.useWorkspaceDragSurface_ =
|
||||
this.workspaceDragSurface_ && Blockly.utils.is3dSupported();
|
||||
|
||||
|
@ -93,6 +104,11 @@ Blockly.WorkspaceSvg = function(options, opt_blockDragSurface, opt_wsDragSurface
|
|||
* @private
|
||||
*/
|
||||
this.highlightedBlocks_ = [];
|
||||
|
||||
this.registerToolboxCategoryCallback(Blockly.VARIABLE_CATEGORY_NAME,
|
||||
Blockly.Variables.flyoutCategory);
|
||||
this.registerToolboxCategoryCallback(Blockly.PROCEDURE_CATEGORY_NAME,
|
||||
Blockly.Procedures.flyoutCategory);
|
||||
};
|
||||
goog.inherits(Blockly.WorkspaceSvg, Blockly.Workspace);
|
||||
|
||||
|
@ -269,6 +285,14 @@ Blockly.WorkspaceSvg.prototype.lastRecordedPageScroll_ = null;
|
|||
*/
|
||||
Blockly.WorkspaceSvg.prototype.flyoutButtonCallbacks_ = {};
|
||||
|
||||
/**
|
||||
* Map from function names to callbacks, for deciding what to do when a custom
|
||||
* toolbox category is opened.
|
||||
* @type {!Object<string, function(!Blockly.Workspace):!Array<!Element>>}
|
||||
* @private
|
||||
*/
|
||||
Blockly.WorkspaceSvg.prototype.toolboxCategoryCallbacks_ = {};
|
||||
|
||||
/**
|
||||
* Inverted screen CTM, for use in mouseToSvg.
|
||||
* @type {SVGMatrix}
|
||||
|
@ -347,7 +371,6 @@ Blockly.WorkspaceSvg.prototype.createDom = function(opt_backgroundClass) {
|
|||
* [Trashcan and/or flyout may go here]
|
||||
* <g class="blocklyBlockCanvas"></g>
|
||||
* <g class="blocklyBubbleCanvas"></g>
|
||||
* [Scrollbars may go here]
|
||||
* </g>
|
||||
* @type {SVGElement}
|
||||
*/
|
||||
|
@ -439,6 +462,13 @@ Blockly.WorkspaceSvg.prototype.dispose = function() {
|
|||
this.zoomControls_.dispose();
|
||||
this.zoomControls_ = null;
|
||||
}
|
||||
|
||||
if (this.toolboxCategoryCallbacks_) {
|
||||
this.toolboxCategoryCallbacks_ = null;
|
||||
}
|
||||
if (this.flyoutButtonCallbacks_) {
|
||||
this.flyoutButtonCallbacks_ = null;
|
||||
}
|
||||
if (!this.options.parentWorkspace) {
|
||||
// Top-most workspace. Dispose of the div that the
|
||||
// svg is injected into (i.e. injectionDiv).
|
||||
|
@ -523,9 +553,8 @@ Blockly.WorkspaceSvg.prototype.addFlyout_ = function(tagName) {
|
|||
* owned by either the toolbox or the workspace, depending on toolbox
|
||||
* configuration. It will be null if there is no flyout.
|
||||
* @return {Blockly.Flyout} The flyout on this workspace.
|
||||
* @package
|
||||
*/
|
||||
Blockly.WorkspaceSvg.prototype.getFlyout_ = function() {
|
||||
Blockly.WorkspaceSvg.prototype.getFlyout = function() {
|
||||
if (this.flyout_) {
|
||||
return this.flyout_;
|
||||
}
|
||||
|
@ -641,20 +670,6 @@ Blockly.WorkspaceSvg.prototype.getParentSvg = function() {
|
|||
return null;
|
||||
};
|
||||
|
||||
/**
|
||||
* Get a flyout associated with this workspace, if one exists.
|
||||
* @return {?Blockly.Flyout} Flyout associated with this workspace.
|
||||
*/
|
||||
Blockly.WorkspaceSvg.prototype.getFlyout = function() {
|
||||
if (this.flyout_) {
|
||||
return this.flyout_;
|
||||
}
|
||||
if (this.toolbox_ && this.toolbox_.flyout_) {
|
||||
return this.toolbox_.flyout_;
|
||||
}
|
||||
return null;
|
||||
};
|
||||
|
||||
/**
|
||||
* Translate this workspace to new coordinates.
|
||||
* @param {number} x Horizontal translation.
|
||||
|
@ -747,8 +762,8 @@ Blockly.WorkspaceSvg.prototype.setVisible = function(isVisible) {
|
|||
|
||||
// Tell the flyout whether its container is visible so it can
|
||||
// tell when to hide itself.
|
||||
if (this.getFlyout_()) {
|
||||
this.getFlyout_().setContainerVisible(isVisible);
|
||||
if (this.getFlyout()) {
|
||||
this.getFlyout().setContainerVisible(isVisible);
|
||||
}
|
||||
|
||||
this.getParentSvg().style.display = isVisible ? 'block' : 'none';
|
||||
|
@ -1094,6 +1109,14 @@ Blockly.WorkspaceSvg.prototype.isDragging = function() {
|
|||
this.dragMode_ == Blockly.DRAG_FREE;
|
||||
};
|
||||
|
||||
/**
|
||||
* Is this workspace draggable and scrollable?
|
||||
* @return {boolean} True if this workspace may be dragged.
|
||||
*/
|
||||
Blockly.WorkspaceSvg.prototype.isDraggable = function() {
|
||||
return !!this.scrollbar;
|
||||
};
|
||||
|
||||
/**
|
||||
* Handle a mouse-wheel on SVG drawing surface.
|
||||
* @param {!Event} e Mouse wheel event.
|
||||
|
@ -1107,7 +1130,7 @@ Blockly.WorkspaceSvg.prototype.onMouseWheel_ = function(e) {
|
|||
var PIXELS_PER_ZOOM_STEP = 50;
|
||||
var delta = -e.deltaY / PIXELS_PER_ZOOM_STEP;
|
||||
var position = Blockly.utils.mouseToSvg(e, this.getParentSvg(),
|
||||
this.getInverseScreenCTM());
|
||||
this.getInverseScreenCTM());
|
||||
this.zoom(position.x, position.y, delta);
|
||||
} else {
|
||||
// This is a regular mouse wheel event - scroll the workspace
|
||||
|
@ -1824,6 +1847,8 @@ Blockly.WorkspaceSvg.prototype.clear = function() {
|
|||
* given button is clicked.
|
||||
*/
|
||||
Blockly.WorkspaceSvg.prototype.registerButtonCallback = function(key, func) {
|
||||
goog.asserts.assert(goog.isFunction(func),
|
||||
'Button callbacks must be functions.');
|
||||
this.flyoutButtonCallbacks_[key] = func;
|
||||
};
|
||||
|
||||
|
@ -1831,11 +1856,56 @@ Blockly.WorkspaceSvg.prototype.registerButtonCallback = function(key, func) {
|
|||
* Get the callback function associated with a given key, for clicks on buttons
|
||||
* and labels in the flyout.
|
||||
* @param {string} key The name to use to look up the function.
|
||||
* @return {function(!Blockly.FlyoutButton)} The function corresponding to the
|
||||
* given key for this workspace.
|
||||
* @return {?function(!Blockly.FlyoutButton)} The function corresponding to the
|
||||
* given key for this workspace; null if no callback is registered.
|
||||
*/
|
||||
Blockly.WorkspaceSvg.prototype.getButtonCallback = function(key) {
|
||||
return this.flyoutButtonCallbacks_[key];
|
||||
var result = this.flyoutButtonCallbacks_[key];
|
||||
return result ? result : null;
|
||||
};
|
||||
|
||||
/**
|
||||
* Remove a callback for a click on a button in the flyout.
|
||||
* @param {string} key The name associated with the callback function.
|
||||
*/
|
||||
Blockly.WorkspaceSvg.prototype.removeButtonCallback = function(key) {
|
||||
this.flyoutButtonCallbacks_[key] = null;
|
||||
};
|
||||
|
||||
/**
|
||||
* Register a callback function associated with a given key, for populating
|
||||
* custom toolbox categories in this workspace. See the variable and procedure
|
||||
* categories as an example.
|
||||
* @param {string} key The name to use to look up this function.
|
||||
* @param {function(!Blockly.Workspace):!Array<!Element>} func The function to
|
||||
* call when the given toolbox category is opened.
|
||||
*/
|
||||
Blockly.WorkspaceSvg.prototype.registerToolboxCategoryCallback = function(key,
|
||||
func) {
|
||||
goog.asserts.assert(goog.isFunction(func),
|
||||
'Toolbox category callbacks must be functions.');
|
||||
this.toolboxCategoryCallbacks_[key] = func;
|
||||
};
|
||||
|
||||
/**
|
||||
* Get the callback function associated with a given key, for populating
|
||||
* custom toolbox categories in this workspace.
|
||||
* @param {string} key The name to use to look up the function.
|
||||
* @return {?function(!Blockly.Workspace):!Array<!Element>} The function
|
||||
* corresponding to the given key for this workspace, or null if no function
|
||||
* is registered.
|
||||
*/
|
||||
Blockly.WorkspaceSvg.prototype.getToolboxCategoryCallback = function(key) {
|
||||
var result = this.toolboxCategoryCallbacks_[key];
|
||||
return result ? result : null;
|
||||
};
|
||||
|
||||
/**
|
||||
* Remove a callback for a click on a custom category's name in the toolbox.
|
||||
* @param {string} key The name associated with the callback function.
|
||||
*/
|
||||
Blockly.WorkspaceSvg.prototype.removeToolboxCategoryCallback = function(key) {
|
||||
this.toolboxCategoryCallbacks_[key] = null;
|
||||
};
|
||||
|
||||
// Export symbols that would otherwise be renamed by Closure compiler.
|
||||
|
|
20
core/xml.js
20
core/xml.js
|
@ -24,10 +24,15 @@
|
|||
*/
|
||||
'use strict';
|
||||
|
||||
/**
|
||||
* @name Blockly.Xml
|
||||
* @namespace
|
||||
**/
|
||||
goog.provide('Blockly.Xml');
|
||||
|
||||
goog.require('goog.asserts');
|
||||
goog.require('goog.dom');
|
||||
goog.require('goog.userAgent');
|
||||
|
||||
|
||||
/**
|
||||
|
@ -368,6 +373,21 @@ Blockly.Xml.domToBlock = function(xmlBlock, workspace) {
|
|||
setTimeout(function() {
|
||||
if (topBlock.workspace) { // Check that the block hasn't been deleted.
|
||||
topBlock.setConnectionsHidden(false);
|
||||
// Force a render on IE and Edge to get around the issue described in
|
||||
// Blockly.Field.getCachedWidth
|
||||
if (goog.userAgent.IE || goog.userAgent.EDGE) {
|
||||
topBlock.render();
|
||||
}
|
||||
}
|
||||
}, 1);
|
||||
} else {
|
||||
setTimeout(function() {
|
||||
if (topBlock.workspace) { // Check that the block hasn't been deleted.
|
||||
// Force a render on IE and Edge to get around the issue described in
|
||||
// Blockly.Field.getCachedWidth
|
||||
if (goog.userAgent.IE || goog.userAgent.EDGE) {
|
||||
topBlock.render();
|
||||
}
|
||||
}
|
||||
}, 1);
|
||||
}
|
||||
|
|
|
@ -35,6 +35,15 @@ def string_is_ascii(s):
|
|||
except UnicodeEncodeError:
|
||||
return False
|
||||
|
||||
def load_constants(filename):
|
||||
"""Read in constants file, which must be output in every language."""
|
||||
constant_defs = read_json_file(filename);
|
||||
constants_text = '\n'
|
||||
for key in constant_defs:
|
||||
value = constant_defs[key]
|
||||
value = value.replace('"', '\\"')
|
||||
constants_text += '\nBlockly.Msg.{0} = \"{1}\";'.format(key, value)
|
||||
return constants_text
|
||||
|
||||
def main():
|
||||
"""Generate .js files defining Blockly core and language messages."""
|
||||
|
@ -49,6 +58,9 @@ def main():
|
|||
parser.add_argument('--source_synonym_file',
|
||||
default=os.path.join('json', 'synonyms.json'),
|
||||
help='Path to .json file with synonym definitions')
|
||||
parser.add_argument('--source_constants_file',
|
||||
default=os.path.join('json', 'constants.json'),
|
||||
help='Path to .json file with constant definitions')
|
||||
parser.add_argument('--output_dir', default='js/',
|
||||
help='relative directory for output files')
|
||||
parser.add_argument('--key_file', default='keys.json',
|
||||
|
@ -78,11 +90,14 @@ def main():
|
|||
synonym_text = '\n'.join(['Blockly.Msg.{0} = Blockly.Msg.{1};'.format(
|
||||
key, synonym_defs[key]) for key in synonym_defs])
|
||||
|
||||
# Read in constants file, which must be output in every language.
|
||||
constants_text = load_constants(os.path.join(os.curdir, args.source_constants_file))
|
||||
|
||||
# Create each output file.
|
||||
for arg_file in args.files:
|
||||
(_, filename) = os.path.split(arg_file)
|
||||
target_lang = filename[:filename.index('.')]
|
||||
if target_lang not in ('qqq', 'keys', 'synonyms'):
|
||||
if target_lang not in ('qqq', 'keys', 'synonyms', 'constants'):
|
||||
target_defs = read_json_file(os.path.join(os.curdir, arg_file))
|
||||
|
||||
# Verify that keys are 'ascii'
|
||||
|
@ -140,6 +155,7 @@ goog.require('Blockly.Msg');
|
|||
filename, ', '.join(synonym_keys)))
|
||||
|
||||
outfile.write(synonym_text)
|
||||
outfile.write(constants_text)
|
||||
|
||||
if not args.quiet:
|
||||
print('Created {0}.'.format(outname))
|
||||
|
|
|
@ -53,6 +53,9 @@ _INPUT_DEF_PATTERN = re.compile("""Blockly.Msg.(\w*)\s*=\s*'([^']*)';?$""")
|
|||
_INPUT_SYN_PATTERN = re.compile(
|
||||
"""Blockly.Msg.(\w*)\s*=\s*Blockly.Msg.(\w*);""")
|
||||
|
||||
_CONSTANT_DESCRIPTION_PATTERN = re.compile(
|
||||
"""{{Notranslate}}""", re.IGNORECASE)
|
||||
|
||||
def main():
|
||||
# Set up argument parser.
|
||||
parser = argparse.ArgumentParser(description='Create translation files.')
|
||||
|
@ -75,6 +78,7 @@ def main():
|
|||
# Read and parse input file.
|
||||
results = []
|
||||
synonyms = {}
|
||||
constants = {} # Values that are constant across all languages.
|
||||
description = ''
|
||||
infile = codecs.open(args.input_file, 'r', 'utf-8')
|
||||
for line in infile:
|
||||
|
@ -86,14 +90,19 @@ def main():
|
|||
else:
|
||||
match = _INPUT_DEF_PATTERN.match(line)
|
||||
if match:
|
||||
result = {}
|
||||
result['meaning'] = match.group(1)
|
||||
result['source'] = match.group(2)
|
||||
key = match.group(1)
|
||||
value = match.group(2)
|
||||
if not description:
|
||||
print('Warning: No description for ' + result['meaning'])
|
||||
result['description'] = description
|
||||
if (description and _CONSTANT_DESCRIPTION_PATTERN.search(description)):
|
||||
constants[key] = value
|
||||
else:
|
||||
result = {}
|
||||
result['meaning'] = key
|
||||
result['source'] = value
|
||||
result['description'] = description
|
||||
results.append(result)
|
||||
description = ''
|
||||
results.append(result)
|
||||
else:
|
||||
match = _INPUT_SYN_PATTERN.match(line)
|
||||
if match:
|
||||
|
@ -115,6 +124,13 @@ def main():
|
|||
print("Wrote {0} synonym pairs to {1}.".format(
|
||||
len(synonyms), synonym_file_name))
|
||||
|
||||
# Create constants.json
|
||||
constants_file_name = os.path.join(os.curdir, args.output_dir, 'constants.json')
|
||||
with open(constants_file_name, 'w') as outfile:
|
||||
json.dump(constants, outfile)
|
||||
if not args.quiet:
|
||||
print("Wrote {0} constant pairs to {1}.".format(
|
||||
len(constants), synonym_file_name))
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
|
|
23
msg/js/en.js
23
msg/js/en.js
|
@ -128,6 +128,9 @@ Blockly.Msg.LISTS_LENGTH_TOOLTIP = "Returns the length of a list.";
|
|||
Blockly.Msg.LISTS_REPEAT_HELPURL = "https://github.com/google/blockly/wiki/Lists#create-list-with";
|
||||
Blockly.Msg.LISTS_REPEAT_TITLE = "create list with item %1 repeated %2 times";
|
||||
Blockly.Msg.LISTS_REPEAT_TOOLTIP = "Creates a list consisting of the given value repeated the specified number of times.";
|
||||
Blockly.Msg.LISTS_REVERSE_HELPURL = "https://github.com/google/blockly/wiki/Lists#reversing-a-list";
|
||||
Blockly.Msg.LISTS_REVERSE_MESSAGE0 = "reverse %1";
|
||||
Blockly.Msg.LISTS_REVERSE_TOOLTIP = "Reverse a copy of a list.";
|
||||
Blockly.Msg.LISTS_SET_INDEX_HELPURL = "https://github.com/google/blockly/wiki/Lists#in-list--set";
|
||||
Blockly.Msg.LISTS_SET_INDEX_INPUT_TO = "as";
|
||||
Blockly.Msg.LISTS_SET_INDEX_INSERT = "insert at";
|
||||
|
@ -315,6 +318,9 @@ Blockly.Msg.TEXT_CHARAT_LAST = "get last letter";
|
|||
Blockly.Msg.TEXT_CHARAT_RANDOM = "get random letter";
|
||||
Blockly.Msg.TEXT_CHARAT_TAIL = "";
|
||||
Blockly.Msg.TEXT_CHARAT_TOOLTIP = "Returns the letter at the specified position.";
|
||||
Blockly.Msg.TEXT_COUNT_HELPURL = "https://github.com/google/blockly/wiki/Text#counting-substrings";
|
||||
Blockly.Msg.TEXT_COUNT_MESSAGE0 = "count %1 in %2";
|
||||
Blockly.Msg.TEXT_COUNT_TOOLTIP = "Count how many times some text occurs within some other text.";
|
||||
Blockly.Msg.TEXT_CREATE_JOIN_ITEM_TOOLTIP = "Add an item to the text.";
|
||||
Blockly.Msg.TEXT_CREATE_JOIN_TITLE_JOIN = "join";
|
||||
Blockly.Msg.TEXT_CREATE_JOIN_TOOLTIP = "Add, remove, or reorder sections to reconfigure this text block.";
|
||||
|
@ -351,6 +357,12 @@ Blockly.Msg.TEXT_PROMPT_TOOLTIP_NUMBER = "Prompt for user for a number.";
|
|||
Blockly.Msg.TEXT_PROMPT_TOOLTIP_TEXT = "Prompt for user for some text.";
|
||||
Blockly.Msg.TEXT_PROMPT_TYPE_NUMBER = "prompt for number with message";
|
||||
Blockly.Msg.TEXT_PROMPT_TYPE_TEXT = "prompt for text with message";
|
||||
Blockly.Msg.TEXT_REPLACE_HELPURL = "https://github.com/google/blockly/wiki/Text#replacing-substrings";
|
||||
Blockly.Msg.TEXT_REPLACE_MESSAGE0 = "replace %1 with %2 in %3";
|
||||
Blockly.Msg.TEXT_REPLACE_TOOLTIP = "Replace all occurances of some text within some other text.";
|
||||
Blockly.Msg.TEXT_REVERSE_HELPURL = "https://github.com/google/blockly/wiki/Text#reversing-text";
|
||||
Blockly.Msg.TEXT_REVERSE_MESSAGE0 = "reverse %1";
|
||||
Blockly.Msg.TEXT_REVERSE_TOOLTIP = "Reverses the order of the characters in the text.";
|
||||
Blockly.Msg.TEXT_TEXT_HELPURL = "https://en.wikipedia.org/wiki/String_(computer_science)";
|
||||
Blockly.Msg.TEXT_TEXT_TOOLTIP = "A letter, word, or line of text.";
|
||||
Blockly.Msg.TEXT_TRIM_HELPURL = "https://github.com/google/blockly/wiki/Text#trimming-removing-spaces";
|
||||
|
@ -388,4 +400,13 @@ Blockly.Msg.LISTS_CREATE_WITH_ITEM_TITLE = Blockly.Msg.VARIABLES_DEFAULT_NAME;
|
|||
Blockly.Msg.TEXT_APPEND_VARIABLE = Blockly.Msg.VARIABLES_DEFAULT_NAME;
|
||||
Blockly.Msg.TEXT_CREATE_JOIN_ITEM_TITLE_ITEM = Blockly.Msg.VARIABLES_DEFAULT_NAME;
|
||||
Blockly.Msg.LISTS_INDEX_OF_INPUT_IN_LIST = Blockly.Msg.LISTS_INLIST;
|
||||
Blockly.Msg.PROCEDURES_DEFRETURN_COMMENT = Blockly.Msg.PROCEDURES_DEFNORETURN_COMMENT;
|
||||
Blockly.Msg.PROCEDURES_DEFRETURN_COMMENT = Blockly.Msg.PROCEDURES_DEFNORETURN_COMMENT;
|
||||
|
||||
Blockly.Msg.MATH_HUE = "230";
|
||||
Blockly.Msg.LOOPS_HUE = "120";
|
||||
Blockly.Msg.LISTS_HUE = "260";
|
||||
Blockly.Msg.LOGIC_HUE = "210";
|
||||
Blockly.Msg.VARIABLES_HUE = "330";
|
||||
Blockly.Msg.TEXTS_HUE = "160";
|
||||
Blockly.Msg.PROCEDURES_HUE = "290";
|
||||
Blockly.Msg.COLOUR_HUE = "20";
|
1
msg/json/constants.json
Normal file
1
msg/json/constants.json
Normal file
|
@ -0,0 +1 @@
|
|||
{"MATH_HUE": "230", "LOOPS_HUE": "120", "LISTS_HUE": "260", "LOGIC_HUE": "210", "VARIABLES_HUE": "330", "TEXTS_HUE": "160", "PROCEDURES_HUE": "290", "COLOUR_HUE": "20"}
|
|
@ -1,7 +1,7 @@
|
|||
{
|
||||
"@metadata": {
|
||||
"author": "Ellen Spertus <ellen.spertus@gmail.com>",
|
||||
"lastupdated": "2017-02-01 21:01:32.200551",
|
||||
"lastupdated": "2017-02-14 16:07:00.331892",
|
||||
"locale": "en",
|
||||
"messagedocumentation" : "qqq"
|
||||
},
|
||||
|
@ -253,6 +253,15 @@
|
|||
"TEXT_PROMPT_TYPE_NUMBER": "prompt for number with message",
|
||||
"TEXT_PROMPT_TOOLTIP_NUMBER": "Prompt for user for a number.",
|
||||
"TEXT_PROMPT_TOOLTIP_TEXT": "Prompt for user for some text.",
|
||||
"TEXT_COUNT_MESSAGE0": "count %1 in %2",
|
||||
"TEXT_COUNT_HELPURL": "https://github.com/google/blockly/wiki/Text#counting-substrings",
|
||||
"TEXT_COUNT_TOOLTIP": "Count how many times some text occurs within some other text.",
|
||||
"TEXT_REPLACE_MESSAGE0": "replace %1 with %2 in %3",
|
||||
"TEXT_REPLACE_HELPURL": "https://github.com/google/blockly/wiki/Text#replacing-substrings",
|
||||
"TEXT_REPLACE_TOOLTIP": "Replace all occurances of some text within some other text.",
|
||||
"TEXT_REVERSE_MESSAGE0": "reverse %1",
|
||||
"TEXT_REVERSE_HELPURL": "https://github.com/google/blockly/wiki/Text#reversing-text",
|
||||
"TEXT_REVERSE_TOOLTIP": "Reverses the order of the characters in the text.",
|
||||
"LISTS_CREATE_EMPTY_HELPURL": "https://github.com/google/blockly/wiki/Lists#create-empty-list",
|
||||
"LISTS_CREATE_EMPTY_TITLE": "create empty list",
|
||||
"LISTS_CREATE_EMPTY_TOOLTIP": "Returns a list, of length 0, containing no data records",
|
||||
|
@ -334,6 +343,9 @@
|
|||
"LISTS_SPLIT_WITH_DELIMITER": "with delimiter",
|
||||
"LISTS_SPLIT_TOOLTIP_SPLIT": "Split text into a list of texts, breaking at each delimiter.",
|
||||
"LISTS_SPLIT_TOOLTIP_JOIN": "Join a list of texts into one text, separated by a delimiter.",
|
||||
"LISTS_REVERSE_HELPURL": "https://github.com/google/blockly/wiki/Lists#reversing-a-list",
|
||||
"LISTS_REVERSE_MESSAGE0": "reverse %1",
|
||||
"LISTS_REVERSE_TOOLTIP": "Reverse a copy of a list.",
|
||||
"ORDINAL_NUMBER_SUFFIX": "",
|
||||
"VARIABLES_GET_HELPURL": "https://github.com/google/blockly/wiki/Variables#get",
|
||||
"VARIABLES_GET_TOOLTIP": "Returns the value of this variable.",
|
||||
|
|
|
@ -48,6 +48,23 @@ goog.require('Blockly.Msg');
|
|||
* them to msg/json/qqq.json, and they show up in the translation console.
|
||||
*/
|
||||
|
||||
/// {{Notranslate}} Hue value for all logic blocks.
|
||||
Blockly.Msg.LOGIC_HUE = '210';
|
||||
/// {{Notranslate}} Hue value for all loop blocks.
|
||||
Blockly.Msg.LOOPS_HUE = '120';
|
||||
/// {{Notranslate}} Hue value for all math blocks.
|
||||
Blockly.Msg.MATH_HUE = '230';
|
||||
/// {{Notranslate}} Hue value for all text blocks.
|
||||
Blockly.Msg.TEXTS_HUE = '160';
|
||||
/// {{Notranslate}} Hue value for all list blocks.
|
||||
Blockly.Msg.LISTS_HUE = '260';
|
||||
/// {{Notranslate}} Hue value for all colour blocks.
|
||||
Blockly.Msg.COLOUR_HUE = '20';
|
||||
/// {{Notranslate}} Hue value for all variable blocks.
|
||||
Blockly.Msg.VARIABLES_HUE = '330';
|
||||
/// {{Notranslate}} Hue value for all procedure blocks.
|
||||
Blockly.Msg.PROCEDURES_HUE = '290';
|
||||
|
||||
/// default name - A simple, general default name for a variable, preferably short.
|
||||
/// For more context, see
|
||||
/// [[Translating:Blockly#infrequent_message_types]].\n{{Identical|Item}}
|
||||
|
@ -222,7 +239,7 @@ Blockly.Msg.CONTROLS_IF_TOOLTIP_3 = 'If the first value is true, then do the fir
|
|||
/// tooltip - Describes [https://github.com/google/blockly/wiki/IfElse#if-else-if-else-blocks if-else-if-else blocks]. Consider using your language's translation of [https://en.wikipedia.org/wiki/If_statement https://en.wikipedia.org/wiki/If_statement], if present.
|
||||
Blockly.Msg.CONTROLS_IF_TOOLTIP_4 = 'If the first value is true, then do the first block of statements. Otherwise, if the second value is true, do the second block of statements. If none of the values are true, do the last block of statements.';
|
||||
/// block text - See [https://github.com/google/blockly/wiki/IfElse https://github.com/google/blockly/wiki/IfElse].
|
||||
/// It is recommended, but not essential, that this have text in common with the translation of 'else if'
|
||||
/// It is recommended, but not essential, that this have text in common with the translation of 'else if'\n{{Identical|If}}
|
||||
Blockly.Msg.CONTROLS_IF_MSG_IF = 'if';
|
||||
/// block text - See [https://github.com/google/blockly/wiki/IfElse https://github.com/google/blockly/wiki/IfElse]. The English words "otherwise if" would probably be clearer than "else if", but the latter is used because it is traditional and shorter.
|
||||
Blockly.Msg.CONTROLS_IF_MSG_ELSEIF = 'else if';
|
||||
|
@ -749,6 +766,30 @@ Blockly.Msg.TEXT_PROMPT_TOOLTIP_NUMBER = 'Prompt for user for a number.';
|
|||
/// https://github.com/google/blockly/wiki/Text#printing-text].
|
||||
Blockly.Msg.TEXT_PROMPT_TOOLTIP_TEXT = 'Prompt for user for some text.';
|
||||
|
||||
/// block text - Title of a block that counts the number of instances of
|
||||
/// a smaller pattern (%1) inside a longer string (%2).
|
||||
Blockly.Msg.TEXT_COUNT_MESSAGE0 = 'count %1 in %2';
|
||||
/// url - Information about counting how many times a string appears in another string.
|
||||
Blockly.Msg.TEXT_COUNT_HELPURL = 'https://github.com/google/blockly/wiki/Text#counting-substrings';
|
||||
/// tooltip - Short description of a block that counts how many times some text occurs within some other text.
|
||||
Blockly.Msg.TEXT_COUNT_TOOLTIP = 'Count how many times some text occurs within some other text.';
|
||||
|
||||
/// block text - Title of a block that returns a copy of text (%3) with all
|
||||
/// instances of some smaller text (%1) replaced with other text (%2).
|
||||
Blockly.Msg.TEXT_REPLACE_MESSAGE0 = 'replace %1 with %2 in %3';
|
||||
/// url - Information about replacing each copy text (or string, in computer lingo) with other text.
|
||||
Blockly.Msg.TEXT_REPLACE_HELPURL = 'https://github.com/google/blockly/wiki/Text#replacing-substrings';
|
||||
/// tooltip - Short description of a block that replaces copies of text in a large text with other text.
|
||||
Blockly.Msg.TEXT_REPLACE_TOOLTIP = 'Replace all occurances of some text within some other text.';
|
||||
|
||||
/// block text - Title of block that returns a copy of text (%1) with the order
|
||||
/// of letters and characters reversed.
|
||||
Blockly.Msg.TEXT_REVERSE_MESSAGE0 = 'reverse %1';
|
||||
/// url - Information about reversing a letters/characters in text.
|
||||
Blockly.Msg.TEXT_REVERSE_HELPURL = 'https://github.com/google/blockly/wiki/Text#reversing-text';
|
||||
/// tooltip - See [https://github.com/google/blockly/wiki/Text].
|
||||
Blockly.Msg.TEXT_REVERSE_TOOLTIP = 'Reverses the order of the characters in the text.';
|
||||
|
||||
// Lists Blocks.
|
||||
/// url - Information on empty lists.
|
||||
Blockly.Msg.LISTS_CREATE_EMPTY_HELPURL = 'https://github.com/google/blockly/wiki/Lists#create-empty-list';
|
||||
|
@ -966,7 +1007,7 @@ Blockly.Msg.LISTS_GET_SUBLIST_TAIL = '';
|
|||
/// [[File:Blockly-get-sublist.png]]
|
||||
Blockly.Msg.LISTS_GET_SUBLIST_TOOLTIP = 'Creates a copy of the specified portion of a list.';
|
||||
|
||||
/// url - Information describing sorting a list.
|
||||
/// {{optional}}\nurl - Information describing sorting a list.
|
||||
Blockly.Msg.LISTS_SORT_HELPURL = 'https://github.com/google/blockly/wiki/Lists#sorting-a-list';
|
||||
/// Sort as type %1 (numeric or alphabetic) in order %2 (ascending or descending) a list of items %3.\n{{Identical|Sort}}
|
||||
Blockly.Msg.LISTS_SORT_TITLE = 'sort %1 %2 %3';
|
||||
|
@ -998,6 +1039,13 @@ Blockly.Msg.LISTS_SPLIT_TOOLTIP_SPLIT = 'Split text into a list of texts, breaki
|
|||
/// https://github.com/google/blockly/wiki/Lists#make-text-from-list] for more information.
|
||||
Blockly.Msg.LISTS_SPLIT_TOOLTIP_JOIN = 'Join a list of texts into one text, separated by a delimiter.';
|
||||
|
||||
/// url - Information describing reversing a list.
|
||||
Blockly.Msg.LISTS_REVERSE_HELPURL = 'https://github.com/google/blockly/wiki/Lists#reversing-a-list';
|
||||
/// block text - Title of block that returns a copy of a list (%1) with the order of items reversed.
|
||||
Blockly.Msg.LISTS_REVERSE_MESSAGE0 = 'reverse %1';
|
||||
/// tooltip - Short description for a block that reverses a copy of a list.
|
||||
Blockly.Msg.LISTS_REVERSE_TOOLTIP = 'Reverse a copy of a list.';
|
||||
|
||||
/// grammar - Text that follows an ordinal number (a number that indicates
|
||||
/// position relative to other numbers). In most languages, such text appears
|
||||
/// before the number, so this should be blank. An exception is Hungarian.
|
||||
|
@ -1107,7 +1155,7 @@ Blockly.Msg.PROCEDURES_CREATE_DO = 'Create "%1"';
|
|||
/// tooltip - If the first value is true, this causes the second value to be returned
|
||||
/// immediately from the enclosing function.
|
||||
Blockly.Msg.PROCEDURES_IFRETURN_TOOLTIP = 'If a value is true, then return a second value.';
|
||||
/// url - Information about guard clauses.
|
||||
/// {{optional}}\nurl - Information about guard clauses.
|
||||
Blockly.Msg.PROCEDURES_IFRETURN_HELPURL = 'http://c2.com/cgi/wiki?GuardClause';
|
||||
/// warning - This appears if the user tries to use this block outside of a function definition.
|
||||
Blockly.Msg.PROCEDURES_IFRETURN_WARNING = 'Warning: This block may be used only within a function definition.';
|
||||
|
|
21
tests/blocks/index.html
Normal file
21
tests/blocks/index.html
Normal file
|
@ -0,0 +1,21 @@
|
|||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<meta charset="utf-8">
|
||||
<title>Unit Tests for Blockly Blocks</title>
|
||||
<script src="../../blockly_uncompressed.js"></script>
|
||||
<script src="../../msg/js/en.js"></script> <!-- TODO: Test messages in all languages. -->
|
||||
<script src="../../blocks/colour.js"></script>
|
||||
<script src="../../blocks/lists.js"></script>
|
||||
<script src="../../blocks/logic.js"></script>
|
||||
<script src="../../blocks/loops.js"></script>
|
||||
<script src="../../blocks/math.js"></script>
|
||||
<script src="../../blocks/procedures.js"></script>
|
||||
<script src="../../blocks/text.js"></script>
|
||||
<script src="../../blocks/variables.js"></script>
|
||||
<script>goog.require('goog.testing.jsunit');</script>
|
||||
</head>
|
||||
<body>
|
||||
<script src="logic_ternary_test.js"></script>
|
||||
</body>
|
||||
</html>
|
316
tests/blocks/logic_ternary_test.js
Normal file
316
tests/blocks/logic_ternary_test.js
Normal file
|
@ -0,0 +1,316 @@
|
|||
/**
|
||||
* @license
|
||||
* Blockly Tests
|
||||
*
|
||||
* Copyright 2017 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.
|
||||
*/
|
||||
'use strict';
|
||||
|
||||
function test_logic_ternary_structure() {
|
||||
var workspace = new Blockly.Workspace();
|
||||
try {
|
||||
var block = workspace.newBlock('logic_ternary');
|
||||
assertEquals(3, block.inputList && block.inputList.length);
|
||||
assertEquals(1, block.getInput('IF').connection.check_.length);
|
||||
assertEquals('Boolean', block.getInput('IF').connection.check_[0]);
|
||||
assertTrue(!!block.onchangeWrapper_); // Has onchange handler
|
||||
} finally {
|
||||
workspace.dispose();
|
||||
}
|
||||
}
|
||||
|
||||
function test_logic_ternary_attachSameTypeCheckInThenAndElseWithoutParent() {
|
||||
var workspace = new Blockly.Workspace();
|
||||
try {
|
||||
var block = workspace.newBlock('logic_ternary');
|
||||
|
||||
var string1 = workspace.newBlock('text');
|
||||
var string2 = workspace.newBlock('text_charAt');
|
||||
|
||||
block.getInput('THEN').connection.connect(string1.outputConnection);
|
||||
Blockly.Events.fireNow_(); // Force synchronous onchange() call.
|
||||
assertEquals(block, string1.getRootBlock());
|
||||
block.getInput('ELSE').connection.connect(string2.outputConnection);
|
||||
Blockly.Events.fireNow_(); // Force synchronous onchange() call.
|
||||
assertEquals(block, string1.getRootBlock()); // Still connected.
|
||||
assertEquals(block, string2.getRootBlock());
|
||||
} finally {
|
||||
workspace.dispose();
|
||||
}
|
||||
}
|
||||
|
||||
function test_logic_ternary_attachDifferectTypeChecksInThenAndElseWithoutParent() {
|
||||
var workspace = new Blockly.Workspace();
|
||||
try {
|
||||
var block = workspace.newBlock('logic_ternary');
|
||||
|
||||
var string = workspace.newBlock('text');
|
||||
var number = workspace.newBlock('math_number');
|
||||
|
||||
block.getInput('THEN').connection.connect(string.outputConnection);
|
||||
Blockly.Events.fireNow_(); // Force synchronous onchange() call.
|
||||
assertEquals(block, string.getRootBlock());
|
||||
block.getInput('ELSE').connection.connect(number.outputConnection);
|
||||
Blockly.Events.fireNow_(); // Force synchronous onchange() call.
|
||||
assertEquals(block, string.getRootBlock()); // Input THEN still connected.
|
||||
assertEquals(block, number.getRootBlock());
|
||||
} finally {
|
||||
workspace.dispose();
|
||||
}
|
||||
}
|
||||
|
||||
function test_logic_ternary_attachSameTypeCheckInThenAndElseWithMatchingParent() {
|
||||
var workspace = new Blockly.Workspace();
|
||||
try {
|
||||
var block = workspace.newBlock('logic_ternary');
|
||||
var parent = workspace.newBlock('text_trim');
|
||||
|
||||
parent.getInput('TEXT').connection.connect(block.outputConnection);
|
||||
assertEquals(parent, block.getRootBlock());
|
||||
|
||||
var string1 = workspace.newBlock('text');
|
||||
var string2 = workspace.newBlock('text_charAt');
|
||||
|
||||
block.getInput('THEN').connection.connect(string1.outputConnection);
|
||||
Blockly.Events.fireNow_(); // Force synchronous onchange() call.
|
||||
assertEquals(parent, block.getRootBlock()); // Still connected to parent.
|
||||
assertEquals(parent, string1.getRootBlock());
|
||||
block.getInput('ELSE').connection.connect(string2.outputConnection);
|
||||
Blockly.Events.fireNow_(); // Force synchronous onchange() call.
|
||||
assertEquals(parent, block.getRootBlock()); // Still connected to parent.
|
||||
assertEquals(parent, string1.getRootBlock()); // Input THEN still connected.
|
||||
assertEquals(parent, string2.getRootBlock());
|
||||
} finally {
|
||||
workspace.dispose();
|
||||
}
|
||||
}
|
||||
|
||||
function test_logic_ternary_attachDifferectTypeChecksInThenAndElseWithUncheckedParent() {
|
||||
var workspace = new Blockly.Workspace();
|
||||
try {
|
||||
var block = workspace.newBlock('logic_ternary');
|
||||
var parent = workspace.newBlock('text_print');
|
||||
|
||||
parent.getInput('TEXT').connection.connect(block.outputConnection);
|
||||
assertEquals(parent, block.parentBlock_);
|
||||
|
||||
var string = workspace.newBlock('text');
|
||||
var number = workspace.newBlock('math_number');
|
||||
|
||||
block.getInput('THEN').connection.connect(string.outputConnection);
|
||||
Blockly.Events.fireNow_(); // Force synchronous onchange() call.
|
||||
assertEquals(parent, block.getRootBlock()); // Still connected to parent.
|
||||
assertEquals(parent, string.getRootBlock());
|
||||
block.getInput('ELSE').connection.connect(number.outputConnection);
|
||||
Blockly.Events.fireNow_(); // Force synchronous onchange() call.
|
||||
assertEquals(parent, block.getRootBlock()); // Still connected to parent.
|
||||
assertEquals(parent, string.getRootBlock()); // Input THEN still connected.
|
||||
assertEquals(parent, number.getRootBlock());
|
||||
} finally {
|
||||
workspace.dispose();
|
||||
}
|
||||
}
|
||||
|
||||
function test_logic_ternary_attachDifferectTypeChecksInThenAndElseWithPermissiveParent() {
|
||||
var workspace = new Blockly.Workspace();
|
||||
try {
|
||||
var block = workspace.newBlock('logic_ternary');
|
||||
var parent = workspace.newBlock('text_length'); // Allows String or Array
|
||||
|
||||
parent.getInput('VALUE').connection.connect(block.outputConnection);
|
||||
assertEquals(parent, block.parentBlock_);
|
||||
|
||||
var string = workspace.newBlock('text');
|
||||
var array = workspace.newBlock('lists_create_empty');
|
||||
|
||||
block.getInput('THEN').connection.connect(string.outputConnection);
|
||||
Blockly.Events.fireNow_(); // Force synchronous onchange() call.
|
||||
assertEquals(parent, block.getRootBlock()); // Still connected to parent.
|
||||
assertEquals(parent, string.getRootBlock());
|
||||
block.getInput('ELSE').connection.connect(array.outputConnection);
|
||||
Blockly.Events.fireNow_(); // Force synchronous onchange() call.
|
||||
assertEquals(parent, block.getRootBlock()); // Still connected to parent.
|
||||
assertEquals(parent, string.getRootBlock()); // Input THEN still connected.
|
||||
assertEquals(parent, array.getRootBlock());
|
||||
} finally {
|
||||
workspace.dispose();
|
||||
}
|
||||
}
|
||||
|
||||
function test_logic_ternary_attachMismatchTypeToThen_breakWithParent() {
|
||||
var workspace = new Blockly.Workspace();
|
||||
try {
|
||||
var block = workspace.newBlock('logic_ternary');
|
||||
var parent = workspace.newBlock('text_length'); // Allows String or Array
|
||||
|
||||
parent.getInput('VALUE').connection.connect(block.outputConnection);
|
||||
Blockly.Events.fireNow_(); // Force synchronous onchange() call.
|
||||
assertEquals(parent, block.parentBlock_);
|
||||
|
||||
var string = workspace.newBlock('text');
|
||||
var number = workspace.newBlock('math_number');
|
||||
|
||||
block.getInput('ELSE').connection.connect(string.outputConnection);
|
||||
Blockly.Events.fireNow_(); // Force synchronous onchange() call.
|
||||
assertEquals(parent, block.getRootBlock()); // Still connected to parent.
|
||||
assertEquals(parent, string.getRootBlock());
|
||||
|
||||
// Adding mismatching number.
|
||||
block.getInput('THEN').connection.connect(number.outputConnection);
|
||||
Blockly.Events.fireNow_(); // Force synchronous onchange() call.
|
||||
assertEquals(block, block.getRootBlock()); // Disconnected from parent.
|
||||
assertEquals(block, number.getRootBlock());
|
||||
assertEquals(block, string.getRootBlock()); // ELSE string still connected.
|
||||
} finally {
|
||||
workspace.dispose();
|
||||
}
|
||||
}
|
||||
|
||||
function test_logic_ternary_attachMismatchTypeToElse_breakWithParent() {
|
||||
var workspace = new Blockly.Workspace();
|
||||
try {
|
||||
var block = workspace.newBlock('logic_ternary');
|
||||
var parent = workspace.newBlock('text_length'); // Allows String or Array
|
||||
|
||||
parent.getInput('VALUE').connection.connect(block.outputConnection);
|
||||
Blockly.Events.fireNow_(); // Force synchronous onchange() call.
|
||||
assertEquals(parent, block.parentBlock_);
|
||||
|
||||
var string = workspace.newBlock('text');
|
||||
var number = workspace.newBlock('math_number');
|
||||
|
||||
block.getInput('THEN').connection.connect(string.outputConnection);
|
||||
Blockly.Events.fireNow_(); // Force synchronous onchange() call.
|
||||
assertEquals(parent, block.getRootBlock()); // Still connected to parent.
|
||||
assertEquals(parent, string.getRootBlock());
|
||||
|
||||
// Adding mismatching number.
|
||||
block.getInput('ELSE').connection.connect(number.outputConnection);
|
||||
Blockly.Events.fireNow_(); // Force synchronous onchange() call.
|
||||
assertEquals(block, block.getRootBlock()); // Disconnected from parent.
|
||||
assertEquals(block, number.getRootBlock());
|
||||
assertEquals(block, string.getRootBlock()); // THEN string still connected.
|
||||
} finally {
|
||||
workspace.dispose();
|
||||
}
|
||||
}
|
||||
|
||||
function test_logic_ternary_attachToUncheckedParentWithDifferentTypes() {
|
||||
var workspace = new Blockly.Workspace();
|
||||
try {
|
||||
var block = workspace.newBlock('logic_ternary');
|
||||
var string = workspace.newBlock('text');
|
||||
var number = workspace.newBlock('math_number');
|
||||
|
||||
block.getInput('THEN').connection.connect(string.outputConnection);
|
||||
Blockly.Events.fireNow_(); // Force synchronous onchange() call.
|
||||
assertEquals(block, string.getRootBlock());
|
||||
block.getInput('ELSE').connection.connect(number.outputConnection);
|
||||
Blockly.Events.fireNow_(); // Force synchronous onchange() call.
|
||||
assertEquals(block, string.getRootBlock()); // Input THEN still connected.
|
||||
assertEquals(block, number.getRootBlock());
|
||||
|
||||
// Attaching to parent.
|
||||
var parent = workspace.newBlock('text_print');
|
||||
parent.getInput('TEXT').connection.connect(block.outputConnection);
|
||||
assertEquals(parent, block.getRootBlock());
|
||||
assertEquals(parent, string.getRootBlock()); // Input THEN still connected.
|
||||
assertEquals(parent, number.getRootBlock()); // Input ELSE still connected.
|
||||
} finally {
|
||||
workspace.dispose();
|
||||
}
|
||||
}
|
||||
|
||||
function test_logic_ternary_attachToPermissiveParentWithDifferentTypes() {
|
||||
var workspace = new Blockly.Workspace();
|
||||
try {
|
||||
var block = workspace.newBlock('logic_ternary');
|
||||
var string = workspace.newBlock('text');
|
||||
var array = workspace.newBlock('lists_create_empty');
|
||||
|
||||
block.getInput('THEN').connection.connect(string.outputConnection);
|
||||
Blockly.Events.fireNow_(); // Force synchronous onchange() call.
|
||||
assertEquals(block, string.getRootBlock());
|
||||
block.getInput('ELSE').connection.connect(array.outputConnection);
|
||||
Blockly.Events.fireNow_(); // Force synchronous onchange() call.
|
||||
assertEquals(block, string.getRootBlock()); // Input THEN still connected.
|
||||
assertEquals(block, array.getRootBlock());
|
||||
|
||||
// Attaching to parent.
|
||||
var parent = workspace.newBlock('text_print');
|
||||
parent.getInput('TEXT').connection.connect(block.outputConnection);
|
||||
Blockly.Events.fireNow_(); // Force synchronous onchange() call.
|
||||
assertEquals(parent, block.getRootBlock());
|
||||
assertEquals(parent, string.getRootBlock()); // Input THEN still connected.
|
||||
assertEquals(parent, array.getRootBlock()); // Input ELSE still connected.
|
||||
} finally {
|
||||
workspace.dispose();
|
||||
}
|
||||
}
|
||||
|
||||
function test_logic_ternary_attachToParentWithMismatchingThen_disconnectThen() {
|
||||
var workspace = new Blockly.Workspace();
|
||||
try {
|
||||
var block = workspace.newBlock('logic_ternary');
|
||||
var number = workspace.newBlock('math_number');
|
||||
var string = workspace.newBlock('text');
|
||||
|
||||
block.getInput('THEN').connection.connect(number.outputConnection);
|
||||
Blockly.Events.fireNow_(); // Force synchronous onchange() call.
|
||||
assertEquals(block, number.getRootBlock());
|
||||
block.getInput('ELSE').connection.connect(string.outputConnection);
|
||||
Blockly.Events.fireNow_(); // Force synchronous onchange() call.
|
||||
assertEquals(block, number.getRootBlock()); // Input THEN still connected.
|
||||
assertEquals(block, string.getRootBlock());
|
||||
|
||||
// Attaching to parent.
|
||||
var parent = workspace.newBlock('text_trim');
|
||||
parent.getInput('TEXT').connection.connect(block.outputConnection);
|
||||
Blockly.Events.fireNow_(); // Force synchronous onchange() call.
|
||||
assertEquals(parent, block.getRootBlock()); // Successful connection to parent.
|
||||
assertEquals(parent, string.getRootBlock()); // Input ELSE still connected.
|
||||
assertEquals(number, number.getRootBlock()); // Input THEN disconnected.
|
||||
} finally {
|
||||
workspace.dispose();
|
||||
}
|
||||
}
|
||||
|
||||
function test_logic_ternary_attachToParentWithMismatchingElse_disconnectElse() {
|
||||
var workspace = new Blockly.Workspace();
|
||||
try {
|
||||
var block = workspace.newBlock('logic_ternary');
|
||||
var string = workspace.newBlock('text');
|
||||
var number = workspace.newBlock('math_number');
|
||||
|
||||
block.getInput('THEN').connection.connect(string.outputConnection);
|
||||
Blockly.Events.fireNow_(); // Force synchronous onchange() call.
|
||||
assertEquals(block, string.getRootBlock());
|
||||
block.getInput('ELSE').connection.connect(number.outputConnection);
|
||||
Blockly.Events.fireNow_(); // Force synchronous onchange() call.
|
||||
assertEquals(block, string.getRootBlock()); // Input THEN still connected.
|
||||
assertEquals(block, number.getRootBlock());
|
||||
|
||||
// Attaching to parent.
|
||||
var parent = workspace.newBlock('text_trim');
|
||||
parent.getInput('TEXT').connection.connect(block.outputConnection);
|
||||
Blockly.Events.fireNow_(); // Force synchronous onchange() call.
|
||||
assertEquals(parent, block.getRootBlock()); // Successful connection to parent.
|
||||
assertEquals(parent, string.getRootBlock()); // Input THEN still connected.
|
||||
assertEquals(number, number.getRootBlock()); // Input ELSE disconnected.
|
||||
} finally {
|
||||
workspace.dispose();
|
||||
}
|
||||
}
|
307
tests/jsunit/extensions_test.js
Normal file
307
tests/jsunit/extensions_test.js
Normal file
|
@ -0,0 +1,307 @@
|
|||
/**
|
||||
* @license
|
||||
* Visual Blocks Editor
|
||||
*
|
||||
* Copyright 2017 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 Tests for Blockly.Extensions
|
||||
* @author Anm@anm.me (Andrew n marshall)
|
||||
*/
|
||||
'use strict';
|
||||
|
||||
function test_extension() {
|
||||
var workspace = new Blockly.Workspace();
|
||||
var block;
|
||||
try {
|
||||
assertUndefined(Blockly.Extensions.ALL_['extensions_test']);
|
||||
|
||||
var numCallsToBefore = 0;
|
||||
var numCallsToAfter = 0;
|
||||
|
||||
// Extension defined before the block type is defined.
|
||||
Blockly.Extensions.register('extensions_test_before', function () {
|
||||
numCallsToBefore++;
|
||||
this.extendedWithBefore = true;
|
||||
});
|
||||
|
||||
Blockly.defineBlocksWithJsonArray([{
|
||||
"type": "extension_test_block",
|
||||
"message0": "extension_test_block",
|
||||
"extensions": ["extensions_test_before", "extensions_test_after"]
|
||||
}]);
|
||||
|
||||
// Extension defined after the block type (but before instantiation).
|
||||
Blockly.Extensions.register('extensions_test_after', function () {
|
||||
numCallsToAfter++;
|
||||
this.extendedWithAfter = true;
|
||||
});
|
||||
|
||||
assert(goog.isFunction(Blockly.Extensions.ALL_['extensions_test_before']));
|
||||
assert(goog.isFunction(Blockly.Extensions.ALL_['extensions_test_after']));
|
||||
assertEquals(0, numCallsToBefore);
|
||||
assertEquals(0, numCallsToAfter);
|
||||
|
||||
block = new Blockly.Block(workspace, 'extension_test_block');
|
||||
|
||||
assertEquals(1, numCallsToBefore);
|
||||
assertEquals(1, numCallsToAfter);
|
||||
assert(block.extendedWithBefore);
|
||||
assert(block.extendedWithAfter);
|
||||
} finally {
|
||||
block && block.dispose();
|
||||
workspace.dispose();
|
||||
|
||||
delete Blockly.Extensions.ALL_['extensions_test_before'];
|
||||
delete Blockly.Extensions.ALL_['extensions_test_after'];
|
||||
delete Blockly.Blocks['extension_test_block'];
|
||||
}
|
||||
}
|
||||
|
||||
function test_extension_missing() {
|
||||
var workspace = new Blockly.Workspace();
|
||||
var block;
|
||||
var exceptionWasThrown = false;
|
||||
try {
|
||||
assertUndefined(Blockly.Extensions.ALL_['missing_extension']);
|
||||
Blockly.defineBlocksWithJsonArray([{
|
||||
"type": "missing_extension_block",
|
||||
"message0": "missing_extension_block",
|
||||
"extensions": ["missing_extension"]
|
||||
}]);
|
||||
|
||||
block = new Blockly.Block(workspace, 'missing_extension_block');
|
||||
} catch (e) {
|
||||
// Expected.
|
||||
exceptionWasThrown = true;
|
||||
} finally {
|
||||
block && block.dispose();
|
||||
workspace.dispose();
|
||||
delete Blockly.Blocks['missing_extension_block'];
|
||||
}
|
||||
assert(exceptionWasThrown);
|
||||
}
|
||||
|
||||
function test_extension_not_a_function() {
|
||||
var exceptionWasThrown = false;
|
||||
try {
|
||||
assertUndefined(Blockly.Extensions.ALL_['extension_just_a_string']);
|
||||
Blockly.Extensions.register('extension_just_a_string', 'extension_just_a_string');
|
||||
} catch (e) {
|
||||
// Expected.
|
||||
exceptionWasThrown = true;
|
||||
} finally {
|
||||
delete Blockly.Extensions.ALL_['extension_just_a_string'];
|
||||
}
|
||||
assert(exceptionWasThrown);
|
||||
|
||||
var exceptionWasThrown = false;
|
||||
try {
|
||||
assertUndefined(Blockly.Extensions.ALL_['extension_is_null']);
|
||||
Blockly.Extensions.register('extension_is_null', null);
|
||||
} catch (e) {
|
||||
// Expected.
|
||||
exceptionWasThrown = true;
|
||||
} finally {
|
||||
delete Blockly.Extensions.ALL_['extension_is_null'];
|
||||
}
|
||||
assert(exceptionWasThrown);
|
||||
|
||||
var exceptionWasThrown = false;
|
||||
try {
|
||||
assertUndefined(Blockly.Extensions.ALL_['extension_is_undefined']);
|
||||
Blockly.Extensions.register('extension_is_undefined');
|
||||
} catch (e) {
|
||||
// Expected.
|
||||
exceptionWasThrown = true;
|
||||
} finally {
|
||||
delete Blockly.Extensions.ALL_['extension_is_undefined'];
|
||||
}
|
||||
assert(exceptionWasThrown);
|
||||
}
|
||||
|
||||
function test_parent_tooltip_when_inline() {
|
||||
var defaultTooltip = "defaultTooltip";
|
||||
var parentTooltip = "parentTooltip";
|
||||
|
||||
var workspace = new Blockly.Workspace();
|
||||
var block;
|
||||
try {
|
||||
Blockly.defineBlocksWithJsonArray([
|
||||
{
|
||||
"type": "test_parent_tooltip_when_inline",
|
||||
"message0": "test_parent_tooltip_when_inline",
|
||||
"output": true,
|
||||
"tooltip": defaultTooltip,
|
||||
"extensions": ["parent_tooltip_when_inline"]
|
||||
},
|
||||
{
|
||||
"type": "test_parent",
|
||||
"message0": "%1",
|
||||
"args0": [
|
||||
{
|
||||
"type": "input_value",
|
||||
"name": "INPUT"
|
||||
}
|
||||
],
|
||||
"tooltip": parentTooltip,
|
||||
"inputsInline": false
|
||||
}
|
||||
]);
|
||||
|
||||
block = new Blockly.Block(workspace, 'test_parent_tooltip_when_inline');
|
||||
|
||||
// Tooltip is dynamic after extension initialization.
|
||||
assert(goog.isFunction(block.tooltip));
|
||||
assertEquals(block.tooltip(), defaultTooltip);
|
||||
|
||||
// Tooltip is normal before connected to parent.
|
||||
var parent = new Blockly.Block(workspace, 'test_parent');
|
||||
assertEquals(parent.tooltip, parentTooltip);
|
||||
assertFalse(!!parent.inputsInline);
|
||||
|
||||
// Tooltip is normal when parent is not inline.
|
||||
parent.getInput('INPUT').connection.connect(block.outputConnection);
|
||||
assertEquals(block.getParent(), parent);
|
||||
assertEquals(block.tooltip(), defaultTooltip);
|
||||
|
||||
// Tooltip is parent's when parent is inline.
|
||||
parent.setInputsInline(true);
|
||||
assertEquals(block.tooltip(), parentTooltip);
|
||||
|
||||
// Tooltip revert when disconnected.
|
||||
parent.getInput('INPUT').connection.disconnect();
|
||||
assert(!block.getParent());
|
||||
assertEquals(block.tooltip(), defaultTooltip);
|
||||
} finally {
|
||||
block && block.dispose();
|
||||
workspace.dispose();
|
||||
|
||||
delete Blockly.Blocks['test_parent_tooltip_when_inline'];
|
||||
delete Blockly.Blocks['test_parent'];
|
||||
}
|
||||
}
|
||||
|
||||
function test_mixin_extension() {
|
||||
var TEST_MIXIN = {
|
||||
field: 'FIELD',
|
||||
method: function() {
|
||||
console.log('TEXT_MIXIN method()');
|
||||
}
|
||||
};
|
||||
|
||||
var workspace = new Blockly.Workspace();
|
||||
var block;
|
||||
try {
|
||||
assertUndefined(Blockly.Extensions.ALL_['mixin_test']);
|
||||
|
||||
// Extension defined before the block type is defined.
|
||||
Blockly.Extensions.registerMixin('mixin_test', TEST_MIXIN);
|
||||
assert(goog.isFunction(Blockly.Extensions.ALL_['mixin_test']));
|
||||
|
||||
Blockly.defineBlocksWithJsonArray([{
|
||||
"type": "test_block_mixin",
|
||||
"message0": "test_block_mixin",
|
||||
"extensions": ["mixin_test"]
|
||||
}]);
|
||||
|
||||
block = new Blockly.Block(workspace, 'test_block_mixin');
|
||||
|
||||
assertEquals(TEST_MIXIN.field, block.field);
|
||||
assertEquals(TEST_MIXIN.method, block.method);
|
||||
} finally {
|
||||
block && block.dispose();
|
||||
workspace.dispose();
|
||||
|
||||
delete Blockly.Extensions.ALL_['mixin_test'];
|
||||
delete Blockly.Blocks['test_block_mixin'];
|
||||
}
|
||||
}
|
||||
|
||||
function test_bad_mixin_overwrites_local_value() {
|
||||
var TEST_MIXIN_BAD_INPUTLIST = {
|
||||
inputList: 'bad inputList' // Defined in constructor
|
||||
};
|
||||
|
||||
var workspace = new Blockly.Workspace();
|
||||
var block;
|
||||
try {
|
||||
assertUndefined(Blockly.Extensions.ALL_['mixin_bad_inputList']);
|
||||
|
||||
// Extension defined before the block type is defined.
|
||||
Blockly.Extensions.registerMixin('mixin_bad_inputList', TEST_MIXIN_BAD_INPUTLIST);
|
||||
assert(goog.isFunction(Blockly.Extensions.ALL_['mixin_bad_inputList']));
|
||||
|
||||
Blockly.defineBlocksWithJsonArray([{
|
||||
"type": "test_block_bad_inputList",
|
||||
"message0": "test_block_bad_inputList",
|
||||
"extensions": ["mixin_bad_inputList"]
|
||||
}]);
|
||||
|
||||
try {
|
||||
block = new Blockly.Block(workspace, 'test_block_bad_inputList');
|
||||
} catch (e) {
|
||||
// Expected Error
|
||||
assert(e.message.indexOf('inputList') >= 0); // Reference the conflict
|
||||
return;
|
||||
}
|
||||
fail('Expected error when constructing block');
|
||||
} finally {
|
||||
block && block.dispose();
|
||||
workspace.dispose();
|
||||
|
||||
delete Blockly.Extensions.ALL_['mixin_bad_inputList'];
|
||||
delete Blockly.Blocks['test_block_bad_inputList'];
|
||||
}
|
||||
}
|
||||
|
||||
function test_bad_mixin_overwrites_prototype() {
|
||||
var TEST_MIXIN_BAD_COLOUR = {
|
||||
colour_: 'bad colour_' // Defined on prototype
|
||||
};
|
||||
|
||||
var workspace = new Blockly.Workspace();
|
||||
var block;
|
||||
try {
|
||||
assertUndefined(Blockly.Extensions.ALL_['mixin_bad_colour_']);
|
||||
|
||||
// Extension defined before the block type is defined.
|
||||
Blockly.Extensions.registerMixin('mixin_bad_colour_', TEST_MIXIN_BAD_COLOUR);
|
||||
assert(goog.isFunction(Blockly.Extensions.ALL_['mixin_bad_colour_']));
|
||||
|
||||
Blockly.defineBlocksWithJsonArray([{
|
||||
"type": "test_block_bad_colour",
|
||||
"message0": "test_block_bad_colour",
|
||||
"extensions": ["mixin_bad_colour_"]
|
||||
}]);
|
||||
|
||||
try {
|
||||
block = new Blockly.Block(workspace, 'test_block_bad_colour');
|
||||
} catch (e) {
|
||||
// Expected Error
|
||||
assert(e.message.indexOf('colour_') >= 0); // Reference the conflict
|
||||
return;
|
||||
}
|
||||
fail('Expected error when constructing block');
|
||||
} finally {
|
||||
block && block.dispose();
|
||||
workspace.dispose();
|
||||
|
||||
delete Blockly.Extensions.ALL_['mixin_bad_colour_'];
|
||||
delete Blockly.Blocks['test_block_bad_colour'];
|
||||
}
|
||||
}
|
39
tests/jsunit/field_angle_test.js
Normal file
39
tests/jsunit/field_angle_test.js
Normal file
|
@ -0,0 +1,39 @@
|
|||
/**
|
||||
* @license
|
||||
* Visual Blocks Editor
|
||||
*
|
||||
* Copyright 2017 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 Tests for Blockly.FieldAngle
|
||||
* @author Anm@anm.me (Andrew n marshall)
|
||||
*/
|
||||
'use strict';
|
||||
|
||||
function test_fieldangle_constructor() {
|
||||
assertEquals(new Blockly.FieldAngle().getValue(), '0');
|
||||
assertEquals(new Blockly.FieldAngle(null).getValue(), '0');
|
||||
assertEquals(new Blockly.FieldAngle(undefined).getValue(), '0');
|
||||
assertEquals(new Blockly.FieldAngle(1).getValue(), '1');
|
||||
assertEquals(new Blockly.FieldAngle(1.5).getValue(), '1.5');
|
||||
assertEquals(new Blockly.FieldAngle('2').getValue(), '2');
|
||||
assertEquals(new Blockly.FieldAngle('2.5').getValue(), '2.5');
|
||||
|
||||
// Bad values
|
||||
assertEquals(new Blockly.FieldAngle('bad').getValue(), '0');
|
||||
assertEquals(new Blockly.FieldAngle(NaN).getValue(), '0');
|
||||
}
|
67
tests/jsunit/field_number_test.js
Normal file
67
tests/jsunit/field_number_test.js
Normal file
|
@ -0,0 +1,67 @@
|
|||
/**
|
||||
* @license
|
||||
* Visual Blocks Editor
|
||||
*
|
||||
* Copyright 2017 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 Tests for Blockly.FieldNumber
|
||||
* @author Anm@anm.me (Andrew n marshall)
|
||||
*/
|
||||
'use strict';
|
||||
|
||||
function test_fieldnumber_constructor() {
|
||||
// No arguments
|
||||
var field = new Blockly.FieldNumber();
|
||||
assertEquals(field.getValue(), '0');
|
||||
|
||||
// Unlike blockly, scratch-blocks doesn't store min, max, and precision.
|
||||
// TODO: Update this to check the restrictor, based on min, max, and precision.
|
||||
assertEquals(field.min_, undefined);
|
||||
assertEquals(field.max_, undefined);
|
||||
assertEquals(field.precision_, undefined);
|
||||
|
||||
// Numeric values
|
||||
field = new Blockly.FieldNumber(1);
|
||||
assertEquals(field.getValue(), '1');
|
||||
field = new Blockly.FieldNumber(1.5);
|
||||
assertEquals(field.getValue(), '1.5');
|
||||
|
||||
// String value
|
||||
field = new Blockly.FieldNumber('2');
|
||||
assertEquals(field.getValue(), '2');
|
||||
field = new Blockly.FieldNumber('2.5');
|
||||
assertEquals(field.getValue(), '2.5');
|
||||
|
||||
// All values
|
||||
field = new Blockly.FieldNumber(
|
||||
/* value */ 0,
|
||||
/* min */ -128,
|
||||
/* max */ 127,
|
||||
/* precision */ 1);
|
||||
// Unlike blockly, scratch-blocks doesn't store min, max, and precision.
|
||||
assertEquals(field.getValue(), '0');
|
||||
assertEquals(field.min_, undefined);
|
||||
assertEquals(field.max_, undefined);
|
||||
assertEquals(field.precision_, undefined);
|
||||
|
||||
// Bad value defaults to '0'
|
||||
field = new Blockly.FieldNumber('bad');
|
||||
assertEquals(field.getValue(), '0');
|
||||
field = new Blockly.FieldNumber(NaN);
|
||||
assertEquals(field.getValue(), '0');
|
||||
}
|
99
tests/jsunit/field_test.js
Normal file
99
tests/jsunit/field_test.js
Normal file
|
@ -0,0 +1,99 @@
|
|||
/**
|
||||
* @license
|
||||
* Visual Blocks Editor
|
||||
*
|
||||
* Copyright 2017 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 Tests for Blockly.Field
|
||||
* @author fenichel@google.com (Rachel Fenichel)
|
||||
*/
|
||||
'use strict';
|
||||
|
||||
function test_field_isEditable_simple() {
|
||||
var field = new Blockly.Field("Dummy text");
|
||||
// EDITABLE is true by default, but without a source block a field can't be
|
||||
// edited.
|
||||
assertFalse('Field without a block is not editable',
|
||||
field.isCurrentlyEditable());
|
||||
}
|
||||
|
||||
function test_field_isEditable_false() {
|
||||
// Setting EDITABLE to false doesn't matter.
|
||||
var field = new Blockly.Field("Dummy text");
|
||||
field.EDITABLE = false;
|
||||
assertFalse('Field without a block is not editable',
|
||||
field.isCurrentlyEditable());
|
||||
}
|
||||
|
||||
function test_field_isEditable_editableBlock() {
|
||||
var editableBlock = {
|
||||
isEditable: function() {
|
||||
return true;
|
||||
}
|
||||
};
|
||||
|
||||
var field = new Blockly.Field("Dummy text");
|
||||
field.sourceBlock_ = editableBlock;
|
||||
|
||||
assertTrue('Editable field with editable block is editable',
|
||||
field.isCurrentlyEditable());
|
||||
}
|
||||
|
||||
function test_field_isEditable_editableBlock_false() {
|
||||
var editableBlock = {
|
||||
isEditable: function() {
|
||||
return true;
|
||||
}
|
||||
};
|
||||
|
||||
var field = new Blockly.Field("Dummy text");
|
||||
field.sourceBlock_ = editableBlock;
|
||||
field.EDITABLE = false;
|
||||
|
||||
assertFalse('Non-editable field with editable block is not editable',
|
||||
field.isCurrentlyEditable());
|
||||
}
|
||||
|
||||
function test_field_isEditable_nonEditableBlock() {
|
||||
var nonEditableBlock = {
|
||||
isEditable: function() {
|
||||
return false;
|
||||
}
|
||||
};
|
||||
|
||||
var field = new Blockly.Field("Dummy text");
|
||||
field.sourceBlock_ = nonEditableBlock;
|
||||
|
||||
assertFalse('Editable field with non-editable block is not editable',
|
||||
field.isCurrentlyEditable());
|
||||
}
|
||||
|
||||
function test_field_isEditable_nonEditableBlock_false() {
|
||||
var nonEditableBlock = {
|
||||
isEditable: function() {
|
||||
return false;
|
||||
}
|
||||
};
|
||||
|
||||
var field = new Blockly.Field("Dummy text");
|
||||
field.sourceBlock_ = nonEditableBlock;
|
||||
field.EDITABLE = false;
|
||||
|
||||
assertFalse('Non-editable field with non-editable block is not editable',
|
||||
field.isCurrentlyEditable());
|
||||
}
|
|
@ -7,15 +7,22 @@
|
|||
<script>goog.require('goog.testing.jsunit');</script>
|
||||
</head>
|
||||
<body>
|
||||
<script src="utils_test.js"></script>
|
||||
<script src="blockly_test.js"></script>
|
||||
<script src="block_test.js"></script>
|
||||
<script src="connection_test.js"></script>
|
||||
<script src="connection_db_test.js"></script>
|
||||
<script src="extensions_test.js"></script>
|
||||
<script src="field_test.js"></script>
|
||||
<script src="field_angle_test.js"></script>
|
||||
<script src="field_number_test.js"></script>
|
||||
<script src="generator_test.js"></script>
|
||||
<script src="input_test.js"></script>
|
||||
<script src="names_test.js"></script>
|
||||
<script src="workspace_test.js"></script>
|
||||
<script src="xml_test.js"></script>
|
||||
<script src="svg_test.js"></script>
|
||||
<script src="json_test.js"></script>
|
||||
|
||||
<div id="blocklyDiv" style="display: none; height: 480px; width: 600px;"></div>
|
||||
<xml id="toolbox" style="display: none"></xml>
|
||||
|
|
202
tests/jsunit/input_test.js
Normal file
202
tests/jsunit/input_test.js
Normal file
|
@ -0,0 +1,202 @@
|
|||
/**
|
||||
* @license
|
||||
* Visual Blocks Editor
|
||||
*
|
||||
* Copyright 2017 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 Tests for Blockly.Input
|
||||
*/
|
||||
'use strict';
|
||||
|
||||
function test_appendField_simple() {
|
||||
var ws = new Blockly.Workspace();
|
||||
var block = new Blockly.Block(ws);
|
||||
var input = new Blockly.Input(Blockly.DUMMY_INPUT, 'INPUT', block);
|
||||
var field1 = new Blockly.FieldLabel('#1');
|
||||
var field2 = new Blockly.FieldLabel('#2');
|
||||
|
||||
// Preconditions
|
||||
assertEquals(0, input.fieldRow.length);
|
||||
|
||||
// Actual Tests
|
||||
input.appendField(field1, 'first');
|
||||
assertEquals(1, input.fieldRow.length);
|
||||
assertEquals(field1, input.fieldRow[0]);
|
||||
assertEquals('first', input.fieldRow[0].name);
|
||||
assertEquals(block, field1.sourceBlock_);
|
||||
|
||||
input.appendField(field2, 'second');
|
||||
assertEquals(2, input.fieldRow.length);
|
||||
assertEquals(field2, input.fieldRow[1]);
|
||||
assertEquals('second', input.fieldRow[1].name);
|
||||
assertEquals(block, field2.sourceBlock_);
|
||||
}
|
||||
|
||||
function test_appendField_string() {
|
||||
var ws = new Blockly.Workspace();
|
||||
var block = new Blockly.Block(ws);
|
||||
var input = new Blockly.Input(Blockly.DUMMY_INPUT, 'INPUT', block);
|
||||
var labelText = 'label';
|
||||
|
||||
// Preconditions
|
||||
assertEquals(0, input.fieldRow.length);
|
||||
|
||||
// Actual Tests
|
||||
input.appendField(labelText, 'name');
|
||||
assertEquals(1, input.fieldRow.length);
|
||||
assertEquals(Blockly.FieldLabel, input.fieldRow[0].constructor);
|
||||
assertEquals(labelText, input.fieldRow[0].getValue());
|
||||
assertEquals('name', input.fieldRow[0].name);
|
||||
}
|
||||
|
||||
function test_appendField_prefix() {
|
||||
var ws = new Blockly.Workspace();
|
||||
var block = new Blockly.Block(ws);
|
||||
var input = new Blockly.Input(Blockly.DUMMY_INPUT, 'INPUT', block);
|
||||
var prefix = new Blockly.FieldLabel('prefix');
|
||||
var field = new Blockly.FieldLabel('field');
|
||||
field.prefixField = prefix;
|
||||
|
||||
// Preconditions
|
||||
assertEquals(0, input.fieldRow.length);
|
||||
|
||||
// Actual Tests
|
||||
input.appendField(field);
|
||||
assertEquals(2, input.fieldRow.length);
|
||||
assertEquals(prefix, input.fieldRow[0]);
|
||||
assertEquals(field, input.fieldRow[1]);
|
||||
}
|
||||
|
||||
function test_appendField_suffix() {
|
||||
var ws = new Blockly.Workspace();
|
||||
var block = new Blockly.Block(ws);
|
||||
var input = new Blockly.Input(Blockly.DUMMY_INPUT, 'INPUT', block);
|
||||
var suffix = new Blockly.FieldLabel('suffix');
|
||||
var field = new Blockly.FieldLabel('field');
|
||||
field.suffixField = suffix;
|
||||
|
||||
// Preconditions
|
||||
assertEquals(0, input.fieldRow.length);
|
||||
|
||||
// Actual Tests
|
||||
input.appendField(field);
|
||||
assertEquals(2, input.fieldRow.length);
|
||||
assertEquals(field, input.fieldRow[0]);
|
||||
assertEquals(suffix, input.fieldRow[1]);
|
||||
}
|
||||
|
||||
function test_insertFieldAt_simple() {
|
||||
var ws = new Blockly.Workspace();
|
||||
var block = new Blockly.Block(ws);
|
||||
var input = new Blockly.Input(Blockly.DUMMY_INPUT, 'INPUT', block);
|
||||
var before = new Blockly.FieldLabel('before');
|
||||
var after = new Blockly.FieldLabel('after');
|
||||
var between = new Blockly.FieldLabel('between');
|
||||
input.appendField(before);
|
||||
input.appendField(after);
|
||||
|
||||
// Preconditions
|
||||
assertEquals(2, input.fieldRow.length);
|
||||
assertEquals(before, input.fieldRow[0]);
|
||||
assertEquals(after, input.fieldRow[1]);
|
||||
|
||||
// Actual Tests
|
||||
input.insertFieldAt(1, between, 'name');
|
||||
assertEquals(3, input.fieldRow.length);
|
||||
assertEquals(before, input.fieldRow[0]);
|
||||
assertEquals(between, input.fieldRow[1]);
|
||||
assertEquals('name', input.fieldRow[1].name);
|
||||
assertEquals(after, input.fieldRow[2]);
|
||||
}
|
||||
|
||||
function test_insertFieldAt_string() {
|
||||
var ws = new Blockly.Workspace();
|
||||
var block = new Blockly.Block(ws);
|
||||
var input = new Blockly.Input(Blockly.DUMMY_INPUT, 'INPUT', block);
|
||||
var before = new Blockly.FieldLabel('before');
|
||||
var after = new Blockly.FieldLabel('after');
|
||||
var labelText = 'label';
|
||||
input.appendField(before);
|
||||
input.appendField(after);
|
||||
|
||||
// Preconditions
|
||||
assertEquals(2, input.fieldRow.length);
|
||||
assertEquals(before, input.fieldRow[0]);
|
||||
assertEquals(after, input.fieldRow[1]);
|
||||
|
||||
// Actual Tests
|
||||
input.insertFieldAt(1, labelText, 'name');
|
||||
assertEquals(3, input.fieldRow.length);
|
||||
assertEquals(before, input.fieldRow[0]);
|
||||
assertEquals(Blockly.FieldLabel, input.fieldRow[1].constructor);
|
||||
assertEquals(labelText, input.fieldRow[1].getValue());
|
||||
assertEquals('name', input.fieldRow[1].name);
|
||||
assertEquals(after, input.fieldRow[2]);
|
||||
}
|
||||
|
||||
function test_insertFieldAt_prefix() {
|
||||
var ws = new Blockly.Workspace();
|
||||
var block = new Blockly.Block(ws);
|
||||
var input = new Blockly.Input(Blockly.DUMMY_INPUT, 'INPUT', block);
|
||||
var before = new Blockly.FieldLabel('before');
|
||||
var after = new Blockly.FieldLabel('after');
|
||||
var prefix = new Blockly.FieldLabel('prefix');
|
||||
var between = new Blockly.FieldLabel('between');
|
||||
between.prefixField = prefix
|
||||
input.appendField(before);
|
||||
input.appendField(after);
|
||||
|
||||
// Preconditions
|
||||
assertEquals(2, input.fieldRow.length);
|
||||
assertEquals(before, input.fieldRow[0]);
|
||||
assertEquals(after, input.fieldRow[1]);
|
||||
|
||||
// Actual Tests
|
||||
input.insertFieldAt(1, between);
|
||||
assertEquals(4, input.fieldRow.length);
|
||||
assertEquals(before, input.fieldRow[0]);
|
||||
assertEquals(prefix, input.fieldRow[1]);
|
||||
assertEquals(between, input.fieldRow[2]);
|
||||
assertEquals(after, input.fieldRow[3]);
|
||||
}
|
||||
|
||||
function test_insertFieldAt_prefix() {
|
||||
var ws = new Blockly.Workspace();
|
||||
var block = new Blockly.Block(ws);
|
||||
var input = new Blockly.Input(Blockly.DUMMY_INPUT, 'INPUT', block);
|
||||
var before = new Blockly.FieldLabel('before');
|
||||
var after = new Blockly.FieldLabel('after');
|
||||
var suffix = new Blockly.FieldLabel('suffix');
|
||||
var between = new Blockly.FieldLabel('between');
|
||||
between.suffixField = suffix
|
||||
input.appendField(before);
|
||||
input.appendField(after);
|
||||
|
||||
// Preconditions
|
||||
assertEquals(2, input.fieldRow.length);
|
||||
assertEquals(before, input.fieldRow[0]);
|
||||
assertEquals(after, input.fieldRow[1]);
|
||||
|
||||
// Actual Tests
|
||||
input.insertFieldAt(1, between);
|
||||
assertEquals(4, input.fieldRow.length);
|
||||
assertEquals(before, input.fieldRow[0]);
|
||||
assertEquals(between, input.fieldRow[1]);
|
||||
assertEquals(suffix, input.fieldRow[2]);
|
||||
assertEquals(after, input.fieldRow[3]);
|
||||
}
|
260
tests/jsunit/json_test.js
Normal file
260
tests/jsunit/json_test.js
Normal file
|
@ -0,0 +1,260 @@
|
|||
/**
|
||||
* @license
|
||||
* Visual Blocks Editor
|
||||
*
|
||||
* Copyright 2017 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.
|
||||
*/
|
||||
'use strict';
|
||||
|
||||
/** Ensure a block can be instantiated from a JSON definition. */
|
||||
function test_json_minimal() {
|
||||
var BLOCK_TYPE = 'test_json_minimal';
|
||||
|
||||
var workspace = new Blockly.Workspace();
|
||||
var block;
|
||||
try {
|
||||
Blockly.defineBlocksWithJsonArray([{
|
||||
"type": BLOCK_TYPE
|
||||
}]);
|
||||
|
||||
block = new Blockly.Block(workspace, BLOCK_TYPE);
|
||||
assertEquals(BLOCK_TYPE, block.type);
|
||||
// TODO: asserts
|
||||
} finally {
|
||||
block.dispose();
|
||||
workspace.dispose();
|
||||
delete Blockly.Blocks[BLOCK_TYPE];
|
||||
}
|
||||
}
|
||||
|
||||
/** Ensure message0 creates an input. */
|
||||
function test_json_message0() {
|
||||
var BLOCK_TYPE = 'test_json_message0';
|
||||
var MESSAGE0 = 'message0';
|
||||
|
||||
var workspace = new Blockly.Workspace();
|
||||
var block;
|
||||
try {
|
||||
Blockly.defineBlocksWithJsonArray([{
|
||||
"type": BLOCK_TYPE,
|
||||
"message0": MESSAGE0
|
||||
}]);
|
||||
|
||||
block = new Blockly.Block(workspace, BLOCK_TYPE);
|
||||
assertEquals(1, block.inputList.length);
|
||||
assertEquals(1, block.inputList[0].fieldRow.length);
|
||||
var textField = block.inputList[0].fieldRow[0];
|
||||
assertEquals(Blockly.FieldLabel, textField.constructor);
|
||||
assertEquals(MESSAGE0, textField.getText());
|
||||
} finally {
|
||||
block && block.dispose();
|
||||
workspace.dispose();
|
||||
delete Blockly.Blocks[BLOCK_TYPE];
|
||||
}
|
||||
}
|
||||
|
||||
/** Ensure message1 creates a new input. */
|
||||
function test_json_message1() {
|
||||
var BLOCK_TYPE = 'test_json_message1';
|
||||
var MESSAGE0 = 'message0';
|
||||
var MESSAGE1 = 'message1';
|
||||
|
||||
var workspace = new Blockly.Workspace();
|
||||
var block;
|
||||
try {
|
||||
Blockly.defineBlocksWithJsonArray([{
|
||||
"type": BLOCK_TYPE,
|
||||
"message0": MESSAGE0,
|
||||
"message1": MESSAGE1
|
||||
}]);
|
||||
|
||||
block = new Blockly.Block(workspace, BLOCK_TYPE);
|
||||
assertEquals(2, block.inputList.length);
|
||||
|
||||
assertEquals(1, block.inputList[0].fieldRow.length);
|
||||
var textField = block.inputList[0].fieldRow[0];
|
||||
assertEquals(Blockly.FieldLabel, textField.constructor);
|
||||
assertEquals(MESSAGE0, textField.getText());
|
||||
|
||||
assertEquals(1, block.inputList[1].fieldRow.length);
|
||||
var textField = block.inputList[1].fieldRow[0];
|
||||
assertEquals(Blockly.FieldLabel, textField.constructor);
|
||||
assertEquals(MESSAGE1, textField.getText());
|
||||
} finally {
|
||||
block && block.dispose();
|
||||
workspace.dispose();
|
||||
delete Blockly.Blocks[BLOCK_TYPE];
|
||||
}
|
||||
}
|
||||
|
||||
/** Ensure message string is dereferenced. */
|
||||
function test_json_message0_i18n() {
|
||||
var BLOCK_TYPE = 'test_json_message0_i18n';
|
||||
var MESSAGE0 = '%{BKY_MESSAGE}';
|
||||
var MESSAGE = 'message';
|
||||
|
||||
Blockly.Msg['MESSAGE'] = MESSAGE;
|
||||
|
||||
var workspace = new Blockly.Workspace();
|
||||
var block;
|
||||
try {
|
||||
Blockly.defineBlocksWithJsonArray([{
|
||||
"type": BLOCK_TYPE,
|
||||
"message0": MESSAGE0
|
||||
}]);
|
||||
|
||||
block = new Blockly.Block(workspace, BLOCK_TYPE);
|
||||
assertEquals(1, block.inputList.length);
|
||||
assertEquals(1, block.inputList[0].fieldRow.length);
|
||||
var textField = block.inputList[0].fieldRow[0];
|
||||
assertEquals(Blockly.FieldLabel, textField.constructor);
|
||||
assertEquals(MESSAGE, textField.getText());
|
||||
} finally {
|
||||
block && block.dispose(); // Disposes of textField, too.
|
||||
workspace.dispose();
|
||||
delete Blockly.Blocks[BLOCK_TYPE];
|
||||
delete Blockly.Msg['MESSAGE'];
|
||||
}
|
||||
}
|
||||
|
||||
function test_json_dropdown() {
|
||||
var BLOCK_TYPE = 'test_json_dropdown';
|
||||
var FIELD_NAME = 'FIELD_NAME';
|
||||
var LABEL0 = 'LABEL0';
|
||||
var VALUE0 = 'VALUE0';
|
||||
var LABEL1 = 'LABEL1';
|
||||
var VALUE1 = 'VALUE1';
|
||||
|
||||
var workspace = new Blockly.Workspace();
|
||||
var block;
|
||||
try {
|
||||
Blockly.defineBlocksWithJsonArray([{
|
||||
"type": BLOCK_TYPE,
|
||||
"message0": "%1",
|
||||
"args0": [
|
||||
{
|
||||
"type": "field_dropdown",
|
||||
"name": FIELD_NAME,
|
||||
"options": [
|
||||
[LABEL0, VALUE0],
|
||||
[LABEL1, VALUE1]
|
||||
]
|
||||
}
|
||||
]
|
||||
}]);
|
||||
|
||||
block = new Blockly.Block(workspace, BLOCK_TYPE);
|
||||
assertEquals(1, block.inputList.length);
|
||||
assertEquals(1, block.inputList[0].fieldRow.length);
|
||||
var dropdown = block.inputList[0].fieldRow[0];
|
||||
assertEquals(dropdown, block.getField(FIELD_NAME));
|
||||
assertEquals(Blockly.FieldDropdown, dropdown.constructor);
|
||||
assertEquals(VALUE0, dropdown.getValue());
|
||||
|
||||
var options = dropdown.getOptions();
|
||||
assertEquals(LABEL0, options[0][0]);
|
||||
assertEquals(VALUE0, options[0][1]);
|
||||
assertEquals(LABEL1, options[1][0]);
|
||||
assertEquals(VALUE1, options[1][1]);
|
||||
} finally {
|
||||
block && block.dispose(); // Disposes of dropdown, too.
|
||||
workspace.dispose();
|
||||
delete Blockly.Blocks[BLOCK_TYPE];
|
||||
}
|
||||
}
|
||||
|
||||
function test_json_dropdown_image() {
|
||||
var BLOCK_TYPE = 'test_json_dropdown';
|
||||
var FIELD_NAME = 'FIELD_NAME';
|
||||
var IMAGE1_ALT_TEXT = 'Localized message.';
|
||||
Blockly.Msg['ALT_TEXT'] = IMAGE1_ALT_TEXT;
|
||||
var IMAGE0 = {
|
||||
'width': 12,
|
||||
'height': 34,
|
||||
'src': 'http://image0.src',
|
||||
'alt': 'IMAGE0 alt text'
|
||||
};
|
||||
var VALUE0 = 'VALUE0';
|
||||
var IMAGE1 = {
|
||||
'width': 56,
|
||||
'height': 78,
|
||||
'src': 'http://image1.src',
|
||||
'alt': '%{BKY_ALT_TEXT}'
|
||||
};
|
||||
var VALUE1 = 'VALUE1';
|
||||
var IMAGE2 = {
|
||||
'width': 90,
|
||||
'height': 123,
|
||||
'src': 'http://image2.src'
|
||||
};
|
||||
var VALUE2 = 'VALUE2';
|
||||
|
||||
var workspace = new Blockly.Workspace();
|
||||
var block;
|
||||
try {
|
||||
Blockly.defineBlocksWithJsonArray([{
|
||||
"type": BLOCK_TYPE,
|
||||
"message0": "%1",
|
||||
"args0": [
|
||||
{
|
||||
"type": "field_dropdown",
|
||||
"name": FIELD_NAME,
|
||||
"options": [
|
||||
[IMAGE0, VALUE0],
|
||||
[IMAGE1, VALUE1],
|
||||
[IMAGE2, VALUE2]
|
||||
]
|
||||
}
|
||||
]
|
||||
}]);
|
||||
|
||||
block = new Blockly.Block(workspace, BLOCK_TYPE);
|
||||
assertEquals(1, block.inputList.length);
|
||||
assertEquals(1, block.inputList[0].fieldRow.length);
|
||||
var dropdown = block.inputList[0].fieldRow[0];
|
||||
assertEquals(dropdown, block.getField(FIELD_NAME));
|
||||
assertEquals(Blockly.FieldDropdown, dropdown.constructor);
|
||||
assertEquals(VALUE0, dropdown.getValue());
|
||||
|
||||
var options = dropdown.getOptions();
|
||||
var image0 = options[0][0];
|
||||
assertEquals(IMAGE0.width, image0.width);
|
||||
assertEquals(IMAGE0.height, image0.height);
|
||||
assertEquals(IMAGE0.src, image0.src);
|
||||
assertEquals(IMAGE0.alt, image0.alt);
|
||||
assertEquals(VALUE0, options[0][1]);
|
||||
|
||||
var image1 = options[1][0];
|
||||
assertEquals(IMAGE1.width, image1.width);
|
||||
assertEquals(IMAGE1.height, image1.height);
|
||||
assertEquals(IMAGE1.src, image1.src);
|
||||
assertEquals(IMAGE1.alt, IMAGE1_ALT_TEXT); // Via Msg reference
|
||||
assertEquals(VALUE1, options[1][1]);
|
||||
|
||||
var image2 = options[2][0];
|
||||
assertEquals(IMAGE2.width, image2.width);
|
||||
assertEquals(IMAGE2.height, image2.height);
|
||||
assertEquals(IMAGE2.src, image2.src);
|
||||
assert(image2.alt == null); // No alt specified.
|
||||
assertEquals(VALUE2, options[2][1]);
|
||||
} finally {
|
||||
block && block.dispose(); // Disposes of dropdown, too.
|
||||
workspace.dispose();
|
||||
delete Blockly.Blocks[BLOCK_TYPE];
|
||||
delete Blockly.Msg['ALTTEXT'];
|
||||
}
|
||||
}
|
||||
|
|
@ -80,80 +80,3 @@ function svgTest_newTwoFieldBlock() {
|
|||
block.render(false);
|
||||
return block;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check that the clicking the block shows the editor when the block has one
|
||||
* field.
|
||||
*/
|
||||
function test_oneFieldBlock_blockClickShowsEditor() {
|
||||
svgTest_setUp();
|
||||
|
||||
try {
|
||||
var block = svgTest_newOneFieldBlock();
|
||||
|
||||
var showEditorCalled = false;
|
||||
block.getField('FIELD').showEditor_ = function() {
|
||||
showEditorCalled = true;
|
||||
};
|
||||
|
||||
block.getSvgRoot().dispatchEvent(new Event('mouseup'));
|
||||
assertTrue('showEditor_() not called', showEditorCalled);
|
||||
} finally {
|
||||
svgTest_tearDown();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Check that the clicking the field shows the editor when the block has more
|
||||
* than one field.
|
||||
*/
|
||||
function test_twoFieldBlock_fieldClickShowsEditor() {
|
||||
svgTest_setUp();
|
||||
|
||||
try {
|
||||
var block = svgTest_newTwoFieldBlock();
|
||||
|
||||
var showEditorCalled = false;
|
||||
block.getField('FIELD').showEditor_ = function() {
|
||||
showEditorCalled = true;
|
||||
};
|
||||
|
||||
block.getField('FIELD').getSvgRoot().dispatchEvent(new Event('mouseup'));
|
||||
assertTrue('showEditor_() not called', showEditorCalled);
|
||||
} finally {
|
||||
svgTest_tearDown();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Check that blocks with one field have the text cursor style.
|
||||
*/
|
||||
function test_oneFieldBlock_blockCursorStyleIsText() {
|
||||
svgTest_setUp();
|
||||
|
||||
try {
|
||||
var block = svgTest_newOneFieldBlock();
|
||||
|
||||
assertEquals('text', block.getSvgRoot().style.cursor);
|
||||
} finally {
|
||||
svgTest_tearDown();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Check that blocks with two fields have the text cursor style on the field
|
||||
* group.
|
||||
*/
|
||||
function test_twoFieldBlock_fieldCursorStyleIsText() {
|
||||
svgTest_setUp();
|
||||
|
||||
try {
|
||||
var block = svgTest_newTwoFieldBlock();
|
||||
|
||||
assertEquals('text', block.getField('FIELD').getSvgRoot().style.cursor);
|
||||
} finally {
|
||||
svgTest_tearDown();
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -7,15 +7,22 @@
|
|||
<script>goog.require('goog.testing.jsunit');</script>
|
||||
</head>
|
||||
<body>
|
||||
<script src="utils_test.js"></script>
|
||||
<script src="blockly_test.js"></script>
|
||||
<script src="block_test.js"></script>
|
||||
<script src="connection_test.js"></script>
|
||||
<script src="extensions_test.js"></script>
|
||||
<script src="field_test.js"></script>
|
||||
<script src="field_angle_test.js"></script>
|
||||
<script src="field_number_test.js"></script>
|
||||
<script src="generator_test.js"></script>
|
||||
<script src="connection_db_test.js"></script>
|
||||
<script src="input_test.js"></script>
|
||||
<script src="names_test.js"></script>
|
||||
<script src="workspace_test.js"></script>
|
||||
<script src="xml_test.js"></script>
|
||||
<script src="svg_test.js"></script>
|
||||
<script src="json_test.js"></script>
|
||||
|
||||
<div id="blocklyDiv" style="display: none; height: 480px; width: 600px;"></div>
|
||||
<xml id="toolbox" style="display: none"></xml>
|
||||
|
|
|
@ -21,62 +21,84 @@
|
|||
|
||||
function test_emptyWorkspace() {
|
||||
var workspace = new Blockly.Workspace();
|
||||
assertEquals('Empty workspace (1).', 0, workspace.getTopBlocks(true).length);
|
||||
assertEquals('Empty workspace (2).', 0, workspace.getTopBlocks(false).length);
|
||||
assertEquals('Empty workspace (3).', 0, workspace.getAllBlocks().length);
|
||||
workspace.clear();
|
||||
assertEquals('Empty workspace (4).', 0, workspace.getTopBlocks(true).length);
|
||||
assertEquals('Empty workspace (5).', 0, workspace.getTopBlocks(false).length);
|
||||
assertEquals('Empty workspace (6).', 0, workspace.getAllBlocks().length);
|
||||
try {
|
||||
assertEquals('Empty workspace (1).', 0, workspace.getTopBlocks(true).length);
|
||||
assertEquals('Empty workspace (2).', 0, workspace.getTopBlocks(false).length);
|
||||
assertEquals('Empty workspace (3).', 0, workspace.getAllBlocks().length);
|
||||
workspace.clear();
|
||||
assertEquals('Empty workspace (4).', 0, workspace.getTopBlocks(true).length);
|
||||
assertEquals('Empty workspace (5).', 0, workspace.getTopBlocks(false).length);
|
||||
assertEquals('Empty workspace (6).', 0, workspace.getAllBlocks().length);
|
||||
} finally {
|
||||
workspace.dispose();
|
||||
}
|
||||
}
|
||||
|
||||
function test_flatWorkspace() {
|
||||
var workspace = new Blockly.Workspace();
|
||||
var blockA = workspace.newBlock('');
|
||||
assertEquals('One block workspace (1).', 1, workspace.getTopBlocks(true).length);
|
||||
assertEquals('One block workspace (2).', 1, workspace.getTopBlocks(false).length);
|
||||
assertEquals('One block workspace (3).', 1, workspace.getAllBlocks().length);
|
||||
var blockB = workspace.newBlock('');
|
||||
assertEquals('Two block workspace (1).', 2, workspace.getTopBlocks(true).length);
|
||||
assertEquals('Two block workspace (2).', 2, workspace.getTopBlocks(false).length);
|
||||
assertEquals('Two block workspace (3).', 2, workspace.getAllBlocks().length);
|
||||
blockA.dispose();
|
||||
assertEquals('One block workspace (4).', 1, workspace.getTopBlocks(true).length);
|
||||
assertEquals('One block workspace (5).', 1, workspace.getTopBlocks(false).length);
|
||||
assertEquals('One block workspace (6).', 1, workspace.getAllBlocks().length);
|
||||
workspace.clear();
|
||||
assertEquals('Cleared workspace (1).', 0, workspace.getTopBlocks(true).length);
|
||||
assertEquals('Cleared workspace (2).', 0, workspace.getTopBlocks(false).length);
|
||||
assertEquals('Cleared workspace (3).', 0, workspace.getAllBlocks().length);
|
||||
var blockA, blockB;
|
||||
try {
|
||||
blockA = workspace.newBlock('');
|
||||
assertEquals('One block workspace (1).', 1, workspace.getTopBlocks(true).length);
|
||||
assertEquals('One block workspace (2).', 1, workspace.getTopBlocks(false).length);
|
||||
assertEquals('One block workspace (3).', 1, workspace.getAllBlocks().length);
|
||||
blockB = workspace.newBlock('');
|
||||
assertEquals('Two block workspace (1).', 2, workspace.getTopBlocks(true).length);
|
||||
assertEquals('Two block workspace (2).', 2, workspace.getTopBlocks(false).length);
|
||||
assertEquals('Two block workspace (3).', 2, workspace.getAllBlocks().length);
|
||||
blockA.dispose();
|
||||
assertEquals('One block workspace (4).', 1, workspace.getTopBlocks(true).length);
|
||||
assertEquals('One block workspace (5).', 1, workspace.getTopBlocks(false).length);
|
||||
assertEquals('One block workspace (6).', 1, workspace.getAllBlocks().length);
|
||||
workspace.clear();
|
||||
assertEquals('Cleared workspace (1).', 0, workspace.getTopBlocks(true).length);
|
||||
assertEquals('Cleared workspace (2).', 0, workspace.getTopBlocks(false).length);
|
||||
assertEquals('Cleared workspace (3).', 0, workspace.getAllBlocks().length);
|
||||
} finally {
|
||||
blockB && blockB.dispose();
|
||||
blockA && blockA.dispose();
|
||||
workspace.dispose();
|
||||
}
|
||||
}
|
||||
|
||||
function test_getWorkspaceById() {
|
||||
var workspaceA = new Blockly.Workspace();
|
||||
var workspaceB = new Blockly.Workspace();
|
||||
assertEquals('Find workspaceA.', workspaceA,
|
||||
Blockly.Workspace.getById(workspaceA.id));
|
||||
assertEquals('Find workspaceB.', workspaceB,
|
||||
Blockly.Workspace.getById(workspaceB.id));
|
||||
assertEquals('No workspace found.', null,
|
||||
Blockly.Workspace.getById('I do not exist.'));
|
||||
workspaceA.dispose();
|
||||
assertEquals('Can\'t find workspaceA.', null,
|
||||
Blockly.Workspace.getById(workspaceA.id));
|
||||
assertEquals('WorkspaceB exists.', workspaceB,
|
||||
Blockly.Workspace.getById(workspaceB.id));
|
||||
try {
|
||||
assertEquals('Find workspaceA.', workspaceA,
|
||||
Blockly.Workspace.getById(workspaceA.id));
|
||||
assertEquals('Find workspaceB.', workspaceB,
|
||||
Blockly.Workspace.getById(workspaceB.id));
|
||||
assertEquals('No workspace found.', null,
|
||||
Blockly.Workspace.getById('I do not exist.'));
|
||||
workspaceA.dispose();
|
||||
assertEquals('Can\'t find workspaceA.', null,
|
||||
Blockly.Workspace.getById(workspaceA.id));
|
||||
assertEquals('WorkspaceB exists.', workspaceB,
|
||||
Blockly.Workspace.getById(workspaceB.id));
|
||||
} finally {
|
||||
workspaceB.dispose();
|
||||
workspaceA.dispose();
|
||||
}
|
||||
}
|
||||
|
||||
function test_getBlockById() {
|
||||
var workspace = new Blockly.Workspace();
|
||||
var blockA = workspace.newBlock('');
|
||||
var blockB = workspace.newBlock('');
|
||||
assertEquals('Find blockA.', blockA, workspace.getBlockById(blockA.id));
|
||||
assertEquals('Find blockB.', blockB, workspace.getBlockById(blockB.id));
|
||||
assertEquals('No block found.', null,
|
||||
workspace.getBlockById('I do not exist.'));
|
||||
blockA.dispose();
|
||||
assertEquals('Can\'t find blockA.', null, workspace.getBlockById(blockA.id));
|
||||
assertEquals('BlockB exists.', blockB, workspace.getBlockById(blockB.id));
|
||||
workspace.clear();
|
||||
assertEquals('Can\'t find blockB.', null, workspace.getBlockById(blockB.id));
|
||||
try {
|
||||
assertEquals('Find blockA.', blockA, workspace.getBlockById(blockA.id));
|
||||
assertEquals('Find blockB.', blockB, workspace.getBlockById(blockB.id));
|
||||
assertEquals('No block found.', null,
|
||||
workspace.getBlockById('I do not exist.'));
|
||||
blockA.dispose();
|
||||
assertEquals('Can\'t find blockA.', null, workspace.getBlockById(blockA.id));
|
||||
assertEquals('BlockB exists.', blockB, workspace.getBlockById(blockB.id));
|
||||
workspace.clear();
|
||||
assertEquals('Can\'t find blockB.', null, workspace.getBlockById(blockB.id));
|
||||
} finally {
|
||||
blockB.dispose();
|
||||
blockA.dispose();
|
||||
workspace.dispose();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -68,17 +68,19 @@ function test_domToWorkspace() {
|
|||
}
|
||||
};
|
||||
|
||||
var workspace = new Blockly.Workspace();
|
||||
try {
|
||||
var dom = Blockly.Xml.textToDom(
|
||||
'<xml xmlns="http://www.w3.org/1999/xhtml">' +
|
||||
' <block type="test_block" inline="true" x="21" y="23">' +
|
||||
' </block>' +
|
||||
'</xml>');
|
||||
var workspace = new Blockly.Workspace();
|
||||
Blockly.Xml.domToWorkspace(dom, workspace);
|
||||
assertEquals('Block count', 1, workspace.getAllBlocks().length);
|
||||
} finally {
|
||||
delete Blockly.Blocks.test_block;
|
||||
|
||||
workspace.dispose();
|
||||
}
|
||||
}
|
||||
|
||||
|
|
399
tests/workspace_svg/index.html
Normal file
399
tests/workspace_svg/index.html
Normal file
|
@ -0,0 +1,399 @@
|
|||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<meta charset="utf-8">
|
||||
<title>Blockly Workspace SVG testing</title>
|
||||
<script src="../../blockly_uncompressed.js"></script>
|
||||
<script>goog.require('goog.testing.jsunit');</script>
|
||||
<script src="../../msg/messages.js"></script>
|
||||
<script src="../../blocks/logic.js"></script>
|
||||
<script src="../../blocks/loops.js"></script>
|
||||
<script src="../../blocks/math.js"></script>
|
||||
<script src="../../blocks/text.js"></script>
|
||||
<script src="../../blocks/lists.js"></script>
|
||||
<script src="../../blocks/colour.js"></script>
|
||||
<script src="../../blocks/variables.js"></script>
|
||||
<script src="../../blocks/procedures.js"></script>
|
||||
<script>
|
||||
'use strict';
|
||||
</script>
|
||||
|
||||
<style>
|
||||
html, body {
|
||||
height: 100%;
|
||||
}
|
||||
body {
|
||||
background-color: #fff;
|
||||
font-family: sans-serif;
|
||||
overflow: hidden;
|
||||
}
|
||||
h1 {
|
||||
font-weight: normal;
|
||||
font-size: 140%;
|
||||
}
|
||||
#blocklyDiv {
|
||||
float: right;
|
||||
height: 95%;
|
||||
width: 70%;
|
||||
}
|
||||
#importExport {
|
||||
font-family: monospace;
|
||||
}
|
||||
|
||||
.ioLabel>.blocklyFlyoutLabelText {
|
||||
font-style: italic;
|
||||
}
|
||||
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
|
||||
<div id="blocklyDiv"></div>
|
||||
|
||||
<h1>Blockly Workspace testing</h1>
|
||||
<script src="workspace_svg_test.js"></script>
|
||||
|
||||
<xml id="toolbox-simple" style="display: none">
|
||||
<block type="controls_ifelse"></block>
|
||||
<block type="logic_compare"></block>
|
||||
<!-- <block type="control_repeat"></block> -->
|
||||
<block type="logic_operation"></block>
|
||||
<block type="controls_repeat_ext">
|
||||
<value name="TIMES">
|
||||
<shadow type="math_number">
|
||||
<field name="NUM">10</field>
|
||||
</shadow>
|
||||
</value>
|
||||
</block>
|
||||
<block type="logic_operation"></block>
|
||||
<block type="logic_negate"></block>
|
||||
<block type="logic_boolean"></block>
|
||||
<block type="logic_null" disabled="true"></block>
|
||||
<block type="logic_ternary"></block>
|
||||
</xml>
|
||||
|
||||
<xml id="toolbox-categories" style="display: none">
|
||||
<category name="Logic" colour="210">
|
||||
<block type="controls_if"></block>
|
||||
<block type="logic_compare"></block>
|
||||
<block type="logic_operation"></block>
|
||||
<block type="logic_negate"></block>
|
||||
<block type="logic_boolean"></block>
|
||||
<block type="logic_null" disabled="true"></block>
|
||||
<block type="logic_ternary"></block>
|
||||
</category>
|
||||
<category name="Loops" colour="120">
|
||||
<block type="controls_repeat_ext">
|
||||
<value name="TIMES">
|
||||
<shadow type="math_number">
|
||||
<field name="NUM">10</field>
|
||||
</shadow>
|
||||
</value>
|
||||
</block>
|
||||
<block type="controls_repeat" disabled="true"></block>
|
||||
<block type="controls_whileUntil"></block>
|
||||
<block type="controls_for">
|
||||
<value name="FROM">
|
||||
<shadow type="math_number">
|
||||
<field name="NUM">1</field>
|
||||
</shadow>
|
||||
</value>
|
||||
<value name="TO">
|
||||
<shadow type="math_number">
|
||||
<field name="NUM">10</field>
|
||||
</shadow>
|
||||
</value>
|
||||
<value name="BY">
|
||||
<shadow type="math_number">
|
||||
<field name="NUM">1</field>
|
||||
</shadow>
|
||||
</value>
|
||||
</block>
|
||||
<block type="controls_forEach"></block>
|
||||
<block type="controls_flow_statements"></block>
|
||||
</category>
|
||||
<category name="Math" colour="230">
|
||||
<block type="math_number" gap="32"></block>
|
||||
<block type="math_arithmetic">
|
||||
<value name="A">
|
||||
<shadow type="math_number">
|
||||
<field name="NUM">1</field>
|
||||
</shadow>
|
||||
</value>
|
||||
<value name="B">
|
||||
<shadow type="math_number">
|
||||
<field name="NUM">1</field>
|
||||
</shadow>
|
||||
</value>
|
||||
</block>
|
||||
<block type="math_single">
|
||||
<value name="NUM">
|
||||
<shadow type="math_number">
|
||||
<field name="NUM">9</field>
|
||||
</shadow>
|
||||
</value>
|
||||
</block>
|
||||
<block type="math_trig">
|
||||
<value name="NUM">
|
||||
<shadow type="math_number">
|
||||
<field name="NUM">45</field>
|
||||
</shadow>
|
||||
</value>
|
||||
</block>
|
||||
<block type="math_constant"></block>
|
||||
<block type="math_number_property">
|
||||
<value name="NUMBER_TO_CHECK">
|
||||
<shadow type="math_number">
|
||||
<field name="NUM">0</field>
|
||||
</shadow>
|
||||
</value>
|
||||
</block>
|
||||
<block type="math_round">
|
||||
<value name="NUM">
|
||||
<shadow type="math_number">
|
||||
<field name="NUM">3.1</field>
|
||||
</shadow>
|
||||
</value>
|
||||
</block>
|
||||
<block type="math_on_list"></block>
|
||||
<block type="math_modulo">
|
||||
<value name="DIVIDEND">
|
||||
<shadow type="math_number">
|
||||
<field name="NUM">64</field>
|
||||
</shadow>
|
||||
</value>
|
||||
<value name="DIVISOR">
|
||||
<shadow type="math_number">
|
||||
<field name="NUM">10</field>
|
||||
</shadow>
|
||||
</value>
|
||||
</block>
|
||||
<block type="math_constrain">
|
||||
<value name="VALUE">
|
||||
<shadow type="math_number">
|
||||
<field name="NUM">50</field>
|
||||
</shadow>
|
||||
</value>
|
||||
<value name="LOW">
|
||||
<shadow type="math_number">
|
||||
<field name="NUM">1</field>
|
||||
</shadow>
|
||||
</value>
|
||||
<value name="HIGH">
|
||||
<shadow type="math_number">
|
||||
<field name="NUM">100</field>
|
||||
</shadow>
|
||||
</value>
|
||||
</block>
|
||||
<block type="math_random_int">
|
||||
<value name="FROM">
|
||||
<shadow type="math_number">
|
||||
<field name="NUM">1</field>
|
||||
</shadow>
|
||||
</value>
|
||||
<value name="TO">
|
||||
<shadow type="math_number">
|
||||
<field name="NUM">100</field>
|
||||
</shadow>
|
||||
</value>
|
||||
</block>
|
||||
<block type="math_random_float"></block>
|
||||
</category>
|
||||
<category name="Text" colour="160">
|
||||
<block type="text"></block>
|
||||
<block type="text_join"></block>
|
||||
<block type="text_append">
|
||||
<value name="TEXT">
|
||||
<shadow type="text"></shadow>
|
||||
</value>
|
||||
</block>
|
||||
<block type="text_length">
|
||||
<value name="VALUE">
|
||||
<shadow type="text">
|
||||
<field name="TEXT">abc</field>
|
||||
</shadow>
|
||||
</value>
|
||||
</block>
|
||||
<block type="text_isEmpty">
|
||||
<value name="VALUE">
|
||||
<shadow type="text">
|
||||
<field name="TEXT"></field>
|
||||
</shadow>
|
||||
</value>
|
||||
</block>
|
||||
<block type="text_indexOf">
|
||||
<value name="VALUE">
|
||||
<block type="variables_get">
|
||||
<field name="VAR">text</field>
|
||||
</block>
|
||||
</value>
|
||||
<value name="FIND">
|
||||
<shadow type="text">
|
||||
<field name="TEXT">abc</field>
|
||||
</shadow>
|
||||
</value>
|
||||
</block>
|
||||
<block type="text_charAt">
|
||||
<value name="VALUE">
|
||||
<block type="variables_get">
|
||||
<field name="VAR">text</field>
|
||||
</block>
|
||||
</value>
|
||||
</block>
|
||||
<block type="text_getSubstring">
|
||||
<value name="STRING">
|
||||
<block type="variables_get">
|
||||
<field name="VAR">text</field>
|
||||
</block>
|
||||
</value>
|
||||
</block>
|
||||
<block type="text_changeCase">
|
||||
<value name="TEXT">
|
||||
<shadow type="text">
|
||||
<field name="TEXT">abc</field>
|
||||
</shadow>
|
||||
</value>
|
||||
</block>
|
||||
<block type="text_trim">
|
||||
<value name="TEXT">
|
||||
<shadow type="text">
|
||||
<field name="TEXT">abc</field>
|
||||
</shadow>
|
||||
</value>
|
||||
</block>
|
||||
<block type="text_count">
|
||||
<value name="SUB">
|
||||
<shadow type="text"></shadow>
|
||||
</value>
|
||||
<value name="TEXT">
|
||||
<shadow type="text"></shadow>
|
||||
</value>
|
||||
</block>
|
||||
<block type="text_replace">
|
||||
<value name="FROM">
|
||||
<shadow type="text"></shadow>
|
||||
</value>
|
||||
<value name="TO">
|
||||
<shadow type="text"></shadow>
|
||||
</value>
|
||||
<value name="TEXT">
|
||||
<shadow type="text"></shadow>
|
||||
</value>
|
||||
</block>
|
||||
<block type="text_reverse">
|
||||
<value name="TEXT">
|
||||
<shadow type="text"></shadow>
|
||||
</value>
|
||||
</block>
|
||||
<label text="Input/Output:" web-class="ioLabel"></label>
|
||||
<block type="text_print">
|
||||
<value name="TEXT">
|
||||
<shadow type="text">
|
||||
<field name="TEXT">abc</field>
|
||||
</shadow>
|
||||
</value>
|
||||
</block>
|
||||
<block type="text_prompt_ext">
|
||||
<value name="TEXT">
|
||||
<shadow type="text">
|
||||
<field name="TEXT">abc</field>
|
||||
</shadow>
|
||||
</value>
|
||||
</block>
|
||||
</category>
|
||||
<category name="Lists" colour="260">
|
||||
<block type="lists_create_with">
|
||||
<mutation items="0"></mutation>
|
||||
</block>
|
||||
<block type="lists_create_with"></block>
|
||||
<block type="lists_repeat">
|
||||
<value name="NUM">
|
||||
<shadow type="math_number">
|
||||
<field name="NUM">5</field>
|
||||
</shadow>
|
||||
</value>
|
||||
</block>
|
||||
<block type="lists_length"></block>
|
||||
<block type="lists_isEmpty"></block>
|
||||
<block type="lists_indexOf">
|
||||
<value name="VALUE">
|
||||
<block type="variables_get">
|
||||
<field name="VAR">list</field>
|
||||
</block>
|
||||
</value>
|
||||
</block>
|
||||
<block type="lists_getIndex">
|
||||
<value name="VALUE">
|
||||
<block type="variables_get">
|
||||
<field name="VAR">list</field>
|
||||
</block>
|
||||
</value>
|
||||
</block>
|
||||
<block type="lists_setIndex">
|
||||
<value name="LIST">
|
||||
<block type="variables_get">
|
||||
<field name="VAR">list</field>
|
||||
</block>
|
||||
</value>
|
||||
</block>
|
||||
<block type="lists_getSublist">
|
||||
<value name="LIST">
|
||||
<block type="variables_get">
|
||||
<field name="VAR">list</field>
|
||||
</block>
|
||||
</value>
|
||||
</block>
|
||||
<block type="lists_split">
|
||||
<value name="DELIM">
|
||||
<shadow type="text">
|
||||
<field name="TEXT">,</field>
|
||||
</shadow>
|
||||
</value>
|
||||
</block>
|
||||
<block type="lists_sort"></block>
|
||||
<block type="lists_reverse"></block>
|
||||
</category>
|
||||
<category name="Colour" colour="20">
|
||||
<block type="colour_picker"></block>
|
||||
<block type="colour_random"></block>
|
||||
<block type="colour_rgb">
|
||||
<value name="RED">
|
||||
<shadow type="math_number">
|
||||
<field name="NUM">100</field>
|
||||
</shadow>
|
||||
</value>
|
||||
<value name="GREEN">
|
||||
<shadow type="math_number">
|
||||
<field name="NUM">50</field>
|
||||
</shadow>
|
||||
</value>
|
||||
<value name="BLUE">
|
||||
<shadow type="math_number">
|
||||
<field name="NUM">0</field>
|
||||
</shadow>
|
||||
</value>
|
||||
</block>
|
||||
<block type="colour_blend">
|
||||
<value name="COLOUR1">
|
||||
<shadow type="colour_picker">
|
||||
<field name="COLOUR">#ff0000</field>
|
||||
</shadow>
|
||||
</value>
|
||||
<value name="COLOUR2">
|
||||
<shadow type="colour_picker">
|
||||
<field name="COLOUR">#3333ff</field>
|
||||
</shadow>
|
||||
</value>
|
||||
<value name="RATIO">
|
||||
<shadow type="math_number">
|
||||
<field name="NUM">0.5</field>
|
||||
</shadow>
|
||||
</value>
|
||||
</block>
|
||||
</category>
|
||||
<sep></sep>
|
||||
<category name="Variables" colour="330" custom="VARIABLE"></category>
|
||||
<category name="Functions" colour="290" custom="PROCEDURE"></category>
|
||||
</xml>
|
||||
</body>
|
||||
</html>
|
72
tests/workspace_svg/workspace_svg_test.js
Normal file
72
tests/workspace_svg/workspace_svg_test.js
Normal file
|
@ -0,0 +1,72 @@
|
|||
/**
|
||||
* @license
|
||||
* Blockly Tests
|
||||
*
|
||||
* Copyright 2017 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.
|
||||
*/
|
||||
'use strict';
|
||||
|
||||
function helper_createWorkspaceWithToolbox() {
|
||||
var toolbox = document.getElementById('toolbox-categories');
|
||||
return Blockly.inject('blocklyDiv', {toolbox: toolbox});
|
||||
}
|
||||
|
||||
function test_createWorkspace() {
|
||||
var workspace = helper_createWorkspaceWithToolbox();
|
||||
workspace.dispose();
|
||||
}
|
||||
|
||||
function test_emptyWorkspace() {
|
||||
var workspace = helper_createWorkspaceWithToolbox();
|
||||
try {
|
||||
assertEquals('Empty workspace (1).', 0, workspace.getTopBlocks(true).length);
|
||||
assertEquals('Empty workspace (2).', 0, workspace.getTopBlocks(false).length);
|
||||
assertEquals('Empty workspace (3).', 0, workspace.getAllBlocks().length);
|
||||
workspace.clear();
|
||||
assertEquals('Empty workspace (4).', 0, workspace.getTopBlocks(true).length);
|
||||
assertEquals('Empty workspace (5).', 0, workspace.getTopBlocks(false).length);
|
||||
assertEquals('Empty workspace (6).', 0, workspace.getAllBlocks().length);
|
||||
} finally {
|
||||
workspace.dispose();
|
||||
}
|
||||
}
|
||||
|
||||
function test_flatWorkspace() {
|
||||
var workspace = helper_createWorkspaceWithToolbox();
|
||||
var blockA, blockB;
|
||||
try {
|
||||
blockA = workspace.newBlock('');
|
||||
assertEquals('One block workspace (1).', 1, workspace.getTopBlocks(true).length);
|
||||
assertEquals('One block workspace (2).', 1, workspace.getTopBlocks(false).length);
|
||||
assertEquals('One block workspace (3).', 1, workspace.getAllBlocks().length);
|
||||
blockB = workspace.newBlock('');
|
||||
assertEquals('Two block workspace (1).', 2, workspace.getTopBlocks(true).length);
|
||||
assertEquals('Two block workspace (2).', 2, workspace.getTopBlocks(false).length);
|
||||
assertEquals('Two block workspace (3).', 2, workspace.getAllBlocks().length);
|
||||
blockA.dispose();
|
||||
assertEquals('One block workspace (4).', 1, workspace.getTopBlocks(true).length);
|
||||
assertEquals('One block workspace (5).', 1, workspace.getTopBlocks(false).length);
|
||||
assertEquals('One block workspace (6).', 1, workspace.getAllBlocks().length);
|
||||
workspace.clear();
|
||||
assertEquals('Cleared workspace (1).', 0, workspace.getTopBlocks(true).length);
|
||||
assertEquals('Cleared workspace (2).', 0, workspace.getTopBlocks(false).length);
|
||||
assertEquals('Cleared workspace (3).', 0, workspace.getAllBlocks().length);
|
||||
} finally {
|
||||
blockB && blockB.dispose();
|
||||
blockA && blockA.dispose();
|
||||
workspace.dispose();
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue