diff --git a/packages/block-editor/src/components/rich-text/index.native.js b/packages/block-editor/src/components/rich-text/index.native.js index a4761ca33f1214..a6d2ef19a86a56 100644 --- a/packages/block-editor/src/components/rich-text/index.native.js +++ b/packages/block-editor/src/components/rich-text/index.native.js @@ -302,11 +302,35 @@ export class RichText extends Component { const isPasted = true; const { onSplit } = this.props; - const { pastedText, pastedHtml } = event.nativeEvent; + const { pastedText, pastedHtml, files } = event.nativeEvent; const currentRecord = this.createRecord( event.nativeEvent ); event.preventDefault(); + // Only process file if no HTML is present. + // Note: a pasted file may have the URL as plain text. + if ( files && files.length > 0 ) { + const uploadId = Number.MAX_SAFE_INTEGER; + let html = ''; + files.forEach( ( file ) => { + html += ``; + } ); + const content = pasteHandler( { + HTML: html, + mode: 'BLOCKS', + tagName: this.props.tagName, + } ); + const shouldReplace = this.props.onReplace && this.isEmpty(); + + if ( shouldReplace ) { + this.props.onReplace( content ); + } else { + this.splitContent( currentRecord, content, isPasted ); + } + + return; + } + // There is a selection, check if a URL is pasted. if ( ! isCollapsed( currentRecord ) ) { const trimmedText = ( pastedHtml || pastedText ).replace( /<[^>]+>/g, '' ) diff --git a/packages/block-library/src/image/edit.native.js b/packages/block-library/src/image/edit.native.js index ebe6388c2df103..af26201f1d35de 100644 --- a/packages/block-library/src/image/edit.native.js +++ b/packages/block-library/src/image/edit.native.js @@ -8,6 +8,7 @@ import { requestMediaPickFromMediaLibrary, requestMediaPickFromDeviceLibrary, requestMediaPickFromDeviceCamera, + requestMediaImport, mediaUploadSync, requestImageFailedRetryDialog, requestImageUploadCancelDialog, @@ -77,9 +78,16 @@ class ImageEdit extends React.Component { componentDidMount() { this.addMediaUploadListener(); - const { attributes } = this.props; + const { attributes, setAttributes } = this.props; if ( attributes.id && ! isURL( attributes.url ) ) { + if ( attributes.url.indexOf( 'file:' ) === 0 ) { + requestMediaImport( attributes.url, ( mediaId, mediaUri ) => { + if ( mediaUri ) { + setAttributes( { url: mediaUri, id: mediaId } ); + } + } ); + } mediaUploadSync(); } } diff --git a/packages/blocks/src/api/raw-handling/image-corrector.native.js b/packages/blocks/src/api/raw-handling/image-corrector.native.js new file mode 100644 index 00000000000000..d450ac7441730c --- /dev/null +++ b/packages/blocks/src/api/raw-handling/image-corrector.native.js @@ -0,0 +1,18 @@ + +/** + * This method check for copy pasted img elements to see if they don't have suspicious attributes. + * + * @param {Node} node The node to check. + * + * @return {void} +*/ +export default function( node ) { + if ( node.nodeName !== 'IMG' ) { + return; + } + + // Remove trackers and hardly visible images. + if ( node.height === 1 || node.width === 1 ) { + node.parentNode.removeChild( node ); + } +}