1
1
import * as React from 'react' ;
2
- import { memo , useRef } from 'react' ;
2
+ import { memo , useRef , useEffect , useCallback } from 'react' ;
3
3
import { IFolderTree } from 'mo' ;
4
+ import { select } from 'mo/common/dom' ;
4
5
import Tree , { FileTypes } from 'mo/components/tree' ;
5
6
import { Menu } from 'mo/components/menu' ;
6
7
import { getEventPosition } from 'mo/common/dom' ;
7
8
import { Button } from 'mo/components/button' ;
8
9
import { IFolderTreeController } from 'mo/controller/explorer/folderTree' ;
9
10
import { useContextView } from 'mo/components/contextView' ;
11
+ import { useContextMenu } from 'mo/components/contextMenu' ;
10
12
import { explorerService } from 'mo/services' ;
11
13
import { TreeNodeModel } from 'mo/model' ;
12
14
@@ -26,6 +28,41 @@ const FolderTree: React.FunctionComponent<IFolderTree> = (
26
28
27
29
const contextView = useContextView ( ) ;
28
30
31
+ let contextViewMenu ;
32
+ const folderContextMenu = [ {
33
+ id : 'addFolder' ,
34
+ name : 'Add Folder to Workspace' ,
35
+ onClick : ( ) => {
36
+ explorerService . addRootFolder ?.(
37
+ new TreeNodeModel ( {
38
+ name : `molecule_temp${ Math . random ( ) } ` ,
39
+ fileType : 'rootFolder' ,
40
+ } )
41
+ ) ;
42
+ }
43
+ } ]
44
+ const onClickMenuItem = useCallback (
45
+ ( e , item ) => {
46
+ contextViewMenu ?. dispose ( ) ;
47
+ } ,
48
+ [ folderContextMenu ]
49
+ ) ;
50
+ const renderContextMenu = ( ) => (
51
+ < Menu onClick = { onClickMenuItem } data = { folderContextMenu } />
52
+ ) ;
53
+
54
+ useEffect ( ( ) => {
55
+ if ( folderContextMenu . length > 0 ) {
56
+ contextViewMenu = useContextMenu ( {
57
+ anchor : select ( '.samplefolder' ) ,
58
+ render : renderContextMenu ,
59
+ } ) ;
60
+ }
61
+ return function cleanup ( ) {
62
+ contextViewMenu ?. dispose ( ) ;
63
+ } ;
64
+ } ) ;
65
+
29
66
const onFocus = ( ) => {
30
67
setTimeout ( ( ) => {
31
68
if ( inputRef . current ) {
@@ -37,15 +74,15 @@ const FolderTree: React.FunctionComponent<IFolderTree> = (
37
74
const setInputVal = ( val ) => {
38
75
setTimeout ( ( ) => {
39
76
if ( inputRef . current ) {
40
- inputRef . current . value = val
77
+ inputRef . current . value = val ;
41
78
}
42
79
} ) ;
43
- }
80
+ } ;
44
81
45
82
const inputEvents = {
46
83
onFocus,
47
- setValue : ( val ) => setInputVal ( val )
48
- }
84
+ setValue : ( val ) => setInputVal ( val ) ,
85
+ } ;
49
86
50
87
const handleRightClick = ( { event, node } ) => {
51
88
const menuItems = filterContextMenu ?.( contextMenu , node . data ) ;
@@ -67,10 +104,13 @@ const FolderTree: React.FunctionComponent<IFolderTree> = (
67
104
} ,
68
105
( ) => {
69
106
if ( node ?. fileType === FileTypes . file && newName ) {
70
- onSelectFile ?.( {
71
- ...node ,
72
- name : newName ,
73
- } , true ) ;
107
+ onSelectFile ?.(
108
+ {
109
+ ...node ,
110
+ name : newName ,
111
+ } ,
112
+ true
113
+ ) ;
74
114
}
75
115
}
76
116
) ;
@@ -104,39 +144,14 @@ const FolderTree: React.FunctionComponent<IFolderTree> = (
104
144
} ;
105
145
106
146
const renderByData = (
107
- < >
108
- < Tree
109
- data = { data }
110
- draggable
111
- onSelectFile = { onSelectFile }
112
- onRightClick = { handleRightClick }
113
- renderTitle = { renderTitle }
114
- { ...restProps }
115
- />
116
- { /* test service */ }
117
- < div style = { { marginTop : '100px' } } >
118
- < Button
119
- onClick = { ( ) => {
120
- explorerService . addRootFolder ?.(
121
- new TreeNodeModel ( {
122
- name : `tree_${ Math . random ( ) * 10 + 1 } ` ,
123
- fileType : 'rootFolder' ,
124
- } )
125
- ) ;
126
- } }
127
- >
128
- Add Folder
129
- </ Button >
130
- < Button
131
- onClick = { ( ) => {
132
- console . log ( 'test' ) ;
133
- explorerService . newFile ?.( ) ;
134
- } }
135
- >
136
- New File
137
- </ Button >
138
- </ div >
139
- </ >
147
+ < Tree
148
+ data = { data }
149
+ draggable
150
+ onSelectFile = { onSelectFile }
151
+ onRightClick = { handleRightClick }
152
+ renderTitle = { renderTitle }
153
+ { ...restProps }
154
+ />
140
155
) ;
141
156
142
157
const renderInitial = (
0 commit comments