openFilesAndUrls.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. const vscode = require('vscode');
  2. let templateFn = require('./iframeTemplateFn.js')
  3. // Example array with files and URLs (Webview links)
  4. let openTabs = require('./openTabs.js')
  5. // Track the last tab in each group for tab order
  6. let groupTabs = {}; // Tracks the last opened tab in each group
  7. // Function to open files or URLs based on the group
  8. async function openFilesAndURLs({beforeFn}) {
  9. const openPromises = [];
  10. const workspaceFolder = vscode.workspace.workspaceFolders && vscode.workspace.workspaceFolders[0];
  11. if (!workspaceFolder) {
  12. vscode.window.showErrorMessage('No workspace folder is open. Please open a folder or workspace.');
  13. return;
  14. }
  15. if (openTabs?.isIgnore) {
  16. return;
  17. }
  18. let groups = openTabs?.groups || []
  19. if (beforeFn) {
  20. await beforeFn();
  21. }
  22. groups.forEach((group, groupIndex) => {
  23. const groupId = groupIndex + 1; // Group ID corresponds to column
  24. // Open files first, in order
  25. group.filter(item => item.relPath).forEach((item, index) => {
  26. const filePath = vscode.Uri.joinPath(workspaceFolder.uri, item.relPath);
  27. openPromises.push(vscode.workspace.openTextDocument(filePath)
  28. .then(doc => {
  29. // Use groupId as the column index
  30. const columnToUse = groupId;
  31. // Manage the tab order per group
  32. let lastTab = groupTabs[groupId] || 1; // Start at tab 1 by default
  33. vscode.window.showTextDocument(doc, { viewColumn: columnToUse, preview: false }).then(() => {
  34. groupTabs[groupId] = lastTab + 1; // Increment tab order for next file
  35. });
  36. }));
  37. });
  38. // After files, open Webview (iframe) last for this group
  39. group.filter(item => item.url).forEach(item => {
  40. openPromises.push(openWebview(item.url, groupId));
  41. });
  42. });
  43. // Wait for all files/URLs to be opened
  44. Promise.all(openPromises).then(() => {
  45. console.log('All files and URLs have been opened.');
  46. }).catch(err => {
  47. console.error('Error opening files or URLs:', err);
  48. });
  49. }
  50. // Function to open a URL in a Webview (iframe) with an editable command line and refresh button
  51. function openWebview(url, groupId) {
  52. const panel = vscode.window.createWebviewPanel(
  53. 'urlPreview', // Panel ID
  54. 'Webview Preview!!', // Panel title
  55. vscode.ViewColumn.One, // Default to the first column
  56. {
  57. enableScripts: true, // Allow running JavaScript in the Webview
  58. localResourceRoots: [] // Define where local resources can be loaded from (if needed)
  59. }
  60. );
  61. // Adjust the column index to match the groupId
  62. panel.reveal(vscode.ViewColumn.One + groupId - 1); // Adjust column index based on groupId
  63. // Initialize the HTML content for the Webview with a command line input and refresh button
  64. panel.webview.html = getWebviewContent(url);
  65. // Add a message listener to handle the URL refresh command from the webview
  66. panel.webview.onDidReceiveMessage((message) => {
  67. if (message.command === 'refreshUrl') {
  68. // Update the iframe URL with the new one entered in the input field
  69. panel.webview.html = getWebviewContent(message.url);
  70. }
  71. });
  72. }
  73. // Helper function to generate the HTML for the Webview with an editable command line and refresh button
  74. function getWebviewContent(url) {
  75. return templateFn(url);
  76. }
  77. module.exports = openFilesAndURLs