-
Notifications
You must be signed in to change notification settings - Fork 4.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Replace Starter Content modal with inserter panel #66836
Changes from 14 commits
1991eef
45d6b89
6ac1ca7
9787d50
2f20ab1
07c5db9
881d7b1
c6c423e
2f958e0
4f9af6c
52b7849
45ec9af
bfae8e3
2ca5234
8e8210f
0c888af
36a302d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,121 +1,18 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { Modal } from '@wordpress/components'; | ||
import { __ } from '@wordpress/i18n'; | ||
import { useState, useMemo } from '@wordpress/element'; | ||
import { | ||
store as blockEditorStore, | ||
__experimentalBlockPatternsList as BlockPatternsList, | ||
} from '@wordpress/block-editor'; | ||
import { useEffect } from '@wordpress/element'; | ||
import { useSelect, useDispatch } from '@wordpress/data'; | ||
import { store as coreStore } from '@wordpress/core-data'; | ||
import { __unstableSerializeAndClean } from '@wordpress/blocks'; | ||
import { store as preferencesStore } from '@wordpress/preferences'; | ||
import { store as interfaceStore } from '@wordpress/interface'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import { store as editorStore } from '../../store'; | ||
import { TEMPLATE_POST_TYPE } from '../../store/constants'; | ||
|
||
export function useStartPatterns() { | ||
// A pattern is a start pattern if it includes 'core/post-content' in its blockTypes, | ||
// and it has no postTypes declared and the current post type is page or if | ||
// the current post type is part of the postTypes declared. | ||
const { blockPatternsWithPostContentBlockType, postType } = useSelect( | ||
( select ) => { | ||
const { getPatternsByBlockTypes, getBlocksByName } = | ||
select( blockEditorStore ); | ||
const { getCurrentPostType, getRenderingMode } = | ||
select( editorStore ); | ||
const rootClientId = | ||
getRenderingMode() === 'post-only' | ||
? '' | ||
: getBlocksByName( 'core/post-content' )?.[ 0 ]; | ||
return { | ||
blockPatternsWithPostContentBlockType: getPatternsByBlockTypes( | ||
'core/post-content', | ||
rootClientId | ||
), | ||
postType: getCurrentPostType(), | ||
}; | ||
}, | ||
[] | ||
); | ||
|
||
return useMemo( () => { | ||
if ( ! blockPatternsWithPostContentBlockType?.length ) { | ||
return []; | ||
} | ||
|
||
/* | ||
* Filter patterns without postTypes declared if the current postType is page | ||
* or patterns that declare the current postType in its post type array. | ||
*/ | ||
return blockPatternsWithPostContentBlockType.filter( ( pattern ) => { | ||
return ( | ||
( postType === 'page' && ! pattern.postTypes ) || | ||
( Array.isArray( pattern.postTypes ) && | ||
pattern.postTypes.includes( postType ) ) | ||
); | ||
} ); | ||
}, [ postType, blockPatternsWithPostContentBlockType ] ); | ||
} | ||
|
||
function PatternSelection( { blockPatterns, onChoosePattern } ) { | ||
const { editEntityRecord } = useDispatch( coreStore ); | ||
const { postType, postId } = useSelect( ( select ) => { | ||
const { getCurrentPostType, getCurrentPostId } = select( editorStore ); | ||
|
||
return { | ||
postType: getCurrentPostType(), | ||
postId: getCurrentPostId(), | ||
}; | ||
}, [] ); | ||
return ( | ||
<BlockPatternsList | ||
blockPatterns={ blockPatterns } | ||
onClickPattern={ ( _pattern, blocks ) => { | ||
editEntityRecord( 'postType', postType, postId, { | ||
blocks, | ||
content: ( { blocks: blocksForSerialization = [] } ) => | ||
__unstableSerializeAndClean( blocksForSerialization ), | ||
} ); | ||
onChoosePattern(); | ||
} } | ||
/> | ||
); | ||
} | ||
|
||
function StartPageOptionsModal( { onClose } ) { | ||
const startPatterns = useStartPatterns(); | ||
const hasStartPattern = startPatterns.length > 0; | ||
|
||
if ( ! hasStartPattern ) { | ||
return null; | ||
} | ||
|
||
return ( | ||
<Modal | ||
title={ __( 'Choose a pattern' ) } | ||
isFullScreen | ||
onRequestClose={ onClose } | ||
> | ||
<div className="editor-start-page-options__modal-content"> | ||
<PatternSelection | ||
blockPatterns={ startPatterns } | ||
onChoosePattern={ onClose } | ||
/> | ||
</div> | ||
</Modal> | ||
); | ||
} | ||
|
||
export default function StartPageOptions() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should we just remove this component and transform it into a hook? |
||
const [ isClosed, setIsClosed ] = useState( false ); | ||
const shouldEnableModal = useSelect( ( select ) => { | ||
const shouldEnable = useSelect( ( select ) => { | ||
const { isEditedPostDirty, isEditedPostEmpty, getCurrentPostType } = | ||
select( editorStore ); | ||
const preferencesModalActive = | ||
|
@@ -129,13 +26,19 @@ export default function StartPageOptions() { | |
! preferencesModalActive && | ||
! isEditedPostDirty() && | ||
isEditedPostEmpty() && | ||
TEMPLATE_POST_TYPE !== getCurrentPostType() | ||
'page' === getCurrentPostType() | ||
); | ||
}, [] ); | ||
const { setIsInserterOpened } = useDispatch( editorStore ); | ||
|
||
useEffect( () => { | ||
if ( shouldEnable ) { | ||
setIsInserterOpened( { | ||
tab: 'patterns', | ||
category: 'core/starter-content', | ||
} ); | ||
} | ||
}, [ shouldEnable, setIsInserterOpened ] ); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think the way this is built, it won't reopen the panel if you use the command palette to "create a new page" when you're already creating a new page. I think we should probably add the postId as a dependency. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In my testing this is not the case - when you create a new page using the command palette, zoom out is invoked again. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Actually on further testing, this is an issue. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. After chatting with @scruffian, I think I have a work around for this in 0c888af. This change should make sure we take control of the zoom level whenever a new page is created. I tested this by creating a new page, zooming in manually, then creating a new page via the command palette, and it should zoom out again automatically. Is this the behaviour we want? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. looks good to me! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I dont think you should remove this change though, I think we need both. |
||
|
||
if ( ! shouldEnableModal || isClosed ) { | ||
return null; | ||
} | ||
|
||
return <StartPageOptionsModal onClose={ () => setIsClosed( true ) } />; | ||
return null; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -424,9 +424,6 @@ test.describe( 'Image', () => { | |
page, | ||
editor, | ||
} ) => { | ||
// This is a temp workaround for dragging and dropping images from the inserter. | ||
// This should be removed when we have the zoom out view for media categories. | ||
await page.setViewportSize( { width: 1400, height: 800 } ); | ||
Comment on lines
-427
to
-429
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It looks like this isn't needed anymore so I went ahead and removed it. |
||
await editor.insertBlock( { name: 'core/image' } ); | ||
const imageBlock = editor.canvas.getByRole( 'document', { | ||
name: 'Block: Image', | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why did we remove the condition?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe because we want this setting to persist across themes, even if they don't have starter patterns?