I’m implementing an editor that supports math equations:
equation: {
group: "block",
content: "text*",
atom: true,
marks: "",
toDOM(node) {
return ["equation", 0]
},
parseDOM: [{ tag: "equation" }]
},
My implementation seems to work, but I ran into a strange issue, If I move the mouse cursor from an equation block to the above paragraph line, and if the cursor is at the beginning of that line and I hit “enter”.
I expect a new text line will be created above. Instead, a new equation block will be created mysteriously below. And the content of the equation block comes from the text line.
I captured the behavior in the following screen recoding.
notice that when I moved the cursor in front of the “sdf” line above the equation block and hit enter. The “sdf” line is disappeared and a new equation block is created with the content “sdf”.
The expected behavior would be a new empty line be created above “sdf”.
I debugged the prose mirror’s code a bit. It seems to be caused by a block-splitting logic. When the prose mirror determines that the text decorations are inconsistent, it will split the text block. But I don’t understand why it involves my equation block.
here is how I update my equation block
deselectNode() {
this.input.blur();
let nn = textSchema.text(this.input.value);
setTimeout(() => {
let tr = this.outerView.state.tr.replaceWith(this.getPos() + 1, this.getPos()+1 + this.node.nodeSize - 2, [nn]);
this.outerView.dispatch(tr);
}, 100);
}
When my equation editor is unfocused, I will update its textContent with
let tr = this.outerView.state.tr.replaceWith(this.getPos() + 1, this.getPos()+1 + this.node.nodeSize - 2, [nn]);
this.outerView.dispatch(tr);
I’m not confident with the above code. I tested that if I comment out the above code, the issue will be gone. But I can’t tell what’s wrong with my above code.
My intention is that given an equation block: xxx, I will select the entire text content xxx and replace it with the new content.
Anyway to solve this?