import { EditorState , Transaction } from 'prosemirror-state' ; import { Node } from 'prosemirror-model' ; import { Command } from 'prosemirror-commands' ; import { getSelectionNodes } from 'ngx-editor/helpers' ; export type Align = 'left' | 'center' | 'right' | 'justify' ; class TextAlign { align : string ; constructor ( align ) { this . align = align ; } toggle ( ) { return ( state , dispatch ? ) : boolean => { const { doc , selection , tr , schema } = state ; const { from , to } = selection ; let applicable = false ; doc . nodesBetween ( from , to , ( node , pos ) => { const nodeType = node . type ; if ( [ schema . nodes . paragraph , schema . nodes . heading ] . includes ( nodeType ) ) { applicable = true ; tr . setNodeMarkup ( pos , nodeType , { ... node . attrs , align : this . align } ) ; } return true ; } ) ; if ( ! applicable ) { return false ; } if ( tr . docChanged ) { dispatch ?. ( tr ) ; } return true ; } ; } isActive ( state ) { const nodes = getSelectionNodes ( state ) ; const active = nodes . find ( ( node ) => { return node . attrs [ 'align' ] === this . align ; } ) ; return Boolean ( active ) ; } canExecute ( state ) { return this . toggle ( ) ( state ) ; } } export default TextAlign ;