diff --git a/service2.js b/service2.js index 00fd583..a6a5c27 100644 --- a/service2.js +++ b/service2.js @@ -176,13 +176,12 @@ class BesService { * Creates grammar mistake markup in DOM. * * @param {Range} range Grammar mistake range - * @returns {Object} Client rectangles and grammar mistake highlight elements + * @returns {Array} Grammar mistake highlight elements */ addMistakeMarkup(range) { - const clientRects = range.getClientRects() const scrollPanelRect = this.scrollPanel.getBoundingClientRect() let highlights = [] - for (let rect of clientRects) { + for (let rect of range.getClientRects()) { const highlight = document.createElement('div') highlight.classList.add('bes-typo-mistake') highlight.style.left = `${rect.left - scrollPanelRect.left}px` @@ -192,7 +191,7 @@ class BesService { this.scrollPanel.appendChild(highlight) highlights.push(highlight) } - return { clientRects, highlights } + return highlights } /** @@ -468,11 +467,8 @@ class BesTreeService extends BesService { } } - const { clientRects, highlights } = - this.addMistakeMarkup(range) matches.push({ - rects: clientRects, - highlights: highlights, + highlights: this.addMistakeMarkup(range), range: range, match: match }) @@ -539,10 +535,8 @@ class BesTreeService extends BesService { repositionAllMarkup() { this.results.forEach(result => { result.matches.forEach(match => { - const { clientRects, highlights } = this.addMistakeMarkup(match.range) - match.rects = clientRects if (match.highlights) match.highlights.forEach(h => h.remove()) - match.highlights = highlights + match.highlights = this.addMistakeMarkup(match.range) }) }) } @@ -696,24 +690,23 @@ class BesTreeService extends BesService { const el = this.getBlockParent(source.targetElement || source.target) if (!el) return - let resultMatch for (let result of this.results) { - let match = result.matches.find(match => { - // This way we can check if the click was inside the rectangle of the highlight even if some content was removed or added. - // We used to check if the click was inside the stored rectangle coordinates which is not reliable because the content can change. - // But this way we can check if the click was inside the rectangle of the highlight even if some content was changed. - // TODO: what to do with rectLists inside match.rects? - let rect = match.highlights[0].getBoundingClientRect() - return BesService.isPointInRect(source.clientX, source.clientY, rect) - }) - if (match) { - resultMatch = match - break + for (let m of result.matches) { + for (let h of m.highlights) { + if ( + BesService.isPointInRect( + source.clientX, + source.clientY, + h.getBoundingClientRect() + ) + ) { + this.popupCorrectionPanel(el, m, source) + return + } + } } } - - if (resultMatch) this.popupCorrectionPanel(el, resultMatch, source) - else BesPopup.hide() + BesPopup.hide() } } @@ -1179,11 +1172,8 @@ class BesPlainTextService extends BesService { nodes[nodeIdx].node, matchEnd - nodes[nodeIdx].start ) - const { clientRects, highlights } = - this.addMistakeMarkup(matchRange) matches.push({ - rects: clientRects, - highlights: highlights, + highlights: this.addMistakeMarkup(matchRange), range: matchRange, match: match }) @@ -1264,10 +1254,8 @@ class BesPlainTextService extends BesService { repositionAllMarkup() { this.results.forEach(result => { result.matches.forEach(match => { - const { clientRects, highlights } = this.addMistakeMarkup(match.range) - match.rects = clientRects if (match.highlights) match.highlights.forEach(h => h.remove()) - match.highlights = highlights + match.highlights = this.addMistakeMarkup(match.range) }) }) } @@ -1333,8 +1321,14 @@ class BesPlainTextService extends BesService { for (let result of this.results) { for (let m of result.matches) { - for (let r of m.rects) { - if (BesService.isPointInRect(source.clientX, source.clientY, r)) { + for (let h of m.highlights) { + if ( + BesService.isPointInRect( + source.clientX, + source.clientY, + h.getBoundingClientRect() + ) + ) { this.popupCorrectionPanel(result.range, m, source) return }