-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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
Refactor File block to use block.json metadata #14862
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,38 @@ | ||
{ | ||
"name": "core/file", | ||
"category": "common" | ||
"category": "common", | ||
"attributes": { | ||
"id": { | ||
"type": "number" | ||
}, | ||
"href": { | ||
"type": "string" | ||
}, | ||
"fileName": { | ||
"type": "string", | ||
"source": "html", | ||
"selector": "a:not([download])" | ||
}, | ||
"textLinkHref": { | ||
"type": "string", | ||
"source": "attribute", | ||
"selector": "a:not([download])", | ||
"attribute": "href" | ||
}, | ||
"textLinkTarget": { | ||
"type": "string", | ||
"source": "attribute", | ||
"selector": "a:not([download])", | ||
"attribute": "target" | ||
}, | ||
"showDownloadButton": { | ||
"type": "boolean", | ||
"default": true | ||
}, | ||
"downloadButtonText": { | ||
"type": "string", | ||
"source": "html", | ||
"selector": "a[download]" | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,242 +1,30 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import { includes } from 'lodash'; | ||
|
||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { __, _x } from '@wordpress/i18n'; | ||
import { createBlobURL } from '@wordpress/blob'; | ||
import { createBlock } from '@wordpress/blocks'; | ||
import { select } from '@wordpress/data'; | ||
import { RichText } from '@wordpress/block-editor'; | ||
import { __ } from '@wordpress/i18n'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import edit from './edit'; | ||
import icon from './icon'; | ||
import metadata from './block.json'; | ||
import save from './save'; | ||
import transforms from './transforms'; | ||
|
||
const { name } = metadata; | ||
|
||
export { metadata, name }; | ||
|
||
export const settings = { | ||
title: __( 'File' ), | ||
|
||
description: __( 'Add a link to a downloadable file.' ), | ||
|
||
icon, | ||
|
||
keywords: [ __( 'document' ), __( 'pdf' ) ], | ||
|
||
attributes: { | ||
id: { | ||
type: 'number', | ||
}, | ||
href: { | ||
type: 'string', | ||
}, | ||
fileName: { | ||
type: 'string', | ||
source: 'html', | ||
selector: 'a:not([download])', | ||
}, | ||
// Differs to the href when the block is configured to link to the attachment page | ||
textLinkHref: { | ||
type: 'string', | ||
source: 'attribute', | ||
selector: 'a:not([download])', | ||
attribute: 'href', | ||
}, | ||
// e.g. `_blank` when the block is configured to open in a new tab | ||
textLinkTarget: { | ||
type: 'string', | ||
source: 'attribute', | ||
selector: 'a:not([download])', | ||
attribute: 'target', | ||
}, | ||
showDownloadButton: { | ||
type: 'boolean', | ||
default: true, | ||
}, | ||
downloadButtonText: { | ||
type: 'string', | ||
source: 'html', | ||
selector: 'a[download]', | ||
default: _x( 'Download', 'button label' ), | ||
}, | ||
}, | ||
|
||
supports: { | ||
align: true, | ||
}, | ||
|
||
transforms: { | ||
from: [ | ||
{ | ||
type: 'files', | ||
isMatch( files ) { | ||
return files.length > 0; | ||
}, | ||
// 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 blocks = []; | ||
|
||
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, | ||
} ) ); | ||
} ); | ||
|
||
return blocks; | ||
}, | ||
}, | ||
{ | ||
type: 'block', | ||
blocks: [ 'core/audio' ], | ||
transform: ( attributes ) => { | ||
return createBlock( 'core/file', { | ||
href: attributes.src, | ||
fileName: attributes.caption, | ||
textLinkHref: attributes.src, | ||
id: attributes.id, | ||
} ); | ||
}, | ||
}, | ||
{ | ||
type: 'block', | ||
blocks: [ 'core/video' ], | ||
transform: ( attributes ) => { | ||
return createBlock( 'core/file', { | ||
href: attributes.src, | ||
fileName: attributes.caption, | ||
textLinkHref: attributes.src, | ||
id: attributes.id, | ||
} ); | ||
}, | ||
}, | ||
{ | ||
type: 'block', | ||
blocks: [ 'core/image' ], | ||
transform: ( attributes ) => { | ||
return createBlock( 'core/file', { | ||
href: attributes.url, | ||
fileName: attributes.caption, | ||
textLinkHref: attributes.url, | ||
id: attributes.id, | ||
} ); | ||
}, | ||
}, | ||
], | ||
to: [ | ||
{ | ||
type: 'block', | ||
blocks: [ 'core/audio' ], | ||
isMatch: ( { id } ) => { | ||
if ( ! id ) { | ||
return false; | ||
} | ||
const { getMedia } = select( 'core' ); | ||
const media = getMedia( id ); | ||
return !! media && includes( media.mime_type, 'audio' ); | ||
}, | ||
transform: ( attributes ) => { | ||
return createBlock( 'core/audio', { | ||
src: attributes.href, | ||
caption: attributes.fileName, | ||
id: attributes.id, | ||
} ); | ||
}, | ||
}, | ||
{ | ||
type: 'block', | ||
blocks: [ 'core/video' ], | ||
isMatch: ( { id } ) => { | ||
if ( ! id ) { | ||
return false; | ||
} | ||
const { getMedia } = select( 'core' ); | ||
const media = getMedia( id ); | ||
return !! media && includes( media.mime_type, 'video' ); | ||
}, | ||
transform: ( attributes ) => { | ||
return createBlock( 'core/video', { | ||
src: attributes.href, | ||
caption: attributes.fileName, | ||
id: attributes.id, | ||
} ); | ||
}, | ||
}, | ||
{ | ||
type: 'block', | ||
blocks: [ 'core/image' ], | ||
isMatch: ( { id } ) => { | ||
if ( ! id ) { | ||
return false; | ||
} | ||
const { getMedia } = select( 'core' ); | ||
const media = getMedia( id ); | ||
return !! media && includes( media.mime_type, 'image' ); | ||
}, | ||
transform: ( attributes ) => { | ||
return createBlock( 'core/image', { | ||
url: attributes.href, | ||
caption: attributes.fileName, | ||
id: attributes.id, | ||
} ); | ||
}, | ||
}, | ||
], | ||
}, | ||
|
||
transforms, | ||
edit, | ||
|
||
save( { attributes } ) { | ||
const { | ||
href, | ||
fileName, | ||
textLinkHref, | ||
textLinkTarget, | ||
showDownloadButton, | ||
downloadButtonText, | ||
} = attributes; | ||
|
||
return ( href && | ||
<div> | ||
{ ! RichText.isEmpty( fileName ) && | ||
<a | ||
href={ textLinkHref } | ||
target={ textLinkTarget } | ||
rel={ textLinkTarget ? 'noreferrer noopener' : false } | ||
> | ||
<RichText.Content | ||
value={ fileName } | ||
/> | ||
</a> | ||
} | ||
{ showDownloadButton && | ||
<a | ||
href={ href } | ||
className="wp-block-file__button" | ||
download={ true } | ||
> | ||
<RichText.Content | ||
value={ downloadButtonText } | ||
/> | ||
</a> | ||
} | ||
</div> | ||
); | ||
}, | ||
|
||
save, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { RichText } from '@wordpress/block-editor'; | ||
|
||
export default function save( { attributes } ) { | ||
const { | ||
href, | ||
fileName, | ||
textLinkHref, | ||
textLinkTarget, | ||
showDownloadButton, | ||
downloadButtonText, | ||
} = attributes; | ||
|
||
return ( href && | ||
<div> | ||
{ ! RichText.isEmpty( fileName ) && | ||
<a | ||
href={ textLinkHref } | ||
target={ textLinkTarget } | ||
rel={ textLinkTarget ? 'noreferrer noopener' : false } | ||
> | ||
<RichText.Content | ||
value={ fileName } | ||
/> | ||
</a> | ||
} | ||
{ showDownloadButton && | ||
<a | ||
href={ href } | ||
className="wp-block-file__button" | ||
download={ true } | ||
> | ||
<RichText.Content | ||
value={ downloadButtonText } | ||
/> | ||
</a> | ||
} | ||
</div> | ||
); | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It was discussed on WordPress.org Slack last week (link requires registration):
https://wordpress.slack.com/archives/C5UNMSU4R/p1554217879115800
The conclusion was that:
To make it work with
block.json
I converted it to the static text. In the previous form, it was confusing enough. It wasn't triggering block validation when editing the same post with a different locale selected, however, it's worth pointing out that the locale of the website may differ from the one that the person editing uses. It's better to make it static and let the author update this value.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I guess the decision here was to set this initially to the value of the author's language. In other words, we could use
componentDidMount
oruseEffect
in theedit
function of the block to set its value when the block is first created. This has the downside of creating an "undo" level maybe but maybe that's fine?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'll give it a go and see how it works 👍
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It is precisely what happens. Is there a way to skip adding undo level when calling
setAttributes
? I noticed that this is an issue also when using drag & drop with files. Triggeringundo
puts the block in a bizarre pulsing state in UI.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@aduth and @ellatrix any thoughts on this undo behavior for the transient state of the block attributes? Do you know if there is any issue or PR opened which tries to solve it?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
#8119 is the main issue tracking the problems in this area.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice, thanks for linking with the issue. It looks like it isn’t a blocker for this PR 👍