@textbus/collaborate 2.0.0-alpha.39 → 2.0.0-alpha.42
Sign up to get free protection for your applications and to get access to all the features.
- package/bundles/collab/collaborate-cursor.d.ts +13 -1
- package/bundles/collab/collaborate-cursor.js +132 -21
- package/bundles/collab/collaborate-cursor.js.map +1 -1
- package/bundles/collab/local-to-remote.js +7 -4
- package/bundles/collab/local-to-remote.js.map +1 -1
- package/bundles/collab/remote-to-local.js +13 -4
- package/bundles/collab/remote-to-local.js.map +1 -1
- package/package.json +5 -5
- package/src/collab/collaborate-cursor.ts +147 -21
- package/src/collab/local-to-remote.ts +8 -5
- package/src/collab/remote-to-local.ts +14 -6
@@ -10,7 +10,16 @@ export interface SelectionRect {
|
|
10
10
|
y: number;
|
11
11
|
width: number;
|
12
12
|
height: number;
|
13
|
+
}
|
14
|
+
export interface SelectionInfo {
|
13
15
|
color: string;
|
16
|
+
username: string;
|
17
|
+
rects: SelectionRect[];
|
18
|
+
}
|
19
|
+
export interface RemoteSelectionCursor {
|
20
|
+
cursor: HTMLElement;
|
21
|
+
anchor: HTMLElement;
|
22
|
+
userTip: HTMLElement;
|
14
23
|
}
|
15
24
|
export declare class CollaborateCursor {
|
16
25
|
private container;
|
@@ -19,7 +28,10 @@ export declare class CollaborateCursor {
|
|
19
28
|
private selection;
|
20
29
|
private canvas;
|
21
30
|
private context;
|
22
|
-
private
|
31
|
+
private tooltips;
|
32
|
+
private onRectsChange;
|
23
33
|
constructor(container: HTMLElement, document: Document, nativeSelection: SelectionBridge, selection: Selection);
|
24
34
|
draw(paths: RemoteSelection[]): void;
|
35
|
+
private drawUserCursor;
|
36
|
+
private getUserCursor;
|
25
37
|
}
|
@@ -62,25 +62,47 @@ let CollaborateCursor = class CollaborateCursor {
|
|
62
62
|
writable: true,
|
63
63
|
value: this.canvas.getContext('2d')
|
64
64
|
});
|
65
|
-
Object.defineProperty(this, "
|
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", {
|
66
82
|
enumerable: true,
|
67
83
|
configurable: true,
|
68
84
|
writable: true,
|
69
85
|
value: new Subject()
|
70
86
|
});
|
71
87
|
container.appendChild(this.canvas);
|
72
|
-
this.
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
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
|
+
}
|
78
99
|
});
|
79
100
|
}
|
80
101
|
draw(paths) {
|
81
102
|
this.canvas.width = this.container.offsetWidth;
|
82
103
|
this.canvas.height = this.container.offsetHeight;
|
83
104
|
this.context.clearRect(0, 0, this.canvas.width, this.canvas.height);
|
105
|
+
const users = [];
|
84
106
|
paths.filter(i => {
|
85
107
|
return i.paths.start.length && i.paths.end.length;
|
86
108
|
}).forEach(item => {
|
@@ -100,34 +122,123 @@ let CollaborateCursor = class CollaborateCursor {
|
|
100
122
|
nativeRange.setStart(position.start.node, position.start.offset);
|
101
123
|
nativeRange.setEnd(position.end.node, position.end.offset);
|
102
124
|
const rects = nativeRange.getClientRects();
|
103
|
-
|
125
|
+
const selectionRects = [];
|
104
126
|
for (let i = rects.length - 1; i >= 0; i--) {
|
105
127
|
const rect = rects[i];
|
106
|
-
|
107
|
-
continue;
|
108
|
-
}
|
109
|
-
prev = rect;
|
110
|
-
this.onRectChange.next({
|
128
|
+
selectionRects.push({
|
111
129
|
x: rect.x,
|
112
130
|
y: rect.y,
|
113
131
|
width: rect.width,
|
114
132
|
height: rect.height,
|
115
|
-
color: item.color
|
116
133
|
});
|
117
134
|
}
|
118
135
|
if (rects.length === 0) {
|
119
136
|
const rect = nativeRange.getBoundingClientRect();
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
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
|
+
}
|
127
145
|
}
|
146
|
+
const info = Object.assign(Object.assign({}, item), { rects: selectionRects });
|
147
|
+
this.onRectsChange.next(info);
|
148
|
+
users.push(info);
|
128
149
|
}
|
129
150
|
}
|
130
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
|
+
};
|
131
242
|
}
|
132
243
|
};
|
133
244
|
CollaborateCursor = __decorate([
|
@@ -1 +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,gBAAgB,EAAE,eAAe,EAAE,MAAM,kBAAkB,CAAA;AACtG,OAAO,EAAE,SAAS,EAAkB,MAAM,eAAe,CAAA;AACzD,OAAO,EAAE,OAAO,EAAE,MAAM,eAAe,CAAA;AAiBvC,IAAa,iBAAiB,GAA9B,MAAa,iBAAiB;IAgB5B,YAA8C,SAAsB,EACrB,QAAkB,EAC7C,eAAgC,EAChC,SAAoB;;;;;mBAHM;;;;;;mBACC;;;;;;mBAC3B;;;;;;mBACA;;QAlBpB;;;;mBAAiB,aAAa,CAAC,QAAQ,EAAE;gBACvC,MAAM,EAAE;oBACN,QAAQ,EAAE,UAAU;oBACpB,OAAO,EAAE,GAAG;oBACZ,IAAI,EAAE,CAAC;oBACP,GAAG,EAAE,CAAC;oBACN,KAAK,EAAE,MAAM;oBACb,MAAM,EAAE,MAAM;oBACd,aAAa,EAAE,MAAM;iBACtB;aACF,CAAsB;WAAA;QACvB;;;;mBAAkB,IAAI,CAAC,MAAM,CAAC,UAAU,CAAC,IAAI,CAAE;WAAA;QAE/C;;;;mBAAuB,IAAI,OAAO,EAAiB;WAAA;QAMjD,SAAS,CAAC,WAAW,CAAC,IAAI,CAAC,MAAM,CAAC,CAAA;QAClC,IAAI,CAAC,YAAY,CAAC,SAAS,CAAC,IAAI,CAAC,EAAE;YACjC,IAAI,CAAC,OAAO,CAAC,SAAS,GAAG,IAAI,CAAC,KAAK,CAAA;YACnC,IAAI,CAAC,OAAO,CAAC,SAAS,EAAE,CAAA;YACxB,IAAI,CAAC,OAAO,CAAC,IAAI,CAAC,IAAI,CAAC,IAAI,CAAC,IAAI,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qBAAqB,EAAE,CAAA;wBAChD,IAAI,CAAC,YAAY,CAAC,IAAI,CAAC;4BACrB,CAAC,EAAE,IAAI,CAAC,CAAC;4BACT,CAAC,EAAE,IAAI,CAAC,CAAC;4BACT,KAAK,EAAE,CAAC;4BACR,MAAM,EAAE,IAAI,CAAC,MAAM;4BACnB,KAAK,EAAE,IAAI,CAAC,KAAK;yBAClB,CAAC,CAAA;qBACH;iBACF;aACF;QACH,CAAC,CAAC,CAAA;IACJ,CAAC;CACF,CAAA;AAtFY,iBAAiB;IAD7B,UAAU,EAAE;IAiBE,WAAA,MAAM,CAAC,gBAAgB,CAAC,CAAA;IACxB,WAAA,MAAM,CAAC,iBAAiB,CAAC,CAAA;qCADmB,WAAW;QACX,QAAQ;QAC5B,eAAe;QACrB,SAAS;GAnB7B,iBAAiB,CAsF7B;SAtFY,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 color: string\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\n private onRectChange = new Subject<SelectionRect>()\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 this.onRectChange.subscribe(rect => {\n this.context.fillStyle = rect.color\n this.context.beginPath()\n this.context.rect(Math.ceil(rect.x), Math.ceil(rect.y), Math.ceil(rect.width), Math.ceil(rect.height))\n this.context.fill()\n this.context.closePath()\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 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 let prev: any = {}\n for (let i = rects.length - 1; i >= 0; i--) {\n const rect = rects[i]\n if (prev.y === rect.y) {\n continue\n }\n prev = rect\n this.onRectChange.next({\n x: rect.x,\n y: rect.y,\n width: rect.width,\n height: rect.height,\n color: item.color\n })\n }\n\n if (rects.length === 0) {\n const rect = nativeRange.getBoundingClientRect()\n this.onRectChange.next({\n x: rect.x,\n y: rect.y,\n width: 1,\n height: rect.height,\n color: item.color\n })\n }\n }\n }\n })\n }\n}\n"]}
|
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,gBAAgB,EAAE,eAAe,EAAE,MAAM,kBAAkB,CAAA;AACtG,OAAO,EAAE,SAAS,EAAkB,MAAM,eAAe,CAAA;AACzD,OAAO,EAAE,OAAO,EAAE,MAAM,eAAe,CAAA;AA4BvC,IAAa,iBAAiB,GAA9B,MAAa,iBAAiB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M,EAAE;YACnC,MAAM,EAAE;gBACN,QAAQ,EAAE,UAAU;gBACpB,GAAG,EAAE,MAAM;gBACX,IAAI,EAAE,MAAM;gBACZ,KAAK,EAAE,KAAK;gBACZ,MAAM,EAAE,KAAK;gBACb,aAAa,EAAE,MAAM;gBACrB,OAAO,EAAE,QAAQ;aAClB;YACD,QAAQ,EAAE,CAAC,OAAO,CAAC;YACnB,EAAE,EAAE;gBACF,UAAU;oBACR,MAAM,CAAC,KAAK,CAAC,SAAS,GAAG,YAAY,CAAA;oBACrC,OAAO,CAAC,KAAK,CAAC,OAAO,GAAG,OAAO,CAAA;gBACjC,CAAC;gBACD,UAAU;oBACR,OAAO,CAAC,KAAK,CAAC,OAAO,GAAG,MAAM,CAAA;oBAC9B,MAAM,CAAC,KAAK,CAAC,SAAS,GAAG,EAAE,CAAA;gBAC7B,CAAC;aACF;SACF,CAAC,CAAA;QACF,KAAK,GAAG,aAAa,CAAC,MAAM,EAAE;YAC5B,MAAM,EAAE;gBACN,QAAQ,EAAE,UAAU;aACrB;YACD,QAAQ,EAAE;gBACR,MAAM;aACP;SACF,CAAC,CAAA;QACF,IAAI,CAAC,QAAQ,CAAC,MAAM,CAAC,KAAK,CAAC,CAAA;QAC3B,OAAO;YACL,MAAM,EAAE,KAAK;YACb,MAAM;YACN,OAAO;SACR,CAAA;IACH,CAAC;CACF,CAAA;AAzMY,iBAAiB;IAD7B,UAAU,EAAE;IA4BE,WAAA,MAAM,CAAC,gBAAgB,CAAC,CAAA;IACxB,WAAA,MAAM,CAAC,iBAAiB,CAAC,CAAA;qCADmB,WAAW;QACX,QAAQ;QAC5B,eAAe;QACrB,SAAS;GA9B7B,iBAAiB,CAyM7B;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,4 +1,6 @@
|
|
1
|
+
import { makeError } from '@textbus/core';
|
1
2
|
import { Array as YArray, Map as YMap, Text as YText } from 'yjs';
|
3
|
+
const collaborateErrorFn = makeError('Collaborate');
|
2
4
|
export class LocalToRemote {
|
3
5
|
transform(operation, root) {
|
4
6
|
const path = [...operation.path];
|
@@ -49,9 +51,9 @@ export class LocalToRemote {
|
|
49
51
|
return;
|
50
52
|
}
|
51
53
|
const content = slotYMap.get('content');
|
52
|
-
this.mergeActionsToSharedSlot(content, actions);
|
54
|
+
this.mergeActionsToSharedSlot(content, actions, slotYMap);
|
53
55
|
}
|
54
|
-
mergeActionsToSharedSlot(content, actions) {
|
56
|
+
mergeActionsToSharedSlot(content, actions, slotYMap) {
|
55
57
|
let index;
|
56
58
|
let length;
|
57
59
|
actions.forEach(action => {
|
@@ -91,7 +93,7 @@ export class LocalToRemote {
|
|
91
93
|
}
|
92
94
|
}
|
93
95
|
else if (action.type === 'apply') {
|
94
|
-
|
96
|
+
slotYMap === null || slotYMap === void 0 ? void 0 : slotYMap.set('state', action.value);
|
95
97
|
}
|
96
98
|
});
|
97
99
|
}
|
@@ -122,6 +124,7 @@ export class LocalToRemote {
|
|
122
124
|
const sharedSlot = new YMap();
|
123
125
|
sharedSlot.set('content', content);
|
124
126
|
sharedSlot.set('schema', slotLiteral.schema);
|
127
|
+
sharedSlot.set('state', slotLiteral.state);
|
125
128
|
return sharedSlot;
|
126
129
|
}
|
127
130
|
makeSharedComponentByComponentLiteral(componentLiteral) {
|
@@ -146,7 +149,7 @@ export class LocalToRemote {
|
|
146
149
|
i += action.insert instanceof YMap ? 1 : action.insert.length;
|
147
150
|
}
|
148
151
|
else {
|
149
|
-
throw
|
152
|
+
throw collaborateErrorFn('Unexpected delta action.');
|
150
153
|
}
|
151
154
|
}
|
152
155
|
return null;
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"local-to-remote.js","sourceRoot":"","sources":["../../src/collab/local-to-remote.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,KAAK,IAAI,MAAM,EAAE,GAAG,IAAI,IAAI,EAAE,IAAI,IAAI,KAAK,EAAE,MAAM,KAAK,CAAA;AAEjE,MAAM,OAAO,aAAa;IACxB,SAAS,CAAC,SAAoB,EAAE,IAAW;QACzC,MAAM,IAAI,GAAG,CAAC,GAAG,SAAS,CAAC,IAAI,CAAC,CAAA;QAChC,IAAI,CAAC,KAAK,EAAE,CAAA;QACZ,IAAI,IAAI,CAAC,MAAM,EAAE;YACf,MAAM,cAAc,GAAG,IAAI,CAAC,KAAK,EAAG,CAAA;YACpC,MAAM,eAAe,GAAG,IAAI,CAAC,yBAAyB,CAAC,IAAI,EAAE,cAAc,CAAC,CAAA;YAC5E,IAAI,eAAe,EAAE;gBACnB,IAAI,CAAC,wCAAwC,CAAC,IAAI,EAAE,SAAS,CAAC,KAAK,EAAE,eAAe,CAAC,CAAA;aACtF;YACD,OAAM;SACP;QACD,IAAI,CAAC,wBAAwB,CAAC,IAAI,EAAE,SAAS,CAAC,KAAK,CAAC,CAAA;IACtD,CAAC;IAEO,wCAAwC,CAAC,IAAc,EAAE,OAAiB,EAAE,aAAwB;QAC1G,MAAM,WAAW,GAAG,aAAa,CAAC,GAAG,CAAC,OAAO,CAAgB,CAAA;QAC7D,IAAI,IAAI,CAAC,MAAM,EAAE;YACf,MAAM,SAAS,GAAG,IA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IAAI,CAAC,OAAO,EAAE,CAAA;QAC5B,IAAI,CAAC,GAAG,CAAC,CAAA;QACT,KAAK,MAAM,MAAM,IAAI,KAAK,EAAE;YAC1B,IAAI,MAAM,CAAC,MAAM,EAAE;gBACjB,IAAI,CAAC,KAAK,KAAK,EAAE;oBACf,OAAO,MAAM,CAAC,MAAM,YAAY,IAAI,CAAC,CAAC,CAAC,MAAM,CAAC,MAAM,CAAC,CAAC,CAAC,IAAI,CAAA;iBAC5D;gBACD,CAAC,IAAI,MAAM,CAAC,MAAM,YAAY,IAAI,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,MAAM,CAAC,MAAM,CAAC,MAAM,CAAA;aAC9D;iBAAM;gBACL,MAAM,IAAI,KAAK,CAAC,KAAK,CAAC,CAAA;aACvB;SACF;QACD,OAAO,IAAI,CAAA;IACb,CAAC;CACF","sourcesContent":["import { Action, Operation, SlotLiteral, ComponentLiteral } from '@textbus/core'\nimport { Array as YArray, Map as YMap, Text as YText } from 'yjs'\n\nexport class LocalToRemote {\n transform(operation: Operation, root: YText) {\n const path = [...operation.path]\n path.shift()\n if (path.length) {\n const componentIndex = path.shift()!\n const sharedComponent = this.getSharedComponentByIndex(root, componentIndex)\n if (sharedComponent) {\n this.applyComponentOperationToSharedComponent(path, operation.apply, sharedComponent)\n }\n return\n }\n this.mergeActionsToSharedSlot(root, operation.apply)\n }\n\n private applyComponentOperationToSharedComponent(path: number[], actions: Action[], componentYMap: YMap<any>) {\n const sharedSlots = componentYMap.get('slots') as YArray<any>\n if (path.length) {\n const slotIndex = path.shift()!\n const sharedSlot = sharedSlots.get(slotIndex)\n this.applySlotOperationToSharedSlot(path, actions, sharedSlot)\n return\n }\n let index: number\n actions.forEach(action => {\n switch (action.type) {\n case 'retain':\n index = action.offset\n break\n case 'insertSlot':\n sharedSlots.insert(index, [this.makeSharedSlotBySlotLiteral(action.slot)])\n index++\n break\n case 'apply':\n componentYMap.set('state', action.value)\n break\n case 'delete':\n sharedSlots.delete(index, action.count)\n break\n }\n })\n }\n\n private applySlotOperationToSharedSlot(path: number[], actions: Action[], slotYMap: YMap<any>) {\n if (path.length) {\n const componentIndex = path.shift()!\n const sharedContent = slotYMap.get('content') as YText\n const sharedComponent = this.getSharedComponentByIndex(sharedContent, componentIndex)!\n this.applyComponentOperationToSharedComponent(path, actions, sharedComponent)\n return\n }\n const content = slotYMap.get('content') as YText\n\n this.mergeActionsToSharedSlot(content, actions)\n }\n\n private mergeActionsToSharedSlot(content: YText, actions: Action[]) {\n let index: number\n let length: number\n\n actions.forEach(action => {\n if (action.type === 'retain') {\n if (action.formats) {\n content.format(index, action.offset, action.formats)\n } else {\n index = action.offset\n }\n } else if (action.type === 'insert') {\n const delta = content.toDelta()\n const isEmpty = delta.length === 1 && delta[0].insert === '\\n'\n\n if (typeof action.content === 'string') {\n length = action.content.length\n content.insert(index, action.content)\n } else {\n length = 1\n content.insertEmbed(index, this.makeSharedComponentByComponentLiteral(action.content))\n }\n if (action.formats) {\n content.format(index, length, action.formats)\n }\n if (isEmpty && index === 0) {\n content.delete(content.length - 1, 1)\n }\n index += length\n } else if (action.type === 'delete') {\n const delta = content.toDelta()\n content.delete(index, action.count)\n if (content.length === 0) {\n content.insert(0, '\\n', delta[0]?.attributes)\n }\n } else if (action.type === 'apply') {\n content.setAttribute('state', action.value)\n }\n })\n }\n\n private makeSharedSlotBySlotLiteral(slotLiteral: SlotLiteral): YMap<any> {\n const content = new YText()\n let index = 0\n slotLiteral.content.forEach(i => {\n let size: number\n if (typeof i === 'string') {\n size = i.length\n content.insert(index, i)\n } else {\n size = 1\n content.insertEmbed(index, this.makeSharedComponentByComponentLiteral(i))\n }\n index += size\n })\n const formats = slotLiteral.formats\n Object.keys(formats).forEach(key => {\n const formatRanges = formats[key]\n formatRanges.forEach(formatRange => {\n content.format(formatRange.startIndex, formatRange.endIndex - formatRange.startIndex, {\n [key]: formatRange.value\n })\n })\n })\n\n const sharedSlot = new YMap()\n sharedSlot.set('content', content)\n sharedSlot.set('schema', slotLiteral.schema)\n return sharedSlot\n }\n\n private makeSharedComponentByComponentLiteral(componentLiteral: ComponentLiteral): YMap<any> {\n const slots = new YArray()\n componentLiteral.slots.forEach(item => {\n slots.push([this.makeSharedSlotBySlotLiteral(item)])\n })\n const sharedComponent = new YMap()\n sharedComponent.set('name', componentLiteral.name)\n sharedComponent.set('slots', slots)\n sharedComponent.set('state', componentLiteral.state)\n return sharedComponent\n }\n\n private getSharedComponentByIndex(host: YText, index: number): YMap<any> | null {\n const delta = host.toDelta()\n let i = 0\n for (const action of delta) {\n if (action.insert) {\n if (i === index) {\n return action.insert instanceof YMap ? action.insert : null\n }\n i += action.insert instanceof YMap ? 1 : action.insert.length\n } else {\n throw new Error('xxx')\n }\n }\n return null\n }\n}\n"]}
|
1
|
+
{"version":3,"file":"local-to-remote.js","sourceRoot":"","sources":["../../src/collab/local-to-remote.ts"],"names":[],"mappings":"AAAA,OAAO,EAAoD,SAAS,EAAE,MAAM,eAAe,CAAA;AAC3F,OAAO,EAAE,KAAK,IAAI,MAAM,EAAE,GAAG,IAAI,IAAI,EAAE,IAAI,IAAI,KAAK,EAAE,MAAM,KAAK,CAAA;AAEjE,MAAM,kBAAkB,GAAG,SAAS,CAAC,aAAa,CAAC,CAAA;AAEnD,MAAM,OAAO,aAAa;IACxB,SAAS,CAAC,SAAoB,EAAE,IAAW;QACzC,MAAM,IAAI,GAAG,CAAC,GAAG,SAAS,CAAC,IAAI,CAAC,CAAA;QAChC,IAAI,CAAC,KAAK,EAAE,CAAA;QACZ,IAAI,IAAI,CAAC,MAAM,EAAE;YACf,MAAM,cAAc,GAAG,IAAI,CAAC,KAAK,EAAG,CAAA;YACpC,MAAM,eAAe,GAAG,IAAI,CAAC,yBAAyB,CAAC,IAAI,EAAE,cAAc,CAAC,CAAA;YAC5E,IAAI,eAAe,EAAE;gBACnB,IAAI,CAAC,wCAAwC,CAAC,IAAI,EAAE,SAAS,CAAC,KAAK,EAAE,eAAe,CAAC,CAAA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A2B,CAAC,IAAI,CAAC,CAAC,CAAC,CAAA;QACtD,CAAC,CAAC,CAAA;QACF,MAAM,eAAe,GAAG,IAAI,IAAI,EAAE,CAAA;QAClC,eAAe,CAAC,GAAG,CAAC,MAAM,EAAE,gBAAgB,CAAC,IAAI,CAAC,CAAA;QAClD,eAAe,CAAC,GAAG,CAAC,OAAO,EAAE,KAAK,CAAC,CAAA;QACnC,eAAe,CAAC,GAAG,CAAC,OAAO,EAAE,gBAAgB,CAAC,KAAK,CAAC,CAAA;QACpD,OAAO,eAAe,CAAA;IACxB,CAAC;IAEO,yBAAyB,CAAC,IAAW,EAAE,KAAa;QAC1D,MAAM,KAAK,GAAG,IAAI,CAAC,OAAO,EAAE,CAAA;QAC5B,IAAI,CAAC,GAAG,CAAC,CAAA;QACT,KAAK,MAAM,MAAM,IAAI,KAAK,EAAE;YAC1B,IAAI,MAAM,CAAC,MAAM,EAAE;gBACjB,IAAI,CAAC,KAAK,KAAK,EAAE;oBACf,OAAO,MAAM,CAAC,MAAM,YAAY,IAAI,CAAC,CAAC,CAAC,MAAM,CAAC,MAAM,CAAC,CAAC,CAAC,IAAI,CAAA;iBAC5D;gBACD,CAAC,IAAI,MAAM,CAAC,MAAM,YAAY,IAAI,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,MAAM,CAAC,MAAM,CAAC,MAAM,CAAA;aAC9D;iBAAM;gBACL,MAAM,kBAAkB,CAAC,0BAA0B,CAAC,CAAA;aACrD;SACF;QACD,OAAO,IAAI,CAAA;IACb,CAAC;CACF","sourcesContent":["import { Action, Operation, SlotLiteral, ComponentLiteral, makeError } from '@textbus/core'\nimport { Array as YArray, Map as YMap, Text as YText } from 'yjs'\n\nconst collaborateErrorFn = makeError('Collaborate')\n\nexport class LocalToRemote {\n transform(operation: Operation, root: YText) {\n const path = [...operation.path]\n path.shift()\n if (path.length) {\n const componentIndex = path.shift()!\n const sharedComponent = this.getSharedComponentByIndex(root, componentIndex)\n if (sharedComponent) {\n this.applyComponentOperationToSharedComponent(path, operation.apply, sharedComponent)\n }\n return\n }\n this.mergeActionsToSharedSlot(root, operation.apply)\n }\n\n private applyComponentOperationToSharedComponent(path: number[], actions: Action[], componentYMap: YMap<any>) {\n const sharedSlots = componentYMap.get('slots') as YArray<any>\n if (path.length) {\n const slotIndex = path.shift()!\n const sharedSlot = sharedSlots.get(slotIndex)\n this.applySlotOperationToSharedSlot(path, actions, sharedSlot)\n return\n }\n let index: number\n actions.forEach(action => {\n switch (action.type) {\n case 'retain':\n index = action.offset\n break\n case 'insertSlot':\n sharedSlots.insert(index, [this.makeSharedSlotBySlotLiteral(action.slot)])\n index++\n break\n case 'apply':\n componentYMap.set('state', action.value)\n break\n case 'delete':\n sharedSlots.delete(index, action.count)\n break\n }\n })\n }\n\n private applySlotOperationToSharedSlot(path: number[], actions: Action[], slotYMap: YMap<any>) {\n if (path.length) {\n const componentIndex = path.shift()!\n const sharedContent = slotYMap.get('content') as YText\n const sharedComponent = this.getSharedComponentByIndex(sharedContent, componentIndex)!\n this.applyComponentOperationToSharedComponent(path, actions, sharedComponent)\n return\n }\n const content = slotYMap.get('content') as YText\n\n this.mergeActionsToSharedSlot(content, actions, slotYMap)\n }\n\n private mergeActionsToSharedSlot(content: YText, actions: Action[], slotYMap?: YMap<any>) {\n let index: number\n let length: number\n\n actions.forEach(action => {\n if (action.type === 'retain') {\n if (action.formats) {\n content.format(index, action.offset, action.formats)\n } else {\n index = action.offset\n }\n } else if (action.type === 'insert') {\n const delta = content.toDelta()\n const isEmpty = delta.length === 1 && delta[0].insert === '\\n'\n\n if (typeof action.content === 'string') {\n length = action.content.length\n content.insert(index, action.content)\n } else {\n length = 1\n content.insertEmbed(index, this.makeSharedComponentByComponentLiteral(action.content))\n }\n if (action.formats) {\n content.format(index, length, action.formats)\n }\n if (isEmpty && index === 0) {\n content.delete(content.length - 1, 1)\n }\n index += length\n } else if (action.type === 'delete') {\n const delta = content.toDelta()\n content.delete(index, action.count)\n if (content.length === 0) {\n content.insert(0, '\\n', delta[0]?.attributes)\n }\n } else if (action.type === 'apply') {\n slotYMap?.set('state', action.value)\n }\n })\n }\n\n private makeSharedSlotBySlotLiteral(slotLiteral: SlotLiteral): YMap<any> {\n const content = new YText()\n let index = 0\n slotLiteral.content.forEach(i => {\n let size: number\n if (typeof i === 'string') {\n size = i.length\n content.insert(index, i)\n } else {\n size = 1\n content.insertEmbed(index, this.makeSharedComponentByComponentLiteral(i))\n }\n index += size\n })\n const formats = slotLiteral.formats\n Object.keys(formats).forEach(key => {\n const formatRanges = formats[key]\n formatRanges.forEach(formatRange => {\n content.format(formatRange.startIndex, formatRange.endIndex - formatRange.startIndex, {\n [key]: formatRange.value\n })\n })\n })\n\n const sharedSlot = new YMap()\n sharedSlot.set('content', content)\n sharedSlot.set('schema', slotLiteral.schema)\n sharedSlot.set('state', slotLiteral.state)\n return sharedSlot\n }\n\n private makeSharedComponentByComponentLiteral(componentLiteral: ComponentLiteral): YMap<any> {\n const slots = new YArray()\n componentLiteral.slots.forEach(item => {\n slots.push([this.makeSharedSlotBySlotLiteral(item)])\n })\n const sharedComponent = new YMap()\n sharedComponent.set('name', componentLiteral.name)\n sharedComponent.set('slots', slots)\n sharedComponent.set('state', componentLiteral.state)\n return sharedComponent\n }\n\n private getSharedComponentByIndex(host: YText, index: number): YMap<any> | null {\n const delta = host.toDelta()\n let i = 0\n for (const action of delta) {\n if (action.insert) {\n if (i === index) {\n return action.insert instanceof YMap ? action.insert : null\n }\n i += action.insert instanceof YMap ? 1 : action.insert.length\n } else {\n throw collaborateErrorFn('Unexpected delta action.')\n }\n }\n return null\n }\n}\n"]}
|
@@ -1,4 +1,6 @@
|
|
1
1
|
import { YArrayEvent, YMapEvent, YTextEvent } from 'yjs';
|
2
|
+
import { makeError } from '@textbus/core';
|
3
|
+
const collaborateErrorFn = makeError('Collaborate');
|
2
4
|
export class RemoteToLocal {
|
3
5
|
constructor(translator, registry) {
|
4
6
|
Object.defineProperty(this, "translator", {
|
@@ -54,7 +56,7 @@ export class RemoteToLocal {
|
|
54
56
|
}
|
55
57
|
else if (action.insert) {
|
56
58
|
action.insert.forEach(item => {
|
57
|
-
slots.insert(this.
|
59
|
+
slots.insert(this.createSlotBySharedSlot(item));
|
58
60
|
});
|
59
61
|
}
|
60
62
|
else if (action.delete) {
|
@@ -113,8 +115,15 @@ export class RemoteToLocal {
|
|
113
115
|
}
|
114
116
|
});
|
115
117
|
}
|
116
|
-
else {
|
117
|
-
|
118
|
+
else if (ev instanceof YMapEvent) {
|
119
|
+
ev.keysChanged.forEach(key => {
|
120
|
+
if (key === 'state') {
|
121
|
+
const state = ev.target.get('state');
|
122
|
+
slot.updateState(draft => {
|
123
|
+
Object.assign(draft, state);
|
124
|
+
});
|
125
|
+
}
|
126
|
+
});
|
118
127
|
}
|
119
128
|
}
|
120
129
|
createComponentBySharedComponent(yMap) {
|
@@ -149,7 +158,7 @@ export class RemoteToLocal {
|
|
149
158
|
}
|
150
159
|
}
|
151
160
|
else {
|
152
|
-
throw
|
161
|
+
throw collaborateErrorFn('Unexpected delta action.');
|
153
162
|
}
|
154
163
|
}
|
155
164
|
return slot;
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"remote-to-local.js","sourceRoot":"","sources":["../../src/collab/remote-to-local.ts"],"names":[],"mappings":"AAAA,OAAO,EAAe,WAAW,EAAU,SAAS,EAAiB,UAAU,EAAmB,MAAM,KAAK,CAAA;AAK7G,MAAM,OAAO,aAAa;IACxB,YAAoB,UAAsB,EACtB,QAAkB;;;;;mBADlB;;;;;;mBACA;;IACpB,CAAC;IAED,SAAS,CAAC,MAAgB,EAAE,IAAU;QACpC,MAAM,CAAC,OAAO,CAAC,EAAE,CAAC,EAAE;YAClB,MAAM,IAAI,GAAU,EAAE,CAAA;YAEtB,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,EAAE,CAAC,IAAI,CAAC,MAAM,EAAE,CAAC,IAAI,CAAC,EAAE;gBAC1C,IAAI,CAAC,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AU,CAAC,GAAG,CAAC,SAAS,CAAU,CAAA;QAClD,MAAM,KAAK,GAAG,OAAO,CAAC,OAAO,EAAE,CAAA;QAE/B,MAAM,IAAI,GAAG,IAAI,CAAC,UAAU,CAAC,UAAU,CAAC;YACtC,MAAM,EAAE,UAAU,CAAC,GAAG,CAAC,QAAQ,CAAC;YAChC,KAAK,EAAE,UAAU,CAAC,GAAG,CAAC,OAAO,CAAC;YAC9B,OAAO,EAAE,EAAE;YACX,OAAO,EAAE,EAAE;SACZ,CAAC,CAAA;QAEF,KAAK,MAAM,MAAM,IAAI,KAAK,EAAE;YAC1B,IAAI,MAAM,CAAC,MAAM,EAAE;gBACjB,IAAI,OAAO,MAAM,CAAC,MAAM,KAAK,QAAQ,EAAE;oBACrC,IAAI,CAAC,MAAM,CAAC,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,UAAU,CAAC,CAAC,CAAC,MAAM,CAAC,IAAI,CAAC,MAAM,CAAC,UAAU,CAAC,CAAC,GAAG,CAAC,GAAG,CAAC,EAAE;wBACtF,OAAO,CAAC,IAAI,CAAC,QAAQ,CAAC,YAAY,CAAC,GAAG,CAAE,EAAE,MAAM,CAAC,UAAW,CAAC,GAAG,CAAC,CAAC,CAAA;oBACpE,CAAC,CAAC,CAAC,CAAC,CAAC,EAAE,CAAC,CAAA;iBACT;qBAAM;oBACL,IAAI,CAAC,MAAM,CAAC,IAAI,CAAC,gCAAgC,CAAC,MAAM,CAAC,MAAM,CAAC,CAAC,CAAA;iBAClE;aACF;iBAAM;gBACL,MAAM,IAAI,KAAK,CAAC,KAAK,CAAC,CAAA;aACvB;SACF;QACD,OAAO,IAAI,CAAA;IACb,CAAC;CACF","sourcesContent":["import { Map as YMap, YArrayEvent, YEvent, YMapEvent, Text as YText, YTextEvent, Array as YArray } from 'yjs'\nimport { ComponentInstance, ComponentLiteral, Registry, Slot, Translator } from '@textbus/core'\n\ntype YPath = [number, string][]\n\nexport class RemoteToLocal {\n constructor(private translator: Translator,\n private registry: Registry) {\n }\n\n transform(events: YEvent[], slot: Slot) {\n events.forEach(ev => {\n const path: YPath = []\n\n for (let i = 0; i < ev.path.length; i += 2) {\n path.push(ev.path.slice(i, i + 2) as [number, string])\n }\n\n if (path.length) {\n const componentIndex = path.shift()![0] as number\n const component = slot.getContentAtIndex(componentIndex) as ComponentInstance\n this.applySharedComponentToComponent(ev, path, component)\n return\n }\n\n this.applySharedSlotToSlot(ev, path, slot)\n })\n }\n\n private applySharedComponentToComponent(ev: YEvent, path: YPath, component: ComponentInstance) {\n if (path.length) {\n const childPath = path.shift()!\n const slot = component.slots.get(childPath[0])!\n this.applySharedSlotToSlot(ev, path, slot)\n return\n }\n if (ev instanceof YMapEvent) {\n ev.keysChanged.forEach(key => {\n if (key === 'state') {\n const state = (ev.target as YMap<any>).get('state')\n component.updateState(draft => {\n Object.assign(draft, state)\n })\n }\n })\n } else if (ev instanceof YArrayEvent) {\n const slots = component.slots\n ev.delta.forEach(action => {\n if (Reflect.has(action, 'retain')) {\n slots.retain(action.retain!)\n } else if (action.insert) {\n (action.insert as Array<any>).forEach(item => {\n slots.insert(this.translator.createSlot(item.toJSON())!)\n })\n } else if (action.delete) {\n slots.retain(slots.index)\n slots.delete(action.delete)\n }\n })\n }\n }\n\n private applySharedSlotToSlot(ev: YEvent, path: YPath, slot: Slot) {\n if (path.length) {\n path.shift()\n const delta = (ev.target.parent as YText).toDelta()\n let componentIndex = 0\n for (let i = 0; i < delta.length; i++) {\n const action = delta[i]\n if (action.insert === ev.target) {\n break\n }\n componentIndex += typeof action.insert === 'string' ? action.insert.length : 1\n }\n const component = slot.getContentAtIndex(componentIndex) as ComponentInstance\n this.applySharedComponentToComponent(ev, path, component)\n return\n }\n\n if (ev instanceof YTextEvent) {\n slot.retain(0)\n ev.delta.forEach(action => {\n if (Reflect.has(action, 'retain')) {\n if (action.attributes) {\n slot.retain(action.retain!, Object.keys(action.attributes).map(key => {\n return [this.registry.getFormatter(key)!, action.attributes![key]]\n }))\n }\n slot.retain(action.retain!)\n } else if (action.insert) {\n if (typeof action.insert === 'string') {\n slot.insert(action.insert, action.attributes ? Object.keys(action.attributes).map(key => {\n return [this.registry.getFormatter(key)!, action.attributes![key]]\n }) : [])\n } else {\n const component = this.createComponentBySharedComponent(action.insert as YMap<any>)\n slot.insert(component)\n }\n } else if (action.delete) {\n slot.retain(slot.index)\n slot.delete(action.delete)\n } else if (action.attributes) {\n slot.updateState(draft => {\n Object.assign(draft, action.attributes)\n })\n }\n })\n } else {\n throw new Error('xxx')\n }\n }\n\n private createComponentBySharedComponent(yMap: YMap<any>): ComponentInstance {\n const slots = yMap.get('slots') as YArray<YMap<any>>\n const componentLiteral: ComponentLiteral = {\n state: yMap.get('state'),\n name: yMap.get('name'),\n slots: slots.map(sharedSlot => {\n return this.createSlotBySharedSlot(sharedSlot).toJSON()\n })\n }\n return this.translator.createComponent(componentLiteral)!\n }\n\n private createSlotBySharedSlot(sharedSlot: YMap<any>): Slot {\n const content = sharedSlot.get('content') as YText\n const delta = content.toDelta()\n\n const slot = this.translator.createSlot({\n schema: sharedSlot.get('schema'),\n state: sharedSlot.get('state'),\n formats: {},\n content: []\n })\n\n for (const action of delta) {\n if (action.insert) {\n if (typeof action.insert === 'string') {\n slot.insert(action.insert, action.attributes ? Object.keys(action.attributes).map(key => {\n return [this.registry.getFormatter(key)!, action.attributes![key]]\n }) : [])\n } else {\n slot.insert(this.createComponentBySharedComponent(action.insert))\n }\n } else {\n throw new Error('xxx')\n }\n }\n return slot\n }\n}\n"]}
|
1
|
+
{"version":3,"file":"remote-to-local.js","sourceRoot":"","sources":["../../src/collab/remote-to-local.ts"],"names":[],"mappings":"AAAA,OAAO,EAAe,WAAW,EAAU,SAAS,EAAiB,UAAU,EAAmB,MAAM,KAAK,CAAA;AAC7G,OAAO,EAAuC,SAAS,EAA8B,MAAM,eAAe,CAAA;AAG1G,MAAM,kBAAkB,GAAG,SAAS,CAAC,aAAa,CAAC,CAAA;AAEnD,MAAM,OAAO,aAAa;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AAC,GAAG,CAAC,GAAG,CAAC,EAAE;wBACtF,OAAO,CAAC,IAAI,CAAC,QAAQ,CAAC,YAAY,CAAC,GAAG,CAAE,EAAE,MAAM,CAAC,UAAW,CAAC,GAAG,CAAC,CAAC,CAAA;oBACpE,CAAC,CAAC,CAAC,CAAC,CAAC,EAAE,CAAC,CAAA;iBACT;qBAAM;oBACL,IAAI,CAAC,MAAM,CAAC,IAAI,CAAC,gCAAgC,CAAC,MAAM,CAAC,MAAM,CAAC,CAAC,CAAA;iBAClE;aACF;iBAAM;gBACL,MAAM,kBAAkB,CAAC,0BAA0B,CAAC,CAAA;aACrD;SACF;QACD,OAAO,IAAI,CAAA;IACb,CAAC;CACF","sourcesContent":["import { Map as YMap, YArrayEvent, YEvent, YMapEvent, Text as YText, YTextEvent, Array as YArray } from 'yjs'\nimport { ComponentInstance, ComponentLiteral, makeError, Registry, Slot, Translator } from '@textbus/core'\n\ntype YPath = [number, string][]\nconst collaborateErrorFn = makeError('Collaborate')\n\nexport class RemoteToLocal {\n constructor(private translator: Translator,\n private registry: Registry) {\n }\n\n transform(events: YEvent[], slot: Slot) {\n events.forEach(ev => {\n const path: YPath = []\n\n for (let i = 0; i < ev.path.length; i += 2) {\n path.push(ev.path.slice(i, i + 2) as [number, string])\n }\n\n if (path.length) {\n const componentIndex = path.shift()![0] as number\n const component = slot.getContentAtIndex(componentIndex) as ComponentInstance\n this.applySharedComponentToComponent(ev, path, component)\n return\n }\n\n this.applySharedSlotToSlot(ev, path, slot)\n })\n }\n\n private applySharedComponentToComponent(ev: YEvent, path: YPath, component: ComponentInstance) {\n if (path.length) {\n const childPath = path.shift()!\n const slot = component.slots.get(childPath[0])!\n this.applySharedSlotToSlot(ev, path, slot)\n return\n }\n if (ev instanceof YMapEvent) {\n ev.keysChanged.forEach(key => {\n if (key === 'state') {\n const state = (ev.target as YMap<any>).get('state')\n component.updateState(draft => {\n Object.assign(draft, state)\n })\n }\n })\n } else if (ev instanceof YArrayEvent) {\n const slots = component.slots\n ev.delta.forEach(action => {\n if (Reflect.has(action, 'retain')) {\n slots.retain(action.retain!)\n } else if (action.insert) {\n (action.insert as Array<YMap<any>>).forEach(item => {\n slots.insert(this.createSlotBySharedSlot(item))\n })\n } else if (action.delete) {\n slots.retain(slots.index)\n slots.delete(action.delete)\n }\n })\n }\n }\n\n private applySharedSlotToSlot(ev: YEvent, path: YPath, slot: Slot) {\n if (path.length) {\n path.shift()\n const delta = (ev.target.parent as YText).toDelta()\n let componentIndex = 0\n for (let i = 0; i < delta.length; i++) {\n const action = delta[i]\n if (action.insert === ev.target) {\n break\n }\n componentIndex += typeof action.insert === 'string' ? action.insert.length : 1\n }\n const component = slot.getContentAtIndex(componentIndex) as ComponentInstance\n this.applySharedComponentToComponent(ev, path, component)\n return\n }\n\n if (ev instanceof YTextEvent) {\n slot.retain(0)\n ev.delta.forEach(action => {\n if (Reflect.has(action, 'retain')) {\n if (action.attributes) {\n slot.retain(action.retain!, Object.keys(action.attributes).map(key => {\n return [this.registry.getFormatter(key)!, action.attributes![key]]\n }))\n }\n slot.retain(action.retain!)\n } else if (action.insert) {\n if (typeof action.insert === 'string') {\n slot.insert(action.insert, action.attributes ? Object.keys(action.attributes).map(key => {\n return [this.registry.getFormatter(key)!, action.attributes![key]]\n }) : [])\n } else {\n const component = this.createComponentBySharedComponent(action.insert as YMap<any>)\n slot.insert(component)\n }\n } else if (action.delete) {\n slot.retain(slot.index)\n slot.delete(action.delete)\n } else if (action.attributes) {\n slot.updateState(draft => {\n Object.assign(draft, action.attributes)\n })\n }\n })\n } else if (ev instanceof YMapEvent) {\n ev.keysChanged.forEach(key => {\n if (key === 'state') {\n const state = (ev.target as YMap<any>).get('state')\n slot.updateState(draft => {\n Object.assign(draft, state)\n })\n }\n })\n }\n }\n\n private createComponentBySharedComponent(yMap: YMap<any>): ComponentInstance {\n const slots = yMap.get('slots') as YArray<YMap<any>>\n const componentLiteral: ComponentLiteral = {\n state: yMap.get('state'),\n name: yMap.get('name'),\n slots: slots.map(sharedSlot => {\n return this.createSlotBySharedSlot(sharedSlot).toJSON()\n })\n }\n return this.translator.createComponent(componentLiteral)!\n }\n\n private createSlotBySharedSlot(sharedSlot: YMap<any>): Slot {\n const content = sharedSlot.get('content') as YText\n const delta = content.toDelta()\n\n const slot = this.translator.createSlot({\n schema: sharedSlot.get('schema'),\n state: sharedSlot.get('state'),\n formats: {},\n content: []\n })\n\n for (const action of delta) {\n if (action.insert) {\n if (typeof action.insert === 'string') {\n slot.insert(action.insert, action.attributes ? Object.keys(action.attributes).map(key => {\n return [this.registry.getFormatter(key)!, action.attributes![key]]\n }) : [])\n } else {\n slot.insert(this.createComponentBySharedComponent(action.insert))\n }\n } else {\n throw collaborateErrorFn('Unexpected delta action.')\n }\n }\n return slot\n }\n}\n"]}
|
package/package.json
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
{
|
2
2
|
"name": "@textbus/collaborate",
|
3
|
-
"version": "2.0.0-alpha.
|
4
|
-
"description": "
|
3
|
+
"version": "2.0.0-alpha.42",
|
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",
|
7
7
|
"typings": "./bundles/public-api.d.ts",
|
@@ -27,8 +27,8 @@
|
|
27
27
|
"dependencies": {
|
28
28
|
"@tanbo/di": "^1.0.5",
|
29
29
|
"@tanbo/stream": "^0.0.12",
|
30
|
-
"@textbus/browser": "^2.0.0-alpha.
|
31
|
-
"@textbus/core": "^2.0.0-alpha.
|
30
|
+
"@textbus/browser": "^2.0.0-alpha.42",
|
31
|
+
"@textbus/core": "^2.0.0-alpha.42",
|
32
32
|
"reflect-metadata": "^0.1.13",
|
33
33
|
"y-protocols": "^1.0.5",
|
34
34
|
"yjs": "^13.5.27"
|
@@ -44,5 +44,5 @@
|
|
44
44
|
"bugs": {
|
45
45
|
"url": "https://github.com/textbus/textbus.git/issues"
|
46
46
|
},
|
47
|
-
"gitHead": "
|
47
|
+
"gitHead": "81f934ca076d258571ba7c5babdb6b1ab71e4760"
|
48
48
|
}
|
@@ -14,7 +14,18 @@ export interface SelectionRect {
|
|
14
14
|
y: number
|
15
15
|
width: number
|
16
16
|
height: number
|
17
|
+
}
|
18
|
+
|
19
|
+
export interface SelectionInfo {
|
17
20
|
color: string
|
21
|
+
username: string
|
22
|
+
rects: SelectionRect[]
|
23
|
+
}
|
24
|
+
|
25
|
+
export interface RemoteSelectionCursor {
|
26
|
+
cursor: HTMLElement
|
27
|
+
anchor: HTMLElement
|
28
|
+
userTip: HTMLElement
|
18
29
|
}
|
19
30
|
|
20
31
|
@Injectable()
|
@@ -31,20 +42,36 @@ export class CollaborateCursor {
|
|
31
42
|
}
|
32
43
|
}) as HTMLCanvasElement
|
33
44
|
private context = this.canvas.getContext('2d')!
|
45
|
+
private tooltips = createElement('div', {
|
46
|
+
styles: {
|
47
|
+
position: 'absolute',
|
48
|
+
left: 0,
|
49
|
+
top: 0,
|
50
|
+
width: '100%',
|
51
|
+
height: '100%',
|
52
|
+
pointerEvents: 'none',
|
53
|
+
fontSize: '12px'
|
54
|
+
}
|
55
|
+
})
|
34
56
|
|
35
|
-
private
|
57
|
+
private onRectsChange = new Subject<SelectionInfo>()
|
36
58
|
|
37
59
|
constructor(@Inject(EDITOR_CONTAINER) private container: HTMLElement,
|
38
60
|
@Inject(EDITABLE_DOCUMENT) private document: Document,
|
39
61
|
private nativeSelection: SelectionBridge,
|
40
62
|
private selection: Selection) {
|
41
63
|
container.appendChild(this.canvas)
|
42
|
-
this.
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
64
|
+
container.appendChild(this.tooltips)
|
65
|
+
this.onRectsChange.subscribe(info => {
|
66
|
+
const color = info.color
|
67
|
+
const rects = info.rects
|
68
|
+
for (const rect of rects) {
|
69
|
+
this.context.fillStyle = 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
|
+
}
|
48
75
|
})
|
49
76
|
}
|
50
77
|
|
@@ -53,6 +80,8 @@ export class CollaborateCursor {
|
|
53
80
|
this.canvas.height = this.container.offsetHeight
|
54
81
|
this.context.clearRect(0, 0, this.canvas.width, this.canvas.height)
|
55
82
|
|
83
|
+
const users: SelectionInfo[] = []
|
84
|
+
|
56
85
|
paths.filter(i => {
|
57
86
|
return i.paths.start.length && i.paths.end.length
|
58
87
|
}).forEach(item => {
|
@@ -74,34 +103,131 @@ export class CollaborateCursor {
|
|
74
103
|
nativeRange.setEnd(position.end.node, position.end.offset)
|
75
104
|
|
76
105
|
const rects = nativeRange.getClientRects()
|
77
|
-
|
106
|
+
const selectionRects: SelectionRect[] = []
|
78
107
|
for (let i = rects.length - 1; i >= 0; i--) {
|
79
108
|
const rect = rects[i]
|
80
|
-
|
81
|
-
continue
|
82
|
-
}
|
83
|
-
prev = rect
|
84
|
-
this.onRectChange.next({
|
109
|
+
selectionRects.push({
|
85
110
|
x: rect.x,
|
86
111
|
y: rect.y,
|
87
112
|
width: rect.width,
|
88
113
|
height: rect.height,
|
89
|
-
color: item.color
|
90
114
|
})
|
91
115
|
}
|
92
116
|
|
93
117
|
if (rects.length === 0) {
|
94
118
|
const rect = nativeRange.getBoundingClientRect()
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
119
|
+
if (rect.x !== 0 || rect.y !== 0 || rect.width !== 0 || rect.height !== 0) {
|
120
|
+
selectionRects.push({
|
121
|
+
x: rect.x,
|
122
|
+
y: rect.y,
|
123
|
+
width: 1,
|
124
|
+
height: rect.height,
|
125
|
+
})
|
126
|
+
}
|
127
|
+
}
|
128
|
+
const info: SelectionInfo = {
|
129
|
+
...item,
|
130
|
+
rects: selectionRects
|
102
131
|
}
|
132
|
+
this.onRectsChange.next(info)
|
133
|
+
|
134
|
+
users.push(info)
|
103
135
|
}
|
104
136
|
}
|
105
137
|
})
|
138
|
+
this.drawUserCursor(users)
|
139
|
+
}
|
140
|
+
|
141
|
+
private drawUserCursor(users: SelectionInfo[]) {
|
142
|
+
for (let i = 0; i < users.length; i++) {
|
143
|
+
const user = users[i]
|
144
|
+
const last = user.rects[user.rects.length - 1]
|
145
|
+
|
146
|
+
const {cursor, userTip, anchor} = this.getUserCursor(i)
|
147
|
+
if (!last) {
|
148
|
+
cursor.style.display = 'none'
|
149
|
+
} else {
|
150
|
+
Object.assign(cursor.style, {
|
151
|
+
left: last.x + last.width + 'px',
|
152
|
+
top: last.y + 'px',
|
153
|
+
width: '2px',
|
154
|
+
height: last.height + 'px',
|
155
|
+
background: user.color,
|
156
|
+
display: 'block'
|
157
|
+
})
|
158
|
+
anchor.style.background = user.color
|
159
|
+
userTip.innerText = user.username
|
160
|
+
userTip.style.background = user.color
|
161
|
+
}
|
162
|
+
}
|
163
|
+
|
164
|
+
for (let i = users.length; i < this.tooltips.children.length; i++) {
|
165
|
+
this.tooltips.removeChild(this.tooltips.children[i])
|
166
|
+
}
|
167
|
+
}
|
168
|
+
|
169
|
+
private getUserCursor(index: number): RemoteSelectionCursor {
|
170
|
+
let child: HTMLElement = this.tooltips.children[index] as HTMLElement
|
171
|
+
if (child) {
|
172
|
+
const anchor = child.children[0] as HTMLElement
|
173
|
+
return {
|
174
|
+
cursor: child,
|
175
|
+
anchor,
|
176
|
+
userTip: anchor.children[0] as HTMLElement
|
177
|
+
}
|
178
|
+
}
|
179
|
+
const userTip = createElement('span', {
|
180
|
+
styles: {
|
181
|
+
position: 'absolute',
|
182
|
+
display: 'none',
|
183
|
+
left: '50%',
|
184
|
+
transform: 'translateX(-50%)',
|
185
|
+
marginBottom: '2px',
|
186
|
+
bottom: '100%',
|
187
|
+
whiteSpace: 'nowrap',
|
188
|
+
color: '#fff',
|
189
|
+
boxShadow: '0 1px 2px rgba(0,0,0,.1)',
|
190
|
+
borderRadius: '3px',
|
191
|
+
padding: '3px 5px',
|
192
|
+
pointerEvents: 'none',
|
193
|
+
}
|
194
|
+
})
|
195
|
+
|
196
|
+
const anchor = createElement('span', {
|
197
|
+
styles: {
|
198
|
+
position: 'absolute',
|
199
|
+
top: '-2px',
|
200
|
+
left: '-2px',
|
201
|
+
width: '6px',
|
202
|
+
height: '6px',
|
203
|
+
pointerEvents: 'auto',
|
204
|
+
pointer: 'cursor',
|
205
|
+
},
|
206
|
+
children: [userTip],
|
207
|
+
on: {
|
208
|
+
mouseenter() {
|
209
|
+
anchor.style.transform = 'scale(1.2)'
|
210
|
+
userTip.style.display = 'block'
|
211
|
+
},
|
212
|
+
mouseleave() {
|
213
|
+
userTip.style.display = 'none'
|
214
|
+
anchor.style.transform = ''
|
215
|
+
}
|
216
|
+
}
|
217
|
+
})
|
218
|
+
child = createElement('span', {
|
219
|
+
styles: {
|
220
|
+
position: 'absolute',
|
221
|
+
},
|
222
|
+
children: [
|
223
|
+
anchor
|
224
|
+
]
|
225
|
+
})
|
226
|
+
this.tooltips.append(child)
|
227
|
+
return {
|
228
|
+
cursor: child,
|
229
|
+
anchor,
|
230
|
+
userTip
|
231
|
+
}
|
106
232
|
}
|
107
233
|
}
|
@@ -1,6 +1,8 @@
|
|
1
|
-
import { Action, Operation, SlotLiteral, ComponentLiteral } from '@textbus/core'
|
1
|
+
import { Action, Operation, SlotLiteral, ComponentLiteral, makeError } from '@textbus/core'
|
2
2
|
import { Array as YArray, Map as YMap, Text as YText } from 'yjs'
|
3
3
|
|
4
|
+
const collaborateErrorFn = makeError('Collaborate')
|
5
|
+
|
4
6
|
export class LocalToRemote {
|
5
7
|
transform(operation: Operation, root: YText) {
|
6
8
|
const path = [...operation.path]
|
@@ -54,10 +56,10 @@ export class LocalToRemote {
|
|
54
56
|
}
|
55
57
|
const content = slotYMap.get('content') as YText
|
56
58
|
|
57
|
-
this.mergeActionsToSharedSlot(content, actions)
|
59
|
+
this.mergeActionsToSharedSlot(content, actions, slotYMap)
|
58
60
|
}
|
59
61
|
|
60
|
-
private mergeActionsToSharedSlot(content: YText, actions: Action[]) {
|
62
|
+
private mergeActionsToSharedSlot(content: YText, actions: Action[], slotYMap?: YMap<any>) {
|
61
63
|
let index: number
|
62
64
|
let length: number
|
63
65
|
|
@@ -93,7 +95,7 @@ export class LocalToRemote {
|
|
93
95
|
content.insert(0, '\n', delta[0]?.attributes)
|
94
96
|
}
|
95
97
|
} else if (action.type === 'apply') {
|
96
|
-
|
98
|
+
slotYMap?.set('state', action.value)
|
97
99
|
}
|
98
100
|
})
|
99
101
|
}
|
@@ -125,6 +127,7 @@ export class LocalToRemote {
|
|
125
127
|
const sharedSlot = new YMap()
|
126
128
|
sharedSlot.set('content', content)
|
127
129
|
sharedSlot.set('schema', slotLiteral.schema)
|
130
|
+
sharedSlot.set('state', slotLiteral.state)
|
128
131
|
return sharedSlot
|
129
132
|
}
|
130
133
|
|
@@ -150,7 +153,7 @@ export class LocalToRemote {
|
|
150
153
|
}
|
151
154
|
i += action.insert instanceof YMap ? 1 : action.insert.length
|
152
155
|
} else {
|
153
|
-
throw
|
156
|
+
throw collaborateErrorFn('Unexpected delta action.')
|
154
157
|
}
|
155
158
|
}
|
156
159
|
return null
|
@@ -1,7 +1,8 @@
|
|
1
1
|
import { Map as YMap, YArrayEvent, YEvent, YMapEvent, Text as YText, YTextEvent, Array as YArray } from 'yjs'
|
2
|
-
import { ComponentInstance, ComponentLiteral, Registry, Slot, Translator } from '@textbus/core'
|
2
|
+
import { ComponentInstance, ComponentLiteral, makeError, Registry, Slot, Translator } from '@textbus/core'
|
3
3
|
|
4
4
|
type YPath = [number, string][]
|
5
|
+
const collaborateErrorFn = makeError('Collaborate')
|
5
6
|
|
6
7
|
export class RemoteToLocal {
|
7
8
|
constructor(private translator: Translator,
|
@@ -49,8 +50,8 @@ export class RemoteToLocal {
|
|
49
50
|
if (Reflect.has(action, 'retain')) {
|
50
51
|
slots.retain(action.retain!)
|
51
52
|
} else if (action.insert) {
|
52
|
-
(action.insert as Array<any
|
53
|
-
slots.insert(this.
|
53
|
+
(action.insert as Array<YMap<any>>).forEach(item => {
|
54
|
+
slots.insert(this.createSlotBySharedSlot(item))
|
54
55
|
})
|
55
56
|
} else if (action.delete) {
|
56
57
|
slots.retain(slots.index)
|
@@ -105,8 +106,15 @@ export class RemoteToLocal {
|
|
105
106
|
})
|
106
107
|
}
|
107
108
|
})
|
108
|
-
} else {
|
109
|
-
|
109
|
+
} else if (ev instanceof YMapEvent) {
|
110
|
+
ev.keysChanged.forEach(key => {
|
111
|
+
if (key === 'state') {
|
112
|
+
const state = (ev.target as YMap<any>).get('state')
|
113
|
+
slot.updateState(draft => {
|
114
|
+
Object.assign(draft, state)
|
115
|
+
})
|
116
|
+
}
|
117
|
+
})
|
110
118
|
}
|
111
119
|
}
|
112
120
|
|
@@ -143,7 +151,7 @@ export class RemoteToLocal {
|
|
143
151
|
slot.insert(this.createComponentBySharedComponent(action.insert))
|
144
152
|
}
|
145
153
|
} else {
|
146
|
-
throw
|
154
|
+
throw collaborateErrorFn('Unexpected delta action.')
|
147
155
|
}
|
148
156
|
}
|
149
157
|
return slot
|