]> BookStack Code Mirror - bookstack/blob - resources/js/components/shortcuts.js
Ran eslint fix on existing codebase
[bookstack] / resources / js / components / shortcuts.js
1 import {Component} from './component';
2
3 function reverseMap(map) {
4     const reversed = {};
5     for (const [key, value] of Object.entries(map)) {
6         reversed[value] = key;
7     }
8     return reversed;
9 }
10
11 export class Shortcuts extends Component {
12
13     setup() {
14         this.container = this.$el;
15         this.mapById = JSON.parse(this.$opts.keyMap);
16         this.mapByShortcut = reverseMap(this.mapById);
17
18         this.hintsShowing = false;
19
20         this.hideHints = this.hideHints.bind(this);
21
22         this.setupListeners();
23     }
24
25     setupListeners() {
26         window.addEventListener('keydown', event => {
27             if (event.target.closest('input, select, textarea')) {
28                 return;
29             }
30
31             this.handleShortcutPress(event);
32         });
33
34         window.addEventListener('keydown', event => {
35             if (event.key === '?') {
36                 this.hintsShowing ? this.hideHints() : this.showHints();
37             }
38         });
39     }
40
41     /**
42      * @param {KeyboardEvent} event
43      */
44     handleShortcutPress(event) {
45         const keys = [
46             event.ctrlKey ? 'Ctrl' : '',
47             event.metaKey ? 'Cmd' : '',
48             event.key,
49         ];
50
51         const combo = keys.filter(s => Boolean(s)).join(' + ');
52
53         const shortcutId = this.mapByShortcut[combo];
54         if (shortcutId) {
55             const wasHandled = this.runShortcut(shortcutId);
56             if (wasHandled) {
57                 event.preventDefault();
58             }
59         }
60     }
61
62     /**
63      * Run the given shortcut, and return a boolean to indicate if the event
64      * was successfully handled by a shortcut action.
65      * @param {String} id
66      * @return {boolean}
67      */
68     runShortcut(id) {
69         const el = this.container.querySelector(`[data-shortcut="${id}"]`);
70         if (!el) {
71             return false;
72         }
73
74         if (el.matches('input, textarea, select')) {
75             el.focus();
76             return true;
77         }
78
79         if (el.matches('a, button')) {
80             el.click();
81             return true;
82         }
83
84         if (el.matches('div[tabindex]')) {
85             el.click();
86             el.focus();
87             return true;
88         }
89
90         console.error('Shortcut attempted to be ran for element type that does not have handling setup', el);
91
92         return false;
93     }
94
95     showHints() {
96         const wrapper = document.createElement('div');
97         wrapper.classList.add('shortcut-container');
98         this.container.append(wrapper);
99
100         const shortcutEls = this.container.querySelectorAll('[data-shortcut]');
101         const displayedIds = new Set();
102         for (const shortcutEl of shortcutEls) {
103             const id = shortcutEl.getAttribute('data-shortcut');
104             if (displayedIds.has(id)) {
105                 continue;
106             }
107
108             const key = this.mapById[id];
109             this.showHintLabel(shortcutEl, key, wrapper);
110             displayedIds.add(id);
111         }
112
113         window.addEventListener('scroll', this.hideHints);
114         window.addEventListener('focus', this.hideHints);
115         window.addEventListener('blur', this.hideHints);
116         window.addEventListener('click', this.hideHints);
117
118         this.hintsShowing = true;
119     }
120
121     /**
122      * @param {Element} targetEl
123      * @param {String} key
124      * @param {Element} wrapper
125      */
126     showHintLabel(targetEl, key, wrapper) {
127         const targetBounds = targetEl.getBoundingClientRect();
128
129         const label = document.createElement('div');
130         label.classList.add('shortcut-hint');
131         label.textContent = key;
132
133         const linkage = document.createElement('div');
134         linkage.classList.add('shortcut-linkage');
135         linkage.style.left = `${targetBounds.x}px`;
136         linkage.style.top = `${targetBounds.y}px`;
137         linkage.style.width = `${targetBounds.width}px`;
138         linkage.style.height = `${targetBounds.height}px`;
139
140         wrapper.append(label, linkage);
141
142         const labelBounds = label.getBoundingClientRect();
143
144         label.style.insetInlineStart = `${((targetBounds.x + targetBounds.width) - (labelBounds.width + 6))}px`;
145         label.style.insetBlockStart = `${(targetBounds.y + (targetBounds.height - labelBounds.height) / 2)}px`;
146     }
147
148     hideHints() {
149         const wrapper = this.container.querySelector('.shortcut-container');
150         wrapper.remove();
151
152         window.removeEventListener('scroll', this.hideHints);
153         window.removeEventListener('focus', this.hideHints);
154         window.removeEventListener('blur', this.hideHints);
155         window.removeEventListener('click', this.hideHints);
156
157         this.hintsShowing = false;
158     }
159
160 }