@pluv/crdt-yjs 1.0.0 → 1.0.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.
@@ -1,5 +1,5 @@
1
1
 
2
- > @pluv/crdt-yjs@1.0.0 build /home/runner/work/pluv/pluv/packages/crdt-yjs
2
+ > @pluv/crdt-yjs@1.0.1 build /home/runner/work/pluv/pluv/packages/crdt-yjs
3
3
  > tsup src/index.ts --format esm,cjs --dts
4
4
 
5
5
  CLI Building entry: src/index.ts
@@ -8,11 +8,11 @@
8
8
  CLI Target: es6
9
9
  ESM Build start
10
10
  CJS Build start
11
- CJS dist/index.js 11.96 KB
12
- CJS ⚡️ Build success in 110ms
13
- ESM dist/index.mjs 11.15 KB
14
- ESM ⚡️ Build success in 110ms
11
+ ESM dist/index.mjs 11.13 KB
12
+ ESM ⚡️ Build success in 95ms
13
+ CJS dist/index.js 12.00 KB
14
+ CJS ⚡️ Build success in 95ms
15
15
  DTS Build start
16
- DTS ⚡️ Build success in 2221ms
17
- DTS dist/index.d.mts 3.95 KB
18
- DTS dist/index.d.ts 3.95 KB
16
+ DTS ⚡️ Build success in 2852ms
17
+ DTS dist/index.d.mts 3.96 KB
18
+ DTS dist/index.d.ts 3.96 KB
package/CHANGELOG.md CHANGED
@@ -1,5 +1,12 @@
1
1
  # @pluv/crdt-yjs
2
2
 
3
+ ## 1.0.1
4
+
5
+ ### Patch Changes
6
+
7
+ - @pluv/crdt@1.0.1
8
+ - @pluv/types@1.0.1
9
+
3
10
  ## 1.0.0
4
11
 
5
12
  ### Major Changes
package/dist/index.d.mts CHANGED
@@ -22,6 +22,7 @@ declare class CrdtYjsDoc<TStorage extends Record<string, YjsType<any, any>>> ext
22
22
  get<TKey extends keyof TStorage>(type: TKey): TStorage[TKey];
23
23
  getEncodedState(): string;
24
24
  isEmpty(): boolean;
25
+ rebuildStorage(reference: TStorage): this;
25
26
  redo(): this;
26
27
  subscribe(listener: (params: DocSubscribeCallbackParams<TStorage>) => void): () => void;
27
28
  track(): this;
@@ -36,7 +37,6 @@ declare class CrdtYjsDocFactory<TStorage extends Record<string, YjsType<any, any
36
37
  constructor(initialStorage?: () => TStorage);
37
38
  getEmpty(): CrdtYjsDoc<TStorage>;
38
39
  getFactory(initialStorage?: (() => TStorage) | undefined): CrdtYjsDocFactory<TStorage>;
39
- getFresh(): CrdtYjsDoc<TStorage>;
40
40
  getInitialized(initialStorage?: () => TStorage): CrdtYjsDoc<TStorage>;
41
41
  }
42
42
 
package/dist/index.d.ts CHANGED
@@ -22,6 +22,7 @@ declare class CrdtYjsDoc<TStorage extends Record<string, YjsType<any, any>>> ext
22
22
  get<TKey extends keyof TStorage>(type: TKey): TStorage[TKey];
23
23
  getEncodedState(): string;
24
24
  isEmpty(): boolean;
25
+ rebuildStorage(reference: TStorage): this;
25
26
  redo(): this;
26
27
  subscribe(listener: (params: DocSubscribeCallbackParams<TStorage>) => void): () => void;
27
28
  track(): this;
@@ -36,7 +37,6 @@ declare class CrdtYjsDocFactory<TStorage extends Record<string, YjsType<any, any
36
37
  constructor(initialStorage?: () => TStorage);
37
38
  getEmpty(): CrdtYjsDoc<TStorage>;
38
39
  getFactory(initialStorage?: (() => TStorage) | undefined): CrdtYjsDocFactory<TStorage>;
39
- getFresh(): CrdtYjsDoc<TStorage>;
40
40
  getInitialized(initialStorage?: () => TStorage): CrdtYjsDoc<TStorage>;
41
41
  }
42
42
 
package/dist/index.js CHANGED
@@ -76,7 +76,7 @@ var array = (value = []) => {
76
76
  // src/doc/CrdtYjsDoc.ts
77
77
  var import_crdt = require("@pluv/crdt");
78
78
  var import_js_base64 = require("js-base64");
79
- var import_yjs7 = require("yjs");
79
+ var import_yjs8 = require("yjs");
80
80
 
81
81
  // src/map/YjsMap.ts
82
82
  var import_yjs2 = require("yjs");
@@ -127,9 +127,22 @@ var YjsXmlElement = class extends import_yjs5.XmlElement {
127
127
  }
128
128
  };
129
129
 
130
- // src/xmlText/YjsXmlText.ts
130
+ // src/xmlFragment/YjsXmlFragment.ts
131
131
  var import_yjs6 = require("yjs");
132
- var YjsXmlText = class extends import_yjs6.XmlText {
132
+ var YjsXmlFragment = class extends import_yjs6.XmlFragment {
133
+ constructor(value) {
134
+ super();
135
+ if (!!value.length) this.insert(0, value);
136
+ this.initialValue = value;
137
+ }
138
+ __pluvType() {
139
+ return {};
140
+ }
141
+ };
142
+
143
+ // src/xmlText/YjsXmlText.ts
144
+ var import_yjs7 = require("yjs");
145
+ var YjsXmlText = class extends import_yjs7.XmlText {
133
146
  constructor(value = "") {
134
147
  super();
135
148
  this.initialValue = value;
@@ -144,7 +157,7 @@ var MERGE_INTERVAL_MS = 1e3;
144
157
  var CrdtYjsDoc = class extends import_crdt.AbstractCrdtDoc {
145
158
  constructor(value = {}) {
146
159
  super();
147
- this.value = new import_yjs7.Doc();
160
+ this.value = new import_yjs8.Doc();
148
161
  this._undoManager = null;
149
162
  this._storage = Object.entries(value).reduce((acc, [key, node]) => {
150
163
  var _a, _b, _c, _d, _e, _f, _g, _h;
@@ -174,16 +187,20 @@ var CrdtYjsDoc = class extends import_crdt.AbstractCrdtDoc {
174
187
  }
175
188
  if (node instanceof YjsXmlElement) {
176
189
  const yXmlElement = this.value.getXmlElement(key);
177
- if (!!((_e = node.initialValue) == null ? void 0 : _e.length)) yXmlElement.insert(0, (_f = node.initialValue) == null ? void 0 : _f.slice(0));
190
+ if (!!((_e = node.initialValue) == null ? void 0 : _e.length)) {
191
+ yXmlElement.insert(0, (_f = node.initialValue) == null ? void 0 : _f.slice(0));
192
+ }
178
193
  return __spreadProps(__spreadValues({}, acc), { [key]: yXmlElement });
179
194
  }
180
- if (node instanceof import_yjs7.XmlFragment) {
195
+ if (node instanceof YjsXmlFragment) {
181
196
  const yXmlFragment = this.value.getXmlFragment(key);
182
- if (!!((_g = node.initialValue) == null ? void 0 : _g.length)) yXmlFragment.insert(0, (_h = node.initialValue) == null ? void 0 : _h.slice(0));
197
+ if (!!((_g = node.initialValue) == null ? void 0 : _g.length)) {
198
+ yXmlFragment.insert(0, (_h = node.initialValue) == null ? void 0 : _h.slice(0));
199
+ }
183
200
  return __spreadProps(__spreadValues({}, acc), { [key]: yXmlFragment });
184
201
  }
185
202
  if (node instanceof YjsXmlText) {
186
- const yXmlText = this.value.get(key, import_yjs7.XmlText);
203
+ const yXmlText = this.value.get(key, import_yjs8.XmlText);
187
204
  return __spreadProps(__spreadValues({}, acc), { [key]: yXmlText });
188
205
  }
189
206
  return acc;
@@ -193,7 +210,7 @@ var CrdtYjsDoc = class extends import_crdt.AbstractCrdtDoc {
193
210
  const { origin, update } = params;
194
211
  if (update === null || typeof update === "undefined") return this;
195
212
  const uint8Update = typeof update === "string" ? (0, import_js_base64.toUint8Array)(update) : update;
196
- (0, import_yjs7.applyUpdate)(this.value, uint8Update, origin);
213
+ (0, import_yjs8.applyUpdate)(this.value, uint8Update, origin);
197
214
  return this;
198
215
  }
199
216
  batchApplyEncodedState(params) {
@@ -213,7 +230,7 @@ var CrdtYjsDoc = class extends import_crdt.AbstractCrdtDoc {
213
230
  return acc;
214
231
  }, []);
215
232
  if (!filtered.length) return this;
216
- const merged = (0, import_yjs7.mergeUpdates)(filtered);
233
+ const merged = (0, import_yjs8.mergeUpdates)(filtered);
217
234
  return this.applyEncodedState({ origin, update: merged });
218
235
  }
219
236
  canRedo() {
@@ -234,11 +251,32 @@ var CrdtYjsDoc = class extends import_crdt.AbstractCrdtDoc {
234
251
  return this._storage[type];
235
252
  }
236
253
  getEncodedState() {
237
- return (0, import_js_base64.fromUint8Array)((0, import_yjs7.encodeStateAsUpdate)(this.value));
254
+ return (0, import_js_base64.fromUint8Array)((0, import_yjs8.encodeStateAsUpdate)(this.value));
238
255
  }
239
256
  isEmpty() {
240
257
  return !this.value.share.size;
241
258
  }
259
+ rebuildStorage(reference) {
260
+ const isBuilt = !!Object.keys(this._storage).length;
261
+ if (isBuilt) {
262
+ console.warn("Attempted to rebuild storage multiple times");
263
+ return this;
264
+ }
265
+ this._storage = Object.entries(reference).reduce((acc, [key, node]) => {
266
+ if (node instanceof import_yjs8.Array) return __spreadProps(__spreadValues({}, acc), { [key]: this.value.getArray(key) });
267
+ if (node instanceof import_yjs8.Map) return __spreadProps(__spreadValues({}, acc), { [key]: this.value.getMap(key) });
268
+ if (node instanceof import_yjs8.Text) return __spreadProps(__spreadValues({}, acc), { [key]: this.value.getText(key) });
269
+ if (node instanceof import_yjs8.XmlElement) {
270
+ return __spreadProps(__spreadValues({}, acc), { [key]: this.value.getXmlElement(key) });
271
+ }
272
+ if (node instanceof import_yjs8.XmlFragment) {
273
+ return __spreadProps(__spreadValues({}, acc), { [key]: this.value.getXmlFragment(key) });
274
+ }
275
+ if (node instanceof import_yjs8.XmlText) return __spreadProps(__spreadValues({}, acc), { [key]: this.value.get(key, import_yjs8.XmlText) });
276
+ return acc;
277
+ }, {});
278
+ return this.track();
279
+ }
242
280
  redo() {
243
281
  var _a;
244
282
  (_a = this._undoManager) == null ? void 0 : _a.redo();
@@ -261,18 +299,21 @@ var CrdtYjsDoc = class extends import_crdt.AbstractCrdtDoc {
261
299
  track() {
262
300
  var _a;
263
301
  if (this._undoManager) this._undoManager.destroy();
264
- const sharedTypes = Object.values(this._storage).reduce((acc, type) => {
265
- if (type instanceof import_yjs7.Array || type instanceof import_yjs7.Map || type instanceof import_yjs7.Text || type instanceof import_yjs7.XmlElement || type instanceof import_yjs7.XmlFragment || type instanceof import_yjs7.XmlText) {
266
- acc.push(type);
267
- }
268
- return acc;
269
- }, []);
302
+ const sharedTypes = Object.values(this._storage).reduce(
303
+ (acc, type) => {
304
+ if (type instanceof import_yjs8.Array || type instanceof import_yjs8.Map || type instanceof import_yjs8.Text || type instanceof import_yjs8.XmlElement || type instanceof import_yjs8.XmlFragment || type instanceof import_yjs8.XmlText) {
305
+ acc.push(type);
306
+ }
307
+ return acc;
308
+ },
309
+ []
310
+ );
270
311
  if (!sharedTypes.length) {
271
312
  (_a = this._undoManager) == null ? void 0 : _a.destroy();
272
313
  this._undoManager = null;
273
314
  return this;
274
315
  }
275
- this._undoManager = new import_yjs7.UndoManager(sharedTypes, { captureTimeout: MERGE_INTERVAL_MS });
316
+ this._undoManager = new import_yjs8.UndoManager(sharedTypes, { captureTimeout: MERGE_INTERVAL_MS });
276
317
  return this;
277
318
  }
278
319
  transact(fn) {
@@ -295,7 +336,27 @@ var CrdtYjsDoc = class extends import_crdt.AbstractCrdtDoc {
295
336
 
296
337
  // src/doc/CrdtYjsDocFactory.ts
297
338
  var import_crdt2 = require("@pluv/crdt");
298
- var import_yjs9 = require("yjs");
339
+ var CrdtYjsDocFactory = class _CrdtYjsDocFactory extends import_crdt2.AbstractCrdtDocFactory {
340
+ constructor(initialStorage = () => ({})) {
341
+ super();
342
+ this._initialStorage = initialStorage;
343
+ }
344
+ getEmpty() {
345
+ return new CrdtYjsDoc();
346
+ }
347
+ getFactory(initialStorage) {
348
+ return new _CrdtYjsDocFactory(initialStorage != null ? initialStorage : this._initialStorage);
349
+ }
350
+ getInitialized(initialStorage) {
351
+ var _a;
352
+ return new CrdtYjsDoc((_a = initialStorage == null ? void 0 : initialStorage()) != null ? _a : this._initialStorage());
353
+ }
354
+ };
355
+
356
+ // src/doc/doc.ts
357
+ var doc = (value = () => ({})) => {
358
+ return new CrdtYjsDocFactory(value);
359
+ };
299
360
 
300
361
  // src/map/map.ts
301
362
  var map = (value = []) => {
@@ -317,19 +378,6 @@ var xmlElement = (name, children) => {
317
378
  return new YjsXmlElement(name, children);
318
379
  };
319
380
 
320
- // src/xmlFragment/YjsXmlFragment.ts
321
- var import_yjs8 = require("yjs");
322
- var YjsXmlFragment = class extends import_yjs8.XmlFragment {
323
- constructor(value) {
324
- super();
325
- if (!!value.length) this.insert(0, value);
326
- this.initialValue = value;
327
- }
328
- __pluvType() {
329
- return {};
330
- }
331
- };
332
-
333
381
  // src/xmlFragment/xmlFragment.ts
334
382
  var xmlFragment = (children) => {
335
383
  return new YjsXmlFragment(children);
@@ -340,58 +388,6 @@ var xmlText = () => {
340
388
  return new YjsXmlText();
341
389
  };
342
390
 
343
- // src/doc/CrdtYjsDocFactory.ts
344
- var CrdtYjsDocFactory = class _CrdtYjsDocFactory extends import_crdt2.AbstractCrdtDocFactory {
345
- constructor(initialStorage = () => ({})) {
346
- super();
347
- this._initialStorage = initialStorage;
348
- }
349
- getEmpty() {
350
- return new CrdtYjsDoc();
351
- }
352
- getFactory(initialStorage) {
353
- return new _CrdtYjsDocFactory(initialStorage != null ? initialStorage : this._initialStorage);
354
- }
355
- getFresh() {
356
- const storage = this._initialStorage();
357
- return new CrdtYjsDoc(
358
- Object.entries(storage).reduce((acc, [key, node]) => {
359
- if (node instanceof YjsArray) {
360
- return __spreadProps(__spreadValues({}, acc), { [key]: array() });
361
- }
362
- if (node instanceof YjsMap) {
363
- return __spreadProps(__spreadValues({}, acc), { [key]: map() });
364
- }
365
- if (node instanceof YjsObject) {
366
- return __spreadProps(__spreadValues({}, acc), { [key]: object() });
367
- }
368
- if (node instanceof YjsText) {
369
- return __spreadProps(__spreadValues({}, acc), { [key]: text() });
370
- }
371
- if (node instanceof YjsXmlElement) {
372
- return __spreadProps(__spreadValues({}, acc), { [key]: xmlElement(node.nodeName, []) });
373
- }
374
- if (node instanceof import_yjs9.XmlFragment) {
375
- return __spreadProps(__spreadValues({}, acc), { [key]: xmlFragment([]) });
376
- }
377
- if (node instanceof YjsXmlText) {
378
- return __spreadProps(__spreadValues({}, acc), { [key]: xmlText() });
379
- }
380
- return acc;
381
- }, {})
382
- );
383
- }
384
- getInitialized(initialStorage) {
385
- var _a;
386
- return new CrdtYjsDoc((_a = initialStorage == null ? void 0 : initialStorage()) != null ? _a : this._initialStorage());
387
- }
388
- };
389
-
390
- // src/doc/doc.ts
391
- var doc = (value = () => ({})) => {
392
- return new CrdtYjsDocFactory(value);
393
- };
394
-
395
391
  // src/yjs.ts
396
392
  var kind = "yjs";
397
393
  // Annotate the CommonJS export names for ESM import in node:
package/dist/index.mjs CHANGED
@@ -64,7 +64,7 @@ import {
64
64
  Map as YMap3,
65
65
  Text as YText2,
66
66
  XmlElement as YXmlElement2,
67
- XmlFragment as YXmlFragment,
67
+ XmlFragment as YXmlFragment2,
68
68
  XmlText as YXmlText2,
69
69
  applyUpdate,
70
70
  encodeStateAsUpdate,
@@ -120,6 +120,19 @@ var YjsXmlElement = class extends YXmlElement {
120
120
  }
121
121
  };
122
122
 
123
+ // src/xmlFragment/YjsXmlFragment.ts
124
+ import { XmlFragment as YXmlFragment } from "yjs";
125
+ var YjsXmlFragment = class extends YXmlFragment {
126
+ constructor(value) {
127
+ super();
128
+ if (!!value.length) this.insert(0, value);
129
+ this.initialValue = value;
130
+ }
131
+ __pluvType() {
132
+ return {};
133
+ }
134
+ };
135
+
123
136
  // src/xmlText/YjsXmlText.ts
124
137
  import { XmlText as YXmlText } from "yjs";
125
138
  var YjsXmlText = class extends YXmlText {
@@ -167,12 +180,16 @@ var CrdtYjsDoc = class extends AbstractCrdtDoc {
167
180
  }
168
181
  if (node instanceof YjsXmlElement) {
169
182
  const yXmlElement = this.value.getXmlElement(key);
170
- if (!!((_e = node.initialValue) == null ? void 0 : _e.length)) yXmlElement.insert(0, (_f = node.initialValue) == null ? void 0 : _f.slice(0));
183
+ if (!!((_e = node.initialValue) == null ? void 0 : _e.length)) {
184
+ yXmlElement.insert(0, (_f = node.initialValue) == null ? void 0 : _f.slice(0));
185
+ }
171
186
  return __spreadProps(__spreadValues({}, acc), { [key]: yXmlElement });
172
187
  }
173
- if (node instanceof YXmlFragment) {
188
+ if (node instanceof YjsXmlFragment) {
174
189
  const yXmlFragment = this.value.getXmlFragment(key);
175
- if (!!((_g = node.initialValue) == null ? void 0 : _g.length)) yXmlFragment.insert(0, (_h = node.initialValue) == null ? void 0 : _h.slice(0));
190
+ if (!!((_g = node.initialValue) == null ? void 0 : _g.length)) {
191
+ yXmlFragment.insert(0, (_h = node.initialValue) == null ? void 0 : _h.slice(0));
192
+ }
176
193
  return __spreadProps(__spreadValues({}, acc), { [key]: yXmlFragment });
177
194
  }
178
195
  if (node instanceof YjsXmlText) {
@@ -232,6 +249,27 @@ var CrdtYjsDoc = class extends AbstractCrdtDoc {
232
249
  isEmpty() {
233
250
  return !this.value.share.size;
234
251
  }
252
+ rebuildStorage(reference) {
253
+ const isBuilt = !!Object.keys(this._storage).length;
254
+ if (isBuilt) {
255
+ console.warn("Attempted to rebuild storage multiple times");
256
+ return this;
257
+ }
258
+ this._storage = Object.entries(reference).reduce((acc, [key, node]) => {
259
+ if (node instanceof YArray2) return __spreadProps(__spreadValues({}, acc), { [key]: this.value.getArray(key) });
260
+ if (node instanceof YMap3) return __spreadProps(__spreadValues({}, acc), { [key]: this.value.getMap(key) });
261
+ if (node instanceof YText2) return __spreadProps(__spreadValues({}, acc), { [key]: this.value.getText(key) });
262
+ if (node instanceof YXmlElement2) {
263
+ return __spreadProps(__spreadValues({}, acc), { [key]: this.value.getXmlElement(key) });
264
+ }
265
+ if (node instanceof YXmlFragment2) {
266
+ return __spreadProps(__spreadValues({}, acc), { [key]: this.value.getXmlFragment(key) });
267
+ }
268
+ if (node instanceof YXmlText2) return __spreadProps(__spreadValues({}, acc), { [key]: this.value.get(key, YXmlText2) });
269
+ return acc;
270
+ }, {});
271
+ return this.track();
272
+ }
235
273
  redo() {
236
274
  var _a;
237
275
  (_a = this._undoManager) == null ? void 0 : _a.redo();
@@ -254,12 +292,15 @@ var CrdtYjsDoc = class extends AbstractCrdtDoc {
254
292
  track() {
255
293
  var _a;
256
294
  if (this._undoManager) this._undoManager.destroy();
257
- const sharedTypes = Object.values(this._storage).reduce((acc, type) => {
258
- if (type instanceof YArray2 || type instanceof YMap3 || type instanceof YText2 || type instanceof YXmlElement2 || type instanceof YXmlFragment || type instanceof YXmlText2) {
259
- acc.push(type);
260
- }
261
- return acc;
262
- }, []);
295
+ const sharedTypes = Object.values(this._storage).reduce(
296
+ (acc, type) => {
297
+ if (type instanceof YArray2 || type instanceof YMap3 || type instanceof YText2 || type instanceof YXmlElement2 || type instanceof YXmlFragment2 || type instanceof YXmlText2) {
298
+ acc.push(type);
299
+ }
300
+ return acc;
301
+ },
302
+ []
303
+ );
263
304
  if (!sharedTypes.length) {
264
305
  (_a = this._undoManager) == null ? void 0 : _a.destroy();
265
306
  this._undoManager = null;
@@ -288,7 +329,27 @@ var CrdtYjsDoc = class extends AbstractCrdtDoc {
288
329
 
289
330
  // src/doc/CrdtYjsDocFactory.ts
290
331
  import { AbstractCrdtDocFactory } from "@pluv/crdt";
291
- import { XmlFragment as YXmlFragment3 } from "yjs";
332
+ var CrdtYjsDocFactory = class _CrdtYjsDocFactory extends AbstractCrdtDocFactory {
333
+ constructor(initialStorage = () => ({})) {
334
+ super();
335
+ this._initialStorage = initialStorage;
336
+ }
337
+ getEmpty() {
338
+ return new CrdtYjsDoc();
339
+ }
340
+ getFactory(initialStorage) {
341
+ return new _CrdtYjsDocFactory(initialStorage != null ? initialStorage : this._initialStorage);
342
+ }
343
+ getInitialized(initialStorage) {
344
+ var _a;
345
+ return new CrdtYjsDoc((_a = initialStorage == null ? void 0 : initialStorage()) != null ? _a : this._initialStorage());
346
+ }
347
+ };
348
+
349
+ // src/doc/doc.ts
350
+ var doc = (value = () => ({})) => {
351
+ return new CrdtYjsDocFactory(value);
352
+ };
292
353
 
293
354
  // src/map/map.ts
294
355
  var map = (value = []) => {
@@ -310,19 +371,6 @@ var xmlElement = (name, children) => {
310
371
  return new YjsXmlElement(name, children);
311
372
  };
312
373
 
313
- // src/xmlFragment/YjsXmlFragment.ts
314
- import { XmlFragment as YXmlFragment2 } from "yjs";
315
- var YjsXmlFragment = class extends YXmlFragment2 {
316
- constructor(value) {
317
- super();
318
- if (!!value.length) this.insert(0, value);
319
- this.initialValue = value;
320
- }
321
- __pluvType() {
322
- return {};
323
- }
324
- };
325
-
326
374
  // src/xmlFragment/xmlFragment.ts
327
375
  var xmlFragment = (children) => {
328
376
  return new YjsXmlFragment(children);
@@ -333,58 +381,6 @@ var xmlText = () => {
333
381
  return new YjsXmlText();
334
382
  };
335
383
 
336
- // src/doc/CrdtYjsDocFactory.ts
337
- var CrdtYjsDocFactory = class _CrdtYjsDocFactory extends AbstractCrdtDocFactory {
338
- constructor(initialStorage = () => ({})) {
339
- super();
340
- this._initialStorage = initialStorage;
341
- }
342
- getEmpty() {
343
- return new CrdtYjsDoc();
344
- }
345
- getFactory(initialStorage) {
346
- return new _CrdtYjsDocFactory(initialStorage != null ? initialStorage : this._initialStorage);
347
- }
348
- getFresh() {
349
- const storage = this._initialStorage();
350
- return new CrdtYjsDoc(
351
- Object.entries(storage).reduce((acc, [key, node]) => {
352
- if (node instanceof YjsArray) {
353
- return __spreadProps(__spreadValues({}, acc), { [key]: array() });
354
- }
355
- if (node instanceof YjsMap) {
356
- return __spreadProps(__spreadValues({}, acc), { [key]: map() });
357
- }
358
- if (node instanceof YjsObject) {
359
- return __spreadProps(__spreadValues({}, acc), { [key]: object() });
360
- }
361
- if (node instanceof YjsText) {
362
- return __spreadProps(__spreadValues({}, acc), { [key]: text() });
363
- }
364
- if (node instanceof YjsXmlElement) {
365
- return __spreadProps(__spreadValues({}, acc), { [key]: xmlElement(node.nodeName, []) });
366
- }
367
- if (node instanceof YXmlFragment3) {
368
- return __spreadProps(__spreadValues({}, acc), { [key]: xmlFragment([]) });
369
- }
370
- if (node instanceof YjsXmlText) {
371
- return __spreadProps(__spreadValues({}, acc), { [key]: xmlText() });
372
- }
373
- return acc;
374
- }, {})
375
- );
376
- }
377
- getInitialized(initialStorage) {
378
- var _a;
379
- return new CrdtYjsDoc((_a = initialStorage == null ? void 0 : initialStorage()) != null ? _a : this._initialStorage());
380
- }
381
- };
382
-
383
- // src/doc/doc.ts
384
- var doc = (value = () => ({})) => {
385
- return new CrdtYjsDocFactory(value);
386
- };
387
-
388
384
  // src/yjs.ts
389
385
  var kind = "yjs";
390
386
  export {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@pluv/crdt-yjs",
3
- "version": "1.0.0",
3
+ "version": "1.0.1",
4
4
  "description": "yjs for @pluv/io",
5
5
  "author": "leedavidcs",
6
6
  "license": "MIT",
@@ -18,8 +18,8 @@
18
18
  },
19
19
  "dependencies": {
20
20
  "js-base64": "^3.7.7",
21
- "@pluv/crdt": "^1.0.0",
22
- "@pluv/types": "^1.0.0"
21
+ "@pluv/crdt": "^1.0.1",
22
+ "@pluv/types": "^1.0.1"
23
23
  },
24
24
  "peerDependencies": {
25
25
  "yjs": "^13.0.0"
@@ -29,8 +29,8 @@
29
29
  "tsup": "^8.4.0",
30
30
  "typescript": "^5.8.3",
31
31
  "yjs": "^13.6.26",
32
- "@pluv/tsconfig": "^1.0.0",
33
- "eslint-config-pluv": "^1.0.0"
32
+ "@pluv/tsconfig": "^1.0.1",
33
+ "eslint-config-pluv": "^1.0.1"
34
34
  },
35
35
  "scripts": {
36
36
  "build": "tsup src/index.ts --format esm,cjs --dts",
@@ -2,6 +2,8 @@ import { Array as YArray } from "yjs";
2
2
  import type { YjsType } from "../types";
3
3
  import { YjsArray } from "./YjsArray";
4
4
 
5
- export const array = <T extends unknown>(value: T[] | readonly T[] = []): YjsType<YArray<T>, T[]> => {
5
+ export const array = <T extends unknown>(
6
+ value: T[] | readonly T[] = [],
7
+ ): YjsType<YArray<T>, T[]> => {
6
8
  return new YjsArray(value);
7
9
  };
@@ -26,11 +26,14 @@ import { YjsObject } from "../object/YjsObject";
26
26
  import { YjsText } from "../text/YjsText";
27
27
  import type { YjsType } from "../types";
28
28
  import { YjsXmlElement } from "../xmlElement/YjsXmlElement";
29
+ import { YjsXmlFragment } from "../xmlFragment/YjsXmlFragment";
29
30
  import { YjsXmlText } from "../xmlText/YjsXmlText";
30
31
 
31
32
  const MERGE_INTERVAL_MS = 1_000;
32
33
 
33
- export class CrdtYjsDoc<TStorage extends Record<string, YjsType<any, any>>> extends AbstractCrdtDoc<TStorage> {
34
+ export class CrdtYjsDoc<
35
+ TStorage extends Record<string, YjsType<any, any>>,
36
+ > extends AbstractCrdtDoc<TStorage> {
34
37
  public value: YDoc = new YDoc();
35
38
 
36
39
  private _storage: TStorage;
@@ -79,15 +82,19 @@ export class CrdtYjsDoc<TStorage extends Record<string, YjsType<any, any>>> exte
79
82
  if (node instanceof YjsXmlElement) {
80
83
  const yXmlElement = this.value.getXmlElement(key);
81
84
 
82
- if (!!node.initialValue?.length) yXmlElement.insert(0, node.initialValue?.slice(0));
85
+ if (!!node.initialValue?.length) {
86
+ yXmlElement.insert(0, node.initialValue?.slice(0));
87
+ }
83
88
 
84
89
  return { ...acc, [key]: yXmlElement };
85
90
  }
86
91
 
87
- if (node instanceof YXmlFragment) {
92
+ if (node instanceof YjsXmlFragment) {
88
93
  const yXmlFragment = this.value.getXmlFragment(key);
89
94
 
90
- if (!!node.initialValue?.length) yXmlFragment.insert(0, node.initialValue?.slice(0));
95
+ if (!!node.initialValue?.length) {
96
+ yXmlFragment.insert(0, node.initialValue?.slice(0));
97
+ }
91
98
 
92
99
  return { ...acc, [key]: yXmlFragment };
93
100
  }
@@ -170,6 +177,32 @@ export class CrdtYjsDoc<TStorage extends Record<string, YjsType<any, any>>> exte
170
177
  return !this.value.share.size;
171
178
  }
172
179
 
180
+ public rebuildStorage(reference: TStorage): this {
181
+ const isBuilt = !!Object.keys(this._storage).length;
182
+
183
+ if (isBuilt) {
184
+ console.warn("Attempted to rebuild storage multiple times");
185
+ return this;
186
+ }
187
+
188
+ this._storage = Object.entries(reference).reduce((acc, [key, node]) => {
189
+ if (node instanceof YArray) return { ...acc, [key]: this.value.getArray(key) };
190
+ if (node instanceof YMap) return { ...acc, [key]: this.value.getMap(key) };
191
+ if (node instanceof YText) return { ...acc, [key]: this.value.getText(key) };
192
+ if (node instanceof YXmlElement) {
193
+ return { ...acc, [key]: this.value.getXmlElement(key) };
194
+ }
195
+ if (node instanceof YXmlFragment) {
196
+ return { ...acc, [key]: this.value.getXmlFragment(key) };
197
+ }
198
+ if (node instanceof YXmlText) return { ...acc, [key]: this.value.get(key, YXmlText) };
199
+
200
+ return acc;
201
+ }, {} as TStorage);
202
+
203
+ return this.track();
204
+ }
205
+
173
206
  public redo(): this {
174
207
  this._undoManager?.redo();
175
208
 
@@ -196,20 +229,23 @@ export class CrdtYjsDoc<TStorage extends Record<string, YjsType<any, any>>> exte
196
229
  public track(): this {
197
230
  if (this._undoManager) this._undoManager.destroy();
198
231
 
199
- const sharedTypes = Object.values(this._storage).reduce<YjsType<AbstractType<any>, any>[]>((acc, type) => {
200
- if (
201
- type instanceof YArray ||
202
- type instanceof YMap ||
203
- type instanceof YText ||
204
- type instanceof YXmlElement ||
205
- type instanceof YXmlFragment ||
206
- type instanceof YXmlText
207
- ) {
208
- acc.push(type);
209
- }
232
+ const sharedTypes = Object.values(this._storage).reduce<YjsType<AbstractType<any>, any>[]>(
233
+ (acc, type) => {
234
+ if (
235
+ type instanceof YArray ||
236
+ type instanceof YMap ||
237
+ type instanceof YText ||
238
+ type instanceof YXmlElement ||
239
+ type instanceof YXmlFragment ||
240
+ type instanceof YXmlText
241
+ ) {
242
+ acc.push(type as YjsType<AbstractType<any>, any>);
243
+ }
210
244
 
211
- return acc;
212
- }, []);
245
+ return acc;
246
+ },
247
+ [],
248
+ );
213
249
 
214
250
  if (!sharedTypes.length) {
215
251
  this._undoManager?.destroy();
@@ -35,44 +35,6 @@ export class CrdtYjsDocFactory<
35
35
  return new CrdtYjsDocFactory<TStorage>(initialStorage ?? this._initialStorage);
36
36
  }
37
37
 
38
- public getFresh(): CrdtYjsDoc<TStorage> {
39
- const storage = this._initialStorage();
40
-
41
- return new CrdtYjsDoc<TStorage>(
42
- Object.entries(storage).reduce((acc, [key, node]) => {
43
- if (node instanceof YjsArray) {
44
- return { ...acc, [key]: array() };
45
- }
46
-
47
- if (node instanceof YjsMap) {
48
- return { ...acc, [key]: map() };
49
- }
50
-
51
- if (node instanceof YjsObject) {
52
- return { ...acc, [key]: object() };
53
- }
54
-
55
- if (node instanceof YjsText) {
56
- return { ...acc, [key]: text() };
57
- }
58
-
59
- if (node instanceof YjsXmlElement) {
60
- return { ...acc, [key]: xmlElement(node.nodeName, []) };
61
- }
62
-
63
- if (node instanceof YXmlFragment) {
64
- return { ...acc, [key]: xmlFragment([]) };
65
- }
66
-
67
- if (node instanceof YjsXmlText) {
68
- return { ...acc, [key]: xmlText() };
69
- }
70
-
71
- return acc;
72
- }, {} as TStorage),
73
- );
74
- }
75
-
76
38
  public getInitialized(initialStorage?: () => TStorage): CrdtYjsDoc<TStorage> {
77
39
  return new CrdtYjsDoc<TStorage>(initialStorage?.() ?? this._initialStorage());
78
40
  }
package/src/map/YjsMap.ts CHANGED
@@ -1,7 +1,10 @@
1
1
  import { Map as YMap } from "yjs";
2
2
  import type { YjsType } from "../types";
3
3
 
4
- export class YjsMap<T extends unknown> extends YMap<T> implements YjsType<YMap<T>, Record<string, T>> {
4
+ export class YjsMap<T extends unknown>
5
+ extends YMap<T>
6
+ implements YjsType<YMap<T>, Record<string, T>>
7
+ {
5
8
  public initialValue?: readonly (readonly [key: string, value: T])[];
6
9
 
7
10
  constructor(value: readonly (readonly [key: string, value: T])[] = []) {
@@ -1,7 +1,10 @@
1
1
  import { Map as YMap } from "yjs";
2
2
  import type { YjsType } from "../types";
3
3
 
4
- export class YjsObject<T extends Record<string, any>> extends YMap<T[keyof T]> implements YjsType<YMap<T[keyof T]>, T> {
4
+ export class YjsObject<T extends Record<string, any>>
5
+ extends YMap<T[keyof T]>
6
+ implements YjsType<YMap<T[keyof T]>, T>
7
+ {
5
8
  public initialValue?: T;
6
9
 
7
10
  constructor(value: T) {
@@ -2,6 +2,8 @@ import type { Map as YMap } from "yjs";
2
2
  import type { YjsType } from "../types";
3
3
  import { YjsObject } from "./YjsObject";
4
4
 
5
- export const object = <T extends Record<string, any>>(value: T = {} as T): YjsType<YMap<T[keyof T]>, T> => {
5
+ export const object = <T extends Record<string, any>>(
6
+ value: T = {} as T,
7
+ ): YjsType<YMap<T[keyof T]>, T> => {
6
8
  return new YjsObject<T>(value);
7
9
  };
package/src/types.ts CHANGED
@@ -1,6 +1,9 @@
1
1
  import type { CrdtType } from "@pluv/crdt";
2
2
 
3
- export type YjsType<TValue extends unknown, TJson extends unknown = any> = CrdtType<TValue, TJson> & {
3
+ export type YjsType<TValue extends unknown, TJson extends unknown = any> = CrdtType<
4
+ TValue,
5
+ TJson
6
+ > & {
4
7
  initialValue?: any;
5
8
  toJSON: () => any;
6
9
  };
@@ -8,7 +8,10 @@ export class YjsXmlElement<T extends Record<string, any> = Record<string, any>>
8
8
  {
9
9
  public initialValue?: (YXmlElement | YXmlText)[] | readonly (YXmlElement | YXmlText)[];
10
10
 
11
- constructor(name: string, value: (YXmlElement | YXmlText)[] | readonly (YXmlElement | YXmlText)[]) {
11
+ constructor(
12
+ name: string,
13
+ value: (YXmlElement | YXmlText)[] | readonly (YXmlElement | YXmlText)[],
14
+ ) {
12
15
  super(name);
13
16
 
14
17
  if (!!value.length) this.insert(0, value as (YXmlElement | YXmlText)[]);
package/tsconfig.json CHANGED
@@ -3,6 +3,6 @@
3
3
  "include": ["next-env.d.ts", "**/*.ts", "**/*.tsx"],
4
4
  "exclude": ["node_modules"],
5
5
  "compilerOptions": {
6
- "lib": ["esnext"]
6
+ "lib": ["dom", "esnext"]
7
7
  }
8
8
  }