aboutsummaryrefslogtreecommitdiff
path: root/src/client/util/SnappingManager.ts
blob: 48ec61f85c1c3185ae46501c352b0a34522dd7b6 (plain)
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
import { observable, action, runInAction, reaction, makeObservable } from 'mobx';
import { Doc } from '../../fields/Doc';

export class SnappingManager {
    private static _manager: SnappingManager;
    private static get Instance() {
        return SnappingManager._manager ?? new SnappingManager();
    }

    @observable _shiftKey = false;
    @observable _ctrlKey = false;
    @observable _isDragging: boolean = false;
    @observable _isResizing: Doc | undefined = undefined;
    @observable _canEmbed: boolean = false;
    @observable _horizSnapLines: number[] = [];
    @observable _vertSnapLines: number[] = [];

    private constructor() {
        SnappingManager._manager = this;
        makeObservable(this);
    }

    @action public static clearSnapLines = () => (this.Instance._vertSnapLines.length = this.Instance._horizSnapLines.length = 0);
    @action public static addSnapLines = (horizLines: number[], vertLines: number[]) => {
        this.Instance._horizSnapLines.push(...horizLines);
        this.Instance._vertSnapLines.push(...vertLines);
    };

    public static get HorizSnapLines() { return this.Instance._horizSnapLines; } // prettier-ignore
    public static get VertSnapLines()  { return this.Instance._vertSnapLines; } // prettier-ignore
    public static get ShiftKey()       { return this.Instance._shiftKey; } // prettier-ignore
    public static get CtrlKey()        { return this.Instance._ctrlKey; } // prettier-ignore
    public static get IsDragging()     { return this.Instance._isDragging; } // prettier-ignore
    public static get IsResizing()     { return this.Instance._isResizing; } // prettier-ignore
    public static get CanEmbed()       { return this.Instance._canEmbed; } // prettier-ignore
    public static SetShiftKey = (down: boolean) => runInAction(() => (this.Instance._shiftKey = down));
    public static SetCtrlKey = (down: boolean) => runInAction(() => (this.Instance._ctrlKey = down));
    public static SetIsDragging = (dragging: boolean) => runInAction(() => (this.Instance._isDragging = dragging));
    public static SetIsResizing = (doc: Doc | undefined) => runInAction(() => (this.Instance._isResizing = doc));
    public static SetCanEmbed = (canEmbed: boolean) => runInAction(() => (this.Instance._canEmbed = canEmbed));
}