|
@@ -16,6 +16,7 @@ import {store} from "../../redux/store";
|
|
|
import notification from "../../components/notification";
|
|
import notification from "../../components/notification";
|
|
|
import {message} from "antd";
|
|
import {message} from "antd";
|
|
|
|
|
|
|
|
|
|
+//简写与语言的对应关系
|
|
|
const extToLang:{[prop:string]: string} = {
|
|
const extToLang:{[prop:string]: string} = {
|
|
|
'html': 'html',
|
|
'html': 'html',
|
|
|
'js': 'javascript',
|
|
'js': 'javascript',
|
|
@@ -28,52 +29,82 @@ const extToLang:{[prop:string]: string} = {
|
|
|
'md': 'markdown'
|
|
'md': 'markdown'
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+//初始化文件树
|
|
|
export async function initFolderTree() {
|
|
export async function initFolderTree() {
|
|
|
|
|
+ //发布订阅模式,每当 dispatch action 的时候就会执行
|
|
|
store.subscribe(async () => {
|
|
store.subscribe(async () => {
|
|
|
|
|
+ //如果状态处于正在加载项目状态
|
|
|
if (store.getState().loadProject.isLoading) {
|
|
if (store.getState().loadProject.isLoading) {
|
|
|
|
|
+ //获取当前打开的所有Tab
|
|
|
const groups = molecule.editor.getState().groups
|
|
const groups = molecule.editor.getState().groups
|
|
|
|
|
+
|
|
|
const hide = message.loading('Please wait, Project is loading...', 0)
|
|
const hide = message.loading('Please wait, Project is loading...', 0)
|
|
|
|
|
+ //获取要加载项目的文件树
|
|
|
const res = await API.getFolderTree();
|
|
const res = await API.getFolderTree();
|
|
|
|
|
+ //提示:正在加载,请稍后
|
|
|
hide()
|
|
hide()
|
|
|
|
|
+ //先将现在打开的所有Tab关闭
|
|
|
groups?.forEach((group) => {
|
|
groups?.forEach((group) => {
|
|
|
molecule.editor.closeAll(group.id)
|
|
molecule.editor.closeAll(group.id)
|
|
|
})
|
|
})
|
|
|
|
|
+ //如果获取文件树错误,弹出错误提示:错误描述
|
|
|
if (res.id === 'error') {
|
|
if (res.id === 'error') {
|
|
|
message.error(res.data.description)
|
|
message.error(res.data.description)
|
|
|
}
|
|
}
|
|
|
|
|
+ //如果获取文件树成功,则弹出成功提示:加载已完成
|
|
|
else message.success('Loading is Complete')
|
|
else message.success('Loading is Complete')
|
|
|
|
|
+ //如果系统状态是加载已完成
|
|
|
if (!store.getState().loadProject.isLoading && store.getState().loadProject.isValid) {
|
|
if (!store.getState().loadProject.isLoading && store.getState().loadProject.isValid) {
|
|
|
|
|
+
|
|
|
const folderTreeData = cloneDeep(res);
|
|
const folderTreeData = cloneDeep(res);
|
|
|
|
|
+ //重置文件树数据
|
|
|
molecule.folderTree.reset();
|
|
molecule.folderTree.reset();
|
|
|
|
|
+ //添加新的文件树数据
|
|
|
molecule.folderTree.add(folderTreeData);
|
|
molecule.folderTree.add(folderTreeData);
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
})
|
|
})
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+//处理选中文件树节点事件
|
|
|
export function handleSelectFolderTree() {
|
|
export function handleSelectFolderTree() {
|
|
|
|
|
+ //双击文件事件处理
|
|
|
molecule.folderTree.onSelectFile((file: IFolderTreeNodeProps) => {
|
|
molecule.folderTree.onSelectFile((file: IFolderTreeNodeProps) => {
|
|
|
|
|
+ //将这个文件树节点以Tab的形式打开
|
|
|
molecule.editor.open(transformToEditorTab(file))
|
|
molecule.editor.open(transformToEditorTab(file))
|
|
|
|
|
+ //更新状态栏语言
|
|
|
updateStatusBarLanguage(file.data.language);
|
|
updateStatusBarLanguage(file.data.language);
|
|
|
});
|
|
});
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+//处理重命名事件
|
|
|
export function handleRenameFile(){
|
|
export function handleRenameFile(){
|
|
|
|
|
+ //处理文件名更新事件
|
|
|
molecule.folderTree.onUpdateFileName(file => {
|
|
molecule.folderTree.onUpdateFileName(file => {
|
|
|
|
|
+ //文件树node解构出这些信息
|
|
|
const {name, id, location, data} = file
|
|
const {name, id, location, data} = file
|
|
|
|
|
+ //分离出文件名和后缀
|
|
|
const frag = name!.split('.');
|
|
const frag = name!.split('.');
|
|
|
|
|
+ //找到父节点,也就是其所属文件夹
|
|
|
const parentNode = molecule.folderTree.getParentNode(id);
|
|
const parentNode = molecule.folderTree.getParentNode(id);
|
|
|
let count = -1;
|
|
let count = -1;
|
|
|
|
|
+ //查找父文件夹的孩子信息,是否存在这个文件,存在几个
|
|
|
if (parentNode && parentNode.children){
|
|
if (parentNode && parentNode.children){
|
|
|
parentNode.children.forEach(((value) => value.name === name?count++:count+=0))
|
|
parentNode.children.forEach(((value) => value.name === name?count++:count+=0))
|
|
|
}
|
|
}
|
|
|
|
|
+ //如果在同一个文件夹下出现了1个以上同名文件,那么这个文件的名字为已更新的名字添加后缀count
|
|
|
if (count>0) frag![0] = frag![0] + `(${count})`;
|
|
if (count>0) frag![0] = frag![0] + `(${count})`;
|
|
|
|
|
+ //更新后拼接上名字
|
|
|
const newName = frag.join('.');
|
|
const newName = frag.join('.');
|
|
|
|
|
+ //文件的扩展名
|
|
|
const ext = frag[frag.length-1];
|
|
const ext = frag[frag.length-1];
|
|
|
|
|
+ //转化成语言
|
|
|
const newLang = extToLang[ext] || 'file'
|
|
const newLang = extToLang[ext] || 'file'
|
|
|
const newLoc = location?.split('/') || [];
|
|
const newLoc = location?.split('/') || [];
|
|
|
|
|
+ //将文件树的这个node的地址中的文件名也更新为新的文件名
|
|
|
newLoc[newLoc.length - 1] = newName;
|
|
newLoc[newLoc.length - 1] = newName;
|
|
|
const newLocation = newLoc.join('/');
|
|
const newLocation = newLoc.join('/');
|
|
|
|
|
+ //创建新的文件树节点,但是id还是那个id
|
|
|
const newFile = {
|
|
const newFile = {
|
|
|
...file,
|
|
...file,
|
|
|
id,
|
|
id,
|
|
@@ -85,29 +116,41 @@ export function handleRenameFile(){
|
|
|
language: newLang
|
|
language: newLang
|
|
|
}
|
|
}
|
|
|
};
|
|
};
|
|
|
|
|
+ //更新文件树的这个节点
|
|
|
folderTree.update(newFile);
|
|
folderTree.update(newFile);
|
|
|
|
|
+ //更新Tab中的语言
|
|
|
const groupId = molecule.editor.getGroupIdByTab(id.toString());
|
|
const groupId = molecule.editor.getGroupIdByTab(id.toString());
|
|
|
const isValidGroupId = !!groupId || groupId === 0;
|
|
const isValidGroupId = !!groupId || groupId === 0;
|
|
|
|
|
+ //如果这个文件被在编辑器打开了
|
|
|
if (isValidGroupId) {
|
|
if (isValidGroupId) {
|
|
|
const prevTab =
|
|
const prevTab =
|
|
|
molecule.editor.getTabById<BuiltInEditorTabDataType>(
|
|
molecule.editor.getTabById<BuiltInEditorTabDataType>(
|
|
|
id.toString(),
|
|
id.toString(),
|
|
|
groupId
|
|
groupId
|
|
|
);
|
|
);
|
|
|
|
|
+ //创建新的Tab
|
|
|
const newTab: IEditorTab = { id: id.toString(), name , breadcrumb: transformToEditorTab(newFile).breadcrumb};
|
|
const newTab: IEditorTab = { id: id.toString(), name , breadcrumb: transformToEditorTab(newFile).breadcrumb};
|
|
|
const prevTabData = prevTab?.data;
|
|
const prevTabData = prevTab?.data;
|
|
|
newTab.data = { ...prevTabData, language: newLang};
|
|
newTab.data = { ...prevTabData, language: newLang};
|
|
|
|
|
+ //如果当前的正在编辑的文件,现在的激活文件Tab,就是这个被重命名的文件
|
|
|
if (molecule.editor.getState().current?.activeTab === id.toString()) {
|
|
if (molecule.editor.getState().current?.activeTab === id.toString()) {
|
|
|
|
|
+ //更新状态栏语言
|
|
|
updateStatusBarLanguage(newLang);
|
|
updateStatusBarLanguage(newLang);
|
|
|
}
|
|
}
|
|
|
|
|
+ //更新Tab
|
|
|
molecule.editor.updateTab(newTab);
|
|
molecule.editor.updateTab(newTab);
|
|
|
}
|
|
}
|
|
|
})
|
|
})
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+
|
|
|
|
|
+//处理创建新文件事件
|
|
|
export function handleNewFile(){
|
|
export function handleNewFile(){
|
|
|
|
|
+ //当创建新文件时
|
|
|
molecule.folderTree.onCreate((type, nodeId) => {
|
|
molecule.folderTree.onCreate((type, nodeId) => {
|
|
|
|
|
+ //文件夹
|
|
|
if (type == 'Folder'){
|
|
if (type == 'Folder'){
|
|
|
|
|
+ //往文件树中添加文件夹节点
|
|
|
molecule.folderTree.add(
|
|
molecule.folderTree.add(
|
|
|
new TreeNodeModel({
|
|
new TreeNodeModel({
|
|
|
id: `${nodeId}_${new Date().getTime()}`,
|
|
id: `${nodeId}_${new Date().getTime()}`,
|
|
@@ -120,7 +163,9 @@ export function handleNewFile(){
|
|
|
}), nodeId
|
|
}), nodeId
|
|
|
);
|
|
);
|
|
|
}
|
|
}
|
|
|
|
|
+ //文件
|
|
|
else {
|
|
else {
|
|
|
|
|
+ //往文件树添加文件
|
|
|
molecule.folderTree.add(
|
|
molecule.folderTree.add(
|
|
|
new TreeNodeModel({
|
|
new TreeNodeModel({
|
|
|
id: `${nodeId}_${new Date().getTime()}`,
|
|
id: `${nodeId}_${new Date().getTime()}`,
|
|
@@ -135,25 +180,37 @@ export function handleNewFile(){
|
|
|
}
|
|
}
|
|
|
})
|
|
})
|
|
|
}
|
|
}
|
|
|
|
|
+//更新状态栏语言
|
|
|
export function updateStatusBarLanguage(language: string) {
|
|
export function updateStatusBarLanguage(language: string) {
|
|
|
|
|
+ //如果传入的是空,不做处理
|
|
|
if (!language) return;
|
|
if (!language) return;
|
|
|
|
|
+ //语言改成里面的每个词的首字母大写
|
|
|
language = language.toUpperCase();
|
|
language = language.toUpperCase();
|
|
|
|
|
+ //获取状态栏的语言item
|
|
|
const languageStatusItem = molecule.statusBar.getStatusBarItem(STATUS_BAR_LANGUAGE.id, Float.right);
|
|
const languageStatusItem = molecule.statusBar.getStatusBarItem(STATUS_BAR_LANGUAGE.id, Float.right);
|
|
|
|
|
+ //如果存在语言Item
|
|
|
if (languageStatusItem) {
|
|
if (languageStatusItem) {
|
|
|
|
|
+ //更新语言
|
|
|
languageStatusItem.name = language;
|
|
languageStatusItem.name = language;
|
|
|
|
|
+ //更新语言item
|
|
|
molecule.statusBar.update(languageStatusItem, Float.right);
|
|
molecule.statusBar.update(languageStatusItem, Float.right);
|
|
|
|
|
+ //如果不存在语言item,创建新的语言item
|
|
|
} else {
|
|
} else {
|
|
|
molecule.statusBar.add(Object.assign({}, STATUS_BAR_LANGUAGE, { name: language } ), Float.right);
|
|
molecule.statusBar.add(Object.assign({}, STATUS_BAR_LANGUAGE, { name: language } ), Float.right);
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+//处理状态栏语言
|
|
|
export function handleStatusBarLanguage() {
|
|
export function handleStatusBarLanguage() {
|
|
|
const moleculeEditor = molecule.editor;
|
|
const moleculeEditor = molecule.editor;
|
|
|
|
|
+ //当选中了这个Tab,也就是选中这个Tab作为当前编辑的Tab
|
|
|
moleculeEditor.onSelectTab((tabId, groupId) => {
|
|
moleculeEditor.onSelectTab((tabId, groupId) => {
|
|
|
|
|
+ //如果groupId或者tab不存在,则返回
|
|
|
if (!groupId) return;
|
|
if (!groupId) return;
|
|
|
const group = moleculeEditor.getGroupById(groupId);
|
|
const group = moleculeEditor.getGroupById(groupId);
|
|
|
if (!group) return;
|
|
if (!group) return;
|
|
|
const tab: any = moleculeEditor.getTabById(tabId, group.id!);
|
|
const tab: any = moleculeEditor.getTabById(tabId, group.id!);
|
|
|
|
|
+ //如果都存在,则更新状态栏语言
|
|
|
if (tab) {
|
|
if (tab) {
|
|
|
updateStatusBarLanguage(tab.data!.language!);
|
|
updateStatusBarLanguage(tab.data!.language!);
|
|
|
}
|
|
}
|