Skip to content

Commit

Permalink
Merge pull request #2319 from WordPress/fix/2300-autosave-edits
Browse files Browse the repository at this point in the history
State: Reset blocks only on initial load
  • Loading branch information
aduth authored Aug 11, 2017
2 parents a358434 + 79ec503 commit f354a5a
Show file tree
Hide file tree
Showing 6 changed files with 143 additions and 32 deletions.
42 changes: 42 additions & 0 deletions editor/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,48 @@
import uuid from 'uuid/v4';
import { partial, castArray } from 'lodash';

/**
* Returns an action object used in signalling that editor has initialized with
* the specified post object.
*
* @param {Object} post Post object
* @return {Object} Action object
*/
export function setInitialPost( post ) {
return {
type: 'SET_INITIAL_POST',
post,
};
}

/**
* Returns an action object used in signalling that the latest version of the
* post has been received, either by initialization or save.
*
* @param {Object} post Post object
* @return {Object} Action object
*/
export function resetPost( post ) {
return {
type: 'RESET_POST',
post,
};
}

/**
* Returns an action object used in signalling that editor has initialized as a
* new post with specified edits which should be considered non-dirtying.
*
* @param {Object} edits Edited attributes object
* @return {Object} Action object
*/
export function setupNewPost( edits ) {
return {
type: 'SETUP_NEW_POST',
edits,
};
}

/**
* Returns an action object used in signalling that blocks state should be
* reset to the specified array of blocks, taking precedence over any other
Expand Down
24 changes: 21 additions & 3 deletions editor/effects.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ import { __ } from '@wordpress/i18n';
*/
import { getGutenbergURL, getWPAdminURL } from './utils/url';
import {
resetPost,
setupNewPost,
resetBlocks,
focusBlock,
replaceBlocks,
Expand Down Expand Up @@ -249,10 +251,26 @@ export default {

dispatch( savePost() );
},
RESET_POST( action ) {
SET_INITIAL_POST( action ) {
const { post } = action;
if ( post.content ) {
return resetBlocks( parse( post.content.raw ) );
const effects = [];

// Parse content as blocks
if ( post.content.raw ) {
effects.push( resetBlocks( parse( post.content.raw ) ) );
}

// Resetting post should occur after blocks have been reset, since it's
// the post reset that restarts history (used in dirty detection).
effects.push( resetPost( post ) );

// Include auto draft title in edits while not flagging post as dirty
if ( post.status === 'auto-draft' ) {
effects.push( setupNewPost( {
title: post.title.raw,
} ) );
}

return effects;
},
};
28 changes: 2 additions & 26 deletions editor/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import { settings } from '@wordpress/date';
import './assets/stylesheets/main.scss';
import Layout from './layout';
import { createReduxStore } from './state';
import { undo } from './actions';
import { setInitialPost, undo } from './actions';
import EditorSettingsProvider from './settings/provider';

/**
Expand Down Expand Up @@ -55,30 +55,6 @@ if ( settings.timezone.string ) {
moment.tz.setDefault( 'WP' );
}

/**
* Initializes Redux state with bootstrapped post, if provided.
*
* @param {Redux.Store} store Redux store instance
* @param {Object} post Bootstrapped post object
*/
function preparePostState( store, post ) {
// Set current post into state
store.dispatch( {
type: 'RESET_POST',
post,
} );

// Include auto draft title in edits while not flagging post as dirty
if ( post.status === 'auto-draft' ) {
store.dispatch( {
type: 'SETUP_NEW_POST',
edits: {
title: post.title.raw,
},
} );
}
}

/**
* Initializes and returns an instance of Editor.
*
Expand All @@ -95,7 +71,7 @@ export function createEditorInstance( id, post, userSettings ) {
settings: editorSettings,
} );

preparePostState( store, post );
store.dispatch( setInitialPost( post ) );

render(
<ReduxProvider store={ store }>
Expand Down
3 changes: 2 additions & 1 deletion editor/state.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import optimist from 'redux-optimist';
import { combineReducers, applyMiddleware, createStore } from 'redux';
import refx from 'refx';
import multi from 'redux-multi';
import { reduce, keyBy, first, last, omit, without, flowRight, mapValues } from 'lodash';

/**
Expand Down Expand Up @@ -532,7 +533,7 @@ export function createReduxStore() {
userData,
} ) );

const enhancers = [ applyMiddleware( refx( effects ) ) ];
const enhancers = [ applyMiddleware( multi, refx( effects ) ) ];
if ( window.__REDUX_DEVTOOLS_EXTENSION__ ) {
enhancers.push( window.__REDUX_DEVTOOLS_EXTENSION__() );
}
Expand Down
77 changes: 75 additions & 2 deletions editor/test/effects.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,22 @@ import { getBlockTypes, unregisterBlockType, registerBlockType, createBlock } fr
/**
* Internal dependencies
*/
import { mergeBlocks, focusBlock, replaceBlocks, editPost, savePost } from '../actions';
import {
resetPost,
setupNewPost,
mergeBlocks,
focusBlock,
replaceBlocks,
editPost,
savePost,
} from '../actions';
import effects from '../effects';
import * as selectors from '../selectors';

jest.mock( '../selectors' );

describe( 'effects', () => {
const defaultBlockSettings = { save: noop, category: 'common' };
const defaultBlockSettings = { save: () => 'Saved', category: 'common' };

beforeEach( () => jest.resetAllMocks() );

Expand Down Expand Up @@ -236,4 +244,69 @@ describe( 'effects', () => {
expect( dispatch ).toHaveBeenCalledWith( savePost() );
} );
} );

describe( '.SET_INITIAL_POST', () => {
const handler = effects.SET_INITIAL_POST;

it( 'should return post reset action', () => {
const post = {
id: 1,
title: {
raw: 'A History of Pork',
},
content: {
raw: '',
},
status: 'draft',
};

const result = handler( { post } );

expect( result ).toEqual( [
resetPost( post ),
] );
} );

it( 'should return block reset with non-empty content', () => {
registerBlockType( 'core/test-block', defaultBlockSettings );
const post = {
id: 1,
title: {
raw: 'A History of Pork',
},
content: {
raw: '<!-- wp:core/test-block -->Saved<!-- /wp:core/test-block -->',
},
status: 'draft',
};

const result = handler( { post } );

expect( result ).toHaveLength( 2 );
expect( result ).toContainEqual( resetPost( post ) );
expect( result.some( ( { blocks } ) => {
return blocks && blocks[ 0 ].name === 'core/test-block';
} ) ).toBe( true );
} );

it( 'should return post setup action only if auto-draft', () => {
const post = {
id: 1,
title: {
raw: 'A History of Pork',
},
content: {
raw: '',
},
status: 'auto-draft',
};

const result = handler( { post } );

expect( result ).toEqual( [
resetPost( post ),
setupNewPost( { title: 'A History of Pork' } ),
] );
} );
} );
} );
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
"react-slot-fill": "^1.0.0-alpha.11",
"react-transition-group": "^1.1.3",
"redux": "^3.6.0",
"redux-multi": "^0.1.12",
"redux-optimist": "0.0.2",
"refx": "^2.0.0",
"rememo": "^2.3.0",
Expand Down

0 comments on commit f354a5a

Please sign in to comment.