123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171 |
- const vscode = require('vscode');
- function activate(context) {
- context.subscriptions.push(
- vscode.window.registerWebviewPanelSerializer('myWebviewType', new MyWebviewSerializer())
- );
- context.subscriptions.push(
- vscode.commands.registerCommand('myExtension.openWebview', () => {
- openWebview(context);
- })
- );
- }
- class MyWebviewSerializer {
- async deserializeWebviewPanel(panel, state) {
- console.log("Restoring WebView", state);
- panel.webview.options = {
- enableScripts: true,
- localResourceRoots: []
- };
- const url = state?.url || 'https://example.com'; // Fallback URL
- panel.webview.html = getWebviewContent(url);
- }
- }
- function openWebview(context) {
- const url = vscode.workspace.getConfiguration().get('itk.lastUrl') || 'https://example.com';
- const panel = vscode.window.createWebviewPanel(
- 'myWebviewType',
- 'My Webview',
- vscode.ViewColumn.One,
- {
- enableScripts: true,
- localResourceRoots: []
- }
- );
- panel.webview.html = getWebviewContent(url);
- panel.onDidDispose(() => {
- vscode.workspace.getConfiguration().update('itk.lastUrl', url, vscode.ConfigurationTarget.Global);
- }, null, context.subscriptions);
- }
- function getWebviewContent(url) {
- return `
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="UTF-8">
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
- <title>WebView</title>
- <style>
- body { margin: 0; padding: 0; }
- iframe { width: 100vw; height: 100vh; border: none; }
- </style>
- </head>
- <body>
- <iframe src="${url}"></iframe>
- </body>
- </html>
- `;
- }
- function deactivate() {}
- function initWelcome(context, defaultUrl) {
- context.subscriptions.push(
- vscode.commands.registerCommand('itk.openWelcomeFrame', () => {
- openWelcomeFrame();
- })
- );
- }
- function initTests(context, defaultUrl) {
- context.subscriptions.push(
- vscode.commands.registerCommand('itk.openTests', () => {
- openTestsFrame();
- })
- );
- }
- function openWelcomeFrame() {
- runFrame({ url: 'https://itrum.ru', name: 'ITK Welcome Frame', id: 'itkWelcomeFrame' })
- }
- function openTestsFrame() {
- runFrame({ url: 'https://itrum.ru', name: 'ITK Tests', id: 'itkTests' })
- }
- function runFrame({ url, name, id} , groupId) {
- const panel = vscode.window.createWebviewPanel(
- id, // Identifies the webview
- name, // Title of the panel
- vscode.ViewColumn.One, // Show in the first column
- {
- enableScripts: true, // Allows JavaScript execution
- localResourceRoots: [] // Define where local resources can be loaded from (if needed)
- }
- );
- panel.reveal(vscode.ViewColumn.One + groupId - 1);
- console.log('groupId', groupId)
- // Set HTML content for the Webview
- panel.webview.html = getWebviewContent(url);
-
- }
- function getWebviewContent(url) {
- return `<!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="UTF-8">
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
- <title>ITK Academy</title>
- <style>
- body { margin: 0; padding: 0; display: flex; height: 100vh; }
- iframe { width: 100%; height: 100%; border: none; }
- </style>
- </head>
- <body>
- <iframe src="${url}"></iframe>
- </body>
- </html>`;
- }
- function initFrame(context) {
- context.subscriptions.push(
- vscode.window.registerWebviewPanelSerializer('myWebviewType', new MyWebviewSerializer())
- );
- }
- function getTemplate() {
- 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>Welcome to ITK.academy!!!!!</h1>
- <p>This is a custom welcome view.</p>
- <p>You can add your own HTML content here.</p>
- <button id="myButton">Click Me</button>
- <script>
- const vscode = acquireVsCodeApi();
- document.getElementById('myButton').addEventListener('click', () => {
- vscode.postMessage({ command: 'buttonClicked' });
- });
- </script>
- </body>
- </html>`;
- }
- module.exports = {
- initWelcome,
- getTemplate,
- initTests,
- initFrame,
- openWelcomeFrame,
- openTestsFrame,
- runFrame
- };
|