From 99db143007588f34f6a1509badbbb89648f4b5b2 Mon Sep 17 00:00:00 2001 From: Aljaz Grilc Date: Wed, 4 Jun 2025 10:26:56 +0200 Subject: [PATCH] Add keyboard shortcut navigation for grammar mistakes MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Implements Ctrl+Š/Đ keyboard shortcuts to navigate between grammar mistakes/highlights. #4 --- service.js | 31 ++++++++++++++++++++++++------- 1 file changed, 24 insertions(+), 7 deletions(-) diff --git a/service.js b/service.js index 670e27e..91f733f 100644 --- a/service.js +++ b/service.js @@ -60,11 +60,11 @@ class BesService { this.hostElement.setAttribute('data-gramm', 'false') this.hostElement.setAttribute('data-gramm_editor', 'false') this.hostElement.setAttribute('data-enable-grammarly', 'false') - this.onTab = this.onTab.bind(this) - this.hostElement.addEventListener('keydown', this.onTab) this.onScroll = this.onScroll.bind(this) this.hostElement.addEventListener('scroll', this.onScroll) + this.onShortcutNavigation = this.onShortcutNavigation.bind(this) + this.hostElement.addEventListener('keydown', this.onShortcutNavigation) this.hostBoundingClientRect = this.hostElement.getBoundingClientRect() this.mutationObserver = new MutationObserver(this.onBodyMutate.bind(this)) @@ -111,6 +111,7 @@ class BesService { if (this.abortController) this.abortController.abort() besServices = besServices.filter(item => item !== this) this.mutationObserver.disconnect() + this.hostElement.removeEventListener('keydown', this.onShortcutNavigation) this.hostElement.removeEventListener('scroll', this.onScroll) this.hostElement.setAttribute('spellcheck', this.originalSpellcheck) this.hostElement.setAttribute('data-gramm', this.originalDataGramm) @@ -334,11 +335,27 @@ class BesService { * * @param {Event} e */ - onTab(e) { - if (e.key === 'Tab' && this.highlightElements.length) { - e.preventDefault() - e.stopPropagation() - this.findNextMistake(e.shiftKey ? -1 : 1) + onShortcutNavigation(e) { + if (!this.highlightElements.length) return + switch (e.code) { + case 'BracketLeft': + if (e.ctrlKey) { + // Handle Ctrl + [ OR Ctrl + Š + e.preventDefault() + e.stopPropagation() + this.findNextMistake(-1) + } + break + case 'BracketRight': + if (e.ctrlKey) { + // Handle Ctrl + ] OR Ctrl + Đ + e.preventDefault() + e.stopPropagation() + this.findNextMistake(1) + } + break + default: + break } }