diff --git a/frontend/webEditor/src/startUpAgent/FocusContainer.ts b/frontend/webEditor/src/startUpAgent/FocusContainer.ts new file mode 100644 index 00000000..32db4013 --- /dev/null +++ b/frontend/webEditor/src/startUpAgent/FocusContainer.ts @@ -0,0 +1,28 @@ +// The focus is run though an action and not directly in the start up agent, as sprotty is not initialized when the later is called, but when the action dispatcher can handle the action, it is +import { inject } from "inversify"; +import { TYPES, ActionDispatcher, Command, CommandExecutionContext, CommandReturn } from "sprotty"; +import { IStartUpAgent } from "./StartUpAgent"; + +export class FocusContainerStartUpAgent implements IStartUpAgent { + constructor(@inject(TYPES.IActionDispatcher) private actionDispatcher: ActionDispatcher) {} + + run(): void { + this.actionDispatcher.dispatch({ kind: FocusContainerCommand.KIND }); + } +} + +export class FocusContainerCommand extends Command { + public static readonly KIND = "focus-container"; + + execute(context: CommandExecutionContext): CommandReturn { + document.getElementById("sprotty_root")?.focus(); + + return context.root; + } + undo(context: CommandExecutionContext): CommandReturn { + return context.root; + } + redo(context: CommandExecutionContext): CommandReturn { + return context.root; + } +} diff --git a/frontend/webEditor/src/startUpAgent/di.config.ts b/frontend/webEditor/src/startUpAgent/di.config.ts index b71d630d..fd3434ea 100644 --- a/frontend/webEditor/src/startUpAgent/di.config.ts +++ b/frontend/webEditor/src/startUpAgent/di.config.ts @@ -3,9 +3,13 @@ import { StartUpAgent } from "./StartUpAgent"; import { LoadDefaultUiExtensionsStartUpAgent } from "./LoadDefaultUiExtensions"; import { LoadDefaultDiagramStartUpAgent } from "./LoadDefaultDiagram"; import { SettingsInitStartUpAgent } from "./settingsInit"; +import { FocusContainerCommand, FocusContainerStartUpAgent } from "./FocusContainer"; +import { configureCommand } from "sprotty"; -export const startUpAgentModule = new ContainerModule((bind) => { +export const startUpAgentModule = new ContainerModule((bind, _, isBound) => { bind(StartUpAgent).to(LoadDefaultUiExtensionsStartUpAgent); bind(StartUpAgent).to(LoadDefaultDiagramStartUpAgent); bind(StartUpAgent).to(SettingsInitStartUpAgent); + bind(StartUpAgent).to(FocusContainerStartUpAgent); + configureCommand({ bind, isBound }, FocusContainerCommand); });