// The module 'vscode' contains the VS Code extensibility API // Import the module and reference it with the alias vscode in your code below const vscode = require('vscode'); const openFilesAndURLs = require('./openFilesAndUrls.js') // const path = require('path'); // This method is called when your extension is activated // Your extension is activated the very first time the command is executed /** * * @param {vscode.ExtensionContext} context */ console.log("ITK LOGS!!!!!") let treeView; let _context; function activate(context) { console.log('Congratulations, your extension "itk" is now active!'); initTreeView(context); initCmdInVsCodeTerminal(context) initOpenChromeUrl(context) initCloseTabs(context) initHtml(context, getTemplate()) initWelcome(context) initTests(context) initGetTabs(context) startAction(context); } async function startAction(context) { _context = context; updateCount(127) openFilesAndURLs({beforeFn: closeAllTabs}) // openHtml() // openWelcomeFrame() // openTestsFrame() setTimeout(() => { getTabs(); }) // openTabsDirectly() // openUrlInChrome(); } async function openStartTabs(groups) { const openPromises = []; const workspaceFolder = vscode.workspace.workspaceFolders && vscode.workspace.workspaceFolders[0]; let columnIndex = 1; // Start from the first column groups.forEach((group, index) => { group.forEach((file) => { if (file.url) { // If the object contains a URL, open it in a Webview (iframe) // openPromises.push( runFrame(file)); runFrame(file, columnIndex + index) } else if (file.relPath) { // Resolve the file path relative to the workspace folder const filePath = vscode.Uri.joinPath(workspaceFolder.uri, file.relPath); // Open the file in a new tab (in a new column, if needed) openPromises.push(vscode.workspace.openTextDocument(filePath) .then(doc => { // If it's the first file of a group, open it in the first column const columnToUse = columnIndex + index; // Show the document in the appropriate column vscode.window.showTextDocument(doc, { viewColumn: columnToUse, preview: false }); // Move to the next column after opening each file })); } }); }); } function initGetTabs(context) { let disposable = vscode.commands.registerCommand('itk.getTabs', () => { getTabs() }); // Add to your extension's subscriptions context.subscriptions.push(disposable); } function getTabs() { const tabGroups = vscode.window.tabGroups.all; if (tabGroups.length === 0) { vscode.window.showInformationMessage('No open tabs found.'); return; } let tabInfo = []; tabGroups.forEach((group, index) => { group.tabs.forEach(tab => { let filePath = "Unknown"; // Check if the tab is a text file and extract its full path if (tab.input instanceof vscode.TabInputText) { filePath = tab.input.uri.fsPath; } tabInfo.push(`Group ${index + 1}: ${tab.label} - ${filePath}`); }); }); vscode.window.showInformationMessage(`Opened Tabs:\n${tabInfo.join('\n')}`); console.log('tabInfo', tabInfo) return tabGroups; } function initOpenChromeUrl(context) { let disposable = vscode.commands.registerCommand('itk.openUrlInBrowser', async () => { openUrlInChrome() }); context.subscriptions.push(disposable); } async function openUrlInChrome() { let url = 'https://new.csb.app'; // if (url) { // try { // new URL(url); // Validate URL // } catch (error) { // vscode.window.showErrorMessage("Invalid URL: " + error.message); // return; // } // // Dynamic import: // try { // const open = await import('open'); // Correct way to import ES module // await open.default(url); // Use open.default to call the function // } catch (error) { // vscode.window.showErrorMessage("Could not open URL: " + error.message); // console.error("Open URL Error:", error); // } // } } function initCloseTabs(context) { let disposable = vscode.commands.registerCommand('itk.closeAllTabs', () => { // Call your close all tabs logic here (the code from above) closeAllTabs() }); context.subscriptions.push(disposable); } async function closeAllTabs() { try { await vscode.commands.executeCommand('workbench.action.closeAllEditors'); } catch (e) { vscode.window.showInformationMessage("Tabs Closing Error"); } } function initHtml(context, defaultUrl) { context.subscriptions.push( vscode.commands.registerCommand('itk.openWelcome', () => { openHtml(context, defaultUrl); }) ); } function openHtml(context, html) { context = context || _context; html = html || getTemplate(); const panel = vscode.window.createWebviewPanel( 'HTML ITK', // Identifies the webview 'ITK HTML', // Title of the panel vscode.ViewColumn.One, // Show in the first column { enableScripts: true, // Allows JavaScript execution } ); // Set HTML content for the Webview panel.webview.html = html panel.webview.onDidReceiveMessage(message => { console.log('message', message, message.command) switch (message.command) { case 'buttonClicked': vscode.window.showInformationMessage('Button clicked!' + new Date().getTime()); return; } }, undefined, context.subscriptions); } 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 ` ITK Academy `; } } function updateCount(newMessagesCount) { treeView.badge = { value: newMessagesCount, tooltip: `You have ${newMessagesCount} new documentation` }; } function initCmdInVsCodeTerminal(context) { const disposable = vscode.commands.registerCommand('itk.openTabs', () => openTabsDirectly()); context.subscriptions.push(disposable); } function initTreeView(context) { const view = vscode.window.createTreeView('itk.mainView', { treeDataProvider: new TabSaverTreeDataProvider(['HTML', 'Preview', 'Close Tabs']) }); context.subscriptions.push(view); treeView = view; const view2 = vscode.window.createTreeView('itk.runView', { treeDataProvider: new TabSaverTreeDataProvider(['Run Tests', 'Close Tabs']) }); context.subscriptions.push(view); context.subscriptions.push(view2); console.log('view', view) view.onDidChangeSelection((event) => treeClickHandler(event, context)); view2.onDidChangeSelection((event) => treeClickHandler(event, context)); } function treeClickHandler(event, context) { if (event.selection.length > 0) { const selectedItem = event.selection[0]; console.log(`Clicked on: ${selectedItem}`); // Log the clicked item's label let cmds = { Preview: openWelcomeFrame, HTML: () => openHtml(null, '

asdfasdf

'), 'Run Tests': openTestsFrame, 'Close Tabs': closeAllTabs } let fn = cmds[selectedItem]; fn && fn(); } } class TabSaverTreeDataProvider { constructor(tabs) { this.tabs = tabs; } getTreeItem(element) { console.log('element', element); return new vscode.TreeItem(element); } getChildren(element) { console.log('element', element); // If no element is provided, return the root elements (tabs) if (!element) { return Promise.resolve(this.tabs); } // If an element is provided, you can handle it accordingly return Promise.resolve([]); } } function openTabsDirectly(tabs) { setTimeout(() => { if (tabs.length === 0) { vscode.window.showInformationMessage('No tabs saved yet.'); return; } // JSON.parse(fs.readFileSync(saveFilePath, 'utf-8')); // No need for "as string[]" console.log("tabssssss", tabs) tabs.forEach(tabPath => { const uri = vscode.Uri.file(tabPath); // Create a Uri object vscode.workspace.openTextDocument(uri).then(doc => { vscode.window.showTextDocument(doc, { viewColumn: vscode.ViewColumn.Beside }); // Open beside }, error => { console.error(`Error opening ${tabPath}: ${error}`); vscode.window.showErrorMessage(`Error opening ${tabPath}: ${error}`); }); }); vscode.window.showInformationMessage('Hello World from itk!'); }, 500) } function getTemplate() { return ` Cat Coding

Welcome to ITK.academy!!!!!

This is a custom welcome view.

You can add your own HTML content here.

`; } // This method is called when your extension is deactivated function deactivate() { console.log("ITK DEACTIVE") } module.exports = { activate, deactivate }