import { MarkType } from 'prosemirror-model' ; import { EditorState } from 'prosemirror-state' ; import { Command , toggleMark } from 'prosemirror-commands' ; import { isMarkActive } from 'ngx-editor/helpers' ; import { removeLink } from 'ngx-editor/commands' ; import { Dispatch } from './types' ; const defaultOptions = { strict : true } ; export interface LinkAttrs { href : string ; title ? : string ; target ? : string ; } class Link { update ( attrs ) { return ( state , dispatch ? ) : boolean => { const { schema , selection } = state ; const type = schema . marks . link ; if ( ! type ) { return false ; } if ( selection . empty ) { return false ; } return toggleMark ( type , attrs ) ( state , dispatch ) ; } ; } insert ( text , attrs ) { return ( state , dispatch ? ) : boolean => { const { schema , tr } = state ; const type = schema . marks . link ; if ( ! type ) { return false ; } const linkAttrs = { href : attrs . href , title : attrs . title ?? text , target : attrs . target ?? '_blank' } ; const node = schema . text ( text , [ schema . marks . link . create ( linkAttrs ) ] ) ; tr . replaceSelectionWith ( node , false ) . scrollIntoView ( ) ; if ( tr . docChanged ) { dispatch ?. ( tr ) ; return true ; } return false ; } ; } isActive ( state , options = defaultOptions ) { if ( options . strict ) { return true ; } const { schema } = state ; const type = schema . marks . link ; if ( ! type ) { return false ; } return isMarkActive ( state , type ) ; } remove ( state , dispatch ? ) { return removeLink ( ) ( state , dispatch ) ; } canExecute ( state ) { const testAttrs = { href : '' } ; return this . insert ( 'Exec' , testAttrs ) ( state ) || this . update ( testAttrs ) ( state ) ; } } export default Link ;