From 66d2297f830e561a0c5c8a390b89d771a293b9ff Mon Sep 17 00:00:00 2001 From: Aaron Dodson Date: Wed, 6 Aug 2025 14:02:11 -0700 Subject: [PATCH] fix: make insertion markers reflect the actual size of the block --- src/css.ts | 4 ++ src/index.ts | 1 + src/scratch_insertion_marker_previewer.ts | 45 +++++++++++++++++++++++ 3 files changed, 50 insertions(+) create mode 100644 src/scratch_insertion_marker_previewer.ts diff --git a/src/css.ts b/src/css.ts index b96e17bee3..656d700dbc 100644 --- a/src/css.ts +++ b/src/css.ts @@ -1197,6 +1197,10 @@ const styles = ` stroke: revert-layer; stroke-width: 1; } + + .blocklyInsertionMarker > g:not(:last-child) { + visibility: hidden; + } `; Blockly.Css.register(styles); diff --git a/src/index.ts b/src/index.ts index f86ffef276..b2dc34bd88 100644 --- a/src/index.ts +++ b/src/index.ts @@ -37,6 +37,7 @@ import "./scratch_dragger"; import "./scratch_variable_map"; import "./scratch_variable_model"; import "./scratch_connection_checker"; +import "./scratch_insertion_marker_previewer"; import "./flyout_checkbox_icon"; import "./events/events_block_comment_change"; import "./events/events_block_comment_collapse"; diff --git a/src/scratch_insertion_marker_previewer.ts b/src/scratch_insertion_marker_previewer.ts new file mode 100644 index 0000000000..81e4397f80 --- /dev/null +++ b/src/scratch_insertion_marker_previewer.ts @@ -0,0 +1,45 @@ +/** + * @license + * Copyright 2025 Google LLC + * SPDX-License-Identifier: Apache-2.0 + */ + +import * as Blockly from "blockly/core"; + +/** + * Displays an indicator of where a block being dragged will be connected. + */ +class ScratchInsertionMarkerPreviewer extends Blockly.InsertionMarkerPreviewer { + /** + * Transforms the given block into a JSON representation used to construct an + * insertion marker. + * + * @param block The block to serialize and use as an insertion marker. + * @returns A JSON-formatted string corresponding to a serialized + * representation of the given block suitable for use as an insertion + * marker. + */ + protected override serializeBlockToInsertionMarker(block: Blockly.BlockSvg) { + const blockJson = Blockly.serialization.blocks.save(block, { + addCoordinates: false, + addInputBlocks: true, + addNextBlocks: false, + doFullSerialization: false, + }); + + if (!blockJson) { + throw new Error( + `Failed to serialize source block. ${block.toDevString()}` + ); + } + + return blockJson; + } +} + +Blockly.registry.register( + Blockly.registry.Type.CONNECTION_PREVIEWER, + Blockly.registry.DEFAULT, + ScratchInsertionMarkerPreviewer, + true +);