|
| 1 | +// Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | +// Licensed under the MIT License. |
| 3 | + |
| 4 | +'use strict'; |
| 5 | + |
| 6 | +import { inject, injectable } from 'inversify'; |
| 7 | +import { QuickPickItem, QuickPickOptions } from 'vscode'; |
| 8 | +import { ICommandNameArgumentTypeMapping } from '../../common/application/commands'; |
| 9 | +import { IApplicationShell, ICommandManager } from '../../common/application/types'; |
| 10 | +import { IDisposable } from '../../common/types'; |
| 11 | +import { Commands } from '../constants'; |
| 12 | +import { ExportFormat, ExportManager, IExportManager } from '../export/exportManager'; |
| 13 | +import { INotebookEditorProvider } from '../types'; |
| 14 | + |
| 15 | +interface IExportQuickPickItem extends QuickPickItem { |
| 16 | + handler(): void; |
| 17 | +} |
| 18 | + |
| 19 | +@injectable() |
| 20 | +export class ExportCommands implements IDisposable { |
| 21 | + private readonly disposables: IDisposable[] = []; |
| 22 | + constructor( |
| 23 | + @inject(ICommandManager) private readonly commandManager: ICommandManager, |
| 24 | + @inject(IExportManager) private exportManager: ExportManager, |
| 25 | + @inject(INotebookEditorProvider) private notebookEditorProvider: INotebookEditorProvider, |
| 26 | + @inject(IApplicationShell) private readonly applicationShell: IApplicationShell |
| 27 | + ) {} |
| 28 | + public register() { |
| 29 | + this.registerCommand(Commands.ExportAsPythonScript, () => this.export(ExportFormat.python)); |
| 30 | + this.registerCommand(Commands.ExportToHTML, () => this.export(ExportFormat.html)); |
| 31 | + this.registerCommand(Commands.ExportToPDF, () => this.export(ExportFormat.pdf)); |
| 32 | + this.registerCommand(Commands.Export, this.export); |
| 33 | + } |
| 34 | + |
| 35 | + public dispose() { |
| 36 | + this.disposables.forEach((d) => d.dispose()); |
| 37 | + } |
| 38 | + |
| 39 | + private registerCommand< |
| 40 | + E extends keyof ICommandNameArgumentTypeMapping, |
| 41 | + U extends ICommandNameArgumentTypeMapping[E] |
| 42 | + // tslint:disable-next-line: no-any |
| 43 | + >(command: E, callback: (...args: U) => any) { |
| 44 | + const disposable = this.commandManager.registerCommand(command, callback, this); |
| 45 | + this.disposables.push(disposable); |
| 46 | + } |
| 47 | + |
| 48 | + private async export(exportMethod?: ExportFormat) { |
| 49 | + // get notebook provider |
| 50 | + const model = this.notebookEditorProvider.activeEditor?.model; |
| 51 | + if (!model) { |
| 52 | + throw Error('No active editor found.'); |
| 53 | + } |
| 54 | + |
| 55 | + if (exportMethod) { |
| 56 | + await this.exportManager.export(exportMethod, model); |
| 57 | + } else { |
| 58 | + const pickedItem = await this.showExportQuickPickMenu().then((item) => item); |
| 59 | + if (pickedItem !== undefined) { |
| 60 | + pickedItem.handler(); |
| 61 | + } |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + private getExportQuickPickItems(): IExportQuickPickItem[] { |
| 66 | + return [ |
| 67 | + { |
| 68 | + label: 'Python Script', |
| 69 | + picked: true, |
| 70 | + handler: () => this.commandManager.executeCommand(Commands.ExportAsPythonScript) |
| 71 | + } |
| 72 | + //{ label: 'HTML', picked: false, handler: () => this.commandManager.executeCommand(Commands.ExportToHTML) }, |
| 73 | + //{ label: 'PDF', picked: false, handler: () => this.commandManager.executeCommand(Commands.ExportToPDF) } |
| 74 | + ]; |
| 75 | + } |
| 76 | + |
| 77 | + private async showExportQuickPickMenu(): Promise<IExportQuickPickItem | undefined> { |
| 78 | + const items = this.getExportQuickPickItems(); |
| 79 | + |
| 80 | + const options: QuickPickOptions = { |
| 81 | + ignoreFocusOut: false, |
| 82 | + matchOnDescription: true, |
| 83 | + matchOnDetail: true, |
| 84 | + placeHolder: 'Export As...' |
| 85 | + }; |
| 86 | + |
| 87 | + return this.applicationShell.showQuickPick(items, options); |
| 88 | + } |
| 89 | +} |
0 commit comments