-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathdata_style.js
More file actions
94 lines (84 loc) · 2.93 KB
/
data_style.js
File metadata and controls
94 lines (84 loc) · 2.93 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
import { getDefaultStyle } from "@ogw_front/utils/default_styles"
import { database } from "../../internal/database/database.js"
import { useDataStore } from "@ogw_front/stores/data"
import { useDataStyleStateStore } from "../../internal/stores/data_style/state"
import useMeshStyle from "../../internal/stores/data_style/mesh/index"
import useModelStyle from "../../internal/stores/data_style/model/index"
export const useDataStyleStore = defineStore("dataStyle", () => {
const dataStyleState = useDataStyleStateStore()
const meshStyleStore = useMeshStyle()
const modelStyleStore = useModelStyle()
const dataStore = useDataStore()
function addDataStyle(id, geode_object) {
dataStyleState.styles[id] = getDefaultStyle(geode_object)
}
async function setVisibility(id, visibility) {
const item = await database.data.get(id)
const viewer_type = item?.viewer_type
if (!viewer_type) {
throw new Error(`Item not found or not loaded: ${id}`)
}
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 database.data.get(id)
const viewer_type = item?.viewer_type
if (!viewer_type) {
throw new Error(`Item not found or not loaded: ${id}`)
}
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 }
}
function importStores(snapshot) {
const stylesSnapshot = snapshot.styles || {}
for (const id of Object.keys(dataStyleState.styles)) {
delete dataStyleState.styles[id]
}
for (const [id, style] of Object.entries(stylesSnapshot)) {
dataStyleState.styles[id] = style
}
}
async function applyAllStylesFromState() {
const ids = Object.keys(dataStyleState.styles || {})
const promises = []
for (const id of ids) {
const meta = await dataStore.item(id)
const viewerType = meta?.viewer_type
const style = dataStyleState.styles[id]
if (style && viewerType === "mesh") {
promises.push(meshStyleStore.applyMeshStyle(id))
} else if (style && viewerType === "model") {
promises.push(modelStyleStore.applyModelStyle(id))
}
}
return Promise.all(promises)
}
return {
styles: dataStyleState.styles,
getStyle: dataStyleState.getStyle,
objectVisibility: dataStyleState.objectVisibility,
selectedObjects: dataStyleState.selectedObjects,
...meshStyleStore,
...modelStyleStore,
addDataStyle,
applyDefaultStyle,
setVisibility,
exportStores,
importStores,
applyAllStylesFromState,
}
})