@atlaskit/modal-dialog
Version:
A modal dialog displays content that requires user interaction, in a layer above the page.
84 lines (83 loc) • 2.93 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.disableDraggingToCrossOriginIFramesForElement = disableDraggingToCrossOriginIFramesForElement;
var _bindEventListener = require("bind-event-listener");
var _combine = require("@atlaskit/pragmatic-drag-and-drop/combine");
var _adapter = require("@atlaskit/pragmatic-drag-and-drop/element/adapter");
var _makeFixForAdapter = require("./make-fix-for-adapter");
function watchForInteractionEnd(_ref) {
var stop = _ref.stop;
var isDragging = false;
function stopIfNotDragging() {
if (isDragging) {
return;
}
stop();
}
var unbindEvents = (0, _bindEventListener.bindAll)(window, [{
// Another interaction is starting, this fix should be removed.
type: 'pointerdown',
listener: stop
}, {
// The user did not start a drag
type: 'pointerup',
listener: stopIfNotDragging
}, {
// if a "dragstart" occurs and the flag is not set,
// then a drag has not started.
// Note: could not use "pointercancel" as it is not
// published in Safari
// → https://bugs.webkit.org/show_bug.cgi?id=222632
type: 'dragstart',
listener: stopIfNotDragging,
// Need to come after the element adapter
options: {
capture: false
}
}], {
// Listening in the capture phase to increase resilience
// against events being stopped.
capture: true,
// Being super clear these should only run once
once: true
});
var unbindMonitor = (0, _adapter.monitorForElements)({
onGenerateDragPreview: function onGenerateDragPreview() {
isDragging = true;
},
onDrop: function onDrop() {
isDragging = false;
stop();
}
});
return (0, _combine.combine)(unbindEvents, unbindMonitor);
}
function watchForInteractionStart(_ref2) {
var start = _ref2.start;
return (0, _bindEventListener.bind)(window, {
// Note: Using "mousedown" rather than "pointerdown" due to a Safari bug.
// Safari not publish a "pointerdown" on the interaction after a drag
// → https://bugs.webkit.org/show_bug.cgi?id=279749
type: 'mousedown',
listener: function listener(event) {
// Only starting if pressing down inside a draggable element
// At this point, we are not sure which if:
// 1. a text selection drag is starting
// 2. a draggable managed by pdnd is going to be dragged
// 3. a draggable not managed by pdnd is going to be dragged
// 4. The user will be dragging anything at all (might be doing a click)
if (event.target instanceof HTMLElement && event.target.closest('[draggable="true"]')) {
start();
}
}
});
}
var api = (0, _makeFixForAdapter.makeFixForAdapter)({
watchForInteractionStart: watchForInteractionStart,
watchForInteractionEnd: watchForInteractionEnd
});
function disableDraggingToCrossOriginIFramesForElement() {
return api.registerUsage();
}