|
| 1 | +import 'reflect-metadata'; |
| 2 | +import React from 'react'; |
| 3 | +import { Controller } from 'mo/react/controller'; |
| 4 | +import { container, singleton } from 'tsyringe'; |
| 5 | +import { |
| 6 | + builtInEditorTreeContextMenu, |
| 7 | + builtInEditorTreeHeaderContextMenu, |
| 8 | + EditorTreeEvent, |
| 9 | +} from 'mo/model/workbench/explorer/editorTree'; |
| 10 | +import { EditorService, ExplorerService, FolderTreeService } from 'mo/services'; |
| 11 | +import { |
| 12 | + builtInExplorerEditorPanel, |
| 13 | + EDITOR_MENU_CLOSE, |
| 14 | + EDITOR_MENU_CLOSE_ALL, |
| 15 | + EDITOR_MENU_CLOSE_OTHERS, |
| 16 | + EDITOR_MENU_CLOSE_SAVED, |
| 17 | +} from 'mo/model'; |
| 18 | +import { |
| 19 | + EditorTree, |
| 20 | + IOpenEditProps, |
| 21 | +} from 'mo/workbench/sidebar/explore/editorTree'; |
| 22 | +import { connect } from 'mo/react'; |
| 23 | +import { IMenuItemProps, ITabProps } from 'mo/components'; |
| 24 | + |
| 25 | +export interface IEditorTreeController { |
| 26 | + readonly onClose: (tabId: string, groupId: number) => void; |
| 27 | + readonly onSelect: (tabId: string, groupId: number) => void; |
| 28 | + readonly onCloseGroup: (groupId: number) => void; |
| 29 | + readonly onSaveGroup: (groupId: number) => void; |
| 30 | + /** |
| 31 | + * Trigger by context menu click event |
| 32 | + * When click the context menu from group header, it doesn't have file info |
| 33 | + */ |
| 34 | + readonly onContextMenu: ( |
| 35 | + menu: IMenuItemProps, |
| 36 | + groupId: number, |
| 37 | + file?: ITabProps |
| 38 | + ) => void; |
| 39 | +} |
| 40 | + |
| 41 | +@singleton() |
| 42 | +export class EditorTreeController |
| 43 | + extends Controller |
| 44 | + implements IEditorTreeController { |
| 45 | + private readonly explorerService: ExplorerService; |
| 46 | + private readonly folderTreeService: FolderTreeService; |
| 47 | + private readonly editService: EditorService; |
| 48 | + |
| 49 | + constructor() { |
| 50 | + super(); |
| 51 | + this.editService = container.resolve(EditorService); |
| 52 | + this.explorerService = container.resolve(ExplorerService); |
| 53 | + this.folderTreeService = container.resolve(FolderTreeService); |
| 54 | + this.initView(); |
| 55 | + } |
| 56 | + |
| 57 | + public initView() { |
| 58 | + const EditorTreeView = connect<IOpenEditProps>( |
| 59 | + this.editService, |
| 60 | + EditorTree |
| 61 | + ); |
| 62 | + const { groupToolbar, ...restEditor } = builtInExplorerEditorPanel(); |
| 63 | + const contextMenu = builtInEditorTreeContextMenu(); |
| 64 | + const headerContextMenu = builtInEditorTreeHeaderContextMenu(); |
| 65 | + |
| 66 | + this.explorerService.addPanel({ |
| 67 | + ...restEditor, |
| 68 | + renderPanel: () => ( |
| 69 | + <EditorTreeView |
| 70 | + contextMenu={contextMenu} |
| 71 | + headerContextMenu={headerContextMenu} |
| 72 | + groupToolbar={groupToolbar} |
| 73 | + onClose={this.onClose} |
| 74 | + onSelect={this.onSelect} |
| 75 | + onCloseGroup={this.onCloseGroup} |
| 76 | + onSaveGroup={this.onSaveGroup} |
| 77 | + onContextMenu={this.onContextMenu} |
| 78 | + getFileIconByExtensionName={ |
| 79 | + this.folderTreeService.getFileIconByExtensionName |
| 80 | + } |
| 81 | + /> |
| 82 | + ), |
| 83 | + }); |
| 84 | + } |
| 85 | + |
| 86 | + public onContextMenu = ( |
| 87 | + menu: IMenuItemProps, |
| 88 | + groupId: number, |
| 89 | + file?: ITabProps |
| 90 | + ) => { |
| 91 | + switch (menu.id) { |
| 92 | + case EDITOR_MENU_CLOSE: |
| 93 | + this.onClose(file?.id!, groupId); |
| 94 | + break; |
| 95 | + |
| 96 | + case EDITOR_MENU_CLOSE_OTHERS: |
| 97 | + this.emit(EditorTreeEvent.onCloseOthers, file, groupId); |
| 98 | + break; |
| 99 | + |
| 100 | + case EDITOR_MENU_CLOSE_SAVED: |
| 101 | + this.emit(EditorTreeEvent.onCloseSaved, groupId); |
| 102 | + break; |
| 103 | + |
| 104 | + case EDITOR_MENU_CLOSE_ALL: |
| 105 | + this.emit(EditorTreeEvent.onCloseAll, groupId); |
| 106 | + break; |
| 107 | + |
| 108 | + default: |
| 109 | + this.emit(EditorTreeEvent.onContextMenu, menu, file, groupId); |
| 110 | + break; |
| 111 | + } |
| 112 | + }; |
| 113 | + |
| 114 | + public onClose = (tabId: string, groupId: number) => { |
| 115 | + this.emit(EditorTreeEvent.onClose, tabId, groupId); |
| 116 | + }; |
| 117 | + |
| 118 | + public onSelect = (tabId: string, groupId: number) => { |
| 119 | + this.emit(EditorTreeEvent.onSelect, tabId, groupId); |
| 120 | + }; |
| 121 | + |
| 122 | + public onCloseGroup = (groupId: number) => { |
| 123 | + this.emit(EditorTreeEvent.onCloseAll, groupId); |
| 124 | + }; |
| 125 | + |
| 126 | + public onSaveGroup = (groupId: number) => { |
| 127 | + this.emit(EditorTreeEvent.onSaveAll, groupId); |
| 128 | + }; |
| 129 | +} |
| 130 | + |
| 131 | +// Register singleton |
| 132 | +container.resolve(EditorTreeController); |
0 commit comments