@textbus/collaborate 2.0.0-alpha.37 → 2.0.0-alpha.40

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,67 +1,115 @@
1
1
  import { Injectable } from '@tanbo/di'
2
- import { debounceTime, filter, Subscription, tap } from '@tanbo/stream'
2
+ import { debounceTime, filter, Observable, Subject, Subscription, tap } from '@tanbo/stream'
3
3
  import {
4
4
  RootComponentRef,
5
5
  Starter,
6
6
  Operation,
7
7
  Translator,
8
- Registry
8
+ Registry,
9
+ Selection,
10
+ SelectionPaths,
11
+ History, Renderer
9
12
  } from '@textbus/core'
10
- import { Doc as YDoc } from 'yjs'
11
- import { Awareness } from 'y-protocols/awareness'
12
- import { localToRemote } from './collab/local-to-remote'
13
- import { remoteToLocal } from './collab/remote-to-local'
14
- import { CollaborateHistory } from './collab/_api'
15
-
16
- // const collaborateErrorFn = makeError('Collaborate')
13
+ import { Doc as YDoc, UndoManager } from 'yjs'
14
+ import { LocalToRemote } from './collab/local-to-remote'
15
+ import { RemoteToLocal } from './collab/remote-to-local'
16
+ import { CollaborateCursor, RemoteSelection } from './collab/_api'
17
17
 
18
18
  @Injectable()
19
- export class Collaborate {
19
+ export class Collaborate implements History {
20
+ onSelectionChange: Observable<SelectionPaths>
20
21
  yDoc = new YDoc()
22
+ onBack: Observable<void>
23
+ onForward: Observable<void>
24
+ onChange: Observable<any>
25
+ onPush: Observable<void>
26
+
27
+ get canBack() {
28
+ return this.manager?.canUndo()
29
+ }
30
+
31
+ get canForward() {
32
+ return this.manager?.canRedo()
33
+ }
34
+
35
+ private localToRemote = new LocalToRemote()
36
+ private remoteToLocal = new RemoteToLocal(this.translator, this.registry)
37
+
38
+ private backEvent = new Subject<void>()
39
+ private forwardEvent = new Subject<void>()
40
+ private changeEvent = new Subject<void>()
41
+ private pushEvent = new Subject<void>()
42
+
43
+ private manager!: UndoManager
21
44
 
22
45
  private subscriptions: Subscription[] = []
23
46
  private updateFromSelf = true
24
47
 
48
+ private selectionChangeEvent = new Subject<SelectionPaths>()
49
+
25
50
  constructor(private rootComponentRef: RootComponentRef,
51
+ private collaborateCursor: CollaborateCursor,
26
52
  private translator: Translator,
53
+ private renderer: Renderer,
27
54
  private registry: Registry,
28
- private collaborateHistory: CollaborateHistory,
55
+ private selection: Selection,
29
56
  private starter: Starter) {
57
+ this.onSelectionChange = this.selectionChangeEvent.asObservable()
58
+ this.onBack = this.backEvent.asObservable()
59
+ this.onForward = this.forwardEvent.asObservable()
60
+ this.onChange = this.changeEvent.asObservable()
61
+ this.onPush = this.pushEvent.asObservable()
30
62
  }
31
63
 
32
- setup(awareness: Awareness) {
64
+ setup() {
33
65
  this.subscriptions.push(
34
66
  this.starter.onReady.subscribe(() => {
35
- this.listen()
67
+ this.listen2()
68
+ }),
69
+ this.selection.onChange.subscribe(() => {
70
+ const paths = this.selection.getPaths()
71
+ this.selectionChangeEvent.next(paths)
36
72
  })
37
73
  )
38
- awareness.on('update', () => {
39
- const users: any[] = []
40
- awareness.getStates().forEach(state => {
41
- if (state.user) {
42
- users.push(state.user)
43
- }
44
- })
45
- console.log(users)
46
- })
74
+ }
75
+
76
+ updateRemoteSelection(paths: RemoteSelection[]) {
77
+ this.collaborateCursor.draw(paths)
78
+ }
79
+
80
+ listen() {
81
+ //
82
+ }
83
+
84
+ back() {
85
+ if (this.canBack) {
86
+ this.manager.undo()
87
+ }
88
+ }
89
+
90
+ forward() {
91
+ if (this.canForward) {
92
+ this.manager.redo()
93
+ }
47
94
  }
48
95
 
49
96
  destroy() {
50
97
  this.subscriptions.forEach(i => i.unsubscribe())
51
98
  }
52
99
 
53
- private listen() {
54
- const root = this.yDoc.getArray('content')
100
+ private listen2() {
101
+ const root = this.yDoc.getText('content')
55
102
  const slot = this.rootComponentRef.component.slots.get(0)!
56
- this.collaborateHistory.init(root)
103
+ this.manager = new UndoManager(root)
57
104
  root.observeDeep((events, transaction) => {
58
105
  if (transaction.origin === this.yDoc) {
59
106
  return
60
107
  }
61
108
  this.updateFromSelf = false
62
109
 
63
- remoteToLocal(events, slot, this.translator, this.registry)
64
-
110
+ this.remoteToLocal.transform(events, slot)
111
+ this.renderer.render()
112
+ this.selection.restore()
65
113
  this.updateFromSelf = true
66
114
  })
67
115
  const operations: Operation[] = []
@@ -77,10 +125,12 @@ export class Collaborate {
77
125
  ).subscribe(() => {
78
126
  this.yDoc.transact(() => {
79
127
  operations.forEach(operation => {
80
- localToRemote(operation, root)
128
+ this.localToRemote.transform(operation, root)
81
129
  })
82
130
  operations.length = 0
83
131
  }, this.yDoc)
132
+ this.renderer.render()
133
+ this.selection.restore()
84
134
  })
85
135
  )
86
136
  }
@@ -1,6 +1,7 @@
1
1
  {
2
2
  "compilerOptions": {
3
3
  "declaration": true,
4
+ "useDefineForClassFields": true,
4
5
  "emitDecoratorMetadata": true,
5
6
  "experimentalDecorators": true,
6
7
  "allowSyntheticDefaultImports": true,
@@ -10,6 +11,7 @@
10
11
  "module": "es2020",
11
12
  "moduleResolution": "node",
12
13
  "sourceMap": true,
14
+ "inlineSources": true,
13
15
  "noImplicitAny": false,
14
16
  "suppressImplicitAnyIndexErrors": true,
15
17
  "outDir": "bundles/",
@@ -1,22 +0,0 @@
1
- import { History } from '@textbus/core';
2
- import { Observable } from '@tanbo/stream';
3
- import { Array as YArray } from 'yjs';
4
- export declare class CollaborateHistory implements History {
5
- onBack: Observable<void>;
6
- onForward: Observable<void>;
7
- onChange: Observable<any>;
8
- onPush: Observable<void>;
9
- get canBack(): boolean;
10
- get canForward(): boolean;
11
- private backEvent;
12
- private forwardEvent;
13
- private changeEvent;
14
- private pushEvent;
15
- private manager;
16
- constructor();
17
- init(yArray: YArray<any>): void;
18
- listen(): void;
19
- back(): void;
20
- forward(): void;
21
- destroy(): void;
22
- }
@@ -1,53 +0,0 @@
1
- var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
2
- var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
3
- if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
4
- else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
5
- return c > 3 && r && Object.defineProperty(target, key, r), r;
6
- };
7
- var __metadata = (this && this.__metadata) || function (k, v) {
8
- if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
9
- };
10
- import { Injectable } from '@tanbo/di';
11
- import { Subject } from '@tanbo/stream';
12
- import { UndoManager } from 'yjs';
13
- let CollaborateHistory = class CollaborateHistory {
14
- constructor() {
15
- this.backEvent = new Subject();
16
- this.forwardEvent = new Subject();
17
- this.changeEvent = new Subject();
18
- this.pushEvent = new Subject();
19
- this.onBack = this.backEvent.asObservable();
20
- this.onForward = this.forwardEvent.asObservable();
21
- this.onChange = this.changeEvent.asObservable();
22
- this.onPush = this.pushEvent.asObservable();
23
- }
24
- get canBack() {
25
- var _a;
26
- return (_a = this.manager) === null || _a === void 0 ? void 0 : _a.canUndo();
27
- }
28
- get canForward() {
29
- var _a;
30
- return (_a = this.manager) === null || _a === void 0 ? void 0 : _a.canRedo();
31
- }
32
- init(yArray) {
33
- this.manager = new UndoManager(yArray);
34
- }
35
- listen() {
36
- //
37
- }
38
- back() {
39
- //
40
- }
41
- forward() {
42
- //
43
- }
44
- destroy() {
45
- //
46
- }
47
- };
48
- CollaborateHistory = __decorate([
49
- Injectable(),
50
- __metadata("design:paramtypes", [])
51
- ], CollaborateHistory);
52
- export { CollaborateHistory };
53
- //# sourceMappingURL=collaborate-history.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"collaborate-history.js","sourceRoot":"","sources":["../../src/collab/collaborate-history.ts"],"names":[],"mappings":";;;;;;;;;AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,WAAW,CAAA;AAEtC,OAAO,EAAc,OAAO,EAAE,MAAM,eAAe,CAAA;AACnD,OAAO,EAAE,WAAW,EAAmB,MAAM,KAAK,CAAA;AAGlD,IAAa,kBAAkB,GAA/B,MAAa,kBAAkB;IAsB7B;QAPQ,cAAS,GAAG,IAAI,OAAO,EAAQ,CAAA;QAC/B,iBAAY,GAAG,IAAI,OAAO,EAAQ,CAAA;QAClC,gBAAW,GAAG,IAAI,OAAO,EAAQ,CAAA;QACjC,cAAS,GAAG,IAAI,OAAO,EAAQ,CAAA;QAKrC,IAAI,CAAC,MAAM,GAAG,IAAI,CAAC,SAAS,CAAC,YAAY,EAAE,CAAA;QAC3C,IAAI,CAAC,SAAS,GAAG,IAAI,CAAC,YAAY,CAAC,YAAY,EAAE,CAAA;QACjD,IAAI,CAAC,QAAQ,GAAG,IAAI,CAAC,WAAW,CAAC,YAAY,EAAE,CAAA;QAC/C,IAAI,CAAC,MAAM,GAAG,IAAI,CAAC,SAAS,CAAC,YAAY,EAAE,CAAA;IAC7C,CAAC;IArBD,IAAI,OAAO;;QACT,OAAO,MAAA,IAAI,CAAC,OAAO,0CAAE,OAAO,EAAE,CAAA;IAChC,CAAC;IAED,IAAI,UAAU;;QACZ,OAAO,MAAA,IAAI,CAAC,OAAO,0CAAE,OAAO,EAAE,CAAA;IAChC,CAAC;IAiBD,IAAI,CAAC,MAAmB;QACtB,IAAI,CAAC,OAAO,GAAG,IAAI,WAAW,CAAC,MAAM,CAAC,CAAA;IACxC,CAAC;IAED,MAAM;QACJ,EAAE;IACJ,CAAC;IAED,IAAI;QACF,EAAE;IACJ,CAAC;IAED,OAAO;QACL,EAAE;IACJ,CAAC;IAED,OAAO;QACL,EAAE;IACJ,CAAC;CACF,CAAA;AAhDY,kBAAkB;IAD9B,UAAU,EAAE;;GACA,kBAAkB,CAgD9B;SAhDY,kBAAkB"}
@@ -1,55 +0,0 @@
1
- import { Injectable } from '@tanbo/di'
2
- import { History } from '@textbus/core'
3
- import { Observable, Subject } from '@tanbo/stream'
4
- import { UndoManager, Array as YArray } from 'yjs'
5
-
6
- @Injectable()
7
- export class CollaborateHistory implements History {
8
- onBack: Observable<void>
9
- onForward: Observable<void>
10
- onChange: Observable<any>
11
- onPush: Observable<void>
12
-
13
- get canBack() {
14
- return this.manager?.canUndo()
15
- }
16
-
17
- get canForward() {
18
- return this.manager?.canRedo()
19
- }
20
-
21
-
22
- private backEvent = new Subject<void>()
23
- private forwardEvent = new Subject<void>()
24
- private changeEvent = new Subject<void>()
25
- private pushEvent = new Subject<void>()
26
-
27
- private manager!: UndoManager
28
-
29
- constructor() {
30
- this.onBack = this.backEvent.asObservable()
31
- this.onForward = this.forwardEvent.asObservable()
32
- this.onChange = this.changeEvent.asObservable()
33
- this.onPush = this.pushEvent.asObservable()
34
- }
35
-
36
- init(yArray: YArray<any>) {
37
- this.manager = new UndoManager(yArray)
38
- }
39
-
40
- listen() {
41
- //
42
- }
43
-
44
- back() {
45
- //
46
- }
47
-
48
- forward() {
49
- //
50
- }
51
-
52
- destroy() {
53
- //
54
- }
55
- }