2024-07-02 12:15:39 +08:00
|
|
|
import * as vscode from 'vscode';
|
|
|
|
|
|
|
|
export default class SIHome {
|
2024-07-03 11:55:49 +08:00
|
|
|
context: vscode.ExtensionContext;
|
2024-07-02 12:15:39 +08:00
|
|
|
|
2024-07-03 11:55:49 +08:00
|
|
|
constructor(context: vscode.ExtensionContext) {
|
|
|
|
this.context = context;
|
2024-07-02 12:15:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
toggle(url: string) {
|
|
|
|
console.log('url: ', url)
|
|
|
|
const panel = vscode.window.createWebviewPanel(
|
|
|
|
'myWebview',
|
|
|
|
'My Webview',
|
|
|
|
vscode.ViewColumn.One,
|
|
|
|
{}
|
|
|
|
);
|
|
|
|
|
|
|
|
panel.webview.options = {
|
|
|
|
enableScripts: true,
|
|
|
|
};
|
|
|
|
|
|
|
|
panel.webview.html = this.getWebviewContent();
|
|
|
|
|
|
|
|
panel.webview.onDidReceiveMessage(
|
|
|
|
(message) => {
|
|
|
|
switch (message.command) {
|
|
|
|
case 'alert':
|
|
|
|
vscode.window.showInformationMessage(message.text);
|
|
|
|
return;
|
|
|
|
case 'openFolder':
|
|
|
|
this.openFolderInVSCode(message.path);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
openFolderInVSCode(path: string): void {
|
|
|
|
try {
|
|
|
|
vscode.commands.executeCommand('vscode.openFolder', vscode.Uri.file(path))
|
|
|
|
console.log('Opened folder: ', path);
|
|
|
|
} catch (error) {
|
|
|
|
console.error('Error opening folder: ', error);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
getWebviewContent() {
|
|
|
|
return `
|
|
|
|
<!DOCTYPE html>
|
|
|
|
<html lang="en">
|
|
|
|
<head>
|
|
|
|
<meta charset="UTF-8">
|
|
|
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
|
|
<title>Cat Coding</title>
|
|
|
|
</head>
|
|
|
|
<body>
|
|
|
|
<h1>Cat Coding</h1>
|
|
|
|
<img src="https://media.giphy.com/media/JIX9t2j0ZTN9S/giphy.gif" width="300" />
|
|
|
|
<button onclick="showAlert()">Show Alert</button>
|
|
|
|
<button onclick="openFolder()">Open Project</button>
|
|
|
|
|
|
|
|
<script>
|
|
|
|
const vscode = acquireVsCodeApi();
|
|
|
|
|
|
|
|
function showAlert() {
|
|
|
|
vscode.postMessage({ command: 'alert', text: 'Hello from the Webview!' });
|
|
|
|
}
|
|
|
|
|
|
|
|
function openFolder() {
|
|
|
|
vscode.postMessage({ command: 'openFolder', path: 'D:/Programs/hello' });
|
|
|
|
}
|
|
|
|
</script>
|
|
|
|
</body>
|
|
|
|
</html>
|
|
|
|
`;
|
|
|
|
}
|
|
|
|
}
|