summaryrefslogtreecommitdiff
path: root/ext/mixed/js/hotkey-handler.js
blob: 01c33f5db0f8d3e68cdf85c6fc1c4a09a427acc5 (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
142
143
144
145
146
/*
 * Copyright (C) 2021  Yomichan Authors
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */

/* global
 * DocumentUtil
 */

/**
 * Class which handles hotkey events and actions.
 */
class HotkeyHandler extends EventDispatcher {
    /**
     * Creates a new instance of the class.
     * @param scope The scope required for hotkey definitions.
     */
    constructor(scope) {
        super();
        this._scope = scope;
        this._hotkeys = new Map();
        this._actions = new Map();
        this._eventListeners = new EventListenerCollection();
    }

    /**
     * Gets the scope required for the hotkey definitions.
     */
    get scope() {
        return this._scope;
    }

    /**
     * Sets the scope required for the hotkey definitions.
     */
    set scope(value) {
        this._scope = value;
    }

    /**
     * Begins listening to key press events in order to detect hotkeys.
     */
    prepare() {
        this._eventListeners.addEventListener(document, 'keydown', this._onKeyDown.bind(this), false);
    }

    /**
     * Stops listening to key press events.
     */
    cleanup() {
        this._eventListeners.removeAllEventListeners();
    }

    /**
     * Registers a set of actions that this hotkey handler supports.
     * @param actions An array of `[name, handler]` entries, where `name` is a string and `handler` is a function.
     */
    registerActions(actions) {
        for (const [name, handler] of actions) {
            this._actions.set(name, handler);
        }
    }

    /**
     * Registers a set of hotkeys
     * @param hotkeys An array of hotkey definitions of the format `{action, key, modifiers, scopes, enabled}`.
     * * `action` - a string indicating which action to perform.
     * * `key` - a keyboard key code indicating which key needs to be pressed.
     * * `modifiers` - an array of keyboard modifiers which also need to be pressed. Supports: `'alt', 'ctrl', 'shift', 'meta'`.
     * * `scopes` - an array of scopes for which the hotkey is valid. If this array does not contain `this.scope`, the hotkey will not be registered.
     * * `enabled` - a boolean indicating whether the hotkey is currently enabled.
     */
    registerHotkeys(hotkeys) {
        for (const {action, key, modifiers, scopes, enabled} of hotkeys) {
            if (
                enabled &&
                action !== '' &&
                scopes.includes(this._scope)
            ) {
                this._registerHotkey(key, modifiers, action);
            }
        }
    }

    /**
     * Removes all registered hotkeys.
     */
    clearHotkeys() {
        this._hotkeys.clear();
    }

    // Private

    _onKeyDown(e) {
        const key = e.code;
        const handlers = this._hotkeys.get(key);
        if (typeof handlers !== 'undefined') {
            const eventModifiers = DocumentUtil.getActiveModifiers(e);
            for (const {modifiers, action} of handlers) {
                if (!this._areSame(modifiers, eventModifiers)) { continue; }

                const actionHandler = this._actions.get(action);
                if (typeof actionHandler === 'undefined') { continue; }

                const result = actionHandler(e);
                if (result !== false) {
                    e.preventDefault();
                    return true;
                }
            }
        }
        this.trigger('keydownNonHotkey', e);
        return false;
    }

    _registerHotkey(key, modifiers, action) {
        let handlers = this._hotkeys.get(key);
        if (typeof handlers === 'undefined') {
            handlers = [];
            this._hotkeys.set(key, handlers);
        }
        handlers.push({modifiers: new Set(modifiers), action});
    }

    _areSame(set, array) {
        if (set.size !== array.length) { return false; }
        for (const value of array) {
            if (!set.has(value)) {
                return false;
            }
        }
        return true;
    }
}