-
Notifications
You must be signed in to change notification settings - Fork 4.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Improve guidance to editing template when focused on editing a page #51366
Merged
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
045df9e
Improve guidance to editing template when focused on editing a page
noisysocks 11fb888
Don't flash content blocks when double clicking
noisysocks 2f9cf3c
Use flashBlock()
noisysocks b5490df
Remove content block flashing for now
noisysocks 0ad5f11
Show edit template notification if not already showing one
noisysocks 4d1cda0
Oops, this doesn't belong here
noisysocks 9de65d5
Restore packages/block-editor from trunk
noisysocks 1f946eb
Optimise if()
noisysocks 734d914
Add periods to notifications
noisysocks 7f633ca
Merge remote-tracking branch 'origin/trunk' into try/improve-page-con…
noisysocks f7ff0dc
Dismiss notification when opening dialog
noisysocks File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
65 changes: 65 additions & 0 deletions
65
packages/edit-site/src/components/page-content-focus-manager/back-to-page-notification.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,65 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { useSelect, useDispatch } from '@wordpress/data'; | ||
import { useEffect, useRef } from '@wordpress/element'; | ||
import { store as noticesStore } from '@wordpress/notices'; | ||
import { __ } from '@wordpress/i18n'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import { store as editSiteStore } from '../../store'; | ||
|
||
/** | ||
* Component that displays a 'You are editing a template' notification when the | ||
* user switches from focusing on editing page content to editing a template. | ||
*/ | ||
export default function BackToPageNotification() { | ||
useBackToPageNotification(); | ||
return null; | ||
} | ||
|
||
/** | ||
* Hook that displays a 'You are editing a template' notification when the user | ||
* switches from focusing on editing page content to editing a template. | ||
*/ | ||
export function useBackToPageNotification() { | ||
const hasPageContentFocus = useSelect( | ||
( select ) => select( editSiteStore ).hasPageContentFocus(), | ||
[] | ||
); | ||
|
||
const alreadySeen = useRef( false ); | ||
const prevHasPageContentFocus = useRef( false ); | ||
|
||
const { createInfoNotice } = useDispatch( noticesStore ); | ||
const { setHasPageContentFocus } = useDispatch( editSiteStore ); | ||
|
||
useEffect( () => { | ||
if ( | ||
! alreadySeen.current && | ||
prevHasPageContentFocus.current && | ||
! hasPageContentFocus | ||
) { | ||
createInfoNotice( __( 'You are editing a template.' ), { | ||
isDismissible: true, | ||
type: 'snackbar', | ||
actions: [ | ||
{ | ||
label: __( 'Back to page' ), | ||
onClick: () => setHasPageContentFocus( true ), | ||
}, | ||
], | ||
} ); | ||
alreadySeen.current = true; | ||
} | ||
prevHasPageContentFocus.current = hasPageContentFocus; | ||
}, [ | ||
alreadySeen, | ||
prevHasPageContentFocus, | ||
hasPageContentFocus, | ||
createInfoNotice, | ||
setHasPageContentFocus, | ||
] ); | ||
} |
File renamed without changes.
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
108 changes: 108 additions & 0 deletions
108
packages/edit-site/src/components/page-content-focus-manager/edit-template-notification.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,108 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { useSelect, useDispatch } from '@wordpress/data'; | ||
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 | ||
*/ | ||
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. | ||
* - 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<HTMLElement>} props.contentRef Ref to the block | ||
* editor iframe canvas. | ||
*/ | ||
noisysocks marked this conversation as resolved.
Show resolved
Hide resolved
|
||
export default function EditTemplateNotification( { contentRef } ) { | ||
const hasPageContentFocus = useSelect( | ||
( select ) => select( editSiteStore ).hasPageContentFocus(), | ||
[] | ||
); | ||
const { getNotices } = useSelect( noticesStore ); | ||
|
||
const { createInfoNotice, removeNotice } = useDispatch( noticesStore ); | ||
const { setHasPageContentFocus } = useDispatch( editSiteStore ); | ||
|
||
const [ isDialogOpen, setIsDialogOpen ] = useState( false ); | ||
|
||
const lastNoticeId = useRef( 0 ); | ||
|
||
useEffect( () => { | ||
const handleClick = async ( event ) => { | ||
if ( ! hasPageContentFocus ) { | ||
return; | ||
} | ||
if ( ! event.target.classList.contains( 'is-root-container' ) ) { | ||
return; | ||
} | ||
const isNoticeAlreadyShowing = getNotices().some( | ||
noisysocks marked this conversation as resolved.
Show resolved
Hide resolved
|
||
( notice ) => notice.id === lastNoticeId.current | ||
); | ||
if ( isNoticeAlreadyShowing ) { | ||
return; | ||
} | ||
const { notice } = await createInfoNotice( | ||
__( 'Edit your template to edit this block.' ), | ||
{ | ||
isDismissible: true, | ||
type: 'snackbar', | ||
actions: [ | ||
{ | ||
label: __( 'Edit template' ), | ||
onClick: () => setHasPageContentFocus( false ), | ||
}, | ||
], | ||
} | ||
); | ||
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 ); | ||
canvas?.addEventListener( 'dblclick', handleDblClick ); | ||
return () => { | ||
canvas?.removeEventListener( 'click', handleClick ); | ||
canvas?.removeEventListener( 'dblclick', handleDblClick ); | ||
}; | ||
}, [ lastNoticeId, hasPageContentFocus, contentRef.current ] ); | ||
|
||
return ( | ||
<ConfirmDialog | ||
isOpen={ isDialogOpen } | ||
confirmButtonText={ __( 'Edit template' ) } | ||
onConfirm={ () => { | ||
setIsDialogOpen( false ); | ||
setHasPageContentFocus( false ); | ||
} } | ||
onCancel={ () => setIsDialogOpen( false ) } | ||
> | ||
{ __( 'Edit your template to edit this block.' ) } | ||
</ConfirmDialog> | ||
); | ||
} |
26 changes: 26 additions & 0 deletions
26
packages/edit-site/src/components/page-content-focus-manager/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,26 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { useSelect } from '@wordpress/data'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import { store as editSiteStore } from '../../store'; | ||
import DisableNonPageContentBlocks from './disable-non-page-content-blocks'; | ||
import EditTemplateNotification from './edit-template-notification'; | ||
import BackToPageNotification from './back-to-page-notification'; | ||
|
||
export default function PageContentFocusManager( { contentRef } ) { | ||
const hasPageContentFocus = useSelect( | ||
( select ) => select( editSiteStore ).hasPageContentFocus(), | ||
[] | ||
); | ||
return ( | ||
<> | ||
{ hasPageContentFocus && <DisableNonPageContentBlocks /> } | ||
<EditTemplateNotification contentRef={ contentRef } /> | ||
<BackToPageNotification /> | ||
</> | ||
); | ||
} |
2 changes: 0 additions & 2 deletions
2
packages/edit-site/src/components/page-content-focus/index.js
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: There's no need to add mutable values like refs as dependencies. When you provide them,
alreadySeen.current
ESLint shows a warning.