@@ -128,7 +128,8 @@ const ControlPanelTreeItem = (props) => {
128128
129129 const getColor = ( nodeId ) => {
130130 const insts = instances . filter ( ( instance ) => instance . instancePath === nodeId ) ;
131- if ( insts . length > 0 && "color" in insts [ 0 ] ) {
131+ const hasChildren = instances . some ( ( instance ) => instance . instancePath . startsWith ( nodeId ) && instance . instancePath !== nodeId ) ;
132+ if ( props . children . length === 0 && insts . length > 0 && "color" in insts [ 0 ] ) {
132133 return insts [ 0 ] . color
133134 }
134135 // we check if all children have the same color
@@ -164,19 +165,30 @@ const ControlPanelTreeItem = (props) => {
164165 }
165166 }
166167
167- const handleColorChange = ( event , nodeId , colorGenerator ) => {
168- const children = window . Instances . getInstance ( nodeId ) . getChildren ( ) . map ( ( instance ) => instance . getInstancePath ( ) ) ;
169- if ( children . length === 0 ) { // If we're on a leaf, we do a classical color change
170- const newInstances = instances . filter ( ( instance ) => ! ( instance . instancePath . startsWith ( nodeId ) ) ) ;
171- newInstances . push ( {
172- instancePath : nodeId ,
173- color : translateColor ( colorGenerator ( ) )
174- } ) ;
175- dispatch ( changeInstanceColor ( newInstances ) ) ;
176- return
177- }
178- // Otherwise, we need to change to color of children also
179- const newInstances = instances . filter ( ( instance ) => {
168+ const collectAllChildren = ( instance ) => {
169+ const children = [ ...instance . getChildren ( ) ]
170+ children . forEach ( child => children . push ( ...collectAllChildren ( child ) ) )
171+ return children
172+ }
173+
174+ const handleLeafColorChange = ( event , nodeId , colorGenerator ) => {
175+ const updateInstances = instances . filter ( ( instance ) => ! instance . pathInstance . startsWith ( nodeId ) ) ;
176+ updateInstances . push ( {
177+ instancePath : nodeId ,
178+ color : translateColor ( colorGenerator ( ) )
179+ } ) ;
180+ dispatch ( changeInstanceColor ( updateInstances ) ) ;
181+ }
182+
183+ const handleContainerColorChange = ( event , nodeId , colorGenerator ) => {
184+ event . stopPropagation ( ) ;
185+ event . preventDefault ( ) ;
186+
187+ const childrenPaths = collectAllChildren ( window . Instances . getInstance ( nodeId ) )
188+ . filter ( ( instance ) => ! instance . getChildren ( ) || instance . getChildren ( ) . length === 0 )
189+ . map ( ( instance ) => instance . getInstancePath ( ) ) ;
190+ const children = childrenPaths . filter ( ( path ) => path . startsWith ( nodeId ) ) ;
191+ const updateInstances = instances . filter ( ( instance ) => {
180192 let condition = true ;
181193 children . forEach ( ( child ) => {
182194 if ( instance . instancePath . startsWith ( child ) ) {
@@ -185,19 +197,22 @@ const ControlPanelTreeItem = (props) => {
185197 } ) ;
186198 return condition ;
187199 } ) ;
188-
189200 children . forEach ( ( child ) => {
190- newInstances . push ( {
201+ updateInstances . push ( {
191202 instancePath : child ,
192- color : translateColor ( colorGenerator ( ) ) ,
203+ color : translateColor ( colorGenerator ( ) )
193204 } ) ;
194205 } ) ;
195- dispatch ( changeInstanceColor ( newInstances ) ) ;
196- if ( event ) {
197- event . stopPropagation ( ) ;
198- event . preventDefault ( ) ;
206+ dispatch ( changeInstanceColor ( updateInstances ) ) ;
207+ }
208+
209+ const handleColorChange = ( event , nodeId , colorGenerator ) => {
210+ if ( props . children . length === 0 ) {
211+ handleLeafColorChange ( event , nodeId , colorGenerator ) ;
212+ return
199213 }
200- } ;
214+ handleContainerColorChange ( event , nodeId , colorGenerator ) ;
215+ }
201216
202217 const changeVisibility = ( event , nodeId ) => {
203218 event . stopPropagation ( ) ;
0 commit comments