@@ -126,19 +126,45 @@ const ControlPanelTreeItem = (props) => {
126126 hex : "#FF7F99"
127127 } ;
128128
129+ const getParent = ( nodeId ) => {
130+ const insts = instances . filter ( ( instance ) =>
131+ nodeId . replace ( instance . instancePath , '' ) !== nodeId && instance . instancePath . length < nodeId . length
132+ ) ;
133+ if ( insts . length === 0 ) {
134+ return null ;
135+ }
136+ return insts . sort ( ( a , b ) => b . instancePath . length - a . instancePath . length ) [ 0 ]
137+ }
138+
129139 const getColor = ( nodeId ) => {
130140 const insts = instances . filter ( ( instance ) => instance . instancePath === nodeId ) ;
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 ] ) {
133- return insts [ 0 ] . color
141+ const parent = getParent ( nodeId ) ;
142+ // If there is no recorded parent, and there is no instances
143+ if ( ! parent && insts . length === 0 ) {
144+ return defaultColor ;
145+ }
146+ // If there is a parent, but no instances, we take the color of the parent
147+ if ( insts . length === 0 ) {
148+ return parent . color
149+ }
150+ const inst = insts [ 0 ]
151+ // If the instance doesn't have children and have a color
152+ if ( props . children && props . children . length === 0 && insts . length > 0 && "color" in inst ) {
153+ return inst . color
134154 }
135155 // we check if all children have the same color
136156 const children = instances . filter ( ( instance ) => instance . instancePath . startsWith ( nodeId ) && instance . instancePath !== nodeId ) ;
157+ // if we cannot find children (no color set for children)
137158 if ( children . length === 0 ) {
159+ // if there is a color, we display it
160+ if ( inst . color ) {
161+ return inst . color
162+ }
163+ // if there is none (color not set) we take the default one
138164 return defaultColor ;
139165 }
140166 const color = children [ 0 ] . color ;
141- if ( children . every ( x => x . color && x . color . hex === color . hex ) ) {
167+ if ( children . length > 1 && children . every ( x => x . color && x . color . hex === color . hex ) ) {
142168 return color
143169 }
144170 return { hex : "#989898" }
@@ -165,12 +191,6 @@ const ControlPanelTreeItem = (props) => {
165191 }
166192 }
167193
168- const collectAllChildren = ( instance ) => {
169- const children = [ ...instance . getChildren ( ) ]
170- children . forEach ( child => children . push ( ...collectAllChildren ( child ) ) )
171- return children
172- }
173-
174194 const handleLeafColorChange = ( event , nodeId , colorGenerator ) => {
175195 const updateInstances = instances . filter ( ( instance ) => ! instance . instancePath . startsWith ( nodeId ) ) ;
176196 updateInstances . push ( {
@@ -184,8 +204,8 @@ const ControlPanelTreeItem = (props) => {
184204 event . stopPropagation ( ) ;
185205 event . preventDefault ( ) ;
186206
187- const childrenPaths = collectAllChildren ( window . Instances . getInstance ( nodeId ) )
188- . filter ( ( instance ) => ! instance . getChildren ( ) || instance . getChildren ( ) . length === 0 )
207+ const instance = window . Instances . getInstance ( nodeId ) ;
208+ const childrenPaths = [ ... instance . getChildren ( ) ]
189209 . map ( ( instance ) => instance . getInstancePath ( ) ) ;
190210 const children = childrenPaths . filter ( ( path ) => path . startsWith ( nodeId ) ) ;
191211 const updateInstances = instances . filter ( ( instance ) => {
@@ -294,7 +314,7 @@ const ControlPanelTreeItem = (props) => {
294314 < IconButton onClick = { ( event ) => changeVisibility ( event , nodeId ) } >
295315 { visibility ? < Visibility style = { { marginRight : '0.5rem' } } /> : < VisibilityOff style = { { marginRight : '0.5rem' } } /> }
296316 </ IconButton >
297- < IconButton disabled = { disableRandom } onClick = { ( event ) => handleColorChange ( event , nodeId , randomColor ) } >
317+ < IconButton onClick = { ( event ) => handleColorChange ( event , nodeId , randomColor ) } >
298318 < RandomColorLensIcon style = { { marginRight : '0.5rem' } } />
299319 </ IconButton >
300320 </ >
0 commit comments