2024-07-02 12:15:39 +08:00
|
|
|
import * as vscode from 'vscode';
|
2024-10-28 15:52:55 +08:00
|
|
|
import * as os from 'os';
|
2024-07-16 17:37:42 +08:00
|
|
|
import RemoteContainer from './remote-container';
|
2024-10-23 13:45:19 +08:00
|
|
|
import User from './user';
|
2024-10-17 16:06:28 +08:00
|
|
|
import * as utils from './utils'
|
2024-07-02 12:15:39 +08:00
|
|
|
|
2024-07-16 23:28:56 +08:00
|
|
|
export default class DSHome {
|
2024-07-16 17:37:42 +08:00
|
|
|
private context: vscode.ExtensionContext;
|
|
|
|
private remoteContainer: RemoteContainer;
|
2024-11-12 19:07:30 +08:00
|
|
|
private user: User;
|
|
|
|
private devstarHomePageUrl: string;
|
2025-01-15 15:13:26 +08:00
|
|
|
private devstarDomain: string | undefined
|
2024-07-02 12:15:39 +08:00
|
|
|
|
2025-03-24 16:29:55 +08:00
|
|
|
constructor(context: vscode.ExtensionContext, user: User) {
|
2024-07-03 11:55:49 +08:00
|
|
|
this.context = context;
|
2025-03-24 16:29:55 +08:00
|
|
|
this.user = user;
|
|
|
|
this.remoteContainer = new RemoteContainer(user);
|
2024-11-12 19:01:40 +08:00
|
|
|
|
2025-03-03 13:15:34 +08:00
|
|
|
this.devstarDomain = utils.getDevstarDomain()
|
2024-12-27 13:02:56 +08:00
|
|
|
if (undefined == this.devstarDomain || "" == this.devstarDomain) {
|
2024-11-12 19:01:40 +08:00
|
|
|
this.devstarHomePageUrl = "https://devstar.cn/devstar-home"
|
|
|
|
} else {
|
2024-12-27 13:02:56 +08:00
|
|
|
this.devstarHomePageUrl = this.devstarDomain.endsWith('/') ? this.devstarDomain + "devstar-home" : this.devstarDomain + "/devstar-home"
|
2024-11-12 19:01:40 +08:00
|
|
|
}
|
2024-07-02 12:15:39 +08:00
|
|
|
}
|
|
|
|
|
2024-11-12 19:01:40 +08:00
|
|
|
async toggle(devstarHomePageUrl: string = this.devstarHomePageUrl) {
|
2024-07-02 12:15:39 +08:00
|
|
|
const panel = vscode.window.createWebviewPanel(
|
2024-09-18 20:46:25 +08:00
|
|
|
'homeWebview',
|
2025-04-22 22:32:06 +08:00
|
|
|
vscode.l10n.t('Home'),
|
2024-07-02 12:15:39 +08:00
|
|
|
vscode.ViewColumn.One,
|
2024-07-16 17:37:42 +08:00
|
|
|
{
|
|
|
|
enableScripts: true,
|
2024-12-27 10:11:22 +08:00
|
|
|
retainContextWhenHidden: true,
|
2024-07-16 17:37:42 +08:00
|
|
|
}
|
2024-07-02 12:15:39 +08:00
|
|
|
);
|
|
|
|
|
2024-11-12 19:01:40 +08:00
|
|
|
panel.webview.html = await this.getWebviewContent(devstarHomePageUrl);
|
2024-07-02 12:15:39 +08:00
|
|
|
|
|
|
|
panel.webview.onDidReceiveMessage(
|
2024-07-16 17:37:42 +08:00
|
|
|
async (message) => {
|
2024-10-23 01:58:10 +08:00
|
|
|
const data = message.data
|
2025-01-15 15:13:26 +08:00
|
|
|
const need_return = message.need_return
|
2025-01-15 18:22:01 +08:00
|
|
|
if (need_return) {
|
2025-01-15 15:13:26 +08:00
|
|
|
// ================= need return ====================
|
|
|
|
switch (message.command) {
|
|
|
|
// ----------------- frequent -----------------------
|
2025-04-22 20:58:09 +08:00
|
|
|
case 'getHomeConfig':
|
|
|
|
const config = {
|
|
|
|
language: vscode.env.language
|
|
|
|
}
|
|
|
|
panel.webview.postMessage({command: 'getHomeConfig', data: {homeConfig: config}})
|
2025-01-15 15:13:26 +08:00
|
|
|
case 'getUserToken':
|
|
|
|
const userToken = this.user.getUserTokenFromLocal()
|
|
|
|
if (userToken === undefined) {
|
|
|
|
panel.webview.postMessage({ command: 'getUserToken', data: { userToken: '' } })
|
|
|
|
break;
|
|
|
|
} else {
|
|
|
|
panel.webview.postMessage({ command: 'getUserToken', data: { userToken: userToken } })
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case 'getUsername':
|
|
|
|
const username = this.user.getUsernameFromLocal()
|
|
|
|
if (username === undefined) {
|
|
|
|
panel.webview.postMessage({ command: 'getUsername', data: { username: '' } })
|
|
|
|
break;
|
|
|
|
} else {
|
|
|
|
panel.webview.postMessage({ command: 'getUsername', data: { username: username } })
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case 'firstOpenRemoteFolder':
|
2025-05-22 18:13:17 +08:00
|
|
|
await this.remoteContainer.firstOpenProject(data.host, data.hostname, data.port, data.username, data.path, this.context)
|
2024-10-23 18:37:27 +08:00
|
|
|
break;
|
2025-01-15 15:13:26 +08:00
|
|
|
case 'openRemoteFolder':
|
2025-02-26 00:43:16 +08:00
|
|
|
this.remoteContainer.openRemoteFolder(data.host, data.port, data.username, data.path);
|
2024-10-23 18:37:27 +08:00
|
|
|
break;
|
2025-01-15 15:13:26 +08:00
|
|
|
case 'getDevstarDomain':
|
|
|
|
panel.webview.postMessage({ command: 'getDevstarDomain', data: { devstarDomain: this.devstarDomain } })
|
2024-10-23 18:37:27 +08:00
|
|
|
break;
|
2025-01-15 15:13:26 +08:00
|
|
|
// ----------------- not frequent -----------------------
|
|
|
|
case 'setUserToken':
|
|
|
|
this.user.setUserTokenToLocal(data.userToken)
|
|
|
|
if (data.userToken === this.user.getUserTokenFromLocal()) {
|
|
|
|
panel.webview.postMessage({ command: 'setUserToken', data: { ok: true } })
|
|
|
|
break;
|
|
|
|
} else {
|
|
|
|
panel.webview.postMessage({ command: 'setUserToken', data: { ok: false } })
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case 'setUsername':
|
|
|
|
this.user.setUsernameToLocal(data.username);
|
|
|
|
if (data.username === this.user.getUsernameFromLocal()) {
|
|
|
|
panel.webview.postMessage({ command: 'setUsername', data: { ok: true } });
|
|
|
|
break;
|
|
|
|
} else {
|
|
|
|
panel.webview.postMessage({ command: 'setUsername', data: { ok: false } });
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case 'getUserPublicKey':
|
|
|
|
var userPublicKey = '';
|
|
|
|
if (this.user.existUserPrivateKey()) {
|
|
|
|
userPublicKey = this.user.getUserPublicKey();
|
|
|
|
panel.webview.postMessage({ command: 'getUserPublicKey', data: { userPublicKey: userPublicKey } })
|
|
|
|
break;
|
|
|
|
} else {
|
|
|
|
panel.webview.postMessage({ command: 'getUserPublicKey', data: { userPublicKey: userPublicKey } })
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case 'createUserPublicKey':
|
|
|
|
await this.user.createUserSSHKey();
|
|
|
|
if (this.user.existUserPublicKey()) {
|
|
|
|
panel.webview.postMessage({ command: 'createUserPublicKey', data: { ok: true } })
|
|
|
|
break;
|
|
|
|
} else {
|
|
|
|
panel.webview.postMessage({ command: 'createUserPublicKey', data: { ok: false } })
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case 'getMachineName':
|
|
|
|
const machineName = os.hostname();
|
|
|
|
panel.webview.postMessage({ command: 'getMachineName', data: { machineName: machineName } })
|
|
|
|
}
|
2025-01-15 18:22:01 +08:00
|
|
|
} else {
|
|
|
|
// ================= don't need return ==============
|
|
|
|
// frequent
|
|
|
|
switch (message.command) {
|
|
|
|
// ----------------- frequent -----------------------
|
|
|
|
case 'showInformationNotification':
|
|
|
|
vscode.window.showInformationMessage(data.message);
|
|
|
|
break;
|
|
|
|
case 'showWarningNotification':
|
|
|
|
vscode.window.showWarningMessage(data.message)
|
|
|
|
break;
|
|
|
|
case 'showErrorNotification':
|
2025-03-23 17:15:33 +08:00
|
|
|
await utils.showErrorNotification(data.message)
|
2025-01-15 18:22:01 +08:00
|
|
|
break;
|
|
|
|
}
|
2024-07-02 12:15:39 +08:00
|
|
|
}
|
|
|
|
},
|
|
|
|
undefined,
|
2024-07-03 11:55:49 +08:00
|
|
|
this.context.subscriptions
|
2024-07-02 12:15:39 +08:00
|
|
|
);
|
2024-07-03 11:55:49 +08:00
|
|
|
|
|
|
|
this.context.subscriptions.push(panel)
|
2024-07-02 12:15:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2024-11-12 19:01:40 +08:00
|
|
|
async getWebviewContent(devstarHomePageUrl: string): Promise<string> {
|
|
|
|
return `
|
|
|
|
<?php
|
|
|
|
header("Access-Control-Allow-Origin: *");
|
|
|
|
header("Access-Control-Allow-Headers: X-API-KEY, Origin, X-Requested-With, Content-Type, Accept, Access-Control-Request-Method");
|
|
|
|
header("Access-Control-Allow-Methods: GET, POST, OPTIONS, PUT, DELETE");
|
|
|
|
header("Allow: GET, POST, OPTIONS, PUT, DELETE");
|
|
|
|
?>
|
|
|
|
<!DOCTYPE html>
|
|
|
|
<html lang="en">
|
|
|
|
|
|
|
|
<head>
|
|
|
|
<meta charset="UTF-8">
|
|
|
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
|
|
<title>DevStar Home</title>
|
|
|
|
</head>
|
|
|
|
|
|
|
|
<body>
|
2024-12-27 10:11:22 +08:00
|
|
|
<iframe id="embedded-devstar" src="${devstarHomePageUrl}" sandbox="allow-popups allow-same-origin allow-scripts allow-forms allow-top-navigation-by-user-activation" width="100%" height="100%" frameborder="0"
|
2024-11-12 19:01:40 +08:00
|
|
|
style="border: 0; left: 0; right: 0; bottom: 0; top: 0; position:absolute;">
|
|
|
|
</iframe>
|
|
|
|
|
|
|
|
<script>
|
|
|
|
const vscode = acquireVsCodeApi();
|
|
|
|
|
|
|
|
function firstOpenRemoteFolder() {
|
|
|
|
vscode.postMessage({ command: 'firstOpenRemoteFolder', host: host, username: username, password: password, port: port, path: path });
|
|
|
|
}
|
|
|
|
|
|
|
|
function openRemoteFolder() {
|
|
|
|
vscode.postMessage({ command: 'openRemoteFolder', host: host, path: path });
|
|
|
|
}
|
|
|
|
|
|
|
|
function firstOpenRemoteFolderWithData(host, username, password, port, path) {
|
|
|
|
vscode.postMessage({ command: 'firstOpenRemoteFolder', host: host, username: username, password: password, port: port, path: path });
|
|
|
|
}
|
|
|
|
|
|
|
|
function openRemoteFolderWithData(host, path) {
|
|
|
|
vscode.postMessage({ command: 'openRemoteFolder', host: host, path: path });
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
async function communicateVSCode(command, data) {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
// request to vscode
|
2025-01-15 15:13:26 +08:00
|
|
|
vscode.postMessage({ command: command, need_return: true, data: data })
|
2024-11-12 19:01:40 +08:00
|
|
|
|
|
|
|
function handleResponse(event) {
|
|
|
|
const jsonData = event.data;
|
|
|
|
if (jsonData.command === command) {
|
|
|
|
// console.log("communicateVSCode", jsonData.data)
|
|
|
|
|
|
|
|
// return vscode response
|
|
|
|
window.removeEventListener('message', handleResponse) // 清理监听器
|
|
|
|
resolve(jsonData.data)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
window.addEventListener('message', handleResponse)
|
|
|
|
|
|
|
|
setTimeout(() => {
|
|
|
|
window.removeEventListener('message', handleResponse)
|
|
|
|
reject('timeout')
|
|
|
|
}, 5000); // 5秒超时
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// 监听子页面的消息
|
|
|
|
window.addEventListener('message', async (event) => {
|
|
|
|
// 出于安全考虑,检查 event.origin 是否是你预期的源
|
|
|
|
// if (event.origin !== "http://expected-origin.com") {
|
|
|
|
// return;
|
|
|
|
// }
|
|
|
|
try {
|
|
|
|
const jsonData = event.data;
|
|
|
|
|
|
|
|
if (jsonData.target === 'vscode') {
|
|
|
|
const actionFromHome = jsonData.action
|
|
|
|
const dataFromHome = jsonData.data
|
|
|
|
const dataFromVSCodeResponse = await communicateVSCode(actionFromHome, dataFromHome)
|
|
|
|
|
|
|
|
var iframe = document.getElementById('embedded-devstar');
|
|
|
|
if (iframe && iframe.contentWindow) {
|
|
|
|
iframe.contentWindow.postMessage({ action: actionFromHome, data: dataFromVSCodeResponse }, '*')
|
|
|
|
}
|
2025-01-15 15:13:26 +08:00
|
|
|
} else if (jsonData.target === 'vscode_no_return') {
|
|
|
|
vscode.postMessage({ command: jsonData.action, need_return: false, data: jsonData.data })
|
2024-11-12 19:01:40 +08:00
|
|
|
}
|
|
|
|
} catch (error) {
|
|
|
|
console.error('Error parsing message:', error);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
</script>
|
|
|
|
</body>
|
|
|
|
|
|
|
|
</html>`
|
2024-07-02 12:15:39 +08:00
|
|
|
}
|
|
|
|
}
|