All files / src/libs/Modals/CustomModal DraggableElement.ts

100% Statements 38/38
100% Branches 5/5
100% Functions 6/6
100% Lines 36/36

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 1221x 1x 1x                     1x         5x 5x 5x 5x 5x   5x                         5x 5x 5x             5x   5x           5x           5x                     2x 2x               3x 3x 3x 3x 3x                 3x 2x 2x 2x   2x 2x                     9x 4x 4x   4x 3x 3x          
import { Component } from 'obsidian';
import { ImplementsStatic } from 'src/classes/decorators/ImplementsStatic';
import { Register } from 'src/libs/DependencyInjection/decorators/Register';
import {
    IDraggableElement,
    IDraggableElement_,
} from './interfaces/IDraggableElement';
 
/**
 * Draggable element class.
 */
@Register('IDraggableElement_')
@ImplementsStatic<IDraggableElement_>()
export class DraggableElement implements IDraggableElement {
    private readonly _component: Component;
    private readonly _draggableElement: HTMLElement;
    private readonly _dragHandle: HTMLElement;
 
    private _isDragging = false;
    private _currentX = 0;
    private _currentY = 0;
    private _initialX = 0;
    private _initialY = 0;
 
    private _animationFrameId: number | null = null;
 
    /**
     * Creates a new instance of the draggable element.
     * @param draggableElement The element that should be draggable.
     * @param dragHandle The element that should be used as the drag handle.
     * @param component The component that the draggable element belongs to.
     */
    constructor(
        draggableElement: HTMLElement,
        dragHandle: HTMLElement,
        component: Component,
    ) {
        this._component = component;
        this._draggableElement = draggableElement;
        this._dragHandle = dragHandle;
    }
 
    /**
     * @inheritdoc
     */
    public enableDragging(): void {
        this._dragHandle.style.cursor = 'grab';
 
        this._component.registerDomEvent(
            this._dragHandle,
            'mousedown',
            this.onMouseDown.bind(this),
        );
 
        this._component.registerDomEvent(
            document,
            'mousemove',
            this.onMouseMove.bind(this),
        );
 
        this._component.registerDomEvent(
            document,
            'mouseup',
            this.onMouseUp.bind(this),
        );
    }
 
    /**
     * Update the position of the draggable element.
     */
    private updatePosition(): void {
        this._draggableElement.style.transform = `translate(${this._currentX}px, ${this._currentY}px)`;
        this._animationFrameId = null;
    }
 
    /**
     * On mouse down event => start dragging.
     * @param event The mouse event.
     */
    private onMouseDown(event: MouseEvent): void {
        if (event.target === this._dragHandle) {
            this._isDragging = true;
            this._initialX = event.clientX - this._currentX;
            this._initialY = event.clientY - this._currentY;
            this._dragHandle.style.cursor = 'grabbing'; // Change cursor to grabbing
        }
    }
 
    /**
     * On mouse move event => move the draggable element.
     * @param event The mouse event.
     */
    private onMouseMove(event: MouseEvent): void {
        if (this._isDragging) {
            event.preventDefault();
            this._currentX = event.clientX - this._initialX;
            this._currentY = event.clientY - this._initialY;
 
            if (this._animationFrameId === null) {
                this._animationFrameId = requestAnimationFrame(
                    this.updatePosition.bind(this),
                );
            }
        }
    }
 
    /**
     * On mouse up event => stop dragging.
     */
    private onMouseUp(): void {
        if (this._isDragging) {
            this._isDragging = false;
            this._dragHandle.style.cursor = 'grab'; // Change cursor back to default
 
            if (this._animationFrameId !== null) {
                cancelAnimationFrame(this._animationFrameId);
                this._animationFrameId = null;
            }
        }
    }
}
 
Zur TypeDoc-Dokumentation