Skip to content

Commit 12ca49c

Browse files
refactor: set selected and curNode in startDrag
1 parent 8702a25 commit 12ca49c

File tree

2 files changed

+14
-10
lines changed

2 files changed

+14
-10
lines changed

src/actions/mover.ts

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@
66

77
import type {BlockSvg, IDragger, IDragStrategy, Gesture} from 'blockly';
88
import {
9-
ASTNode,
10-
common,
119
Connection,
1210
registry,
1311
utils,
@@ -101,10 +99,6 @@ export class Mover {
10199
const cursor = workspace?.getCursor();
102100
if (!cursor) throw new Error('precondition failure');
103101

104-
// Select and focus block.
105-
common.setSelected(block);
106-
cursor.setCurNode(ASTNode.createBlockNode(block));
107-
108102
this.patchWorkspace(workspace);
109103
this.patchDragStrategy(block);
110104
// Begin dragging block.

src/keyboard_drag_strategy.ts

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,12 @@
55
*/
66

77
import {
8+
common,
89
ASTNode,
910
BlockSvg,
1011
ConnectionType,
1112
RenderedConnection,
13+
LineCursor,
1214
dragging,
1315
utils,
1416
} from 'blockly';
@@ -35,14 +37,21 @@ export class KeyboardDragStrategy extends dragging.BlockDragStrategy {
3537
/** Where a constrained movement should start when traversing the tree. */
3638
private searchNode: ASTNode | null = null;
3739

40+
private cursor: LineCursor | null;
41+
3842
constructor(
3943
private block: BlockSvg,
4044
private navigation: Navigation,
4145
) {
4246
super(block);
47+
this.cursor = block.workspace.getCursor();
4348
}
4449

4550
override startDrag(e?: PointerEvent) {
51+
if (!this.cursor) throw new Error('precondition failure');
52+
// Select and focus block.
53+
common.setSelected(this.block);
54+
this.cursor.setCurNode(ASTNode.createBlockNode(this.block));
4655
super.startDrag(e);
4756
// Set position of the dragging block, so that it doesn't pop
4857
// to the top left of the workspace.
@@ -148,8 +157,9 @@ export class KeyboardDragStrategy extends dragging.BlockDragStrategy {
148157
draggingBlock: BlockSvg,
149158
localConns: RenderedConnection[],
150159
): ConnectionCandidate | null {
151-
const cursor = draggingBlock.workspace.getCursor();
152-
if (!cursor) return null;
160+
// TODO: Handle the case where the cursor is null, or never return null
161+
// from workspace.getCursor()
162+
if (!this.cursor) throw new Error('precondition failure');
153163

154164
// Helper function for traversal.
155165
function isConnection(node: ASTNode | null): boolean {
@@ -162,9 +172,9 @@ export class KeyboardDragStrategy extends dragging.BlockDragStrategy {
162172
const dir = this.currentDragDirection;
163173
while (potential && !candidateConnection) {
164174
if (dir === Direction.Up || dir === Direction.Left) {
165-
potential = cursor.getPreviousNode(potential, isConnection, true);
175+
potential = this.cursor.getPreviousNode(potential, isConnection, true);
166176
} else if (dir === Direction.Down || dir === Direction.Right) {
167-
potential = cursor.getNextNode(potential, isConnection, true);
177+
potential = this.cursor.getNextNode(potential, isConnection, true);
168178
}
169179

170180
localConns.forEach((conn: RenderedConnection) => {

0 commit comments

Comments
 (0)