Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🐛 - Move onEnd event to the end of event queue #65

Merged
merged 1 commit into from
Jan 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion src/components/HelloWorld.vue
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,8 @@ const elements = computed(() => {
];
});

const sortable = ref<InstanceType<typeof Sortable> | null>(null);

const logEvent = (evt: Event, evt2?: Event) => {
if (evt2) {
console.log(evt, evt2);
Expand All @@ -117,6 +119,11 @@ const logEvent = (evt: Event, evt2?: Event) => {
}
};

const logClick = (evt: Event) => {
if (sortable.value?.isDragging) return;
logEvent(evt);
};

const animating = ref(true);
const scrollSensitivity = ref(50);
const scrollSpeed = ref(10);
Expand Down Expand Up @@ -221,9 +228,10 @@ main {
@filter="logEvent"
@move="logEvent"
@clone="logEvent"
ref="sortable"
>
<template #item="{ element, index }">
<div class="draggable" :key="element.id">
<div class="draggable" :key="element.id" @click="logClick">
{{ element.text }}
<Sortable
v-if="element.children"
Expand Down
22 changes: 18 additions & 4 deletions src/components/Sortable.vue
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ const emit = defineEmits<{

const attrs = useAttrs();

const isDragging = ref(false);
const containerRef = ref<HTMLElement | null>(null);
const sortable = ref<Sortable | null>(null);
const getKey = computed(() => {
Expand All @@ -73,16 +74,30 @@ const getKey = computed(() => {
return props.itemKey;
});

defineExpose({ containerRef, sortable: <Ref<Sortable | null>>sortable });
defineExpose({
containerRef,
sortable: <Ref<Sortable | null>>sortable,
isDragging,
});

watch(containerRef, (newDraggable) => {
if (newDraggable) {
sortable.value = new Sortable(newDraggable, {
...props.options,
onChoose: (event) => emit("choose", event),
onUnchoose: (event) => emit("unchoose", event),
onStart: (event) => emit("start", event),
onEnd: (event) => emit("end", event),
onStart: (event) => {
isDragging.value = true;
emit("start", event);
},
onEnd: (event) => {
// This is a hack to move the event to the end of the event queue.
// cf this issue: https://github.com/SortableJS/Sortable/issues/1184
setTimeout(() => {
isDragging.value = false;
emit("end", event);
});
},
onAdd: (event) => emit("add", event),
onUpdate: (event) => emit("update", event),
onSort: (event) => emit("sort", event),
Expand Down Expand Up @@ -117,7 +132,6 @@ onUnmounted(() => {
sortable.value = null;
}
});

</script>

<template>
Expand Down