Skip to content

Commit

Permalink
362 - fix: if a dropzone is destroyed during drag, it is kept in the …
Browse files Browse the repository at this point in the history
…DOM (hidden) until a drop takes place to prevent mid drag errors
  • Loading branch information
isaacHagoel committed Mar 22, 2022
1 parent 80bd3a3 commit b8e9b91
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 6 deletions.
2 changes: 1 addition & 1 deletion src/helpers/styler.js
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ export function styleDraggable(draggableEl, dragDisabled) {
* Hides the provided element so that it can stay in the dom without interrupting
* @param {HTMLElement} dragTarget
*/
export function hideOriginalDragTarget(dragTarget) {
export function hideElement(dragTarget) {
dragTarget.style.display = "none";
dragTarget.style.position = "fixed";
dragTarget.style.zIndex = "-5";
Expand Down
34 changes: 29 additions & 5 deletions src/pointerAction.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import {armWindowScroller, disarmWindowScroller} from "./helpers/windowScroller"
import {
createDraggedElementFrom,
decorateShadowEl,
hideOriginalDragTarget,
hideElement,
morphDraggedElementToBeLike,
moveDraggedElementToWasDroppedState,
preventShrinking,
Expand Down Expand Up @@ -55,6 +55,7 @@ let isWorkingOnPreviousDrag = false;
let finalizingPreviousDrag = false;
let unlockOriginDzMinDimensions;
let isDraggedOutsideOfAnyDz = false;
let scheduledForRemovalAfterDrop = [];

// a map from type to a set of drop-zones
const typeToDropZones = new Map();
Expand Down Expand Up @@ -280,10 +281,25 @@ function animateDraggedToFinalPosition(shadowElIdx, callback) {
window.setTimeout(callback, dropAnimationDurationMs);
}

function scheduleDZForRemovalAfterDrop(dz, destroy) {
scheduledForRemovalAfterDrop.push({dz, destroy});
window.requestAnimationFrame(() => {
hideElement(dz);
document.body.appendChild(dz);
});
}
/* cleanup */
function cleanupPostDrop() {
draggedEl.remove();
originalDragTarget.remove();
if (scheduledForRemovalAfterDrop.length) {
printDebug(() => ["will destroy zones that were removed during drag", scheduledForRemovalAfterDrop]);
scheduledForRemovalAfterDrop.forEach(({dz, destroy}) => {
destroy();
dz.remove();
})
scheduledForRemovalAfterDrop = [];
}
draggedEl = undefined;
originalDragTarget = undefined;
draggedElData = undefined;
Expand Down Expand Up @@ -398,7 +414,7 @@ export function dndzone(node, options) {
// to prevent the outline from disappearing
draggedEl.focus();
watchDraggedElement();
hideOriginalDragTarget(originalDragTarget);
hideElement(originalDragTarget);
originDropZoneRoot.appendChild(originalDragTarget);
} else {
window.requestAnimationFrame(keepOriginalElementInDom);
Expand Down Expand Up @@ -529,9 +545,17 @@ export function dndzone(node, options) {
configure(newOptions);
},
destroy: () => {
printDebug(() => "pointer dndzone will destroy");
unregisterDropZone(node, config.type);
dzToConfig.delete(node);
function destroyDz() {
printDebug(() => "pointer dndzone will destroy");
unregisterDropZone(node, dzToConfig.get(node).type);
dzToConfig.delete(node);
}
if (isWorkingOnPreviousDrag) {
printDebug(() => "pointer dndzone will be scheduled for destruction");
scheduleDZForRemovalAfterDrop(node, destroyDz);
} else {
destroyDz();
}
}
};
}

0 comments on commit b8e9b91

Please sign in to comment.