@@ -26,7 +26,9 @@ import moduleStyles from '../modules/styles.css';
26
26
class BubbleCardEditor extends LitElement {
27
27
28
28
setConfig ( config ) {
29
- this . _config = config ;
29
+ this . _config = {
30
+ ...config
31
+ } ;
30
32
}
31
33
32
34
static get properties ( ) {
@@ -816,31 +818,77 @@ class BubbleCardEditor extends LitElement {
816
818
return ;
817
819
}
818
820
819
- const { configValue, checked } = target ;
820
- if ( configValue ) {
821
- const value = checked !== undefined ? checked : rawValue ;
822
- const configKeys = configValue . split ( '.' ) ;
823
- let obj = this . _config ;
824
-
825
- for ( let i = 0 ; i < configKeys . length - 1 ; i ++ ) {
826
- obj [ configKeys [ i ] ] = obj [ configKeys [ i ] ] || { } ;
827
- obj = obj [ configKeys [ i ] ] ;
821
+ // Create a new config object to avoid mutating the original config
822
+ let newConfig ;
823
+ try {
824
+ newConfig = { ...this . _config } ;
825
+
826
+ const { configValue, checked } = target ;
827
+ if ( configValue ) {
828
+ const value = checked !== undefined ? checked : rawValue ;
829
+ const configKeys = configValue . split ( '.' ) ;
830
+
831
+ // For nested properties, we need to clone progressively the structure
832
+ if ( configKeys . length > 1 ) {
833
+ let tempConfig = newConfig ;
834
+ let path = '' ;
835
+
836
+ for ( let i = 0 ; i < configKeys . length - 1 ; i ++ ) {
837
+ const key = configKeys [ i ] ;
838
+ path = path ? `${ path } .${ key } ` : key ;
839
+
840
+ // Create the object if it doesn't exist
841
+ if ( ! tempConfig [ key ] ) tempConfig [ key ] = { } ;
842
+
843
+ // Clone the object to ensure it is extensible
844
+ tempConfig [ key ] = { ...tempConfig [ key ] } ;
845
+ tempConfig = tempConfig [ key ] ;
846
+ }
847
+
848
+ // Update the value
849
+ const lastKey = configKeys [ configKeys . length - 1 ] ;
850
+ if ( ev . type === 'input' ) {
851
+ tempConfig [ lastKey ] = rawValue ;
852
+ } else if ( detail && tempConfig [ lastKey ] !== detail . value ) {
853
+ tempConfig [ lastKey ] = detail . value ;
854
+ } else if ( target . tagName === 'HA-SWITCH' ) {
855
+ tempConfig [ lastKey ] = rawValue ;
856
+ }
857
+ } else {
858
+ // Simple case - top level key
859
+ const key = configKeys [ 0 ] ;
860
+ if ( ev . type === 'input' ) {
861
+ newConfig [ key ] = rawValue ;
862
+ } else if ( detail && newConfig [ key ] !== detail . value ) {
863
+ newConfig [ key ] = detail . value ;
864
+ } else if ( target . tagName === 'HA-SWITCH' ) {
865
+ newConfig [ key ] = rawValue ;
866
+ }
867
+ }
868
+ } else {
869
+ newConfig = detail . value ;
828
870
}
829
-
830
- // If the event is of type 'input', update the configuration directly with the input's value
831
- if ( ev . type === 'input' ) {
832
- obj [ configKeys [ configKeys . length - 1 ] ] = rawValue ;
833
- } else if ( detail && obj [ configKeys [ configKeys . length - 1 ] ] !== detail . value ) {
834
- obj [ configKeys [ configKeys . length - 1 ] ] = detail . value ;
835
- } else if ( target . tagName === 'HA-SWITCH' ) {
836
- obj [ configKeys [ configKeys . length - 1 ] ] = rawValue ;
871
+
872
+ // Update this._config with the new config
873
+ this . _config = newConfig ;
874
+
875
+ } catch ( error ) {
876
+ console . error ( "Erreur lors de la mise à jour de la configuration:" , error ) ;
877
+
878
+ // If an error occurs, try to update directly with the new config
879
+ if ( configValue && detail ) {
880
+ const simpleConfig = { ...this . _config } ;
881
+ simpleConfig [ configValue ] = detail . value ;
882
+ newConfig = simpleConfig ;
883
+ } else if ( detail ) {
884
+ newConfig = detail . value ;
885
+ } else {
886
+ return ; // Do nothing if we can't retrieve the value
837
887
}
838
- } else {
839
- this . _config = detail . value ;
840
888
}
841
889
842
- fireEvent ( this , "config-changed" , { config : this . _config } ) ;
843
- //this.requestUpdate( );
890
+ // Emit the event with the new configuration
891
+ fireEvent ( this , "config-changed" , { config : newConfig } ) ;
844
892
}
845
893
846
894
_arrayValueChange ( index , value , array ) {
0 commit comments