Skip to content

Commit

Permalink
fix: paste at active cursor location instead of at marker location (#167
Browse files Browse the repository at this point in the history
)

* 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
  • Loading branch information
rachel-fenichel authored Jan 26, 2025
1 parent fe0bdf2 commit 05f024d
Showing 1 changed file with 44 additions and 39 deletions.
83 changes: 44 additions & 39 deletions src/navigation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand All @@ -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) {
Expand Down Expand Up @@ -608,7 +614,7 @@ export class Navigation {
}
const markerNode = this.getMarker(workspace)!.getCurNode();
if (
!this.tryToConnectMarkerAndCursor(
!this.tryToConnectNodes(
workspace,
markerNode,
Blockly.ASTNode.createBlockNode(newBlock)!,
Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -1098,7 +1103,7 @@ export class Navigation {

if (wasVisitingConnection) {
const connectionNode =
Blockly.ASTNode.createConnectionNode(superiorConnection);
Blockly.ASTNode.createConnectionNode(superiorConnection);
workspace.getCursor()!.setCurNode(connectionNode!);
}
}
Expand Down Expand Up @@ -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)!,
);
}
Expand Down

0 comments on commit 05f024d

Please sign in to comment.