Improve asynchronous handling and status rendering in proof function
The changes ensure that all recursive calls are properly awaited, leading to more predictable and reliable behavior. Additionally, the status rendering has been also improved with these changes.
This commit is contained in:
parent
5b13bd3d28
commit
cf8bc8afbd
49
service.js
49
service.js
@ -40,10 +40,6 @@ class BesService {
|
||||
static register(hostElement, textAreaService) {
|
||||
let service = new BesService(hostElement)
|
||||
service.proof(hostElement)
|
||||
if (service.statusIcon.classList.contains('bes-status-loading')) {
|
||||
service.updateStatusIcon('bes-status-success')
|
||||
service.statusDiv.title = 'BesService je registriran.'
|
||||
}
|
||||
if (textAreaService) service.textAreaService = textAreaService
|
||||
return service
|
||||
}
|
||||
@ -75,7 +71,7 @@ class BesService {
|
||||
* @param {Node} node DOM root node to proof
|
||||
* @returns {Array} Markup of text to proof using BesStr
|
||||
*/
|
||||
async proof(node) {
|
||||
async proof(node, isInitialCall = true) {
|
||||
this.updateStatusIcon('bes-status-loading')
|
||||
this.statusDiv.title = 'BesService je v procesu preverjanja pravopisa.'
|
||||
switch (node.nodeType) {
|
||||
@ -91,9 +87,10 @@ class BesService {
|
||||
]
|
||||
}
|
||||
this.clearMistakeMarkup(node)
|
||||
let data = []
|
||||
for (const el2 of node.childNodes)
|
||||
data = data.concat(await this.proof(el2))
|
||||
let dataPromises = Array.from(node.childNodes).map(child =>
|
||||
this.proof(child, false)
|
||||
)
|
||||
let data = (await Promise.all(dataPromises)).flat()
|
||||
if (data.some(x => !x.markup && !/^\s*$/.test(x.text))) {
|
||||
const requestData = {
|
||||
format: 'plain',
|
||||
@ -111,7 +108,7 @@ class BesService {
|
||||
body: new URLSearchParams(requestData)
|
||||
})
|
||||
const signal = this.abortController.signal
|
||||
fetch(request, { signal })
|
||||
await fetch(request, { signal })
|
||||
.then(response => {
|
||||
if (!response.ok) {
|
||||
this.updateStatusIcon('bes-status-error')
|
||||
@ -181,14 +178,27 @@ class BesService {
|
||||
)
|
||||
})
|
||||
}
|
||||
if (isInitialCall) {
|
||||
// TODO: Check count in 'makrofinančno' case.
|
||||
const count = this.children.reduce(
|
||||
(total, child) => total + child.matches.length,
|
||||
0
|
||||
)
|
||||
if (count > 0) {
|
||||
this.updateStatusIcon('bes-status-mistakes')
|
||||
this.statusDiv.title = 'Število napak: ' + count
|
||||
} else {
|
||||
this.updateStatusIcon('bes-status-success')
|
||||
this.statusDiv.title = 'BesService je registriran.'
|
||||
this.statusDiv.title = 'V besedilu ni napak.'
|
||||
}
|
||||
}
|
||||
return [{ text: '<' + node.tagName + '/>', node: node, markup: true }]
|
||||
} else {
|
||||
// Inline elements require no markup. Keep plain text only.
|
||||
let data = []
|
||||
for (const el2 of node.childNodes)
|
||||
data = data.concat(await this.proof(el2))
|
||||
let dataPromises = Array.from(node.childNodes).map(child =>
|
||||
this.proof(child, false)
|
||||
)
|
||||
let data = (await Promise.all(dataPromises)).flat()
|
||||
return data
|
||||
}
|
||||
|
||||
@ -288,19 +298,6 @@ class BesService {
|
||||
element: el,
|
||||
matches: matches
|
||||
})
|
||||
|
||||
// TODO: This also shows the count of mistakes that are not visible, meaning that they are hidden behind the shown ones.
|
||||
const count = this.children.reduce(
|
||||
(total, child) => total + child.matches.length,
|
||||
0
|
||||
)
|
||||
if (count > 0) {
|
||||
this.updateStatusIcon('bes-status-mistakes')
|
||||
this.statusDiv.title = 'Število napak: ' + count
|
||||
} else {
|
||||
this.updateStatusIcon('bes-status-success')
|
||||
this.statusDiv.title = 'V besedilu ni napak.'
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
Loading…
x
Reference in New Issue
Block a user