-
Notifications
You must be signed in to change notification settings - Fork 4.8k
Site Editor: Support starter patterns #60745
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
Changes from all commits
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 |
|---|---|---|
|
|
@@ -3,31 +3,39 @@ | |
| */ | ||
| import { Modal } from '@wordpress/components'; | ||
| import { __ } from '@wordpress/i18n'; | ||
| import { useState, useMemo } from '@wordpress/element'; | ||
| import { useState, useMemo, useEffect } from '@wordpress/element'; | ||
| import { | ||
| store as blockEditorStore, | ||
| __experimentalBlockPatternsList as BlockPatternsList, | ||
| } from '@wordpress/block-editor'; | ||
| import { useSelect, useDispatch } from '@wordpress/data'; | ||
| import { useAsyncList } from '@wordpress/compose'; | ||
| import { store as editorStore } from '@wordpress/editor'; | ||
| import { store as coreStore } from '@wordpress/core-data'; | ||
|
|
||
| /** | ||
| * Internal dependencies | ||
| */ | ||
| import { store as editPostStore } from '../../store'; | ||
| import { store as editorStore } from '../../store'; | ||
|
|
||
| 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 } = select( blockEditorStore ); | ||
| const { getCurrentPostType } = select( editorStore ); | ||
| 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' ), | ||
| blockPatternsWithPostContentBlockType: getPatternsByBlockTypes( | ||
| 'core/post-content', | ||
| rootClientId | ||
|
Contributor
Author
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. If we don't pass the right rootClientId, the list of patterns can be empty because we're not allowed to insert patterns top level in "template-locked" mode. |
||
| ), | ||
| postType: getCurrentPostType(), | ||
| }; | ||
| }, | ||
|
|
@@ -49,13 +57,21 @@ function useStartPatterns() { | |
|
|
||
| function PatternSelection( { blockPatterns, onChoosePattern } ) { | ||
| const shownBlockPatterns = useAsyncList( blockPatterns ); | ||
| const { resetEditorBlocks } = useDispatch( editorStore ); | ||
| const { editEntityRecord } = useDispatch( coreStore ); | ||
| const { postType, postId } = useSelect( ( select ) => { | ||
| const { getCurrentPostType, getCurrentPostId } = select( editorStore ); | ||
|
|
||
| return { | ||
| postType: getCurrentPostType(), | ||
| postId: getCurrentPostId(), | ||
| }; | ||
| }, [] ); | ||
| return ( | ||
| <BlockPatternsList | ||
| blockPatterns={ blockPatterns } | ||
| shownPatterns={ shownBlockPatterns } | ||
| onClickPattern={ ( _pattern, blocks ) => { | ||
| resetEditorBlocks( blocks ); | ||
| editEntityRecord( 'postType', postType, postId, { blocks } ); | ||
|
Contributor
Author
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. This works regardless of the mode, we're actually replacing the patterns of the given post type directly. |
||
| onChoosePattern(); | ||
| } } | ||
| /> | ||
|
|
@@ -72,12 +88,11 @@ function StartPageOptionsModal( { onClose } ) { | |
|
|
||
| return ( | ||
| <Modal | ||
| className="edit-post-start-page-options__modal" | ||
| title={ __( 'Choose a pattern' ) } | ||
| isFullScreen | ||
| onRequestClose={ onClose } | ||
| > | ||
| <div className="edit-post-start-page-options__modal-content"> | ||
| <div className="editor-start-page-options__modal-content"> | ||
| <PatternSelection | ||
| blockPatterns={ startPatterns } | ||
| onChoosePattern={ onClose } | ||
|
|
@@ -89,17 +104,29 @@ function StartPageOptionsModal( { onClose } ) { | |
|
|
||
| export default function StartPageOptions() { | ||
| const [ isClosed, setIsClosed ] = useState( false ); | ||
| const shouldEnableModal = useSelect( ( select ) => { | ||
| const { isCleanNewPost, getRenderingMode } = select( editorStore ); | ||
| const { isFeatureActive } = select( editPostStore ); | ||
| const { shouldEnableModal, postType, postId } = useSelect( ( select ) => { | ||
| const { | ||
| isEditedPostDirty, | ||
| isEditedPostEmpty, | ||
| getCurrentPostType, | ||
| getCurrentPostId, | ||
| getEditorSettings, | ||
| } = select( editorStore ); | ||
| const { __unstableIsPreviewMode: isPreviewMode } = getEditorSettings(); | ||
|
|
||
| return ( | ||
| getRenderingMode() === 'post-only' && | ||
| ! isFeatureActive( 'welcomeGuide' ) && | ||
|
Contributor
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. We don't want to preserve the welcome guides check?
Contributor
Author
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 didn't feel that important for me for this PR and it's still and edit-post only preference, so if we want to do that, we'll have to first migrate it in its own PR. |
||
| isCleanNewPost() | ||
| ); | ||
| return { | ||
| shouldEnableModal: | ||
| ! isPreviewMode && ! isEditedPostDirty() && isEditedPostEmpty(), | ||
| postType: getCurrentPostType(), | ||
| postId: getCurrentPostId(), | ||
| }; | ||
| }, [] ); | ||
|
|
||
| useEffect( () => { | ||
|
ntsekouras marked this conversation as resolved.
|
||
| // Should reset the modal state when navigating to a new page/post. | ||
| setIsClosed( false ); | ||
| }, [ postType, postId ] ); | ||
|
|
||
| if ( ! shouldEnableModal || isClosed ) { | ||
| return null; | ||
| } | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.