From c7a57af45ea48f8317c087e7f6a73f58d8acdbf0 Mon Sep 17 00:00:00 2001 From: Glen Davies Date: Wed, 7 Feb 2024 13:15:12 +1300 Subject: [PATCH 01/11] Rename the getPostLinkProps setting --- packages/block-library/src/block/edit.js | 23 ++++------ packages/edit-post/src/editor.js | 10 ++--- ...t-history.js => use-load-entity-record.js} | 34 +++++---------- .../block-editor/use-load-entity-record.js | 29 +++++++++++++ .../block-editor/use-post-link-props.js | 24 ----------- .../block-editor/use-site-editor-settings.js | 8 ++-- .../edit-site/src/components/routes/link.js | 13 +----- .../edit-template-blocks-notification.js | 4 +- .../components/post-template/block-theme.js | 43 +++++++++++-------- .../components/post-template/classic-theme.js | 21 +++++---- .../create-new-template-modal.js | 16 +++---- .../provider/use-block-editor-settings.js | 2 +- 12 files changed, 103 insertions(+), 124 deletions(-) rename packages/edit-post/src/hooks/{use-post-history.js => use-load-entity-record.js} (66%) create mode 100644 packages/edit-site/src/components/block-editor/use-load-entity-record.js delete mode 100644 packages/edit-site/src/components/block-editor/use-post-link-props.js diff --git a/packages/block-library/src/block/edit.js b/packages/block-library/src/block/edit.js index f8359c9889312c..6526317d08308f 100644 --- a/packages/block-library/src/block/edit.js +++ b/packages/block-library/src/block/edit.js @@ -212,7 +212,7 @@ export default function ReusableBlockEdit( { innerBlocks, userCanEdit, getBlockEditingMode, - getPostLinkProps, + onSelectEntityRecord, editingMode, } = useSelect( ( select ) => { @@ -230,19 +230,17 @@ export default function ReusableBlockEdit( { innerBlocks: blocks, userCanEdit: canEdit, getBlockEditingMode: _getBlockEditingMode, - getPostLinkProps: getSettings().getPostLinkProps, + onSelectEntityRecord: getSettings().onSelectEntityRecord, editingMode: _getBlockEditingMode( patternClientId ), }; }, [ patternClientId, ref ] ); - const editOriginalProps = getPostLinkProps - ? getPostLinkProps( { - postId: ref, - postType: 'wp_block', - } ) - : {}; + const editOriginal = onSelectEntityRecord( { + postId: ref, + postType: 'wp_block', + } ); // Sync the editing mode of the pattern block with the inner blocks. useEffect( () => { @@ -343,7 +341,7 @@ export default function ReusableBlockEdit( { }, [ syncDerivedUpdates, patternClientId, registry, setAttributes ] ); const handleEditOriginal = ( event ) => { - editOriginalProps.onClick( event ); + editOriginal( event ); }; const resetContent = () => { @@ -380,13 +378,10 @@ export default function ReusableBlockEdit( { return ( - { userCanEdit && editOriginalProps && ( + { userCanEdit && editOriginal && ( - + { __( 'Edit original' ) } diff --git a/packages/edit-post/src/editor.js b/packages/edit-post/src/editor.js index ce99ed1eef38eb..4fe88fc3a0ef28 100644 --- a/packages/edit-post/src/editor.js +++ b/packages/edit-post/src/editor.js @@ -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 useLoadEntityRecord from './hooks/use-load-entity-record'; const { ExperimentalEditorProvider } = unlock( editorPrivateApis ); @@ -32,8 +32,8 @@ function Editor( { initialEdits, ...props } ) { - const { currentPost, getPostLinkProps, initialPost, goBack } = - usePostHistory( initialPostId, initialPostType ); + const { initialPost, currentPost, onSelectEntityRecord, goBack } = + useLoadEntityRecord( initialPostId, initialPostType ); const { hasInlineToolbar, post, preferredStyleVariations, template } = useSelect( @@ -81,7 +81,7 @@ function Editor( { const editorSettings = useMemo( () => ( { ...settings, - getPostLinkProps, + onSelectEntityRecord, goBack, defaultRenderingMode, __experimentalPreferredStyleVariations: { @@ -95,7 +95,7 @@ function Editor( { hasInlineToolbar, preferredStyleVariations, updatePreferredStyleVariations, - getPostLinkProps, + onSelectEntityRecord, goBack, defaultRenderingMode, ] diff --git a/packages/edit-post/src/hooks/use-post-history.js b/packages/edit-post/src/hooks/use-load-entity-record.js similarity index 66% rename from packages/edit-post/src/hooks/use-post-history.js rename to packages/edit-post/src/hooks/use-load-entity-record.js index fa76258a0c2468..5804fd7d1d38b6 100644 --- a/packages/edit-post/src/hooks/use-post-history.js +++ b/packages/edit-post/src/hooks/use-load-entity-record.js @@ -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 @@ -16,9 +15,9 @@ 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. + * `onSelectEntityRecord` and `goBack` functions. */ -export default function usePostHistory( initialPostId, initialPostType ) { +export default function useLoadEntityRecord( initialPostId, initialPostType ) { const [ postHistory, dispatch ] = useReducer( ( historyState, { type, post } ) => { if ( type === 'push' ) { @@ -42,27 +41,13 @@ 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', - } ); - - return { - href: newUrl, - onClick: ( event ) => { - event?.preventDefault(); - dispatch( { - type: 'push', - post: { postId: params.postId, postType: params.postType }, - } ); - }, + const onSelectEntityRecord = useCallback( ( params ) => { + return ( event ) => { + event?.preventDefault(); + dispatch( { + type: 'push', + post: { postId: params.postId, postType: params.postType }, + } ); }; }, [] ); @@ -76,6 +61,7 @@ export default function usePostHistory( initialPostId, initialPostType ) { currentPost, getPostLinkProps, initialPost, + onSelectEntityRecord, goBack: postHistory.length > 1 ? goBack : undefined, }; } diff --git a/packages/edit-site/src/components/block-editor/use-load-entity-record.js b/packages/edit-site/src/components/block-editor/use-load-entity-record.js new file mode 100644 index 00000000000000..de7f9d35e8e4b7 --- /dev/null +++ b/packages/edit-site/src/components/block-editor/use-load-entity-record.js @@ -0,0 +1,29 @@ +/** + * 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 useLoadEntityRecord() { + const history = useHistory(); + + const onSelectEntityRecord = useCallback( + ( params ) => { + return ( event ) => { + event?.preventDefault(); + + history.push( { ...params, focusMode: true, canvas: 'edit' } ); + }; + }, + [ history ] + ); + + return onSelectEntityRecord; +} diff --git a/packages/edit-site/src/components/block-editor/use-post-link-props.js b/packages/edit-site/src/components/block-editor/use-post-link-props.js deleted file mode 100644 index 3fee876ceb96d5..00000000000000 --- a/packages/edit-site/src/components/block-editor/use-post-link-props.js +++ /dev/null @@ -1,24 +0,0 @@ -/** - * WordPress dependencies - */ -import { privateApis as routerPrivateApis } from '@wordpress/router'; - -/** - * Internal dependencies - */ -import { unlock } from '../../lock-unlock'; -import { getPostLinkProps } from '../routes/link'; - -const { useHistory } = unlock( routerPrivateApis ); - -export function usePostLinkProps() { - const history = useHistory(); - - return ( params, state ) => { - return getPostLinkProps( - history, - { ...params, focusMode: true, canvas: 'edit' }, - state - ); - }; -} diff --git a/packages/edit-site/src/components/block-editor/use-site-editor-settings.js b/packages/edit-site/src/components/block-editor/use-site-editor-settings.js index 77e9e69f02a984..f47a0f2a90ee19 100644 --- a/packages/edit-site/src/components/block-editor/use-site-editor-settings.js +++ b/packages/edit-site/src/components/block-editor/use-site-editor-settings.js @@ -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 useLoadEntityRecord from './use-load-entity-record'; import { FOCUSABLE_ENTITIES } from '../../utils/constants'; const { useBlockEditorSettings } = unlock( editorPrivateApis ); @@ -103,7 +103,7 @@ function useGoBack() { } export function useSpecificEditorSettings() { - const getPostLinkProps = usePostLinkProps(); + const onSelectEntityRecord = useLoadEntityRecord(); const { templateSlug, canvasMode, settings, postWithTemplate } = useSelect( ( select ) => { const { @@ -142,7 +142,7 @@ export function useSpecificEditorSettings() { supportsTemplateMode: true, focusMode: canvasMode !== 'view', defaultRenderingMode, - getPostLinkProps, + onSelectEntityRecord, goBack, // I wonder if they should be set in the post editor too __experimentalArchiveTitleTypeLabel: archiveLabels.archiveTypeLabel, @@ -152,7 +152,7 @@ export function useSpecificEditorSettings() { settings, canvasMode, defaultRenderingMode, - getPostLinkProps, + onSelectEntityRecord, goBack, archiveLabels.archiveTypeLabel, archiveLabels.archiveNameLabel, diff --git a/packages/edit-site/src/components/routes/link.js b/packages/edit-site/src/components/routes/link.js index 1e358328942e68..4423eeeb1d6e8d 100644 --- a/packages/edit-site/src/components/routes/link.js +++ b/packages/edit-site/src/components/routes/link.js @@ -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(); @@ -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, diff --git a/packages/editor/src/components/editor-canvas/edit-template-blocks-notification.js b/packages/editor/src/components/editor-canvas/edit-template-blocks-notification.js index acf13c9025bce3..86cb3b5c4e5dd7 100644 --- a/packages/editor/src/components/editor-canvas/edit-template-blocks-notification.js +++ b/packages/editor/src/components/editor-canvas/edit-template-blocks-notification.js @@ -68,7 +68,7 @@ export default function EditTemplateBlocksNotification( { contentRef } ) { actions: [ { label: __( 'Edit template' ), - onClick: () => editTemplate.onClick(), + onClick: () => editTemplate(), }, ], } @@ -101,7 +101,7 @@ export default function EditTemplateBlocksNotification( { contentRef } ) { confirmButtonText={ __( 'Edit template' ) } onConfirm={ () => { setIsDialogOpen( false ); - editTemplate.onClick(); + editTemplate(); } } onCancel={ () => setIsDialogOpen( false ) } > diff --git a/packages/editor/src/components/post-template/block-theme.js b/packages/editor/src/components/post-template/block-theme.js index a6a102e8470187..6236986883eed6 100644 --- a/packages/editor/src/components/post-template/block-theme.js +++ b/packages/editor/src/components/post-template/block-theme.js @@ -24,18 +24,22 @@ const POPOVER_PROPS = { }; export default function BlockThemeControl( { id } ) { - const { isTemplateHidden, getPostLinkProps, getEditorSettings, hasGoBack } = - useSelect( ( select ) => { - const { getRenderingMode, getEditorSettings: _getEditorSettings } = - unlock( select( editorStore ) ); - const editorSettings = _getEditorSettings(); - return { - isTemplateHidden: getRenderingMode() === 'post-only', - getPostLinkProps: editorSettings.getPostLinkProps, - getEditorSettings: _getEditorSettings, - hasGoBack: editorSettings.hasOwnProperty( 'goBack' ), - }; - }, [] ); + const { + isTemplateHidden, + onSelectEntityRecord, + getEditorSettings, + hasGoBack, + } = useSelect( ( select ) => { + const { getRenderingMode, getEditorSettings: _getEditorSettings } = + unlock( select( editorStore ) ); + const editorSettings = _getEditorSettings(); + return { + isTemplateHidden: getRenderingMode() === 'post-only', + onSelectEntityRecord: editorSettings.onSelectEntityRecord, + getEditorSettings: _getEditorSettings, + hasGoBack: editorSettings.hasOwnProperty( 'goBack' ), + }; + }, [] ); const { editedRecord: template, hasResolved } = useEntityRecord( 'postType', @@ -44,16 +48,16 @@ export default function BlockThemeControl( { id } ) { ); const { createSuccessNotice } = useDispatch( noticesStore ); const { setRenderingMode } = useDispatch( editorStore ); - const editTemplate = getPostLinkProps - ? getPostLinkProps( { - postId: template.id, - postType: 'wp_template', - } ) - : {}; if ( ! hasResolved ) { return null; } + + const selectTemplate = onSelectEntityRecord( { + postId: template.id, + postType: 'wp_template', + } ); + // The site editor does not have a `goBack` setting as it uses its own routing // and assigns its own backlink to focusMode pages. const notificationAction = hasGoBack @@ -80,7 +84,7 @@ export default function BlockThemeControl( { id } ) { { - editTemplate.onClick( event ); + selectTemplate( event ); onClose(); createSuccessNotice( __( @@ -95,6 +99,7 @@ export default function BlockThemeControl( { id } ) { > { __( 'Edit template' ) } + diff --git a/packages/editor/src/components/post-template/classic-theme.js b/packages/editor/src/components/post-template/classic-theme.js index 3d731a500b9d17..7488e9d961eb7e 100644 --- a/packages/editor/src/components/post-template/classic-theme.js +++ b/packages/editor/src/components/post-template/classic-theme.js @@ -64,7 +64,7 @@ function PostTemplateDropdownContent( { onClose } ) { canCreate, canEdit, currentTemplateId, - getPostLinkProps, + onSelectEntityRecord, getEditorSettings, } = useSelect( ( select ) => { @@ -94,20 +94,19 @@ function PostTemplateDropdownContent( { onClose } ) { editorSettings.supportsTemplateMode && !! _currentTemplateId, currentTemplateId: _currentTemplateId, - getPostLinkProps: editorSettings.getPostLinkProps, + onSelectEntityRecord: editorSettings.onSelectEntityRecord, getEditorSettings: select( editorStore ).getEditorSettings, }; }, [ allowSwitchingTemplate ] ); - const editTemplate = - getPostLinkProps && currentTemplateId - ? getPostLinkProps( { - postId: currentTemplateId, - postType: 'wp_template', - } ) - : {}; + const selectTemplate = currentTemplateId + ? onSelectEntityRecord( { + postId: currentTemplateId, + postType: 'wp_template', + } ) + : undefined; const options = useMemo( () => @@ -168,12 +167,12 @@ function PostTemplateDropdownContent( { onClose } ) { } /> ) } - { canEdit && ( + { canEdit && selectTemplate && (