<script>
import Selecto from "vue3-selecto";
import Moveable from "vue3-moveable";
import { ref } from "vue";

export default {
    components: { Moveable, Selecto },
    setup() {
        const targets = ref([]);
        const moveableRef = ref(null);
        const selectoRef = ref(null);
        const cubes = [];
        for (let i = 0; i < 30; ++i) {
            cubes.push(i);
        }
        const onClickGroup = e => {
            selectoRef.value.clickTarget(e.inputEvent, e.inputTarget);
        };
        const onDrag = e => {
            e.target.style.transform = e.transform;
        };
        const onDragGroup = e => {
            e.events.forEach(ev => {
                ev.target.style.transform = ev.transform;
            });
        };
        const onDragStart = e => {
            const moveable = moveableRef.value;
            const target = e.inputEvent.target;
            if (moveable.isMoveableElement(target)
                || targets.value.some(t => t === target || t.contains(target))
            ) {
                e.stop();
            }
        };
        const onSelectEnd = e => {
            const moveable = moveableRef.value;
            if (e.isDragStart) {
                e.inputEvent.preventDefault();
                moveable.waitToChangeTarget().then(() => {
                    moveable.dragStart(e.inputEvent);
                });
            }
            targets.value = e.selected;
        };
        return {
            moveableRef,
            targets,
            onClickGroup,
            onDrag,
            onDragGroup,
            selectoRef,
            window,
            onDragStart,
            onSelectEnd,
            cubes
        };
    }
};
</script>
<template>
    <div class="root">
        <div class="container">
            <Moveable
                ref="moveableRef"
                :draggable="true"
                :target="targets"
                @clickGroup="onClickGroup"
                @drag="onDrag"
                @dragGroup="onDragGroup"
            ></Moveable>
            <Selecto
                ref="selectoRef"
                :dragContainer="window"
                :selectableTargets="['.selecto-area .cube']"
                :hitRate="0"
                :selectByClick="true"
                :selectFromInside="false"
                :toggleContinueSelect="['shift']"
                :ratio="0"
                @dragStart="onDragStart"
                @selectEnd="onSelectEnd"
            ></Selecto>
            <div class="elements selecto-area">
                <div
                    class="cube"
                    :key="i"
                    v-for="i in cubes"
                ></div>
            </div>
            <div class="empty elements"></div>
        </div>
    </div>
</template>