Skip to content

Commit

Permalink
Add: Option to pick a pattern on page start.
Browse files Browse the repository at this point in the history
  • Loading branch information
jorgefilipecosta committed Apr 4, 2022
1 parent 15a9154 commit c19587f
Show file tree
Hide file tree
Showing 5 changed files with 155 additions and 4 deletions.
8 changes: 4 additions & 4 deletions lib/compat/wordpress-6.0/block-patterns-update.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ function gutenberg_register_gutenberg_patterns() {
$patterns = array(
'query-standard-posts' => array(
'title' => _x( 'Standard', 'Block pattern title', 'gutenberg' ),
'blockTypes' => array( 'core/query' ),
'blockTypes' => array( 'core/query', 'core/post-content' ),
'categories' => array( 'query' ),
'content' => '<!-- wp:query {"query":{"perPage":3,"pages":0,"offset":0,"postType":"post","order":"desc","orderBy":"date","author":"","search":"","exclude":[],"sticky":"","inherit":false}} -->
<div class="wp-block-query">
Expand All @@ -35,7 +35,7 @@ function gutenberg_register_gutenberg_patterns() {
),
'query-medium-posts' => array(
'title' => _x( 'Image at left', 'Block pattern title', 'gutenberg' ),
'blockTypes' => array( 'core/query' ),
'blockTypes' => array( 'core/query', 'core/post-content' ),
'categories' => array( 'query' ),
'content' => '<!-- wp:query {"query":{"perPage":3,"pages":0,"offset":0,"postType":"post","order":"desc","orderBy":"date","author":"","search":"","exclude":[],"sticky":"","inherit":false}} -->
<div class="wp-block-query">
Expand All @@ -55,7 +55,7 @@ function gutenberg_register_gutenberg_patterns() {
),
'query-small-posts' => array(
'title' => _x( 'Small image and title', 'Block pattern title', 'gutenberg' ),
'blockTypes' => array( 'core/query' ),
'blockTypes' => array( 'core/query', 'core/post-content' ),
'categories' => array( 'query' ),
'content' => '<!-- wp:query {"query":{"perPage":3,"pages":0,"offset":0,"postType":"post","order":"desc","orderBy":"date","author":"","search":"","exclude":[],"sticky":"","inherit":false}} -->
<div class="wp-block-query">
Expand All @@ -74,7 +74,7 @@ function gutenberg_register_gutenberg_patterns() {
),
'query-grid-posts' => array(
'title' => _x( 'Grid', 'Block pattern title', 'gutenberg' ),
'blockTypes' => array( 'core/query' ),
'blockTypes' => array( 'core/query', 'core/post-content' ),
'categories' => array( 'query' ),
'content' => '<!-- wp:query {"query":{"perPage":6,"pages":0,"offset":0,"postType":"post","order":"desc","orderBy":"date","author":"","search":"","exclude":[],"sticky":"exclude","inherit":false},"displayLayout":{"type":"flex","columns":3}} -->
<div class="wp-block-query">
Expand Down
2 changes: 2 additions & 0 deletions packages/edit-post/src/components/layout/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ import SettingsSidebar from '../sidebar/settings-sidebar';
import MetaBoxes from '../meta-boxes';
import WelcomeGuide from '../welcome-guide';
import ActionsPanel from './actions-panel';
import StartPageOptions from '../start-page-options';
import { store as editPostStore } from '../../store';

const interfaceLabels = {
Expand Down Expand Up @@ -286,6 +287,7 @@ function Layout( { styles } ) {
<EditPostPreferencesModal />
<KeyboardShortcutHelpModal />
<WelcomeGuide />
<StartPageOptions />
<Popover.Slot />
<PluginArea onError={ onPluginAreaError } />
</>
Expand Down
121 changes: 121 additions & 0 deletions packages/edit-post/src/components/start-page-options/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
/**
* WordPress dependencies
*/
import { Modal } from '@wordpress/components';
import { __ } from '@wordpress/i18n';
import { useState, 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';

/**
* Internal dependencies
*/
import { store as editPostStore } from '../../store';

function PatternSelection( { onChoosePattern } ) {
const { blockPatterns } = useSelect( ( select ) => {
const { __experimentalGetPatternsByBlockTypes } = select(
blockEditorStore
);
return {
blockPatterns: __experimentalGetPatternsByBlockTypes(
'core/post-content'
),
};
}, [] );
const shownBlockPatterns = useAsyncList( blockPatterns );
const { resetEditorBlocks } = useDispatch( editorStore );
useEffect( () => {
if ( blockPatterns.length <= 1 ) {
onChoosePattern();
}
}, [ blockPatterns.length ] );
return (
<BlockPatternsList
blockPatterns={ blockPatterns }
shownPatterns={ shownBlockPatterns }
onClickPattern={ ( _pattern, blocks ) => {
resetEditorBlocks( blocks );
onChoosePattern();
} }
/>
);
}

const START_PAGE_MODAL_STATES = {
INITIAL: 'INITIAL',
PATTERN: 'PATTERN',
CLOSED: 'CLOSED',
};

export default function StartPageOptions() {
const [ modalState, setModalState ] = useState(
START_PAGE_MODAL_STATES.INITIAL
);
const shouldOpenModel = useSelect(
( select ) => {
if ( modalState !== START_PAGE_MODAL_STATES.INITIAL ) {
return false;
}
const { __experimentalGetPatternsByBlockTypes } = select(
blockEditorStore
);
const {
getCurrentPostType,
getEditedPostContent,
isEditedPostSaveable,
} = select( editorStore );
const { isEditingTemplate, isFeatureActive } = select(
editPostStore
);
return (
getCurrentPostType() === 'page' &&
! isEditedPostSaveable() &&
'' === getEditedPostContent() &&
! isEditingTemplate() &&
! isFeatureActive( 'welcomeGuide' ) &&
__experimentalGetPatternsByBlockTypes( 'core/post-content' )
.length >= 1
);
},
[ modalState ]
);

useEffect( () => {
if ( shouldOpenModel ) {
setModalState( START_PAGE_MODAL_STATES.PATTERN );
}
}, [ shouldOpenModel ] );

if (
modalState === START_PAGE_MODAL_STATES.INITIAL ||
modalState === START_PAGE_MODAL_STATES.CLOSED
) {
return null;
}
return (
<Modal
className="edit-post-start-page-options__modal"
title={ __( 'Choose a pattern' ) }
closeLabel={ __( 'Cancel' ) }
onRequestClose={ () => {
setModalState( START_PAGE_MODAL_STATES.CLOSED );
} }
>
<div className="edit-post-start-page-options__modal-content">
{ modalState === START_PAGE_MODAL_STATES.PATTERN && (
<PatternSelection
onChoosePattern={ () => {
setModalState( START_PAGE_MODAL_STATES.CLOSED );
} }
/>
) }
</div>
</Modal>
);
}
27 changes: 27 additions & 0 deletions packages/edit-post/src/components/start-page-options/style.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
.edit-post-start-page-options__modal {
// 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: $break-medium - $grid-unit-20 * 2;
}
@include break-large() {
height: 70%;
}
}

.edit-post-start-page-options__modal-content .block-editor-block-patterns-list {
display: grid;
grid-template-columns: 1fr 1fr;
grid-gap: $grid-unit-10;

.block-editor-block-patterns-list__list-item {
margin-bottom: 0;
.block-editor-block-preview__container {
min-height: 100px;
}
}
}
1 change: 1 addition & 0 deletions packages/edit-post/src/style.scss
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
@import "./components/text-editor/style.scss";
@import "./components/visual-editor/style.scss";
@import "./components/welcome-guide/style.scss";
@import "./components/start-page-options/style.scss";

/**
* Animations
Expand Down

0 comments on commit c19587f

Please sign in to comment.