-
Notifications
You must be signed in to change notification settings - Fork 4.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Experiment a new way to setup the native editor that can be customize…
…d with hooks
- Loading branch information
Showing
5 changed files
with
127 additions
and
68 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import { isEmpty, omit } from 'lodash'; | ||
|
||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { applyFilters, doAction } from '@wordpress/hooks'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import { cloneElement, useEffect } from './react'; | ||
|
||
const render = ( element ) => ( propsFromNative ) => { | ||
useEffect( () => { | ||
doAction( 'native.init', propsFromNative ); | ||
}, [] ); | ||
|
||
useEffect( () => { | ||
doAction( 'native.render', propsFromNative ); | ||
} ); | ||
|
||
// if we have not received props from a parent native app | ||
// just render the element as it is | ||
if ( isEmpty( omit( propsFromNative, [ 'rootTag' ] ) ) ) { | ||
return element; | ||
} | ||
|
||
// Otherwise overwrite the existing props using a filter hook | ||
let filteredProps = null; | ||
|
||
useEffect( () => { | ||
filteredProps = applyFilters( | ||
'native.block_editor_props', | ||
propsFromNative | ||
); | ||
}, [] ); | ||
|
||
if ( ! filteredProps ) { | ||
return null; | ||
} | ||
|
||
return cloneElement( element, filteredProps ); | ||
}; | ||
|
||
/** | ||
* Renders a given element into the target DOM node. | ||
* | ||
* @param {WPElement} element Element to render. | ||
* @param {HTMLElement} target DOM node into which element should be rendered. | ||
*/ | ||
export { render }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,6 +4,4 @@ | |
/** | ||
* Internal dependencies | ||
*/ | ||
import { registerApp } from './src'; | ||
|
||
registerApp(); | ||
import './src'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters