44 * SPDX-License-Identifier: Apache-2.0
55 */
66
7- import { ShortcutRegistry , WorkspaceSvg , utils } from 'blockly/core' ;
7+ import {
8+ ShortcutRegistry ,
9+ WorkspaceSvg ,
10+ BlockSvg ,
11+ navigateStacks ,
12+ isSelectable ,
13+ utils ,
14+ } from 'blockly/core' ;
815import * as Constants from '../constants' ;
916
1017/**
@@ -16,26 +23,13 @@ export class StackNavigationAction {
1623
1724 install ( ) {
1825 const preconditionFn = ( workspace : WorkspaceSvg ) =>
19- ! ! getCurNodeRoot ( workspace ) ;
20-
21- function getCurNodeRoot ( workspace : WorkspaceSvg ) {
22- const cursor = workspace . getCursor ( ) ;
23- // The fallback case includes workspace comments.
24- return cursor . getSourceBlock ( ) ?. getRootBlock ( ) ?? cursor . getCurNode ( ) ;
25- }
26+ ! ! this . getCurNodeRoot ( workspace ) ;
2627
2728 const previousStackShortcut : ShortcutRegistry . KeyboardShortcut = {
2829 name : Constants . SHORTCUT_NAMES . PREVIOUS_STACK ,
2930 preconditionFn,
3031 callback : ( workspace ) => {
31- const curNodeRoot = getCurNodeRoot ( workspace ) ;
32- if ( ! curNodeRoot ) return false ;
33- const prevRoot = workspace
34- . getNavigator ( )
35- . getPreviousSibling ( curNodeRoot ) ;
36- if ( ! prevRoot ) return false ;
37- workspace . getCursor ( ) . setCurNode ( prevRoot ) ;
38- return true ;
32+ return this . navigate ( workspace , - 1 ) ;
3933 } ,
4034 keyCodes : [ utils . KeyCodes . B ] ,
4135 } ;
@@ -44,12 +38,7 @@ export class StackNavigationAction {
4438 name : Constants . SHORTCUT_NAMES . NEXT_STACK ,
4539 preconditionFn,
4640 callback : ( workspace ) => {
47- const curNodeRoot = getCurNodeRoot ( workspace ) ;
48- if ( ! curNodeRoot ) return false ;
49- const nextRoot = workspace . getNavigator ( ) . getNextSibling ( curNodeRoot ) ;
50- if ( ! nextRoot ) return false ;
51- workspace . getCursor ( ) . setCurNode ( nextRoot ) ;
52- return true ;
41+ return this . navigate ( workspace , 1 ) ;
5342 } ,
5443 keyCodes : [ utils . KeyCodes . N ] ,
5544 } ;
@@ -60,6 +49,27 @@ export class StackNavigationAction {
6049 this . stackShortcuts . push ( nextStackShortcut ) ;
6150 }
6251
52+ private getCurNodeRoot ( workspace : WorkspaceSvg ) {
53+ const cursor = workspace . getCursor ( ) ;
54+ // The fallback case includes workspace comments.
55+ return cursor . getSourceBlock ( ) ?. getRootBlock ( ) ?? cursor . getCurNode ( ) ;
56+ }
57+
58+ private navigate ( workspace : WorkspaceSvg , delta : number ) {
59+ const curNodeRoot = this . getCurNodeRoot ( workspace ) ;
60+ if ( ! curNodeRoot || ! isSelectable ( curNodeRoot ) ) {
61+ return false ;
62+ }
63+
64+ let nextRoot = navigateStacks ( curNodeRoot , delta ) ;
65+ if ( ! nextRoot ) return false ;
66+ if ( nextRoot instanceof BlockSvg ) {
67+ nextRoot = nextRoot . getRootBlock ( ) ;
68+ }
69+ workspace . getCursor ( ) . setCurNode ( nextRoot ) ;
70+ return true ;
71+ }
72+
6373 /**
6474 * Unregisters the shortcut.
6575 */
0 commit comments