|
| 1 | +import { inject, injectable } from 'inversify'; |
| 2 | +import { MonacoEditor } from '@theia/monaco/lib/browser/monaco-editor'; |
| 3 | +import { EditorManager } from '@theia/editor/lib/browser/editor-manager'; |
| 4 | +import { WindowService } from '@theia/core/lib/browser/window/window-service'; |
| 5 | +import { CommandHandler } from '@theia/core/lib/common/command'; |
| 6 | +import { QuickInputService } from '@theia/core/lib/browser/quick-open/quick-input-service'; |
| 7 | +import { ArduinoMenus } from '../menu/arduino-menus'; |
| 8 | +import { Contribution, Command, MenuModelRegistry, CommandRegistry, KeybindingRegistry } from './contribution'; |
| 9 | + |
| 10 | +@injectable() |
| 11 | +export class Help extends Contribution { |
| 12 | + |
| 13 | + @inject(EditorManager) |
| 14 | + protected readonly editorManager: EditorManager; |
| 15 | + |
| 16 | + @inject(WindowService) |
| 17 | + protected readonly windowService: WindowService; |
| 18 | + |
| 19 | + @inject(QuickInputService) |
| 20 | + protected readonly quickInputService: QuickInputService; |
| 21 | + |
| 22 | + registerCommands(registry: CommandRegistry): void { |
| 23 | + const open = (url: string) => this.windowService.openNewWindow(url, { external: true }); |
| 24 | + const createOpenHandler = (url: string) => <CommandHandler>{ |
| 25 | + execute: () => open(url) |
| 26 | + }; |
| 27 | + registry.registerCommand(Help.Commands.GETTING_STARTED, createOpenHandler('https://www.arduino.cc/en/Guide')); |
| 28 | + registry.registerCommand(Help.Commands.ENVIRONMENT, createOpenHandler('https://www.arduino.cc/en/Guide/Environment')); |
| 29 | + registry.registerCommand(Help.Commands.TROUBLESHOOTING, createOpenHandler('https://support.arduino.cc/hc/en-us')); |
| 30 | + registry.registerCommand(Help.Commands.REFERENCE, createOpenHandler('https://www.arduino.cc/reference/en/')); |
| 31 | + registry.registerCommand(Help.Commands.FIND_IN_REFERENCE, { |
| 32 | + execute: async () => { |
| 33 | + let searchFor: string | undefined = undefined; |
| 34 | + const { currentEditor } = this.editorManager; |
| 35 | + if (currentEditor && currentEditor.editor instanceof MonacoEditor) { |
| 36 | + const codeEditor = currentEditor.editor.getControl(); |
| 37 | + const selection = codeEditor.getSelection(); |
| 38 | + const model = codeEditor.getModel(); |
| 39 | + if (model && selection && !monaco.Range.isEmpty(selection)) { |
| 40 | + searchFor = model.getValueInRange(selection); |
| 41 | + } |
| 42 | + } |
| 43 | + if (!searchFor) { |
| 44 | + searchFor = await this.quickInputService.open({ |
| 45 | + prompt: 'Search on Arduino.cc', |
| 46 | + placeHolder: 'Type a keyword' |
| 47 | + }); |
| 48 | + } |
| 49 | + if (searchFor) { |
| 50 | + return open(`https://www.arduino.cc/search?q=${encodeURIComponent(searchFor)}&tab=reference`); |
| 51 | + } |
| 52 | + } |
| 53 | + }); |
| 54 | + registry.registerCommand(Help.Commands.FAQ, createOpenHandler('https://support.arduino.cc/hc/en-us')); |
| 55 | + registry.registerCommand(Help.Commands.VISIT_ARDUINO, createOpenHandler('https://www.arduino.cc/')); |
| 56 | + } |
| 57 | + |
| 58 | + registerMenus(registry: MenuModelRegistry): void { |
| 59 | + registry.registerMenuAction(ArduinoMenus.HELP__MAIN_GROUP, { |
| 60 | + commandId: Help.Commands.GETTING_STARTED.id, |
| 61 | + order: '0' |
| 62 | + }); |
| 63 | + registry.registerMenuAction(ArduinoMenus.HELP__MAIN_GROUP, { |
| 64 | + commandId: Help.Commands.ENVIRONMENT.id, |
| 65 | + order: '1' |
| 66 | + }); |
| 67 | + registry.registerMenuAction(ArduinoMenus.HELP__MAIN_GROUP, { |
| 68 | + commandId: Help.Commands.TROUBLESHOOTING.id, |
| 69 | + order: '2' |
| 70 | + }); |
| 71 | + registry.registerMenuAction(ArduinoMenus.HELP__MAIN_GROUP, { |
| 72 | + commandId: Help.Commands.REFERENCE.id, |
| 73 | + order: '3' |
| 74 | + }); |
| 75 | + |
| 76 | + registry.registerMenuAction(ArduinoMenus.HELP__FIND_GROUP, { |
| 77 | + commandId: Help.Commands.FIND_IN_REFERENCE.id, |
| 78 | + order: '4' |
| 79 | + }); |
| 80 | + registry.registerMenuAction(ArduinoMenus.HELP__FIND_GROUP, { |
| 81 | + commandId: Help.Commands.FAQ.id, |
| 82 | + order: '5' |
| 83 | + }); |
| 84 | + registry.registerMenuAction(ArduinoMenus.HELP__FIND_GROUP, { |
| 85 | + commandId: Help.Commands.VISIT_ARDUINO.id, |
| 86 | + order: '6' |
| 87 | + }); |
| 88 | + } |
| 89 | + |
| 90 | + registerKeybindings(registry: KeybindingRegistry): void { |
| 91 | + registry.registerKeybinding({ |
| 92 | + command: Help.Commands.FIND_IN_REFERENCE.id, |
| 93 | + keybinding: 'CtrlCmd+Shift+F' |
| 94 | + }); |
| 95 | + } |
| 96 | + |
| 97 | +} |
| 98 | + |
| 99 | +export namespace Help { |
| 100 | + export namespace Commands { |
| 101 | + export const GETTING_STARTED: Command = { |
| 102 | + id: 'arduino-getting-started', |
| 103 | + label: 'Getting Started', |
| 104 | + category: 'Arduino' |
| 105 | + }; |
| 106 | + export const ENVIRONMENT: Command = { |
| 107 | + id: 'arduino-environment', |
| 108 | + label: 'Environment', |
| 109 | + category: 'Arduino' |
| 110 | + }; |
| 111 | + export const TROUBLESHOOTING: Command = { |
| 112 | + id: 'arduino-troubleshooting', |
| 113 | + label: 'Troubleshooting', |
| 114 | + category: 'Arduino' |
| 115 | + }; |
| 116 | + export const REFERENCE: Command = { |
| 117 | + id: 'arduino-reference', |
| 118 | + label: 'Reference', |
| 119 | + category: 'Arduino' |
| 120 | + }; |
| 121 | + export const FIND_IN_REFERENCE: Command = { |
| 122 | + id: 'arduino-find-in-reference', |
| 123 | + label: 'Find in Reference', |
| 124 | + category: 'Arduino' |
| 125 | + }; |
| 126 | + export const FAQ: Command = { |
| 127 | + id: 'arduino-faq', |
| 128 | + label: 'Frequently Asked Questions', |
| 129 | + category: 'Arduino' |
| 130 | + }; |
| 131 | + export const VISIT_ARDUINO: Command = { |
| 132 | + id: 'arduino-visit-arduino', |
| 133 | + label: 'Visit Arduino.cc', |
| 134 | + category: 'Arduino' |
| 135 | + }; |
| 136 | + } |
| 137 | +} |
0 commit comments