123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394 |
- // 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 `<!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="UTF-8">
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
- <title>ITK Academy</title>
- <style>
- body { margin: 0; padding: 0; display: flex; height: 100vh; }
- iframe { width: 100%; height: 100%; border: none; }
- </style>
- </head>
- <body>
- <iframe src="${url}"></iframe>
- </body>
- </html>`;
- }
- }
- 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, '<h1>asdfasdf</h1>'),
- '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 `<!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="UTF-8">
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
- <title>Cat Coding</title>
- </head>
- <body>
- <h1>Welcome to ITK.academy!!!!!</h1>
- <p>This is a custom welcome view.</p>
- <p>You can add your own HTML content here.</p>
- <button id="myButton">Click Me</button>
- <script>
- const vscode = acquireVsCodeApi();
- document.getElementById('myButton').addEventListener('click', () => {
- vscode.postMessage({ command: 'buttonClicked' });
- });
- </script>
- </body>
- </html>`;
- }
- // This method is called when your extension is deactivated
- function deactivate() {
- console.log("ITK DEACTIVE")
- }
- module.exports = {
- activate,
- deactivate
- }
|