Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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
26 changes: 23 additions & 3 deletions src/context_menu_items.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,7 @@ export function registerDeleteBlock() {
if (!scope.block) {
return
}
Blockly.Events.setGroup(true)
scope.block.dispose(true, true)
Blockly.Events.setGroup(false)
deleteBlock(scope.block)
},
scopeType: Blockly.ContextMenuRegistry.ScopeType.BLOCK,
id: 'blockDelete',
Expand All @@ -45,6 +43,28 @@ export function registerDeleteBlock() {
Blockly.ContextMenuRegistry.registry.register(deleteOption)
}

export function deleteBlock(block: Blockly.Block) {
if (block.workspace.isFlyout) return

const priorGroup = Blockly.Events.getGroup()
Blockly.Events.setGroup(true)
Comment thread
Joeclinton1 marked this conversation as resolved.
Outdated
try {
if (!block.outputConnection && !block.previousConnection?.isConnected()) {
block.nextConnection?.disconnect()
Comment thread
Joeclinton1 marked this conversation as resolved.
Outdated
}
if (block.workspace instanceof Blockly.WorkspaceSvg) {
block.workspace.hideChaff()
}
if (block instanceof Blockly.BlockSvg) {
block.dispose(!block.outputConnection, true)
} else {
block.dispose(!block.outputConnection)
}
Comment thread
Joeclinton1 marked this conversation as resolved.
} finally {
Blockly.Events.setGroup(priorGroup)
}
}

function getDeletableBlocksInStack(block: Blockly.Block): Blockly.Block[] {
let descendants = block.getDescendants(false).filter(isDeletable)
const nextBlock = block.getNextBlock()
Expand Down
3 changes: 3 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,9 @@ if (!blockCommentMenuItem) {
}
Blockly.ContextMenuRegistry.registry.unregister('blockDelete')
contextMenuItems.registerDeleteBlock()
Blockly.BlockSvg.prototype.checkAndDelete = function () {
contextMenuItems.deleteBlock(this)
}
contextMenuItems.registerDuplicateBlock()
contextMenuItems.registerCopyShortcut()
contextMenuItems.registerCutShortcut()
Expand Down
46 changes: 45 additions & 1 deletion tests/unit/context_menu_items.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
*/
import * as Blockly from 'blockly/core'
import { afterAll, afterEach, assert, beforeAll, beforeEach, describe, expect, it } from 'vitest'
import { registerDeleteBlock } from '../../src/context_menu_items'
import { deleteBlock, registerDeleteBlock } from '../../src/context_menu_items'

// Tests for the scratch-specific delete context menu override (registerDeleteBlock).
// The copy/cut/paste override (registerDuplicateBlock, issue #3470) is tested
Expand Down Expand Up @@ -171,4 +171,48 @@ describe('registerDeleteBlock', () => {
delete Blockly.Blocks.test_output_block
}
})

it('callback deletes only a top stack block and preserves its next block', () => {
const first = workspace.newBlock('test_stack_block')
const second = workspace.newBlock('test_stack_block')
const nextConn = first.nextConnection
const prevConn = second.previousConnection
assert(nextConn, 'Expected next connection')
assert(prevConn, 'Expected previous connection')
nextConn.connect(prevConn)

const item = Blockly.ContextMenuRegistry.registry.getItem('blockDelete')
assert(item, 'Expected blockDelete item to be registered')
const callback = item.callback as (scope: Blockly.ContextMenuRegistry.Scope) => void
callback({ block: asBlockSvg(first) })

expect(workspace.getAllBlocks(false)).toEqual([second])
expect(second.getParent()).toBeNull()
})

it('checkAndDelete override routes through deleteBlock and preserves next block', () => {
const first = workspace.newBlock('test_stack_block')
const second = workspace.newBlock('test_stack_block')
const nextConn = first.nextConnection
const prevConn = second.previousConnection
assert(nextConn, 'Expected next connection')
assert(prevConn, 'Expected previous connection')
nextConn.connect(prevConn)

// Mirror the wiring in src/index.ts so we cover the keyboard-delete path.
// Tests use plain Workspace (not WorkspaceSvg), so blocks aren't BlockSvg
// instances — invoke the override via prototype to simulate the call site.
const originalCheckAndDelete = Blockly.BlockSvg.prototype.checkAndDelete
Blockly.BlockSvg.prototype.checkAndDelete = function () {
deleteBlock(this)
}
try {
Blockly.BlockSvg.prototype.checkAndDelete.call(first)
} finally {
Blockly.BlockSvg.prototype.checkAndDelete = originalCheckAndDelete
}

expect(workspace.getAllBlocks(false)).toEqual([second])
expect(second.getParent()).toBeNull()
})
})
Loading