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

export default {
    components: { Moveable },
    setup() {
        const resizable = "$preview_resizable";
        const keepRatio = "$preview_keepRatio";
        const throttleResize = "$preview_throttleResize";
        const renderDirections = "$preview_renderDirections";
        const widthInputRef = ref(null);
        const heightInputRef = ref(null);
        const moveableRef = ref(null);
        const onInput = (e) => {
            const ev = (e.nativeEvent || e);
            if (typeof ev.data === "undefined") {
                moveableRef.value.request("resizable", {
                    offsetWidth: parseFloat(widthInputRef.value.value),
                    offsetHeight: parseFloat(heightInputRef.value.value)
                }, true);
            }
        };
        const onKeyUp = (e) => {
            e.stopPropagation();
            if (e.keyCode === 13) {
                moveableRef.value.request("resizable", {
                    offsetWidth: parseFloat(widthInputRef.value.value),
                    offsetHeight: parseFloat(heightInputRef.value.value)
                }, true);
            }
        };
        const onResize = (e) => {
            e.target.style.width = `${e.width}px`;
            e.target.style.height = `${e.height}px`;
            e.target.style.transform = e.drag.transform;
        };
        const onResizeEnd = (e) => {
            requestAnimationFrame(() => {
                const rect = e.moveable.getRect();
                widthInputRef.value.value = `${rect.offsetWidth}`;
                heightInputRef.value.value = `${rect.offsetHeight}`;
            });
        };
        return {
            widthInputRef,
            onInput,
            onKeyUp,
            heightInputRef,
            moveableRef,
            resizable,
            keepRatio,
            throttleResize,
            renderDirections,
            onResize,
            onResizeEnd
        };
    }
};
</script>
<template>
    <div class="root">
        <div>
            width:
            <input
                ref="widthInputRef"
                type="number"
                defaultValue="280"
                @input="onInput"
                @keyUp="onKeyUp"
                data-horizontal="true"/>
            &nbsp;
                        height:
            <input
                ref="heightInputRef"
                type="number"
                defaultValue="230"
                @input="onInput"
                @keyUp="onKeyUp"
                data-horizontal="false"/>
        </div>
        <div class="container">
            <div class="target">Target1</div>
            <Moveable
                ref="moveableRef"
                :target="'.target'"
                :resizable="resizable"
                :keepRatio="keepRatio"
                :throttleResize="throttleResize"
                :renderDirections="renderDirections"
                @resize="onResize"
                @resizeEnd="onResizeEnd"/>
        </div>
    </div>
</template>