From a0e90627f7c7b81393d912b2a48d4d7165040b88 Mon Sep 17 00:00:00 2001 From: Rachel Fenichel Date: Thu, 13 Sep 2018 13:37:21 -0700 Subject: [PATCH] Add some functions for insertion markers --- core/block.js | 72 +++++++++++++++++++++++++++++++++++++++++++++++ core/block_svg.js | 18 ++++++++++++ 2 files changed, 90 insertions(+) diff --git a/core/block.js b/core/block.js index 8b451bd6c..32262446c 100644 --- a/core/block.js +++ b/core/block.js @@ -146,6 +146,13 @@ Blockly.Block = function(workspace, prototypeName, opt_id) { /** @type {boolean} */ this.RTL = workspace.RTL; + /** + * True if this block is an insertion marker. + * @type {boolean} + * @protected + */ + this.isInsertionMarker_ = false; + // Copy the type-specific functions and data from the prototype. if (prototypeName) { /** @type {string} */ @@ -508,6 +515,29 @@ Blockly.Block.prototype.getNextBlock = function() { return this.nextConnection && this.nextConnection.targetBlock(); }; +/** + * Return the previous statement block directly connected to this block. + * @return {Blockly.Block} The previous statement block or null. + */ +Blockly.Block.prototype.getPreviousBlock = function() { + return this.previousConnection && this.previousConnection.targetBlock(); +}; + +/** + * Return the connection on the first statement input on this block, or null if + * there are none. + * @return {Blockly.Connection} The first statement connection or null. + * @package + */ +Blockly.Block.prototype.getFirstStatementConnection = function() { + for (var i = 0, input; input = this.inputList[i]; i++) { + if (input.connection && input.connection.type == Blockly.NEXT_STATEMENT) { + return input.connection; + } + } + return null; +}; + /** * Return the top-most block in this block's tree. * This will return itself if this block is at the top level. @@ -655,6 +685,25 @@ Blockly.Block.prototype.setShadow = function(shadow) { this.isShadow_ = shadow; }; +/** + * Get whether this block is an insertion marker block or not. + * @return {boolean} True if an insertion marker. + * @package + */ +Blockly.Block.prototype.isInsertionMarker = function() { + return this.isInsertionMarker_; +}; + +/** + * Set whether this block is an insertion marker block or not. + * Once set this cannot be unset. + * @param {boolean} insertionMarker True if an insertion marker. + * @package + */ +Blockly.Block.prototype.setInsertionMarker = function(insertionMarker) { + this.isInsertionMarker_ = insertionMarker; +}; + /** * Get whether this block is editable or not. * @return {boolean} True if editable. @@ -710,6 +759,29 @@ Blockly.Block.prototype.setConnectionsHidden = function(hidden) { } }; +/** + * Find the connection on this block that corresponds to the given connection + * on the other block. + * Used to match connections between a block and its insertion marker. + * @param {!Blockly.Block} otherBlock The other block to match against. + * @param {!Blockly.Connection} conn The other connection to match. + * @return {Blockly.Connection} the matching connection on this block, or null. + * @package + */ +Blockly.Block.prototype.getMatchingConnection = function(otherBlock, conn) { + var connections = this.getConnections_(true); + var otherConnections = otherBlock.getConnections_(true); + if (connections.length != otherConnections.length) { + throw Error("Connection lists did not match in length."); + } + for (var i = 0; i < otherConnections.length; i++) { + if (otherConnections[i] == conn) { + return connections[i]; + } + } + return null; +}; + /** * Set the URL of this block's help page. * @param {string|Function} url URL string for block help, or function that diff --git a/core/block_svg.js b/core/block_svg.js index 54445d3ff..83dd77a8f 100644 --- a/core/block_svg.js +++ b/core/block_svg.js @@ -794,6 +794,24 @@ Blockly.BlockSvg.prototype.setShadow = function(shadow) { this.updateColour(); }; +/** + * Set whether this block is an insertion marker block or not. + * Once set this cannot be unset. + * @param {boolean} insertionMarker True if an insertion marker. + * @package + */ +Blockly.BlockSvg.prototype.setInsertionMarker = function(insertionMarker) { + if (this.isInsertionMarker_ == insertionMarker) { + return; // No change. + } + this.isInsertionMarker_ = insertionMarker; + if (this.isInsertionMarker_) { + this.setColour(Blockly.INSERTION_MARKER_COLOUR); + Blockly.utils.addClass(/** @type {!Element} */ (this.svgGroup_), + 'blocklyInsertionMarker'); + } +}; + /** * Return the root node of the SVG or null if none exists. * @return {Element} The root SVG node (probably a group).