Skip to content

Commit 05f024d

Browse files
fix: paste at active cursor location instead of at marker location (#167)
* chore: rename tryToConnectMarkerAndCursor to tryToConnectNodes * fix: insert pasted block at cursor location instead of marker location * chore: rename markedNode to targetNode * chore: rename parameters in tryToConnectNodes
1 parent fe0bdf2 commit 05f024d

File tree

1 file changed

+44
-39
lines changed

1 file changed

+44
-39
lines changed

src/navigation.ts

Lines changed: 44 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -537,7 +537,10 @@ export class Navigation {
537537
* @param workspace The workspace to focus on.
538538
* @param keepCursorPosition Whether to retain the cursor's previous position.
539539
*/
540-
focusWorkspace(workspace: Blockly.WorkspaceSvg, keepCursorPosition: boolean = false) {
540+
focusWorkspace(
541+
workspace: Blockly.WorkspaceSvg,
542+
keepCursorPosition: boolean = false,
543+
) {
541544
workspace.hideChaff();
542545
const reset = !!workspace.getToolbox();
543546

@@ -554,7 +557,10 @@ export class Navigation {
554557
* @param workspace The main Blockly workspace.
555558
* @param keepPosition Whether to retain the cursor's previous position.
556559
*/
557-
setCursorOnWorkspaceFocus(workspace: Blockly.WorkspaceSvg, keepPosition: boolean) {
560+
setCursorOnWorkspaceFocus(
561+
workspace: Blockly.WorkspaceSvg,
562+
keepPosition: boolean,
563+
) {
558564
const topBlocks = workspace.getTopBlocks(true);
559565
const cursor = workspace.getCursor();
560566
if (!cursor) {
@@ -608,7 +614,7 @@ export class Navigation {
608614
}
609615
const markerNode = this.getMarker(workspace)!.getCurNode();
610616
if (
611-
!this.tryToConnectMarkerAndCursor(
617+
!this.tryToConnectNodes(
612618
workspace,
613619
markerNode,
614620
Blockly.ASTNode.createBlockNode(newBlock)!,
@@ -687,57 +693,56 @@ export class Navigation {
687693
const cursorNode = workspace.getCursor()!.getCurNode();
688694

689695
if (markerNode && cursorNode) {
690-
return this.tryToConnectMarkerAndCursor(
691-
workspace,
692-
markerNode,
693-
cursorNode,
694-
);
696+
return this.tryToConnectNodes(workspace, markerNode, cursorNode);
695697
}
696698
return false;
697699
}
698700

699701
/**
700-
* Tries to connect the given marker and cursor node.
702+
* Tries to intelligently connect the blocks or connections
703+
* represented by the given nodes, based on node types and locations.
701704
*
702705
* @param workspace The main workspace.
703-
* @param markerNode The node to try to connect to.
704-
* @param cursorNode The node to connect to the markerNode.
706+
* @param stationaryNode The first node to connect.
707+
* @param movingNode The second node to connect.
705708
* @returns True if the key was handled; false if something went
706709
* wrong.
707710
*/
708-
tryToConnectMarkerAndCursor(
711+
tryToConnectNodes(
709712
workspace: Blockly.WorkspaceSvg,
710-
markerNode: Blockly.ASTNode,
711-
cursorNode: Blockly.ASTNode,
713+
stationaryNode: Blockly.ASTNode,
714+
movingNode: Blockly.ASTNode,
712715
): boolean {
713-
if (!this.logConnectionWarning(markerNode, cursorNode)) {
716+
if (!this.logConnectionWarning(stationaryNode, movingNode)) {
714717
return false;
715718
}
716719

717-
const markerType = markerNode.getType();
718-
const cursorType = cursorNode.getType();
720+
const stationaryType = stationaryNode.getType();
721+
const movingType = movingNode.getType();
719722

720-
const cursorLoc = cursorNode.getLocation();
721-
const markerLoc = markerNode.getLocation();
722-
if (markerNode.isConnection() && cursorNode.isConnection()) {
723-
const cursorConnection = cursorLoc as Blockly.RenderedConnection;
724-
const markerConnection = markerLoc as Blockly.RenderedConnection;
725-
return this.connect(cursorConnection, markerConnection);
723+
const stationaryLoc = stationaryNode.getLocation();
724+
const movingLoc = movingNode.getLocation();
725+
if (stationaryNode.isConnection() && movingNode.isConnection()) {
726+
const stationaryAsConnection =
727+
stationaryLoc as Blockly.RenderedConnection;
728+
const movingAsConnection = movingLoc as Blockly.RenderedConnection;
729+
return this.connect(movingAsConnection, stationaryAsConnection);
726730
} else if (
727-
markerNode.isConnection() &&
728-
(cursorType == Blockly.ASTNode.types.BLOCK ||
729-
cursorType == Blockly.ASTNode.types.STACK)
731+
stationaryNode.isConnection() &&
732+
(movingType == Blockly.ASTNode.types.BLOCK ||
733+
movingType == Blockly.ASTNode.types.STACK)
730734
) {
731-
const cursorBlock = cursorLoc as Blockly.BlockSvg;
732-
const markerConnection = markerLoc as Blockly.RenderedConnection;
733-
return this.insertBlock(cursorBlock, markerConnection);
734-
} else if (markerType == Blockly.ASTNode.types.WORKSPACE) {
735-
const block = cursorNode
736-
? (cursorNode.getSourceBlock() as Blockly.BlockSvg)
735+
const stationaryAsConnection =
736+
stationaryLoc as Blockly.RenderedConnection;
737+
const movingAsBlock = movingLoc as Blockly.BlockSvg;
738+
return this.insertBlock(movingAsBlock, stationaryAsConnection);
739+
} else if (stationaryType == Blockly.ASTNode.types.WORKSPACE) {
740+
const block = movingNode
741+
? (movingNode.getSourceBlock() as Blockly.BlockSvg)
737742
: null;
738-
return this.moveBlockToWorkspace(block, markerNode);
743+
return this.moveBlockToWorkspace(block, stationaryNode);
739744
}
740-
this.warn('Unexpected state in tryToConnectMarkerAndCursor.');
745+
this.warn('Unexpected state in tryToConnectNodes.');
741746
return false;
742747
}
743748

@@ -1098,7 +1103,7 @@ export class Navigation {
10981103

10991104
if (wasVisitingConnection) {
11001105
const connectionNode =
1101-
Blockly.ASTNode.createConnectionNode(superiorConnection);
1106+
Blockly.ASTNode.createConnectionNode(superiorConnection);
11021107
workspace.getCursor()!.setCurNode(connectionNode!);
11031108
}
11041109
}
@@ -1343,11 +1348,11 @@ export class Navigation {
13431348
block: Blockly.BlockSvg,
13441349
): boolean {
13451350
let isHandled = false;
1346-
const markedNode = workspace.getMarker(this.MARKER_NAME)?.getCurNode();
1347-
if (markedNode) {
1348-
isHandled = this.tryToConnectMarkerAndCursor(
1351+
const targetNode = workspace.getCursor()?.getCurNode();
1352+
if (targetNode) {
1353+
isHandled = this.tryToConnectNodes(
13491354
workspace,
1350-
markedNode,
1355+
targetNode,
13511356
Blockly.ASTNode.createBlockNode(block)!,
13521357
);
13531358
}

0 commit comments

Comments
 (0)