Create XmlFragment clone / copy

Hello .So I want to make a copy of an existing XmlFragment .Lets say I have fragment1 and fragment2
,fragment1 is the one with the data .I want fragment2 to be a copy of fragment1.

`
fragment1 = ydoc.getXmlFragment(‘fragment1’)
fragment2 = ydoc.getXmlFragment(‘fragment2’)

fragment1.toArray().forEach((el,i)=>{
fragment2.insert(i,el) // or fragment2.push(el)
})
`

When I try this I’m getting the fallowing error :
TypeError: content.forEach is not a function
at typeListInsertGenericsAfter (yjs.mjs:4860)
at typeListInsertGenerics (yjs.mjs:4913)
at yjs.mjs:7095
at transact (yjs.mjs:3198)
at YXmlFragment.insert (yjs.mjs:7094)

2 Likes

Solved it. Saw the code of the clone() function and this worked :

fragment2.insert(0,fragment1.toArray().map(item=>item instanceof AbstractType ? item.clone() : item));

1 Like

Thank you so much for this, i was cloning the fragment but i couldn’t insert its children to another document fragment, your solution worked

1 Like

Also, thank you so much. I was running into the same exact problem, and this taught me I couldn’t just clone the Y.XmlFragment, I had to clone the Y.XmlElement’s inside of it.