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: folderTree support to set entry page #209

Merged
merged 2 commits into from
Jun 29, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
11 changes: 1 addition & 10 deletions src/controller/explorer/explorer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,9 @@ import {
IExplorerService,
ISidebarService,
IActivityBarService,
IFolderTreeService,
ActivityBarService,
SidebarService,
ExplorerService,
FolderTreeService,
} from 'mo/services';
import { FolderTreeController, IFolderTreeController } from './folderTree';

Expand All @@ -54,15 +52,13 @@ export class ExplorerController
private readonly activityBarService: IActivityBarService;
private readonly sidebarService: ISidebarService;
private readonly explorerService: IExplorerService;
private readonly folderTreeService: IFolderTreeService;
private readonly folderTreeController: IFolderTreeController;

constructor() {
super();
this.activityBarService = container.resolve(ActivityBarService);
this.sidebarService = container.resolve(SidebarService);
this.explorerService = container.resolve(ExplorerService);
this.folderTreeService = container.resolve(FolderTreeService);
this.folderTreeController = container.resolve(FolderTreeController);

this.initView();
Expand Down Expand Up @@ -153,12 +149,7 @@ export class ExplorerController
};

public renderFolderTree = () => {
return (
<FolderTreeView
{...this.folderTreeService.getState()?.folderTree}
{...this.folderTreeController}
/>
);
return <FolderTreeView />;
};
}

Expand Down
1 change: 0 additions & 1 deletion src/model/workbench/explorer/explorer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,6 @@ export function builtInExplorerFolderPanel() {
id: SAMPLE_FOLDER_PANEL_ID,
sortIndex: 8,
name: localize('menu.defaultProjectName', 'No Open Folder'),
className: 'samplefolder',
toolbar: [
{
id: NEW_FILE_COMMAND_ID,
Expand Down
8 changes: 7 additions & 1 deletion src/model/workbench/explorer/folderTree.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ export interface IFolderTreeSubItem {
}
export interface IFolderTree {
folderTree?: IFolderTreeSubItem;
entry?: React.ReactNode;
}

export const NEW_FILE_COMMAND_ID = 'explorer.newFile';
Expand Down Expand Up @@ -138,8 +139,13 @@ const builtInFolderTree = {

export class IFolderTreeModel implements IFolderTree {
public folderTree: IFolderTreeSubItem;
public entry: React.ReactNode;

constructor(folderTree: IFolderTreeSubItem = builtInFolderTree) {
constructor(
folderTree: IFolderTreeSubItem = builtInFolderTree,
entry?: React.ReactNode
) {
this.folderTree = folderTree;
this.entry = entry;
}
}
7 changes: 7 additions & 0 deletions src/services/workbench/explorer/folderTreeService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ export interface IFolderTreeService extends Component<IFolderTree> {
addRootFolder(folder?: ITreeNodeItemProps): void;
removeRootFolder(id: number): void;
setActive(id?: number): void;
setEntry(entry: React.ReactNode): void;
onDropTree(treeData: ITreeNodeItemProps[]): void;
getFileIconByExtensionName(name: string, fileType: FileType): string;
/**
Expand Down Expand Up @@ -213,6 +214,12 @@ export class FolderTreeService
});
}

public setEntry(entry: React.ReactNode) {
this.setState({
entry,
});
}

public onRename(callback: (id: number) => void) {
this.subscribe(FolderTreeEvent.onRename, callback);
}
Expand Down
56 changes: 33 additions & 23 deletions src/workbench/sidebar/explore/folderTree.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ import React, {
useCallback,
useLayoutEffect,
} from 'react';
import { IFolderTreeSubItem } from 'mo/model';
import { IFolderTree, IFolderTreeSubItem } from 'mo/model';
import { select, getEventPosition } from 'mo/common/dom';
import Tree, { ITreeNodeItemProps } from 'mo/components/tree';
import { IMenuItemProps, Menu } from 'mo/components/menu';
import { Button } from 'mo/components/button';
import { IFolderTreeController } from 'mo/controller/explorer/folderTree';
import { FolderTreeController } from 'mo/controller/explorer/folderTree';
import { useContextView } from 'mo/components/contextView';
import { useContextMenu } from 'mo/components/contextMenu';
import {
Expand Down Expand Up @@ -66,13 +66,12 @@ const Input = React.forwardRef(
}
);

const FolderTree: React.FunctionComponent<IFolderTreeSubItem> = (
props: IFolderTreeSubItem & IFolderTreeController
) => {
const FolderTree: React.FunctionComponent<
FolderTreeController & IFolderTree
> = (props) => {
const {
data = [],
contextMenu: rawContextMenu = [],
folderPanelContextMenu = [],
folderTree = {},
entry,
onUpdateFileName,
onSelectFile,
onDropTree,
Expand All @@ -82,6 +81,30 @@ const FolderTree: React.FunctionComponent<IFolderTreeSubItem> = (
getInputEvent,
...restProps
} = props;

const {
data = [],
contextMenu: rawContextMenu = [],
folderPanelContextMenu = [],
} = folderTree;

const handleAddRootFolder = () => {
onNewRootFolder?.();
};

const welcomePage = entry ? (
<>{entry}</>
) : (
<div style={{ padding: '10px 5px' }}>
you have not yet opened a folder
<Button onClick={handleAddRootFolder}>Add Folder</Button>
</div>
);

if (!data.length) return welcomePage;

console.log('folderTree:', folderTree);
mortalYoung marked this conversation as resolved.
Show resolved Hide resolved

const inputRef = useRef<HTMLInputElement>(null);
// tree context view
const contextMenu = useRef<ReturnType<typeof useContextMenu>>();
Expand Down Expand Up @@ -164,10 +187,6 @@ const FolderTree: React.FunctionComponent<IFolderTreeSubItem> = (
}
};

const handleAddRootFolder = () => {
onNewRootFolder?.();
};

const renderTitle = (node: ITreeNodeItemProps) => {
const { isEditable, name } = node;

Expand All @@ -194,9 +213,9 @@ const FolderTree: React.FunctionComponent<IFolderTreeSubItem> = (
return () => {
contextMenu.current?.dispose();
};
}, [data?.length]);
}, [data.length]);

const renderByData = (
return (
<Tree
// root folder do not render
data={data[0]?.children || []}
Expand All @@ -211,14 +230,5 @@ const FolderTree: React.FunctionComponent<IFolderTreeSubItem> = (
{...restProps}
/>
);

const renderInitial = (
<div style={{ padding: '10px 5px' }}>
you have not yet opened a folder
<Button onClick={handleAddRootFolder}>Add Folder</Button>
</div>
);

return data?.length > 0 ? renderByData : renderInitial;
};
export default memo(FolderTree);