Skip to content

Commit

Permalink
Edit Site: Load root template in block editor.
Browse files Browse the repository at this point in the history
  • Loading branch information
epiqueras committed Dec 12, 2019
1 parent c54fd1b commit fa7a8cb
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 10 deletions.
2 changes: 2 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions packages/edit-site/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@
"@babel/runtime": "^7.4.4",
"@wordpress/block-editor": "file:../block-editor",
"@wordpress/block-library": "file:../block-library",
"@wordpress/blocks": "file:../blocks",
"@wordpress/components": "file:../components",
"@wordpress/core-data": "file:../core-data",
"@wordpress/data": "file:../data",
"@wordpress/element": "file:../element",
"@wordpress/hooks": "file:../hooks",
Expand Down
29 changes: 26 additions & 3 deletions packages/edit-site/src/components/block-editor/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@
* WordPress dependencies
*/
import { useSelect } from '@wordpress/data';
import { useMemo, useState } from '@wordpress/element';
import { useMemo, useCallback } from '@wordpress/element';
import { uploadMedia } from '@wordpress/media-utils';
import { useEntityProp } from '@wordpress/core-data';
import { parse, serialize } from '@wordpress/blocks';
import {
BlockEditorProvider,
BlockEditorKeyboardShortcuts,
Expand Down Expand Up @@ -39,13 +41,34 @@ export default function BlockEditor( { settings: _settings } ) {
},
};
}, [ canUserCreateMedia, _settings ] );
const [ blocks, setBlocks ] = useState( [] );
const [ content, _setContent ] = useEntityProp(
'postType',
'wp_template',
'content'
);
const initialBlocks = useMemo( () => {
if ( typeof content !== 'function' ) {
const parsedContent = parse( content );
return parsedContent.length ? parsedContent : undefined;
}
}, [] );
const [ blocks = initialBlocks, setBlocks ] = useEntityProp(
'postType',
'wp_template',
'blocks'
);
const setContent = useCallback( ( nextBlocks ) => {
setBlocks( nextBlocks );
_setContent( ( { blocks: blocksForSerialization = nextBlocks } ) =>
serialize( blocksForSerialization )
);
}, [] );
return (
<BlockEditorProvider
settings={ settings }
value={ blocks }
onInput={ setBlocks }
onChange={ setBlocks }
onChange={ setContent }
>
<BlockEditorKeyboardShortcuts />
<Sidebar.InspectorFill>
Expand Down
31 changes: 24 additions & 7 deletions packages/edit-site/src/components/editor/index.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
/**
* WordPress dependencies
*/
import { useSelect } from '@wordpress/data';
import {
SlotFillProvider,
DropZoneProvider,
Popover,
navigateRegions,
} from '@wordpress/components';
import { EntityProvider } from '@wordpress/core-data';

/**
* Internal dependencies
Expand All @@ -17,17 +19,32 @@ import Sidebar from '../sidebar';
import BlockEditor from '../block-editor';

function Editor( { settings } ) {
return (
const template = useSelect(
( select ) =>
select( 'core' ).getEntityRecord(
'postType',
'wp_template',
settings.templateId
),
[]
);
return template ? (
<SlotFillProvider>
<DropZoneProvider>
<Notices />
<Header />
<Sidebar />
<BlockEditor settings={ settings } />
<Popover.Slot />
<EntityProvider
kind="postType"
type="wp_template"
id={ settings.templateId }
>
<Notices />
<Header />
<Sidebar />
<BlockEditor settings={ settings } />
<Popover.Slot />
</EntityProvider>
</DropZoneProvider>
</SlotFillProvider>
);
) : null;
}

export default navigateRegions( Editor );

0 comments on commit fa7a8cb

Please sign in to comment.