wix-style-react
Version:
227 lines (175 loc) • 7.96 kB
JavaScript
;
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
value: true
});
exports["default"] = void 0;
var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck"));
var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass"));
var _assertThisInitialized2 = _interopRequireDefault(require("@babel/runtime/helpers/assertThisInitialized"));
var _inherits2 = _interopRequireDefault(require("@babel/runtime/helpers/inherits"));
var _possibleConstructorReturn2 = _interopRequireDefault(require("@babel/runtime/helpers/possibleConstructorReturn"));
var _getPrototypeOf2 = _interopRequireDefault(require("@babel/runtime/helpers/getPrototypeOf"));
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
var _react = _interopRequireDefault(require("react"));
var _propTypes = _interopRequireDefault(require("prop-types"));
var _reactDnd = require("react-dnd");
var _DragUtils = require("../DragUtils");
var _types = require("../types");
function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = (0, _getPrototypeOf2["default"])(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = (0, _getPrototypeOf2["default"])(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return (0, _possibleConstructorReturn2["default"])(this, result); }; }
function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); return true; } catch (e) { return false; } }
/* eslint-disable new-cap */
var getRelativePositions = function getRelativePositions(child, parent) {
var topPositionOfChild = child.getBoundingClientRect().top - parent.getBoundingClientRect().top;
var bottomPositionOfChild = topPositionOfChild + child.getBoundingClientRect().height;
return {
top: topPositionOfChild,
bottom: bottomPositionOfChild
};
};
var target = {
drop: function drop(props, monitor) {
/** if drop was already done(on child), we skip this drop call */
if (monitor.getDropResult()) {
return;
}
return {
containerId: props.containerId,
groupName: props.groupName,
index: monitor.getItem().index
};
},
canDrop: function canDrop(props) {
return props.droppable;
},
hover: function hover(props, monitor, component) {
if (!component) {
return;
}
/**
in this block we check that user dragging item over container empty pars
and not over other draggable(sortable items)
*/
var _getRelativePositions = getRelativePositions(component.childNode, component.rootNode),
top = _getRelativePositions.top,
bottom = _getRelativePositions.bottom;
var _dragCoordinates = (0, _DragUtils.dragCoordinates)({
monitor: monitor,
component: component
}),
hoverClientY = _dragCoordinates.hoverClientY;
var isHoverInBannedArea = hoverClientY > top && hoverClientY < bottom || !monitor.isOver();
if (isHoverInBannedArea) {
return;
}
/** end of block */
/**
in this block we check, that user dragging item(from some container with group)
over container with same group
*/
var monitorItem = monitor.getItem();
var isSameGroup = props.groupName && monitorItem.groupName && props.groupName === monitorItem.groupName;
if (!props.droppable || !isSameGroup || !component) {
return;
}
/** end of block */
var dragIndex = monitorItem.index; // position of item that we drag in items array
var hoverIndex = hoverClientY < top ? 0 : props.total; // if user drag item above other items - add to the top, otherwise - add to the bottom
var isSameContainer = props.containerId === monitorItem.realTime.containerId; // check do we hover over same container(from which item is)
/** in case that we hover over itself - do nothing */
if (!component || hoverIndex === dragIndex && isSameContainer) {
return;
}
/**
if item is from same group but different container, that's mean that we move item
from one container to another, and we need to move out item from previous container
*/
if (isSameGroup && !isSameContainer) {
monitorItem.realTime.onMoveOut(monitorItem.id);
}
/**
as react-dnd store same snapshot in monitor(so containerId of item will be same, even if we moved it with hover to another container)
after any hovers, we need to save also real position of monitor, with real links to current container
*/
monitorItem.realTime.onMoveOut = props.onMoveOut;
monitorItem.realTime.onDrop = props.onDrop;
monitorItem.realTime.containerId = props.containerId;
/**
call callback, to ask parent to do some action, for example swap items or add new one,
we send original position of item and new one, also id of item and original item state(
it required for case, when we moving item from 1 container to another
)
*/
props.onHover({
removedIndex: dragIndex,
addedIndex: hoverIndex,
id: monitorItem.id,
item: monitorItem.originalItem
});
/** set new index for item */
monitor.getItem().index = hoverIndex;
}
};
var Container = /*#__PURE__*/function (_React$PureComponent) {
(0, _inherits2["default"])(Container, _React$PureComponent);
var _super = _createSuper(Container);
function Container() {
var _this;
(0, _classCallCheck2["default"])(this, Container);
for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
args[_key] = arguments[_key];
}
_this = _super.call.apply(_super, [this].concat(args));
(0, _defineProperty2["default"])((0, _assertThisInitialized2["default"])(_this), "setRootRef", function (node) {
return _this.rootNode = node;
});
(0, _defineProperty2["default"])((0, _assertThisInitialized2["default"])(_this), "setChildRef", function (node) {
return _this.childNode = node;
});
return _this;
}
(0, _createClass2["default"])(Container, [{
key: "render",
value: function render() {
var _this$props = this.props,
dataHook = _this$props.dataHook,
className = _this$props.className,
connectDropTarget = _this$props.connectDropTarget,
children = _this$props.children,
style = _this$props.style;
if (!connectDropTarget) {
return null;
}
return connectDropTarget( /*#__PURE__*/_react["default"].createElement("div", {
"data-hook": dataHook,
className: className,
ref: this.setRootRef,
style: style
}, /*#__PURE__*/_react["default"].cloneElement(children, {
ref: this.setChildRef
})));
}
}]);
return Container;
}(_react["default"].PureComponent);
Container.propTypes = {
dataHook: _propTypes["default"].string,
children: _propTypes["default"].element.isRequired,
className: _propTypes["default"].string,
containerId: _propTypes["default"].string,
groupName: _propTypes["default"].string,
index: _propTypes["default"].number,
onMoveOut: _propTypes["default"].func,
onDrop: _propTypes["default"].func,
onHover: _propTypes["default"].func,
droppable: _propTypes["default"].bool
};
Container.defaultProps = {
droppable: true
};
var _default = (0, _reactDnd.DropTarget)(_types.ItemTypes.DRAGGABLE, target, function (connect) {
return {
connectDropTarget: connect.dropTarget()
};
})(Container);
exports["default"] = _default;