封鎖拖曳策略
區塊拖曳策略是一種物件,用來決定區塊應處理拖曳的方式。協同程式會實作邏輯,讓區塊成為「可拖曳」。建立新的區塊拖曳策略,即可切換區塊如何處理拖曳作業,而不必建立自訂的可選取項目或處理選取作業。
例如,您可能希望在拖曳時複製區塊,而非正常拖曳。封鎖拖曳策略即可達成此目標。
除了 getRelativeToSurfaceXY
方法之外,拖曳策略使用的方法與 IDraggable
相同。
導入作業
如要建立拖曳策略,您必須實作 IDragStrategy
介面。這需要與 IDraggable
介面相同的所有方法,但區塊已實作的 getRelativeToSurfaceXY
方法除外。
您可以按照實作可拖曳的操作說明實作拖曳策略,但略過實作 getRelativeToSurfaceXY()
。
使用方式
如要使用自訂拖曳策略,您需要將拖曳策略套用至區塊的每個例項。您可以在區塊的 init
方法中呼叫 setDragStrategy
來執行這項操作。
Blockly.Blocks['my_block'] = {
init: function() {
// Other initialization...
this.setDragStrategy(new MyDragStrategy());
// Other initialization...
}
}
除非另有註明,否則本頁面中的內容是採用創用 CC 姓名標示 4.0 授權,程式碼範例則為阿帕契 2.0 授權。詳情請參閱《Google Developers 網站政策》。Java 是 Oracle 和/或其關聯企業的註冊商標。
上次更新時間:2024-04-12 (世界標準時間)。
[null,null,["上次更新時間:2024-04-12 (世界標準時間)。"],[[["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"]]