main.js 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. // The module 'vscode' contains the VS Code extensibility API
  2. // Import the module and reference it with the alias vscode in your code below
  3. const vscode = require('vscode');
  4. const openFilesAndURLs = require('./openFilesAndUrls.js')
  5. const {initFrame, runFrame, getCurrentPreviewState} = require('./openFrame.js')
  6. const {updateCount, initTreeView} = require('./updateCount.js')
  7. // const savedTabsPath =
  8. // const path = require('path');
  9. // This method is called when your extension is activated
  10. // Your extension is activated the very first time the command is executed
  11. /**
  12. *
  13. * @param {vscode.ExtensionContext} context
  14. */
  15. console.log("ITK LOGS!!!!!")
  16. let _context;
  17. function activate(context) {
  18. console.log('Congratulations, your extension "itk" is now active!');
  19. initTreeView(context);
  20. initCmdInVsCodeTerminal(context)
  21. initOpenChromeUrl(context)
  22. initCloseTabs(context)
  23. // initHtml(context, getTemplate())
  24. // initWelcome(context)
  25. // initTests(context)
  26. initFrame(context)
  27. initFileChanges(context)
  28. initGetTabs(context)
  29. startAction(context);
  30. }
  31. async function startAction(context) {
  32. _context = context;
  33. updateCount(123)
  34. // runFrame('https://itrum.ru')
  35. // openFilesAndURLs({beforeFn: closeAllTabs})
  36. // openFilesAndURLs({beforeFn: () => {}})
  37. // openHtml()
  38. // openWelcomeFrame()
  39. // openTestsFrame()
  40. // setTimeout(() => {
  41. // getTabs();
  42. // })
  43. // openTabsDirectly()
  44. // openUrlInChrome();
  45. }
  46. function initFileChanges(context) {
  47. let disposable = vscode.window.onDidChangeVisibleTextEditors(backupTabs);
  48. context.subscriptions.push(disposable); // Add to subscriptions to prevent memory leaks
  49. }
  50. async function backupTabs() {
  51. let v = await getTabs()
  52. writeFile('./../files/backupTabs.js', `module.exports = ${JSON.stringify(v, null, 4)}`)
  53. }
  54. global.backupTabs = backupTabs;
  55. function writeFile(relPath, content) {
  56. const filePath = path.join(__dirname, relPath);
  57. fs.writeFileSync(filePath, content);
  58. }
  59. async function openStartTabs(groups) {
  60. const openPromises = [];
  61. const workspaceFolder = vscode.workspace.workspaceFolders && vscode.workspace.workspaceFolders[0];
  62. let columnIndex = 1; // Start from the first column
  63. groups.forEach((group, index) => {
  64. group.forEach((file) => {
  65. if (file.url) {
  66. // If the object contains a URL, open it in a Webview (iframe)
  67. // openPromises.push( runFrame(file));
  68. runFrame(file, columnIndex + index)
  69. } else if (file.relPath) {
  70. // Resolve the file path relative to the workspace folder
  71. const filePath = vscode.Uri.joinPath(workspaceFolder.uri, file.relPath);
  72. // Open the file in a new tab (in a new column, if needed)
  73. openPromises.push(vscode.workspace.openTextDocument(filePath)
  74. .then(doc => {
  75. // If it's the first file of a group, open it in the first column
  76. const columnToUse = columnIndex + index;
  77. // Show the document in the appropriate column
  78. vscode.window.showTextDocument(doc, { viewColumn: columnToUse, preview: false });
  79. // Move to the next column after opening each file
  80. }));
  81. }
  82. });
  83. });
  84. }
  85. function initGetTabs(context) {
  86. let disposable = vscode.commands.registerCommand('itk.getTabs', () => getTabs());
  87. context.subscriptions.push(disposable);
  88. }
  89. function getTabs() {
  90. const tabGroups = vscode.window.tabGroups.all;
  91. if (tabGroups.length === 0) {
  92. vscode.window.showInformationMessage('No open tabs found.');
  93. return;
  94. }
  95. let tabInfo = [];
  96. const workspaceFolder = vscode.workspace.workspaceFolders?.[0]?.uri.fsPath; // Get workspace folder
  97. tabGroups.forEach((group, index) => {
  98. let curTabsInGroup = []
  99. group.tabs.forEach(tab => {
  100. let filePath = "";
  101. if (tab.input instanceof vscode.TabInputText) {
  102. filePath = tab.input.uri.fsPath;
  103. }
  104. let relativePath = "";
  105. if (filePath) {
  106. relativePath = path.relative(workspaceFolder, filePath); // Use path.relative
  107. }
  108. curTabsInGroup.push({
  109. group: tab.group.viewColumn,
  110. label: tab.label,
  111. isActive: tab.isActive,
  112. isPinned: tab.isPinned,
  113. absPath: filePath,
  114. relPath: relativePath,
  115. });
  116. });
  117. tabInfo.push(curTabsInGroup)
  118. });
  119. vscode.window.showInformationMessage(`Opened Tabs:\n${tabInfo.join('\n')}`);
  120. return {tabs: tabInfo, preview: getCurrentPreviewState()};
  121. }
  122. function initOpenChromeUrl(context) {
  123. let disposable = vscode.commands.registerCommand('itk.openUrlInBrowser', async () => {
  124. openUrlInChrome()
  125. });
  126. context.subscriptions.push(disposable);
  127. }
  128. async function openUrlInChrome() {
  129. let url = 'https://new.csb.app';
  130. // if (url) {
  131. // try {
  132. // new URL(url); // Validate URL
  133. // } catch (error) {
  134. // vscode.window.showErrorMessage("Invalid URL: " + error.message);
  135. // return;
  136. // }
  137. // // Dynamic import:
  138. // try {
  139. // const open = await import('open'); // Correct way to import ES module
  140. // await open.default(url); // Use open.default to call the function
  141. // } catch (error) {
  142. // vscode.window.showErrorMessage("Could not open URL: " + error.message);
  143. // console.error("Open URL Error:", error);
  144. // }
  145. // }
  146. }
  147. function initCloseTabs(context) {
  148. let disposable = vscode.commands.registerCommand('itk.closeAllTabs', closeAllTabs);
  149. context.subscriptions.push(disposable);
  150. }
  151. async function closeAllTabs() {
  152. try {
  153. await vscode.commands.executeCommand('workbench.action.closeAllEditors');
  154. } catch (e) {
  155. vscode.window.showInformationMessage("Tabs Closing Error");
  156. }
  157. }
  158. function initCmdInVsCodeTerminal(context) {
  159. const disposable = vscode.commands.registerCommand('itk.openTabs', () => restoreBackupTabs());
  160. context.subscriptions.push(disposable);
  161. }
  162. function restoreBackupTabs() {
  163. setTimeout(() => {
  164. vscode.window.showInformationMessage('Tabs Restored From backup');
  165. }, 500)
  166. }
  167. // This method is called when your extension is deactivated
  168. function deactivate() {
  169. console.log("ITK DEACTIVE")
  170. }
  171. module.exports = {
  172. activate,
  173. deactivate
  174. }