|
@@ -0,0 +1,58 @@
|
|
|
+import { onMounted, onUnmounted, ref } from "vue";
|
|
|
+
|
|
|
+export function dragImage() {
|
|
|
+ // grab moving
|
|
|
+ let pos = { top: 0, left: 0, x: 0, y: 0 };
|
|
|
+ const dragContainer = ref((null as unknown) as HTMLDivElement);
|
|
|
+ // const isGrabbing = ref(false);
|
|
|
+
|
|
|
+ const mouseDownHandler = function (e: MouseEvent) {
|
|
|
+ pos = {
|
|
|
+ // The current scroll
|
|
|
+ left: dragContainer.value.scrollLeft,
|
|
|
+ top: dragContainer.value.scrollTop,
|
|
|
+ // Get the current mouse position
|
|
|
+ x: e.clientX,
|
|
|
+ y: e.clientY,
|
|
|
+ };
|
|
|
+ // isGrabbing.value = true;
|
|
|
+ if (dragContainer.value) {
|
|
|
+ dragContainer.value.style.cursor = "grabbing";
|
|
|
+
|
|
|
+ dragContainer.value.addEventListener("mousemove", mouseMoveHandler);
|
|
|
+ dragContainer.value.addEventListener("mouseup", mouseUpHandler);
|
|
|
+ }
|
|
|
+ };
|
|
|
+
|
|
|
+ const mouseMoveHandler = function (e: MouseEvent) {
|
|
|
+ // if (!isGrabbing.value) return;
|
|
|
+ // How far the mouse has been moved
|
|
|
+ const dx = e.clientX - pos.x;
|
|
|
+ const dy = e.clientY - pos.y;
|
|
|
+
|
|
|
+ // Scroll the element
|
|
|
+ dragContainer.value.scrollTop = pos.top - dy;
|
|
|
+ dragContainer.value.scrollLeft = pos.left - dx;
|
|
|
+ };
|
|
|
+ const mouseUpHandler = function () {
|
|
|
+ // isGrabbing.value = false;
|
|
|
+ if (dragContainer.value) {
|
|
|
+ dragContainer.value.removeEventListener("mousemove", mouseMoveHandler);
|
|
|
+ dragContainer.value.removeEventListener("mouseup", mouseUpHandler);
|
|
|
+ dragContainer.value.style.cursor = "auto";
|
|
|
+ }
|
|
|
+ };
|
|
|
+
|
|
|
+ onMounted(() => {
|
|
|
+ dragContainer.value.addEventListener("mousedown", mouseDownHandler);
|
|
|
+ });
|
|
|
+ onUnmounted(() => {
|
|
|
+ if (dragContainer.value) {
|
|
|
+ dragContainer.value.removeEventListener("mousedown", mouseDownHandler);
|
|
|
+ dragContainer.value.removeEventListener("mousemove", mouseMoveHandler);
|
|
|
+ dragContainer.value.removeEventListener("mouseup", mouseUpHandler);
|
|
|
+ }
|
|
|
+ });
|
|
|
+
|
|
|
+ return { dragContainer };
|
|
|
+}
|