Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import { EditorState, ContentState, convertFromRaw } from 'draft-js';
- const editorStateReducer = (state = defaultEditorState, action) => {
- switch(action.type) {
- case MORE_CONTENT_RETRIEVED: {
- // Capture current state
- const currentContentState = state.getCurrentContent();
- const currentBlockMap = currentContentState.getBlockMap();
- const currentSelection = state.getSelection();
- // Create new ContentBlocks
- const { blocks } = action.payload;
- const newContentState = convertFromRaw({ blocks, entityMap: {} });
- const newBlockMap = newContentState.getBlockMap();
- // Combine new and existing ContentBlocks
- const combinedBlockMap = newBlockMap.concat(currentBlockMap);
- const combinedContentState = ContentState.createFromBlockArray(combinedBlockMap.toArray());
- // Push EditorState while excluding changes from undo/redo stack
- const stateNoUndo = EditorState.set(state, { allowUndo: false });
- const newState = EditorState.push(stateNoUndo, combinedContentState, 'insert-fragment');
- const stateAllowUndo = EditorState.set(newState, { allowUndo: true });
- // Maintain SelectionState
- const newStateWithSelection = EditorState.forceSelection(stateAllowUndo, currentSelection);
- // Return combined selection state
- return newStateWithSelection;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement