Add MutationObserver for Editor Changes.
This allows detection and handling of deleted 'div' elements, thereby improving error correction and overall editor functionality.
This commit is contained in:
parent
06c88c37cd
commit
6bdb339b88
@ -17,6 +17,7 @@ class BesEditor {
|
|||||||
edit.addEventListener('scroll', () =>
|
edit.addEventListener('scroll', () =>
|
||||||
this.handleScrollEvent(this.el, this.scrollPanel)
|
this.handleScrollEvent(this.el, this.scrollPanel)
|
||||||
)
|
)
|
||||||
|
this.observeDeletions(this.el)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Register editor
|
// Register editor
|
||||||
@ -160,20 +161,44 @@ class BesEditor {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Marks section of text that is about to change as not-yet-grammar-proofed.
|
// Marks section of text that is about to change as not-yet-grammar-proofed.
|
||||||
handleBeforeInput(event) {
|
handleBeforeInput() {
|
||||||
if (this.timer) clearTimeout(this.timer)
|
if (this.timer) clearTimeout(this.timer)
|
||||||
let editor = this
|
let editor = this
|
||||||
this.timer = setTimeout(function () {
|
this.timer = setTimeout(function () {
|
||||||
editor.proof(editor.el)
|
editor.proof(editor.el)
|
||||||
}, 1000)
|
}, 1000)
|
||||||
|
}
|
||||||
|
|
||||||
// No need to invalidate elements after range.startContainer since they will
|
observeDeletions(editor) {
|
||||||
// get either deleted or replaced.
|
const observer = new MutationObserver(mutations => {
|
||||||
event
|
mutations.forEach(mutation => {
|
||||||
.getTargetRanges()
|
if (mutation.type === 'characterData') {
|
||||||
.forEach(range =>
|
this.clearProofed(this.getBlockParent(mutation.target))
|
||||||
this.clearProofed(this.getBlockParent(range.startContainer))
|
}
|
||||||
)
|
if (mutation.type === 'childList' && mutation.removedNodes.length) {
|
||||||
|
mutation.removedNodes.forEach(node => {
|
||||||
|
// TODO: This is a temporary solution. We need to handle all cases such as <p></p>, <br>, etc.
|
||||||
|
if (node.nodeName === 'DIV') {
|
||||||
|
this.children = this.children.filter(
|
||||||
|
child => child.elements !== node
|
||||||
|
)
|
||||||
|
} else console.log(node)
|
||||||
|
})
|
||||||
|
this.children.forEach(child => {
|
||||||
|
this.clearMistakeMarkup(child.elements)
|
||||||
|
child.matches.forEach(match => {
|
||||||
|
match.rects = this.addMistakeMarkup(match.range, this.scrollPanel)
|
||||||
|
})
|
||||||
|
})
|
||||||
|
}
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
observer.observe(editor, {
|
||||||
|
childList: true,
|
||||||
|
characterData: true,
|
||||||
|
subtree: true
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
// Test if given block element has already been grammar-proofed.
|
// Test if given block element has already been grammar-proofed.
|
||||||
|
Loading…
x
Reference in New Issue
Block a user