import * as vscode from 'vscode'; import * as os from 'os'; import RemoteContainer from './remote-container'; import User from './user'; import * as utils from './utils' export default class DSHome { private context: vscode.ExtensionContext; private remoteContainer: RemoteContainer; private user: User; private devstarHomePageUrl: string; private devstarDomain: string | undefined constructor(context: vscode.ExtensionContext) { this.context = context; this.user = new User(context); this.remoteContainer = new RemoteContainer(this.user); this.devstarDomain = vscode.workspace.getConfiguration('devstar').get('devstarDomain') if (undefined == this.devstarDomain || "" == this.devstarDomain) { this.devstarHomePageUrl = "https://devstar.cn/devstar-home" } else { this.devstarHomePageUrl = this.devstarDomain.endsWith('/') ? this.devstarDomain + "devstar-home" : this.devstarDomain + "/devstar-home" } } async toggle(devstarHomePageUrl: string = this.devstarHomePageUrl) { const panel = vscode.window.createWebviewPanel( 'homeWebview', 'Home', vscode.ViewColumn.One, { enableScripts: true, retainContextWhenHidden: true, } ); panel.webview.html = await this.getWebviewContent(devstarHomePageUrl); panel.webview.onDidReceiveMessage( async (message) => { const data = message.data const need_return = message.need_return if (need_return) { // ================= need return ==================== switch (message.command) { // ----------------- frequent ----------------------- 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': await this.remoteContainer.firstConnect(data.host, data.username, data.port) .then((_res) => { if (_res == 'success') { // only success then open folder RemoteContainer.openRemoteFolder(data.host, data.port, data.username, data.path); } }) break; case 'openRemoteFolder': RemoteContainer.openRemoteFolder(data.host, data.port, data.username, data.path); break; case 'getDevstarDomain': panel.webview.postMessage({ command: 'getDevstarDomain', data: { devstarDomain: this.devstarDomain } }) break; // ----------------- 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 } }) } } 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': const selection = await vscode.window.showErrorMessage(data.message, 'Open Console', 'Report a problem',); if (selection === 'Open Console') { vscode.commands.executeCommand('workbench.action.toggleDevTools'); } else if (selection === 'Report a problem') { vscode.commands.executeCommand('vscode.open', vscode.Uri.parse('https://gitee.com/SuperIDE/DevStar/issues')); } break; } } }, undefined, this.context.subscriptions ); this.context.subscriptions.push(panel) } async getWebviewContent(devstarHomePageUrl: string): Promise { return ` DevStar Home ` } }