@pluv/crdt-yjs 0.15.0 → 0.16.1
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/.eslintrc.js +2 -2
- package/.turbo/turbo-build.log +9 -9
- package/CHANGELOG.md +45 -37
- package/README.md +13 -29
- package/dist/index.d.mts +152 -55
- package/dist/index.d.ts +152 -55
- package/dist/index.js +433 -107
- package/dist/index.mjs +445 -101
- package/package.json +9 -8
- package/src/array/CrdtYjsArray.ts +55 -0
- package/src/array/array.ts +7 -0
- package/src/array/index.ts +2 -0
- package/src/doc/CrdtYjsDoc.ts +274 -0
- package/src/doc/CrdtYjsDocFactory.ts +83 -0
- package/src/doc/doc.ts +10 -0
- package/src/doc/index.ts +3 -0
- package/src/index.ts +1 -20
- package/src/map/CrdtYjsMap.ts +43 -0
- package/src/map/index.ts +2 -0
- package/src/{map.ts → map/map.ts} +3 -3
- package/src/object/CrdtYjsObject.ts +32 -0
- package/src/object/index.ts +2 -0
- package/src/object/object.ts +7 -0
- package/src/shared/index.ts +2 -0
- package/src/shared/isWrapper.ts +25 -0
- package/src/shared/toYjsValue.ts +5 -0
- package/src/text/CrdtYjsText.ts +34 -0
- package/src/text/index.ts +2 -0
- package/src/text/text.ts +5 -0
- package/src/types.ts +54 -35
- package/src/xmlElement/CrdtYjsXmlElement.ts +60 -0
- package/src/xmlElement/index.ts +2 -0
- package/src/xmlElement/xmlElement.ts +9 -0
- package/src/xmlFragment/CrdtYjsXmlFragment.ts +56 -0
- package/src/xmlFragment/index.ts +2 -0
- package/src/xmlFragment/xmlFragment.ts +9 -0
- package/src/xmlText/CrdtYjsXmlText.ts +38 -0
- package/src/xmlText/index.ts +2 -0
- package/src/xmlText/xmlText.ts +5 -0
- package/src/yjs.ts +9 -0
- package/src/array.ts +0 -11
- package/src/doc.ts +0 -160
- package/src/object.ts +0 -16
- package/src/text.ts +0 -5
- package/src/xmlElement.ts +0 -19
- package/src/xmlFragment.ts +0 -16
- package/src/xmlText.ts +0 -5
package/dist/index.mjs
CHANGED
|
@@ -17,52 +17,366 @@ var __spreadValues = (a, b) => {
|
|
|
17
17
|
return a;
|
|
18
18
|
};
|
|
19
19
|
var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
|
|
20
|
+
var __export = (target, all) => {
|
|
21
|
+
for (var name in all)
|
|
22
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
// src/yjs.ts
|
|
26
|
+
var yjs_exports = {};
|
|
27
|
+
__export(yjs_exports, {
|
|
28
|
+
CrdtYjsArray: () => CrdtYjsArray,
|
|
29
|
+
CrdtYjsDoc: () => CrdtYjsDoc,
|
|
30
|
+
CrdtYjsMap: () => CrdtYjsMap,
|
|
31
|
+
CrdtYjsObject: () => CrdtYjsObject,
|
|
32
|
+
CrdtYjsText: () => CrdtYjsText,
|
|
33
|
+
CrdtYjsXmlElement: () => CrdtYjsXmlElement,
|
|
34
|
+
CrdtYjsXmlFragment: () => CrdtYjsXmlFragment,
|
|
35
|
+
CrdtYjsXmlText: () => CrdtYjsXmlText,
|
|
36
|
+
array: () => array,
|
|
37
|
+
doc: () => doc,
|
|
38
|
+
map: () => map,
|
|
39
|
+
object: () => object,
|
|
40
|
+
xmlElement: () => xmlElement,
|
|
41
|
+
xmlFragment: () => xmlFragment,
|
|
42
|
+
xmlText: () => xmlText
|
|
43
|
+
});
|
|
20
44
|
|
|
21
|
-
// src/array.ts
|
|
45
|
+
// src/array/CrdtYjsArray.ts
|
|
46
|
+
import { AbstractCrdtType as AbstractCrdtType7 } from "@pluv/crdt";
|
|
22
47
|
import { Array as YArray } from "yjs";
|
|
48
|
+
|
|
49
|
+
// src/map/CrdtYjsMap.ts
|
|
50
|
+
import { AbstractCrdtType } from "@pluv/crdt";
|
|
51
|
+
import { Map as YMap } from "yjs";
|
|
52
|
+
var CrdtYjsMap = class extends AbstractCrdtType {
|
|
53
|
+
constructor(value = []) {
|
|
54
|
+
super();
|
|
55
|
+
this.initialValue = value.map(([k, v]) => [k, toYjsValue(v)]);
|
|
56
|
+
this.value = new YMap(this.initialValue);
|
|
57
|
+
}
|
|
58
|
+
get size() {
|
|
59
|
+
return this.value.size;
|
|
60
|
+
}
|
|
61
|
+
delete(prop) {
|
|
62
|
+
this.value.delete(prop);
|
|
63
|
+
return this;
|
|
64
|
+
}
|
|
65
|
+
set(prop, value) {
|
|
66
|
+
this.value.set(prop, toYjsValue(value));
|
|
67
|
+
return this;
|
|
68
|
+
}
|
|
69
|
+
toJson() {
|
|
70
|
+
return this.value.toJSON();
|
|
71
|
+
}
|
|
72
|
+
};
|
|
73
|
+
|
|
74
|
+
// src/object/CrdtYjsObject.ts
|
|
75
|
+
import { AbstractCrdtType as AbstractCrdtType2 } from "@pluv/crdt";
|
|
76
|
+
import { Map as YMap2 } from "yjs";
|
|
77
|
+
var CrdtYjsObject = class extends AbstractCrdtType2 {
|
|
78
|
+
constructor(value = {}) {
|
|
79
|
+
super();
|
|
80
|
+
this.initialValue = Object.entries(value).map(([k, v]) => [
|
|
81
|
+
k,
|
|
82
|
+
toYjsValue(v)
|
|
83
|
+
]);
|
|
84
|
+
this.value = new YMap2(this.initialValue);
|
|
85
|
+
}
|
|
86
|
+
set(prop, value) {
|
|
87
|
+
this.value.set(prop.toString(), toYjsValue(value));
|
|
88
|
+
return this;
|
|
89
|
+
}
|
|
90
|
+
toJson() {
|
|
91
|
+
return this.value.toJSON();
|
|
92
|
+
}
|
|
93
|
+
};
|
|
94
|
+
|
|
95
|
+
// src/text/CrdtYjsText.ts
|
|
96
|
+
import { AbstractCrdtType as AbstractCrdtType3 } from "@pluv/crdt";
|
|
97
|
+
import { Text as YText } from "yjs";
|
|
98
|
+
var CrdtYjsText = class extends AbstractCrdtType3 {
|
|
99
|
+
constructor(value = "") {
|
|
100
|
+
super();
|
|
101
|
+
this.initialValue = value;
|
|
102
|
+
this.value = new YText(value);
|
|
103
|
+
}
|
|
104
|
+
get length() {
|
|
105
|
+
return this.value.length;
|
|
106
|
+
}
|
|
107
|
+
delete(index, length = 1) {
|
|
108
|
+
this.value.delete(index, length);
|
|
109
|
+
return this;
|
|
110
|
+
}
|
|
111
|
+
insert(index, text2) {
|
|
112
|
+
this.value.insert(index, text2);
|
|
113
|
+
return this;
|
|
114
|
+
}
|
|
115
|
+
toJson() {
|
|
116
|
+
return this.value.toJSON();
|
|
117
|
+
}
|
|
118
|
+
};
|
|
119
|
+
|
|
120
|
+
// src/xmlElement/CrdtYjsXmlElement.ts
|
|
121
|
+
import { AbstractCrdtType as AbstractCrdtType4 } from "@pluv/crdt";
|
|
122
|
+
import { XmlElement as YXmlElement } from "yjs";
|
|
123
|
+
var CrdtYjsXmlElement = class extends AbstractCrdtType4 {
|
|
124
|
+
constructor(name, children) {
|
|
125
|
+
super();
|
|
126
|
+
this.initialValue = children.map((item) => item.value);
|
|
127
|
+
this.name = name;
|
|
128
|
+
this.value = new YXmlElement(name);
|
|
129
|
+
this.push(...children);
|
|
130
|
+
}
|
|
131
|
+
get length() {
|
|
132
|
+
return this.value.length;
|
|
133
|
+
}
|
|
134
|
+
delete(index, length = 1) {
|
|
135
|
+
this.value.delete(index, length);
|
|
136
|
+
return this;
|
|
137
|
+
}
|
|
138
|
+
insert(index, ...children) {
|
|
139
|
+
const converted = children.map((child) => toYjsValue(child));
|
|
140
|
+
this.value.insert(index, converted);
|
|
141
|
+
return this;
|
|
142
|
+
}
|
|
143
|
+
push(...children) {
|
|
144
|
+
return this.insert(this.length, ...children);
|
|
145
|
+
}
|
|
146
|
+
unshift(...children) {
|
|
147
|
+
return this.insert(0, ...children);
|
|
148
|
+
}
|
|
149
|
+
toJson() {
|
|
150
|
+
return this.value.toJSON();
|
|
151
|
+
}
|
|
152
|
+
};
|
|
153
|
+
|
|
154
|
+
// src/xmlElement/xmlElement.ts
|
|
155
|
+
var xmlElement = (name, children) => {
|
|
156
|
+
return new CrdtYjsXmlElement(name, children);
|
|
157
|
+
};
|
|
158
|
+
|
|
159
|
+
// src/xmlFragment/CrdtYjsXmlFragment.ts
|
|
160
|
+
import { AbstractCrdtType as AbstractCrdtType5 } from "@pluv/crdt";
|
|
161
|
+
import { XmlFragment as YXmlFragment } from "yjs";
|
|
162
|
+
var CrdtYjsXmlFragment = class extends AbstractCrdtType5 {
|
|
163
|
+
constructor(children) {
|
|
164
|
+
super();
|
|
165
|
+
this.initialValue = children.map((item) => item.value);
|
|
166
|
+
this.value = new YXmlFragment();
|
|
167
|
+
this.push(...children);
|
|
168
|
+
}
|
|
169
|
+
get length() {
|
|
170
|
+
return this.value.length;
|
|
171
|
+
}
|
|
172
|
+
delete(index, length = 1) {
|
|
173
|
+
this.value.delete(index, length);
|
|
174
|
+
return this;
|
|
175
|
+
}
|
|
176
|
+
insert(index, ...children) {
|
|
177
|
+
const converted = children.map((child) => toYjsValue(child));
|
|
178
|
+
this.value.insert(index, converted);
|
|
179
|
+
return this;
|
|
180
|
+
}
|
|
181
|
+
push(...children) {
|
|
182
|
+
return this.insert(this.length, ...children);
|
|
183
|
+
}
|
|
184
|
+
unshift(...children) {
|
|
185
|
+
return this.insert(0, ...children);
|
|
186
|
+
}
|
|
187
|
+
toJson() {
|
|
188
|
+
return this.value.toJSON();
|
|
189
|
+
}
|
|
190
|
+
};
|
|
191
|
+
|
|
192
|
+
// src/xmlFragment/xmlFragment.ts
|
|
193
|
+
var xmlFragment = (children) => {
|
|
194
|
+
return new CrdtYjsXmlFragment(children);
|
|
195
|
+
};
|
|
196
|
+
|
|
197
|
+
// src/xmlText/CrdtYjsXmlText.ts
|
|
198
|
+
import { AbstractCrdtType as AbstractCrdtType6 } from "@pluv/crdt";
|
|
199
|
+
import { XmlText as YXmlText } from "yjs";
|
|
200
|
+
var CrdtYjsXmlText = class extends AbstractCrdtType6 {
|
|
201
|
+
constructor(value = "") {
|
|
202
|
+
super();
|
|
203
|
+
this.initialValue = value;
|
|
204
|
+
this.value = new YXmlText(value);
|
|
205
|
+
}
|
|
206
|
+
get length() {
|
|
207
|
+
return this.value.length;
|
|
208
|
+
}
|
|
209
|
+
delete(index, length = 1) {
|
|
210
|
+
this.value.delete(index, length);
|
|
211
|
+
return this;
|
|
212
|
+
}
|
|
213
|
+
insert(index, text2, attributes) {
|
|
214
|
+
this.value.insert(index, text2, attributes);
|
|
215
|
+
return this;
|
|
216
|
+
}
|
|
217
|
+
toJson() {
|
|
218
|
+
return this.value.toJSON();
|
|
219
|
+
}
|
|
220
|
+
};
|
|
221
|
+
|
|
222
|
+
// src/xmlText/xmlText.ts
|
|
223
|
+
var xmlText = (value = "") => {
|
|
224
|
+
return new CrdtYjsXmlText(value);
|
|
225
|
+
};
|
|
226
|
+
|
|
227
|
+
// src/shared/isWrapper.ts
|
|
228
|
+
var isWrapper = (item) => {
|
|
229
|
+
return item instanceof CrdtYjsArray || item instanceof CrdtYjsMap || item instanceof CrdtYjsObject || item instanceof CrdtYjsText || item instanceof CrdtYjsXmlElement || item instanceof CrdtYjsXmlFragment || item instanceof CrdtYjsXmlText;
|
|
230
|
+
};
|
|
231
|
+
|
|
232
|
+
// src/shared/toYjsValue.ts
|
|
233
|
+
var toYjsValue = (item) => {
|
|
234
|
+
return isWrapper(item) ? item.value : item;
|
|
235
|
+
};
|
|
236
|
+
|
|
237
|
+
// src/array/CrdtYjsArray.ts
|
|
238
|
+
var CrdtYjsArray = class extends AbstractCrdtType7 {
|
|
239
|
+
constructor(value = []) {
|
|
240
|
+
super();
|
|
241
|
+
const initialValue = value.map((item) => toYjsValue(item));
|
|
242
|
+
this.initialValue = initialValue;
|
|
243
|
+
this.value = new YArray();
|
|
244
|
+
this.push(...value);
|
|
245
|
+
}
|
|
246
|
+
get length() {
|
|
247
|
+
return this.value.length;
|
|
248
|
+
}
|
|
249
|
+
delete(index, length = 1) {
|
|
250
|
+
this.value.delete(index, length);
|
|
251
|
+
return this;
|
|
252
|
+
}
|
|
253
|
+
insert(index, ...items) {
|
|
254
|
+
const converted = items.map((item) => toYjsValue(item));
|
|
255
|
+
this.value.insert(index, converted);
|
|
256
|
+
return this;
|
|
257
|
+
}
|
|
258
|
+
push(...items) {
|
|
259
|
+
return this.insert(this.length, ...items);
|
|
260
|
+
}
|
|
261
|
+
unshift(...items) {
|
|
262
|
+
return this.insert(0, ...items);
|
|
263
|
+
}
|
|
264
|
+
toJson() {
|
|
265
|
+
return this.value.toJSON();
|
|
266
|
+
}
|
|
267
|
+
};
|
|
268
|
+
|
|
269
|
+
// src/array/array.ts
|
|
23
270
|
var array = (value = []) => {
|
|
24
|
-
|
|
25
|
-
yarray.push(value.slice());
|
|
26
|
-
return yarray;
|
|
271
|
+
return new CrdtYjsArray(value);
|
|
27
272
|
};
|
|
28
273
|
|
|
29
|
-
// src/doc.ts
|
|
274
|
+
// src/doc/CrdtYjsDoc.ts
|
|
275
|
+
import { AbstractCrdtDoc } from "@pluv/crdt";
|
|
30
276
|
import { fromUint8Array, toUint8Array } from "js-base64";
|
|
31
277
|
import {
|
|
32
278
|
UndoManager,
|
|
279
|
+
Array as YArray2,
|
|
33
280
|
Doc as YDoc,
|
|
281
|
+
Map as YMap3,
|
|
282
|
+
Text as YText2,
|
|
283
|
+
XmlElement as YXmlElement2,
|
|
284
|
+
XmlFragment as YXmlFragment2,
|
|
285
|
+
XmlText as YXmlText2,
|
|
34
286
|
applyUpdate,
|
|
35
287
|
encodeStateAsUpdate
|
|
36
288
|
} from "yjs";
|
|
37
|
-
var
|
|
38
|
-
constructor(value) {
|
|
289
|
+
var CrdtYjsDoc = class extends AbstractCrdtDoc {
|
|
290
|
+
constructor(value = {}) {
|
|
291
|
+
super();
|
|
39
292
|
this.value = new YDoc();
|
|
40
293
|
this._undoManager = null;
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
294
|
+
this._storage = Object.entries(value).reduce((acc, [key, node]) => {
|
|
295
|
+
if (node instanceof CrdtYjsArray) {
|
|
296
|
+
const yArray = this.value.get(key, YArray2);
|
|
297
|
+
yArray.insert(0, node.initialValue.slice(0));
|
|
298
|
+
node.value = yArray;
|
|
299
|
+
return __spreadProps(__spreadValues({}, acc), { [key]: node });
|
|
300
|
+
}
|
|
301
|
+
if (node instanceof CrdtYjsMap || node instanceof CrdtYjsObject) {
|
|
302
|
+
const yMap = this.value.get(key, YMap3);
|
|
303
|
+
node.initialValue.forEach(([k, v]) => {
|
|
304
|
+
yMap.set(k.toString(), v);
|
|
305
|
+
});
|
|
306
|
+
node.value = yMap;
|
|
307
|
+
return __spreadProps(__spreadValues({}, acc), { [key]: node });
|
|
308
|
+
}
|
|
309
|
+
if (node instanceof CrdtYjsObject) {
|
|
310
|
+
const yMap = this.value.get(key, YMap3);
|
|
311
|
+
node.initialValue.forEach(([k, v]) => {
|
|
312
|
+
yMap.set(k.toString(), v);
|
|
313
|
+
});
|
|
314
|
+
node.value = yMap;
|
|
315
|
+
return __spreadProps(__spreadValues({}, acc), { [key]: node });
|
|
316
|
+
}
|
|
317
|
+
if (node instanceof CrdtYjsText) {
|
|
318
|
+
const yText = this.value.get(key, YText2);
|
|
319
|
+
yText.insert(0, node.initialValue);
|
|
320
|
+
node.value = yText;
|
|
321
|
+
return __spreadProps(__spreadValues({}, acc), { [key]: node });
|
|
322
|
+
}
|
|
323
|
+
if (node instanceof CrdtYjsXmlElement) {
|
|
324
|
+
const yXmlElement = this.value.get(
|
|
325
|
+
key,
|
|
326
|
+
YXmlElement2
|
|
327
|
+
);
|
|
328
|
+
yXmlElement.insert(0, node.initialValue.slice(0));
|
|
329
|
+
node.value = yXmlElement;
|
|
330
|
+
return __spreadProps(__spreadValues({}, acc), { [key]: node });
|
|
331
|
+
}
|
|
332
|
+
if (node instanceof CrdtYjsXmlFragment) {
|
|
333
|
+
const yXmlFragment = this.value.get(
|
|
334
|
+
key,
|
|
335
|
+
YXmlFragment2
|
|
336
|
+
);
|
|
337
|
+
yXmlFragment.insert(0, node.initialValue.slice(0));
|
|
338
|
+
node.value = yXmlFragment;
|
|
339
|
+
return __spreadProps(__spreadValues({}, acc), { [key]: node });
|
|
340
|
+
}
|
|
341
|
+
if (node instanceof CrdtYjsXmlText) {
|
|
342
|
+
const yXmlText = this.value.get(key, YXmlText2);
|
|
343
|
+
yXmlText.insert(0, node.initialValue);
|
|
344
|
+
node.value = yXmlText;
|
|
345
|
+
return __spreadProps(__spreadValues({}, acc), { [key]: node });
|
|
346
|
+
}
|
|
347
|
+
return acc;
|
|
348
|
+
}, {});
|
|
55
349
|
}
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
}
|
|
59
|
-
applyUpdate(update, origin) {
|
|
350
|
+
applyEncodedState(params) {
|
|
351
|
+
const { origin, update } = params;
|
|
60
352
|
if (update === null || typeof update === "undefined")
|
|
61
353
|
return this;
|
|
62
354
|
const uint8Update = typeof update === "string" ? toUint8Array(update) : update;
|
|
63
355
|
applyUpdate(this.value, uint8Update, origin);
|
|
64
356
|
return this;
|
|
65
357
|
}
|
|
358
|
+
batchApplyEncodedState(updates) {
|
|
359
|
+
const params = updates.reduce(
|
|
360
|
+
(acc, update) => {
|
|
361
|
+
if (!update)
|
|
362
|
+
return acc;
|
|
363
|
+
if (typeof update === "string") {
|
|
364
|
+
acc.push({ update });
|
|
365
|
+
return acc;
|
|
366
|
+
}
|
|
367
|
+
if (!!update && typeof update === "object") {
|
|
368
|
+
acc.push(update);
|
|
369
|
+
return acc;
|
|
370
|
+
}
|
|
371
|
+
return acc;
|
|
372
|
+
},
|
|
373
|
+
[]
|
|
374
|
+
);
|
|
375
|
+
return params.reduce(
|
|
376
|
+
(doc2, update) => doc2.applyEncodedState(update),
|
|
377
|
+
this
|
|
378
|
+
);
|
|
379
|
+
}
|
|
66
380
|
canRedo() {
|
|
67
381
|
var _a;
|
|
68
382
|
return !!((_a = this._undoManager) == null ? void 0 : _a.canRedo());
|
|
@@ -76,111 +390,141 @@ var YjsDoc = class _YjsDoc {
|
|
|
76
390
|
(_a = this._undoManager) == null ? void 0 : _a.destroy();
|
|
77
391
|
this.value.destroy();
|
|
78
392
|
}
|
|
79
|
-
encodeStateAsUpdate() {
|
|
80
|
-
return fromUint8Array(encodeStateAsUpdate(this.value));
|
|
81
|
-
}
|
|
82
393
|
get(key) {
|
|
83
|
-
|
|
394
|
+
if (typeof key === "undefined")
|
|
395
|
+
return this._storage;
|
|
396
|
+
return this._storage[key];
|
|
84
397
|
}
|
|
85
|
-
|
|
86
|
-
return
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
398
|
+
getEncodedState() {
|
|
399
|
+
return fromUint8Array(encodeStateAsUpdate(this.value));
|
|
400
|
+
}
|
|
401
|
+
isEmpty() {
|
|
402
|
+
return !this.value.share.size;
|
|
90
403
|
}
|
|
91
404
|
redo() {
|
|
92
405
|
var _a;
|
|
93
406
|
(_a = this._undoManager) == null ? void 0 : _a.redo();
|
|
407
|
+
return this;
|
|
94
408
|
}
|
|
95
|
-
subscribe(
|
|
96
|
-
const
|
|
97
|
-
|
|
409
|
+
subscribe(listener) {
|
|
410
|
+
const fn = (update, origin, doc2) => {
|
|
411
|
+
listener({
|
|
412
|
+
doc: this,
|
|
413
|
+
local: origin === null || typeof origin === "undefined",
|
|
414
|
+
origin: origin ? String(origin) : null,
|
|
415
|
+
update: fromUint8Array(update)
|
|
416
|
+
});
|
|
417
|
+
};
|
|
418
|
+
this.value.on("update", fn);
|
|
98
419
|
return () => {
|
|
99
|
-
this.value.off("update",
|
|
420
|
+
this.value.off("update", fn);
|
|
100
421
|
};
|
|
101
422
|
}
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
trackOrigins(options) {
|
|
106
|
-
const { captureTimeout, trackedOrigins } = options;
|
|
107
|
-
if (this._undoManager) {
|
|
423
|
+
track() {
|
|
424
|
+
var _a;
|
|
425
|
+
if (this._undoManager)
|
|
108
426
|
this._undoManager.destroy();
|
|
427
|
+
const sharedTypes = Object.values(this._storage).reduce((acc, type) => {
|
|
428
|
+
if (type instanceof CrdtYjsArray || type instanceof CrdtYjsMap || type instanceof CrdtYjsObject || type instanceof CrdtYjsText || type instanceof CrdtYjsXmlElement || type instanceof CrdtYjsXmlFragment || type instanceof CrdtYjsXmlText) {
|
|
429
|
+
acc.push(type.value);
|
|
430
|
+
}
|
|
431
|
+
return acc;
|
|
432
|
+
}, []);
|
|
433
|
+
if (!sharedTypes.length) {
|
|
434
|
+
(_a = this._undoManager) == null ? void 0 : _a.destroy();
|
|
435
|
+
this._undoManager = null;
|
|
436
|
+
return this;
|
|
109
437
|
}
|
|
110
|
-
this._undoManager = new UndoManager(
|
|
111
|
-
|
|
112
|
-
trackedOrigins: trackedOrigins ? new Set(trackedOrigins) : void 0
|
|
113
|
-
});
|
|
114
|
-
}
|
|
115
|
-
transact(fn, origin) {
|
|
116
|
-
this.value.transact((tx) => {
|
|
117
|
-
fn(tx);
|
|
118
|
-
}, origin);
|
|
438
|
+
this._undoManager = new UndoManager(sharedTypes, { captureTimeout: 0 });
|
|
439
|
+
return this;
|
|
119
440
|
}
|
|
120
|
-
|
|
121
|
-
|
|
441
|
+
transact(fn) {
|
|
442
|
+
this.value.transact(fn);
|
|
443
|
+
return this;
|
|
122
444
|
}
|
|
123
|
-
|
|
124
|
-
return
|
|
445
|
+
toJson() {
|
|
446
|
+
return Object.entries(this._storage).reduce(
|
|
447
|
+
(acc, [key, value]) => __spreadProps(__spreadValues({}, acc), { [key]: value.toJson() }),
|
|
448
|
+
{}
|
|
449
|
+
);
|
|
125
450
|
}
|
|
126
451
|
undo() {
|
|
127
452
|
var _a;
|
|
128
453
|
(_a = this._undoManager) == null ? void 0 : _a.undo();
|
|
454
|
+
return this;
|
|
129
455
|
}
|
|
130
456
|
};
|
|
131
|
-
var doc = (value) => {
|
|
132
|
-
return new YjsDoc(value);
|
|
133
|
-
};
|
|
134
457
|
|
|
135
|
-
// src/
|
|
136
|
-
import {
|
|
458
|
+
// src/doc/CrdtYjsDocFactory.ts
|
|
459
|
+
import { AbstractCrdtDocFactory } from "@pluv/crdt";
|
|
460
|
+
|
|
461
|
+
// src/map/map.ts
|
|
137
462
|
var map = (value = []) => {
|
|
138
|
-
return new
|
|
463
|
+
return new CrdtYjsMap(value);
|
|
139
464
|
};
|
|
140
465
|
|
|
141
|
-
// src/object.ts
|
|
142
|
-
import { Map as YMap3 } from "yjs";
|
|
466
|
+
// src/object/object.ts
|
|
143
467
|
var object = (value) => {
|
|
144
|
-
return new
|
|
145
|
-
};
|
|
146
|
-
|
|
147
|
-
// src/text.ts
|
|
148
|
-
import { Text as YText } from "yjs";
|
|
149
|
-
var text = (value = "") => {
|
|
150
|
-
return new YText(value);
|
|
151
|
-
};
|
|
152
|
-
|
|
153
|
-
// src/xmlElement.ts
|
|
154
|
-
import { XmlElement as YXmlElement } from "yjs";
|
|
155
|
-
var xmlElement = (name, params = {}) => {
|
|
156
|
-
const { children = [] } = params;
|
|
157
|
-
const element = new YXmlElement(name);
|
|
158
|
-
element.push(children.slice());
|
|
159
|
-
return element;
|
|
468
|
+
return new CrdtYjsObject(value);
|
|
160
469
|
};
|
|
161
470
|
|
|
162
|
-
// src/
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
471
|
+
// src/doc/CrdtYjsDocFactory.ts
|
|
472
|
+
var CrdtYjsDocFactory = class _CrdtYjsDocFactory extends AbstractCrdtDocFactory {
|
|
473
|
+
constructor(initialStorage = () => ({})) {
|
|
474
|
+
super();
|
|
475
|
+
this._initialStorage = initialStorage;
|
|
476
|
+
}
|
|
477
|
+
getEmpty() {
|
|
478
|
+
return new CrdtYjsDoc();
|
|
479
|
+
}
|
|
480
|
+
getFactory(initialStorage) {
|
|
481
|
+
return new _CrdtYjsDocFactory(
|
|
482
|
+
initialStorage != null ? initialStorage : this._initialStorage
|
|
483
|
+
);
|
|
484
|
+
}
|
|
485
|
+
getFresh() {
|
|
486
|
+
const storage = this._initialStorage();
|
|
487
|
+
return new CrdtYjsDoc(
|
|
488
|
+
Object.entries(storage).reduce((acc, [key, node]) => {
|
|
489
|
+
if (node instanceof CrdtYjsArray) {
|
|
490
|
+
return __spreadProps(__spreadValues({}, acc), { [key]: new CrdtYjsArray([]) });
|
|
491
|
+
}
|
|
492
|
+
if (node instanceof CrdtYjsMap) {
|
|
493
|
+
return __spreadProps(__spreadValues({}, acc), { [key]: new CrdtYjsMap([]) });
|
|
494
|
+
}
|
|
495
|
+
if (node instanceof CrdtYjsObject) {
|
|
496
|
+
return __spreadProps(__spreadValues({}, acc), { [key]: new CrdtYjsObject({}) });
|
|
497
|
+
}
|
|
498
|
+
if (node instanceof CrdtYjsText) {
|
|
499
|
+
return __spreadProps(__spreadValues({}, acc), { [key]: new CrdtYjsText("") });
|
|
500
|
+
}
|
|
501
|
+
if (node instanceof CrdtYjsXmlElement) {
|
|
502
|
+
return __spreadProps(__spreadValues({}, acc), {
|
|
503
|
+
[key]: new CrdtYjsXmlElement(node.name, [])
|
|
504
|
+
});
|
|
505
|
+
}
|
|
506
|
+
if (node instanceof CrdtYjsXmlFragment) {
|
|
507
|
+
return __spreadProps(__spreadValues({}, acc), { [key]: new CrdtYjsXmlFragment([]) });
|
|
508
|
+
}
|
|
509
|
+
if (node instanceof CrdtYjsXmlText) {
|
|
510
|
+
return __spreadProps(__spreadValues({}, acc), { [key]: new CrdtYjsXmlText("") });
|
|
511
|
+
}
|
|
512
|
+
return acc;
|
|
513
|
+
}, {})
|
|
514
|
+
);
|
|
515
|
+
}
|
|
516
|
+
getInitialized(initialStorage) {
|
|
517
|
+
var _a;
|
|
518
|
+
return new CrdtYjsDoc(
|
|
519
|
+
(_a = initialStorage == null ? void 0 : initialStorage()) != null ? _a : this._initialStorage()
|
|
520
|
+
);
|
|
521
|
+
}
|
|
169
522
|
};
|
|
170
523
|
|
|
171
|
-
// src/
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
return new YXmlText(value);
|
|
524
|
+
// src/doc/doc.ts
|
|
525
|
+
var doc = (value = () => ({})) => {
|
|
526
|
+
return new CrdtYjsDocFactory(value);
|
|
175
527
|
};
|
|
176
528
|
export {
|
|
177
|
-
|
|
178
|
-
array,
|
|
179
|
-
doc,
|
|
180
|
-
map,
|
|
181
|
-
object,
|
|
182
|
-
text,
|
|
183
|
-
xmlElement,
|
|
184
|
-
xmlFragment,
|
|
185
|
-
xmlText
|
|
529
|
+
yjs_exports as yjs
|
|
186
530
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@pluv/crdt-yjs",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.16.1",
|
|
4
4
|
"description": "yjs for @pluv/io",
|
|
5
5
|
"author": "leedavidcs",
|
|
6
6
|
"license": "MIT",
|
|
@@ -17,19 +17,20 @@
|
|
|
17
17
|
"access": "public"
|
|
18
18
|
},
|
|
19
19
|
"dependencies": {
|
|
20
|
-
"js-base64": "^3.7.
|
|
21
|
-
"@pluv/
|
|
20
|
+
"js-base64": "^3.7.6",
|
|
21
|
+
"@pluv/crdt": "^0.16.1",
|
|
22
|
+
"@pluv/types": "^0.16.1"
|
|
22
23
|
},
|
|
23
24
|
"peerDependencies": {
|
|
24
25
|
"yjs": "^13.0.0"
|
|
25
26
|
},
|
|
26
27
|
"devDependencies": {
|
|
27
28
|
"eslint": "^8.56.0",
|
|
28
|
-
"tsup": "^8.0.
|
|
29
|
-
"typescript": "^5.
|
|
30
|
-
"yjs": "^13.6.
|
|
31
|
-
"@pluv/tsconfig": "^0.
|
|
32
|
-
"eslint-config-pluv": "^0.
|
|
29
|
+
"tsup": "^8.0.2",
|
|
30
|
+
"typescript": "^5.3.3",
|
|
31
|
+
"yjs": "^13.6.12",
|
|
32
|
+
"@pluv/tsconfig": "^0.16.1",
|
|
33
|
+
"eslint-config-pluv": "^0.16.1"
|
|
33
34
|
},
|
|
34
35
|
"scripts": {
|
|
35
36
|
"build": "tsup src/index.ts --format esm,cjs --dts",
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import type { InferCrdtStorageJson } from "@pluv/crdt";
|
|
2
|
+
import { AbstractCrdtType } from "@pluv/crdt";
|
|
3
|
+
import { Array as YArray } from "yjs";
|
|
4
|
+
import { toYjsValue } from "../shared";
|
|
5
|
+
import type { InferYjsJson, InferYjsType } from "../types";
|
|
6
|
+
|
|
7
|
+
export class CrdtYjsArray<T extends unknown> extends AbstractCrdtType<
|
|
8
|
+
YArray<InferYjsType<T>>,
|
|
9
|
+
InferYjsJson<T>[]
|
|
10
|
+
> {
|
|
11
|
+
public readonly initialValue:
|
|
12
|
+
| InferYjsType<T>[]
|
|
13
|
+
| readonly InferYjsType<T>[];
|
|
14
|
+
public value: YArray<InferYjsType<T>>;
|
|
15
|
+
|
|
16
|
+
constructor(value: T[] | readonly T[] = []) {
|
|
17
|
+
super();
|
|
18
|
+
|
|
19
|
+
const initialValue = value.map((item) => toYjsValue(item));
|
|
20
|
+
|
|
21
|
+
this.initialValue = initialValue;
|
|
22
|
+
this.value = new YArray<InferYjsType<T>>();
|
|
23
|
+
this.push(...value);
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
public get length(): number {
|
|
27
|
+
return this.value.length;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
public delete(index: number, length: number = 1): this {
|
|
31
|
+
this.value.delete(index, length);
|
|
32
|
+
|
|
33
|
+
return this;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
public insert(index: number, ...items: T[]): this {
|
|
37
|
+
const converted = items.map((item) => toYjsValue(item));
|
|
38
|
+
|
|
39
|
+
this.value.insert(index, converted);
|
|
40
|
+
|
|
41
|
+
return this;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
public push(...items: T[]): this {
|
|
45
|
+
return this.insert(this.length, ...items);
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
public unshift(...items: T[]): this {
|
|
49
|
+
return this.insert(0, ...items);
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
public toJson(): InferCrdtStorageJson<InferYjsJson<T>>[] {
|
|
53
|
+
return this.value.toJSON();
|
|
54
|
+
}
|
|
55
|
+
}
|