import { VersionVector } from '@casual-simulation/causal-trees' ; import { createAbsolutePositionFromRelativePosition , createID , Doc , findRootTypeKey , ID , Item , RelativePosition , Text , } from 'yjs' ; param store export function getStateVector ( doc ) { const sm = { } as VersionVector ; doc . store . clients . forEach ( ( structs , client ) => { const struct = structs [ structs . length - 1 ] ; sm [ client . toString ( ) ] = struct . id . clock + struct . length ; } ) ; return sm ; } param doc param client export function getClock ( doc , client ) { const structs = doc . store . clients . get ( client ) ; if ( structs ) { const struct = structs [ structs . length - 1 ] ; if ( struct ) { return struct . id . clock + struct . length ; } } return undefined ; } param text param vector param index export function createRelativePositionFromStateVector ( text , vector , index , assoc = 0 , includeDeleted = false ) { let t = text . _start ; if ( assoc < 0 ) { if ( index === 0 ) { return createRelativePosition ( text , null , assoc ) ; } index -- ; } while ( t !== null ) { const clientVersion = vector [ t . id . client . toString ( ) ] ; if ( typeof clientVersion === 'number' && t . id . clock <= clientVersion ) { if ( ( ! t . deleted || includeDeleted ) && t . countable ) { if ( t . length > index ) { return createRelativePosition ( text , createID ( t . id . client , t . id . clock + index ) , assoc ) ; } index -= t . length ; } if ( t . right === null && assoc < 0 ) { return createRelativePosition ( text , t . lastId , assoc ) ; } } t = t . right ; } return createRelativePosition ( text , null , assoc ) ; } param text param vector param index export function createAbsolutePositionFromStateVector ( doc , text , vector , index , assoc = 0 , includeDeleted = false ) { const relative = createRelativePositionFromStateVector ( text , vector , index , assoc , includeDeleted ) ; const absolute = createAbsolutePositionFromRelativePosition ( relative , doc ) ; return absolute ; } param { AbstractType < any > } type param { ID | null } item param { number } assoc function function createRelativePosition ( type , item , assoc ) { let typeid = null ; let tname = null ; if ( type . _item === null ) { tname = findRootTypeKey ( type ) ; } else { typeid = createID ( type . _item . id . client , type . _item . id . clock ) ; } return new RelativePosition ( typeid , tname , item , assoc ) ; }