Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import { useReducedMotion } from '@wordpress/compose';
*/
import { store as editPostStore } from '../../../store';

function FullscreenModeClose( { showTooltip, icon, href } ) {
function FullscreenModeClose( { showTooltip, icon, href, initialPost } ) {
const { isActive, isRequestingSiteIcon, postType, siteIconUrl } = useSelect(
( select ) => {
const { getCurrentPostType } = select( editorStore );
Expand All @@ -33,15 +33,15 @@ function FullscreenModeClose( { showTooltip, icon, href } ) {
select( coreStore );
const siteData =
getEntityRecord( 'root', '__unstableBase', undefined ) || {};

const _postType = initialPost?.type || getCurrentPostType();
return {
isActive: isFeatureActive( 'fullscreenMode' ),
isRequestingSiteIcon: isResolving( 'getEntityRecord', [
'root',
'__unstableBase',
undefined,
] ),
postType: getPostType( getCurrentPostType() ),
postType: getPostType( _postType ),
siteIconUrl: siteData.site_icon_url,
};
},
Expand Down Expand Up @@ -88,17 +88,20 @@ function FullscreenModeClose( { showTooltip, icon, href } ) {
'has-icon': siteIconUrl,
} );

const buttonHref =
href ??
addQueryArgs( 'edit.php', {
post_type: postType.slug,
} );

const buttonLabel = postType?.labels?.view_items ?? __( 'Back' );

return (
<motion.div whileHover="expand">
<Button
className={ classes }
href={
href ??
addQueryArgs( 'edit.php', {
post_type: postType.slug,
} )
}
label={ postType?.labels?.view_items ?? __( 'Back' ) }
href={ buttonHref }
label={ buttonLabel }
showTooltip={ showTooltip }
>
{ buttonIcon }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@ import FullscreenModeClose from '../';

jest.mock( '@wordpress/data/src/components/use-select', () => {
// This allows us to tweak the returned value on each test.
const mock = jest.fn();
return mock;
return jest.fn();
Comment thread
ramonjd marked this conversation as resolved.
} );

jest.mock( '@wordpress/core-data' );
Expand All @@ -28,16 +27,15 @@ describe( 'FullscreenModeClose', () => {
return cb( () => ( {
isResolving: () => false,
isFeatureActive: () => true,
getCurrentPostType: () => {},
getPostType: () => true,
getEntityRecord: () => ( {
site_icon_url: 'https://fakeUrl.com',
} ),
getCurrentPostType: () => 'post',
} ) );
} );

render( <FullscreenModeClose /> );

const siteIcon = screen.getByAltText( 'Site Icon' );

expect( siteIcon ).toBeVisible();
Expand All @@ -48,11 +46,11 @@ describe( 'FullscreenModeClose', () => {
return cb( () => ( {
isResolving: () => false,
isFeatureActive: () => true,
getCurrentPostType: () => {},
getPostType: () => true,
getEntityRecord: () => ( {
site_icon_url: '',
} ),
getCurrentPostType: () => 'post',
} ) );
} );

Expand All @@ -64,5 +62,33 @@ describe( 'FullscreenModeClose', () => {

expect( container ).toMatchSnapshot();
} );

it( 'should add correct href using post type from initialPost props', () => {
useSelect.mockImplementation( ( cb ) => {
return cb( () => ( {
isResolving: () => false,
isFeatureActive: () => true,
getPostType: () => {
return {
slug: 'page',
labels: {
view_items: 'View Pages',
},
};
},
getEntityRecord: () => ( {
site_icon_url: '',
} ),
getCurrentPostType: () => 'post',
} ) );
} );

render( <FullscreenModeClose initialPost={ { type: 'page' } } /> );

const button = screen.getByLabelText( 'View Pages' );
expect( button.href ).toBe(
'http://localhost/edit.php?post_type=page'
);
} );
} );
} );
7 changes: 5 additions & 2 deletions packages/edit-post/src/components/header/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ const slideX = {
hover: { x: 0, transition: { type: 'tween', delay: 0.2 } },
};

function Header( { setEntitiesSavedStatesCallback } ) {
function Header( { setEntitiesSavedStatesCallback, initialPost } ) {
const isWideViewport = useViewportMatch( 'large' );
const isLargeViewport = useViewportMatch( 'medium' );
const blockToolbarRef = useRef();
Expand Down Expand Up @@ -101,7 +101,10 @@ function Header( { setEntitiesSavedStatesCallback } ) {
variants={ slideX }
transition={ { type: 'tween', delay: 0.8 } }
>
<FullscreenModeClose showTooltip />
<FullscreenModeClose
showTooltip
initialPost={ initialPost }
/>
</motion.div>
</MainDashboardButton.Slot>
<motion.div
Expand Down
3 changes: 2 additions & 1 deletion packages/edit-post/src/components/layout/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ function useEditorStyles() {
] );
}

function Layout() {
function Layout( { initialPost } ) {
useCommands();
useCommonCommands();
useBlockCommands();
Expand Down Expand Up @@ -304,6 +304,7 @@ function Layout() {
setEntitiesSavedStatesCallback={
setEntitiesSavedStatesCallback
}
initialPost={ initialPost }
/>
}
editorNotices={ <EditorNotices /> }
Expand Down
34 changes: 16 additions & 18 deletions packages/edit-post/src/editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,8 @@ function Editor( {
initialEdits,
...props
} ) {
const { currentPost, getPostLinkProps, goBack } = usePostHistory(
initialPostId,
initialPostType
);
const { currentPost, getPostLinkProps, initialPost, goBack } =
usePostHistory( initialPostId, initialPostType );

const { hasInlineToolbar, post, preferredStyleVariations, template } =
useSelect(
Expand Down Expand Up @@ -80,8 +78,8 @@ function Editor( {
const defaultRenderingMode =
currentPost.postType === 'wp_template' ? 'all' : 'post-only';

const editorSettings = useMemo( () => {
const result = {
const editorSettings = useMemo(
() => ( {
Comment thread
ramonjd marked this conversation as resolved.
...settings,
getPostLinkProps,
goBack,
Expand All @@ -91,17 +89,17 @@ function Editor( {
onChange: updatePreferredStyleVariations,
},
hasInlineToolbar,
};
return result;
}, [
settings,
hasInlineToolbar,
preferredStyleVariations,
updatePreferredStyleVariations,
getPostLinkProps,
goBack,
defaultRenderingMode,
] );
} ),
[
settings,
hasInlineToolbar,
preferredStyleVariations,
updatePreferredStyleVariations,
getPostLinkProps,
goBack,
defaultRenderingMode,
]
);

if ( ! post ) {
return null;
Expand All @@ -120,7 +118,7 @@ function Editor( {
<ErrorBoundary>
<CommandMenu />
<EditorInitialization postId={ currentPost.postId } />
<Layout />
<Layout initialPost={ initialPost } />
</ErrorBoundary>
<PostLockedModal />
</ExperimentalEditorProvider>
Expand Down
10 changes: 9 additions & 1 deletion packages/edit-post/src/hooks/use-post-history.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* WordPress dependencies
*/
import { useCallback, useReducer } from '@wordpress/element';
import { useCallback, useReducer, useMemo } from '@wordpress/element';
import { addQueryArgs, getQueryArgs, removeQueryArgs } from '@wordpress/url';

/**
Expand Down Expand Up @@ -35,6 +35,13 @@ export default function usePostHistory( initialPostId, initialPostType ) {
[ { postId: initialPostId, postType: initialPostType } ]
);

const initialPost = useMemo( () => {
return {
type: initialPostType,
id: initialPostId,
};
}, [ initialPostType, initialPostId ] );

const getPostLinkProps = useCallback( ( params ) => {
const currentArgs = getQueryArgs( window.location.href );
const currentUrlWithoutArgs = removeQueryArgs(
Expand Down Expand Up @@ -68,6 +75,7 @@ export default function usePostHistory( initialPostId, initialPostType ) {
return {
currentPost,
getPostLinkProps,
initialPost,
goBack: postHistory.length > 1 ? goBack : undefined,
};
}