Skip to content

Commit 3806d54

Browse files
committed
feat(import_project): add 2 buttons : Export and import
Compatibility with data_style
1 parent 2ca553b commit 3806d54

File tree

2 files changed

+72
-85
lines changed

2 files changed

+72
-85
lines changed

composables/project_manager.js

Lines changed: 43 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,11 @@
11
import back_schemas from "@geode/opengeodeweb-back/opengeodeweb_back_schemas.json"
22
import viewer_schemas from "@geode/opengeodeweb-viewer/opengeodeweb_viewer_schemas.json"
33
import fileDownload from "js-file-download"
4-
import { useAppStore } from "../stores/app.js"
5-
import { useGeodeStore } from "../stores/geode.js"
6-
import { useInfraStore } from "../stores/infra.js"
7-
import { viewer_call } from "./viewer_call.js"
8-
import { api_fetch } from "./api_fetch.js"
4+
95

106
export function useProjectManager() {
11-
const appStore = useAppStore()
127
const geode = useGeodeStore()
8+
const appStore = useAppStore()
139

1410
async function exportProject() {
1511
geode.start_request()
@@ -41,83 +37,19 @@ export function useProjectManager() {
4137
const infra = useInfraStore()
4238
await infra.create_connection()
4339

44-
const isJson = (file.name || "").toLowerCase().endsWith(".json")
45-
if (isJson) {
46-
const snapshot = JSON.parse(await file.text())
47-
await viewer_call({
48-
schema: viewer_schemas.opengeodeweb_viewer.import_project,
49-
params: {},
50-
})
51-
await viewer_call({
52-
schema: viewer_schemas.opengeodeweb_viewer.viewer.reset_visualization,
53-
params: {},
54-
})
55-
await appStore.importStores(snapshot)
56-
return
57-
}
58-
59-
// Import ZIP → multipart/form-data
6040
const form = new FormData()
6141
form.append("file", file, file.name || "project.zip")
6242

63-
const importId =
43+
const importPath =
6444
back_schemas.opengeodeweb_back.import_project?.$id ||
6545
"opengeodeweb_back/import_project"
66-
const importURL = new URL(
67-
"/" + String(importId),
68-
infra?.base_url || window.location.origin,
69-
).toString()
7046

71-
let result
72-
try {
73-
result = await $fetch(importURL, { method: "POST", body: form })
74-
} catch (error) {
75-
const status = error?.response?.status ?? error?.status
76-
const data = error?.response?._data ?? error?.data
77-
if (status === 423) {
78-
await viewer_call({
79-
schema:
80-
viewer_schemas.opengeodeweb_viewer.viewer.reset_visualization,
81-
params: {},
82-
})
83-
result = await $fetch(importURL, { method: "POST", body: form })
84-
} else {
85-
console.error("Backend import_project erreur:", data ?? error)
86-
}
87-
}
88-
89-
// Si le backend renvoie un snapshot, on met à jour les stores
90-
if (result?.snapshot) {
91-
await appStore.importStores(result.snapshot)
92-
}
93-
94-
// Si pas de viewables prêts, on tente des conversions courantes
95-
const needsViewables = result?.viewables_ready === false || result == null
96-
if (needsViewables) {
97-
const candidates = [
98-
{ input_geode_object: "BRep", filename: "native/main.og_brep" },
99-
{
100-
input_geode_object: "SurfaceMesh",
101-
filename: "native/main.og_mesh",
102-
},
103-
]
104-
for (const c of candidates) {
105-
try {
106-
await api_fetch({
107-
schema: back_schemas.opengeodeweb_back.save_viewable_file,
108-
params: {
109-
input_geode_object: c.input_geode_object,
110-
filename: c.filename,
111-
},
112-
})
113-
break
114-
} catch (_) {
115-
// On ignore et on essaie le candidat suivant
116-
}
117-
}
118-
}
47+
const result = await $fetch(importPath, {
48+
baseURL: geode.base_url,
49+
method: "POST",
50+
body: form,
51+
})
11952

120-
// Synchronisation Viewer
12153
await viewer_call({
12254
schema: viewer_schemas.opengeodeweb_viewer.import_project,
12355
params: {},
@@ -126,10 +58,45 @@ export function useProjectManager() {
12658
schema: viewer_schemas.opengeodeweb_viewer.viewer.reset_visualization,
12759
params: {},
12860
})
61+
62+
await appStore.importStores(result.snapshot)
63+
64+
const dataBaseStore = useDataBaseStore()
65+
for (const [id, item] of Object.entries(dataBaseStore.items)) {
66+
const registerSchema =
67+
item.object_type === "model"
68+
? viewer_schemas.opengeodeweb_viewer.model.register
69+
: viewer_schemas.opengeodeweb_viewer.mesh.register
70+
await viewer_call({ schema: registerSchema, params: { id } })
71+
72+
if (item.vtk_js?.binary_light_viewable) {
73+
await viewer_call({
74+
schema: viewer_schemas.opengeodeweb_viewer.viewer.update_data,
75+
params: {
76+
id,
77+
vtk_js: {
78+
binary_light_viewable: item.vtk_js.binary_light_viewable,
79+
},
80+
},
81+
})
82+
}
83+
84+
if (item.viewable_filename) {
85+
await viewer_call({
86+
schema: viewer_schemas.opengeodeweb_viewer.viewer.update_data,
87+
params: {
88+
id,
89+
vtk_js: { viewable_file_name: item.viewable_filename },
90+
},
91+
})
92+
}
93+
}
12994
} finally {
13095
geode.stop_request()
13196
}
13297
}
13398

13499
return { exportProject, importProjectFile }
135100
}
101+
102+
export default useProjectManager

stores/data_style.js

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,7 @@ export const useDataStyleStore = defineStore("dataStyle", () => {
1010
const dataBaseStore = useDataBaseStore()
1111

1212
/** Actions **/
13-
function addDataStyle(id, geode_object, object_type) {
14-
dataStyleState.styles[id] = getDefaultStyle(geode_object)
15-
const promise_array = []
13+
// function applyDataStyle() {
1614
if (object_type === "mesh") {
1715
promise_array.push(meshStyleStore.applyMeshDefaultStyle(id))
1816
} else if (object_type === "model") {
@@ -21,15 +19,37 @@ export const useDataStyleStore = defineStore("dataStyle", () => {
2119
} else {
2220
throw new Error("Unknown object type")
2321
}
24-
return Promise.all(promise_array)
22+
await Promise.all(promise_array);
23+
await rpc_call("opengeodeweb_viewer.viewer.render_now", {});
24+
return true;
2525
}
2626

27-
function setVisibility(id, visibility) {
28-
const object_type = dataBaseStore.itemMetaDatas(id).object_type
27+
async function setVisibility(payloadOrId, visibility) {
28+
const id =
29+
typeof payloadOrId === "string"
30+
? payloadOrId
31+
: payloadOrId?.id ?? payloadOrId?.data_id ?? payloadOrId?.model_id
32+
if (!id) return Promise.resolve([])
33+
34+
const meta = dataBaseStore.itemMetaDatas(id)
35+
const object_type = meta?.object_type
36+
if (!object_type) return Promise.resolve([])
37+
38+
if (!dataStyleState.styles[id]) {
39+
await addDataStyle(id, meta.geode_object, object_type)
40+
}
41+
42+
const visible =
43+
typeof visibility === "boolean"
44+
? visibility
45+
: payloadOrId?.visible != null
46+
? !!payloadOrId.visible
47+
: true
48+
2949
if (object_type === "mesh") {
30-
return Promise.all([meshStyleStore.setMeshVisibility(id, visibility)])
31-
} else if (object_type === "model") {
32-
return Promise.all([modelStyleStore.setModelVisibility(id, visibility)])
50+
return Promise.all([meshStyleStore.setMeshVisibility(id, visible)])
51+
} else {
52+
return Promise.all([modelStyleStore.setModelVisibility(id, visible)])
3353
}
3454
}
3555

0 commit comments

Comments
 (0)