-
Notifications
You must be signed in to change notification settings - Fork 4.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add: Modal to choose a start pattern on new templates.
- Loading branch information
1 parent
aede098
commit f592ccb
Showing
8 changed files
with
264 additions
and
23 deletions.
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
166 changes: 166 additions & 0 deletions
166
packages/edit-site/src/components/start-template-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 |
---|---|---|
@@ -0,0 +1,166 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { Modal } from '@wordpress/components'; | ||
import { __ } from '@wordpress/i18n'; | ||
import { useState, useEffect, useMemo } from '@wordpress/element'; | ||
import { __experimentalBlockPatternsList as BlockPatternsList } from '@wordpress/block-editor'; | ||
import { useSelect } from '@wordpress/data'; | ||
import { useAsyncList } from '@wordpress/compose'; | ||
import { store as preferencesStore } from '@wordpress/preferences'; | ||
import { parse } from '@wordpress/blocks'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import { store as editSiteStore } from '../../store'; | ||
import { store as coreStore, useEntityBlockEditor } from '@wordpress/core-data'; | ||
import apiFetch from '@wordpress/api-fetch'; | ||
import { addQueryArgs } from '@wordpress/url'; | ||
|
||
function useFallbackTemplateContent( slug, isCustom = false ) { | ||
const [ templateContent, setTemplateContent ] = useState( '' ); | ||
|
||
useEffect( () => { | ||
apiFetch( { | ||
path: addQueryArgs( '/wp/v2/templates/lookup', { | ||
slug, | ||
is_custom: isCustom, | ||
ignore_empty: true, | ||
} ), | ||
} ).then( ( { content } ) => setTemplateContent( content.raw ) ); | ||
}, [ slug ] ); | ||
return templateContent; | ||
} | ||
|
||
function PatternSelection( { fallbackContent, onChoosePattern, postType } ) { | ||
const [ , , onChange ] = useEntityBlockEditor( 'postType', postType ); | ||
const blockPatterns = useMemo( | ||
() => [ | ||
{ | ||
name: 'fallback', | ||
blocks: parse( fallbackContent ), | ||
}, | ||
{ | ||
name: 'start-blank', | ||
blocks: parse( | ||
'<!-- wp:paragraph --><p></p><!-- /wp:paragraph -->' | ||
), | ||
title: __( 'Start blank' ), | ||
}, | ||
], | ||
[ fallbackContent ] | ||
); | ||
const shownBlockPatterns = useAsyncList( blockPatterns ); | ||
return ( | ||
<BlockPatternsList | ||
blockPatterns={ blockPatterns } | ||
shownPatterns={ shownBlockPatterns } | ||
onClickPattern={ ( pattern, blocks ) => { | ||
onChange( 'start-blank' === pattern.name ? [] : blocks, { | ||
selection: undefined, | ||
} ); | ||
onChoosePattern(); | ||
} } | ||
/> | ||
); | ||
} | ||
|
||
function StartModal( { slug, isCustom, onClose, postType } ) { | ||
const fallbackContent = useFallbackTemplateContent( slug, isCustom ); | ||
if ( ! fallbackContent ) { | ||
return null; | ||
} | ||
return ( | ||
<Modal | ||
className="edit-site-start-template-options__modal" | ||
title={ __( 'Choose a pattern' ) } | ||
closeLabel={ __( 'Cancel' ) } | ||
focusOnMount="firstElement" | ||
onRequestClose={ onClose } | ||
> | ||
<div className="edit-site-start-template-options__modal-content"> | ||
<PatternSelection | ||
fallbackContent={ fallbackContent } | ||
slug={ slug } | ||
isCustom={ isCustom } | ||
postType={ postType } | ||
onChoosePattern={ () => { | ||
onClose(); | ||
} } | ||
/> | ||
</div> | ||
</Modal> | ||
); | ||
} | ||
|
||
const START_TEMPLATE_MODAL_STATES = { | ||
INITIAL: 'INITIAL', | ||
PATTERN: 'PATTERN', | ||
CLOSED: 'CLOSED', | ||
}; | ||
|
||
export default function StartTemplateOptions() { | ||
const [ modalState, setModalState ] = useState( | ||
START_TEMPLATE_MODAL_STATES.INITIAL | ||
); | ||
const { shouldOpenModel, slug, isCustom, postType } = useSelect( | ||
( select ) => { | ||
const { getEditedPostType, getEditedPostId } = | ||
select( editSiteStore ); | ||
const _postType = getEditedPostType(); | ||
const postId = getEditedPostId(); | ||
const { | ||
__experimentalGetDirtyEntityRecords, | ||
getEditedEntityRecord, | ||
} = select( coreStore ); | ||
const templateRecord = getEditedEntityRecord( | ||
'postType', | ||
_postType, | ||
postId | ||
); | ||
|
||
const hasDirtyEntityRecords = | ||
__experimentalGetDirtyEntityRecords().length > 0; | ||
|
||
return { | ||
shouldOpenModel: | ||
modalState === START_TEMPLATE_MODAL_STATES.INITIAL && | ||
! hasDirtyEntityRecords && | ||
'' === templateRecord.content && | ||
'wp_template' === _postType && | ||
! select( preferencesStore ).get( | ||
'core/edit-site', | ||
'welcomeGuide' | ||
), | ||
slug: templateRecord.slug, | ||
isCustom: templateRecord.is_custom, | ||
postType: _postType, | ||
}; | ||
}, | ||
[ modalState ] | ||
); | ||
|
||
useEffect( () => { | ||
if ( shouldOpenModel ) { | ||
setModalState( START_TEMPLATE_MODAL_STATES.PATTERN ); | ||
} | ||
}, [ shouldOpenModel ] ); | ||
|
||
if ( | ||
modalState === START_TEMPLATE_MODAL_STATES.INITIAL || | ||
modalState === START_TEMPLATE_MODAL_STATES.CLOSED | ||
) { | ||
return null; | ||
} | ||
return ( | ||
<StartModal | ||
slug={ slug } | ||
isCustom={ isCustom } | ||
postType={ postType } | ||
onClose={ () => | ||
setModalState( START_TEMPLATE_MODAL_STATES.CLOSED ) | ||
} | ||
/> | ||
); | ||
} |
59 changes: 59 additions & 0 deletions
59
packages/edit-site/src/components/start-template-options/style.scss
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 |
---|---|---|
@@ -0,0 +1,59 @@ | ||
.edit-site-start-template-options__modal.components-modal__frame { | ||
// To keep modal dimensions consistent as subsections are navigated, width | ||
// and height are used instead of max-(width/height). | ||
@include break-small() { | ||
width: calc(100% - #{ $grid-unit-20 * 2 }); | ||
height: calc(100% - #{ $header-height * 2 }); | ||
} | ||
@include break-medium() { | ||
width: 70%; | ||
} | ||
@include break-large() { | ||
height: fit-content; | ||
} | ||
} | ||
|
||
// 2 column masonry layout. | ||
.edit-site-start-template-options__modal-content .block-editor-block-patterns-list { | ||
display: flex; | ||
width: 100%; | ||
margin-top: $grid-unit-05; | ||
gap: $grid-unit-10; | ||
|
||
.block-editor-block-patterns-list__list-item { | ||
break-inside: avoid-column; | ||
margin-bottom: $grid-unit-30; | ||
width: 240px; | ||
|
||
> div { | ||
width: 240px !important; | ||
} | ||
|
||
.block-editor-block-preview__container { | ||
height: 320px; | ||
width: 240px; | ||
} | ||
|
||
.block-editor-block-preview__content { | ||
width: 100%; | ||
position: absolute; | ||
} | ||
} | ||
|
||
// The start blank pattern is the last and we are selecting it. | ||
.block-editor-block-patterns-list__list-item:nth-last-child(2) { | ||
.block-editor-block-patterns-list__item-title { | ||
position: absolute; | ||
padding: 0; | ||
width: 100%; | ||
top: 50%; | ||
margin-top: -1em; | ||
} | ||
.block-editor-block-preview__container { | ||
background: #f0f0f0; | ||
} | ||
iframe { | ||
display: none; | ||
} | ||
} | ||
} |
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