From e4ba4dd3f160f796bdb8c97a316e4a7409b008a6 Mon Sep 17 00:00:00 2001 From: Aljaz Grilc Date: Thu, 5 Jun 2025 08:54:56 +0200 Subject: [PATCH] Refactor findNextMistake method to be static and its calls #4 --- service.js | 33 ++++++++++++++++++--------------- 1 file changed, 18 insertions(+), 15 deletions(-) diff --git a/service.js b/service.js index 70c2e5b..ebc978c 100644 --- a/service.js +++ b/service.js @@ -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) } /**