1
- import { Menu , Item , Separator , Submenu , useContextMenu , PredicateParams , ItemProps , ItemParams } from "react-contexify" ;
1
+ import { Menu , Item , Separator , Submenu , useContextMenu , PredicateParams , ItemParams } from "react-contexify" ;
2
2
import * as React from 'react' ;
3
- import { ContextMenu , SubMenuItem , TitledContextMenuItem , ClickEventType , BaseContextMenuItem , ContextMenuItem , SeparatorMenuItem } from "./context-menu" ;
3
+ import { ContextMenu , SubMenuItem , TitledContextMenuItem , ClickEventType , BaseContextMenuItem , ContextMenuItem , SeparatorMenuItem , SetNewContextMenuGenerator } from "./context-menu" ;
4
4
5
5
type ItemData = any ;
6
6
7
- export class HTMLContextMenu implements ContextMenu {
7
+ export class HTMLContextMenu < PropType > extends ContextMenu < PropType > {
8
8
constructor ( component_id ?: string ) {
9
+ super ( ) ;
9
10
this . menu = null ;
10
- this . items = null ;
11
+ this . items = [ ] ;
11
12
if ( ! component_id )
12
13
component_id = "ContextMenu" + HTMLContextMenu . auto_id ++ ;
13
14
this . component_id = component_id ;
@@ -16,62 +17,71 @@ export class HTMLContextMenu implements ContextMenu {
16
17
component_id : string ;
17
18
menu : JSX . Element | null ;
18
19
19
- ContextMenuItemClicked ( { id, event, props, data, triggerEvent } : ItemParams < ItemProps , ItemData > ) {
20
+ ContextMenuItemClicked = ( { id, event, props, data, triggerEvent } : ItemParams < PropType , ItemData > ) => {
20
21
21
22
let item = this . GetContextMenuItemById ( this . items , id ) ;
22
23
if ( item && item instanceof ContextMenuItem )
23
24
item . onClick ( event , props ) ;
25
+ else
26
+ console . log ( "Unable to find sub item on context menu something likely wrong" ) ;
24
27
}
25
28
26
29
27
- protected GetContextMenuItemById ( items : BaseContextMenuItem [ ] | null | undefined , id : string | undefined ) : BaseContextMenuItem | null {
30
+ protected GetContextMenuItemById ( items : BaseContextMenuItem < PropType > [ ] | null | undefined , id : string | undefined ) : BaseContextMenuItem < PropType > | null {
28
31
if ( ! items )
29
32
return null ;
30
33
let item = items ?. find ( i => i . component_id === id ) ;
31
- if ( item == null ) {
32
- items ?. forEach ( sub_item => {
34
+ if ( item == null && items ) {
35
+ for ( let sub_item of items ) {
33
36
if ( sub_item instanceof SubMenuItem )
34
37
return this . GetContextMenuItemById ( sub_item . sub_items , id ) ;
35
- } ) ;
38
+ }
36
39
}
37
40
if ( ! item )
38
41
return null ;
39
42
return item ;
40
43
}
41
44
42
- items : BaseContextMenuItem [ ] | null ;
43
- CreateContextMenu ( items : BaseContextMenuItem [ ] ) {
45
+ items : BaseContextMenuItem < PropType > [ ] ;
46
+ SetContextMenuItems ( items : BaseContextMenuItem < PropType > [ ] ) {
44
47
this . items = items ;
45
- this . menu = this . CreateMenuComponent ( items ) ;
48
+ this . menu = null ;
49
+ }
50
+ AppendContextMenuItem ( item : BaseContextMenuItem < PropType > ) {
51
+ this . items . push ( item ) ;
52
+ }
53
+ GetMenuComponent ( ) {
54
+ if ( this . menu == null )
55
+ this . menu = this . CreateMenuComponent ( this . items ) ;
46
56
return this . menu ;
47
57
}
48
- GetContextMenuHandler ( props : any ) {
58
+ GetContextMenuHandler ( props : PropType ) {
49
59
return ( e : ClickEventType ) => this . displayMenu ( e , props ) ;
50
60
}
51
61
// AttachMenuToItem(domElem: any, props: any) {
52
62
// domElem.setState( {onContextMenu: );
53
63
// //would need to ues clonelem
54
64
// return domElem;
55
65
// }
56
- displayMenu ( e : ClickEventType , props : object ) {
57
- useContextMenu ( {
58
- id : this . component_id
59
- } ) . show ( { event : e , props : props } ) ;
66
+ displayMenu ( e : ClickEventType , props : PropType ) {
67
+ let selection = window . getSelection ( ) ;
68
+ if ( selection == null || selection . toString ( ) . length < 2 ) //if text is selected use native menu, note if text is selected but its not what we right click on this will still work as that text is deselected prior to this call
69
+ useContextMenu ( { id : this . component_id } ) . show ( { event : e , props : props } ) ;
60
70
}
61
71
62
- IsDisabled = ( { id, triggerEvent, props, data } : PredicateParams < ItemProps , ItemData > ) => {
72
+ IsDisabled = ( { id, triggerEvent, props, data } : PredicateParams < PropType , ItemData > ) => {
63
73
let item = this . GetContextMenuItemById ( this . items , id ) ;
64
74
if ( item && item instanceof TitledContextMenuItem )
65
- return item . disabled ( ) ;
75
+ return item . disabled ( props ) ;
66
76
return false ;
67
77
}
68
- IsHidden = ( { id, triggerEvent, props, data } : PredicateParams < ItemProps , ItemData > ) => {
78
+ IsHidden = ( { id, triggerEvent, props, data } : PredicateParams < PropType , ItemData > ) => {
69
79
let item = this . GetContextMenuItemById ( this . items , id ) ;
70
80
if ( item && item instanceof TitledContextMenuItem )
71
- return item . hidden ( ) ;
81
+ return item . hidden ( props ) ;
72
82
return false ;
73
83
}
74
- GetItem ( item : BaseContextMenuItem ) {
84
+ GetItem ( item : BaseContextMenuItem < PropType > ) {
75
85
if ( item instanceof SubMenuItem ) {
76
86
return < Submenu id = { item . component_id } label = { item . title } >
77
87
{ item . sub_items ?. map ( ( item ) => this . GetItem ( item ) ) }
@@ -82,7 +92,7 @@ export class HTMLContextMenu implements ContextMenu {
82
92
else if ( item instanceof SeparatorMenuItem )
83
93
return < Separator key = { item . component_id } />
84
94
}
85
- CreateMenuComponent ( items : BaseContextMenuItem [ ] ) {
95
+ CreateMenuComponent ( items : BaseContextMenuItem < PropType > [ ] ) {
86
96
87
97
return ( < >
88
98
< Menu id = { this . component_id } >
@@ -92,4 +102,6 @@ export class HTMLContextMenu implements ContextMenu {
92
102
93
103
}
94
104
95
- }
105
+ }
106
+ SetNewContextMenuGenerator ( < PropType , > ( component_id ?: string ) => new HTMLContextMenu < PropType > ( component_id ) ) ;
107
+ export function noop ( ) { }
0 commit comments