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

Create file blocks when dropping multiple files at once #11297

Merged
merged 4 commits into from
Nov 1, 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
6 changes: 6 additions & 0 deletions packages/block-library/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
## 2.1.8 (Unreleased)

### Polish

- File Block: Create file blocks when dropping multiple files at once.

## 2.1.7 (2018-10-30)

## 2.1.6 (2018-10-30)
Expand Down
23 changes: 15 additions & 8 deletions packages/block-library/src/file/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,20 +77,27 @@ export const settings = {
from: [
{
type: 'files',
isMatch: ( files ) => files.length === 1,
isMatch( files ) {
return files.length > 0;
Copy link
Member Author

Choose a reason for hiding this comment

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

isMatch is required per

( transform ) => transform.type === 'files' && transform.isMatch( files )
so this is just a simple sanity check.

},
// We define a lower priorty (higher number) than the default of 10. This
// ensures that the File block is only created as a fallback.
priority: 15,
transform: ( files ) => {
const file = files[ 0 ];
const blobURL = createBlobURL( file );
const blocks = [];

// File will be uploaded in componentDidMount()
return createBlock( 'core/file', {
href: blobURL,
fileName: file.name,
textLinkHref: blobURL,
files.map( ( file ) => {
const blobURL = createBlobURL( file );
Copy link
Contributor

Choose a reason for hiding this comment

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

Out of scope of this PR but I thought I'd mention for context. Ideally, we shouldn't rely on the edit function to properly upload the file and use the final URL. Instead we should avoid storing temporary attributes in blocks.

That said, we don't have a proper solution for this problem at the moment, we'll more likely need to think about a "reactive" transform API but this is not high-priority.


// File will be uploaded in componentDidMount()
blocks.push( createBlock( 'core/file', {
href: blobURL,
fileName: file.name,
textLinkHref: blobURL,
} ) );
} );

return blocks;
},
},
{
Expand Down