<script>
import Moveable from "vue3-moveable";
import InfiniteViewer from "vue3-infinite-viewer";
import { ref, onMounted } from "vue";

export default {
    components: { InfiniteViewer, Moveable },
    setup() {
        const viewerRef = ref(null);
        const scrollOptions = {
            container: () => viewerRef.value.getContainer(),
            threshold: 20,
            getScrollPosition: () => {
                return [
                    viewerRef.value.getScrollLeft({ absolute: true }),
                    viewerRef.value.getScrollTop({ absolute: true }),
                ];
            }
        };
        const onClick = () => {
            viewerRef.value.scrollCenter();
        };
        const onRender = e => {
            e.target.style.cssText += e.cssText;
        };
        const onScroll = ({ direction }) => {
            viewerRef.value.scrollBy(direction[0] * 10, direction[1] * 10);
        };
        onMounted(() => {
            setTimeout(() => {
                viewerRef.value.scrollCenter();
            }, 100);
        });
        return { onClick, viewerRef, scrollOptions, onRender, onScroll };
    }
};
</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>