Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
event.dataTransfer ||
this.editor.getDoc().dataTransfer ||
// Removes the need for further `dataTransfer` checks.
{ getData: () => '' };
const { items = [], files = [], types = [] } = dataTransfer;
const item = find([ ...items, ...files ], ({ type }) => /^image\/(?:jpe?g|png|gif)$/.test(type));
const plainText = dataTransfer.getData('text/plain');
const HTML = dataTransfer.getData('text/html');
// Only process file if no HTML is present.
// Note: a pasted file may have the URL as plain text.
if (item && ! HTML) {
const file = item.getAsFile ? item.getAsFile() : item;
const content = rawHandler({
HTML: `<img src="${createBlobURL(file)}">`,
mode: 'BLOCKS',
tagName: this.props.tagName,
});
const shouldReplace = this.props.onReplace && this.isEmpty();
// Allows us to ask for this information when we get a report.
window.console.log('Received item:\n\n', file);
if (shouldReplace) {
// Necessary to allow the paste bin to be removed without errors.
this.props.setTimeout(() => this.props.onReplace(content));
}
else if (this.props.onSplit) {
// Necessary to get the right range.
// Also done in the TinyMCE paste plugin.
this.props.setTimeout(() => this.splitContent(content));
event.dataTransfer ||
this.editor.getDoc().dataTransfer ||
// Removes the need for further `dataTransfer` checks.
{ getData: () => '' };
const { items = [], files = [], types = [] } = dataTransfer;
const item = find( [ ...items, ...files ], ( { type } ) => /^image\/(?:jpe?g|png|gif)$/.test( type ) );
const plainText = dataTransfer.getData( 'text/plain' );
const HTML = dataTransfer.getData( 'text/html' );
// Only process file if no HTML is present.
// Note: a pasted file may have the URL as plain text.
if ( item && ! HTML ) {
const file = item.getAsFile ? item.getAsFile() : item;
const content = rawHandler( {
HTML: `<img src="${ createBlobURL( file ) }">`,
mode: 'BLOCKS',
tagName: this.props.tagName,
} );
const shouldReplace = this.props.onReplace && this.isEmpty();
// Allows us to ask for this information when we get a report.
window.console.log( 'Received item:\n\n', file );
if ( shouldReplace ) {
// Necessary to allow the paste bin to be removed without errors.
this.props.setTimeout( () => this.props.onReplace( content ) );
} else if ( this.props.onSplit ) {
// Necessary to get the right range.
// Also done in the TinyMCE paste plugin.
this.props.setTimeout( () => this.splitContent( content ) );
}
event.dataTransfer ||
this.editor.getDoc().dataTransfer ||
// Removes the need for further `dataTransfer` checks.
{ getData: () => '' };
const { items = [], files = [], types = [] } = dataTransfer;
const item = find( [ ...items, ...files ], ( { type } ) => /^image\/(?:jpe?g|png|gif)$/.test( type ) );
const plainText = dataTransfer.getData( 'text/plain' );
const HTML = dataTransfer.getData( 'text/html' );
// Only process file if no HTML is present.
// Note: a pasted file may have the URL as plain text.
if ( item && ! HTML ) {
const file = item.getAsFile ? item.getAsFile() : item;
const content = rawHandler( {
HTML: `<img src="${ createBlobURL( file ) }">`,
mode: 'BLOCKS',
tagName: this.props.tagName,
} );
const shouldReplace = this.props.onReplace && this.isEmpty();
// Allows us to ask for this information when we get a report.
window.console.log( 'Received item:\n\n', file );
if ( shouldReplace ) {
// Necessary to allow the paste bin to be removed without errors.
this.props.setTimeout( () => this.props.onReplace( content ) );
} else if ( this.props.onSplit ) {
// Necessary to get the right range.
// Also done in the TinyMCE paste plugin.
this.props.setTimeout( () => this.splitContent( content ) );
}
// Don't allow empty files to be uploaded.
if ( mediaFile.size <= 0 ) {
triggerError( {
code: 'EMPTY_FILE',
message: __( 'This file is empty.' ),
file: mediaFile,
} );
continue;
}
validFiles.push( mediaFile );
// Set temporary URL to create placeholder media file, this is replaced
// with final file from media gallery when upload is `done` below
filesSet.push( { url: createBlobURL( mediaFile ) } );
onFileChange( filesSet );
}
for ( let idx = 0; idx < validFiles.length; ++idx ) {
const mediaFile = validFiles[ idx ];
try {
const savedMedia = await createMediaFromFile( mediaFile, additionalData );
const mediaObject = {
...omit( savedMedia, [ 'alt_text', 'source_url' ] ),
alt: savedMedia.alt_text,
caption: get( savedMedia, [ 'caption', 'raw' ], '' ),
title: savedMedia.title.raw,
url: savedMedia.source_url,
};
setAndUpdateFiles( idx, mediaObject );
} catch ( error ) {
files.forEach( ( file ) => {
const blobURL = createBlobURL( file );
// File will be uploaded in componentDidMount()
blocks.push( createBlock( 'core/file', {
href: blobURL,
fileName: file.name,
textLinkHref: blobURL,
} ) );
} );
transform: ( files ) => {
const file = files[ 0 ];
const blobURL = createBlobURL( file );
// File will be uploaded in componentDidMount()
return createBlock( 'core/file', {
href: blobURL,
fileName: file.name,
textLinkHref: blobURL,
} );
},
},
transform( files ) {
const file = files[ 0 ];
// We don't need to upload the media directly here
// It's already done as part of the `componentDidMount`
// in the audio block
const block = createBlock( 'core/audio', {
src: createBlobURL( file ),
} );
return block;
},
},
decoded = atob( data );
} catch ( e ) {
node.src = '';
return;
}
const uint8Array = new Uint8Array( decoded.length );
for ( let i = 0; i < uint8Array.length; i++ ) {
uint8Array[ i ] = decoded.charCodeAt( i );
}
const name = type.replace( '/', '.' );
const file = new File( [ uint8Array ], name, { type } );
node.src = createBlobURL( file );
}
// Remove trackers and hardly visible images.
if ( node.height === 1 || node.width === 1 ) {
node.parentNode.removeChild( node );
}
}
files.forEach( file => {
const block = createBlock( 'core/video', {
src: createBlobURL( file ),
} );
mediaUpload( {
filesList: [ file ],
onFileChange: ( [ { id, url } ] ) => {
onChange( block.clientId, { id, src: url } );
},
allowedTypes: [ 'video' ],
} );
blocks.push( block );
} );
return blocks;
images: files.map( ( file ) => pickRelevantMediaFiles( {
url: createBlobURL( file ),
} ) ),
} );