-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathdata_style.js
More file actions
105 lines (92 loc) · 3.52 KB
/
Copy pathdata_style.js
File metadata and controls
105 lines (92 loc) · 3.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
import { database } from "@ogw_internal/database/database.js";
import { getDefaultStyle } from "@ogw_front/utils/default_styles";
import { useDataStore } from "@ogw_front/stores/data";
import { useDataStyleState } from "@ogw_internal/stores/data_style/state";
import { useMeshStyle } from "@ogw_internal/stores/data_style/mesh/index";
import { useModelStyle } from "@ogw_internal/stores/data_style/model/index";
// oxlint-disable-next-line max-lines-per-function
export const useDataStyleStore = defineStore("dataStyle", () => {
const dataStyleState = useDataStyleState();
const meshStyleStore = useMeshStyle();
const modelStyleStore = useModelStyle();
const dataStore = useDataStore();
const data_style_db = database.data_style;
const model_component_type_datastyle_db = database.model_component_type_datastyle;
const component_datastyle_db = database.model_component_datastyle;
async function addDataStyle(id, geode_object) {
await data_style_db.put(structuredClone({ id, ...getDefaultStyle(geode_object) }));
}
async function setVisibility(id, visibility) {
const item = await dataStore.item(id);
const { viewer_type } = item;
if (viewer_type === "mesh") {
return meshStyleStore.setMeshVisibility(id, visibility);
}
if (viewer_type === "model") {
return modelStyleStore.setModelVisibility(id, visibility);
}
throw new Error("Unknown viewer_type");
}
async function applyDefaultStyle(id) {
const item = await dataStore.item(id);
const { viewer_type } = item;
if (viewer_type === "mesh") {
return meshStyleStore.applyMeshStyle(id);
}
if (viewer_type === "model") {
return modelStyleStore.applyModelStyle(id);
}
throw new Error(`Unknown viewer_type: ${viewer_type}`);
}
function exportStores() {
return {
styles: dataStyleState.styles.value,
componentStyles: dataStyleState.componentStyles.value,
modelComponentTypeStyles: dataStyleState.modelComponentTypeStyles.value,
};
}
async function importStores(snapshot) {
const stylesSnapshot = snapshot.styles;
const componentStylesSnapshot = snapshot.componentStyles;
const modelComponentTypeStylesSnapshot = snapshot.modelComponentTypeStyles;
await dataStyleState.clear();
const style_promises = Object.entries(stylesSnapshot).map(([id, style]) =>
data_style_db.put(structuredClone({ id, ...style })),
);
const component_style_promises = Object.values(componentStylesSnapshot).map((style) =>
component_datastyle_db.put(structuredClone(style)),
);
const model_component_type_style_promises = Object.values(modelComponentTypeStylesSnapshot).map(
(style) => model_component_type_datastyle_db.put(structuredClone(style)),
);
await Promise.all([
...style_promises,
...component_style_promises,
...model_component_type_style_promises,
]);
}
function applyAllStylesFromState() {
const ids = Object.keys(dataStyleState.styles.value);
const promises = ids.map(async (id) => {
const meta = await dataStore.item(id);
const viewerType = meta.viewer_type;
if (viewerType === "mesh") {
return meshStyleStore.applyMeshStyle(id);
} else if (viewerType === "model") {
return modelStyleStore.applyModelStyle(id);
}
});
return Promise.all(promises);
}
return {
addDataStyle,
applyDefaultStyle,
setVisibility,
exportStores,
importStores,
applyAllStylesFromState,
...dataStyleState,
...meshStyleStore,
...modelStyleStore,
};
});