All files / roosterjs-content-model-plugins/lib/imageEdit/utils doubleCheckResize.ts

93.33% Statements 14/15
85.71% Branches 12/14
100% Functions 1/1
93.33% Lines 14/15

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                        1x           38x 19x     19x   19x 19x 19x 19x   19x 19x   19x 3x 2x   1x        
import type { ImageMetadataFormat } from 'roosterjs-content-model-types';
 
/**
 * @internal
 * Double check if the changed size can satisfy current width of container.
 * When resize an image and preserve ratio, its size can be limited by the size of container.
 * So we need to check the actual size and calculate the size again
 * @param editInfo Edit info of the image
 * @param preserveRatio Whether w/h ratio need to be preserved
 * @param actualWidth Actual width of the image after resize
 * @param actualHeight Actual height of the image after resize
 */
export function doubleCheckResize(
    editInfo: ImageMetadataFormat,
    preserveRatio: boolean,
    actualWidth: number,
    actualHeight: number
) {
    let { widthPx, heightPx } = editInfo;
    Iif (widthPx == undefined || heightPx == undefined) {
        return;
    }
    const ratio = heightPx > 0 ? widthPx / heightPx : 0;
 
    actualWidth = Math.floor(actualWidth);
    actualHeight = Math.floor(actualHeight);
    widthPx = Math.floor(widthPx);
    heightPx = Math.floor(heightPx);
 
    editInfo.widthPx = actualWidth;
    editInfo.heightPx = actualHeight;
 
    if (preserveRatio && ratio > 0 && (widthPx !== actualWidth || heightPx !== actualHeight)) {
        if (actualWidth < widthPx) {
            editInfo.heightPx = actualWidth / ratio;
        } else {
            editInfo.widthPx = actualHeight * ratio;
        }
    }
}