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

Lit + SortableJS enhancements #1345

Open
silvester-pari opened this issue Nov 4, 2024 · 0 comments
Open

Lit + SortableJS enhancements #1345

silvester-pari opened this issue Nov 4, 2024 · 0 comments
Labels
enhancement New feature or request layercontrol

Comments

@silvester-pari
Copy link
Collaborator

I believe there are still some cases where sorting layers causes troubles because of the interaction between Lit and SortableJS. There have been some discussions over in the SortableJs repository which might come in handy. One proposed workaround was: https://lit.dev/playground/#gist=242f45fd2dbe21ecb6902f144686aae8

This addresses the underlying issue which was causing us some trouble:

  firstUpdated() {
    /* 
      List elements have important part marker nodes around them
      which do *not* move when items are dragged with Sortable.
      This can cause the Sortable ordering and Lit's ordering
      to get out of sync.
      To address this, whenever Sortable moves an item, ensure
      the item's markers move with it and this requires understanding how Lit renders parts.
      
      For a list, Lit renders DOM as follows:
      <parent>
      <!-- list start -->
        <!-- item 0 start --> item <!-- item 0 end -->
        <!-- item 1 start --> item <!-- item 1 end -->
        <!-- item N start --> item 
      <!-- list end -->
      </parent>
      
      Importantly Lit does *not* have a last item part marker and this must be accounted for.
    */ 
    let before;
    Sortable.create(this.draggableList, {
      animation: 150,
      ghostClass: 'ghost',
      onStart: (e) => {
        before = e.item.previousSibling;
      },
      onEnd: (e) => {
        // put the item back
        before.after(e.item);
        this.items.splice(e.newIndex, 0, this.items.splice(e.oldIndex, 1)[0]);
        this.requestUpdate();
      },
    });
  }

image

@silvester-pari silvester-pari added enhancement New feature or request layercontrol labels Nov 4, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request layercontrol
Projects
None yet
Development

No branches or pull requests

1 participant