forked from microsoft/vscode-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplotViewer.ts
More file actions
174 lines (153 loc) · 6.88 KB
/
plotViewer.ts
File metadata and controls
174 lines (153 loc) · 6.88 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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
'use strict';
import '../../common/extensions';
import { inject, injectable } from 'inversify';
import * as path from 'path';
import { Event, EventEmitter, ViewColumn } from 'vscode';
import { traceInfo } from '../../../client/common/logger';
import { createDeferred } from '../../../client/common/utils/async';
import { IApplicationShell, IWebPanelProvider, IWorkspaceService } from '../../common/application/types';
import { EXTENSION_ROOT_DIR } from '../../common/constants';
import { WebHostNotebook } from '../../common/experimentGroups';
import { traceError } from '../../common/logger';
import { IFileSystem } from '../../common/platform/types';
import { IConfigurationService, IDisposable, IExperimentsManager } from '../../common/types';
import * as localize from '../../common/utils/localize';
import { ICodeCssGenerator, IPlotViewer, IThemeFinder } from '../types';
import { WebViewHost } from '../webViewHost';
import { PlotViewerMessageListener } from './plotViewerMessageListener';
import { IExportPlotRequest, IPlotViewerMapping, PlotViewerMessages } from './types';
const plotDir = path.join(EXTENSION_ROOT_DIR, 'out', 'datascience-ui', 'viewers');
@injectable()
export class PlotViewer extends WebViewHost<IPlotViewerMapping> implements IPlotViewer, IDisposable {
private closedEvent: EventEmitter<IPlotViewer> = new EventEmitter<IPlotViewer>();
private removedEvent: EventEmitter<number> = new EventEmitter<number>();
constructor(
@inject(IWebPanelProvider) provider: IWebPanelProvider,
@inject(IConfigurationService) configuration: IConfigurationService,
@inject(ICodeCssGenerator) cssGenerator: ICodeCssGenerator,
@inject(IThemeFinder) themeFinder: IThemeFinder,
@inject(IWorkspaceService) workspaceService: IWorkspaceService,
@inject(IApplicationShell) private applicationShell: IApplicationShell,
@inject(IFileSystem) private fileSystem: IFileSystem,
@inject(IExperimentsManager) experimentsManager: IExperimentsManager
) {
super(
configuration,
provider,
cssGenerator,
themeFinder,
workspaceService,
(c, v, d) => new PlotViewerMessageListener(c, v, d),
plotDir,
[path.join(plotDir, 'commons.initial.bundle.js'), path.join(plotDir, 'plotViewer.js')],
localize.DataScience.plotViewerTitle(),
ViewColumn.One,
experimentsManager.inExperiment(WebHostNotebook.experiment)
);
// Load the web panel using our current directory as we don't expect to load any other files
super.loadWebPanel(process.cwd()).catch(traceError);
}
public get closed(): Event<IPlotViewer> {
return this.closedEvent.event;
}
public get removed(): Event<number> {
return this.removedEvent.event;
}
public async show(): Promise<void> {
if (!this.isDisposed) {
// Then show our web panel.
return super.show(true);
}
}
public addPlot = async (imageHtml: string): Promise<void> => {
if (!this.isDisposed) {
// Make sure we're shown
await super.show(false);
// Send a message with our data
this.postMessage(PlotViewerMessages.SendPlot, imageHtml).ignoreErrors();
}
};
public dispose() {
super.dispose();
if (this.closedEvent) {
this.closedEvent.fire(this);
}
}
//tslint:disable-next-line:no-any
protected onMessage(message: string, payload: any) {
switch (message) {
case PlotViewerMessages.CopyPlot:
this.copyPlot(payload.toString()).ignoreErrors();
break;
case PlotViewerMessages.ExportPlot:
this.exportPlot(payload).ignoreErrors();
break;
case PlotViewerMessages.RemovePlot:
this.removePlot(payload);
break;
default:
break;
}
super.onMessage(message, payload);
}
private removePlot(payload: number) {
this.removedEvent.fire(payload);
}
private copyPlot(_svg: string): Promise<void> {
// This should be handled actually in the web view. Leaving
// this here for now in case need node to handle it.
return Promise.resolve();
}
private async exportPlot(payload: IExportPlotRequest): Promise<void> {
traceInfo('exporting plot...');
const filtersObject: Record<string, string[]> = {};
filtersObject[localize.DataScience.pdfFilter()] = ['pdf'];
filtersObject[localize.DataScience.pngFilter()] = ['png'];
filtersObject[localize.DataScience.svgFilter()] = ['svg'];
// Ask the user what file to save to
const file = await this.applicationShell.showSaveDialog({
saveLabel: localize.DataScience.exportPlotTitle(),
filters: filtersObject
});
try {
if (file) {
const ext = path.extname(file.fsPath);
switch (ext.toLowerCase()) {
case '.pdf':
traceInfo('Attempting pdf write...');
// Import here since pdfkit is so huge.
// tslint:disable-next-line: no-require-imports
const SVGtoPDF = require('svg-to-pdfkit');
const deferred = createDeferred<void>();
// tslint:disable-next-line: no-require-imports
const pdfkit = require('pdfkit');
const doc = new pdfkit();
const ws = this.fileSystem.createWriteStream(file.fsPath);
traceInfo(`Writing pdf to ${file.fsPath}`);
ws.on('finish', () => deferred.resolve);
SVGtoPDF(doc, payload.svg, 0, 0);
doc.pipe(ws);
doc.end();
traceInfo(`Finishing pdf to ${file.fsPath}`);
await deferred.promise;
traceInfo(`Completed pdf to ${file.fsPath}`);
break;
case '.png':
const buffer = new Buffer(payload.png.replace('data:image/png;base64', ''), 'base64');
await this.fileSystem.writeFile(file.fsPath, buffer);
break;
default:
case '.svg':
// This is the easy one:
await this.fileSystem.writeFile(file.fsPath, payload.svg);
break;
}
}
} catch (e) {
traceError(e);
this.applicationShell.showErrorMessage(localize.DataScience.exportImageFailed().format(e));
}
}
}