Skip to content

Commit

Permalink
Refactor initializeEditor and init props
Browse files Browse the repository at this point in the history
  • Loading branch information
Tug committed May 15, 2020
1 parent 3c9d92e commit 2cfee15
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 54 deletions.
5 changes: 3 additions & 2 deletions packages/edit-post/src/editor.native.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ class Editor extends Component {
hiddenBlockTypes,
blockTypes,
post,
postId,
postType,
...props
} = this.props;
Expand All @@ -112,9 +113,9 @@ class Editor extends Component {
);

const normalizedPost = post || {
id: 1,
id: postId,
title: {
raw: props.initialTitle,
raw: props.initialTitle || '',
},
content: {
// make sure the post content is in sync with gutenberg store
Expand Down
22 changes: 6 additions & 16 deletions packages/edit-post/src/index.native.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,12 @@ let blocksRegistered = false;
/**
* Initializes the Editor and returns a componentProvider
* that can be registered with `AppRegistry.registerComponent`
*
* @param {string} id Unique identifier for editor instance.
* @param {Object} postType Post type of the post to edit.
* @param {Object} postId ID of the post to edit (unused right now)
*/
export function initializeEditor( {
id,
initialHtml,
initialTitle,
initialHtmlModeEnabled,
postType,
} ) {
export function initializeEditor( id, postType, postId ) {
if ( blocksRegistered ) {
return;
}
Expand All @@ -36,13 +34,5 @@ export function initializeEditor( {
registerCoreBlocks();
blocksRegistered = true;

render(
<Editor
initialHtml={ initialHtml }
initialHtmlModeEnabled={ initialHtmlModeEnabled }
initialTitle={ initialTitle }
postType={ postType }
/>,
id
);
render( <Editor postId={ postId } postType={ postType } />, id );
}
20 changes: 3 additions & 17 deletions packages/element/src/react-platform.native.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* External dependencies
*/
import { AppRegistry } from 'react-native';
import { isEmpty, omit } from 'lodash';
import { omit } from 'lodash';

/**
* WordPress dependencies
Expand All @@ -17,26 +17,12 @@ import { cloneElement } from './react';
const render = ( element, id ) =>
AppRegistry.registerComponent( id, () => ( propsFromParent ) => {
const parentProps = omit( propsFromParent || {}, [ 'rootTag' ] );
let filteredProps;

doAction( 'native.pre-render', parentProps );

// If we have not received props from the parent app, we're in the demo app
if ( isEmpty( parentProps ) ) {
filteredProps = applyFilters(
'native.block_editor_props_default',
element.props
);
} else {
filteredProps = applyFilters(
'native.block_editor_props_from_parent',
parentProps
);
}

filteredProps = applyFilters(
const filteredProps = applyFilters(
'native.block_editor_props',
filteredProps
parentProps
);

doAction( 'native.render', filteredProps );
Expand Down
39 changes: 20 additions & 19 deletions packages/react-native-editor/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,7 @@ const gutenbergSetup = () => {
setupInitHooks();

const initializeEditor = require( '@wordpress/edit-post' ).initializeEditor;
initializeEditor( {
id: 'gutenberg',
initialHtml,
initialHtmlModeEnabled: false,
initialTitle: 'Welcome to Gutenberg!',
postType: 'post',
} );
initializeEditor( 'gutenberg', 'post', 1 );
};

const setupInitHooks = () => {
Expand All @@ -58,20 +52,27 @@ const setupInitHooks = () => {
);

// Map native props to Editor props
// TODO: normalize props in the bridge (So we don't have to map initialData to initialHtml)
wpHooks.addFilter(
'native.block_editor_props_from_parent',
'native.block_editor_props',
'core/react-native-editor',
( {
initialData,
initialTitle,
initialHtmlModeEnabled,
postType,
} ) => ( {
initialHtml: initialData,
initialHtmlModeEnabled,
initialTitle,
postType,
} ),
( { initialData, initialTitle, initialHtmlModeEnabled, postType } ) => {
const isDemo = initialData === undefined && __DEV__;
if ( isDemo ) {
return {
initialHtml,
initialHtmlModeEnabled: false,
initialTitle: 'Welcome to Gutenberg!',
postType: 'post',
};
}
return {
initialHtml: initialData,
initialHtmlModeEnabled,
initialTitle,
postType,
};
},
5
);
};
Expand Down

0 comments on commit 2cfee15

Please sign in to comment.