56 lines
1.2 KiB
JavaScript
56 lines
1.2 KiB
JavaScript
|
/**
|
||
|
* External dependencies
|
||
|
*/
|
||
|
import { createContext, useContext } from '@wordpress/element';
|
||
|
import { useSelect } from '@wordpress/data';
|
||
|
|
||
|
/**
|
||
|
* @typedef {import('@woocommerce/type-defs/contexts').EditorDataContext} EditorDataContext
|
||
|
*/
|
||
|
|
||
|
const EditorContext = createContext( {
|
||
|
isEditor: false,
|
||
|
currentPostId: 0,
|
||
|
} );
|
||
|
|
||
|
/**
|
||
|
* @return {EditorDataContext} Returns the editor data context value
|
||
|
*/
|
||
|
export const useEditorContext = () => {
|
||
|
return useContext( EditorContext );
|
||
|
};
|
||
|
|
||
|
/**
|
||
|
* Editor provider
|
||
|
*
|
||
|
* @param {Object} props Incoming props for the provider.
|
||
|
* @param {*} props.children The children being wrapped.
|
||
|
* @param {number} props.currentPostId The post being edited.
|
||
|
*/
|
||
|
export const EditorProvider = ( { children, currentPostId = 0 } ) => {
|
||
|
const editingPostId = useSelect(
|
||
|
( select ) => {
|
||
|
if ( ! currentPostId ) {
|
||
|
const store = select( 'core/editor' );
|
||
|
return store.getCurrentPostId();
|
||
|
}
|
||
|
return currentPostId;
|
||
|
},
|
||
|
[ currentPostId ]
|
||
|
);
|
||
|
|
||
|
/**
|
||
|
* @type {EditorDataContext}
|
||
|
*/
|
||
|
const editorData = {
|
||
|
isEditor: true,
|
||
|
currentPostId: editingPostId,
|
||
|
};
|
||
|
|
||
|
return (
|
||
|
<EditorContext.Provider value={ editorData }>
|
||
|
{ children }
|
||
|
</EditorContext.Provider>
|
||
|
);
|
||
|
};
|