Skip to content

Commit

Permalink
feat: code format premitter
Browse files Browse the repository at this point in the history
code format premitter
  • Loading branch information
zhangtengjin authored and wewoor committed Feb 25, 2021
1 parent 6c80a4f commit 519a651
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 48 deletions.
4 changes: 2 additions & 2 deletions src/controller/explorer/explorer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
explorerService,
} from 'mo';
import * as React from 'react';
import { IFolderTree } from 'mo/model'
import { IFolderTree } from 'mo/model';
import { ExplorerView, FolderTreeView } from 'mo/workbench/sidebar/explore';
import { IActionBarItem } from 'mo/components/actionBar';

Expand Down Expand Up @@ -100,7 +100,7 @@ export class ExplorerController
id: 'new_file',
title: 'New File',
iconName: 'codicon-new-file',
onClick: () => { },
onClick: () => {},
},
{
id: 'new_folder',
Expand Down
2 changes: 1 addition & 1 deletion src/controller/explorer/folderTree.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export class FolderTreeController
this.initView();
}

private initView() { }
private initView() {}

public readonly onSelectFile = (file: ITreeNodeItem) => {
const tabData = {
Expand Down
109 changes: 64 additions & 45 deletions src/services/workbench/explorerService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ export class TreeView implements ITreeInterface {
1,
{
...node,
...extra
...extra,
}
);
this.updateChildren(parentIndex[this.childNodeName]);
Expand All @@ -164,14 +164,12 @@ export class TreeView implements ITreeInterface {

updateChildren(children: IIndex) {
const self = this;
children.forEach(
function (id, i) {
const index = self.getIndex(id);
index.prev = index.next = null;
if (i > 0) index.prev = children[i - 1];
if (i < children.length - 1) index.next = children[i + 1];
}
);
children.forEach(function (id, i) {
const index = self.getIndex(id);
index.prev = index.next = null;
if (i > 0) index.prev = children[i - 1];
if (i < children.length - 1) index.next = children[i + 1];
});
}

insert(obj: ITreeNodeItem, parentId: number, i: number) {
Expand Down Expand Up @@ -222,7 +220,6 @@ export class TreeView implements ITreeInterface {
}
}


export interface IExplorerService extends Component<IExplorer> {
addPanel(panel: IPanelItem | IPanelItem[]): void;
reset(): void;
Expand Down Expand Up @@ -275,13 +272,16 @@ export class ExplorerService
next.splice(index, 1);
}
this.setState({
data: next
data: next,
});
}

/* ============================Tree============================ */

private getFileIconByExtensionName(name: string, fileType: FileType): string {
private getFileIconByExtensionName(
name: string,
fileType: FileType
): string {
if (fileType === FileTypes.FOLDER) return '';
const fileExtension = name && name.split('.')?.[1];
let icon = 'symbol-file';
Expand Down Expand Up @@ -310,15 +310,19 @@ export class ExplorerService

private getCurrentRootFolderAndIndex(id: number) {
const currentRootFolder: ITreeNodeItem = this.getRootFolderById(id);
const index = this.getRootFolderIndexByRootId((currentRootFolder as any).id) as number;
const index = this.getRootFolderIndexByRootId(
(currentRootFolder as any).id
) as number;
return {
index,
currentRootFolder
}
currentRootFolder,
};
}

public getRootFolderIndexByRootId(id: number): number | undefined {
return this.state.folderTree?.data!.findIndex((folder) => folder.id === id);
return this.state.folderTree?.data!.findIndex(
(folder) => folder.id === id
);
}

public getRootFolderByRootId(id: number): ITreeNodeItem | undefined {
Expand All @@ -327,12 +331,12 @@ export class ExplorerService

public getRootFolderById(id: number): ITreeNodeItem {
let rootNode = {};
this.state.folderTree?.data?.forEach(folder => {
this.state.folderTree?.data?.forEach((folder) => {
const treeInstance = new TreeView(folder);
if (treeInstance.get(id)) {
rootNode = folder;
}
})
});
return rootNode;
}

Expand All @@ -345,7 +349,7 @@ export class ExplorerService
next?.push(folder);
}
this.setState({
folderTree: { ...folderTree, data: next }
folderTree: { ...folderTree, data: next },
});
}

Expand All @@ -357,95 +361,111 @@ export class ExplorerService
next.splice(index, 1);
}
this.setState({
folderTree: { ...folderTree, data: next }
folderTree: { ...folderTree, data: next },
});
}

public updateFile(file, callback) {
const { folderTree } = this.state;
const { id, name, fileType } = file;
const cloneData: ITreeNodeItem[] = folderTree?.data || [];
const { currentRootFolder, index } = this.getCurrentRootFolderAndIndex(id)
const { currentRootFolder, index } = this.getCurrentRootFolderAndIndex(
id
);
const tree = new TreeView(currentRootFolder);
if (name) {
tree.update(id, {
...file,
icon: this.getFileIconByExtensionName(name, fileType),
modify: false
})
modify: false,
});
} else {
tree.remove(id)
tree.remove(id);
}
if (index > -1) cloneData[index] = tree.obj;
this.setState({
folderTree: { ...folderTree, data: cloneData }
folderTree: { ...folderTree, data: cloneData },
});
if (callback) callback();
}

public rename(id: number, callback?: Function) {
const { folderTree } = this.state;
const cloneData: ITreeNodeItem[] = folderTree?.data || [];
const { currentRootFolder, index } = this.getCurrentRootFolderAndIndex(id)
const { currentRootFolder, index } = this.getCurrentRootFolderAndIndex(
id
);
const tree = new TreeView(currentRootFolder);
tree.update(id, {
modify: true
})
modify: true,
});
if (index > -1) cloneData[index] = tree.obj;
this.setState({
folderTree: { ...folderTree, data: cloneData }
folderTree: { ...folderTree, data: cloneData },
});
if (callback) callback();
}

public delete(id: number, callback?: Function) {
const { folderTree } = this.state;
const cloneData: ITreeNodeItem[] = folderTree?.data || [];
const { currentRootFolder, index } = this.getCurrentRootFolderAndIndex(id)
const { currentRootFolder, index } = this.getCurrentRootFolderAndIndex(
id
);
const tree = new TreeView(currentRootFolder);
tree.remove(id);
if (index > -1) cloneData[index] = tree.obj;
this.setState({
folderTree: { ...folderTree, data: cloneData }
folderTree: { ...folderTree, data: cloneData },
});
if (callback) callback();
}

public newFile(parentId: number, callback?: Function) {
const { folderTree } = this.state;
const cloneData: ITreeNodeItem[] = folderTree?.data || [];
const { currentRootFolder, index } = this.getCurrentRootFolderAndIndex(parentId)
const { currentRootFolder, index } = this.getCurrentRootFolderAndIndex(
parentId
);
const tree = new TreeView(currentRootFolder);
if (!parentId) {
const tabData = {
id: `${Math.random() * 10 + 1}`,
name: `Untitled`,
modified: false
modified: false,
};
editorService.open(tabData)
editorService.open(tabData);
}
tree.append(new TreeNodeModel({
modify: true
}), parentId)
tree.append(
new TreeNodeModel({
modify: true,
}),
parentId
);
if (index > -1) cloneData[index] = tree.obj;
this.setState({
folderTree: { ...folderTree, data: cloneData }
folderTree: { ...folderTree, data: cloneData },
});
if (callback) callback();
}

public newFolder(parentId, callback: Function) {
const { folderTree } = this.state;
const cloneData: ITreeNodeItem[] = folderTree?.data || [];
const { currentRootFolder, index } = this.getCurrentRootFolderAndIndex(parentId)
const { currentRootFolder, index } = this.getCurrentRootFolderAndIndex(
parentId
);
const tree = new TreeView(currentRootFolder);
tree.append(new TreeNodeModel({
fileType: FileTypes.FOLDER as FileType,
modify: true
}), parentId)
tree.append(
new TreeNodeModel({
fileType: FileTypes.FOLDER as FileType,
modify: true,
}),
parentId
);
if (index > -1) cloneData[index] = tree.obj;
this.setState({
folderTree: { ...folderTree, data: cloneData }
folderTree: { ...folderTree, data: cloneData },
});
if (callback) callback();
}
Expand All @@ -457,5 +477,4 @@ export class ExplorerService
}),
});
};

}

0 comments on commit 519a651

Please sign in to comment.