Repository navigation
Expand file tree
/
Copy pathcodeSnippetWebview.ts
More file actions
110 lines (87 loc) · 3.66 KB
/
Copy pathcodeSnippetWebview.ts
File metadata and controls
110 lines (87 loc) · 3.66 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
import { Clipboard, commands, env, ExtensionContext, ViewColumn, WebviewPanel, window } from 'vscode';
import { trace } from '../utils/decorator';
import { disposeAll } from '../utils/dispose';
import { BaseWebview } from './baseWebview';
const hljs = require('highlight.js');
const codeHighlightLinenums = require('code-highlight-linenums');
export class CodeSnippetWebview extends BaseWebview {
protected get viewType(): string {
return 'rest-code-snippet';
}
protected get previewActiveContextKey(): string {
return 'codeSnippetPreviewFocus';
}
private readonly clipboard: Clipboard = env.clipboard;
private activeCodeSnippet: string | undefined;
public constructor(context: ExtensionContext) {
super(context);
this.context.subscriptions.push(commands.registerCommand('rest-client.copy-codesnippet', this.copy, this));
}
public async render(convertResult: string, title: string, lang: string) {
let panel: WebviewPanel;
if (this.panels.length === 0) {
panel = window.createWebviewPanel(
this.viewType,
title,
ViewColumn.Two,
{
enableFindWidget: true,
retainContextWhenHidden: true
});
panel.onDidDispose(() => {
this.setPreviewActiveContext(false);
this.panels.pop();
this._onDidCloseAllWebviewPanels.fire();
});
panel.onDidChangeViewState(({ webviewPanel }) => {
this.setPreviewActiveContext(webviewPanel.active);
});
panel.iconPath = this.iconFilePath;
this.panels.push(panel);
} else {
panel = this.panels[0];
panel.title = title;
}
panel.webview.html = this.getHtmlForWebview(panel, convertResult, lang);
this.setPreviewActiveContext(true);
this.activeCodeSnippet = convertResult;
panel.reveal(ViewColumn.Two);
}
public dispose() {
disposeAll(this.panels);
}
@trace('Copy Code Snippet')
private async copy() {
if (this.activeCodeSnippet) {
await this.clipboard.writeText(this.activeCodeSnippet);
}
}
private getHtmlForWebview(panel: WebviewPanel, convertResult: string, lang: string): string {
const csp = this.getCsp();
return `
<head>
<link rel="stylesheet" type="text/css" href="${panel.webview.asWebviewUri(this.baseFilePath)}">
<link rel="stylesheet" type="text/css" href="${panel.webview.asWebviewUri(this.vscodeStyleFilePath)}">
<link rel="stylesheet" type="text/css" href="${panel.webview.asWebviewUri(this.customStyleFilePath)}">
${csp}
</head>
<body>
<div>
<pre><code>${codeHighlightLinenums(convertResult, { hljs, lang: this.getHighlightJsLanguageAlias(lang), start: 1 })}</code></pre>
<a id="scroll-to-top" role="button" aria-label="scroll to top" onclick="window.scroll(0,0)"><span class="icon"></span></a>
</div>
</body>`;
}
private getHighlightJsLanguageAlias(lang: string) {
if (!lang || lang === 'shell') {
return 'bash';
}
if (lang === 'node') {
return 'javascript';
}
return lang;
}
private getCsp(): string {
return `<meta http-equiv="Content-Security-Policy" content="default-src 'none'; img-src 'self' http: https: data: vscode-resource:; style-src 'self' 'unsafe-inline' http: https: data: vscode-resource:;">`;
}
}