-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
correctly shifting draggable post update
- Loading branch information
1 parent
e802a06
commit fdef7ae
Showing
8 changed files
with
146 additions
and
23 deletions.
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 |
---|---|---|
@@ -0,0 +1,11 @@ | ||
// @flow | ||
import type { Position } from 'css-box-model'; | ||
import type { ItemPositions } from '../types'; | ||
import { add } from './position'; | ||
|
||
export default (client: ItemPositions, windowScroll: Position): ItemPositions => ({ | ||
selection: add(client.selection, windowScroll), | ||
borderBoxCenter: add(client.borderBoxCenter, windowScroll), | ||
offset: add(client.offset, windowScroll), | ||
}); | ||
|
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
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 |
---|---|---|
@@ -0,0 +1,96 @@ | ||
// @flow | ||
import invariant from 'invariant'; | ||
import type { Position } from 'css-box-model'; | ||
import { isEqual, subtract, add, negate } from '../position'; | ||
import getPageItemPositions from '../get-page-item-positions'; | ||
import type { | ||
DragPositions, | ||
Viewport, | ||
ItemPositions, | ||
} from '../../types'; | ||
|
||
type Args = {| | ||
initial: DragPositions, | ||
current: DragPositions, | ||
oldClientBorderBoxCenter: Position, | ||
newClientBorderBoxCenter: Position, | ||
viewport: Viewport, | ||
|} | ||
|
||
type Result = {| | ||
initial: DragPositions, | ||
current: DragPositions, | ||
|} | ||
|
||
const origin: Position = { x: 0, y: 0 }; | ||
|
||
export default ({ | ||
initial: oldInitial, | ||
current: oldCurrent, | ||
oldClientBorderBoxCenter, | ||
newClientBorderBoxCenter, | ||
viewport, | ||
}: Args): Result => { | ||
// Nothing needs to be changed | ||
if (isEqual(oldClientBorderBoxCenter, newClientBorderBoxCenter)) { | ||
return { initial: oldInitial, current: oldCurrent }; | ||
} | ||
|
||
// how much the dragging item has shifted | ||
const centerDiff: Position = subtract(newClientBorderBoxCenter, oldClientBorderBoxCenter); | ||
// const displacement: Position = negate(centerDiff); | ||
|
||
console.log('center offset', centerDiff); | ||
|
||
const clientSelection: Position = add( | ||
oldInitial.client.selection, centerDiff | ||
); | ||
console.log('old client selection', oldInitial.client.selection); | ||
console.log('new client selection', clientSelection); | ||
|
||
const initial: DragPositions = (() => { | ||
const client: ItemPositions = { | ||
selection: clientSelection, | ||
borderBoxCenter: newClientBorderBoxCenter, | ||
offset: origin, | ||
}; | ||
|
||
return { | ||
client, | ||
page: getPageItemPositions(client, viewport.scroll.initial), | ||
}; | ||
})(); | ||
|
||
const offset: Position = subtract( | ||
// The offset before the update | ||
oldCurrent.client.offset, | ||
// The change caused by the update | ||
centerDiff, | ||
); | ||
|
||
const current: DragPositions = (() => { | ||
const client: ItemPositions = { | ||
selection: add(initial.client.selection, offset), | ||
// this should be the same as the previous client borderBox center | ||
borderBoxCenter: add(initial.client.borderBoxCenter, offset), | ||
offset, | ||
}; | ||
|
||
invariant( | ||
isEqual(oldCurrent.client.borderBoxCenter, client.borderBoxCenter), | ||
` | ||
Incorrect new client center position. | ||
Expected ${JSON.stringify(oldCurrent.client.borderBoxCenter)} | ||
to equal ${JSON.stringify(client.borderBoxCenter)} | ||
` | ||
); | ||
|
||
return { | ||
client, | ||
page: getPageItemPositions(client, viewport.scroll.current), | ||
}; | ||
})(); | ||
|
||
return { current, initial }; | ||
}; | ||
|
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
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