@textbus/collaborate 2.0.0-beta.5 → 2.0.0-beta.50
Sign up to get free protection for your applications and to get access to all the features.
- package/bundles/collaborate-cursor.d.ts +18 -6
- package/bundles/collaborate-cursor.js +150 -119
- package/bundles/collaborate.d.ts +10 -4
- package/bundles/collaborate.js +241 -208
- package/bundles/public-api.d.ts +2 -0
- package/bundles/public-api.js +14 -1
- package/bundles/unknown.component.d.ts +1 -0
- package/bundles/unknown.component.js +22 -0
- package/package.json +7 -7
- package/src/collaborate-cursor.ts +141 -66
- package/src/collaborate.ts +238 -59
- package/src/public-api.ts +16 -0
- package/src/unknown.component.ts +22 -0
- package/tsconfig-build.json +1 -1
package/bundles/public-api.d.ts
CHANGED
package/bundles/public-api.js
CHANGED
@@ -1,3 +1,16 @@
|
|
1
|
+
import { History } from '@textbus/core';
|
2
|
+
import { Collaborate } from './collaborate';
|
3
|
+
import { CollaborateCursor } from './collaborate-cursor';
|
1
4
|
export * from './collaborate';
|
2
5
|
export * from './collaborate-cursor';
|
3
|
-
|
6
|
+
export const collaborateModule = {
|
7
|
+
providers: [
|
8
|
+
Collaborate,
|
9
|
+
CollaborateCursor,
|
10
|
+
{
|
11
|
+
provide: History,
|
12
|
+
useClass: Collaborate
|
13
|
+
}
|
14
|
+
]
|
15
|
+
};
|
16
|
+
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoicHVibGljLWFwaS5qcyIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzIjpbIi4uL3NyYy9wdWJsaWMtYXBpLnRzIl0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiJBQUFBLE9BQU8sRUFBRSxPQUFPLEVBQVUsTUFBTSxlQUFlLENBQUE7QUFFL0MsT0FBTyxFQUFFLFdBQVcsRUFBRSxNQUFNLGVBQWUsQ0FBQTtBQUMzQyxPQUFPLEVBQUUsaUJBQWlCLEVBQUUsTUFBTSxzQkFBc0IsQ0FBQTtBQUV4RCxjQUFjLGVBQWUsQ0FBQTtBQUM3QixjQUFjLHNCQUFzQixDQUFBO0FBRXBDLE1BQU0sQ0FBQyxNQUFNLGlCQUFpQixHQUFXO0lBQ3ZDLFNBQVMsRUFBRTtRQUNULFdBQVc7UUFDWCxpQkFBaUI7UUFDakI7WUFDRSxPQUFPLEVBQUUsT0FBTztZQUNoQixRQUFRLEVBQUUsV0FBVztTQUN0QjtLQUNGO0NBQ0YsQ0FBQSIsInNvdXJjZXNDb250ZW50IjpbImltcG9ydCB7IEhpc3RvcnksIE1vZHVsZSB9IGZyb20gJ0B0ZXh0YnVzL2NvcmUnXG5cbmltcG9ydCB7IENvbGxhYm9yYXRlIH0gZnJvbSAnLi9jb2xsYWJvcmF0ZSdcbmltcG9ydCB7IENvbGxhYm9yYXRlQ3Vyc29yIH0gZnJvbSAnLi9jb2xsYWJvcmF0ZS1jdXJzb3InXG5cbmV4cG9ydCAqIGZyb20gJy4vY29sbGFib3JhdGUnXG5leHBvcnQgKiBmcm9tICcuL2NvbGxhYm9yYXRlLWN1cnNvcidcblxuZXhwb3J0IGNvbnN0IGNvbGxhYm9yYXRlTW9kdWxlOiBNb2R1bGUgPSB7XG4gIHByb3ZpZGVyczogW1xuICAgIENvbGxhYm9yYXRlLFxuICAgIENvbGxhYm9yYXRlQ3Vyc29yLFxuICAgIHtcbiAgICAgIHByb3ZpZGU6IEhpc3RvcnksXG4gICAgICB1c2VDbGFzczogQ29sbGFib3JhdGVcbiAgICB9XG4gIF1cbn1cbiJdfQ==
|
@@ -0,0 +1 @@
|
|
1
|
+
export declare function createUnknownComponent(factoryName: string, canInsertInlineComponent: boolean): any;
|
@@ -0,0 +1,22 @@
|
|
1
|
+
import { ContentType, defineComponent, VElement } from '@textbus/core';
|
2
|
+
export function createUnknownComponent(factoryName, canInsertInlineComponent) {
|
3
|
+
const unknownComponent = defineComponent({
|
4
|
+
type: canInsertInlineComponent ? ContentType.InlineComponent : ContentType.BlockComponent,
|
5
|
+
name: 'UnknownComponent',
|
6
|
+
setup() {
|
7
|
+
console.error(`cannot find component factory \`${factoryName}\`.`);
|
8
|
+
return {
|
9
|
+
render() {
|
10
|
+
return VElement.createElement('textbus-unknown-component', {
|
11
|
+
style: {
|
12
|
+
display: canInsertInlineComponent ? 'inline' : 'block',
|
13
|
+
color: '#f00'
|
14
|
+
}
|
15
|
+
}, unknownComponent.name);
|
16
|
+
}
|
17
|
+
};
|
18
|
+
}
|
19
|
+
});
|
20
|
+
return unknownComponent;
|
21
|
+
}
|
22
|
+
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoidW5rbm93bi5jb21wb25lbnQuanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi9zcmMvdW5rbm93bi5jb21wb25lbnQudHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6IkFBQUEsT0FBTyxFQUFFLFdBQVcsRUFBRSxlQUFlLEVBQUUsUUFBUSxFQUFFLE1BQU0sZUFBZSxDQUFBO0FBRXRFLE1BQU0sVUFBVSxzQkFBc0IsQ0FBQyxXQUFtQixFQUFFLHdCQUFpQztJQUMzRixNQUFNLGdCQUFnQixHQUFHLGVBQWUsQ0FBQztRQUN2QyxJQUFJLEVBQUUsd0JBQXdCLENBQUMsQ0FBQyxDQUFDLFdBQVcsQ0FBQyxlQUFlLENBQUMsQ0FBQyxDQUFDLFdBQVcsQ0FBQyxjQUFjO1FBQ3pGLElBQUksRUFBRSxrQkFBa0I7UUFDeEIsS0FBSztZQUNILE9BQU8sQ0FBQyxLQUFLLENBQUMsbUNBQW1DLFdBQVcsS0FBSyxDQUFDLENBQUE7WUFDbEUsT0FBTztnQkFDTCxNQUFNO29CQUNKLE9BQU8sUUFBUSxDQUFDLGFBQWEsQ0FBQywyQkFBMkIsRUFBRTt3QkFDekQsS0FBSyxFQUFFOzRCQUNMLE9BQU8sRUFBRSx3QkFBd0IsQ0FBQyxDQUFDLENBQUMsUUFBUSxDQUFDLENBQUMsQ0FBQyxPQUFPOzRCQUN0RCxLQ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4gICAgICAgICAgfSwgdW5rbm93bkNvbXBvbmVudC5uYW1lKVxuICAgICAgICB9XG4gICAgICB9XG4gICAgfVxuICB9KVxuICByZXR1cm4gdW5rbm93bkNvbXBvbmVudFxufVxuIl19
|
package/package.json
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
{
|
2
2
|
"name": "@textbus/collaborate",
|
3
|
-
"version": "2.0.0-beta.
|
3
|
+
"version": "2.0.0-beta.50",
|
4
4
|
"description": "Textbus is a rich text editor and framework that is highly customizable and extensible to achieve rich wysiwyg effects.",
|
5
5
|
"main": "./bundles/public-api.js",
|
6
6
|
"module": "./bundles/public-api.js",
|
@@ -25,13 +25,13 @@
|
|
25
25
|
"typescript editor"
|
26
26
|
],
|
27
27
|
"dependencies": {
|
28
|
-
"@tanbo/di": "^1.1.
|
29
|
-
"@tanbo/stream": "^1.
|
30
|
-
"@textbus/browser": "^2.0.0-beta.
|
31
|
-
"@textbus/core": "^2.0.0-beta.
|
28
|
+
"@tanbo/di": "^1.1.1",
|
29
|
+
"@tanbo/stream": "^1.1.3",
|
30
|
+
"@textbus/browser": "^2.0.0-beta.50",
|
31
|
+
"@textbus/core": "^2.0.0-beta.50",
|
32
32
|
"reflect-metadata": "^0.1.13",
|
33
33
|
"y-protocols": "^1.0.5",
|
34
|
-
"yjs": "^13.5.
|
34
|
+
"yjs": "^13.5.39"
|
35
35
|
},
|
36
36
|
"author": {
|
37
37
|
"name": "Tanbo",
|
@@ -44,5 +44,5 @@
|
|
44
44
|
"bugs": {
|
45
45
|
"url": "https://github.com/textbus/textbus.git/issues"
|
46
46
|
},
|
47
|
-
"gitHead": "
|
47
|
+
"gitHead": "62add70ee0449f8d23a398ef67a3041e3d72e9b7"
|
48
48
|
}
|
@@ -1,13 +1,12 @@
|
|
1
|
-
import { Inject, Injectable } from '@tanbo/di'
|
1
|
+
import { Inject, Injectable, Optional } from '@tanbo/di'
|
2
2
|
import {
|
3
3
|
createElement,
|
4
|
-
|
5
|
-
EDITOR_CONTAINER,
|
4
|
+
VIEW_CONTAINER,
|
6
5
|
getLayoutRectByRange,
|
7
|
-
SelectionBridge
|
6
|
+
SelectionBridge, VIEW_SCROLLER
|
8
7
|
} from '@textbus/browser'
|
9
|
-
import { Selection, SelectionPaths } from '@textbus/core'
|
10
|
-
import { Subject } from '@tanbo/stream'
|
8
|
+
import { Selection, SelectionPaths, Range as TBRange } from '@textbus/core'
|
9
|
+
import { fromEvent, Subject, Subscription } from '@tanbo/stream'
|
11
10
|
|
12
11
|
export interface RemoteSelection {
|
13
12
|
color: string
|
@@ -15,23 +14,51 @@ export interface RemoteSelection {
|
|
15
14
|
paths: SelectionPaths
|
16
15
|
}
|
17
16
|
|
18
|
-
export interface
|
19
|
-
color: string
|
20
|
-
username: string
|
17
|
+
export interface Rect {
|
21
18
|
x: number
|
22
19
|
y: number
|
23
20
|
width: number
|
24
21
|
height: number
|
25
22
|
}
|
26
23
|
|
24
|
+
export interface SelectionRect extends Rect {
|
25
|
+
color: string
|
26
|
+
username: string
|
27
|
+
}
|
28
|
+
|
27
29
|
export interface RemoteSelectionCursor {
|
28
30
|
cursor: HTMLElement
|
29
31
|
anchor: HTMLElement
|
30
32
|
userTip: HTMLElement
|
31
33
|
}
|
32
34
|
|
35
|
+
export abstract class CollaborateCursorAwarenessDelegate {
|
36
|
+
abstract getRects(range: TBRange, nativeRange: Range): false | Rect[]
|
37
|
+
}
|
38
|
+
|
33
39
|
@Injectable()
|
34
40
|
export class CollaborateCursor {
|
41
|
+
private host = createElement('div', {
|
42
|
+
styles: {
|
43
|
+
position: 'absolute',
|
44
|
+
left: 0,
|
45
|
+
top: 0,
|
46
|
+
width: '100%',
|
47
|
+
height: '100%',
|
48
|
+
pointerEvents: 'none',
|
49
|
+
zIndex: 1
|
50
|
+
}
|
51
|
+
})
|
52
|
+
private canvasContainer = createElement('div', {
|
53
|
+
styles: {
|
54
|
+
position: 'absolute',
|
55
|
+
left: 0,
|
56
|
+
top: 0,
|
57
|
+
width: '100%',
|
58
|
+
height: '100%',
|
59
|
+
overflow: 'hidden'
|
60
|
+
}
|
61
|
+
})
|
35
62
|
private canvas = createElement('canvas', {
|
36
63
|
styles: {
|
37
64
|
position: 'absolute',
|
@@ -39,8 +66,8 @@ export class CollaborateCursor {
|
|
39
66
|
left: 0,
|
40
67
|
top: 0,
|
41
68
|
width: '100%',
|
42
|
-
height: '
|
43
|
-
pointerEvents: 'none'
|
69
|
+
height: document.documentElement.clientHeight + 'px',
|
70
|
+
pointerEvents: 'none',
|
44
71
|
}
|
45
72
|
}) as HTMLCanvasElement
|
46
73
|
private context = this.canvas.getContext('2d')!
|
@@ -59,12 +86,18 @@ export class CollaborateCursor {
|
|
59
86
|
|
60
87
|
private onRectsChange = new Subject<SelectionRect[]>()
|
61
88
|
|
62
|
-
|
63
|
-
|
89
|
+
private subscription = new Subscription()
|
90
|
+
private currentSelection: RemoteSelection[] = []
|
91
|
+
|
92
|
+
constructor(@Inject(VIEW_CONTAINER) private container: HTMLElement,
|
93
|
+
@Optional() @Inject(VIEW_SCROLLER) private scroller: HTMLElement,
|
94
|
+
@Optional() private awarenessDelegate: CollaborateCursorAwarenessDelegate,
|
64
95
|
private nativeSelection: SelectionBridge,
|
65
96
|
private selection: Selection) {
|
66
|
-
|
67
|
-
this.
|
97
|
+
this.canvasContainer.append(this.canvas)
|
98
|
+
this.host.append(this.canvasContainer, this.tooltips)
|
99
|
+
container.prepend(this.host)
|
100
|
+
this.subscription.add(this.onRectsChange.subscribe(rects => {
|
68
101
|
for (const rect of rects) {
|
69
102
|
this.context.fillStyle = rect.color
|
70
103
|
this.context.beginPath()
|
@@ -72,68 +105,110 @@ export class CollaborateCursor {
|
|
72
105
|
this.context.fill()
|
73
106
|
this.context.closePath()
|
74
107
|
}
|
75
|
-
})
|
108
|
+
}), fromEvent(window, 'resize').subscribe(() => {
|
109
|
+
this.canvas.style.height = document.documentElement.clientHeight + 'px'
|
110
|
+
this.refresh()
|
111
|
+
}))
|
112
|
+
|
113
|
+
if (scroller) {
|
114
|
+
this.subscription.add(fromEvent(scroller, 'scroll').subscribe(() => {
|
115
|
+
this.refresh()
|
116
|
+
}))
|
117
|
+
}
|
118
|
+
}
|
119
|
+
|
120
|
+
refresh() {
|
121
|
+
this.draw(this.currentSelection)
|
122
|
+
}
|
123
|
+
|
124
|
+
destroy() {
|
125
|
+
this.subscription.unsubscribe()
|
76
126
|
}
|
77
127
|
|
78
128
|
draw(paths: RemoteSelection[]) {
|
129
|
+
this.currentSelection = paths
|
79
130
|
const containerRect = this.container.getBoundingClientRect()
|
80
|
-
this.canvas.
|
81
|
-
this.canvas.
|
131
|
+
this.canvas.style.top = containerRect.y * -1 + 'px'
|
132
|
+
this.canvas.width = this.canvas.offsetWidth
|
133
|
+
this.canvas.height = this.canvas.offsetHeight
|
82
134
|
this.context.clearRect(0, 0, this.canvas.width, this.canvas.height)
|
83
135
|
|
84
136
|
const users: SelectionRect[] = []
|
85
137
|
|
86
|
-
|
87
138
|
paths.filter(i => {
|
88
|
-
return i.paths.
|
139
|
+
return i.paths.anchor.length && i.paths.focus.length
|
89
140
|
}).forEach(item => {
|
90
|
-
const
|
91
|
-
const
|
92
|
-
const
|
93
|
-
const
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
141
|
+
const anchorPaths = [...item.paths.anchor]
|
142
|
+
const focusPaths = [...item.paths.focus]
|
143
|
+
const anchorOffset = anchorPaths.pop()!
|
144
|
+
const anchorSlot = this.selection.findSlotByPaths(anchorPaths)
|
145
|
+
const focusOffset = focusPaths.pop()!
|
146
|
+
const focusSlot = this.selection.findSlotByPaths(focusPaths)
|
147
|
+
if (!anchorSlot || !focusSlot) {
|
148
|
+
return
|
149
|
+
}
|
150
|
+
|
151
|
+
const { focus, anchor } = this.nativeSelection.getPositionByRange({
|
152
|
+
focusOffset,
|
153
|
+
anchorOffset,
|
154
|
+
focusSlot,
|
155
|
+
anchorSlot
|
156
|
+
})
|
157
|
+
if (!focus || !anchor) {
|
158
|
+
return
|
159
|
+
}
|
160
|
+
const nativeRange = document.createRange()
|
161
|
+
nativeRange.setStart(anchor.node, anchor.offset)
|
162
|
+
nativeRange.setEnd(focus.node, focus.offset)
|
163
|
+
if ((anchor.node !== focus.node || anchor.offset !== focus.offset) && nativeRange.collapsed) {
|
164
|
+
nativeRange.setStart(focus.node, focus.offset)
|
165
|
+
nativeRange.setEnd(anchor.node, anchor.offset)
|
166
|
+
}
|
167
|
+
|
168
|
+
let rects: Rect[] | DOMRectList | false = false
|
169
|
+
if (this.awarenessDelegate) {
|
170
|
+
rects = this.awarenessDelegate.getRects({
|
171
|
+
focusOffset,
|
172
|
+
anchorOffset,
|
173
|
+
focusSlot,
|
174
|
+
anchorSlot
|
175
|
+
}, nativeRange)
|
176
|
+
}
|
177
|
+
if (!rects) {
|
178
|
+
rects = nativeRange.getClientRects()
|
179
|
+
}
|
180
|
+
const selectionRects: SelectionRect[] = []
|
181
|
+
for (let i = rects.length - 1; i >= 0; i--) {
|
182
|
+
const rect = rects[i]
|
183
|
+
selectionRects.push({
|
184
|
+
color: item.color,
|
185
|
+
username: item.username,
|
186
|
+
x: rect.x - containerRect.x,
|
187
|
+
y: rect.y,
|
188
|
+
width: rect.width,
|
189
|
+
height: rect.height,
|
101
190
|
})
|
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
191
|
}
|
192
|
+
this.onRectsChange.next(selectionRects)
|
193
|
+
|
194
|
+
const cursorRange = nativeRange.cloneRange()
|
195
|
+
cursorRange.setStart(focus.node, focus.offset)
|
196
|
+
cursorRange.collapse(true)
|
197
|
+
|
198
|
+
const cursorRect = getLayoutRectByRange(cursorRange)
|
199
|
+
|
200
|
+
const rect: SelectionRect = {
|
201
|
+
username: item.username,
|
202
|
+
color: item.color,
|
203
|
+
x: cursorRect.x - containerRect.x,
|
204
|
+
y: cursorRect.y - containerRect.y,
|
205
|
+
width: 2,
|
206
|
+
height: cursorRect.height
|
207
|
+
}
|
208
|
+
if (rect.x < 0 || rect.y < 0 || rect.x > containerRect.width) {
|
209
|
+
return
|
210
|
+
}
|
211
|
+
users.push(rect)
|
137
212
|
})
|
138
213
|
this.drawUserCursor(users)
|
139
214
|
}
|
@@ -141,7 +216,7 @@ export class CollaborateCursor {
|
|
141
216
|
private drawUserCursor(rects: SelectionRect[]) {
|
142
217
|
for (let i = 0; i < rects.length; i++) {
|
143
218
|
const rect = rects[i]
|
144
|
-
const {cursor, userTip, anchor} = this.getUserCursor(i)
|
219
|
+
const { cursor, userTip, anchor } = this.getUserCursor(i)
|
145
220
|
Object.assign(cursor.style, {
|
146
221
|
left: rect.x + 'px',
|
147
222
|
top: rect.y + 'px',
|