Fix BesQuillService when pasting a simple block of text

This commit is contained in:
Aljaž Grilc 2025-04-16 09:27:20 +02:00
parent c27f9628f4
commit 5cbac62de3

View File

@ -1857,6 +1857,7 @@ class BesQuillService extends BesTreeService {
onChangeData(delta) {
let index = 0
let reproofNeeded = false
const affectedBlocks = new Set()
delta.ops.forEach(op => {
if (op.retain) {
@ -1866,11 +1867,12 @@ class BesQuillService extends BesTreeService {
}
} else if (op.insert) {
reproofNeeded = true
index += op.insert.length
index += typeof op.insert === 'string' ? op.insert.length : 1 // Handle string or embed
} else if (op.delete) {
reproofNeeded = true
}
})
if (reproofNeeded) {
const editorLength = this.quillInstance.getLength()
const clampedIndex = Math.min(index, editorLength - 1)
@ -1879,22 +1881,55 @@ class BesQuillService extends BesTreeService {
if (leaf) {
let domElement = leaf.domNode
// Traverse up to find the block element
while (domElement && !this.isBlockElement(domElement)) {
domElement = domElement.parentNode
}
if (domElement) {
this.clearProofing(domElement)
setTimeout(() => {
this.redrawAllMistakeMarkup()
this.scheduleProofing(1000)
}, 0)
}
if (domElement) affectedBlocks.add(domElement)
} else {
console.warn(
'Leaf is null. The index might be out of bounds or the editor content is empty.'
)
}
// Handle pasted content spanning multiple blocks
const selection = this.quillInstance.getSelection()
if (selection) {
const [startLeaf] = this.quillInstance.getLeaf(selection.index)
const [endLeaf] = this.quillInstance.getLeaf(
selection.index + selection.length
)
if (startLeaf && endLeaf) {
let startElement = startLeaf.domNode
let endElement = endLeaf.domNode
while (startElement && !this.isBlockElement(startElement)) {
startElement = startElement.parentNode
}
while (endElement && !this.isBlockElement(endElement)) {
endElement = endElement.parentNode
}
if (startElement && endElement) {
let currentElement = startElement
while (currentElement) {
affectedBlocks.add(currentElement)
if (currentElement === endElement) break
currentElement = currentElement.nextElementSibling
}
}
}
}
// Clear proofing for all affected blocks
affectedBlocks.forEach(block => this.clearProofing(block))
// Schedule proofing for all affected blocks
setTimeout(() => {
this.scheduleProofing(1000)
}, 0)
}
}