Skip to content

Commit

Permalink
feat: optimize folderPanel contextMenu logic and extract style to mo
Browse files Browse the repository at this point in the history
optimize folderPanel contextMenu logic and extract style   to mo
  • Loading branch information
zhangtengjin authored and wewoor committed Mar 11, 2021
1 parent dbe3558 commit d1191a5
Show file tree
Hide file tree
Showing 7 changed files with 65 additions and 34 deletions.
1 change: 1 addition & 0 deletions build/webpack.css.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ const styles = [
'workbench/settings/style',
'workbench/sidebar/style',
'workbench/statusBar/style',
'workbench/sidebar/explore/style',
// ============= Molecule.css =============
'style/mo',
];
Expand Down
1 change: 1 addition & 0 deletions src/controller/explorer/explorer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ export class ExplorerController
const folderProps: IFolderTree = {
data: explorerState.folderTree?.data,
contextMenu: explorerState.folderTree?.contextMenu,
folderPanelContextMenu: explorerState.folderTree?.folderPanelContextMenu
};
return (
<FolderTreeView
Expand Down
35 changes: 26 additions & 9 deletions src/controller/explorer/folderTree.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { editorService, explorerService } from 'mo';
import { EditorController } from 'mo/controller/editor';
import { IMenuItem } from 'mo/components/menu';
import Modal from 'mo/components/dialog';
import { IFolderInputEvent, TreeNodeModel } from 'mo/model';
const confirm = Modal.confirm;

export interface IFolderTreeController {
Expand All @@ -15,13 +16,14 @@ export interface IFolderTreeController {
readonly onClickContextMenu?: (
e: React.MouseEvent,
item: IMenuItem,
node: ITreeNodeItem,
events?: Object
node?: ITreeNodeItem,
events?: IFolderInputEvent
) => void;
readonly filterContextMenu?: (
menus: IMenuItem[],
treeNode: ITreeNodeItem
) => IMenuItem[];
readonly getInputEvent?: (events: IFolderInputEvent) => IFolderInputEvent;
}

@singleton()
Expand All @@ -33,7 +35,7 @@ export class FolderTreeController
this.initView();
}

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

public readonly onSelectFile = (file: ITreeNodeItem, isAuto?: boolean) => {
const tabData = {
Expand Down Expand Up @@ -72,19 +74,24 @@ export class FolderTreeController
explorerService.onDropTree(treeNode);
};

public readonly getInputEvent = (events: IFolderInputEvent): IFolderInputEvent => {
return events;
}

public readonly onClickContextMenu = (
e: React.MouseEvent,
item: IMenuItem,
node: ITreeNodeItem,
events?: Object
node = {},
events?: IFolderInputEvent
) => {
const menuId = item.id;
const { id: nodeId, name } = node as any;
console.log('onClickContextMenu => Item', item)
switch (menuId) {
case 'rename': {
explorerService.rename(nodeId, () => {
events?.['setValue'](name);
events?.['onFocus']();
events?.setValue?.(name);
events?.onFocus();
});
break;
}
Expand All @@ -105,20 +112,29 @@ export class FolderTreeController
}
case 'newFile': {
explorerService.newFile(nodeId, () => {
events?.['onFocus']();
events?.onFocus();
});
break;
}
case 'newFolder': {
explorerService.newFolder(nodeId, () => {
events?.['onFocus']();
events?.onFocus();
});
break;
}
case 'remove': {
explorerService.removeRootFolder(nodeId);
break;
}
case 'addRootFolder': {
explorerService.addRootFolder?.(
new TreeNodeModel({
name: `molecule_temp${Math.random()}`,
fileType: 'rootFolder',
})
);
break;
}
case 'openTab': {
console.log('OpenTab');
break;
Expand Down Expand Up @@ -178,4 +194,5 @@ export class FolderTreeController
}
return menu;
};

}
23 changes: 23 additions & 0 deletions src/model/workbench/explorer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,15 @@ export interface IPanelItem<T = any> extends IActivityBarItem {
toolbar?: T;
}

export interface IFolderInputEvent {
onFocus: () => void;
setValue: (value: string) => void;
}

export interface IFolderTree {
data?: ITreeNodeItem[];
contextMenu?: IMenuItem[];
folderPanelContextMenu?: IMenuItem[];
current?: ITreeNodeItem | null;
}
export interface IExplorer {
Expand Down Expand Up @@ -60,6 +66,22 @@ const commonContextMenu = [
name: 'Delete',
},
];

// Sample folder panel area ContextMenu
const folderPanelContextMenu = [
{
id: 'addRootFolder',
name: 'Add Folder to Workspace...'
},
{
id: 'find',
name: 'Find in Workspace...'
},
{
id: 'download',
name: 'Download...'
},
]
// Dedault Panel
export const EDITOR_PANEL = {
id: 'OpenEditors',
Expand Down Expand Up @@ -141,6 +163,7 @@ export class IExplorerModel implements IExplorer {
public folderTree: IFolderTree = {
contextMenu: commonContextMenu,
current: null,
folderPanelContextMenu: folderPanelContextMenu,
data: [],
};
public headerToolBar: IActivityBarItem[] = builtInHeaderToolbar;
Expand Down
1 change: 1 addition & 0 deletions src/style/mo.scss
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
@import 'mo/workbench/settings/style';
@import 'mo/workbench/sidebar/style';
@import 'mo/workbench/statusBar/style';
@import 'mo/workbench/sidebar/explore/style';

.#{$prefix} {
bottom: 0;
Expand Down
37 changes: 13 additions & 24 deletions src/workbench/sidebar/explore/folderTree.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,51 +10,40 @@ import { IFolderTreeController } from 'mo/controller/explorer/folderTree';
import { useContextView } from 'mo/components/contextView';
import { useContextMenu } from 'mo/components/contextMenu';
import { explorerService } from 'mo/services';
import { TreeNodeModel } from 'mo/model';
import { TreeNodeModel, IFolderInputEvent } from 'mo/model';

const FolderTree: React.FunctionComponent<IFolderTree> = (
props: IFolderTree & IFolderTreeController
) => {
const {
data = [],
contextMenu = [],
folderPanelContextMenu = [],
onSelectFile,
onDropTree,
filterContextMenu,
onClickContextMenu,
getInputEvent,
...restProps
} = props;
const inputRef = useRef<any>(null);

const contextView = useContextView();

let contextViewMenu;
const folderContextMenu = [
{
id: 'addFolder',
name: 'Add Folder to Workspace',
onClick: () => {
explorerService.addRootFolder?.(
new TreeNodeModel({
name: `molecule_temp${Math.random()}`,
fileType: 'rootFolder',
})
);
},
},
];
const onClickMenuItem = useCallback(
(e, item) => {
onClickContextMenu?.(e, item)
contextViewMenu?.dispose();
},
[folderContextMenu]
[folderPanelContextMenu]
);
const renderContextMenu = () => (
<Menu onClick={onClickMenuItem} data={folderContextMenu} />
<Menu onClick={onClickMenuItem} data={folderPanelContextMenu} />
);

useEffect(() => {
if (folderContextMenu.length > 0) {
if (folderPanelContextMenu.length > 0) {
contextViewMenu = useContextMenu({
anchor: select('.samplefolder'),
render: renderContextMenu,
Expand All @@ -73,23 +62,23 @@ const FolderTree: React.FunctionComponent<IFolderTree> = (
});
};

const setInputVal = (val) => {
const setInputValue = (val) => {
setTimeout(() => {
if (inputRef.current) {
inputRef.current.value = val;
}
});
};

const inputEvents = {
const inputEvents: IFolderInputEvent = {
onFocus,
setValue: (val) => setInputVal(val),
setValue: (val) => setInputValue(val),
};

const handleRightClick = ({ event, node }) => {
const menuItems = filterContextMenu?.(contextMenu, node.data);
const handleOnMenuClick = (e: React.MouseEvent, item) => {
onClickContextMenu?.(e, item, node.data, inputEvents);
onClickContextMenu?.(e, item, node.data, getInputEvent?.(inputEvents));
contextView.hide();
};
contextView?.show(getEventPosition(event), () => (
Expand Down Expand Up @@ -141,8 +130,8 @@ const FolderTree: React.FunctionComponent<IFolderTree> = (
onBlur={handleInputBlur}
/>
) : (
name
);
name
);
};

const renderByData = (
Expand Down
1 change: 0 additions & 1 deletion src/workbench/sidebar/explore/index.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import 'mo/workbench/sidebar/explore/style.scss';
import { connect } from 'mo/react';
import { explorerService } from 'mo/services';
import { explorerController, folderTreeController } from 'mo/controller';
Expand Down

0 comments on commit d1191a5

Please sign in to comment.