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

97.83% Statements 45/46
80% Branches 40/50
100% Functions 9/9
97.83% Lines 45/46

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 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 1501x                         1x 115x           1x 53x                       1x 82x     82x 82x 82x           1x         28x 28x                 1x             31x 31x 6x 6x 2x 2x 2x   4x 4x 4x   25x 25x           1x                 1x 1x 1x 1x 1x 1x                 1x 13x   13x 13x       1x   12x         21x 21x           1x         25x   25x           25x          
import { MIN_HEIGHT_WIDTH } from '../constants/constants';
 
/**
 * @internal
 */
interface WrapperDimensions {
    height: number;
    width: number;
}
 
/**
 * @internal
 */
export function getPx(value: number): string {
    return value + 'px';
}
 
/**
 * @internal
 */
export function isASmallImage(widthPx: number, heightPx: number): boolean {
    return widthPx && heightPx && (widthPx < MIN_HEIGHT_WIDTH || heightPx < MIN_HEIGHT_WIDTH)
        ? true
        : false;
}
 
/**
 * @internal Calculate the rotated x and y distance for mouse moving
 * @param x Original x distance
 * @param y Original y distance
 * @param angle Rotated angle, in radian
 * @returns rotated x and y distances
 */
export function rotateCoordinate(x: number, y: number, angle: number): [number, number] {
    Iif (x == 0 && y == 0) {
        return [0, 0];
    }
    const hypotenuse = Math.sqrt(x * x + y * y);
    angle = Math.atan2(y, x) - angle;
    return [hypotenuse * Math.cos(angle), hypotenuse * Math.sin(angle)];
}
 
/**
 * @internal
 */
export function setFlipped(
    element: HTMLElement | null,
    flippedHorizontally?: boolean,
    flippedVertically?: boolean
) {
    Eif (element) {
        element.style.transform = `scale(${flippedHorizontally ? -1 : 1}, ${
            flippedVertically ? -1 : 1
        })`;
    }
}
 
/**
 * @internal
 */
export function setWrapperSizeDimensions(
    wrapper: HTMLElement,
    image: HTMLImageElement,
    width: number,
    height: number,
    isRotating: boolean
) {
    const hasBorder = image.style.borderStyle;
    if (hasBorder) {
        const borderWidth = image.style.borderWidth ? 2 * parseInt(image.style.borderWidth) : 2;
        if (isRotating) {
            wrapper.style.width = getPx(parseInt(image.style.width) + borderWidth);
            wrapper.style.height = getPx(parseInt(image.style.height) + borderWidth);
            return;
        }
        wrapper.style.width = getPx(width + borderWidth);
        wrapper.style.height = getPx(height + borderWidth);
        return;
    }
    wrapper.style.width = getPx(width);
    wrapper.style.height = getPx(height);
}
 
/**
 * @internal
 */
export function setSize(
    element: HTMLElement,
    left: number | undefined,
    top: number | undefined,
    right: number | undefined,
    bottom: number | undefined,
    width: number | undefined,
    height: number | undefined
) {
    element.style.left = left !== undefined ? getPx(left) : element.style.left;
    element.style.top = top !== undefined ? getPx(top) : element.style.top;
    element.style.right = right !== undefined ? getPx(right) : element.style.right;
    element.style.bottom = bottom !== undefined ? getPx(bottom) : element.style.bottom;
    element.style.width = width !== undefined ? getPx(width) : element.style.width;
    element.style.height = height !== undefined ? getPx(height) : element.style.height;
}
 
/**
 * @internal
 * Check if the current image was resized by the user
 * @param image the current image
 * @returns if the user resized the image, returns true, otherwise, returns false
 */
export function checkIfImageWasResized(image: HTMLImageElement): boolean {
    const { style } = image;
    const isMaxWidthInitial =
        style.maxWidth === '' || style.maxWidth === 'initial' || style.maxWidth === 'auto';
    if (
        isMaxWidthInitial &&
        (isFixedNumberValue(style.height) || isFixedNumberValue(style.width))
    ) {
        return true;
    } else {
        return false;
    }
}
 
function isFixedNumberValue(value: string | number) {
    const numberValue = typeof value === 'string' ? parseInt(value) : value;
    return !isNaN(numberValue);
}
 
/**
 * @internal
 */
export function getActualWrapperDimensions(
    image: HTMLImageElement,
    wrapperWidth: number,
    wrapperHeight: number
): WrapperDimensions {
    const hasBorder = image.style.borderStyle;
    const borderWidth =
        hasBorder && image.style.borderWidth
            ? 2 * parseInt(image.style.borderWidth)
            : hasBorder
            ? 2
            : 0;
 
    return {
        width: wrapperWidth - borderWidth,
        height: wrapperHeight - borderWidth,
    };
}