All files / roosterjs-content-model-plugins/lib/pluginUtils/Rect getIntersectedRect.ts

100% Statements 12/12
66.67% Branches 4/6
100% Functions 7/7
100% Lines 12/12

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 471x                                                       1x   2x   2x 2x   4x   2x 4x 4x 4x 4x     2x    
import { normalizeRect } from 'roosterjs-content-model-dom';
import type { Rect } from 'roosterjs-content-model-types';
 
/**
 * Get the intersected Rect of elements provided
 *
 * @example
 * The result of the following Elements Rects would be:
    {
        top: Element2.top,
        bottom: Element1.bottom,
        left: Element2.left,
        right: Element2.right
    }
    +-------------------------+
    | Element 1               |
    |   +-----------------+   |
    |   | Element2        |   |
    |   |                 |   |
    |   |                 |   |
    +-------------------------+
        |                 |
        +-----------------+
 * @internal
 * @param elements Elements to use.
 * @param additionalRects additional rects to use
 * @returns If the Rect is valid return the rect, if not, return null.
 */
export function getIntersectedRect(
    elements: HTMLElement[],
    IadditionalRects: Rect[] = []
): Rect | null {
    const rects = elements
        .map(element => normalizeRect(element.getBoundingClientRect()))
        .concat(additionalRects)
        .filter(element => !!element) as Rect[];
 
    const result: Rect = {
        top: Math.max(...rects.map(r => r.top)),
        bottom: Math.min(...rects.map(r => r.bottom)),
        left: Math.max(...rects.map(r => r.left)),
        right: Math.min(...rects.map(r => r.right)),
    };
 
    return result.top < result.bottom && result.left < result.right ? result : null;
}