All files / src/stores mcad-floating.store.ts

100% Statements 78/78
100% Branches 36/36
100% Functions 15/15
100% Lines 77/77

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 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143      4x 4x   4x 4x 4x 4x     4x 1x 1x   1x 1x 1x   1x 1x       1x 1x 1x 1x 1x         6x 6x 6x 2x         9x 9x 9x 2x       1x 2x                 45x 45x 45x 45x   45x 45x 45x                   21x 21x 9x 12x 3x   9x           7x   7x 4x 4x   7x 3x 3x   7x 2x 2x   7x 2x 2x   7x 7x 7x 7x 7x 2x 2x           8x 8x 1x 1x 1x     8x 1x 1x 1x       1x 2x 1x 1x 1x       1x    
import { defineStore } from 'pinia';
import { computed, nextTick, ref, watch } from 'vue';
 
const resetSmoothAnimation = 300;
const edgePx = 12;
 
export enum FloatingState {
  REGULAR = 'regular',
  VERTICAL_SQUASH = 'vertical_squash',
  HORIZONTAL_SQUASH = 'horizontal_squash',
}
 
export const useMcadFloatingStore = defineStore('mcad-floating', () => {
  const x = ref(16);
  const y = ref(16);
 
  const state = ref<FloatingState>(FloatingState.REGULAR);
  const smooth = ref<boolean>(false);
  const isDragging = ref<boolean>(false);
 
  addEventListener('resize', (event) => {
    smoothXY(16, 16);
  });
 
  function smoothXY(newX: number, newY: number) {
    smooth.value = true;
    x.value = newX;
    y.value = newY;
    setTimeout(() => {
      smooth.value = false;
    }, resetSmoothAnimation);
  }
 
  function smoothDeltaX(newX: number) {
    smooth.value = true;
    x.value += newX;
    setTimeout(() => {
      smooth.value = false;
    }, resetSmoothAnimation);
  }
 
  function smoothDeltaY(newY: number) {
    smooth.value = true;
    y.value += newY;
    setTimeout(() => {
      smooth.value = false;
    }, resetSmoothAnimation);
  }
 
  const style = computed(() => {
    return {
      transform: 'translate(' + x.value + 'px, ' + y.value + 'px)',
      transition: smooth.value
        ? `all ${resetSmoothAnimation}ms ease-in-out`
        : '',
    };
  });
 
  function getDimensions() {
    const self = document.getElementById('drag-set');
    const parent = self?.parentElement;
    const currentHeight = self?.clientHeight || 0;
    const parentY = (parent?.clientHeight || 0) - currentHeight;
 
    const currentWidth = self?.clientWidth || 0;
    const parentX = (parent?.clientWidth || 0) - currentWidth;
    return {
      parentX,
      parentY,
      currentWidth,
      currentHeight,
    };
  }
 
  // Update the edge state if sticky
  function setEdge() {
    const { parentX, parentY } = getDimensions();
    if (x.value <= edgePx || x.value >= parentX - edgePx) {
      state.value = FloatingState.VERTICAL_SQUASH;
    } else if (y.value <= edgePx || y.value >= parentY - edgePx) {
      state.value = FloatingState.HORIZONTAL_SQUASH;
    } else {
      state.value = FloatingState.REGULAR;
    }
  }
 
  // Update the edge state if sticky
  async function open() {
    const { parentX, parentY } = getDimensions();
 
    if (y.value >= parentY - edgePx) {
      await nextTick();
      smoothDeltaY(-(160 + 20));
    }
    if (x.value >= parentX - edgePx) {
      await nextTick();
      smoothDeltaX(-266);
    }
    if (y.value <= edgePx) {
      await nextTick();
      smoothDeltaY(20);
    }
    if (x.value <= edgePx) {
      await nextTick();
      smoothDeltaX(20);
    }
    setEdge();
    await reposition();
    setEdge();
    const { currentHeight } = getDimensions();
    if (y.value >= parentY - edgePx) {
      await nextTick();
      smoothDeltaY(-currentHeight);
    }
  }
 
  // If changing the edge state moves the container out of edge, reposition
  async function reposition() {
    const { parentX, parentY, currentWidth, currentHeight } = getDimensions();
    if (y.value >= parentY - edgePx) {
      await nextTick();
      const { currentHeight: latestHeight } = getDimensions();
      smoothDeltaY(currentHeight - latestHeight);
    }
 
    if (x.value >= parentX - edgePx) {
      await nextTick();
      const { currentWidth: latestWidth } = getDimensions();
      smoothDeltaX(currentWidth - latestWidth);
    }
  }
 
  watch(isDragging, async (dragging) => {
    if (!dragging) {
      setEdge();
      await reposition();
      setEdge();
    }
  });
 
  return { style, state, x, y, isDragging, open, setEdge };
});