|
| 1 | +/** |
| 2 | + * @license |
| 3 | + * Copyright 2025 Google LLC |
| 4 | + * SPDX-License-Identifier: Apache-2.0 |
| 5 | + */ |
| 6 | + |
| 7 | +import * as Blockly from 'blockly/core'; |
| 8 | + |
| 9 | +/** |
| 10 | + * Bubble that displays a four-way arrow attached to a block to indicate that |
| 11 | + * it is in move mode. |
| 12 | + */ |
| 13 | +export class MoveIndicatorBubble |
| 14 | + implements Blockly.IBubble, Blockly.IRenderedElement |
| 15 | +{ |
| 16 | + /** |
| 17 | + * Root SVG element for this bubble. |
| 18 | + */ |
| 19 | + svgRoot: SVGGElement; |
| 20 | + |
| 21 | + /** |
| 22 | + * The location of this bubble in workspace coordinates. |
| 23 | + */ |
| 24 | + location = new Blockly.utils.Coordinate(0, 0); |
| 25 | + |
| 26 | + /** |
| 27 | + * Creates a new move indicator bubble. |
| 28 | + * |
| 29 | + * @param sourceBlock The block this bubble should be associated with. |
| 30 | + */ |
| 31 | + constructor(private sourceBlock: Blockly.BlockSvg) { |
| 32 | + this.svgRoot = Blockly.utils.dom.createSvgElement( |
| 33 | + Blockly.utils.Svg.G, |
| 34 | + {}, |
| 35 | + this.sourceBlock.workspace.getBubbleCanvas(), |
| 36 | + ); |
| 37 | + Blockly.utils.dom.createSvgElement( |
| 38 | + Blockly.utils.Svg.CIRCLE, |
| 39 | + { |
| 40 | + 'fill': 'white', |
| 41 | + 'fill-opacity': '0.8', |
| 42 | + 'stroke': 'grey', |
| 43 | + 'stroke-width': '1', |
| 44 | + 'r': 20, |
| 45 | + 'cx': 20, |
| 46 | + 'cy': 20, |
| 47 | + }, |
| 48 | + this.svgRoot, |
| 49 | + ); |
| 50 | + Blockly.utils.dom.createSvgElement( |
| 51 | + Blockly.utils.Svg.PATH, |
| 52 | + { |
| 53 | + 'fill': 'none', |
| 54 | + 'stroke': 'currentColor', |
| 55 | + 'stroke-linecap': 'round', |
| 56 | + 'stroke-linejoin': 'round', |
| 57 | + 'stroke-width': '2', |
| 58 | + 'd': 'm18 9l3 3l-3 3m-3-3h6M6 9l-3 3l3 3m-3-3h6m0 6l3 3l3-3m-3-3v6m3-15l-3-3l-3 3m3-3v6', |
| 59 | + 'transform': 'translate(8 8)', |
| 60 | + }, |
| 61 | + this.svgRoot, |
| 62 | + ); |
| 63 | + |
| 64 | + this.updateLocation(); |
| 65 | + } |
| 66 | + |
| 67 | + /** |
| 68 | + * Returns whether this bubble is movable by the user. |
| 69 | + * |
| 70 | + * @returns Always returns false. |
| 71 | + */ |
| 72 | + isMovable(): boolean { |
| 73 | + return false; |
| 74 | + } |
| 75 | + |
| 76 | + /** |
| 77 | + * Returns the root SVG element for this bubble. |
| 78 | + * |
| 79 | + * @returns The root SVG element. |
| 80 | + */ |
| 81 | + getSvgRoot(): SVGGElement { |
| 82 | + return this.svgRoot; |
| 83 | + } |
| 84 | + |
| 85 | + /** |
| 86 | + * Recalculates this bubble's location, keeping it adjacent to its block. |
| 87 | + */ |
| 88 | + updateLocation() { |
| 89 | + const bounds = this.sourceBlock.getBoundingRectangle(); |
| 90 | + const x = this.sourceBlock.workspace.RTL |
| 91 | + ? bounds.left + 20 |
| 92 | + : bounds.right - 20; |
| 93 | + const y = bounds.top - 20; |
| 94 | + this.moveTo(x, y); |
| 95 | + this.sourceBlock.workspace.getLayerManager()?.moveToDragLayer(this); |
| 96 | + } |
| 97 | + |
| 98 | + /** |
| 99 | + * Moves this bubble to the specified location. |
| 100 | + * |
| 101 | + * @param x The location on the X axis to move to. |
| 102 | + * @param y The location on the Y axis to move to. |
| 103 | + */ |
| 104 | + moveTo(x: number, y: number) { |
| 105 | + this.location.x = x; |
| 106 | + this.location.y = y; |
| 107 | + this.svgRoot.setAttribute('transform', `translate(${x}, ${y})`); |
| 108 | + } |
| 109 | + |
| 110 | + /** |
| 111 | + * Returns this bubble's location in workspace coordinates. |
| 112 | + * |
| 113 | + * @returns The bubble's location. |
| 114 | + */ |
| 115 | + getRelativeToSurfaceXY(): Blockly.utils.Coordinate { |
| 116 | + return this.location; |
| 117 | + } |
| 118 | + |
| 119 | + /** |
| 120 | + * Disposes of this move indicator bubble. |
| 121 | + */ |
| 122 | + dispose() { |
| 123 | + Blockly.utils.dom.removeNode(this.svgRoot); |
| 124 | + } |
| 125 | + |
| 126 | + // These methods are required by the interfaces, but intentionally have no |
| 127 | + // implementation, largely because this bubble's location is fixed relative |
| 128 | + // to its block and is not draggable by the user. |
| 129 | + showContextMenu() {} |
| 130 | + |
| 131 | + setDragging(dragging: boolean) {} |
| 132 | + |
| 133 | + startDrag(event: PointerEvent) {} |
| 134 | + |
| 135 | + drag(newLocation: Blockly.utils.Coordinate, event: PointerEvent) {} |
| 136 | + |
| 137 | + moveDuringDrag(newLocation: Blockly.utils.Coordinate) {} |
| 138 | + |
| 139 | + endDrag() {} |
| 140 | + |
| 141 | + revertDrag() {} |
| 142 | + |
| 143 | + setDeleteStyle(enable: boolean) {} |
| 144 | +} |
0 commit comments