UNPKG

react-dnd-html5-backend

Version:

HTML5 backend for React DnD

734 lines (586 loc) 27.9 kB
"use strict"; function _typeof(obj) { "@babel/helpers - typeof"; if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); } Object.defineProperty(exports, "__esModule", { value: true }); exports.HTML5BackendImpl = void 0; var _EnterLeaveCounter = require("./EnterLeaveCounter"); var _OffsetUtils = require("./OffsetUtils"); var _NativeDragSources = require("./NativeDragSources"); var NativeTypes = _interopRequireWildcard(require("./NativeTypes")); var _OptionsReader = require("./OptionsReader"); function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); } function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || _typeof(obj) !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; } function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) { symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); } keys.push.apply(keys, symbols); } return keys; } function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; } function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; } function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } var HTML5BackendImpl = /*#__PURE__*/function () { // React-Dnd Components // Internal State function HTML5BackendImpl(manager, globalContext, options) { var _this = this; _classCallCheck(this, HTML5BackendImpl); _defineProperty(this, "options", void 0); _defineProperty(this, "actions", void 0); _defineProperty(this, "monitor", void 0); _defineProperty(this, "registry", void 0); _defineProperty(this, "enterLeaveCounter", void 0); _defineProperty(this, "sourcePreviewNodes", new Map()); _defineProperty(this, "sourcePreviewNodeOptions", new Map()); _defineProperty(this, "sourceNodes", new Map()); _defineProperty(this, "sourceNodeOptions", new Map()); _defineProperty(this, "dragStartSourceIds", null); _defineProperty(this, "dropTargetIds", []); _defineProperty(this, "dragEnterTargetIds", []); _defineProperty(this, "currentNativeSource", null); _defineProperty(this, "currentNativeHandle", null); _defineProperty(this, "currentDragSourceNode", null); _defineProperty(this, "altKeyPressed", false); _defineProperty(this, "mouseMoveTimeoutTimer", null); _defineProperty(this, "asyncEndDragFrameId", null); _defineProperty(this, "dragOverTargetIds", null); _defineProperty(this, "getSourceClientOffset", function (sourceId) { var source = _this.sourceNodes.get(sourceId); return source && (0, _OffsetUtils.getNodeClientOffset)(source) || null; }); _defineProperty(this, "endDragNativeItem", function () { if (!_this.isDraggingNativeItem()) { return; } _this.actions.endDrag(); if (_this.currentNativeHandle) { _this.registry.removeSource(_this.currentNativeHandle); } _this.currentNativeHandle = null; _this.currentNativeSource = null; }); _defineProperty(this, "isNodeInDocument", function (node) { // Check the node either in the main document or in the current context return Boolean(node && _this.document && _this.document.body && _this.document.body.contains(node)); }); _defineProperty(this, "endDragIfSourceWasRemovedFromDOM", function () { var node = _this.currentDragSourceNode; if (node == null || _this.isNodeInDocument(node)) { return; } if (_this.clearCurrentDragSourceNode() && _this.monitor.isDragging()) { _this.actions.endDrag(); } }); _defineProperty(this, "handleTopDragStartCapture", function () { _this.clearCurrentDragSourceNode(); _this.dragStartSourceIds = []; }); _defineProperty(this, "handleTopDragStart", function (e) { if (e.defaultPrevented) { return; } var dragStartSourceIds = _this.dragStartSourceIds; _this.dragStartSourceIds = null; var clientOffset = (0, _OffsetUtils.getEventClientOffset)(e); // Avoid crashing if we missed a drop event or our previous drag died if (_this.monitor.isDragging()) { _this.actions.endDrag(); } // Don't publish the source just yet (see why below) _this.actions.beginDrag(dragStartSourceIds || [], { publishSource: false, getSourceClientOffset: _this.getSourceClientOffset, clientOffset: clientOffset }); var dataTransfer = e.dataTransfer; var nativeType = (0, _NativeDragSources.matchNativeItemType)(dataTransfer); if (_this.monitor.isDragging()) { if (dataTransfer && typeof dataTransfer.setDragImage === 'function') { // Use custom drag image if user specifies it. // If child drag source refuses drag but parent agrees, // use parent's node as drag image. Neither works in IE though. var sourceId = _this.monitor.getSourceId(); var sourceNode = _this.sourceNodes.get(sourceId); var dragPreview = _this.sourcePreviewNodes.get(sourceId) || sourceNode; if (dragPreview) { var _this$getCurrentSourc = _this.getCurrentSourcePreviewNodeOptions(), anchorX = _this$getCurrentSourc.anchorX, anchorY = _this$getCurrentSourc.anchorY, offsetX = _this$getCurrentSourc.offsetX, offsetY = _this$getCurrentSourc.offsetY; var anchorPoint = { anchorX: anchorX, anchorY: anchorY }; var offsetPoint = { offsetX: offsetX, offsetY: offsetY }; var dragPreviewOffset = (0, _OffsetUtils.getDragPreviewOffset)(sourceNode, dragPreview, clientOffset, anchorPoint, offsetPoint); dataTransfer.setDragImage(dragPreview, dragPreviewOffset.x, dragPreviewOffset.y); } } try { // Firefox won't drag without setting data dataTransfer === null || dataTransfer === void 0 ? void 0 : dataTransfer.setData('application/json', {}); } catch (err) {// IE doesn't support MIME types in setData } // Store drag source node so we can check whether // it is removed from DOM and trigger endDrag manually. _this.setCurrentDragSourceNode(e.target); // Now we are ready to publish the drag source.. or are we not? var _this$getCurrentSourc2 = _this.getCurrentSourcePreviewNodeOptions(), captureDraggingState = _this$getCurrentSourc2.captureDraggingState; if (!captureDraggingState) { // Usually we want to publish it in the next tick so that browser // is able to screenshot the current (not yet dragging) state. // // It also neatly avoids a situation where render() returns null // in the same tick for the source element, and browser freaks out. setTimeout(function () { return _this.actions.publishDragSource(); }, 0); } else { // In some cases the user may want to override this behavior, e.g. // to work around IE not supporting custom drag previews. // // When using a custom drag layer, the only way to prevent // the default drag preview from drawing in IE is to screenshot // the dragging state in which the node itself has zero opacity // and height. In this case, though, returning null from render() // will abruptly end the dragging, which is not obvious. // // This is the reason such behavior is strictly opt-in. _this.actions.publishDragSource(); } } else if (nativeType) { // A native item (such as URL) dragged from inside the document _this.beginDragNativeItem(nativeType); } else if (dataTransfer && !dataTransfer.types && (e.target && !e.target.hasAttribute || !e.target.hasAttribute('draggable'))) { // Looks like a Safari bug: dataTransfer.types is null, but there was no draggable. // Just let it drag. It's a native type (URL or text) and will be picked up in // dragenter handler. return; } else { // If by this time no drag source reacted, tell browser not to drag. e.preventDefault(); } }); _defineProperty(this, "handleTopDragEndCapture", function () { if (_this.clearCurrentDragSourceNode() && _this.monitor.isDragging()) { // Firefox can dispatch this event in an infinite loop // if dragend handler does something like showing an alert. // Only proceed if we have not handled it already. _this.actions.endDrag(); } }); _defineProperty(this, "handleTopDragEnterCapture", function (e) { _this.dragEnterTargetIds = []; var isFirstEnter = _this.enterLeaveCounter.enter(e.target); if (!isFirstEnter || _this.monitor.isDragging()) { return; } var dataTransfer = e.dataTransfer; var nativeType = (0, _NativeDragSources.matchNativeItemType)(dataTransfer); if (nativeType) { // A native item (such as file or URL) dragged from outside the document _this.beginDragNativeItem(nativeType, dataTransfer); } }); _defineProperty(this, "handleTopDragEnter", function (e) { var dragEnterTargetIds = _this.dragEnterTargetIds; _this.dragEnterTargetIds = []; if (!_this.monitor.isDragging()) { // This is probably a native item type we don't understand. return; } _this.altKeyPressed = e.altKey; // If the target changes position as the result of `dragenter`, `dragover` might still // get dispatched despite target being no longer there. The easy solution is to check // whether there actually is a target before firing `hover`. if (dragEnterTargetIds.length > 0) { _this.actions.hover(dragEnterTargetIds, { clientOffset: (0, _OffsetUtils.getEventClientOffset)(e) }); } var canDrop = dragEnterTargetIds.some(function (targetId) { return _this.monitor.canDropOnTarget(targetId); }); if (canDrop) { // IE requires this to fire dragover events e.preventDefault(); if (e.dataTransfer) { e.dataTransfer.dropEffect = _this.getCurrentDropEffect(); } } }); _defineProperty(this, "handleTopDragOverCapture", function () { _this.dragOverTargetIds = []; }); _defineProperty(this, "handleTopDragOver", function (e) { var dragOverTargetIds = _this.dragOverTargetIds; _this.dragOverTargetIds = []; if (!_this.monitor.isDragging()) { // This is probably a native item type we don't understand. // Prevent default "drop and blow away the whole document" action. e.preventDefault(); if (e.dataTransfer) { e.dataTransfer.dropEffect = 'none'; } return; } _this.altKeyPressed = e.altKey; _this.actions.hover(dragOverTargetIds || [], { clientOffset: (0, _OffsetUtils.getEventClientOffset)(e) }); var canDrop = (dragOverTargetIds || []).some(function (targetId) { return _this.monitor.canDropOnTarget(targetId); }); if (canDrop) { // Show user-specified drop effect. e.preventDefault(); if (e.dataTransfer) { e.dataTransfer.dropEffect = _this.getCurrentDropEffect(); } } else if (_this.isDraggingNativeItem()) { // Don't show a nice cursor but still prevent default // "drop and blow away the whole document" action. e.preventDefault(); } else { e.preventDefault(); if (e.dataTransfer) { e.dataTransfer.dropEffect = 'none'; } } }); _defineProperty(this, "handleTopDragLeaveCapture", function (e) { if (_this.isDraggingNativeItem()) { e.preventDefault(); } var isLastLeave = _this.enterLeaveCounter.leave(e.target); if (!isLastLeave) { return; } if (_this.isDraggingNativeItem()) { setTimeout(function () { return _this.endDragNativeItem(); }, 0); } }); _defineProperty(this, "handleTopDropCapture", function (e) { _this.dropTargetIds = []; if (_this.isDraggingNativeItem()) { var _this$currentNativeSo; e.preventDefault(); (_this$currentNativeSo = _this.currentNativeSource) === null || _this$currentNativeSo === void 0 ? void 0 : _this$currentNativeSo.loadDataTransfer(e.dataTransfer); } else if ((0, _NativeDragSources.matchNativeItemType)(e.dataTransfer)) { // Dragging some elements, like <a> and <img> may still behave like a native drag event, // even if the current drag event matches a user-defined type. // Stop the default behavior when we're not expecting a native item to be dropped. e.preventDefault(); } _this.enterLeaveCounter.reset(); }); _defineProperty(this, "handleTopDrop", function (e) { var dropTargetIds = _this.dropTargetIds; _this.dropTargetIds = []; _this.actions.hover(dropTargetIds, { clientOffset: (0, _OffsetUtils.getEventClientOffset)(e) }); _this.actions.drop({ dropEffect: _this.getCurrentDropEffect() }); if (_this.isDraggingNativeItem()) { _this.endDragNativeItem(); } else if (_this.monitor.isDragging()) { _this.actions.endDrag(); } }); _defineProperty(this, "handleSelectStart", function (e) { var target = e.target; // Only IE requires us to explicitly say // we want drag drop operation to start if (typeof target.dragDrop !== 'function') { return; } // Inputs and textareas should be selectable if (target.tagName === 'INPUT' || target.tagName === 'SELECT' || target.tagName === 'TEXTAREA' || target.isContentEditable) { return; } // For other targets, ask IE // to enable drag and drop e.preventDefault(); target.dragDrop(); }); this.options = new _OptionsReader.OptionsReader(globalContext, options); this.actions = manager.getActions(); this.monitor = manager.getMonitor(); this.registry = manager.getRegistry(); this.enterLeaveCounter = new _EnterLeaveCounter.EnterLeaveCounter(this.isNodeInDocument); } /** * Generate profiling statistics for the HTML5Backend. */ _createClass(HTML5BackendImpl, [{ key: "profile", value: function profile() { var _this$dragStartSource, _this$dragOverTargetI; return { sourcePreviewNodes: this.sourcePreviewNodes.size, sourcePreviewNodeOptions: this.sourcePreviewNodeOptions.size, sourceNodeOptions: this.sourceNodeOptions.size, sourceNodes: this.sourceNodes.size, dragStartSourceIds: ((_this$dragStartSource = this.dragStartSourceIds) === null || _this$dragStartSource === void 0 ? void 0 : _this$dragStartSource.length) || 0, dropTargetIds: this.dropTargetIds.length, dragEnterTargetIds: this.dragEnterTargetIds.length, dragOverTargetIds: ((_this$dragOverTargetI = this.dragOverTargetIds) === null || _this$dragOverTargetI === void 0 ? void 0 : _this$dragOverTargetI.length) || 0 }; } // public for test }, { key: "window", get: function get() { return this.options.window; } }, { key: "document", get: function get() { return this.options.document; } /** * Get the root element to use for event subscriptions */ }, { key: "rootElement", get: function get() { return this.options.rootElement; } }, { key: "setup", value: function setup() { var root = this.rootElement; if (root === undefined) { return; } if (root.__isReactDndBackendSetUp) { throw new Error('Cannot have two HTML5 backends at the same time.'); } root.__isReactDndBackendSetUp = true; this.addEventListeners(root); } }, { key: "teardown", value: function teardown() { var root = this.rootElement; if (root === undefined) { return; } root.__isReactDndBackendSetUp = false; this.removeEventListeners(this.rootElement); this.clearCurrentDragSourceNode(); if (this.asyncEndDragFrameId) { var _this$window; (_this$window = this.window) === null || _this$window === void 0 ? void 0 : _this$window.cancelAnimationFrame(this.asyncEndDragFrameId); } } }, { key: "connectDragPreview", value: function connectDragPreview(sourceId, node, options) { var _this2 = this; this.sourcePreviewNodeOptions.set(sourceId, options); this.sourcePreviewNodes.set(sourceId, node); return function () { _this2.sourcePreviewNodes.delete(sourceId); _this2.sourcePreviewNodeOptions.delete(sourceId); }; } }, { key: "connectDragSource", value: function connectDragSource(sourceId, node, options) { var _this3 = this; this.sourceNodes.set(sourceId, node); this.sourceNodeOptions.set(sourceId, options); var handleDragStart = function handleDragStart(e) { return _this3.handleDragStart(e, sourceId); }; var handleSelectStart = function handleSelectStart(e) { return _this3.handleSelectStart(e); }; node.setAttribute('draggable', 'true'); node.addEventListener('dragstart', handleDragStart); node.addEventListener('selectstart', handleSelectStart); return function () { _this3.sourceNodes.delete(sourceId); _this3.sourceNodeOptions.delete(sourceId); node.removeEventListener('dragstart', handleDragStart); node.removeEventListener('selectstart', handleSelectStart); node.setAttribute('draggable', 'false'); }; } }, { key: "connectDropTarget", value: function connectDropTarget(targetId, node) { var _this4 = this; var handleDragEnter = function handleDragEnter(e) { return _this4.handleDragEnter(e, targetId); }; var handleDragOver = function handleDragOver(e) { return _this4.handleDragOver(e, targetId); }; var handleDrop = function handleDrop(e) { return _this4.handleDrop(e, targetId); }; node.addEventListener('dragenter', handleDragEnter); node.addEventListener('dragover', handleDragOver); node.addEventListener('drop', handleDrop); return function () { node.removeEventListener('dragenter', handleDragEnter); node.removeEventListener('dragover', handleDragOver); node.removeEventListener('drop', handleDrop); }; } }, { key: "addEventListeners", value: function addEventListeners(target) { // SSR Fix (https://github.com/react-dnd/react-dnd/pull/813 if (!target.addEventListener) { return; } target.addEventListener('dragstart', this.handleTopDragStart); target.addEventListener('dragstart', this.handleTopDragStartCapture, true); target.addEventListener('dragend', this.handleTopDragEndCapture, true); target.addEventListener('dragenter', this.handleTopDragEnter); target.addEventListener('dragenter', this.handleTopDragEnterCapture, true); target.addEventListener('dragleave', this.handleTopDragLeaveCapture, true); target.addEventListener('dragover', this.handleTopDragOver); target.addEventListener('dragover', this.handleTopDragOverCapture, true); target.addEventListener('drop', this.handleTopDrop); target.addEventListener('drop', this.handleTopDropCapture, true); } }, { key: "removeEventListeners", value: function removeEventListeners(target) { // SSR Fix (https://github.com/react-dnd/react-dnd/pull/813 if (!target.removeEventListener) { return; } target.removeEventListener('dragstart', this.handleTopDragStart); target.removeEventListener('dragstart', this.handleTopDragStartCapture, true); target.removeEventListener('dragend', this.handleTopDragEndCapture, true); target.removeEventListener('dragenter', this.handleTopDragEnter); target.removeEventListener('dragenter', this.handleTopDragEnterCapture, true); target.removeEventListener('dragleave', this.handleTopDragLeaveCapture, true); target.removeEventListener('dragover', this.handleTopDragOver); target.removeEventListener('dragover', this.handleTopDragOverCapture, true); target.removeEventListener('drop', this.handleTopDrop); target.removeEventListener('drop', this.handleTopDropCapture, true); } }, { key: "getCurrentSourceNodeOptions", value: function getCurrentSourceNodeOptions() { var sourceId = this.monitor.getSourceId(); var sourceNodeOptions = this.sourceNodeOptions.get(sourceId); return _objectSpread({ dropEffect: this.altKeyPressed ? 'copy' : 'move' }, sourceNodeOptions || {}); } }, { key: "getCurrentDropEffect", value: function getCurrentDropEffect() { if (this.isDraggingNativeItem()) { // It makes more sense to default to 'copy' for native resources return 'copy'; } return this.getCurrentSourceNodeOptions().dropEffect; } }, { key: "getCurrentSourcePreviewNodeOptions", value: function getCurrentSourcePreviewNodeOptions() { var sourceId = this.monitor.getSourceId(); var sourcePreviewNodeOptions = this.sourcePreviewNodeOptions.get(sourceId); return _objectSpread({ anchorX: 0.5, anchorY: 0.5, captureDraggingState: false }, sourcePreviewNodeOptions || {}); } }, { key: "isDraggingNativeItem", value: function isDraggingNativeItem() { var itemType = this.monitor.getItemType(); return Object.keys(NativeTypes).some(function (key) { return NativeTypes[key] === itemType; }); } }, { key: "beginDragNativeItem", value: function beginDragNativeItem(type, dataTransfer) { this.clearCurrentDragSourceNode(); this.currentNativeSource = (0, _NativeDragSources.createNativeDragSource)(type, dataTransfer); this.currentNativeHandle = this.registry.addSource(type, this.currentNativeSource); this.actions.beginDrag([this.currentNativeHandle]); } }, { key: "setCurrentDragSourceNode", value: function setCurrentDragSourceNode(node) { var _this5 = this; this.clearCurrentDragSourceNode(); this.currentDragSourceNode = node; // A timeout of > 0 is necessary to resolve Firefox issue referenced // See: // * https://github.com/react-dnd/react-dnd/pull/928 // * https://github.com/react-dnd/react-dnd/issues/869 var MOUSE_MOVE_TIMEOUT = 1000; // Receiving a mouse event in the middle of a dragging operation // means it has ended and the drag source node disappeared from DOM, // so the browser didn't dispatch the dragend event. // // We need to wait before we start listening for mousemove events. // This is needed because the drag preview needs to be drawn or else it fires an 'mousemove' event // immediately in some browsers. // // See: // * https://github.com/react-dnd/react-dnd/pull/928 // * https://github.com/react-dnd/react-dnd/issues/869 // this.mouseMoveTimeoutTimer = setTimeout(function () { var _this5$rootElement; return (_this5$rootElement = _this5.rootElement) === null || _this5$rootElement === void 0 ? void 0 : _this5$rootElement.addEventListener('mousemove', _this5.endDragIfSourceWasRemovedFromDOM, true); }, MOUSE_MOVE_TIMEOUT); } }, { key: "clearCurrentDragSourceNode", value: function clearCurrentDragSourceNode() { if (this.currentDragSourceNode) { this.currentDragSourceNode = null; if (this.rootElement) { var _this$window2; (_this$window2 = this.window) === null || _this$window2 === void 0 ? void 0 : _this$window2.clearTimeout(this.mouseMoveTimeoutTimer || undefined); this.rootElement.removeEventListener('mousemove', this.endDragIfSourceWasRemovedFromDOM, true); } this.mouseMoveTimeoutTimer = null; return true; } return false; } }, { key: "handleDragStart", value: function handleDragStart(e, sourceId) { if (e.defaultPrevented) { return; } if (!this.dragStartSourceIds) { this.dragStartSourceIds = []; } this.dragStartSourceIds.unshift(sourceId); } }, { key: "handleDragEnter", value: function handleDragEnter(e, targetId) { this.dragEnterTargetIds.unshift(targetId); } }, { key: "handleDragOver", value: function handleDragOver(e, targetId) { if (this.dragOverTargetIds === null) { this.dragOverTargetIds = []; } this.dragOverTargetIds.unshift(targetId); } }, { key: "handleDrop", value: function handleDrop(e, targetId) { this.dropTargetIds.unshift(targetId); } }]); return HTML5BackendImpl; }(); exports.HTML5BackendImpl = HTML5BackendImpl;