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

Paste: reuse file transforms for file pasting #45891

Merged
merged 5 commits into from
Dec 15, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 53 additions & 7 deletions packages/block-editor/src/components/copy-handler/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import {
pasteHandler,
store as blocksStore,
createBlock,
findTransform,
getBlockTransforms,
} from '@wordpress/blocks';
import {
documentHasSelection,
Expand Down Expand Up @@ -84,13 +86,15 @@ export function useClipboardHandler() {
__unstableIsSelectionCollapsed,
__unstableIsSelectionMergeable,
__unstableGetSelectedBlocksWithPartialSelection,
canInsertBlockType,
} = useSelect( blockEditorStore );
const {
flashBlock,
removeBlocks,
replaceBlocks,
__unstableDeleteSelection,
__unstableExpandSelection,
insertBlocks,
} = useDispatch( blockEditorStore );
const notifyCopy = useNotifyCopy();

Expand Down Expand Up @@ -201,13 +205,55 @@ export function useClipboardHandler() {
__experimentalCanUserUseUnfilteredHTML:
canUserUseUnfilteredHTML,
} = getSettings();
const { plainText, html } = getPasteEventData( event );
const blocks = pasteHandler( {
HTML: html,
plainText,
mode: 'BLOCKS',
canUserUseUnfilteredHTML,
} );
const { plainText, html, files } = getPasteEventData( event );
let blocks = [];

if ( files.length ) {
const fromTransforms = getBlockTransforms( 'from' );
blocks = files
.reduce( ( accumulator, file ) => {
const transformation = findTransform(
fromTransforms,
( transform ) =>
transform.type === 'files' &&
transform.isMatch( [ file ] )
);
if ( transformation ) {
accumulator.push(
transformation.transform( [ file ] )
);
}
return accumulator;
}, [] )
.flat();
} else {
blocks = pasteHandler( {
HTML: html,
plainText,
mode: 'BLOCKS',
canUserUseUnfilteredHTML,
} );
}

if ( selectedBlockClientIds.length === 1 ) {
const [ selectedBlockClientId ] = selectedBlockClientIds;

if (
blocks.every( ( block ) =>
canInsertBlockType(
block.name,
selectedBlockClientId
)
)
) {
insertBlocks(
blocks,
undefined,
selectedBlockClientId
);
return;
}
}

replaceBlocks(
selectedBlockClientIds,
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@
import { useRef } from '@wordpress/element';
import { useRefEffect } from '@wordpress/compose';
import { getFilesFromDataTransfer } from '@wordpress/dom';
import { pasteHandler } from '@wordpress/blocks';
import {
pasteHandler,
findTransform,
getBlockTransforms,
} from '@wordpress/blocks';
import {
isEmpty,
insert,
Expand All @@ -17,7 +21,6 @@ import { isURL } from '@wordpress/url';
/**
* Internal dependencies
*/
import { filePasteHandler } from './file-paste-handler';
import { addActiveFormats, isShortcode } from './utils';
import { splitValue } from './split-value';
import { shouldDismissPastedFiles } from '../../utils/pasting';
Expand Down Expand Up @@ -155,6 +158,12 @@ export function usePasteHandler( props ) {
return;
}

if ( files?.length ) {
// Allows us to ask for this information when we get a report.
// eslint-disable-next-line no-console
window.console.log( 'Received items:\n\n', files );
}

// Process any attached files, unless we infer that the files in
// question are redundant "screenshots" of the actual HTML payload,
// as created by certain office-type programs.
Expand All @@ -164,23 +173,33 @@ export function usePasteHandler( props ) {
files?.length &&
! shouldDismissPastedFiles( files, html, plainText )
) {
const content = pasteHandler( {
HTML: filePasteHandler( files ),
mode: 'BLOCKS',
tagName,
preserveWhiteSpace,
} );

// Allows us to ask for this information when we get a report.
// eslint-disable-next-line no-console
window.console.log( 'Received items:\n\n', files );
const fromTransforms = getBlockTransforms( 'from' );
const blocks = files
.reduce( ( accumulator, file ) => {
const transformation = findTransform(
fromTransforms,
( transform ) =>
transform.type === 'files' &&
transform.isMatch( [ file ] )
);
if ( transformation ) {
accumulator.push(
transformation.transform( [ file ] )
);
}
return accumulator;
}, [] )
.flat();
if ( ! blocks.length ) {
return;
}

if ( onReplace && isEmpty( value ) ) {
onReplace( content );
onReplace( blocks );
} else {
splitValue( {
value,
pastedBlocks: content,
pastedBlocks: blocks,
onReplace,
onSplit,
onSplitMiddle,
Expand Down
12 changes: 3 additions & 9 deletions packages/block-editor/src/utils/pasting.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
/**
* WordPress dependencies
*/
import { createBlobURL } from '@wordpress/blob';
import { getFilesFromDataTransfer } from '@wordpress/dom';

export function getPasteEventData( { clipboardData } ) {
Expand All @@ -25,21 +24,16 @@ export function getPasteEventData( { clipboardData } ) {
}
}

const files = getFilesFromDataTransfer( clipboardData ).filter(
( { type } ) => /^image\/(?:jpe?g|png|gif|webp)$/.test( type )
);
const files = getFilesFromDataTransfer( clipboardData );

if (
files.length &&
! shouldDismissPastedFiles( files, html, plainText )
) {
html = files
.map( ( file ) => `<img src="${ createBlobURL( file ) }">` )
.join( '' );
plainText = '';
return { files };
}

return { html, plainText };
return { html, plainText, files: [] };
}

/**
Expand Down
18 changes: 16 additions & 2 deletions packages/block-library/src/gallery/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import {
MediaPlaceholder,
InspectorControls,
useBlockProps,
useInnerBlocksProps,
BlockControls,
MediaReplaceFlow,
} from '@wordpress/block-editor';
Expand Down Expand Up @@ -64,6 +65,7 @@ const linkOptions = [
},
];
const ALLOWED_MEDIA_TYPES = [ 'image' ];
const allowedBlocks = [ 'core/image' ];

const PLACEHOLDER_TEXT = Platform.isNative
? __( 'ADD MEDIA' )
Expand Down Expand Up @@ -484,8 +486,20 @@ function GalleryEdit( props ) {
className: classnames( className, 'has-nested-images' ),
} );

const innerBlocksProps = useInnerBlocksProps( blockProps, {
allowedBlocks,
orientation: 'horizontal',
renderAppender: false,
__experimentalLayout: { type: 'default', alignments: [] },
} );

if ( ! hasImages ) {
return <View { ...blockProps }>{ mediaPlaceholder }</View>;
return (
<View { ...innerBlocksProps }>
{ innerBlocksProps.children }
{ mediaPlaceholder }
</View>
);
}

const hasLinkTo = linkTo && linkTo !== 'none';
Expand Down Expand Up @@ -580,7 +594,7 @@ function GalleryEdit( props ) {
? mediaPlaceholder
: undefined
}
blockProps={ blockProps }
blockProps={ innerBlocksProps }
insertBlocksAfter={ insertBlocksAfter }
/>
</>
Expand Down
16 changes: 3 additions & 13 deletions packages/block-library/src/gallery/gallery.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,13 @@ import classnames from 'classnames';
*/
import {
RichText,
useInnerBlocksProps,
__experimentalGetElementClassName,
} from '@wordpress/block-editor';
import { VisuallyHidden } from '@wordpress/components';
import { __ } from '@wordpress/i18n';
import { createBlock, getDefaultBlockName } from '@wordpress/blocks';
import { View } from '@wordpress/primitives';

const allowedBlocks = [ 'core/image' ];

export const Gallery = ( props ) => {
const {
attributes,
Expand All @@ -31,16 +28,9 @@ export const Gallery = ( props ) => {

const { align, columns, caption, imageCrop } = attributes;

const { children, ...innerBlocksProps } = useInnerBlocksProps( blockProps, {
allowedBlocks,
orientation: 'horizontal',
renderAppender: false,
__experimentalLayout: { type: 'default', alignments: [] },
} );

return (
<figure
{ ...innerBlocksProps }
{ ...blockProps }
className={ classnames(
blockProps.className,
layoutClassNames,
Expand All @@ -53,8 +43,8 @@ export const Gallery = ( props ) => {
}
) }
>
{ children }
{ isSelected && ! children && (
{ blockProps.children }
{ isSelected && ! blockProps.children && (
<View className="blocks-gallery-media-placeholder-wrapper">
{ mediaPlaceholder }
</View>
Expand Down