Skip to content

Commit a64a983

Browse files
authored
Fixed some editor issues. #1416 #1423
1 parent e9dbaad commit a64a983

File tree

3 files changed

+72
-24
lines changed

3 files changed

+72
-24
lines changed

src/editor/bubble-card-editor.js

Lines changed: 70 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,9 @@ import moduleStyles from '../modules/styles.css';
2626
class BubbleCardEditor extends LitElement {
2727

2828
setConfig(config) {
29-
this._config = config;
29+
this._config = {
30+
...config
31+
};
3032
}
3133

3234
static get properties() {
@@ -816,31 +818,77 @@ class BubbleCardEditor extends LitElement {
816818
return;
817819
}
818820

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;
828870
}
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
837887
}
838-
} else {
839-
this._config = detail.value;
840888
}
841889

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 });
844892
}
845893

846894
_arrayValueChange(index, value, array) {

src/modules/editor.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ export function makeModulesEditor(context) {
137137
<code-block><pre>
138138
# Storage for Bubble Card Modules
139139
template:
140-
- triggers:
140+
- trigger:
141141
- trigger: event
142142
event_type: bubble_card_update_modules
143143
sensor:

src/var/version.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
export let version = 'v3.0.0-beta.2';
1+
export let version = 'v3.0.0-beta.3';

0 commit comments

Comments
 (0)