]> BookStack Code Mirror - bookstack/blobdiff - resources/js/components/shortcuts.js
Dependancies: Updated PHP deps via composer
[bookstack] / resources / js / components / shortcuts.js
index 8e927e34cfcd05d2c371eca42ce1c1585fd84396..b22c467318e73f383259119d5b96126b8e39c98f 100644 (file)
@@ -18,24 +18,27 @@ export class Shortcuts extends Component {
         this.hintsShowing = false;
 
         this.hideHints = this.hideHints.bind(this);
+        this.hintAbortController = null;
 
         this.setupListeners();
     }
 
     setupListeners() {
         window.addEventListener('keydown', event => {
-            if (event.target.closest('input, select, textarea')) {
+            if (event.target.closest('input, select, textarea, .cm-editor')) {
                 return;
             }
 
-            this.handleShortcutPress(event);
-        });
-
-        window.addEventListener('keydown', event => {
             if (event.key === '?') {
-                const action = this.hintsShowing ? this.hideHints : this.showHints;
-                action();
+                if (this.hintsShowing) {
+                    this.hideHints();
+                } else {
+                    this.showHints();
+                }
+                return;
             }
+
+            this.handleShortcutPress(event);
         });
     }
 
@@ -111,10 +114,12 @@ export class Shortcuts extends Component {
             displayedIds.add(id);
         }
 
-        window.addEventListener('scroll', this.hideHints);
-        window.addEventListener('focus', this.hideHints);
-        window.addEventListener('blur', this.hideHints);
-        window.addEventListener('click', this.hideHints);
+        this.hintAbortController = new AbortController();
+        const signal = this.hintAbortController.signal;
+        window.addEventListener('scroll', this.hideHints, {signal});
+        window.addEventListener('focus', this.hideHints, {signal});
+        window.addEventListener('blur', this.hideHints, {signal});
+        window.addEventListener('click', this.hideHints, {signal});
 
         this.hintsShowing = true;
     }
@@ -149,12 +154,7 @@ export class Shortcuts extends Component {
     hideHints() {
         const wrapper = this.container.querySelector('.shortcut-container');
         wrapper.remove();
-
-        window.removeEventListener('scroll', this.hideHints);
-        window.removeEventListener('focus', this.hideHints);
-        window.removeEventListener('blur', this.hideHints);
-        window.removeEventListener('click', this.hideHints);
-
+        this.hintAbortController?.abort();
         this.hintsShowing = false;
     }