Revise rule classification

The LanguageTool browser plugin marks spelling mistakes with rule(s)
which ID starts with "MORFOLOGIK_RULE". This adopts the same logic.
This commit is contained in:
Simon Rozman 2024-06-10 16:18:54 +02:00
parent a23037d23a
commit 56a27c8432

View File

@ -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
)
})
})
}