11import { bytesToBase64DataUrl , dataUrlToBytes } from "@/lib/utils/data" ;
22import { showVersion } from "@/lib/utils/loro" ;
3- import { Change , Loro , LoroEvent , OpId , setDebug } from "loro-crdt" ;
3+ import { Change , LoroDoc , LoroEventBatch , OpId , Subscription } from "loro-crdt" ;
44import Quill from "quill" ;
55import { QuillBinding } from "./QuillBinding" ;
66
7- setDebug ( "*" ) ;
87type PeerProfile = { name : string ; peerId : string } ;
98
109type Frontiers = OpId [ ] ;
@@ -39,14 +38,14 @@ const QUILL_TOOLBAR_MAP: WeakMap<HTMLElement, Quill> = new WeakMap();
3938class EditorInstance {
4039 #index: number ;
4140 #profile: PeerProfile ; // Exportable (JSON)
42- #text: Loro ; // Exportable (binary data)
41+ #text: LoroDoc ; // Exportable (binary data)
4342 #quill: Quill ;
4443 #binding: QuillBinding ;
4544 #connected: boolean = true ; // Exportable (JSON)
4645 #manager: EditorManager ;
4746
4847 public async export ( ) : Promise < EditorInstanceExportData > {
49- const rawData = this . #text. exportSnapshot ( ) ;
48+ const rawData = this . #text. export ( { mode : "snapshot" } ) ;
5049 return {
5150 profile : {
5251 name : this . #profile. name ,
@@ -86,7 +85,7 @@ class EditorInstance {
8685 this . #index = index ;
8786 this . #profile = profile ;
8887 this . #manager = manager ;
89- this . #text = new Loro ( ) ;
88+ this . #text = new LoroDoc ( ) ;
9089 this . #text. configTextStyle ( {
9190 bold : { expand : "after" } ,
9291 italic : { expand : "after" } ,
@@ -114,12 +113,12 @@ class EditorInstance {
114113 this . #text. subscribe ( ( e ) => {
115114 // Synchronize the change to the sum text.
116115 const sumVersion = this . #manager. sumText . version ( ) ;
117- const updateData = this . #text. exportFrom ( sumVersion ) ;
116+ const updateData = this . #text. export ( { mode : "update" , from : sumVersion } ) ;
118117 this . #manager. sumText . import ( updateData ) ;
119118 // If this is a local change (i.e., not a change synchronized from
120119 // other peers), we synchronize to other connected peers if we're
121120 // connected.
122- if ( e . local ) {
121+ if ( e . by === " local" ) {
123122 this . synchronize ( ) ;
124123 }
125124 } ) ;
@@ -133,7 +132,7 @@ class EditorInstance {
133132 return this . #profile. name ;
134133 }
135134
136- public get text ( ) : Loro {
135+ public get text ( ) : LoroDoc {
137136 return this . #text;
138137 }
139138
@@ -166,8 +165,8 @@ class EditorInstance {
166165 await Promise . resolve ( ) ;
167166 for ( const that of this . #otherPeers( ) ) {
168167 if ( ! that . connected ) return ;
169- this . #text. import ( that . #text. exportFrom ( this . #text. version ( ) ) ) ;
170- that . #text. import ( this . #text. exportFrom ( that . #text. version ( ) ) ) ;
168+ this . #text. import ( that . #text. export ( { mode : "update" , from : this . #text. version ( ) } ) ) ;
169+ that . #text. import ( this . #text. export ( { mode : "update" , from : that . #text. version ( ) } ) ) ;
171170 }
172171 }
173172 }
@@ -220,10 +219,10 @@ export async function decodeExport(
220219}
221220
222221export class EditorManager {
223- #sumText: Loro ;
222+ #sumText: LoroDoc ;
224223 #numberOfOperations: number = 0 ;
225224 #peers: EditorInstance [ ] ;
226- #subscriptions: Map < number , number [ ] > = new Map ( ) ;
225+ #subscriptions: Map < number , Subscription [ ] > = new Map ( ) ;
227226
228227 static import (
229228 data : EditorManagerImportData ,
@@ -253,9 +252,9 @@ export class EditorManager {
253252 ) {
254253 throw new RangeError ( "insufficient HTML elements for creating editors" ) ;
255254 }
256- this . #sumText = new Loro ( ) ;
255+ this . #sumText = new LoroDoc ( ) ;
257256 this . #sumText. subscribe ( ( e ) => {
258- if ( ! e . fromCheckout ) {
257+ if ( e . by === "local" ) {
259258 this . #numberOfOperations += 1 ;
260259 }
261260 } ) ;
@@ -277,7 +276,7 @@ export class EditorManager {
277276 } ;
278277 }
279278
280- public get sumText ( ) : Loro {
279+ public get sumText ( ) : LoroDoc {
281280 return this . #sumText;
282281 }
283282
@@ -314,7 +313,7 @@ export class EditorManager {
314313 }
315314
316315 public subscribeAll (
317- listener : ( instance : EditorInstance , e : LoroEvent ) => void
316+ listener : ( instance : EditorInstance , e : LoroEventBatch ) => void
318317 ) : number {
319318 this . #subscriptions. set (
320319 this . #subscriptions. size ,
@@ -329,7 +328,7 @@ export class EditorManager {
329328 if ( ! this . #subscriptions. has ( subscription ) ) return ;
330329 this . #subscriptions
331330 . get ( subscription )
332- ?. forEach ( ( n , index ) => this . #peers [ index ] . text . unsubscribe ( n ) ) ;
331+ ?. forEach ( ( unsubscribe , index ) => unsubscribe ( ) ) ;
333332 this . #subscriptions. set ( subscription , [ ] ) ;
334333 }
335334
0 commit comments