forked from hello-pangea/dnd
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
87 additions
and
5 deletions.
There are no files selected for viewing
67 changes: 67 additions & 0 deletions
67
src/state/auto-scroller/fluid-scroller/get-iframe-scroll.ts
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,67 @@ | ||
import { BoxModel, getBox } from 'css-box-model'; | ||
import { DraggableDimension, DraggingState } from '../../../types'; | ||
import querySelectorAllIframe from '../../../view/iframe/query-selector-all-iframe'; | ||
import getScroll from './get-scroll'; | ||
import { AutoScrollerOptions } from './auto-scroller-options-types'; | ||
|
||
const resetToOrigin = (box: BoxModel) => ({ | ||
width: box.marginBox.width, | ||
height: box.marginBox.height, | ||
top: 0, | ||
left: 0, | ||
right: box.marginBox.width, | ||
bottom: box.marginBox.height, | ||
center: { | ||
x: box.marginBox.width / 2, | ||
y: box.marginBox.height / 2, | ||
}, | ||
x: 0, | ||
y: 0, | ||
}); | ||
|
||
/** | ||
* Get the scroll for a draggable inside an iframe | ||
* | ||
* - Since iframes are not fully managed by the state, we have to access the elements directly. | ||
* - This will not work with multiple draggable contexts | ||
*/ | ||
export default ({ | ||
draggable, | ||
dragStartTime, | ||
getAutoScrollerOptions, | ||
shouldUseTimeDampening, | ||
state, | ||
}: { | ||
state: DraggingState; | ||
draggable: DraggableDimension; | ||
dragStartTime: number; | ||
shouldUseTimeDampening: boolean; | ||
getAutoScrollerOptions: () => AutoScrollerOptions; | ||
}) => { | ||
const el = querySelectorAllIframe( | ||
`[data-rfd-draggable-id="${state.critical.draggable.id}"]`, | ||
)[0]; | ||
|
||
const win = el?.ownerDocument.defaultView || window; | ||
|
||
const isInIframe = win !== window; | ||
|
||
if (isInIframe) { | ||
const iframe = win.frameElement as HTMLIFrameElement; | ||
const viewportBox = getBox(iframe); | ||
const box = getBox(el); | ||
|
||
const change = getScroll({ | ||
dragStartTime, | ||
container: resetToOrigin(viewportBox), // Reset to origin because we don't care about position of the iframe | ||
subject: draggable.client.marginBox, | ||
center: box.borderBox.center, | ||
shouldUseTimeDampening, | ||
getAutoScrollerOptions, | ||
}); | ||
|
||
return { change, window: win }; | ||
} | ||
|
||
return null; | ||
}; |
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 |
---|---|---|
@@ -1,6 +1,6 @@ | ||
import type { Position } from 'css-box-model'; | ||
|
||
// Not guarenteed to scroll by the entire amount | ||
export default (change: Position): void => { | ||
window.scrollBy(change.x, change.y); | ||
// Not guaranteed to scroll by the entire amount | ||
export default (change: Position, win: Window = window): void => { | ||
win.scrollBy(change.x, change.y); | ||
}; |