import { Operation , Range } from 'slate' ; import { CursorData } from '../model' ; export const setCursor = ( id , selection , doc , operations , cursorData ) => { const cursorOps = operations . filter ( op => op . type === 'set_selection' ) ; if ( ! doc . cursors ) doc . cursors = { } ; const newCursor = cursorOps [ cursorOps . length - 1 ] ?. newProperties || { } ; if ( selection ) { const newCursorData = Object . assign ( ( doc . cursors [ id ] && JSON . parse ( doc . cursors [ id ] ) ) || { } , newCursor , selection , { ... cursorData , isForward : Range . isForward ( selection ) } ) ; doc . cursors [ id ] = JSON . stringify ( newCursorData ) ; } else { delete doc . cursors [ id ] ; } return doc ; } ;