66
77import {
88 ASTNode ,
9- Connection ,
109 ContextMenu ,
1110 ContextMenuRegistry ,
1211 ShortcutRegistry ,
@@ -22,10 +21,6 @@ const createSerializedKey = ShortcutRegistry.registry.createSerializedKey.bind(
2221 ShortcutRegistry . registry ,
2322) ;
2423
25- export interface ScopeWithConnection extends ContextMenuRegistry . Scope {
26- connection ?: Connection ;
27- }
28-
2924/**
3025 * Keyboard shortcut to show the action menu on Cmr/Ctrl/Alt+Enter key.
3126 */
@@ -114,10 +109,10 @@ export class ActionMenu {
114109 const connection = node . getLocation ( ) as RenderedConnection ;
115110 rtl = connection . getSourceBlock ( ) . RTL ;
116111
117- // Slightly hacky: get insert action from registry. Hacky
118- // because registry typings don't include {connection: ...} as
119- // a possible kind of scope.
120- const menuOptions = this . addConnectionItems ( connection , menuOpenEvent ) ;
112+ const menuOptions = ContextMenuRegistry . registry . getContextMenuOptions (
113+ { focusedNode : connection } ,
114+ menuOpenEvent ,
115+ ) ;
121116 // If no valid options, don't show a menu
122117 if ( ! menuOptions ?. length ) return true ;
123118 const location = this . calculateLocationForConnectionMenu ( connection ) ;
@@ -153,45 +148,6 @@ export class ActionMenu {
153148 return true ;
154149 }
155150
156- /**
157- * Add menu items for a context menu on a connection scope.
158- *
159- * @param connection The connection on which the menu is shown.
160- * @param menuOpenEvent The event that opened this context menu.
161- */
162- private addConnectionItems ( connection : Connection , menuOpenEvent : Event ) {
163- const menuOptions : Array <
164- | ContextMenuRegistry . ContextMenuOption
165- | ContextMenuRegistry . LegacyContextMenuOption
166- > = [ ] ;
167- const possibleOptions = [
168- this . getContextMenuAction ( 'insert' ) ,
169- this . getContextMenuAction ( 'blockPasteFromContextMenu' ) ,
170- ] ;
171-
172- // Check preconditions and get menu texts.
173- const scope = {
174- connection,
175- } as unknown as ContextMenuRegistry . Scope ;
176-
177- for ( const option of possibleOptions ) {
178- const precondition = option . preconditionFn ?.( scope , menuOpenEvent ) ;
179- if ( precondition === 'hidden' ) continue ;
180- const displayText =
181- ( typeof option . displayText === 'function'
182- ? option . displayText ( scope )
183- : option . displayText ) ?? '' ;
184- menuOptions . push ( {
185- text : displayText ,
186- enabled : precondition === 'enabled' ,
187- callback : option . callback ,
188- scope,
189- weight : option . weight ,
190- } ) ;
191- }
192- return menuOptions ;
193- }
194-
195151 /**
196152 * Find a context menu action, throwing an `Error` if it is not present or
197153 * not an action. This usefully narrows the type to `ActionRegistryItem`
0 commit comments