Open ezeidman opened 1 year ago
I ran into a variant of this that results in incorrect formatting instead of an error getting thrown. It's a similar idea where we convert a delta insert into an insert_text but the insert_text is no longer the correct operation to use if something else was inserted first at the same location.
This is my repro:
const paragraph: Paragraph = {
type: "paragraph",
elementId: "uuid-1",
children: [textNode("original text")],
};
function textNode(text: string): TextNode {
return {
type: "text",
text,
};
}
function boldTextNode(text: string): TextNode {
return {
type: "text",
text,
bold: true,
};
}
it("adds text nodes with different formatting", () => {
const { editor1, editor2, synchronize } = setupTreeTestEnvironment();
editor1.apply({
type: "insert_node",
node: paragraph,
path: [0],
});
synchronize(editor1, editor2);
editor1.apply({
type: "insert_node",
node: boldTextNode("new text"),
path: [0, 0],
});
editor1.apply({
type: "insert_node",
node: textNode("new text"),
path: [0, 0],
});
synchronize(editor1, editor2);
});
In this example all of the new text shows up as bold in editor 2
Hitting an issue when the same YEvent adds both text and an inline void node at the same time. The applyToSlate logic decides to convert the new text to an insert_text operation but that operation is invalid because the inline void gets added first with an insert_node operation.
The insert_text operation fails at:
https://github.com/ianstormtaylor/slate/blob/33a1e9b9239726cdbc1d65a664d88ca3d1c8a41e/packages/slate/src/interfaces/node.ts#L468
because the insertion point is no longer text after inserting the inline void node.
Full stacktrace:
Repro'd the issue in a simple test:
For some reason flushing changes between edits is a workaround but on the other hand we also found that this workaround no longer works if the Yjs updates are merged together. Not being able to merge updates is possible but also requires special handling and feels antithetical to the spirit of Yjs :)
It seems like when we decide to insert_text that's based only on what's currently at the insertion point and doesn't factor in what other changes in the same delta might have inserted (around https://github.com/BitPhinix/slate-yjs/blob/main/packages/core/src/applyToSlate/textEvent.ts#L163).