Merge pull request #2051 from rachel-fenichel/feature/block_utility_fns

Add some functions for insertion markers
This commit is contained in:
Rachel Fenichel
2018-09-13 16:16:54 -07:00
committed by GitHub
2 changed files with 90 additions and 0 deletions

View File

@@ -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

View File

@@ -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).