diff --git a/src/navigation.ts b/src/navigation.ts index 7595dd2..9b222f7 100644 --- a/src/navigation.ts +++ b/src/navigation.ts @@ -537,7 +537,10 @@ export class Navigation { * @param workspace The workspace to focus on. * @param keepCursorPosition Whether to retain the cursor's previous position. */ - focusWorkspace(workspace: Blockly.WorkspaceSvg, keepCursorPosition: boolean = false) { + focusWorkspace( + workspace: Blockly.WorkspaceSvg, + keepCursorPosition: boolean = false, + ) { workspace.hideChaff(); const reset = !!workspace.getToolbox(); @@ -554,7 +557,10 @@ export class Navigation { * @param workspace The main Blockly workspace. * @param keepPosition Whether to retain the cursor's previous position. */ - setCursorOnWorkspaceFocus(workspace: Blockly.WorkspaceSvg, keepPosition: boolean) { + setCursorOnWorkspaceFocus( + workspace: Blockly.WorkspaceSvg, + keepPosition: boolean, + ) { const topBlocks = workspace.getTopBlocks(true); const cursor = workspace.getCursor(); if (!cursor) { @@ -608,7 +614,7 @@ export class Navigation { } const markerNode = this.getMarker(workspace)!.getCurNode(); if ( - !this.tryToConnectMarkerAndCursor( + !this.tryToConnectNodes( workspace, markerNode, Blockly.ASTNode.createBlockNode(newBlock)!, @@ -687,57 +693,56 @@ export class Navigation { const cursorNode = workspace.getCursor()!.getCurNode(); if (markerNode && cursorNode) { - return this.tryToConnectMarkerAndCursor( - workspace, - markerNode, - cursorNode, - ); + return this.tryToConnectNodes(workspace, markerNode, cursorNode); } return false; } /** - * Tries to connect the given marker and cursor node. + * Tries to intelligently connect the blocks or connections + * represented by the given nodes, based on node types and locations. * * @param workspace The main workspace. - * @param markerNode The node to try to connect to. - * @param cursorNode The node to connect to the markerNode. + * @param stationaryNode The first node to connect. + * @param movingNode The second node to connect. * @returns True if the key was handled; false if something went * wrong. */ - tryToConnectMarkerAndCursor( + tryToConnectNodes( workspace: Blockly.WorkspaceSvg, - markerNode: Blockly.ASTNode, - cursorNode: Blockly.ASTNode, + stationaryNode: Blockly.ASTNode, + movingNode: Blockly.ASTNode, ): boolean { - if (!this.logConnectionWarning(markerNode, cursorNode)) { + if (!this.logConnectionWarning(stationaryNode, movingNode)) { return false; } - const markerType = markerNode.getType(); - const cursorType = cursorNode.getType(); + const stationaryType = stationaryNode.getType(); + const movingType = movingNode.getType(); - const cursorLoc = cursorNode.getLocation(); - const markerLoc = markerNode.getLocation(); - if (markerNode.isConnection() && cursorNode.isConnection()) { - const cursorConnection = cursorLoc as Blockly.RenderedConnection; - const markerConnection = markerLoc as Blockly.RenderedConnection; - return this.connect(cursorConnection, markerConnection); + const stationaryLoc = stationaryNode.getLocation(); + const movingLoc = movingNode.getLocation(); + if (stationaryNode.isConnection() && movingNode.isConnection()) { + const stationaryAsConnection = + stationaryLoc as Blockly.RenderedConnection; + const movingAsConnection = movingLoc as Blockly.RenderedConnection; + return this.connect(movingAsConnection, stationaryAsConnection); } else if ( - markerNode.isConnection() && - (cursorType == Blockly.ASTNode.types.BLOCK || - cursorType == Blockly.ASTNode.types.STACK) + stationaryNode.isConnection() && + (movingType == Blockly.ASTNode.types.BLOCK || + movingType == Blockly.ASTNode.types.STACK) ) { - const cursorBlock = cursorLoc as Blockly.BlockSvg; - const markerConnection = markerLoc as Blockly.RenderedConnection; - return this.insertBlock(cursorBlock, markerConnection); - } else if (markerType == Blockly.ASTNode.types.WORKSPACE) { - const block = cursorNode - ? (cursorNode.getSourceBlock() as Blockly.BlockSvg) + const stationaryAsConnection = + stationaryLoc as Blockly.RenderedConnection; + const movingAsBlock = movingLoc as Blockly.BlockSvg; + return this.insertBlock(movingAsBlock, stationaryAsConnection); + } else if (stationaryType == Blockly.ASTNode.types.WORKSPACE) { + const block = movingNode + ? (movingNode.getSourceBlock() as Blockly.BlockSvg) : null; - return this.moveBlockToWorkspace(block, markerNode); + return this.moveBlockToWorkspace(block, stationaryNode); } - this.warn('Unexpected state in tryToConnectMarkerAndCursor.'); + this.warn('Unexpected state in tryToConnectNodes.'); return false; } @@ -1098,7 +1103,7 @@ export class Navigation { if (wasVisitingConnection) { const connectionNode = - Blockly.ASTNode.createConnectionNode(superiorConnection); + Blockly.ASTNode.createConnectionNode(superiorConnection); workspace.getCursor()!.setCurNode(connectionNode!); } } @@ -1343,11 +1348,11 @@ export class Navigation { block: Blockly.BlockSvg, ): boolean { let isHandled = false; - const markedNode = workspace.getMarker(this.MARKER_NAME)?.getCurNode(); - if (markedNode) { - isHandled = this.tryToConnectMarkerAndCursor( + const targetNode = workspace.getCursor()?.getCurNode(); + if (targetNode) { + isHandled = this.tryToConnectNodes( workspace, - markedNode, + targetNode, Blockly.ASTNode.createBlockNode(block)!, ); }