import { injectable } from '@cloudbeaver/core-di' ; import type { IDataContextProvider } from '../DataContext/IDataContextProvider' ; import { ActionItem } from './ActionItem' ; import type { IAction } from './IAction' ; import type { IActionHandler } from './IActionHandler' ; import type { IActionItem } from './IActionItem' ; import { KeyBindingService } from './KeyBinding/KeyBindingService' ; @ injectable ( ) export class ActionService { private handlers : Map < string , IActionHandler > ; constructor ( private readonly keyBindingService ) { this . handlers = new Map ( ) ; } addHandler ( handler ) { if ( this . handlers . has ( handler . id ) ) { throw new Error ( `Action handler with same id (${ handler . id }) already exists` ) ; } this . handlers . set ( handler . id , handler ) ; } getHandler ( context , action ) { for ( const handler of this . handlers . values ( ) ) { if ( handler . isActionApplicable ( context , action ) ) { return handler ; } } return null ; } getAction ( context , action ) { const handler = this . getHandler ( context , action ) ; if ( handler ) { const binding = this . keyBindingService . getKeyBindingHandler ( context , action ) ; return new ActionItem ( action , handler , binding , context ) ; } return null ; } }