const vscode = require('vscode'); let templateFn = require('./iframeTemplateFn.js') // Example array with files and URLs (Webview links) let openTabs = require('./openTabs.js') // Track the last tab in each group for tab order let groupTabs = {}; // Tracks the last opened tab in each group // Function to open files or URLs based on the group async function openFilesAndURLs({beforeFn}) { const openPromises = []; const workspaceFolder = vscode.workspace.workspaceFolders && vscode.workspace.workspaceFolders[0]; if (!workspaceFolder) { vscode.window.showErrorMessage('No workspace folder is open. Please open a folder or workspace.'); return; } if (openTabs?.isIgnore) { return; } let groups = openTabs?.groups || [] if (beforeFn) { await beforeFn(); } groups.forEach((group, groupIndex) => { const groupId = groupIndex + 1; // Group ID corresponds to column // Open files first, in order group.filter(item => item.relPath).forEach((item, index) => { const filePath = vscode.Uri.joinPath(workspaceFolder.uri, item.relPath); openPromises.push(vscode.workspace.openTextDocument(filePath) .then(doc => { // Use groupId as the column index const columnToUse = groupId; // Manage the tab order per group let lastTab = groupTabs[groupId] || 1; // Start at tab 1 by default vscode.window.showTextDocument(doc, { viewColumn: columnToUse, preview: false }).then(() => { groupTabs[groupId] = lastTab + 1; // Increment tab order for next file }); })); }); // After files, open Webview (iframe) last for this group group.filter(item => item.url).forEach(item => { openPromises.push(openWebview(item.url, groupId)); }); }); // Wait for all files/URLs to be opened Promise.all(openPromises).then(() => { console.log('All files and URLs have been opened.'); }).catch(err => { console.error('Error opening files or URLs:', err); }); } // Function to open a URL in a Webview (iframe) with an editable command line and refresh button function openWebview(url, groupId) { const panel = vscode.window.createWebviewPanel( 'urlPreview', // Panel ID 'Webview Preview!!', // Panel title vscode.ViewColumn.One, // Default to the first column { enableScripts: true, // Allow running JavaScript in the Webview localResourceRoots: [] // Define where local resources can be loaded from (if needed) } ); // Adjust the column index to match the groupId panel.reveal(vscode.ViewColumn.One + groupId - 1); // Adjust column index based on groupId // Initialize the HTML content for the Webview with a command line input and refresh button panel.webview.html = getWebviewContent(url); // Add a message listener to handle the URL refresh command from the webview panel.webview.onDidReceiveMessage((message) => { if (message.command === 'refreshUrl') { // Update the iframe URL with the new one entered in the input field panel.webview.html = getWebviewContent(message.url); } }); } // Helper function to generate the HTML for the Webview with an editable command line and refresh button function getWebviewContent(url) { return templateFn(url); } module.exports = openFilesAndURLs