-
-
Notifications
You must be signed in to change notification settings - Fork 262
Expand file tree
/
Copy pathdiff.ts
More file actions
160 lines (139 loc) · 3.8 KB
/
Copy pathdiff.ts
File metadata and controls
160 lines (139 loc) · 3.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
import * as DeepDiff from 'deep-diff';
import * as Y from 'yjs';
export { DeepDiff, Y };
export function toJSON<T>(source: unknown): T {
if (source instanceof Y.AbstractType) {
source = source.toJSON();
for (const [key, value] of getEntries(source)) {
setValue(source, key, value, toJSON);
}
}
if (Array.isArray(source)) {
return source.map(toJSON) as unknown as T;
}
if (typeof source === 'object' && source !== null) {
const obj = {};
for (const [key, value] of Object.entries(source)) {
(obj as any)[key] = toJSON(value);
}
return obj as unknown as T;
}
return source as unknown as T;
}
function toYjsType(source: any) {
if (Array.isArray(source)) {
return new Y.Array();
}
if (typeof source === 'object' && source !== null) {
return new Y.Map();
}
return source;
}
function getEntries(source: any): any[] {
if (Array.isArray(source)) {
return [...source.entries()];
}
if (typeof source === 'object' && source !== null) {
return Object.entries(source);
}
if (source instanceof Y.Array) {
return source.map((value, index) => [index, value]);
}
if (source instanceof Y.Map) {
return [...source.entries()];
}
return [];
}
function setValue(target: any, key: string | number, value: any, mapFn = toYjsType) {
const yValue = mapFn(value);
if (target instanceof Y.Map) {
target.set(String(key), yValue);
getEntries(value).forEach(([k, v]) => {
setValue(target.get(String(key)), k, v);
});
return;
}
if (target instanceof Y.Array && typeof key === 'number') {
if (target.length > key) {
target.delete(key, 1);
}
const over = key - target.length;
if (over > 0) {
target.push(Array(over).fill(null));
}
target.insert(key, [yValue]);
getEntries(value).forEach(([k, v]) => {
setValue(target.get(key), k, v);
});
return;
}
target[key] = yValue;
}
function applyArrayChange(arr: Y.Array<any>, index: number, change: DeepDiff.Diff<any>) {
if (change.path && change.path.length) {
let it = arr.get(index);
let i: number;
const u = change.path.length - 1;
for (i = 0; i < u; i++) {
it = it.get(change.path[i]);
switch (change.kind) {
case 'A':
applyArrayChange(it.get(change.path[i]), change.index, change.item);
break;
case 'D':
it.delete(change.path[i], 1);
break;
case 'E':
case 'N':
setValue(it, change.path[i], change.rhs);
break;
}
}
} else {
switch (change.kind) {
case 'A':
applyArrayChange(arr.get(index), change.index, change.item);
break;
case 'D':
arr.delete(index, 1);
break;
case 'E':
case 'N':
setValue(arr, index, change.rhs);
break;
}
}
return arr;
}
export function applyChange<LHS = any, RHS = any>(target: any, change: DeepDiff.Diff<LHS, RHS>) {
if (target && change && change.kind) {
let it = target;
let i = -1;
const last = change.path ? change.path.length - 1 : 0;
while (++i < last) {
if (typeof it.get(change.path?.[i]) === 'undefined') {
setValue(
it,
change.path?.[i],
typeof change.path?.[i + 1] === 'number' ? new Y.Array() : new Y.Map(),
);
}
it = it.get(change.path?.[i]);
}
switch (change.kind) {
case 'A':
if (change.path && typeof it.get(change.path[i]) === 'undefined') {
setValue(it, change.path[i], new Y.Array());
}
applyArrayChange(change.path ? it.get(change.path[i]) : it, change.index, change.item);
break;
case 'D':
it.delete(change.path?.[i], 1);
break;
case 'E':
case 'N':
setValue(it, change.path?.[i], change.rhs);
break;
}
}
}