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