Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Edit Site: Implement navigation.
  • Loading branch information
epiqueras committed Jul 28, 2020
commit e262ae21533adb8d1162de4703a1538bfb93d0cd
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ function ToolSelector( props, ref ) {

const onSwitchMode = ( mode ) => {
setNavigationMode( mode === 'edit' ? false : true );
setBrowseMode( mode === 'browse' ? false : true );
setBrowseMode( mode === 'browse' );
setTool( mode );
};
return (
Expand Down
2 changes: 2 additions & 0 deletions packages/edit-site/src/components/block-editor/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import {
/**
* Internal dependencies
*/
import BrowseModeNavigation from '../browse-mode-navigation';
import NavigateToLink from '../navigate-to-link';
import { SidebarInspectorFill } from '../sidebar';

Expand Down Expand Up @@ -48,6 +49,7 @@ export default function BlockEditor( { setIsInserterOpen } ) {
onChange={ onChange }
useSubRegistry={ false }
>
<BrowseModeNavigation />
<BlockEditorKeyboardShortcuts />
<__experimentalLinkControl.ViewerFill>
{ useCallback(
Expand Down
56 changes: 56 additions & 0 deletions packages/edit-site/src/components/browse-mode-navigation/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/**
* WordPress dependencies
*/
import { useEffect } from '@wordpress/element';
import { getPathAndQueryString } from '@wordpress/url';
import { useRegistry, useSelect, useDispatch } from '@wordpress/data';

function useBrowseModeNavigation( isBrowseMode, registry, setPage ) {
useEffect( () => {
const listener = async ( event ) => {
if ( ! event.target.dataset.type || ! event.target.dataset.id )
return;

const entity = await registry
.__experimentalResolveSelect( 'core' )
.getEntityRecord(
'postType',
event.target.dataset.type,
event.target.dataset.id
);

setPage( {
type: entity.type,
slug: entity.slug,
path: getPathAndQueryString( entity.link || entity.url ),
context: { postType: entity.type, postId: entity.id },
} );
};

const unsubscribe = () =>
document
.getElementsByClassName( 'editor-styles-wrapper' )?.[ 0 ]
?.removeEventListener( 'click', listener, true );

if ( ! isBrowseMode ) unsubscribe();
else
return (
document
.getElementsByClassName( 'editor-styles-wrapper' )?.[ 0 ]
?.addEventListener( 'click', listener, true ) && unsubscribe
);
}, [ isBrowseMode ] );
}

export default function BrowseModeNavigation() {
const registry = useRegistry();
useBrowseModeNavigation(
useSelect(
( select ) => select( 'core/block-editor' ).isBrowseMode(),
[]
),
registry,
useDispatch( 'core/edit-site' ).setPage
);
return null;
}