Skip to content

Commit

Permalink
Allow media paste onto Media & Text, Cover, etc.
Browse files Browse the repository at this point in the history
  • Loading branch information
ellatrix committed Dec 10, 2024
1 parent 24c0076 commit 77d7e78
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
getBlockTransforms,
hasBlockSupport,
switchToBlockType,
createBlock,
} from '@wordpress/blocks';
import {
documentHasSelection,
Expand Down Expand Up @@ -42,6 +43,7 @@ export default function useClipboardHandler() {
flashBlock,
removeBlocks,
replaceBlocks,
updateBlockAttributes,
__unstableDeleteSelection,
__unstableExpandSelection,
__unstableSplitSelection,
Expand Down Expand Up @@ -165,6 +167,40 @@ export default function useClipboardHandler() {
return accumulator;
}, [] )
.flat();

if (
selectedBlockClientIds.length === 1 &&
blocks.length === 1
) {
const selectedName = getBlockName(
selectedBlockClientIds[ 0 ]
);
// Attempt to transform to the selected block type.
const transformed = switchToBlockType(
blocks[ 0 ],
selectedName
);
if ( transformed?.length === 1 ) {
const block = transformed[ 0 ];
const emptyBlock = createBlock( selectedName );
const updates = {};
for ( const attributeName in block.attributes ) {
if (
block.attributes[ attributeName ] !==
emptyBlock.attributes[ attributeName ]
) {
updates[ attributeName ] =
block.attributes[ attributeName ];
}
}
event.preventDefault();
updateBlockAttributes(
selectedBlockClientIds[ 0 ],
updates
);
return;
}
}
} else {
blocks = pasteHandler( {
HTML: html,
Expand Down
13 changes: 11 additions & 2 deletions packages/block-library/src/cover/transforms.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,21 @@ const transforms = {
{
type: 'block',
blocks: [ 'core/image' ],
transform: ( { caption, url, alt, align, id, anchor, style } ) =>
transform: ( {
caption,
url,
blob,
alt,
align,
id,
anchor,
style,
} ) =>
createBlock(
'core/cover',
{
dimRatio: 50,
url,
url: url ?? blob,
alt,
align,
id,
Expand Down
4 changes: 2 additions & 2 deletions packages/block-library/src/media-text/transforms.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ const transforms = {
{
type: 'block',
blocks: [ 'core/image' ],
transform: ( { alt, url, id, anchor } ) =>
transform: ( { alt, url, blob, id, anchor } ) =>
createBlock( 'core/media-text', {
mediaAlt: alt,
mediaId: id,
mediaUrl: url,
mediaUrl: url ?? blob,
mediaType: 'image',
anchor,
} ),
Expand Down

0 comments on commit 77d7e78

Please sign in to comment.