implement clone for y documents
This commit is contained in:
parent
294ba351b6
commit
d9d1e69130
3956
package-lock.json
generated
3956
package-lock.json
generated
File diff suppressed because it is too large
Load Diff
@ -227,6 +227,23 @@ export class Doc extends Observable {
|
|||||||
return this.get(name, YXmlFragment)
|
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
|
* 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(..)).
|
* Doesn't log types that have not been defined (using ydoc.getType(..)).
|
||||||
|
@ -57,6 +57,37 @@ export const testToJSON = tc => {
|
|||||||
}
|
}
|
||||||
}, 'doc.toJSON has array and recursive map')
|
}, '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
|
* @param {t.TestCase} tc
|
||||||
|
Loading…
x
Reference in New Issue
Block a user