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
26 changes: 10 additions & 16 deletions packages/block-library/src/block/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ export default function ReusableBlockEdit( {
innerBlocks,
userCanEdit,
getBlockEditingMode,
getPostLinkProps,
onNavigateToEntityRecord,
editingMode,
} = useSelect(
( select ) => {
Expand All @@ -230,20 +230,14 @@ export default function ReusableBlockEdit( {
innerBlocks: blocks,
userCanEdit: canEdit,
getBlockEditingMode: _getBlockEditingMode,
getPostLinkProps: getSettings().getPostLinkProps,
onNavigateToEntityRecord:
getSettings().onNavigateToEntityRecord,
editingMode: _getBlockEditingMode( patternClientId ),
};
},
[ patternClientId, ref ]
);

const editOriginalProps = getPostLinkProps
? getPostLinkProps( {
postId: ref,
postType: 'wp_block',
} )
: {};

// Sync the editing mode of the pattern block with the inner blocks.
useEffect( () => {
setBlockEditMode(
Expand Down Expand Up @@ -342,8 +336,11 @@ export default function ReusableBlockEdit( {
}, blockEditorStore );
}, [ syncDerivedUpdates, patternClientId, registry, setAttributes ] );

const handleEditOriginal = ( event ) => {
editOriginalProps.onClick( event );
const handleEditOriginal = () => {
onNavigateToEntityRecord( {
postId: ref,
postType: 'wp_block',
} );
};

const resetContent = () => {
Expand Down Expand Up @@ -380,13 +377,10 @@ export default function ReusableBlockEdit( {

return (
<RecursionProvider uniqueId={ ref }>
{ userCanEdit && editOriginalProps && (
{ userCanEdit && onNavigateToEntityRecord && (
<BlockControls>
<ToolbarGroup>
<ToolbarButton
href={ editOriginalProps.href }
onClick={ handleEditOriginal }
>
<ToolbarButton onClick={ handleEditOriginal }>
{ __( 'Edit original' ) }
</ToolbarButton>
</ToolbarGroup>
Expand Down
4 changes: 3 additions & 1 deletion packages/edit-post/src/components/header/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,9 @@ function Header( { setEntitiesSavedStatesCallback, initialPost } ) {
hasBlockSelection:
!! select( blockEditorStore ).getBlockSelectionStart(),
hasActiveMetaboxes: select( editPostStore ).hasMetaBoxes(),
hasHistory: !! select( editorStore ).getEditorSettings().goBack,
hasHistory:
!! select( editorStore ).getEditorSettings()
.onNavigateToPreviousEntityRecord,
isPublishSidebarOpened:
select( editPostStore ).isPublishSidebarOpened(),
hasFixedToolbar: getPreference( 'core', 'fixedToolbar' ),
Expand Down
2 changes: 1 addition & 1 deletion packages/edit-post/src/components/layout/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ function Layout( { initialPost } ) {
documentLabel: postTypeLabel || _x( 'Document', 'noun' ),
hasBlockSelected:
!! select( blockEditorStore ).getBlockSelectionStart(),
hasHistory: !! getEditorSettings().goBack,
hasHistory: !! getEditorSettings().onNavigateToPreviousEntityRecord,
};
}, [] );

Expand Down
18 changes: 11 additions & 7 deletions packages/edit-post/src/editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import Layout from './components/layout';
import EditorInitialization from './components/editor-initialization';
import { store as editPostStore } from './store';
import { unlock } from './lock-unlock';
import usePostHistory from './hooks/use-post-history';
import useNavigateToEntityRecord from './hooks/use-navigate-to-entity-record';

const { ExperimentalEditorProvider } = unlock( editorPrivateApis );

Expand All @@ -32,8 +32,12 @@ function Editor( {
initialEdits,
...props
} ) {
const { currentPost, getPostLinkProps, initialPost, goBack } =
usePostHistory( initialPostId, initialPostType );
const {
initialPost,
currentPost,
onNavigateToEntityRecord,
onNavigateToPreviousEntityRecord,
} = useNavigateToEntityRecord( initialPostId, initialPostType );

const { hasInlineToolbar, post, preferredStyleVariations, template } =
useSelect(
Expand Down Expand Up @@ -81,8 +85,8 @@ function Editor( {
const editorSettings = useMemo(
() => ( {
...settings,
getPostLinkProps,
goBack,
onNavigateToEntityRecord,
onNavigateToPreviousEntityRecord,
defaultRenderingMode,
__experimentalPreferredStyleVariations: {
value: preferredStyleVariations,
Expand All @@ -95,8 +99,8 @@ function Editor( {
hasInlineToolbar,
preferredStyleVariations,
updatePreferredStyleVariations,
getPostLinkProps,
goBack,
onNavigateToEntityRecord,
onNavigateToPreviousEntityRecord,
defaultRenderingMode,
]
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
* WordPress dependencies
*/
import { useCallback, useReducer, useMemo } from '@wordpress/element';
import { addQueryArgs, getQueryArgs, removeQueryArgs } from '@wordpress/url';

/**
* A hook that records the 'entity' history in the post editor as a user
Expand All @@ -16,9 +15,12 @@ import { addQueryArgs, getQueryArgs, removeQueryArgs } from '@wordpress/url';
* @param {string} initialPostType The post type of the post when the editor loaded.
*
* @return {Object} An object containing the `currentPost` variable and
* `getPostLinkProps` and `goBack` functions.
* `onNavigateToEntityRecord` and `onNavigateToPreviousEntityRecord` functions.
*/
export default function usePostHistory( initialPostId, initialPostType ) {
export default function useNavigateToEntityRecord(
initialPostId,
initialPostType
) {
const [ postHistory, dispatch ] = useReducer(
( historyState, { type, post } ) => {
if ( type === 'push' ) {
Expand All @@ -42,40 +44,26 @@ export default function usePostHistory( initialPostId, initialPostType ) {
};
}, [ initialPostType, initialPostId ] );

const getPostLinkProps = useCallback( ( params ) => {
const currentArgs = getQueryArgs( window.location.href );
const currentUrlWithoutArgs = removeQueryArgs(
window.location.href,
...Object.keys( currentArgs )
);

const newUrl = addQueryArgs( currentUrlWithoutArgs, {
post: params.postId,
action: 'edit',
const onNavigateToEntityRecord = useCallback( ( params ) => {
dispatch( {
type: 'push',
post: { postId: params.postId, postType: params.postType },
} );

return {
href: newUrl,
onClick: ( event ) => {
event?.preventDefault();
dispatch( {
type: 'push',
post: { postId: params.postId, postType: params.postType },
} );
},
};
}, [] );

const goBack = useCallback( () => {
const onNavigateToPreviousEntityRecord = useCallback( () => {
dispatch( { type: 'pop' } );
}, [] );

const currentPost = postHistory[ postHistory.length - 1 ];

return {
currentPost,
getPostLinkProps,
initialPost,
goBack: postHistory.length > 1 ? goBack : undefined,
onNavigateToEntityRecord,
onNavigateToPreviousEntityRecord:
postHistory.length > 1
? onNavigateToPreviousEntityRecord
: undefined,
};
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/**
* WordPress dependencies
*/
import { privateApis as routerPrivateApis } from '@wordpress/router';
import { useCallback } from '@wordpress/element';

/**
* Internal dependencies
*/
import { unlock } from '../../lock-unlock';

const { useHistory } = unlock( routerPrivateApis );

export default function useNavigateToEntityRecord() {
const history = useHistory();

const onNavigateToEntityRecord = useCallback(
( params ) => {
history.push( { ...params, focusMode: true, canvas: 'edit' } );
},
[ history ]
);

return onNavigateToEntityRecord;
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { privateApis as routerPrivateApis } from '@wordpress/router';
*/
import { store as editSiteStore } from '../../store';
import { unlock } from '../../lock-unlock';
import { usePostLinkProps } from './use-post-link-props';
import useNavigateToEntityRecord from './use-navigate-to-entity-record';
import { FOCUSABLE_ENTITIES } from '../../utils/constants';

const { useBlockEditorSettings } = unlock( editorPrivateApis );
Expand Down Expand Up @@ -90,7 +90,7 @@ function useArchiveLabel( templateSlug ) {
);
}

function useGoBack() {
function useNavigateToPreviousEntityRecord() {
const location = useLocation();
const history = useHistory();
const goBack = useMemo( () => {
Expand All @@ -103,7 +103,7 @@ function useGoBack() {
}

export function useSpecificEditorSettings() {
const getPostLinkProps = usePostLinkProps();
const onNavigateToEntityRecord = useNavigateToEntityRecord();
const { templateSlug, canvasMode, settings, postWithTemplate } = useSelect(
( select ) => {
const {
Expand Down Expand Up @@ -133,7 +133,8 @@ export function useSpecificEditorSettings() {
);
const archiveLabels = useArchiveLabel( templateSlug );
const defaultRenderingMode = postWithTemplate ? 'template-locked' : 'all';
const goBack = useGoBack();
const onNavigateToPreviousEntityRecord =
useNavigateToPreviousEntityRecord();
const defaultEditorSettings = useMemo( () => {
return {
...settings,
Expand All @@ -142,8 +143,8 @@ export function useSpecificEditorSettings() {
supportsTemplateMode: true,
focusMode: canvasMode !== 'view',
defaultRenderingMode,
getPostLinkProps,
goBack,
onNavigateToEntityRecord,
onNavigateToPreviousEntityRecord,
// I wonder if they should be set in the post editor too
__experimentalArchiveTitleTypeLabel: archiveLabels.archiveTypeLabel,
__experimentalArchiveTitleNameLabel: archiveLabels.archiveNameLabel,
Expand All @@ -152,8 +153,8 @@ export function useSpecificEditorSettings() {
settings,
canvasMode,
defaultRenderingMode,
getPostLinkProps,
goBack,
onNavigateToEntityRecord,
onNavigateToPreviousEntityRecord,
archiveLabels.archiveTypeLabel,
archiveLabels.archiveNameLabel,
] );
Expand Down
13 changes: 2 additions & 11 deletions packages/edit-site/src/components/routes/link.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,8 @@ import {

const { useHistory } = unlock( routerPrivateApis );

export function getPostLinkProps(
history,
params = {},
state,
shouldReplace = false
) {
export function useLink( params, state, shouldReplace = false ) {
const history = useHistory();
function onClick( event ) {
event?.preventDefault();

Expand Down Expand Up @@ -52,11 +48,6 @@ export function getPostLinkProps(
};
}

export function useLink( params, state, shouldReplace ) {
const history = useHistory();
return getPostLinkProps( history, params, state, shouldReplace );
}

export default function Link( {
params = {},
state,
Expand Down
39 changes: 22 additions & 17 deletions packages/editor/src/components/document-bar/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,32 +48,37 @@ const icons = {
};

export default function DocumentBar() {
const { postType, postId, goBack } = useSelect( ( select ) => {
const {
getCurrentPostId,
getCurrentPostType,
getEditorSettings: getSettings,
} = select( editorStore );
const back = getSettings().goBack;
return {
postType: getCurrentPostType(),
postId: getCurrentPostId(),
goBack: typeof back === 'function' ? back : undefined,
getEditorSettings: getSettings,
};
}, [] );
const { postType, postId, onNavigateToPreviousEntityRecord } = useSelect(
( select ) => {
const {
getCurrentPostId,
getCurrentPostType,
getEditorSettings: getSettings,
} = select( editorStore );
return {
postType: getCurrentPostType(),
postId: getCurrentPostId(),
onNavigateToPreviousEntityRecord:
getSettings().onNavigateToPreviousEntityRecord,
getEditorSettings: getSettings,
};
},
[]
);

const handleOnBack = () => {
if ( goBack ) {
goBack();
if ( onNavigateToPreviousEntityRecord ) {
onNavigateToPreviousEntityRecord();
}
};

return (
<BaseDocumentActions
postType={ postType }
postId={ postId }
onBack={ goBack ? handleOnBack : undefined }
onBack={
onNavigateToPreviousEntityRecord ? handleOnBack : undefined
}
/>
);
}
Expand Down
Loading