@textbus/collaborate 5.2.3 → 5.2.4

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.
package/dist/index.js CHANGED
@@ -1,1406 +1,2442 @@
1
- "use strict";
2
- Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
3
- const core$1 = require("@viewfly/core");
4
- const core = require("@textbus/core");
5
- const stream = require("@tanbo/stream");
6
- const yjs = require("yjs");
7
- const provider = require("@hocuspocus/provider");
8
- const yWebsocket = require("y-websocket");
9
- var __getOwnPropDesc$3 = Object.getOwnPropertyDescriptor;
10
- var __decorateClass$3 = (decorators, target, key, kind) => {
11
- var result = kind > 1 ? void 0 : kind ? __getOwnPropDesc$3(target, key) : target;
12
- for (var i = decorators.length - 1, decorator; i >= 0; i--)
13
- if (decorator = decorators[i])
14
- result = decorator(result) || result;
15
- return result;
16
- };
17
- var __decorateParam$1 = (index, decorator) => (target, key) => decorator(target, key, index);
18
- class CustomUndoManagerConfig {
19
- }
20
- const collabHistoryErrorFn = core.makeError("CollabHistory");
21
- exports.CollabHistory = class CollabHistory {
22
- constructor(rootComponentRef, collaborate, scheduler, stackSize, undoManagerConfig) {
23
- this.rootComponentRef = rootComponentRef;
24
- this.collaborate = collaborate;
25
- this.scheduler = scheduler;
26
- this.stackSize = stackSize;
27
- this.undoManagerConfig = undoManagerConfig;
28
- this.onBack = this.backEvent.asObservable();
29
- this.onForward = this.forwardEvent.asObservable();
30
- this.onChange = this.changeEvent.asObservable();
31
- this.onPush = this.pushEvent.asObservable();
32
- }
33
- rootComponentRef;
34
- collaborate;
35
- scheduler;
36
- stackSize;
37
- undoManagerConfig;
38
- onBack;
39
- onForward;
40
- onChange;
41
- onPush;
42
- get canBack() {
43
- return this.manager?.canUndo() || false;
44
- }
45
- get canForward() {
46
- return this.manager?.canRedo() || false;
47
- }
48
- manager = null;
49
- historyItems = [];
50
- index = 0;
51
- subscriptions = [];
52
- backEvent = new stream.Subject();
53
- forwardEvent = new stream.Subject();
54
- changeEvent = new stream.Subject();
55
- pushEvent = new stream.Subject();
56
- listen() {
57
- const root = this.collaborate.yDoc.getMap("RootComponent");
58
- const rootComponent = this.rootComponentRef.component;
59
- this.collaborate.syncRootComponent(this.collaborate.yDoc, root, rootComponent);
60
- const undoManagerConfig = this.undoManagerConfig || {};
61
- const manager = new yjs.UndoManager(root, {
62
- trackedOrigins: /* @__PURE__ */ new Set([this.collaborate.yDoc]),
63
- captureTransaction(arg) {
64
- if (undoManagerConfig.captureTransaction) {
65
- return undoManagerConfig.captureTransaction(arg);
66
- }
67
- return true;
68
- },
69
- deleteFilter(item) {
70
- if (undoManagerConfig.deleteFilter) {
71
- return undoManagerConfig.deleteFilter(item);
72
- }
73
- return true;
74
- }
75
- });
76
- this.manager = manager;
77
- let beforePosition = null;
78
- this.subscriptions.push(
79
- this.scheduler.onLocalChangeBefore.subscribe(() => {
80
- beforePosition = this.collaborate.getRelativeCursorLocation();
81
- }),
82
- this.collaborate.onAddSubModel.subscribe(() => {
83
- throw collabHistoryErrorFn("single document does not support submodels.");
84
- })
85
- );
86
- manager.on("stack-item-added", (event) => {
87
- if (event.type === "undo") {
88
- if (event.origin === manager) {
89
- this.index++;
90
- } else {
91
- this.historyItems.length = this.index;
92
- this.historyItems.push({
93
- before: beforePosition,
94
- after: this.collaborate.getRelativeCursorLocation()
95
- });
96
- this.index++;
97
- }
98
- } else {
99
- this.index--;
100
- }
101
- if (manager.undoStack.length > this.stackSize) {
102
- this.historyItems.shift();
103
- manager.undoStack.shift();
104
- }
105
- if (event.origin === this.collaborate.yDoc) {
106
- this.pushEvent.next();
107
- }
108
- this.changeEvent.next();
109
- });
110
- manager.on("stack-item-popped", (ev) => {
111
- const index = ev.type === "undo" ? this.index : this.index - 1;
112
- const position = this.historyItems[index] || null;
113
- const p = ev.type === "undo" ? position?.before : position?.after;
114
- this.collaborate.restoreCursorPosition(p);
115
- });
116
- }
117
- back() {
118
- if (this.canBack) {
119
- this.manager?.undo();
120
- this.backEvent.next();
121
- }
122
- }
123
- forward() {
124
- if (this.canForward) {
125
- this.manager?.redo();
126
- this.forwardEvent.next();
127
- }
128
- }
129
- clear() {
130
- const last = this.historyItems.pop();
131
- this.historyItems = last ? [last] : [];
132
- this.index = last ? 1 : 0;
133
- this.manager?.clear();
134
- this.changeEvent.next();
135
- }
136
- destroy() {
137
- this.index = 0;
138
- this.historyItems = [];
139
- this.subscriptions.forEach((i) => i.unsubscribe());
140
- if (this.manager) {
141
- this.manager.destroy();
142
- this.manager.captureTransaction = () => true;
143
- this.manager.deleteFilter = () => true;
144
- this.manager.trackedOrigins = /* @__PURE__ */ new Set([null]);
1
+ 'use strict';
2
+
3
+ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
4
+
5
+ const core$1 = require('@viewfly/core');
6
+ const core = require('@textbus/core');
7
+ const stream = require('@tanbo/stream');
8
+ const yjs = require('yjs');
9
+ const provider = require('@hocuspocus/provider');
10
+ const yWebsocket = require('y-websocket');
11
+
12
+ function _assert_this_initialized$2(self) {
13
+ if (self === void 0) {
14
+ throw new ReferenceError("this hasn't been initialised - super() hasn't been called");
145
15
  }
146
- this.manager = null;
147
- }
148
- };
149
- exports.CollabHistory = __decorateClass$3([
150
- core$1.Injectable(),
151
- __decorateParam$1(3, core$1.Inject(core.HISTORY_STACK_SIZE)),
152
- __decorateParam$1(4, core$1.Optional())
153
- ], exports.CollabHistory);
154
- var __getOwnPropDesc$2 = Object.getOwnPropertyDescriptor;
155
- var __decorateClass$2 = (decorators, target, key, kind) => {
156
- var result = kind > 1 ? void 0 : kind ? __getOwnPropDesc$2(target, key) : target;
157
- for (var i = decorators.length - 1, decorator; i >= 0; i--)
158
- if (decorator = decorators[i])
159
- result = decorator(result) || result;
160
- return result;
161
- };
162
- const collaborateErrorFn = core.makeError("Collaborate");
163
- class SlotMap {
164
- slotAndYTextMap = /* @__PURE__ */ new WeakMap();
165
- yTextAndSlotMap = /* @__PURE__ */ new WeakMap();
166
- set(key, value) {
167
- if (key instanceof core.Slot) {
168
- this.slotAndYTextMap.set(key, value);
169
- this.yTextAndSlotMap.set(value, key);
170
- } else {
171
- this.slotAndYTextMap.set(value, key);
172
- this.yTextAndSlotMap.set(key, value);
16
+ return self;
17
+ }
18
+ function _call_super$2(_this, derived, args) {
19
+ derived = _get_prototype_of$2(derived);
20
+ return _possible_constructor_return$2(_this, _is_native_reflect_construct$2() ? Reflect.construct(derived, args || [], _get_prototype_of$2(_this).constructor) : derived.apply(_this, args));
21
+ }
22
+ function _class_call_check$9(instance, Constructor) {
23
+ if (!(instance instanceof Constructor)) {
24
+ throw new TypeError("Cannot call a class as a function");
173
25
  }
174
- }
175
- get(key) {
176
- if (key instanceof core.Slot) {
177
- return this.slotAndYTextMap.get(key) || null;
26
+ }
27
+ function _defineProperties$8(target, props) {
28
+ for(var i = 0; i < props.length; i++){
29
+ var descriptor = props[i];
30
+ descriptor.enumerable = descriptor.enumerable || false;
31
+ descriptor.configurable = true;
32
+ if ("value" in descriptor) descriptor.writable = true;
33
+ Object.defineProperty(target, descriptor.key, descriptor);
178
34
  }
179
- return this.yTextAndSlotMap.get(key) || null;
180
- }
181
- delete(key) {
182
- if (key instanceof core.Slot) {
183
- const v = this.slotAndYTextMap.get(key);
184
- this.slotAndYTextMap.delete(key);
185
- if (v) {
186
- this.yTextAndSlotMap.delete(v);
187
- }
188
- } else {
189
- const v = this.yTextAndSlotMap.get(key);
190
- this.yTextAndSlotMap.delete(key);
191
- if (v) {
192
- this.slotAndYTextMap.delete(v);
193
- }
35
+ }
36
+ function _create_class$8(Constructor, protoProps, staticProps) {
37
+ if (protoProps) _defineProperties$8(Constructor.prototype, protoProps);
38
+ return Constructor;
39
+ }
40
+ function _get_prototype_of$2(o) {
41
+ _get_prototype_of$2 = Object.setPrototypeOf ? Object.getPrototypeOf : function getPrototypeOf(o) {
42
+ return o.__proto__ || Object.getPrototypeOf(o);
43
+ };
44
+ return _get_prototype_of$2(o);
45
+ }
46
+ function _inherits$2(subClass, superClass) {
47
+ if (typeof superClass !== "function" && superClass !== null) {
48
+ throw new TypeError("Super expression must either be null or a function");
194
49
  }
195
- }
196
- }
197
- exports.Collaborate = class Collaborate {
198
- constructor(scheduler, registry, selection, subModelLoader) {
199
- this.scheduler = scheduler;
200
- this.registry = registry;
201
- this.selection = selection;
202
- this.subModelLoader = subModelLoader;
203
- this.onAddSubModel = this.addSubModelEvent.asObservable();
204
- }
205
- scheduler;
206
- registry;
207
- selection;
208
- subModelLoader;
209
- yDoc = new yjs.Doc();
210
- slotMap = new SlotMap();
211
- onAddSubModel;
212
- subscriptions = [];
213
- updateFromRemote = false;
214
- addSubModelEvent = new stream.Subject();
215
- updateRemoteActions = /* @__PURE__ */ new WeakMap();
216
- noRecord = {};
217
- syncRootComponent(yDoc, sharedComponent, localComponent) {
218
- this.initSyncEvent(yDoc);
219
- this.syncComponent(yDoc, sharedComponent, localComponent);
220
- }
221
- syncRootSlot(yDoc, sharedSlot, localSlot) {
222
- if (sharedSlot.length) {
223
- localSlot.retain(0);
224
- localSlot.delete(localSlot.length);
225
- localSlot.cleanAttributes();
226
- localSlot.cleanFormats();
227
- this.initLocalSlotBySharedSlot(sharedSlot, localSlot);
228
- } else {
229
- yDoc.transact(() => {
230
- this.initSharedSlotByLocalSlot(sharedSlot, localSlot);
231
- });
50
+ subClass.prototype = Object.create(superClass && superClass.prototype, {
51
+ constructor: {
52
+ value: subClass,
53
+ writable: true,
54
+ configurable: true
55
+ }
56
+ });
57
+ if (superClass) _set_prototype_of$2(subClass, superClass);
58
+ }
59
+ function _possible_constructor_return$2(self, call) {
60
+ if (call && (_type_of$3(call) === "object" || typeof call === "function")) {
61
+ return call;
232
62
  }
233
- this.initSyncEvent(yDoc);
234
- this.syncSlot(sharedSlot, localSlot);
235
- }
236
- getAbstractSelection(position) {
237
- const anchorPosition = yjs.createAbsolutePositionFromRelativePosition(position.anchor.position, position.anchor.doc);
238
- const focusPosition = yjs.createAbsolutePositionFromRelativePosition(position.focus.position, position.focus.doc);
239
- if (anchorPosition && focusPosition) {
240
- const focusSlot = this.slotMap.get(focusPosition.type);
241
- const anchorSlot = this.slotMap.get(anchorPosition.type);
242
- if (focusSlot && anchorSlot) {
243
- return {
244
- anchorSlot,
245
- anchorOffset: anchorPosition.index,
246
- focusSlot,
247
- focusOffset: focusPosition.index
248
- };
249
- }
63
+ return _assert_this_initialized$2(self);
64
+ }
65
+ function _set_prototype_of$2(o, p) {
66
+ _set_prototype_of$2 = Object.setPrototypeOf || function setPrototypeOf(o, p) {
67
+ o.__proto__ = p;
68
+ return o;
69
+ };
70
+ return _set_prototype_of$2(o, p);
71
+ }
72
+ function _type_of$3(obj) {
73
+ "@swc/helpers - typeof";
74
+ return obj && typeof Symbol !== "undefined" && obj.constructor === Symbol ? "symbol" : typeof obj;
75
+ }
76
+ function _is_native_reflect_construct$2() {
77
+ try {
78
+ var result = !Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function() {}));
79
+ } catch (_) {}
80
+ return (_is_native_reflect_construct$2 = function() {
81
+ return !!result;
82
+ })();
83
+ }
84
+ function _ts_decorate$3(decorators, target, key, desc) {
85
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
86
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
87
+ 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;
88
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
89
+ }
90
+ var subModelLoaderErrorFn = core.makeError('subModelLoaderError');
91
+ /**
92
+ * 子文档加载器
93
+ */ var SubModelLoader = function SubModelLoader() {
94
+ _class_call_check$9(this, SubModelLoader);
95
+ };
96
+ exports.NonSubModelLoader = /*#__PURE__*/ function(SubModelLoader) {
97
+ _inherits$2(NonSubModelLoader, SubModelLoader);
98
+ function NonSubModelLoader() {
99
+ _class_call_check$9(this, NonSubModelLoader);
100
+ return _call_super$2(this, NonSubModelLoader, arguments);
250
101
  }
251
- return null;
252
- }
253
- getRelativeCursorLocation() {
254
- const { anchorSlot, anchorOffset, focusSlot, focusOffset } = this.selection;
255
- if (anchorSlot) {
256
- const anchorYText = this.slotMap.get(anchorSlot);
257
- if (anchorYText) {
258
- const anchorPosition = yjs.createRelativePositionFromTypeIndex(anchorYText, anchorOffset);
259
- if (focusSlot) {
260
- const focusYText = this.slotMap.get(focusSlot);
261
- if (focusYText) {
262
- const focusPosition = yjs.createRelativePositionFromTypeIndex(focusYText, focusOffset);
263
- return {
264
- focus: {
265
- doc: focusYText.doc,
266
- position: focusPosition
267
- },
268
- anchor: {
269
- doc: anchorYText.doc,
270
- position: anchorPosition
271
- }
272
- };
273
- }
102
+ _create_class$8(NonSubModelLoader, [
103
+ {
104
+ key: "createSubModelBySlot",
105
+ value: function createSubModelBySlot() {
106
+ throw subModelLoaderErrorFn('single document does not support async slot.');
107
+ }
108
+ },
109
+ {
110
+ key: "createSubModelByComponent",
111
+ value: function createSubModelByComponent() {
112
+ throw subModelLoaderErrorFn('single document does not support async component.');
113
+ }
114
+ },
115
+ {
116
+ key: "loadSubModelByComponent",
117
+ value: function loadSubModelByComponent() {
118
+ throw subModelLoaderErrorFn('single document does not support async component.');
119
+ }
120
+ },
121
+ {
122
+ key: "loadSubModelBySlot",
123
+ value: function loadSubModelBySlot() {
124
+ throw subModelLoaderErrorFn('single document does not support async slot.');
125
+ }
126
+ },
127
+ {
128
+ key: "getLoadedModelBySlot",
129
+ value: function getLoadedModelBySlot() {
130
+ throw subModelLoaderErrorFn('single document does not support async slot.');
131
+ }
132
+ },
133
+ {
134
+ key: "getLoadedModelByComponent",
135
+ value: function getLoadedModelByComponent() {
136
+ throw subModelLoaderErrorFn('single document does not support async component.');
137
+ }
274
138
  }
275
- }
139
+ ]);
140
+ return NonSubModelLoader;
141
+ }(SubModelLoader);
142
+ exports.NonSubModelLoader = _ts_decorate$3([
143
+ core$1.Injectable()
144
+ ], exports.NonSubModelLoader);
145
+
146
+ function _array_like_to_array(arr, len) {
147
+ if (len == null || len > arr.length) len = arr.length;
148
+ for(var i = 0, arr2 = new Array(len); i < len; i++)arr2[i] = arr[i];
149
+ return arr2;
150
+ }
151
+ function _array_with_holes(arr) {
152
+ if (Array.isArray(arr)) return arr;
153
+ }
154
+ function _array_without_holes(arr) {
155
+ if (Array.isArray(arr)) return _array_like_to_array(arr);
156
+ }
157
+ function _class_call_check$8(instance, Constructor) {
158
+ if (!(instance instanceof Constructor)) {
159
+ throw new TypeError("Cannot call a class as a function");
160
+ }
161
+ }
162
+ function _defineProperties$7(target, props) {
163
+ for(var i = 0; i < props.length; i++){
164
+ var descriptor = props[i];
165
+ descriptor.enumerable = descriptor.enumerable || false;
166
+ descriptor.configurable = true;
167
+ if ("value" in descriptor) descriptor.writable = true;
168
+ Object.defineProperty(target, descriptor.key, descriptor);
276
169
  }
277
- return null;
278
- }
279
- restoreCursorPosition(position) {
280
- if (!position) {
281
- this.selection.unSelect();
282
- return;
170
+ }
171
+ function _create_class$7(Constructor, protoProps, staticProps) {
172
+ if (protoProps) _defineProperties$7(Constructor.prototype, protoProps);
173
+ return Constructor;
174
+ }
175
+ function _define_property$8(obj, key, value) {
176
+ if (key in obj) {
177
+ Object.defineProperty(obj, key, {
178
+ value: value,
179
+ enumerable: true,
180
+ configurable: true,
181
+ writable: true
182
+ });
183
+ } else {
184
+ obj[key] = value;
283
185
  }
284
- const selection = this.getAbstractSelection(position);
285
- if (selection) {
286
- this.selection.setBaseAndExtent(
287
- selection.anchorSlot,
288
- selection.anchorOffset,
289
- selection.focusSlot,
290
- selection.focusOffset
291
- );
186
+ return obj;
187
+ }
188
+ function _instanceof$1(left, right) {
189
+ "@swc/helpers - instanceof";
190
+ if (right != null && typeof Symbol !== "undefined" && right[Symbol.hasInstance]) {
191
+ return !!right[Symbol.hasInstance](left);
192
+ } else {
193
+ return left instanceof right;
292
194
  }
293
- }
294
- initSyncEvent(yDoc) {
295
- this.subscriptions.push(
296
- this.scheduler.onDocChanged.pipe(
297
- stream.map((item) => {
298
- return item.filter((i) => {
299
- return i.from !== core.ChangeOrigin.Remote;
300
- });
301
- }),
302
- stream.filter((item) => {
303
- return item.length;
304
- })
305
- ).subscribe(() => {
306
- const updates = [];
307
- let update = null;
308
- const updateRemoteActions = this.updateRemoteActions.get(yDoc) || [];
309
- for (const item of updateRemoteActions) {
310
- if (!update) {
311
- update = {
312
- record: item.record,
313
- actions: []
314
- };
315
- updates.push(update);
316
- }
317
- if (update.record === item.record) {
318
- update.actions.push(item.action);
319
- } else {
320
- update = {
321
- record: item.record,
322
- actions: [item.action]
323
- };
324
- updates.push(update);
325
- }
195
+ }
196
+ function _iterable_to_array(iter) {
197
+ if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter);
198
+ }
199
+ function _iterable_to_array_limit(arr, i) {
200
+ var _i = arr == null ? null : typeof Symbol !== "undefined" && arr[Symbol.iterator] || arr["@@iterator"];
201
+ if (_i == null) return;
202
+ var _arr = [];
203
+ var _n = true;
204
+ var _d = false;
205
+ var _s, _e;
206
+ try {
207
+ for(_i = _i.call(arr); !(_n = (_s = _i.next()).done); _n = true){
208
+ _arr.push(_s.value);
209
+ if (i && _arr.length === i) break;
326
210
  }
327
- this.updateRemoteActions.delete(yDoc);
328
- for (const item of updates) {
329
- yDoc.transact(() => {
330
- item.actions.forEach((fn) => {
331
- fn();
332
- });
333
- }, item.record ? yDoc : this.noRecord);
211
+ } catch (err) {
212
+ _d = true;
213
+ _e = err;
214
+ } finally{
215
+ try {
216
+ if (!_n && _i["return"] != null) _i["return"]();
217
+ } finally{
218
+ if (_d) throw _e;
334
219
  }
335
- })
336
- );
337
- }
338
- syncComponent(yDoc, sharedComponent, localComponent) {
339
- let state = sharedComponent.get("state");
340
- if (!state) {
341
- state = new yjs.Map();
342
- this.syncLocalMapToSharedMap(localComponent.state, state);
343
- yDoc.transact(() => {
344
- sharedComponent.set("state", state);
345
- });
346
- } else {
347
- Object.keys(localComponent.state).forEach((key) => {
348
- Reflect.deleteProperty(localComponent.state, key);
349
- });
350
- this.syncSharedMapToLocalMap(state, localComponent.state);
351
220
  }
352
- }
353
- syncSlot(sharedSlot, localSlot) {
354
- const syncRemote = (ev, tr) => {
355
- this.runRemoteUpdate(tr, () => {
356
- localSlot.retain(0);
357
- ev.keysChanged.forEach((key) => {
358
- const change = ev.keys.get(key);
359
- if (!change) {
360
- return;
361
- }
362
- const updateType = change.action;
363
- if (updateType === "update" || updateType === "add") {
364
- const attribute = this.registry.getAttribute(key);
365
- if (attribute) {
366
- localSlot.setAttribute(attribute, sharedSlot.getAttribute(key));
367
- }
368
- } else if (updateType === "delete") {
369
- const attribute = this.registry.getAttribute(key);
370
- if (attribute) {
371
- localSlot.removeAttribute(attribute);
372
- }
373
- }
374
- });
375
- ev.delta.forEach((action) => {
376
- if (Reflect.has(action, "retain")) {
377
- if (action.attributes) {
378
- const formats = remoteFormatsToLocal(this.registry, action.attributes);
379
- if (formats.length) {
380
- localSlot.retain(action.retain, formats);
381
- }
382
- }
383
- localSlot.retain(localSlot.index + action.retain);
384
- } else if (action.insert) {
385
- const index = localSlot.index;
386
- let length = 1;
387
- if (typeof action.insert === "string") {
388
- length = action.insert.length;
389
- localSlot.insert(action.insert, remoteFormatsToLocal(this.registry, action.attributes));
390
- } else {
391
- const sharedComponent = action.insert;
392
- const component = this.createLocalComponentBySharedComponent(sharedComponent);
393
- localSlot.insert(component);
394
- }
395
- if (this.selection.isSelected && !(tr.origin instanceof yjs.UndoManager)) {
396
- if (localSlot === this.selection.anchorSlot && this.selection.anchorOffset > index) {
397
- this.selection.setAnchor(localSlot, this.selection.anchorOffset + length);
398
- }
399
- if (localSlot === this.selection.focusSlot && this.selection.focusOffset > index) {
400
- this.selection.setFocus(localSlot, this.selection.focusOffset + length);
401
- }
402
- }
403
- } else if (action.delete) {
404
- const index = localSlot.index;
405
- localSlot.delete(action.delete);
406
- if (this.selection.isSelected && !(tr.origin instanceof yjs.UndoManager)) {
407
- if (localSlot === this.selection.anchorSlot && this.selection.anchorOffset >= index) {
408
- this.selection.setAnchor(localSlot, this.selection.startOffset - action.delete);
409
- }
410
- if (localSlot === this.selection.focusSlot && this.selection.focusOffset >= index) {
411
- this.selection.setFocus(localSlot, this.selection.focusOffset - action.delete);
412
- }
413
- }
414
- }
415
- });
416
- });
417
- };
418
- sharedSlot.observe(syncRemote);
419
- const sub = localSlot.onContentChange.subscribe((actions) => {
420
- this.runLocalUpdate(sharedSlot.doc, true, () => {
421
- let offset = 0;
422
- let length = 0;
423
- for (const action of actions) {
424
- if (action.type === "retain") {
425
- const formats = action.formats;
426
- if (formats) {
427
- const keys = Object.keys(formats);
428
- let length2 = keys.length;
429
- keys.forEach((key) => {
430
- const formatter = this.registry.getFormatter(key);
431
- if (!formatter) {
432
- length2--;
433
- Reflect.deleteProperty(formats, key);
221
+ return _arr;
222
+ }
223
+ function _non_iterable_rest() {
224
+ throw new TypeError("Invalid attempt to destructure non-iterable instance.\\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
225
+ }
226
+ function _non_iterable_spread() {
227
+ throw new TypeError("Invalid attempt to spread non-iterable instance.\\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
228
+ }
229
+ function _sliced_to_array(arr, i) {
230
+ return _array_with_holes(arr) || _iterable_to_array_limit(arr, i) || _unsupported_iterable_to_array(arr, i) || _non_iterable_rest();
231
+ }
232
+ function _to_consumable_array(arr) {
233
+ return _array_without_holes(arr) || _iterable_to_array(arr) || _unsupported_iterable_to_array(arr) || _non_iterable_spread();
234
+ }
235
+ function _type_of$2(obj) {
236
+ "@swc/helpers - typeof";
237
+ return obj && typeof Symbol !== "undefined" && obj.constructor === Symbol ? "symbol" : typeof obj;
238
+ }
239
+ function _unsupported_iterable_to_array(o, minLen) {
240
+ if (!o) return;
241
+ if (typeof o === "string") return _array_like_to_array(o, minLen);
242
+ var n = Object.prototype.toString.call(o).slice(8, -1);
243
+ if (n === "Object" && o.constructor) n = o.constructor.name;
244
+ if (n === "Map" || n === "Set") return Array.from(n);
245
+ if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _array_like_to_array(o, minLen);
246
+ }
247
+ function _ts_decorate$2(decorators, target, key, desc) {
248
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
249
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
250
+ 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;
251
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
252
+ }
253
+ function _ts_metadata$2(k, v) {
254
+ if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
255
+ }
256
+ var collaborateErrorFn = core.makeError('Collaborate');
257
+ var SlotMap = /*#__PURE__*/ function() {
258
+ function SlotMap() {
259
+ _class_call_check$8(this, SlotMap);
260
+ _define_property$8(this, "slotAndYTextMap", new WeakMap());
261
+ _define_property$8(this, "yTextAndSlotMap", new WeakMap());
262
+ }
263
+ _create_class$7(SlotMap, [
264
+ {
265
+ key: "set",
266
+ value: function set(key, value) {
267
+ if (_instanceof$1(key, core.Slot)) {
268
+ this.slotAndYTextMap.set(key, value);
269
+ this.yTextAndSlotMap.set(value, key);
270
+ } else {
271
+ this.slotAndYTextMap.set(value, key);
272
+ this.yTextAndSlotMap.set(key, value);
434
273
  }
435
- });
436
- if (length2) {
437
- sharedSlot.format(offset, action.offset, formats);
438
- }
439
- } else {
440
- offset = action.offset;
441
- }
442
- } else if (action.type === "contentInsert") {
443
- const delta = sharedSlot.toDelta();
444
- const isEmpty = delta.length === 1 && delta[0].insert === core.Slot.emptyPlaceholder;
445
- if (typeof action.content === "string") {
446
- length = action.content.length;
447
- sharedSlot.insert(offset, action.content, action.formats || {});
448
- } else {
449
- length = 1;
450
- const sharedComponent = this.createSharedComponentByLocalComponent(action.ref);
451
- sharedSlot.insertEmbed(offset, sharedComponent, action.formats || {});
452
- }
453
- if (isEmpty && offset === 0) {
454
- sharedSlot.delete(sharedSlot.length - 1, 1);
455
- }
456
- offset += length;
457
- } else if (action.type === "delete") {
458
- const delta = sharedSlot.toDelta();
459
- if (sharedSlot.length) {
460
- sharedSlot.delete(offset, action.count);
461
- }
462
- if (sharedSlot.length === 0) {
463
- sharedSlot.insert(0, "\n", delta[0]?.attributes);
464
- }
465
- } else if (action.type === "attrSet") {
466
- sharedSlot.setAttribute(action.name, action.value);
467
- } else if (action.type === "attrDelete") {
468
- sharedSlot.removeAttribute(action.name);
469
- }
470
- }
471
- });
472
- });
473
- this.slotMap.set(localSlot, sharedSlot);
474
- localSlot.__changeMarker__.addDetachCallback(() => {
475
- this.slotMap.delete(localSlot);
476
- sharedSlot.unobserve(syncRemote);
477
- sub.unsubscribe();
478
- });
479
- }
480
- destroy() {
481
- this.subscriptions.forEach((i) => i.unsubscribe());
482
- this.subscriptions = [];
483
- }
484
- syncSharedMapToLocalMap(sharedMap, localMap) {
485
- sharedMap.forEach((value, key) => {
486
- localMap[key] = this.createLocalModelBySharedByModel(value);
487
- });
488
- this.syncObject(sharedMap, localMap);
489
- }
490
- createLocalMapBySharedMap(sharedMap) {
491
- const localMap = core.observe({});
492
- this.syncSharedMapToLocalMap(sharedMap, localMap);
493
- return localMap;
494
- }
495
- createLocalArrayBySharedArray(sharedArray) {
496
- const localArray = core.observe([]);
497
- localArray.push(...sharedArray.map((item) => this.createLocalModelBySharedByModel(item)));
498
- this.syncArray(sharedArray, localArray);
499
- return localArray;
500
- }
501
- syncLocalMapToSharedMap(localMap, sharedMap) {
502
- Object.entries(localMap).forEach(([key, value]) => {
503
- sharedMap.set(key, this.createSharedModelByLocalModel(value));
504
- });
505
- this.syncObject(sharedMap, localMap);
506
- }
507
- createSharedMapByLocalMap(localMap) {
508
- const sharedMap = new yjs.Map();
509
- this.syncLocalMapToSharedMap(localMap, sharedMap);
510
- return sharedMap;
511
- }
512
- createSharedArrayByLocalArray(localArray) {
513
- const sharedArray = new yjs.Array();
514
- localArray.forEach((value) => {
515
- sharedArray.push([this.createSharedModelByLocalModel(value)]);
516
- });
517
- this.syncArray(sharedArray, localArray);
518
- return sharedArray;
519
- }
520
- createSharedSlotByLocalSlot(localSlot) {
521
- const sharedSlot = new yjs.Text();
522
- const isAsyncSlot = localSlot instanceof core.AsyncSlot;
523
- sharedSlot.setAttribute("schema", [...localSlot.schema]);
524
- sharedSlot.setAttribute("type", isAsyncSlot ? "async" : "sync");
525
- if (isAsyncSlot) {
526
- let isDestroyed = false;
527
- const sharedMetadata = this.createSharedMapByLocalMap(localSlot.metadata);
528
- sharedSlot.setAttribute("metadata", sharedMetadata);
529
- this.subModelLoader.createSubModelBySlot(localSlot).then((subDocument) => {
530
- if (isDestroyed) {
531
- return;
274
+ }
275
+ },
276
+ {
277
+ key: "get",
278
+ value: function get(key) {
279
+ if (_instanceof$1(key, core.Slot)) {
280
+ return this.slotAndYTextMap.get(key) || null;
281
+ }
282
+ return this.yTextAndSlotMap.get(key) || null;
283
+ }
284
+ },
285
+ {
286
+ key: "delete",
287
+ value: function _delete(key) {
288
+ if (_instanceof$1(key, core.Slot)) {
289
+ var v = this.slotAndYTextMap.get(key);
290
+ this.slotAndYTextMap.delete(key);
291
+ if (v) {
292
+ this.yTextAndSlotMap.delete(v);
293
+ }
294
+ } else {
295
+ var v1 = this.yTextAndSlotMap.get(key);
296
+ this.yTextAndSlotMap.delete(key);
297
+ if (v1) {
298
+ this.slotAndYTextMap.delete(v1);
299
+ }
300
+ }
301
+ }
532
302
  }
533
- const content = subDocument.getText("content");
534
- const state = subDocument.getMap("state");
535
- this.syncLocalMapToSharedMap(localSlot.state, state);
536
- this.initSharedSlotByLocalSlot(content, localSlot);
537
- this.syncSlot(content, localSlot);
538
- this.addSubModelEvent.next({
539
- yDoc: subDocument,
540
- yType: content
541
- });
542
- this.initSyncEvent(subDocument);
543
- localSlot.loader.markAsLoaded();
544
- });
545
- localSlot.__changeMarker__.addDetachCallback(() => {
546
- isDestroyed = true;
547
- });
548
- return sharedSlot;
303
+ ]);
304
+ return SlotMap;
305
+ }();
306
+ exports.Collaborate = /*#__PURE__*/ function() {
307
+ function Collaborate(scheduler, registry, selection, subModelLoader) {
308
+ _class_call_check$8(this, Collaborate);
309
+ _define_property$8(this, "scheduler", void 0);
310
+ _define_property$8(this, "registry", void 0);
311
+ _define_property$8(this, "selection", void 0);
312
+ _define_property$8(this, "subModelLoader", void 0);
313
+ _define_property$8(this, "yDoc", void 0);
314
+ _define_property$8(this, "slotMap", void 0);
315
+ _define_property$8(this, "onAddSubModel", void 0);
316
+ _define_property$8(this, "subscriptions", void 0);
317
+ _define_property$8(this, "updateFromRemote", void 0);
318
+ _define_property$8(this, "addSubModelEvent", void 0);
319
+ _define_property$8(this, "updateRemoteActions", void 0);
320
+ _define_property$8(this, "noRecord", void 0);
321
+ this.scheduler = scheduler;
322
+ this.registry = registry;
323
+ this.selection = selection;
324
+ this.subModelLoader = subModelLoader;
325
+ this.yDoc = new yjs.Doc();
326
+ this.slotMap = new SlotMap();
327
+ this.subscriptions = [];
328
+ this.updateFromRemote = false;
329
+ this.addSubModelEvent = new stream.Subject();
330
+ this.updateRemoteActions = new WeakMap();
331
+ this.noRecord = {};
332
+ this.onAddSubModel = this.addSubModelEvent.asObservable();
549
333
  }
550
- const sharedSlotState = new yjs.Map();
551
- this.syncLocalMapToSharedMap(localSlot.state, sharedSlotState);
552
- sharedSlot.setAttribute("state", sharedSlotState);
553
- const sharedContent = new yjs.Text();
554
- this.initSharedSlotByLocalSlot(sharedContent, localSlot);
555
- sharedSlot.insertEmbed(0, sharedContent);
556
- this.syncSlot(sharedContent, localSlot);
557
- return sharedSlot;
558
- }
559
- initSharedSlotByLocalSlot(sharedContent, localSlot) {
560
- let offset = 0;
561
- localSlot.toDelta().forEach((i) => {
562
- let formats = {};
563
- if (i.formats) {
564
- i.formats.forEach((item) => {
565
- formats[item[0].name] = item[1];
566
- });
567
- } else {
568
- formats = null;
569
- }
570
- if (typeof i.insert === "string") {
571
- sharedContent.insert(offset, i.insert, formats);
572
- } else {
573
- const sharedComponent = this.createSharedComponentByLocalComponent(i.insert);
574
- sharedContent.insertEmbed(offset, sharedComponent, formats);
575
- }
576
- offset += i.insert.length;
577
- });
578
- localSlot.getAttributes().forEach((item) => {
579
- sharedContent.setAttribute(item[0].name, item[1]);
580
- });
581
- }
582
- createLocalSlotBySharedSlot(sharedSlot) {
583
- const type = sharedSlot.getAttribute("type");
584
- const schema = sharedSlot.getAttribute("schema");
585
- if (type === "async") {
586
- const metadata = sharedSlot.getAttribute("metadata");
587
- const slot = new core.AsyncSlot(schema || [], {}, {});
588
- this.syncSharedMapToLocalMap(metadata, slot.metadata);
589
- const loadedSubDocument = this.subModelLoader.getLoadedModelBySlot(slot);
590
- if (loadedSubDocument) {
591
- const subContent = loadedSubDocument.getText("content");
592
- const data = loadedSubDocument.getMap("state");
593
- this.syncSharedMapToLocalMap(data, slot.state);
594
- this.syncRootSlot(loadedSubDocument, subContent, slot);
595
- this.addSubModelEvent.next({
596
- yDoc: loadedSubDocument,
597
- yType: subContent
598
- });
599
- slot.loader.markAsLoaded();
600
- return slot;
601
- }
602
- let isDestroyed = false;
603
- slot.loader.onRequestLoad.toPromise().then(() => {
604
- return this.subModelLoader.loadSubModelBySlot(slot);
605
- }).then((subDocument) => {
606
- if (isDestroyed) {
607
- return;
334
+ _create_class$7(Collaborate, [
335
+ {
336
+ key: "syncRootComponent",
337
+ value: function syncRootComponent(yDoc, sharedComponent, localComponent) {
338
+ this.initSyncEvent(yDoc);
339
+ this.syncComponent(yDoc, sharedComponent, localComponent);
340
+ }
341
+ },
342
+ {
343
+ key: "syncRootSlot",
344
+ value: function syncRootSlot(yDoc, sharedSlot, localSlot) {
345
+ var _this = this;
346
+ if (sharedSlot.length) {
347
+ localSlot.retain(0);
348
+ localSlot.delete(localSlot.length);
349
+ localSlot.cleanAttributes();
350
+ localSlot.cleanFormats();
351
+ this.initLocalSlotBySharedSlot(sharedSlot, localSlot);
352
+ } else {
353
+ yDoc.transact(function() {
354
+ _this.initSharedSlotByLocalSlot(sharedSlot, localSlot);
355
+ });
356
+ }
357
+ this.initSyncEvent(yDoc);
358
+ this.syncSlot(sharedSlot, localSlot);
359
+ }
360
+ },
361
+ {
362
+ key: "getAbstractSelection",
363
+ value: function getAbstractSelection(position) {
364
+ var anchorPosition = yjs.createAbsolutePositionFromRelativePosition(position.anchor.position, position.anchor.doc);
365
+ var focusPosition = yjs.createAbsolutePositionFromRelativePosition(position.focus.position, position.focus.doc);
366
+ if (anchorPosition && focusPosition) {
367
+ var focusSlot = this.slotMap.get(focusPosition.type);
368
+ var anchorSlot = this.slotMap.get(anchorPosition.type);
369
+ if (focusSlot && anchorSlot) {
370
+ return {
371
+ anchorSlot: anchorSlot,
372
+ anchorOffset: anchorPosition.index,
373
+ focusSlot: focusSlot,
374
+ focusOffset: focusPosition.index
375
+ };
376
+ }
377
+ }
378
+ return null;
379
+ }
380
+ },
381
+ {
382
+ key: "getRelativeCursorLocation",
383
+ value: function getRelativeCursorLocation() {
384
+ var _this_selection = this.selection, anchorSlot = _this_selection.anchorSlot, anchorOffset = _this_selection.anchorOffset, focusSlot = _this_selection.focusSlot, focusOffset = _this_selection.focusOffset;
385
+ if (anchorSlot) {
386
+ var anchorYText = this.slotMap.get(anchorSlot);
387
+ if (anchorYText) {
388
+ var anchorPosition = yjs.createRelativePositionFromTypeIndex(anchorYText, anchorOffset);
389
+ if (focusSlot) {
390
+ var focusYText = this.slotMap.get(focusSlot);
391
+ if (focusYText) {
392
+ var focusPosition = yjs.createRelativePositionFromTypeIndex(focusYText, focusOffset);
393
+ return {
394
+ focus: {
395
+ doc: focusYText.doc,
396
+ position: focusPosition
397
+ },
398
+ anchor: {
399
+ doc: anchorYText.doc,
400
+ position: anchorPosition
401
+ }
402
+ };
403
+ }
404
+ }
405
+ }
406
+ }
407
+ return null;
408
+ }
409
+ },
410
+ {
411
+ key: "restoreCursorPosition",
412
+ value: function restoreCursorPosition(position) {
413
+ if (!position) {
414
+ this.selection.unSelect();
415
+ return;
416
+ }
417
+ var selection = this.getAbstractSelection(position);
418
+ if (selection) {
419
+ this.selection.setBaseAndExtent(selection.anchorSlot, selection.anchorOffset, selection.focusSlot, selection.focusOffset);
420
+ }
421
+ }
422
+ },
423
+ {
424
+ key: "initSyncEvent",
425
+ value: function initSyncEvent(yDoc) {
426
+ var _this = this;
427
+ this.subscriptions.push(this.scheduler.onDocChanged.pipe(stream.map(function(item) {
428
+ return item.filter(function(i) {
429
+ return i.from !== core.ChangeOrigin.Remote;
430
+ });
431
+ }), stream.filter(function(item) {
432
+ return item.length;
433
+ })).subscribe(function() {
434
+ var updates = [];
435
+ var update = null;
436
+ var updateRemoteActions = _this.updateRemoteActions.get(yDoc) || [];
437
+ var _iteratorNormalCompletion = true, _didIteratorError = false, _iteratorError = undefined;
438
+ try {
439
+ for(var _iterator = updateRemoteActions[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true){
440
+ var item = _step.value;
441
+ if (!update) {
442
+ update = {
443
+ record: item.record,
444
+ actions: []
445
+ };
446
+ updates.push(update);
447
+ }
448
+ if (update.record === item.record) {
449
+ update.actions.push(item.action);
450
+ } else {
451
+ update = {
452
+ record: item.record,
453
+ actions: [
454
+ item.action
455
+ ]
456
+ };
457
+ updates.push(update);
458
+ }
459
+ }
460
+ } catch (err) {
461
+ _didIteratorError = true;
462
+ _iteratorError = err;
463
+ } finally{
464
+ try {
465
+ if (!_iteratorNormalCompletion && _iterator.return != null) {
466
+ _iterator.return();
467
+ }
468
+ } finally{
469
+ if (_didIteratorError) {
470
+ throw _iteratorError;
471
+ }
472
+ }
473
+ }
474
+ _this.updateRemoteActions.delete(yDoc);
475
+ var _iteratorNormalCompletion1 = true, _didIteratorError1 = false, _iteratorError1 = undefined;
476
+ try {
477
+ var _loop = function() {
478
+ var item = _step1.value;
479
+ yDoc.transact(function() {
480
+ item.actions.forEach(function(fn) {
481
+ fn();
482
+ });
483
+ }, item.record ? yDoc : _this.noRecord);
484
+ };
485
+ for(var _iterator1 = updates[Symbol.iterator](), _step1; !(_iteratorNormalCompletion1 = (_step1 = _iterator1.next()).done); _iteratorNormalCompletion1 = true)_loop();
486
+ } catch (err) {
487
+ _didIteratorError1 = true;
488
+ _iteratorError1 = err;
489
+ } finally{
490
+ try {
491
+ if (!_iteratorNormalCompletion1 && _iterator1.return != null) {
492
+ _iterator1.return();
493
+ }
494
+ } finally{
495
+ if (_didIteratorError1) {
496
+ throw _iteratorError1;
497
+ }
498
+ }
499
+ }
500
+ }));
501
+ }
502
+ },
503
+ {
504
+ key: "syncComponent",
505
+ value: function syncComponent(yDoc, sharedComponent, localComponent) {
506
+ var state = sharedComponent.get('state');
507
+ if (!state) {
508
+ state = new yjs.Map();
509
+ this.syncLocalMapToSharedMap(localComponent.state, state);
510
+ yDoc.transact(function() {
511
+ sharedComponent.set('state', state);
512
+ });
513
+ } else {
514
+ Object.keys(localComponent.state).forEach(function(key) {
515
+ Reflect.deleteProperty(localComponent.state, key);
516
+ });
517
+ this.syncSharedMapToLocalMap(state, localComponent.state);
518
+ }
519
+ }
520
+ },
521
+ {
522
+ key: "syncSlot",
523
+ value: function syncSlot(sharedSlot, localSlot) {
524
+ var _this = this;
525
+ var syncRemote = function syncRemote(ev, tr) {
526
+ _this.runRemoteUpdate(tr, function() {
527
+ localSlot.retain(0);
528
+ ev.keysChanged.forEach(function(key) {
529
+ var change = ev.keys.get(key);
530
+ if (!change) {
531
+ return;
532
+ }
533
+ var updateType = change.action;
534
+ if (updateType === 'update' || updateType === 'add') {
535
+ var attribute = _this.registry.getAttribute(key);
536
+ if (attribute) {
537
+ localSlot.setAttribute(attribute, sharedSlot.getAttribute(key));
538
+ }
539
+ } else if (updateType === 'delete') {
540
+ var attribute1 = _this.registry.getAttribute(key);
541
+ if (attribute1) {
542
+ localSlot.removeAttribute(attribute1);
543
+ }
544
+ }
545
+ });
546
+ ev.delta.forEach(function(action) {
547
+ if (Reflect.has(action, 'retain')) {
548
+ if (action.attributes) {
549
+ var formats = remoteFormatsToLocal(_this.registry, action.attributes);
550
+ if (formats.length) {
551
+ localSlot.retain(action.retain, formats);
552
+ }
553
+ }
554
+ localSlot.retain(localSlot.index + action.retain);
555
+ } else if (action.insert) {
556
+ var index = localSlot.index;
557
+ var length = 1;
558
+ if (typeof action.insert === 'string') {
559
+ length = action.insert.length;
560
+ localSlot.insert(action.insert, remoteFormatsToLocal(_this.registry, action.attributes));
561
+ } else {
562
+ var sharedComponent = action.insert;
563
+ var component = _this.createLocalComponentBySharedComponent(sharedComponent);
564
+ localSlot.insert(component);
565
+ }
566
+ if (_this.selection.isSelected && !_instanceof$1(tr.origin, yjs.UndoManager)) {
567
+ if (localSlot === _this.selection.anchorSlot && _this.selection.anchorOffset > index) {
568
+ _this.selection.setAnchor(localSlot, _this.selection.anchorOffset + length);
569
+ }
570
+ if (localSlot === _this.selection.focusSlot && _this.selection.focusOffset > index) {
571
+ _this.selection.setFocus(localSlot, _this.selection.focusOffset + length);
572
+ }
573
+ }
574
+ } else if (action.delete) {
575
+ var index1 = localSlot.index;
576
+ localSlot.delete(action.delete);
577
+ if (_this.selection.isSelected && !_instanceof$1(tr.origin, yjs.UndoManager)) {
578
+ if (localSlot === _this.selection.anchorSlot && _this.selection.anchorOffset >= index1) {
579
+ _this.selection.setAnchor(localSlot, _this.selection.startOffset - action.delete);
580
+ }
581
+ if (localSlot === _this.selection.focusSlot && _this.selection.focusOffset >= index1) {
582
+ _this.selection.setFocus(localSlot, _this.selection.focusOffset - action.delete);
583
+ }
584
+ }
585
+ }
586
+ });
587
+ });
588
+ };
589
+ sharedSlot.observe(syncRemote);
590
+ var sub = localSlot.onContentChange.subscribe(function(actions) {
591
+ _this.runLocalUpdate(sharedSlot.doc, true, function() {
592
+ var offset = 0;
593
+ var length = 0;
594
+ var _iteratorNormalCompletion = true, _didIteratorError = false, _iteratorError = undefined;
595
+ try {
596
+ var _loop = function() {
597
+ var action = _step.value;
598
+ if (action.type === 'retain') {
599
+ var formats = action.formats;
600
+ if (formats) {
601
+ var keys = Object.keys(formats);
602
+ var length1 = keys.length;
603
+ keys.forEach(function(key) {
604
+ var formatter = _this.registry.getFormatter(key);
605
+ if (!formatter) {
606
+ length1--;
607
+ Reflect.deleteProperty(formats, key);
608
+ }
609
+ });
610
+ if (length1) {
611
+ sharedSlot.format(offset, action.offset, formats);
612
+ }
613
+ } else {
614
+ offset = action.offset;
615
+ }
616
+ } else if (action.type === 'contentInsert') {
617
+ var delta = sharedSlot.toDelta();
618
+ var isEmpty = delta.length === 1 && delta[0].insert === core.Slot.emptyPlaceholder;
619
+ if (typeof action.content === 'string') {
620
+ length = action.content.length;
621
+ sharedSlot.insert(offset, action.content, action.formats || {});
622
+ } else {
623
+ length = 1;
624
+ var sharedComponent = _this.createSharedComponentByLocalComponent(action.ref);
625
+ sharedSlot.insertEmbed(offset, sharedComponent, action.formats || {});
626
+ }
627
+ if (isEmpty && offset === 0) {
628
+ sharedSlot.delete(sharedSlot.length - 1, 1);
629
+ }
630
+ offset += length;
631
+ } else if (action.type === 'delete') {
632
+ var delta1 = sharedSlot.toDelta();
633
+ if (sharedSlot.length) {
634
+ sharedSlot.delete(offset, action.count);
635
+ }
636
+ if (sharedSlot.length === 0) {
637
+ var _delta_;
638
+ sharedSlot.insert(0, '\n', (_delta_ = delta1[0]) === null || _delta_ === void 0 ? void 0 : _delta_.attributes);
639
+ }
640
+ } else if (action.type === 'attrSet') {
641
+ sharedSlot.setAttribute(action.name, action.value);
642
+ } else if (action.type === 'attrDelete') {
643
+ sharedSlot.removeAttribute(action.name);
644
+ }
645
+ };
646
+ for(var _iterator = actions[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true)_loop();
647
+ } catch (err) {
648
+ _didIteratorError = true;
649
+ _iteratorError = err;
650
+ } finally{
651
+ try {
652
+ if (!_iteratorNormalCompletion && _iterator.return != null) {
653
+ _iterator.return();
654
+ }
655
+ } finally{
656
+ if (_didIteratorError) {
657
+ throw _iteratorError;
658
+ }
659
+ }
660
+ }
661
+ });
662
+ });
663
+ this.slotMap.set(localSlot, sharedSlot);
664
+ localSlot.__changeMarker__.addDetachCallback(function() {
665
+ _this.slotMap.delete(localSlot);
666
+ sharedSlot.unobserve(syncRemote);
667
+ sub.unsubscribe();
668
+ });
669
+ }
670
+ },
671
+ {
672
+ key: "destroy",
673
+ value: function destroy() {
674
+ this.subscriptions.forEach(function(i) {
675
+ return i.unsubscribe();
676
+ });
677
+ this.subscriptions = [];
678
+ }
679
+ },
680
+ {
681
+ key: "syncSharedMapToLocalMap",
682
+ value: function syncSharedMapToLocalMap(sharedMap, localMap) {
683
+ var _this = this;
684
+ sharedMap.forEach(function(value, key) {
685
+ localMap[key] = _this.createLocalModelBySharedByModel(value);
686
+ });
687
+ this.syncObject(sharedMap, localMap);
688
+ }
689
+ },
690
+ {
691
+ key: "createLocalMapBySharedMap",
692
+ value: function createLocalMapBySharedMap(sharedMap) {
693
+ var localMap = core.observe({});
694
+ this.syncSharedMapToLocalMap(sharedMap, localMap);
695
+ return localMap;
696
+ }
697
+ },
698
+ {
699
+ key: "createLocalArrayBySharedArray",
700
+ value: function createLocalArrayBySharedArray(sharedArray) {
701
+ var _this = this;
702
+ var _localArray;
703
+ var localArray = core.observe([]);
704
+ (_localArray = localArray).push.apply(_localArray, _to_consumable_array(sharedArray.map(function(item) {
705
+ return _this.createLocalModelBySharedByModel(item);
706
+ })));
707
+ this.syncArray(sharedArray, localArray);
708
+ return localArray;
709
+ }
710
+ },
711
+ {
712
+ key: "syncLocalMapToSharedMap",
713
+ value: function syncLocalMapToSharedMap(localMap, sharedMap) {
714
+ var _this = this;
715
+ Object.entries(localMap).forEach(function(param) {
716
+ var _param = _sliced_to_array(param, 2), key = _param[0], value = _param[1];
717
+ sharedMap.set(key, _this.createSharedModelByLocalModel(value));
718
+ });
719
+ this.syncObject(sharedMap, localMap);
720
+ }
721
+ },
722
+ {
723
+ key: "createSharedMapByLocalMap",
724
+ value: function createSharedMapByLocalMap(localMap) {
725
+ var sharedMap = new yjs.Map();
726
+ this.syncLocalMapToSharedMap(localMap, sharedMap);
727
+ return sharedMap;
728
+ }
729
+ },
730
+ {
731
+ key: "createSharedArrayByLocalArray",
732
+ value: function createSharedArrayByLocalArray(localArray) {
733
+ var _this = this;
734
+ var sharedArray = new yjs.Array();
735
+ localArray.forEach(function(value) {
736
+ sharedArray.push([
737
+ _this.createSharedModelByLocalModel(value)
738
+ ]);
739
+ });
740
+ this.syncArray(sharedArray, localArray);
741
+ return sharedArray;
742
+ }
743
+ },
744
+ {
745
+ key: "createSharedSlotByLocalSlot",
746
+ value: function createSharedSlotByLocalSlot(localSlot) {
747
+ var _this = this;
748
+ var sharedSlot = new yjs.Text();
749
+ var isAsyncSlot = _instanceof$1(localSlot, core.AsyncSlot);
750
+ sharedSlot.setAttribute('schema', _to_consumable_array(localSlot.schema));
751
+ sharedSlot.setAttribute('type', isAsyncSlot ? 'async' : 'sync');
752
+ if (isAsyncSlot) {
753
+ var isDestroyed = false;
754
+ var sharedMetadata = this.createSharedMapByLocalMap(localSlot.metadata);
755
+ sharedSlot.setAttribute('metadata', sharedMetadata);
756
+ this.subModelLoader.createSubModelBySlot(localSlot).then(function(subDocument) {
757
+ if (isDestroyed) {
758
+ return;
759
+ }
760
+ var content = subDocument.getText('content');
761
+ var state = subDocument.getMap('state');
762
+ _this.syncLocalMapToSharedMap(localSlot.state, state);
763
+ _this.initSharedSlotByLocalSlot(content, localSlot);
764
+ _this.syncSlot(content, localSlot);
765
+ _this.addSubModelEvent.next({
766
+ yDoc: subDocument,
767
+ yType: content
768
+ });
769
+ _this.initSyncEvent(subDocument);
770
+ localSlot.loader.markAsLoaded();
771
+ });
772
+ localSlot.__changeMarker__.addDetachCallback(function() {
773
+ isDestroyed = true;
774
+ });
775
+ return sharedSlot;
776
+ }
777
+ var sharedSlotState = new yjs.Map();
778
+ this.syncLocalMapToSharedMap(localSlot.state, sharedSlotState);
779
+ sharedSlot.setAttribute('state', sharedSlotState);
780
+ var sharedContent = new yjs.Text();
781
+ this.initSharedSlotByLocalSlot(sharedContent, localSlot);
782
+ sharedSlot.insertEmbed(0, sharedContent);
783
+ this.syncSlot(sharedContent, localSlot);
784
+ return sharedSlot;
785
+ }
786
+ },
787
+ {
788
+ key: "initSharedSlotByLocalSlot",
789
+ value: function initSharedSlotByLocalSlot(sharedContent, localSlot) {
790
+ var _this = this;
791
+ var offset = 0;
792
+ localSlot.toDelta().forEach(function(i) {
793
+ var formats = {};
794
+ if (i.formats) {
795
+ i.formats.forEach(function(item) {
796
+ formats[item[0].name] = item[1];
797
+ });
798
+ } else {
799
+ formats = null;
800
+ }
801
+ if (typeof i.insert === 'string') {
802
+ sharedContent.insert(offset, i.insert, formats);
803
+ } else {
804
+ var sharedComponent = _this.createSharedComponentByLocalComponent(i.insert);
805
+ sharedContent.insertEmbed(offset, sharedComponent, formats);
806
+ }
807
+ offset += i.insert.length;
808
+ });
809
+ localSlot.getAttributes().forEach(function(item) {
810
+ sharedContent.setAttribute(item[0].name, item[1]);
811
+ });
812
+ }
813
+ },
814
+ {
815
+ key: "createLocalSlotBySharedSlot",
816
+ value: function createLocalSlotBySharedSlot(sharedSlot) {
817
+ var _this = this;
818
+ var _contentDelta_;
819
+ var type = sharedSlot.getAttribute('type');
820
+ var schema = sharedSlot.getAttribute('schema');
821
+ if (type === 'async') {
822
+ var metadata = sharedSlot.getAttribute('metadata');
823
+ var slot = new core.AsyncSlot(schema || [], {}, {});
824
+ this.syncSharedMapToLocalMap(metadata, slot.metadata);
825
+ var loadedSubDocument = this.subModelLoader.getLoadedModelBySlot(slot);
826
+ if (loadedSubDocument) {
827
+ var subContent = loadedSubDocument.getText('content');
828
+ var data = loadedSubDocument.getMap('state');
829
+ this.syncSharedMapToLocalMap(data, slot.state);
830
+ this.syncRootSlot(loadedSubDocument, subContent, slot);
831
+ this.addSubModelEvent.next({
832
+ yDoc: loadedSubDocument,
833
+ yType: subContent
834
+ });
835
+ slot.loader.markAsLoaded();
836
+ return slot;
837
+ }
838
+ var isDestroyed = false;
839
+ slot.loader.onRequestLoad.toPromise().then(function() {
840
+ return _this.subModelLoader.loadSubModelBySlot(slot);
841
+ }).then(function(subDocument) {
842
+ if (isDestroyed) {
843
+ return;
844
+ }
845
+ var subContent = subDocument.getText('content');
846
+ var state = subDocument.getMap('state');
847
+ _this.syncSharedMapToLocalMap(state, slot.state);
848
+ _this.syncRootSlot(subDocument, subContent, slot);
849
+ _this.addSubModelEvent.next({
850
+ yDoc: subDocument,
851
+ yType: subContent
852
+ });
853
+ slot.loader.markAsLoaded();
854
+ });
855
+ slot.__changeMarker__.addDetachCallback(function() {
856
+ isDestroyed = true;
857
+ });
858
+ return slot;
859
+ }
860
+ var contentDelta = sharedSlot.toDelta();
861
+ var content = (_contentDelta_ = contentDelta[0]) === null || _contentDelta_ === void 0 ? void 0 : _contentDelta_.insert;
862
+ if (!_instanceof$1(content, yjs.Text)) {
863
+ throw collaborateErrorFn('shared slot content type is not `YText`.');
864
+ }
865
+ var localSlot = new core.Slot(schema || [], {});
866
+ var sharedSlotState = sharedSlot.getAttribute('state');
867
+ this.syncSharedMapToLocalMap(sharedSlotState, localSlot.state);
868
+ this.initLocalSlotBySharedSlot(content, localSlot);
869
+ this.syncSlot(content, localSlot);
870
+ return localSlot;
871
+ }
872
+ },
873
+ {
874
+ key: "initLocalSlotBySharedSlot",
875
+ value: function initLocalSlotBySharedSlot(content, localSlot) {
876
+ var _this = this;
877
+ var delta = content.toDelta();
878
+ var attrs = content.getAttributes();
879
+ Object.keys(attrs).forEach(function(key) {
880
+ var attribute = _this.registry.getAttribute(key);
881
+ if (attribute) {
882
+ localSlot.setAttribute(attribute, attrs[key]);
883
+ }
884
+ });
885
+ var _iteratorNormalCompletion = true, _didIteratorError = false, _iteratorError = undefined;
886
+ try {
887
+ for(var _iterator = delta[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true){
888
+ var action = _step.value;
889
+ if (action.insert) {
890
+ if (typeof action.insert === 'string') {
891
+ var formats = remoteFormatsToLocal(this.registry, action.attributes);
892
+ localSlot.insert(action.insert, formats);
893
+ } else {
894
+ var sharedComponent = action.insert;
895
+ var component = this.createLocalComponentBySharedComponent(sharedComponent);
896
+ localSlot.insert(component, remoteFormatsToLocal(this.registry, action.attributes));
897
+ }
898
+ } else {
899
+ throw collaborateErrorFn('unexpected delta action.');
900
+ }
901
+ }
902
+ } catch (err) {
903
+ _didIteratorError = true;
904
+ _iteratorError = err;
905
+ } finally{
906
+ try {
907
+ if (!_iteratorNormalCompletion && _iterator.return != null) {
908
+ _iterator.return();
909
+ }
910
+ } finally{
911
+ if (_didIteratorError) {
912
+ throw _iteratorError;
913
+ }
914
+ }
915
+ }
916
+ }
917
+ },
918
+ {
919
+ key: "createSharedModelByLocalModel",
920
+ value: function createSharedModelByLocalModel(localModel) {
921
+ if (_instanceof$1(localModel, core.Slot)) {
922
+ return this.createSharedSlotByLocalSlot(localModel);
923
+ }
924
+ if (Array.isArray(localModel)) {
925
+ return this.createSharedArrayByLocalArray(localModel);
926
+ }
927
+ if ((typeof localModel === "undefined" ? "undefined" : _type_of$2(localModel)) === 'object' && localModel !== null) {
928
+ return this.createSharedMapByLocalMap(localModel);
929
+ }
930
+ return localModel;
931
+ }
932
+ },
933
+ {
934
+ key: "createLocalModelBySharedByModel",
935
+ value: function createLocalModelBySharedByModel(sharedModel) {
936
+ if (_instanceof$1(sharedModel, yjs.Map)) {
937
+ return this.createLocalMapBySharedMap(sharedModel);
938
+ }
939
+ if (_instanceof$1(sharedModel, yjs.Array)) {
940
+ return this.createLocalArrayBySharedArray(sharedModel);
941
+ }
942
+ if (_instanceof$1(sharedModel, yjs.Text)) {
943
+ return this.createLocalSlotBySharedSlot(sharedModel);
944
+ }
945
+ return sharedModel;
946
+ }
947
+ },
948
+ {
949
+ key: "createSharedComponentByLocalComponent",
950
+ value: function createSharedComponentByLocalComponent(component) {
951
+ var _this = this;
952
+ var sharedComponent = new yjs.Map();
953
+ sharedComponent.set('name', component.name);
954
+ if (_instanceof$1(component, core.AsyncComponent)) {
955
+ sharedComponent.set('type', 'async');
956
+ var sharedMetadata = this.createSharedMapByLocalMap(component.metadata);
957
+ sharedComponent.set('metadata', sharedMetadata);
958
+ var state = component.state;
959
+ var isDestroyed = false;
960
+ state.__changeMarker__.addDetachCallback(function() {
961
+ isDestroyed = true;
962
+ });
963
+ this.subModelLoader.createSubModelByComponent(component).then(function(subDocument) {
964
+ if (isDestroyed) {
965
+ return;
966
+ }
967
+ var state = subDocument.getMap('state');
968
+ _this.syncComponent(subDocument, state, component);
969
+ _this.addSubModelEvent.next({
970
+ yType: state,
971
+ yDoc: subDocument
972
+ });
973
+ _this.initSyncEvent(subDocument);
974
+ component.loader.markAsLoaded();
975
+ });
976
+ return sharedComponent;
977
+ }
978
+ var sharedState = this.createSharedMapByLocalMap(component.state);
979
+ sharedComponent.set('state', sharedState);
980
+ sharedComponent.set('type', 'sync');
981
+ return sharedComponent;
982
+ }
983
+ },
984
+ {
985
+ key: "createLocalComponentBySharedComponent",
986
+ value: function createLocalComponentBySharedComponent(yMap) {
987
+ var _this = this;
988
+ var componentName = yMap.get('name');
989
+ var type = yMap.get('type');
990
+ var instance;
991
+ if (type === 'async') {
992
+ instance = this.registry.createComponentByData(componentName, {}, {});
993
+ if (_instanceof$1(instance, core.AsyncComponent)) {
994
+ var sharedMetadata = yMap.get('metadata');
995
+ this.syncSharedMapToLocalMap(sharedMetadata, instance.metadata);
996
+ var loadedSubDocument = this.subModelLoader.getLoadedModelByComponent(instance);
997
+ if (loadedSubDocument) {
998
+ var state = loadedSubDocument.getMap('state');
999
+ this.syncComponent(loadedSubDocument, state, instance);
1000
+ this.addSubModelEvent.next({
1001
+ yType: state,
1002
+ yDoc: loadedSubDocument
1003
+ });
1004
+ instance.loader.markAsLoaded();
1005
+ return instance;
1006
+ }
1007
+ var state1 = instance.state;
1008
+ var isDestroyed = false;
1009
+ instance.loader.onRequestLoad.toPromise().then(function() {
1010
+ return _this.subModelLoader.loadSubModelByComponent(instance);
1011
+ }).then(function(subDocument) {
1012
+ if (isDestroyed) {
1013
+ return;
1014
+ }
1015
+ var state = subDocument.getMap('state');
1016
+ _this.syncComponent(subDocument, state, instance);
1017
+ _this.addSubModelEvent.next({
1018
+ yType: state,
1019
+ yDoc: subDocument
1020
+ });
1021
+ instance.loader.markAsLoaded();
1022
+ });
1023
+ state1.__changeMarker__.addDetachCallback(function() {
1024
+ isDestroyed = true;
1025
+ });
1026
+ } else if (_instanceof$1(instance, core.Component)) {
1027
+ throw collaborateErrorFn("component name `".concat(componentName, "` is not a async component."));
1028
+ }
1029
+ } else {
1030
+ var sharedState = yMap.get('state');
1031
+ var state2 = this.createLocalMapBySharedMap(sharedState);
1032
+ instance = this.registry.createComponentByData(componentName, state2);
1033
+ }
1034
+ if (instance) {
1035
+ return instance;
1036
+ }
1037
+ throw collaborateErrorFn("cannot find component factory `".concat(componentName, "`."));
1038
+ }
1039
+ },
1040
+ {
1041
+ key: "syncArray",
1042
+ value: /**
1043
+ * 双向同步数组
1044
+ * @param sharedArray
1045
+ * @param localArray
1046
+ * @private
1047
+ */ function syncArray(sharedArray, localArray) {
1048
+ var _this = this;
1049
+ function logError(type) {
1050
+ console.error(collaborateErrorFn("".concat(type, " error, length exceeded, path in ").concat(localArray.__changeMarker__.getPaths().join('/'))));
1051
+ }
1052
+ var sub = localArray.__changeMarker__.onSelfChange.subscribe(function(actions) {
1053
+ _this.runLocalUpdate(sharedArray.doc, !localArray.__changeMarker__.irrevocableUpdate, function() {
1054
+ var index = 0;
1055
+ var _iteratorNormalCompletion = true, _didIteratorError = false, _iteratorError = undefined;
1056
+ try {
1057
+ for(var _iterator = actions[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true){
1058
+ var action = _step.value;
1059
+ switch(action.type){
1060
+ case 'retain':
1061
+ index = action.offset;
1062
+ break;
1063
+ case 'insert':
1064
+ {
1065
+ var ref = action.ref;
1066
+ if (!Array.isArray(ref)) {
1067
+ throw collaborateErrorFn('The insertion action must have a reference value.');
1068
+ }
1069
+ var data = ref.map(function(item) {
1070
+ return _this.createSharedModelByLocalModel(item);
1071
+ });
1072
+ if (index <= sharedArray.length) {
1073
+ sharedArray.insert(index, data);
1074
+ } else {
1075
+ sharedArray.insert(sharedArray.length, data);
1076
+ logError('insert');
1077
+ }
1078
+ }
1079
+ break;
1080
+ case 'delete':
1081
+ if (action.count <= 0) {
1082
+ break;
1083
+ }
1084
+ if (index < sharedArray.length) {
1085
+ sharedArray.delete(index, action.count);
1086
+ } else {
1087
+ logError('delete');
1088
+ }
1089
+ break;
1090
+ case 'setIndex':
1091
+ if (action.index < sharedArray.length) {
1092
+ sharedArray.delete(action.index, 1);
1093
+ sharedArray.insert(action.index, [
1094
+ _this.createSharedModelByLocalModel(action.ref)
1095
+ ]);
1096
+ } else {
1097
+ sharedArray.insert(sharedArray.length, [
1098
+ _this.createSharedModelByLocalModel(action.ref)
1099
+ ]);
1100
+ logError('setIndex');
1101
+ }
1102
+ break;
1103
+ }
1104
+ }
1105
+ } catch (err) {
1106
+ _didIteratorError = true;
1107
+ _iteratorError = err;
1108
+ } finally{
1109
+ try {
1110
+ if (!_iteratorNormalCompletion && _iterator.return != null) {
1111
+ _iterator.return();
1112
+ }
1113
+ } finally{
1114
+ if (_didIteratorError) {
1115
+ throw _iteratorError;
1116
+ }
1117
+ }
1118
+ }
1119
+ });
1120
+ });
1121
+ var syncRemote = function syncRemote(ev, tr) {
1122
+ _this.runRemoteUpdate(tr, function() {
1123
+ var index = 0;
1124
+ ev.delta.forEach(function(action) {
1125
+ if (Reflect.has(action, 'retain')) {
1126
+ index += action.retain;
1127
+ } else if (action.insert) {
1128
+ var _localArray;
1129
+ var data = action.insert.map(function(item) {
1130
+ return _this.createLocalModelBySharedByModel(item);
1131
+ });
1132
+ (_localArray = localArray).splice.apply(_localArray, [
1133
+ index,
1134
+ 0
1135
+ ].concat(_to_consumable_array(data)));
1136
+ index += data.length;
1137
+ } else if (action.delete) {
1138
+ localArray.splice(index, action.delete);
1139
+ }
1140
+ });
1141
+ });
1142
+ };
1143
+ sharedArray.observe(syncRemote);
1144
+ localArray.__changeMarker__.addDetachCallback(function() {
1145
+ sub.unsubscribe();
1146
+ sharedArray.unobserve(syncRemote);
1147
+ });
1148
+ }
1149
+ },
1150
+ {
1151
+ key: "syncObject",
1152
+ value: /**
1153
+ * 双向同步对象
1154
+ * @param sharedObject
1155
+ * @param localObject
1156
+ * @private
1157
+ */ function syncObject(sharedObject, localObject) {
1158
+ var _this = this;
1159
+ var syncRemote = function syncRemote(ev, tr) {
1160
+ _this.runRemoteUpdate(tr, function() {
1161
+ ev.changes.keys.forEach(function(item, key) {
1162
+ if (item.action === 'add' || item.action === 'update') {
1163
+ var value = sharedObject.get(key);
1164
+ localObject[key] = _this.createLocalModelBySharedByModel(value);
1165
+ } else {
1166
+ Reflect.deleteProperty(localObject, key);
1167
+ }
1168
+ });
1169
+ });
1170
+ };
1171
+ sharedObject.observe(syncRemote);
1172
+ var sub = localObject.__changeMarker__.onSelfChange.subscribe(function(actions) {
1173
+ _this.runLocalUpdate(sharedObject.doc, !localObject.__changeMarker__.irrevocableUpdate, function() {
1174
+ var _iteratorNormalCompletion = true, _didIteratorError = false, _iteratorError = undefined;
1175
+ try {
1176
+ for(var _iterator = actions[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true){
1177
+ var action = _step.value;
1178
+ switch(action.type){
1179
+ case 'propSet':
1180
+ {
1181
+ var subModel = _this.createSharedModelByLocalModel(action.ref);
1182
+ sharedObject.set(action.key, subModel);
1183
+ if (sharedObject.size === 0) {
1184
+ // 奇怪的 bug,设置了子模型,但子模型会标记为 deleted,导致设置后无效
1185
+ console.error(collaborateErrorFn("prop set error, key is ".concat(action.key)));
1186
+ }
1187
+ }
1188
+ break;
1189
+ case 'propDelete':
1190
+ sharedObject.delete(action.key);
1191
+ break;
1192
+ }
1193
+ }
1194
+ } catch (err) {
1195
+ _didIteratorError = true;
1196
+ _iteratorError = err;
1197
+ } finally{
1198
+ try {
1199
+ if (!_iteratorNormalCompletion && _iterator.return != null) {
1200
+ _iterator.return();
1201
+ }
1202
+ } finally{
1203
+ if (_didIteratorError) {
1204
+ throw _iteratorError;
1205
+ }
1206
+ }
1207
+ }
1208
+ });
1209
+ });
1210
+ localObject.__changeMarker__.addDetachCallback(function() {
1211
+ sharedObject.unobserve(syncRemote);
1212
+ sub.unsubscribe();
1213
+ });
1214
+ }
1215
+ },
1216
+ {
1217
+ key: "runLocalUpdate",
1218
+ value: function runLocalUpdate(yDoc, record, fn) {
1219
+ if (this.updateFromRemote || !yDoc) {
1220
+ return;
1221
+ }
1222
+ var changeList = this.updateRemoteActions.get(yDoc);
1223
+ if (!changeList) {
1224
+ changeList = [];
1225
+ this.updateRemoteActions.set(yDoc, changeList);
1226
+ }
1227
+ changeList.push({
1228
+ record: record,
1229
+ action: fn
1230
+ });
1231
+ }
1232
+ },
1233
+ {
1234
+ key: "runRemoteUpdate",
1235
+ value: function runRemoteUpdate(tr, fn) {
1236
+ if (tr.origin === tr.doc) {
1237
+ return;
1238
+ }
1239
+ this.updateFromRemote = true;
1240
+ if (_instanceof$1(tr.origin, yjs.UndoManager)) {
1241
+ this.scheduler.historyApplyTransact(fn);
1242
+ } else {
1243
+ this.scheduler.remoteUpdateTransact(fn);
1244
+ }
1245
+ this.updateFromRemote = false;
1246
+ }
608
1247
  }
609
- const subContent = subDocument.getText("content");
610
- const state = subDocument.getMap("state");
611
- this.syncSharedMapToLocalMap(state, slot.state);
612
- this.syncRootSlot(subDocument, subContent, slot);
613
- this.addSubModelEvent.next({
614
- yDoc: subDocument,
615
- yType: subContent
1248
+ ]);
1249
+ return Collaborate;
1250
+ }();
1251
+ exports.Collaborate = _ts_decorate$2([
1252
+ core$1.Injectable(),
1253
+ _ts_metadata$2("design:type", Function),
1254
+ _ts_metadata$2("design:paramtypes", [
1255
+ typeof core.Scheduler === "undefined" ? Object : core.Scheduler,
1256
+ typeof core.Registry === "undefined" ? Object : core.Registry,
1257
+ typeof core.Selection === "undefined" ? Object : core.Selection,
1258
+ typeof SubModelLoader === "undefined" ? Object : SubModelLoader
1259
+ ])
1260
+ ], exports.Collaborate);
1261
+ function remoteFormatsToLocal(registry, attrs) {
1262
+ var formats = [];
1263
+ if (attrs) {
1264
+ Object.keys(attrs).forEach(function(key) {
1265
+ var formatter = registry.getFormatter(key);
1266
+ if (formatter) {
1267
+ formats.push([
1268
+ formatter,
1269
+ attrs[key]
1270
+ ]);
1271
+ }
616
1272
  });
617
- slot.loader.markAsLoaded();
618
- });
619
- slot.__changeMarker__.addDetachCallback(() => {
620
- isDestroyed = true;
621
- });
622
- return slot;
623
1273
  }
624
- const contentDelta = sharedSlot.toDelta();
625
- const content = contentDelta[0]?.insert;
626
- if (!(content instanceof yjs.Text)) {
627
- throw collaborateErrorFn("shared slot content type is not `YText`.");
1274
+ return formats;
1275
+ }
1276
+
1277
+ function _class_call_check$7(instance, Constructor) {
1278
+ if (!(instance instanceof Constructor)) {
1279
+ throw new TypeError("Cannot call a class as a function");
628
1280
  }
629
- const localSlot = new core.Slot(schema || [], {});
630
- const sharedSlotState = sharedSlot.getAttribute("state");
631
- this.syncSharedMapToLocalMap(sharedSlotState, localSlot.state);
632
- this.initLocalSlotBySharedSlot(content, localSlot);
633
- this.syncSlot(content, localSlot);
634
- return localSlot;
635
- }
636
- initLocalSlotBySharedSlot(content, localSlot) {
637
- const delta = content.toDelta();
638
- const attrs = content.getAttributes();
639
- Object.keys(attrs).forEach((key) => {
640
- const attribute = this.registry.getAttribute(key);
641
- if (attribute) {
642
- localSlot.setAttribute(attribute, attrs[key]);
643
- }
644
- });
645
- for (const action of delta) {
646
- if (action.insert) {
647
- if (typeof action.insert === "string") {
648
- const formats = remoteFormatsToLocal(this.registry, action.attributes);
649
- localSlot.insert(action.insert, formats);
650
- } else {
651
- const sharedComponent = action.insert;
652
- const component = this.createLocalComponentBySharedComponent(sharedComponent);
653
- localSlot.insert(component, remoteFormatsToLocal(this.registry, action.attributes));
654
- }
655
- } else {
656
- throw collaborateErrorFn("unexpected delta action.");
657
- }
1281
+ }
1282
+ function _defineProperties$6(target, props) {
1283
+ for(var i = 0; i < props.length; i++){
1284
+ var descriptor = props[i];
1285
+ descriptor.enumerable = descriptor.enumerable || false;
1286
+ descriptor.configurable = true;
1287
+ if ("value" in descriptor) descriptor.writable = true;
1288
+ Object.defineProperty(target, descriptor.key, descriptor);
658
1289
  }
659
- }
660
- createSharedModelByLocalModel(localModel) {
661
- if (localModel instanceof core.Slot) {
662
- return this.createSharedSlotByLocalSlot(localModel);
1290
+ }
1291
+ function _create_class$6(Constructor, protoProps, staticProps) {
1292
+ if (protoProps) _defineProperties$6(Constructor.prototype, protoProps);
1293
+ return Constructor;
1294
+ }
1295
+ function _define_property$7(obj, key, value) {
1296
+ if (key in obj) {
1297
+ Object.defineProperty(obj, key, {
1298
+ value: value,
1299
+ enumerable: true,
1300
+ configurable: true,
1301
+ writable: true
1302
+ });
1303
+ } else {
1304
+ obj[key] = value;
663
1305
  }
664
- if (Array.isArray(localModel)) {
665
- return this.createSharedArrayByLocalArray(localModel);
1306
+ return obj;
1307
+ }
1308
+ function _ts_decorate$1(decorators, target, key, desc) {
1309
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
1310
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
1311
+ 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;
1312
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
1313
+ }
1314
+ function _ts_metadata$1(k, v) {
1315
+ if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
1316
+ }
1317
+ function _ts_param$1(paramIndex, decorator) {
1318
+ return function(target, key) {
1319
+ decorator(target, key, paramIndex);
1320
+ };
1321
+ }
1322
+ var CustomUndoManagerConfig = function CustomUndoManagerConfig() {
1323
+ _class_call_check$7(this, CustomUndoManagerConfig);
1324
+ };
1325
+ var collabHistoryErrorFn = core.makeError('CollabHistory');
1326
+ exports.CollabHistory = /*#__PURE__*/ function() {
1327
+ function CollabHistory(rootComponentRef, collaborate, scheduler, stackSize, undoManagerConfig) {
1328
+ _class_call_check$7(this, CollabHistory);
1329
+ _define_property$7(this, "rootComponentRef", void 0);
1330
+ _define_property$7(this, "collaborate", void 0);
1331
+ _define_property$7(this, "scheduler", void 0);
1332
+ _define_property$7(this, "stackSize", void 0);
1333
+ _define_property$7(this, "undoManagerConfig", void 0);
1334
+ _define_property$7(this, "onBack", void 0);
1335
+ _define_property$7(this, "onForward", void 0);
1336
+ _define_property$7(this, "onChange", void 0);
1337
+ _define_property$7(this, "onPush", void 0);
1338
+ _define_property$7(this, "manager", void 0);
1339
+ _define_property$7(this, "historyItems", void 0);
1340
+ _define_property$7(this, "index", void 0);
1341
+ _define_property$7(this, "subscriptions", void 0);
1342
+ _define_property$7(this, "backEvent", void 0);
1343
+ _define_property$7(this, "forwardEvent", void 0);
1344
+ _define_property$7(this, "changeEvent", void 0);
1345
+ _define_property$7(this, "pushEvent", void 0);
1346
+ this.rootComponentRef = rootComponentRef;
1347
+ this.collaborate = collaborate;
1348
+ this.scheduler = scheduler;
1349
+ this.stackSize = stackSize;
1350
+ this.undoManagerConfig = undoManagerConfig;
1351
+ this.manager = null;
1352
+ this.historyItems = [];
1353
+ this.index = 0;
1354
+ this.subscriptions = [];
1355
+ this.backEvent = new stream.Subject();
1356
+ this.forwardEvent = new stream.Subject();
1357
+ this.changeEvent = new stream.Subject();
1358
+ this.pushEvent = new stream.Subject();
1359
+ this.onBack = this.backEvent.asObservable();
1360
+ this.onForward = this.forwardEvent.asObservable();
1361
+ this.onChange = this.changeEvent.asObservable();
1362
+ this.onPush = this.pushEvent.asObservable();
666
1363
  }
667
- if (typeof localModel === "object" && localModel !== null) {
668
- return this.createSharedMapByLocalMap(localModel);
1364
+ _create_class$6(CollabHistory, [
1365
+ {
1366
+ key: "canBack",
1367
+ get: function get() {
1368
+ var _this_manager;
1369
+ return ((_this_manager = this.manager) === null || _this_manager === void 0 ? void 0 : _this_manager.canUndo()) || false;
1370
+ }
1371
+ },
1372
+ {
1373
+ key: "canForward",
1374
+ get: function get() {
1375
+ var _this_manager;
1376
+ return ((_this_manager = this.manager) === null || _this_manager === void 0 ? void 0 : _this_manager.canRedo()) || false;
1377
+ }
1378
+ },
1379
+ {
1380
+ key: "listen",
1381
+ value: function listen() {
1382
+ var _this = this;
1383
+ var root = this.collaborate.yDoc.getMap('RootComponent');
1384
+ var rootComponent = this.rootComponentRef.component;
1385
+ this.collaborate.syncRootComponent(this.collaborate.yDoc, root, rootComponent);
1386
+ var undoManagerConfig = this.undoManagerConfig || {};
1387
+ var manager = new yjs.UndoManager(root, {
1388
+ trackedOrigins: new Set([
1389
+ this.collaborate.yDoc
1390
+ ]),
1391
+ captureTransaction: function captureTransaction(arg) {
1392
+ if (undoManagerConfig.captureTransaction) {
1393
+ return undoManagerConfig.captureTransaction(arg);
1394
+ }
1395
+ return true;
1396
+ },
1397
+ deleteFilter: function deleteFilter(item) {
1398
+ if (undoManagerConfig.deleteFilter) {
1399
+ return undoManagerConfig.deleteFilter(item);
1400
+ }
1401
+ return true;
1402
+ }
1403
+ });
1404
+ this.manager = manager;
1405
+ var beforePosition = null;
1406
+ this.subscriptions.push(this.scheduler.onLocalChangeBefore.subscribe(function() {
1407
+ beforePosition = _this.collaborate.getRelativeCursorLocation();
1408
+ }), this.collaborate.onAddSubModel.subscribe(function() {
1409
+ throw collabHistoryErrorFn('single document does not support submodels.');
1410
+ }));
1411
+ manager.on('stack-item-added', function(event) {
1412
+ if (event.type === 'undo') {
1413
+ if (event.origin === manager) {
1414
+ _this.index++;
1415
+ } else {
1416
+ _this.historyItems.length = _this.index;
1417
+ _this.historyItems.push({
1418
+ before: beforePosition,
1419
+ after: _this.collaborate.getRelativeCursorLocation()
1420
+ });
1421
+ _this.index++;
1422
+ }
1423
+ } else {
1424
+ _this.index--;
1425
+ }
1426
+ if (manager.undoStack.length > _this.stackSize) {
1427
+ _this.historyItems.shift();
1428
+ manager.undoStack.shift();
1429
+ }
1430
+ if (event.origin === _this.collaborate.yDoc) {
1431
+ _this.pushEvent.next();
1432
+ }
1433
+ _this.changeEvent.next();
1434
+ });
1435
+ manager.on('stack-item-popped', function(ev) {
1436
+ var index = ev.type === 'undo' ? _this.index : _this.index - 1;
1437
+ var position = _this.historyItems[index] || null;
1438
+ var p = ev.type === 'undo' ? position === null || position === void 0 ? void 0 : position.before : position === null || position === void 0 ? void 0 : position.after;
1439
+ _this.collaborate.restoreCursorPosition(p);
1440
+ });
1441
+ }
1442
+ },
1443
+ {
1444
+ key: "back",
1445
+ value: function back() {
1446
+ if (this.canBack) {
1447
+ var _this_manager;
1448
+ (_this_manager = this.manager) === null || _this_manager === void 0 ? void 0 : _this_manager.undo();
1449
+ this.backEvent.next();
1450
+ }
1451
+ }
1452
+ },
1453
+ {
1454
+ key: "forward",
1455
+ value: function forward() {
1456
+ if (this.canForward) {
1457
+ var _this_manager;
1458
+ (_this_manager = this.manager) === null || _this_manager === void 0 ? void 0 : _this_manager.redo();
1459
+ this.forwardEvent.next();
1460
+ }
1461
+ }
1462
+ },
1463
+ {
1464
+ key: "clear",
1465
+ value: function clear() {
1466
+ var _this_manager;
1467
+ var last = this.historyItems.pop();
1468
+ this.historyItems = last ? [
1469
+ last
1470
+ ] : [];
1471
+ this.index = last ? 1 : 0;
1472
+ (_this_manager = this.manager) === null || _this_manager === void 0 ? void 0 : _this_manager.clear();
1473
+ this.changeEvent.next();
1474
+ }
1475
+ },
1476
+ {
1477
+ key: "destroy",
1478
+ value: function destroy() {
1479
+ this.index = 0;
1480
+ this.historyItems = [];
1481
+ this.subscriptions.forEach(function(i) {
1482
+ return i.unsubscribe();
1483
+ });
1484
+ if (this.manager) {
1485
+ this.manager.destroy();
1486
+ this.manager.captureTransaction = function() {
1487
+ return true;
1488
+ };
1489
+ this.manager.deleteFilter = function() {
1490
+ return true;
1491
+ };
1492
+ this.manager.trackedOrigins = new Set([
1493
+ null
1494
+ ]);
1495
+ }
1496
+ this.manager = null;
1497
+ }
1498
+ }
1499
+ ]);
1500
+ return CollabHistory;
1501
+ }();
1502
+ exports.CollabHistory = _ts_decorate$1([
1503
+ core$1.Injectable(),
1504
+ _ts_param$1(3, core$1.Inject(core.HISTORY_STACK_SIZE)),
1505
+ _ts_param$1(4, core$1.Optional()),
1506
+ _ts_metadata$1("design:type", Function),
1507
+ _ts_metadata$1("design:paramtypes", [
1508
+ typeof core.RootComponentRef === "undefined" ? Object : core.RootComponentRef,
1509
+ typeof exports.Collaborate === "undefined" ? Object : exports.Collaborate,
1510
+ typeof core.Scheduler === "undefined" ? Object : core.Scheduler,
1511
+ Number,
1512
+ typeof CustomUndoManagerConfig === "undefined" ? Object : CustomUndoManagerConfig
1513
+ ])
1514
+ ], exports.CollabHistory);
1515
+
1516
+ function _class_call_check$6(instance, Constructor) {
1517
+ if (!(instance instanceof Constructor)) {
1518
+ throw new TypeError("Cannot call a class as a function");
669
1519
  }
670
- return localModel;
671
- }
672
- createLocalModelBySharedByModel(sharedModel) {
673
- if (sharedModel instanceof yjs.Map) {
674
- return this.createLocalMapBySharedMap(sharedModel);
1520
+ }
1521
+ function _defineProperties$5(target, props) {
1522
+ for(var i = 0; i < props.length; i++){
1523
+ var descriptor = props[i];
1524
+ descriptor.enumerable = descriptor.enumerable || false;
1525
+ descriptor.configurable = true;
1526
+ if ("value" in descriptor) descriptor.writable = true;
1527
+ Object.defineProperty(target, descriptor.key, descriptor);
675
1528
  }
676
- if (sharedModel instanceof yjs.Array) {
677
- return this.createLocalArrayBySharedArray(sharedModel);
1529
+ }
1530
+ function _create_class$5(Constructor, protoProps, staticProps) {
1531
+ if (protoProps) _defineProperties$5(Constructor.prototype, protoProps);
1532
+ return Constructor;
1533
+ }
1534
+ function _define_property$6(obj, key, value) {
1535
+ if (key in obj) {
1536
+ Object.defineProperty(obj, key, {
1537
+ value: value,
1538
+ enumerable: true,
1539
+ configurable: true,
1540
+ writable: true
1541
+ });
1542
+ } else {
1543
+ obj[key] = value;
678
1544
  }
679
- if (sharedModel instanceof yjs.Text) {
680
- return this.createLocalSlotBySharedSlot(sharedModel);
1545
+ return obj;
1546
+ }
1547
+ /**
1548
+ * 协作消息总线,用于同步各终端消息
1549
+ */ var MessageBus = /*#__PURE__*/ function() {
1550
+ function MessageBus() {
1551
+ _class_call_check$6(this, MessageBus);
1552
+ _define_property$6(this, "onSync", void 0);
1553
+ _define_property$6(this, "syncEvent", new stream.Subject());
1554
+ this.onSync = this.syncEvent.asObservable();
681
1555
  }
682
- return sharedModel;
683
- }
684
- createSharedComponentByLocalComponent(component) {
685
- const sharedComponent = new yjs.Map();
686
- sharedComponent.set("name", component.name);
687
- if (component instanceof core.AsyncComponent) {
688
- sharedComponent.set("type", "async");
689
- const sharedMetadata = this.createSharedMapByLocalMap(component.metadata);
690
- sharedComponent.set("metadata", sharedMetadata);
691
- const state = component.state;
692
- let isDestroyed = false;
693
- state.__changeMarker__.addDetachCallback(() => {
694
- isDestroyed = true;
695
- });
696
- this.subModelLoader.createSubModelByComponent(component).then((subDocument) => {
697
- if (isDestroyed) {
698
- return;
1556
+ _create_class$5(MessageBus, [
1557
+ {
1558
+ /**
1559
+ * 立即同步消息
1560
+ */ key: "sync",
1561
+ value: function sync() {
1562
+ this.syncEvent.next();
1563
+ }
699
1564
  }
700
- const state2 = subDocument.getMap("state");
701
- this.syncComponent(subDocument, state2, component);
702
- this.addSubModelEvent.next({
703
- yType: state2,
704
- yDoc: subDocument
705
- });
706
- this.initSyncEvent(subDocument);
707
- component.loader.markAsLoaded();
708
- });
709
- return sharedComponent;
1565
+ ]);
1566
+ return MessageBus;
1567
+ }();
1568
+
1569
+ function _class_call_check$5(instance, Constructor) {
1570
+ if (!(instance instanceof Constructor)) {
1571
+ throw new TypeError("Cannot call a class as a function");
710
1572
  }
711
- const sharedState = this.createSharedMapByLocalMap(component.state);
712
- sharedComponent.set("state", sharedState);
713
- sharedComponent.set("type", "sync");
714
- return sharedComponent;
715
- }
716
- createLocalComponentBySharedComponent(yMap) {
717
- const componentName = yMap.get("name");
718
- const type = yMap.get("type");
719
- let instance;
720
- if (type === "async") {
721
- instance = this.registry.createComponentByData(componentName, {}, {});
722
- if (instance instanceof core.AsyncComponent) {
723
- const sharedMetadata = yMap.get("metadata");
724
- this.syncSharedMapToLocalMap(sharedMetadata, instance.metadata);
725
- const loadedSubDocument = this.subModelLoader.getLoadedModelByComponent(instance);
726
- if (loadedSubDocument) {
727
- const state2 = loadedSubDocument.getMap("state");
728
- this.syncComponent(loadedSubDocument, state2, instance);
729
- this.addSubModelEvent.next({
730
- yType: state2,
731
- yDoc: loadedSubDocument
732
- });
733
- instance.loader.markAsLoaded();
734
- return instance;
735
- }
736
- const state = instance.state;
737
- let isDestroyed = false;
738
- instance.loader.onRequestLoad.toPromise().then(() => {
739
- return this.subModelLoader.loadSubModelByComponent(instance);
740
- }).then((subDocument) => {
741
- if (isDestroyed) {
742
- return;
743
- }
744
- const state2 = subDocument.getMap("state");
745
- this.syncComponent(subDocument, state2, instance);
746
- this.addSubModelEvent.next({
747
- yType: state2,
748
- yDoc: subDocument
749
- });
750
- instance.loader.markAsLoaded();
751
- });
752
- state.__changeMarker__.addDetachCallback(() => {
753
- isDestroyed = true;
1573
+ }
1574
+ function _defineProperties$4(target, props) {
1575
+ for(var i = 0; i < props.length; i++){
1576
+ var descriptor = props[i];
1577
+ descriptor.enumerable = descriptor.enumerable || false;
1578
+ descriptor.configurable = true;
1579
+ if ("value" in descriptor) descriptor.writable = true;
1580
+ Object.defineProperty(target, descriptor.key, descriptor);
1581
+ }
1582
+ }
1583
+ function _create_class$4(Constructor, protoProps, staticProps) {
1584
+ if (protoProps) _defineProperties$4(Constructor.prototype, protoProps);
1585
+ return Constructor;
1586
+ }
1587
+ function _define_property$5(obj, key, value) {
1588
+ if (key in obj) {
1589
+ Object.defineProperty(obj, key, {
1590
+ value: value,
1591
+ enumerable: true,
1592
+ configurable: true,
1593
+ writable: true
754
1594
  });
755
- } else if (instance instanceof core.Component) {
756
- throw collaborateErrorFn(`component name \`${componentName}\` is not a async component.`);
757
- }
758
1595
  } else {
759
- const sharedState = yMap.get("state");
760
- const state = this.createLocalMapBySharedMap(sharedState);
761
- instance = this.registry.createComponentByData(componentName, state);
1596
+ obj[key] = value;
762
1597
  }
763
- if (instance) {
764
- return instance;
1598
+ return obj;
1599
+ }
1600
+ function _instanceof(left, right) {
1601
+ "@swc/helpers - instanceof";
1602
+ if (right != null && typeof Symbol !== "undefined" && right[Symbol.hasInstance]) {
1603
+ return !!right[Symbol.hasInstance](left);
1604
+ } else {
1605
+ return left instanceof right;
765
1606
  }
766
- throw collaborateErrorFn(`cannot find component factory \`${componentName}\`.`);
767
- }
768
- /**
769
- * 双向同步数组
770
- * @param sharedArray
771
- * @param localArray
772
- * @private
773
- */
774
- syncArray(sharedArray, localArray) {
775
- function logError(type) {
776
- console.error(collaborateErrorFn(`${type} error, length exceeded, path in ${localArray.__changeMarker__.getPaths().join("/")}`));
1607
+ }
1608
+ function _ts_decorate(decorators, target, key, desc) {
1609
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
1610
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
1611
+ 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;
1612
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
1613
+ }
1614
+ function _ts_metadata(k, v) {
1615
+ if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
1616
+ }
1617
+ function _ts_param(paramIndex, decorator) {
1618
+ return function(target, key) {
1619
+ decorator(target, key, paramIndex);
1620
+ };
1621
+ }
1622
+ exports.MultipleDocCollabHistory = /*#__PURE__*/ function() {
1623
+ function MultipleDocCollabHistory(collaborate, scheduler, rootComponentRef, stackSize, undoManagerConfig) {
1624
+ _class_call_check$5(this, MultipleDocCollabHistory);
1625
+ _define_property$5(this, "collaborate", void 0);
1626
+ _define_property$5(this, "scheduler", void 0);
1627
+ _define_property$5(this, "rootComponentRef", void 0);
1628
+ _define_property$5(this, "stackSize", void 0);
1629
+ _define_property$5(this, "undoManagerConfig", void 0);
1630
+ _define_property$5(this, "onChange", void 0);
1631
+ _define_property$5(this, "onBack", void 0);
1632
+ _define_property$5(this, "onForward", void 0);
1633
+ _define_property$5(this, "onPush", void 0);
1634
+ _define_property$5(this, "isListen", void 0);
1635
+ _define_property$5(this, "changeEvent", void 0);
1636
+ _define_property$5(this, "backEvent", void 0);
1637
+ _define_property$5(this, "forwardEvent", void 0);
1638
+ _define_property$5(this, "pushEvent", void 0);
1639
+ _define_property$5(this, "actionStack", void 0);
1640
+ _define_property$5(this, "index", void 0);
1641
+ _define_property$5(this, "stackItem", void 0);
1642
+ _define_property$5(this, "timer", void 0);
1643
+ _define_property$5(this, "beforePosition", void 0);
1644
+ _define_property$5(this, "subscription", void 0);
1645
+ _define_property$5(this, "subDocs", void 0);
1646
+ _define_property$5(this, "listenerCaches", void 0);
1647
+ this.collaborate = collaborate;
1648
+ this.scheduler = scheduler;
1649
+ this.rootComponentRef = rootComponentRef;
1650
+ this.stackSize = stackSize;
1651
+ this.undoManagerConfig = undoManagerConfig;
1652
+ this.isListen = false;
1653
+ this.changeEvent = new stream.Subject();
1654
+ this.backEvent = new stream.Subject();
1655
+ this.forwardEvent = new stream.Subject();
1656
+ this.pushEvent = new stream.Subject();
1657
+ this.actionStack = [];
1658
+ this.index = 0;
1659
+ this.stackItem = null;
1660
+ this.timer = null;
1661
+ this.beforePosition = null;
1662
+ this.subscription = new stream.Subscription();
1663
+ this.subDocs = new Set();
1664
+ this.listenerCaches = new Set();
1665
+ this.onChange = this.changeEvent.asObservable();
1666
+ this.onBack = this.backEvent.asObservable();
1667
+ this.onForward = this.forwardEvent.asObservable();
1668
+ this.onPush = this.pushEvent.asObservable();
777
1669
  }
778
- const sub = localArray.__changeMarker__.onSelfChange.subscribe((actions) => {
779
- this.runLocalUpdate(sharedArray.doc, !localArray.__changeMarker__.irrevocableUpdate, () => {
780
- let index = 0;
781
- for (const action of actions) {
782
- switch (action.type) {
783
- case "retain":
784
- index = action.offset;
785
- break;
786
- case "insert":
787
- {
788
- const ref = action.ref;
789
- if (!Array.isArray(ref)) {
790
- throw collaborateErrorFn("The insertion action must have a reference value.");
1670
+ _create_class$4(MultipleDocCollabHistory, [
1671
+ {
1672
+ key: "canBack",
1673
+ get: function get() {
1674
+ return this.actionStack.length > 0 && this.index > 0;
1675
+ }
1676
+ },
1677
+ {
1678
+ key: "canForward",
1679
+ get: function get() {
1680
+ return this.actionStack.length > 0 && this.index < this.actionStack.length;
1681
+ }
1682
+ },
1683
+ {
1684
+ key: "listen",
1685
+ value: function listen() {
1686
+ var _this = this;
1687
+ this.isListen = true;
1688
+ var root = this.collaborate.yDoc.getMap('RootComponent');
1689
+ var rootComponent = this.rootComponentRef.component;
1690
+ this.collaborate.syncRootComponent(this.collaborate.yDoc, root, rootComponent);
1691
+ this.listenItem(root, this.collaborate.yDoc);
1692
+ this.subscription.add(this.collaborate.onAddSubModel.subscribe(function(param) {
1693
+ var yType = param.yType, yDoc = param.yDoc;
1694
+ if (_this.subDocs.has(yType)) {
1695
+ return;
1696
+ }
1697
+ _this.subDocs.add(yType);
1698
+ if (_this.isListen) {
1699
+ _this.listenItem(yType, yDoc);
1700
+ }
1701
+ }), this.scheduler.onLocalChangeBefore.subscribe(function() {
1702
+ _this.beforePosition = _this.collaborate.getRelativeCursorLocation();
1703
+ }));
1704
+ }
1705
+ },
1706
+ {
1707
+ key: "forward",
1708
+ value: function forward() {
1709
+ if (!this.canForward) {
1710
+ return;
791
1711
  }
792
- const data = ref.map((item) => {
793
- return this.createSharedModelByLocalModel(item);
794
- });
795
- if (index <= sharedArray.length) {
796
- sharedArray.insert(index, data);
1712
+ clearTimeout(this.timer);
1713
+ var item = this.actionStack[this.index];
1714
+ if (item) {
1715
+ var _iteratorNormalCompletion = true, _didIteratorError = false, _iteratorError = undefined;
1716
+ try {
1717
+ for(var _iterator = item.undoManagers[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true){
1718
+ var i = _step.value;
1719
+ i.redo();
1720
+ }
1721
+ } catch (err) {
1722
+ _didIteratorError = true;
1723
+ _iteratorError = err;
1724
+ } finally{
1725
+ try {
1726
+ if (!_iteratorNormalCompletion && _iterator.return != null) {
1727
+ _iterator.return();
1728
+ }
1729
+ } finally{
1730
+ if (_didIteratorError) {
1731
+ throw _iteratorError;
1732
+ }
1733
+ }
1734
+ }
1735
+ this.collaborate.restoreCursorPosition(item.after);
1736
+ }
1737
+ this.index++;
1738
+ this.forwardEvent.next();
1739
+ this.changeEvent.next();
1740
+ }
1741
+ },
1742
+ {
1743
+ key: "back",
1744
+ value: function back() {
1745
+ if (!this.canBack) {
1746
+ return;
1747
+ }
1748
+ clearTimeout(this.timer);
1749
+ var historyStackItem;
1750
+ if (this.stackItem) {
1751
+ historyStackItem = this.stackItem;
1752
+ this.stackItem = null;
797
1753
  } else {
798
- sharedArray.insert(sharedArray.length, data);
799
- logError("insert");
1754
+ this.index--;
1755
+ historyStackItem = this.actionStack[this.index];
1756
+ }
1757
+ var len = historyStackItem.undoManagers.length;
1758
+ while(len > 0){
1759
+ len--;
1760
+ historyStackItem.undoManagers[len].undo();
1761
+ }
1762
+ if (historyStackItem) {
1763
+ var beforePosition = historyStackItem.before;
1764
+ this.collaborate.restoreCursorPosition(beforePosition);
1765
+ this.backEvent.next();
1766
+ this.changeEvent.next();
800
1767
  }
801
- }
802
- break;
803
- case "delete":
804
- if (action.count <= 0) {
805
- break;
806
- }
807
- if (index < sharedArray.length) {
808
- sharedArray.delete(index, action.count);
809
- } else {
810
- logError("delete");
811
- }
812
- break;
813
- case "setIndex":
814
- if (action.index < sharedArray.length) {
815
- sharedArray.delete(action.index, 1);
816
- sharedArray.insert(action.index, [this.createSharedModelByLocalModel(action.ref)]);
817
- } else {
818
- sharedArray.insert(sharedArray.length, [this.createSharedModelByLocalModel(action.ref)]);
819
- logError("setIndex");
820
- }
821
- break;
822
- }
1768
+ }
1769
+ },
1770
+ {
1771
+ key: "clear",
1772
+ value: function clear() {
1773
+ this.actionStack = [];
1774
+ this.stackItem = null;
1775
+ this.index = 0;
1776
+ this.beforePosition = null;
1777
+ clearTimeout(this.timer);
1778
+ this.listenerCaches.forEach(function(undoManager) {
1779
+ undoManager.clear();
1780
+ });
1781
+ this.changeEvent.next();
1782
+ }
1783
+ },
1784
+ {
1785
+ key: "destroy",
1786
+ value: function destroy() {
1787
+ this.clear();
1788
+ this.beforePosition = this.stackItem = null;
1789
+ this.subscription.unsubscribe();
1790
+ this.listenerCaches.forEach(function(undoManager) {
1791
+ undoManager.destroy();
1792
+ });
1793
+ this.subDocs.clear();
1794
+ this.listenerCaches.clear();
1795
+ }
1796
+ },
1797
+ {
1798
+ key: "listenItem",
1799
+ value: function listenItem(yType, yDoc) {
1800
+ var _this = this;
1801
+ var undoManagerConfig = this.undoManagerConfig || {};
1802
+ var undoManager = new yjs.UndoManager(yType, {
1803
+ trackedOrigins: new Set([
1804
+ yDoc
1805
+ ]),
1806
+ captureTimeout: 0,
1807
+ captureTransaction: function captureTransaction(arg) {
1808
+ if (undoManagerConfig.captureTransaction) {
1809
+ return undoManagerConfig.captureTransaction(arg);
1810
+ }
1811
+ return true;
1812
+ },
1813
+ deleteFilter: function deleteFilter(item) {
1814
+ if (undoManagerConfig.deleteFilter) {
1815
+ return undoManagerConfig.deleteFilter(item);
1816
+ }
1817
+ return true;
1818
+ }
1819
+ });
1820
+ undoManager.on('stack-item-added', function(event) {
1821
+ if (event.type === 'undo' && !_instanceof(event.origin, yjs.UndoManager)) {
1822
+ if (_this.index != _this.actionStack.length) {
1823
+ var redoStack = _this.actionStack.slice(_this.index);
1824
+ redoStack.forEach(function(item) {
1825
+ item.undoManagers.forEach(function(i) {
1826
+ i.clear(false, true);
1827
+ });
1828
+ });
1829
+ _this.actionStack.length = _this.index;
1830
+ _this.changeEvent.next();
1831
+ }
1832
+ if (_this.stackItem === null) {
1833
+ _this.stackItem = {
1834
+ before: _this.beforePosition,
1835
+ after: null,
1836
+ undoManagers: []
1837
+ };
1838
+ _this.timer = setTimeout(function() {
1839
+ if (_this.actionStack.length >= _this.stackSize) {
1840
+ _this.actionStack.shift();
1841
+ } else {
1842
+ _this.index++;
1843
+ }
1844
+ // this.beforePosition = this.collaborate.getRelativeCursorLocation()
1845
+ _this.stackItem.after = _this.beforePosition;
1846
+ _this.actionStack.push(_this.stackItem);
1847
+ _this.stackItem = null;
1848
+ _this.pushEvent.next();
1849
+ _this.changeEvent.next();
1850
+ }, 500);
1851
+ }
1852
+ _this.stackItem.undoManagers.push(undoManager);
1853
+ }
1854
+ });
1855
+ this.listenerCaches.add(undoManager);
1856
+ }
823
1857
  }
824
- });
825
- });
826
- const syncRemote = (ev, tr) => {
827
- this.runRemoteUpdate(tr, () => {
828
- let index = 0;
829
- ev.delta.forEach((action) => {
830
- if (Reflect.has(action, "retain")) {
831
- index += action.retain;
832
- } else if (action.insert) {
833
- const data = action.insert.map((item) => {
834
- return this.createLocalModelBySharedByModel(item);
835
- });
836
- localArray.splice(index, 0, ...data);
837
- index += data.length;
838
- } else if (action.delete) {
839
- localArray.splice(index, action.delete);
840
- }
1858
+ ]);
1859
+ return MultipleDocCollabHistory;
1860
+ }();
1861
+ exports.MultipleDocCollabHistory = _ts_decorate([
1862
+ core$1.Injectable(),
1863
+ _ts_param(3, core$1.Inject(core.HISTORY_STACK_SIZE)),
1864
+ _ts_param(4, core$1.Optional()),
1865
+ _ts_metadata("design:type", Function),
1866
+ _ts_metadata("design:paramtypes", [
1867
+ typeof exports.Collaborate === "undefined" ? Object : exports.Collaborate,
1868
+ typeof core.Scheduler === "undefined" ? Object : core.Scheduler,
1869
+ typeof core.RootComponentRef === "undefined" ? Object : core.RootComponentRef,
1870
+ Number,
1871
+ typeof CustomUndoManagerConfig === "undefined" ? Object : CustomUndoManagerConfig
1872
+ ])
1873
+ ], exports.MultipleDocCollabHistory);
1874
+
1875
+ function _class_call_check$4(instance, Constructor) {
1876
+ if (!(instance instanceof Constructor)) {
1877
+ throw new TypeError("Cannot call a class as a function");
1878
+ }
1879
+ }
1880
+ function _define_property$4(obj, key, value) {
1881
+ if (key in obj) {
1882
+ Object.defineProperty(obj, key, {
1883
+ value: value,
1884
+ enumerable: true,
1885
+ configurable: true,
1886
+ writable: true
841
1887
  });
842
- });
843
- };
844
- sharedArray.observe(syncRemote);
845
- localArray.__changeMarker__.addDetachCallback(() => {
846
- sub.unsubscribe();
847
- sharedArray.unobserve(syncRemote);
848
- });
849
- }
850
- /**
851
- * 双向同步对象
852
- * @param sharedObject
853
- * @param localObject
854
- * @private
855
- */
856
- syncObject(sharedObject, localObject) {
857
- const syncRemote = (ev, tr) => {
858
- this.runRemoteUpdate(tr, () => {
859
- ev.changes.keys.forEach((item, key) => {
860
- if (item.action === "add" || item.action === "update") {
861
- const value = sharedObject.get(key);
862
- localObject[key] = this.createLocalModelBySharedByModel(value);
863
- } else {
864
- Reflect.deleteProperty(localObject, key);
865
- }
1888
+ } else {
1889
+ obj[key] = value;
1890
+ }
1891
+ return obj;
1892
+ }
1893
+ /**
1894
+ * 协作通信通用接口
1895
+ */ var SyncConnector = function SyncConnector() {
1896
+ _class_call_check$4(this, SyncConnector);
1897
+ /**
1898
+ * 当文档加载完成时触发的观察者
1899
+ */ _define_property$4(this, "onLoad", void 0);
1900
+ /**
1901
+ * 当文档 awareness 状态变更时触发的观察者
1902
+ */ _define_property$4(this, "onStateChange", void 0);
1903
+ _define_property$4(this, "loadEvent", new stream.Subject());
1904
+ _define_property$4(this, "stateChangeEvent", new stream.Subject());
1905
+ this.onLoad = this.loadEvent.asObservable();
1906
+ this.onStateChange = this.stateChangeEvent.asObservable();
1907
+ };
1908
+
1909
+ function _assert_this_initialized$1(self) {
1910
+ if (self === void 0) {
1911
+ throw new ReferenceError("this hasn't been initialised - super() hasn't been called");
1912
+ }
1913
+ return self;
1914
+ }
1915
+ function _call_super$1(_this, derived, args) {
1916
+ derived = _get_prototype_of$1(derived);
1917
+ return _possible_constructor_return$1(_this, _is_native_reflect_construct$1() ? Reflect.construct(derived, [], _get_prototype_of$1(_this).constructor) : derived.apply(_this, args));
1918
+ }
1919
+ function _class_call_check$3(instance, Constructor) {
1920
+ if (!(instance instanceof Constructor)) {
1921
+ throw new TypeError("Cannot call a class as a function");
1922
+ }
1923
+ }
1924
+ function _defineProperties$3(target, props) {
1925
+ for(var i = 0; i < props.length; i++){
1926
+ var descriptor = props[i];
1927
+ descriptor.enumerable = descriptor.enumerable || false;
1928
+ descriptor.configurable = true;
1929
+ if ("value" in descriptor) descriptor.writable = true;
1930
+ Object.defineProperty(target, descriptor.key, descriptor);
1931
+ }
1932
+ }
1933
+ function _create_class$3(Constructor, protoProps, staticProps) {
1934
+ if (protoProps) _defineProperties$3(Constructor.prototype, protoProps);
1935
+ return Constructor;
1936
+ }
1937
+ function _define_property$3(obj, key, value) {
1938
+ if (key in obj) {
1939
+ Object.defineProperty(obj, key, {
1940
+ value: value,
1941
+ enumerable: true,
1942
+ configurable: true,
1943
+ writable: true
866
1944
  });
867
- });
1945
+ } else {
1946
+ obj[key] = value;
1947
+ }
1948
+ return obj;
1949
+ }
1950
+ function _get_prototype_of$1(o) {
1951
+ _get_prototype_of$1 = Object.setPrototypeOf ? Object.getPrototypeOf : function getPrototypeOf(o) {
1952
+ return o.__proto__ || Object.getPrototypeOf(o);
868
1953
  };
869
- sharedObject.observe(syncRemote);
870
- const sub = localObject.__changeMarker__.onSelfChange.subscribe((actions) => {
871
- this.runLocalUpdate(sharedObject.doc, !localObject.__changeMarker__.irrevocableUpdate, () => {
872
- for (const action of actions) {
873
- switch (action.type) {
874
- case "propSet":
875
- {
876
- const subModel = this.createSharedModelByLocalModel(action.ref);
877
- sharedObject.set(action.key, subModel);
878
- if (sharedObject.size === 0) {
879
- console.error(collaborateErrorFn(`prop set error, key is ${action.key}`));
880
- }
881
- }
882
- break;
883
- case "propDelete":
884
- sharedObject.delete(action.key);
885
- break;
886
- }
1954
+ return _get_prototype_of$1(o);
1955
+ }
1956
+ function _inherits$1(subClass, superClass) {
1957
+ if (typeof superClass !== "function" && superClass !== null) {
1958
+ throw new TypeError("Super expression must either be null or a function");
1959
+ }
1960
+ subClass.prototype = Object.create(superClass && superClass.prototype, {
1961
+ constructor: {
1962
+ value: subClass,
1963
+ writable: true,
1964
+ configurable: true
887
1965
  }
888
- });
889
1966
  });
890
- localObject.__changeMarker__.addDetachCallback(function() {
891
- sharedObject.unobserve(syncRemote);
892
- sub.unsubscribe();
893
- });
894
- }
895
- runLocalUpdate(yDoc, record, fn) {
896
- if (this.updateFromRemote || !yDoc) {
897
- return;
898
- }
899
- let changeList = this.updateRemoteActions.get(yDoc);
900
- if (!changeList) {
901
- changeList = [];
902
- this.updateRemoteActions.set(yDoc, changeList);
1967
+ if (superClass) _set_prototype_of$1(subClass, superClass);
1968
+ }
1969
+ function _object_spread(target) {
1970
+ for(var i = 1; i < arguments.length; i++){
1971
+ var source = arguments[i] != null ? arguments[i] : {};
1972
+ var ownKeys = Object.keys(source);
1973
+ if (typeof Object.getOwnPropertySymbols === "function") {
1974
+ ownKeys = ownKeys.concat(Object.getOwnPropertySymbols(source).filter(function(sym) {
1975
+ return Object.getOwnPropertyDescriptor(source, sym).enumerable;
1976
+ }));
1977
+ }
1978
+ ownKeys.forEach(function(key) {
1979
+ _define_property$3(target, key, source[key]);
1980
+ });
903
1981
  }
904
- changeList.push({
905
- record,
906
- action: fn
907
- });
908
- }
909
- runRemoteUpdate(tr, fn) {
910
- if (tr.origin === tr.doc) {
911
- return;
1982
+ return target;
1983
+ }
1984
+ function ownKeys(object, enumerableOnly) {
1985
+ var keys = Object.keys(object);
1986
+ if (Object.getOwnPropertySymbols) {
1987
+ var symbols = Object.getOwnPropertySymbols(object);
1988
+ keys.push.apply(keys, symbols);
912
1989
  }
913
- this.updateFromRemote = true;
914
- if (tr.origin instanceof yjs.UndoManager) {
915
- this.scheduler.historyApplyTransact(fn);
1990
+ return keys;
1991
+ }
1992
+ function _object_spread_props(target, source) {
1993
+ source = source != null ? source : {};
1994
+ if (Object.getOwnPropertyDescriptors) {
1995
+ Object.defineProperties(target, Object.getOwnPropertyDescriptors(source));
916
1996
  } else {
917
- this.scheduler.remoteUpdateTransact(fn);
1997
+ ownKeys(Object(source)).forEach(function(key) {
1998
+ Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key));
1999
+ });
918
2000
  }
919
- this.updateFromRemote = false;
920
- }
921
- };
922
- exports.Collaborate = __decorateClass$2([
923
- core$1.Injectable()
924
- ], exports.Collaborate);
925
- function remoteFormatsToLocal(registry, attrs) {
926
- const formats = [];
927
- if (attrs) {
928
- Object.keys(attrs).forEach((key) => {
929
- const formatter = registry.getFormatter(key);
930
- if (formatter) {
931
- formats.push([formatter, attrs[key]]);
932
- }
933
- });
934
- }
935
- return formats;
936
- }
937
- class MessageBus {
938
- onSync;
939
- syncEvent = new stream.Subject();
940
- constructor() {
941
- this.onSync = this.syncEvent.asObservable();
942
- }
943
- /**
944
- * 立即同步消息
945
- */
946
- sync() {
947
- this.syncEvent.next();
948
- }
949
- }
950
- var __getOwnPropDesc$1 = Object.getOwnPropertyDescriptor;
951
- var __decorateClass$1 = (decorators, target, key, kind) => {
952
- var result = kind > 1 ? void 0 : kind ? __getOwnPropDesc$1(target, key) : target;
953
- for (var i = decorators.length - 1, decorator; i >= 0; i--)
954
- if (decorator = decorators[i])
955
- result = decorator(result) || result;
956
- return result;
957
- };
958
- var __decorateParam = (index, decorator) => (target, key) => decorator(target, key, index);
959
- exports.MultipleDocCollabHistory = class MultipleDocCollabHistory {
960
- constructor(collaborate, scheduler, rootComponentRef, stackSize, undoManagerConfig) {
961
- this.collaborate = collaborate;
962
- this.scheduler = scheduler;
963
- this.rootComponentRef = rootComponentRef;
964
- this.stackSize = stackSize;
965
- this.undoManagerConfig = undoManagerConfig;
966
- this.onChange = this.changeEvent.asObservable();
967
- this.onBack = this.backEvent.asObservable();
968
- this.onForward = this.forwardEvent.asObservable();
969
- this.onPush = this.pushEvent.asObservable();
970
- }
971
- collaborate;
972
- scheduler;
973
- rootComponentRef;
974
- stackSize;
975
- undoManagerConfig;
976
- onChange;
977
- onBack;
978
- onForward;
979
- onPush;
980
- get canBack() {
981
- return this.actionStack.length > 0 && this.index > 0;
982
- }
983
- get canForward() {
984
- return this.actionStack.length > 0 && this.index < this.actionStack.length;
985
- }
986
- isListen = false;
987
- changeEvent = new stream.Subject();
988
- backEvent = new stream.Subject();
989
- forwardEvent = new stream.Subject();
990
- pushEvent = new stream.Subject();
991
- actionStack = [];
992
- index = 0;
993
- stackItem = null;
994
- timer = null;
995
- beforePosition = null;
996
- subscription = new stream.Subscription();
997
- subDocs = /* @__PURE__ */ new Set();
998
- listenerCaches = /* @__PURE__ */ new Set();
999
- listen() {
1000
- this.isListen = true;
1001
- const root = this.collaborate.yDoc.getMap("RootComponent");
1002
- const rootComponent = this.rootComponentRef.component;
1003
- this.collaborate.syncRootComponent(this.collaborate.yDoc, root, rootComponent);
1004
- this.listenItem(root, this.collaborate.yDoc);
1005
- this.subscription.add(
1006
- this.collaborate.onAddSubModel.subscribe(({ yType, yDoc }) => {
1007
- if (this.subDocs.has(yType)) {
1008
- return;
1009
- }
1010
- this.subDocs.add(yType);
1011
- if (this.isListen) {
1012
- this.listenItem(yType, yDoc);
2001
+ return target;
2002
+ }
2003
+ function _possible_constructor_return$1(self, call) {
2004
+ if (call && (_type_of$1(call) === "object" || typeof call === "function")) {
2005
+ return call;
2006
+ }
2007
+ return _assert_this_initialized$1(self);
2008
+ }
2009
+ function _set_prototype_of$1(o, p) {
2010
+ _set_prototype_of$1 = Object.setPrototypeOf || function setPrototypeOf(o, p) {
2011
+ o.__proto__ = p;
2012
+ return o;
2013
+ };
2014
+ return _set_prototype_of$1(o, p);
2015
+ }
2016
+ function _type_of$1(obj) {
2017
+ "@swc/helpers - typeof";
2018
+ return obj && typeof Symbol !== "undefined" && obj.constructor === Symbol ? "symbol" : typeof obj;
2019
+ }
2020
+ function _is_native_reflect_construct$1() {
2021
+ try {
2022
+ var result = !Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function() {}));
2023
+ } catch (_) {}
2024
+ return (_is_native_reflect_construct$1 = function() {
2025
+ return !!result;
2026
+ })();
2027
+ }
2028
+ var HocuspocusConnector = /*#__PURE__*/ function(SyncConnector) {
2029
+ _inherits$1(HocuspocusConnector, SyncConnector);
2030
+ function HocuspocusConnector(config) {
2031
+ _class_call_check$3(this, HocuspocusConnector);
2032
+ var _this;
2033
+ _this = _call_super$1(this, HocuspocusConnector), _define_property$3(_this, "provide", void 0);
2034
+ _this.provide = new provider.HocuspocusProvider(_object_spread_props(_object_spread({}, config), {
2035
+ onSynced: function onSynced(data) {
2036
+ var _config_onSynced;
2037
+ (_config_onSynced = config.onSynced) === null || _config_onSynced === void 0 ? void 0 : _config_onSynced.call(config, data);
2038
+ _this.loadEvent.next();
2039
+ },
2040
+ onAwarenessUpdate: function onAwarenessUpdate(data) {
2041
+ var _config_onAwarenessUpdate;
2042
+ (_config_onAwarenessUpdate = config.onAwarenessUpdate) === null || _config_onAwarenessUpdate === void 0 ? void 0 : _config_onAwarenessUpdate.call(config, data);
2043
+ var states = data.states.map(function(state) {
2044
+ return {
2045
+ clientId: state.clientId,
2046
+ message: state.message
2047
+ };
2048
+ });
2049
+ _this.stateChangeEvent.next(states);
2050
+ }
2051
+ }));
2052
+ return _this;
2053
+ }
2054
+ _create_class$3(HocuspocusConnector, [
2055
+ {
2056
+ key: "setLocalStateField",
2057
+ value: function setLocalStateField(key, data) {
2058
+ this.provide.setAwarenessField(key, data);
2059
+ }
2060
+ },
2061
+ {
2062
+ key: "onDestroy",
2063
+ value: function onDestroy() {
2064
+ this.provide.disconnect();
2065
+ this.provide.destroy();
2066
+ }
1013
2067
  }
1014
- }),
1015
- this.scheduler.onLocalChangeBefore.subscribe(() => {
1016
- this.beforePosition = this.collaborate.getRelativeCursorLocation();
1017
- })
1018
- );
1019
- }
1020
- forward() {
1021
- if (!this.canForward) {
1022
- return;
2068
+ ]);
2069
+ return HocuspocusConnector;
2070
+ }(SyncConnector);
2071
+
2072
+ function _assert_this_initialized(self) {
2073
+ if (self === void 0) {
2074
+ throw new ReferenceError("this hasn't been initialised - super() hasn't been called");
1023
2075
  }
1024
- clearTimeout(this.timer);
1025
- const item = this.actionStack[this.index];
1026
- if (item) {
1027
- for (const i of item.undoManagers) {
1028
- i.redo();
1029
- }
1030
- this.collaborate.restoreCursorPosition(item.after);
2076
+ return self;
2077
+ }
2078
+ function _call_super(_this, derived, args) {
2079
+ derived = _get_prototype_of(derived);
2080
+ return _possible_constructor_return(_this, _is_native_reflect_construct() ? Reflect.construct(derived, [], _get_prototype_of(_this).constructor) : derived.apply(_this, args));
2081
+ }
2082
+ function _class_call_check$2(instance, Constructor) {
2083
+ if (!(instance instanceof Constructor)) {
2084
+ throw new TypeError("Cannot call a class as a function");
1031
2085
  }
1032
- this.index++;
1033
- this.forwardEvent.next();
1034
- this.changeEvent.next();
1035
- }
1036
- back() {
1037
- if (!this.canBack) {
1038
- return;
2086
+ }
2087
+ function _defineProperties$2(target, props) {
2088
+ for(var i = 0; i < props.length; i++){
2089
+ var descriptor = props[i];
2090
+ descriptor.enumerable = descriptor.enumerable || false;
2091
+ descriptor.configurable = true;
2092
+ if ("value" in descriptor) descriptor.writable = true;
2093
+ Object.defineProperty(target, descriptor.key, descriptor);
1039
2094
  }
1040
- clearTimeout(this.timer);
1041
- let historyStackItem;
1042
- if (this.stackItem) {
1043
- historyStackItem = this.stackItem;
1044
- this.stackItem = null;
2095
+ }
2096
+ function _create_class$2(Constructor, protoProps, staticProps) {
2097
+ if (protoProps) _defineProperties$2(Constructor.prototype, protoProps);
2098
+ return Constructor;
2099
+ }
2100
+ function _define_property$2(obj, key, value) {
2101
+ if (key in obj) {
2102
+ Object.defineProperty(obj, key, {
2103
+ value: value,
2104
+ enumerable: true,
2105
+ configurable: true,
2106
+ writable: true
2107
+ });
1045
2108
  } else {
1046
- this.index--;
1047
- historyStackItem = this.actionStack[this.index];
2109
+ obj[key] = value;
1048
2110
  }
1049
- let len = historyStackItem.undoManagers.length;
1050
- while (len > 0) {
1051
- len--;
1052
- historyStackItem.undoManagers[len].undo();
1053
- }
1054
- if (historyStackItem) {
1055
- const beforePosition = historyStackItem.before;
1056
- this.collaborate.restoreCursorPosition(beforePosition);
1057
- this.backEvent.next();
1058
- this.changeEvent.next();
2111
+ return obj;
2112
+ }
2113
+ function _get_prototype_of(o) {
2114
+ _get_prototype_of = Object.setPrototypeOf ? Object.getPrototypeOf : function getPrototypeOf(o) {
2115
+ return o.__proto__ || Object.getPrototypeOf(o);
2116
+ };
2117
+ return _get_prototype_of(o);
2118
+ }
2119
+ function _inherits(subClass, superClass) {
2120
+ if (typeof superClass !== "function" && superClass !== null) {
2121
+ throw new TypeError("Super expression must either be null or a function");
1059
2122
  }
1060
- }
1061
- clear() {
1062
- this.actionStack = [];
1063
- this.stackItem = null;
1064
- this.index = 0;
1065
- this.beforePosition = null;
1066
- clearTimeout(this.timer);
1067
- this.listenerCaches.forEach((undoManager) => {
1068
- undoManager.clear();
1069
- });
1070
- this.changeEvent.next();
1071
- }
1072
- destroy() {
1073
- this.clear();
1074
- this.beforePosition = this.stackItem = null;
1075
- this.subscription.unsubscribe();
1076
- this.listenerCaches.forEach((undoManager) => {
1077
- undoManager.destroy();
1078
- });
1079
- this.subDocs.clear();
1080
- this.listenerCaches.clear();
1081
- }
1082
- listenItem(yType, yDoc) {
1083
- const undoManagerConfig = this.undoManagerConfig || {};
1084
- const undoManager = new yjs.UndoManager(yType, {
1085
- trackedOrigins: /* @__PURE__ */ new Set([yDoc]),
1086
- captureTimeout: 0,
1087
- captureTransaction(arg) {
1088
- if (undoManagerConfig.captureTransaction) {
1089
- return undoManagerConfig.captureTransaction(arg);
2123
+ subClass.prototype = Object.create(superClass && superClass.prototype, {
2124
+ constructor: {
2125
+ value: subClass,
2126
+ writable: true,
2127
+ configurable: true
1090
2128
  }
1091
- return true;
1092
- },
1093
- deleteFilter(item) {
1094
- if (undoManagerConfig.deleteFilter) {
1095
- return undoManagerConfig.deleteFilter(item);
1096
- }
1097
- return true;
1098
- }
1099
2129
  });
1100
- undoManager.on("stack-item-added", (event) => {
1101
- if (event.type === "undo" && !(event.origin instanceof yjs.UndoManager)) {
1102
- if (this.index != this.actionStack.length) {
1103
- const redoStack = this.actionStack.slice(this.index);
1104
- redoStack.forEach((item) => {
1105
- item.undoManagers.forEach((i) => {
1106
- i.clear(false, true);
2130
+ if (superClass) _set_prototype_of(subClass, superClass);
2131
+ }
2132
+ function _possible_constructor_return(self, call) {
2133
+ if (call && (_type_of(call) === "object" || typeof call === "function")) {
2134
+ return call;
2135
+ }
2136
+ return _assert_this_initialized(self);
2137
+ }
2138
+ function _set_prototype_of(o, p) {
2139
+ _set_prototype_of = Object.setPrototypeOf || function setPrototypeOf(o, p) {
2140
+ o.__proto__ = p;
2141
+ return o;
2142
+ };
2143
+ return _set_prototype_of(o, p);
2144
+ }
2145
+ function _type_of(obj) {
2146
+ "@swc/helpers - typeof";
2147
+ return obj && typeof Symbol !== "undefined" && obj.constructor === Symbol ? "symbol" : typeof obj;
2148
+ }
2149
+ function _is_native_reflect_construct() {
2150
+ try {
2151
+ var result = !Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function() {}));
2152
+ } catch (_) {}
2153
+ return (_is_native_reflect_construct = function() {
2154
+ return !!result;
2155
+ })();
2156
+ }
2157
+ var YWebsocketConnector = /*#__PURE__*/ function(SyncConnector) {
2158
+ _inherits(YWebsocketConnector, SyncConnector);
2159
+ function YWebsocketConnector(url, roomName, yDoc) {
2160
+ _class_call_check$2(this, YWebsocketConnector);
2161
+ var _this;
2162
+ _this = _call_super(this, YWebsocketConnector), _define_property$2(_this, "provide", void 0), _define_property$2(_this, "onSync", function(is) {
2163
+ if (is) {
2164
+ _this.loadEvent.next();
2165
+ }
2166
+ }), _define_property$2(_this, "onUpdate", function() {
2167
+ var syncStates = [];
2168
+ _this.provide.awareness.getStates().forEach(function(state, id) {
2169
+ syncStates.push({
2170
+ clientId: id,
2171
+ message: state.message
2172
+ });
1107
2173
  });
1108
- });
1109
- this.actionStack.length = this.index;
1110
- this.changeEvent.next();
1111
- }
1112
- if (this.stackItem === null) {
1113
- this.stackItem = {
1114
- before: this.beforePosition,
1115
- after: null,
1116
- undoManagers: []
1117
- };
1118
- this.timer = setTimeout(() => {
1119
- if (this.actionStack.length >= this.stackSize) {
1120
- this.actionStack.shift();
1121
- } else {
1122
- this.index++;
1123
- }
1124
- this.stackItem.after = this.beforePosition;
1125
- this.actionStack.push(this.stackItem);
1126
- this.stackItem = null;
1127
- this.pushEvent.next();
1128
- this.changeEvent.next();
1129
- }, 500);
2174
+ _this.stateChangeEvent.next(syncStates);
2175
+ });
2176
+ _this.onLoad = _this.loadEvent.asObservable();
2177
+ _this.onStateChange = _this.stateChangeEvent.asObservable();
2178
+ _this.provide = new yWebsocket.WebsocketProvider(url, roomName, yDoc);
2179
+ _this.provide.once('sync', _this.onSync);
2180
+ _this.provide.awareness.on('update', _this.onUpdate);
2181
+ return _this;
2182
+ }
2183
+ _create_class$2(YWebsocketConnector, [
2184
+ {
2185
+ key: "setLocalStateField",
2186
+ value: function setLocalStateField(key, data) {
2187
+ this.provide.awareness.setLocalStateField(key, data);
2188
+ }
2189
+ },
2190
+ {
2191
+ key: "onDestroy",
2192
+ value: function onDestroy() {
2193
+ this.provide.awareness.off('update', this.onUpdate);
2194
+ this.provide.disconnect();
2195
+ this.provide.destroy();
2196
+ }
1130
2197
  }
1131
- this.stackItem.undoManagers.push(undoManager);
1132
- }
1133
- });
1134
- this.listenerCaches.add(undoManager);
1135
- }
1136
- };
1137
- exports.MultipleDocCollabHistory = __decorateClass$1([
1138
- core$1.Injectable(),
1139
- __decorateParam(3, core$1.Inject(core.HISTORY_STACK_SIZE)),
1140
- __decorateParam(4, core$1.Optional())
1141
- ], exports.MultipleDocCollabHistory);
1142
- var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
1143
- var __decorateClass = (decorators, target, key, kind) => {
1144
- var result = kind > 1 ? void 0 : kind ? __getOwnPropDesc(target, key) : target;
1145
- for (var i = decorators.length - 1, decorator; i >= 0; i--)
1146
- if (decorator = decorators[i])
1147
- result = decorator(result) || result;
1148
- return result;
1149
- };
1150
- const subModelLoaderErrorFn = core.makeError("subModelLoaderError");
1151
- class SubModelLoader {
1152
- }
1153
- exports.NonSubModelLoader = class NonSubModelLoader extends SubModelLoader {
1154
- createSubModelBySlot() {
1155
- throw subModelLoaderErrorFn("single document does not support async slot.");
1156
- }
1157
- createSubModelByComponent() {
1158
- throw subModelLoaderErrorFn("single document does not support async component.");
1159
- }
1160
- loadSubModelByComponent() {
1161
- throw subModelLoaderErrorFn("single document does not support async component.");
1162
- }
1163
- loadSubModelBySlot() {
1164
- throw subModelLoaderErrorFn("single document does not support async slot.");
1165
- }
1166
- getLoadedModelBySlot() {
1167
- throw subModelLoaderErrorFn("single document does not support async slot.");
1168
- }
1169
- getLoadedModelByComponent() {
1170
- throw subModelLoaderErrorFn("single document does not support async component.");
1171
- }
1172
- };
1173
- exports.NonSubModelLoader = __decorateClass([
1174
- core$1.Injectable()
1175
- ], exports.NonSubModelLoader);
1176
- class SyncConnector {
1177
- /**
1178
- * 当文档加载完成时触发的观察者
1179
- */
1180
- onLoad;
1181
- /**
1182
- * 当文档 awareness 状态变更时触发的观察者
1183
- */
1184
- onStateChange;
1185
- loadEvent = new stream.Subject();
1186
- stateChangeEvent = new stream.Subject();
1187
- constructor() {
1188
- this.onLoad = this.loadEvent.asObservable();
1189
- this.onStateChange = this.stateChangeEvent.asObservable();
1190
- }
1191
- }
1192
- class HocuspocusConnector extends SyncConnector {
1193
- provide;
1194
- constructor(config) {
1195
- super();
1196
- this.provide = new provider.HocuspocusProvider({
1197
- ...config,
1198
- onSynced: (data) => {
1199
- config.onSynced?.(data);
1200
- this.loadEvent.next();
1201
- },
1202
- onAwarenessUpdate: (data) => {
1203
- config.onAwarenessUpdate?.(data);
1204
- const states = data.states.map((state) => {
1205
- return {
1206
- clientId: state.clientId,
1207
- message: state.message
1208
- };
2198
+ ]);
2199
+ return YWebsocketConnector;
2200
+ }(SyncConnector);
2201
+
2202
+ function _class_call_check$1(instance, Constructor) {
2203
+ if (!(instance instanceof Constructor)) {
2204
+ throw new TypeError("Cannot call a class as a function");
2205
+ }
2206
+ }
2207
+ function _defineProperties$1(target, props) {
2208
+ for(var i = 0; i < props.length; i++){
2209
+ var descriptor = props[i];
2210
+ descriptor.enumerable = descriptor.enumerable || false;
2211
+ descriptor.configurable = true;
2212
+ if ("value" in descriptor) descriptor.writable = true;
2213
+ Object.defineProperty(target, descriptor.key, descriptor);
2214
+ }
2215
+ }
2216
+ function _create_class$1(Constructor, protoProps, staticProps) {
2217
+ if (protoProps) _defineProperties$1(Constructor.prototype, protoProps);
2218
+ return Constructor;
2219
+ }
2220
+ function _define_property$1(obj, key, value) {
2221
+ if (key in obj) {
2222
+ Object.defineProperty(obj, key, {
2223
+ value: value,
2224
+ enumerable: true,
2225
+ configurable: true,
2226
+ writable: true
1209
2227
  });
1210
- this.stateChangeEvent.next(states);
1211
- }
1212
- });
1213
- }
1214
- setLocalStateField(key, data) {
1215
- this.provide.setAwarenessField(key, data);
1216
- }
1217
- onDestroy() {
1218
- this.provide.disconnect();
1219
- this.provide.destroy();
1220
- }
1221
- }
1222
- class YWebsocketConnector extends SyncConnector {
1223
- provide;
1224
- onSync = (is) => {
1225
- if (is) {
1226
- this.loadEvent.next();
2228
+ } else {
2229
+ obj[key] = value;
1227
2230
  }
1228
- };
1229
- onUpdate = () => {
1230
- const syncStates = [];
1231
- this.provide.awareness.getStates().forEach((state, id) => {
1232
- syncStates.push({
1233
- clientId: id,
1234
- message: state.message
1235
- });
1236
- });
1237
- this.stateChangeEvent.next(syncStates);
1238
- };
1239
- constructor(url, roomName, yDoc) {
1240
- super();
1241
- this.onLoad = this.loadEvent.asObservable();
1242
- this.onStateChange = this.stateChangeEvent.asObservable();
1243
- this.provide = new yWebsocket.WebsocketProvider(url, roomName, yDoc);
1244
- this.provide.once("sync", this.onSync);
1245
- this.provide.awareness.on("update", this.onUpdate);
1246
- }
1247
- setLocalStateField(key, data) {
1248
- this.provide.awareness.setLocalStateField(key, data);
1249
- }
1250
- onDestroy() {
1251
- this.provide.awareness.off("update", this.onUpdate);
1252
- this.provide.disconnect();
1253
- this.provide.destroy();
1254
- }
1255
- }
1256
- class CollaborateModule {
1257
- constructor(config) {
1258
- this.config = config;
1259
- }
1260
- config;
1261
- subscription = new stream.Subscription();
1262
- providers = [
1263
- exports.Collaborate,
1264
- exports.CollabHistory,
1265
- {
1266
- provide: core.History,
1267
- useExisting: exports.CollabHistory
1268
- },
1269
- {
1270
- provide: SyncConnector,
1271
- useFactory: (collab) => {
1272
- return this.config.createConnector(collab.yDoc);
1273
- },
1274
- deps: [exports.Collaborate]
1275
- },
1276
- {
1277
- provide: SubModelLoader,
1278
- useClass: exports.NonSubModelLoader
2231
+ return obj;
2232
+ }
2233
+ var CollaborateModule = /*#__PURE__*/ function() {
2234
+ function CollaborateModule(config) {
2235
+ var _this = this;
2236
+ _class_call_check$1(this, CollaborateModule);
2237
+ _define_property$1(this, "config", void 0);
2238
+ _define_property$1(this, "subscription", void 0);
2239
+ _define_property$1(this, "providers", void 0);
2240
+ _define_property$1(this, "timer", void 0);
2241
+ this.config = config;
2242
+ this.subscription = new stream.Subscription();
2243
+ this.providers = [
2244
+ exports.Collaborate,
2245
+ exports.CollabHistory,
2246
+ {
2247
+ provide: core.History,
2248
+ useExisting: exports.CollabHistory
2249
+ },
2250
+ {
2251
+ provide: SyncConnector,
2252
+ useFactory: function useFactory(collab) {
2253
+ return _this.config.createConnector(collab.yDoc);
2254
+ },
2255
+ deps: [
2256
+ exports.Collaborate
2257
+ ]
2258
+ },
2259
+ {
2260
+ provide: SubModelLoader,
2261
+ useClass: exports.NonSubModelLoader
2262
+ }
2263
+ ];
2264
+ this.timer = null;
1279
2265
  }
1280
- ];
1281
- timer = null;
1282
- setup(textbus) {
1283
- const messageBus = textbus.get(MessageBus, null);
1284
- const connector = textbus.get(SyncConnector);
1285
- const collab = textbus.get(exports.Collaborate);
1286
- if (messageBus) {
1287
- const selection = textbus.get(core.Selection);
1288
- connector.setLocalStateField("message", messageBus.get(textbus));
1289
- this.subscription.add(
1290
- messageBus.onSync.subscribe(() => {
1291
- connector.setLocalStateField("message", messageBus.get(textbus));
1292
- }),
1293
- selection.onChange.subscribe(() => {
1294
- connector.setLocalStateField("message", messageBus.get(textbus));
1295
- }),
1296
- connector.onStateChange.subscribe((states) => {
1297
- messageBus.consume(states, textbus);
1298
- })
1299
- );
2266
+ _create_class$1(CollaborateModule, [
2267
+ {
2268
+ key: "setup",
2269
+ value: function setup(textbus) {
2270
+ var _this = this;
2271
+ var messageBus = textbus.get(MessageBus, null);
2272
+ var connector = textbus.get(SyncConnector);
2273
+ var collab = textbus.get(exports.Collaborate);
2274
+ if (messageBus) {
2275
+ var selection = textbus.get(core.Selection);
2276
+ connector.setLocalStateField('message', messageBus.get(textbus));
2277
+ this.subscription.add(messageBus.onSync.subscribe(function() {
2278
+ connector.setLocalStateField('message', messageBus.get(textbus));
2279
+ }), selection.onChange.subscribe(function() {
2280
+ connector.setLocalStateField('message', messageBus.get(textbus));
2281
+ }), connector.onStateChange.subscribe(function(states) {
2282
+ messageBus.consume(states, textbus);
2283
+ }));
2284
+ }
2285
+ return connector.onLoad.toPromise().then(function() {
2286
+ if (!_this.config.onlyLoad) {
2287
+ return;
2288
+ }
2289
+ var root = collab.yDoc.getMap('RootComponent');
2290
+ if (root.has('state')) {
2291
+ return;
2292
+ }
2293
+ return new Promise(function(resolve) {
2294
+ var testing = function testing1() {
2295
+ if (root.has('state')) {
2296
+ resolve();
2297
+ } else {
2298
+ _this.timer = setTimeout(testing, 1000);
2299
+ }
2300
+ };
2301
+ _this.timer = setTimeout(testing, 1000);
2302
+ });
2303
+ });
2304
+ }
2305
+ },
2306
+ {
2307
+ key: "onDestroy",
2308
+ value: function onDestroy(textbus) {
2309
+ this.subscription.unsubscribe();
2310
+ textbus.get(exports.Collaborate).destroy();
2311
+ textbus.get(core.History).destroy();
2312
+ textbus.get(SyncConnector).onDestroy();
2313
+ clearTimeout(this.timer);
2314
+ }
2315
+ }
2316
+ ]);
2317
+ return CollaborateModule;
2318
+ }();
2319
+
2320
+ function _class_call_check(instance, Constructor) {
2321
+ if (!(instance instanceof Constructor)) {
2322
+ throw new TypeError("Cannot call a class as a function");
1300
2323
  }
1301
- return connector.onLoad.toPromise().then(() => {
1302
- if (!this.config.onlyLoad) {
1303
- return;
1304
- }
1305
- const root = collab.yDoc.getMap("RootComponent");
1306
- if (root.has("state")) {
1307
- return;
1308
- }
1309
- return new Promise((resolve) => {
1310
- const testing = () => {
1311
- if (root.has("state")) {
1312
- resolve();
1313
- } else {
1314
- this.timer = setTimeout(testing, 1e3);
1315
- }
1316
- };
1317
- this.timer = setTimeout(testing, 1e3);
1318
- });
1319
- });
1320
- }
1321
- onDestroy(textbus) {
1322
- this.subscription.unsubscribe();
1323
- textbus.get(exports.Collaborate).destroy();
1324
- textbus.get(core.History).destroy();
1325
- textbus.get(SyncConnector).onDestroy();
1326
- clearTimeout(this.timer);
1327
- }
1328
- }
1329
- class MultipleDocumentCollaborateModule {
1330
- constructor(config) {
1331
- this.config = config;
1332
- }
1333
- config;
1334
- subscription = new stream.Subscription();
1335
- providers = [
1336
- exports.Collaborate,
1337
- exports.MultipleDocCollabHistory,
1338
- {
1339
- provide: core.History,
1340
- useExisting: exports.MultipleDocCollabHistory
1341
- },
1342
- {
1343
- provide: SyncConnector,
1344
- useFactory: (collab) => {
1345
- return this.config.createConnector(collab.yDoc);
1346
- },
1347
- deps: [exports.Collaborate]
1348
- },
1349
- {
1350
- provide: SubModelLoader,
1351
- useFactory: () => {
1352
- return this.config.subModelLoader;
1353
- }
2324
+ }
2325
+ function _defineProperties(target, props) {
2326
+ for(var i = 0; i < props.length; i++){
2327
+ var descriptor = props[i];
2328
+ descriptor.enumerable = descriptor.enumerable || false;
2329
+ descriptor.configurable = true;
2330
+ if ("value" in descriptor) descriptor.writable = true;
2331
+ Object.defineProperty(target, descriptor.key, descriptor);
1354
2332
  }
1355
- ];
1356
- timer = null;
1357
- setup(textbus) {
1358
- const messageBus = textbus.get(MessageBus, null);
1359
- const connector = textbus.get(SyncConnector);
1360
- const collab = textbus.get(exports.Collaborate);
1361
- if (messageBus) {
1362
- const selection = textbus.get(core.Selection);
1363
- connector.setLocalStateField("message", messageBus.get(textbus));
1364
- this.subscription.add(
1365
- messageBus.onSync.subscribe(() => {
1366
- connector.setLocalStateField("message", messageBus.get(textbus));
1367
- }),
1368
- selection.onChange.subscribe(() => {
1369
- connector.setLocalStateField("message", messageBus.get(textbus));
1370
- }),
1371
- connector.onStateChange.subscribe((states) => {
1372
- messageBus.consume(states, textbus);
1373
- })
1374
- );
2333
+ }
2334
+ function _create_class(Constructor, protoProps, staticProps) {
2335
+ if (protoProps) _defineProperties(Constructor.prototype, protoProps);
2336
+ return Constructor;
2337
+ }
2338
+ function _define_property(obj, key, value) {
2339
+ if (key in obj) {
2340
+ Object.defineProperty(obj, key, {
2341
+ value: value,
2342
+ enumerable: true,
2343
+ configurable: true,
2344
+ writable: true
2345
+ });
2346
+ } else {
2347
+ obj[key] = value;
1375
2348
  }
1376
- return connector.onLoad.toPromise().then(() => {
1377
- if (!this.config.onlyLoad) {
1378
- return;
1379
- }
1380
- const root = collab.yDoc.getMap("RootComponent");
1381
- if (root.has("state")) {
1382
- return;
1383
- }
1384
- return new Promise((resolve) => {
1385
- const testing = () => {
1386
- if (root.has("state")) {
1387
- resolve();
1388
- } else {
1389
- this.timer = setTimeout(testing, 1e3);
1390
- }
1391
- };
1392
- this.timer = setTimeout(testing, 1e3);
1393
- });
1394
- });
1395
- }
1396
- onDestroy(textbus) {
1397
- this.subscription.unsubscribe();
1398
- textbus.get(exports.Collaborate).destroy();
1399
- textbus.get(core.History).destroy();
1400
- textbus.get(SyncConnector).onDestroy();
1401
- clearTimeout(this.timer);
1402
- }
2349
+ return obj;
1403
2350
  }
2351
+ var MultipleDocumentCollaborateModule = /*#__PURE__*/ function() {
2352
+ function MultipleDocumentCollaborateModule(config) {
2353
+ var _this = this;
2354
+ _class_call_check(this, MultipleDocumentCollaborateModule);
2355
+ _define_property(this, "config", void 0);
2356
+ _define_property(this, "subscription", void 0);
2357
+ _define_property(this, "providers", void 0);
2358
+ _define_property(this, "timer", void 0);
2359
+ this.config = config;
2360
+ this.subscription = new stream.Subscription();
2361
+ this.providers = [
2362
+ exports.Collaborate,
2363
+ exports.MultipleDocCollabHistory,
2364
+ {
2365
+ provide: core.History,
2366
+ useExisting: exports.MultipleDocCollabHistory
2367
+ },
2368
+ {
2369
+ provide: SyncConnector,
2370
+ useFactory: function useFactory(collab) {
2371
+ return _this.config.createConnector(collab.yDoc);
2372
+ },
2373
+ deps: [
2374
+ exports.Collaborate
2375
+ ]
2376
+ },
2377
+ {
2378
+ provide: SubModelLoader,
2379
+ useFactory: function useFactory() {
2380
+ return _this.config.subModelLoader;
2381
+ }
2382
+ }
2383
+ ];
2384
+ this.timer = null;
2385
+ }
2386
+ _create_class(MultipleDocumentCollaborateModule, [
2387
+ {
2388
+ key: "setup",
2389
+ value: function setup(textbus) {
2390
+ var _this = this;
2391
+ var messageBus = textbus.get(MessageBus, null);
2392
+ var connector = textbus.get(SyncConnector);
2393
+ var collab = textbus.get(exports.Collaborate);
2394
+ if (messageBus) {
2395
+ var selection = textbus.get(core.Selection);
2396
+ connector.setLocalStateField('message', messageBus.get(textbus));
2397
+ this.subscription.add(messageBus.onSync.subscribe(function() {
2398
+ connector.setLocalStateField('message', messageBus.get(textbus));
2399
+ }), selection.onChange.subscribe(function() {
2400
+ connector.setLocalStateField('message', messageBus.get(textbus));
2401
+ }), connector.onStateChange.subscribe(function(states) {
2402
+ messageBus.consume(states, textbus);
2403
+ }));
2404
+ }
2405
+ return connector.onLoad.toPromise().then(function() {
2406
+ if (!_this.config.onlyLoad) {
2407
+ return;
2408
+ }
2409
+ var root = collab.yDoc.getMap('RootComponent');
2410
+ if (root.has('state')) {
2411
+ return;
2412
+ }
2413
+ return new Promise(function(resolve) {
2414
+ var testing = function testing1() {
2415
+ if (root.has('state')) {
2416
+ resolve();
2417
+ } else {
2418
+ _this.timer = setTimeout(testing, 1000);
2419
+ }
2420
+ };
2421
+ _this.timer = setTimeout(testing, 1000);
2422
+ });
2423
+ });
2424
+ }
2425
+ },
2426
+ {
2427
+ key: "onDestroy",
2428
+ value: function onDestroy(textbus) {
2429
+ this.subscription.unsubscribe();
2430
+ textbus.get(exports.Collaborate).destroy();
2431
+ textbus.get(core.History).destroy();
2432
+ textbus.get(SyncConnector).onDestroy();
2433
+ clearTimeout(this.timer);
2434
+ }
2435
+ }
2436
+ ]);
2437
+ return MultipleDocumentCollaborateModule;
2438
+ }();
2439
+
1404
2440
  exports.CollaborateModule = CollaborateModule;
1405
2441
  exports.CustomUndoManagerConfig = CustomUndoManagerConfig;
1406
2442
  exports.HocuspocusConnector = HocuspocusConnector;