Skip to content

Commit 695b226

Browse files
committed
fix(core): solve bug when using nested configs
1 parent 4d4df75 commit 695b226

4 files changed

Lines changed: 147 additions & 121 deletions

File tree

packages/core/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "@thatopen/components",
33
"description": "Collection of core functionalities to author BIM apps.",
4-
"version": "2.4.0-alpha.31",
4+
"version": "2.4.0-alpha.32",
55
"author": "That Open Company",
66
"contributors": [
77
"Antonio Gonzalez Viegas (https://github.com/agviegas)",
Lines changed: 47 additions & 118 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,5 @@
11
import * as THREE from "three";
2-
import {
3-
ControlsSchema,
4-
ControlEntry,
5-
BooleanSettingsControl,
6-
ColorSettingsControl,
7-
TextSettingsControl,
8-
NumberSettingControl,
9-
SelectSettingControl,
10-
Vector3SettingControl,
11-
TextSetSettingControl,
12-
NoControl,
13-
} from "../../Types";
2+
import { ControlsSchema, ControlsUtils } from "../../Types";
143
import { Components } from "../../Components";
154
import { ConfigManager } from "../index";
165
import { UUID } from "../../../utils";
@@ -28,12 +17,7 @@ export abstract class Configurator<
2817
uuid: string;
2918

3019
get controls(): U {
31-
const copy: any = {};
32-
for (const name in this._config) {
33-
const entry = this._config[name] as ControlEntry;
34-
copy[name] = this.copyEntry(entry);
35-
}
36-
return copy as U;
20+
return ControlsUtils.copySchema(this._config);
3721
}
3822

3923
constructor(
@@ -58,116 +42,61 @@ export abstract class Configurator<
5842
}
5943
}
6044

61-
export() {
62-
const serializedData: any = {};
63-
for (const id in this._config) {
64-
const control = this._config[id];
65-
66-
if (control.type === "Color") {
67-
const { r, g, b } = control.value;
68-
serializedData[id] = { ...control, value: { r, g, b } };
69-
} else if (control.type === "Vector3") {
70-
const { x, y, z } = control.value;
71-
serializedData[id] = { ...control, value: { x, y, z } };
72-
} else if (control.type === "TextSet") {
73-
const value = Array.from(control.value);
74-
serializedData[id] = { ...control, value };
75-
} else if (control.type === "Select") {
76-
const options = Array.from(control.options);
77-
serializedData[id] = { ...control, options };
45+
export(controls = this._config as ControlsSchema, exported: any = {}) {
46+
for (const id in controls) {
47+
const control = controls[id];
48+
const isControl = ControlsUtils.isEntry(control);
49+
if (isControl) {
50+
if (control.type === "Color") {
51+
const { r, g, b } = control.value;
52+
exported[id] = { ...control, value: { r, g, b } };
53+
} else if (control.type === "Vector3") {
54+
const { x, y, z } = control.value;
55+
exported[id] = { ...control, value: { x, y, z } };
56+
} else if (control.type === "TextSet") {
57+
const value = Array.from(control.value);
58+
exported[id] = { ...control, value };
59+
} else if (control.type === "Select") {
60+
const options = Array.from(control.options);
61+
exported[id] = { ...control, options };
62+
} else {
63+
exported[id] = { ...control };
64+
}
7865
} else {
79-
serializedData[id] = { ...control };
66+
exported[id] = {};
67+
this.export(control as ControlsSchema, exported[id]);
8068
}
8169
}
8270

83-
return serializedData;
71+
return exported;
8472
}
8573

86-
import(serializedData: any) {
87-
const imported: any = {};
88-
89-
for (const id in serializedData) {
90-
const control = serializedData[id];
91-
if (control.type === "Color") {
92-
const { r, g, b } = control.value;
93-
imported[id] = { ...control, value: new THREE.Color(r, g, b) };
94-
} else if (control.type === "Vector3") {
95-
const { x, y, z } = control.value;
96-
imported[id] = { ...control, value: new THREE.Vector3(x, y, z) };
97-
} else if (control.type === "TextSet") {
98-
imported[id] = { ...control, value: new Set(control.value) };
99-
} else if (control.type === "Select") {
100-
imported[id] = { ...control, options: new Set(control.options) };
74+
import(exported: any, imported: any = {}, first = true) {
75+
for (const id in exported) {
76+
const control = exported[id];
77+
const isControl = ControlsUtils.isEntry(control);
78+
if (isControl) {
79+
if (control.type === "Color") {
80+
const { r, g, b } = control.value;
81+
imported[id] = { ...control, value: new THREE.Color(r, g, b) };
82+
} else if (control.type === "Vector3") {
83+
const { x, y, z } = control.value;
84+
imported[id] = { ...control, value: new THREE.Vector3(x, y, z) };
85+
} else if (control.type === "TextSet") {
86+
imported[id] = { ...control, value: new Set(control.value) };
87+
} else if (control.type === "Select") {
88+
imported[id] = { ...control, options: new Set(control.options) };
89+
} else {
90+
imported[id] = { ...control };
91+
}
10192
} else {
102-
imported[id] = { ...control };
93+
imported[id] = {};
94+
this.import(control, imported[id], false);
10395
}
10496
}
10597

106-
this.set(imported);
107-
}
108-
109-
copyEntry(controlEntry: ControlEntry): ControlEntry {
110-
if (controlEntry.type === "Boolean") {
111-
const entry = controlEntry as BooleanSettingsControl;
112-
return {
113-
type: entry.type,
114-
value: entry.value,
115-
};
116-
}
117-
if (controlEntry.type === "Color") {
118-
const entry = controlEntry as ColorSettingsControl;
119-
return {
120-
type: entry.type,
121-
value: entry.value.clone(),
122-
};
123-
}
124-
if (controlEntry.type === "Text") {
125-
const entry = controlEntry as TextSettingsControl;
126-
return {
127-
type: entry.type,
128-
value: entry.value,
129-
};
130-
}
131-
if (controlEntry.type === "Number") {
132-
const entry = controlEntry as NumberSettingControl;
133-
return {
134-
type: entry.type,
135-
value: entry.value,
136-
min: entry.min,
137-
max: entry.max,
138-
interpolable: entry.interpolable,
139-
};
140-
}
141-
if (controlEntry.type === "Select") {
142-
const entry = controlEntry as SelectSettingControl;
143-
return {
144-
type: entry.type,
145-
value: entry.value,
146-
multiple: entry.multiple,
147-
options: new Set(entry.options),
148-
};
149-
}
150-
if (controlEntry.type === "Vector3") {
151-
const entry = controlEntry as Vector3SettingControl;
152-
return {
153-
type: entry.type,
154-
value: entry.value.clone(),
155-
};
156-
}
157-
if (controlEntry.type === "TextSet") {
158-
const entry = controlEntry as TextSetSettingControl;
159-
return {
160-
type: entry.type,
161-
value: new Set(entry.value),
162-
};
163-
}
164-
if (controlEntry.type === "None") {
165-
const entry = controlEntry as NoControl;
166-
return {
167-
type: entry.type,
168-
value: entry.value,
169-
};
98+
if (first) {
99+
this.set(imported);
170100
}
171-
throw new Error("Invalid entry!");
172101
}
173102
}

packages/core/src/core/Types/src/config-types.ts

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,3 +58,100 @@ export type ControlEntry =
5858
export interface ControlsSchema {
5959
[name: string]: ControlEntry | ControlsSchema;
6060
}
61+
62+
export class ControlsUtils {
63+
static isEntry(item: any) {
64+
const types = new Set([
65+
"Boolean",
66+
"Color",
67+
"Text",
68+
"Number",
69+
"Select",
70+
"Vector3",
71+
"TextSet",
72+
"None",
73+
]);
74+
return types.has(item.type);
75+
}
76+
77+
static copySchema<T extends ControlsSchema = ControlsSchema>(
78+
schema: T,
79+
copy: ControlsSchema = {},
80+
) {
81+
for (const name in schema) {
82+
const entry = schema[name];
83+
if (this.isEntry(entry)) {
84+
copy[name] = this.copyEntry(entry as ControlEntry);
85+
} else {
86+
copy[name] = {};
87+
this.copySchema(entry as ControlsSchema, copy[name] as ControlsSchema);
88+
}
89+
}
90+
return copy as T;
91+
}
92+
93+
static copyEntry(controlEntry: ControlEntry): ControlEntry {
94+
if (controlEntry.type === "Boolean") {
95+
const entry = controlEntry as BooleanSettingsControl;
96+
return {
97+
type: entry.type,
98+
value: entry.value,
99+
};
100+
}
101+
if (controlEntry.type === "Color") {
102+
const entry = controlEntry as ColorSettingsControl;
103+
return {
104+
type: entry.type,
105+
value: entry.value.clone(),
106+
};
107+
}
108+
if (controlEntry.type === "Text") {
109+
const entry = controlEntry as TextSettingsControl;
110+
return {
111+
type: entry.type,
112+
value: entry.value,
113+
};
114+
}
115+
if (controlEntry.type === "Number") {
116+
const entry = controlEntry as NumberSettingControl;
117+
return {
118+
type: entry.type,
119+
value: entry.value,
120+
min: entry.min,
121+
max: entry.max,
122+
interpolable: entry.interpolable,
123+
};
124+
}
125+
if (controlEntry.type === "Select") {
126+
const entry = controlEntry as SelectSettingControl;
127+
return {
128+
type: entry.type,
129+
value: entry.value,
130+
multiple: entry.multiple,
131+
options: new Set(entry.options),
132+
};
133+
}
134+
if (controlEntry.type === "Vector3") {
135+
const entry = controlEntry as Vector3SettingControl;
136+
return {
137+
type: entry.type,
138+
value: entry.value.clone(),
139+
};
140+
}
141+
if (controlEntry.type === "TextSet") {
142+
const entry = controlEntry as TextSetSettingControl;
143+
return {
144+
type: entry.type,
145+
value: new Set(entry.value),
146+
};
147+
}
148+
if (controlEntry.type === "None") {
149+
const entry = controlEntry as NoControl;
150+
return {
151+
type: entry.type,
152+
value: entry.value,
153+
};
154+
}
155+
throw new Error("Invalid entry!");
156+
}
157+
}

packages/front/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "@thatopen/components-front",
33
"description": "Collection of frontend tools to author BIM apps.",
4-
"version": "2.4.0-alpha.31",
4+
"version": "2.4.0-alpha.32",
55
"author": "That Open Company",
66
"contributors": [
77
"Antonio Gonzalez Viegas (https://github.com/agviegas)",
@@ -47,7 +47,7 @@
4747
"web-ifc": "0.0.61"
4848
},
4949
"dependencies": {
50-
"@thatopen/components": ">=2.4.0-alpha.30",
50+
"@thatopen/components": ">=2.4.0-alpha.32",
5151
"camera-controls": "2.7.3",
5252
"dexie": "^4.0.4",
5353
"earcut": "^2.2.4",

0 commit comments

Comments
 (0)