wix-style-react
Version:
wix-style-react
266 lines (234 loc) • 9.35 kB
JavaScript
var _createClass = 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); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
var _dec, _class;
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }
import React from 'react';
import ReactDOM from 'react-dom';
import PropTypes from 'prop-types';
import { DragSource } from 'react-dnd';
import { getEmptyImage } from 'react-dnd-html5-backend';
import noop from 'lodash/noop';
import DragLayer from './DragLayer';
import { ItemTypes } from '../types';
/* eslint-disable new-cap */
var source = {
beginDrag: function beginDrag(_ref) {
var id = _ref.id,
index = _ref.index,
containerId = _ref.containerId,
groupName = _ref.groupName,
item = _ref.item,
onMoveOut = _ref.onMoveOut,
onDragStart = _ref.onDragStart;
if (onDragStart) {
onDragStart({
id: id,
index: index,
containerId: containerId,
groupName: groupName,
item: item
});
}
/** we setup monitor.getItem() snapshot, so we will be always able to get info about item that we drag */
return {
id: id,
index: index,
containerId: containerId,
groupName: groupName,
originalIndex: index, // as index is mutable during d&d, we need another storage for original index
originalItem: item,
onMoveOut: onMoveOut,
realTime: {
onMoveOut: onMoveOut,
containerId: containerId
}
};
},
endDrag: function endDrag(_ref2, monitor) {
var id = _ref2.id,
index = _ref2.index,
containerId = _ref2.containerId,
groupName = _ref2.groupName,
item = _ref2.item,
onDrop = _ref2.onDrop,
onDragEnd = _ref2.onDragEnd;
/** if drop was called, on drop target and drag is end, then we notify parent about this */
if (onDragEnd) {
onDragEnd({
id: id,
index: index,
containerId: containerId,
groupName: groupName,
item: item
});
}
if (monitor.getDropResult()) {
var isSameGroup = groupName && monitor.getItem().groupName && groupName === monitor.getDropResult().groupName;
var isSameContainer = containerId === monitor.getDropResult().containerId;
if (isSameGroup || isSameContainer) {
onDrop({
payload: monitor.getItem().originalItem, // original item
removedIndex: monitor.getItem().originalIndex, // original item index
addedIndex: monitor.getItem().index, // new item index
addedToContainerId: monitor.getDropResult().containerId, // new container for item
removedFromContainerId: containerId // original item container
});
}
}
},
isDragging: function isDragging(_ref3, monitor) {
var id = _ref3.id,
containerId = _ref3.containerId,
groupName = _ref3.groupName;
var item = monitor.getItem();
var isSameGroup = groupName && item.groupName && groupName === item.groupName;
var isSameContainer = containerId === item.containerId;
return (isSameGroup || isSameContainer) && item.id === id;
}
};
var collect = function collect(connect, monitor) {
return {
connectDragSource: connect.dragSource(),
connectDragPreview: connect.dragPreview(),
isDragging: monitor.isDragging()
};
};
var DraggableSource = (_dec = DragSource(ItemTypes.DRAGGABLE, source, collect), _dec(_class = function (_React$Component) {
_inherits(DraggableSource, _React$Component);
function DraggableSource() {
var _ref4;
var _temp, _this, _ret;
_classCallCheck(this, DraggableSource);
for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) {
args[_key] = arguments[_key];
}
return _ret = (_temp = (_this = _possibleConstructorReturn(this, (_ref4 = DraggableSource.__proto__ || Object.getPrototypeOf(DraggableSource)).call.apply(_ref4, [this].concat(args))), _this), _this.state = {
offsetOfHandle: { x: 0, y: 0 }
}, _this._renderPreview = function (_ref5) {
var previewStyles = _ref5.previewStyles;
var _this$props = _this.props,
renderItem = _this$props.renderItem,
id = _this$props.id,
item = _this$props.item;
return renderItem({
id: id,
item: item,
previewStyles: previewStyles,
isPreview: true,
connectHandle: noop
});
}, _temp), _possibleConstructorReturn(_this, _ret);
}
_createClass(DraggableSource, [{
key: 'componentDidMount',
value: function componentDidMount() {
if (this.props.connectDragPreview) {
this.props.connectDragPreview(getEmptyImage(), {
captureDraggingState: true
});
}
this.updateDiff();
}
}, {
key: 'componentDidUpdate',
value: function componentDidUpdate(prevProps) {
if (prevProps.id !== this.props.id || prevProps.containerId !== this.props.containerId) {
this.updateDiff();
}
}
}, {
key: 'updateDiff',
value: function updateDiff() {
/* in case if we have handle, the drag will start in wrong position and we need to fix this */
if (this.props.withHandle && this.handleNode) {
this.setState({
offsetOfHandle: {
x: this.handleNode.getBoundingClientRect().x - this.rootNode.getBoundingClientRect().x,
y: this.handleNode.getBoundingClientRect().y - this.rootNode.getBoundingClientRect().y
}
});
}
}
}, {
key: '_renderDraggableItem',
value: function _renderDraggableItem() {
var _this2 = this;
var _props = this.props,
isDragging = _props.isDragging,
connectDragSource = _props.connectDragSource,
withHandle = _props.withHandle,
renderItem = _props.renderItem,
id = _props.id,
item = _props.item;
if (withHandle) {
return renderItem({
id: id,
item: item,
isPlaceholder: isDragging,
connectHandle: function connectHandle(handle) {
var handleWithRef = React.cloneElement(handle, {
ref: function ref(node) {
return _this2.handleNode = ReactDOM.findDOMNode(node);
}
});
return connectDragSource(handleWithRef);
}
});
}
return connectDragSource(renderItem({
id: id,
item: item,
isPlaceholder: isDragging,
connectHandle: noop
}));
}
}, {
key: '_renderPreviewItem',
value: function _renderPreviewItem() {
var _props2 = this.props,
id = _props2.id,
usePortal = _props2.usePortal;
return React.createElement(DragLayer, {
offsetOfHandle: this.state.offsetOfHandle,
usePortal: usePortal,
renderPreview: this._renderPreview,
id: id,
draggedType: ItemTypes.DRAGGABLE
});
}
}, {
key: 'render',
value: function render() {
var _this3 = this;
var connectDragSource = this.props.connectDragSource;
return connectDragSource ? React.createElement(
'div',
{ ref: function ref(node) {
return _this3.rootNode = node;
} },
this._renderDraggableItem(),
this._renderPreviewItem()
) : null;
}
}]);
return DraggableSource;
}(React.Component)) || _class);
export { DraggableSource as default };
DraggableSource.propTypes = {
isDragging: PropTypes.bool, // from react-dnd
connectDragSource: PropTypes.func, // from react-dnd
connectDragPreview: PropTypes.func, // from react-dnd
usePortal: PropTypes.bool,
groupName: PropTypes.string,
containerId: PropTypes.oneOfType([PropTypes.number, PropTypes.string]),
renderItem: PropTypes.func,
index: PropTypes.number,
id: PropTypes.oneOfType([PropTypes.number, PropTypes.string]),
item: PropTypes.object,
withHandle: PropTypes.bool,
onDrop: PropTypes.func,
onMoveOut: PropTypes.func,
onDragStart: PropTypes.func,
onDragEnd: PropTypes.func
};