aboutsummaryrefslogtreecommitdiff
path: root/src/client/views/GlobalKeyHandler.ts
blob: bb10f27cf2a0bce87c5dacd4628fb2d34c1f01a7 (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
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
import { UndoManager } from "../util/UndoManager";
import { SelectionManager } from "../util/SelectionManager";
import { CollectionDockingView } from "./collections/CollectionDockingView";
import { MainView } from "./MainView";
import { DragManager } from "../util/DragManager";
import { action } from "mobx";

const modifiers = ["Control", "Meta", "Shift", "Alt"];
type KeyHandler = (keycode: string) => KeyControlInfo;
type KeyControlInfo = {
    preventDefault: boolean,
    stopPropagation: boolean
};

export default class KeyManager {
    public static Handler: KeyManager;
    private mainView: MainView;
    private router = new Map<string, KeyHandler>();

    constructor(mainView: MainView) {
        this.mainView = mainView;
        this.router.set("0000", this.unmodified);
        this.router.set("0100", this.ctrl);
        this.router.set("0010", this.alt);
        this.router.set("1100", this.ctrl_shift);
    }

    public handle = (e: KeyboardEvent) => {
        let keyname = e.key.toLowerCase();
        this.handleGreedy(keyname);

        if (modifiers.includes(keyname)) {
            return;
        }

        let bit = (value: boolean) => value ? "1" : "0";
        let modifierIndex = bit(e.shiftKey) + bit(e.ctrlKey) + bit(e.altKey) + bit(e.metaKey);

        let handleConstrained = this.router.get(modifierIndex);
        if (!handleConstrained) {
            return;
        }

        let control = handleConstrained(keyname);

        control.stopPropagation && e.stopPropagation();
        control.preventDefault && e.preventDefault();
    }

    private handleGreedy = action((keyname: string) => {
        switch (keyname) {
        }
    });

    private unmodified = action((keyname: string) => {
        switch (keyname) {
            case "escape":
                if (CollectionDockingView.Instance.HasFullScreen()) {
                    CollectionDockingView.Instance.CloseFullScreen();
                } else {
                    SelectionManager.DeselectAll();
                }
                DragManager.AbortDrag();
                break;
        }

        return {
            stopPropagation: false,
            preventDefault: false
        };
    });

    private alt = action((keyname: string) => {
        let stopPropagation = true;
        let preventDefault = true;

        switch (keyname) {
            case "n":
                let toggle = this.mainView.addMenuToggle.current!;
                toggle.checked = !toggle.checked;
                break;
        }

        return {
            stopPropagation: stopPropagation,
            preventDefault: preventDefault
        };
    });

    private ctrl = action((keyname: string) => {
        let stopPropagation = true;
        let preventDefault = true;

        switch (keyname) {
            case "arrowright":
                this.mainView.mainFreeform && CollectionDockingView.Instance.AddRightSplit(this.mainView.mainFreeform, undefined);
                break;
            case "arrowleft":
                this.mainView.mainFreeform && CollectionDockingView.Instance.CloseRightSplit(this.mainView.mainFreeform);
                break;
            case "f":
                this.mainView.isSearchVisible = !this.mainView.isSearchVisible;
                break;
            case "o":
                let target = SelectionManager.SelectedDocuments()[0];
                target && target.fullScreenClicked();
                break;
            case "r":
                preventDefault = false;
                break;
            case "y":
                UndoManager.Redo();
                break;
            case "z":
                UndoManager.Undo();
                break;
        }

        return {
            stopPropagation: stopPropagation,
            preventDefault: preventDefault
        };
    });

    private ctrl_shift = action((keyname: string) => {
        let stopPropagation = true;
        let preventDefault = true;

        switch (keyname) {
            case "z":
                UndoManager.Redo();
                break;
        }

        return {
            stopPropagation: stopPropagation,
            preventDefault: preventDefault
        };
    });

}