|
5 | 5 | */ |
6 | 6 |
|
7 | 7 | import * as Blockly from "blockly/core"; |
| 8 | +import { BlockDragOutside } from "./events_block_drag_outside.js"; |
| 9 | +import { BlockDragEnd } from "./events_block_drag_end.js"; |
| 10 | + |
| 11 | +const BOUNDLESS_CLASS = "boundless"; |
8 | 12 |
|
9 | 13 | class ScratchDragger extends Blockly.dragging.Dragger { |
| 14 | + constructor(draggable, workspace) { |
| 15 | + super(draggable, workspace); |
| 16 | + this.draggedOutOfBounds = false; |
| 17 | + this.originatedFromFlyout = false; |
| 18 | + } |
| 19 | + |
10 | 20 | setDraggable(draggable) { |
11 | 21 | this.draggable = draggable; |
12 | 22 | } |
13 | 23 |
|
| 24 | + onDragStart(event) { |
| 25 | + super.onDragStart(event); |
| 26 | + if (this.draggable instanceof Blockly.BlockSvg) { |
| 27 | + this.workspace.addClass(BOUNDLESS_CLASS); |
| 28 | + const absoluteMetrics = this.workspace |
| 29 | + .getMetricsManager() |
| 30 | + .getAbsoluteMetrics(); |
| 31 | + const viewMetrics = this.workspace.getMetricsManager().getViewMetrics(); |
| 32 | + if ( |
| 33 | + this.workspace.RTL |
| 34 | + ? event.clientX > |
| 35 | + this.workspace.getParentSvg().getBoundingClientRect().left + |
| 36 | + viewMetrics.width |
| 37 | + : event.clientX < absoluteMetrics.left |
| 38 | + ) { |
| 39 | + this.originatedFromFlyout = true; |
| 40 | + } |
| 41 | + } |
| 42 | + } |
| 43 | + |
| 44 | + onDrag(event, totalDelta) { |
| 45 | + super.onDrag(event, totalDelta); |
| 46 | + this.updateOutOfBoundsState(event); |
| 47 | + } |
| 48 | + |
| 49 | + updateOutOfBoundsState(event) { |
| 50 | + if (this.draggable instanceof Blockly.BlockSvg) { |
| 51 | + const outOfBounds = !this.isInsideWorkspace(event); |
| 52 | + if (outOfBounds !== this.draggedOutOfBounds) { |
| 53 | + const event = new BlockDragOutside( |
| 54 | + this.getDragRoot(this.draggable), |
| 55 | + outOfBounds |
| 56 | + ); |
| 57 | + Blockly.Events.fire(event); |
| 58 | + this.draggedOutOfBounds = outOfBounds; |
| 59 | + } |
| 60 | + } |
| 61 | + } |
| 62 | + |
14 | 63 | onDragEnd(event) { |
15 | 64 | if ( |
16 | 65 | this.draggable instanceof Blockly.BlockSvg && |
@@ -30,7 +79,58 @@ class ScratchDragger extends Blockly.dragging.Dragger { |
30 | 79 | return; |
31 | 80 | } |
32 | 81 | } |
| 82 | + |
33 | 83 | super.onDragEnd(event); |
| 84 | + |
| 85 | + this.updateOutOfBoundsState(event); |
| 86 | + if (this.draggable instanceof Blockly.BlockSvg) { |
| 87 | + const event = new BlockDragEnd( |
| 88 | + this.getDragRoot(this.draggable), |
| 89 | + this.draggedOutOfBounds |
| 90 | + ); |
| 91 | + Blockly.Events.fire(event); |
| 92 | + // If this block was dragged out of the flyout and dropped outside of |
| 93 | + // the workspace (e.g. on a different sprite), the block that was created |
| 94 | + // on the workspace in order to depict the block mid-drag needs to be |
| 95 | + // deleted. |
| 96 | + if (this.originatedFromFlyout && this.draggedOutOfBounds) { |
| 97 | + Blockly.renderManagement.finishQueuedRenders().then(() => { |
| 98 | + this.getDragRoot(this.draggable).dispose(); |
| 99 | + }); |
| 100 | + } |
| 101 | + } |
| 102 | + this.workspace.removeClass(BOUNDLESS_CLASS); |
| 103 | + } |
| 104 | + |
| 105 | + shouldReturnToStart(event, rootDraggable) { |
| 106 | + // If a block is dragged out of the workspace to be e.g. dropped on another |
| 107 | + // sprite, it should remain in the same place on the workspace where it was, |
| 108 | + // rather than being moved to an invisible part of the workspace. |
| 109 | + return ( |
| 110 | + this.draggedOutOfBounds || super.shouldReturnToStart(event, rootDraggable) |
| 111 | + ); |
| 112 | + } |
| 113 | + |
| 114 | + getDragRoot(block) { |
| 115 | + // We can't just use getRootBlock() here because, when blocks are detached |
| 116 | + // from a stack via dragging, getRootBlock() still returns the root of that |
| 117 | + // stack. |
| 118 | + if (block.isShadow()) { |
| 119 | + return block.getParent(); |
| 120 | + } |
| 121 | + |
| 122 | + return block; |
| 123 | + } |
| 124 | + |
| 125 | + isInsideWorkspace(event) { |
| 126 | + const bounds = this.workspace.getParentSvg().getBoundingClientRect(); |
| 127 | + const workspaceRect = new Blockly.utils.Rect( |
| 128 | + bounds.top, |
| 129 | + bounds.bottom, |
| 130 | + bounds.left, |
| 131 | + bounds.right |
| 132 | + ); |
| 133 | + return workspaceRect.contains(event.clientX, event.clientY); |
34 | 134 | } |
35 | 135 | } |
36 | 136 |
|
|