Block drag strategy
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 2024-09-18 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 2024-09-18 UTC."],[[["Block drag strategies dictate how blocks respond to drag events, offering an alternative to custom selectables."],["By implementing the `IDragStrategy` interface, developers can create custom drag behaviors like block duplication on drag."],["Drag strategies encompass the functionality of `IDraggable`, excluding the `getRelativeToSurfaceXY` method, which is handled internally by the block."],["Applying a custom drag strategy requires using `setDragStrategy` within the block's `init` method to associate the strategy with each block instance."]]],["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"]]