<script>
import Moveable from "vue-moveable";
import InfiniteViewer from "vue-infinite-viewer";

export default {
    components: { InfiniteViewer, Moveable },
    data() {
        const viewerRef = null;
        const scrollOptions = {
            container: () => viewerRef.getContainer(),
            threshold: 20,
            getScrollPosition: () => {
                return [
                    viewerRef.getScrollLeft({ absolute: true }),
                    viewerRef.getScrollTop({ absolute: true }),
                ];
            }
        };
        return { viewerRef, scrollOptions };
    },
    methods: {
        onClick() {
            this.$refs.viewerRef.scrollCenter();
        },
        onRender(e) {
            e.target.style.cssText += e.cssText;
        },
        onScroll({ direction }) {
            this.$refs.viewerRef.scrollBy(
                direction[0] * 10,
                direction[1] * 10
            );
        }
    },
    mounted() {
        setTimeout(() => {
            this.$refs.viewerRef.scrollCenter();
        }, 100);
        this.viewerRef = this.$refs.viewerRef;
    }
};
</script>
<template>
    <div class="container">
        <button
            class="button"
            @click="onClick"
            style="position: absolute;top: 10px;right: 10px;z-index: 1;"
        >Scroll Center</button>
        <InfiniteViewer
            className="infinite-viewer"
            ref="viewerRef"
        >
            <div
                class="viewport"
                style="width: 400px;height: 400px;border: 1px solid #ccc;"
            >
                <div class="target">Target</div>
                <Moveable
                    :scrollable="true"
                    :scrollOptions="scrollOptions"
                    :target="'.target'"
                    :draggable="true"
                    :resizable="true"
                    :rotatable="true"
                    @render="onRender"
                    @scroll="onScroll"
                ></Moveable>
            </div>
        </InfiniteViewer>
    </div>
</template>