diff --git a/packages/edit-site/src/components/page-content-focus-manager/edit-template-dialog.js b/packages/edit-site/src/components/page-content-focus-manager/edit-template-dialog.js deleted file mode 100644 index 0e31aa549f1c9..0000000000000 --- a/packages/edit-site/src/components/page-content-focus-manager/edit-template-dialog.js +++ /dev/null @@ -1,52 +0,0 @@ -/** - * WordPress dependencies - */ -import { useDispatch } from '@wordpress/data'; -import { useEffect, useState } from '@wordpress/element'; -import { __ } from '@wordpress/i18n'; -import { __experimentalConfirmDialog as ConfirmDialog } from '@wordpress/components'; - -/** - * Internal dependencies - */ -import { store as editSiteStore } from '../../store'; - -/** - * Component that displays a 'Edit your template to edit this block' - * notification when the user is focusing on editing page content and clicks on - * a disabled template block. - * - * @param {Object} props - * @param {import('react').RefObject} props.contentRef Ref to the block - * editor iframe canvas. - */ -export default function EditTemplateDialog( { contentRef } ) { - const [ isOpen, setIsOpen ] = useState( false ); - - useEffect( () => { - const handleDblClick = ( event ) => { - if ( event.target.classList.contains( 'is-root-container' ) ) { - setIsOpen( true ); - } - }; - const canvas = contentRef.current; - canvas?.addEventListener( 'dblclick', handleDblClick ); - return () => canvas?.removeEventListener( 'dblclick', handleDblClick ); - }, [ contentRef.current ] ); - - const { setHasPageContentFocus } = useDispatch( editSiteStore ); - - return ( - { - setIsOpen( false ); - setHasPageContentFocus( false ); - } } - onCancel={ () => setIsOpen( false ) } - > - { __( 'Edit your template to edit this block' ) } - - ); -} diff --git a/packages/edit-site/src/components/page-content-focus-manager/edit-template-notification.js b/packages/edit-site/src/components/page-content-focus-manager/edit-template-notification.js index 4038428e9bf03..3518bc8c3c51d 100644 --- a/packages/edit-site/src/components/page-content-focus-manager/edit-template-notification.js +++ b/packages/edit-site/src/components/page-content-focus-manager/edit-template-notification.js @@ -2,9 +2,10 @@ * WordPress dependencies */ import { useSelect, useDispatch } from '@wordpress/data'; -import { useEffect, useRef } from '@wordpress/element'; +import { useEffect, useState, useRef } from '@wordpress/element'; import { store as noticesStore } from '@wordpress/notices'; import { __ } from '@wordpress/i18n'; +import { __experimentalConfirmDialog as ConfirmDialog } from '@wordpress/components'; /** * Internal dependencies @@ -12,37 +13,31 @@ import { __ } from '@wordpress/i18n'; import { store as editSiteStore } from '../../store'; /** - * Component that displays a 'Edit your template to edit this block' - * notification when the user is focusing on editing page content and clicks on - * a disabled template block. + * Component that: + * + * - Displays a 'Edit your template to edit this block' notification when the + * user is focusing on editing page content and clicks on a disabled template + * block. + * - Displays a 'Edit your template to edit this block' dialog when the user + * is focusing on editing page conetnt and double clicks on a disabled + * template block. * * @param {Object} props * @param {import('react').RefObject} props.contentRef Ref to the block * editor iframe canvas. */ export default function EditTemplateNotification( { contentRef } ) { - useEditTemplateNotification( contentRef ); - return null; -} - -/** - * Hook that displays a 'Edit your template to edit this block' notification - * when the user is focusing on editing page content and clicks on a disabled - * template block. - * - * @param {import('react').RefObject} contentRef Ref to the block - * editor iframe canvas. - */ -function useEditTemplateNotification( contentRef ) { const hasPageContentFocus = useSelect( ( select ) => select( editSiteStore ).hasPageContentFocus(), [] ); const { getNotices } = useSelect( noticesStore ); - const { createInfoNotice } = useDispatch( noticesStore ); + const { createInfoNotice, removeNotice } = useDispatch( noticesStore ); const { setHasPageContentFocus } = useDispatch( editSiteStore ); + const [ isDialogOpen, setIsDialogOpen ] = useState( false ); + const lastNoticeId = useRef( 0 ); useEffect( () => { @@ -74,8 +69,40 @@ function useEditTemplateNotification( contentRef ) { ); lastNoticeId.current = notice.id; }; + + const handleDblClick = ( event ) => { + if ( ! hasPageContentFocus ) { + return; + } + if ( ! event.target.classList.contains( 'is-root-container' ) ) { + return; + } + if ( lastNoticeId.current ) { + removeNotice( lastNoticeId.current ); + } + setIsDialogOpen( true ); + }; + const canvas = contentRef.current; canvas?.addEventListener( 'click', handleClick ); - return () => canvas?.removeEventListener( 'click', handleClick ); + canvas?.addEventListener( 'dblclick', handleDblClick ); + return () => { + canvas?.removeEventListener( 'click', handleClick ); + canvas?.removeEventListener( 'dblclick', handleDblClick ); + }; }, [ lastNoticeId, hasPageContentFocus, contentRef.current ] ); + + return ( + { + setIsDialogOpen( false ); + setHasPageContentFocus( false ); + } } + onCancel={ () => setIsDialogOpen( false ) } + > + { __( 'Edit your template to edit this block.' ) } + + ); } diff --git a/packages/edit-site/src/components/page-content-focus-manager/index.js b/packages/edit-site/src/components/page-content-focus-manager/index.js index 0767be05cc843..935ba1c96248c 100644 --- a/packages/edit-site/src/components/page-content-focus-manager/index.js +++ b/packages/edit-site/src/components/page-content-focus-manager/index.js @@ -8,7 +8,6 @@ import { useSelect } from '@wordpress/data'; */ import { store as editSiteStore } from '../../store'; import DisableNonPageContentBlocks from './disable-non-page-content-blocks'; -import EditTemplateDialog from './edit-template-dialog'; import EditTemplateNotification from './edit-template-notification'; import BackToPageNotification from './back-to-page-notification'; @@ -19,12 +18,7 @@ export default function PageContentFocusManager( { contentRef } ) { ); return ( <> - { hasPageContentFocus && ( - <> - - - - ) } + { hasPageContentFocus && }