@pluv/crdt-loro 0.19.0 → 0.21.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,234 +0,0 @@
1
- import { AbstractCrdtType } from "@pluv/crdt";
2
- import type { Container } from "loro-crdt";
3
- import { LoroList, LoroMap, LoroText } from "loro-crdt";
4
- import { CrdtLoroArray } from "../array";
5
- import { CrdtLoroMap } from "../map";
6
- import { CrdtLoroObject } from "../object";
7
- import { CrdtLoroText } from "../text";
8
-
9
- interface CloneArrayParams<T extends unknown> {
10
- source: CrdtLoroArray<T>;
11
- target: LoroList<T[]>;
12
- }
13
-
14
- interface CloneMapParams<T extends unknown> {
15
- source: CrdtLoroMap<T>;
16
- target: LoroMap<Record<string, T>>;
17
- }
18
-
19
- interface CloneObjectParams<T extends Record<string, any>> {
20
- source: CrdtLoroObject<T>;
21
- target: LoroMap<T>;
22
- }
23
-
24
- interface CloneTextParams {
25
- source: CrdtLoroText;
26
- target: LoroText;
27
- }
28
-
29
- type SourceType<TType extends Container> =
30
- TType extends LoroList<(infer I)[]>
31
- ? CrdtLoroArray<I>
32
- : TType extends LoroMap<infer I>
33
- ? CrdtLoroMap<I[keyof I]> | CrdtLoroObject<I>
34
- : TType extends LoroText
35
- ? CrdtLoroText
36
- : never;
37
-
38
- export interface CloneTypeParams<TType extends Container> {
39
- source: SourceType<TType>;
40
- target: TType;
41
- }
42
-
43
- function cloneArray<T extends unknown>(params: CloneArrayParams<T>): void {
44
- const { source, target } = params;
45
-
46
- const items = source.initialValue;
47
-
48
- items.forEach((item, i) => {
49
- if (!(item instanceof AbstractCrdtType)) {
50
- target.insert(i, item);
51
-
52
- return;
53
- }
54
-
55
- if (item instanceof CrdtLoroArray) {
56
- const list = target.insertContainer(i, "List");
57
-
58
- item.value = list;
59
- cloneArray({ source: item, target: list });
60
-
61
- return;
62
- }
63
-
64
- if (item instanceof CrdtLoroMap) {
65
- const map = target.insertContainer(i, "Map");
66
-
67
- item.value = map;
68
- cloneMap({ source: item, target: map });
69
-
70
- return;
71
- }
72
-
73
- if (item instanceof CrdtLoroObject) {
74
- const map = target.insertContainer(i, "Map");
75
-
76
- item.value = map;
77
- cloneObject({ source: item, target: map });
78
-
79
- return;
80
- }
81
-
82
- if (item instanceof CrdtLoroText) {
83
- const text = target.insertContainer(i, "Text");
84
-
85
- item.value = text;
86
- cloneText({ source: item, target: text });
87
-
88
- return;
89
- }
90
-
91
- throw new Error("This type is not yet supported");
92
- });
93
- }
94
-
95
- function cloneMap<T extends unknown>(params: CloneMapParams<T>): void {
96
- const { source, target } = params;
97
-
98
- const items = source.initialValue;
99
-
100
- items.forEach(([key, item]) => {
101
- if (!(item instanceof AbstractCrdtType)) {
102
- target.set(key, item);
103
-
104
- return;
105
- }
106
-
107
- if (item instanceof CrdtLoroArray) {
108
- const list = target.setContainer(key, "List");
109
-
110
- item.value = list;
111
- cloneArray({ source: item, target: list });
112
-
113
- return;
114
- }
115
-
116
- if (item instanceof CrdtLoroMap) {
117
- const map = target.setContainer(key, "Map");
118
-
119
- item.value = map;
120
- cloneMap({ source: item, target: map });
121
-
122
- return;
123
- }
124
-
125
- if (item instanceof CrdtLoroObject) {
126
- const map = target.setContainer(key, "Map");
127
-
128
- item.value = map;
129
- cloneObject({ source: item, target: map });
130
-
131
- return;
132
- }
133
-
134
- if (item instanceof CrdtLoroText) {
135
- const text = target.setContainer(key, "Text");
136
-
137
- item.value = text;
138
- cloneText({ source: item, target: text });
139
-
140
- return;
141
- }
142
-
143
- throw new Error("This type is not yet supported");
144
- });
145
- }
146
-
147
- function cloneObject<T extends Record<string, any>>(params: CloneObjectParams<T>): void {
148
- const { source, target } = params;
149
-
150
- const items = source.initialValue;
151
-
152
- items.forEach(([key, item]) => {
153
- if (!(item instanceof AbstractCrdtType)) {
154
- target.set(key, item);
155
-
156
- return;
157
- }
158
-
159
- if (item instanceof CrdtLoroArray) {
160
- const list = target.setContainer(key, "List");
161
-
162
- item.value = list;
163
- cloneArray({ source: item, target: list });
164
-
165
- return;
166
- }
167
-
168
- if (item instanceof CrdtLoroMap) {
169
- const map = target.setContainer(key, "Map");
170
-
171
- item.value = map;
172
- cloneMap({ source: item, target: map });
173
-
174
- return;
175
- }
176
-
177
- if (item instanceof CrdtLoroObject) {
178
- const map = target.setContainer(key, "Map");
179
-
180
- item.value = map;
181
- cloneObject({ source: item, target: map });
182
-
183
- return;
184
- }
185
-
186
- if (item instanceof CrdtLoroText) {
187
- const text = target.setContainer(key, "Text");
188
-
189
- item.value = text;
190
- cloneText({ source: item, target: text });
191
-
192
- return;
193
- }
194
-
195
- throw new Error("This type is not yet supported");
196
- });
197
- }
198
-
199
- function cloneText(params: CloneTextParams): void {
200
- const { source, target } = params;
201
-
202
- source.value = target;
203
- source.insert(0, source.initalValue);
204
- }
205
-
206
- export const cloneType = <TType extends Container>(params: CloneTypeParams<TType>) => {
207
- const { source, target } = params;
208
-
209
- if (source instanceof CrdtLoroArray) {
210
- cloneArray({ source, target: target as LoroList });
211
-
212
- return;
213
- }
214
-
215
- if (source instanceof CrdtLoroMap) {
216
- cloneMap({ source, target: target as LoroMap });
217
-
218
- return;
219
- }
220
-
221
- if (source instanceof CrdtLoroObject) {
222
- cloneObject({ source, target: target as LoroMap });
223
-
224
- return;
225
- }
226
-
227
- if (source instanceof CrdtLoroText) {
228
- cloneText({ source, target: target as LoroText });
229
-
230
- return;
231
- }
232
-
233
- throw new Error("This type is not yet supported");
234
- };
@@ -1,22 +0,0 @@
1
- import { AbstractCrdtType } from "@pluv/crdt";
2
- import { CrdtLoroArray } from "../array";
3
- import { CrdtLoroMap } from "../map";
4
- import { CrdtLoroObject } from "../object";
5
- import { CrdtLoroText } from "../text";
6
-
7
- export const getLoroContainerType = <T extends AbstractCrdtType<any, any>>(
8
- value: T,
9
- ): T extends CrdtLoroArray<any>
10
- ? "List"
11
- : T extends CrdtLoroMap<any> | CrdtLoroObject<any>
12
- ? "Map"
13
- : T extends CrdtLoroText
14
- ? "Text"
15
- : never => {
16
- if (value instanceof CrdtLoroArray) return "List" as any;
17
- if (value instanceof CrdtLoroMap) return "Map" as any;
18
- if (value instanceof CrdtLoroObject) return "Map" as any;
19
- if (value instanceof CrdtLoroText) return "Text" as any;
20
-
21
- throw new Error("This type is not yet supported");
22
- };
@@ -1,5 +0,0 @@
1
- export { cloneType } from "./cloneType";
2
- export type { CloneTypeParams } from "./cloneType";
3
- export { getLoroContainerType } from "./getLoroContainerType";
4
- export { isWrapper } from "./isWrapper";
5
- export { toLoroValue } from "./toLoroValue";
@@ -1,15 +0,0 @@
1
- import { CrdtLoroArray } from "../array/CrdtLoroArray";
2
- import { CrdtLoroMap } from "../map/CrdtLoroMap";
3
- import { CrdtLoroObject } from "../object/CrdtLoroObject";
4
- import { CrdtLoroText } from "../text/CrdtLoroText";
5
-
6
- export const isWrapper = (
7
- item: any,
8
- ): item is CrdtLoroArray<any> | CrdtLoroMap<any> | CrdtLoroObject<any> | CrdtLoroText => {
9
- return (
10
- item instanceof CrdtLoroArray ||
11
- item instanceof CrdtLoroMap ||
12
- item instanceof CrdtLoroObject ||
13
- item instanceof CrdtLoroText
14
- );
15
- };
@@ -1,5 +0,0 @@
1
- import { isWrapper } from "./isWrapper";
2
-
3
- export const toLoroValue = (item: any): any => {
4
- return isWrapper(item) ? item.value : item;
5
- };
@@ -1,68 +0,0 @@
1
- import { AbstractCrdtType } from "@pluv/crdt";
2
- import { LoroText } from "loro-crdt";
3
- import type { CrdtLoroDoc } from "../doc/CrdtLoroDoc";
4
- import { cloneType } from "../shared";
5
-
6
- export class CrdtLoroText extends AbstractCrdtType<LoroText, string> {
7
- public initalValue: string;
8
-
9
- private _doc: CrdtLoroDoc<any> | null = null;
10
- private _initialized: boolean = false;
11
- private _value: LoroText = new LoroText();
12
-
13
- constructor(value: string = "") {
14
- super();
15
-
16
- this.initalValue = value;
17
- this.value = new LoroText();
18
- }
19
-
20
- public set doc(doc: CrdtLoroDoc<any>) {
21
- if (this._doc) throw new Error("Cannot overwrite array doc");
22
-
23
- this._doc = doc;
24
- }
25
-
26
- public get length(): number {
27
- return this.value.length;
28
- }
29
-
30
- public get value(): LoroText {
31
- return this._value;
32
- }
33
-
34
- public set value(value: LoroText) {
35
- if (this._initialized) throw new Error("Cannot re-assign text");
36
-
37
- this._initialized = true;
38
- this._value = value;
39
-
40
- cloneType({ source: this, target: this.value });
41
- }
42
-
43
- public delete(index: number, length: number = 1): this {
44
- this._guardInitialized();
45
-
46
- this.value.delete(index, length);
47
- this._doc?.value.commit();
48
-
49
- return this;
50
- }
51
-
52
- public insert(index: number, text: string): this {
53
- this._guardInitialized();
54
-
55
- this.value.insert(index, text);
56
- this._doc?.value.commit();
57
-
58
- return this;
59
- }
60
-
61
- public toJson(): string {
62
- return this.value.toString();
63
- }
64
-
65
- private _guardInitialized(): void {
66
- if (!this._initialized) throw new Error("Array is not yet initialized");
67
- }
68
- }
package/src/text/index.ts DELETED
@@ -1,2 +0,0 @@
1
- export { CrdtLoroText } from "./CrdtLoroText";
2
- export { text } from "./text";
package/src/text/text.ts DELETED
@@ -1,5 +0,0 @@
1
- import { CrdtLoroText } from "./CrdtLoroText";
2
-
3
- export const text = (value: string = ""): CrdtLoroText => {
4
- return new CrdtLoroText(value);
5
- };