react-beautiful-dnd-next
Version:
Beautiful and accessible drag and drop for lists with React
1,687 lines (1,451 loc) • 240 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 _extends = _interopDefault(require('@babel/runtime-corejs2/helpers/extends'));
var React = require('react');
var React__default = _interopDefault(React);
var useMemoOne = require('use-memo-one');
var _inheritsLoose = _interopDefault(require('@babel/runtime-corejs2/helpers/inheritsLoose'));
var invariant = _interopDefault(require('tiny-invariant'));
var redux = require('redux');
var reactRedux = require('react-redux');
var cssBoxModel = require('css-box-model');
var memoizeOne = _interopDefault(require('memoize-one'));
var _Object$values = _interopDefault(require('@babel/runtime-corejs2/core-js/object/values'));
var _Object$keys = _interopDefault(require('@babel/runtime-corejs2/core-js/object/keys'));
var _Date$now = _interopDefault(require('@babel/runtime-corejs2/core-js/date/now'));
var rafSchd = _interopDefault(require('raf-schd'));
var _Object$assign = _interopDefault(require('@babel/runtime-corejs2/core-js/object/assign'));
var _Number$isInteger = _interopDefault(require('@babel/runtime-corejs2/core-js/number/is-integer'));
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';
var warning = function warning(message) {
var _console;
if (isProduction) {
return;
}
if (typeof window !== 'undefined' && window[isDisabledFlag]) {
return;
}
(_console = console).warn.apply(_console, getFormattedMessage(message));
};
function printFatalError(error) {
var _console;
if (process.env.NODE_ENV === 'production') {
return;
}
(_console = console).error.apply(_console, getFormattedMessage("\n An error has occurred while a drag is occurring.\n Any existing drag will be cancelled.\n\n > " + error.message + "\n "));
console.error('raw', error);
}
function shouldRecover(error) {
return error.message.indexOf('Invariant failed') !== -1;
}
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.onError = void 0;
_this.setOnError = function (fn) {
_this.onError = fn;
};
_this.onFatalError = function (error) {
printFatalError(error);
if (_this.onError) {
_this.onError();
} else {
process.env.NODE_ENV !== "production" ? warning('Could not find recovering function') : void 0;
}
if (shouldRecover(error)) {
_this.setState({});
}
};
return _this;
}
var _proto = ErrorBoundary.prototype;
_proto.componentDidMount = function componentDidMount() {
window.addEventListener('error', this.onFatalError);
};
_proto.componentWillUnmount = function componentWillUnmount() {
window.removeEventListener('error', this.onFatalError);
};
_proto.componentDidCatch = function componentDidCatch(error) {
this.onFatalError(error);
if (!shouldRecover(error)) {
throw error;
}
};
_proto.render = function render() {
return this.props.children(this.setOnError);
};
return ErrorBoundary;
}(React__default.Component);
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 isEqual$1 = function isEqual(first, second) {
return first.top === second.top && first.right === second.right && first.bottom === second.bottom && first.left === second.left;
};
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;
});
var records = {};
var isEnabled = false;
var isTimingsEnabled = function isTimingsEnabled() {
return isEnabled;
};
var start = function start(key) {
if (process.env.NODE_ENV !== 'production') {
if (!isTimingsEnabled()) {
return;
}
var now = performance.now();
records[key] = now;
}
};
var finish = function finish(key) {
if (process.env.NODE_ENV !== 'production') {
if (!isTimingsEnabled()) {
return;
}
var now = performance.now();
var previous = records[key];
if (!previous) {
console.warn('cannot finish timing as no previous time found', key);
return;
}
var result = now - previous;
var rounded = result.toFixed(2);
var style = function () {
if (result < 12) {
return {
textColor: 'green',
symbol: '✅'
};
}
if (result < 40) {
return {
textColor: 'orange',
symbol: '⚠️'
};
}
return {
textColor: 'red',
symbol: '❌'
};
}();
console.log(style.symbol + " %cTiming %c" + rounded + " %cms %c" + key, 'color: blue; font-weight: bold;', "color: " + style.textColor + "; font-size: 1.1em;", 'color: grey;', 'color: purple; font-weight: bold;');
}
};
var whatIsDraggedOver = (function (impact) {
var merge = impact.merge,
destination = impact.destination;
if (destination) {
return destination.droppableId;
}
if (merge) {
return merge.combine.droppableId;
}
return null;
});
function values(map) {
return _Object$values(map);
}
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;
}
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 isWithin = (function (lowerBound, upperBound) {
return function (value) {
return lowerBound <= value && value <= upperBound;
};
});
var isPositionInFrame = (function (frame) {
var isWithinVertical = isWithin(frame.top, frame.bottom);
var isWithinHorizontal = isWithin(frame.left, frame.right);
return function (point) {
return isWithinVertical(point.y) && isWithinVertical(point.y) && isWithinHorizontal(point.x) && isWithinHorizontal(point.x);
};
});
var getDroppableOver = (function (_ref) {
var target = _ref.target,
droppables = _ref.droppables;
var maybe = find(toDroppableList(droppables), function (droppable) {
if (!droppable.isEnabled) {
return false;
}
var active = droppable.subject.active;
if (!active) {
return false;
}
return isPositionInFrame(active)(target);
});
return maybe ? maybe.descriptor.id : null;
});
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;
});
var withDroppableScroll = (function (droppable, point) {
var frame = droppable.frame;
if (!frame) {
return point;
}
return add(point, frame.scroll.diff.value);
});
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 isUserMovingForward = (function (axis, direction) {
return axis === vertical ? direction.vertical === 'down' : direction.horizontal === 'right';
});
var didStartDisplaced = (function (draggableId, onLift) {
return Boolean(onLift.wasDisplaced[draggableId]);
});
var getCombinedItemDisplacement = (function (_ref) {
var displaced = _ref.displaced,
onLift = _ref.onLift,
combineWith = _ref.combineWith,
displacedBy = _ref.displacedBy;
var isDisplaced = Boolean(displaced[combineWith]);
if (didStartDisplaced(combineWith, onLift)) {
return isDisplaced ? origin : negate(displacedBy.point);
}
return isDisplaced ? displacedBy.point : origin;
});
var getWhenEntered = function getWhenEntered(id, current, oldMerge) {
if (!oldMerge) {
return current;
}
if (id !== oldMerge.combine.draggableId) {
return current;
}
return oldMerge.whenEntered;
};
var isCombiningWith = function isCombiningWith(_ref) {
var id = _ref.id,
currentCenter = _ref.currentCenter,
axis = _ref.axis,
borderBox = _ref.borderBox,
displaceBy = _ref.displaceBy,
currentUserDirection = _ref.currentUserDirection,
oldMerge = _ref.oldMerge;
var start = borderBox[axis.start] + displaceBy[axis.line];
var end = borderBox[axis.end] + displaceBy[axis.line];
var size = borderBox[axis.size];
var twoThirdsOfSize = size * 0.666;
var whenEntered = getWhenEntered(id, currentUserDirection, oldMerge);
var isMovingForward = isUserMovingForward(axis, whenEntered);
var targetCenter = currentCenter[axis.line];
if (isMovingForward) {
return isWithin(start, start + twoThirdsOfSize)(targetCenter);
}
return isWithin(end - twoThirdsOfSize, end)(targetCenter);
};
var getCombineImpact = (function (_ref2) {
var currentCenter = _ref2.pageBorderBoxCenterWithDroppableScrollChange,
previousImpact = _ref2.previousImpact,
destination = _ref2.destination,
insideDestinationWithoutDraggable = _ref2.insideDestinationWithoutDraggable,
userDirection = _ref2.userDirection,
onLift = _ref2.onLift;
if (!destination.isCombineEnabled) {
return null;
}
var axis = destination.axis;
var map = previousImpact.movement.map;
var canBeDisplacedBy = previousImpact.movement.displacedBy;
var oldMerge = previousImpact.merge;
var target = find(insideDestinationWithoutDraggable, function (child) {
var id = child.descriptor.id;
var displaceBy = getCombinedItemDisplacement({
displaced: map,
onLift: onLift,
combineWith: id,
displacedBy: canBeDisplacedBy
});
return isCombiningWith({
id: id,
currentCenter: currentCenter,
axis: axis,
borderBox: child.page.borderBox,
displaceBy: displaceBy,
currentUserDirection: userDirection,
oldMerge: oldMerge
});
});
if (!target) {
return null;
}
var merge = {
whenEntered: getWhenEntered(target.descriptor.id, userDirection, oldMerge),
combine: {
draggableId: target.descriptor.id,
droppableId: destination.descriptor.id
}
};
var withMerge = _extends({}, previousImpact, {
destination: null,
merge: merge
});
return withMerge;
});
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 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(forceShouldAnimate, isVisible, previous) {
if (typeof forceShouldAnimate === 'boolean') {
return forceShouldAnimate;
}
if (!isVisible) {
return false;
}
if (!previous) {
return true;
}
return previous.shouldAnimate;
};
var getTarget = function getTarget(draggable, onLift) {
var marginBox = draggable.page.marginBox;
if (!didStartDisplaced(draggable.descriptor.id, onLift)) {
return marginBox;
}
var expandBy = {
top: onLift.displacedBy.point.y,
right: 0,
bottom: 0,
left: onLift.displacedBy.point.x
};
return cssBoxModel.getRect(cssBoxModel.expand(marginBox, expandBy));
};
var getDisplacement = (function (_ref) {
var draggable = _ref.draggable,
destination = _ref.destination,
previousImpact = _ref.previousImpact,
viewport = _ref.viewport,
onLift = _ref.onLift,
forceShouldAnimate = _ref.forceShouldAnimate;
var id = draggable.descriptor.id;
var map = previousImpact.movement.map;
var target = getTarget(draggable, onLift);
var isVisible = isPartiallyVisible({
target: target,
destination: destination,
viewport: viewport,
withDroppableDisplacement: true
});
var shouldAnimate = getShouldAnimate(forceShouldAnimate, isVisible, map[id]);
var displacement = {
draggableId: id,
isVisible: isVisible,
shouldAnimate: shouldAnimate
};
return displacement;
});
var getDisplacementMap = memoizeOne(function (displaced) {
return displaced.reduce(function (map, displacement) {
map[displacement.draggableId] = displacement;
return map;
}, {});
});
var getDisplacedBy = memoizeOne(function (axis, displaceBy) {
var displacement = displaceBy[axis.line];
return {
value: displacement,
point: patch(axis.line, displacement)
};
});
var getReorderImpact = (function (_ref) {
var currentCenter = _ref.pageBorderBoxCenterWithDroppableScrollChange,
draggable = _ref.draggable,
destination = _ref.destination,
insideDestinationWithoutDraggable = _ref.insideDestinationWithoutDraggable,
previousImpact = _ref.previousImpact,
viewport = _ref.viewport,
userDirection = _ref.userDirection,
onLift = _ref.onLift;
var axis = destination.axis;
var isMovingForward = isUserMovingForward(destination.axis, userDirection);
var displacedBy = getDisplacedBy(destination.axis, draggable.displaceBy);
var targetCenter = currentCenter[axis.line];
var displacement = displacedBy.value;
var displaced = insideDestinationWithoutDraggable.filter(function (child) {
var borderBox = child.page.borderBox;
var start = borderBox[axis.start];
var end = borderBox[axis.end];
var didStartDisplaced$1 = didStartDisplaced(child.descriptor.id, onLift);
if (isMovingForward) {
if (didStartDisplaced$1) {
return targetCenter < start;
}
return targetCenter < start + displacement;
}
if (didStartDisplaced$1) {
return targetCenter <= end - displacement;
}
return targetCenter <= end;
}).map(function (dimension) {
return getDisplacement({
draggable: dimension,
destination: destination,
previousImpact: previousImpact,
viewport: viewport.frame,
onLift: onLift
});
});
var newIndex = insideDestinationWithoutDraggable.length - displaced.length;
var movement = {
displacedBy: displacedBy,
displaced: displaced,
map: getDisplacementMap(displaced)
};
var impact = {
movement: movement,
destination: {
droppableId: destination.descriptor.id,
index: newIndex
},
merge: null
};
return impact;
});
var noDisplacedBy = {
point: origin,
value: 0
};
var noMovement = {
displaced: [],
map: {},
displacedBy: noDisplacedBy
};
var noImpact = {
movement: noMovement,
destination: null,
merge: null
};
var removeDraggableFromList = memoizeOne(function (remove, list) {
return list.filter(function (item) {
return item.descriptor.id !== remove.descriptor.id;
});
});
var getDragImpact = (function (_ref) {
var pageBorderBoxCenter = _ref.pageBorderBoxCenter,
draggable = _ref.draggable,
draggables = _ref.draggables,
droppables = _ref.droppables,
previousImpact = _ref.previousImpact,
viewport = _ref.viewport,
userDirection = _ref.userDirection,
onLift = _ref.onLift;
var destinationId = getDroppableOver({
target: pageBorderBoxCenter,
droppables: droppables
});
if (!destinationId) {
return noImpact;
}
var destination = droppables[destinationId];
var insideDestination = getDraggablesInsideDroppable(destination.descriptor.id, draggables);
var insideDestinationWithoutDraggable = removeDraggableFromList(draggable, insideDestination);
var pageBorderBoxCenterWithDroppableScrollChange = withDroppableScroll(destination, pageBorderBoxCenter);
var withMerge = getCombineImpact({
pageBorderBoxCenterWithDroppableScrollChange: pageBorderBoxCenterWithDroppableScrollChange,
previousImpact: previousImpact,
destination: destination,
insideDestinationWithoutDraggable: insideDestinationWithoutDraggable,
userDirection: userDirection,
onLift: onLift
});
if (withMerge) {
return withMerge;
}
return getReorderImpact({
pageBorderBoxCenterWithDroppableScrollChange: pageBorderBoxCenterWithDroppableScrollChange,
destination: destination,
draggable: draggable,
insideDestinationWithoutDraggable: insideDestinationWithoutDraggable,
previousImpact: previousImpact,
viewport: viewport,
userDirection: userDirection,
onLift: onLift
});
});
var getHomeLocation = (function (descriptor) {
return {
index: descriptor.index,
droppableId: descriptor.droppableId
};
});
var getHomeOnLift = (function (_ref) {
var draggable = _ref.draggable,
home = _ref.home,
draggables = _ref.draggables,
viewport = _ref.viewport;
var displacedBy = getDisplacedBy(home.axis, draggable.displaceBy);
var insideHome = getDraggablesInsideDroppable(home.descriptor.id, draggables);
var originallyDisplaced = insideHome.slice(draggable.descriptor.index + 1);
var wasDisplaced = originallyDisplaced.reduce(function (previous, item) {
previous[item.descriptor.id] = true;
return previous;
}, {});
var onLift = {
displacedBy: displacedBy,
wasDisplaced: wasDisplaced
};
var displaced = originallyDisplaced.map(function (dimension) {
return getDisplacement({
draggable: dimension,
destination: home,
previousImpact: noImpact,
viewport: viewport.frame,
forceShouldAnimate: false,
onLift: onLift
});
});
var movement = {
displaced: displaced,
map: getDisplacementMap(displaced),
displacedBy: displacedBy
};
var impact = {
movement: movement,
destination: getHomeLocation(draggable.descriptor),
merge: null
};
return {
impact: impact,
onLift: onLift
};
});
var getDragPositions = (function (_ref) {
var oldInitial = _ref.initial,
oldCurrent = _ref.current,
oldClientBorderBoxCenter = _ref.oldClientBorderBoxCenter,
newClientBorderBoxCenter = _ref.newClientBorderBoxCenter,
viewport = _ref.viewport;
var shift = subtract(newClientBorderBoxCenter, oldClientBorderBoxCenter);
var initial = function () {
var client = {
selection: add(oldInitial.client.selection, shift),
borderBoxCenter: newClientBorderBoxCenter,
offset: origin
};
var page = {
selection: add(client.selection, viewport.scroll.initial),
borderBoxCenter: add(client.selection, viewport.scroll.initial)
};
return {
client: client,
page: page
};
}();
var current = function () {
var reverse = negate(shift);
var offset = add(oldCurrent.client.offset, reverse);
var client = {
selection: add(initial.client.selection, offset),
borderBoxCenter: add(initial.client.borderBoxCenter, offset),
offset: offset
};
var page = {
selection: add(client.selection, viewport.scroll.current),
borderBoxCenter: add(client.borderBoxCenter, viewport.scroll.current)
};
!isEqual(oldCurrent.client.borderBoxCenter, client.borderBoxCenter) ? process.env.NODE_ENV !== "production" ? invariant(false, "\n Incorrect new client center position.\n Expected (" + oldCurrent.client.borderBoxCenter.x + ", " + oldCurrent.client.borderBoxCenter.y + ")\n to equal (" + client.borderBoxCenter.x + ", " + client.borderBoxCenter.y + ")\n ") : invariant(false) : void 0;
return {
client: client,
page: page
};
}();
return {
current: current,
initial: initial
};
});
var offsetDraggable = (function (_ref) {
var draggable = _ref.draggable,
offset = _ref.offset,
initialWindowScroll = _ref.initialWindowScroll;
var client = cssBoxModel.offset(draggable.client, offset);
var page = cssBoxModel.withScroll(client, initialWindowScroll);
var moved = _extends({}, draggable, {
placeholder: _extends({}, draggable.placeholder, {
client: client
}),
client: client,
page: page
});
return moved;
});
var adjustExistingForAdditionsAndRemovals = (function (_ref) {
var existing = _ref.existing,
droppables = _ref.droppables,
addedDraggables = _ref.additions,
removedDraggables = _ref.removals,
viewport = _ref.viewport;
var shifted = {};
toDroppableList(droppables).forEach(function (droppable) {
var axis = droppable.axis;
var original = getDraggablesInsideDroppable(droppable.descriptor.id, existing);
var toShift = {};
var addShift = function addShift(id, shift) {
var previous = toShift[id];
if (!previous) {
toShift[id] = shift;
return;
}
toShift[id] = {
indexChange: previous.indexChange + shift.indexChange,
offset: add(previous.offset, shift.offset)
};
};
var removals = toDraggableMap(removedDraggables.map(function (id) {
var item = existing[id];
!item ? process.env.NODE_ENV !== "production" ? invariant(false, "Could not find removed draggable \"" + id + "\"") : invariant(false) : void 0;
return item;
}).filter(function (draggable) {
return draggable.descriptor.droppableId === droppable.descriptor.id;
}));
var withRemovals = original.filter(function (item, index) {
var isBeingRemoved = Boolean(removals[item.descriptor.id]);
if (!isBeingRemoved) {
return true;
}
var offset = negate(patch(axis.line, item.displaceBy[axis.line]));
original.slice(index).forEach(function (sibling) {
if (removals[sibling.descriptor.id]) {
return;
}
addShift(sibling.descriptor.id, {
indexChange: -1,
offset: offset
});
});
return false;
});
var additions = addedDraggables.filter(function (draggable) {
return draggable.descriptor.droppableId === droppable.descriptor.id;
});
var withAdditions = withRemovals.slice(0);
additions.forEach(function (item) {
withAdditions.splice(item.descriptor.index, 0, item);
});
var additionMap = toDraggableMap(additions);
withAdditions.forEach(function (item, index) {
var wasAdded = Boolean(additionMap[item.descriptor.id]);
if (!wasAdded) {
return;
}
var offset = patch(axis.line, item.client.marginBox[axis.size]);
withAdditions.slice(index).forEach(function (sibling) {
if (additionMap[sibling.descriptor.id]) {
return;
}
addShift(sibling.descriptor.id, {
indexChange: 1,
offset: offset
});
});
});
withAdditions.forEach(function (item) {
if (additionMap[item.descriptor.id]) {
return;
}
var shift = toShift[item.descriptor.id];
if (!shift) {
return;
}
var moved = offsetDraggable({
draggable: item,
offset: shift.offset,
initialWindowScroll: viewport.scroll.initial
});
var index = item.descriptor.index + shift.indexChange;
var updated = _extends({}, moved, {
descriptor: _extends({}, item.descriptor, {
index: index
})
});
shifted[moved.descriptor.id] = updated;
});
});
var map = _extends({}, existing, shifted);
return map;
});
var adjustAdditionsForScrollChanges = (function (_ref) {
var additions = _ref.additions,
updatedDroppables = _ref.updatedDroppables,
viewport = _ref.viewport;
var windowScrollChange = viewport.scroll.diff.value;
return additions.map(function (draggable) {
var droppableId = draggable.descriptor.droppableId;
var modified = updatedDroppables[droppableId];
var frame = modified.frame;
!frame ? process.env.NODE_ENV !== "production" ? invariant(false) : invariant(false) : void 0;
var droppableScrollChange = frame.scroll.diff.value;
var totalChange = add(windowScrollChange, droppableScrollChange);
var moved = offsetDraggable({
draggable: draggable,
offset: totalChange,
initialWindowScroll: viewport.scroll.initial
});
return moved;
});
});
var adjustAdditionsForCollapsedHome = (function (_ref) {
var additions = _ref.additions,
dragging = _ref.dragging,
home = _ref.home,
viewport = _ref.viewport;
var displacedBy = getDisplacedBy(home.axis, dragging.displaceBy);
return additions.map(function (draggable) {
if (draggable.descriptor.droppableId !== home.descriptor.id) {
return draggable;
}
if (draggable.descriptor.index < dragging.descriptor.index) {
return draggable;
}
return offsetDraggable({
draggable: draggable,
offset: displacedBy.point,
initialWindowScroll: viewport.scroll.initial
});
});
});
var updateDraggables = (function (_ref) {
var updatedDroppables = _ref.updatedDroppables,
criticalId = _ref.criticalId,
unmodifiedExisting = _ref.existing,
unmodifiedAdditions = _ref.additions,
removals = _ref.removals,
viewport = _ref.viewport;
var existing = adjustExistingForAdditionsAndRemovals({
droppables: updatedDroppables,
existing: unmodifiedExisting,
additions: unmodifiedAdditions,
removals: removals,
viewport: viewport
});
var dragging = existing[criticalId];
var home = updatedDroppables[dragging.descriptor.droppableId];
var scrolledAdditions = adjustAdditionsForScrollChanges({
additions: unmodifiedAdditions,
updatedDroppables: updatedDroppables,
viewport: viewport
});
var additions = adjustAdditionsForCollapsedHome({
additions: scrolledAdditions,
dragging: dragging,
home: home,
viewport: viewport
});
var map = _extends({}, existing, toDraggableMap(additions));
removals.forEach(function (id) {
delete map[id];
});
return map;
});
var getMaxScroll = (function (_ref) {
var scrollHeight = _ref.scrollHeight,
scrollWidth = _ref.scrollWidth,
height = _ref.height,
width = _ref.width;
var maxScroll = subtract({
x: scrollWidth,
y: scrollHeight
}, {
x: width,
y: height
});
var adjustedMaxScroll = {
x: Math.max(0, maxScroll.x),
y: Math.max(0, maxScroll.y)
};
return adjustedMaxScroll;
});
var getDroppableDimension = (function (_ref) {
var descriptor = _ref.descriptor,
isEnabled = _ref.isEnabled,
isCombineEnabled = _ref.isCombineEnabled,
isFixedOnPage = _ref.isFixedOnPage,
direction = _ref.direction,
client = _ref.client,
page = _ref.page,
closest = _ref.closest;
var frame = function () {
if (!closest) {
return null;
}
var scrollSize = closest.scrollSize,
frameClient = closest.client;
var maxScroll = getMaxScroll({
scrollHeight: scrollSize.scrollHeight,
scrollWidth: scrollSize.scrollWidth,
height: frameClient.paddingBox.height,
width: frameClient.paddingBox.width
});
return {
pageMarginBox: closest.page.marginBox,
frameClient: frameClient,
scrollSize: scrollSize,
shouldClipSubject: closest.shouldClipSubject,
scroll: {
initial: closest.scroll,
current: closest.scroll,
max: maxScroll,
diff: {
value: origin,
displacement: origin
}
}
};
}();
var axis = direction === 'vertical' ? vertical : horizontal;
var subject = getSubject({
page: page,
withPlaceholder: null,
axis: axis,
frame: frame
});
var dimension = {
descriptor: descriptor,
isCombineEnabled: isCombineEnabled,
isFixedOnPage: isFixedOnPage,
axis: axis,
isEnabled: isEnabled,
client: client,
page: page,
frame: frame,
subject: subject
};
return dimension;
});
var isHomeOf = (function (draggable, destination) {
return draggable.descriptor.droppableId === destination.descriptor.id;
});
var getRequiredGrowthForPlaceholder = function getRequiredGrowthForPlaceholder(droppable, placeholderSize, draggables) {
var axis = droppable.axis;
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 getFrame = (function (droppable) {
var frame = droppable.frame;
!frame ? process.env.NODE_ENV !== "production" ? invariant(false, 'Expected Droppable to have a frame') : invariant(false) : void 0;
return frame;
});
var throwIfSpacingChange = function throwIfSpacingChange(old, fresh) {
if (process.env.NODE_ENV !== 'production') {
var getMessage = function getMessage(spacingType) {
return "Cannot change the " + spacingType + " of a Droppable during a drag";
};
!isEqual$1(old.margin, fresh.margin) ? process.env.NODE_ENV !== "production" ? invariant(false, getMessage('margin')) : invariant(false) : void 0;
!isEqual$1(old.border, fresh.border) ? process.env.NODE_ENV !== "production" ? invariant(false, getMessage('border')) : invariant(false) : void 0;
!isEqual$1(old.padding, fresh.padding) ? process.env.NODE_ENV !== "production" ? invariant(false, getMessage('padding')) : invariant(false) : void 0;
}
};
var adjustBorderBoxSize = function adjustBorderBoxSize(axis, old, fresh) {
return {
top: old.top,
left: old.left,
right: old.left + fresh.width,
bottom: old.top + fresh.height
};
};
var updateDroppables = (function (_ref) {
var modified = _ref.modified,
existing = _ref.existing,
viewport = _ref.viewport;
if (!modified.length) {
return existing;
}
var adjusted = modified.map(function (provided) {
var raw = existing[provided.descriptor.id];
!raw ? process.env.NODE_ENV !== "production" ? invariant(false, 'Could not locate droppable in existing droppables') : invariant(false) : void 0;
var hasPlaceholder = Boolean(raw.subject.withPlaceholder);
var dimension = hasPlaceholder ? removePlaceholder(raw) : raw;
var oldClient = dimension.client;
var newClient = provided.client;
var oldScrollable = getFrame(dimension);
var newScrollable = getFrame(provided);
if (process.env.NODE_ENV !== 'production') {
throwIfSpacingChange(dimension.client, provided.client);
throwIfSpacingChange(oldScrollable.frameClient, newScrollable.frameClient);
var isFrameEqual = oldScrollable.frameClient.borderBox.height === newScrollable.frameClient.borderBox.height && oldScrollable.frameClient.borderBox.width === newScrollable.frameClient.borderBox.width;
!isFrameEqual ? process.env.NODE_ENV !== "production" ? invariant(false, 'The width and height of your Droppable scroll container cannot change when adding or removing Draggables during a drag') : invariant(false) : void 0;
}
var client = cssBoxModel.createBox({
borderBox: adjustBorderBoxSize(dimension.axis, oldClient.borderBox, newClient.borderBox),
margin: oldClient.margin,
border: oldClient.border,
padding: oldClient.padding
});
var closest = {
client: oldScrollable.frameClient,
page: cssBoxModel.withScroll(oldScrollable.frameClient, viewport.scroll.initial),
shouldClipSubject: oldScrollable.shouldClipSubject,
scrollSize: newScrollable.scrollSize,
scroll: oldScrollable.scroll.initial
};
var withSizeChanged = getDroppableDimension({
descriptor: provided.descriptor,
isEnabled: provided.isEnabled,
isCombineEnabled: provided.isCombineEnabled,
isFixedOnPage: provided.isFixedOnPage,
direction: provided.axis.direction,
client: client,
page: cssBoxModel.withScroll(client, viewport.scroll.initial),
closest: closest
});
var scrolled = scrollDroppable(withSizeChanged, newScrollable.scroll.current);
return scrolled;
});
var result = _extends({}, existing, toDroppableMap(adjusted));
return result;
});
var withNoAnimatedDisplacement = (function (impact) {
var displaced = impact.movement.displaced;
if (!displaced.length) {
return impact;
}
var withoutAnimation = displaced.map(function (displacement) {
if (!displacement.isVisible) {
return displacement;
}
if (!displacement.shouldAnimate) {
return displacement;
}
return _extends({}, displacement, {
shouldAnimate: false
});
});
var result = _extends({}, impact, {
movement: _extends({}, impact.movement, {
displaced: withoutAnimation,
map: getDisplacementMap(withoutAnimation)
})
});
return result;
});
var patchDroppableMap = (function (droppables, updated) {
var _extends2;
return _extends({}, droppables, (_extends2 = {}, _extends2[updated.descriptor.id] = updated, _extends2));
});
var clearUnusedPlaceholder = function clearUnusedPlaceholder(_ref) {
var previousImpact = _ref.previousImpact,
impact = _ref.impact,
droppables = _ref.droppables;
var last = whatIsDraggedOver(previousImpact);
var now = whatIsDraggedOver(impact);
if (!last) {
return droppables;
}
if (last === now) {
return droppables;
}
var lastDroppable = droppables[last];
if (!lastDroppable.subject.withPlaceholder) {
return droppables;
}
var updated = removePlaceholder(lastDroppable);
return patchDroppableMap(droppables, updated);
};
var recomputePlaceholders = (function (_ref2) {
var draggable = _ref2.draggable,
draggables = _ref2.draggables,
droppables = _ref2.droppables,
previousImpact = _ref2.previousImpact,
impact = _ref2.impact;
var cleaned = clearUnusedPlaceholder({
previousImpact: previousImpact,
impact: impact,
droppables: droppables
});
var isOver = whatIsDraggedOver(impact);
if (!isOver) {
return cleaned;
}
var droppable = droppables[isOver];
if (isHomeOf(draggable, droppable)) {
return cleaned;
}
if (droppable.subject.withPlaceholder) {
return cleaned;
}
var patched = addPlaceholder(droppable, draggable, draggables);
return patchDroppableMap(cleaned, patched);
});
var timingsKey = 'Processing dynamic changes';
var publishWhileDragging = (function (_ref) {
var _extends2, _extends3;
var state = _ref.state,
published = _ref.published;
start(timingsKey);
var updatedDroppables = updateDroppables({
modified: published.modified,
existing: state.dimensions.droppables,
viewport: state.viewport
});
var draggables = updateDraggables({
updatedDroppables: updatedDroppables,
criticalId: state.critical.draggable.id,
existing: state.dimensions.draggables,
additions: published.additions,
removals: published.removals,
viewport: state.viewport
});
var critical = {
draggable: draggables[state.critical.draggable.id].descriptor,
droppable: updatedDroppables[state.critical.droppable.id].descriptor
};
var original = state.dimensions.draggables[critical.draggable.id];
var updated = draggables[critical.draggable.id];
var droppables = recomputePlaceholders({
draggable: updated,
draggables: draggables,
droppables: updatedDroppables,
previousImpact: state.impact,
impact: state.impact
});
var dimensions = {
draggables: draggables,
droppables: droppables
};
var _getDragPositions = getDragPositions({
initial: state.initial,
current: state.current,
oldClientBorderBoxCenter: original.client.borderBox.center,
newClientBorderBoxCenter: updated.client.borderBox.center,
viewport: state.viewport
}),
initial = _getDragPositions.initial,
current = _getDragPositions.current;
var _getHomeOnLift = getHomeOnLift({
draggable: updated,
home: dimensions.droppables[critical.droppable.id],
draggables: dimensions.draggables,
viewport: state.viewport
}),
homeImpact = _getHomeOnLift.impact,
onLift = _getHomeOnLift.onLift;
var impact = withNoAnimatedDisplacement(getDragImpact({
pageBorderBoxCenter: current.page.borderBoxCenter,
draggable: updated,
draggables: dimensions.draggables,
droppables: dimensions.droppables,
previousImpact: homeImpact,
viewport: state.viewport,
userDirection: state.userDirection,
onLift: onLift
}));
var isOrphaned = Boolean(state.movementMode === 'SNAP' && !whatIsDraggedOver(impact));
!!isOrphaned ? process.env.NODE_ENV !== "production" ? invariant(false, 'Dragging item no longer has a valid merge/destination after a dynamic update. This is not supported') : invariant(false) : void 0;
finish(timingsKey);
var draggingState = _extends({
phase: 'DRAGGING'
}, state, (_extends2 = {}, _extends2["phase"] = 'DRAGGING', _extends2.critical = critical, _extends2.current = current, _extends2.initial = initial, _extends2.impact = impact, _extends2.dimensions = dimensions, _extends2.onLift = onLift, _extends2.onLiftImpact = homeImpact, _extends2.forceShouldAnimate = false, _extends2));
if (state.phase === 'COLLECTING') {
return draggingState;
}
var dropPending = _extends({
phase: 'DROP_PENDING'
}, draggingState, (_extends3 = {}, _extends3["phase"] = 'DROP_PENDING', _extends3.reason = state.reason, _extends3.isWaiting = false, _extends3));
return dropPending;
});
var forward = {
vertical: 'down',
horizontal: 'right'
};
var backward = {
vertical: 'up',
horizontal: 'left'
};
var moveToNextCombine = (function (_ref) {
var isMovingForward = _ref.isMovingForward,
isInHomeList = _ref.isInHomeList,
draggable = _ref.draggable,
destination = _ref.destination,
originalInsideDestination = _ref.insideDestination,
previousImpact = _ref.previousImpact;
if (!destination.isCombineEnabled) {
return null;
}
if (previousImpact.merge) {
return null;
}
var location = previousImpact.destination;
!location ? process.env.NODE_ENV !== "production" ? invariant(false, 'Need a previous location to move from into a combine') : invariant(false) : void 0;
var cur