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

73.33% Statements 44/60
55.26% Branches 21/38
75% Functions 3/4
73.33% Lines 44/60

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 1451x 1x 1x 1x 1x 1x     1x                       1x                                 140x   20x 20x                   120x   20x 20x 20x 20x 20x 20x     20x 20x 20x       20x             20x       20x 20x 20x 20x     20x 20x   20x   20x     20x                                                       20x 16x 16x   16x   16x   128x 128x       128x     128x   16x 16x     16x      
import { doubleCheckResize } from './doubleCheckResize';
import { getGeneratedImageSize } from './generateImageSize';
import { ImageEditElementClass } from '../types/ImageEditElementClass';
import { isElementOfType, isNodeOfType, toArray } from 'roosterjs-content-model-dom';
import { updateHandleCursor } from './updateHandleCursor';
import { updateSideHandlesVisibility } from '../Resizer/updateSideHandlesVisibility';
import type { ImageEditOptions } from '../types/ImageEditOptions';
import type { ImageMetadataFormat } from 'roosterjs-content-model-types';
import {
    getPx,
    isASmallImage,
    isRTL,
    setFlipped,
    setSize,
    setWrapperSizeDimensions,
} from './imageEditUtils';
 
/**
 * @internal
 */
export function updateWrapper(
    editInfo: ImageMetadataFormat,
    options: ImageEditOptions,
    image: HTMLImageElement,
    clonedImage: HTMLImageElement,
    wrapper: HTMLSpanElement,
    resizers?: HTMLDivElement[],
    croppers?: HTMLDivElement[]
) {
    const {
        angleRad,
        bottomPercent,
        leftPercent,
        rightPercent,
        topPercent,
        flippedHorizontal,
        flippedVertical,
    } = editInfo;
 
    const generateImageSize = getGeneratedImageSize(editInfo, croppers && croppers?.length > 0);
    Iif (!generateImageSize) {
        return;
    }
    const {
        targetWidth,
        targetHeight,
        originalWidth,
        originalHeight,
        visibleWidth,
        visibleHeight,
    } = generateImageSize;
 
    const marginHorizontal = (targetWidth - visibleWidth) / 2;
    const marginVertical = (targetHeight - visibleHeight) / 2;
    const cropLeftPx = originalWidth * (leftPercent || 0);
    const cropRightPx = originalWidth * (rightPercent || 0);
    const cropTopPx = originalHeight * (topPercent || 0);
    const cropBottomPx = originalHeight * (bottomPercent || 0);
 
    // Update size and margin of the wrapper
    wrapper.style.margin = `${marginVertical}px ${marginHorizontal}px`;
    wrapper.style.transform = `rotate(${angleRad}rad)`;
    setWrapperSizeDimensions(wrapper, image, visibleWidth, visibleHeight);
 
    // Update the text-alignment to avoid the image to overflow if the parent element have align center or right
    // or if the direction is Right To Left
    Iif (isRTL(clonedImage)) {
        wrapper.style.textAlign = 'right';
        if (!croppers) {
            clonedImage.style.left = getPx(cropLeftPx);
            clonedImage.style.right = getPx(-cropRightPx);
        }
    } else {
        wrapper.style.textAlign = 'left';
    }
 
    // Update size of the image
    clonedImage.style.width = getPx(originalWidth);
    clonedImage.style.height = getPx(originalHeight);
    clonedImage.style.verticalAlign = 'bottom';
    clonedImage.style.position = 'absolute';
 
    //Update flip direction
    setFlipped(clonedImage.parentElement, flippedHorizontal, flippedVertical);
    const smallImage = isASmallImage(visibleWidth, visibleWidth);
 
    Eif (!croppers) {
        // For rotate/resize, set the margin of the image so that cropped part won't be visible
        clonedImage.style.margin = `${-cropTopPx}px 0 0 ${-cropLeftPx}px`;
    }
 
    Iif (croppers && croppers.length > 0) {
        const cropContainer = croppers[0];
        const cropOverlays = croppers.filter(
            cropper => cropper.className === ImageEditElementClass.CropOverlay
        );
        const cropHandles = toArray<HTMLElement>(
            cropContainer.querySelectorAll(`.${ImageEditElementClass.CropHandle}`)
        );
 
        setSize(
            cropContainer,
            cropLeftPx,
            cropTopPx,
            cropRightPx,
            cropBottomPx,
            undefined,
            undefined
        );
        setSize(cropOverlays[0], 0, 0, cropRightPx, undefined, undefined, cropTopPx);
        setSize(cropOverlays[1], undefined, 0, 0, cropBottomPx, cropRightPx, undefined);
        setSize(cropOverlays[2], cropLeftPx, undefined, 0, 0, undefined, cropBottomPx);
        setSize(cropOverlays[3], 0, cropTopPx, undefined, 0, cropLeftPx, undefined);
 
        if (angleRad !== undefined) {
            updateHandleCursor(cropHandles, angleRad);
        }
    }
 
    if (resizers) {
        const clientWidth = wrapper.clientWidth;
        const clientHeight = wrapper.clientHeight;
 
        doubleCheckResize(editInfo, options.preserveRatio || false, clientWidth, clientHeight);
 
        const resizeHandles = resizers
            .map(resizer => {
                const resizeHandle = resizer.firstElementChild;
                Eif (
                    isNodeOfType(resizeHandle, 'ELEMENT_NODE') &&
                    isElementOfType(resizeHandle, 'div')
                ) {
                    return resizeHandle;
                }
            })
            .filter(handle => !!handle) as HTMLDivElement[];
 
        Eif (angleRad !== undefined) {
            updateHandleCursor(resizeHandles, angleRad);
        }
 
        updateSideHandlesVisibility(resizeHandles, smallImage);
    }
}