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

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.
@@ -1,3 +1,3 @@
1
- export * from './collaborate-history';
1
+ export * from './collaborate-cursor';
2
2
  export * from './local-to-remote';
3
3
  export * from './remote-to-local';
@@ -1,4 +1,4 @@
1
- export * from './collaborate-history';
1
+ export * from './collaborate-cursor';
2
2
  export * from './local-to-remote';
3
3
  export * from './remote-to-local';
4
4
  //# sourceMappingURL=_api.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"_api.js","sourceRoot":"","sources":["../../src/collab/_api.ts"],"names":[],"mappings":"AAAA,cAAc,uBAAuB,CAAA;AACrC,cAAc,mBAAmB,CAAA;AACjC,cAAc,mBAAmB,CAAA"}
1
+ {"version":3,"file":"_api.js","sourceRoot":"","sources":["../../src/collab/_api.ts"],"names":[],"mappings":"AAAA,cAAc,sBAAsB,CAAA;AACpC,cAAc,mBAAmB,CAAA;AACjC,cAAc,mBAAmB,CAAA","sourcesContent":["export * from './collaborate-cursor'\nexport * from './local-to-remote'\nexport * from './remote-to-local'\n"]}
@@ -0,0 +1,37 @@
1
+ import { SelectionBridge } from '@textbus/browser';
2
+ import { Selection, SelectionPaths } from '@textbus/core';
3
+ export interface RemoteSelection {
4
+ color: string;
5
+ username: string;
6
+ paths: SelectionPaths;
7
+ }
8
+ export interface SelectionRect {
9
+ x: number;
10
+ y: number;
11
+ width: number;
12
+ height: number;
13
+ }
14
+ export interface SelectionInfo {
15
+ color: string;
16
+ username: string;
17
+ rects: SelectionRect[];
18
+ }
19
+ export interface RemoteSelectionCursor {
20
+ cursor: HTMLElement;
21
+ anchor: HTMLElement;
22
+ userTip: HTMLElement;
23
+ }
24
+ export declare class CollaborateCursor {
25
+ private container;
26
+ private document;
27
+ private nativeSelection;
28
+ private selection;
29
+ private canvas;
30
+ private context;
31
+ private tooltips;
32
+ private onRectsChange;
33
+ constructor(container: HTMLElement, document: Document, nativeSelection: SelectionBridge, selection: Selection);
34
+ draw(paths: RemoteSelection[]): void;
35
+ private drawUserCursor;
36
+ private getUserCursor;
37
+ }
@@ -0,0 +1,254 @@
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
+ var __param = (this && this.__param) || function (paramIndex, decorator) {
11
+ return function (target, key) { decorator(target, key, paramIndex); }
12
+ };
13
+ import { Inject, Injectable } from '@tanbo/di';
14
+ import { createElement, EDITABLE_DOCUMENT, EDITOR_CONTAINER, SelectionBridge } from '@textbus/browser';
15
+ import { Selection } from '@textbus/core';
16
+ import { Subject } from '@tanbo/stream';
17
+ let CollaborateCursor = class CollaborateCursor {
18
+ constructor(container, document, nativeSelection, selection) {
19
+ Object.defineProperty(this, "container", {
20
+ enumerable: true,
21
+ configurable: true,
22
+ writable: true,
23
+ value: container
24
+ });
25
+ Object.defineProperty(this, "document", {
26
+ enumerable: true,
27
+ configurable: true,
28
+ writable: true,
29
+ value: document
30
+ });
31
+ Object.defineProperty(this, "nativeSelection", {
32
+ enumerable: true,
33
+ configurable: true,
34
+ writable: true,
35
+ value: nativeSelection
36
+ });
37
+ Object.defineProperty(this, "selection", {
38
+ enumerable: true,
39
+ configurable: true,
40
+ writable: true,
41
+ value: selection
42
+ });
43
+ Object.defineProperty(this, "canvas", {
44
+ enumerable: true,
45
+ configurable: true,
46
+ writable: true,
47
+ value: createElement('canvas', {
48
+ styles: {
49
+ position: 'absolute',
50
+ opacity: 0.5,
51
+ left: 0,
52
+ top: 0,
53
+ width: '100%',
54
+ height: '100%',
55
+ pointerEvents: 'none'
56
+ }
57
+ })
58
+ });
59
+ Object.defineProperty(this, "context", {
60
+ enumerable: true,
61
+ configurable: true,
62
+ writable: true,
63
+ value: this.canvas.getContext('2d')
64
+ });
65
+ Object.defineProperty(this, "tooltips", {
66
+ enumerable: true,
67
+ configurable: true,
68
+ writable: true,
69
+ value: createElement('div', {
70
+ styles: {
71
+ position: 'absolute',
72
+ left: 0,
73
+ top: 0,
74
+ width: '100%',
75
+ height: '100%',
76
+ pointerEvents: 'none',
77
+ fontSize: '12px'
78
+ }
79
+ })
80
+ });
81
+ Object.defineProperty(this, "onRectsChange", {
82
+ enumerable: true,
83
+ configurable: true,
84
+ writable: true,
85
+ value: new Subject()
86
+ });
87
+ container.appendChild(this.canvas);
88
+ container.appendChild(this.tooltips);
89
+ this.onRectsChange.subscribe(info => {
90
+ const color = info.color;
91
+ const rects = info.rects;
92
+ for (const rect of rects) {
93
+ this.context.fillStyle = color;
94
+ this.context.beginPath();
95
+ this.context.rect(rect.x, rect.y, rect.width, rect.height);
96
+ this.context.fill();
97
+ this.context.closePath();
98
+ }
99
+ });
100
+ }
101
+ draw(paths) {
102
+ this.canvas.width = this.container.offsetWidth;
103
+ this.canvas.height = this.container.offsetHeight;
104
+ this.context.clearRect(0, 0, this.canvas.width, this.canvas.height);
105
+ const users = [];
106
+ paths.filter(i => {
107
+ return i.paths.start.length && i.paths.end.length;
108
+ }).forEach(item => {
109
+ const startOffset = item.paths.start.pop();
110
+ const startSlot = this.selection.findSlotByPaths(item.paths.start);
111
+ const endOffset = item.paths.end.pop();
112
+ const endSlot = this.selection.findSlotByPaths(item.paths.end);
113
+ if (startSlot && endSlot) {
114
+ const position = this.nativeSelection.getPositionByRange({
115
+ startOffset,
116
+ endOffset,
117
+ startSlot,
118
+ endSlot
119
+ });
120
+ if (position.start && position.end) {
121
+ const nativeRange = this.document.createRange();
122
+ nativeRange.setStart(position.start.node, position.start.offset);
123
+ nativeRange.setEnd(position.end.node, position.end.offset);
124
+ const rects = nativeRange.getClientRects();
125
+ const selectionRects = [];
126
+ for (let i = rects.length - 1; i >= 0; i--) {
127
+ const rect = rects[i];
128
+ selectionRects.push({
129
+ x: rect.x,
130
+ y: rect.y,
131
+ width: rect.width,
132
+ height: rect.height,
133
+ });
134
+ }
135
+ if (rects.length === 0) {
136
+ const rect = nativeRange.getBoundingClientRect();
137
+ if (rect.x !== 0 || rect.y !== 0 || rect.width !== 0 || rect.height !== 0) {
138
+ selectionRects.push({
139
+ x: rect.x,
140
+ y: rect.y,
141
+ width: 1,
142
+ height: rect.height,
143
+ });
144
+ }
145
+ }
146
+ const info = Object.assign(Object.assign({}, item), { rects: selectionRects });
147
+ this.onRectsChange.next(info);
148
+ users.push(info);
149
+ }
150
+ }
151
+ });
152
+ this.drawUserCursor(users);
153
+ }
154
+ drawUserCursor(users) {
155
+ for (let i = 0; i < users.length; i++) {
156
+ const user = users[i];
157
+ const last = user.rects[user.rects.length - 1];
158
+ const { cursor, userTip, anchor } = this.getUserCursor(i);
159
+ if (!last) {
160
+ cursor.style.display = 'none';
161
+ }
162
+ else {
163
+ Object.assign(cursor.style, {
164
+ left: last.x + last.width + 'px',
165
+ top: last.y + 'px',
166
+ width: '2px',
167
+ height: last.height + 'px',
168
+ background: user.color,
169
+ display: 'block'
170
+ });
171
+ anchor.style.background = user.color;
172
+ userTip.innerText = user.username;
173
+ userTip.style.background = user.color;
174
+ }
175
+ }
176
+ for (let i = users.length; i < this.tooltips.children.length; i++) {
177
+ this.tooltips.removeChild(this.tooltips.children[i]);
178
+ }
179
+ }
180
+ getUserCursor(index) {
181
+ let child = this.tooltips.children[index];
182
+ if (child) {
183
+ const anchor = child.children[0];
184
+ return {
185
+ cursor: child,
186
+ anchor,
187
+ userTip: anchor.children[0]
188
+ };
189
+ }
190
+ const userTip = createElement('span', {
191
+ styles: {
192
+ position: 'absolute',
193
+ display: 'none',
194
+ left: '50%',
195
+ transform: 'translateX(-50%)',
196
+ marginBottom: '2px',
197
+ bottom: '100%',
198
+ whiteSpace: 'nowrap',
199
+ color: '#fff',
200
+ boxShadow: '0 1px 2px rgba(0,0,0,.1)',
201
+ borderRadius: '3px',
202
+ padding: '3px 5px',
203
+ pointerEvents: 'none',
204
+ }
205
+ });
206
+ const anchor = createElement('span', {
207
+ styles: {
208
+ position: 'absolute',
209
+ top: '-2px',
210
+ left: '-2px',
211
+ width: '6px',
212
+ height: '6px',
213
+ pointerEvents: 'auto',
214
+ pointer: 'cursor',
215
+ },
216
+ children: [userTip],
217
+ on: {
218
+ mouseenter() {
219
+ anchor.style.transform = 'scale(1.2)';
220
+ userTip.style.display = 'block';
221
+ },
222
+ mouseleave() {
223
+ userTip.style.display = 'none';
224
+ anchor.style.transform = '';
225
+ }
226
+ }
227
+ });
228
+ child = createElement('span', {
229
+ styles: {
230
+ position: 'absolute',
231
+ },
232
+ children: [
233
+ anchor
234
+ ]
235
+ });
236
+ this.tooltips.append(child);
237
+ return {
238
+ cursor: child,
239
+ anchor,
240
+ userTip
241
+ };
242
+ }
243
+ };
244
+ CollaborateCursor = __decorate([
245
+ Injectable(),
246
+ __param(0, Inject(EDITOR_CONTAINER)),
247
+ __param(1, Inject(EDITABLE_DOCUMENT)),
248
+ __metadata("design:paramtypes", [HTMLElement,
249
+ Document,
250
+ SelectionBridge,
251
+ Selection])
252
+ ], CollaborateCursor);
253
+ export { CollaborateCursor };
254
+ //# sourceMappingURL=collaborate-cursor.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"collaborate-cursor.js","sourceRoot":"","sources":["../../src/collab/collaborate-cursor.ts"],"names":[],"mappings":";;;;;;;;;;;;AAAA,OAAO,EAAE,MAAM,EAAE,UAAU,EAAE,MAAM,WAAW,CAAA;AAC9C,OAAO,EAAE,aAAa,EAAE,iBAAiB,EAAE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AyM7B;SAzMY,iBAAiB","sourcesContent":["import { Inject, Injectable } from '@tanbo/di'\nimport { createElement, EDITABLE_DOCUMENT, EDITOR_CONTAINER, SelectionBridge } from '@textbus/browser'\nimport { Selection, SelectionPaths } from '@textbus/core'\nimport { Subject } from '@tanbo/stream'\n\nexport interface RemoteSelection {\n color: string\n username: string\n paths: SelectionPaths\n}\n\nexport interface SelectionRect {\n x: number\n y: number\n width: number\n height: number\n}\n\nexport interface SelectionInfo {\n color: string\n username: string\n rects: SelectionRect[]\n}\n\nexport interface RemoteSelectionCursor {\n cursor: HTMLElement\n anchor: HTMLElement\n userTip: HTMLElement\n}\n\n@Injectable()\nexport class CollaborateCursor {\n private canvas = createElement('canvas', {\n styles: {\n position: 'absolute',\n opacity: 0.5,\n left: 0,\n top: 0,\n width: '100%',\n height: '100%',\n pointerEvents: 'none'\n }\n }) as HTMLCanvasElement\n private context = this.canvas.getContext('2d')!\n private tooltips = createElement('div', {\n styles: {\n position: 'absolute',\n left: 0,\n top: 0,\n width: '100%',\n height: '100%',\n pointerEvents: 'none',\n fontSize: '12px'\n }\n })\n\n private onRectsChange = new Subject<SelectionInfo>()\n\n constructor(@Inject(EDITOR_CONTAINER) private container: HTMLElement,\n @Inject(EDITABLE_DOCUMENT) private document: Document,\n private nativeSelection: SelectionBridge,\n private selection: Selection) {\n container.appendChild(this.canvas)\n container.appendChild(this.tooltips)\n this.onRectsChange.subscribe(info => {\n const color = info.color\n const rects = info.rects\n for (const rect of rects) {\n this.context.fillStyle = color\n this.context.beginPath()\n this.context.rect(rect.x, rect.y, rect.width, rect.height)\n this.context.fill()\n this.context.closePath()\n }\n })\n }\n\n draw(paths: RemoteSelection[]) {\n this.canvas.width = this.container.offsetWidth\n this.canvas.height = this.container.offsetHeight\n this.context.clearRect(0, 0, this.canvas.width, this.canvas.height)\n\n const users: SelectionInfo[] = []\n\n paths.filter(i => {\n return i.paths.start.length && i.paths.end.length\n }).forEach(item => {\n const startOffset = item.paths.start.pop()!\n const startSlot = this.selection.findSlotByPaths(item.paths.start)\n const endOffset = item.paths.end.pop()!\n const endSlot = this.selection.findSlotByPaths(item.paths.end)\n\n if (startSlot && endSlot) {\n const position = this.nativeSelection.getPositionByRange({\n startOffset,\n endOffset,\n startSlot,\n endSlot\n })\n if (position.start && position.end) {\n const nativeRange = this.document.createRange()\n nativeRange.setStart(position.start.node, position.start.offset)\n nativeRange.setEnd(position.end.node, position.end.offset)\n\n const rects = nativeRange.getClientRects()\n const selectionRects: SelectionRect[] = []\n for (let i = rects.length - 1; i >= 0; i--) {\n const rect = rects[i]\n selectionRects.push({\n x: rect.x,\n y: rect.y,\n width: rect.width,\n height: rect.height,\n })\n }\n\n if (rects.length === 0) {\n const rect = nativeRange.getBoundingClientRect()\n if (rect.x !== 0 || rect.y !== 0 || rect.width !== 0 || rect.height !== 0) {\n selectionRects.push({\n x: rect.x,\n y: rect.y,\n width: 1,\n height: rect.height,\n })\n }\n }\n const info: SelectionInfo = {\n ...item,\n rects: selectionRects\n }\n this.onRectsChange.next(info)\n\n users.push(info)\n }\n }\n })\n this.drawUserCursor(users)\n }\n\n private drawUserCursor(users: SelectionInfo[]) {\n for (let i = 0; i < users.length; i++) {\n const user = users[i]\n const last = user.rects[user.rects.length - 1]\n\n const {cursor, userTip, anchor} = this.getUserCursor(i)\n if (!last) {\n cursor.style.display = 'none'\n } else {\n Object.assign(cursor.style, {\n left: last.x + last.width + 'px',\n top: last.y + 'px',\n width: '2px',\n height: last.height + 'px',\n background: user.color,\n display: 'block'\n })\n anchor.style.background = user.color\n userTip.innerText = user.username\n userTip.style.background = user.color\n }\n }\n\n for (let i = users.length; i < this.tooltips.children.length; i++) {\n this.tooltips.removeChild(this.tooltips.children[i])\n }\n }\n\n private getUserCursor(index: number): RemoteSelectionCursor {\n let child: HTMLElement = this.tooltips.children[index] as HTMLElement\n if (child) {\n const anchor = child.children[0] as HTMLElement\n return {\n cursor: child,\n anchor,\n userTip: anchor.children[0] as HTMLElement\n }\n }\n const userTip = createElement('span', {\n styles: {\n position: 'absolute',\n display: 'none',\n left: '50%',\n transform: 'translateX(-50%)',\n marginBottom: '2px',\n bottom: '100%',\n whiteSpace: 'nowrap',\n color: '#fff',\n boxShadow: '0 1px 2px rgba(0,0,0,.1)',\n borderRadius: '3px',\n padding: '3px 5px',\n pointerEvents: 'none',\n }\n })\n\n const anchor = createElement('span', {\n styles: {\n position: 'absolute',\n top: '-2px',\n left: '-2px',\n width: '6px',\n height: '6px',\n pointerEvents: 'auto',\n pointer: 'cursor',\n },\n children: [userTip],\n on: {\n mouseenter() {\n anchor.style.transform = 'scale(1.2)'\n userTip.style.display = 'block'\n },\n mouseleave() {\n userTip.style.display = 'none'\n anchor.style.transform = ''\n }\n }\n })\n child = createElement('span', {\n styles: {\n position: 'absolute',\n },\n children: [\n anchor\n ]\n })\n this.tooltips.append(child)\n return {\n cursor: child,\n anchor,\n userTip\n }\n }\n}\n"]}
@@ -1,3 +1,11 @@
1
1
  import { Operation } from '@textbus/core';
2
- import { Array as YArray } from 'yjs';
3
- export declare function localToRemote(operation: Operation, root: YArray<any>): void;
2
+ import { Text as YText } from 'yjs';
3
+ export declare class LocalToRemote {
4
+ transform(operation: Operation, root: YText): void;
5
+ private applyComponentOperationToSharedComponent;
6
+ private applySlotOperationToSharedSlot;
7
+ private mergeActionsToSharedSlot;
8
+ private makeSharedSlotBySlotLiteral;
9
+ private makeSharedComponentByComponentLiteral;
10
+ private getSharedComponentByIndex;
11
+ }