-
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
Merged
Merged
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
1991eef
Inserter: Add 'Starter Content' category to the inserter
ellatrix 45d6b89
Add cat condition
ellatrix 6ac1ca7
Replace Starter Content modal with inserter panel
ellatrix 9787d50
remove option condition
ellatrix 2f20ab1
remove duplicated category
draganescu 07c5db9
Attempt to fix media panel test
mikachan 881d7b1
Attempt to fix style variations test
mikachan c6c423e
Attempt to fix recursive pattern test
mikachan 2f958e0
Attempt to fix template resolution test
mikachan 4f9af6c
Attempt to fix new page test
mikachan 52b7849
Tweak template resolution test
mikachan 45ec9af
Remove categoryObject
mikachan bfae8e3
Fix aria-current on category tabs
mikachan 2ca5234
Fix selected cat in patterns explorer
mikachan 8e8210f
pass postId so that the inserter gets reopened when a new page is cre…
scruffian 0c888af
Take control of zoom if a new page is created
mikachan 36a302d
Add post Id back
scruffian File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
147 changes: 28 additions & 119 deletions
147
packages/editor/src/components/start-page-options/index.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,141 +1,50 @@ | ||
/** | ||
* 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 { isEditedPostDirty, isEditedPostEmpty, getCurrentPostType } = | ||
select( editorStore ); | ||
const { postId, shouldEnable } = useSelect( ( select ) => { | ||
const { | ||
isEditedPostDirty, | ||
isEditedPostEmpty, | ||
getCurrentPostId, | ||
getCurrentPostType, | ||
} = select( editorStore ); | ||
const preferencesModalActive = | ||
select( interfaceStore ).isModalActive( 'editor/preferences' ); | ||
const choosePatternModalEnabled = select( preferencesStore ).get( | ||
'core', | ||
'enableChoosePatternModal' | ||
); | ||
return ( | ||
choosePatternModalEnabled && | ||
! preferencesModalActive && | ||
! isEditedPostDirty() && | ||
isEditedPostEmpty() && | ||
TEMPLATE_POST_TYPE !== getCurrentPostType() | ||
); | ||
return { | ||
postId: getCurrentPostId(), | ||
shouldEnable: | ||
choosePatternModalEnabled && | ||
! preferencesModalActive && | ||
! isEditedPostDirty() && | ||
isEditedPostEmpty() && | ||
'page' === getCurrentPostType(), | ||
}; | ||
}, [] ); | ||
const { setIsInserterOpened } = useDispatch( editorStore ); | ||
|
||
useEffect( () => { | ||
if ( shouldEnable ) { | ||
setIsInserterOpened( { | ||
tab: 'patterns', | ||
category: 'core/starter-content', | ||
} ); | ||
} | ||
}, [ postId, shouldEnable, setIsInserterOpened ] ); | ||
|
||
if ( ! shouldEnableModal || isClosed ) { | ||
return null; | ||
} | ||
|
||
return <StartPageOptionsModal onClose={ () => setIsClosed( true ) } />; | ||
return null; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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', | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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?