1- import React , { useEffect , useMemo , useState } from 'react' ;
1+ import React , { useEffect , useState } from 'react' ;
22import { useSelector } from 'react-redux' ;
33import selectors from 'selectors' ;
44import core from 'core' ;
55import { getStylePanelComponent } from './StylePanelFactory' ;
66import DataElementWrapper from '../DataElementWrapper' ;
77import NoToolStylePanel from './panels/NoToolStylePanel' ;
88import getAnnotationCreateToolNames from 'helpers/getAnnotationCreateToolNames' ;
9- import {
10- shouldShowNoStyles
11- } from 'helpers/stylePanelHelper' ;
9+ import { shouldShowNoStyles } from 'helpers/stylePanelHelper' ;
10+ import debounce from 'lodash/debounce' ;
1211
1312import './StylePanel.scss' ;
1413
@@ -20,26 +19,15 @@ const StylePanelContainer = () => {
2019
2120 const [ selectedAnnotations , setSelectedAnnotations ] = useState ( core . getSelectedAnnotations ( ) ) ;
2221 const [ currentTool , setCurrentTool ] = useState ( core . getToolMode ( ) ) ;
23- const [ showStyles , setShowStyles ] = useState ( false ) ;
2422
2523 const filteredTypes = [ Annotations . PushButtonWidgetAnnotation ] ;
2624
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 ( ( ) => {
3826 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 ( ) ;
4229
30+ const allSelectedAnnotations = new Set ( annotations ) ;
4331 annotations . forEach ( ( annotation ) => {
4432 if ( annotation . isGrouped ( ) ) {
4533 const groupedAnnotations = annotationManager . getGroupAnnotations ( annotation ) ;
@@ -48,68 +36,46 @@ const StylePanelContainer = () => {
4836 annotation . getGroupedChildren ( ) . forEach ( ( child ) => allSelectedAnnotations . add ( child ) ) ;
4937 }
5038 } ) ;
51-
5239 const selectedAnnotations = Array . from ( allSelectedAnnotations ) ;
5340
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 } ) ;
7744
7845 useEffect ( ( ) => {
7946 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 ( ) ;
8848 }
8949 } , [ isPanelOpen ] ) ;
9050
9151 useEffect ( ( ) => {
92- core . addEventListener ( 'annotationSelected' , handleAnnotationSelected ) ;
93- core . addEventListener ( 'toolModeUpdated' , handleToolModeChange ) ;
52+ core . addEventListener ( 'annotationSelected' , handleChange ) ;
53+ core . addEventListener ( 'toolModeUpdated' , handleChange ) ;
9454
9555 return ( ) => {
96- core . removeEventListener ( 'annotationSelected' , handleAnnotationSelected ) ;
97- core . removeEventListener ( 'toolModeUpdated' , handleToolModeChange ) ;
56+ core . removeEventListener ( 'annotationSelected' , handleChange ) ;
57+ core . removeEventListener ( 'toolModeUpdated' , handleChange ) ;
9858 } ;
9959 } , [ ] ) ;
10060
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 ) {
10367 return NoToolStylePanel ;
10468 }
10569
10670 return getStylePanelComponent ( currentTool , selectedAnnotations ) ;
107- } , [ showStyles , currentTool , selectedAnnotations ] ) ;
71+ } ;
10872
10973 if ( ! isPanelOpen ) {
11074 return null ;
11175 }
11276
77+ const StylePanelComponent = getComponent ( ) ;
78+
11379 return (
11480 < DataElementWrapper dataElement = "stylePanel" className = "Panel StylePanel" >
11581 < StylePanelComponent
0 commit comments