k-line/src/extension.ts

82 lines
2.3 KiB
TypeScript

import * as vscode from 'vscode';
// import { getWebviewContent } from './webviewContent';
export function activate(context: vscode.ExtensionContext) {
// 注册命令
const openWebviewCommand = vscode.commands.registerCommand('myExtension.openWebview', () => {
openFullScreenWebview(context);
});
// 将命令添加到订阅中
context.subscriptions.push(openWebviewCommand);
// 监听视图的点击事件
vscode.window.registerTreeDataProvider('myExtensionView', {
getTreeItem: () => new vscode.TreeItem('Open Webview', vscode.TreeItemCollapsibleState.None),
getChildren: () => Promise.resolve([]),
});
// 当用户点击活动栏图标时触发命令
vscode.commands.executeCommand('myExtension.openWebview');
}
function openFullScreenWebview(context: vscode.ExtensionContext) {
// 创建 Webview Panel
const panel = vscode.window.createWebviewPanel(
'myWebview', // 标识符
'股票', // 标题
vscode.ViewColumn.One, // 显示在编辑器的一侧
{
enableScripts: true, // 启用 JavaScript
retainContextWhenHidden: true, // 在隐藏时保留上下文
}
);
const scriptUri = panel.webview.asWebviewUri(
vscode.Uri.joinPath(context.extensionUri, 'dist8', 'assets', 'index-BO9WyeNZ.js')
);
const styleUri = panel.webview.asWebviewUri(
vscode.Uri.joinPath(context.extensionUri, 'dist8', 'assets', 'index-Cj3DiBUD.css')
);
// 设置 Webview 的内容
panel.webview.html = `
<!DOCTYPE html>
<html lang="">
<head>
<meta charset="UTF-8">
<meta http-equiv="Content-Security-Policy" name="viewport" content="width=device-width, initial-scale=1.0">
<style>
body {
display: flex;
flex-direction: column;
margin: 0;
padding: 0;
}
#app {
flex: 1;
}
::-webkit-scrollbar {
width: 0;
}
</style>
<script type="module" crossorigin src="${scriptUri}"></script>
<link rel="stylesheet" crossorigin href="${styleUri}">
</head>
<body>
<div id="app"></div>
</body>
</html>
`;
// 处理 Webview 关闭事件
panel.onDidDispose(
() => {
console.log('Webview closed');
},
null,
context.subscriptions
);
}
export function deactivate() {}