mobx-keystone-yjs 1.5.5 → 1.6.0
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/CHANGELOG.md +57 -52
- package/dist/mobx-keystone-yjs.esm.js +334 -216
- package/dist/mobx-keystone-yjs.esm.mjs +334 -216
- package/dist/mobx-keystone-yjs.umd.js +332 -214
- package/dist/types/binding/applyMobxChangeToYjsObject.d.ts +3 -0
- package/dist/types/binding/applyYjsEventToMobx.d.ts +8 -0
- package/dist/types/binding/convertJsonToYjsData.d.ts +22 -3
- package/dist/types/binding/resolveYjsPath.d.ts +14 -1
- package/dist/types/binding/yjsSnapshotTracking.d.ts +24 -0
- package/dist/types/index.d.ts +1 -0
- package/package.json +90 -88
- package/src/binding/applyMobxChangeToYjsObject.ts +77 -0
- package/src/binding/applyYjsEventToMobx.ts +173 -0
- package/src/binding/bindYjsToMobxKeystone.ts +300 -202
- package/src/binding/convertJsonToYjsData.ts +218 -73
- package/src/binding/resolveYjsPath.ts +51 -27
- package/src/binding/yjsSnapshotTracking.ts +40 -0
- package/src/index.ts +11 -10
- package/src/utils/getOrCreateYjsCollectionAtom.ts +27 -27
- package/dist/types/binding/applyMobxKeystonePatchToYjsObject.d.ts +0 -2
- package/dist/types/binding/convertYjsEventToPatches.d.ts +0 -3
- package/src/binding/applyMobxKeystonePatchToYjsObject.ts +0 -103
- package/src/binding/convertYjsEventToPatches.ts +0 -84
|
@@ -1,84 +0,0 @@
|
|
|
1
|
-
import { Patch } from "mobx-keystone"
|
|
2
|
-
import * as Y from "yjs"
|
|
3
|
-
import { failure } from "../utils/error"
|
|
4
|
-
import { convertYjsDataToJson } from "./convertYjsDataToJson"
|
|
5
|
-
|
|
6
|
-
export function convertYjsEventToPatches(event: Y.YEvent<any>): Patch[] {
|
|
7
|
-
const patches: Patch[] = []
|
|
8
|
-
|
|
9
|
-
if (event instanceof Y.YMapEvent) {
|
|
10
|
-
const source = event.target
|
|
11
|
-
|
|
12
|
-
event.changes.keys.forEach((change, key) => {
|
|
13
|
-
const path = [...event.path, key]
|
|
14
|
-
|
|
15
|
-
switch (change.action) {
|
|
16
|
-
case "add":
|
|
17
|
-
patches.push({
|
|
18
|
-
op: "add",
|
|
19
|
-
path,
|
|
20
|
-
value: convertYjsDataToJson(source.get(key)),
|
|
21
|
-
})
|
|
22
|
-
break
|
|
23
|
-
|
|
24
|
-
case "update":
|
|
25
|
-
patches.push({
|
|
26
|
-
op: "replace",
|
|
27
|
-
path,
|
|
28
|
-
value: convertYjsDataToJson(source.get(key)),
|
|
29
|
-
})
|
|
30
|
-
break
|
|
31
|
-
|
|
32
|
-
case "delete":
|
|
33
|
-
patches.push({
|
|
34
|
-
op: "remove",
|
|
35
|
-
path,
|
|
36
|
-
})
|
|
37
|
-
break
|
|
38
|
-
|
|
39
|
-
default:
|
|
40
|
-
throw failure(`unsupported Yjs map event action: ${change.action}`)
|
|
41
|
-
}
|
|
42
|
-
})
|
|
43
|
-
} else if (event instanceof Y.YArrayEvent) {
|
|
44
|
-
let retain = 0
|
|
45
|
-
event.changes.delta.forEach((change) => {
|
|
46
|
-
if (change.retain) {
|
|
47
|
-
retain += change.retain
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
if (change.delete) {
|
|
51
|
-
// remove X items at retain position
|
|
52
|
-
const path = [...event.path, retain]
|
|
53
|
-
for (let i = 0; i < change.delete; i++) {
|
|
54
|
-
patches.push({
|
|
55
|
-
op: "remove",
|
|
56
|
-
path,
|
|
57
|
-
})
|
|
58
|
-
}
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
if (change.insert) {
|
|
62
|
-
const newValues = Array.isArray(change.insert) ? change.insert : [change.insert]
|
|
63
|
-
newValues.forEach((v) => {
|
|
64
|
-
const path = [...event.path, retain]
|
|
65
|
-
patches.push({
|
|
66
|
-
op: "add",
|
|
67
|
-
path,
|
|
68
|
-
value: convertYjsDataToJson(v),
|
|
69
|
-
})
|
|
70
|
-
retain++
|
|
71
|
-
})
|
|
72
|
-
}
|
|
73
|
-
})
|
|
74
|
-
} else if (event instanceof Y.YTextEvent) {
|
|
75
|
-
const path = [...event.path, "deltaList", -1 /* last item */]
|
|
76
|
-
patches.push({
|
|
77
|
-
op: "add",
|
|
78
|
-
path,
|
|
79
|
-
value: { $frozen: true, data: event.delta },
|
|
80
|
-
})
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
return patches
|
|
84
|
-
}
|