From e0e9f1a65116771e0706723a046661691afb654c Mon Sep 17 00:00:00 2001 From: Simon Rozman Date: Wed, 22 May 2024 11:03:54 +0200 Subject: [PATCH] service2.js: Fix empty paragraph handling --- service2.js | 128 +++++++++++++++++++++++++++------------------------- 1 file changed, 67 insertions(+), 61 deletions(-) diff --git a/service2.js b/service2.js index 425c941..b34c101 100644 --- a/service2.js +++ b/service2.js @@ -432,9 +432,9 @@ class BesDOMService extends BesTreeService { case Node.ELEMENT_NODE: if (this.isBlockElement(node)) { // Block elements are grammar-checked independently. - this.onProofing() let result = this.getProofing(node) if (result) { + this.onProofing() this.onProofingProgress(result.matches.length) return [{ text: `<${node.tagName}/>`, node: node, markup: true }] } @@ -444,6 +444,7 @@ class BesDOMService extends BesTreeService { data = data.concat(this.proofNode(el2, abortController)) if (data.some(x => !x.markup && !/^\s*$/.test(x.text))) { // Block element contains some text. + this.onProofing() const signal = abortController.signal fetch( new Request(besUrl + '/check', { @@ -948,73 +949,78 @@ class BesPlainTextService extends BesService { paragraphRange.setEnd(nodes[nodeIdx].node, end - nodes[nodeIdx].start) while (nodeIdx < nodes.length && nodes[nodeIdx].end <= end) nodeIdx++ - this.onProofing() let result = this.getProofing(paragraphRange) if (result) { + this.onProofing() this.onProofingProgress(result.matches.length) continue } - const signal = this.abortController.signal - fetch( - new Request(besUrl + '/check', { - method: 'POST', - headers: { - 'Content-Type': 'application/x-www-form-urlencoded' - }, - body: new URLSearchParams({ - format: 'plain', - data: JSON.stringify({ - annotation: [ - { - text: text.substring(start, end) - } - ] - }), - language: this.hostElement.lang ? this.hostElement.lang : 'sl', - level: 'picky' - }) - }), - { signal } - ) - .then(response => { - if (!response.ok) { - this.onFailedProofing(response) - throw new Error('Unexpected BesStr server response') - } - return response.json() - }) - .then(responseData => { - let matches = [] - responseData.matches.forEach(match => { - let matchRange = document.createRange() - let nodeIdx = 0, - matchStart = start + match.offset - while (nodeIdx < nodes.length && nodes[nodeIdx].end < matchStart) - nodeIdx++ - matchRange.setStart( - nodes[nodeIdx].node, - matchStart - nodes[nodeIdx].start - ) - let matchEnd = matchStart + match.length - while (nodeIdx < nodes.length && nodes[nodeIdx].end < matchEnd) - nodeIdx++ - matchRange.setEnd( - nodes[nodeIdx].node, - matchEnd - nodes[nodeIdx].start - ) - const { clientRects, highlights } = - this.addMistakeMarkup(matchRange) - matches.push({ - rects: clientRects, - highlights: highlights, - range: matchRange, - match: match + let paragraphText = text.substring(start, end) + if (!/^\s*$/.test(paragraphText)) { + // Paragraph contains some text. + this.onProofing() + const signal = this.abortController.signal + fetch( + new Request(besUrl + '/check', { + method: 'POST', + headers: { + 'Content-Type': 'application/x-www-form-urlencoded' + }, + body: new URLSearchParams({ + format: 'plain', + data: JSON.stringify({ + annotation: [ + { + text: paragraphText + } + ] + }), + language: this.hostElement.lang ? this.hostElement.lang : 'sl', + level: 'picky' }) + }), + { signal } + ) + .then(response => { + if (!response.ok) { + this.onFailedProofing(response) + throw new Error('Unexpected BesStr server response') + } + return response.json() }) - this.markProofed(paragraphRange, matches) - this.onProofingProgress(matches.length) - }) - .catch(error => this.onFailedProofingResult(error)) + .then(responseData => { + let matches = [] + responseData.matches.forEach(match => { + let matchRange = document.createRange() + let nodeIdx = 0, + matchStart = start + match.offset + while (nodeIdx < nodes.length && nodes[nodeIdx].end < matchStart) + nodeIdx++ + matchRange.setStart( + nodes[nodeIdx].node, + matchStart - nodes[nodeIdx].start + ) + let matchEnd = matchStart + match.length + while (nodeIdx < nodes.length && nodes[nodeIdx].end < matchEnd) + nodeIdx++ + matchRange.setEnd( + nodes[nodeIdx].node, + matchEnd - nodes[nodeIdx].start + ) + const { clientRects, highlights } = + this.addMistakeMarkup(matchRange) + matches.push({ + rects: clientRects, + highlights: highlights, + range: matchRange, + match: match + }) + }) + this.markProofed(paragraphRange, matches) + this.onProofingProgress(matches.length) + }) + .catch(error => this.onFailedProofingResult(error)) + } } this.onProofingProgress(0)