All files / roosterjs-content-model-plugins/lib/pluginUtils/DragAndDrop DragAndDropHelper.ts

94.92% Statements 56/59
73.53% Branches 25/34
100% Functions 13/13
94.64% Lines 53/56

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 150                                                            1x 1x               1x 1x                 2x       2x 2x 2x         2x             1x 167x 167x 167x                           167x 167x 167x 167x 167x 167x     167x 167x           1x 130x 130x     1x 2x     1x 2x 2x 2x     1x 132x 132x 132x     167x 2x 2x 2x 2x 2x     167x 2x 2x 2x 2x 2x       2x       167x 2x 2x 2x 2x     1x  
import type { Disposable } from '../Disposable';
import type { DragAndDropHandler } from './DragAndDropHandler';
 
/**
 * @internal
 */
interface MouseEventMoves {
    MOUSEDOWN: string;
    MOUSEMOVE: string;
    MOUSEUP: string;
}
 
/**
 * @internal
 */
interface MouseEventInfo extends MouseEventMoves {
    getPageXY: (e: MouseEvent) => number[];
}
 
/**
 * @internal
 * Compatible mouse event names for different platform
 */
interface TouchEventInfo extends MouseEventMoves {
    getPageXY: (e: TouchEvent) => number[];
}
 
/**
 * Generate event names and getXY function based on different platforms to be compatible with desktop and mobile browsers
 */
const MOUSE_EVENT_INFO_DESKTOP: MouseEventInfo = (() => {
    return {
        MOUSEDOWN: 'mousedown',
        MOUSEMOVE: 'mousemove',
        MOUSEUP: 'mouseup',
        getPageXY: getMouseEventPageXY,
    };
})();
 
const MOUSE_EVENT_INFO_MOBILE: TouchEventInfo = (() => {
    return {
        MOUSEDOWN: 'touchstart',
        MOUSEMOVE: 'touchmove',
        MOUSEUP: 'touchend',
        getPageXY: getTouchEventPageXY,
    };
})();
 
function getMouseEventPageXY(e: MouseEvent): [number, number] {
    return [e.pageX, e.pageY];
}
 
function getTouchEventPageXY(e: TouchEvent): [number, number] {
    let pageX = 0;
    let pageY = 0;
    Iif (e.targetTouches && e.targetTouches.length > 0) {
        const touch = e.targetTouches[0];
        pageX = touch.pageX;
        pageY = touch.pageY;
    }
    return [pageX, pageY];
}
 
/**
 * @internal
 * A helper class to help manage drag and drop to an HTML element
 */
export class DragAndDropHelper<TContext, TInitValue> implements Disposable {
    private initX: number = 0;
    private initY: number = 0;
    private initValue: TInitValue | undefined = undefined;
    private dndMouse: MouseEventInfo | TouchEventInfo;
 
    /**
     * Create a new instance of DragAndDropHelper class
     * @param trigger The trigger element. When user start drag on this element,
     * events will be fired to the handler object
     * @param context Context object that will be passed to handler function when event is fired,
     * so that the handler object knows which element it is triggered from.
     * @param onSubmit A callback that will be invoked when event handler in handler object returns true
     * @param handler The event handler object, see DragAndDropHandler interface for more information
     * @param zoomScale The zoom scale of the editor
     * @param forceMobile A boolean to force the use of touch controls for the helper
     */
    constructor(
        private trigger: HTMLElement,
        private context: TContext,
        private onSubmit: (context: TContext, trigger: HTMLElement) => void,
        private handler: DragAndDropHandler<TContext, TInitValue>,
        private zoomScale: number,
        forceMobile?: boolean
    ) {
        this.dndMouse = forceMobile ? MOUSE_EVENT_INFO_MOBILE : MOUSE_EVENT_INFO_DESKTOP;
        trigger.addEventListener(this.dndMouse.MOUSEDOWN, this.onMouseDown);
    }
 
    /**
     * Dispose this object, remove all event listeners that has been attached
     */
    dispose() {
        this.trigger.removeEventListener(this.dndMouse.MOUSEDOWN, this.onMouseDown);
        this.removeDocumentEvents();
    }
 
    public get mouseType(): string {
        return this.dndMouse == MOUSE_EVENT_INFO_MOBILE ? 'touch' : 'mouse';
    }
 
    private addDocumentEvents() {
        const doc = this.trigger.ownerDocument;
        doc.addEventListener(this.dndMouse.MOUSEMOVE, this.onMouseMove, true /*useCapture*/);
        doc.addEventListener(this.dndMouse.MOUSEUP, this.onMouseUp, true /*useCapture*/);
    }
 
    private removeDocumentEvents() {
        const doc = this.trigger.ownerDocument;
        doc.removeEventListener(this.dndMouse.MOUSEMOVE, this.onMouseMove, true /*useCapture*/);
        doc.removeEventListener(this.dndMouse.MOUSEUP, this.onMouseUp, true /*useCapture*/);
    }
 
    private onMouseDown = (e: Event) => {
        e.preventDefault();
        e.stopPropagation();
        this.addDocumentEvents();
        [this.initX, this.initY] = this.dndMouse.getPageXY(e as MouseEvent & TouchEvent);
        this.initValue = this.handler.onDragStart?.(this.context, e as MouseEvent);
    };
 
    private onMouseMove = (e: Event) => {
        e.preventDefault();
        const [pageX, pageY] = this.dndMouse.getPageXY(e as MouseEvent & TouchEvent);
        const deltaX = (pageX - this.initX) / this.zoomScale;
        const deltaY = (pageY - this.initY) / this.zoomScale;
        Eif (
            this.initValue &&
            this.handler.onDragging?.(this.context, e as MouseEvent, this.initValue, deltaX, deltaY)
        ) {
            this.onSubmit?.(this.context, this.trigger);
        }
    };
 
    private onMouseUp = (e: Event) => {
        e.preventDefault();
        this.removeDocumentEvents();
        Eif (this.handler.onDragEnd?.(this.context, e as MouseEvent, this.initValue)) {
            this.onSubmit?.(this.context, this.trigger);
        }
    };
}