import { Element , Node , Text } from 'slate' ; import * as Y from 'yjs' ; import { DeltaInsert , InsertDelta } from '../model/types' ; import { yTextToInsertDelta } from './delta' ; import { getProperties } from './slate' ; export function yTextToSlateElement ( yText ) { const delta = yTextToInsertDelta ( yText ) ; const children = delta . length > 0 ? delta . map ( deltaInsertToSlateNode ) : [ { text : '' } ] ; return { ... yText . getAttributes ( ) , children } ; } export function deltaInsertToSlateNode ( insert ) { if ( typeof insert . insert === 'string' ) { return { ... insert . attributes , text : insert . insert } ; } return yTextToSlateElement ( insert . insert ) ; } export function slateNodesToInsertDelta ( nodes ) { return nodes . map ( ( node ) => { if ( Text . isText ( node ) ) { return { insert : node . text , attributes : getProperties ( node ) } ; } return { insert : slateElementToYText ( node ) } ; } ) ; } export function slateElementToYText ( { children , ... attributes } ) { const yElement = new Y . XmlText ( ) ; Object . entries ( attributes ) . forEach ( ( [ key , value ] ) => { yElement . setAttribute ( key , value ) ; } ) ; yElement . applyDelta ( slateNodesToInsertDelta ( children ) , { sanitize : false } ) ; return yElement ; }