Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 27 additions & 7 deletions packages/blockly/core/dragging/block_drag_strategy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import {Coordinate} from '../utils.js';
import * as aria from '../utils/aria.js';
import * as dom from '../utils/dom.js';
import {Rect} from '../utils/rect.js';
import * as svgMath from '../utils/svg_math.js';
import type {WorkspaceSvg} from '../workspace_svg.js';

/** Represents a valid pair of connections between the dragging block and a block on the workspace. */
Expand Down Expand Up @@ -117,9 +118,21 @@ export class BlockDragStrategy implements IDragStrategy {
/**
* Positions a cloned block on its new workspace.
*
* @param oldBlock The flyout block that was cloned.
* @param newBlock The new block to position.
*/
private positionNewBlock(newBlock: BlockSvg) {
private positionNewBlock(oldBlock: BlockSvg, newBlock: BlockSvg) {
const screenCoordinate = svgMath.wsToScreenCoordinates(
oldBlock.workspace,
oldBlock.getRelativeToSurfaceXY(),
);
const workspaceCoordinates = svgMath.screenToWsCoordinates(
newBlock.workspace,
screenCoordinate,
);
newBlock.moveDuringDrag(workspaceCoordinates);

if (this.moveMode !== MoveMode.CONSTRAINED) return;
const workspace = newBlock.workspace;
const initialY = this.WORKSPACE_MARGIN;
const initialX = this.WORKSPACE_MARGIN;
Expand Down Expand Up @@ -223,7 +236,7 @@ export class BlockDragStrategy implements IDragStrategy {
},
) as BlockSvg;
eventUtils.setRecordUndo(false);
this.positionNewBlock(newBlock);
this.positionNewBlock(this.block, newBlock);
eventUtils.setRecordUndo(true);

return newBlock;
Expand Down Expand Up @@ -290,6 +303,7 @@ export class BlockDragStrategy implements IDragStrategy {
* from any parent blocks.
*/
startDrag(e?: PointerEvent | KeyboardEvent) {
this.updateMoveMode(e);
const alternateTarget = this.getTargetBlock();
if (alternateTarget !== this.block) {
return alternateTarget.startDrag(e);
Expand Down Expand Up @@ -549,11 +563,7 @@ export class BlockDragStrategy implements IDragStrategy {

/** Moves the block and updates any connection previews. */
drag(newLoc: Coordinate, e?: PointerEvent | KeyboardEvent): void {
this.moveMode =
e instanceof KeyboardEvent && !(e.ctrlKey || e.metaKey)
? MoveMode.CONSTRAINED
: MoveMode.UNCONSTRAINED;

this.updateMoveMode(e);
if (this.moveMode === MoveMode.UNCONSTRAINED) {
this.block.moveDuringDrag(newLoc);
}
Expand Down Expand Up @@ -1216,4 +1226,14 @@ export class BlockDragStrategy implements IDragStrategy {

return null;
}

/**
* Updates the current move mode based on the most recent drag-related event.
*/
private updateMoveMode(event: KeyboardEvent | PointerEvent | undefined) {
this.moveMode =
event instanceof KeyboardEvent && !(event.ctrlKey || event.metaKey)
? MoveMode.CONSTRAINED
: MoveMode.UNCONSTRAINED;
}
}
2 changes: 1 addition & 1 deletion packages/blockly/tests/mocha/keyboard_movement_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1262,7 +1262,7 @@ suite('Keyboard-driven movement', function () {
this.clock.tick(10);
this.moveAndAssert(
moveRight,
['Moving', 'else if, do', 'around', 'draw', '❤️'],
['Moving', 'else', 'around', 'draw', '❤️'],
['of'],
);
this.moveAndAssert(
Expand Down
Loading