Skip to content

Commit

Permalink
Utils: Remove getters handling from undoable reducer
Browse files Browse the repository at this point in the history
Adds complexity (preserving getters/setters through cloning), obscurity (magic proxying of keys to history subkey), and is not respecting of desired plain object structure of state.
  • Loading branch information
aduth committed Nov 3, 2017
1 parent 412f59e commit 68e22aa
Show file tree
Hide file tree
Showing 6 changed files with 411 additions and 363 deletions.
13 changes: 10 additions & 3 deletions editor/reducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import optimist from 'redux-optimist';
import { combineReducers } from 'redux';
import {
flow,
partialRight,
difference,
get,
reduce,
Expand All @@ -26,7 +28,7 @@ import { getBlockTypes, getBlockType } from '@wordpress/blocks';
/**
* Internal dependencies
*/
import { combineUndoableReducers } from './utils/undoable-reducer';
import undoableReducer from './utils/undoable-reducer';
import { STORE_DEFAULTS } from './store-defaults';
import saveState from './state/save-state';

Expand Down Expand Up @@ -64,7 +66,12 @@ export function getPostRawValue( value ) {
* @param {Object} action Dispatched action
* @return {Object} Updated state
*/
export const editor = combineUndoableReducers( {
export const editor = flow( [
combineReducers,

// Track undo history, starting at editor initialization.
partialRight( undoableReducer, { resetTypes: [ 'SETUP_EDITOR' ] } ),
] )( {
edits( state = {}, action ) {
switch ( action.type ) {
case 'EDIT_POST':
Expand Down Expand Up @@ -262,7 +269,7 @@ export const editor = combineUndoableReducers( {

return state;
},
}, { resetTypes: [ 'SETUP_EDITOR' ] } );
} );

/**
* Reducer returning the last-known state of the current post, in the format
Expand Down
62 changes: 31 additions & 31 deletions editor/selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ export function isEditorSidebarPanelOpened( state, panel ) {
* @return {Boolean} Whether undo history exists
*/
export function hasEditorUndo( state ) {
return state.editor.history.past.length > 0;
return state.editor.past.length > 0;
}

/**
Expand All @@ -158,7 +158,7 @@ export function hasEditorUndo( state ) {
* @return {Boolean} Whether redo history exists
*/
export function hasEditorRedo( state ) {
return state.editor.history.future.length > 0;
return state.editor.future.length > 0;
}

/**
Expand Down Expand Up @@ -256,7 +256,7 @@ export function getCurrentPostLastRevisionId( state ) {
* @return {Object} Object of key value pairs comprising unsaved edits
*/
export function getPostEdits( state ) {
return state.editor.edits;
return state.editor.present.edits;
}

/**
Expand All @@ -269,9 +269,9 @@ export function getPostEdits( state ) {
* @return {*} Post attribute value
*/
export function getEditedPostAttribute( state, attributeName ) {
return state.editor.edits[ attributeName ] === undefined ?
return state.editor.present.edits[ attributeName ] === undefined ?
state.currentPost[ attributeName ] :
state.editor.edits[ attributeName ];
state.editor.present.edits[ attributeName ];
}

/**
Expand Down Expand Up @@ -390,9 +390,9 @@ export function getDocumentTitle( state ) {
* @return {String} Raw post excerpt
*/
export function getEditedPostExcerpt( state ) {
return state.editor.edits.excerpt === undefined ?
return state.editor.present.edits.excerpt === undefined ?
state.currentPost.excerpt :
state.editor.edits.excerpt;
state.editor.present.edits.excerpt;
}

/**
Expand Down Expand Up @@ -422,7 +422,7 @@ export function getEditedPostPreviewLink( state ) {
*/
export const getBlock = createSelector(
( state, uid ) => {
const block = state.editor.blocksByUid[ uid ];
const block = state.editor.present.blocksByUid[ uid ];
if ( ! block ) {
return null;
}
Expand Down Expand Up @@ -475,11 +475,11 @@ function getPostMeta( state, key ) {
*/
export const getBlocks = createSelector(
( state ) => {
return state.editor.blockOrder.map( ( uid ) => getBlock( state, uid ) );
return state.editor.present.blockOrder.map( ( uid ) => getBlock( state, uid ) );
},
( state ) => [
state.editor.blockOrder,
state.editor.blocksByUid,
state.editor.present.blockOrder,
state.editor.present.blocksByUid,
]
);

Expand Down Expand Up @@ -533,7 +533,7 @@ export function getSelectedBlock( state ) {
*/
export const getMultiSelectedBlockUids = createSelector(
( state ) => {
const { blockOrder } = state.editor;
const { blockOrder } = state.editor.present;
const { start, end } = state.blockSelection;
if ( start === end ) {
return [];
Expand All @@ -549,7 +549,7 @@ export const getMultiSelectedBlockUids = createSelector(
return blockOrder.slice( startIndex, endIndex + 1 );
},
( state ) => [
state.editor.blockOrder,
state.editor.present.blockOrder,
state.blockSelection.start,
state.blockSelection.end,
],
Expand All @@ -565,11 +565,11 @@ export const getMultiSelectedBlockUids = createSelector(
export const getMultiSelectedBlocks = createSelector(
( state ) => getMultiSelectedBlockUids( state ).map( ( uid ) => getBlock( state, uid ) ),
( state ) => [
state.editor.blockOrder,
state.editor.present.blockOrder,
state.blockSelection.start,
state.blockSelection.end,
state.editor.blocksByUid,
state.editor.edits.meta,
state.editor.present.blocksByUid,
state.editor.present.edits.meta,
state.currentPost.meta,
]
);
Expand Down Expand Up @@ -665,7 +665,7 @@ export function getMultiSelectedBlocksEndUid( state ) {
* @return {Array} Ordered unique IDs of post blocks
*/
export function getBlockUids( state ) {
return state.editor.blockOrder;
return state.editor.present.blockOrder;
}

/**
Expand All @@ -677,7 +677,7 @@ export function getBlockUids( state ) {
* @return {Number} Index at which block exists in order
*/
export function getBlockIndex( state, uid ) {
return state.editor.blockOrder.indexOf( uid );
return state.editor.present.blockOrder.indexOf( uid );
}

/**
Expand All @@ -689,7 +689,7 @@ export function getBlockIndex( state, uid ) {
* @return {Boolean} Whether block is first in post
*/
export function isFirstBlock( state, uid ) {
return first( state.editor.blockOrder ) === uid;
return first( state.editor.present.blockOrder ) === uid;
}

/**
Expand All @@ -701,7 +701,7 @@ export function isFirstBlock( state, uid ) {
* @return {Boolean} Whether block is last in post
*/
export function isLastBlock( state, uid ) {
return last( state.editor.blockOrder ) === uid;
return last( state.editor.present.blockOrder ) === uid;
}

/**
Expand All @@ -714,7 +714,7 @@ export function isLastBlock( state, uid ) {
*/
export function getPreviousBlock( state, uid ) {
const order = getBlockIndex( state, uid );
return state.editor.blocksByUid[ state.editor.blockOrder[ order - 1 ] ] || null;
return state.editor.present.blocksByUid[ state.editor.present.blockOrder[ order - 1 ] ] || null;
}

/**
Expand All @@ -727,7 +727,7 @@ export function getPreviousBlock( state, uid ) {
*/
export function getNextBlock( state, uid ) {
const order = getBlockIndex( state, uid );
return state.editor.blocksByUid[ state.editor.blockOrder[ order + 1 ] ] || null;
return state.editor.present.blocksByUid[ state.editor.present.blockOrder[ order + 1 ] ] || null;
}

/**
Expand Down Expand Up @@ -818,7 +818,7 @@ export function isTyping( state ) {
*/
export function getBlockInsertionPoint( state ) {
if ( getEditorMode( state ) !== 'visual' ) {
return state.editor.blockOrder.length;
return state.editor.present.blockOrder.length;
}

const position = getBlockSiblingInserterPosition( state );
Expand All @@ -836,7 +836,7 @@ export function getBlockInsertionPoint( state ) {
return getBlockIndex( state, selectedBlock.uid ) + 1;
}

return state.editor.blockOrder.length;
return state.editor.present.blockOrder.length;
}

/**
Expand Down Expand Up @@ -906,20 +906,20 @@ export function didPostSaveRequestFail( state ) {
* @return {?String} Suggested post format
*/
export function getSuggestedPostFormat( state ) {
const blocks = state.editor.blockOrder;
const blocks = state.editor.present.blockOrder;

let name;
// If there is only one block in the content of the post grab its name
// so we can derive a suitable post format from it.
if ( blocks.length === 1 ) {
name = state.editor.blocksByUid[ blocks[ 0 ] ].name;
name = state.editor.present.blocksByUid[ blocks[ 0 ] ].name;
}

// If there are two blocks in the content and the last one is a text blocks
// grab the name of the first one to also suggest a post format from it.
if ( blocks.length === 2 ) {
if ( state.editor.blocksByUid[ blocks[ 1 ] ].name === 'core/paragraph' ) {
name = state.editor.blocksByUid[ blocks[ 0 ] ].name;
if ( state.editor.present.blocksByUid[ blocks[ 1 ] ].name === 'core/paragraph' ) {
name = state.editor.present.blocksByUid[ blocks[ 0 ] ].name;
}
}

Expand Down Expand Up @@ -962,9 +962,9 @@ export const getEditedPostContent = createSelector(
return serialize( getBlocks( state ) );
},
( state ) => [
state.editor.edits.content,
state.editor.blocksByUid,
state.editor.blockOrder,
state.editor.present.edits.content,
state.editor.present.blocksByUid,
state.editor.present.blockOrder,
],
);

Expand Down
Loading

0 comments on commit 68e22aa

Please sign in to comment.