Skip to content

Commit

Permalink
Refactor setupEditor effects to actions (#14513)
Browse files Browse the repository at this point in the history
* refactor setupEditor effects to action-generator
* remove no longer needed redux-multi dependency/usage
* add changelog entry
* update docs
  • Loading branch information
nerrad committed Mar 22, 2019
1 parent 31242d3 commit 3f28c95
Show file tree
Hide file tree
Showing 9 changed files with 69 additions and 157 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -734,7 +734,7 @@ The editor settings object.

### setupEditor

Returns an action object used in signalling that editor has initialized with
Returns an action generator used in signalling that editor has initialized with
the specified post object and editor settings.

*Parameters*
Expand Down
1 change: 0 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions packages/editor/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
## 9.1.1 (Unreleased)

### Internal

- Refactor setupEditor effects to action-generator using controls ([#14513](https://github.com/WordPress/gutenberg/pull/14513))
- Remove redux-multi dependency (no longer needed/used with above refactor)

## 9.1.0 (2019-03-06)

### New Features
Expand Down
1 change: 0 additions & 1 deletion packages/editor/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@
"lodash": "^4.17.11",
"memize": "^1.0.5",
"react-autosize-textarea": "^3.0.2",
"redux-multi": "^0.1.12",
"redux-optimist": "^1.0.0",
"refx": "^3.0.0",
"rememo": "^3.0.0",
Expand Down
39 changes: 33 additions & 6 deletions packages/editor/src/store/actions.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* External dependencies
*/
import { castArray, pick } from 'lodash';
import { castArray, pick, has } from 'lodash';
import { BEGIN, COMMIT, REVERT } from 'redux-optimist';

/**
Expand All @@ -26,22 +26,49 @@ import {
} from './utils/notice-builder';

/**
* Returns an action object used in signalling that editor has initialized with
* WordPress dependencies
*/
import {
parse,
synchronizeBlocksWithTemplate,
} from '@wordpress/blocks';

/**
* Returns an action generator used in signalling that editor has initialized with
* the specified post object and editor settings.
*
* @param {Object} post Post object.
* @param {Object} edits Initial edited attributes object.
* @param {Array?} template Block Template.
*
* @return {Object} Action object.
*/
export function setupEditor( post, edits, template ) {
return {
export function* setupEditor( post, edits, template ) {
yield {
type: 'SETUP_EDITOR',
post,
edits,
template,
};

// In order to ensure maximum of a single parse during setup, edits are
// included as part of editor setup action. Assume edited content as
// canonical if provided, falling back to post.
let content;
if ( has( edits, [ 'content' ] ) ) {
content = edits.content;
} else {
content = post.content.raw;
}

let blocks = parse( content );

// Apply a template for new posts only, if exists.
const isNewPost = post.status === 'auto-draft';
if ( isNewPost && template ) {
blocks = synchronizeBlocksWithTemplate( blocks, template );
}

yield resetEditorBlocks( blocks );
yield setupEditorState( post );
}

/**
Expand Down
43 changes: 0 additions & 43 deletions packages/editor/src/store/effects.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,6 @@
/**
* External dependencies
*/
import { has } from 'lodash';

/**
* WordPress dependencies
*/
import {
parse,
synchronizeBlocksWithTemplate,
} from '@wordpress/blocks';

/**
* Internal dependencies
*/
import {
setupEditorState,
resetEditorBlocks,
} from './actions';
import {
fetchReusableBlocks,
saveReusableBlocks,
Expand All @@ -28,32 +11,6 @@ import {
} from './effects/reusable-blocks';

export default {
SETUP_EDITOR( action ) {
const { post, edits, template } = action;

// In order to ensure maximum of a single parse during setup, edits are
// included as part of editor setup action. Assume edited content as
// canonical if provided, falling back to post.
let content;
if ( has( edits, [ 'content' ] ) ) {
content = edits.content;
} else {
content = post.content.raw;
}

let blocks = parse( content );

// Apply a template for new posts only, if exists.
const isNewPost = post.status === 'auto-draft';
if ( isNewPost && template ) {
blocks = synchronizeBlocksWithTemplate( blocks, template );
}

return [
resetEditorBlocks( blocks ),
setupEditorState( post ),
];
},
FETCH_REUSABLE_BLOCKS: ( action, store ) => {
fetchReusableBlocks( action, store );
},
Expand Down
12 changes: 2 additions & 10 deletions packages/editor/src/store/middlewares.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
* External dependencies
*/
import refx from 'refx';
import multi from 'redux-multi';
import { flowRight } from 'lodash';

/**
* Internal dependencies
Expand All @@ -18,25 +16,19 @@ import effects from './effects';
* @return {Object} Update Store Object.
*/
function applyMiddlewares( store ) {
const middlewares = [
refx( effects ),
multi,
];

let enhancedDispatch = () => {
throw new Error(
'Dispatching while constructing your middleware is not allowed. ' +
'Other middleware would not be applied to this dispatch.'
);
};
let chain = [];

const middlewareAPI = {
getState: store.getState,
dispatch: ( ...args ) => enhancedDispatch( ...args ),
};
chain = middlewares.map( ( middleware ) => middleware( middlewareAPI ) );
enhancedDispatch = flowRight( ...chain )( store.dispatch );

enhancedDispatch = refx( effects )( middlewareAPI )( store.dispatch );

store.dispatch = enhancedDispatch;
return store;
Expand Down
33 changes: 26 additions & 7 deletions packages/editor/src/store/test/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -625,16 +625,35 @@ describe( 'Post generator actions', () => {
} );
} );

describe( 'actions', () => {
describe( 'setupEditor', () => {
it( 'should return the SETUP_EDITOR action', () => {
const post = {};
const result = actions.setupEditor( post );
expect( result ).toEqual( {
type: 'SETUP_EDITOR',
describe( 'Editor actions', () => {
describe( 'setupEditor()', () => {
let fulfillment;
const reset = ( post, edits, template ) => fulfillment = actions
.setupEditor(
post,
edits,
template,
);
it( 'should yield the SETUP_EDITOR action', () => {
reset( { content: { raw: '' }, status: 'publish' } );
const { value } = fulfillment.next();
expect( value ).toEqual( {
type: 'SETUP_EDITOR',
post: { content: { raw: '' }, status: 'publish' },
} );
} );
it( 'should yield action object for resetEditorBlocks', () => {
const { value } = fulfillment.next();
expect( value ).toEqual( actions.resetEditorBlocks( [] ) );
} );
it( 'should yield action object for setupEditorState', () => {
const { value } = fulfillment.next();
expect( value ).toEqual(
actions.setupEditorState(
{ content: { raw: '' }, status: 'publish' }
)
);
} );
} );

describe( 'resetPost', () => {
Expand Down
88 changes: 0 additions & 88 deletions packages/editor/src/store/test/effects.js

This file was deleted.

0 comments on commit 3f28c95

Please sign in to comment.