Skip to content

Commit e8b5c40

Browse files
feat: wire isNewBlock into keyboard drag strategy
1 parent 12ca49c commit e8b5c40

File tree

4 files changed

+17
-16
lines changed

4 files changed

+17
-16
lines changed

src/actions/enter.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ export class EnterAction {
149149
this.navigation.focusWorkspace(workspace);
150150
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
151151
workspace.getCursor()?.setCurNode(ASTNode.createBlockNode(newBlock)!);
152-
this.mover.startMove(workspace, newBlock);
152+
this.mover.startMove(workspace, newBlock, true);
153153
}
154154

155155
/**

src/actions/move.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,9 @@ export class MoveActions {
3535
},
3636
callback: (workspace) => {
3737
const startBlock = this.getCurrentBlock(workspace);
38-
return !!startBlock && this.mover.startMove(workspace, startBlock);
38+
return (
39+
!!startBlock && this.mover.startMove(workspace, startBlock, false)
40+
);
3941
},
4042
keyCodes: [KeyCodes.M],
4143
},
@@ -148,7 +150,9 @@ export class MoveActions {
148150
const workspace = scope.block?.workspace as WorkspaceSvg | null;
149151
if (!workspace) return false;
150152
const startBlock = this.getCurrentBlock(workspace);
151-
return !!startBlock && this.mover.startMove(workspace, startBlock);
153+
return (
154+
!!startBlock && this.mover.startMove(workspace, startBlock, false)
155+
);
152156
},
153157
scopeType: ContextMenuRegistry.ScopeType.BLOCK,
154158
id: 'move',

src/actions/mover.ts

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,7 @@
55
*/
66

77
import type {BlockSvg, IDragger, IDragStrategy, Gesture} from 'blockly';
8-
import {
9-
Connection,
10-
registry,
11-
utils,
12-
WorkspaceSvg,
13-
} from 'blockly';
8+
import {Connection, registry, utils, WorkspaceSvg} from 'blockly';
149
import * as Constants from '../constants';
1510
import {Direction, getXYFromDirection} from '../drag_direction';
1611
import {KeyboardDragStrategy} from '../keyboard_drag_strategy';
@@ -93,14 +88,12 @@ export class Mover {
9388
*
9489
* @param workspace The workspace we might be moving on.
9590
* @param block The block to start dragging.
91+
* @param isNewBlock Whether the moving block was created for this action.
9692
* @returns True iff a move has successfully begun.
9793
*/
98-
startMove(workspace: WorkspaceSvg, block: BlockSvg) {
99-
const cursor = workspace?.getCursor();
100-
if (!cursor) throw new Error('precondition failure');
101-
94+
startMove(workspace: WorkspaceSvg, block: BlockSvg, isNewBlock: boolean) {
10295
this.patchWorkspace(workspace);
103-
this.patchDragStrategy(block);
96+
this.patchDragStrategy(block, isNewBlock);
10497
// Begin dragging block.
10598
const DraggerClass = registry.getClassFromOptions(
10699
registry.Type.BLOCK_DRAGGER,
@@ -257,11 +250,14 @@ export class Mover {
257250
* Monkeypatch: replace the block's drag strategy and cache the old value.
258251
*
259252
* @param block The block to patch.
253+
* @param isNewBlock Whether the moving block was created for this action.
260254
*/
261-
private patchDragStrategy(block: BlockSvg) {
255+
private patchDragStrategy(block: BlockSvg, isNewBlock: boolean) {
262256
// @ts-expect-error block.dragStrategy is private.
263257
this.oldDragStrategy = block.dragStrategy;
264-
block.setDragStrategy(new KeyboardDragStrategy(block, this.navigation));
258+
block.setDragStrategy(
259+
new KeyboardDragStrategy(block, this.navigation, isNewBlock),
260+
);
265261
}
266262

267263
/**

src/keyboard_drag_strategy.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ export class KeyboardDragStrategy extends dragging.BlockDragStrategy {
4242
constructor(
4343
private block: BlockSvg,
4444
private navigation: Navigation,
45+
private isNewBlock: boolean,
4546
) {
4647
super(block);
4748
this.cursor = block.workspace.getCursor();

0 commit comments

Comments
 (0)