@cocreate/crdt 1.14.35 → 1.15.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -1,3 +1,24 @@
1
+ ## [1.15.1](https://github.com/CoCreate-app/CoCreate-crdt/compare/v1.15.0...v1.15.1) (2023-01-04)
2
+
3
+
4
+ ### Bug Fixes
5
+
6
+ * add cdn to demo ([26e4302](https://github.com/CoCreate-app/CoCreate-crdt/commit/26e43026f178a74437146087142ce7e9e5cf696e))
7
+ * string was returned as array in sttead of string ([e3314ba](https://github.com/CoCreate-app/CoCreate-crdt/commit/e3314ba703419f7a651d2572536c2891bbfac7c3))
8
+ * typo tex to text ([adb8052](https://github.com/CoCreate-app/CoCreate-crdt/commit/adb80523e2f9b5b3d3b6594af6d9435ea10b67ab))
9
+
10
+ # [1.15.0](https://github.com/CoCreate-app/CoCreate-crdt/compare/v1.14.35...v1.15.0) (2023-01-04)
11
+
12
+
13
+ ### Bug Fixes
14
+
15
+ * bump cdn to 1.39.4 ([0fb1769](https://github.com/CoCreate-app/CoCreate-crdt/commit/0fb176942d89c52f78b33c6fcdca2330e869cabc))
16
+
17
+
18
+ ### Features
19
+
20
+ * sync docName if on itit if server has changed ([a338782](https://github.com/CoCreate-app/CoCreate-crdt/commit/a3387820756d2a4ce4aa2bc97bf8011459b91b92))
21
+
1
22
  ## [1.14.35](https://github.com/CoCreate-app/CoCreate-crdt/compare/v1.14.34...v1.14.35) (2023-01-01)
2
23
 
3
24
 
package/demo/index.html CHANGED
@@ -3,16 +3,16 @@
3
3
  </head>
4
4
  <body>
5
5
 
6
- <!--<textarea collection="apple" document_id="6193e3f6cbf74500119cfae8" name='test' width="100%" height="300px"></textarea>-->
6
+ <textarea collection="apple" document_id="6193e3f6cbf74500119cfae8" name='test' width="100%" height="300px"></textarea>
7
7
  <h1 collection="apple" document_id="6193e3f6cbf74500119cfae8" name='test'></h1>
8
- <h5 collection="apple" document_id="6193e3f6cbf74500119cfae8" name='test' contenteditable></h5>
8
+ <!-- <h5 collection="apple" document_id="6193e3f6cbf74500119cfae8" name='test' contenteditable></h5> -->
9
9
  <!--<iframe collection="apples" document_id="602c4dadb7fb6c9da35ca126" name='html' width="100%" height="300px"></iframe>-->
10
10
 
11
11
  <br/>
12
12
  CoCreate.crdt.replaceText({
13
13
  collection: "apples",
14
14
  document_id: "602c4dadb7fb6c9da35ca126",
15
- name: "html",
15
+ name: "test",
16
16
  value: 'testing new value'
17
17
  })
18
18
  <br/>
@@ -39,8 +39,9 @@
39
39
  })
40
40
 
41
41
 
42
- <!--<script src="../dist/CoCreate-crdt.js"></script>-->
43
- <script src="https://cdn.cocreate.app/latest/CoCreate.min.js" ></script>
42
+ <!--<script src="../dist/CoCreate-crdt.js"></script>-->
43
+ <!-- <script src="../../../CoCreateJS/dist/CoCreate.js" ></script> -->
44
+ <script src="https://cdn.cocreate.app/latest/CoCreate.min.js" ></script>
44
45
  </body>
45
46
  </html>
46
47
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cocreate/crdt",
3
- "version": "1.14.35",
3
+ "version": "1.15.1",
4
4
  "description": "CoCreate crdt",
5
5
  "author": "CoCreate LLC",
6
6
  "license": "SEE LICENSE",
package/src/index.js CHANGED
@@ -21,30 +21,18 @@ async function getDoc(data) {
21
21
  try {
22
22
  if (['_id', 'organization_id'].includes(data.name))
23
23
  return
24
- let docName = generateDocName(data);
25
- let typeName = data.name;
24
+ let docName = getDocName(data);
26
25
  let doc = docs.get(docName);
27
26
 
28
27
  if (!doc) {
29
- let docNameMap = new Map();
30
- docs.set(docName, docNameMap);
28
+ docs.set(docName, new Map());
31
29
  doc = docs.get(docName);
32
30
  }
33
31
 
34
- let type = doc.get(typeName)
35
- if (!type) {
36
- let typeNameMap = new Map();
37
- doc.set(typeName, typeNameMap);
38
- type = doc.get(typeName)
39
- }
40
- if (!type.has('changeLog')) {
32
+ if (!doc.has('changeLog')) {
41
33
  let changeLog = [];
42
-
43
- let undoLog = new Map()
44
- type.set('undoLog', undoLog)
45
-
46
- let redoLog = new Map()
47
- type.set('redoLog', redoLog)
34
+ doc.set('undoLog', new Map())
35
+ doc.set('redoLog', new Map())
48
36
 
49
37
  if (data.read != 'false') {
50
38
  if (!data.newDocument) {
@@ -58,18 +46,18 @@ async function getDoc(data) {
58
46
  }]
59
47
  }
60
48
  });
61
- if (response.document.length && response.document[0][typeName]) {
62
- changeLog = response.document[0][typeName];
49
+ if (response.document && response.document[0] && response.document[0].changeLog) {
50
+ changeLog = response.document[0].changeLog;
63
51
  }
64
52
  }
65
- type.set('changeLog', changeLog);
53
+ doc.set('changeLog', changeLog);
66
54
  await generateText(data, true);
67
55
  }
68
56
  }
69
- else if (!type.has('text')){
57
+ else if (!doc.has('text')){
70
58
  await generateText(data, false);
71
59
  }
72
- return true;
60
+ return doc;
73
61
  }
74
62
  catch (e) {
75
63
  console.log('Invalid param', e);
@@ -82,9 +70,10 @@ String.prototype.customSplice = function (index, absIndex, string) {
82
70
 
83
71
  async function generateText(data, flag) {
84
72
  try {
85
- let name = docs.get(`${data.collection}${data.document_id}`).get(data.name);
73
+ let doc = docs.get(getDocName(data))
74
+
86
75
  let string = '';
87
- let changeLog = name.get('changeLog');
76
+ let changeLog = doc.get('changeLog');
88
77
  for (let change of changeLog) {
89
78
  if (change || change !== null ) {
90
79
  string = string.customSplice(change.start, change.length, change.value);
@@ -93,7 +82,7 @@ async function generateText(data, flag) {
93
82
  if (string === '' && data.read !== 'false') {
94
83
  string = await checkDb(data, flag);
95
84
  }
96
- name.set('text', string);
85
+ doc.set('text', string);
97
86
  return;
98
87
  }
99
88
  catch (e) {
@@ -123,10 +112,13 @@ async function checkDb(data, flag) {
123
112
  }
124
113
 
125
114
  function insertChange(data, flag) {
126
- let docName = generateDocName(data);
127
- let typeName = data.name;
128
- let type = 'insert';
129
-
115
+ let docName = getDocName(data);
116
+ let doc = docs.get(docName);
117
+ let changeLog = doc.get('changeLog');
118
+ if (!changeLog)
119
+ return
120
+
121
+ let type = 'insert';
130
122
  if (data.start == undefined) return;
131
123
  if (!data.value)
132
124
  type = 'delete';
@@ -142,11 +134,6 @@ function insertChange(data, flag) {
142
134
  type
143
135
  };
144
136
 
145
- let name = docs.get(docName).get(typeName);
146
- let changeLog = name.get('changeLog');
147
- if (!changeLog)
148
- return
149
-
150
137
  let lastChange = changeLog[changeLog.length - 1];
151
138
  if (lastChange && change.datetime && lastChange.datetime){
152
139
  if (change.datetime < lastChange.datetime){
@@ -175,20 +162,20 @@ function insertChange(data, flag) {
175
162
  console.log('null change')
176
163
  return
177
164
  }
178
- let string = name.get('text') || '';
165
+ let string = doc.get('text') || '';
179
166
  if (change.length > 0)
180
167
  change.removedValue = string.substr(change.start, change.length);
181
168
 
182
169
  if (flag == 'replace') {
183
170
  // ToDo get current string and create new changeLog array then push new change
184
171
  changeLog = [change];
185
- name.set('changeLog', changeLog)
172
+ doc.set('changeLog', changeLog)
186
173
  } else {
187
174
  changeLog.push(change);
188
175
  }
189
176
 
190
- name.set('text', string.customSplice(change.start, change.length, change.value));
191
- string = {string: name.get('text')};
177
+ doc.set('text', string.customSplice(change.start, change.length, change.value));
178
+ string = {string: doc.get('text')};
192
179
 
193
180
  if (!data.clientId){
194
181
  data['datetime'] = change.datetime;
@@ -216,22 +203,28 @@ function broadcastChange(data){
216
203
 
217
204
  function localChange(data, string) {
218
205
  const localChange = new CustomEvent('cocreate-crdt-update', {
219
- detail: { ...data, ...string },
206
+ detail: { ...data, string },
220
207
  });
221
208
  window.dispatchEvent(localChange);
222
209
  }
223
210
 
224
211
  function persistChange(data) {
225
- let docName = generateDocName(data);
226
- let typeName = data.name;
227
- let name = docs.get(docName).get(typeName);
228
- let changeLog = name.get('changeLog');
212
+ let docName = getDocName(data);
213
+ let doc = docs.get(docName);
214
+ let changeLog = doc.get('changeLog');
215
+ let text = doc.get('text');
229
216
  let Data = {
230
217
  collection: 'crdt-transactions',
231
218
  document: {
232
219
  _id: data.document_id,
233
220
  docName,
234
- [typeName]: changeLog
221
+ changeLog,
222
+ text,
223
+ crud: {
224
+ collection: data.collection,
225
+ document_id: data.document_id,
226
+ name: data.name
227
+ }
235
228
  },
236
229
  upsert: true,
237
230
  namespace: data.namespace,
@@ -246,14 +239,38 @@ function persistChange(data) {
246
239
 
247
240
  message.listen('crdt', function(response) {
248
241
  let data = response.data
249
- if (docs.get(`${data.collection}${data.document_id}`)){
250
- if (docs.get(`${data.collection}${data.document_id}`).get(data.name))
242
+ let docName = getDocName(data);
243
+ let doc = docs.get(docName);
244
+
245
+ if (doc){
251
246
  if (data.clientId !== clientId){
252
247
  insertChange(data);
253
248
  }
254
249
  }
255
250
  });
256
251
 
252
+ crud.listen('sync', function(data) {
253
+ if (data.collection.includes('crdt-transactions')) {
254
+ if (data.document && data.document[0]) {
255
+ let Data = data.document[0];
256
+ let docName = Data.docName;
257
+ let doc = docs.get(docName);
258
+ if (doc && Data.crud) {
259
+ Data.crud.value = Data.text
260
+ Data.crud.start = 0
261
+ Data.crud.length = doc.get('text').length
262
+
263
+ doc.set('changeLog', Data.changeLog)
264
+ doc.set('text', Data.text)
265
+ // ToDo: compare modified dates to check if arrays need to merged and orderd by date or if we just use server
266
+ localChange(Data.crud, Data.text)
267
+ console.log('crdtSync')
268
+ }
269
+ }
270
+ }
271
+ });
272
+
273
+
257
274
  /*
258
275
  crdt.getText({
259
276
  collection: 'modules',
@@ -263,11 +280,9 @@ crdt.getText({
263
280
  */
264
281
  async function getText(data) {
265
282
  try {
266
- let docName = generateDocName(data);
267
- let typeName = data.name;
268
283
  let doc = await getDoc(data);
269
284
  if (doc) {
270
- let value = docs.get(docName).get(typeName).get('text')
285
+ let value = doc.get('text')
271
286
  return value;
272
287
  }
273
288
  else {
@@ -367,12 +382,10 @@ function createChange(data, change){
367
382
  }
368
383
 
369
384
  function undoText(data){
370
- let docName = generateDocName(data);
371
- let typeName = data.name;
372
-
373
- let name = docs.get(docName).get(typeName);
374
- let changeLog = name.get('changeLog');
375
- let undoLog = name.get('undoLog')
385
+ let docName = getDocName(data);
386
+ let doc = docs.get(docName);
387
+ let changeLog = doc.get('changeLog');
388
+ let undoLog = doc.get('undoLog')
376
389
 
377
390
  for (let index = changeLog.length - 1; index >= 0; index--) {
378
391
  let change = Object.assign({}, changeLog[index]);
@@ -393,12 +406,10 @@ function undoText(data){
393
406
  }
394
407
 
395
408
  function redoText(data){
396
- let docName = generateDocName(data);
397
- let typeName = data.name;
398
-
399
- let name = docs.get(docName).get(typeName);
400
- let redoLog = name.get('redoLog')
401
- let undoLog = Array.from(name.get('undoLog').values());
409
+ let docName = getDocName(data);
410
+ let doc = docs.get(docName);
411
+ let redoLog = doc.get('redoLog')
412
+ let undoLog = Array.from(doc.get('undoLog').values());
402
413
 
403
414
  for (let index = undoLog.length - 1; index >= 0; index--) {
404
415
  let change = Object.assign({}, undoLog[index]);
@@ -419,9 +430,11 @@ function redoText(data){
419
430
 
420
431
  async function viewVersion(data) {
421
432
  try {
422
- let name = docs.get(`${data.collection}${data.document_id}`).get(data.name);
433
+ let docName = getDocName(data);
434
+ let doc = docs.get(docName);
435
+ let changeLog = doc.get('changeLog');
423
436
  let string = '';
424
- let changeLog = name.get('changeLog');
437
+
425
438
  let log = changeLog.slice(0, data.version)
426
439
  for (let change of log) {
427
440
  if (change || change !== null ) {
@@ -448,10 +461,8 @@ async function viewVersion(data) {
448
461
  // this.docs[docName].socket.awareness.off('change', this._awarenessListener);
449
462
  // }
450
463
 
451
- function generateDocName(data) {
452
- let docName = { collection: data.collection, document_id: data.document_id };
453
- return `${data.collection}${data.document_id}`;
454
- // return btoa(JSON.stringify(docName));
464
+ function getDocName(data) {
465
+ return `${data.collection}${data.document_id}${data.name}`;
455
466
  }
456
467
 
457
468
  export default { init, getText, updateText, replaceText, undoText, redoText, viewVersion };