import molecule from '@dtinsight/molecule'; import { BuiltInEditorTabDataType, FileTypes, Float, IEditorTab, IFolderTreeNodeProps, TreeNodeModel } from '@dtinsight/molecule/esm/model'; import { transformToEditorTab } from '../../common'; import {cloneDeep, forEach} from 'lodash'; import API from '../../api'; import {LoadingTab, STATUS_BAR_LANGUAGE} from './base'; import {folderTree} from "@dtinsight/molecule/esm/molecule.api"; import {store} from "../../redux/store"; import notification from "../../components/notification"; import {message} from "antd"; const extToLang:{[prop:string]: string} = { 'html': 'html', 'js': 'javascript', 'ts': 'typescript', 'java': 'java', 'py': 'python', 'cpp': 'cpp', 'json': 'json', 'xml': 'xml', 'md': 'markdown' } export async function initFolderTree() { store.subscribe(async () => { if (store.getState().loadProject.isLoading) { const groups = molecule.editor.getState().groups const hide = message.loading('Please wait, Project is loading...', 0) const res = await API.getFolderTree(); hide() groups?.forEach((group) => { molecule.editor.closeAll(group.id) }) if (res.id === 'error') { message.error(res.data.description) } else message.success('Loading is Complete') if (!store.getState().loadProject.isLoading && store.getState().loadProject.isValid) { const folderTreeData = cloneDeep(res); molecule.folderTree.reset(); molecule.folderTree.add(folderTreeData); } } }) } export function handleSelectFolderTree() { molecule.folderTree.onSelectFile((file: IFolderTreeNodeProps) => { molecule.editor.open(transformToEditorTab(file)) updateStatusBarLanguage(file.data.language); }); } export function handleRenameFile(){ molecule.folderTree.onUpdateFileName(file => { const {name, id, location, data} = file const frag = name!.split('.'); const parentNode = molecule.folderTree.getParentNode(id); let count = -1; if (parentNode && parentNode.children){ parentNode.children.forEach(((value) => value.name === name?count++:count+=0)) } if (count>0) frag![0] = frag![0] + `(${count})`; const newName = frag.join('.'); const ext = frag[frag.length-1]; const newLang = extToLang[ext] || 'file' const newLoc = location?.split('/') || []; newLoc[newLoc.length - 1] = newName; const newLocation = newLoc.join('/'); const newFile = { ...file, id, name: newName, location: newLocation, isEditable: false, data: { ...data, language: newLang } }; folderTree.update(newFile); const groupId = molecule.editor.getGroupIdByTab(id.toString()); const isValidGroupId = !!groupId || groupId === 0; if (isValidGroupId) { const prevTab = molecule.editor.getTabById( id.toString(), groupId ); const newTab: IEditorTab = { id: id.toString(), name , breadcrumb: transformToEditorTab(newFile).breadcrumb}; const prevTabData = prevTab?.data; newTab.data = { ...prevTabData, language: newLang}; if (molecule.editor.getState().current?.activeTab === id.toString()) { updateStatusBarLanguage(newLang); } molecule.editor.updateTab(newTab); } }) } export function handleNewFile(){ molecule.folderTree.onCreate((type, nodeId) => { if (type == 'Folder'){ molecule.folderTree.add( new TreeNodeModel({ id: `${nodeId}_${new Date().getTime()}`, name: '', isLeaf: false, fileType: FileTypes.Folder, isEditable: true, data: { } }), nodeId ); } else { molecule.folderTree.add( new TreeNodeModel({ id: `${nodeId}_${new Date().getTime()}`, name: '', isLeaf: type === FileTypes.File, fileType: type, isEditable: true, data: { } }), nodeId ); } }) } export function updateStatusBarLanguage(language: string) { if (!language) return; language = language.toUpperCase(); const languageStatusItem = molecule.statusBar.getStatusBarItem(STATUS_BAR_LANGUAGE.id, Float.right); if (languageStatusItem) { languageStatusItem.name = language; molecule.statusBar.update(languageStatusItem, Float.right); } else { molecule.statusBar.add(Object.assign({}, STATUS_BAR_LANGUAGE, { name: language } ), Float.right); } } export function handleStatusBarLanguage() { const moleculeEditor = molecule.editor; moleculeEditor.onSelectTab((tabId, groupId) => { if (!groupId) return; const group = moleculeEditor.getGroupById(groupId); if (!group) return; const tab: any = moleculeEditor.getTabById(tabId, group.id!); if (tab) { updateStatusBarLanguage(tab.data!.language!); } }) }