You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

145 lines
4.3 KiB

"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.TargetConnector = void 0;
var _shallowequal = require("@react-dnd/shallowequal");
var _wrapConnectorHooks = _interopRequireDefault(require("./wrapConnectorHooks"));
var _isRef = require("../utils/isRef");
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
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; }
var TargetConnector =
/*#__PURE__*/
function () {
function TargetConnector(backend) {
var _this = this;
_classCallCheck(this, TargetConnector);
this.hooks = (0, _wrapConnectorHooks.default)({
dropTarget: function dropTarget(node, options) {
_this.clearDropTarget();
_this.dropTargetOptions = options;
if ((0, _isRef.isRef)(node)) {
_this.dropTargetRef = node;
} else {
_this.dropTargetNode = node;
}
_this.reconnect();
}
});
this.handlerId = null; // The drop target may either be attached via ref or connect function
this.dropTargetRef = null;
this.dropTargetOptionsInternal = null;
this.lastConnectedHandlerId = null;
this.lastConnectedDropTarget = null;
this.lastConnectedDropTargetOptions = null;
this.backend = backend;
}
_createClass(TargetConnector, [{
key: "reconnect",
value: function reconnect() {
// if nothing has changed then don't resubscribe
var didChange = this.didHandlerIdChange() || this.didDropTargetChange() || this.didOptionsChange();
if (didChange) {
this.disconnectDropTarget();
}
var dropTarget = this.dropTarget;
if (!this.handlerId) {
return;
}
if (!dropTarget) {
this.lastConnectedDropTarget = dropTarget;
return;
}
if (didChange) {
this.lastConnectedHandlerId = this.handlerId;
this.lastConnectedDropTarget = dropTarget;
this.lastConnectedDropTargetOptions = this.dropTargetOptions;
this.unsubscribeDropTarget = this.backend.connectDropTarget(this.handlerId, dropTarget, this.dropTargetOptions);
}
}
}, {
key: "receiveHandlerId",
value: function receiveHandlerId(newHandlerId) {
if (newHandlerId === this.handlerId) {
return;
}
this.handlerId = newHandlerId;
this.reconnect();
}
}, {
key: "didHandlerIdChange",
value: function didHandlerIdChange() {
return this.lastConnectedHandlerId !== this.handlerId;
}
}, {
key: "didDropTargetChange",
value: function didDropTargetChange() {
return this.lastConnectedDropTarget !== this.dropTarget;
}
}, {
key: "didOptionsChange",
value: function didOptionsChange() {
return !(0, _shallowequal.shallowEqual)(this.lastConnectedDropTargetOptions, this.dropTargetOptions);
}
}, {
key: "disconnectDropTarget",
value: function disconnectDropTarget() {
if (this.unsubscribeDropTarget) {
this.unsubscribeDropTarget();
this.unsubscribeDropTarget = undefined;
}
}
}, {
key: "clearDropTarget",
value: function clearDropTarget() {
this.dropTargetRef = null;
this.dropTargetNode = null;
}
}, {
key: "connectTarget",
get: function get() {
return this.dropTarget;
}
}, {
key: "dropTargetOptions",
get: function get() {
return this.dropTargetOptionsInternal;
},
set: function set(options) {
this.dropTargetOptionsInternal = options;
}
}, {
key: "dropTarget",
get: function get() {
return this.dropTargetNode || this.dropTargetRef && this.dropTargetRef.current;
}
}]);
return TargetConnector;
}();
exports.TargetConnector = TargetConnector;