forked from platformio/platformio-vscode-ide
-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhome.js
More file actions
164 lines (151 loc) · 4.84 KB
/
home.js
File metadata and controls
164 lines (151 loc) · 4.84 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
/**
* Copyright (c) 2017-present PlatformIO <contact@platformio.org>
* All rights reserved.
*
* This source code is licensed under the license found in the LICENSE file in
* the root directory of this source tree.
*/
import * as pioNodeHelpers from 'platformio-node-helpers';
import { extension } from './main';
import { notifyError } from './utils';
import path from 'path';
import vscode from 'vscode';
export default class PIOHome {
static defaultStartUrl = '/';
constructor() {
this.subscriptions = [];
this._currentPanel = undefined;
this._lastStartUrl = PIOHome.defaultStartUrl;
// close PIO Home when workspaces folders are changed (VSCode reactivates extensiuon)
this.subscriptions.push(
vscode.workspace.onDidChangeWorkspaceFolders(this.disposePanel.bind(this))
);
}
async toggle(startUrl = PIOHome.defaultStartUrl) {
const column = vscode.window.activeTextEditor
? vscode.window.activeTextEditor.viewColumn
: undefined;
try {
if (this._currentPanel) {
if (this._lastStartUrl !== startUrl) {
this._currentPanel.webview.html = await this.getWebviewContent(startUrl);
}
return this._currentPanel.reveal(column);
}
} catch (err) {
console.warn(err);
}
this._currentPanel = await this.newPanel(startUrl);
}
async newPanel(startUrl) {
const panel = vscode.window.createWebviewPanel(
'pioHome',
extension.getEnterpriseSetting('pioHomeTitle', 'PIO Home'),
vscode.ViewColumn.One,
{
enableScripts: true,
retainContextWhenHidden: true
}
);
this.subscriptions.push(panel.onDidDispose(this.onPanelDisposed.bind(this)));
panel.iconPath = vscode.Uri.file(
path.join(
extension.context.extensionPath,
'resources',
'platformio-mini-logo.svg'
)
);
panel.webview.html = this.getLoadingContent();
try {
panel.webview.html = await this.getWebviewContent(startUrl);
} catch (err) {
notifyError('Start PIO Home Server', err);
}
return panel;
}
getTheme() {
const workbench = vscode.workspace.getConfiguration('workbench') || {};
return (workbench.colorTheme || '').toLowerCase().includes('light')
? 'light'
: 'dark';
}
getLoadingContent() {
const theme = this.getTheme();
return `<!DOCTYPE html>
<html lang="en">
<body style="background-color: ${theme === 'light' ? '#FFF' : '#1E1E1E'}">
<div style="padding: 15px;">Loading...</div>
</body>
</html>`;
}
async getWebviewContent(startUrl) {
this._lastStartUrl = startUrl;
const params = await pioNodeHelpers.home.ensureServerStarted({
port: extension.getSetting('pioHomeServerHttpPort'),
onIDECommand: async (command, params) => {
if (command === 'open_project') {
this.disposePanel();
if (vscode.workspace.workspaceFolders) {
vscode.workspace.updateWorkspaceFolders(
vscode.workspace.workspaceFolders.length,
null,
{ uri: vscode.Uri.file(params) }
);
} else {
vscode.commands.executeCommand(
'vscode.openFolder',
vscode.Uri.file(params)
);
}
vscode.commands.executeCommand('workbench.view.explorer');
} else if (command === 'open_text_document') {
const editor = await vscode.window.showTextDocument(
vscode.Uri.file(params.path)
);
const gotoPosition = new vscode.Position(
(params.line || 1) - 1,
(params.column || 1) - 1
);
editor.selection = new vscode.Selection(gotoPosition, gotoPosition);
editor.revealRange(
new vscode.Range(gotoPosition, gotoPosition),
vscode.TextEditorRevealType.InCenter
);
}
}
});
const theme = this.getTheme();
return `<!DOCTYPE html>
<html lang="en">
<body style="margin: 0; padding: 0; height: 100%; overflow: hidden; background-color: ${
theme === 'light' ? '#FFF' : '#1E1E1E'
}">
<iframe src="${pioNodeHelpers.home.getFrontendUri(params.host, params.port, {
start: startUrl,
theme,
workspace: extension.getEnterpriseSetting('defaultPIOHomeWorkspace')
})}"
width="100%"
height="100%"
frameborder="0"
style="border: 0; left: 0; right: 0; bottom: 0; top: 0; position:absolute;" />
</body>
</html>
`;
}
onPanelDisposed() {
this._currentPanel = undefined;
}
disposePanel() {
if (!this._currentPanel) {
return;
}
this._currentPanel.dispose();
this._currentPanel = undefined;
}
dispose() {
this.disposePanel();
pioNodeHelpers.misc.disposeSubscriptions(this.subscriptions);
pioNodeHelpers.home.shutdownServer();
}
}