diff --git a/service.js b/service.js index 45af630..a1e9659 100644 --- a/service.js +++ b/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 { ) }) } - this.updateStatusIcon('bes-status-success') - this.statusDiv.title = 'BesService je registriran.' + 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 = '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.' - } } /**