refactor: convert js to ts
This commit is contained in:
78
src/home.ts
Normal file
78
src/home.ts
Normal file
@@ -0,0 +1,78 @@
|
||||
import * as vscode from 'vscode';
|
||||
|
||||
export default class SIHome {
|
||||
|
||||
constructor() {
|
||||
}
|
||||
|
||||
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,
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
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>
|
||||
`;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user