import { Command as ProseCommand } from "prosemirror-commands" ; import { TextSelection } from "prosemirror-state" ; todo export const moveSelection = ( dir ) : ProseCommand => { return ( state , dispatch , view ) => { let { $from , $to } = state . selection ; let fromIndex = $from . index ( 0 ) ; let toIndex = $to . index ( 0 ) ; let MOVING_DOWN = ( dir > 0 ) ; let moveNodeIndex = ( MOVING_DOWN ? toIndex + 1 : fromIndex - 1 ) ; let insertIndex = ( MOVING_DOWN ? fromIndex : toIndex + 1 ) ; let maxIndex = state . doc . content . content . length ; if ( moveNodeIndex < 0 || moveNodeIndex >= maxIndex ) { return true ; } note let moveNodePos = ( MOVING_DOWN ? $to : $from ) . posAtIndex ( moveNodeIndex , 0 ) ; let insertPos = ( MOVING_DOWN ? $from : $to ) . posAtIndex ( insertIndex , 0 ) ; let moveNode = state . doc . nodeAt ( moveNodePos ) ; if ( ! moveNode ) { return false ; } if ( dispatch ) { todo let tr = state . tr . insert ( insertPos , moveNode ) ; tr = tr . deleteRange ( tr . mapping . map ( moveNodePos ) , tr . mapping . map ( moveNodePos + moveNode . nodeSize ) ) ; dispatch ( tr ) ; } return true ; } ; } ; export const moveSelectionUp = ( ) => moveSelection ( - 1 ) ; export const moveSelectionDown = ( ) => moveSelection ( + 1 ) ;