@textbus/collaborate 2.0.0-beta.4 → 2.0.0-beta.42

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,245 +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
- 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, getLayoutRectByRange, 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
- zIndex: 10
79
- }
80
- })
81
- });
82
- Object.defineProperty(this, "onRectsChange", {
83
- enumerable: true,
84
- configurable: true,
85
- writable: true,
86
- value: new Subject()
87
- });
88
- container.prepend(this.canvas, this.tooltips);
89
- this.onRectsChange.subscribe(rects => {
90
- for (const rect of rects) {
91
- this.context.fillStyle = rect.color;
92
- this.context.beginPath();
93
- this.context.rect(rect.x, rect.y, rect.width, rect.height);
94
- this.context.fill();
95
- this.context.closePath();
96
- }
97
- });
98
- }
99
- draw(paths) {
100
- const containerRect = this.container.getBoundingClientRect();
101
- this.canvas.width = containerRect.width;
102
- this.canvas.height = containerRect.height;
103
- this.context.clearRect(0, 0, this.canvas.width, this.canvas.height);
104
- const users = [];
105
- paths.filter(i => {
106
- return i.paths.start.length && i.paths.end.length;
107
- }).forEach(item => {
108
- const startOffset = item.paths.start.pop();
109
- const startSlot = this.selection.findSlotByPaths(item.paths.start);
110
- const endOffset = item.paths.end.pop();
111
- const endSlot = this.selection.findSlotByPaths(item.paths.end);
112
- if (startSlot && endSlot) {
113
- const position = this.nativeSelection.getPositionByRange({
114
- startOffset,
115
- endOffset,
116
- startSlot,
117
- endSlot
118
- });
119
- if (position.start && position.end) {
120
- const nativeRange = this.document.createRange();
121
- nativeRange.setStart(position.start.node, position.start.offset);
122
- nativeRange.setEnd(position.end.node, position.end.offset);
123
- const rects = nativeRange.getClientRects();
124
- const selectionRects = [];
125
- for (let i = rects.length - 1; i >= 0; i--) {
126
- const rect = rects[i];
127
- selectionRects.push({
128
- color: item.color,
129
- username: item.username,
130
- x: rect.x - containerRect.x,
131
- y: rect.y - containerRect.y,
132
- width: rect.width,
133
- height: rect.height,
134
- });
135
- }
136
- this.onRectsChange.next(selectionRects);
137
- const cursorRange = nativeRange.cloneRange();
138
- cursorRange.collapse(!item.paths.focusEnd);
139
- const cursorRect = getLayoutRectByRange(cursorRange);
140
- users.push({
141
- username: item.username,
142
- color: item.color,
143
- x: cursorRect.x - containerRect.x,
144
- y: cursorRect.y - containerRect.y,
145
- width: 2,
146
- height: cursorRect.height
147
- });
148
- }
149
- }
150
- });
151
- this.drawUserCursor(users);
152
- }
153
- drawUserCursor(rects) {
154
- for (let i = 0; i < rects.length; i++) {
155
- const rect = rects[i];
156
- const { cursor, userTip, anchor } = this.getUserCursor(i);
157
- Object.assign(cursor.style, {
158
- left: rect.x + 'px',
159
- top: rect.y + 'px',
160
- width: rect.width + 'px',
161
- height: rect.height + 'px',
162
- background: rect.color,
163
- display: 'block'
164
- });
165
- anchor.style.background = rect.color;
166
- userTip.innerText = rect.username;
167
- userTip.style.background = rect.color;
168
- }
169
- for (let i = rects.length; i < this.tooltips.children.length; i++) {
170
- this.tooltips.removeChild(this.tooltips.children[i]);
171
- }
172
- }
173
- getUserCursor(index) {
174
- let child = this.tooltips.children[index];
175
- if (child) {
176
- const anchor = child.children[0];
177
- return {
178
- cursor: child,
179
- anchor,
180
- userTip: anchor.children[0]
181
- };
182
- }
183
- const userTip = createElement('span', {
184
- styles: {
185
- position: 'absolute',
186
- display: 'none',
187
- left: '50%',
188
- transform: 'translateX(-50%)',
189
- marginBottom: '2px',
190
- bottom: '100%',
191
- whiteSpace: 'nowrap',
192
- color: '#fff',
193
- boxShadow: '0 1px 2px rgba(0,0,0,.1)',
194
- borderRadius: '3px',
195
- padding: '3px 5px',
196
- pointerEvents: 'none',
197
- }
198
- });
199
- const anchor = createElement('span', {
200
- styles: {
201
- position: 'absolute',
202
- top: '-2px',
203
- left: '-2px',
204
- width: '6px',
205
- height: '6px',
206
- pointerEvents: 'auto',
207
- pointer: 'cursor',
208
- },
209
- children: [userTip],
210
- on: {
211
- mouseenter() {
212
- userTip.style.display = 'block';
213
- },
214
- mouseleave() {
215
- userTip.style.display = 'none';
216
- }
217
- }
218
- });
219
- child = createElement('span', {
220
- styles: {
221
- position: 'absolute',
222
- },
223
- children: [
224
- anchor
225
- ]
226
- });
227
- this.tooltips.append(child);
228
- return {
229
- cursor: child,
230
- anchor,
231
- userTip
232
- };
233
- }
234
- };
235
- CollaborateCursor = __decorate([
236
- Injectable(),
237
- __param(0, Inject(EDITOR_CONTAINER)),
238
- __param(1, Inject(EDITABLE_DOCUMENT)),
239
- __metadata("design:paramtypes", [HTMLElement,
240
- Document,
241
- SelectionBridge,
242
- Selection])
243
- ], CollaborateCursor);
244
- export { CollaborateCursor };
245
- //# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiY29sbGFib3JhdGUtY3Vyc29yLmpzIiwic291cmNlUm9vdCI6IiIsInNvdXJjZXMiOlsiLi4vLi4vc3JjL2NvbGxhYi9jb2xsYWJvcmF0ZS1jdXJzb3IudHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6Ijs7Ozs7Ozs7Ozs7O0FBQUEsT0FBTyxFQUFFLE1BQU0sRUFBRSxVQUFVLEVBQUUsTUFBTSxXQUFXLENBQUE7QUFDOUMsT0FBTyxFQUNMLGFBQWEsRUFDYixpQkFBaUIsRUFDakIsZ0JBQWdCLEVBQ2hCLG9CQUFvQixFQUNwQixlQUFlLEVBQ2hCLE1BQU0sa0JBQWtCLENBQUE7QUFDekIsT0FBTyxFQUFFLFNBQVMsRUFBa0IsTUFBTSxlQUFlLENBQUE7QUFDekQsT0FBTyxFQUFFLE9BQU8sRUFBRSxNQUFNLGVBQWUsQ0FBQTtBQXdCdkMsSUFBYSxpQkFBaUIsR0FBOUIsTUFBYSxpQkFBaUI7SUE0Q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lKCFpdGVtLnBhdGhzLmZvY3VzRW5kKVxuXG4gICAgICAgICAgY29uc3QgY3Vyc29yUmVjdCA9IGdldExheW91dFJlY3RCeVJhbmdlKGN1cnNvclJhbmdlKVxuXG4gICAgICAgICAgdXNlcnMucHVzaCh7XG4gICAgICAgICAgICB1c2VybmFtZTogaXRlbS51c2VybmFtZSxcbiAgICAgICAgICAgIGNvbG9yOiBpdGVtLmNvbG9yLFxuICAgICAgICAgICAgeDogY3Vyc29yUmVjdC54IC0gY29udGFpbmVyUmVjdC54LFxuICAgICAgICAgICAgeTogY3Vyc29yUmVjdC55IC0gY29udGFpbmVyUmVjdC55LFxuICAgICAgICAgICAgd2lkdGg6IDIsXG4gICAgICAgICAgICBoZWlnaHQ6IGN1cnNvclJlY3QuaGVpZ2h0XG4gICAgICAgICAgfSlcbiAgICAgICAgfVxuICAgICAgfVxuICAgIH0pXG4gICAgdGhpcy5kcmF3VXNlckN1cnNvcih1c2VycylcbiAgfVxuXG4gIHByaXZhdGUgZHJhd1VzZXJDdXJzb3IocmVjdHM6IFNlbGVjdGlvblJlY3RbXSkge1xuICAgIGZvciAobGV0IGkgPSAwOyBpIDwgcmVjdHMubGVuZ3RoOyBpKyspIHtcbiAgICAgIGNvbnN0IHJlY3QgPSByZWN0c1tpXVxuICAgICAgY29uc3Qge2N1cnNvciwgdXNlclRpcCwgYW5jaG9yfSA9IHRoaXMuZ2V0VXNlckN1cnNvcihpKVxuICAgICAgT2JqZWN0LmFzc2lnbihjdXJzb3Iuc3R5bGUsIHtcbiAgICAgICAgbGVmdDogcmVjdC54ICsgJ3B4JyxcbiAgICAgICAgdG9wOiByZWN0LnkgKyAncHgnLFxuICAgICAgICB3aWR0aDogcmVjdC53aWR0aCArICdweCcsXG4gICAgICAgIGhlaWdodDogcmVjdC5oZWlnaHQgKyAncHgnLFxuICAgICAgICBiYWNrZ3JvdW5kOiByZWN0LmNvbG9yLFxuICAgICAgICBkaXNwbGF5OiAnYmxvY2snXG4gICAgICB9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cbiAgICAgIF1cbiAgICB9KVxuICAgIHRoaXMudG9vbHRpcHMuYXBwZW5kKGNoaWxkKVxuICAgIHJldHVybiB7XG4gICAgICBjdXJzb3I6IGNoaWxkLFxuICAgICAgYW5jaG9yLFxuICAgICAgdXNlclRpcFxuICAgIH1cbiAgfVxufVxuIl19
@@ -1 +0,0 @@
1
- export * from './collaborate-cursor'
@@ -1,225 +0,0 @@
1
- import { Inject, Injectable } from '@tanbo/di'
2
- import {
3
- createElement,
4
- EDITABLE_DOCUMENT,
5
- EDITOR_CONTAINER,
6
- getLayoutRectByRange,
7
- SelectionBridge
8
- } from '@textbus/browser'
9
- import { Selection, SelectionPaths } from '@textbus/core'
10
- import { Subject } from '@tanbo/stream'
11
-
12
- export interface RemoteSelection {
13
- color: string
14
- username: string
15
- paths: SelectionPaths
16
- }
17
-
18
- export interface SelectionRect {
19
- color: string
20
- username: string
21
- x: number
22
- y: number
23
- width: number
24
- height: number
25
- }
26
-
27
- export interface RemoteSelectionCursor {
28
- cursor: HTMLElement
29
- anchor: HTMLElement
30
- userTip: HTMLElement
31
- }
32
-
33
- @Injectable()
34
- export class CollaborateCursor {
35
- private canvas = createElement('canvas', {
36
- styles: {
37
- position: 'absolute',
38
- opacity: 0.5,
39
- left: 0,
40
- top: 0,
41
- width: '100%',
42
- height: '100%',
43
- pointerEvents: 'none'
44
- }
45
- }) as HTMLCanvasElement
46
- private context = this.canvas.getContext('2d')!
47
- private tooltips = createElement('div', {
48
- styles: {
49
- position: 'absolute',
50
- left: 0,
51
- top: 0,
52
- width: '100%',
53
- height: '100%',
54
- pointerEvents: 'none',
55
- fontSize: '12px',
56
- zIndex: 10
57
- }
58
- })
59
-
60
- private onRectsChange = new Subject<SelectionRect[]>()
61
-
62
- constructor(@Inject(EDITOR_CONTAINER) private container: HTMLElement,
63
- @Inject(EDITABLE_DOCUMENT) private document: Document,
64
- private nativeSelection: SelectionBridge,
65
- private selection: Selection) {
66
- container.prepend(this.canvas, this.tooltips)
67
- this.onRectsChange.subscribe(rects => {
68
- for (const rect of rects) {
69
- this.context.fillStyle = rect.color
70
- this.context.beginPath()
71
- this.context.rect(rect.x, rect.y, rect.width, rect.height)
72
- this.context.fill()
73
- this.context.closePath()
74
- }
75
- })
76
- }
77
-
78
- draw(paths: RemoteSelection[]) {
79
- const containerRect = this.container.getBoundingClientRect()
80
- this.canvas.width = containerRect.width
81
- this.canvas.height = containerRect.height
82
- this.context.clearRect(0, 0, this.canvas.width, this.canvas.height)
83
-
84
- const users: SelectionRect[] = []
85
-
86
-
87
- paths.filter(i => {
88
- return i.paths.start.length && i.paths.end.length
89
- }).forEach(item => {
90
- const startOffset = item.paths.start.pop()!
91
- const startSlot = this.selection.findSlotByPaths(item.paths.start)
92
- const endOffset = item.paths.end.pop()!
93
- const endSlot = this.selection.findSlotByPaths(item.paths.end)
94
-
95
- if (startSlot && endSlot) {
96
- const position = this.nativeSelection.getPositionByRange({
97
- startOffset,
98
- endOffset,
99
- startSlot,
100
- endSlot
101
- })
102
- if (position.start && position.end) {
103
- const nativeRange = this.document.createRange()
104
- nativeRange.setStart(position.start.node, position.start.offset)
105
- nativeRange.setEnd(position.end.node, position.end.offset)
106
-
107
- const rects = nativeRange.getClientRects()
108
- const selectionRects: SelectionRect[] = []
109
- for (let i = rects.length - 1; i >= 0; i--) {
110
- const rect = rects[i]
111
- selectionRects.push({
112
- color: item.color,
113
- username: item.username,
114
- x: rect.x - containerRect.x,
115
- y: rect.y - containerRect.y,
116
- width: rect.width,
117
- height: rect.height,
118
- })
119
- }
120
- this.onRectsChange.next(selectionRects)
121
-
122
- const cursorRange = nativeRange.cloneRange()
123
- cursorRange.collapse(!item.paths.focusEnd)
124
-
125
- const cursorRect = getLayoutRectByRange(cursorRange)
126
-
127
- users.push({
128
- username: item.username,
129
- color: item.color,
130
- x: cursorRect.x - containerRect.x,
131
- y: cursorRect.y - containerRect.y,
132
- width: 2,
133
- height: cursorRect.height
134
- })
135
- }
136
- }
137
- })
138
- this.drawUserCursor(users)
139
- }
140
-
141
- private drawUserCursor(rects: SelectionRect[]) {
142
- for (let i = 0; i < rects.length; i++) {
143
- const rect = rects[i]
144
- const {cursor, userTip, anchor} = this.getUserCursor(i)
145
- Object.assign(cursor.style, {
146
- left: rect.x + 'px',
147
- top: rect.y + 'px',
148
- width: rect.width + 'px',
149
- height: rect.height + 'px',
150
- background: rect.color,
151
- display: 'block'
152
- })
153
- anchor.style.background = rect.color
154
- userTip.innerText = rect.username
155
- userTip.style.background = rect.color
156
- }
157
-
158
- for (let i = rects.length; i < this.tooltips.children.length; i++) {
159
- this.tooltips.removeChild(this.tooltips.children[i])
160
- }
161
- }
162
-
163
- private getUserCursor(index: number): RemoteSelectionCursor {
164
- let child: HTMLElement = this.tooltips.children[index] as HTMLElement
165
- if (child) {
166
- const anchor = child.children[0] as HTMLElement
167
- return {
168
- cursor: child,
169
- anchor,
170
- userTip: anchor.children[0] as HTMLElement
171
- }
172
- }
173
- const userTip = createElement('span', {
174
- styles: {
175
- position: 'absolute',
176
- display: 'none',
177
- left: '50%',
178
- transform: 'translateX(-50%)',
179
- marginBottom: '2px',
180
- bottom: '100%',
181
- whiteSpace: 'nowrap',
182
- color: '#fff',
183
- boxShadow: '0 1px 2px rgba(0,0,0,.1)',
184
- borderRadius: '3px',
185
- padding: '3px 5px',
186
- pointerEvents: 'none',
187
- }
188
- })
189
-
190
- const anchor = createElement('span', {
191
- styles: {
192
- position: 'absolute',
193
- top: '-2px',
194
- left: '-2px',
195
- width: '6px',
196
- height: '6px',
197
- pointerEvents: 'auto',
198
- pointer: 'cursor',
199
- },
200
- children: [userTip],
201
- on: {
202
- mouseenter() {
203
- userTip.style.display = 'block'
204
- },
205
- mouseleave() {
206
- userTip.style.display = 'none'
207
- }
208
- }
209
- })
210
- child = createElement('span', {
211
- styles: {
212
- position: 'absolute',
213
- },
214
- children: [
215
- anchor
216
- ]
217
- })
218
- this.tooltips.append(child)
219
- return {
220
- cursor: child,
221
- anchor,
222
- userTip
223
- }
224
- }
225
- }