@cocreate/crdt
Advanced tools
Comparing version 1.0.12 to 1.1.1
@@ -0,1 +1,15 @@ | ||
## [1.1.1](https://github.com/CoCreate-app/CoCreate-crdt/compare/v1.1.0...v1.1.1) (2021-05-12) | ||
### Bug Fixes | ||
* release issue ([fb6c729](https://github.com/CoCreate-app/CoCreate-crdt/commit/fb6c72915a571f30a21238985b632ccf21c52f65)) | ||
# [1.1.0](https://github.com/CoCreate-app/CoCreate-crdt/compare/v1.0.12...v1.1.0) (2021-05-12) | ||
### Features | ||
* add crud:flase option to not save toString to db ([3cd23b5](https://github.com/CoCreate-app/CoCreate-crdt/commit/3cd23b536effd1ba0244c2e066c63f156bfeb492)) | ||
## [1.0.12](https://github.com/CoCreate-app/CoCreate-crdt/compare/v1.0.11...v1.0.12) (2021-05-08) | ||
@@ -2,0 +16,0 @@ |
{ | ||
"name": "@cocreate/crdt", | ||
"version": "1.0.12", | ||
"version": "1.1.1", | ||
"description": "CoCreate crdt", | ||
@@ -5,0 +5,0 @@ "author": "CoCreate LLC", |
@@ -75,2 +75,2 @@ # CoCreate-crdt | ||
[The MIT License (MIT)](https://github.com/CoCreate-app/CoCreate-crdt/blob/master/LICENSE) | ||
[UNLICENSED](https://github.com/CoCreate-app/CoCreate-crdt/blob/master/LICENSE) |
@@ -154,2 +154,4 @@ import * as Y from 'yjs' | ||
// let is_crud = eventDelta.attributes && eventDelta.attributes.crud === false ? false : true; | ||
const wholestring = event.target.toString() | ||
@@ -185,12 +187,12 @@ const store_event = new CustomEvent('store-content-db', { | ||
if (is_save_value) { | ||
crud.updateDocument({ | ||
collection: info.collection, | ||
document_id: info.document_id, | ||
data: { | ||
[info.name]: wholestring | ||
}, | ||
metadata: 'yjs-change' | ||
}) | ||
} | ||
// if (is_save_value && is_crud) { | ||
// crud.updateDocument({ | ||
// collection: info.collection, | ||
// document_id: info.document_id, | ||
// data: { | ||
// [info.name]: wholestring | ||
// }, | ||
// metadata: 'yjs-change' | ||
// }) | ||
// } | ||
} else { | ||
@@ -197,0 +199,0 @@ |
375
src/index.js
@@ -8,175 +8,206 @@ import CoCreateYSocket from "./core.js" | ||
{ | ||
constructor(org, doc) { | ||
super(org, doc) | ||
} | ||
constructor(org, doc) { | ||
super(org, doc) | ||
} | ||
/* | ||
crdt.init({ | ||
collection: "module", | ||
document_id: "", | ||
name: "", | ||
element: dom_object, | ||
metadata: "xxxx" | ||
}) | ||
*/ | ||
init(info) { | ||
try { | ||
this.__validateKeysJson(info, ['collection', 'document_id', 'name']); | ||
const id = this.__getYDocId(info['collection'], info['document_id'], info['name']) | ||
/* | ||
crdt.init({ | ||
collection: "module", | ||
document_id: "", | ||
name: "", | ||
element: dom_object, | ||
metadata: "xxxx" | ||
}) | ||
*/ | ||
init(info) { | ||
try { | ||
this.__validateKeysJson(info, ['collection', 'document_id', 'name']); | ||
const id = this.__getYDocId(info['collection'], info['document_id'], info['name']) | ||
if (!id) return; | ||
const status = this.createDoc(id, info.element) | ||
console.log("InitCrdt") | ||
} catch(e) { | ||
console.log('Invalid param', e); | ||
} | ||
} | ||
if (!id) return; | ||
const status = this.createDoc(id, info.element) | ||
console.log("InitCrdt") | ||
} catch(e) { | ||
console.log('Invalid param', e); | ||
} | ||
} | ||
/*. init data function | ||
crdt.replaceText({ | ||
collection: "module", | ||
document_id: "", | ||
name: "", | ||
value: "", | ||
updateCrud: true | false, | ||
element: dom_object, | ||
metadata: "xxxx" | ||
}) | ||
*/ | ||
replaceText(info){ | ||
if (!info) return; | ||
const id = this.__getYDocId(info.collection, info.document_id, info.name) | ||
if (!id) return; | ||
/*. init data function | ||
crdt.replaceText({ | ||
collection: "module", | ||
document_id: "", | ||
name: "", | ||
value: "", | ||
updateCrud: true | false, | ||
element: dom_object, | ||
metadata: "xxxx" | ||
}) | ||
*/ | ||
replaceText(info){ | ||
if (!info) return; | ||
const id = this.__getYDocId(info.collection, info.document_id, info.name) | ||
if (!id) return; | ||
if (info.updateCrud != false) info.updateCrud = true; | ||
if (this.getType(id) ) { | ||
let oldData = this.getType(id).toString(); | ||
let textValue = info.value.toString(); | ||
if (oldData && oldData.length > 0) { | ||
this.deleteData(id, 0, Math.max(oldData.length, textValue.length)); | ||
} | ||
this.insertData(id, 0, textValue); | ||
} | ||
if (info.updateCrud) { | ||
crud.updateDocument({ | ||
collection: info.collection, | ||
document_id: info.document_id, | ||
data: {[info.name]: info.value}, | ||
element: info.element, | ||
metadata:info.metadata, | ||
namespace: info.namespace, | ||
room: info.room, | ||
broadcast: info.broadcast, | ||
upsert: info.upsert, | ||
broadcast_sender: info.broadcast_sender | ||
}) | ||
} | ||
} | ||
/* | ||
crdt.insertText({ | ||
collection: 'module_activities', | ||
document_id: '5e4802ce3ed96d38e71fc7e5', | ||
name: 'name', | ||
value: 'T', | ||
position: '8', | ||
attributes: {bold: true} | ||
}) | ||
*/ | ||
insertText(info) { | ||
try { | ||
this.__validateKeysJson(info,['collection','document_id','name','value','position']); | ||
let id = this.__getYDocId(info['collection'], info['document_id'], info['name']) | ||
if (id) { | ||
this.insertData(id, info['position'], info['value'].toString(), info['attributes']); | ||
} | ||
} | ||
catch (e) { | ||
console.error(e); | ||
} | ||
} | ||
/* | ||
crdt.deleteText({ | ||
collection: 'module_activities', | ||
document_id: '5e4802ce3ed96d38e71fc7e5', | ||
name: 'name', | ||
position: '8', | ||
length: 2, | ||
}) | ||
*/ | ||
deleteText(info) { | ||
try{ | ||
this.__validateKeysJson(info,['collection','document_id','name', 'position','length']); | ||
let id = this.__getYDocId(info['collection'], info['document_id'], info['name']) | ||
if (id) { | ||
this.deleteData(id, info['position'], info['length']); | ||
} | ||
} | ||
catch (e) { | ||
console.error(e); | ||
} | ||
} | ||
/* | ||
crdt.getText({ | ||
collection: 'module_activities', | ||
document_id: '5e4802ce3ed96d38e71fc7e5', | ||
name: 'name' | ||
}) | ||
*/ | ||
getText(info) { | ||
try{ | ||
this.__validateKeysJson(info,['collection','document_id','name']); | ||
let id = this.__getYDocId(info['collection'], info['document_id'], info['name']) | ||
if (id) { | ||
return this.getWholeString(id); | ||
} else { | ||
return ""; | ||
} | ||
} | ||
catch (e) { | ||
console.error(e); | ||
return ""; | ||
} | ||
} | ||
if (info.updateCrud != false) info.updateCrud = true; | ||
if (this.getType(id) ) { | ||
let oldData = this.getType(id).toString(); | ||
let textValue = info.value.toString(); | ||
if (oldData && oldData.length > 0) { | ||
this.deleteData(id, 0, Math.max(oldData.length, textValue.length)); | ||
} | ||
this.insertData(id, 0, textValue); | ||
} | ||
if (info.updateCrud) { | ||
crud.updateDocument({ | ||
collection: info.collection, | ||
document_id: info.document_id, | ||
data: {[info.name]: info.value}, | ||
element: info.element, | ||
metadata:info.metadata, | ||
namespace: info.namespace, | ||
room: info.room, | ||
broadcast: info.broadcast, | ||
upsert: info.upsert, | ||
broadcast_sender: info.broadcast_sender | ||
}) | ||
} | ||
} | ||
/* | ||
crdt.insertText({ | ||
collection: 'module_activities', | ||
document_id: '5e4802ce3ed96d38e71fc7e5', | ||
name: 'name', | ||
value: 'T', | ||
position: '8', | ||
attributes: {bold: true} | ||
}) | ||
*/ | ||
insertText(info) { | ||
try { | ||
this.__validateKeysJson(info,['collection','document_id','name','value','position']); | ||
let id = this.__getYDocId(info['collection'], info['document_id'], info['name']) | ||
if (id) { | ||
this.insertData(id, info['position'], info['value'].toString(), info['attributes']); | ||
let wholestring = this.getType(id).toString(); | ||
console.log(wholestring) | ||
if (info.crud != false) { | ||
crud.updateDocument({ | ||
collection: info.collection, | ||
document_id: info.document_id, | ||
data: { | ||
[info.name]: wholestring | ||
}, | ||
metadata: 'yjs-change' | ||
}) | ||
} | ||
} | ||
} | ||
catch (e) { | ||
console.error(e); | ||
} | ||
} | ||
/* | ||
crdt.deleteText({ | ||
collection: 'module_activities', | ||
document_id: '5e4802ce3ed96d38e71fc7e5', | ||
name: 'name', | ||
position: '8', | ||
length: 2, | ||
}) | ||
*/ | ||
deleteText(info) { | ||
try{ | ||
this.__validateKeysJson(info,['collection','document_id','name', 'position','length']); | ||
let id = this.__getYDocId(info['collection'], info['document_id'], info['name']) | ||
if (id) { | ||
this.deleteData(id, info['position'], info['length']); | ||
let wholestring = this.getType(id).toString(); | ||
console.log(wholestring) | ||
if (info.crud != false) { | ||
crud.updateDocument({ | ||
collection: info.collection, | ||
document_id: info.document_id, | ||
data: { | ||
[info.name]: wholestring | ||
}, | ||
metadata: 'yjs-change' | ||
}) | ||
} | ||
} | ||
} | ||
catch (e) { | ||
console.error(e); | ||
} | ||
} | ||
/* | ||
crdt.getText({ | ||
collection: 'module_activities', | ||
document_id: '5e4802ce3ed96d38e71fc7e5', | ||
name: 'name' | ||
}) | ||
*/ | ||
getText(info) { | ||
try{ | ||
this.__validateKeysJson(info,['collection','document_id','name']); | ||
let id = this.__getYDocId(info['collection'], info['document_id'], info['name']) | ||
if (id) { | ||
return this.getWholeString(id); | ||
} else { | ||
return ""; | ||
} | ||
} | ||
catch (e) { | ||
console.error(e); | ||
return ""; | ||
} | ||
} | ||
/* | ||
crdt.getPosition(function(data)) | ||
crdt.getPosition(function(data){console.log(" EScuchando ahora ",data)}) | ||
*/ | ||
getPosition(callback){ | ||
if(typeof miFuncion === 'function') | ||
this.changeListenAwereness(callback); | ||
else | ||
console.error('Callback should be a function') | ||
} | ||
/* | ||
crdt.getPosition(function(data)) | ||
crdt.getPosition(function(data){console.log(" EScuchando ahora ",data)}) | ||
*/ | ||
getPosition(callback){ | ||
if(typeof miFuncion === 'function') | ||
this.changeListenAwereness(callback); | ||
else | ||
console.error('Callback should be a function') | ||
} | ||
__getYDocId(collection, document_id, name) { | ||
if (!crud.checkValue(collection) || | ||
!crud.checkValue(document_id) || | ||
!crud.checkValue(name)) | ||
{ | ||
return null; | ||
} | ||
return this.generateID(config.organization_Id, collection, document_id, name); | ||
} | ||
__validateKeysJson(json,rules){ | ||
let keys_json = Object.keys(json); | ||
keys_json.forEach(key=>{ | ||
const index = rules.indexOf(key); | ||
if(index != -1) | ||
rules.splice(index, 1); | ||
}); | ||
if( rules.length ) | ||
throw "Requires the following "+ rules.toString(); | ||
} | ||
__getYDocId(collection, document_id, name) { | ||
if (!crud.checkValue(collection) || | ||
!crud.checkValue(document_id) || | ||
!crud.checkValue(name)) | ||
{ | ||
return null; | ||
} | ||
return this.generateID(config.organization_Id, collection, document_id, name); | ||
} | ||
__validateKeysJson(json,rules){ | ||
let keys_json = Object.keys(json); | ||
keys_json.forEach(key=>{ | ||
const index = rules.indexOf(key); | ||
if(index != -1) | ||
rules.splice(index, 1); | ||
}); | ||
if( rules.length ) | ||
throw "Requires the following "+ rules.toString(); | ||
} | ||
} | ||
@@ -186,8 +217,8 @@ | ||
if (!window.CoCreateCrdt) { | ||
const crdtDoc = new Y.Doc(); | ||
CoCreateCrdt = new CoCreateCRDTClass(config.organization_Id, crdtDoc); | ||
window.Y = Y; | ||
window.CoCreateCrdt = CoCreateCrdt; | ||
const crdtDoc = new Y.Doc(); | ||
CoCreateCrdt = new CoCreateCRDTClass(config.organization_Id, crdtDoc); | ||
window.Y = Y; | ||
window.CoCreateCrdt = CoCreateCrdt; | ||
} else { | ||
CoCreateCrdt = window.CoCreateCrdt; | ||
CoCreateCrdt = window.CoCreateCrdt; | ||
} | ||
@@ -194,0 +225,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
285885
18
1158
75