X-Git-Url: http://source.bookstackapp.com/bookstack/blobdiff_plain/9135a85de4eef32a91c7a3ee0aa405ed454e5a4c..refs/heads/development:/resources/js/components/shortcuts.js diff --git a/resources/js/components/shortcuts.js b/resources/js/components/shortcuts.js index a87213b2e..8bf26fbb5 100644 --- a/resources/js/components/shortcuts.js +++ b/resources/js/components/shortcuts.js @@ -1,4 +1,4 @@ -import {Component} from "./component"; +import {Component} from './component'; function reverseMap(map) { const reversed = {}; @@ -8,7 +8,6 @@ function reverseMap(map) { return reversed; } - export class Shortcuts extends Component { setup() { @@ -19,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, .editor-container')) { return; } - this.handleShortcutPress(event); - }); - - window.addEventListener('keydown', event => { if (event.key === '?') { - this.hintsShowing ? this.hideHints() : this.showHints(); + if (this.hintsShowing) { + this.hideHints(); + } else { + this.showHints(); + } + return; } + + this.handleShortcutPress(event); }); } @@ -44,7 +46,6 @@ export class Shortcuts extends Component { * @param {KeyboardEvent} event */ handleShortcutPress(event) { - const keys = [ event.ctrlKey ? 'Ctrl' : '', event.metaKey ? 'Cmd' : '', @@ -90,7 +91,7 @@ export class Shortcuts extends Component { return true; } - console.error(`Shortcut attempted to be ran for element type that does not have handling setup`, el); + console.error('Shortcut attempted to be ran for element type that does not have handling setup', el); return false; } @@ -113,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; } @@ -135,10 +138,10 @@ export class Shortcuts extends Component { const linkage = document.createElement('div'); linkage.classList.add('shortcut-linkage'); - linkage.style.left = targetBounds.x + 'px'; - linkage.style.top = targetBounds.y + 'px'; - linkage.style.width = targetBounds.width + 'px'; - linkage.style.height = targetBounds.height + 'px'; + linkage.style.left = `${targetBounds.x}px`; + linkage.style.top = `${targetBounds.y}px`; + linkage.style.width = `${targetBounds.width}px`; + linkage.style.height = `${targetBounds.height}px`; wrapper.append(label, linkage); @@ -151,12 +154,8 @@ 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; } -} \ No newline at end of file + +}