import { NoteSnippetContent } from './note-content.model' ; import { NoteEditorAction , NoteEditorActionTypes } from './note-editor.actions' ; import { createNoteEditorInitialState , NoteEditorState } from './note-editor.state' ; export function withNoteSnippetContentInsertion ( state , index , snippetContent ) { if ( ! state . loaded ) { return state ; } const snippets = [ ... state . selectedNoteContent . snippets ] ; snippets . splice ( index , 0 , snippetContent ) ; return { ... state , selectedNoteContent : { ... state . selectedNoteContent , snippets , } , } ; } export function withNoteSnippetContentAppendation ( state , snippetContent ) { if ( ! state . loaded ) { return state ; } return { ... state , selectedNoteContent : { ... state . selectedNoteContent , snippets : state . selectedNoteContent . snippets . concat ( [ snippetContent ] ) , } , } ; } export function withNoteSnippetContentRemove ( state , index ) { if ( ! state . loaded ) { return state ; } const snippets = [ ... state . selectedNoteContent . snippets ] ; snippets . splice ( index , 1 ) ; return { ... state , selectedNoteContent : { ... state . selectedNoteContent , snippets , } , } ; } export function withNoteSnippetContentUpadation ( state , index , patch ) { if ( ! state . loaded ) { return state ; } const snippets = [ ... state . selectedNoteContent . snippets ] ; if ( ! snippets [ index ] ) { return state ; } snippets [ index ] = { ... snippets [ index ] , ... patch } ; return { ... state , selectedNoteContent : { ... state . selectedNoteContent , snippets , } , } ; } param state param action export function noteEditorReducer ( state = createNoteEditorInitialState ( ) , action ) { switch ( action . type ) { case NoteEditorActionTypes . INIT : return state ; case NoteEditorActionTypes . LOAD_NOTE_CONTENT : return { ... state , loading : true , } ; case NoteEditorActionTypes . LOAD_NOTE_CONTENT_COMPLETE : return { ... state , loading : false , loaded : true , selectedNoteContent : { ... action . payload . content } , } ; case NoteEditorActionTypes . APPEND_SNIPPET : return withNoteSnippetContentAppendation ( state , action . payload . snippet ) ; case NoteEditorActionTypes . INSERT_SNIPPET : return withNoteSnippetContentInsertion ( state , action . payload . index , action . payload . snippet ) ; case NoteEditorActionTypes . REMOVE_SNIPPET : return withNoteSnippetContentRemove ( state , action . payload . index ) ; case NoteEditorActionTypes . UPDATE_SNIPPET : return withNoteSnippetContentUpadation ( state , action . payload . index , action . payload . patch ) ; case NoteEditorActionTypes . FOCUS_SNIPPET : return { ... state , activeSnippetIndex : action . payload . index , } ; case NoteEditorActionTypes . BLUR_SNIPPET : return { ... state , activeSnippetIndex : null , } ; case NoteEditorActionTypes . CHANGE_VIEW_MODE : return { ... state , viewMode : action . payload . viewMode , } ; default : return state ; } }