自訂區塊拖曳策略
透過集合功能整理內容
你可以依據偏好儲存及分類內容。
區塊拖曳策略是用來決定區塊如何處理拖曳動作的物件。實作邏輯,讓區塊可拖曳。建立新的拖曳區塊策略,可讓您切換區塊處理拖曳的方式,而無須建立自訂可選取或處理選取項目。
舉例來說,你可能想在拖曳時複製區塊,而非
請照常拖曳您可以使用拖曳阻斷策略來實現這一點。
拖曳策略的方法與 IDraggable
相同,
除了 getRelativeToSurfaceXY
方法之外。
導入作業
如要建立拖曳策略,您必須實作 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 和/或其關聯企業的註冊商標。
上次更新時間:2025-07-25 (世界標準時間)。
[null,null,["上次更新時間:2025-07-25 (世界標準時間)。"],[],["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,[]]