Skip to content

Commit aac686e

Browse files
committed
fix: complete setConfig en getConfig
1 parent bf6e023 commit aac686e

File tree

2 files changed

+61
-10
lines changed

2 files changed

+61
-10
lines changed

src/GEOComp.tsx

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@ import {
1414
arrayStringExposingStateControl,
1515
withMethodExposing,
1616
AutoHeightControl,
17+
changeValueAction,
1718
} from "lowcoder-sdk";
19+
//import { changeValueAction } from "lowcoder-core";
1820
import styles from "./styles.module.css";
1921
import { i18nObjs, trans } from "./i18n/comps";
2022
import { Geo } from "./vendors";
@@ -25,6 +27,7 @@ import { useResizeDetector } from "react-resize-detector";
2527
import Notification from 'ol-ext/control/Notification'
2628
import { featureControl } from './FeaturesControl';
2729
import { geoContext } from './GEOContext';
30+
import { deepMerge } from './vendors/helpers/DeepMerge';
2831

2932
export const CompStyles = [
3033
{
@@ -547,15 +550,9 @@ GEOComp = withMethodExposing(GEOComp, [
547550
}
548551
}
549552
}
550-
for (const [key, value] of Object.entries(data)) {
551-
var child = comp.children[key]
552-
console.log(key, child)
553-
if (child.value) {
554-
child.value(value)
555-
} else {
556-
console.debug("setConfig not supported for ", child)
557-
}
558-
}
553+
//Load by the new values dispatching them,
554+
//first merging the current values with the new values
555+
comp.dispatch(changeValueAction(deepMerge(comp.toJsonValue(), data), true))
559556
} catch (e) {
560557
console.error("Failed to parse config data", e)
561558
return false
@@ -590,7 +587,6 @@ GEOComp = withMethodExposing(GEOComp, [
590587
}
591588
}
592589
}
593-
594590
//Should we convert the data into string
595591
data = params[1] !== true ? data : JSON.stringify(data, null)
596592
if (geoContext.previewMode)

src/vendors/helpers/DeepMerge.js

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
export function deepMerge(...objs) {
2+
3+
/**
4+
* Get the object type
5+
* @param {*} obj The object
6+
* @return {String} The object type
7+
*/
8+
function getType(obj) {
9+
return Object.prototype.toString.call(obj).slice(8, -1).toLowerCase();
10+
}
11+
12+
/**
13+
* Deep merge two objects
14+
* @return {Object}
15+
*/
16+
function mergeObj(clone, obj) {
17+
for (let [key, value] of Object.entries(obj)) {
18+
let type = getType(value);
19+
if (clone[key] !== undefined && getType(clone[key]) === type && ['array', 'object'].includes(type)) {
20+
clone[key] = deepMerge(clone[key], value);
21+
} else {
22+
clone[key] = structuredClone(value);
23+
}
24+
}
25+
}
26+
27+
// Create a clone of the first item in the objs array
28+
let clone = structuredClone(objs.shift());
29+
30+
// Loop through each item
31+
for (let obj of objs) {
32+
33+
// Get the object type
34+
let type = getType(obj);
35+
36+
// If the current item isn't the same type as the clone, replace it
37+
if (getType(clone) !== type) {
38+
clone = structuredClone(obj);
39+
continue;
40+
}
41+
42+
// Otherwise, merge
43+
if (type === 'array') {
44+
clone = [...clone, ...structuredClone(obj)];
45+
} else if (type === 'object') {
46+
mergeObj(clone, obj);
47+
} else {
48+
clone = obj;
49+
}
50+
51+
}
52+
53+
return clone;
54+
55+
}

0 commit comments

Comments
 (0)