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

More Events #58

Closed
xcrap opened this issue Sep 12, 2024 · 18 comments
Closed

More Events #58

xcrap opened this issue Sep 12, 2024 · 18 comments

Comments

@xcrap
Copy link

xcrap commented Sep 12, 2024

It would be good to have an even only on dragend or something, this way the requests to update the image order to my database would be much smoother instead of every single element switch :)

@TahaSh
Copy link
Owner

TahaSh commented Sep 16, 2024

@xcrap Adding more events is on my list for the upcoming releases.

@TahaSh
Copy link
Owner

TahaSh commented Sep 21, 2024

@xcrap I've added two new events in v0.3.0: onSwapEnd and onSwapStart.

Example:

swapy.onSwapStart(() => {
  console.log('Start')
})

swapy.onSwapEnd((event) => {
  console.log('end')
})

@xcrap
Copy link
Author

xcrap commented Sep 21, 2024

@xcrap I've added two new events in v0.3.0: onSwapEnd and onSwapStart.

Example:

swapy.onSwapStart(() => {
  console.log('Start')
})

swapy.onSwapEnd((event) => {
  console.log('end')
})

The problem is, if i Drag more than one element he still fires multiple times, so If I start dragging and move 3 elements and i finish dragging the onSwapEnd will fire 3 times. It was supposed to fire only once because it was still the same drag? So the onSwapEnd is having the same effect of onSwap

@TahaSh
Copy link
Owner

TahaSh commented Sep 21, 2024

@xcrap in your case, you need to use swapMode: 'drop':

createSwapy(container, {
  swapMode: 'drop'
})

@TahaSh
Copy link
Owner

TahaSh commented Sep 21, 2024

@xcrap sorry, I misread your comment. It's strange that it fires three times. It should fire once per dragging session. Does it happen if you try a simple setup? I couldn't reproduce it here.

@xcrap
Copy link
Author

xcrap commented Sep 21, 2024

document.addEventListener('DOMContentLoaded', () => {
    const container = document.querySelector('.blog-sortable-container');

    if (container) {

        const swapy = createSwapy(container, {
            animation: 'dynamic',
            // swapMode: 'drop'
        });

        swapy.onSwapEnd((event) => {
            console.log('Swap ended', event);

            updateOrder(event.data.map);
            console.log('Boink');
        });
}
});
Screenshot 2024-09-21 at 15 46 08

This is so confusing to me, it should be happening once but it's always making two requests, even i drag to the side or multiple items it's actually making always two requests independent on the number of moves. So it's not making in every single move that's already an update but I have no idea why it's happening twice.

If I go back to onSwap it's only a request per move that's the expected behavior, but something's happening here :\

@xcrap
Copy link
Author

xcrap commented Sep 21, 2024

Also the onSwapStart fires once as expected.

@TahaSh
Copy link
Owner

TahaSh commented Sep 21, 2024

@xcrap I found the bug. I'll push the fix soon and let you know!

@TahaSh
Copy link
Owner

TahaSh commented Sep 21, 2024

@xcrap It should be fixed now in v0.3.1!

@xcrap
Copy link
Author

xcrap commented Sep 21, 2024

Yes I confirm everything working as expected, thanks a lot :)

@TahaSh
Copy link
Owner

TahaSh commented Sep 22, 2024

Great, thanks!

@TahaSh TahaSh closed this as completed Sep 22, 2024
@xcrap
Copy link
Author

xcrap commented Sep 24, 2024

I've noticed another issue with this, if you click in a link inside a slot, just clicking the dragging end is also triggered, so there's an issue with that, clicking should not be considered dragging. Also if you drag but make no order change there should be something that warns about it. The clicking I think it's a bug, the drag but no change it's a suggestion :)

@TahaSh
Copy link
Owner

TahaSh commented Sep 30, 2024

@xcrap I've just released v0.4.0, which includes:

  • Prevents triggering swapEnd on click without dragging.
  • The swapEnd event now includes a parameter to indicate whether any items were swapped during the drag session. Here's an example:
swapy.onSwapEnd(({ data, hasChanged }) => {
  console.log(hasChanged);
  console.log('end', data);
})

Thanks for the suggestion :)

@xcrap
Copy link
Author

xcrap commented Sep 30, 2024

That's really cool, already updated the the click but not dragging is working great, however about the hasChanged I don't think it's making what's supposed to be, because if I drag a item to another position and drag it back to the same and then release the mouse he will return true which should actually be false because they're in the same positions :)

Thanks for the updates, really great lib!

@xcrap
Copy link
Author

xcrap commented Sep 30, 2024

Still in the back issue, I mean "indicate whether any items were swapped", they are swapped, but I think it would be more useful if you they are actually changed/updated :)

@TahaSh
Copy link
Owner

TahaSh commented Sep 30, 2024

@xcrap good catch! It should be fixed now in v0.4.1. Thanks!

@xcrap
Copy link
Author

xcrap commented Sep 30, 2024

Oh yeah it's working like a charm and it's very useful while dealing with database updates is nothing happens, great add on thanks a lot for your work, this update and the onSwapEnd has saved me dozens of requests! Life saving :)

@TahaSh
Copy link
Owner

TahaSh commented Sep 30, 2024

Thank you so much for your kind words :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants