@loro-extended/change 4.0.0 → 5.0.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/README.md +173 -149
- package/dist/index.d.ts +962 -335
- package/dist/index.js +1040 -598
- package/dist/index.js.map +1 -1
- package/package.json +4 -4
- package/src/change.test.ts +51 -52
- package/src/functional-helpers.test.ts +316 -4
- package/src/functional-helpers.ts +96 -6
- package/src/grand-unified-api.test.ts +35 -29
- package/src/index.ts +25 -1
- package/src/json-patch.test.ts +46 -27
- package/src/loro.test.ts +449 -0
- package/src/loro.ts +273 -0
- package/src/overlay-recursion.test.ts +1 -1
- package/src/path-evaluator.ts +1 -1
- package/src/path-selector.test.ts +94 -1
- package/src/shape.ts +47 -15
- package/src/typed-doc.ts +99 -98
- package/src/typed-refs/base.ts +126 -35
- package/src/typed-refs/counter-ref-internals.ts +62 -0
- package/src/typed-refs/{counter.test.ts → counter-ref.test.ts} +5 -4
- package/src/typed-refs/counter-ref.ts +45 -0
- package/src/typed-refs/{doc.ts → doc-ref-internals.ts} +33 -38
- package/src/typed-refs/doc-ref.ts +47 -0
- package/src/typed-refs/encapsulation.test.ts +226 -0
- package/src/typed-refs/list-ref-base-internals.ts +280 -0
- package/src/typed-refs/{list-base.ts → list-ref-base.ts} +255 -160
- package/src/typed-refs/list-ref-internals.ts +21 -0
- package/src/typed-refs/{list.ts → list-ref.ts} +10 -11
- package/src/typed-refs/movable-list-ref-internals.ts +38 -0
- package/src/typed-refs/movable-list-ref.ts +31 -0
- package/src/typed-refs/proxy-handlers.ts +13 -4
- package/src/typed-refs/{record.ts → record-ref-internals.ts} +78 -79
- package/src/typed-refs/{record.test.ts → record-ref.test.ts} +21 -16
- package/src/typed-refs/record-ref.ts +80 -0
- package/src/typed-refs/struct-ref-internals.ts +195 -0
- package/src/typed-refs/{struct-value-updates.test.ts → struct-ref.test.ts} +5 -3
- package/src/typed-refs/struct-ref.ts +257 -0
- package/src/typed-refs/text-ref-internals.ts +100 -0
- package/src/typed-refs/text-ref.ts +72 -0
- package/src/typed-refs/tree-node-ref-internals.ts +111 -0
- package/src/typed-refs/{tree-node.ts → tree-node-ref.ts} +58 -94
- package/src/typed-refs/tree-ref-internals.ts +110 -0
- package/src/typed-refs/tree-ref.ts +194 -0
- package/src/typed-refs/utils.ts +21 -23
- package/src/typed-refs/counter.ts +0 -62
- package/src/typed-refs/movable-list.ts +0 -32
- package/src/typed-refs/struct.ts +0 -201
- package/src/typed-refs/text.ts +0 -91
- package/src/typed-refs/tree.ts +0 -268
- /package/src/typed-refs/{list-value-updates.test.ts → list-ref-value-updates.test.ts} +0 -0
- /package/src/typed-refs/{list.test.ts → list-ref.test.ts} +0 -0
- /package/src/typed-refs/{movable-list.test.ts → movable-list-ref.test.ts} +0 -0
- /package/src/typed-refs/{record-value-updates.test.ts → record-ref-value-updates.test.ts} +0 -0
- /package/src/typed-refs/{tree-node-value-updates.test.ts → tree-node-ref.test.ts} +0 -0
- /package/src/typed-refs/{tree.test.ts → tree-node.test.ts} +0 -0
package/dist/index.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { LoroDoc,
|
|
1
|
+
import { LoroDoc, Container, LoroTreeNode, TreeID, Subscription, LoroText, LoroList, LoroMovableList, LoroMap, LoroCounter, LoroTree, Value } from 'loro-crdt';
|
|
2
2
|
|
|
3
3
|
type Prev = [never, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
|
|
4
4
|
/**
|
|
@@ -71,181 +71,911 @@ type InferPlaceholderType<T> = T extends Shape<any, any, infer P> ? P : never;
|
|
|
71
71
|
*/
|
|
72
72
|
type Mutable<T extends DocShape<Record<string, ContainerShape>>> = InferMutableType<T>;
|
|
73
73
|
|
|
74
|
+
/** biome-ignore-all lint/suspicious/noExplicitAny: JSON Patch values can be any type */
|
|
75
|
+
|
|
76
|
+
type JsonPatchAddOperation = {
|
|
77
|
+
op: "add";
|
|
78
|
+
path: string | (string | number)[];
|
|
79
|
+
value: any;
|
|
80
|
+
};
|
|
81
|
+
type JsonPatchRemoveOperation = {
|
|
82
|
+
op: "remove";
|
|
83
|
+
path: string | (string | number)[];
|
|
84
|
+
};
|
|
85
|
+
type JsonPatchReplaceOperation = {
|
|
86
|
+
op: "replace";
|
|
87
|
+
path: string | (string | number)[];
|
|
88
|
+
value: any;
|
|
89
|
+
};
|
|
90
|
+
type JsonPatchMoveOperation = {
|
|
91
|
+
op: "move";
|
|
92
|
+
path: string | (string | number)[];
|
|
93
|
+
from: string | (string | number)[];
|
|
94
|
+
};
|
|
95
|
+
type JsonPatchCopyOperation = {
|
|
96
|
+
op: "copy";
|
|
97
|
+
path: string | (string | number)[];
|
|
98
|
+
from: string | (string | number)[];
|
|
99
|
+
};
|
|
100
|
+
type JsonPatchTestOperation = {
|
|
101
|
+
op: "test";
|
|
102
|
+
path: string | (string | number)[];
|
|
103
|
+
value: any;
|
|
104
|
+
};
|
|
105
|
+
type JsonPatchOperation = JsonPatchAddOperation | JsonPatchRemoveOperation | JsonPatchReplaceOperation | JsonPatchMoveOperation | JsonPatchCopyOperation | JsonPatchTestOperation;
|
|
106
|
+
type JsonPatch = JsonPatchOperation[];
|
|
107
|
+
|
|
108
|
+
/** biome-ignore-all lint/suspicious/noExplicitAny: fix later */
|
|
109
|
+
|
|
110
|
+
/**
|
|
111
|
+
* The proxied TypedDoc type that provides direct schema access.
|
|
112
|
+
* Schema properties are accessed directly on the doc object.
|
|
113
|
+
*
|
|
114
|
+
* @example
|
|
115
|
+
* ```typescript
|
|
116
|
+
* const doc = createTypedDoc(schema);
|
|
117
|
+
*
|
|
118
|
+
* // Direct schema access
|
|
119
|
+
* doc.count.increment(5);
|
|
120
|
+
* doc.title.insert(0, "Hello");
|
|
121
|
+
*
|
|
122
|
+
* // Serialize to JSON (works on doc and all refs)
|
|
123
|
+
* const snapshot = doc.toJSON();
|
|
124
|
+
* const users = doc.users.toJSON();
|
|
125
|
+
*
|
|
126
|
+
* // Batched mutations via change()
|
|
127
|
+
* doc.change(draft => {
|
|
128
|
+
* draft.count.increment(10);
|
|
129
|
+
* draft.title.update("World");
|
|
130
|
+
* });
|
|
131
|
+
*
|
|
132
|
+
* // Access CRDT internals via loro()
|
|
133
|
+
* import { loro } from "@loro-extended/change";
|
|
134
|
+
* loro(doc).doc; // LoroDoc
|
|
135
|
+
* loro(doc).subscribe(callback);
|
|
136
|
+
* ```
|
|
137
|
+
*/
|
|
138
|
+
type TypedDoc<Shape extends DocShape> = Mutable<Shape> & {
|
|
139
|
+
/**
|
|
140
|
+
* The primary method of mutating typed documents.
|
|
141
|
+
* Batches multiple mutations into a single transaction.
|
|
142
|
+
* All changes commit together at the end.
|
|
143
|
+
*
|
|
144
|
+
* Use this for:
|
|
145
|
+
* - Find-and-mutate operations (required due to JS limitations)
|
|
146
|
+
* - Performance (fewer commits)
|
|
147
|
+
* - Atomic undo (all changes = one undo step)
|
|
148
|
+
*
|
|
149
|
+
* Returns the doc for chaining.
|
|
150
|
+
*
|
|
151
|
+
* @example
|
|
152
|
+
* ```typescript
|
|
153
|
+
* doc.change(draft => {
|
|
154
|
+
* draft.count.increment(10);
|
|
155
|
+
* draft.title.update("World");
|
|
156
|
+
* });
|
|
157
|
+
* ```
|
|
158
|
+
*/
|
|
159
|
+
change(fn: (draft: Mutable<Shape>) => void): TypedDoc<Shape>;
|
|
160
|
+
/**
|
|
161
|
+
* Returns the full plain JavaScript object representation of the document.
|
|
162
|
+
* This is an O(N) operation that serializes the entire document.
|
|
163
|
+
*
|
|
164
|
+
* @example
|
|
165
|
+
* ```typescript
|
|
166
|
+
* const snapshot = doc.toJSON();
|
|
167
|
+
* console.log(snapshot.count); // number
|
|
168
|
+
* ```
|
|
169
|
+
*/
|
|
170
|
+
toJSON(): Infer<Shape>;
|
|
171
|
+
};
|
|
172
|
+
/**
|
|
173
|
+
* Creates a new TypedDoc with the given schema.
|
|
174
|
+
* Returns a proxied document where schema properties are accessed directly.
|
|
175
|
+
*
|
|
176
|
+
* @param shape - The document schema (with optional .placeholder() values)
|
|
177
|
+
* @param existingDoc - Optional existing LoroDoc to wrap
|
|
178
|
+
* @returns A proxied TypedDoc with direct schema access
|
|
179
|
+
*
|
|
180
|
+
* @example
|
|
181
|
+
* ```typescript
|
|
182
|
+
* const schema = Shape.doc({
|
|
183
|
+
* title: Shape.text(),
|
|
184
|
+
* count: Shape.counter(),
|
|
185
|
+
* });
|
|
186
|
+
*
|
|
187
|
+
* const doc = createTypedDoc(schema);
|
|
188
|
+
*
|
|
189
|
+
* // Direct mutations (auto-commit)
|
|
190
|
+
* doc.count.increment(5);
|
|
191
|
+
* doc.title.insert(0, "Hello");
|
|
192
|
+
*
|
|
193
|
+
* // Batched mutations via change()
|
|
194
|
+
* doc.change(draft => {
|
|
195
|
+
* draft.count.increment(10);
|
|
196
|
+
* draft.title.update("World");
|
|
197
|
+
* });
|
|
198
|
+
*
|
|
199
|
+
* // Get plain JSON
|
|
200
|
+
* const snapshot = doc.toJSON();
|
|
201
|
+
*
|
|
202
|
+
* // Access CRDT internals via loro()
|
|
203
|
+
* import { loro } from "@loro-extended/change";
|
|
204
|
+
* loro(doc).doc; // LoroDoc
|
|
205
|
+
* loro(doc).subscribe(callback);
|
|
206
|
+
* ```
|
|
207
|
+
*/
|
|
208
|
+
declare function createTypedDoc<Shape extends DocShape>(shape: Shape, existingDoc?: LoroDoc): TypedDoc<Shape>;
|
|
209
|
+
|
|
210
|
+
/**
|
|
211
|
+
* Internal implementation for ListRefBase.
|
|
212
|
+
* Contains all logic, state, and implementation details for list operations.
|
|
213
|
+
*/
|
|
214
|
+
declare class ListRefBaseInternals<NestedShape extends ContainerOrValueShape, Item = NestedShape["_plain"], MutableItem = NestedShape["_mutable"]> extends BaseRefInternals<any> {
|
|
215
|
+
private itemCache;
|
|
216
|
+
/** Get typed ref params for creating child refs at an index */
|
|
217
|
+
getTypedRefParams(index: number, shape: ContainerShape): TypedRefParams<ContainerShape>;
|
|
218
|
+
/** Get item for predicate functions (returns plain value) */
|
|
219
|
+
getPredicateItem(index: number): Item | undefined;
|
|
220
|
+
/** Get mutable item for return values (returns ref or cached value) */
|
|
221
|
+
getMutableItem(index: number): MutableItem | undefined;
|
|
222
|
+
/** Insert with automatic conversion */
|
|
223
|
+
insertWithConversion(index: number, item: unknown): void;
|
|
224
|
+
/** Push with automatic conversion */
|
|
225
|
+
pushWithConversion(item: unknown): void;
|
|
226
|
+
/** Absorb value at specific index (for value shapes) - subclasses override */
|
|
227
|
+
absorbValueAtIndex(_index: number, _value: unknown): void;
|
|
228
|
+
/** Update cache indices after a delete operation */
|
|
229
|
+
updateCacheForDelete(deleteIndex: number, deleteLen: number): void;
|
|
230
|
+
/** Update cache indices after an insert operation */
|
|
231
|
+
updateCacheForInsert(insertIndex: number): void;
|
|
232
|
+
/** Absorb mutated plain values back into Loro containers */
|
|
233
|
+
absorbPlainValues(): void;
|
|
234
|
+
/** Create the loro namespace for list */
|
|
235
|
+
protected createLoroNamespace(): LoroListRef;
|
|
236
|
+
}
|
|
237
|
+
/**
|
|
238
|
+
* Shared logic for list operations - thin facade that delegates to ListRefBaseInternals.
|
|
239
|
+
*/
|
|
240
|
+
declare abstract class ListRefBase<NestedShape extends ContainerOrValueShape, Item = NestedShape["_plain"], MutableItem = NestedShape["_mutable"]> extends TypedRef<any> {
|
|
241
|
+
[INTERNAL_SYMBOL]: ListRefBaseInternals<NestedShape, Item, MutableItem>;
|
|
242
|
+
constructor(params: TypedRefParams<any>);
|
|
243
|
+
/** Subclasses override to create their specific internals */
|
|
244
|
+
protected abstract createInternals(params: TypedRefParams<any>): ListRefBaseInternals<NestedShape, Item, MutableItem>;
|
|
245
|
+
find(predicate: (item: Item, index: number) => boolean): MutableItem | undefined;
|
|
246
|
+
findIndex(predicate: (item: Item, index: number) => boolean): number;
|
|
247
|
+
map<ReturnType>(callback: (item: Item, index: number) => ReturnType): ReturnType[];
|
|
248
|
+
filter(predicate: (item: Item, index: number) => boolean): MutableItem[];
|
|
249
|
+
forEach(callback: (item: Item, index: number) => void): void;
|
|
250
|
+
some(predicate: (item: Item, index: number) => boolean): boolean;
|
|
251
|
+
every(predicate: (item: Item, index: number) => boolean): boolean;
|
|
252
|
+
slice(start?: number, end?: number): MutableItem[];
|
|
253
|
+
insert(index: number, item: Item): void;
|
|
254
|
+
delete(index: number, len: number): void;
|
|
255
|
+
push(item: Item): void;
|
|
256
|
+
pushContainer(container: Container): Container;
|
|
257
|
+
insertContainer(index: number, container: Container): Container;
|
|
258
|
+
get(index: number): MutableItem | undefined;
|
|
259
|
+
toArray(): Item[];
|
|
260
|
+
toJSON(): Item[];
|
|
261
|
+
[Symbol.iterator](): IterableIterator<MutableItem>;
|
|
262
|
+
get length(): number;
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
/**
|
|
266
|
+
* Internal implementation for ListRef.
|
|
267
|
+
* Extends ListRefBaseInternals with LoroList-specific absorption logic.
|
|
268
|
+
*/
|
|
269
|
+
declare class ListRefInternals<NestedShape extends ContainerOrValueShape, Item = NestedShape["_plain"], MutableItem = NestedShape["_mutable"]> extends ListRefBaseInternals<NestedShape, Item, MutableItem> {
|
|
270
|
+
/** Absorb value at specific index for LoroList */
|
|
271
|
+
absorbValueAtIndex(index: number, value: unknown): void;
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
/**
|
|
275
|
+
* List typed ref - thin facade that delegates to ListRefInternals.
|
|
276
|
+
*/
|
|
277
|
+
declare class ListRef<NestedShape extends ContainerOrValueShape> extends ListRefBase<NestedShape> {
|
|
278
|
+
[index: number]: InferMutableType<NestedShape> | undefined;
|
|
279
|
+
protected createInternals(params: TypedRefParams<any>): ListRefInternals<NestedShape>;
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
/**
|
|
283
|
+
* Internal implementation for MovableListRef.
|
|
284
|
+
* Extends ListRefBaseInternals with LoroMovableList-specific methods.
|
|
285
|
+
*/
|
|
286
|
+
declare class MovableListRefInternals<NestedShape extends ContainerOrValueShape, Item = NestedShape["_plain"], MutableItem = NestedShape["_mutable"]> extends ListRefBaseInternals<NestedShape, Item, MutableItem> {
|
|
287
|
+
/** Absorb value at specific index for LoroMovableList */
|
|
288
|
+
absorbValueAtIndex(index: number, value: unknown): void;
|
|
289
|
+
/** Move an item from one index to another */
|
|
290
|
+
move(from: number, to: number): void;
|
|
291
|
+
/** Set an item at a specific index */
|
|
292
|
+
set(index: number, item: unknown): void;
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
/**
|
|
296
|
+
* Movable list typed ref - thin facade that delegates to MovableListRefInternals.
|
|
297
|
+
*/
|
|
298
|
+
declare class MovableListRef<NestedShape extends ContainerOrValueShape, Item = NestedShape["_plain"]> extends ListRefBase<NestedShape> {
|
|
299
|
+
[INTERNAL_SYMBOL]: MovableListRefInternals<NestedShape>;
|
|
300
|
+
[index: number]: InferMutableType<NestedShape> | undefined;
|
|
301
|
+
protected createInternals(params: TypedRefParams<any>): MovableListRefInternals<NestedShape>;
|
|
302
|
+
move(from: number, to: number): void;
|
|
303
|
+
set(index: number, item: Exclude<Item, Container>): void;
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
/**
|
|
307
|
+
* Internal implementation for RecordRef.
|
|
308
|
+
* Contains all logic, state, and implementation details.
|
|
309
|
+
*/
|
|
310
|
+
declare class RecordRefInternals<NestedShape extends ContainerOrValueShape> extends BaseRefInternals<any> {
|
|
311
|
+
private refCache;
|
|
312
|
+
/** Get typed ref params for creating child refs at a key */
|
|
313
|
+
getTypedRefParams(key: string, shape: ContainerShape): TypedRefParams<ContainerShape>;
|
|
314
|
+
/** Get a ref for a key without creating (returns undefined for non-existent container keys) */
|
|
315
|
+
getRef(key: string): unknown;
|
|
316
|
+
/** Get or create a ref for a key (always creates for container shapes) */
|
|
317
|
+
getOrCreateRef(key: string): unknown;
|
|
318
|
+
/** Set a value at a key */
|
|
319
|
+
set(key: string, value: any): void;
|
|
320
|
+
/** Delete a key */
|
|
321
|
+
delete(key: string): void;
|
|
322
|
+
/** Absorb mutated plain values back into Loro containers */
|
|
323
|
+
absorbPlainValues(): void;
|
|
324
|
+
/** Create the loro namespace for record */
|
|
325
|
+
protected createLoroNamespace(): LoroMapRef;
|
|
326
|
+
}
|
|
327
|
+
|
|
328
|
+
/**
|
|
329
|
+
* Record typed ref - thin facade that delegates to RecordRefInternals.
|
|
330
|
+
*/
|
|
331
|
+
declare class RecordRef<NestedShape extends ContainerOrValueShape> extends TypedRef<any> {
|
|
332
|
+
[key: string]: InferMutableType<NestedShape> | undefined | any;
|
|
333
|
+
[INTERNAL_SYMBOL]: RecordRefInternals<NestedShape>;
|
|
334
|
+
constructor(params: TypedRefParams<any>);
|
|
335
|
+
/** Set a value at a key */
|
|
336
|
+
set(key: string, value: any): void;
|
|
337
|
+
/** Delete a key */
|
|
338
|
+
delete(key: string): void;
|
|
339
|
+
get(key: string): InferMutableType<NestedShape> | undefined;
|
|
340
|
+
setContainer<C extends Container>(key: string, container: C): C;
|
|
341
|
+
has(key: string): boolean;
|
|
342
|
+
keys(): string[];
|
|
343
|
+
values(): any[];
|
|
344
|
+
get size(): number;
|
|
345
|
+
toJSON(): Record<string, Infer<NestedShape>>;
|
|
346
|
+
}
|
|
347
|
+
|
|
348
|
+
/**
|
|
349
|
+
* Typed ref for struct containers (objects with fixed keys).
|
|
350
|
+
* Uses LoroMap as the underlying container.
|
|
351
|
+
*
|
|
352
|
+
* Supports JavaScript-native object behavior:
|
|
353
|
+
* - Property access: obj.key
|
|
354
|
+
* - Property assignment: obj.key = value
|
|
355
|
+
* - Object.keys(obj)
|
|
356
|
+
* - 'key' in obj
|
|
357
|
+
* - delete obj.key
|
|
358
|
+
*
|
|
359
|
+
* @example
|
|
360
|
+
* ```typescript
|
|
361
|
+
* const schema = Shape.doc({
|
|
362
|
+
* settings: Shape.struct({
|
|
363
|
+
* darkMode: Shape.plain.boolean().placeholder(false),
|
|
364
|
+
* fontSize: Shape.plain.number().placeholder(14),
|
|
365
|
+
* }),
|
|
366
|
+
* });
|
|
367
|
+
*
|
|
368
|
+
* const doc = createTypedDoc(schema);
|
|
369
|
+
*
|
|
370
|
+
* // Property access
|
|
371
|
+
* doc.settings.darkMode = true;
|
|
372
|
+
* console.log(doc.settings.darkMode); // true
|
|
373
|
+
*
|
|
374
|
+
* // Object.keys()
|
|
375
|
+
* console.log(Object.keys(doc.settings)); // ['darkMode', 'fontSize']
|
|
376
|
+
*
|
|
377
|
+
* // 'key' in obj
|
|
378
|
+
* console.log('darkMode' in doc.settings); // true
|
|
379
|
+
*
|
|
380
|
+
* // delete obj.key
|
|
381
|
+
* delete doc.settings.darkMode;
|
|
382
|
+
*
|
|
383
|
+
* // CRDT access via loro()
|
|
384
|
+
* import { loro } from "@loro-extended/change";
|
|
385
|
+
* loro(doc.settings).setContainer('nested', loroMap);
|
|
386
|
+
* loro(doc.settings).subscribe(callback);
|
|
387
|
+
* ```
|
|
388
|
+
*/
|
|
389
|
+
type StructRef<NestedShapes extends Record<string, ContainerOrValueShape>> = {
|
|
390
|
+
[K in keyof NestedShapes]: NestedShapes[K]["_mutable"];
|
|
391
|
+
} & {
|
|
392
|
+
/**
|
|
393
|
+
* Serializes the struct to a plain JSON-compatible object.
|
|
394
|
+
*/
|
|
395
|
+
toJSON(): Infer<StructContainerShape<NestedShapes>>;
|
|
396
|
+
/**
|
|
397
|
+
* Internal methods accessed via INTERNAL_SYMBOL.
|
|
398
|
+
* @internal
|
|
399
|
+
*/
|
|
400
|
+
[INTERNAL_SYMBOL]: RefInternalsBase;
|
|
401
|
+
};
|
|
402
|
+
|
|
403
|
+
/**
|
|
404
|
+
* Internal implementation for TextRef.
|
|
405
|
+
* Contains all logic, state, and implementation details.
|
|
406
|
+
*/
|
|
407
|
+
declare class TextRefInternals extends BaseRefInternals<TextContainerShape> {
|
|
408
|
+
private materialized;
|
|
409
|
+
/** Insert text at the given index */
|
|
410
|
+
insert(index: number, content: string): void;
|
|
411
|
+
/** Delete text at the given index */
|
|
412
|
+
delete(index: number, len: number): void;
|
|
413
|
+
/** Update the entire text content */
|
|
414
|
+
update(text: string): void;
|
|
415
|
+
/** Mark a range of text with a key-value pair */
|
|
416
|
+
mark(range: {
|
|
417
|
+
start: number;
|
|
418
|
+
end: number;
|
|
419
|
+
}, key: string, value: any): void;
|
|
420
|
+
/** Remove a mark from a range of text */
|
|
421
|
+
unmark(range: {
|
|
422
|
+
start: number;
|
|
423
|
+
end: number;
|
|
424
|
+
}, key: string): void;
|
|
425
|
+
/** Apply a delta to the text */
|
|
426
|
+
applyDelta(delta: any[]): void;
|
|
427
|
+
/** Get the text as a string */
|
|
428
|
+
getStringValue(): string;
|
|
429
|
+
/** Get the text as a delta */
|
|
430
|
+
toDelta(): any[];
|
|
431
|
+
/** Get the length of the text */
|
|
432
|
+
getLength(): number;
|
|
433
|
+
/** No plain values in text */
|
|
434
|
+
absorbPlainValues(): void;
|
|
435
|
+
/** Create the loro namespace for text */
|
|
436
|
+
protected createLoroNamespace(): LoroTextRef;
|
|
437
|
+
}
|
|
438
|
+
|
|
439
|
+
/**
|
|
440
|
+
* Text typed ref - thin facade that delegates to TextRefInternals.
|
|
441
|
+
*/
|
|
442
|
+
declare class TextRef extends TypedRef<TextContainerShape> {
|
|
443
|
+
[INTERNAL_SYMBOL]: TextRefInternals;
|
|
444
|
+
constructor(params: TypedRefParams<TextContainerShape>);
|
|
445
|
+
/** Insert text at the given index */
|
|
446
|
+
insert(index: number, content: string): void;
|
|
447
|
+
/** Delete text at the given index */
|
|
448
|
+
delete(index: number, len: number): void;
|
|
449
|
+
/** Update the entire text content */
|
|
450
|
+
update(text: string): void;
|
|
451
|
+
/** Mark a range of text with a key-value pair */
|
|
452
|
+
mark(range: {
|
|
453
|
+
start: number;
|
|
454
|
+
end: number;
|
|
455
|
+
}, key: string, value: any): void;
|
|
456
|
+
/** Remove a mark from a range of text */
|
|
457
|
+
unmark(range: {
|
|
458
|
+
start: number;
|
|
459
|
+
end: number;
|
|
460
|
+
}, key: string): void;
|
|
461
|
+
/** Apply a delta to the text */
|
|
462
|
+
applyDelta(delta: any[]): void;
|
|
463
|
+
/** Get the text as a string */
|
|
464
|
+
toString(): string;
|
|
465
|
+
valueOf(): string;
|
|
466
|
+
toJSON(): string;
|
|
467
|
+
[Symbol.toPrimitive](_hint: string): string;
|
|
468
|
+
/** Get the text as a delta */
|
|
469
|
+
toDelta(): any[];
|
|
470
|
+
/** Get the length of the text */
|
|
471
|
+
get length(): number;
|
|
472
|
+
}
|
|
473
|
+
|
|
474
|
+
interface TreeRefLike<DataShape extends StructContainerShape> {
|
|
475
|
+
getOrCreateNodeRef(node: LoroTreeNode): TreeNodeRef<DataShape>;
|
|
476
|
+
}
|
|
477
|
+
/**
|
|
478
|
+
* Internal implementation for TreeNodeRef.
|
|
479
|
+
* Contains all logic, state, and implementation details.
|
|
480
|
+
*/
|
|
481
|
+
declare class TreeNodeRefInternals<DataShape extends StructContainerShape> implements RefInternalsBase {
|
|
482
|
+
private readonly params;
|
|
483
|
+
private dataRef;
|
|
484
|
+
constructor(params: TreeNodeRefParams<DataShape>);
|
|
485
|
+
/** Get the underlying LoroTreeNode */
|
|
486
|
+
getNode(): LoroTreeNode;
|
|
487
|
+
/** Get the data shape for this node */
|
|
488
|
+
getDataShape(): DataShape;
|
|
489
|
+
/** Get the parent TreeRef */
|
|
490
|
+
getTreeRef(): TreeRefLike<DataShape>;
|
|
491
|
+
/** Check if autoCommit is enabled */
|
|
492
|
+
getAutoCommit(): boolean;
|
|
493
|
+
/** Check if in batched mutation mode */
|
|
494
|
+
getBatchedMutation(): boolean;
|
|
495
|
+
/** Get the LoroDoc */
|
|
496
|
+
getDoc(): LoroDoc;
|
|
497
|
+
/** Commit changes if autoCommit is enabled */
|
|
498
|
+
commitIfAuto(): void;
|
|
499
|
+
/** Get or create the data StructRef */
|
|
500
|
+
getOrCreateDataRef(): StructRef<DataShape["shapes"]>;
|
|
501
|
+
/** Absorb mutated plain values back into Loro containers */
|
|
502
|
+
absorbPlainValues(): void;
|
|
503
|
+
}
|
|
504
|
+
|
|
505
|
+
interface TreeNodeRefParams<DataShape extends StructContainerShape> {
|
|
506
|
+
node: LoroTreeNode;
|
|
507
|
+
dataShape: DataShape;
|
|
508
|
+
treeRef: TreeRefLike<DataShape>;
|
|
509
|
+
autoCommit?: boolean;
|
|
510
|
+
batchedMutation?: boolean;
|
|
511
|
+
getDoc: () => LoroDoc;
|
|
512
|
+
}
|
|
513
|
+
/**
|
|
514
|
+
* Typed ref for a single tree node - thin facade that delegates to TreeNodeRefInternals.
|
|
515
|
+
* Provides type-safe access to node metadata via the `.data` property.
|
|
516
|
+
*
|
|
517
|
+
* **Note:** TreeNodeRef is not a subclass of TypedRef, but it implements
|
|
518
|
+
* `[INTERNAL_SYMBOL]: RefInternalsBase` for consistency with other refs.
|
|
519
|
+
* This allows internal code to call `absorbPlainValues()` uniformly
|
|
520
|
+
* across all ref types during the `change()` commit phase.
|
|
521
|
+
*
|
|
522
|
+
* @example
|
|
523
|
+
* ```typescript
|
|
524
|
+
* const node = tree.createNode({ name: "idle", facts: {} })
|
|
525
|
+
* node.data.name = "active" // Typed access
|
|
526
|
+
* const child = node.createNode({ name: "running", facts: {} })
|
|
527
|
+
* ```
|
|
528
|
+
*/
|
|
529
|
+
declare class TreeNodeRef<DataShape extends StructContainerShape> {
|
|
530
|
+
[INTERNAL_SYMBOL]: TreeNodeRefInternals<DataShape>;
|
|
531
|
+
constructor(params: TreeNodeRefParams<DataShape>);
|
|
532
|
+
/**
|
|
533
|
+
* The unique TreeID of this node.
|
|
534
|
+
*/
|
|
535
|
+
get id(): TreeID;
|
|
536
|
+
/**
|
|
537
|
+
* Typed access to the node's metadata.
|
|
538
|
+
* This is a StructRef wrapping the node's LoroMap data container.
|
|
539
|
+
*/
|
|
540
|
+
get data(): StructRef<DataShape["shapes"]> & {
|
|
541
|
+
[K in keyof DataShape["shapes"]]: DataShape["shapes"][K]["_mutable"];
|
|
542
|
+
};
|
|
543
|
+
/**
|
|
544
|
+
* Create a child node under this node.
|
|
545
|
+
*
|
|
546
|
+
* @param initialData - Optional partial data to initialize the child with
|
|
547
|
+
* @param index - Optional position among siblings
|
|
548
|
+
* @returns The created child TreeNodeRef
|
|
549
|
+
*/
|
|
550
|
+
createNode(initialData?: Partial<Infer<DataShape>>, index?: number): TreeNodeRef<DataShape>;
|
|
551
|
+
/**
|
|
552
|
+
* Get the parent node, if any.
|
|
553
|
+
*/
|
|
554
|
+
parent(): TreeNodeRef<DataShape> | undefined;
|
|
555
|
+
/**
|
|
556
|
+
* Get all child nodes in order.
|
|
557
|
+
*/
|
|
558
|
+
children(): TreeNodeRef<DataShape>[];
|
|
559
|
+
/**
|
|
560
|
+
* Move this node to a new parent.
|
|
561
|
+
*
|
|
562
|
+
* @param newParent - The new parent node (undefined for root)
|
|
563
|
+
* @param index - Optional position among siblings
|
|
564
|
+
*/
|
|
565
|
+
move(newParent?: TreeNodeRef<DataShape>, index?: number): void;
|
|
566
|
+
/**
|
|
567
|
+
* Move this node to be after the given sibling.
|
|
568
|
+
*/
|
|
569
|
+
moveAfter(sibling: TreeNodeRef<DataShape>): void;
|
|
570
|
+
/**
|
|
571
|
+
* Move this node to be before the given sibling.
|
|
572
|
+
*/
|
|
573
|
+
moveBefore(sibling: TreeNodeRef<DataShape>): void;
|
|
574
|
+
/**
|
|
575
|
+
* Get the index of this node among its siblings.
|
|
576
|
+
*/
|
|
577
|
+
index(): number | undefined;
|
|
578
|
+
/**
|
|
579
|
+
* Get the fractional index string for ordering.
|
|
580
|
+
*/
|
|
581
|
+
fractionalIndex(): string | undefined;
|
|
582
|
+
/**
|
|
583
|
+
* Check if this node has been deleted.
|
|
584
|
+
*/
|
|
585
|
+
isDeleted(): boolean;
|
|
586
|
+
/**
|
|
587
|
+
* Serialize this node and its descendants to JSON.
|
|
588
|
+
*/
|
|
589
|
+
toJSON(): {
|
|
590
|
+
id: TreeID;
|
|
591
|
+
parent: TreeID | null;
|
|
592
|
+
index: number;
|
|
593
|
+
fractionalIndex: string;
|
|
594
|
+
data: Infer<DataShape>;
|
|
595
|
+
children: any[];
|
|
596
|
+
};
|
|
597
|
+
}
|
|
598
|
+
|
|
599
|
+
/**
|
|
600
|
+
* Internal implementation for TreeRef.
|
|
601
|
+
* Contains all logic, state, and implementation details.
|
|
602
|
+
*/
|
|
603
|
+
declare class TreeRefInternals<DataShape extends StructContainerShape> extends BaseRefInternals<TreeContainerShape<DataShape>> {
|
|
604
|
+
private nodeCache;
|
|
605
|
+
private treeRef;
|
|
606
|
+
/** Set the parent TreeRef (needed for creating node refs) */
|
|
607
|
+
setTreeRef(treeRef: TreeRef<DataShape>): void;
|
|
608
|
+
/** Get the data shape for tree nodes */
|
|
609
|
+
getDataShape(): DataShape;
|
|
610
|
+
/** Get or create a node ref for a LoroTreeNode */
|
|
611
|
+
getOrCreateNodeRef(node: LoroTreeNode): TreeNodeRef<DataShape>;
|
|
612
|
+
/** Get a node by its ID */
|
|
613
|
+
getNodeByID(id: TreeID): TreeNodeRef<DataShape> | undefined;
|
|
614
|
+
/** Delete a node from the tree */
|
|
615
|
+
delete(target: TreeID | TreeNodeRef<DataShape>): void;
|
|
616
|
+
/** Absorb mutated plain values back into Loro containers */
|
|
617
|
+
absorbPlainValues(): void;
|
|
618
|
+
/** Create the loro namespace for tree */
|
|
619
|
+
protected createLoroNamespace(): LoroTreeRef;
|
|
620
|
+
}
|
|
621
|
+
|
|
622
|
+
/**
|
|
623
|
+
* Typed ref for tree (forest) containers - thin facade that delegates to TreeRefInternals.
|
|
624
|
+
* Wraps LoroTree with type-safe access to node metadata.
|
|
625
|
+
*
|
|
626
|
+
* @example
|
|
627
|
+
* ```typescript
|
|
628
|
+
* const StateNodeDataShape = Shape.struct({
|
|
629
|
+
* name: Shape.text(),
|
|
630
|
+
* facts: Shape.record(Shape.plain.any()),
|
|
631
|
+
* })
|
|
632
|
+
*
|
|
633
|
+
* doc.change(draft => {
|
|
634
|
+
* const root = draft.states.createNode({ name: "idle", facts: {} })
|
|
635
|
+
* const child = root.createNode({ name: "running", facts: {} })
|
|
636
|
+
* child.data.name = "active"
|
|
637
|
+
* })
|
|
638
|
+
* ```
|
|
639
|
+
*/
|
|
640
|
+
declare class TreeRef<DataShape extends StructContainerShape> extends TypedRef<TreeContainerShape<DataShape>> {
|
|
641
|
+
[INTERNAL_SYMBOL]: TreeRefInternals<DataShape>;
|
|
642
|
+
constructor(params: TypedRefParams<TreeContainerShape<DataShape>>);
|
|
643
|
+
/**
|
|
644
|
+
* Get the data shape for tree nodes.
|
|
645
|
+
*/
|
|
646
|
+
private get dataShape();
|
|
647
|
+
/**
|
|
648
|
+
* Get or create a node ref for a LoroTreeNode.
|
|
649
|
+
*/
|
|
650
|
+
getOrCreateNodeRef(node: LoroTreeNode): TreeNodeRef<DataShape>;
|
|
651
|
+
/**
|
|
652
|
+
* Get a node by its ID.
|
|
653
|
+
*/
|
|
654
|
+
getNodeByID(id: TreeID): TreeNodeRef<DataShape> | undefined;
|
|
655
|
+
/**
|
|
656
|
+
* Delete a node from the tree.
|
|
657
|
+
*/
|
|
658
|
+
delete(target: TreeID | TreeNodeRef<DataShape>): void;
|
|
659
|
+
/**
|
|
660
|
+
* Serialize the tree to a nested JSON structure.
|
|
661
|
+
* Each node includes its data and children recursively.
|
|
662
|
+
*/
|
|
663
|
+
toJSON(): Infer<TreeContainerShape<DataShape>>;
|
|
664
|
+
/**
|
|
665
|
+
* Create a new root node with optional initial data.
|
|
666
|
+
*
|
|
667
|
+
* @param initialData - Optional partial data to initialize the node with
|
|
668
|
+
* @returns The created TreeNodeRef
|
|
669
|
+
*/
|
|
670
|
+
createNode(initialData?: Partial<Infer<DataShape>>): TreeNodeRef<DataShape>;
|
|
671
|
+
/**
|
|
672
|
+
* Get all root nodes (nodes without parents).
|
|
673
|
+
* Returns nodes in their fractional index order.
|
|
674
|
+
*/
|
|
675
|
+
roots(): TreeNodeRef<DataShape>[];
|
|
676
|
+
/**
|
|
677
|
+
* Get all nodes in the tree (unordered).
|
|
678
|
+
* Includes all nodes, not just roots.
|
|
679
|
+
*/
|
|
680
|
+
nodes(): TreeNodeRef<DataShape>[];
|
|
681
|
+
/**
|
|
682
|
+
* Check if a node with the given ID exists in the tree.
|
|
683
|
+
*/
|
|
684
|
+
has(id: TreeID): boolean;
|
|
685
|
+
/**
|
|
686
|
+
* Enable fractional index generation for ordering.
|
|
687
|
+
*
|
|
688
|
+
* @param jitter - Optional jitter value to avoid conflicts (0 = no jitter)
|
|
689
|
+
*/
|
|
690
|
+
enableFractionalIndex(jitter?: number): void;
|
|
691
|
+
/**
|
|
692
|
+
* Transform Loro's native JSON format to our typed format.
|
|
693
|
+
*/
|
|
694
|
+
private transformNativeJson;
|
|
695
|
+
/**
|
|
696
|
+
* Get a flat array representation of all nodes.
|
|
697
|
+
* Flattens the nested tree structure into a single array.
|
|
698
|
+
*/
|
|
699
|
+
toArray(): Array<{
|
|
700
|
+
id: TreeID;
|
|
701
|
+
parent: TreeID | null;
|
|
702
|
+
index: number;
|
|
703
|
+
fractionalIndex: string;
|
|
704
|
+
data: Infer<DataShape>;
|
|
705
|
+
}>;
|
|
706
|
+
}
|
|
707
|
+
|
|
708
|
+
/**
|
|
709
|
+
* The `loro()` function - single escape hatch for CRDT internals.
|
|
710
|
+
*
|
|
711
|
+
* Design Principle:
|
|
712
|
+
* > If it takes a plain JavaScript value, keep it on the ref.
|
|
713
|
+
* > If it takes a Loro container or exposes CRDT internals, move to `loro()`.
|
|
714
|
+
*
|
|
715
|
+
* @example
|
|
716
|
+
* ```typescript
|
|
717
|
+
* import { loro } from "@loro-extended/change"
|
|
718
|
+
*
|
|
719
|
+
* // Access underlying LoroDoc
|
|
720
|
+
* loro(ref).doc
|
|
721
|
+
*
|
|
722
|
+
* // Access underlying Loro container (correctly typed)
|
|
723
|
+
* loro(ref).container // LoroList, LoroMap, LoroText, etc.
|
|
724
|
+
*
|
|
725
|
+
* // Subscribe to changes
|
|
726
|
+
* loro(ref).subscribe(callback)
|
|
727
|
+
*
|
|
728
|
+
* // Container operations
|
|
729
|
+
* loro(list).pushContainer(loroMap)
|
|
730
|
+
* loro(struct).setContainer('key', loroMap)
|
|
731
|
+
* ```
|
|
732
|
+
*/
|
|
733
|
+
|
|
734
|
+
/**
|
|
735
|
+
* Well-known Symbol for loro() access.
|
|
736
|
+
* This is exported so advanced users can access it directly if needed.
|
|
737
|
+
*/
|
|
738
|
+
declare const LORO_SYMBOL: unique symbol;
|
|
739
|
+
/**
|
|
740
|
+
* Base interface for all loro() return types.
|
|
741
|
+
* Provides access to the underlying LoroDoc, container, and subscription.
|
|
742
|
+
*/
|
|
743
|
+
interface LoroRefBase {
|
|
744
|
+
/** The underlying LoroDoc */
|
|
745
|
+
readonly doc: LoroDoc;
|
|
746
|
+
/** The underlying Loro container */
|
|
747
|
+
readonly container: unknown;
|
|
748
|
+
/**
|
|
749
|
+
* Subscribe to container-level changes.
|
|
750
|
+
* @param callback - Function called when the container changes
|
|
751
|
+
* @returns Subscription that can be used to unsubscribe
|
|
752
|
+
*/
|
|
753
|
+
subscribe(callback: (event: unknown) => void): Subscription;
|
|
754
|
+
}
|
|
755
|
+
/**
|
|
756
|
+
* loro() return type for ListRef and MovableListRef.
|
|
757
|
+
* Provides container operations that take Loro containers.
|
|
758
|
+
*/
|
|
759
|
+
interface LoroListRef extends LoroRefBase {
|
|
760
|
+
/** The underlying LoroList or LoroMovableList */
|
|
761
|
+
readonly container: LoroList | LoroMovableList;
|
|
762
|
+
/**
|
|
763
|
+
* Push a Loro container to the end of the list.
|
|
764
|
+
* Use this when you need to add a pre-existing container.
|
|
765
|
+
*/
|
|
766
|
+
pushContainer(container: Container): Container;
|
|
767
|
+
/**
|
|
768
|
+
* Insert a Loro container at the specified index.
|
|
769
|
+
* Use this when you need to insert a pre-existing container.
|
|
770
|
+
*/
|
|
771
|
+
insertContainer(index: number, container: Container): Container;
|
|
772
|
+
}
|
|
773
|
+
/**
|
|
774
|
+
* loro() return type for StructRef and RecordRef.
|
|
775
|
+
* Provides container operations that take Loro containers.
|
|
776
|
+
*/
|
|
777
|
+
interface LoroMapRef extends LoroRefBase {
|
|
778
|
+
/** The underlying LoroMap */
|
|
779
|
+
readonly container: LoroMap;
|
|
780
|
+
/**
|
|
781
|
+
* Set a Loro container at the specified key.
|
|
782
|
+
* Use this when you need to set a pre-existing container.
|
|
783
|
+
*/
|
|
784
|
+
setContainer(key: string, container: Container): Container;
|
|
785
|
+
}
|
|
786
|
+
/**
|
|
787
|
+
* loro() return type for TextRef.
|
|
788
|
+
*/
|
|
789
|
+
interface LoroTextRef extends LoroRefBase {
|
|
790
|
+
/** The underlying LoroText */
|
|
791
|
+
readonly container: LoroText;
|
|
792
|
+
}
|
|
793
|
+
/**
|
|
794
|
+
* loro() return type for CounterRef.
|
|
795
|
+
*/
|
|
796
|
+
interface LoroCounterRef extends LoroRefBase {
|
|
797
|
+
/** The underlying LoroCounter */
|
|
798
|
+
readonly container: LoroCounter;
|
|
799
|
+
}
|
|
800
|
+
/**
|
|
801
|
+
* loro() return type for TreeRef.
|
|
802
|
+
*/
|
|
803
|
+
interface LoroTreeRef extends LoroRefBase {
|
|
804
|
+
/** The underlying LoroTree */
|
|
805
|
+
readonly container: LoroTree;
|
|
806
|
+
}
|
|
807
|
+
/**
|
|
808
|
+
* loro() return type for TypedDoc.
|
|
809
|
+
* Provides access to doc-level operations.
|
|
810
|
+
*/
|
|
811
|
+
interface LoroTypedDocRef extends LoroRefBase {
|
|
812
|
+
/** The underlying LoroDoc (same as doc for TypedDoc) */
|
|
813
|
+
readonly container: LoroDoc;
|
|
814
|
+
/**
|
|
815
|
+
* Apply JSON Patch operations to the document.
|
|
816
|
+
* @param patch - Array of JSON Patch operations (RFC 6902)
|
|
817
|
+
* @param pathPrefix - Optional path prefix for scoped operations
|
|
818
|
+
*/
|
|
819
|
+
applyPatch(patch: JsonPatch, pathPrefix?: (string | number)[]): void;
|
|
820
|
+
/** Access the document schema shape */
|
|
821
|
+
readonly docShape: DocShape;
|
|
822
|
+
/** Get raw CRDT value without placeholder overlay */
|
|
823
|
+
readonly rawValue: unknown;
|
|
824
|
+
}
|
|
825
|
+
/**
|
|
826
|
+
* Access CRDT internals for a ListRef.
|
|
827
|
+
*/
|
|
828
|
+
declare function loro<NestedShape extends ContainerShape>(ref: ListRef<NestedShape>): LoroListRef;
|
|
829
|
+
/**
|
|
830
|
+
* Access CRDT internals for a MovableListRef.
|
|
831
|
+
*/
|
|
832
|
+
declare function loro<NestedShape extends ContainerShape>(ref: MovableListRef<NestedShape>): LoroListRef;
|
|
833
|
+
/**
|
|
834
|
+
* Access CRDT internals for a StructRef.
|
|
835
|
+
*/
|
|
836
|
+
declare function loro<NestedShapes extends Record<string, ContainerOrValueShape>>(ref: StructRef<NestedShapes>): LoroMapRef;
|
|
837
|
+
/**
|
|
838
|
+
* Access CRDT internals for a RecordRef.
|
|
839
|
+
*/
|
|
840
|
+
declare function loro<NestedShape extends ContainerShape>(ref: RecordRef<NestedShape>): LoroMapRef;
|
|
841
|
+
/**
|
|
842
|
+
* Access CRDT internals for a TextRef.
|
|
843
|
+
*/
|
|
844
|
+
declare function loro(ref: TextRef): LoroTextRef;
|
|
845
|
+
/**
|
|
846
|
+
* Access CRDT internals for a CounterRef.
|
|
847
|
+
*/
|
|
848
|
+
declare function loro(ref: CounterRef): LoroCounterRef;
|
|
849
|
+
/**
|
|
850
|
+
* Access CRDT internals for a TreeRef.
|
|
851
|
+
*/
|
|
852
|
+
declare function loro<DataShape extends StructContainerShape>(ref: TreeRef<DataShape>): LoroTreeRef;
|
|
853
|
+
/**
|
|
854
|
+
* Access CRDT internals for a TypedDoc.
|
|
855
|
+
*/
|
|
856
|
+
declare function loro<Shape extends DocShape>(doc: TypedDoc<Shape>): LoroTypedDocRef;
|
|
857
|
+
/**
|
|
858
|
+
* Access CRDT internals for any TypedRef.
|
|
859
|
+
*/
|
|
860
|
+
declare function loro<Shape extends ContainerShape>(ref: TypedRef<Shape>): LoroRefBase;
|
|
861
|
+
|
|
862
|
+
/**
|
|
863
|
+
* Symbol for internal methods that should not be enumerable or accessible to users.
|
|
864
|
+
* Used to hide implementation details like absorbPlainValues(), getTypedRefParams(), etc.
|
|
865
|
+
*
|
|
866
|
+
* This achieves Success Criteria #7 from loro-api-refactor.md:
|
|
867
|
+
* "Internal methods hidden - Via Symbol, not enumerable"
|
|
868
|
+
*/
|
|
869
|
+
declare const INTERNAL_SYMBOL: unique symbol;
|
|
870
|
+
/**
|
|
871
|
+
* Minimal interface for refs that only need absorbPlainValues.
|
|
872
|
+
* Used by TreeNodeRef which doesn't extend TypedRef.
|
|
873
|
+
*/
|
|
874
|
+
interface RefInternalsBase {
|
|
875
|
+
/** Absorb mutated plain values back into Loro containers */
|
|
876
|
+
absorbPlainValues(): void;
|
|
877
|
+
}
|
|
74
878
|
type TypedRefParams<Shape extends DocShape | ContainerShape> = {
|
|
75
879
|
shape: Shape;
|
|
76
880
|
placeholder?: Infer<Shape>;
|
|
77
881
|
getContainer: () => ShapeToContainer<Shape>;
|
|
78
882
|
autoCommit?: boolean;
|
|
79
883
|
batchedMutation?: boolean;
|
|
80
|
-
getDoc
|
|
884
|
+
getDoc: () => LoroDoc;
|
|
81
885
|
};
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
886
|
+
/**
|
|
887
|
+
* Abstract base class for all ref internal implementations.
|
|
888
|
+
* Contains shared logic that was previously in TypedRef.createBaseInternals().
|
|
889
|
+
*
|
|
890
|
+
* Subclasses implement specific behavior for each ref type.
|
|
891
|
+
*/
|
|
892
|
+
declare abstract class BaseRefInternals<Shape extends DocShape | ContainerShape> implements RefInternalsBase {
|
|
893
|
+
protected readonly params: TypedRefParams<Shape>;
|
|
894
|
+
protected cachedContainer: ShapeToContainer<Shape> | undefined;
|
|
895
|
+
protected loroNamespace: LoroRefBase | undefined;
|
|
896
|
+
constructor(params: TypedRefParams<Shape>);
|
|
897
|
+
/** Get the underlying Loro container (cached) */
|
|
898
|
+
getContainer(): ShapeToContainer<Shape>;
|
|
899
|
+
/** Commit changes if autoCommit is enabled */
|
|
900
|
+
commitIfAuto(): void;
|
|
901
|
+
/** Get the shape for this ref */
|
|
902
|
+
getShape(): Shape;
|
|
903
|
+
/** Get the placeholder value */
|
|
904
|
+
getPlaceholder(): Infer<Shape> | undefined;
|
|
905
|
+
/** Check if autoCommit is enabled */
|
|
906
|
+
getAutoCommit(): boolean;
|
|
907
|
+
/** Check if in batched mutation mode */
|
|
908
|
+
getBatchedMutation(): boolean;
|
|
909
|
+
/** Get the LoroDoc */
|
|
910
|
+
getDoc(): LoroDoc;
|
|
911
|
+
/** Get the loro namespace (cached) */
|
|
912
|
+
getLoroNamespace(): LoroRefBase;
|
|
913
|
+
/** Absorb mutated plain values back into Loro containers - subclasses override */
|
|
86
914
|
abstract absorbPlainValues(): void;
|
|
915
|
+
/** Create the loro() namespace object - subclasses override for specific types */
|
|
916
|
+
protected createLoroNamespace(): LoroRefBase;
|
|
917
|
+
}
|
|
918
|
+
/**
|
|
919
|
+
* Base class for all typed refs.
|
|
920
|
+
*
|
|
921
|
+
* All internal methods are accessed via [INTERNAL_SYMBOL] to prevent
|
|
922
|
+
* namespace collisions with user data properties.
|
|
923
|
+
*
|
|
924
|
+
* Uses the Facade + Implementation pattern:
|
|
925
|
+
* - TypedRef is the thin public facade
|
|
926
|
+
* - BaseRefInternals subclasses contain all implementation logic
|
|
927
|
+
*/
|
|
928
|
+
declare abstract class TypedRef<Shape extends DocShape | ContainerShape> {
|
|
929
|
+
/**
|
|
930
|
+
* Internal implementation accessed via Symbol.
|
|
931
|
+
* Subclasses must set this to their specific internals class instance.
|
|
932
|
+
*/
|
|
933
|
+
abstract [INTERNAL_SYMBOL]: BaseRefInternals<Shape>;
|
|
87
934
|
/**
|
|
88
935
|
* Serializes the ref to a plain JSON-compatible value.
|
|
89
936
|
* Returns the plain type inferred from the shape.
|
|
90
937
|
*/
|
|
91
938
|
abstract toJSON(): Infer<Shape>;
|
|
92
|
-
protected get shape(): Shape;
|
|
93
|
-
protected get placeholder(): Infer<Shape> | undefined;
|
|
94
|
-
protected get autoCommit(): boolean;
|
|
95
|
-
protected get batchedMutation(): boolean;
|
|
96
|
-
protected get doc(): LoroDoc | undefined;
|
|
97
939
|
/**
|
|
98
|
-
*
|
|
99
|
-
*
|
|
940
|
+
* Access the loro() namespace via the well-known symbol.
|
|
941
|
+
* This is used by the loro() function to access CRDT internals.
|
|
100
942
|
*/
|
|
101
|
-
|
|
102
|
-
protected get container(): ShapeToContainer<Shape>;
|
|
943
|
+
get [LORO_SYMBOL](): LoroRefBase;
|
|
103
944
|
}
|
|
104
945
|
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
946
|
+
/**
|
|
947
|
+
* Internal implementation for CounterRef.
|
|
948
|
+
* Contains all logic, state, and implementation details.
|
|
949
|
+
*/
|
|
950
|
+
declare class CounterRefInternals extends BaseRefInternals<CounterContainerShape> {
|
|
951
|
+
private materialized;
|
|
952
|
+
/** Increment the counter value */
|
|
109
953
|
increment(value?: number): void;
|
|
954
|
+
/** Decrement the counter value */
|
|
110
955
|
decrement(value?: number): void;
|
|
111
|
-
/**
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
* returns the placeholder value if available.
|
|
115
|
-
*/
|
|
116
|
-
get value(): number;
|
|
117
|
-
valueOf(): number;
|
|
118
|
-
toJSON(): number;
|
|
119
|
-
[Symbol.toPrimitive](hint: string): number | string;
|
|
120
|
-
}
|
|
121
|
-
|
|
122
|
-
declare abstract class ListRefBase<NestedShape extends ContainerOrValueShape, Item = NestedShape["_plain"], MutableItem = NestedShape["_mutable"]> extends TypedRef<any> {
|
|
123
|
-
private itemCache;
|
|
124
|
-
protected get container(): LoroList | LoroMovableList;
|
|
125
|
-
protected get shape(): ListContainerShape<NestedShape> | MovableListContainerShape<NestedShape>;
|
|
126
|
-
absorbPlainValues(): void;
|
|
127
|
-
protected abstract absorbValueAtIndex(index: number, value: any): void;
|
|
128
|
-
protected insertWithConversion(index: number, item: Item): void;
|
|
129
|
-
protected pushWithConversion(item: Item): void;
|
|
130
|
-
getTypedRefParams(index: number, shape: ContainerShape): TypedRefParams<ContainerShape>;
|
|
131
|
-
protected getPredicateItem(index: number): Item;
|
|
132
|
-
protected getMutableItem(index: number): any;
|
|
133
|
-
find(predicate: (item: Item, index: number) => boolean): MutableItem | undefined;
|
|
134
|
-
findIndex(predicate: (item: Item, index: number) => boolean): number;
|
|
135
|
-
map<ReturnType>(callback: (item: Item, index: number) => ReturnType): ReturnType[];
|
|
136
|
-
filter(predicate: (item: Item, index: number) => boolean): MutableItem[];
|
|
137
|
-
forEach(callback: (item: Item, index: number) => void): void;
|
|
138
|
-
some(predicate: (item: Item, index: number) => boolean): boolean;
|
|
139
|
-
every(predicate: (item: Item, index: number) => boolean): boolean;
|
|
140
|
-
slice(start?: number, end?: number): MutableItem[];
|
|
141
|
-
insert(index: number, item: Item): void;
|
|
142
|
-
delete(index: number, len: number): void;
|
|
143
|
-
push(item: Item): void;
|
|
144
|
-
pushContainer(container: Container): Container;
|
|
145
|
-
insertContainer(index: number, container: Container): Container;
|
|
146
|
-
get(index: number): MutableItem | undefined;
|
|
147
|
-
toArray(): Item[];
|
|
148
|
-
toJSON(): Item[];
|
|
149
|
-
[Symbol.iterator](): IterableIterator<MutableItem>;
|
|
150
|
-
get length(): number;
|
|
151
|
-
private updateCacheForDelete;
|
|
152
|
-
private updateCacheForInsert;
|
|
153
|
-
}
|
|
154
|
-
|
|
155
|
-
declare class ListRef<NestedShape extends ContainerOrValueShape> extends ListRefBase<NestedShape> {
|
|
156
|
-
[index: number]: InferMutableType<NestedShape> | undefined;
|
|
157
|
-
protected get container(): LoroList;
|
|
158
|
-
protected absorbValueAtIndex(index: number, value: any): void;
|
|
159
|
-
}
|
|
160
|
-
|
|
161
|
-
declare class MovableListRef<NestedShape extends ContainerOrValueShape, Item = NestedShape["_plain"]> extends ListRefBase<NestedShape> {
|
|
162
|
-
[index: number]: InferMutableType<NestedShape> | undefined;
|
|
163
|
-
protected get container(): LoroMovableList;
|
|
164
|
-
protected absorbValueAtIndex(index: number, value: any): void;
|
|
165
|
-
move(from: number, to: number): void;
|
|
166
|
-
set(index: number, item: Exclude<Item, Container>): void;
|
|
167
|
-
}
|
|
168
|
-
|
|
169
|
-
declare class RecordRef<NestedShape extends ContainerOrValueShape> extends TypedRef<any> {
|
|
170
|
-
[key: string]: InferMutableType<NestedShape> | undefined | any;
|
|
171
|
-
private refCache;
|
|
172
|
-
protected get shape(): RecordContainerShape<NestedShape>;
|
|
173
|
-
protected get container(): LoroMap;
|
|
956
|
+
/** Get the current counter value */
|
|
957
|
+
getValue(): number;
|
|
958
|
+
/** No plain values in counter */
|
|
174
959
|
absorbPlainValues(): void;
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
* Gets an existing ref for a key, or returns undefined if the key doesn't exist.
|
|
178
|
-
* Used for reading operations where we want optional chaining to work.
|
|
179
|
-
*/
|
|
180
|
-
getRef(key: string): any;
|
|
181
|
-
/**
|
|
182
|
-
* Gets or creates a ref for a key.
|
|
183
|
-
* Always creates the container if it doesn't exist.
|
|
184
|
-
* This is the method used for write operations.
|
|
185
|
-
*/
|
|
186
|
-
getOrCreateRef(key: string): any;
|
|
187
|
-
get(key: string): InferMutableType<NestedShape> | undefined;
|
|
188
|
-
set(key: string, value: any): void;
|
|
189
|
-
setContainer<C extends Container>(key: string, container: C): C;
|
|
190
|
-
delete(key: string): void;
|
|
191
|
-
has(key: string): boolean;
|
|
192
|
-
keys(): string[];
|
|
193
|
-
values(): any[];
|
|
194
|
-
get size(): number;
|
|
195
|
-
toJSON(): Record<string, Infer<NestedShape>>;
|
|
960
|
+
/** Create the loro namespace for counter */
|
|
961
|
+
protected createLoroNamespace(): LoroCounterRef;
|
|
196
962
|
}
|
|
197
963
|
|
|
198
964
|
/**
|
|
199
|
-
*
|
|
200
|
-
* Uses LoroMap as the underlying container.
|
|
965
|
+
* Counter typed ref - thin facade that delegates to CounterRefInternals.
|
|
201
966
|
*/
|
|
202
|
-
declare class
|
|
203
|
-
|
|
204
|
-
constructor(params: TypedRefParams<
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
setContainer<C extends Container>(key: string, container: C): C;
|
|
215
|
-
delete(key: string): void;
|
|
216
|
-
has(key: string): boolean;
|
|
217
|
-
keys(): string[];
|
|
218
|
-
values(): any[];
|
|
219
|
-
get size(): number;
|
|
220
|
-
}
|
|
221
|
-
|
|
222
|
-
declare class TextRef extends TypedRef<TextContainerShape> {
|
|
223
|
-
private _materialized;
|
|
224
|
-
protected get container(): LoroText;
|
|
225
|
-
absorbPlainValues(): void;
|
|
226
|
-
insert(index: number, content: string): void;
|
|
227
|
-
delete(index: number, len: number): void;
|
|
228
|
-
/**
|
|
229
|
-
* Returns the text content.
|
|
230
|
-
* If the text hasn't been materialized (no operations performed),
|
|
231
|
-
* returns the placeholder value if available.
|
|
232
|
-
*/
|
|
233
|
-
toString(): string;
|
|
234
|
-
valueOf(): string;
|
|
235
|
-
toJSON(): string;
|
|
236
|
-
[Symbol.toPrimitive](_hint: string): string;
|
|
237
|
-
update(text: string): void;
|
|
238
|
-
mark(range: {
|
|
239
|
-
start: number;
|
|
240
|
-
end: number;
|
|
241
|
-
}, key: string, value: any): void;
|
|
242
|
-
unmark(range: {
|
|
243
|
-
start: number;
|
|
244
|
-
end: number;
|
|
245
|
-
}, key: string): void;
|
|
246
|
-
toDelta(): any[];
|
|
247
|
-
applyDelta(delta: any[]): void;
|
|
248
|
-
get length(): number;
|
|
967
|
+
declare class CounterRef extends TypedRef<CounterContainerShape> {
|
|
968
|
+
[INTERNAL_SYMBOL]: CounterRefInternals;
|
|
969
|
+
constructor(params: TypedRefParams<CounterContainerShape>);
|
|
970
|
+
/** Increment the counter by the given value (default 1) */
|
|
971
|
+
increment(value?: number): void;
|
|
972
|
+
/** Decrement the counter by the given value (default 1) */
|
|
973
|
+
decrement(value?: number): void;
|
|
974
|
+
/** Get the current counter value */
|
|
975
|
+
get value(): number;
|
|
976
|
+
valueOf(): number;
|
|
977
|
+
toJSON(): number;
|
|
978
|
+
[Symbol.toPrimitive](hint: string): number | string;
|
|
249
979
|
}
|
|
250
980
|
|
|
251
981
|
type WithPlaceholder<S extends Shape<any, any, any>> = S & {
|
|
@@ -286,14 +1016,43 @@ type TreeNodeJSON<DataShape extends StructContainerShape> = {
|
|
|
286
1016
|
data: DataShape["_plain"];
|
|
287
1017
|
children: TreeNodeJSON<DataShape>[];
|
|
288
1018
|
};
|
|
1019
|
+
/**
|
|
1020
|
+
* Interface describing the TreeRef API for use in shape definitions.
|
|
1021
|
+
* This avoids circular type references that would occur with the TreeRef class.
|
|
1022
|
+
* @internal
|
|
1023
|
+
*/
|
|
1024
|
+
interface TreeRefInterface<DataShape extends StructContainerShape> {
|
|
1025
|
+
/** Get or create a node ref for a LoroTreeNode */
|
|
1026
|
+
getOrCreateNodeRef(node: unknown): TreeNodeRef<DataShape>;
|
|
1027
|
+
/** Get a node by its ID */
|
|
1028
|
+
getNodeByID(id: TreeID): TreeNodeRef<DataShape> | undefined;
|
|
1029
|
+
/** Delete a node from the tree */
|
|
1030
|
+
delete(target: TreeID | TreeNodeRef<DataShape>): void;
|
|
1031
|
+
/** Serialize the tree to a nested JSON structure */
|
|
1032
|
+
toJSON(): TreeNodeJSON<DataShape>[];
|
|
1033
|
+
/** Create a new root node with optional initial data */
|
|
1034
|
+
createNode(initialData?: Partial<DataShape["_plain"]>): TreeNodeRef<DataShape>;
|
|
1035
|
+
/** Get all root nodes (nodes without parents) */
|
|
1036
|
+
roots(): TreeNodeRef<DataShape>[];
|
|
1037
|
+
/** Get all nodes in the tree (unordered) */
|
|
1038
|
+
nodes(): TreeNodeRef<DataShape>[];
|
|
1039
|
+
/** Check if a node with the given ID exists in the tree */
|
|
1040
|
+
has(id: TreeID): boolean;
|
|
1041
|
+
/** Enable fractional index generation for ordering */
|
|
1042
|
+
enableFractionalIndex(jitter?: number): void;
|
|
1043
|
+
/** Get a flat array representation of all nodes */
|
|
1044
|
+
toArray(): Array<{
|
|
1045
|
+
id: TreeID;
|
|
1046
|
+
parent: TreeID | null;
|
|
1047
|
+
index: number;
|
|
1048
|
+
fractionalIndex: string;
|
|
1049
|
+
data: DataShape["_plain"];
|
|
1050
|
+
}>;
|
|
1051
|
+
}
|
|
289
1052
|
/**
|
|
290
1053
|
* Container shape for tree (forest) structures.
|
|
291
1054
|
* Each node in the tree has typed metadata stored in a LoroMap.
|
|
292
1055
|
*
|
|
293
|
-
* Note: The Mutable type (second generic parameter) is `any` here to avoid
|
|
294
|
-
* circular dependency with TreeRef. The actual type is resolved at runtime
|
|
295
|
-
* and through the InferMutableType helper.
|
|
296
|
-
*
|
|
297
1056
|
* @example
|
|
298
1057
|
* ```typescript
|
|
299
1058
|
* const StateNodeDataShape = Shape.struct({
|
|
@@ -306,7 +1065,7 @@ type TreeNodeJSON<DataShape extends StructContainerShape> = {
|
|
|
306
1065
|
* })
|
|
307
1066
|
* ```
|
|
308
1067
|
*/
|
|
309
|
-
interface TreeContainerShape<DataShape extends StructContainerShape = StructContainerShape> extends Shape<TreeNodeJSON<DataShape>[],
|
|
1068
|
+
interface TreeContainerShape<DataShape extends StructContainerShape = StructContainerShape> extends Shape<TreeNodeJSON<DataShape>[], TreeRefInterface<DataShape>, never[]> {
|
|
310
1069
|
readonly _type: "tree";
|
|
311
1070
|
/**
|
|
312
1071
|
* The shape of each node's data (metadata).
|
|
@@ -528,7 +1287,7 @@ declare const Shape: {
|
|
|
528
1287
|
* states: Shape.tree(StateNodeDataShape),
|
|
529
1288
|
* })
|
|
530
1289
|
*
|
|
531
|
-
* doc
|
|
1290
|
+
* doc.change(draft => {
|
|
532
1291
|
* const root = draft.states.createNode({ name: "idle", facts: {} })
|
|
533
1292
|
* const child = root.createNode({ name: "running", facts: {} })
|
|
534
1293
|
* child.data.name = "active"
|
|
@@ -634,180 +1393,6 @@ declare function derivePlaceholder<T extends DocShape>(schema: T): InferPlacehol
|
|
|
634
1393
|
*/
|
|
635
1394
|
declare function deriveShapePlaceholder(shape: ContainerOrValueShape): unknown;
|
|
636
1395
|
|
|
637
|
-
/** biome-ignore-all lint/suspicious/noExplicitAny: JSON Patch values can be any type */
|
|
638
|
-
|
|
639
|
-
type JsonPatchAddOperation = {
|
|
640
|
-
op: "add";
|
|
641
|
-
path: string | (string | number)[];
|
|
642
|
-
value: any;
|
|
643
|
-
};
|
|
644
|
-
type JsonPatchRemoveOperation = {
|
|
645
|
-
op: "remove";
|
|
646
|
-
path: string | (string | number)[];
|
|
647
|
-
};
|
|
648
|
-
type JsonPatchReplaceOperation = {
|
|
649
|
-
op: "replace";
|
|
650
|
-
path: string | (string | number)[];
|
|
651
|
-
value: any;
|
|
652
|
-
};
|
|
653
|
-
type JsonPatchMoveOperation = {
|
|
654
|
-
op: "move";
|
|
655
|
-
path: string | (string | number)[];
|
|
656
|
-
from: string | (string | number)[];
|
|
657
|
-
};
|
|
658
|
-
type JsonPatchCopyOperation = {
|
|
659
|
-
op: "copy";
|
|
660
|
-
path: string | (string | number)[];
|
|
661
|
-
from: string | (string | number)[];
|
|
662
|
-
};
|
|
663
|
-
type JsonPatchTestOperation = {
|
|
664
|
-
op: "test";
|
|
665
|
-
path: string | (string | number)[];
|
|
666
|
-
value: any;
|
|
667
|
-
};
|
|
668
|
-
type JsonPatchOperation = JsonPatchAddOperation | JsonPatchRemoveOperation | JsonPatchReplaceOperation | JsonPatchMoveOperation | JsonPatchCopyOperation | JsonPatchTestOperation;
|
|
669
|
-
type JsonPatch = JsonPatchOperation[];
|
|
670
|
-
|
|
671
|
-
/** biome-ignore-all lint/suspicious/noExplicitAny: fix later */
|
|
672
|
-
|
|
673
|
-
/**
|
|
674
|
-
* Meta-operations namespace for TypedDoc.
|
|
675
|
-
* Access via doc.$ to perform batch operations, serialization, etc.
|
|
676
|
-
*/
|
|
677
|
-
declare class TypedDocMeta<Shape extends DocShape> {
|
|
678
|
-
private internal;
|
|
679
|
-
constructor(internal: TypedDocInternal<Shape>);
|
|
680
|
-
/**
|
|
681
|
-
* The primary method of mutating typed documents.
|
|
682
|
-
* Batches multiple mutations into a single transaction.
|
|
683
|
-
* All changes commit together at the end.
|
|
684
|
-
*
|
|
685
|
-
* Use this for:
|
|
686
|
-
* - Find-and-mutate operations (required due to JS limitations)
|
|
687
|
-
* - Performance (fewer commits)
|
|
688
|
-
* - Atomic undo (all changes = one undo step)
|
|
689
|
-
*
|
|
690
|
-
* Returns the doc for chaining.
|
|
691
|
-
*/
|
|
692
|
-
change(fn: (draft: Mutable<Shape>) => void): TypedDoc<Shape>;
|
|
693
|
-
/**
|
|
694
|
-
* Returns the full plain JavaScript object representation of the document.
|
|
695
|
-
* This is an expensive O(N) operation that serializes the entire document.
|
|
696
|
-
*/
|
|
697
|
-
toJSON(): Infer<Shape>;
|
|
698
|
-
/**
|
|
699
|
-
* Apply JSON Patch operations to the document
|
|
700
|
-
*
|
|
701
|
-
* @param patch - Array of JSON Patch operations (RFC 6902)
|
|
702
|
-
* @param pathPrefix - Optional path prefix for scoped operations
|
|
703
|
-
* @returns Updated document value
|
|
704
|
-
*/
|
|
705
|
-
applyPatch(patch: JsonPatch, pathPrefix?: (string | number)[]): TypedDoc<Shape>;
|
|
706
|
-
/**
|
|
707
|
-
* Access the underlying LoroDoc for advanced operations.
|
|
708
|
-
*/
|
|
709
|
-
get loroDoc(): LoroDoc;
|
|
710
|
-
/**
|
|
711
|
-
* Access the document schema shape.
|
|
712
|
-
*/
|
|
713
|
-
get docShape(): Shape;
|
|
714
|
-
/**
|
|
715
|
-
* Get raw CRDT value without placeholder overlay.
|
|
716
|
-
*/
|
|
717
|
-
get rawValue(): any;
|
|
718
|
-
}
|
|
719
|
-
/**
|
|
720
|
-
* Internal TypedDoc implementation (not directly exposed to users).
|
|
721
|
-
* Users interact with the proxied version that provides direct schema access.
|
|
722
|
-
*/
|
|
723
|
-
declare class TypedDocInternal<Shape extends DocShape> {
|
|
724
|
-
private shape;
|
|
725
|
-
private placeholder;
|
|
726
|
-
private doc;
|
|
727
|
-
private _valueRef;
|
|
728
|
-
proxy: TypedDoc<Shape> | null;
|
|
729
|
-
constructor(shape: Shape, doc?: LoroDoc);
|
|
730
|
-
get value(): Mutable<Shape>;
|
|
731
|
-
toJSON(): Infer<Shape>;
|
|
732
|
-
change(fn: (draft: Mutable<Shape>) => void): void;
|
|
733
|
-
applyPatch(patch: JsonPatch, pathPrefix?: (string | number)[]): void;
|
|
734
|
-
get loroDoc(): LoroDoc;
|
|
735
|
-
get docShape(): Shape;
|
|
736
|
-
get rawValue(): any;
|
|
737
|
-
}
|
|
738
|
-
/**
|
|
739
|
-
* The proxied TypedDoc type that provides direct schema access.
|
|
740
|
-
* Schema properties are accessed directly on the doc object.
|
|
741
|
-
* Meta-operations are available via the $ namespace.
|
|
742
|
-
*
|
|
743
|
-
* @example
|
|
744
|
-
* ```typescript
|
|
745
|
-
* const doc = createTypedDoc(schema);
|
|
746
|
-
*
|
|
747
|
-
* // Direct schema access
|
|
748
|
-
* doc.count.increment(5);
|
|
749
|
-
* doc.title.insert(0, "Hello");
|
|
750
|
-
*
|
|
751
|
-
* // Serialize to JSON (works on doc and all refs)
|
|
752
|
-
* const snapshot = doc.toJSON();
|
|
753
|
-
* const users = doc.users.toJSON();
|
|
754
|
-
*
|
|
755
|
-
* // Meta-operations via $ (escape hatch)
|
|
756
|
-
* doc.$.change(draft => { ... });
|
|
757
|
-
* doc.$.loroDoc;
|
|
758
|
-
* ```
|
|
759
|
-
*/
|
|
760
|
-
type TypedDoc<Shape extends DocShape> = Mutable<Shape> & {
|
|
761
|
-
/**
|
|
762
|
-
* Meta-operations namespace.
|
|
763
|
-
* Use for change(), loroDoc, etc.
|
|
764
|
-
*/
|
|
765
|
-
$: TypedDocMeta<Shape>;
|
|
766
|
-
/**
|
|
767
|
-
* Returns the full plain JavaScript object representation of the document.
|
|
768
|
-
* This is an O(N) operation that serializes the entire document.
|
|
769
|
-
*
|
|
770
|
-
* @example
|
|
771
|
-
* ```typescript
|
|
772
|
-
* const snapshot = doc.toJSON();
|
|
773
|
-
* console.log(snapshot.count); // number
|
|
774
|
-
* ```
|
|
775
|
-
*/
|
|
776
|
-
toJSON(): Infer<Shape>;
|
|
777
|
-
};
|
|
778
|
-
/**
|
|
779
|
-
* Creates a new TypedDoc with the given schema.
|
|
780
|
-
* Returns a proxied document where schema properties are accessed directly.
|
|
781
|
-
*
|
|
782
|
-
* @param shape - The document schema (with optional .placeholder() values)
|
|
783
|
-
* @param existingDoc - Optional existing LoroDoc to wrap
|
|
784
|
-
* @returns A proxied TypedDoc with direct schema access and $ namespace
|
|
785
|
-
*
|
|
786
|
-
* @example
|
|
787
|
-
* ```typescript
|
|
788
|
-
* const schema = Shape.doc({
|
|
789
|
-
* title: Shape.text(),
|
|
790
|
-
* count: Shape.counter(),
|
|
791
|
-
* });
|
|
792
|
-
*
|
|
793
|
-
* const doc = createTypedDoc(schema);
|
|
794
|
-
*
|
|
795
|
-
* // Direct mutations (auto-commit)
|
|
796
|
-
* doc.count.increment(5);
|
|
797
|
-
* doc.title.insert(0, "Hello");
|
|
798
|
-
*
|
|
799
|
-
* // Batched mutations are committed together via `change`
|
|
800
|
-
* doc.$.change(draft => {
|
|
801
|
-
* draft.count.increment(10);
|
|
802
|
-
* draft.title.update("World");
|
|
803
|
-
* });
|
|
804
|
-
*
|
|
805
|
-
* // Get plain JSON
|
|
806
|
-
* const snapshot = doc.toJSON();
|
|
807
|
-
* ```
|
|
808
|
-
*/
|
|
809
|
-
declare function createTypedDoc<Shape extends DocShape>(shape: Shape, existingDoc?: LoroDoc): TypedDoc<Shape>;
|
|
810
|
-
|
|
811
1396
|
/**
|
|
812
1397
|
* The primary method of mutating typed documents.
|
|
813
1398
|
* Batches multiple mutations into a single transaction.
|
|
@@ -840,20 +1425,62 @@ declare function createTypedDoc<Shape extends DocShape>(shape: Shape, existingDo
|
|
|
840
1425
|
declare function change<Shape extends DocShape>(doc: TypedDoc<Shape>, fn: (draft: Mutable<Shape>) => void): TypedDoc<Shape>;
|
|
841
1426
|
/**
|
|
842
1427
|
* Access the underlying LoroDoc for advanced operations.
|
|
1428
|
+
* Works on both TypedDoc and any typed ref (TextRef, CounterRef, ListRef, etc.).
|
|
843
1429
|
*
|
|
844
|
-
* @param
|
|
845
|
-
* @returns The underlying LoroDoc instance
|
|
1430
|
+
* @param docOrRef - The TypedDoc or typed ref to unwrap
|
|
1431
|
+
* @returns The underlying LoroDoc instance (or undefined for refs created outside a doc context)
|
|
846
1432
|
*
|
|
847
1433
|
* @example
|
|
848
1434
|
* ```typescript
|
|
849
1435
|
* import { getLoroDoc } from "@loro-extended/change"
|
|
850
1436
|
*
|
|
1437
|
+
* // From TypedDoc
|
|
851
1438
|
* const loroDoc = getLoroDoc(doc)
|
|
852
1439
|
* const version = loroDoc.version()
|
|
853
1440
|
* loroDoc.subscribe(() => console.log("changed"))
|
|
1441
|
+
*
|
|
1442
|
+
* // From any ref (TextRef, CounterRef, ListRef, etc.)
|
|
1443
|
+
* const titleRef = doc.title
|
|
1444
|
+
* const loroDoc = getLoroDoc(titleRef)
|
|
1445
|
+
* loroDoc?.subscribe(() => console.log("changed"))
|
|
854
1446
|
* ```
|
|
855
1447
|
*/
|
|
856
1448
|
declare function getLoroDoc<Shape extends DocShape>(doc: TypedDoc<Shape>): LoroDoc;
|
|
1449
|
+
declare function getLoroDoc<Shape extends ContainerShape>(ref: TypedRef<Shape>): LoroDoc;
|
|
1450
|
+
declare function getLoroDoc<DataShape extends StructContainerShape>(ref: TreeRef<DataShape>): LoroDoc;
|
|
1451
|
+
/**
|
|
1452
|
+
* Access the underlying Loro container from a typed ref.
|
|
1453
|
+
* Returns the correctly-typed container based on the ref type.
|
|
1454
|
+
*
|
|
1455
|
+
* @param ref - The typed ref to unwrap
|
|
1456
|
+
* @returns The underlying Loro container (LoroText, LoroCounter, LoroList, etc.)
|
|
1457
|
+
*
|
|
1458
|
+
* @example
|
|
1459
|
+
* ```typescript
|
|
1460
|
+
* import { getLoroContainer } from "@loro-extended/change"
|
|
1461
|
+
*
|
|
1462
|
+
* const titleRef = doc.title
|
|
1463
|
+
* const loroText = getLoroContainer(titleRef) // LoroText
|
|
1464
|
+
*
|
|
1465
|
+
* const countRef = doc.count
|
|
1466
|
+
* const loroCounter = getLoroContainer(countRef) // LoroCounter
|
|
1467
|
+
*
|
|
1468
|
+
* const itemsRef = doc.items
|
|
1469
|
+
* const loroList = getLoroContainer(itemsRef) // LoroList
|
|
1470
|
+
*
|
|
1471
|
+
* // Subscribe to container-level changes
|
|
1472
|
+
* loroText.subscribe((event) => console.log("Text changed:", event))
|
|
1473
|
+
* ```
|
|
1474
|
+
*/
|
|
1475
|
+
declare function getLoroContainer(ref: TypedRef<TextContainerShape>): LoroText;
|
|
1476
|
+
declare function getLoroContainer(ref: TypedRef<CounterContainerShape>): LoroCounter;
|
|
1477
|
+
declare function getLoroContainer(ref: TypedRef<ListContainerShape>): LoroList;
|
|
1478
|
+
declare function getLoroContainer(ref: TypedRef<MovableListContainerShape>): LoroMovableList;
|
|
1479
|
+
declare function getLoroContainer(ref: TypedRef<RecordContainerShape>): LoroMap;
|
|
1480
|
+
declare function getLoroContainer(ref: TypedRef<StructContainerShape>): LoroMap;
|
|
1481
|
+
declare function getLoroContainer<NestedShapes extends Record<string, ContainerOrValueShape>>(ref: StructRef<NestedShapes>): LoroMap;
|
|
1482
|
+
declare function getLoroContainer<DataShape extends StructContainerShape>(ref: TreeRef<DataShape>): LoroTree;
|
|
1483
|
+
declare function getLoroContainer<Shape extends ContainerShape>(ref: TypedRef<Shape>): ShapeToContainer<Shape>;
|
|
857
1484
|
|
|
858
1485
|
/**
|
|
859
1486
|
* Overlays CRDT state with placeholder defaults
|
|
@@ -996,4 +1623,4 @@ declare function createPlaceholderProxy<T extends object>(target: T): T;
|
|
|
996
1623
|
*/
|
|
997
1624
|
declare function validatePlaceholder<T extends DocShape>(placeholder: unknown, schema: T): Infer<T>;
|
|
998
1625
|
|
|
999
|
-
export { type AnyContainerShape, type AnyValueShape, type ArrayValueShape, type ContainerOrValueShape, type ContainerShape, type CounterContainerShape, type DiscriminatedUnionValueShape, type DocShape, type Infer, type InferMutableType, type InferPlaceholderType, type InferRaw, type ListContainerShape, type MapContainerShape, type MovableListContainerShape, type Mutable, type ObjectValueShape, type PathBuilder, type PathNode, type PathSegment, type PathSelector, type RecordContainerShape, type RecordValueShape, type ContainerType as RootContainerType, Shape, type StructContainerShape, type StructValueShape, type TextContainerShape, type TreeContainerShape, type TypedDoc, type UnionValueShape, type ValueShape, type WithNullable, type WithPlaceholder, change, compileToJsonPath, createPathBuilder, createPlaceholderProxy, createTypedDoc, derivePlaceholder, deriveShapePlaceholder, evaluatePath, evaluatePathOnValue, getLoroDoc, hasWildcard, mergeValue, overlayPlaceholder, validatePlaceholder };
|
|
1626
|
+
export { type AnyContainerShape, type AnyValueShape, type ArrayValueShape, type ContainerOrValueShape, type ContainerShape, type CounterContainerShape, CounterRef, type DiscriminatedUnionValueShape, type DocShape, type Infer, type InferMutableType, type InferPlaceholderType, type InferRaw, LORO_SYMBOL, type ListContainerShape, ListRef, type LoroCounterRef, type LoroListRef, type LoroMapRef, type LoroRefBase, type LoroTextRef, type LoroTreeRef, type LoroTypedDocRef, type MapContainerShape, type MovableListContainerShape, MovableListRef, type Mutable, type ObjectValueShape, type PathBuilder, type PathNode, type PathSegment, type PathSelector, type RecordContainerShape, RecordRef, type RecordValueShape, type ContainerType as RootContainerType, Shape, type StructContainerShape, type StructRef, type StructValueShape, type TextContainerShape, TextRef, type TreeContainerShape, type TreeNodeJSON, TreeNodeRef, TreeRef, type TreeRefInterface, type TypedDoc, type UnionValueShape, type ValueShape, type WithNullable, type WithPlaceholder, change, compileToJsonPath, createPathBuilder, createPlaceholderProxy, createTypedDoc, derivePlaceholder, deriveShapePlaceholder, evaluatePath, evaluatePathOnValue, getLoroContainer, getLoroDoc, hasWildcard, loro, mergeValue, overlayPlaceholder, validatePlaceholder };
|