mirror of
https://github.com/google/blockly.git
synced 2026-01-09 10:00:09 +01:00
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:
@@ -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;
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user