Open lihebi opened 2 months ago
It doesn't seem to be a problem of yjs:
import * as Y from "yjs";
function test() {
const ydoc = new Y.Doc();
const yxml1 = ydoc.getXmlFragment("my xml fragment");
// construct: <heading level="1" textAlignment="left">h1</heading>
const elem = new Y.XmlElement("heading");
elem.setAttribute("level", "1");
elem.setAttribute("textAlignment", "left");
elem.insert(0, [new Y.XmlText("h1")]);
yxml1.insert(0, [elem]);
console.log("yxml1", yxml1.toString());
// clone it
const yxml2 = yxml1.clone();
const testMap = ydoc.getMap("test").set("1", yxml2);
console.log("yxml2", yxml2.toString());
}
Output:
yxml1 <heading level="1" textAlignment="left">h1</heading>
yxml2 <heading level="1" textAlignment="left">h1</heading>
Great find. Seems to be caused here:
It seems like Yjs doesn't allow number attributes, and the level
prop incorrectly gets set as a number value. I think we should fix this by making sure all node attributes are first converted to strings.
What are you working on btw?
Ah, I see. Thanks for the quick debugging!
I'm building a collaborative IDE, and I'm adding the feature to duplicate/copy/paste an editor. I could use editor.document, but yjs.clone would be simpler since the data is stored in Y.XmlFragment.
Describe the bug
When used with yjs, the Y.XmlFragment cannot be cloned correctly. The "level" attribute of "heading" is missing.
To Reproduce
Here's the sandbox: https://stackblitz.com/edit/github-wwxgqp?file=App.tsx
The code is a minimal BlockNote setup with yjs.
Type some different headings (level 1,2,3), the output yxml and cloned yxml2 are different, yxml2 doesn't have the level attributes.
yxml
yxml2