sortable icon indicating copy to clipboard operation
sortable copied to clipboard

How would we go about using the SortAnimation Plugin

Open joleenshook opened this issue 4 years ago • 1 comments

I'm not sure if this is possible - it looks like the underlying library is no longer maintained by Shopify. But it would be awesome if we could. If no one is maintaining this library, can you tell me how to go about importing the appropriate plugins? I tried this:

import {Sortable, Plugins} from '@shopify/draggable/lib/sortable'


if (typeof window.livewire === 'undefined') {
    throw 'Livewire Sortable Plugin: window.livewire is undefined. Make sure @livewireScripts is placed above this script include'
}

window.livewire.directive('sortable-group', (el, directive, component) => {
    if (directive.modifiers.includes('item-group')) {
        // This will take care of new items added from Livewire during runtime.
        el.closest('[wire\\:sortable-group]').livewire_sortable.addContainer(el)
    }

    // Only fire the rest of this handler on the "root" directive.
    if (directive.modifiers.length > 0) return

    let options = {
        draggable: '[wire\\:sortable-group\\.item]',
        delay: 0,
        mirror: {
            constrainDimensions: true,
        },
        plugins: [Plugins.SortAnimation],
        swapAnimation: {
            duration: 200,
            easingFunction: 'ease-in-out',
        },
    }

    if (el.querySelector('[wire\\:sortable-group\\.handle]')) {
        options.handle ='[wire\\:sortable-group\\.handle]'
    }

    const sortable = el.livewire_sortable = new Sortable([], options);

    sortable.on('sortable:stop', () => {
        setTimeout(() => {
            let groups = []

            el.querySelectorAll('[wire\\:sortable-group\\.item-group]').forEach((el, index) => {
                let items = []
                el.querySelectorAll('[wire\\:sortable-group\\.item]').forEach((el, index) => {
                    items.push({ order: index + 1, value: el.getAttribute('wire:sortable-group.item')})
                })

                groups.push({
                    order: index + 1,
                    value: el.getAttribute('wire:sortable-group.item-group'),
                    items: items,
                })
            })

            component.call(directive.method, groups)
        }, 1)
    })
})

window.livewire.directive('sortable', (el, directive, component) => {
    // Only fire this handler on the "root" directive.
    if (directive.modifiers.length > 0) return

    let options = { draggable: '[wire\\:sortable\\.item]' }

    if (el.querySelector('[wire\\:sortable\\.handle]')) {
        options.handle ='[wire\\:sortable\\.handle]'
    }

    const sortable = new Sortable(el, options);

    sortable.on('sortable:stop', () => {
        setTimeout(() => {
            let items = []

            el.querySelectorAll('[wire\\:sortable\\.item]').forEach((el, index) => {
                items.push({ order: index + 1, value: el.getAttribute('wire:sortable.item')})
            })

            component.call(directive.method, items)
        }, 1)
    })
})

joleenshook avatar Jun 04 '21 22:06 joleenshook

edited because i had SwapAnimation and SortAnimation confused, but I still haven't got it working.

joleenshook avatar Jun 05 '21 17:06 joleenshook