Skip to content

Commit 8ec8c8b

Browse files
DonJayamannerchiodo
authored andcommitted
Allow downloading plotly images (#9234)
* Allow downloading plotly images * Oops
1 parent f8ee70f commit 8ec8c8b

File tree

4 files changed

+27
-5
lines changed

4 files changed

+27
-5
lines changed

src/client/common/application/webPanels/webPanel.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ export class WebPanel implements IWebPanel {
131131
<head>
132132
<meta charset="utf-8">
133133
<meta name="viewport" content="width=device-width,initial-scale=1,shrink-to-fit=no">
134-
<meta http-equiv="Content-Security-Policy" content="img-src 'self' data: https: http: blob:; default-src 'unsafe-inline' 'unsafe-eval' vscode-resource: data: https: http:;">
134+
<meta http-equiv="Content-Security-Policy" content="img-src 'self' data: https: http: blob:; default-src 'unsafe-inline' 'unsafe-eval' vscode-resource: data: https: http: blob:;">
135135
<meta name="theme-color" content="#000000">
136136
<meta name="theme" content="${Identifiers.GeneratedThemeName}"/>
137137
<title>React App</title>
@@ -165,7 +165,7 @@ export class WebPanel implements IWebPanel {
165165
<head>
166166
<meta charset="utf-8">
167167
<meta name="viewport" content="width=device-width,initial-scale=1,shrink-to-fit=no">
168-
<meta http-equiv="Content-Security-Policy" content="img-src 'self' data: https: http: blob:; default-src 'unsafe-inline' 'unsafe-eval' vscode-resource: data: https: http:;">
168+
<meta http-equiv="Content-Security-Policy" content="img-src 'self' data: https: http: blob:; default-src 'unsafe-inline' 'unsafe-eval' vscode-resource: data: https: http: blob:;">
169169
<meta name="theme-color" content="#000000">
170170
<meta name="theme" content="${Identifiers.GeneratedThemeName}"/>
171171
<title>React App</title>

src/client/common/application/webPanels/webPanelServer.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ export class WebPanelServer {
132132
<head>
133133
<meta charset="utf-8">
134134
<meta name="viewport" content="width=device-width,initial-scale=1,shrink-to-fit=no">
135-
<meta http-equiv="Content-Security-Policy" content="img-src 'self' data: https: http: blob:; default-src 'unsafe-inline' 'unsafe-eval' vscode-resource: data: https: http:;">
135+
<meta http-equiv="Content-Security-Policy" content="img-src 'self' data: https: http: blob:; default-src 'unsafe-inline' 'unsafe-eval' vscode-resource: data: https: http: blob:;">
136136
<meta name="theme-color" content="#000000">
137137
<meta name="theme" content="${Identifiers.GeneratedThemeName}"/>
138138
<title>React App</title>

src/datascience-ui/interactive-common/handlers.ts

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,30 @@ export function handleLinkClick(ev: MouseEvent, linkClick: (href: string) => voi
1313
anchor = inner[0];
1414
}
1515
}
16-
if (anchor && anchor.href && !anchor.href.startsWith('vscode')) {
16+
if (!anchor || !anchor.href || anchor.href.startsWith('vscode')) {
17+
return;
18+
}
19+
if (!anchor.href.startsWith('blob:')) {
1720
linkClick(anchor.href);
1821
}
22+
23+
// We an have an image (as a blob) and the reference is blob://null:<someguid>
24+
// We need to get the blob, for that make a http request and the response will be the Blob
25+
// Next convert the blob into something that can be sent to the client side.
26+
// Just send an inlined base64 image to `linkClick`, such as `data:image/png;base64,xxxxx`
27+
const xhr = new XMLHttpRequest();
28+
xhr.open('GET', anchor.href, true);
29+
xhr.responseType = 'blob';
30+
xhr.onload = () => {
31+
const blob = xhr.response;
32+
const reader = new FileReader();
33+
reader.readAsDataURL(blob);
34+
reader.onload = () => {
35+
if (typeof reader.result === 'string'){
36+
linkClick(reader.result);
37+
}
38+
};
39+
};
40+
xhr.send();
1941
}
2042
}

src/datascience-ui/native-editor/nativeCell.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ export class NativeCell extends React.Component<INativeCellProps> {
164164
private onMouseClick = (ev: React.MouseEvent<HTMLDivElement>) => {
165165
if (ev.nativeEvent.target) {
166166
const elem = ev.nativeEvent.target as HTMLElement;
167-
if (!elem.className.includes('image-button')) {
167+
if (!elem.className.includes || !elem.className.includes('image-button')) {
168168
// Not a click on an button in a toolbar, select the cell.
169169
ev.stopPropagation();
170170
this.lastKeyPressed = undefined;

0 commit comments

Comments
 (0)