folderTreeController.ts 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. import molecule from '@dtinsight/molecule';
  2. import {
  3. BuiltInEditorTabDataType,
  4. FileTypes,
  5. Float, IEditorTab,
  6. IFolderTreeNodeProps,
  7. TreeNodeModel
  8. } from '@dtinsight/molecule/esm/model';
  9. import { transformToEditorTab } from '../../common';
  10. import {cloneDeep, forEach} from 'lodash';
  11. import API from '../../api';
  12. import {LoadingTab, STATUS_BAR_LANGUAGE} from './base';
  13. import {folderTree} from "@dtinsight/molecule/esm/molecule.api";
  14. import {store} from "../../redux/store";
  15. import notification from "../../components/notification";
  16. import {message} from "antd";
  17. const extToLang:{[prop:string]: string} = {
  18. 'html': 'html',
  19. 'js': 'javascript',
  20. 'ts': 'typescript',
  21. 'java': 'java',
  22. 'py': 'python',
  23. 'cpp': 'cpp',
  24. 'json': 'json',
  25. 'xml': 'xml',
  26. 'md': 'markdown'
  27. }
  28. export async function initFolderTree() {
  29. store.subscribe(async () => {
  30. if (store.getState().loadProject.isLoading) {
  31. const groups = molecule.editor.getState().groups
  32. const hide = message.loading('Please wait, Project is loading...', 0)
  33. const res = await API.getFolderTree();
  34. hide()
  35. groups?.forEach((group) => {
  36. molecule.editor.closeAll(group.id)
  37. })
  38. if (res.id === 'error') {
  39. message.error(res.data.description)
  40. }
  41. else message.success('Loading is Complete')
  42. if (!store.getState().loadProject.isLoading && store.getState().loadProject.isValid) {
  43. const folderTreeData = cloneDeep(res);
  44. molecule.folderTree.reset();
  45. molecule.folderTree.add(folderTreeData);
  46. }
  47. }
  48. })
  49. }
  50. export function handleSelectFolderTree() {
  51. molecule.folderTree.onSelectFile((file: IFolderTreeNodeProps) => {
  52. molecule.editor.open(transformToEditorTab(file))
  53. updateStatusBarLanguage(file.data.language);
  54. });
  55. }
  56. export function handleRenameFile(){
  57. molecule.folderTree.onUpdateFileName(file => {
  58. const {name, id, location, data} = file
  59. const frag = name!.split('.');
  60. const parentNode = molecule.folderTree.getParentNode(id);
  61. let count = -1;
  62. if (parentNode && parentNode.children){
  63. parentNode.children.forEach(((value) => value.name === name?count++:count+=0))
  64. }
  65. if (count>0) frag![0] = frag![0] + `(${count})`;
  66. const newName = frag.join('.');
  67. const ext = frag[frag.length-1];
  68. const newLang = extToLang[ext] || 'file'
  69. const newLoc = location?.split('/') || [];
  70. newLoc[newLoc.length - 1] = newName;
  71. const newLocation = newLoc.join('/');
  72. const newFile = {
  73. ...file,
  74. id,
  75. name: newName,
  76. location: newLocation,
  77. isEditable: false,
  78. data: {
  79. ...data,
  80. language: newLang
  81. }
  82. };
  83. folderTree.update(newFile);
  84. const groupId = molecule.editor.getGroupIdByTab(id.toString());
  85. const isValidGroupId = !!groupId || groupId === 0;
  86. if (isValidGroupId) {
  87. const prevTab =
  88. molecule.editor.getTabById<BuiltInEditorTabDataType>(
  89. id.toString(),
  90. groupId
  91. );
  92. const newTab: IEditorTab = { id: id.toString(), name , breadcrumb: transformToEditorTab(newFile).breadcrumb};
  93. const prevTabData = prevTab?.data;
  94. newTab.data = { ...prevTabData, language: newLang};
  95. if (molecule.editor.getState().current?.activeTab === id.toString()) {
  96. updateStatusBarLanguage(newLang);
  97. }
  98. molecule.editor.updateTab(newTab);
  99. }
  100. })
  101. }
  102. export function handleNewFile(){
  103. molecule.folderTree.onCreate((type, nodeId) => {
  104. if (type == 'Folder'){
  105. molecule.folderTree.add(
  106. new TreeNodeModel({
  107. id: `${nodeId}_${new Date().getTime()}`,
  108. name: '',
  109. isLeaf: false,
  110. fileType: FileTypes.Folder,
  111. isEditable: true,
  112. data: {
  113. }
  114. }), nodeId
  115. );
  116. }
  117. else {
  118. molecule.folderTree.add(
  119. new TreeNodeModel({
  120. id: `${nodeId}_${new Date().getTime()}`,
  121. name: '',
  122. isLeaf: type === FileTypes.File,
  123. fileType: type,
  124. isEditable: true,
  125. data: {
  126. }
  127. }), nodeId
  128. );
  129. }
  130. })
  131. }
  132. export function updateStatusBarLanguage(language: string) {
  133. if (!language) return;
  134. language = language.toUpperCase();
  135. const languageStatusItem = molecule.statusBar.getStatusBarItem(STATUS_BAR_LANGUAGE.id, Float.right);
  136. if (languageStatusItem) {
  137. languageStatusItem.name = language;
  138. molecule.statusBar.update(languageStatusItem, Float.right);
  139. } else {
  140. molecule.statusBar.add(Object.assign({}, STATUS_BAR_LANGUAGE, { name: language } ), Float.right);
  141. }
  142. }
  143. export function handleStatusBarLanguage() {
  144. const moleculeEditor = molecule.editor;
  145. moleculeEditor.onSelectTab((tabId, groupId) => {
  146. if (!groupId) return;
  147. const group = moleculeEditor.getGroupById(groupId);
  148. if (!group) return;
  149. const tab: any = moleculeEditor.getTabById(tabId, group.id!);
  150. if (tab) {
  151. updateStatusBarLanguage(tab.data!.language!);
  152. }
  153. })
  154. }