All files / src/tools mcad-rotate.tool.ts

100% Statements 23/23
100% Branches 18/18
100% Functions 5/5
100% Lines 23/23

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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                19x 19x   19x 19x   19x 9x 1x 1x       19x 3x 1x 1x       19x 8x 6x 4x 4x 2x 1x 1x           7x 7x     19x    
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 useMcadRotateTool() {
  const { mcadEvent } = storeToRefs(useMcadStore());
  const viewer3cr = useViewer3cr();
 
  const isActive = ref<boolean>(false);
  const selected = ref<DataOverlayMcad | null>(null);
 
  watch(isActive, async (isActive) => {
    if (!isActive && selected.value?.Id) {
      await rotate(selected.value.Id, false);
      selected.value = null;
    }
  });
 
  watch(mcadEvent, async (event) => {
    if (isActive.value && event?.Interaction === InteractionType.POINTER_UP) {
      await rotate(event.Mcad.Id, true);
      selected.value = event.Mcad;
    }
  });
 
  watch(selected, async (next, prev) => {
    if (isActive.value) {
      if (next && next.Id !== prev?.Id) {
        await rotate(next.Id, true);
        selected.value = next;
      } else if (!next && prev?.Id) {
        await rotate(prev.Id, false);
        selected.value = null;
      }
    }
  });
 
  async function rotate(id: string, value: boolean): Promise<void> {
    const message: ObjectBoolean = { Version: '0.0.0', Id: id, Value: value };
    await viewer3cr.rotateMcadObject({ message });
  }
 
  return { isActive, selected };
}