-
Notifications
You must be signed in to change notification settings - Fork 4.1k
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
Flash editable block outlines instead of always showing them #58159
Changes from all commits
c0d4e8b
b1808a5
1ec65f8
bf845d3
fc1f606
9eca467
d7671f0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { useRefEffect } from '@wordpress/compose'; | ||
import { useSelect } from '@wordpress/data'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import { store as blockEditorStore } from '../../store'; | ||
import { unlock } from '../../lock-unlock'; | ||
|
||
export function useFlashEditableBlocks( { | ||
clientId = '', | ||
isEnabled = true, | ||
} = {} ) { | ||
const { getEnabledClientIdsTree } = unlock( useSelect( blockEditorStore ) ); | ||
|
||
return useRefEffect( | ||
( element ) => { | ||
if ( ! isEnabled ) { | ||
return; | ||
} | ||
|
||
const flashEditableBlocks = () => { | ||
getEnabledClientIdsTree( clientId ).forEach( | ||
( { clientId: id } ) => { | ||
const block = element.querySelector( | ||
`[data-block="${ id }"]` | ||
); | ||
if ( ! block ) { | ||
return; | ||
} | ||
block.classList.remove( 'has-editable-outline' ); | ||
// Force reflow to trigger the animation. | ||
// eslint-disable-next-line no-unused-expressions | ||
block.offsetWidth; | ||
block.classList.add( 'has-editable-outline' ); | ||
} | ||
); | ||
}; | ||
|
||
const handleClick = ( event ) => { | ||
const shouldFlash = | ||
event.target === element || | ||
event.target.classList.contains( 'is-root-container' ); | ||
if ( ! shouldFlash ) { | ||
return; | ||
} | ||
if ( event.defaultPrevented ) { | ||
return; | ||
} | ||
event.preventDefault(); | ||
flashEditableBlocks(); | ||
}; | ||
|
||
element.addEventListener( 'click', handleClick ); | ||
return () => element.removeEventListener( 'click', handleClick ); | ||
}, | ||
[ isEnabled ] | ||
); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -27,27 +27,18 @@ import { store as editorStore } from '../../store'; | |
* editor iframe canvas. | ||
*/ | ||
export default function EditTemplateBlocksNotification( { contentRef } ) { | ||
const { renderingMode, getPostLinkProps, templateId } = useSelect( | ||
( select ) => { | ||
const { | ||
getRenderingMode, | ||
getEditorSettings, | ||
getCurrentTemplateId, | ||
} = select( editorStore ); | ||
return { | ||
renderingMode: getRenderingMode(), | ||
getPostLinkProps: getEditorSettings().getPostLinkProps, | ||
templateId: getCurrentTemplateId(), | ||
}; | ||
}, | ||
[] | ||
); | ||
const editTemplate = getPostLinkProps | ||
? getPostLinkProps( { | ||
postId: templateId, | ||
postType: 'wp_template', | ||
} ) | ||
: {}; | ||
const editTemplate = useSelect( ( select ) => { | ||
const { getEditorSettings, getCurrentTemplateId } = | ||
select( editorStore ); | ||
const { getPostLinkProps } = getEditorSettings(); | ||
return getPostLinkProps | ||
? getPostLinkProps( { | ||
postId: getCurrentTemplateId(), | ||
postType: 'wp_template', | ||
} ) | ||
: {}; | ||
}, [] ); | ||
|
||
const { getNotices } = useSelect( noticesStore ); | ||
|
||
const { createInfoNotice, removeNotice } = useDispatch( noticesStore ); | ||
|
@@ -58,18 +49,17 @@ export default function EditTemplateBlocksNotification( { contentRef } ) { | |
|
||
useEffect( () => { | ||
const handleClick = async ( event ) => { | ||
if ( renderingMode !== 'template-locked' ) { | ||
return; | ||
} | ||
Comment on lines
-61
to
-63
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just double-checking that I'm following correctly: the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah you have it right. It's not necessary for this PR at all—leftover code from some explorations I was doing that went nowhere. But I kept these changes as I think it's much nicer to simply not mount the component at all if it's not needed rather than unnecessarily mount the component and attach all these unnecessary event listeners that run unnecessary code on every click. |
||
if ( ! event.target.classList.contains( 'is-root-container' ) ) { | ||
return; | ||
} | ||
|
||
const isNoticeAlreadyShowing = getNotices().some( | ||
( notice ) => notice.id === lastNoticeId.current | ||
); | ||
if ( isNoticeAlreadyShowing ) { | ||
return; | ||
} | ||
|
||
const { notice } = await createInfoNotice( | ||
__( 'Edit your template to edit this block.' ), | ||
{ | ||
|
@@ -87,9 +77,6 @@ export default function EditTemplateBlocksNotification( { contentRef } ) { | |
}; | ||
|
||
const handleDblClick = ( event ) => { | ||
if ( renderingMode !== 'template-locked' ) { | ||
return; | ||
} | ||
if ( ! event.target.classList.contains( 'is-root-container' ) ) { | ||
return; | ||
} | ||
|
@@ -106,7 +93,7 @@ export default function EditTemplateBlocksNotification( { contentRef } ) { | |
canvas?.removeEventListener( 'click', handleClick ); | ||
canvas?.removeEventListener( 'dblclick', handleDblClick ); | ||
}; | ||
}, [ lastNoticeId, renderingMode, contentRef.current ] ); | ||
}, [ lastNoticeId, contentRef.current ] ); | ||
|
||
return ( | ||
<ConfirmDialog | ||
|
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.
Oh, clever!