Files
coopgo/node_modules/react-dnd/dist/esm/internals/SourceConnector.js
sgauthier 6e64e138e2
All checks were successful
Publish To Prod / deploy_and_publish (push) Successful in 35s
planning
2024-10-14 09:15:30 +02:00

241 lines
7.5 KiB
JavaScript

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; }
import { wrapConnectorHooks } from './wrapConnectorHooks';
import { isRef } from './isRef';
import { shallowEqual } from '@react-dnd/shallowequal';
export var SourceConnector = /*#__PURE__*/function () {
// The drop target may either be attached via ref or connect function
// The drag preview may either be attached via ref or connect function
function SourceConnector(backend) {
var _this = this;
_classCallCheck(this, SourceConnector);
_defineProperty(this, "hooks", wrapConnectorHooks({
dragSource: function dragSource(node, options) {
_this.clearDragSource();
_this.dragSourceOptions = options || null;
if (isRef(node)) {
_this.dragSourceRef = node;
} else {
_this.dragSourceNode = node;
}
_this.reconnectDragSource();
},
dragPreview: function dragPreview(node, options) {
_this.clearDragPreview();
_this.dragPreviewOptions = options || null;
if (isRef(node)) {
_this.dragPreviewRef = node;
} else {
_this.dragPreviewNode = node;
}
_this.reconnectDragPreview();
}
}));
_defineProperty(this, "handlerId", null);
_defineProperty(this, "dragSourceRef", null);
_defineProperty(this, "dragSourceNode", void 0);
_defineProperty(this, "dragSourceOptionsInternal", null);
_defineProperty(this, "dragSourceUnsubscribe", void 0);
_defineProperty(this, "dragPreviewRef", null);
_defineProperty(this, "dragPreviewNode", void 0);
_defineProperty(this, "dragPreviewOptionsInternal", null);
_defineProperty(this, "dragPreviewUnsubscribe", void 0);
_defineProperty(this, "lastConnectedHandlerId", null);
_defineProperty(this, "lastConnectedDragSource", null);
_defineProperty(this, "lastConnectedDragSourceOptions", null);
_defineProperty(this, "lastConnectedDragPreview", null);
_defineProperty(this, "lastConnectedDragPreviewOptions", null);
_defineProperty(this, "backend", void 0);
this.backend = backend;
}
_createClass(SourceConnector, [{
key: "receiveHandlerId",
value: function receiveHandlerId(newHandlerId) {
if (this.handlerId === newHandlerId) {
return;
}
this.handlerId = newHandlerId;
this.reconnect();
}
}, {
key: "connectTarget",
get: function get() {
return this.dragSource;
}
}, {
key: "dragSourceOptions",
get: function get() {
return this.dragSourceOptionsInternal;
},
set: function set(options) {
this.dragSourceOptionsInternal = options;
}
}, {
key: "dragPreviewOptions",
get: function get() {
return this.dragPreviewOptionsInternal;
},
set: function set(options) {
this.dragPreviewOptionsInternal = options;
}
}, {
key: "reconnect",
value: function reconnect() {
this.reconnectDragSource();
this.reconnectDragPreview();
}
}, {
key: "reconnectDragSource",
value: function reconnectDragSource() {
var dragSource = this.dragSource; // if nothing has changed then don't resubscribe
var didChange = this.didHandlerIdChange() || this.didConnectedDragSourceChange() || this.didDragSourceOptionsChange();
if (didChange) {
this.disconnectDragSource();
}
if (!this.handlerId) {
return;
}
if (!dragSource) {
this.lastConnectedDragSource = dragSource;
return;
}
if (didChange) {
this.lastConnectedHandlerId = this.handlerId;
this.lastConnectedDragSource = dragSource;
this.lastConnectedDragSourceOptions = this.dragSourceOptions;
this.dragSourceUnsubscribe = this.backend.connectDragSource(this.handlerId, dragSource, this.dragSourceOptions);
}
}
}, {
key: "reconnectDragPreview",
value: function reconnectDragPreview() {
var dragPreview = this.dragPreview; // if nothing has changed then don't resubscribe
var didChange = this.didHandlerIdChange() || this.didConnectedDragPreviewChange() || this.didDragPreviewOptionsChange();
if (didChange) {
this.disconnectDragPreview();
}
if (!this.handlerId) {
return;
}
if (!dragPreview) {
this.lastConnectedDragPreview = dragPreview;
return;
}
if (didChange) {
this.lastConnectedHandlerId = this.handlerId;
this.lastConnectedDragPreview = dragPreview;
this.lastConnectedDragPreviewOptions = this.dragPreviewOptions;
this.dragPreviewUnsubscribe = this.backend.connectDragPreview(this.handlerId, dragPreview, this.dragPreviewOptions);
}
}
}, {
key: "didHandlerIdChange",
value: function didHandlerIdChange() {
return this.lastConnectedHandlerId !== this.handlerId;
}
}, {
key: "didConnectedDragSourceChange",
value: function didConnectedDragSourceChange() {
return this.lastConnectedDragSource !== this.dragSource;
}
}, {
key: "didConnectedDragPreviewChange",
value: function didConnectedDragPreviewChange() {
return this.lastConnectedDragPreview !== this.dragPreview;
}
}, {
key: "didDragSourceOptionsChange",
value: function didDragSourceOptionsChange() {
return !shallowEqual(this.lastConnectedDragSourceOptions, this.dragSourceOptions);
}
}, {
key: "didDragPreviewOptionsChange",
value: function didDragPreviewOptionsChange() {
return !shallowEqual(this.lastConnectedDragPreviewOptions, this.dragPreviewOptions);
}
}, {
key: "disconnectDragSource",
value: function disconnectDragSource() {
if (this.dragSourceUnsubscribe) {
this.dragSourceUnsubscribe();
this.dragSourceUnsubscribe = undefined;
}
}
}, {
key: "disconnectDragPreview",
value: function disconnectDragPreview() {
if (this.dragPreviewUnsubscribe) {
this.dragPreviewUnsubscribe();
this.dragPreviewUnsubscribe = undefined;
this.dragPreviewNode = null;
this.dragPreviewRef = null;
}
}
}, {
key: "dragSource",
get: function get() {
return this.dragSourceNode || this.dragSourceRef && this.dragSourceRef.current;
}
}, {
key: "dragPreview",
get: function get() {
return this.dragPreviewNode || this.dragPreviewRef && this.dragPreviewRef.current;
}
}, {
key: "clearDragSource",
value: function clearDragSource() {
this.dragSourceNode = null;
this.dragSourceRef = null;
}
}, {
key: "clearDragPreview",
value: function clearDragPreview() {
this.dragPreviewNode = null;
this.dragPreviewRef = null;
}
}]);
return SourceConnector;
}();