Update mistake rendering and clearing functionality

This commit is contained in:
Aljaž Grilc 2024-02-06 10:11:18 +01:00
parent 41c6c22dfa
commit a9330e50b9

View File

@ -10,13 +10,13 @@ window.onload = () => {
children: [ children: [
{ {
elements: null, elements: null,
isProofed: false, isProofed: false
matches: [ // matches: [
{ // {
range: null, // range: null,
rects: null // rects: null
} // }
] // ]
} }
] ]
} }
@ -74,7 +74,7 @@ async function besProof(el) {
return response.json() return response.json()
}) })
.then(responseData => { .then(responseData => {
const matches = [{ range: null, rects: null, message: null }] let matches = []
responseData.matches.forEach(match => { responseData.matches.forEach(match => {
let range = document.createRange() let range = document.createRange()
@ -167,34 +167,63 @@ function besHandleBeforeInput(editorId, event) {
// Test if given block element has already been grammar-proofed. // Test if given block element has already been grammar-proofed.
function besIsProofed(el) { function besIsProofed(el) {
return el.getAttribute('besProofed') === 'true' if (el.id.startsWith('ed')) return
const editorId = el.parentElement.id
const editor = besEditors[editorId]
let filteredChildren = editor?.children.filter(child => child.elements === el)
return filteredChildren[0]?.isProofed
} }
// Mark given block element as grammar-proofed. // Mark given block element as grammar-proofed.
function besMarkProofed(el, matches) { function besMarkProofed(el, matches) {
const editorId = el.parentElement.id const editorId = el.parentElement.id
const editor = besEditors[editorId] const editor = besEditors[editorId]
editor.children.push({ let newChild = {
isProofed: true, isProofed: true,
elements: el, elements: el,
matches: matches matches: matches
}) }
// editor.children['matches'] = matches
editor.children = editor.children.map(child =>
child.elements === newChild.elements ? newChild : child
)
if (!editor.children.some(child => child.elements === newChild.elements)) {
editor.children.push(newChild)
}
} }
// Mark given block element as not grammar-proofed. // Mark given block element as not grammar-proofed.
function besClearProofed(el) { function besClearProofed(el) {
el?.removeAttribute('besProofed') const editorId = el.parentElement.id
const editor = besEditors[editorId]
let filteredChildren = editor.children.filter(child => child.elements === el)
if (filteredChildren.length) filteredChildren[0].isProofed = false
} }
// Remove all grammar mistakes markup for given block element. // Remove all grammar mistakes markup for given block element.
function besClearAllMistakes(el) { function besClearAllMistakes(el) {
for (const el2 of el.childNodes) { if (el.id.startsWith('ed')) return
if (el2.tagName === 'SPAN' && el2.classList.contains('bes-typo-mistake')) { const editorId = el.parentElement.id
el2.replaceWith(...el2.childNodes) const editor = besEditors[editorId]
el2.remove() let filteredChildren = editor?.children.filter(child => child.elements === el)
if (!filteredChildren.length) return
const correctionPanel = document.getElementById('correction-panel')
filteredChildren[0].matches.forEach(match => {
for (const rect of match.rects) {
for (let child of correctionPanel.children) {
let childRect = child.getBoundingClientRect()
const isWithinRect =
childRect.left >= rect.left &&
childRect.right <= rect.right &&
childRect.top >= rect.top &&
childRect.bottom <= rect.bottom + 20
if (isWithinRect) {
child.remove()
}
}
} }
} })
} }
// Adds grammar mistake markup // Adds grammar mistake markup