forked from DonJayamanne/pythonVSCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexportCommands.ts
More file actions
151 lines (137 loc) · 6.27 KB
/
exportCommands.ts
File metadata and controls
151 lines (137 loc) · 6.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
'use strict';
import { inject, injectable } from 'inversify';
import { QuickPickItem, QuickPickOptions, Uri } from 'vscode';
import { getLocString } from '../../../datascience-ui/react-common/locReactSide';
import { ICommandNameArgumentTypeMapping } from '../../common/application/commands';
import { IApplicationShell, ICommandManager } from '../../common/application/types';
import { IDisposable } from '../../common/types';
import { DataScience } from '../../common/utils/localize';
import { isUri } from '../../common/utils/misc';
import { sendTelemetryEvent } from '../../telemetry';
import { Commands, Telemetry } from '../constants';
import { ExportManager } from '../export/exportManager';
import { ExportFormat, IExportManager } from '../export/types';
import { IDataScienceFileSystem, INotebookEditorProvider, INotebookModel } from '../types';
interface IExportQuickPickItem extends QuickPickItem {
handler(): void;
}
@injectable()
export class ExportCommands implements IDisposable {
private readonly disposables: IDisposable[] = [];
constructor(
@inject(ICommandManager) private readonly commandManager: ICommandManager,
@inject(IExportManager) private exportManager: ExportManager,
@inject(IApplicationShell) private readonly applicationShell: IApplicationShell,
@inject(INotebookEditorProvider) private readonly notebookProvider: INotebookEditorProvider,
@inject(IDataScienceFileSystem) private readonly fs: IDataScienceFileSystem
) {}
public register() {
this.registerCommand(Commands.ExportAsPythonScript, (model) => this.export(model, ExportFormat.python));
this.registerCommand(Commands.ExportToHTML, (model, defaultFileName?) =>
this.export(model, ExportFormat.html, defaultFileName)
);
this.registerCommand(Commands.ExportToPDF, (model, defaultFileName?) =>
this.export(model, ExportFormat.pdf, defaultFileName)
);
this.registerCommand(Commands.Export, (model, defaultFileName?) =>
this.export(model, undefined, defaultFileName)
);
}
public dispose() {
this.disposables.forEach((d) => d.dispose());
}
private registerCommand<
E extends keyof ICommandNameArgumentTypeMapping,
U extends ICommandNameArgumentTypeMapping[E]
// tslint:disable-next-line: no-any
>(command: E, callback: (...args: U) => any) {
const disposable = this.commandManager.registerCommand(command, callback, this);
this.disposables.push(disposable);
}
private async export(modelOrUri: Uri | INotebookModel, exportMethod?: ExportFormat, defaultFileName?: string) {
defaultFileName = typeof defaultFileName === 'string' ? defaultFileName : undefined;
let model: INotebookModel | undefined;
if (modelOrUri && isUri(modelOrUri)) {
const uri = modelOrUri;
const editor = this.notebookProvider.editors.find((item) => this.fs.arePathsSame(item.file, uri));
if (editor && editor.model) {
model = editor.model;
}
} else {
model = modelOrUri;
}
if (!model) {
// if no model was passed then this was called from the command palette,
// so we need to get the active editor
const activeEditor = this.notebookProvider.activeEditor;
if (!activeEditor || !activeEditor.model) {
return;
}
model = activeEditor.model;
if (exportMethod) {
sendTelemetryEvent(Telemetry.ExportNotebookAsCommand, undefined, { format: exportMethod });
}
}
if (exportMethod) {
await this.exportManager.export(exportMethod, model, defaultFileName);
} else {
// if we don't have an export method we need to ask for one and display the
// quickpick menu
const pickedItem = await this.showExportQuickPickMenu(model, defaultFileName).then((item) => item);
if (pickedItem !== undefined) {
pickedItem.handler();
} else {
sendTelemetryEvent(Telemetry.ClickedExportNotebookAsQuickPick);
}
}
}
private getExportQuickPickItems(model: INotebookModel, defaultFileName?: string): IExportQuickPickItem[] {
return [
{
label: DataScience.exportPythonQuickPickLabel(),
picked: true,
handler: () => {
sendTelemetryEvent(Telemetry.ClickedExportNotebookAsQuickPick, undefined, {
format: ExportFormat.python
});
this.commandManager.executeCommand(Commands.ExportAsPythonScript, model);
}
},
{
label: DataScience.exportHTMLQuickPickLabel(),
picked: false,
handler: () => {
sendTelemetryEvent(Telemetry.ClickedExportNotebookAsQuickPick, undefined, {
format: ExportFormat.html
});
this.commandManager.executeCommand(Commands.ExportToHTML, model, defaultFileName);
}
},
{
label: DataScience.exportPDFQuickPickLabel(),
picked: false,
handler: () => {
sendTelemetryEvent(Telemetry.ClickedExportNotebookAsQuickPick, undefined, {
format: ExportFormat.pdf
});
this.commandManager.executeCommand(Commands.ExportToPDF, model, defaultFileName);
}
}
];
}
private async showExportQuickPickMenu(
model: INotebookModel,
defaultFileName?: string
): Promise<IExportQuickPickItem | undefined> {
const items = this.getExportQuickPickItems(model, defaultFileName);
const options: QuickPickOptions = {
ignoreFocusOut: false,
matchOnDescription: true,
matchOnDetail: true,
placeHolder: getLocString('DataScience.exportAsQuickPickPlaceholder', 'Export As...')
};
return this.applicationShell.showQuickPick(items, options);
}
}