Skip to content

Commit fdd7b47

Browse files
feat: pass insertStartPoint into the drag strategy after all
1 parent 678dd98 commit fdd7b47

File tree

4 files changed

+33
-24
lines changed

4 files changed

+33
-24
lines changed

src/actions/enter.ts

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -131,14 +131,9 @@ export class EnterAction {
131131
const stationaryNode = this.navigation.getStationaryNode(workspace);
132132
const newBlock = this.createNewBlock(workspace);
133133
if (!newBlock) return;
134-
if (stationaryNode) {
135-
if (!this.navigation.tryToConnectBlock(stationaryNode, newBlock)) {
136-
console.warn(
137-
'Something went wrong while inserting a block from the flyout.',
138-
);
139-
}
140-
}
141-
134+
const insertStartPoint = stationaryNode
135+
? this.navigation.findInsertStartPoint(stationaryNode, newBlock)
136+
: null;
142137
if (workspace.getTopBlocks().includes(newBlock)) {
143138
this.positionNewTopLevelBlock(workspace, newBlock);
144139
}
@@ -149,7 +144,7 @@ export class EnterAction {
149144
this.navigation.focusWorkspace(workspace);
150145
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
151146
workspace.getCursor()?.setCurNode(ASTNode.createBlockNode(newBlock)!);
152-
this.mover.startMove(workspace, newBlock, true);
147+
this.mover.startMove(workspace, newBlock, insertStartPoint);
153148
}
154149

155150
/**

src/actions/move.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ export class MoveActions {
3636
callback: (workspace) => {
3737
const startBlock = this.getCurrentBlock(workspace);
3838
return (
39-
!!startBlock && this.mover.startMove(workspace, startBlock, false)
39+
!!startBlock && this.mover.startMove(workspace, startBlock, null)
4040
);
4141
},
4242
keyCodes: [KeyCodes.M],
@@ -151,7 +151,7 @@ export class MoveActions {
151151
if (!workspace) return false;
152152
const startBlock = this.getCurrentBlock(workspace);
153153
return (
154-
!!startBlock && this.mover.startMove(workspace, startBlock, false)
154+
!!startBlock && this.mover.startMove(workspace, startBlock, null)
155155
);
156156
},
157157
scopeType: ContextMenuRegistry.ScopeType.BLOCK,

src/actions/mover.ts

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,13 @@
44
* SPDX-License-Identifier: Apache-2.0
55
*/
66

7-
import type {BlockSvg, IDragger, IDragStrategy, Gesture} from 'blockly';
7+
import type {
8+
BlockSvg,
9+
IDragger,
10+
IDragStrategy,
11+
Gesture,
12+
RenderedConnection,
13+
} from 'blockly';
814
import {
915
ASTNode,
1016
Connection,
@@ -107,12 +113,17 @@ export class Mover {
107113
*
108114
* @param workspace The workspace we might be moving on.
109115
* @param block The block to start dragging.
110-
* @param isNewBlock Whether the moving block was created for this action.
116+
* @param insertStartPoint Where to insert the block, or null if the block
117+
* already existed.
111118
* @returns True iff a move has successfully begun.
112119
*/
113-
startMove(workspace: WorkspaceSvg, block: BlockSvg, isNewBlock: boolean) {
120+
startMove(
121+
workspace: WorkspaceSvg,
122+
block: BlockSvg,
123+
insertStartPoint: RenderedConnection | null,
124+
) {
114125
this.patchWorkspace(workspace);
115-
this.patchDragStrategy(block, isNewBlock);
126+
this.patchDragStrategy(block, insertStartPoint);
116127
// Begin dragging block.
117128
const DraggerClass = registry.getClassFromOptions(
118129
registry.Type.BLOCK_DRAGGER,
@@ -286,14 +297,16 @@ export class Mover {
286297
* Monkeypatch: replace the block's drag strategy and cache the old value.
287298
*
288299
* @param block The block to patch.
289-
* @param isNewBlock Whether the moving block was created for this action.
300+
* @param insertStartPoint Where to insert the block, or null if the block
301+
* already existed.
290302
*/
291-
private patchDragStrategy(block: BlockSvg, isNewBlock: boolean) {
303+
private patchDragStrategy(
304+
block: BlockSvg,
305+
insertStartPoint: RenderedConnection | null,
306+
) {
292307
// @ts-expect-error block.dragStrategy is private.
293308
this.oldDragStrategy = block.dragStrategy;
294-
block.setDragStrategy(
295-
new KeyboardDragStrategy(block, this.navigation, isNewBlock),
296-
);
309+
block.setDragStrategy(new KeyboardDragStrategy(block, insertStartPoint));
297310
}
298311

299312
/**

src/keyboard_drag_strategy.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ import {
1515
utils,
1616
} from 'blockly';
1717
import {Direction, getDirectionFromXY} from './drag_direction';
18-
import {Navigation} from './navigation';
1918

2019
// Copied in from core because it is not exported.
2120
interface ConnectionCandidate {
@@ -39,12 +38,14 @@ export class KeyboardDragStrategy extends dragging.BlockDragStrategy {
3938

4039
private cursor: LineCursor | null;
4140

41+
isNewBlock: boolean;
42+
4243
constructor(
4344
private block: BlockSvg,
44-
private navigation: Navigation,
45-
readonly isNewBlock: boolean,
45+
private insertStartPoint: RenderedConnection | null,
4646
) {
4747
super(block);
48+
this.isNewBlock = !!this.insertStartPoint;
4849
this.cursor = block.workspace.getCursor();
4950
}
5051

@@ -279,7 +280,7 @@ export class KeyboardDragStrategy extends dragging.BlockDragStrategy {
279280
*/
280281
private createInitialCandidate(): ConnectionCandidate | null {
281282
// @ts-expect-error startParentConn is private.
282-
const neighbour = this.startParentConn;
283+
const neighbour = this.insertStartPoint ?? this.startParentConn;
283284
if (neighbour) {
284285
this.searchNode = ASTNode.createConnectionNode(neighbour);
285286
switch (neighbour.type) {

0 commit comments

Comments
 (0)