My app had an odd bug. I’ve fixed it, but I’m confused about why things works this way. And given everything in ProseMirror is so carefully considered, I figure I might be missing something important.
I’m adding image support to my app, and have added a node type blockImage
, and a node-view that handles drag-and-drop of image files. I had a weird bug where, on deleting an image, whatever block was next in the doc was erroneously rendered using the image node view, even if it wasn’t an image. It looked like the block had vanished.
I checked out the guide and this is indeed expected. e.g. see this example:
let view = new EditorView({
state,
nodeViews: {
paragraph(node) { return new ParagraphView(node) }
}
})
class ParagraphView {
constructor(node) {
this.dom = this.contentDOM = document.createElement("p")
if (node.content.size == 0) this.dom.classList.add("empty")
}
update(node) {
if (node.type.name != "paragraph") return false // <<<<<<<<< The fix!
if (node.content.size > 0) this.dom.classList.remove("empty")
else this.dom.classList.add("empty")
return true
}
}
Adding a similar line to my image node view fixed the problem.
My question is (I’ll write it in terms of this example code, not my app), given the node view is registered specifically for paragraphs, why would .update
be called with some other node type? Surely the only sensible thing to do in that case would be to remove the node view from the dom?
Put another way, would one ever write a node view without that “if not paragraph return false”? Why?