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