kutlugsahin / smooth-dnd

drag and drop library for javascript
MIT License
599 stars 147 forks source link

Bug on moving draggables between two containers #26

Open adjioev opened 5 years ago

adjioev commented 5 years ago

Good day,

found the following bug: I have a layout when one container has a set of child containers and draggables: untitled diagram

If I drag say Draggable2-1 from container 2 to container 1 slowly it works fine but if I flick the mouse (do it quickly) draggable will end up in both Container 1 and Root container (duplicate itself).

What I found: container.notifyParentOnPositionCapture({element}) method has a race condition. Because there are 2 containers inside the root container and assuming we are holding draggable above container 1:

I solved this problem with zero timeout trick:

function notifyParentOnPositionCapture({ element }) {
  let isCaptured = false;
  return ({ draggableInfo, dragResult }) => {
    if (getContainer(element).getParentContainer() && isCaptured !== (dragResult.pos !== null)) {
      isCaptured = dragResult.pos !== null;
      const parent = getContainer(element).getParentContainer();
      if (isCaptured === true) {
        setTimeout(() => {
          parent.onChildPositionCaptured(isCaptured);
        }, 0);
      } else {
        parent.onChildPositionCaptured(isCaptured);
      }
    }
  };
}

Hope that helps.

Best regards, Alex