@cocreate/crdt 1.10.0 → 1.11.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 +21 -0
- package/package.json +4 -5
- package/src/index.js +92 -31
package/CHANGELOG.md
CHANGED
|
@@ -1,3 +1,24 @@
|
|
|
1
|
+
## [1.11.1](https://github.com/CoCreate-app/CoCreate-crdt/compare/v1.11.0...v1.11.1) (2022-06-12)
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
### Bug Fixes
|
|
5
|
+
|
|
6
|
+
* update dependencies ([89fbc88](https://github.com/CoCreate-app/CoCreate-crdt/commit/89fbc886a5eb564b4fe41a9ff016814e34c0f0c0))
|
|
7
|
+
|
|
8
|
+
# [1.11.0](https://github.com/CoCreate-app/CoCreate-crdt/compare/v1.10.1...v1.11.0) (2022-06-11)
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
### Features
|
|
12
|
+
|
|
13
|
+
* undo and redo text ([3d0c74e](https://github.com/CoCreate-app/CoCreate-crdt/commit/3d0c74ef80b2cf1e9cfa6557007ac40a5ddc4949))
|
|
14
|
+
|
|
15
|
+
## [1.10.1](https://github.com/CoCreate-app/CoCreate-crdt/compare/v1.10.0...v1.10.1) (2022-05-23)
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
### Bug Fixes
|
|
19
|
+
|
|
20
|
+
* bump all dependencies ([88fcdae](https://github.com/CoCreate-app/CoCreate-crdt/commit/88fcdae04ed2331258123e65d9ddc412bdeeff78))
|
|
21
|
+
|
|
1
22
|
# [1.10.0](https://github.com/CoCreate-app/CoCreate-crdt/compare/v1.9.11...v1.10.0) (2022-05-14)
|
|
2
23
|
|
|
3
24
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@cocreate/crdt",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.11.1",
|
|
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/
|
|
47
|
-
"@cocreate/
|
|
48
|
-
"@cocreate/
|
|
49
|
-
"@cocreate/utils": "^1.6.4"
|
|
46
|
+
"@cocreate/crud-client": "^1.7.0",
|
|
47
|
+
"@cocreate/message-client": "^1.1.70",
|
|
48
|
+
"@cocreate/utils": "^1.7.2"
|
|
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,7 +37,13 @@ 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
48
|
let response = await crud.readDocuments({
|
|
44
49
|
collection: "crdt-transactions",
|
|
@@ -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
|
-
|
|
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,
|
|
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
|
-
|
|
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,
|
|
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
|
-
|
|
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 };
|