@textbus/collaborate 4.0.0-alpha.5 → 4.0.0-alpha.50
Sign up to get free protection for your applications and to get access to all the features.
- package/bundles/collaborate-module.d.ts +10 -1
- package/bundles/collaborate.d.ts +55 -64
- package/bundles/index.esm.js +342 -318
- package/bundles/index.js +338 -314
- package/bundles/public-api.d.ts +1 -0
- package/bundles/user-activity.d.ts +23 -0
- package/package.json +7 -7
package/bundles/index.js
CHANGED
@@ -4,6 +4,7 @@ var core$1 = require('@viewfly/core');
|
|
4
4
|
var stream = require('@tanbo/stream');
|
5
5
|
var core = require('@textbus/core');
|
6
6
|
var yjs = require('yjs');
|
7
|
+
var yWebsocket = require('y-websocket');
|
7
8
|
|
8
9
|
/******************************************************************************
|
9
10
|
Copyright (c) Microsoft Corporation.
|
@@ -43,38 +44,38 @@ typeof SuppressedError === "function" ? SuppressedError : function (error, suppr
|
|
43
44
|
};
|
44
45
|
|
45
46
|
const collaborateErrorFn = core.makeError('Collaborate');
|
46
|
-
class
|
47
|
+
class SlotMap {
|
47
48
|
constructor() {
|
48
49
|
this.slotAndYTextMap = new WeakMap();
|
49
|
-
this.
|
50
|
+
this.yTextAndSlotMap = new WeakMap();
|
50
51
|
}
|
51
52
|
set(key, value) {
|
52
53
|
if (key instanceof core.Slot) {
|
53
54
|
this.slotAndYTextMap.set(key, value);
|
54
|
-
this.
|
55
|
+
this.yTextAndSlotMap.set(value, key);
|
55
56
|
}
|
56
57
|
else {
|
57
58
|
this.slotAndYTextMap.set(value, key);
|
58
|
-
this.
|
59
|
+
this.yTextAndSlotMap.set(key, value);
|
59
60
|
}
|
60
61
|
}
|
61
62
|
get(key) {
|
62
63
|
if (key instanceof core.Slot) {
|
63
64
|
return this.slotAndYTextMap.get(key) || null;
|
64
65
|
}
|
65
|
-
return this.
|
66
|
+
return this.yTextAndSlotMap.get(key) || null;
|
66
67
|
}
|
67
68
|
delete(key) {
|
68
69
|
if (key instanceof core.Slot) {
|
69
70
|
const v = this.slotAndYTextMap.get(key);
|
70
71
|
this.slotAndYTextMap.delete(key);
|
71
72
|
if (v) {
|
72
|
-
this.
|
73
|
+
this.yTextAndSlotMap.delete(v);
|
73
74
|
}
|
74
75
|
}
|
75
76
|
else {
|
76
|
-
const v = this.
|
77
|
-
this.
|
77
|
+
const v = this.yTextAndSlotMap.get(key);
|
78
|
+
this.yTextAndSlotMap.delete(key);
|
78
79
|
if (v) {
|
79
80
|
this.slotAndYTextMap.delete(v);
|
80
81
|
}
|
@@ -107,13 +108,9 @@ exports.Collaborate = class Collaborate {
|
|
107
108
|
this.manager = null;
|
108
109
|
this.subscriptions = [];
|
109
110
|
this.updateFromRemote = false;
|
110
|
-
this.contentSyncCaches = new WeakMap();
|
111
|
-
this.slotStateSyncCaches = new WeakMap();
|
112
|
-
this.slotsSyncCaches = new WeakMap();
|
113
|
-
this.componentStateSyncCaches = new WeakMap();
|
114
111
|
this.localChangesAppliedEvent = new stream.Subject();
|
115
112
|
this.selectionChangeEvent = new stream.Subject();
|
116
|
-
this.
|
113
|
+
this.slotMap = new SlotMap();
|
117
114
|
this.updateRemoteActions = [];
|
118
115
|
this.noRecord = {};
|
119
116
|
this.historyItems = [];
|
@@ -148,7 +145,7 @@ exports.Collaborate = class Collaborate {
|
|
148
145
|
this.subscriptions.push(this.scheduler.onLocalChangeBefore.subscribe(() => {
|
149
146
|
beforePosition = this.getRelativeCursorLocation();
|
150
147
|
}));
|
151
|
-
manager.on('stack-item-added', event => {
|
148
|
+
manager.on('stack-item-added', (event) => {
|
152
149
|
if (event.type === 'undo') {
|
153
150
|
if (event.origin === manager) {
|
154
151
|
this.index++;
|
@@ -260,50 +257,145 @@ exports.Collaborate = class Collaborate {
|
|
260
257
|
(_a = this.manager) === null || _a === void 0 ? void 0 : _a.destroy();
|
261
258
|
}
|
262
259
|
syncRootComponent(root, rootComponent) {
|
263
|
-
let
|
264
|
-
if (!
|
265
|
-
|
266
|
-
|
267
|
-
const sharedSlot = this.createSharedSlotBySlot(i);
|
268
|
-
slots.push([sharedSlot]);
|
269
|
-
});
|
260
|
+
let state = root.get('state');
|
261
|
+
if (!state) {
|
262
|
+
state = new yjs.Map();
|
263
|
+
this.syncLocalMapToSharedMap(rootComponent.state, state);
|
270
264
|
this.yDoc.transact(() => {
|
271
|
-
root.set('state',
|
272
|
-
root.set('slots', slots);
|
265
|
+
root.set('state', state);
|
273
266
|
});
|
274
267
|
}
|
275
|
-
else
|
276
|
-
rootComponent.
|
277
|
-
|
268
|
+
else {
|
269
|
+
Object.keys(rootComponent.state).forEach(key => {
|
270
|
+
Reflect.deleteProperty(rootComponent.state, key);
|
278
271
|
});
|
279
|
-
this.
|
280
|
-
|
281
|
-
|
282
|
-
|
272
|
+
this.syncSharedMapToLocalMap(state, rootComponent.state);
|
273
|
+
}
|
274
|
+
}
|
275
|
+
syncSharedMapToLocalMap(sharedMap, localMap) {
|
276
|
+
sharedMap.forEach((value, key) => {
|
277
|
+
localMap[key] = this.createLocalModelBySharedByModel(value);
|
278
|
+
});
|
279
|
+
this.syncObject(sharedMap, localMap);
|
280
|
+
}
|
281
|
+
createLocalMapBySharedMap(sharedMap) {
|
282
|
+
const localMap = core.createObjectProxy({});
|
283
|
+
this.syncSharedMapToLocalMap(sharedMap, localMap);
|
284
|
+
return localMap;
|
285
|
+
}
|
286
|
+
createLocalArrayBySharedArray(sharedArray) {
|
287
|
+
const localArray = core.createArrayProxy([]);
|
288
|
+
localArray.push(...sharedArray.map(item => this.createLocalModelBySharedByModel(item)));
|
289
|
+
this.syncArray(sharedArray, localArray);
|
290
|
+
return localArray;
|
291
|
+
}
|
292
|
+
syncLocalMapToSharedMap(localMap, sharedMap) {
|
293
|
+
Object.entries(localMap).forEach(([key, value]) => {
|
294
|
+
sharedMap.set(key, this.createSharedModelByLocalModel(value));
|
295
|
+
});
|
296
|
+
this.syncObject(sharedMap, localMap);
|
297
|
+
}
|
298
|
+
createSharedMapByLocalMap(localMap) {
|
299
|
+
const sharedMap = new yjs.Map();
|
300
|
+
this.syncLocalMapToSharedMap(localMap, sharedMap);
|
301
|
+
return sharedMap;
|
302
|
+
}
|
303
|
+
createSharedArrayByLocalArray(localArray) {
|
304
|
+
const sharedArray = new yjs.Array();
|
305
|
+
localArray.forEach(value => {
|
306
|
+
sharedArray.push([this.createSharedModelByLocalModel(value)]);
|
307
|
+
});
|
308
|
+
this.syncArray(sharedArray, localArray);
|
309
|
+
return sharedArray;
|
310
|
+
}
|
311
|
+
createSharedSlotByLocalSlot(localSlot) {
|
312
|
+
const sharedSlot = new yjs.Text();
|
313
|
+
sharedSlot.setAttribute('__schema__', [...localSlot.schema]);
|
314
|
+
let offset = 0;
|
315
|
+
localSlot.toDelta().forEach(i => {
|
316
|
+
let formats = {};
|
317
|
+
if (i.formats) {
|
318
|
+
i.formats.forEach(item => {
|
319
|
+
formats[item[0].name] = item[1];
|
283
320
|
});
|
284
|
-
}
|
321
|
+
}
|
322
|
+
else {
|
323
|
+
formats = null;
|
324
|
+
}
|
325
|
+
if (typeof i.insert === 'string') {
|
326
|
+
sharedSlot.insert(offset, i.insert, formats);
|
327
|
+
}
|
328
|
+
else {
|
329
|
+
const sharedComponent = this.createSharedComponentByLocalComponent(i.insert);
|
330
|
+
sharedSlot.insertEmbed(offset, sharedComponent, formats);
|
331
|
+
}
|
332
|
+
offset += i.insert.length;
|
333
|
+
});
|
334
|
+
localSlot.getAttributes().forEach(item => {
|
335
|
+
sharedSlot.setAttribute(item[0].name, item[1]);
|
336
|
+
});
|
337
|
+
this.syncSlot(sharedSlot, localSlot);
|
338
|
+
return sharedSlot;
|
339
|
+
}
|
340
|
+
createLocalSlotBySharedSlot(sharedSlot) {
|
341
|
+
const delta = sharedSlot.toDelta();
|
342
|
+
const localSlot = new core.Slot(sharedSlot.getAttribute('__schema__') || []); // TODO 这里有潜在的问题
|
343
|
+
const attrs = sharedSlot.getAttributes();
|
344
|
+
Object.keys(attrs).forEach(key => {
|
345
|
+
const attribute = this.registry.getAttribute(key);
|
346
|
+
if (attribute) {
|
347
|
+
localSlot.setAttribute(attribute, attrs[key]);
|
348
|
+
}
|
349
|
+
});
|
350
|
+
for (const action of delta) {
|
351
|
+
if (action.insert) {
|
352
|
+
if (typeof action.insert === 'string') {
|
353
|
+
const formats = remoteFormatsToLocal(this.registry, action.attributes);
|
354
|
+
localSlot.insert(action.insert, formats);
|
355
|
+
}
|
356
|
+
else {
|
357
|
+
const sharedComponent = action.insert;
|
358
|
+
const component = this.createLocalComponentBySharedComponent(sharedComponent);
|
359
|
+
localSlot.insert(component, remoteFormatsToLocal(this.registry, action.attributes));
|
360
|
+
}
|
361
|
+
}
|
362
|
+
else {
|
363
|
+
throw collaborateErrorFn('unexpected delta action.');
|
364
|
+
}
|
285
365
|
}
|
286
|
-
|
287
|
-
|
288
|
-
|
289
|
-
|
290
|
-
|
291
|
-
|
292
|
-
|
293
|
-
|
294
|
-
|
295
|
-
|
296
|
-
|
366
|
+
this.syncSlot(sharedSlot, localSlot);
|
367
|
+
return localSlot;
|
368
|
+
}
|
369
|
+
createSharedModelByLocalModel(localModel) {
|
370
|
+
if (localModel instanceof core.Slot) {
|
371
|
+
return this.createSharedSlotByLocalSlot(localModel);
|
372
|
+
}
|
373
|
+
if (Array.isArray(localModel)) {
|
374
|
+
return this.createSharedArrayByLocalArray(localModel);
|
375
|
+
}
|
376
|
+
if (typeof localModel === 'object' && localModel !== null) {
|
377
|
+
return this.createSharedMapByLocalMap(localModel);
|
297
378
|
}
|
298
|
-
|
299
|
-
|
379
|
+
return localModel;
|
380
|
+
}
|
381
|
+
createLocalModelBySharedByModel(sharedModel) {
|
382
|
+
if (sharedModel instanceof yjs.Map) {
|
383
|
+
return this.createLocalMapBySharedMap(sharedModel);
|
384
|
+
}
|
385
|
+
if (sharedModel instanceof yjs.Array) {
|
386
|
+
return this.createLocalArrayBySharedArray(sharedModel);
|
387
|
+
}
|
388
|
+
if (sharedModel instanceof yjs.Text) {
|
389
|
+
return this.createLocalSlotBySharedSlot(sharedModel);
|
390
|
+
}
|
391
|
+
return sharedModel;
|
300
392
|
}
|
301
393
|
getAbstractSelection(position) {
|
302
394
|
const anchorPosition = yjs.createAbsolutePositionFromRelativePosition(position.anchor, this.yDoc);
|
303
395
|
const focusPosition = yjs.createAbsolutePositionFromRelativePosition(position.focus, this.yDoc);
|
304
396
|
if (anchorPosition && focusPosition) {
|
305
|
-
const focusSlot = this.
|
306
|
-
const anchorSlot = this.
|
397
|
+
const focusSlot = this.slotMap.get(focusPosition.type);
|
398
|
+
const anchorSlot = this.slotMap.get(anchorPosition.type);
|
307
399
|
if (focusSlot && anchorSlot) {
|
308
400
|
return {
|
309
401
|
anchorSlot,
|
@@ -318,11 +410,11 @@ exports.Collaborate = class Collaborate {
|
|
318
410
|
getRelativeCursorLocation() {
|
319
411
|
const { anchorSlot, anchorOffset, focusSlot, focusOffset } = this.selection;
|
320
412
|
if (anchorSlot) {
|
321
|
-
const anchorYText = this.
|
413
|
+
const anchorYText = this.slotMap.get(anchorSlot);
|
322
414
|
if (anchorYText) {
|
323
415
|
const anchorPosition = yjs.createRelativePositionFromTypeIndex(anchorYText, anchorOffset);
|
324
416
|
if (focusSlot) {
|
325
|
-
const focusYText = this.
|
417
|
+
const focusYText = this.slotMap.get(focusSlot);
|
326
418
|
if (focusYText) {
|
327
419
|
const focusPosition = yjs.createRelativePositionFromTypeIndex(focusYText, focusOffset);
|
328
420
|
return {
|
@@ -335,11 +427,10 @@ exports.Collaborate = class Collaborate {
|
|
335
427
|
}
|
336
428
|
return null;
|
337
429
|
}
|
338
|
-
|
339
|
-
this.contentMap.set(slot, content);
|
430
|
+
syncSlot(sharedSlot, localSlot) {
|
340
431
|
const syncRemote = (ev, tr) => {
|
341
432
|
this.runRemoteUpdate(tr, () => {
|
342
|
-
|
433
|
+
localSlot.retain(0);
|
343
434
|
ev.keysChanged.forEach(key => {
|
344
435
|
const change = ev.keys.get(key);
|
345
436
|
if (!change) {
|
@@ -349,13 +440,13 @@ exports.Collaborate = class Collaborate {
|
|
349
440
|
if (updateType === 'update' || updateType === 'add') {
|
350
441
|
const attribute = this.registry.getAttribute(key);
|
351
442
|
if (attribute) {
|
352
|
-
|
443
|
+
localSlot.setAttribute(attribute, sharedSlot.getAttribute(key));
|
353
444
|
}
|
354
445
|
}
|
355
446
|
else if (updateType === 'delete') {
|
356
447
|
const attribute = this.registry.getAttribute(key);
|
357
448
|
if (attribute) {
|
358
|
-
|
449
|
+
localSlot.removeAttribute(attribute);
|
359
450
|
}
|
360
451
|
}
|
361
452
|
});
|
@@ -364,54 +455,52 @@ exports.Collaborate = class Collaborate {
|
|
364
455
|
if (action.attributes) {
|
365
456
|
const formats = remoteFormatsToLocal(this.registry, action.attributes);
|
366
457
|
if (formats.length) {
|
367
|
-
|
458
|
+
localSlot.retain(action.retain, formats);
|
368
459
|
}
|
369
|
-
|
460
|
+
localSlot.retain(localSlot.index + action.retain);
|
370
461
|
}
|
371
462
|
else {
|
372
|
-
|
463
|
+
localSlot.retain(action.retain);
|
373
464
|
}
|
374
465
|
}
|
375
466
|
else if (action.insert) {
|
376
|
-
const index =
|
467
|
+
const index = localSlot.index;
|
377
468
|
let length = 1;
|
378
469
|
if (typeof action.insert === 'string') {
|
379
470
|
length = action.insert.length;
|
380
|
-
|
471
|
+
localSlot.insert(action.insert, remoteFormatsToLocal(this.registry, action.attributes));
|
381
472
|
}
|
382
473
|
else {
|
383
474
|
const sharedComponent = action.insert;
|
384
|
-
const component = this.
|
385
|
-
|
386
|
-
this.syncComponentState(sharedComponent, component);
|
387
|
-
slot.insert(component);
|
475
|
+
const component = this.createLocalComponentBySharedComponent(sharedComponent);
|
476
|
+
localSlot.insert(component);
|
388
477
|
}
|
389
478
|
if (this.selection.isSelected && tr.origin !== this.manager) {
|
390
|
-
if (
|
391
|
-
this.selection.setAnchor(
|
479
|
+
if (localSlot === this.selection.anchorSlot && this.selection.anchorOffset > index) {
|
480
|
+
this.selection.setAnchor(localSlot, this.selection.anchorOffset + length);
|
392
481
|
}
|
393
|
-
if (
|
394
|
-
this.selection.setFocus(
|
482
|
+
if (localSlot === this.selection.focusSlot && this.selection.focusOffset > index) {
|
483
|
+
this.selection.setFocus(localSlot, this.selection.focusOffset + length);
|
395
484
|
}
|
396
485
|
}
|
397
486
|
}
|
398
487
|
else if (action.delete) {
|
399
|
-
const index =
|
400
|
-
|
488
|
+
const index = localSlot.index;
|
489
|
+
localSlot.delete(action.delete);
|
401
490
|
if (this.selection.isSelected && tr.origin !== this.manager) {
|
402
|
-
if (
|
403
|
-
this.selection.setAnchor(
|
491
|
+
if (localSlot === this.selection.anchorSlot && this.selection.anchorOffset >= index) {
|
492
|
+
this.selection.setAnchor(localSlot, this.selection.startOffset - action.delete);
|
404
493
|
}
|
405
|
-
if (
|
406
|
-
this.selection.setFocus(
|
494
|
+
if (localSlot === this.selection.focusSlot && this.selection.focusOffset >= index) {
|
495
|
+
this.selection.setFocus(localSlot, this.selection.focusOffset - action.delete);
|
407
496
|
}
|
408
497
|
}
|
409
498
|
}
|
410
499
|
});
|
411
500
|
});
|
412
501
|
};
|
413
|
-
|
414
|
-
const sub =
|
502
|
+
sharedSlot.observe(syncRemote);
|
503
|
+
const sub = localSlot.onContentChange.subscribe(actions => {
|
415
504
|
this.runLocalUpdate(() => {
|
416
505
|
var _a;
|
417
506
|
let offset = 0;
|
@@ -430,170 +519,173 @@ exports.Collaborate = class Collaborate {
|
|
430
519
|
}
|
431
520
|
});
|
432
521
|
if (length) {
|
433
|
-
|
522
|
+
sharedSlot.format(offset, action.offset, formats);
|
434
523
|
}
|
435
524
|
}
|
436
525
|
else {
|
437
526
|
offset = action.offset;
|
438
527
|
}
|
439
528
|
}
|
440
|
-
else if (action.type === '
|
441
|
-
const delta =
|
529
|
+
else if (action.type === 'contentInsert') {
|
530
|
+
const delta = sharedSlot.toDelta();
|
442
531
|
const isEmpty = delta.length === 1 && delta[0].insert === core.Slot.emptyPlaceholder;
|
443
532
|
if (typeof action.content === 'string') {
|
444
533
|
length = action.content.length;
|
445
|
-
|
534
|
+
sharedSlot.insert(offset, action.content, action.formats || {});
|
446
535
|
}
|
447
536
|
else {
|
448
537
|
length = 1;
|
449
|
-
const sharedComponent = this.
|
450
|
-
|
538
|
+
const sharedComponent = this.createSharedComponentByLocalComponent(action.ref);
|
539
|
+
sharedSlot.insertEmbed(offset, sharedComponent, action.formats || {});
|
451
540
|
}
|
452
541
|
if (isEmpty && offset === 0) {
|
453
|
-
|
542
|
+
sharedSlot.delete(sharedSlot.length - 1, 1);
|
454
543
|
}
|
455
544
|
offset += length;
|
456
545
|
}
|
457
546
|
else if (action.type === 'delete') {
|
458
|
-
const delta =
|
459
|
-
if (
|
460
|
-
|
547
|
+
const delta = sharedSlot.toDelta();
|
548
|
+
if (sharedSlot.length) {
|
549
|
+
sharedSlot.delete(offset, action.count);
|
461
550
|
}
|
462
|
-
if (
|
463
|
-
|
551
|
+
if (sharedSlot.length === 0) {
|
552
|
+
sharedSlot.insert(0, '\n', (_a = delta[0]) === null || _a === void 0 ? void 0 : _a.attributes);
|
464
553
|
}
|
465
554
|
}
|
466
555
|
else if (action.type === 'attrSet') {
|
467
|
-
|
556
|
+
sharedSlot.setAttribute(action.name, action.value);
|
468
557
|
}
|
469
|
-
else if (action.type === '
|
470
|
-
|
558
|
+
else if (action.type === 'attrDelete') {
|
559
|
+
sharedSlot.removeAttribute(action.name);
|
471
560
|
}
|
472
561
|
}
|
473
562
|
});
|
474
563
|
});
|
475
|
-
|
476
|
-
|
477
|
-
|
478
|
-
|
479
|
-
}));
|
480
|
-
this.contentSyncCaches.set(slot, () => {
|
481
|
-
content.unobserve(syncRemote);
|
564
|
+
this.slotMap.set(localSlot, sharedSlot);
|
565
|
+
localSlot.__changeMarker__.destroyCallbacks.push(() => {
|
566
|
+
this.slotMap.delete(localSlot);
|
567
|
+
sharedSlot.unobserve(syncRemote);
|
482
568
|
sub.unsubscribe();
|
483
569
|
});
|
484
570
|
}
|
485
|
-
|
486
|
-
const
|
487
|
-
|
488
|
-
|
489
|
-
|
490
|
-
|
491
|
-
|
492
|
-
|
493
|
-
|
494
|
-
|
495
|
-
|
496
|
-
|
571
|
+
createSharedComponentByLocalComponent(component) {
|
572
|
+
const sharedComponent = new yjs.Map();
|
573
|
+
const sharedState = this.createSharedMapByLocalMap(component.state);
|
574
|
+
sharedComponent.set('name', component.name);
|
575
|
+
sharedComponent.set('state', sharedState);
|
576
|
+
return sharedComponent;
|
577
|
+
}
|
578
|
+
createLocalComponentBySharedComponent(yMap) {
|
579
|
+
const componentName = yMap.get('name');
|
580
|
+
const sharedState = yMap.get('state');
|
581
|
+
const state = this.createLocalMapBySharedMap(sharedState);
|
582
|
+
const instance = this.registry.createComponentByData(componentName, state);
|
583
|
+
if (instance) {
|
584
|
+
return instance;
|
585
|
+
}
|
586
|
+
throw collaborateErrorFn(`cannot find component factory \`${componentName}\`.`);
|
587
|
+
}
|
588
|
+
/**
|
589
|
+
* 双向同步数组
|
590
|
+
* @param sharedArray
|
591
|
+
* @param localArray
|
592
|
+
* @private
|
593
|
+
*/
|
594
|
+
syncArray(sharedArray, localArray) {
|
595
|
+
const sub = localArray.__changeMarker__.onSelfChange.subscribe((actions) => {
|
596
|
+
this.runLocalUpdate(() => {
|
597
|
+
let index = 0;
|
598
|
+
for (const action of actions) {
|
599
|
+
switch (action.type) {
|
600
|
+
case 'retain':
|
601
|
+
index = action.offset;
|
602
|
+
break;
|
603
|
+
case 'insert':
|
604
|
+
{
|
605
|
+
const ref = action.ref;
|
606
|
+
if (!Array.isArray(ref)) {
|
607
|
+
throw collaborateErrorFn('The insertion action must have a reference value.');
|
608
|
+
}
|
609
|
+
const data = ref.map(item => {
|
610
|
+
return this.createSharedModelByLocalModel(item);
|
611
|
+
});
|
612
|
+
sharedArray.insert(index, data);
|
497
613
|
}
|
498
|
-
|
614
|
+
break;
|
615
|
+
case 'delete':
|
616
|
+
sharedArray.delete(index, 1);
|
617
|
+
break;
|
618
|
+
case 'setIndex':
|
619
|
+
sharedArray.delete(action.index, 1);
|
620
|
+
sharedArray.insert(action.index, [this.createSharedModelByLocalModel(action.ref)]);
|
621
|
+
break;
|
499
622
|
}
|
500
|
-
}
|
623
|
+
}
|
501
624
|
});
|
502
|
-
};
|
503
|
-
remoteSlot.observe(syncRemote);
|
504
|
-
const sub = slot.onStateChange.subscribe(change => {
|
505
|
-
this.runLocalUpdate(() => {
|
506
|
-
remoteSlot.set('state', change.newState);
|
507
|
-
}, change.record);
|
508
|
-
});
|
509
|
-
this.slotStateSyncCaches.set(slot, () => {
|
510
|
-
remoteSlot.unobserve(syncRemote);
|
511
|
-
sub.unsubscribe();
|
512
625
|
});
|
513
|
-
}
|
514
|
-
syncComponentSlots(remoteSlots, component) {
|
515
|
-
const slots = component.slots;
|
516
626
|
const syncRemote = (ev, tr) => {
|
517
627
|
this.runRemoteUpdate(tr, () => {
|
518
628
|
let index = 0;
|
519
|
-
|
520
|
-
ev.delta.forEach(action => {
|
629
|
+
ev.delta.forEach((action) => {
|
521
630
|
if (Reflect.has(action, 'retain')) {
|
522
631
|
index += action.retain;
|
523
|
-
slots.retain(index);
|
524
632
|
}
|
525
633
|
else if (action.insert) {
|
526
|
-
action.insert.
|
527
|
-
|
528
|
-
slots.insert(slot);
|
529
|
-
this.syncSlotContent(item.get('content'), slot);
|
530
|
-
this.syncSlotState(item, slot);
|
531
|
-
index++;
|
634
|
+
const data = action.insert.map((item) => {
|
635
|
+
return this.createLocalModelBySharedByModel(item);
|
532
636
|
});
|
637
|
+
localArray.splice(index, 0, ...data);
|
638
|
+
index += data.length;
|
533
639
|
}
|
534
640
|
else if (action.delete) {
|
535
|
-
|
536
|
-
slots.delete(action.delete);
|
641
|
+
localArray.splice(index, action.delete);
|
537
642
|
}
|
538
643
|
});
|
539
644
|
});
|
540
645
|
};
|
541
|
-
|
542
|
-
|
543
|
-
this.runLocalUpdate(() => {
|
544
|
-
const applyActions = operations.apply;
|
545
|
-
let index;
|
546
|
-
applyActions.forEach(action => {
|
547
|
-
if (action.type === 'retain') {
|
548
|
-
index = action.offset;
|
549
|
-
}
|
550
|
-
else if (action.type === 'insertSlot') {
|
551
|
-
const sharedSlot = this.createSharedSlotBySlot(action.ref);
|
552
|
-
remoteSlots.insert(index, [sharedSlot]);
|
553
|
-
index++;
|
554
|
-
}
|
555
|
-
else if (action.type === 'delete') {
|
556
|
-
remoteSlots.delete(index, action.count);
|
557
|
-
}
|
558
|
-
});
|
559
|
-
});
|
560
|
-
});
|
561
|
-
sub.add(slots.onChildSlotRemove.subscribe(slots => {
|
562
|
-
slots.forEach(slot => {
|
563
|
-
this.cleanSubscriptionsBySlot(slot);
|
564
|
-
});
|
565
|
-
}));
|
566
|
-
this.slotsSyncCaches.set(component, () => {
|
567
|
-
remoteSlots.unobserve(syncRemote);
|
646
|
+
sharedArray.observe(syncRemote);
|
647
|
+
localArray.__changeMarker__.destroyCallbacks.push(() => {
|
568
648
|
sub.unsubscribe();
|
649
|
+
sharedArray.unobserve(syncRemote);
|
569
650
|
});
|
570
651
|
}
|
571
|
-
|
652
|
+
/**
|
653
|
+
* 双向同步对象
|
654
|
+
* @param sharedObject
|
655
|
+
* @param localObject
|
656
|
+
* @private
|
657
|
+
*/
|
658
|
+
syncObject(sharedObject, localObject) {
|
572
659
|
const syncRemote = (ev, tr) => {
|
573
660
|
this.runRemoteUpdate(tr, () => {
|
574
|
-
ev.
|
575
|
-
if (
|
576
|
-
const
|
577
|
-
|
578
|
-
|
579
|
-
|
580
|
-
|
581
|
-
else {
|
582
|
-
return state;
|
583
|
-
}
|
584
|
-
});
|
661
|
+
ev.changes.keys.forEach((item, key) => {
|
662
|
+
if (item.action === 'add' || item.action === 'update') {
|
663
|
+
const value = sharedObject.get(key);
|
664
|
+
localObject[key] = this.createLocalModelBySharedByModel(value);
|
665
|
+
}
|
666
|
+
else {
|
667
|
+
Reflect.deleteProperty(localObject, key);
|
585
668
|
}
|
586
669
|
});
|
587
670
|
});
|
588
671
|
};
|
589
|
-
|
590
|
-
const sub =
|
672
|
+
sharedObject.observe(syncRemote);
|
673
|
+
const sub = localObject.__changeMarker__.onSelfChange.subscribe((actions) => {
|
591
674
|
this.runLocalUpdate(() => {
|
592
|
-
|
593
|
-
|
675
|
+
for (const action of actions) {
|
676
|
+
switch (action.type) {
|
677
|
+
case 'propSet':
|
678
|
+
sharedObject.set(action.key, this.createSharedModelByLocalModel(action.value));
|
679
|
+
break;
|
680
|
+
case 'propDelete':
|
681
|
+
sharedObject.delete(action.key);
|
682
|
+
break;
|
683
|
+
}
|
684
|
+
}
|
685
|
+
});
|
594
686
|
});
|
595
|
-
|
596
|
-
|
687
|
+
localObject.__changeMarker__.destroyCallbacks.push(function () {
|
688
|
+
sharedObject.unobserve(syncRemote);
|
597
689
|
sub.unsubscribe();
|
598
690
|
});
|
599
691
|
}
|
@@ -619,140 +711,6 @@ exports.Collaborate = class Collaborate {
|
|
619
711
|
}
|
620
712
|
this.updateFromRemote = false;
|
621
713
|
}
|
622
|
-
createSharedComponentByComponent(component) {
|
623
|
-
const sharedComponent = new yjs.Map();
|
624
|
-
sharedComponent.set('state', component.state);
|
625
|
-
sharedComponent.set('name', component.name);
|
626
|
-
const sharedSlots = new yjs.Array();
|
627
|
-
sharedComponent.set('slots', sharedSlots);
|
628
|
-
component.slots.toArray().forEach(slot => {
|
629
|
-
const sharedSlot = this.createSharedSlotBySlot(slot);
|
630
|
-
sharedSlots.push([sharedSlot]);
|
631
|
-
});
|
632
|
-
this.syncComponentSlots(sharedSlots, component);
|
633
|
-
this.syncComponentState(sharedComponent, component);
|
634
|
-
return sharedComponent;
|
635
|
-
}
|
636
|
-
createSharedSlotBySlot(slot) {
|
637
|
-
const sharedSlot = new yjs.Map();
|
638
|
-
sharedSlot.set('schema', slot.schema);
|
639
|
-
sharedSlot.set('state', slot.state);
|
640
|
-
const sharedContent = new yjs.Text();
|
641
|
-
sharedSlot.set('content', sharedContent);
|
642
|
-
let offset = 0;
|
643
|
-
slot.toDelta().forEach(i => {
|
644
|
-
let formats = {};
|
645
|
-
if (i.formats) {
|
646
|
-
i.formats.forEach(item => {
|
647
|
-
formats[item[0].name] = item[1];
|
648
|
-
});
|
649
|
-
}
|
650
|
-
else {
|
651
|
-
formats = null;
|
652
|
-
}
|
653
|
-
if (typeof i.insert === 'string') {
|
654
|
-
sharedContent.insert(offset, i.insert, formats);
|
655
|
-
}
|
656
|
-
else {
|
657
|
-
const sharedComponent = this.createSharedComponentByComponent(i.insert);
|
658
|
-
sharedContent.insertEmbed(offset, sharedComponent, formats);
|
659
|
-
}
|
660
|
-
offset += i.insert.length;
|
661
|
-
});
|
662
|
-
slot.getAttributes().forEach(item => {
|
663
|
-
sharedContent.setAttribute(item[0].name, item[1]);
|
664
|
-
});
|
665
|
-
this.syncSlotContent(sharedContent, slot);
|
666
|
-
this.syncSlotState(sharedSlot, slot);
|
667
|
-
return sharedSlot;
|
668
|
-
}
|
669
|
-
createComponentBySharedComponent(yMap) {
|
670
|
-
const sharedSlots = yMap.get('slots');
|
671
|
-
const slots = [];
|
672
|
-
sharedSlots.forEach(sharedSlot => {
|
673
|
-
const slot = this.createSlotBySharedSlot(sharedSlot);
|
674
|
-
slots.push(slot);
|
675
|
-
});
|
676
|
-
const name = yMap.get('name');
|
677
|
-
const state = yMap.get('state');
|
678
|
-
const instance = this.registry.createComponentByData(name, {
|
679
|
-
state,
|
680
|
-
slots
|
681
|
-
});
|
682
|
-
if (instance) {
|
683
|
-
instance.slots.toArray().forEach((slot, index) => {
|
684
|
-
let sharedSlot = sharedSlots.get(index);
|
685
|
-
if (!sharedSlot) {
|
686
|
-
sharedSlot = this.createSharedSlotBySlot(slot);
|
687
|
-
sharedSlots.push([sharedSlot]);
|
688
|
-
}
|
689
|
-
this.syncSlotState(sharedSlot, slot);
|
690
|
-
this.syncSlotContent(sharedSlot.get('content'), slot);
|
691
|
-
});
|
692
|
-
return instance;
|
693
|
-
}
|
694
|
-
throw collaborateErrorFn(`cannot find component factory \`${name}\`.`);
|
695
|
-
}
|
696
|
-
createSlotBySharedSlot(sharedSlot) {
|
697
|
-
const content = sharedSlot.get('content');
|
698
|
-
const delta = content.toDelta();
|
699
|
-
const slot = this.registry.createSlot({
|
700
|
-
schema: sharedSlot.get('schema'),
|
701
|
-
state: sharedSlot.get('state'),
|
702
|
-
attributes: {},
|
703
|
-
formats: {},
|
704
|
-
content: []
|
705
|
-
});
|
706
|
-
const attrs = content.getAttributes();
|
707
|
-
Object.keys(attrs).forEach(key => {
|
708
|
-
const attribute = this.registry.getAttribute(key);
|
709
|
-
if (attribute) {
|
710
|
-
slot.setAttribute(attribute, attrs[key]);
|
711
|
-
}
|
712
|
-
});
|
713
|
-
for (const action of delta) {
|
714
|
-
if (action.insert) {
|
715
|
-
if (typeof action.insert === 'string') {
|
716
|
-
const formats = remoteFormatsToLocal(this.registry, action.attributes);
|
717
|
-
slot.insert(action.insert, formats);
|
718
|
-
}
|
719
|
-
else {
|
720
|
-
const sharedComponent = action.insert;
|
721
|
-
const component = this.createComponentBySharedComponent(sharedComponent);
|
722
|
-
slot.insert(component, remoteFormatsToLocal(this.registry, action.attributes));
|
723
|
-
this.syncComponentSlots(sharedComponent.get('slots'), component);
|
724
|
-
this.syncComponentState(sharedComponent, component);
|
725
|
-
}
|
726
|
-
}
|
727
|
-
else {
|
728
|
-
throw collaborateErrorFn('unexpected delta action.');
|
729
|
-
}
|
730
|
-
}
|
731
|
-
return slot;
|
732
|
-
}
|
733
|
-
cleanSubscriptionsBySlot(slot) {
|
734
|
-
this.contentMap.delete(slot);
|
735
|
-
[this.contentSyncCaches.get(slot), this.slotStateSyncCaches.get(slot)].forEach(fn => {
|
736
|
-
if (fn) {
|
737
|
-
fn();
|
738
|
-
}
|
739
|
-
});
|
740
|
-
slot.sliceContent().forEach(i => {
|
741
|
-
if (typeof i !== 'string') {
|
742
|
-
this.cleanSubscriptionsByComponent(i);
|
743
|
-
}
|
744
|
-
});
|
745
|
-
}
|
746
|
-
cleanSubscriptionsByComponent(component) {
|
747
|
-
[this.slotsSyncCaches.get(component), this.componentStateSyncCaches.get(component)].forEach(fn => {
|
748
|
-
if (fn) {
|
749
|
-
fn();
|
750
|
-
}
|
751
|
-
});
|
752
|
-
component.slots.toArray().forEach(slot => {
|
753
|
-
this.cleanSubscriptionsBySlot(slot);
|
754
|
-
});
|
755
|
-
}
|
756
714
|
};
|
757
715
|
exports.Collaborate = __decorate([
|
758
716
|
core$1.Injectable(),
|
@@ -777,16 +735,82 @@ function remoteFormatsToLocal(registry, attrs) {
|
|
777
735
|
return formats;
|
778
736
|
}
|
779
737
|
|
738
|
+
exports.UserActivity = class UserActivity {
|
739
|
+
constructor(websocketProvider, selection) {
|
740
|
+
this.websocketProvider = websocketProvider;
|
741
|
+
this.selection = selection;
|
742
|
+
this.stateChangeEvent = new stream.Subject();
|
743
|
+
this.userChangeEvent = new stream.Subject();
|
744
|
+
this.subscription = new stream.Subscription();
|
745
|
+
this.onStateChange = this.stateChangeEvent.asObservable();
|
746
|
+
this.onUserChange = this.userChangeEvent.asObservable();
|
747
|
+
}
|
748
|
+
init(userinfo) {
|
749
|
+
const provide = this.websocketProvider;
|
750
|
+
provide.awareness.setLocalStateField('user', userinfo);
|
751
|
+
this.subscription.add(this.selection.onChange.subscribe(() => {
|
752
|
+
const selection = this.selection.getPaths();
|
753
|
+
provide.awareness.setLocalStateField('selection', Object.assign(Object.assign({}, userinfo), { selection }));
|
754
|
+
}));
|
755
|
+
provide.awareness.on('update', () => {
|
756
|
+
const users = [];
|
757
|
+
const remoteSelections = [];
|
758
|
+
provide.awareness.getStates().forEach(state => {
|
759
|
+
if (state.user) {
|
760
|
+
users.push(state.user);
|
761
|
+
}
|
762
|
+
if (state.selection) {
|
763
|
+
remoteSelections.push(state.selection);
|
764
|
+
}
|
765
|
+
});
|
766
|
+
const selections = remoteSelections.filter(i => i.id !== userinfo.id);
|
767
|
+
this.userChangeEvent.next(users);
|
768
|
+
this.stateChangeEvent.next(selections);
|
769
|
+
});
|
770
|
+
}
|
771
|
+
destroy() {
|
772
|
+
this.subscription.unsubscribe();
|
773
|
+
}
|
774
|
+
};
|
775
|
+
exports.UserActivity = __decorate([
|
776
|
+
core$1.Injectable(),
|
777
|
+
__metadata("design:paramtypes", [yWebsocket.WebsocketProvider,
|
778
|
+
core.Selection])
|
779
|
+
], exports.UserActivity);
|
780
|
+
|
780
781
|
class CollaborateModule {
|
781
|
-
constructor() {
|
782
|
+
constructor(config) {
|
783
|
+
this.config = config;
|
782
784
|
this.providers = [
|
783
785
|
exports.Collaborate,
|
786
|
+
exports.UserActivity,
|
784
787
|
{
|
785
788
|
provide: core.History,
|
786
789
|
useExisting: exports.Collaborate
|
790
|
+
}, {
|
791
|
+
provide: yWebsocket.WebsocketProvider,
|
792
|
+
useFactory: (collab) => {
|
793
|
+
return new yWebsocket.WebsocketProvider(this.config.url, this.config.roomName, collab.yDoc);
|
794
|
+
},
|
795
|
+
deps: [exports.Collaborate]
|
787
796
|
}
|
788
797
|
];
|
789
798
|
}
|
799
|
+
setup(textbus) {
|
800
|
+
const provide = textbus.get(yWebsocket.WebsocketProvider);
|
801
|
+
const userActivity = textbus.get(exports.UserActivity);
|
802
|
+
userActivity.init(this.config.userinfo);
|
803
|
+
return new Promise((resolve) => {
|
804
|
+
provide.on('sync', (is) => {
|
805
|
+
if (is) {
|
806
|
+
resolve(() => {
|
807
|
+
provide.disconnect();
|
808
|
+
userActivity.destroy();
|
809
|
+
});
|
810
|
+
}
|
811
|
+
});
|
812
|
+
});
|
813
|
+
}
|
790
814
|
}
|
791
815
|
|
792
816
|
exports.CollaborateModule = CollaborateModule;
|