All files / src/tools data-overlay.tool.ts

100% Statements 29/29
100% Branches 23/23
100% Functions 6/6
100% Lines 29/29

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 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                                21x     21x   21x 21x 21x 21x   21x 21x 21x   21x 21x 21x     32x 27x         17x 13x         27x 19x         31x 26x 15x 11x 5x           20x 16x 3x 13x 6x         21x    
import { ref, watch } from 'vue';
import { InteractionType } from '@3cr/types-ts';
import { useDataOverlayStore } from '@/stores/data-overlay.store';
import { useMcadStore } from '@/stores/mcad.store';
import { storeToRefs } from 'pinia';
import {
  DataOverlay,
  DataOverlayEvent,
  McadObjectInteraction,
} from '@3cr/viewer-types-ts';
import { useEventListener } from '@vueuse/core';
 
/**
 * Displays an information modal when clicking on an annotation, measurement, or MCAD object.
 */
export function useDataOverlayTool() {
  const { annotationEvent, measurementEvent } = storeToRefs(
    useDataOverlayStore(),
  );
  const { mcadEvent } = storeToRefs(useMcadStore());
 
  const _flag = ref<boolean>(false);
  const _focused = ref<DataOverlay | null>(null);
  const isActive = ref<boolean>(false);
  const selected = ref<DataOverlay | null>(null);
 
  useEventListener(document.body, 'mousedown', onMouseDown);
  useEventListener(document.body, 'mousemove', onMouseMove);
  useEventListener(document.body, 'mouseup', onMouseUp);
 
  watch(annotationEvent, onInteraction);
  watch(measurementEvent, onInteraction);
  watch(mcadEvent, onMcadInteraction);
 
  function onMouseDown(event: MouseEvent): void {
    if (isActive.value && event.button === 0) {
      _flag.value = true;
    }
  }
 
  function onMouseMove(): void {
    if (isActive.value) {
      _flag.value = false;
    }
  }
 
  function onMouseUp(event: MouseEvent): void {
    if (isActive.value && event.button === 0 && _flag.value) {
      selected.value = _focused.value;
    }
  }
 
  function onInteraction(event: DataOverlayEvent<DataOverlay> | null): void {
    if (isActive.value) {
      if (event?.Data.Interaction === InteractionType.HOVER_START) {
        _focused.value = event.Data.DataOverlay;
      } else if (event?.Data.Interaction === InteractionType.HOVER_END) {
        _focused.value = null;
      }
    }
  }
 
  function onMcadInteraction(event: McadObjectInteraction | null): void {
    if (isActive.value) {
      if (event?.Interaction === InteractionType.HOVER_START) {
        _focused.value = event.Mcad;
      } else if (event?.Interaction === InteractionType.HOVER_END) {
        _focused.value = null;
      }
    }
  }
 
  return { isActive, selected };
}