service2.js: Fix empty paragraph handling

This commit is contained in:
Simon Rozman 2024-05-22 11:03:54 +02:00
parent 6eac44165f
commit e0e9f1a651

View File

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