上一篇:使用Theia——創建語言支持
命令和快捷鍵
Theia可以通過多種不同的方式進行擴展,命令允許packages提供可以被其它包呼叫的唯一命令,還可以向這些命令添加快捷鍵和背景關系,使得它們只能在某些特定的條件下被呼叫(如視窗獲取焦點、當前選項等),
在Theia中添加命令
要將命令添加到Theia,必須實作CommandContribution類,如:
java-commands.ts
@injectable()export class JavaCommandContribution implements CommandContribution {... registerCommands(commands: CommandRegistry): void { commands.registerCommand(SHOW_JAVA_REFERENCES, { execute: (uri: string, position: Position, locations: Location[]) => commands.executeCommand(SHOW_REFERENCES.id, uri, position, locations) }); commands.registerCommand(APPLY_WORKSPACE_EDIT, { execute: (changes: WorkspaceEdit) => !!this.workspace.applyEdit && this.workspace.applyEdit(changes) }); }}每一個contribution需要提供一個唯一的命令id,以及一個命令處理程式(由回呼函式執行), 將contribution系結到CommandContribution, 然后將contribution的類注入到適當的模塊中(確保類被標記為@injectable()),像這樣:java-frontend-module.ts
export default new ContainerModule(bind => { bind(CommandContribution).to(JavaCommandContribution).inSingletonScope(); ...});
負責注冊和執行命令的類是CommandRegistry,通過get commandIds() api可以獲取命令串列,
添加快捷鍵
默認情況下不需要給命令添加快捷鍵,因為它可以通過許多不同的方式來呼叫(通過編程方式或者用戶點擊),不過,我們仍然可以將具有特定背景關系的快捷鍵系結到命令上以完成相同的命令功能, 要添加快捷鍵,只需要注入一個KeybindingContribution的實作,editor-keybinding.ts@injectable()export class EditorKeybindingContribution implements KeybindingContribution { constructor( @inject(EditorKeybindingContext) protected readonly editorKeybindingContext: EditorKeybindingContext ) { } registerKeybindings(registry: KeybindingRegistry): void { [ { command: 'editor.close', context: this.editorKeybindingContext, keybinding: "alt+w" }, { command: 'editor.close.all', context: this.editorKeybindingContext, keybinding: "alt+shift+w" } ].forEach(binding => { registry.registerKeybinding(binding); }); }}commandId必須是預先注冊好的命令,而且必須唯一,context是一個簡單的類,用于確保命令和快捷鍵的系結組合在特定條件下是可用的,對于編輯器而言,它看起來是這樣的:editor-keybinding.ts
@injectable()export class EditorKeybindingContext implements KeybindingContext { constructor( @inject(EditorManager) protected readonly editorService: EditorManager) { } id = 'editor.keybinding.context'; isEnabled(arg?: Keybinding) { return this.editorService && !!this.editorService.activeEditor; }}context也有一個唯一的id,用于在背景關系中將快捷鍵注冊到命令中,isEnabled()方法會根據給定的條件回傳true或false,請注意,context是一個可選引數,如果沒有提供,則默認為NOOP_CONTEXT,使用該context注冊的快捷鍵將始終保持開啟狀態,可以在應用程式的任何地方使用, 對于id而言,引數keycode是必須的,它是一個包含實際快捷鍵系結的結構,下面是一個正確的結構:keys.ts
export declare type Keystroke = { first: Key, modifiers?: Modifier[] };Modifier是平臺無關的,所以Modifier.M1在OS X上是Command而在Windows/Linux上是CTRL,Key字串常量定義在keys.ts中,
將contribution系結到KeybindingContribution
與系結命令contribution一樣,快捷鍵contribution也需要按模塊進行系結,像這樣:editor-frontend-module.tsexport default new ContainerModule(bind => { ... bind(CommandContribution).to(EditorCommandHandlers); bind(EditorKeybindingContext).toSelf().inSingletonScope(); bind(KeybindingContext).toDynamicValue(context => context.container.get(EditorKeybindingContext)); bind(KeybindingContribution).to(EditorKeybindingContribution);});
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/6680.html
標籤:其他
上一篇:使用Theia——添加語言支持
