Added Max Instances Property to Workspace Options (#2130)

* Added Max Instances property to Blocks

* eslint cleanup

* eslint cleanup 2

* Moved maxInstances property from block to workspace (as a map of block type to max instances). isDuplicate() changed to correctly handle siblings/branches.

* eslint cleanup

* Changed checking types to map. Added hasBlockLimits. Fixed Nits.

* Added limit_instances test block. eslint fixes.

* fixup! Added limit_instances test block. eslint fixes.

* Changed sorting objects to a private static function of the workspace. Fixed nits. Undeleted .eslintrc

* Reverted .gitignore file.

* Added getBlockTypeCounts() to utils. Added isCapacityAvailable() to workspace. Changed clipboard to save typeCountsMap rather than object.
This commit is contained in:
BeksOmega
2018-11-27 16:34:21 -08:00
committed by RoboErikG
parent 6169a6488f
commit 1c4ba38300
10 changed files with 221 additions and 35 deletions

View File

@@ -974,3 +974,32 @@ Blockly.utils.containsNode = function(parent, descendant) {
return !!(parent.compareDocumentPosition(descendant) &
Node.DOCUMENT_POSITION_CONTAINED_BY);
};
/**
* Get a map of all the block's descendants mapping their type to the number of
* children with that type.
* @param {!Blockly.Block} block The block to map.
* @param {boolean=} opt_stripFollowing Optionally ignore all following
* statements (blocks that are not inside a value or statement input
* of the block).
* @returns {!Object} Map of types to type counts for descendants of the bock.
*/
Blockly.utils.getBlockTypeCounts = function(block, opt_stripFollowing) {
var typeCountsMap = Object.create(null);
var descendants = block.getDescendants(true);
if (opt_stripFollowing) {
var nextBlock = block.getNextBlock();
if (nextBlock) {
var index = descendants.indexOf(nextBlock);
descendants.splice(index, descendants.length - index);
}
}
for (var i = 0, checkBlock; checkBlock = descendants[i]; i++) {
if (typeCountsMap[checkBlock.type]) {
typeCountsMap[checkBlock.type]++;
} else {
typeCountsMap[checkBlock.type] = 1;
}
}
return typeCountsMap;
};