-
Notifications
You must be signed in to change notification settings - Fork 13
feat: Introduce Mover actions + experimental DragMover implementation
#317
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
cda9d83
feat: Introduce Mover class, moving mode
cpcallen 9b2b06e
feat: Save block starting location and connections
cpcallen 746685c
feat: Experiment: unconstrained movement using Dragger
cpcallen 7fd74a4
feat: Add Move Block (M) context menu item
cpcallen b83bcf3
fix: Allow ctrl+arrows to also perform unconstrained moves
cpcallen b24abf3
fix: Use better fake PointerEvents to prevent random deletions
cpcallen File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,201 @@ | ||
| /** | ||
| * @license | ||
| * Copyright 2025 Google LLC | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| import * as Constants from '../constants'; | ||
| import { | ||
| ASTNode, | ||
| Connection, | ||
| ShortcutRegistry, | ||
| WorkspaceSvg, | ||
| common, | ||
| registry, | ||
| utils, | ||
| } from 'blockly'; | ||
| import type {Block, BlockSvg, IDragger} from 'blockly'; | ||
| import {Mover, MoveInfo} from './mover'; | ||
| import {Navigation} from '../navigation'; | ||
|
|
||
| /** | ||
| * An experimental implementation of Mover that uses a dragger to | ||
| * perform unconstraind movment. | ||
| */ | ||
| export class DragMover extends Mover { | ||
| /** | ||
| * The distance to move an item, in workspace coordinates, when | ||
| * making an unconstrained move. | ||
| */ | ||
| UNCONSTRAINED_MOVE_DISTANCE = 20; | ||
|
|
||
| /** | ||
| * Map of moves in progress. | ||
| * | ||
| * An entry for a given workspace in this map means that the this | ||
| * Mover is moving a block on that workspace, and will disable | ||
| * normal cursor movement until the move is complete. | ||
| */ | ||
| protected declare moves: Map<WorkspaceSvg, DragMoveInfo>; | ||
|
|
||
| /** | ||
| * Start moving the currently-focused item on workspace, if | ||
| * possible. | ||
| * | ||
| * Should only be called if canMove has returned true. | ||
| * | ||
| * @param workspace The workspace we might be moving on. | ||
| * @returns True iff a move has successfully begun. | ||
| */ | ||
| override startMove(workspace: WorkspaceSvg) { | ||
| const cursor = workspace?.getCursor(); | ||
| const curNode = cursor?.getCurNode(); | ||
| const block = curNode?.getSourceBlock() as BlockSvg | null; | ||
| if (!cursor || !block) throw new Error('precondition failure'); | ||
|
|
||
| // Select and focus block. | ||
| common.setSelected(block); | ||
| cursor.setCurNode(ASTNode.createBlockNode(block)!); | ||
| // Begin dragging block. | ||
| const DraggerClass = registry.getClassFromOptions( | ||
| registry.Type.BLOCK_DRAGGER, | ||
| workspace.options, | ||
| true, | ||
| ); | ||
| if (!DraggerClass) throw new Error('no Dragger registered'); | ||
| const dragger = new DraggerClass(block, workspace); | ||
| // Record that a move is in progress and start dragging. | ||
| const info = new DragMoveInfo(block, dragger); | ||
| this.moves.set(workspace, info); | ||
| // Begin drag. | ||
| dragger.onDragStart(info.fakePointerEvent('pointerdown')); | ||
| return true; | ||
| } | ||
|
|
||
| /** | ||
| * Finish moving the currently-focused item on workspace. | ||
| * | ||
| * Should only be called if isMoving has returned true. | ||
| * | ||
| * @param workspace The workspace on which we are moving. | ||
| * @returns True iff move successfully finished. | ||
| */ | ||
| override finishMove(workspace: WorkspaceSvg) { | ||
| if (!workspace) return false; | ||
| const info = this.moves.get(workspace); | ||
| if (!info) throw new Error('no move info for workspace'); | ||
|
|
||
| info.dragger.onDragEnd( | ||
| info.fakePointerEvent('pointerup'), | ||
| new utils.Coordinate(0, 0), | ||
| ); | ||
|
|
||
| this.moves.delete(workspace); | ||
| return true; | ||
| } | ||
|
|
||
| /** | ||
| * Abort moving the currently-focused item on workspace. | ||
| * | ||
| * Should only be called if isMoving has returned true. | ||
| * | ||
| * @param workspace The workspace on which we are moving. | ||
| * @returns True iff move successfully aborted. | ||
| */ | ||
| override abortMove(workspace: WorkspaceSvg) { | ||
| if (!workspace) return false; | ||
| const info = this.moves.get(workspace); | ||
| if (!info) throw new Error('no move info for workspace'); | ||
|
|
||
| // Monkey patch dragger to trigger call to draggable.revertDrag. | ||
| (info.dragger as any).shouldReturnToStart = () => true; | ||
| info.dragger.onDragEnd( | ||
| info.fakePointerEvent('pointerup'), | ||
| new utils.Coordinate(0, 0), | ||
| ); | ||
|
|
||
| this.moves.delete(workspace); | ||
| return true; | ||
| } | ||
|
|
||
| /** | ||
| * Action to move the item being moved in the given direction, | ||
| * constrained to valid attachment points (if any). | ||
| * | ||
| * @param workspace The workspace to move on. | ||
| * @returns True iff this action applies and has been performed. | ||
| */ | ||
| override moveConstrained( | ||
| workspace: WorkspaceSvg, | ||
| /* ... */ | ||
| ) { | ||
| // Not yet implemented. Absorb keystroke to avoid moving cursor. | ||
| alert(`Constrained movement not implemented. | ||
|
|
||
| Use ctrl+arrow or alt+arrow (option+arrow on macOS) for unconstrained move. | ||
| Use enter to complete the move, or escape to abort.`); | ||
| return true; | ||
| } | ||
|
|
||
| /** | ||
| * Action to move the item being moved in the given direction, | ||
| * without constraint. | ||
| * | ||
| * @param workspace The workspace to move on. | ||
| * @param xDirection -1 to move left. 1 to move right. | ||
| * @param yDirection -1 to move up. 1 to move down. | ||
| * @returns True iff this action applies and has been performed. | ||
| */ | ||
| override moveUnconstrained( | ||
| workspace: WorkspaceSvg, | ||
| xDirection: number, | ||
| yDirection: number, | ||
| ): boolean { | ||
| if (!workspace) return false; | ||
| const info = this.moves.get(workspace); | ||
| if (!info) throw new Error('no move info for workspace'); | ||
|
|
||
| info.totalDelta.x += | ||
| xDirection * this.UNCONSTRAINED_MOVE_DISTANCE * workspace.scale; | ||
| info.totalDelta.y += | ||
| yDirection * this.UNCONSTRAINED_MOVE_DISTANCE * workspace.scale; | ||
|
|
||
| info.dragger.onDrag(info.fakePointerEvent('pointermove'), info.totalDelta); | ||
| return true; | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Information about the currently in-progress move for a given | ||
| * Workspace. | ||
| */ | ||
| class DragMoveInfo extends MoveInfo { | ||
| /** Total distance moved, in screen pixels */ | ||
| totalDelta = new utils.Coordinate(0, 0); | ||
|
|
||
| constructor( | ||
| public readonly block: Block, | ||
| public readonly dragger: IDragger, | ||
| ) { | ||
| super(block); | ||
| } | ||
|
|
||
| /** Create fake pointer event for dragging. */ | ||
| fakePointerEvent(type: string): PointerEvent { | ||
| const workspace = this.block.workspace; | ||
| if (!(workspace instanceof WorkspaceSvg)) throw new TypeError(); | ||
|
|
||
| const blockCoords = utils.svgMath.wsToScreenCoordinates( | ||
| workspace, | ||
| new utils.Coordinate( | ||
| this.startLocation.x + this.totalDelta.x, | ||
| this.startLocation.y + this.totalDelta.y, | ||
| ), | ||
| ); | ||
| return new PointerEvent(type, { | ||
| clientX: blockCoords.x, | ||
| clientY: blockCoords.y, | ||
| }); | ||
| } | ||
|
|
||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As in the base class, either remove the null check or make the arg nullable here and in
abortMove