-
Notifications
You must be signed in to change notification settings - Fork 3.6k
/
getelementsintersectionrect.ts
54 lines (45 loc) · 1.64 KB
/
getelementsintersectionrect.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
/**
* @license Copyright (c) 2003-2023, CKSource Holding sp. z o.o. All rights reserved.
* For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
*/
/**
* @module utils/dom/getelementsintersectionrect
*/
import global from './global';
import Rect from './rect';
/**
* Calculates the intersection `Rect` of a given set of elements (and/or a `document`).
* Also, takes into account the viewport top offset configuration.
*
* @internal
* @param elements
* @param viewportTopOffset
*/
export default function getElementsIntersectionRect(
elements: Array<HTMLElement | Document>,
viewportTopOffset: number = 0
): Rect | null {
const elementRects = elements.map( element => {
// The document (window) is yet another "element", but cropped by the top offset.
if ( element instanceof Document ) {
const windowRect = new Rect( global.window );
windowRect.top += viewportTopOffset;
windowRect.height -= viewportTopOffset;
return windowRect;
} else {
return new Rect( element );
}
} );
let intersectionRect: Rect | null = elementRects[ 0 ];
// @if CK_DEBUG_GETELEMENTSINTERSECTIONRECT // for ( const rect of elementRects ) {
// @if CK_DEBUG_GETELEMENTSINTERSECTIONRECT // RectDrawer.draw( rect, {
// @if CK_DEBUG_GETELEMENTSINTERSECTIONRECT // outlineWidth: '1px', opacity: '.7', outlineStyle: 'dashed'
// @if CK_DEBUG_GETELEMENTSINTERSECTIONRECT // }, 'Scrollable element' );
// @if CK_DEBUG_GETELEMENTSINTERSECTIONRECT // }
for ( const rect of elementRects.slice( 1 ) ) {
if ( intersectionRect ) {
intersectionRect = intersectionRect.getIntersection( rect );
}
}
return intersectionRect;
}