Skip to content

Commit

Permalink
feat: contextmenu action to insert above a block (#171)
Browse files Browse the repository at this point in the history
* feat: first pass at inserting above a block from the context menu

* chore: format

* feat: update tryToConnectNodes to handle two block nodes

* chore: update insert action text
  • Loading branch information
rachel-fenichel authored Feb 7, 2025
1 parent d9b13a9 commit 3ab05d3
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 22 deletions.
70 changes: 48 additions & 22 deletions src/navigation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -715,25 +715,44 @@ export class Navigation {

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 (
stationaryNode.isConnection() &&
(movingType == Blockly.ASTNode.types.BLOCK ||
movingType == Blockly.ASTNode.types.STACK)
) {
const stationaryAsConnection =
stationaryLoc as Blockly.RenderedConnection;
const movingAsBlock = movingLoc as Blockly.BlockSvg;
return this.insertBlock(movingAsBlock, stationaryAsConnection);

if (stationaryNode.isConnection()) {
if (movingNode.isConnection()) {
const stationaryAsConnection =
stationaryLoc as Blockly.RenderedConnection;
const movingAsConnection = movingLoc as Blockly.RenderedConnection;
return this.connect(movingAsConnection, stationaryAsConnection);
}
// Connect the moving block to the stationary connection using
// the most plausible connection on the moving block.
if (
movingType == Blockly.ASTNode.types.BLOCK ||
movingType == Blockly.ASTNode.types.STACK
) {
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, stationaryNode);
} else if (
stationaryType == Blockly.ASTNode.types.BLOCK &&
movingType == Blockly.ASTNode.types.BLOCK
) {
// Insert the moving block above the stationary block, if the
// appropriate connections exist.
const stationaryBlock = stationaryLoc as Blockly.BlockSvg;
const movingBlock = movingLoc as Blockly.BlockSvg;
if (stationaryBlock.previousConnection) {
return this.insertBlock(
movingBlock,
stationaryBlock.previousConnection,
);
}
}
this.warn('Unexpected state in tryToConnectNodes.');
return false;
Expand Down Expand Up @@ -767,9 +786,6 @@ export class Navigation {
if (markerType == Blockly.ASTNode.types.FIELD) {
this.warn('Should not have been able to mark a field.');
return false;
} else if (markerType == Blockly.ASTNode.types.BLOCK) {
this.warn('Should not have been able to mark a block.');
return false;
} else if (markerType == Blockly.ASTNode.types.STACK) {
this.warn('Should not have been able to mark a stack.');
return false;
Expand Down Expand Up @@ -1257,16 +1273,26 @@ export class Navigation {
curNode.isConnection() ||
nodeType == Blockly.ASTNode.types.WORKSPACE
) {
if (workspace.getToolbox()) {
this.focusToolbox(workspace);
} else {
this.focusFlyout(workspace);
}
this.openToolboxOrFlyout(workspace);
} else if (nodeType == Blockly.ASTNode.types.STACK) {
this.warn('Cannot mark a stack.');
}
}

/**
* Save the current cursor location and open the toolbox or flyout
* to select and insert a block.
* @param workspace The active workspace.
*/
openToolboxOrFlyout(workspace: Blockly.WorkspaceSvg) {
this.markAtCursor(workspace);
if (workspace.getToolbox()) {
this.focusToolbox(workspace);
} else {
this.focusFlyout(workspace);
}
}

/**
* Show the action menu for a given node.
*
Expand Down
32 changes: 32 additions & 0 deletions src/navigation_controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -978,6 +978,37 @@ export class NavigationController {
ContextMenuRegistry.registry.register(copyAction);
}

/**
* Register the action for inserting above a block.
*/
protected registerInsertAction() {
const insertAboveAction: ContextMenuRegistry.RegistryItem = {
displayText: (scope) => 'Insert block above',
preconditionFn: (scope) => {
const ws = scope.block?.workspace;
if (!ws) return 'hidden';

if (!scope.block?.previousConnection) return 'hidden';

return this.canCurrentlyEdit(ws) ? 'enabled' : 'hidden';
},
callback: (scope) => {
const ws = scope.block?.workspace;
if (!ws) return false;

if (this.navigation.getState(ws) == Constants.STATE.WORKSPACE) {
this.navigation.openToolboxOrFlyout(ws);
return true;
}
return false;
},
scopeType: ContextMenuRegistry.ScopeType.BLOCK,
id: 'blockInsertAbove',
weight: 12,
};
ContextMenuRegistry.registry.register(insertAboveAction);
}

/**
* Registers all default keyboard shortcut items for keyboard
* navigation. This should be called once per instance of
Expand All @@ -990,6 +1021,7 @@ export class NavigationController {

this.registerDeleteAction();
this.registerCopyAction();
this.registerInsertAction();

// Initalise the shortcut modal with available shortcuts. Needs
// to be done separately rather at construction, as many shortcuts
Expand Down

0 comments on commit 3ab05d3

Please sign in to comment.