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

Swappable:swap cancel async #603

Open
Buffele opened this issue Feb 28, 2024 · 1 comment
Open

Swappable:swap cancel async #603

Buffele opened this issue Feb 28, 2024 · 1 comment

Comments

@Buffele
Copy link

Buffele commented Feb 28, 2024

Is it possible to cancel or delay the swappable swap event?

I'd like to implement swapping, doing an ajax call, depending on the result, success -> continue with swapped, failure, revert the swapping or rather not let it continue. For now event.cancel(); only seems to work with a synchronous call.

function delay(t, val) {
    return new Promise(resolve => setTimeout(resolve, t, val));
}

const containers = document.querySelectorAll('#container');

if (containers.length === 0) {
    return false;
}

const swappable = new Draggable.Swappable(containers, {
    draggable: '.Block--isDraggable',
    mirror: {
        constrainDimensions: true,
    },
    plugins: [Draggable.Plugins.ResizeMirror],
});

swappable.on('swappable:swap', async (e) => {
    await delay(1000);  
    e.cancel(); // Does not work anymore
    console.log('Swap');
});
@Developateam
Copy link

To achieve asynchronous event handling with the swappable:swap event in the Draggable library, you need to handle the cancellation or continuation of the swap in a way that integrates with your asynchronous logic. The e.cancel() method only works synchronously, so you need to find a way to defer the decision to cancel the event until your asynchronous code completes.

Here's how you can approach this problem by using a combination of requestAnimationFrame and a custom flag to handle the swap after the asynchronous operation completes:
function delay(t, val) {
return new Promise(resolve => setTimeout(resolve, t, val));
}

const containers = document.querySelectorAll('#container');

if (containers.length === 0) {
return false;
}

const swappable = new Draggable.Swappable(containers, {
draggable: '.Block--isDraggable',
mirror: {
constrainDimensions: true,
},
plugins: [Draggable.Plugins.ResizeMirror],
});

let shouldCancelSwap = false;

swappable.on('swappable:start', (e) => {
shouldCancelSwap = false;
});

swappable.on('swappable:swap', async (e) => {
// Prevent the default swap action immediately
e.cancel();

// Perform your asynchronous operation
const result = await delay(1000);

// Depending on the result, decide whether to cancel the swap or not
if (/* condition to allow swap, e.g., result is successful */) {
    shouldCancelSwap = false;
} else {
    shouldCancelSwap = true;
}

// Use requestAnimationFrame to wait for the next frame before continuing
requestAnimationFrame(() => {
    if (!shouldCancelSwap) {
        // Manually trigger the swap event if the condition is met
        swappable.trigger('swappable:swap', e);
    }
});

});

// Optional: Handle the end of the swappable event to revert if necessary
swappable.on('swappable:end', (e) => {
if (shouldCancel

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