All files / roosterjs-content-model-plugins/lib/imageEdit/Resizer resizerContext.ts

70% Statements 21/30
81.08% Branches 30/37
50% Functions 1/2
69.23% Lines 18/26

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 591x                 1x   36x 36x             36x 36x 36x 36x   36x 36x     36x       36x 8x                 8x 8x             36x 36x 36x            
import { rotateCoordinate } from '../utils/imageEditUtils';
import type { DragAndDropHandler } from '../../pluginUtils/DragAndDrop/DragAndDropHandler';
import type { ImageResizeMetadataFormat } from 'roosterjs-content-model-types';
import type { DragAndDropContext } from '../types/DragAndDropContext';
 
/**
 * @internal
 * The resize drag and drop handler
 */
export const Resizer: DragAndDropHandler<DragAndDropContext, ImageResizeMetadataFormat> = {
    onDragStart: ({ editInfo }) => ({ ...editInfo }),
    onDragging: ({ x, y, editInfo, options }, e, base, deltaX, deltaY) => {
        Eif (
            base.heightPx &&
            base.widthPx &&
            options.minWidth !== undefined &&
            options.minHeight !== undefined
        ) {
            const ratio =
                base.widthPx > 0 && base.heightPx > 0 ? (base.widthPx * 1.0) / base.heightPx : 0;
            [deltaX, deltaY] = rotateCoordinate(deltaX, deltaY, editInfo.angleRad ?? 0);
            const horizontalOnly = x == '';
            const verticalOnly = y == '';
            const shouldPreserveRatio =
                !(horizontalOnly || verticalOnly) && (options.preserveRatio || e.shiftKey);
            let newWidth = horizontalOnly
                ? base.widthPx
                : Math.max(base.widthPx + deltaX * (x == 'w' ? -1 : 1), options.minWidth);
            let newHeight = verticalOnly
                ? base.heightPx
                : Math.max(base.heightPx + deltaY * (y == 'n' ? -1 : 1), options.minHeight);
 
            if (shouldPreserveRatio && ratio > 0) {
                Iif (ratio > 1) {
                    // first sure newHeight is right,calculate newWidth
                    newWidth = newHeight * ratio;
                    if (newWidth < options.minWidth) {
                        newWidth = options.minWidth;
                        newHeight = newWidth / ratio;
                    }
                } else {
                    // first sure newWidth is right,calculate newHeight
                    newHeight = newWidth / ratio;
                    Iif (newHeight < options.minHeight) {
                        newHeight = options.minHeight;
                        newWidth = newHeight * ratio;
                    }
                }
            }
 
            editInfo.widthPx = newWidth;
            editInfo.heightPx = newHeight;
            return true;
        } else {
            return false;
        }
    },
};