import { ref, watch } from 'vue'; import { InteractionType, ObjectBoolean } from '@3cr/types-ts'; import { useMcadStore } from '@/stores/mcad.store'; import { useViewer3cr } from '@/composables/useViewer3cr'; import { storeToRefs } from 'pinia'; import { DataOverlayMcad } from '@3cr/viewer-types-ts'; export function useMcadMoveTool() { const { mcadEvent } = storeToRefs(useMcadStore()); const viewer3cr = useViewer3cr(); const isActive = ref(false); const selected = ref(null); watch(isActive, async (isActive) => { if (!isActive && selected.value?.Id) { await move(selected.value.Id, false); selected.value = null; } }); watch(mcadEvent, async (event) => { if (isActive.value && event?.Interaction === InteractionType.POINTER_UP) { await move(event.Mcad.Id, true); selected.value = event.Mcad; } }); watch(selected, async (next, prev) => { if (isActive.value) { if (next && next.Id !== prev?.Id) { await move(next.Id, true); selected.value = next; } else if (!next && prev?.Id) { await move(prev.Id, false); selected.value = null; } } }); async function move(id: string, value: boolean): Promise { const message: ObjectBoolean = { Version: '0.0.0', Id: id, Value: value }; await viewer3cr.moveMcadObject({ message }); } return { isActive, selected }; }