Skip to content

Commit

Permalink
Fix padding appender hook (#66143)
Browse files Browse the repository at this point in the history
* Fix focus inserted default block

* Support clicks below the padding in the iframed editor

Co-authored-by: t-hamano <[email protected]>
Co-authored-by: kevin940726 <[email protected]>
Co-authored-by: afercia <[email protected]>
  • Loading branch information
4 people authored Oct 16, 2024
1 parent e015455 commit 0b67b89
Showing 1 changed file with 13 additions and 4 deletions.
17 changes: 13 additions & 4 deletions packages/edit-post/src/components/layout/use-padding-appender.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,13 @@ export function usePaddingAppender( enabled ) {
const effect = useRefEffect(
( node ) => {
function onMouseDown( event ) {
if ( event.target !== node ) {
if (
event.target !== node &&
// Tests for the parent element because in the iframed editor if the click is
// below the padding the target will be the parent element (html) and should
// still be treated as intent to append.
event.target !== node.parentElement
) {
return;
}

Expand All @@ -31,7 +37,7 @@ export function usePaddingAppender( enabled ) {
return;
}

event.stopPropagation();
event.preventDefault();

const blockOrder = registry
.select( blockEditorStore )
Expand All @@ -50,9 +56,12 @@ export function usePaddingAppender( enabled ) {
insertDefaultBlock();
}
}
node.addEventListener( 'mousedown', onMouseDown );
const { ownerDocument } = node;
// Adds the listener on the document so that in the iframed editor clicks below the
// padding can be handled as they too should be treated as intent to append.
ownerDocument.addEventListener( 'mousedown', onMouseDown );
return () => {
node.removeEventListener( 'mousedown', onMouseDown );
ownerDocument.removeEventListener( 'mousedown', onMouseDown );
};
},
[ registry ]
Expand Down

0 comments on commit 0b67b89

Please sign in to comment.