All files / roosterjs-content-model-plugins/lib/imageEdit/Rotator rotatorContext.ts

84.21% Statements 16/19
70.59% Branches 12/17
50% Functions 1/2
88.24% Lines 15/17

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 341x                 1x   18x 18x 18x 18x 18x 18x   18x 18x   18x 18x     18x 18x 18x            
import { DEFAULT_ROTATE_HANDLE_HEIGHT, DEG_PER_RAD } from '../constants/constants';
import type { ImageRotateMetadataFormat } from 'roosterjs-content-model-types';
import type { DragAndDropHandler } from '../../pluginUtils/DragAndDrop/DragAndDropHandler';
import type { DragAndDropContext } from '../types/DragAndDropContext';
 
/**
 * @internal
 * The rotate drag and drop handler
 */
export const Rotator: DragAndDropHandler<DragAndDropContext, ImageRotateMetadataFormat> = {
    onDragStart: ({ editInfo }) => ({ ...editInfo }),
    onDragging: ({ editInfo, options }, e, base, deltaX, deltaY) => {
        Eif (editInfo.heightPx) {
            const distance = editInfo.heightPx / 2 + DEFAULT_ROTATE_HANDLE_HEIGHT;
            const newX = distance * Math.sin(base.angleRad ?? 0) + deltaX;
            const newY = distance * Math.cos(base.angleRad ?? 0) - deltaY;
            let angleInRad = Math.atan2(newX, newY);
 
            Eif (!e.altKey && options && options.minRotateDeg !== undefined) {
                const angleInDeg = angleInRad * DEG_PER_RAD;
                const adjustedAngleInDeg =
                    Math.round(angleInDeg / options.minRotateDeg) * options.minRotateDeg;
                angleInRad = adjustedAngleInDeg / DEG_PER_RAD;
            }
 
            Eif (editInfo.angleRad != angleInRad) {
                editInfo.angleRad = angleInRad;
                return true;
            }
        }
        return false;
    },
};