This repository has been archived by the owner on Mar 20, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 13
/
atlasMapWebView.ts
101 lines (82 loc) · 2.75 KB
/
atlasMapWebView.ts
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
import * as vscode from 'vscode';
/**
* Manages atlas mapper webview panels
*/
export default class AtlasMapPanel {
/**
* Track the currently panel. Only allow a single panel to exist at a time.
*/
public static currentPanel: AtlasMapPanel | undefined;
public static readonly viewType = 'atlasmap';
public readonly _panel: vscode.WebviewPanel;
private _disposables: vscode.Disposable[] = [];
public static createOrShow(url: string) {
const column = vscode.window.activeTextEditor ? vscode.window.activeTextEditor.viewColumn : undefined;
// If we already have a panel, show it.
if (AtlasMapPanel.currentPanel) {
AtlasMapPanel.currentPanel._panel.reveal(column);
return;
}
// Otherwise, create a new panel.
const panel = vscode.window.createWebviewPanel(AtlasMapPanel.viewType, "AtlasMap", column || vscode.ViewColumn.One, {
enableScripts: true
});
AtlasMapPanel.currentPanel = new AtlasMapPanel(panel, url);
}
public static close() {
if (AtlasMapPanel.currentPanel) {
AtlasMapPanel.currentPanel.dispose();
}
}
public static revive(panel: vscode.WebviewPanel, url: string) {
AtlasMapPanel.currentPanel = new AtlasMapPanel(panel, url);
}
private constructor(panel: vscode.WebviewPanel, url: string) {
this._panel = panel;
// Listen for when the panel is disposed
// This happens when the user closes the panel or when the panel is closed programatically
this._panel.onDidDispose(() => this.dispose(), null, this._disposables);
// Update the content based on view changes
this._panel.onDidChangeViewState(e => {
if (this._panel.visible) {
this._update(url);
}
}, null, this._disposables);
// Handle messages from the webview
this._panel.webview.onDidReceiveMessage(message => {
if (message.command == 'alert') {
vscode.window.showErrorMessage(message.text);
return;
}
}, null, this._disposables);
// Set the webview's initial html content
this._update(url);
}
public dispose() {
AtlasMapPanel.currentPanel = undefined;
this._panel.dispose();
while (this._disposables.length) {
const x = this._disposables.pop();
if (x) {
x.dispose();
}
}
}
private _update(url: string) {
this._panel.title = "AtlasMap";
this.loadWebContent(url);
}
private loadWebContent(url: string) {
var fetchUrl = require("fetch").fetchUrl;
fetchUrl(url, function(error, meta, body) {
try {
var content = body.toString();
var contentWithHrefFullySpecified = content.replace('href="/"', 'href="'+url+'/"');
var contentWithHrefFullySpecifiedAndCSS = contentWithHrefFullySpecified.replace('<body>', '<body style="padding: 0">');
AtlasMapPanel.currentPanel._panel.webview.html = contentWithHrefFullySpecifiedAndCSS;
} catch (err) {
console.error(err);
}
});
}
}