-
Notifications
You must be signed in to change notification settings - Fork 4.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Editor: Implement
EntityProvider
and use it to refactor custom sour…
…ces with a backwards compatibility hook for meta sources.
- Loading branch information
Showing
13 changed files
with
158 additions
and
302 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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,76 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { createContext, useContext, useCallback } from '@wordpress/element'; | ||
import { useSelect, useDispatch } from '@wordpress/data'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import { defaultEntities } from './entities'; | ||
|
||
const entities = defaultEntities.reduce( | ||
( acc, entity ) => { | ||
if ( ! acc[ entity.kind ] ) { | ||
acc[ entity.kind ] = {}; | ||
} | ||
acc[ entity.kind ][ entity.name ] = { context: createContext() }; | ||
return acc; | ||
}, | ||
{ postType: { post: { context: createContext() } } } | ||
); | ||
|
||
/** | ||
* Context provider component for providing | ||
* an entity for a specific entity type. | ||
* | ||
* @param {Object} props The component's props. | ||
* @param {string} props.kind The entity kind. | ||
* @param {string} props.type The entity type. | ||
* @param {number} props.id The entity ID. | ||
* @param {*} props.children The children to wrap. | ||
* | ||
* @return {Object} The provided children, wrapped with | ||
* the entity's context provider. | ||
*/ | ||
export default function EntityProvider( { kind, type, id, children } ) { | ||
const Provider = entities[ kind ][ type ].context.Provider; | ||
return <Provider value={ id }>{ children }</Provider>; | ||
} | ||
|
||
/** | ||
* Hook that returns the value and a setter for the | ||
* specified property of the nearest provided | ||
* entity of the specified type. | ||
* | ||
* @param {string} kind The entity kind. | ||
* @param {string} type The entity type. | ||
* @param {string} prop The property name. | ||
* | ||
* @return {[*, Function]} A tuple where the first item is the | ||
* property value and the second is the | ||
* setter. | ||
*/ | ||
export function useEntityProp( kind, type, prop ) { | ||
const id = useContext( entities[ kind ][ type ].context ); | ||
|
||
const value = useSelect( | ||
( select ) => { | ||
const entity = select( 'core' ).getEditedEntityRecord( kind, type, id ); | ||
return entity && entity[ prop ]; | ||
}, | ||
[ kind, type, id, prop ] | ||
); | ||
|
||
const { editEntityRecord } = useDispatch( 'core' ); | ||
const setValue = useCallback( | ||
( newValue ) => { | ||
editEntityRecord( kind, type, id, { | ||
[ prop ]: newValue, | ||
} ); | ||
}, | ||
[ kind, type, id, prop ] | ||
); | ||
|
||
return [ value, setValue ]; | ||
} |
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
Oops, something went wrong.