Files
devstar_plugin/src/home.ts

78 lines
2.6 KiB
TypeScript
Raw Normal View History

2024-07-02 12:15:39 +08:00
import * as vscode from 'vscode';
import RemoteContainer from './remote-container';
2024-09-18 20:46:25 +08:00
import {fetch} from './utils'
2024-07-02 12:15:39 +08:00
export default class DSHome {
private context: vscode.ExtensionContext;
private remoteContainer: RemoteContainer;
static defaultUrl = 'http://localhost:8080/tmp/index.html';
2024-07-02 12:15:39 +08:00
constructor(context: vscode.ExtensionContext) {
this.context = context;
this.remoteContainer = new RemoteContainer()
2024-07-02 12:15:39 +08:00
}
async toggle(url: string = DSHome.defaultUrl) {
console.log(url);
2024-07-02 12:15:39 +08:00
const panel = vscode.window.createWebviewPanel(
2024-09-18 20:46:25 +08:00
'homeWebview',
'Home',
2024-07-02 12:15:39 +08:00
vscode.ViewColumn.One,
{
enableScripts: true,
}
2024-07-02 12:15:39 +08:00
);
fetch(url).then(async (content) => {
panel.webview.html = await this.getWebviewContent(content);
})
2024-07-02 12:15:39 +08:00
panel.webview.onDidReceiveMessage(
async (message) => {
2024-07-02 12:15:39 +08:00
switch (message.command) {
case 'firstOpenRemoteFolder':
await this.remoteContainer.firstConnect(message.host, message.username, message.password, message.port)
.then((result) => {
if (result === 'success') {
// only success then open folder
this.remoteContainer.openRemoteFolder(message.host, message.path);
}
})
2024-09-18 20:46:25 +08:00
break;
case 'openRemoteFolder':
this.remoteContainer.openRemoteFolder(message.host, message.path);
2024-09-18 20:46:25 +08:00
break;
case 'getUserToken':
const userToken = this.context.globalState.get('devstarUserToken')
if (userToken === undefined) {
panel.webview.postMessage({ command: 'getUserToken', data: {userToken: 'none'}})
break;
} else {
panel.webview.postMessage({ command: 'getUserToken', data: {userToken: userToken}})
break;
}
case 'setUserToken':
this.context.globalState.update('devstarUserToken', message.data.userToken)
console.log(this.context.globalState.get('devstarUserToken'))
console.log(message.data.userToken)
if (message.data.userToken === this.context.globalState.get('devstarUserToken')) {
panel.webview.postMessage({ command: 'setUserToken', data: {ok: true}})
break;
} else {
panel.webview.postMessage({ command: 'setUserToken', data: {ok: false}})
break;
}
2024-07-02 12:15:39 +08:00
}
},
undefined,
this.context.subscriptions
2024-07-02 12:15:39 +08:00
);
this.context.subscriptions.push(panel)
2024-07-02 12:15:39 +08:00
}
async getWebviewContent(content: string): Promise<string> {
return `${content}`
2024-07-02 12:15:39 +08:00
}
}