import { MarkType } from 'prosemirror-model' ; import { Command } from 'prosemirror-commands' ; import { EditorState , TextSelection , Transaction } from 'prosemirror-state' ; import { markApplies } from 'ngx-editor/helpers' ; export const applyMark = ( type , attrs = { } ) : Command => { return ( state , dispatch ? ) : boolean => { const { tr , selection } = state ; const { empty , ranges , $from , $to } = selection ; if ( empty && selection instanceof TextSelection ) { const { $cursor } = selection ; if ( ! $cursor || ! markApplies ( state . doc , ranges , type ) ) { return false ; } tr . addStoredMark ( type . create ( attrs ) ) ; if ( ! tr . storedMarksSet ) { return false ; } dispatch ?. ( tr ) ; } else { tr . addMark ( $from . pos , $to . pos , type . create ( attrs ) ) ; if ( ! tr . docChanged ) { return false ; } dispatch ?. ( tr . scrollIntoView ( ) ) ; } return true ; } ; } ;