Add keyboard shortcut navigation for grammar mistakes
- Implements Ctrl+Š/Đ keyboard shortcuts to navigate between grammar mistakes/highlights. #4
This commit is contained in:
parent
c7c90101a2
commit
99db143007
27
service.js
27
service.js
@ -60,11 +60,11 @@ class BesService {
|
|||||||
this.hostElement.setAttribute('data-gramm', 'false')
|
this.hostElement.setAttribute('data-gramm', 'false')
|
||||||
this.hostElement.setAttribute('data-gramm_editor', 'false')
|
this.hostElement.setAttribute('data-gramm_editor', 'false')
|
||||||
this.hostElement.setAttribute('data-enable-grammarly', '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.onScroll = this.onScroll.bind(this)
|
||||||
this.hostElement.addEventListener('scroll', this.onScroll)
|
this.hostElement.addEventListener('scroll', this.onScroll)
|
||||||
|
this.onShortcutNavigation = this.onShortcutNavigation.bind(this)
|
||||||
|
this.hostElement.addEventListener('keydown', this.onShortcutNavigation)
|
||||||
|
|
||||||
this.hostBoundingClientRect = this.hostElement.getBoundingClientRect()
|
this.hostBoundingClientRect = this.hostElement.getBoundingClientRect()
|
||||||
this.mutationObserver = new MutationObserver(this.onBodyMutate.bind(this))
|
this.mutationObserver = new MutationObserver(this.onBodyMutate.bind(this))
|
||||||
@ -111,6 +111,7 @@ class BesService {
|
|||||||
if (this.abortController) this.abortController.abort()
|
if (this.abortController) this.abortController.abort()
|
||||||
besServices = besServices.filter(item => item !== this)
|
besServices = besServices.filter(item => item !== this)
|
||||||
this.mutationObserver.disconnect()
|
this.mutationObserver.disconnect()
|
||||||
|
this.hostElement.removeEventListener('keydown', this.onShortcutNavigation)
|
||||||
this.hostElement.removeEventListener('scroll', this.onScroll)
|
this.hostElement.removeEventListener('scroll', this.onScroll)
|
||||||
this.hostElement.setAttribute('spellcheck', this.originalSpellcheck)
|
this.hostElement.setAttribute('spellcheck', this.originalSpellcheck)
|
||||||
this.hostElement.setAttribute('data-gramm', this.originalDataGramm)
|
this.hostElement.setAttribute('data-gramm', this.originalDataGramm)
|
||||||
@ -334,11 +335,27 @@ class BesService {
|
|||||||
*
|
*
|
||||||
* @param {Event} e
|
* @param {Event} e
|
||||||
*/
|
*/
|
||||||
onTab(e) {
|
onShortcutNavigation(e) {
|
||||||
if (e.key === 'Tab' && this.highlightElements.length) {
|
if (!this.highlightElements.length) return
|
||||||
|
switch (e.code) {
|
||||||
|
case 'BracketLeft':
|
||||||
|
if (e.ctrlKey) {
|
||||||
|
// Handle Ctrl + [ OR Ctrl + Š
|
||||||
e.preventDefault()
|
e.preventDefault()
|
||||||
e.stopPropagation()
|
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
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user