Skip to content

Commit 158e987

Browse files
mjgallagjosmas
andauthored
optimize delete block count; depth-first traversal (#115)
* optimize delete block count; depth-first traversal * add multi-select back to delete count * add more checks for multi-selection --------- Co-authored-by: josmas <[email protected]>
1 parent 8604df7 commit 158e987

File tree

1 file changed

+26
-13
lines changed

1 file changed

+26
-13
lines changed

src/multiselect_contextmenu.js

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -611,23 +611,36 @@ const registerDelete = function() {
611611
const dragSelection = dragSelectionWeakMap.get(workspace);
612612

613613
const countDescendants = function(block) {
614-
if (block && !hasSelectedParent(block)) {
615-
// Count the number of blocks that are nested in this block.
616-
descendantCount += block.getDescendants(false).length;
617-
for (const subBlocks of block.getDescendants(false)) {
618-
if (subBlocks.isShadow()) {
619-
descendantCount -= 1;
614+
if (!block || hasSelectedParent(block)) return;
615+
616+
const countBlockAndDescendants = function(currentBlock) {
617+
if (currentBlock.isShadow()) return 0;
618+
let count = 1; // Initial block
619+
for (const input of currentBlock.inputList) {
620+
if (input.connection && input.connection.targetBlock()) {
621+
const connectedBlock = input.connection.targetBlock();
622+
count += countBlockAndDescendants(connectedBlock);
623+
// For statement inputs, also follow the next chain
624+
if (input.type === Blockly.inputs.inputTypes.STATEMENT) {
625+
let nextInStatement = connectedBlock.getNextBlock();
626+
while (nextInStatement) {
627+
count += countBlockAndDescendants(nextInStatement);
628+
nextInStatement = nextInStatement.getNextBlock();
629+
}
630+
}
620631
}
621632
}
622-
const nextBlock = block.getNextBlock();
623-
if (nextBlock) {
624-
// Blocks in the current stack would survive this block's deletion.
625-
descendantCount -= nextBlock.getDescendants(false).length;
626-
}
627-
}
633+
634+
return count;
635+
};
636+
637+
descendantCount += countBlockAndDescendants(block);
628638
};
629639

630-
if (!dragSelection.size) {
640+
const isInMultiselection = dragSelection &&
641+
dragSelection.has(scope.block.id);
642+
643+
if (!dragSelection || dragSelection.size === 0 || !isInMultiselection) {
631644
// Handle single block selection
632645
countDescendants(scope.block);
633646
} else {

0 commit comments

Comments
 (0)