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

feat: support listen to onCollapseAllFolders #673

Merged
merged 3 commits into from
Mar 1, 2022
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
15 changes: 8 additions & 7 deletions src/components/tree/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ const TreeView = ({
onLoadData,
onTreeClick,
}: ITreeProps) => {
const [expandKeys, setExpandKeys] = useState<string[]>([]);
const [expandKeys, setExpandKeys] = useState<UniqueId[]>([]);
const [activeKey, setActiveKey] = useState<string | null>(null);
const loadDataCache = useRef<Record<string, boolean>>({});
const [loadingKeys, setLoadingKeys] = useState<string[]>([]);
Expand Down Expand Up @@ -125,15 +125,16 @@ const TreeView = ({
};

const handleExpandKey = (key: string, node: ITreeNodeItemProps) => {
const index = expandKeys.findIndex((e) => e === key);
const nextExpandKeys = (controlExpandKeys || expandKeys).concat();
const index = nextExpandKeys.findIndex((e) => e === key);
if (index > -1) {
expandKeys.splice(index, 1);
nextExpandKeys.splice(index, 1);
} else {
expandKeys.push(key);
nextExpandKeys.push(key);
}
onExpand
? onExpand(expandKeys.concat(), node)
: setExpandKeys(expandKeys.concat());
? onExpand(nextExpandKeys.concat(), node)
: setExpandKeys(nextExpandKeys.concat());
};

const handleNodeClick = (
Expand Down Expand Up @@ -415,7 +416,7 @@ const TreeView = ({
return node.id.toString();
});
const nextExpandKeys = Array.from(
new Set([...keys, ...expandKeys])
new Set([...keys, ...(controlExpandKeys || expandKeys)])
);

onExpand
Expand Down
13 changes: 13 additions & 0 deletions src/controller/__tests__/explorer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,19 @@ describe('The explorer controller', () => {

expect(mockFn).toBeCalled();

// COLLAPSE_COMMAND_ID
mockItem.id = constants.COLLAPSE_COMMAND_ID;
mockFn.mockClear();
expect(mockFn).not.toBeCalled();
explorerController.subscribe(
ExplorerEvent.onCollapseAllFolders,
mockFn
);

explorerController.onToolbarClick(mockItem, mockParentPanel);

expect(mockFn).toBeCalled();

// default
mockItem.id = 'custom-id';
mockFn.mockClear();
Expand Down
5 changes: 5 additions & 0 deletions src/controller/explorer/explorer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ export class ExplorerController
NEW_FILE_COMMAND_ID,
NEW_FOLDER_COMMAND_ID,
REMOVE_COMMAND_ID,
COLLAPSE_COMMAND_ID,
EXPLORER_TOGGLE_CLOSE_ALL_EDITORS,
EXPLORER_TOGGLE_SAVE_ALL,
EXPLORER_TOGGLE_VERTICAL,
Expand All @@ -149,6 +150,10 @@ export class ExplorerController
this.emit(ExplorerEvent.onRemovePanel, parentPanel);
break;
}
case COLLAPSE_COMMAND_ID: {
this.emit(ExplorerEvent.onCollapseAllFolders);
break;
}
case EXPLORER_TOGGLE_CLOSE_ALL_EDITORS: {
this.emit(EditorTreeEvent.onCloseAll);
break;
Expand Down
1 change: 1 addition & 0 deletions src/model/workbench/explorer/explorer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export enum ExplorerEvent {
onPanelToolbarClick = 'explorer.onPanelToolbarClick',
onCollapseChange = 'explorer.onCollapseChange',
onRemovePanel = 'explorer.onRemovePanel',
onCollapseAllFolders = 'explorer.onCollapseAllFolders',
}

export type RenderFunctionProps = (props) => React.ReactNode;
Expand Down
10 changes: 9 additions & 1 deletion src/services/__tests__/explorerService.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { ExplorerEvent, IExplorerPanelItem } from 'mo/model';
import 'reflect-metadata';
import { expectLoggerErrorToBeCalled } from '@test/utils';
import { expectFnCalled, expectLoggerErrorToBeCalled } from '@test/utils';
import { container } from 'tsyringe';
import { searchById } from 'mo/common/utils';
import { ExplorerService } from '../workbench';
Expand Down Expand Up @@ -290,4 +290,12 @@ describe('Test the Explorer Service', () => {
expect(mockFn.mock.calls[0][0]).toEqual(panelData);
expect(mockFn.mock.calls[0][1]).toEqual('toolbar-id');
});

test('Should support to subscribe onCollapseAllFolders event', () => {
expectFnCalled((fn) => {
explorerService.onCollapseAllFolders(fn);

explorerService.emit(ExplorerEvent.onCollapseAllFolders);
});
});
});
3 changes: 2 additions & 1 deletion src/services/builtinService/const.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ export const constants = {
EXPLORER_TOGGLE_CLOSE_GROUP_EDITORS: 'sidebar.explore.closeGroupEditors',
NEW_FILE_COMMAND_ID: 'explorer.newFile',
NEW_FOLDER_COMMAND_ID: 'explorer.newFolder',
COLLAPSE_COMMAND_ID: 'explorer.collapse',
RENAME_COMMAND_ID: 'explorer.rename',
REMOVE_COMMAND_ID: 'explorer.remove',
DELETE_COMMAND_ID: 'explorer.delete',
Expand Down Expand Up @@ -127,7 +128,7 @@ export const modules = {
icon: 'refresh',
},
{
id: 'collapse',
id: constants.COLLAPSE_COMMAND_ID,
title: localize(
'sidebar.explore.collapseFolders',
'Collapse Folders in Explorer'
Expand Down
9 changes: 9 additions & 0 deletions src/services/workbench/explorer/explorerService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,11 @@ export interface IExplorerService extends Component<IExplorer> {
* @param callback
*/
onRemovePanel(callback: (panel: IExplorerPanelItem) => void): void;
/**
* Listen to the FolderTree Panel collapse all folders event
* @param callback
*/
onCollapseAllFolders(callback: () => void): void;
/**
* Listen to the Explorer panel toolbar click event
* @param callback
Expand Down Expand Up @@ -313,6 +318,10 @@ export class ExplorerService
this.subscribe(ExplorerEvent.onRemovePanel, callback);
}

public onCollapseAllFolders(callback: () => void) {
this.subscribe(ExplorerEvent.onCollapseAllFolders, callback);
}

public onPanelToolbarClick(
callback: (panel: IExplorerPanelItem, toolbarId: string) => void
) {
Expand Down
4 changes: 4 additions & 0 deletions stories/extensions/test/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -175,5 +175,9 @@ export const ExtendsTestPane: IExtension = {
molecule.folderTree.onRemove((id) => {
molecule.folderTree.remove(id);
});

molecule.explorer.onCollapseAllFolders(() => {
molecule.folderTree.setExpandKeys([]);
});
},
};