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

Blocks: Showing a preview of the block immediately after drag and drop #4567

Merged
merged 2 commits into from
Jan 19, 2018
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
4 changes: 4 additions & 0 deletions blocks/library/gallery/editor.scss
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@
outline: 4px solid $blue-medium-500;
outline-offset: -4px;
}

&.is-transient img {
@include loading_fade;
}
}

.blocks-gallery-item__inline-menu {
Expand Down
1 change: 1 addition & 0 deletions blocks/library/gallery/gallery-image.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ class GalleryImage extends Component {

const className = classnames( {
'is-selected': isSelected,
'is-transient': 0 === url.indexOf( 'blob:' ),
} );

// Disable reason: Each block can be selected by clicking on it and we should keep the same saved markup
Expand Down
28 changes: 19 additions & 9 deletions blocks/library/gallery/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { filter, every } from 'lodash';
* WordPress dependencies
*/
import { __ } from '@wordpress/i18n';
import { createMediaFromFile } from '@wordpress/utils';
import { createMediaFromFile, preloadImage } from '@wordpress/utils';

/**
* Internal dependencies
Expand Down Expand Up @@ -115,14 +115,24 @@ registerBlockType( 'core/gallery', {
isMatch( files ) {
return files.length !== 1 && every( files, ( file ) => file.type.indexOf( 'image/' ) === 0 );
},
transform( files ) {
return Promise.all( files.map( ( file ) => createMediaFromFile( file ) ) )
.then( ( medias ) => createBlock( 'core/gallery', {
images: medias.map( media => ( {
id: media.id,
url: media.source_url,
} ) ),
} ) );
transform( files, onChange ) {
const block = createBlock( 'core/gallery', {
images: files.map( ( file ) => ( {
url: window.URL.createObjectURL( file ),
} ) ),
} );

Promise.all( files.map( ( file ) =>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor: should we use Promise.all here and wait for all images to load or update each image after they load?
On the gallery block insertion, we update image by image. But I'm fine with merging it is as I think the code is simpler and then we can iterate and make behaviors identical / reuse code if possible.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch, I was wondering the same thing. This code is slightly simpler though :)

createMediaFromFile( file )
.then( ( media ) => preloadImage( media.source_url ).then( () => media ) )
) ).then( ( medias ) => onChange( block.uid, {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Typo: There is no such word "medias". Media itself is a plural.

http://www.dictionary.com/browse/media

images: medias.map( media => ( {
id: media.id,
url: media.source_url,
} ) ),
} ) );

return block;
},
},
],
Expand Down
20 changes: 13 additions & 7 deletions blocks/library/image/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* WordPress dependencies
*/
import { __ } from '@wordpress/i18n';
import { createMediaFromFile } from '@wordpress/utils';
import { createMediaFromFile, preloadImage } from '@wordpress/utils';

/**
* Internal dependencies
Expand Down Expand Up @@ -88,12 +88,18 @@ registerBlockType( 'core/image', {
isMatch( files ) {
return files.length === 1 && files[ 0 ].type.indexOf( 'image/' ) === 0;
},
transform( files ) {
return createMediaFromFile( files[ 0 ] )
.then( ( media ) => createBlock( 'core/image', {
id: media.id,
url: media.source_url,
} ) );
transform( files, onChange ) {
const file = files[ 0 ];
const block = createBlock( 'core/image', {
url: window.URL.createObjectURL( file ),
} );

createMediaFromFile( file )
.then( ( media ) => preloadImage( media.source_url ).then(
() => onChange( block.uid, { id: media.id, url: media.source_url } )
) );

return block;
},
},
{
Expand Down
10 changes: 4 additions & 6 deletions editor/components/block-drop-zone/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import { compose } from '@wordpress/element';
/**
* Internal dependencies
*/
import { insertBlocks } from '../../store/actions';
import { insertBlocks, updateBlockAttributes } from '../../store/actions';

function BlockDropZone( { index, isLocked, ...props } ) {
if ( isLocked ) {
Expand All @@ -40,10 +40,8 @@ function BlockDropZone( { index, isLocked, ...props } ) {

if ( transformation ) {
const insertPosition = getInsertPosition( position );

transformation.transform( files ).then( ( blocks ) => {
props.insertBlocks( blocks, insertPosition );
} );
const blocks = transformation.transform( files, props.updateBlockAttributes );
props.insertBlocks( blocks, insertPosition );
}
};

Expand All @@ -66,7 +64,7 @@ function BlockDropZone( { index, isLocked, ...props } ) {
export default compose(
connect(
undefined,
{ insertBlocks }
{ insertBlocks, updateBlockAttributes }
),
withContext( 'editor' )( ( settings ) => {
const { templateLock } = settings;
Expand Down
16 changes: 16 additions & 0 deletions utils/mediaupload.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,3 +60,19 @@ export function createMediaFromFile( file ) {
contentType: false,
} );
}

/**
* Utility used to preload an image before displaying it.
*
* @param {string} url Image Url.
* @returns {Promise} Pormise resolved once the image is preloaded.
*/
export function preloadImage( url ) {
return new Promise( resolve => {
const newImg = new window.Image();
newImg.onload = function() {
resolve( url );
};
newImg.src = url;
} );
}