From fa78b66aea850d13e0621d0fcc6ddaf5ca3772f4 Mon Sep 17 00:00:00 2001 From: Ramon Date: Mon, 9 Sep 2024 09:41:03 +1000 Subject: [PATCH] Patterns: add opt out preference to the 'Choose a Pattern' modal when adding a page (#65026) * Adds a preference to disable the loading of the Choose a pattern modal when adding new pages. * Return an empty array from the memoized value of useStartPatterns() so that the preferences modal has a value to test. * Don't reset the `isClosed` value in the same editor session. This prevents the modal popping open between page > template part > page navigation. Co-authored-by: ramonjd Co-authored-by: andrewserong Co-authored-by: kevin940726 Co-authored-by: noisysocks Co-authored-by: jasmussen Co-authored-by: jakobtrost Co-authored-by: johnstonphilip Co-authored-by: annezazu Co-authored-by: MattrCoUk Co-authored-by: jasonbahl Co-authored-by: colorful-tones Co-authored-by: Blindmikey Co-authored-by: richtabor Co-authored-by: luminuu Co-authored-by: stokesman Co-authored-by: jordesign --- packages/edit-post/src/index.js | 1 + packages/edit-site/src/index.js | 1 + packages/edit-site/src/posts.js | 1 + .../src/components/preferences-modal/index.js | 13 +++++ .../components/start-page-options/index.js | 54 ++++++++++--------- 5 files changed, 44 insertions(+), 26 deletions(-) diff --git a/packages/edit-post/src/index.js b/packages/edit-post/src/index.js index 51abac654da29..daf789cb0a2ec 100644 --- a/packages/edit-post/src/index.js +++ b/packages/edit-post/src/index.js @@ -70,6 +70,7 @@ export function initializeEditor( showBlockBreadcrumbs: true, showIconLabels: false, showListViewByDefault: false, + enableChoosePatternModal: true, isPublishSidebarEnabled: true, } ); diff --git a/packages/edit-site/src/index.js b/packages/edit-site/src/index.js index 4ed5b96e3844d..1aceecc4d8b1f 100644 --- a/packages/edit-site/src/index.js +++ b/packages/edit-site/src/index.js @@ -79,6 +79,7 @@ export function initializeEditor( id, settings ) { openPanels: [ 'post-status' ], showBlockBreadcrumbs: true, showListViewByDefault: false, + enableChoosePatternModal: true, } ); if ( window.__experimentalMediaProcessing ) { diff --git a/packages/edit-site/src/posts.js b/packages/edit-site/src/posts.js index 9e2582ac23328..78d823b56c4c1 100644 --- a/packages/edit-site/src/posts.js +++ b/packages/edit-site/src/posts.js @@ -72,6 +72,7 @@ export function initializePostsDashboard( id, settings ) { openPanels: [ 'post-status' ], showBlockBreadcrumbs: true, showListViewByDefault: false, + enableChoosePatternModal: true, } ); dispatch( editSiteStore ).updateSettings( settings ); diff --git a/packages/editor/src/components/preferences-modal/index.js b/packages/editor/src/components/preferences-modal/index.js index e7cc637dd0aed..a8cfd8245522c 100644 --- a/packages/editor/src/components/preferences-modal/index.js +++ b/packages/editor/src/components/preferences-modal/index.js @@ -26,6 +26,7 @@ import PageAttributesCheck from '../page-attributes/check'; import PostTypeSupportCheck from '../post-type-support-check'; import { store as editorStore } from '../../store'; import { unlock } from '../../lock-unlock'; +import { useStartPatterns } from '../start-page-options'; const { PreferencesModal, @@ -57,6 +58,7 @@ export default function EditorPreferencesModal( { extraSections = {} } ) { const { setIsListViewOpened, setIsInserterOpened } = useDispatch( editorStore ); const { set: setPreference } = useDispatch( preferencesStore ); + const hasStarterPatterns = !! useStartPatterns().length; const sections = useMemo( () => @@ -97,6 +99,16 @@ export default function EditorPreferencesModal( { extraSections = {} } ) { 'Allow right-click contextual menus' ) } /> + { hasStarterPatterns && ( + + ) } { - // filter patterns without postTypes declared if the current postType is page - // or patterns that declare the current postType in its post type array. + 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 ) || @@ -110,30 +118,24 @@ function StartPageOptionsModal( { onClose } ) { export default function StartPageOptions() { const [ isClosed, setIsClosed ] = useState( false ); - const { shouldEnableModal, postType, postId } = useSelect( ( select ) => { - const { - isEditedPostDirty, - isEditedPostEmpty, - getCurrentPostType, - getCurrentPostId, - } = select( editorStore ); - const _postType = getCurrentPostType(); - - return { - shouldEnableModal: - ! isEditedPostDirty() && - isEditedPostEmpty() && - TEMPLATE_POST_TYPE !== _postType, - postType: _postType, - postId: getCurrentPostId(), - }; + const shouldEnableModal = useSelect( ( select ) => { + const { isEditedPostDirty, isEditedPostEmpty, 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() + ); }, [] ); - useEffect( () => { - // Should reset the modal state when navigating to a new page/post. - setIsClosed( false ); - }, [ postType, postId ] ); - if ( ! shouldEnableModal || isClosed ) { return null; }