Skip to content

Commit

Permalink
Polish root
Browse files Browse the repository at this point in the history
  • Loading branch information
ellatrix committed Nov 14, 2024
1 parent 38cc0b2 commit 16d5b16
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,11 @@ export default function useNavigateToEntityRecord() {

const onNavigateToEntityRecord = useCallback(
( params ) => {
history.push( { ...params, focusMode: true, canvas: 'edit' } );
const _params = { ...params, focusMode: true, canvas: 'edit' };
if ( params.focusMode === false ) {
delete _params.focusMode;
}
history.push( _params );
},
[ history ]
);
Expand Down
3 changes: 3 additions & 0 deletions packages/edit-site/src/components/editor/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,9 @@ export default function EditSiteEditor( { isPostsList = false } ) {
...currentPost,
id: undefined,
type: 'wp_template',
// It would be great if we could use auto-draft, but
// that doesn't seem to work.
status: 'draft',
} );
history.push( {
postId: newPost.id,
Expand Down
36 changes: 23 additions & 13 deletions packages/editor/src/components/post-transform-panel/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,17 +35,23 @@ function TemplatesList( { availableTemplates, onSelect } ) {

function PostTransform() {
const registry = useRegistry();
const { record, postType, postId } = useSelect( ( select ) => {
const { getCurrentPostType, getCurrentPostId } = select( editorStore );
const { getEditedEntityRecord } = select( coreStore );
const type = getCurrentPostType();
const id = getCurrentPostId();
return {
postType: type,
postId: id,
record: getEditedEntityRecord( 'postType', type, id ),
};
}, [] );
const { record, postType, postId, onNavigateToEntityRecord } = useSelect(
( select ) => {
const { getCurrentPostType, getCurrentPostId, getEditorSettings } =
select( editorStore );
const { getEditedEntityRecord } = select( coreStore );
const type = getCurrentPostType();
const id = getCurrentPostId();
return {
postType: type,
postId: id,
record: getEditedEntityRecord( 'postType', type, id ),
onNavigateToEntityRecord:
getEditorSettings().onNavigateToEntityRecord,
};
},
[]
);
const availablePatterns = useAvailablePatterns( record );
const onTemplateSelect = async ( selectedTemplate ) => {
const currentPost = await registry
Expand All @@ -59,9 +65,13 @@ function PostTransform() {
type: 'wp_template',
blocks: selectedTemplate.blocks,
content: serialize( selectedTemplate.blocks ),
status: 'draft',
} );
// To do, this component needs to be aware of edit-site history.
window.location.href = `?postId=${ newPost.id }&postType=${ newPost.type }&canvas=edit`;
onNavigateToEntityRecord( {
postId: newPost.id,
postType: 'wp_template',
focusMode: false,
} );
};
if ( ! availablePatterns?.length ) {
return null;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* WordPress dependencies
*/
import { useSelect } from '@wordpress/data';
import { useSelect, useRegistry } from '@wordpress/data';
import { store as coreStore } from '@wordpress/core-data';
import { useEffect, useState } from '@wordpress/element';
import { __ } from '@wordpress/i18n';
Expand All @@ -27,6 +27,7 @@ import { store as editorStore } from '../../store';
* editor iframe canvas.
*/
export default function EditTemplateBlocksNotification( { contentRef } ) {
const registry = useRegistry();
const { onNavigateToEntityRecord, templateId } = useSelect( ( select ) => {
const { getEditorSettings, getCurrentTemplateId } =
select( editorStore );
Expand Down Expand Up @@ -83,12 +84,31 @@ export default function EditTemplateBlocksNotification( { contentRef } ) {
<ConfirmDialog
isOpen={ isDialogOpen }
confirmButtonText={ __( 'Edit template' ) }
onConfirm={ () => {
onConfirm={ async () => {
setIsDialogOpen( false );
onNavigateToEntityRecord( {
postId: templateId,
postType: 'wp_template',
} );
if ( templateId && typeof templateId === 'string' ) {
onNavigateToEntityRecord( {
postId: templateId,
postType: 'wp_template',
} );
} else {
const currentPost = registry
.select( editorStore )
.getCurrentPost();
const newPost = await registry
.dispatch( coreStore )
.saveEntityRecord( 'postType', 'wp_template', {
...currentPost,
id: undefined,
type: 'wp_template',
status: 'draft',
} );
onNavigateToEntityRecord( {
postId: newPost.id,
postType: 'wp_template',
focusMode: false,
} );
}
} }
onCancel={ () => setIsDialogOpen( false ) }
size="medium"
Expand Down

0 comments on commit 16d5b16

Please sign in to comment.