Skip to content

Commit

Permalink
Edit Post: Move initial edits handling to post editor component
Browse files Browse the repository at this point in the history
  • Loading branch information
aduth committed Sep 14, 2018
1 parent 3d67a79 commit 9ba4034
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 41 deletions.
98 changes: 62 additions & 36 deletions edit-post/editor.js
Original file line number Diff line number Diff line change
@@ -1,50 +1,76 @@
/**
* WordPress dependencies
*/
import { withSelect } from '@wordpress/data';
import { compose } from '@wordpress/compose';
import { withSelect, withDispatch } from '@wordpress/data';
import { EditorProvider, ErrorBoundary } from '@wordpress/editor';
import { StrictMode } from '@wordpress/element';
import { StrictMode, Component } from '@wordpress/element';

/**
* Internal dependencies
*/
import Layout from './components/layout';

function Editor( {
settings,
hasFixedToolbar,
focusMode,
post,
onError,
...props
} ) {
if ( ! post ) {
return null;
class Editor extends Component {
constructor( props ) {
super( ...arguments );

const { initialEdits, editPost } = props;
if ( initialEdits ) {
editPost( initialEdits, { quiet: true } );
}
}

const editorSettings = {
...settings,
hasFixedToolbar,
focusMode,
};

return (
<StrictMode>
<EditorProvider
settings={ editorSettings }
post={ post }
{ ...props }
>
<ErrorBoundary onError={ onError }>
<Layout />
</ErrorBoundary>
</EditorProvider>
</StrictMode>
);
render() {
const {
settings,
hasFixedToolbar,
focusMode,
post,
onError,
...props
} = this.props;

if ( ! post ) {
return null;
}

const editorSettings = {
...settings,
hasFixedToolbar,
focusMode,
};

return (
<StrictMode>
<EditorProvider
settings={ editorSettings }
post={ post }
{ ...props }
>
<ErrorBoundary onError={ onError }>
<Layout />
</ErrorBoundary>
</EditorProvider>
</StrictMode>
);
}
}

export default withSelect( ( select, { postId, postType } ) => ( {
hasFixedToolbar: select( 'core/edit-post' ).isFeatureActive( 'fixedToolbar' ),
focusMode: select( 'core/edit-post' ).isFeatureActive( 'focusMode' ),
post: select( 'core' ).getEntityRecord( 'postType', postType, postId ),
} ) )( Editor );
export default compose( [
withSelect( ( select, { postId, postType } ) => {
const { isFeatureActive } = select( 'core/edit-post' );
const { getEntityRecord } = select( 'core' );

return {
hasFixedToolbar: isFeatureActive( 'fixedToolbar' ),
focusMode: isFeatureActive( 'focusMode' ),
post: getEntityRecord( 'postType', postType, postId ),
};
} ),
withDispatch( ( dispatch ) => {
const { editPost } = dispatch( 'core/editor' );

return { editPost };
} ),
] )( Editor );
12 changes: 7 additions & 5 deletions edit-post/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,12 +65,14 @@ export function initializeEditor( id, postType, postId, settings, initialEdits )
'core/editor.publish',
] );

if ( initialEdits ) {
dispatch( 'core/editor' ).editPost( initialEdits, { quiet: true } );
}

render(
<Editor settings={ settings } onError={ reboot } postId={ postId } postType={ postType } />,
<Editor
settings={ settings }
onError={ reboot }
postId={ postId }
postType={ postType }
initialEdits={ initialEdits }
/>,
target
);

Expand Down

0 comments on commit 9ba4034

Please sign in to comment.