To ensure the dragging of the selected item keeps happening until it is let go, you can modify the doDrag function to compare the new item index with the current moving item index, rather than restricting the movement to adjacent items. Here's an updated version of the code:
In the <script setup> section, update the doDrag function:
const doDrag = (event) => {
const mouseY = event ? (event.pageY || event.touches[0].pageY) : 0;
const direction = mouseY > state.initialMouseY ? 1 : -1;
if (state.draggingIndex !== null) {
const newIndex = state.movingItemIndex + direction;
if (newIndex >= 0 && newIndex < state.list.length && newIndex !== state.draggingIndex) {
[state.list[state.draggingIndex], state.list[newIndex]] = [state.list[newIndex], state.list[state.draggingIndex]];
state.movingItemIndex = newIndex;
state.draggingIndex = newIndex;
}
}
};
Here, we've removed the Math.abs(newIndex - state.draggingIndex) === 1 condition because we want the swapping to happen freely across the list as long as the user doesn't let go of the dragged item. Additionally, we reassign the draggingIndex to newIndex to keep the dragged item index up to date.
Now, when you drag an item, it will keep swapping positions with other items as long as you're dragging it without letting go.
Here's the updated full code:
<script setup>
import { reactive, computed } from 'vue';
const state = reactive({
list: [
{ id: 1, value: 'Item 1' },
{ id: 2, value: 'Item 2' },
{ id: 3, value: 'Item 3' },
{ id: 4, value: 'Item 4' },
],
draggingIndex: null,
initialMouseY: 0,
movingItemIndex: null,
});
const list = computed(() => state.list);
const draggingIndex = computed(() => state.draggingIndex);
const movingItemIndex = computed(() => state.movingItemIndex);
const startDrag = (index, event) => {
state.draggingIndex = index;
state.initialMouseY = event ? (event.pageY || event.touches[0].pageY) : 0;
state.movingItemIndex = index;
document.addEventListener('mousemove', doDrag);
document.addEventListener('touchmove', doDrag);
document.addEventListener('mouseup', stopDrag);
document.addEventListener('touchend', stopDrag);
};
const doDrag = (event) => {
const mouseY = event ? (event.pageY || event.touches[0].pageY) : 0;
const direction = mouseY > state.initialMouseY ? 1 : -1;
if (state.draggingIndex !== null) {
const newIndex = state.movingItemIndex + direction;
if (newIndex >= 0 && newIndex < state.list.length && newIndex !== state.draggingIndex) {
[state.list[state.draggingIndex], state.list[newIndex]] = [state.list[newIndex], state.list[state.draggingIndex]];
state.movingItemIndex = newIndex;
state.draggingIndex = newIndex;
}
}
};
const stopDrag = () => {
state.draggingIndex = null;
state.initialMouseY = 0;
state.movingItemIndex = null;
document.removeEventListener('mousemove', doDrag);
document.removeEventListener('touchmove', doDrag);
document.removeEventListener('mouseup', stopDrag);
document.removeEventListener('touchend', stopDrag);
};
</script>
<template>
<transition-group name="list-item" tag="div" class="list-container">
<div
v-for="(item, index) in list"
:key="item.id"
:class="['list-item', { 'is-dragging': draggingIndex === index, 'list-item-move': movingItemIndex === index }]"
@mousedown="startDrag(index, $event)"
@touchstart="startDrag(index, $event)"
@mouseup="stopDrag"
@touchend="stopDrag"
@mousemove="doDrag($event)"
@touchmove="doDrag($event)"
>
{{ item.value }}
</div>
</transition-group>
</template>
Now, the dragging will continue happening until the user lets go of the mouse or touch end event.