<template>
  <div draggable @dragstart="startDrag" @dragend="endDrag">
    <div v-if="!dragging" style="pointer-events: none">
      <slot></slot>
    </div>
    <div v-else class="dragging-box p-3">
      Place the component here
    </div>
  </div>
</template>

<script>
export default {
  name: "VDraggable",
  data() {
    return {
      dragging: false,
    };
  },
  methods: {
    startDrag(evt) {
      evt.dataTransfer.dropEffect = "move";
      evt.dataTransfer.effectAllowed = "move";
      evt.dataTransfer.setData("component", this);

      // Drag image
      var img = document.createElement("img");
      img.src = "../assets/logo.png";
      evt.dataTransfer.setDragImage(img, 0, 0);

      this.dragging = true;
    },
    endDrag() {
      this.dragging = false;
    },
  },
};
</script>

<style lang="scss" scoped>
.dragging-box {
  border: 2px dashed;
  opacity: 0.6;
}
</style>
