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: case Node.ELEMENT_NODE:
if (this.isBlockElement(node)) { if (this.isBlockElement(node)) {
// Block elements are grammar-checked independently. // Block elements are grammar-checked independently.
this.onProofing()
let result = this.getProofing(node) let result = this.getProofing(node)
if (result) { if (result) {
this.onProofing()
this.onProofingProgress(result.matches.length) this.onProofingProgress(result.matches.length)
return [{ text: `<${node.tagName}/>`, node: node, markup: true }] return [{ text: `<${node.tagName}/>`, node: node, markup: true }]
} }
@ -444,6 +444,7 @@ class BesDOMService extends BesTreeService {
data = data.concat(this.proofNode(el2, abortController)) data = data.concat(this.proofNode(el2, abortController))
if (data.some(x => !x.markup && !/^\s*$/.test(x.text))) { if (data.some(x => !x.markup && !/^\s*$/.test(x.text))) {
// Block element contains some text. // Block element contains some text.
this.onProofing()
const signal = abortController.signal const signal = abortController.signal
fetch( fetch(
new Request(besUrl + '/check', { new Request(besUrl + '/check', {
@ -948,73 +949,78 @@ class BesPlainTextService extends BesService {
paragraphRange.setEnd(nodes[nodeIdx].node, end - nodes[nodeIdx].start) paragraphRange.setEnd(nodes[nodeIdx].node, end - nodes[nodeIdx].start)
while (nodeIdx < nodes.length && nodes[nodeIdx].end <= end) nodeIdx++ while (nodeIdx < nodes.length && nodes[nodeIdx].end <= end) nodeIdx++
this.onProofing()
let result = this.getProofing(paragraphRange) let result = this.getProofing(paragraphRange)
if (result) { if (result) {
this.onProofing()
this.onProofingProgress(result.matches.length) this.onProofingProgress(result.matches.length)
continue continue
} }
const signal = this.abortController.signal let paragraphText = text.substring(start, end)
fetch( if (!/^\s*$/.test(paragraphText)) {
new Request(besUrl + '/check', { // Paragraph contains some text.
method: 'POST', this.onProofing()
headers: { const signal = this.abortController.signal
'Content-Type': 'application/x-www-form-urlencoded' fetch(
}, new Request(besUrl + '/check', {
body: new URLSearchParams({ method: 'POST',
format: 'plain', headers: {
data: JSON.stringify({ 'Content-Type': 'application/x-www-form-urlencoded'
annotation: [ },
{ body: new URLSearchParams({
text: text.substring(start, end) format: 'plain',
} data: JSON.stringify({
] annotation: [
}), {
language: this.hostElement.lang ? this.hostElement.lang : 'sl', text: paragraphText
level: 'picky' }
}) ]
}), }),
{ signal } language: this.hostElement.lang ? this.hostElement.lang : 'sl',
) level: 'picky'
.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
}) })
}),
{ signal }
)
.then(response => {
if (!response.ok) {
this.onFailedProofing(response)
throw new Error('Unexpected BesStr server response')
}
return response.json()
}) })
this.markProofed(paragraphRange, matches) .then(responseData => {
this.onProofingProgress(matches.length) let matches = []
}) responseData.matches.forEach(match => {
.catch(error => this.onFailedProofingResult(error)) 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) this.onProofingProgress(0)