-
Notifications
You must be signed in to change notification settings - Fork 95
Expand file tree
/
Copy pathusePersistCameraConfig.ts
More file actions
61 lines (55 loc) · 1.58 KB
/
usePersistCameraConfig.ts
File metadata and controls
61 lines (55 loc) · 1.58 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
import { MaybeRef, Ref, computed, unref } from 'vue';
import { Maybe } from '@/src/types';
import useViewCameraStore from '@/src/store/view-configs/camera';
import vtkCamera from '@kitware/vtk.js/Rendering/Core/Camera';
import { vtkFieldRef } from '@/src/core/vtk/vtkFieldRef';
import { syncRef } from '@vueuse/core';
import { guardedWritableRef } from '@/src/utils/guardedWritableRef';
export function usePersistCameraConfig(
viewID: MaybeRef<string>,
dataID: MaybeRef<Maybe<string>>,
camera: MaybeRef<vtkCamera>
) {
const viewCameraStore = useViewCameraStore();
const keys = [
'position',
'focalPoint',
'viewUp',
'parallelScale',
'directionOfProjection',
] as const;
type KeyType = (typeof keys)[number];
const cameraRefs = keys.reduce(
(refs, key) => ({
...refs,
[key]: guardedWritableRef(
vtkFieldRef(camera, key),
(incoming) => !!incoming
),
}),
{} as Record<KeyType, Ref<any>>
);
const config = computed(() =>
viewCameraStore.getConfig(unref(viewID), unref(dataID))
);
const configRefs = keys.reduce(
(refs, key) => ({
...refs,
[key]: computed({
get: () => config.value?.[key],
set: (v) => {
const viewIDVal = unref(viewID);
const dataIDVal = unref(dataID);
if (!viewIDVal || !dataIDVal) return;
viewCameraStore.updateConfig(viewIDVal, dataIDVal, {
[key]: v,
});
},
}),
}),
{} as Record<KeyType, Ref<any>>
);
keys.forEach((key) => {
syncRef(configRefs[key], cameraRefs[key]);
});
}