From 56a27c843250b8b8b2a7eab15b7a5ee1840ce601 Mon Sep 17 00:00:00 2001 From: Simon Rozman Date: Mon, 10 Jun 2024 16:18:54 +0200 Subject: [PATCH] Revise rule classification The LanguageTool browser plugin marks spelling mistakes with rule(s) which ID starts with "MORFOLOGIK_RULE". This adopts the same logic. --- service2.js | 43 +++++++++++++++++++------------------------ 1 file changed, 19 insertions(+), 24 deletions(-) diff --git a/service2.js b/service2.js index 21d34c1..0914204 100644 --- a/service2.js +++ b/service2.js @@ -48,16 +48,6 @@ class BesService { this.resizeObserver = new ResizeObserver(this.onResize.bind(this)) this.resizeObserver.observe(this.hostElement) - // TODO: Include missing rules - // Initial user feedback indicated a requirement for clear differentiation between spelling and grammar errors. - this.ruleTypes = { - BESANA_1: 'bes-spelling-mistake', - BESANA_2: 'bes-spelling-mistake', - BESANA_3: 'bes-spelling-mistake', - BESANA_4: 'bes-grammar-mistake', - BESANA_5: 'bes-spelling-mistake' - } - besServices.push(this) } @@ -184,14 +174,19 @@ class BesService { * Creates grammar mistake markup in DOM. * * @param {Range} range Grammar mistake range + * @param {String} ruleId Grammar mistake rule ID as reported by BesStr * @returns {Array} Grammar mistake highlight elements */ - addMistakeMarkup(range, ruleType) { + addMistakeMarkup(range, ruleId) { const scrollPanelRect = this.scrollPanel.getBoundingClientRect() let highlights = [] for (let rect of range.getClientRects()) { const highlight = document.createElement('div') - highlight.classList.add(ruleType) + highlight.classList.add( + ruleId.startsWith('MORFOLOGIK_RULE') + ? 'bes-spelling-mistake' + : 'bes-grammar-mistake' + ) highlight.style.left = `${rect.left - scrollPanelRect.left}px` highlight.style.top = `${rect.top - scrollPanelRect.top}px` highlight.style.width = `${rect.width}px` @@ -482,13 +477,10 @@ class BesTreeService extends BesService { } } - const ruleType = - this.ruleTypes[match.rule.id] || 'bes-spelling-mistake' matches.push({ - highlights: this.addMistakeMarkup(range, ruleType), + highlights: this.addMistakeMarkup(range, match.rule.id), range: range, - match: match, - ruleType: ruleType + match: match }) }) this.markProofed(node, matches) @@ -554,7 +546,10 @@ class BesTreeService extends BesService { this.results.forEach(result => { result.matches.forEach(match => { if (match.highlights) match.highlights.forEach(h => h.remove()) - match.highlights = this.addMistakeMarkup(match.range, match.ruleType) + match.highlights = this.addMistakeMarkup( + match.range, + match.match.rule.id + ) }) }) } @@ -1190,13 +1185,10 @@ class BesPlainTextService extends BesService { nodes[nodeIdx].node, matchEnd - nodes[nodeIdx].start ) - const ruleType = - this.ruleTypes[match.rule.id] || 'bes-spelling-mistake' matches.push({ - highlights: this.addMistakeMarkup(matchRange, ruleType), + highlights: this.addMistakeMarkup(matchRange, match.rule.id), range: matchRange, - match: match, - ruleType: ruleType + match: match }) }) this.markProofed(paragraphRange, matches) @@ -1276,7 +1268,10 @@ class BesPlainTextService extends BesService { this.results.forEach(result => { result.matches.forEach(match => { if (match.highlights) match.highlights.forEach(h => h.remove()) - match.highlights = this.addMistakeMarkup(match.range, match.ruleType) + match.highlights = this.addMistakeMarkup( + match.range, + match.match.rule.id + ) }) }) }