<script>
import Moveable from "vue-moveable";

export default {
    components: { Moveable },
    data() {
        const resizable = "$preview_resizable";
        const keepRatio = "$preview_keepRatio";
        const throttleResize = "$preview_throttleResize";
        const renderDirections = "$preview_renderDirections";
        const widthInputRef = null;
        const heightInputRef = null;
        const moveableRef = null;
        const onInput = (e) => {
            const ev = (e.nativeEvent || e);
            if (typeof ev.data === "undefined") {
                moveableRef.request("resizable", {
                    offsetWidth: parseFloat(widthInputRef.value),
                    offsetHeight: parseFloat(heightInputRef.value)
                }, true);
            }
        };
        const onKeyUp = (e) => {
            e.stopPropagation();
            if (e.keyCode === 13) {
                moveableRef.request("resizable", {
                    offsetWidth: parseFloat(widthInputRef.value),
                    offsetHeight: parseFloat(heightInputRef.value)
                }, true);
            }
        };
        return {
            resizable,
            keepRatio,
            throttleResize,
            renderDirections,
            widthInputRef,
            heightInputRef,
            moveableRef,
            onInput,
            onKeyUp
        };
    },
    methods: {
        onResize(e) {
            e.target.style.width = `${e.width}px`;
            e.target.style.height = `${e.height}px`;
            e.target.style.transform = e.drag.transform;
        },
        onResizeEnd(e) {
            requestAnimationFrame(() => {
                const rect = e.moveable.getRect();
                this.$refs.widthInputRef.value = `${rect.offsetWidth}`;
                this.$refs.heightInputRef.value = `${rect.offsetHeight}`;
            });
        }
    },
    mounted() {
        this.widthInputRef = this.$refs.widthInputRef;
        this.heightInputRef = this.$refs.heightInputRef;
        this.moveableRef = this.$refs.moveableRef;
    }
};
</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>