Refactor findNextMistake method to be static and its calls #4

This commit is contained in:
Aljaž Grilc 2025-06-05 08:54:56 +02:00
parent b99d233abc
commit e4ba4dd3f1

View File

@ -342,7 +342,7 @@ class BesService {
// Handle Ctrl + [ OR Ctrl + Š
e.preventDefault()
e.stopPropagation()
this.findNextMistake(-1)
BesService.findNextMistake(this, -1)
}
break
case 'BracketRight':
@ -350,7 +350,7 @@ class BesService {
// Handle Ctrl + ] OR Ctrl + Đ
e.preventDefault()
e.stopPropagation()
this.findNextMistake(1)
BesService.findNextMistake(this, 1)
}
break
case 'Enter':
@ -1034,7 +1034,7 @@ class BesService {
static arrowBtnNavigation(value, service) {
const direction = value === 'forward' ? 1 : value === 'back' ? -1 : 0
service.findNextMistake(direction)
BesService.findNextMistake(service, direction)
}
/**
@ -1180,32 +1180,35 @@ class BesService {
* @param {Number} direction Navigation direction: 1 for next, -1 for previous
* @returns
*/
findNextMistake(direction = 1) {
if (!this.sortedMatches || !this.sortedMatches.length) return
const active = this.highlightElements.find(({ matchSorted }) => matchSorted)
static findNextMistake(service, direction = 1) {
if (!service || !service.sortedMatches || !service.sortedMatches.length)
return
const active = service.highlightElements.find(
({ matchSorted }) => matchSorted
)
let current = -1
if (active && active.matchSorted) {
current = this.sortedMatches.findIndex(
current = service.sortedMatches.findIndex(
entry => entry.match === active.matchSorted.match
)
}
const len = this.sortedMatches.length
const len = service.sortedMatches.length
const next = (current + direction + len) % len
this.activeMatchIndex = next
const { el, match } = this.sortedMatches[next]
service.activeMatchIndex = next
const { el, match } = service.sortedMatches[next]
// Not the cleanest solution, but it is good enough for now
el.scrollIntoView({ behavior: 'instant' })
setTimeout(() => {
this.dismissPopup()
service.dismissPopup()
const popup = document.querySelector('bes-popup-el')
BesPopup.clearReplacements()
popup.setContent(el, match, this, this.isContentEditable())
this.highlightMistake(match)
popup.setContent(el, match, service, service.isContentEditable())
service.highlightMistake(match)
match.highlights.forEach(el => {
const clientY = `${el.y + 150}`
popup.show(el.x, clientY, this)
popup.show(el.x, clientY, service)
})
}, 150)
}
@ -1224,7 +1227,7 @@ class BesService {
firstReplacement.click()
} else if (replacementDiv.childElementCount > 1) {
firstReplacement.focus()
} else this.findNextMistake(1)
} else BesService.findNextMistake(this, 1)
}
/**