Advertisement
Guest User

Untitled

a guest
Apr 20th, 2019
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.36 KB | None | 0 0
  1. import { EditorState, ContentState, convertFromRaw } from 'draft-js';
  2.  
  3. const editorStateReducer = (state = defaultEditorState, action) => {
  4. switch(action.type) {
  5. case MORE_CONTENT_RETRIEVED: {
  6. // Capture current state
  7. const currentContentState = state.getCurrentContent();
  8. const currentBlockMap = currentContentState.getBlockMap();
  9. const currentSelection = state.getSelection();
  10.  
  11. // Create new ContentBlocks
  12. const { blocks } = action.payload;
  13. const newContentState = convertFromRaw({ blocks, entityMap: {} });
  14. const newBlockMap = newContentState.getBlockMap();
  15.  
  16. // Combine new and existing ContentBlocks
  17. const combinedBlockMap = newBlockMap.concat(currentBlockMap);
  18. const combinedContentState = ContentState.createFromBlockArray(combinedBlockMap.toArray());
  19.  
  20. // Push EditorState while excluding changes from undo/redo stack
  21. const stateNoUndo = EditorState.set(state, { allowUndo: false });
  22. const newState = EditorState.push(stateNoUndo, combinedContentState, 'insert-fragment');
  23. const stateAllowUndo = EditorState.set(newState, { allowUndo: true });
  24.  
  25. // Maintain SelectionState
  26. const newStateWithSelection = EditorState.forceSelection(stateAllowUndo, currentSelection);
  27.  
  28. // Return combined selection state
  29. return newStateWithSelection;
  30. }
  31. }
  32. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement