123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100 |
- const vscode = require('vscode');
- let _context;
- let panel;
- const previewKey = 'savedPreviewUrl'; // Key to store last opened URL
- const previewColumnKey = 'savedPreviewColumn'; // Key to store last panel position
- function initFrame(context) {
- _context = context;
- // Register command to open the preview tab
- let disposable = vscode.commands.registerCommand('itk.openPreview', async () => {
- const url = await vscode.window.showInputBox({
- prompt: 'Enter the URL to preview',
- placeHolder: 'https://example.com'
- });
- if (url) {
- runFrame(url);
- }
- });
- context.subscriptions.push(disposable);
- // Restore last opened preview on activation
- const savedUrl = context.globalState.get(previewKey);
- const savedColumn = context.globalState.get(previewColumnKey) || vscode.ViewColumn.One;
-
- if (savedUrl) {
- runFrame(savedUrl, savedColumn);
- }
- }
- // Function to open a WebView preview
- function runFrame(url, column = vscode.ViewColumn.Active) {
- let globalState = _context.globalState;
- if (panel) {
- // If panel exists, update the content and restore position
- panel.webview.html = getWebviewContent(url);
- panel.reveal(column);
- } else {
- // Create a new WebView panel in the last known position
- panel = vscode.window.createWebviewPanel(
- 'webPreview',
- 'Web Preview',
- column,
- { enableScripts: true }
- );
- panel.webview.html = getWebviewContent(url);
- // Save URL & position for persistence
- globalState.update(previewKey, url);
- globalState.update(previewColumnKey, column);
- // Track panel movement
- panel.onDidChangeViewState(e => {
- if (panel.visible) {
- globalState.update(previewColumnKey, panel.viewColumn);
- }
- });
- // Handle WebView disposal (clear references)
- panel.onDidDispose(() => {
- panel = null;
- globalState.update(previewKey, undefined);
- globalState.update(previewColumnKey, undefined);
- });
- }
- global.backupTabs && global.backupTabs();
- }
- // Generate HTML content with an iframe
- 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>Web Preview</title>
- <style>
- body { margin: 0; padding: 0; }
- iframe { width: 100vw; height: 100vh; border: none; }
- </style>
- </head>
- <body>
- <iframe src="${url}" sandbox="allow-scripts allow-same-origin"></iframe>
- </body>
- </html>`;
- }
- function getCurrentPreviewState() {
- return {
- url: _context.globalState.get(previewKey),
- group: _context.globalState.get(previewColumnKey) || vscode.ViewColumn.One
- }
- }
- module.exports = { initFrame, runFrame, getCurrentPreviewState };
|