diff --git a/examples/yjs-dist.esm b/examples/yjs-dist.esm index 4434e027..cecc1d9b 100644 --- a/examples/yjs-dist.esm +++ b/examples/yjs-dist.esm @@ -3,7 +3,7 @@ import Y from '../src/y.js' import yArray from '../../y-array/src/y-array.js' import yIndexedDB from '../../y-indexeddb/src/y-indexeddb.js' import yMap from '../../y-map/src/y-map.js' -import yText from '../../y-text/src/Text.js' +import yText from '../../y-text/src/y-text.js' import yXml from '../../y-xml/src/y-xml.js' import yWebsocketsClient from '../../y-websockets-client/src/y-websockets-client.js' diff --git a/src/Utils.js b/src/Utils.js index 823353b4..e0d3aa15 100644 --- a/src/Utils.js +++ b/src/Utils.js @@ -902,4 +902,34 @@ export default function Utils (Y) { } return args } + + Y.utils.writeObjectToYMap = function writeObjectToYMap (object, type) { + for (var key in object) { + var val = object[key] + if (Array.isArray(val)) { + type.set(key, Y.Array) + Y.utils.writeArrayToYArray(val, type.get(key)) + } else if (typeof val === 'object') { + type.set(key, Y.Map) + Y.utils.writeObjectToYMap(val, type.get(key)) + } else { + type.set(key, val) + } + } + } + + Y.utils.writeArrayToYArray = function writeArrayToYArray (array, type) { + for (var i = array.length - 1; i >= 0; i--) { + var val = array[i] + if (Array.isArray(val)) { + type.insert(0, [Y.Array]) + Y.utils.writeArrayToYArray(val, type.get(0)) + } else if (typeof val === 'object') { + type.insert(0, [Y.Map]) + Y.utils.writeObjectToYMap(val, type.get(0)) + } else { + type.insert(0, [val]) + } + } + } }