implement clone for y documents

This commit is contained in:
filip 2021-12-01 16:55:20 +01:00
parent 294ba351b6
commit d9d1e69130
3 changed files with 3989 additions and 15 deletions

3956
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -227,6 +227,23 @@ export class Doc extends Observable {
return this.get(name, YXmlFragment)
}
/**
* Clone all shared data types into a fresh Y.Doc instance.
*
* @return {Doc}
*/
clone({ guid = random.uuidv4(), collectionid = null, gc = true, gcFilter = () => true, meta = null, autoLoad = false, shouldLoad = true } = {}){
const cloned = new Doc({guid, collectionid, gc, gcFilter, meta, autoLoad, shouldLoad});
this.share.forEach((value, key) => {
const clonedType = value.clone();
clonedType._integrate(this, null);
cloned.share.set(key, clonedType)
});
return cloned;
}
/**
* Converts the entire document into a js object, recursively traversing each yjs type
* Doesn't log types that have not been defined (using ydoc.getType(..)).

View File

@ -57,6 +57,37 @@ export const testToJSON = tc => {
}
}, 'doc.toJSON has array and recursive map')
}
/**
* @param {t.TestCase} tc
*/
export const testClone = tc => {
const doc = new Y.Doc();
const map = doc.getMap();
const map2 = new Y.Map();
map2.set("c","d");
map.set("a", "b");
map.set("b", map2);
const yxmlFragment = doc.getXmlFragment('xml')
const yxmlText = new Y.XmlText()
yxmlFragment.insert(0, [yxmlText])
yxmlFragment.insertAfter(yxmlText, [new Y.XmlElement('node-name')])
const ytext = doc.getText('text')
ytext.insert(0, 'abc') ;
ytext.format(1, 2, { bold: true }) ;
const yarray = doc.getArray('array')
yarray.insert(0, [1, 2, 3]);
yarray.delete(1, 1) ;
const clonedDoc = doc.clone();
for(const key of doc.share.keys()){
t.compare(doc.get(key).toJSON(), clonedDoc.get(key).toJSON());
}
}
/**
* @param {t.TestCase} tc