1
- import React , { useEffect , useMemo , useState } from 'react' ;
1
+ import React , { useEffect , useState } from 'react' ;
2
2
import { useSelector } from 'react-redux' ;
3
3
import selectors from 'selectors' ;
4
4
import core from 'core' ;
5
5
import { getStylePanelComponent } from './StylePanelFactory' ;
6
6
import DataElementWrapper from '../DataElementWrapper' ;
7
7
import NoToolStylePanel from './panels/NoToolStylePanel' ;
8
8
import getAnnotationCreateToolNames from 'helpers/getAnnotationCreateToolNames' ;
9
- import {
10
- shouldShowNoStyles
11
- } from 'helpers/stylePanelHelper' ;
9
+ import { shouldShowNoStyles } from 'helpers/stylePanelHelper' ;
10
+ import debounce from 'lodash/debounce' ;
12
11
13
12
import './StylePanel.scss' ;
14
13
@@ -20,26 +19,15 @@ const StylePanelContainer = () => {
20
19
21
20
const [ selectedAnnotations , setSelectedAnnotations ] = useState ( core . getSelectedAnnotations ( ) ) ;
22
21
const [ currentTool , setCurrentTool ] = useState ( core . getToolMode ( ) ) ;
23
- const [ showStyles , setShowStyles ] = useState ( false ) ;
24
22
25
23
const filteredTypes = [ Annotations . PushButtonWidgetAnnotation ] ;
26
24
27
- const handleAnnotationDeselected = ( ) => {
28
- const latestTool = core . getToolMode ( ) ;
29
- if ( latestTool instanceof window . Core . Tools . AnnotationEditTool || window . Core . Tools . TextSelectTool ) {
30
- setShowStyles ( false ) ;
31
- }
32
- setSelectedAnnotations ( [ ] ) ;
33
- handleToolModeChange ( latestTool ) ;
34
- core . setToolMode ( latestTool . name ) ;
35
- } ;
36
-
37
- const handleAnnotationSelected = ( annotations , action ) => {
25
+ const handleChange = debounce ( ( ) => {
38
26
const annotationManager = core . getAnnotationManager ( ) ;
39
- const allSelectedAnnotations = new Set ( ) ;
40
-
41
- annotations . forEach ( ( annotation ) => allSelectedAnnotations . add ( annotation ) ) ;
27
+ const tool = core . getToolMode ( ) ;
28
+ const annotations = core . getSelectedAnnotations ( ) ;
42
29
30
+ const allSelectedAnnotations = new Set ( annotations ) ;
43
31
annotations . forEach ( ( annotation ) => {
44
32
if ( annotation . isGrouped ( ) ) {
45
33
const groupedAnnotations = annotationManager . getGroupAnnotations ( annotation ) ;
@@ -48,68 +36,46 @@ const StylePanelContainer = () => {
48
36
annotation . getGroupedChildren ( ) . forEach ( ( child ) => allSelectedAnnotations . add ( child ) ) ;
49
37
}
50
38
} ) ;
51
-
52
39
const selectedAnnotations = Array . from ( allSelectedAnnotations ) ;
53
40
54
- if ( action === 'selected' ) {
55
- if ( shouldShowNoStyles ( selectedAnnotations , filteredTypes ) ) {
56
- setShowStyles ( false ) ;
57
- return ;
58
- }
59
- setSelectedAnnotations ( selectedAnnotations ) ;
60
- setShowStyles ( true ) ;
61
- } else if ( action === 'deselected' ) {
62
- handleAnnotationDeselected ( ) ;
63
- }
64
- } ;
65
-
66
- const handleToolModeChange = ( newTool ) => {
67
- setCurrentTool ( newTool ) ;
68
- setSelectedAnnotations ( [ ] ) ;
69
-
70
- const toolName = newTool ?. name ;
71
- if ( annotationCreateToolNames . includes ( toolName ) ) {
72
- setShowStyles ( true ) ;
73
- } else {
74
- setShowStyles ( false ) ;
75
- }
76
- } ;
41
+ setSelectedAnnotations ( selectedAnnotations ) ;
42
+ setCurrentTool ( tool ) ;
43
+ } , 150 , { leading : false , trailing : true } ) ;
77
44
78
45
useEffect ( ( ) => {
79
46
if ( isPanelOpen ) {
80
- const annotations = core . getSelectedAnnotations ( ) ;
81
- if ( annotations . length > 0 ) {
82
- setSelectedAnnotations ( annotations ) ;
83
- setShowStyles ( true ) ;
84
- } else {
85
- const tool = core . getToolMode ( ) ;
86
- handleToolModeChange ( tool ) ;
87
- }
47
+ handleChange ( ) ;
88
48
}
89
49
} , [ isPanelOpen ] ) ;
90
50
91
51
useEffect ( ( ) => {
92
- core . addEventListener ( 'annotationSelected' , handleAnnotationSelected ) ;
93
- core . addEventListener ( 'toolModeUpdated' , handleToolModeChange ) ;
52
+ core . addEventListener ( 'annotationSelected' , handleChange ) ;
53
+ core . addEventListener ( 'toolModeUpdated' , handleChange ) ;
94
54
95
55
return ( ) => {
96
- core . removeEventListener ( 'annotationSelected' , handleAnnotationSelected ) ;
97
- core . removeEventListener ( 'toolModeUpdated' , handleToolModeChange ) ;
56
+ core . removeEventListener ( 'annotationSelected' , handleChange ) ;
57
+ core . removeEventListener ( 'toolModeUpdated' , handleChange ) ;
98
58
} ;
99
59
} , [ ] ) ;
100
60
101
- const StylePanelComponent = useMemo ( ( ) => {
102
- if ( ! showStyles ) {
61
+ const getComponent = ( ) => {
62
+ const hideStyles = selectedAnnotations . length > 0 ?
63
+ shouldShowNoStyles ( selectedAnnotations , filteredTypes ) :
64
+ ! annotationCreateToolNames . includes ( currentTool ?. name ) ;
65
+
66
+ if ( hideStyles ) {
103
67
return NoToolStylePanel ;
104
68
}
105
69
106
70
return getStylePanelComponent ( currentTool , selectedAnnotations ) ;
107
- } , [ showStyles , currentTool , selectedAnnotations ] ) ;
71
+ } ;
108
72
109
73
if ( ! isPanelOpen ) {
110
74
return null ;
111
75
}
112
76
77
+ const StylePanelComponent = getComponent ( ) ;
78
+
113
79
return (
114
80
< DataElementWrapper dataElement = "stylePanel" className = "Panel StylePanel" >
115
81
< StylePanelComponent
0 commit comments