1
1
import * as React from 'react' ;
2
- import { memo , useCallback } from 'react' ;
2
+ import { memo } from 'react' ;
3
3
import { Icon } from 'mo/components/icon' ;
4
4
import { Menu } from 'mo/components/menu' ;
5
5
import { DropDown , DropDownRef } from 'mo/components/dropdown' ;
6
- import { IEditorAction } from 'mo/model' ;
7
- import { groupActionsClassName , groupActionsItemClassName } from './base' ;
6
+ import { IEditorActionsProps , IEditorAction } from 'mo/model' ;
7
+ import {
8
+ groupActionItemDisabledClassName ,
9
+ groupActionsClassName ,
10
+ groupActionsItemClassName ,
11
+ } from './base' ;
8
12
import { IEditorController } from 'mo/controller/editor' ;
13
+ import { classNames } from 'mo/common/className' ;
9
14
10
15
export interface IEditorActionProps extends IEditorAction {
11
16
isActiveGroup : boolean ;
12
17
}
13
18
19
+ const MAX_ACTIONS_LENGTH = 6 ;
20
+
21
+ function splitActions ( actions : IEditorActionsProps [ ] ) {
22
+ const outerActions : IEditorActionsProps [ ] = [ ] ;
23
+ const innerActions : IEditorActionsProps [ ] = [ ] ;
24
+
25
+ actions . forEach ( ( action ) => {
26
+ if ( action . place === 'outer' ) {
27
+ outerActions . push ( action ) ;
28
+ } else {
29
+ innerActions . push ( action ) ;
30
+ }
31
+ } ) ;
32
+
33
+ if ( outerActions . length > MAX_ACTIONS_LENGTH ) {
34
+ const surplusActions = outerActions . splice (
35
+ 0 ,
36
+ MAX_ACTIONS_LENGTH - outerActions . length
37
+ ) ;
38
+
39
+ innerActions . concat ( surplusActions ) ;
40
+ }
41
+
42
+ return [ outerActions , innerActions ] ;
43
+ }
44
+
14
45
function EditorAction ( props : IEditorActionProps & IEditorController ) {
15
- const {
16
- actions = [ ] ,
17
- isActiveGroup = false ,
18
- onClickContextMenu,
19
- onSplitEditorRight,
20
- } = props ;
46
+ const { actions = [ ] , isActiveGroup = false , onClickActions } = props ;
47
+ const [ outer , inner ] = splitActions ( actions ) ;
21
48
22
49
const childRef = React . useRef < DropDownRef > ( null ) ;
23
50
24
- const handleOnMenuClick = ( e : React . MouseEvent , item ) => {
25
- onClickContextMenu ?.( e , item ) ;
26
- ( childRef . current as any ) ! . dispose ( ) ;
51
+ const handleOnMenuClick = ( _ , item ) => {
52
+ onClickActions ( item ) ;
53
+ childRef . current ?. dispose ( ) ;
54
+ } ;
55
+
56
+ const handleActionsClick = ( action ) => {
57
+ onClickActions ( action ) ;
27
58
} ;
28
59
29
60
const overlay =
30
- actions . length > 0 ? (
61
+ inner . length > 0 ? (
31
62
< Menu
32
63
style = { { width : 200 } }
33
- data = { actions }
64
+ data = { inner }
34
65
onClick = { handleOnMenuClick }
35
66
/>
36
67
) : (
@@ -44,33 +75,38 @@ function EditorAction(props: IEditorActionProps & IEditorController) {
44
75
</ span >
45
76
) ;
46
77
47
- const handleSplitEditor = useCallback (
48
- ( e : React . MouseEvent ) => {
49
- onSplitEditorRight ?.( ) ;
50
- } ,
51
- [ actions ]
52
- ) ;
53
78
return (
54
79
< div className = { groupActionsClassName } >
55
- { isActiveGroup ? (
56
- < div
57
- onClick = { handleSplitEditor }
80
+ { isActiveGroup &&
81
+ outer . map ( ( action ) => (
82
+ < div
83
+ key = { action . id }
84
+ onClick = { ( ) => handleActionsClick ( action ) }
85
+ className = { classNames (
86
+ groupActionsItemClassName ,
87
+ action . disabled && groupActionItemDisabledClassName
88
+ ) }
89
+ title = { action . name ?. toString ( ) }
90
+ >
91
+ { action . icon ? (
92
+ < Icon type = { action . icon } />
93
+ ) : (
94
+ action . name
95
+ ) }
96
+ </ div >
97
+ ) ) }
98
+ { Boolean ( inner . length ) && (
99
+ < DropDown
100
+ ref = { childRef }
101
+ placement = "bottom"
58
102
className = { groupActionsItemClassName }
59
- title = "Split Editor Right"
103
+ trigger = "click"
104
+ title = "More Actions..."
105
+ overlay = { overlay }
60
106
>
61
- < Icon type = "split-horizontal" />
62
- </ div >
63
- ) : null }
64
- < DropDown
65
- ref = { childRef }
66
- placement = "bottom"
67
- className = { groupActionsItemClassName }
68
- trigger = "click"
69
- title = "More Actions..."
70
- overlay = { overlay }
71
- >
72
- < Icon type = "ellipsis" />
73
- </ DropDown >
107
+ < Icon type = "ellipsis" />
108
+ </ DropDown >
109
+ ) }
74
110
</ div >
75
111
) ;
76
112
}
0 commit comments