react-beautiful-dnd
Version:
Beautiful and accessible drag and drop for lists with React
1,700 lines (1,444 loc) • 243 kB
JavaScript
'use strict';
Object.defineProperty(exports, '__esModule', { value: true });
function _interopDefault (ex) { return (ex && (typeof ex === 'object') && 'default' in ex) ? ex['default'] : ex; }
var React = require('react');
var React__default = _interopDefault(React);
var _inheritsLoose = _interopDefault(require('@babel/runtime/helpers/inheritsLoose'));
var _extends = _interopDefault(require('@babel/runtime/helpers/extends'));
var redux = require('redux');
var reactRedux = require('react-redux');
var useMemoOne = require('use-memo-one');
var cssBoxModel = require('css-box-model');
var memoizeOne = _interopDefault(require('memoize-one'));
var rafSchd = _interopDefault(require('raf-schd'));
var ReactDOM = _interopDefault(require('react-dom'));
var isProduction = process.env.NODE_ENV === 'production';
var spacesAndTabs = /[ \t]{2,}/g;
var lineStartWithSpaces = /^[ \t]*/gm;
var clean = function clean(value) {
return value.replace(spacesAndTabs, ' ').replace(lineStartWithSpaces, '').trim();
};
var getDevMessage = function getDevMessage(message) {
return clean("\n %creact-beautiful-dnd\n\n %c" + clean(message) + "\n\n %c\uD83D\uDC77\u200D This is a development only message. It will be removed in production builds.\n");
};
var getFormattedMessage = function getFormattedMessage(message) {
return [getDevMessage(message), 'color: #00C584; font-size: 1.2em; font-weight: bold;', 'line-height: 1.5', 'color: #723874;'];
};
var isDisabledFlag = '__react-beautiful-dnd-disable-dev-warnings';
function log(type, message) {
var _console;
if (isProduction) {
return;
}
if (typeof window !== 'undefined' && window[isDisabledFlag]) {
return;
}
(_console = console)[type].apply(_console, getFormattedMessage(message));
}
var warning = log.bind(null, 'warn');
var error = log.bind(null, 'error');
function noop() {}
function getOptions(shared, fromBinding) {
return _extends({}, shared, {}, fromBinding);
}
function bindEvents(el, bindings, sharedOptions) {
var unbindings = bindings.map(function (binding) {
var options = getOptions(sharedOptions, binding.options);
el.addEventListener(binding.eventName, binding.fn, options);
return function unbind() {
el.removeEventListener(binding.eventName, binding.fn, options);
};
});
return function unbindAll() {
unbindings.forEach(function (unbind) {
unbind();
});
};
}
var isProduction$1 = process.env.NODE_ENV === 'production';
var prefix = 'Invariant failed';
function RbdInvariant(message) {
this.message = message;
}
RbdInvariant.prototype.toString = function toString() {
return this.message;
};
function invariant(condition, message) {
if (condition) {
return;
}
if (isProduction$1) {
throw new RbdInvariant(prefix);
} else {
throw new RbdInvariant(prefix + ": " + (message || ''));
}
}
var ErrorBoundary = function (_React$Component) {
_inheritsLoose(ErrorBoundary, _React$Component);
function ErrorBoundary() {
var _this;
for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
args[_key] = arguments[_key];
}
_this = _React$Component.call.apply(_React$Component, [this].concat(args)) || this;
_this.callbacks = null;
_this.unbind = noop;
_this.onWindowError = function (event) {
var callbacks = _this.getCallbacks();
if (callbacks.isDragging()) {
callbacks.tryAbort();
process.env.NODE_ENV !== "production" ? warning("\n An error was caught by our window 'error' event listener while a drag was occurring.\n The active drag has been aborted.\n ") : void 0;
}
var err = event.error;
if (err instanceof RbdInvariant) {
event.preventDefault();
if (process.env.NODE_ENV !== 'production') {
error(err.message);
}
}
};
_this.getCallbacks = function () {
if (!_this.callbacks) {
throw new Error('Unable to find AppCallbacks in <ErrorBoundary/>');
}
return _this.callbacks;
};
_this.setCallbacks = function (callbacks) {
_this.callbacks = callbacks;
};
return _this;
}
var _proto = ErrorBoundary.prototype;
_proto.componentDidMount = function componentDidMount() {
this.unbind = bindEvents(window, [{
eventName: 'error',
fn: this.onWindowError
}]);
};
_proto.componentDidCatch = function componentDidCatch(err) {
if (err instanceof RbdInvariant) {
if (process.env.NODE_ENV !== 'production') {
error(err.message);
}
this.setState({});
return;
}
throw err;
};
_proto.componentWillUnmount = function componentWillUnmount() {
this.unbind();
};
_proto.render = function render() {
return this.props.children(this.setCallbacks);
};
return ErrorBoundary;
}(React__default.Component);
var dragHandleUsageInstructions = "\n Press space bar to start a drag.\n When dragging you can use the arrow keys to move the item around and escape to cancel.\n Some screen readers may require you to be in focus mode or to use your pass through key\n";
var position = function position(index) {
return index + 1;
};
var onDragStart = function onDragStart(start) {
return "\n You have lifted an item in position " + position(start.source.index) + "\n";
};
var withLocation = function withLocation(source, destination) {
var isInHomeList = source.droppableId === destination.droppableId;
var startPosition = position(source.index);
var endPosition = position(destination.index);
if (isInHomeList) {
return "\n You have moved the item from position " + startPosition + "\n to position " + endPosition + "\n ";
}
return "\n You have moved the item from position " + startPosition + "\n in list " + source.droppableId + "\n to list " + destination.droppableId + "\n in position " + endPosition + "\n ";
};
var withCombine = function withCombine(id, source, combine) {
var inHomeList = source.droppableId === combine.droppableId;
if (inHomeList) {
return "\n The item " + id + "\n has been combined with " + combine.draggableId;
}
return "\n The item " + id + "\n in list " + source.droppableId + "\n has been combined with " + combine.draggableId + "\n in list " + combine.droppableId + "\n ";
};
var onDragUpdate = function onDragUpdate(update) {
var location = update.destination;
if (location) {
return withLocation(update.source, location);
}
var combine = update.combine;
if (combine) {
return withCombine(update.draggableId, update.source, combine);
}
return 'You are over an area that cannot be dropped on';
};
var returnedToStart = function returnedToStart(source) {
return "\n The item has returned to its starting position\n of " + position(source.index) + "\n";
};
var onDragEnd = function onDragEnd(result) {
if (result.reason === 'CANCEL') {
return "\n Movement cancelled.\n " + returnedToStart(result.source) + "\n ";
}
var location = result.destination;
var combine = result.combine;
if (location) {
return "\n You have dropped the item.\n " + withLocation(result.source, location) + "\n ";
}
if (combine) {
return "\n You have dropped the item.\n " + withCombine(result.draggableId, result.source, combine) + "\n ";
}
return "\n The item has been dropped while not over a drop area.\n " + returnedToStart(result.source) + "\n ";
};
var preset = {
dragHandleUsageInstructions: dragHandleUsageInstructions,
onDragStart: onDragStart,
onDragUpdate: onDragUpdate,
onDragEnd: onDragEnd
};
var origin = {
x: 0,
y: 0
};
var add = function add(point1, point2) {
return {
x: point1.x + point2.x,
y: point1.y + point2.y
};
};
var subtract = function subtract(point1, point2) {
return {
x: point1.x - point2.x,
y: point1.y - point2.y
};
};
var isEqual = function isEqual(point1, point2) {
return point1.x === point2.x && point1.y === point2.y;
};
var negate = function negate(point) {
return {
x: point.x !== 0 ? -point.x : 0,
y: point.y !== 0 ? -point.y : 0
};
};
var patch = function patch(line, value, otherValue) {
var _ref;
if (otherValue === void 0) {
otherValue = 0;
}
return _ref = {}, _ref[line] = value, _ref[line === 'x' ? 'y' : 'x'] = otherValue, _ref;
};
var distance = function distance(point1, point2) {
return Math.sqrt(Math.pow(point2.x - point1.x, 2) + Math.pow(point2.y - point1.y, 2));
};
var closest = function closest(target, points) {
return Math.min.apply(Math, points.map(function (point) {
return distance(target, point);
}));
};
var apply = function apply(fn) {
return function (point) {
return {
x: fn(point.x),
y: fn(point.y)
};
};
};
var executeClip = (function (frame, subject) {
var result = cssBoxModel.getRect({
top: Math.max(subject.top, frame.top),
right: Math.min(subject.right, frame.right),
bottom: Math.min(subject.bottom, frame.bottom),
left: Math.max(subject.left, frame.left)
});
if (result.width <= 0 || result.height <= 0) {
return null;
}
return result;
});
var offsetByPosition = function offsetByPosition(spacing, point) {
return {
top: spacing.top + point.y,
left: spacing.left + point.x,
bottom: spacing.bottom + point.y,
right: spacing.right + point.x
};
};
var getCorners = function getCorners(spacing) {
return [{
x: spacing.left,
y: spacing.top
}, {
x: spacing.right,
y: spacing.top
}, {
x: spacing.left,
y: spacing.bottom
}, {
x: spacing.right,
y: spacing.bottom
}];
};
var noSpacing = {
top: 0,
right: 0,
bottom: 0,
left: 0
};
var scroll = function scroll(target, frame) {
if (!frame) {
return target;
}
return offsetByPosition(target, frame.scroll.diff.displacement);
};
var increase = function increase(target, axis, withPlaceholder) {
if (withPlaceholder && withPlaceholder.increasedBy) {
var _extends2;
return _extends({}, target, (_extends2 = {}, _extends2[axis.end] = target[axis.end] + withPlaceholder.increasedBy[axis.line], _extends2));
}
return target;
};
var clip = function clip(target, frame) {
if (frame && frame.shouldClipSubject) {
return executeClip(frame.pageMarginBox, target);
}
return cssBoxModel.getRect(target);
};
var getSubject = (function (_ref) {
var page = _ref.page,
withPlaceholder = _ref.withPlaceholder,
axis = _ref.axis,
frame = _ref.frame;
var scrolled = scroll(page.marginBox, frame);
var increased = increase(scrolled, axis, withPlaceholder);
var clipped = clip(increased, frame);
return {
page: page,
withPlaceholder: withPlaceholder,
active: clipped
};
});
var scrollDroppable = (function (droppable, newScroll) {
!droppable.frame ? process.env.NODE_ENV !== "production" ? invariant(false) : invariant(false) : void 0;
var scrollable = droppable.frame;
var scrollDiff = subtract(newScroll, scrollable.scroll.initial);
var scrollDisplacement = negate(scrollDiff);
var frame = _extends({}, scrollable, {
scroll: {
initial: scrollable.scroll.initial,
current: newScroll,
diff: {
value: scrollDiff,
displacement: scrollDisplacement
},
max: scrollable.scroll.max
}
});
var subject = getSubject({
page: droppable.subject.page,
withPlaceholder: droppable.subject.withPlaceholder,
axis: droppable.axis,
frame: frame
});
var result = _extends({}, droppable, {
frame: frame,
subject: subject
});
return result;
});
function isInteger(value) {
if (Number.isInteger) {
return Number.isInteger(value);
}
return typeof value === 'number' && isFinite(value) && Math.floor(value) === value;
}
function values(map) {
if (Object.values) {
return Object.values(map);
}
return Object.keys(map).map(function (key) {
return map[key];
});
}
function findIndex(list, predicate) {
if (list.findIndex) {
return list.findIndex(predicate);
}
for (var i = 0; i < list.length; i++) {
if (predicate(list[i])) {
return i;
}
}
return -1;
}
function find(list, predicate) {
if (list.find) {
return list.find(predicate);
}
var index = findIndex(list, predicate);
if (index !== -1) {
return list[index];
}
return undefined;
}
function toArray(list) {
return Array.prototype.slice.call(list);
}
var toDroppableMap = memoizeOne(function (droppables) {
return droppables.reduce(function (previous, current) {
previous[current.descriptor.id] = current;
return previous;
}, {});
});
var toDraggableMap = memoizeOne(function (draggables) {
return draggables.reduce(function (previous, current) {
previous[current.descriptor.id] = current;
return previous;
}, {});
});
var toDroppableList = memoizeOne(function (droppables) {
return values(droppables);
});
var toDraggableList = memoizeOne(function (draggables) {
return values(draggables);
});
var getDraggablesInsideDroppable = memoizeOne(function (droppableId, draggables) {
var result = toDraggableList(draggables).filter(function (draggable) {
return droppableId === draggable.descriptor.droppableId;
}).sort(function (a, b) {
return a.descriptor.index - b.descriptor.index;
});
return result;
});
function tryGetDestination(impact) {
if (impact.at && impact.at.type === 'REORDER') {
return impact.at.destination;
}
return null;
}
function tryGetCombine(impact) {
if (impact.at && impact.at.type === 'COMBINE') {
return impact.at.combine;
}
return null;
}
var removeDraggableFromList = memoizeOne(function (remove, list) {
return list.filter(function (item) {
return item.descriptor.id !== remove.descriptor.id;
});
});
var moveToNextCombine = (function (_ref) {
var isMovingForward = _ref.isMovingForward,
draggable = _ref.draggable,
destination = _ref.destination,
insideDestination = _ref.insideDestination,
previousImpact = _ref.previousImpact;
if (!destination.isCombineEnabled) {
return null;
}
var location = tryGetDestination(previousImpact);
if (!location) {
return null;
}
function getImpact(target) {
var at = {
type: 'COMBINE',
combine: {
draggableId: target,
droppableId: destination.descriptor.id
}
};
return _extends({}, previousImpact, {
at: at
});
}
var all = previousImpact.displaced.all;
var closestId = all.length ? all[0] : null;
if (isMovingForward) {
return closestId ? getImpact(closestId) : null;
}
var withoutDraggable = removeDraggableFromList(draggable, insideDestination);
if (!closestId) {
if (!withoutDraggable.length) {
return null;
}
var last = withoutDraggable[withoutDraggable.length - 1];
return getImpact(last.descriptor.id);
}
var indexOfClosest = findIndex(withoutDraggable, function (d) {
return d.descriptor.id === closestId;
});
!(indexOfClosest !== -1) ? process.env.NODE_ENV !== "production" ? invariant(false, 'Could not find displaced item in set') : invariant(false) : void 0;
var proposedIndex = indexOfClosest - 1;
if (proposedIndex < 0) {
return null;
}
var before = withoutDraggable[proposedIndex];
return getImpact(before.descriptor.id);
});
var isHomeOf = (function (draggable, destination) {
return draggable.descriptor.droppableId === destination.descriptor.id;
});
var noDisplacedBy = {
point: origin,
value: 0
};
var emptyGroups = {
invisible: {},
visible: {},
all: []
};
var noImpact = {
displaced: emptyGroups,
displacedBy: noDisplacedBy,
at: null
};
var isWithin = (function (lowerBound, upperBound) {
return function (value) {
return lowerBound <= value && value <= upperBound;
};
});
var isPartiallyVisibleThroughFrame = (function (frame) {
var isWithinVertical = isWithin(frame.top, frame.bottom);
var isWithinHorizontal = isWithin(frame.left, frame.right);
return function (subject) {
var isContained = isWithinVertical(subject.top) && isWithinVertical(subject.bottom) && isWithinHorizontal(subject.left) && isWithinHorizontal(subject.right);
if (isContained) {
return true;
}
var isPartiallyVisibleVertically = isWithinVertical(subject.top) || isWithinVertical(subject.bottom);
var isPartiallyVisibleHorizontally = isWithinHorizontal(subject.left) || isWithinHorizontal(subject.right);
var isPartiallyContained = isPartiallyVisibleVertically && isPartiallyVisibleHorizontally;
if (isPartiallyContained) {
return true;
}
var isBiggerVertically = subject.top < frame.top && subject.bottom > frame.bottom;
var isBiggerHorizontally = subject.left < frame.left && subject.right > frame.right;
var isTargetBiggerThanFrame = isBiggerVertically && isBiggerHorizontally;
if (isTargetBiggerThanFrame) {
return true;
}
var isTargetBiggerOnOneAxis = isBiggerVertically && isPartiallyVisibleHorizontally || isBiggerHorizontally && isPartiallyVisibleVertically;
return isTargetBiggerOnOneAxis;
};
});
var isTotallyVisibleThroughFrame = (function (frame) {
var isWithinVertical = isWithin(frame.top, frame.bottom);
var isWithinHorizontal = isWithin(frame.left, frame.right);
return function (subject) {
var isContained = isWithinVertical(subject.top) && isWithinVertical(subject.bottom) && isWithinHorizontal(subject.left) && isWithinHorizontal(subject.right);
return isContained;
};
});
var vertical = {
direction: 'vertical',
line: 'y',
crossAxisLine: 'x',
start: 'top',
end: 'bottom',
size: 'height',
crossAxisStart: 'left',
crossAxisEnd: 'right',
crossAxisSize: 'width'
};
var horizontal = {
direction: 'horizontal',
line: 'x',
crossAxisLine: 'y',
start: 'left',
end: 'right',
size: 'width',
crossAxisStart: 'top',
crossAxisEnd: 'bottom',
crossAxisSize: 'height'
};
var isTotallyVisibleThroughFrameOnAxis = (function (axis) {
return function (frame) {
var isWithinVertical = isWithin(frame.top, frame.bottom);
var isWithinHorizontal = isWithin(frame.left, frame.right);
return function (subject) {
if (axis === vertical) {
return isWithinVertical(subject.top) && isWithinVertical(subject.bottom);
}
return isWithinHorizontal(subject.left) && isWithinHorizontal(subject.right);
};
};
});
var getDroppableDisplaced = function getDroppableDisplaced(target, destination) {
var displacement = destination.frame ? destination.frame.scroll.diff.displacement : origin;
return offsetByPosition(target, displacement);
};
var isVisibleInDroppable = function isVisibleInDroppable(target, destination, isVisibleThroughFrameFn) {
if (!destination.subject.active) {
return false;
}
return isVisibleThroughFrameFn(destination.subject.active)(target);
};
var isVisibleInViewport = function isVisibleInViewport(target, viewport, isVisibleThroughFrameFn) {
return isVisibleThroughFrameFn(viewport)(target);
};
var isVisible = function isVisible(_ref) {
var toBeDisplaced = _ref.target,
destination = _ref.destination,
viewport = _ref.viewport,
withDroppableDisplacement = _ref.withDroppableDisplacement,
isVisibleThroughFrameFn = _ref.isVisibleThroughFrameFn;
var displacedTarget = withDroppableDisplacement ? getDroppableDisplaced(toBeDisplaced, destination) : toBeDisplaced;
return isVisibleInDroppable(displacedTarget, destination, isVisibleThroughFrameFn) && isVisibleInViewport(displacedTarget, viewport, isVisibleThroughFrameFn);
};
var isPartiallyVisible = function isPartiallyVisible(args) {
return isVisible(_extends({}, args, {
isVisibleThroughFrameFn: isPartiallyVisibleThroughFrame
}));
};
var isTotallyVisible = function isTotallyVisible(args) {
return isVisible(_extends({}, args, {
isVisibleThroughFrameFn: isTotallyVisibleThroughFrame
}));
};
var isTotallyVisibleOnAxis = function isTotallyVisibleOnAxis(args) {
return isVisible(_extends({}, args, {
isVisibleThroughFrameFn: isTotallyVisibleThroughFrameOnAxis(args.destination.axis)
}));
};
var getShouldAnimate = function getShouldAnimate(id, last, forceShouldAnimate) {
if (typeof forceShouldAnimate === 'boolean') {
return forceShouldAnimate;
}
if (!last) {
return true;
}
var invisible = last.invisible,
visible = last.visible;
if (invisible[id]) {
return false;
}
var previous = visible[id];
return previous ? previous.shouldAnimate : true;
};
function getTarget(draggable, displacedBy) {
var marginBox = draggable.page.marginBox;
var expandBy = {
top: displacedBy.point.y,
right: 0,
bottom: 0,
left: displacedBy.point.x
};
return cssBoxModel.getRect(cssBoxModel.expand(marginBox, expandBy));
}
function getDisplacementGroups(_ref) {
var afterDragging = _ref.afterDragging,
destination = _ref.destination,
displacedBy = _ref.displacedBy,
viewport = _ref.viewport,
forceShouldAnimate = _ref.forceShouldAnimate,
last = _ref.last;
return afterDragging.reduce(function process(groups, draggable) {
var target = getTarget(draggable, displacedBy);
var id = draggable.descriptor.id;
groups.all.push(id);
var isVisible = isPartiallyVisible({
target: target,
destination: destination,
viewport: viewport,
withDroppableDisplacement: true
});
if (!isVisible) {
groups.invisible[draggable.descriptor.id] = true;
return groups;
}
var shouldAnimate = getShouldAnimate(id, last, forceShouldAnimate);
var displacement = {
draggableId: id,
shouldAnimate: shouldAnimate
};
groups.visible[id] = displacement;
return groups;
}, {
all: [],
visible: {},
invisible: {}
});
}
function getIndexOfLastItem(draggables, options) {
if (!draggables.length) {
return 0;
}
var indexOfLastItem = draggables[draggables.length - 1].descriptor.index;
return options.inHomeList ? indexOfLastItem : indexOfLastItem + 1;
}
function goAtEnd(_ref) {
var insideDestination = _ref.insideDestination,
inHomeList = _ref.inHomeList,
displacedBy = _ref.displacedBy,
destination = _ref.destination;
var newIndex = getIndexOfLastItem(insideDestination, {
inHomeList: inHomeList
});
return {
displaced: emptyGroups,
displacedBy: displacedBy,
at: {
type: 'REORDER',
destination: {
droppableId: destination.descriptor.id,
index: newIndex
}
}
};
}
function calculateReorderImpact(_ref2) {
var draggable = _ref2.draggable,
insideDestination = _ref2.insideDestination,
destination = _ref2.destination,
viewport = _ref2.viewport,
displacedBy = _ref2.displacedBy,
last = _ref2.last,
index = _ref2.index,
forceShouldAnimate = _ref2.forceShouldAnimate;
var inHomeList = isHomeOf(draggable, destination);
if (index == null) {
return goAtEnd({
insideDestination: insideDestination,
inHomeList: inHomeList,
displacedBy: displacedBy,
destination: destination
});
}
var match = find(insideDestination, function (item) {
return item.descriptor.index === index;
});
if (!match) {
return goAtEnd({
insideDestination: insideDestination,
inHomeList: inHomeList,
displacedBy: displacedBy,
destination: destination
});
}
var withoutDragging = removeDraggableFromList(draggable, insideDestination);
var sliceFrom = insideDestination.indexOf(match);
var impacted = withoutDragging.slice(sliceFrom);
var displaced = getDisplacementGroups({
afterDragging: impacted,
destination: destination,
displacedBy: displacedBy,
last: last,
viewport: viewport.frame,
forceShouldAnimate: forceShouldAnimate
});
return {
displaced: displaced,
displacedBy: displacedBy,
at: {
type: 'REORDER',
destination: {
droppableId: destination.descriptor.id,
index: index
}
}
};
}
function didStartAfterCritical(draggableId, afterCritical) {
return Boolean(afterCritical.effected[draggableId]);
}
var fromCombine = (function (_ref) {
var isMovingForward = _ref.isMovingForward,
destination = _ref.destination,
draggables = _ref.draggables,
combine = _ref.combine,
afterCritical = _ref.afterCritical;
if (!destination.isCombineEnabled) {
return null;
}
var combineId = combine.draggableId;
var combineWith = draggables[combineId];
var combineWithIndex = combineWith.descriptor.index;
var didCombineWithStartAfterCritical = didStartAfterCritical(combineId, afterCritical);
if (didCombineWithStartAfterCritical) {
if (isMovingForward) {
return combineWithIndex;
}
return combineWithIndex - 1;
}
if (isMovingForward) {
return combineWithIndex + 1;
}
return combineWithIndex;
});
var fromReorder = (function (_ref) {
var isMovingForward = _ref.isMovingForward,
isInHomeList = _ref.isInHomeList,
insideDestination = _ref.insideDestination,
location = _ref.location;
if (!insideDestination.length) {
return null;
}
var currentIndex = location.index;
var proposedIndex = isMovingForward ? currentIndex + 1 : currentIndex - 1;
var firstIndex = insideDestination[0].descriptor.index;
var lastIndex = insideDestination[insideDestination.length - 1].descriptor.index;
var upperBound = isInHomeList ? lastIndex : lastIndex + 1;
if (proposedIndex < firstIndex) {
return null;
}
if (proposedIndex > upperBound) {
return null;
}
return proposedIndex;
});
var moveToNextIndex = (function (_ref) {
var isMovingForward = _ref.isMovingForward,
isInHomeList = _ref.isInHomeList,
draggable = _ref.draggable,
draggables = _ref.draggables,
destination = _ref.destination,
insideDestination = _ref.insideDestination,
previousImpact = _ref.previousImpact,
viewport = _ref.viewport,
afterCritical = _ref.afterCritical;
var wasAt = previousImpact.at;
!wasAt ? process.env.NODE_ENV !== "production" ? invariant(false, 'Cannot move in direction without previous impact location') : invariant(false) : void 0;
if (wasAt.type === 'REORDER') {
var _newIndex = fromReorder({
isMovingForward: isMovingForward,
isInHomeList: isInHomeList,
location: wasAt.destination,
insideDestination: insideDestination
});
if (_newIndex == null) {
return null;
}
return calculateReorderImpact({
draggable: draggable,
insideDestination: insideDestination,
destination: destination,
viewport: viewport,
last: previousImpact.displaced,
displacedBy: previousImpact.displacedBy,
index: _newIndex
});
}
var newIndex = fromCombine({
isMovingForward: isMovingForward,
destination: destination,
displaced: previousImpact.displaced,
draggables: draggables,
combine: wasAt.combine,
afterCritical: afterCritical
});
if (newIndex == null) {
return null;
}
return calculateReorderImpact({
draggable: draggable,
insideDestination: insideDestination,
destination: destination,
viewport: viewport,
last: previousImpact.displaced,
displacedBy: previousImpact.displacedBy,
index: newIndex
});
});
var getCombinedItemDisplacement = (function (_ref) {
var displaced = _ref.displaced,
afterCritical = _ref.afterCritical,
combineWith = _ref.combineWith,
displacedBy = _ref.displacedBy;
var isDisplaced = Boolean(displaced.visible[combineWith] || displaced.invisible[combineWith]);
if (didStartAfterCritical(combineWith, afterCritical)) {
return isDisplaced ? origin : negate(displacedBy.point);
}
return isDisplaced ? displacedBy.point : origin;
});
var whenCombining = (function (_ref) {
var afterCritical = _ref.afterCritical,
impact = _ref.impact,
draggables = _ref.draggables;
var combine = tryGetCombine(impact);
!combine ? process.env.NODE_ENV !== "production" ? invariant(false) : invariant(false) : void 0;
var combineWith = combine.draggableId;
var center = draggables[combineWith].page.borderBox.center;
var displaceBy = getCombinedItemDisplacement({
displaced: impact.displaced,
afterCritical: afterCritical,
combineWith: combineWith,
displacedBy: impact.displacedBy
});
return add(center, displaceBy);
});
var distanceFromStartToBorderBoxCenter = function distanceFromStartToBorderBoxCenter(axis, box) {
return box.margin[axis.start] + box.borderBox[axis.size] / 2;
};
var distanceFromEndToBorderBoxCenter = function distanceFromEndToBorderBoxCenter(axis, box) {
return box.margin[axis.end] + box.borderBox[axis.size] / 2;
};
var getCrossAxisBorderBoxCenter = function getCrossAxisBorderBoxCenter(axis, target, isMoving) {
return target[axis.crossAxisStart] + isMoving.margin[axis.crossAxisStart] + isMoving.borderBox[axis.crossAxisSize] / 2;
};
var goAfter = function goAfter(_ref) {
var axis = _ref.axis,
moveRelativeTo = _ref.moveRelativeTo,
isMoving = _ref.isMoving;
return patch(axis.line, moveRelativeTo.marginBox[axis.end] + distanceFromStartToBorderBoxCenter(axis, isMoving), getCrossAxisBorderBoxCenter(axis, moveRelativeTo.marginBox, isMoving));
};
var goBefore = function goBefore(_ref2) {
var axis = _ref2.axis,
moveRelativeTo = _ref2.moveRelativeTo,
isMoving = _ref2.isMoving;
return patch(axis.line, moveRelativeTo.marginBox[axis.start] - distanceFromEndToBorderBoxCenter(axis, isMoving), getCrossAxisBorderBoxCenter(axis, moveRelativeTo.marginBox, isMoving));
};
var goIntoStart = function goIntoStart(_ref3) {
var axis = _ref3.axis,
moveInto = _ref3.moveInto,
isMoving = _ref3.isMoving;
return patch(axis.line, moveInto.contentBox[axis.start] + distanceFromStartToBorderBoxCenter(axis, isMoving), getCrossAxisBorderBoxCenter(axis, moveInto.contentBox, isMoving));
};
var whenReordering = (function (_ref) {
var impact = _ref.impact,
draggable = _ref.draggable,
draggables = _ref.draggables,
droppable = _ref.droppable,
afterCritical = _ref.afterCritical;
var insideDestination = getDraggablesInsideDroppable(droppable.descriptor.id, draggables);
var draggablePage = draggable.page;
var axis = droppable.axis;
if (!insideDestination.length) {
return goIntoStart({
axis: axis,
moveInto: droppable.page,
isMoving: draggablePage
});
}
var displaced = impact.displaced,
displacedBy = impact.displacedBy;
var closestAfter = displaced.all[0];
if (closestAfter) {
var closest = draggables[closestAfter];
if (didStartAfterCritical(closestAfter, afterCritical)) {
return goBefore({
axis: axis,
moveRelativeTo: closest.page,
isMoving: draggablePage
});
}
var withDisplacement = cssBoxModel.offset(closest.page, displacedBy.point);
return goBefore({
axis: axis,
moveRelativeTo: withDisplacement,
isMoving: draggablePage
});
}
var last = insideDestination[insideDestination.length - 1];
if (last.descriptor.id === draggable.descriptor.id) {
return draggablePage.borderBox.center;
}
if (didStartAfterCritical(last.descriptor.id, afterCritical)) {
var page = cssBoxModel.offset(last.page, negate(afterCritical.displacedBy.point));
return goAfter({
axis: axis,
moveRelativeTo: page,
isMoving: draggablePage
});
}
return goAfter({
axis: axis,
moveRelativeTo: last.page,
isMoving: draggablePage
});
});
var withDroppableDisplacement = (function (droppable, point) {
var frame = droppable.frame;
if (!frame) {
return point;
}
return add(point, frame.scroll.diff.displacement);
});
var getResultWithoutDroppableDisplacement = function getResultWithoutDroppableDisplacement(_ref) {
var impact = _ref.impact,
draggable = _ref.draggable,
droppable = _ref.droppable,
draggables = _ref.draggables,
afterCritical = _ref.afterCritical;
var original = draggable.page.borderBox.center;
var at = impact.at;
if (!droppable) {
return original;
}
if (!at) {
return original;
}
if (at.type === 'REORDER') {
return whenReordering({
impact: impact,
draggable: draggable,
draggables: draggables,
droppable: droppable,
afterCritical: afterCritical
});
}
return whenCombining({
impact: impact,
draggables: draggables,
afterCritical: afterCritical
});
};
var getPageBorderBoxCenterFromImpact = (function (args) {
var withoutDisplacement = getResultWithoutDroppableDisplacement(args);
var droppable = args.droppable;
var withDisplacement = droppable ? withDroppableDisplacement(droppable, withoutDisplacement) : withoutDisplacement;
return withDisplacement;
});
var scrollViewport = (function (viewport, newScroll) {
var diff = subtract(newScroll, viewport.scroll.initial);
var displacement = negate(diff);
var frame = cssBoxModel.getRect({
top: newScroll.y,
bottom: newScroll.y + viewport.frame.height,
left: newScroll.x,
right: newScroll.x + viewport.frame.width
});
var updated = {
frame: frame,
scroll: {
initial: viewport.scroll.initial,
max: viewport.scroll.max,
current: newScroll,
diff: {
value: diff,
displacement: displacement
}
}
};
return updated;
});
function getDraggables(ids, draggables) {
return ids.map(function (id) {
return draggables[id];
});
}
function tryGetVisible(id, groups) {
for (var i = 0; i < groups.length; i++) {
var displacement = groups[i].visible[id];
if (displacement) {
return displacement;
}
}
return null;
}
var speculativelyIncrease = (function (_ref) {
var impact = _ref.impact,
viewport = _ref.viewport,
destination = _ref.destination,
draggables = _ref.draggables,
maxScrollChange = _ref.maxScrollChange;
var scrolledViewport = scrollViewport(viewport, add(viewport.scroll.current, maxScrollChange));
var scrolledDroppable = destination.frame ? scrollDroppable(destination, add(destination.frame.scroll.current, maxScrollChange)) : destination;
var last = impact.displaced;
var withViewportScroll = getDisplacementGroups({
afterDragging: getDraggables(last.all, draggables),
destination: destination,
displacedBy: impact.displacedBy,
viewport: scrolledViewport.frame,
last: last,
forceShouldAnimate: false
});
var withDroppableScroll = getDisplacementGroups({
afterDragging: getDraggables(last.all, draggables),
destination: scrolledDroppable,
displacedBy: impact.displacedBy,
viewport: viewport.frame,
last: last,
forceShouldAnimate: false
});
var invisible = {};
var visible = {};
var groups = [last, withViewportScroll, withDroppableScroll];
last.all.forEach(function (id) {
var displacement = tryGetVisible(id, groups);
if (displacement) {
visible[id] = displacement;
return;
}
invisible[id] = true;
});
var newImpact = _extends({}, impact, {
displaced: {
all: last.all,
invisible: invisible,
visible: visible
}
});
return newImpact;
});
var withViewportDisplacement = (function (viewport, point) {
return add(viewport.scroll.diff.displacement, point);
});
var getClientFromPageBorderBoxCenter = (function (_ref) {
var pageBorderBoxCenter = _ref.pageBorderBoxCenter,
draggable = _ref.draggable,
viewport = _ref.viewport;
var withoutPageScrollChange = withViewportDisplacement(viewport, pageBorderBoxCenter);
var offset = subtract(withoutPageScrollChange, draggable.page.borderBox.center);
return add(draggable.client.borderBox.center, offset);
});
var isTotallyVisibleInNewLocation = (function (_ref) {
var draggable = _ref.draggable,
destination = _ref.destination,
newPageBorderBoxCenter = _ref.newPageBorderBoxCenter,
viewport = _ref.viewport,
withDroppableDisplacement = _ref.withDroppableDisplacement,
_ref$onlyOnMainAxis = _ref.onlyOnMainAxis,
onlyOnMainAxis = _ref$onlyOnMainAxis === void 0 ? false : _ref$onlyOnMainAxis;
var changeNeeded = subtract(newPageBorderBoxCenter, draggable.page.borderBox.center);
var shifted = offsetByPosition(draggable.page.borderBox, changeNeeded);
var args = {
target: shifted,
destination: destination,
withDroppableDisplacement: withDroppableDisplacement,
viewport: viewport
};
return onlyOnMainAxis ? isTotallyVisibleOnAxis(args) : isTotallyVisible(args);
});
var moveToNextPlace = (function (_ref) {
var isMovingForward = _ref.isMovingForward,
draggable = _ref.draggable,
destination = _ref.destination,
draggables = _ref.draggables,
previousImpact = _ref.previousImpact,
viewport = _ref.viewport,
previousPageBorderBoxCenter = _ref.previousPageBorderBoxCenter,
previousClientSelection = _ref.previousClientSelection,
afterCritical = _ref.afterCritical;
if (!destination.isEnabled) {
return null;
}
var insideDestination = getDraggablesInsideDroppable(destination.descriptor.id, draggables);
var isInHomeList = isHomeOf(draggable, destination);
var impact = moveToNextCombine({
isMovingForward: isMovingForward,
draggable: draggable,
destination: destination,
insideDestination: insideDestination,
previousImpact: previousImpact
}) || moveToNextIndex({
isMovingForward: isMovingForward,
isInHomeList: isInHomeList,
draggable: draggable,
draggables: draggables,
destination: destination,
insideDestination: insideDestination,
previousImpact: previousImpact,
viewport: viewport,
afterCritical: afterCritical
});
if (!impact) {
return null;
}
var pageBorderBoxCenter = getPageBorderBoxCenterFromImpact({
impact: impact,
draggable: draggable,
droppable: destination,
draggables: draggables,
afterCritical: afterCritical
});
var isVisibleInNewLocation = isTotallyVisibleInNewLocation({
draggable: draggable,
destination: destination,
newPageBorderBoxCenter: pageBorderBoxCenter,
viewport: viewport.frame,
withDroppableDisplacement: false,
onlyOnMainAxis: true
});
if (isVisibleInNewLocation) {
var clientSelection = getClientFromPageBorderBoxCenter({
pageBorderBoxCenter: pageBorderBoxCenter,
draggable: draggable,
viewport: viewport
});
return {
clientSelection: clientSelection,
impact: impact,
scrollJumpRequest: null
};
}
var distance = subtract(pageBorderBoxCenter, previousPageBorderBoxCenter);
var cautious = speculativelyIncrease({
impact: impact,
viewport: viewport,
destination: destination,
draggables: draggables,
maxScrollChange: distance
});
return {
clientSelection: previousClientSelection,
impact: cautious,
scrollJumpRequest: distance
};
});
var getKnownActive = function getKnownActive(droppable) {
var rect = droppable.subject.active;
!rect ? process.env.NODE_ENV !== "production" ? invariant(false, 'Cannot get clipped area from droppable') : invariant(false) : void 0;
return rect;
};
var getBestCrossAxisDroppable = (function (_ref) {
var isMovingForward = _ref.isMovingForward,
pageBorderBoxCenter = _ref.pageBorderBoxCenter,
source = _ref.source,
droppables = _ref.droppables,
viewport = _ref.viewport;
var active = source.subject.active;
if (!active) {
return null;
}
var axis = source.axis;
var isBetweenSourceClipped = isWithin(active[axis.start], active[axis.end]);
var candidates = toDroppableList(droppables).filter(function (droppable) {
return droppable !== source;
}).filter(function (droppable) {
return droppable.isEnabled;
}).filter(function (droppable) {
return Boolean(droppable.subject.active);
}).filter(function (droppable) {
return isPartiallyVisibleThroughFrame(viewport.frame)(getKnownActive(droppable));
}).filter(function (droppable) {
var activeOfTarget = getKnownActive(droppable);
if (isMovingForward) {
return active[axis.crossAxisEnd] < activeOfTarget[axis.crossAxisEnd];
}
return activeOfTarget[axis.crossAxisStart] < active[axis.crossAxisStart];
}).filter(function (droppable) {
var activeOfTarget = getKnownActive(droppable);
var isBetweenDestinationClipped = isWithin(activeOfTarget[axis.start], activeOfTarget[axis.end]);
return isBetweenSourceClipped(activeOfTarget[axis.start]) || isBetweenSourceClipped(activeOfTarget[axis.end]) || isBetweenDestinationClipped(active[axis.start]) || isBetweenDestinationClipped(active[axis.end]);
}).sort(function (a, b) {
var first = getKnownActive(a)[axis.crossAxisStart];
var second = getKnownActive(b)[axis.crossAxisStart];
if (isMovingForward) {
return first - second;
}
return second - first;
}).filter(function (droppable, index, array) {
return getKnownActive(droppable)[axis.crossAxisStart] === getKnownActive(array[0])[axis.crossAxisStart];
});
if (!candidates.length) {
return null;
}
if (candidates.length === 1) {
return candidates[0];
}
var contains = candidates.filter(function (droppable) {
var isWithinDroppable = isWithin(getKnownActive(droppable)[axis.start], getKnownActive(droppable)[axis.end]);
return isWithinDroppable(pageBorderBoxCenter[axis.line]);
});
if (contains.length === 1) {
return contains[0];
}
if (contains.length > 1) {
return contains.sort(function (a, b) {
return getKnownActive(a)[axis.start] - getKnownActive(b)[axis.start];
})[0];
}
return candidates.sort(function (a, b) {
var first = closest(pageBorderBoxCenter, getCorners(getKnownActive(a)));
var second = closest(pageBorderBoxCenter, getCorners(getKnownActive(b)));
if (first !== second) {
return first - second;
}
return getKnownActive(a)[axis.start] - getKnownActive(b)[axis.start];
})[0];
});
var getCurrentPageBorderBoxCenter = function getCurrentPageBorderBoxCenter(draggable, afterCritical) {
var original = draggable.page.borderBox.center;
return didStartAfterCritical(draggable.descriptor.id, afterCritical) ? subtract(original, afterCritical.displacedBy.point) : original;
};
var getCurrentPageBorderBox = function getCurrentPageBorderBox(draggable, afterCritical) {
var original = draggable.page.borderBox;
return didStartAfterCritical(draggable.descriptor.id, afterCritical) ? offsetByPosition(original, negate(afterCritical.displacedBy.point)) : original;
};
var getClosestDraggable = (function (_ref) {
var pageBorderBoxCenter = _ref.pageBorderBoxCenter,
viewport = _ref.viewport,
destination = _ref.destination,
insideDestination = _ref.insideDestination,
afterCritical = _ref.afterCritical;
var sorted = insideDestination.filter(function (draggable) {
return isTotallyVisible({
target: getCurrentPageBorderBox(draggable, afterCritical),
destination: destination,
viewport: viewport.frame,
withDroppableDisplacement: true
});
}).sort(function (a, b) {
var distanceToA = distance(pageBorderBoxCenter, withDroppableDisplacement(destination, getCurrentPageBorderBoxCenter(a, afterCritical)));
var distanceToB = distance(pageBorderBoxCenter, withDroppableDisplacement(destination, getCurrentPageBorderBoxCenter(b, afterCritical)));
if (distanceToA < distanceToB) {
return -1;
}
if (distanceToB < distanceToA) {
return 1;
}
return a.descriptor.index - b.descriptor.index;
});
return sorted[0] || null;
});
var getDisplacedBy = memoizeOne(function getDisplacedBy(axis, displaceBy) {
var displacement = displaceBy[axis.line];
return {
value: displacement,
point: patch(axis.line, displacement)
};
});
var getRequiredGrowthForPlaceholder = function getRequiredGrowthForPlaceholder(droppable, placeholderSize, draggables) {
var axis = droppable.axis;
if (droppable.descriptor.mode === 'virtual') {
return patch(axis.line, placeholderSize[axis.line]);
}
var availableSpace = droppable.subject.page.contentBox[axis.size];
var insideDroppable = getDraggablesInsideDroppable(droppable.descriptor.id, draggables);
var spaceUsed = insideDroppable.reduce(function (sum, dimension) {
return sum + dimension.client.marginBox[axis.size];
}, 0);
var requiredSpace = spaceUsed + placeholderSize[axis.line];
var needsToGrowBy = requiredSpace - availableSpace;
if (needsToGrowBy <= 0) {
return null;
}
return patch(axis.line, needsToGrowBy);
};
var withMaxScroll = function withMaxScroll(frame, max) {
return _extends({}, frame, {
scroll: _extends({}, frame.scroll, {
max: max
})
});
};
var addPlaceholder = function addPlaceholder(droppable, draggable, draggables) {
var frame = droppable.frame;
!!isHomeOf(draggable, droppable) ? process.env.NODE_ENV !== "production" ? invariant(false, 'Should not add placeholder space to home list') : invariant(false) : void 0;
!!droppable.subject.withPlaceholder ? process.env.NODE_ENV !== "production" ? invariant(false, 'Cannot add placeholder size to a subject when it already has one') : invariant(false) : void 0;
var placeholderSize = getDisplacedBy(droppable.axis, draggable.displaceBy).point;
var requiredGrowth = getRequiredGrowthForPlaceholder(droppable, placeholderSize, draggables);
var added = {
placeholderSize: placeholderSize,
increasedBy: requiredGrowth,
oldFrameMaxScroll: droppable.frame ? droppable.frame.scroll.max : null
};
if (!frame) {
var _subject = getSubject({
page: droppable.subject.page,
withPlaceholder: added,
axis: droppable.axis,
frame: droppable.frame
});
return _extends({}, droppable, {
subject: _subject
});
}
var maxScroll = requiredGrowth ? add(frame.scroll.max, requiredGrowth) : frame.scroll.max;
var newFrame = withMaxScroll(frame, maxScroll);
var subject = getSubject({
page: droppable.subject.page,
withPlaceholder: added,
axis: droppable.axis,
frame: newFrame
});
return _extends({}, droppable, {
subject: subject,
frame: newFrame
});
};
var removePlaceholder = function removePlaceholder(droppable) {
var added = droppable.subject.withPlaceholder;
!added ? process.env.NODE_ENV !== "production" ? invariant(false, 'Cannot remove placeholder form subject when there was none') : invariant(false) : void 0;
var frame = droppable.frame;
if (!frame) {
var _subject2 = getSubject({
page: droppable.subject.page,
axis: droppable.axis,
frame: null,
withPlaceholder: null
});
return _extends({}, droppable, {
subject: _subject2
});
}
var oldMaxScroll = added.oldFrameMaxScroll;
!oldMaxScroll ? process.env.NODE_ENV !== "production" ? invariant(false, 'Expected droppable with frame to have old max frame scroll when removing placeholder') : invariant(false) : void 0;
var newFrame = withMaxScroll(frame, oldMaxScroll);
var subject = getSubject({
page: droppable.subject.page,
axis: droppable.axis,
frame: newFrame,
withPlaceholder: null
});
return _extends({}, droppable, {
subject: subject,
frame: newFrame
});
};
var moveToNewDroppable = (function (_ref) {
var previousPageBorderBoxCenter = _ref.previousPageBorderBoxCenter,
moveRelativeTo = _ref.moveRelativeTo,
insideDestination = _ref.insideDestination,
draggable = _ref.draggable,
draggables = _ref.draggables,
destination = _ref.destination,
viewport = _ref.viewport,
afterCritical = _ref.afterCritical;
if (!moveRelativeTo) {
if (insideDestination.length) {
return null;
}
var proposed = {
displaced: emptyGroups,
displacedBy: noDisplacedBy,
at: {
type: 'REORDER',
destination: {
droppableId: destination.descriptor.id,
index: 0
}
}
};
var proposedPageBorderBoxCenter = getPageBorderBoxCenterFromImpact({
impact: proposed,
draggable: draggable,
droppable: destination,
draggables: draggables,
afterCritical: afterCritical
});
var withPlaceholder = isHomeOf(draggable, destination) ? destination : addPlaceholder(destination, draggable, draggables);
var isVisibleInNewLocation = isTotallyVisibleInNewLocation({
draggable: draggable,
destination: withPlaceholder,
newPageBorderBoxCenter: proposedPageBorderBoxCenter,
viewport: viewport.frame,
withDroppableDisplacement: false,
onlyOnMainAxis: true
});
return isVisibleInNewLocation ? proposed : null;
}
var isGoingBeforeTarget = Boolean(previousPageBorderBoxCenter[destination.axis.line] <= moveRelativeTo.page.borderBox.center[destination.axis.line]);
var proposedIndex = function () {
var relativeTo = moveRelativeTo.descriptor.index;
if (moveRelativeTo.descriptor.id === draggable.descriptor.id) {
return relativeTo;
}
if (isGoingBeforeTarget) {
return relativeTo;
}
return relativeTo + 1;
}();
var displacedBy = getDisplacedBy(destination.axis, draggable.displaceBy);
return calculateReorderImpact({
draggable: draggable,
insideDestination: insideDestination,
destination: destination,
viewport: viewport,
displacedBy: displacedBy,
last: emptyGroups,
index: proposedIndex
});
});
var moveCrossAxis = (function (_ref) {
var isMovingForward = _ref.isMovingForward,
previousPageBorderBoxCenter = _ref.previousPageBorderBoxCenter,
draggable = _ref.draggable,
isOver = _ref.isOver,
draggables = _ref.draggables,
droppa