1313
1414import type { BlockSvg } from './block_svg.js' ;
1515import { RenderedWorkspaceComment } from './comments/rendered_workspace_comment.js' ;
16+ import type { IFocusableNode } from './interfaces/i_focusable_node.js' ;
1617import { Coordinate } from './utils/coordinate.js' ;
1718import type { WorkspaceSvg } from './workspace_svg.js' ;
1819
@@ -71,56 +72,60 @@ export class ContextMenuRegistry {
7172 }
7273
7374 /**
74- * Gets the valid context menu options for the given scope type (e.g. block or
75- * workspace) and scope. Blocks are only shown if the preconditionFn shows
75+ * Gets the valid context menu options for the given scope.
76+ * Options are only included if the preconditionFn shows
7677 * they should not be hidden.
7778 *
78- * @param scopeType Type of scope where menu should be shown (e.g. on a block
79- * or on a workspace)
8079 * @param scope Current scope of context menu (i.e., the exact workspace or
81- * block being clicked on)
80+ * block being clicked on).
81+ * @param menuOpenEvent Event that caused the menu to open.
8282 * @returns the list of ContextMenuOptions
8383 */
8484 getContextMenuOptions (
85- scopeType : ScopeType ,
8685 scope : Scope ,
8786 menuOpenEvent : Event ,
8887 ) : ContextMenuOption [ ] {
8988 const menuOptions : ContextMenuOption [ ] = [ ] ;
9089 for ( const item of this . registeredItems . values ( ) ) {
91- if ( scopeType === item . scopeType ) {
92- let menuOption :
93- | ContextMenuRegistry . CoreContextMenuOption
94- | ContextMenuRegistry . SeparatorContextMenuOption
95- | ContextMenuRegistry . ActionContextMenuOption ;
90+ if ( item . scopeType ) {
91+ // If the scopeType is present, check to make sure
92+ // that the option is compatible with the current scope
93+ if ( item . scopeType === ScopeType . BLOCK && ! scope . block ) continue ;
94+ if ( item . scopeType === ScopeType . COMMENT && ! scope . comment ) continue ;
95+ if ( item . scopeType === ScopeType . WORKSPACE && ! scope . workspace )
96+ continue ;
97+ }
98+ let menuOption :
99+ | ContextMenuRegistry . CoreContextMenuOption
100+ | ContextMenuRegistry . SeparatorContextMenuOption
101+ | ContextMenuRegistry . ActionContextMenuOption ;
102+ menuOption = {
103+ scope,
104+ weight : item . weight ,
105+ } ;
106+
107+ if ( item . separator ) {
96108 menuOption = {
97- scope ,
98- weight : item . weight ,
109+ ... menuOption ,
110+ separator : true ,
99111 } ;
112+ } else {
113+ const precondition = item . preconditionFn ( scope , menuOpenEvent ) ;
114+ if ( precondition === 'hidden' ) continue ;
100115
101- if ( item . separator ) {
102- menuOption = {
103- ...menuOption ,
104- separator : true ,
105- } ;
106- } else {
107- const precondition = item . preconditionFn ( scope , menuOpenEvent ) ;
108- if ( precondition === 'hidden' ) continue ;
109-
110- const displayText =
111- typeof item . displayText === 'function'
112- ? item . displayText ( scope )
113- : item . displayText ;
114- menuOption = {
115- ...menuOption ,
116- text : displayText ,
117- callback : item . callback ,
118- enabled : precondition === 'enabled' ,
119- } ;
120- }
121-
122- menuOptions . push ( menuOption ) ;
116+ const displayText =
117+ typeof item . displayText === 'function'
118+ ? item . displayText ( scope )
119+ : item . displayText ;
120+ menuOption = {
121+ ...menuOption ,
122+ text : displayText ,
123+ callback : item . callback ,
124+ enabled : precondition === 'enabled' ,
125+ } ;
123126 }
127+
128+ menuOptions . push ( menuOption ) ;
124129 }
125130 menuOptions . sort ( function ( a , b ) {
126131 return a . weight - b . weight ;
@@ -142,20 +147,23 @@ export namespace ContextMenuRegistry {
142147 }
143148
144149 /**
145- * The actual workspace/block where the menu is being rendered. This is passed
146- * to callback and displayText functions that depend on this information.
150+ * The actual workspace/block/focused object where the menu is being
151+ * rendered. This is passed to callback and displayText functions
152+ * that depend on this information.
147153 */
148154 export interface Scope {
149155 block ?: BlockSvg ;
150156 workspace ?: WorkspaceSvg ;
151157 comment ?: RenderedWorkspaceComment ;
158+ // TODO(#8839): Remove any once Block, etc. implement IFocusableNode
159+ focusedNode ?: IFocusableNode | any ;
152160 }
153161
154162 /**
155163 * Fields common to all context menu registry items.
156164 */
157165 interface CoreRegistryItem {
158- scopeType : ScopeType ;
166+ scopeType ? : ScopeType ;
159167 weight : number ;
160168 id : string ;
161169 }
0 commit comments