@cocreate/crdt 1.9.11 → 1.11.0

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.11.0](https://github.com/CoCreate-app/CoCreate-crdt/compare/v1.10.1...v1.11.0) (2022-06-11)
2
+
3
+
4
+ ### Features
5
+
6
+ * undo and redo text ([3d0c74e](https://github.com/CoCreate-app/CoCreate-crdt/commit/3d0c74ef80b2cf1e9cfa6557007ac40a5ddc4949))
7
+
8
+ ## [1.10.1](https://github.com/CoCreate-app/CoCreate-crdt/compare/v1.10.0...v1.10.1) (2022-05-23)
9
+
10
+
11
+ ### Bug Fixes
12
+
13
+ * bump all dependencies ([88fcdae](https://github.com/CoCreate-app/CoCreate-crdt/commit/88fcdae04ed2331258123e65d9ddc412bdeeff78))
14
+
15
+ # [1.10.0](https://github.com/CoCreate-app/CoCreate-crdt/compare/v1.9.11...v1.10.0) (2022-05-14)
16
+
17
+
18
+ ### Features
19
+
20
+ * function readDocumentList has been renamed to readDocuments ([6b7c24c](https://github.com/CoCreate-app/CoCreate-crdt/commit/6b7c24c360d4e74a82a68b9db0d9e5423b1142a1))
21
+
1
22
  ## [1.9.11](https://github.com/CoCreate-app/CoCreate-crdt/compare/v1.9.10...v1.9.11) (2022-05-06)
2
23
 
3
24
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cocreate/crdt",
3
- "version": "1.9.11",
3
+ "version": "1.11.0",
4
4
  "description": "CoCreate crdt",
5
5
  "author": "CoCreate LLC",
6
6
  "license": "SEE LICENSE",
@@ -43,9 +43,8 @@
43
43
  "webpack-log": "^3.0.1"
44
44
  },
45
45
  "dependencies": {
46
- "@cocreate/actions": "^1.3.33",
47
- "@cocreate/crud-client": "^1.4.47",
48
- "@cocreate/message-client": "^1.1.64",
49
- "@cocreate/utils": "^1.6.4"
46
+ "@cocreate/crud-client": "^1.6.2",
47
+ "@cocreate/message-client": "^1.1.69",
48
+ "@cocreate/utils": "^1.7.1"
50
49
  }
51
50
  }
package/src/index.js CHANGED
@@ -2,7 +2,6 @@
2
2
  import crud from '@cocreate/crud-client';
3
3
  import message from '@cocreate/message-client';
4
4
  import uuid from '@cocreate/uuid';
5
- import action from '@cocreate/actions';
6
5
 
7
6
  const docs = new Map();
8
7
  const clientId = config.clientId || window.CoCreateSockets.clientId || uuid.generate(12);
@@ -38,9 +37,15 @@ async function getDoc(info) {
38
37
  }
39
38
  if (!type.has('changeLog')) {
40
39
  let changeLog = [];
41
-
40
+
41
+ let undoLog = new Map()
42
+ type.set('undoLog', undoLog)
43
+
44
+ let redoLog = new Map()
45
+ type.set('redoLog', redoLog)
46
+
42
47
  if (info.read != 'false') {
43
- let response = await crud.readDocumentList({
48
+ let response = await crud.readDocuments({
44
49
  collection: "crdt-transactions",
45
50
  operator: {
46
51
  filters: [{
@@ -78,7 +83,10 @@ async function generateText(info, flag) {
78
83
  let string = '';
79
84
  let changeLog = name.get('changeLog');
80
85
  for (let change of changeLog) {
81
- string = string.customSplice(change.start, change.length, change.value);
86
+ if (change || change !== null ) {
87
+ string = string.customSplice(change.start, change.length, change.value);
88
+ }
89
+ // string = string.customSplice(change.start, change.length, change.value);
82
90
  }
83
91
  if (string === '' && info.read !== 'false'){
84
92
  string = await checkDb(info, flag);
@@ -105,7 +113,7 @@ async function checkDb(info, flag) {
105
113
  return string || '';
106
114
  }
107
115
 
108
- function insertChange(info, broadcast, flag) {
116
+ function insertChange(info, flag) {
109
117
  let docName = generateDocName(info);
110
118
  let typeName = info.name;
111
119
  let type = 'insert';
@@ -151,8 +159,15 @@ function insertChange(info, broadcast, flag) {
151
159
  }
152
160
  }
153
161
  }
154
- changeLog.push(change);
162
+ if (!change || change == null ) {
163
+ console.log('null change')
164
+ return
165
+ }
155
166
  let string = name.get('text') || '';
167
+ if (change.length > 0)
168
+ change.removedValue = string.substr(change.start, change.length);
169
+
170
+ changeLog.push(change);
156
171
  name.set('text', string.customSplice(change.start, change.length, change.value));
157
172
  string = {string: name.get('text')};
158
173
 
@@ -170,14 +185,6 @@ function insertChange(info, broadcast, flag) {
170
185
  persistChange(info);
171
186
  }
172
187
 
173
- function undoChange(){
174
-
175
- }
176
-
177
- function redoChange(){
178
-
179
- }
180
-
181
188
  function broadcastChange(info){
182
189
  message.send({
183
190
  room: "",
@@ -298,7 +305,7 @@ async function updateText(info, flag) {
298
305
  let doc = await getDoc(info);
299
306
  if (doc) {
300
307
 
301
- insertChange(info, broadcast, flag);
308
+ insertChange(info, flag);
302
309
 
303
310
  if (info.crud != 'false' && info.save != 'false') {
304
311
  let wholestring = await getText(info);
@@ -319,6 +326,76 @@ async function updateText(info, flag) {
319
326
  }
320
327
  }
321
328
 
329
+ function createChange(info, change){
330
+ if (change.value && change.length == 0) {
331
+ change.length = change.value.length
332
+ change.removedValue = change.value
333
+ change.value = '';
334
+ change.type = 'delete';
335
+ } else {
336
+ change.value = change.removedValue
337
+ change.length = 0;
338
+ change.type = 'insert';
339
+ }
340
+ info = {...info, ...change};
341
+ delete info.clientId
342
+ delete info.datetime
343
+ delete change.clientId
344
+ delete change.datetime
345
+ return {info, change}
346
+ }
347
+
348
+ function undoText(info){
349
+ let docName = generateDocName(info);
350
+ let typeName = info.name;
351
+
352
+ let name = docs.get(docName).get(typeName);
353
+ let changeLog = name.get('changeLog');
354
+ let undoLog = name.get('undoLog')
355
+
356
+ for (let index = changeLog.length - 1; index >= 0; index--) {
357
+ let change = Object.assign({}, changeLog[index]);
358
+ if (change && change.clientId == clientId){
359
+ let log = undoLog.get(index)
360
+ if (!log) {
361
+ if (log != 'undo') {
362
+ undoLog.set(index, 'undo')
363
+ change.index = changeLog.length += 1;
364
+ let updated = createChange(info, change);
365
+ undoLog.set(updated.change.index, updated.change)
366
+ updateText(updated.info)
367
+ return
368
+ }
369
+ }
370
+ }
371
+ }
372
+ }
373
+
374
+ function redoText(info){
375
+ let docName = generateDocName(info);
376
+ let typeName = info.name;
377
+
378
+ let name = docs.get(docName).get(typeName);
379
+ let redoLog = name.get('redoLog')
380
+ let undoLog = Array.from(name.get('undoLog').values());
381
+
382
+ for (let index = undoLog.length - 1; index >= 0; index--) {
383
+ let change = Object.assign({}, undoLog[index]);
384
+ if (change && change != 'undo') {
385
+ let log = redoLog.get(change.index)
386
+
387
+ if (!log) {
388
+ if (log != 'redo') {
389
+ let updated = createChange(info, change);
390
+ redoLog.set(updated.change.index, updated.change)
391
+ updateText(updated.info)
392
+ return
393
+ }
394
+ }
395
+ }
396
+ }
397
+ }
398
+
322
399
 
323
400
 
324
401
  // function deleteDoc(docName) {
@@ -338,20 +415,4 @@ function generateDocName(info) {
338
415
  // return btoa(JSON.stringify(docName));
339
416
  }
340
417
 
341
- action.init({
342
- name: "undo",
343
- endEvent: "undo",
344
- callback: (btn, data) => {
345
- undoChange(btn);
346
- }
347
- });
348
-
349
- action.init({
350
- name: "redo",
351
- endEvent: "redo",
352
- callback: (btn, data) => {
353
- redoChange(btn);
354
- }
355
- });
356
-
357
- export default { init, getText, updateText, replaceText };
418
+ export default { init, getText, updateText, replaceText, undoText, redoText };