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.
@@ -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
- }