Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: improve the insert strategy in folderTree #486

Merged
merged 2 commits into from
Oct 28, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/controller/explorer/folderTree.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,9 @@ export class FolderTreeController
const folderTreeState = this.folderTreeService.getState();
const { data, current } = folderTreeState?.folderTree || {};
// The current selected node id or the first root node
const nodeId = current?.id || data?.[0]?.id;
const nodeId =
typeof current?.id === 'undefined' ? data?.[0]?.id : current?.id;

this.emit(FolderTreeEvent.onCreate, type, nodeId);
};

Expand Down
49 changes: 49 additions & 0 deletions src/services/workbench/__tests__/folderTreeService.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { container } from 'tsyringe';
import {
FileTypes,
IFolderTreeNodeProps,
TreeNodeModel,
} from 'mo/model/workbench/explorer/folderTree';
import { expectFnCalled, expectLoggerErrorToBeCalled } from '@test/utils';

Expand All @@ -12,6 +13,7 @@ import {
FolderTreeService,
} from '../explorer/folderTreeService';
import { FolderTreeEvent } from 'mo/model/workbench/explorer/folderTree';
import { randomId } from 'mo/common/utils';

const TEST_ID = 'test-id';

Expand Down Expand Up @@ -135,6 +137,53 @@ describe('Test StatusBarService', () => {
expect(folderTreeService.get(mockTreeData.id)?.name).toBe(TEST_ID);
});

test('Should insert a file or a folder in a sorted way', () => {
const mockRootTree = new TreeNodeModel({
id: randomId(),
name: 'molecule_temp',
fileType: FileTypes.RootFolder,
});
folderTreeService.add(mockRootTree);
const mockFolder = new TreeNodeModel({
id: randomId(),
name: 'mock_folder',
fileType: FileTypes.Folder,
isLeaf: false,
});
folderTreeService.add(mockFolder, mockRootTree.id);
let rootFolder = folderTreeService.get(mockRootTree.id)!;

expect(rootFolder.children).toHaveLength(1);

const anotherMockFolder = new TreeNodeModel({
id: randomId(),
name: 'another_mock_folder',
fileType: FileTypes.Folder,
isLeaf: false,
});
folderTreeService.add(anotherMockFolder, mockRootTree.id);
rootFolder = folderTreeService.get(mockRootTree.id)!;

expect(rootFolder.children).toHaveLength(2);
expect(rootFolder.children).toEqual([anotherMockFolder, mockFolder]);

const mockFile = new TreeNodeModel({
id: randomId(),
name: 'mock_file',
fileType: FileTypes.File,
isLeaf: true,
});
folderTreeService.add(mockFile, mockRootTree.id);
rootFolder = folderTreeService.get(mockRootTree.id)!;

expect(rootFolder.children).toHaveLength(3);
expect(rootFolder.children).toEqual([
anotherMockFolder,
mockFolder,
mockFile,
]);
});

test('Should NOT add root folder when there is a root folder', () => {
const mockRootTree: IFolderTreeNodeProps = {
id: '0',
Expand Down
31 changes: 29 additions & 2 deletions src/services/workbench/explorer/folderTreeService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,23 @@ export class FolderTreeService
};
}

// Get the position of file by type
// We considered by default that the list is sorted in fileType
private getPosOfType(
type: keyof typeof FileTypes,
folderList: IFolderTreeNodeProps[]
) {
if (!folderList.length) return 0;
if (type === FileTypes.Folder || type === FileTypes.RootFolder) {
return 0;
}
// find the first file type
const index = folderList.findIndex(
(list) => list.fileType === FileTypes.File
);
return index === -1 ? folderList.length : index;
}

public add(data: IFolderTreeNodeProps, id?: UniqueId): void {
const isRootFolder = data.fileType === 'RootFolder';

Expand Down Expand Up @@ -284,10 +301,20 @@ export class FolderTreeService
/(?<=\/)[^\/]+$/,
`${data.name}`
) || '';
tree!.prepend(data, currentIndex.parent!);

const parentNode = tree!.getNode(currentIndex.parent!)!;
const pos = this.getPosOfType(
data.fileType!,
parentNode.children || []
);
tree!.insertNode(data, currentIndex.parent!, pos);
} else {
this.setCurrentFolderLocation(data, id);
tree!.append(data, id);
const pos = this.getPosOfType(
data.fileType!,
currentIndex.node.children || []
);
tree?.insertNode(data, currentIndex.id, pos);
}

cloneData[index] = tree!.obj;
Expand Down
3 changes: 2 additions & 1 deletion src/workbench/sidebar/explore/style.scss
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
}

#{$folderTree} {
height: 100%;
min-height: 100%;
padding-bottom: 22px;

&--editable {
background: rgba(0, 0, 0, 0.3);
Expand Down