openFrame copy.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. const vscode = require('vscode');
  2. function activate(context) {
  3. context.subscriptions.push(
  4. vscode.window.registerWebviewPanelSerializer('myWebviewType', new MyWebviewSerializer())
  5. );
  6. context.subscriptions.push(
  7. vscode.commands.registerCommand('myExtension.openWebview', () => {
  8. openWebview(context);
  9. })
  10. );
  11. }
  12. class MyWebviewSerializer {
  13. async deserializeWebviewPanel(panel, state) {
  14. console.log("Restoring WebView", state);
  15. panel.webview.options = {
  16. enableScripts: true,
  17. localResourceRoots: []
  18. };
  19. const url = state?.url || 'https://example.com'; // Fallback URL
  20. panel.webview.html = getWebviewContent(url);
  21. }
  22. }
  23. function openWebview(context) {
  24. const url = vscode.workspace.getConfiguration().get('itk.lastUrl') || 'https://example.com';
  25. const panel = vscode.window.createWebviewPanel(
  26. 'myWebviewType',
  27. 'My Webview',
  28. vscode.ViewColumn.One,
  29. {
  30. enableScripts: true,
  31. localResourceRoots: []
  32. }
  33. );
  34. panel.webview.html = getWebviewContent(url);
  35. panel.onDidDispose(() => {
  36. vscode.workspace.getConfiguration().update('itk.lastUrl', url, vscode.ConfigurationTarget.Global);
  37. }, null, context.subscriptions);
  38. }
  39. function getWebviewContent(url) {
  40. return `
  41. <!DOCTYPE html>
  42. <html lang="en">
  43. <head>
  44. <meta charset="UTF-8">
  45. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  46. <title>WebView</title>
  47. <style>
  48. body { margin: 0; padding: 0; }
  49. iframe { width: 100vw; height: 100vh; border: none; }
  50. </style>
  51. </head>
  52. <body>
  53. <iframe src="${url}"></iframe>
  54. </body>
  55. </html>
  56. `;
  57. }
  58. function deactivate() {}
  59. function initWelcome(context, defaultUrl) {
  60. context.subscriptions.push(
  61. vscode.commands.registerCommand('itk.openWelcomeFrame', () => {
  62. openWelcomeFrame();
  63. })
  64. );
  65. }
  66. function initTests(context, defaultUrl) {
  67. context.subscriptions.push(
  68. vscode.commands.registerCommand('itk.openTests', () => {
  69. openTestsFrame();
  70. })
  71. );
  72. }
  73. function openWelcomeFrame() {
  74. runFrame({ url: 'https://itrum.ru', name: 'ITK Welcome Frame', id: 'itkWelcomeFrame' })
  75. }
  76. function openTestsFrame() {
  77. runFrame({ url: 'https://itrum.ru', name: 'ITK Tests', id: 'itkTests' })
  78. }
  79. function runFrame({ url, name, id} , groupId) {
  80. const panel = vscode.window.createWebviewPanel(
  81. id, // Identifies the webview
  82. name, // Title of the panel
  83. vscode.ViewColumn.One, // Show in the first column
  84. {
  85. enableScripts: true, // Allows JavaScript execution
  86. localResourceRoots: [] // Define where local resources can be loaded from (if needed)
  87. }
  88. );
  89. panel.reveal(vscode.ViewColumn.One + groupId - 1);
  90. console.log('groupId', groupId)
  91. // Set HTML content for the Webview
  92. panel.webview.html = getWebviewContent(url);
  93. }
  94. function getWebviewContent(url) {
  95. return `<!DOCTYPE html>
  96. <html lang="en">
  97. <head>
  98. <meta charset="UTF-8">
  99. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  100. <title>ITK Academy</title>
  101. <style>
  102. body { margin: 0; padding: 0; display: flex; height: 100vh; }
  103. iframe { width: 100%; height: 100%; border: none; }
  104. </style>
  105. </head>
  106. <body>
  107. <iframe src="${url}"></iframe>
  108. </body>
  109. </html>`;
  110. }
  111. function initFrame(context) {
  112. context.subscriptions.push(
  113. vscode.window.registerWebviewPanelSerializer('myWebviewType', new MyWebviewSerializer())
  114. );
  115. }
  116. function getTemplate() {
  117. return `<!DOCTYPE html>
  118. <html lang="en">
  119. <head>
  120. <meta charset="UTF-8">
  121. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  122. <title>Cat Coding</title>
  123. </head>
  124. <body>
  125. <h1>Welcome to ITK.academy!!!!!</h1>
  126. <p>This is a custom welcome view.</p>
  127. <p>You can add your own HTML content here.</p>
  128. <button id="myButton">Click Me</button>
  129. <script>
  130. const vscode = acquireVsCodeApi();
  131. document.getElementById('myButton').addEventListener('click', () => {
  132. vscode.postMessage({ command: 'buttonClicked' });
  133. });
  134. </script>
  135. </body>
  136. </html>`;
  137. }
  138. module.exports = {
  139. initWelcome,
  140. getTemplate,
  141. initTests,
  142. initFrame,
  143. openWelcomeFrame,
  144. openTestsFrame,
  145. runFrame
  146. };