Skip to content

Commit

Permalink
Fix: File Block is crashing as soon as a file is selected (#10323)
Browse files Browse the repository at this point in the history
  • Loading branch information
jorgefilipecosta authored Oct 4, 2018
1 parent b6303f1 commit 4a47dff
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 17 deletions.
6 changes: 2 additions & 4 deletions packages/block-library/src/file/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import {
mediaUpload,
} from '@wordpress/editor';
import { compose } from '@wordpress/compose';
import { create } from '@wordpress/rich-text';

/**
* Internal dependencies
Expand Down Expand Up @@ -80,7 +81,7 @@ class FileEdit extends Component {
this.setState( { hasError: false } );
this.props.setAttributes( {
href: media.url,
fileName: media.title,
fileName: create( { text: media.title } ),
textLinkHref: media.url,
id: media.id,
} );
Expand Down Expand Up @@ -189,9 +190,7 @@ class FileEdit extends Component {
<RichText
wrapperClassName={ `${ className }__textlink` }
tagName="div" // must be block-level or else cursor disappears
format="string"
value={ fileName }
multiline="false"
placeholder={ __( 'Write file name…' ) }
keepPlaceholderOnFocus
formattingControls={ [] } // disable controls
Expand All @@ -207,7 +206,6 @@ class FileEdit extends Component {
formattingControls={ [] } // disable controls
placeholder={ __( 'Add text…' ) }
keepPlaceholderOnFocus
multiline="false"
onChange={ ( text ) => setAttributes( { downloadButtonText: text } ) }
/>
</div>
Expand Down
20 changes: 12 additions & 8 deletions packages/block-library/src/file/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import { __ } from '@wordpress/i18n';
import { createBlobURL } from '@wordpress/blob';
import { createBlock } from '@wordpress/blocks';
import { select } from '@wordpress/data';
import { RichText } from '@wordpress/editor';
import { getTextContent, isEmpty } from '@wordpress/rich-text';

/**
* Internal dependencies
Expand Down Expand Up @@ -37,8 +39,7 @@ export const settings = {
type: 'string',
},
fileName: {
type: 'string',
source: 'text',
source: 'rich-text',
selector: 'a:not([download])',
},
// Differs to the href when the block is configured to link to the attachment page
Expand All @@ -60,8 +61,7 @@ export const settings = {
default: true,
},
downloadButtonText: {
type: 'string',
source: 'text',
source: 'rich-text',
selector: 'a[download]',
default: __( 'Download' ),
},
Expand Down Expand Up @@ -203,13 +203,15 @@ export const settings = {

return ( href &&
<div>
{ fileName &&
{ ! isEmpty( fileName ) &&
<a
href={ textLinkHref }
target={ textLinkTarget }
rel={ textLinkTarget ? 'noreferrer noopener' : false }
>
{ fileName }
<RichText.Content
value={ fileName }
/>
</a>
}
{ showDownloadButton &&
Expand All @@ -219,9 +221,11 @@ export const settings = {
// ensure download attribute is still set when fileName
// is undefined. Using '' here as `true` still leaves
// the attribute unset.
download={ fileName || '' }
download={ getTextContent( fileName ) }
>
{ downloadButtonText }
<RichText.Content
value={ downloadButtonText }
/>
</a>
}
</div>
Expand Down
24 changes: 22 additions & 2 deletions test/integration/full-content/fixtures/core__file__new-window.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,31 @@
"attributes": {
"id": 176,
"href": "http://localhost:8888/wp-content/uploads/2018/05/keycodes.js",
"fileName": "6546",
"fileName": {
"formats": [
null,
null,
null,
null
],
"text": "6546"
},
"textLinkHref": "http://localhost:8888/wp-content/uploads/2018/05/keycodes.js",
"textLinkTarget": "_blank",
"showDownloadButton": true,
"downloadButtonText": "Download"
"downloadButtonText": {
"formats": [
null,
null,
null,
null,
null,
null,
null,
null
],
"text": "Download"
}
},
"innerBlocks": [],
"originalContent": "<div class=\"wp-block-file\"><a href=\"http://localhost:8888/wp-content/uploads/2018/05/keycodes.js\" target=\"_blank\" rel=\"noreferrer noopener\">6546</a><a href=\"http://localhost:8888/wp-content/uploads/2018/05/keycodes.js\" class=\"wp-block-file__button\" download=\"6546\">Download</a></div>"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,26 @@
"attributes": {
"id": 176,
"href": "http://localhost:8888/wp-content/uploads/2018/05/keycodes.js",
"fileName": "lkjfijwef",
"fileName": {
"formats": [
null,
null,
null,
null,
null,
null,
null,
null,
null
],
"text": "lkjfijwef"
},
"textLinkHref": "http://localhost:8888/?attachment_id=176",
"showDownloadButton": false,
"downloadButtonText": "Download"
"downloadButtonText": {
"formats": [],
"text": ""
}
},
"innerBlocks": [],
"originalContent": "<div class=\"wp-block-file\"><a href=\"http://localhost:8888/?attachment_id=176\">lkjfijwef</a></div>"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,24 @@
"attributes": {
"id": 176,
"href": "http://localhost:8888/wp-content/uploads/2018/05/keycodes.js",
"fileName": {
"formats": [],
"text": ""
},
"showDownloadButton": true,
"downloadButtonText": "Download"
"downloadButtonText": {
"formats": [
null,
null,
null,
null,
null,
null,
null,
null
],
"text": "Download"
}
},
"innerBlocks": [],
"originalContent": "<div class=\"wp-block-file\"><a href=\"http://localhost:8888/wp-content/uploads/2018/05/keycodes.js\" class=\"wp-block-file__button\" download=\"\">Download</a></div>"
Expand Down

0 comments on commit 4a47dff

Please sign in to comment.