Add keyboard shortcut navigation for grammar mistakes

- Implements Ctrl+Š/Đ keyboard shortcuts to navigate between grammar mistakes/highlights. #4
This commit is contained in:
Aljaž Grilc 2025-06-04 10:26:56 +02:00
parent c7c90101a2
commit 99db143007

View File

@ -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) {
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(e.shiftKey ? -1 : 1)
this.findNextMistake(-1)
}
break
case 'BracketRight':
if (e.ctrlKey) {
// Handle Ctrl + ] OR Ctrl + Đ
e.preventDefault()
e.stopPropagation()
this.findNextMistake(1)
}
break
default:
break
}
}