From 8c30cdaf03da4b511ed3f0cf9a22118408f1e78c Mon Sep 17 00:00:00 2001 From: Richard Knoll Date: Thu, 19 Mar 2026 10:17:55 -0700 Subject: [PATCH] disable blockly orphan connection logic (#11201) --- pxtblocks/monkeyPatches/connection.ts | 20 ++++++++++++++++++++ pxtblocks/monkeyPatches/index.ts | 2 ++ 2 files changed, 22 insertions(+) create mode 100644 pxtblocks/monkeyPatches/connection.ts diff --git a/pxtblocks/monkeyPatches/connection.ts b/pxtblocks/monkeyPatches/connection.ts new file mode 100644 index 000000000000..01afdac45f87 --- /dev/null +++ b/pxtblocks/monkeyPatches/connection.ts @@ -0,0 +1,20 @@ +import * as Blockly from "blockly"; + +export function monkeyPatchConnection() { + const oldConnectionForOrphanedConnection = Blockly.Connection.getConnectionForOrphanedConnection; + Blockly.Connection.getConnectionForOrphanedConnection = function (startBlock: Blockly.Block, orphanConnection: Blockly.Connection) { + // When a block is dragged into an input that already has a non-shadow block connected, Blockly has + // some logic that checks to see if the block you just dragged has a single input and, if so, connects + // the previously connected block to the input of the block you just dragged. Presumably this is to make + // it easier to wrap blocks with a new block (for example, dragging an absolute value block onto a number + // in an equation will wrap it theoretically saving you an extra drag). However, nobody ever expects this + // behavior and only the most powerful of power users would use it to their advantage. It also can cause + // type errors when the displaced block is a variable since variables can be connected to any input regardless + // of what type they actually hold. It's better to just disable it entirely. + if (orphanConnection.type === Blockly.ConnectionType.OUTPUT_VALUE) { + return null; + } + + return oldConnectionForOrphanedConnection(startBlock, orphanConnection); + } +} \ No newline at end of file diff --git a/pxtblocks/monkeyPatches/index.ts b/pxtblocks/monkeyPatches/index.ts index e03d650cc939..cc61606ff3e5 100644 --- a/pxtblocks/monkeyPatches/index.ts +++ b/pxtblocks/monkeyPatches/index.ts @@ -1,4 +1,5 @@ import { monkeyPatchBlockSvg } from "./blockSvg"; +import { monkeyPatchConnection } from "./connection"; import { monkeyPatchGesture } from "./gesture"; import { monkeyPatchGrid } from "./grid"; import { monkeyPatchAddKeyMapping } from "./shortcut_registry"; @@ -8,4 +9,5 @@ export function applyMonkeyPatches() { monkeyPatchGrid(); monkeyPatchGesture(); monkeyPatchAddKeyMapping(); + monkeyPatchConnection(); } \ No newline at end of file