Skip to content

Commit

Permalink
Fix fileName not respect when the image is drag&dropped (#6960)
Browse files Browse the repository at this point in the history
Contrary to all the other uploads we have, when uploading an image to the editor via drag&drop without an image block previously created the file name of the image we upload is not respect.

The fileName is now passed from the transform to the region where we upload via the al attribute. using the alt attribute seems the best approach because using the URL attribute is not possible. Url is being used to store the blob and if we change like append '?fileName=' the browsers start ignoring the blob URL. Given that we pass the file via URL attribute from the transform to the upload code passing the fileName in the alt attribute seems the most logical way.
  • Loading branch information
jorgefilipecosta committed May 29, 2018
1 parent 400f8fc commit 8e7682b
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 8 deletions.
8 changes: 5 additions & 3 deletions core-blocks/image/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,19 +73,21 @@ class ImageEdit extends Component {

componentDidMount() {
const { attributes, setAttributes } = this.props;
const { id, url = '' } = attributes;
const { id, url = '', alt: fileName } = attributes;

if ( ! id && url.indexOf( 'blob:' ) === 0 ) {
getBlobByURL( url )
.then(
( file ) =>
( file ) => {
file.name = fileName;
editorMediaUpload( {
filesList: [ file ],
onFileChange: ( [ image ] ) => {
setAttributes( { ...image } );
},
allowedType: 'image',
} )
} );
}
);
}
}
Expand Down
1 change: 1 addition & 0 deletions core-blocks/image/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ export const settings = {
// It's already done as part of the `componentDidMount`
// int the image block
const block = createBlock( 'core/image', {
alt: file.name,
url: window.URL.createObjectURL( file ),
} );

Expand Down
8 changes: 3 additions & 5 deletions utils/mediaupload.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,14 +53,12 @@ export function mediaUpload( {
return createMediaFromFile( mediaFile, additionalData ).then(
( savedMedia ) => {
const mediaObject = {
alt: savedMedia.alt_text,
caption: get( savedMedia, [ 'caption', 'raw' ], '' ),
id: savedMedia.id,
url: savedMedia.source_url,
link: savedMedia.link,
url: savedMedia.source_url,
};
const caption = get( savedMedia, [ 'caption', 'raw' ] );
if ( caption ) {
mediaObject.caption = [ caption ];
}
setAndUpdateFiles( idx, mediaObject );
},
() => {
Expand Down

0 comments on commit 8e7682b

Please sign in to comment.