-
Notifications
You must be signed in to change notification settings - Fork 2.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
get drag impact refactor #92
Merged
Merged
Changes from 2 commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
0fead00
cleaning up get-drag-impact and simplifying how page properties are h…
alexreardon 6af5857
rewriting tests for get-drag-impact
alexreardon 2509780
more test cases
alexreardon 0ff22ce
continuing work on tests
alexreardon dd5c0bd
wip
alexreardon f714f62
wip
alexreardon 1392b33
tests for disabled droppable
alexreardon 20bed70
pulling home list and foriegn list functions apart
alexreardon 02ce65a
finalising refactor
alexreardon 31e0427
fixing unconnected draggable tests
alexreardon 2ab5768
Merge branch 'master' of github.com:atlassian/react-beautiful-dnd int…
alexreardon 23fcf78
removing temp file
alexreardon d87a782
laying out variables in a cleaner way
alexreardon 101a8c5
get-new-home-client-center now using new preset
alexreardon 9cc6f01
converting move-to-new-droppable tests to use new presets
alexreardon ba84b56
adding some variety to preset
alexreardon 5cfbc29
fixing flow issues
alexreardon File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,11 +8,10 @@ import type { DraggableId, | |
DroppableDimensionMap, | ||
DragImpact, | ||
DimensionFragment, | ||
WithinDroppable, | ||
Axis, | ||
Position, | ||
} from '../types'; | ||
import { patch } from './position'; | ||
import { add, subtract, patch } from './position'; | ||
import getDroppableOver from './get-droppable-over'; | ||
import getDraggablesInsideDroppable from './get-draggables-inside-droppable'; | ||
import noImpact, { noMovement } from './no-impact'; | ||
|
@@ -38,63 +37,57 @@ const getDroppablesScrollDiff = ({ | |
// It is the responsibility of this function | ||
// to return the impact of a drag | ||
|
||
type ImpactArgs = {| | ||
// used to lookup which droppable you are over | ||
page: Position, | ||
// used for comparison with other dimensions | ||
withinDroppable: WithinDroppable, | ||
// item being dragged | ||
draggableId: DraggableId, | ||
type Args = {| | ||
pageCenter: Position, | ||
draggable: DraggableDimension, | ||
homeDroppable: DroppableDimension, | ||
// all dimensions in system | ||
draggables: DraggableDimensionMap, | ||
droppables: DroppableDimensionMap | ||
|} | ||
|
||
export default ({ | ||
page, | ||
withinDroppable, | ||
draggableId, | ||
pageCenter, | ||
draggable, | ||
draggables, | ||
droppables, | ||
}: ImpactArgs): DragImpact => { | ||
const droppableId: ?DroppableId = getDroppableOver( | ||
page, droppables, | ||
}: Args): DragImpact => { | ||
const destinationId: ?DroppableId = getDroppableOver( | ||
pageCenter, droppables, | ||
); | ||
|
||
// not dragging over anything | ||
if (!droppableId) { | ||
if (!destinationId) { | ||
return noImpact; | ||
} | ||
|
||
const droppable: DroppableDimension = droppables[droppableId]; | ||
const axis: Axis = droppable.axis; | ||
const destination: DroppableDimension = droppables[destinationId]; | ||
const axis: Axis = destination.axis; | ||
|
||
if (!droppable.isEnabled) { | ||
return { | ||
movement: noMovement, | ||
direction: axis.direction, | ||
destination: null, | ||
}; | ||
if (!destination.isEnabled) { | ||
return noImpact; | ||
} | ||
|
||
const insideDroppable: DraggableDimension[] = getDraggablesInsideDroppable( | ||
droppable, | ||
const source: DroppableDimension = droppables[draggable.droppableId]; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. hopefully this is much clearer now |
||
const sourceScrollDiff: Position = subtract(source.scroll.current, source.scroll.initial); | ||
const originalCenter: Position = draggable.page.withoutMargin.center; | ||
// where the element actually is now | ||
const currentCenter: Position = add(pageCenter, sourceScrollDiff); | ||
const isWithinHomeDroppable = draggable.droppableId === destinationId; | ||
|
||
const insideDestination: DraggableDimension[] = getDraggablesInsideDroppable( | ||
destination, | ||
draggables, | ||
); | ||
|
||
const newCenter: Position = withinDroppable.center; | ||
const draggingDimension: DraggableDimension = draggables[draggableId]; | ||
const isWithinHomeDroppable = draggingDimension.droppableId === droppableId; | ||
|
||
// not considering margin so that items move based on visible edges | ||
const draggableCenter: Position = draggingDimension.page.withoutMargin.center; | ||
const isBeyondStartPosition: boolean = newCenter[axis.line] - draggableCenter[axis.line] > 0; | ||
const isBeyondStartPosition: boolean = currentCenter[axis.line] - originalCenter[axis.line] > 0; | ||
const shouldDisplaceItemsForward = isWithinHomeDroppable ? isBeyondStartPosition : false; | ||
|
||
const moved: DraggableId[] = insideDroppable | ||
const moved: DraggableId[] = insideDestination | ||
.filter((dimension: DraggableDimension): boolean => { | ||
// do not want to move the item that is dragging | ||
if (dimension === draggingDimension) { | ||
if (dimension === draggable) { | ||
return false; | ||
} | ||
|
||
|
@@ -104,30 +97,30 @@ export default ({ | |
// if they sit ahead of the dragging item | ||
if (!isWithinHomeDroppable) { | ||
const scrollDiff = getDroppablesScrollDiff({ | ||
sourceDroppable: droppables[draggingDimension.droppableId], | ||
destinationDroppable: droppable, | ||
sourceDroppable: droppables[draggable.droppableId], | ||
destinationDroppable: destination, | ||
line: axis.line, | ||
}); | ||
return (newCenter[axis.line] - scrollDiff) < fragment[axis.end]; | ||
return (currentCenter[axis.line] - scrollDiff) < fragment[axis.end]; | ||
} | ||
|
||
if (isBeyondStartPosition) { | ||
// 1. item needs to start ahead of the moving item | ||
// 2. the dragging item has moved over it | ||
if (fragment.center[axis.line] < draggableCenter[axis.line]) { | ||
if (fragment.center[axis.line] < originalCenter[axis.line]) { | ||
return false; | ||
} | ||
|
||
return newCenter[axis.line] > fragment[axis.start]; | ||
return currentCenter[axis.line] > fragment[axis.start]; | ||
} | ||
// moving backwards | ||
// 1. item needs to start behind the moving item | ||
// 2. the dragging item has moved over it | ||
if (draggableCenter[axis.line] < fragment.center[axis.line]) { | ||
if (originalCenter[axis.line] < fragment.center[axis.line]) { | ||
return false; | ||
} | ||
|
||
return newCenter[axis.line] < fragment[axis.end]; | ||
return currentCenter[axis.line] < fragment[axis.end]; | ||
}) | ||
.map((dimension: DraggableDimension): DroppableId => dimension.id); | ||
|
||
|
@@ -139,12 +132,14 @@ export default ({ | |
return isBeyondStartPosition ? moved.reverse() : moved; | ||
})(); | ||
|
||
const startIndex = insideDroppable.indexOf(draggingDimension); | ||
const index: number = (() => { | ||
// is over foreign list | ||
if (!isWithinHomeDroppable) { | ||
return insideDroppable.length - moved.length; | ||
return insideDestination.length - moved.length; | ||
} | ||
|
||
// is over home list | ||
const startIndex = insideDestination.indexOf(draggable); | ||
if (!moved.length) { | ||
return startIndex; | ||
} | ||
|
@@ -156,15 +151,8 @@ export default ({ | |
return startIndex - moved.length; | ||
})(); | ||
|
||
const distance = index !== startIndex ? | ||
// need to ensure that the whole item is moved | ||
draggingDimension.page.withMargin[axis.size] : | ||
0; | ||
|
||
const amount: Position = patch(axis.line, distance); | ||
|
||
const movement: DragMovement = { | ||
amount, | ||
amount: patch(axis.line, draggable.page.withMargin[axis.size]), | ||
draggables: ordered, | ||
isBeyondStartPosition: shouldDisplaceItemsForward, | ||
}; | ||
|
@@ -173,7 +161,7 @@ export default ({ | |
movement, | ||
direction: axis.direction, | ||
destination: { | ||
droppableId, | ||
droppableId: destinationId, | ||
index, | ||
}, | ||
}; | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Removed page from all these actions - easy to compute once with windowScroll in the reducer