Skip to content

Commit

Permalink
Add: Patterns to the template start modal.
Browse files Browse the repository at this point in the history
  • Loading branch information
jorgefilipecosta committed Feb 17, 2023
1 parent 0b5ca22 commit 26893d4
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 8 deletions.
11 changes: 11 additions & 0 deletions lib/blocks.php
Original file line number Diff line number Diff line change
Expand Up @@ -352,3 +352,14 @@ function gutenberg_register_legacy_social_link_blocks() {
}

add_action( 'init', 'gutenberg_register_legacy_social_link_blocks' );

register_block_pattern(
'query/template-type-test',
array(
'title' => __( 'Template type test', 'gutenberg' ),
'templateTypes' => array( '404', 'author' ),
'content' => '<!-- wp:paragraph {"align":"center","fontSize":"x-large"} -->
<p class="has-text-align-center has-x-large-font-size">404</p>
<!-- /wp:paragraph -->',
)
);
48 changes: 40 additions & 8 deletions packages/edit-site/src/components/start-template-options/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@
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 {
__experimentalBlockPatternsList as BlockPatternsList,
store as blockEditorStore,
} from '@wordpress/block-editor';
import { useSelect } from '@wordpress/data';
import { useAsyncList } from '@wordpress/compose';
import { store as preferencesStore } from '@wordpress/preferences';
Expand Down Expand Up @@ -35,25 +38,54 @@ function useFallbackTemplateContent( slug, isCustom = false ) {

const START_BLANK_TITLE = __( 'Start blank' );

function PatternSelection( { fallbackContent, onChoosePattern, postType } ) {
const [ , , onChange ] = useEntityBlockEditor( 'postType', postType );
const blockPatterns = useMemo(
() => [
function useStartPatterns( fallbackContent ) {
const { slug, patterns } = useSelect( ( select ) => {
const { getEditedPostType, getEditedPostId } = select( editSiteStore );
const { getEntityRecord } = select( coreStore );
const postId = getEditedPostId();
const postType = getEditedPostType();
const record = getEntityRecord( 'postType', postType, postId );
const { getSettings } = select( blockEditorStore );
return {
slug: record.slug,
patterns: getSettings().__experimentalBlockPatterns,
};
}, [] );

return useMemo( () => {
// filter patterns that are supposed to be used in the current template being edited.
return [
{
name: 'fallback',
blocks: parse( fallbackContent ),
title: __( 'Fallback content' ),
},
...patterns
.filter( ( pattern ) => {
return (
Array.isArray( pattern.templateTypes ) &&
pattern.templateTypes.some( ( templateType ) =>
slug.startsWith( templateType )
)
);
} )
.map( ( pattern ) => {
return { ...pattern, blocks: parse( pattern.content ) };
} ),
{
name: 'start-blank',
blocks: parse(
'<!-- wp:paragraph --><p></p><!-- /wp:paragraph -->'
),
title: START_BLANK_TITLE,
},
],
[ fallbackContent ]
);
];
}, [ fallbackContent, slug, patterns ] );
}

function PatternSelection( { fallbackContent, onChoosePattern, postType } ) {
const [ , , onChange ] = useEntityBlockEditor( 'postType', postType );
const blockPatterns = useStartPatterns( fallbackContent );
const shownBlockPatterns = useAsyncList( blockPatterns );

return (
Expand Down

0 comments on commit 26893d4

Please sign in to comment.