Skip to content
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

Allow dropping an image on an empty paragraph block to create an image block #42722

Merged
merged 14 commits into from
Sep 26, 2022
Merged
43 changes: 29 additions & 14 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@
"@types/highlight-words-core": "1.2.1",
"@types/istanbul-lib-report": "3.0.0",
"@types/lodash": "4.14.172",
"@types/mime": "2.0.3",
"@types/npm-package-arg": "6.1.1",
"@types/prettier": "2.4.4",
"@types/qs": "6.9.7",
Expand Down
1 change: 1 addition & 0 deletions packages/block-editor/src/components/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,7 @@ export {
export { default as __experimentalBlockPatternsList } from './block-patterns-list';
export { default as __experimentalPublishDateTimePicker } from './publish-date-time-picker';
export { default as __experimentalInspectorPopoverHeader } from './inspector-popover-header';
export { default as __experimentalUseOnBlockDrop } from './use-on-block-drop';
Copy link
Member

@ellatrix ellatrix Sep 28, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we mark this unstable/internal? I don't think this is an API that we want to expose in this shape.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure. I don't mind changing it to __internal or any other naming. I only use __experimental here because that's a more established pattern.


/*
* State Related Components
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,20 @@ export function getNearestBlockIndex( elements, position, orientation ) {
return candidateIndex;
}

/**
* Determine if the element is an empty paragraph block.
*
* @param {?HTMLElement} element The element being tested.
* @return {boolean} True or False.
*/
function isEmptyParagraph( element ) {
return (
!! element &&
element.dataset.type === 'core/paragraph' &&
element.dataset.empty === 'true'
);
}

/**
* @typedef {Object} WPBlockDropZoneConfig
* @property {string} rootClientId The root client id for the block list.
Expand Down Expand Up @@ -130,7 +144,18 @@ export default function useBlockDropZone( {

setTargetBlockIndex( targetIndex === undefined ? 0 : targetIndex );

if ( targetIndex !== null ) {
if ( targetIndex !== undefined ) {
const nextBlock = blockElements[ targetIndex ];
const previousBlock = blockElements[ targetIndex - 1 ];

// Don't show the insertion point when it's near an empty paragraph block.
if (
isEmptyParagraph( nextBlock ) ||
isEmptyParagraph( previousBlock )
) {
return;
}

showInsertionPoint( targetRootClientId, targetIndex );
}
}, [] ),
Expand Down
Loading