From ea41379a0e48704a61e165a9933b49134bdeb34f Mon Sep 17 00:00:00 2001 From: Darren Ethier Date: Tue, 19 Mar 2019 11:08:04 -0400 Subject: [PATCH 1/7] remove unnecessary test file for effects --- packages/editor/src/store/test/effects.js | 88 ----------------------- 1 file changed, 88 deletions(-) delete mode 100644 packages/editor/src/store/test/effects.js diff --git a/packages/editor/src/store/test/effects.js b/packages/editor/src/store/test/effects.js deleted file mode 100644 index cdc7223858e0fe..00000000000000 --- a/packages/editor/src/store/test/effects.js +++ /dev/null @@ -1,88 +0,0 @@ -/** - * WordPress dependencies - */ -import { - getBlockTypes, - unregisterBlockType, - registerBlockType, -} from '@wordpress/blocks'; - -/** - * Internal dependencies - */ -import { setupEditorState, resetEditorBlocks } from '../actions'; -import effects from '../effects'; -import '../../'; - -describe( 'effects', () => { - const defaultBlockSettings = { save: () => 'Saved', category: 'common', title: 'block title' }; - - describe( '.SETUP_EDITOR', () => { - const handler = effects.SETUP_EDITOR; - - afterEach( () => { - getBlockTypes().forEach( ( block ) => { - unregisterBlockType( block.name ); - } ); - } ); - - it( 'should return post reset action', () => { - const post = { - id: 1, - title: { - raw: 'A History of Pork', - }, - content: { - raw: '', - }, - status: 'draft', - }; - - const result = handler( { post, settings: {} } ); - - expect( result ).toEqual( [ - resetEditorBlocks( [] ), - setupEditorState( 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: 'Saved', - }, - status: 'draft', - }; - - const result = handler( { post } ); - - expect( result[ 0 ].blocks ).toHaveLength( 1 ); - expect( result[ 1 ] ).toEqual( setupEditorState( post, result[ 0 ].blocks, {} ) ); - } ); - - 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( [ - resetEditorBlocks( [] ), - setupEditorState( post, [], { title: 'A History of Pork' } ), - ] ); - } ); - } ); -} ); From 0f590bb2eab3c5551e0c0abd1601f6719ee8edc1 Mon Sep 17 00:00:00 2001 From: Darren Ethier Date: Tue, 19 Mar 2019 11:09:56 -0400 Subject: [PATCH 2/7] refactor setupEditor effects to action-generator --- packages/editor/src/store/actions.js | 48 ++++++++++++++++++++--- packages/editor/src/store/effects.js | 43 -------------------- packages/editor/src/store/test/actions.js | 38 ++++++++++++++---- 3 files changed, 73 insertions(+), 56 deletions(-) diff --git a/packages/editor/src/store/actions.js b/packages/editor/src/store/actions.js index c0c88d1072fe9a..522cf17adf4a71 100644 --- a/packages/editor/src/store/actions.js +++ b/packages/editor/src/store/actions.js @@ -1,7 +1,7 @@ /** * External dependencies */ -import { castArray, pick } from 'lodash'; +import { castArray, pick, has } from 'lodash'; import { BEGIN, COMMIT, REVERT } from 'redux-optimist'; /** @@ -26,22 +26,58 @@ 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 dispatch( + STORE_KEY, + 'resetEditorBlocks', + blocks + ); + + yield dispatch( + STORE_KEY, + 'setupEditorState', + post + ); } /** diff --git a/packages/editor/src/store/effects.js b/packages/editor/src/store/effects.js index 84f51151137667..0893f8315cbf11 100644 --- a/packages/editor/src/store/effects.js +++ b/packages/editor/src/store/effects.js @@ -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, @@ -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 ); }, diff --git a/packages/editor/src/store/test/actions.js b/packages/editor/src/store/test/actions.js index 296a365fe4c490..0abfe99c7ee319 100644 --- a/packages/editor/src/store/test/actions.js +++ b/packages/editor/src/store/test/actions.js @@ -625,16 +625,40 @@ 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 dispatch action for resetEditorBlocks', () => { + const { value } = fulfillment.next(); + expect( value.type ).toBe( 'DISPATCH' ); + expect( value.storeKey ).toBe( STORE_KEY ); + expect( value.actionName ).toBe( 'resetEditorBlocks' ); + expect( value.args ).toEqual( [ [] ] ); + } ); + it( 'should yield dispatch action for setupEditorState', () => { + const { value } = fulfillment.next(); + expect( value ).toEqual( + dispatch( + STORE_KEY, + 'setupEditorState', + { content: { raw: '' }, status: 'publish' } + ) + ); + } ); } ); describe( 'resetPost', () => { From f320abcb0d9a0f838c5c03b0bbab19736878c989 Mon Sep 17 00:00:00 2001 From: Darren Ethier Date: Tue, 19 Mar 2019 11:13:17 -0400 Subject: [PATCH 3/7] add changelog entry --- packages/editor/CHANGELOG.md | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/packages/editor/CHANGELOG.md b/packages/editor/CHANGELOG.md index ce396bfa582ec3..61bc8a65e72e33 100644 --- a/packages/editor/CHANGELOG.md +++ b/packages/editor/CHANGELOG.md @@ -1,3 +1,9 @@ +## 9.1.1 (Unreleased) + +### Internal + +- Refactor setupEditor effects to action-generator using controls ([#14513](https://github.com/WordPress/gutenberg/pull/14513)) + ## 9.1.0 (2019-03-06) ### New Features From 8ca0b55ca8291e5f21b39da1bbb7a1911c731cd7 Mon Sep 17 00:00:00 2001 From: Darren Ethier Date: Tue, 19 Mar 2019 11:14:51 -0400 Subject: [PATCH 4/7] update docs --- docs/designers-developers/developers/data/data-core-editor.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/designers-developers/developers/data/data-core-editor.md b/docs/designers-developers/developers/data/data-core-editor.md index cd251dc92d6787..34016e71536622 100644 --- a/docs/designers-developers/developers/data/data-core-editor.md +++ b/docs/designers-developers/developers/data/data-core-editor.md @@ -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* From 451de2aca62168738a9f0458b2a0b11474690ac6 Mon Sep 17 00:00:00 2001 From: Darren Ethier Date: Wed, 20 Mar 2019 20:34:48 -0400 Subject: [PATCH 5/7] remove no longer needed redux-multi dependency/usage --- package-lock.json | 1 - packages/editor/package.json | 1 - packages/editor/src/store/middlewares.js | 12 ++---------- 3 files changed, 2 insertions(+), 12 deletions(-) diff --git a/package-lock.json b/package-lock.json index d8bc6f6abec6f3..44e9965126ee60 100644 --- a/package-lock.json +++ b/package-lock.json @@ -2829,7 +2829,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", diff --git a/packages/editor/package.json b/packages/editor/package.json index f2132eb5c6e671..844cf5fcd58fe4 100644 --- a/packages/editor/package.json +++ b/packages/editor/package.json @@ -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", diff --git a/packages/editor/src/store/middlewares.js b/packages/editor/src/store/middlewares.js index 6381132bb81e08..b19b1b9bb5817b 100644 --- a/packages/editor/src/store/middlewares.js +++ b/packages/editor/src/store/middlewares.js @@ -2,8 +2,6 @@ * External dependencies */ import refx from 'refx'; -import multi from 'redux-multi'; -import { flowRight } from 'lodash'; /** * Internal dependencies @@ -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; From ce1e4fa935562ba4d05554c8a9e65c9059f7feb3 Mon Sep 17 00:00:00 2001 From: Darren Ethier Date: Wed, 20 Mar 2019 20:35:09 -0400 Subject: [PATCH 6/7] update changelog about redux-multi removal --- packages/editor/CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/packages/editor/CHANGELOG.md b/packages/editor/CHANGELOG.md index 61bc8a65e72e33..cefab057402b61 100644 --- a/packages/editor/CHANGELOG.md +++ b/packages/editor/CHANGELOG.md @@ -3,6 +3,7 @@ ### 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) From 46a53e042e9cf0cb29448da8e25a074e69ed3045 Mon Sep 17 00:00:00 2001 From: Darren Ethier Date: Fri, 22 Mar 2019 10:57:38 -0400 Subject: [PATCH 7/7] yield actions directly instead of controls --- packages/editor/src/store/actions.js | 13 ++----------- packages/editor/src/store/test/actions.js | 13 ++++--------- 2 files changed, 6 insertions(+), 20 deletions(-) diff --git a/packages/editor/src/store/actions.js b/packages/editor/src/store/actions.js index 522cf17adf4a71..32f3bed210e10d 100644 --- a/packages/editor/src/store/actions.js +++ b/packages/editor/src/store/actions.js @@ -67,17 +67,8 @@ export function* setupEditor( post, edits, template ) { blocks = synchronizeBlocksWithTemplate( blocks, template ); } - yield dispatch( - STORE_KEY, - 'resetEditorBlocks', - blocks - ); - - yield dispatch( - STORE_KEY, - 'setupEditorState', - post - ); + yield resetEditorBlocks( blocks ); + yield setupEditorState( post ); } /** diff --git a/packages/editor/src/store/test/actions.js b/packages/editor/src/store/test/actions.js index 0abfe99c7ee319..3f0e453b384ebe 100644 --- a/packages/editor/src/store/test/actions.js +++ b/packages/editor/src/store/test/actions.js @@ -642,19 +642,14 @@ describe( 'Editor actions', () => { post: { content: { raw: '' }, status: 'publish' }, } ); } ); - it( 'should yield dispatch action for resetEditorBlocks', () => { + it( 'should yield action object for resetEditorBlocks', () => { const { value } = fulfillment.next(); - expect( value.type ).toBe( 'DISPATCH' ); - expect( value.storeKey ).toBe( STORE_KEY ); - expect( value.actionName ).toBe( 'resetEditorBlocks' ); - expect( value.args ).toEqual( [ [] ] ); + expect( value ).toEqual( actions.resetEditorBlocks( [] ) ); } ); - it( 'should yield dispatch action for setupEditorState', () => { + it( 'should yield action object for setupEditorState', () => { const { value } = fulfillment.next(); expect( value ).toEqual( - dispatch( - STORE_KEY, - 'setupEditorState', + actions.setupEditorState( { content: { raw: '' }, status: 'publish' } ) );