Skip to content

Commit

Permalink
Blocks: Showing a preview of the block immediately after drag and drop (
Browse files Browse the repository at this point in the history
  • Loading branch information
youknowriad committed Jan 19, 2018
1 parent 060200c commit 036f60a
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 22 deletions.
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 ) =>
createMediaFromFile( file )
.then( ( media ) => preloadImage( media.source_url ).then( () => media ) )
) ).then( ( medias ) => onChange( block.uid, {
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;
} );
}

0 comments on commit 036f60a

Please sign in to comment.