Custom block drag strategies
Stay organized with collections
Save and categorize content based on your preferences.
A block drag strategy is an object that determines how a block should handle
drags. They implement the logic to make a block a draggable.
Creating new block drag strategies lets you switch out how blocks should handle
drags without having to deal with creating a custom selectable, or handling
selection.
For example, you might want your block to duplicate on drag, rather than
dragging normally. Block drag strategies allow you to do that.
Drag strategies have all the same methods as an IDraggable
,
besides the getRelativeToSurfaceXY
method.
Implementation
To create a drag strategy, you need to implement the
IDragStrategy
interface. This requires all the same methods
as the IDraggable
interface, except for the getRelativeToSurfaceXY
method,
which the block already implements.
You can follow the
instructions for implementing a draggable to
implement your drag strategy, but skip implementing getRelativeToSurfaceXY()
.
Usage
To use a custom drag strategy, you need to apply the drag strategy to each
instance of a block. You can do this in the init
method of your block, by
calling setDragStrategy
.
Blockly.Blocks['my_block'] = {
init: function() {
// Other initialization...
this.setDragStrategy(new MyDragStrategy());
// Other initialization...
}
}
Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.
Last updated 2025-05-23 UTC.
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Missing the information I need","missingTheInformationINeed","thumb-down"],["Too complicated / too many steps","tooComplicatedTooManySteps","thumb-down"],["Out of date","outOfDate","thumb-down"],["Samples / code issue","samplesCodeIssue","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2025-05-23 UTC."],[[["\u003cp\u003eBlock drag strategies dictate how blocks respond to drag events, offering an alternative to custom selectables.\u003c/p\u003e\n"],["\u003cp\u003eBy implementing the \u003ccode\u003eIDragStrategy\u003c/code\u003e interface, developers can create custom drag behaviors like block duplication on drag.\u003c/p\u003e\n"],["\u003cp\u003eDrag strategies encompass the functionality of \u003ccode\u003eIDraggable\u003c/code\u003e, excluding the \u003ccode\u003egetRelativeToSurfaceXY\u003c/code\u003e method, which is handled internally by the block.\u003c/p\u003e\n"],["\u003cp\u003eApplying a custom drag strategy requires using \u003ccode\u003esetDragStrategy\u003c/code\u003e within the block's \u003ccode\u003einit\u003c/code\u003e method to associate the strategy with each block instance.\u003c/p\u003e\n"]]],["Block drag strategies dictate how a block behaves during drags, allowing for custom drag behaviors like duplication. Developers implement the `IDragStrategy` interface, which mirrors `IDraggable` methods except for `getRelativeToSurfaceXY`. To apply a custom strategy, use the `setDragStrategy` method within a block's `init` method. This enables switching block drag behavior without altering core selection or drag handling. You must implement all the methods of `IDraggable` in the custom strategy except for `getRelativeToSurfaceXY`.\n"],null,["# Custom block drag strategies\n\nA block drag strategy is an object that determines how a block should handle\ndrags. They implement the logic to make a block a [draggable](/blockly/guides/configure/web/dragging/draggable).\nCreating new block drag strategies lets you switch out how blocks should handle\ndrags without having to deal with creating a custom selectable, or handling\nselection.\n\nFor example, you might want your block to duplicate on drag, rather than\ndragging normally. Block drag strategies allow you to do that.\n\nDrag strategies have all the same methods as an [`IDraggable`](/blockly/reference/js/blockly.idraggable_interface),\nbesides the `getRelativeToSurfaceXY` method.\n\nImplementation\n--------------\n\nTo create a drag strategy, you need to implement the\n[`IDragStrategy`](/blockly/reference/js/blockly.idragstrategy_interface) interface. This requires all the same methods\nas the `IDraggable` interface, except for the `getRelativeToSurfaceXY` method,\nwhich the block already implements.\n\nYou can follow the\n[instructions for implementing a draggable](/blockly/guides/configure/web/dragging/draggable#implementation) to\nimplement your drag strategy, but skip implementing `getRelativeToSurfaceXY()`.\n\nUsage\n-----\n\nTo use a custom drag strategy, you need to apply the drag strategy to each\n*instance* of a block. You can do this in the `init` method of your block, by\ncalling [`setDragStrategy`](/blockly/reference/js/blockly.blocksvg_class.setdragstrategy_1_method). \n\n Blockly.Blocks['my_block'] = {\n init: function() {\n // Other initialization...\n this.setDragStrategy(new MyDragStrategy());\n // Other initialization...\n }\n }"]]