Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: delete context menu to display the correct number of blocks #127

Merged
merged 1 commit into from
Aug 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
146 changes: 146 additions & 0 deletions src/context_menu_items.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
/**
* @license
* Copyright 2024 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/

import * as Blockly from "blockly/core";

/**
* Registers a block delete option that ignores shadows in the block count.
*/
export function registerDeleteBlock() {
const deleteOption = {
displayText(scope) {
const descendantCount = getDeletableBlocksInStack(scope.block).length;
return descendantCount === 1
? Blockly.Msg["DELETE_BLOCK"]
: Blockly.Msg["DELETE_X_BLOCKS"].replace("%1", `${descendantCount}`);
},
preconditionFn(scope) {
if (!scope.block.isInFlyout && scope.block.isDeletable()) {
return "enabled";
}
return "hidden";
},
callback(scope) {
Blockly.Events.setGroup(true);
scope.block.dispose(true, true);
Blockly.Events.setGroup(false);
},
scopeType: Blockly.ContextMenuRegistry.ScopeType.BLOCK,
id: "blockDelete",
weight: 6,
};
Blockly.ContextMenuRegistry.registry.register(deleteOption);
}

function getDeletableBlocksInStack(block) {
let descendants = block.getDescendants(false).filter(isDeletable);
if (block.getNextBlock()) {
// Next blocks are not deleted.
const nextDescendants = block
.getNextBlock()
.getDescendants(false)
.filter(isDeletable);
descendants = descendants.filter((b) => !nextDescendants.includes(b));
}
return descendants;
}

function isDeletable(block) {
return block.isDeletable() && !block.isShadow();
}

/**
* Option to delete all blocks.
*/
export function registerDeleteAll() {
const deleteOption = {
displayText(scope) {
if (!scope.workspace) {
return "";
}
const deletableBlocksLength = getDeletableBlocksInWorkspace(scope.workspace).length;
if (deletableBlocksLength === 1) {
return Blockly.Msg["DELETE_BLOCK"];
}
return Blockly.Msg["DELETE_X_BLOCKS"].replace(
"%1",
`${deletableBlocksLength}`
);
},
preconditionFn(scope) {
if (!scope.workspace) {
return "disabled";
}
const deletableBlocksLength = getDeletableBlocksInWorkspace(scope.workspace).length;
return deletableBlocksLength > 0 ? "enabled" : "disabled";
},
callback(scope) {
if (!scope.workspace) {
return;
}
scope.workspace.cancelCurrentGesture();
const deletableBlocks = getDeletableBlocksInWorkspace(scope.workspace);
if (deletableBlocks.length < 2) {
deleteNext(deletableBlocks);
} else {
Blockly.dialog.confirm(
Blockly.Msg["DELETE_ALL_BLOCKS"].replace(
"%1",
String(deletableBlocks.length)
),
function (ok) {
if (ok) {
deleteNext(deletableBlocks);
}
}
);
}
},
scopeType: Blockly.ContextMenuRegistry.ScopeType.WORKSPACE,
id: "workspaceDelete",
weight: 6,
};
Blockly.ContextMenuRegistry.registry.register(deleteOption);
}

/*
* Constructs a list of blocks that can be deleted in the given workspace.
*
* @param workspace to delete all blocks from.
* @returns list of blocks to delete.
*/
function getDeletableBlocksInWorkspace(workspace) {
return workspace
.getTopBlocks(true)
.flatMap((b) => b.getDescendants(false).filter(isDeletable));
}

/**
* Deletes the given blocks. Used to delete all blocks in the workspace.
*
* @param deleteList List of blocks to delete.
* @param eventGroup Event group ID with which all delete events should be
* associated. If not specified, create a new group.
*/
function deleteNext(deleteList, eventGroup) {
const DELAY = 10;
if (eventGroup) {
Blockly.Events.setGroup(eventGroup);
} else {
Blockly.Events.setGroup(true);
eventGroup = Blockly.Events.getGroup();
}
const block = deleteList.shift();
if (block) {
if (!block.isDeadOrDying()) {
block.dispose(false, true);
setTimeout(deleteNext, DELAY, deleteList, eventGroup);
} else {
deleteNext(deleteList, eventGroup);
}
}
Blockly.Events.setGroup(false);
}
6 changes: 6 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import * as ScratchVariables from "./variables.js";
import "../core/css.js";
import "../core/field_vertical_separator.js";
import "./renderer/renderer.js";
import * as contextMenuItems from "./context_menu_items.js";
import {
ContinuousToolbox,
ContinuousFlyout,
Expand Down Expand Up @@ -67,6 +68,7 @@ export { glowStack };
export { scratchBlocksUtils };
export { CheckableContinuousFlyout };
export { ScratchVariables };
export { contextMenuItems };

export function inject(container, options) {
Object.assign(options, {
Expand Down Expand Up @@ -111,3 +113,7 @@ Blockly.FlyoutButton.TEXT_MARGIN_Y = 10;
Blockly.ContextMenuRegistry.registry.unregister("blockDisable");
Blockly.ContextMenuRegistry.registry.unregister("blockInline");
Blockly.ContextMenuItems.registerCommentOptions();
Blockly.ContextMenuRegistry.registry.unregister("blockDelete");
contextMenuItems.registerDeleteBlock();
Blockly.ContextMenuRegistry.registry.unregister("workspaceDelete");
contextMenuItems.registerDeleteAll();