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
Implement File block #7622
Merged
Merged
Implement File block #7622
Changes from 17 commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
54c7f79
Implement File block
noisysocks d2b4236
e → event
noisysocks 51d2cab
Specify type on all of the file block's attributes
noisysocks 65c677c
Use instead of calling setAttributes() in constructor
noisysocks 08dd3bc
this.props.attributes → attributes
noisysocks 6627400
Use classnames to simplify className computation
noisysocks 1384512
this.props.hrefs → hrefs
noisysocks 5fc44df
Use let instead of IIFE
noisysocks f96344b
Add help text to Show Download Button toggle, improve copy
noisysocks 86e5df1
Make FileBlockInspector a function component
noisysocks 557cd04
Move inline functions in render to class methods
talldan 03cfbdd
Minor stylistic changes in editable-link component
talldan 2911f3c
Add visited styling for file block button in front-end
talldan 1f910fc
Add explanatory comment about use of RichText component
talldan 3854c32
Renaming and commenting for file block attributes
talldan fc224c3
Renaming for openInNewWindow attribute for clarity
talldan 7b9358e
Fix file block content tests
noisysocks 15f8086
Use wp.blob instead of deprecated wp.utils
talldan 5202a06
Fix error with file block when dragging/dropping file
talldan a690317
Improve copy for file block description
talldan File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
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
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,235 @@ | ||
/** | ||
* External depedencies | ||
*/ | ||
import classnames from 'classnames'; | ||
|
||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { __ } from '@wordpress/i18n'; | ||
import { getBlobByURL, revokeBlobURL } from '@wordpress/utils'; | ||
import { | ||
ClipboardButton, | ||
IconButton, | ||
Toolbar, | ||
withNotices, | ||
} from '@wordpress/components'; | ||
import { withSelect } from '@wordpress/data'; | ||
import { Component, compose, Fragment } from '@wordpress/element'; | ||
import { | ||
MediaUpload, | ||
MediaPlaceholder, | ||
BlockControls, | ||
RichText, | ||
editorMediaUpload, | ||
} from '@wordpress/editor'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import './editor.scss'; | ||
import FileBlockInspector from './inspector'; | ||
import FileBlockEditableLink from './editable-link'; | ||
|
||
class FileEdit extends Component { | ||
constructor() { | ||
super( ...arguments ); | ||
|
||
this.onSelectFile = this.onSelectFile.bind( this ); | ||
this.confirmCopyURL = this.confirmCopyURL.bind( this ); | ||
this.resetCopyConfirmation = this.resetCopyConfirmation.bind( this ); | ||
this.changeLinkDestinationOption = this.changeLinkDestinationOption.bind( this ); | ||
this.changeOpenInNewWindow = this.changeOpenInNewWindow.bind( this ); | ||
this.changeShowDownloadButton = this.changeShowDownloadButton.bind( this ); | ||
|
||
this.state = { | ||
showCopyConfirmation: false, | ||
}; | ||
} | ||
|
||
componentDidMount() { | ||
const { href } = this.props.attributes; | ||
|
||
// Upload a file drag-and-dropped into the editor | ||
if ( this.isBlobURL( href ) ) { | ||
getBlobByURL( href ) | ||
.then( ( file ) => { | ||
editorMediaUpload( { | ||
allowedType: '*', | ||
filesList: [ file ], | ||
onFileChange: ( [ media ] ) => this.onSelectFile( media ), | ||
} ); | ||
revokeBlobURL( href ); | ||
} ); | ||
} | ||
} | ||
|
||
componentDidUpdate( prevProps ) { | ||
// Reset copy confirmation state when block is deselected | ||
if ( prevProps.isSelected && ! this.props.isSelected ) { | ||
this.setState( { showCopyConfirmation: false } ); | ||
} | ||
} | ||
|
||
onSelectFile( media ) { | ||
if ( media && media.url ) { | ||
this.props.setAttributes( { | ||
href: media.url, | ||
fileName: media.title, | ||
textLinkHref: media.url, | ||
id: media.id, | ||
} ); | ||
} | ||
} | ||
|
||
isBlobURL( url = '' ) { | ||
return url.indexOf( 'blob:' ) === 0; | ||
} | ||
|
||
confirmCopyURL() { | ||
this.setState( { showCopyConfirmation: true } ); | ||
} | ||
|
||
resetCopyConfirmation() { | ||
this.setState( { showCopyConfirmation: false } ); | ||
} | ||
|
||
changeLinkDestinationOption( newHref ) { | ||
// Choose Media File or Attachment Page (when file is in Media Library) | ||
this.props.setAttributes( { textLinkHref: newHref } ); | ||
} | ||
|
||
changeOpenInNewWindow( newValue ) { | ||
this.props.setAttributes( { | ||
textLinkTarget: newValue ? '_blank' : false, | ||
} ); | ||
} | ||
|
||
changeShowDownloadButton( newValue ) { | ||
this.props.setAttributes( { showDownloadButton: newValue } ); | ||
} | ||
|
||
render() { | ||
const { | ||
className, | ||
isSelected, | ||
attributes, | ||
setAttributes, | ||
noticeUI, | ||
noticeOperations, | ||
media, | ||
} = this.props; | ||
const { | ||
fileName, | ||
href, | ||
textLinkHref, | ||
textLinkTarget, | ||
showDownloadButton, | ||
downloadButtonText, | ||
id, | ||
} = attributes; | ||
const { showCopyConfirmation } = this.state; | ||
const attachmentPage = media && media.link; | ||
|
||
const classes = classnames( className, { | ||
'is-transient': this.isBlobURL( href ), | ||
} ); | ||
|
||
if ( ! href ) { | ||
return ( | ||
<MediaPlaceholder | ||
icon="media-default" | ||
labels={ { | ||
title: __( 'File' ), | ||
name: __( 'a file' ), | ||
} } | ||
onSelect={ this.onSelectFile } | ||
notices={ noticeUI } | ||
onError={ noticeOperations.createErrorNotice } | ||
accept="*" | ||
type="*" | ||
/> | ||
); | ||
} | ||
|
||
return ( | ||
<Fragment> | ||
<FileBlockInspector | ||
hrefs={ { href, textLinkHref, attachmentPage } } | ||
{ ...{ | ||
openInNewWindow: !! textLinkTarget, | ||
showDownloadButton, | ||
changeLinkDestinationOption: this.changeLinkDestinationOption, | ||
changeOpenInNewWindow: this.changeOpenInNewWindow, | ||
changeShowDownloadButton: this.changeShowDownloadButton, | ||
} } | ||
/> | ||
<BlockControls> | ||
<Toolbar> | ||
<MediaUpload | ||
onSelect={ this.onSelectFile } | ||
type="*" | ||
value={ id } | ||
render={ ( { open } ) => ( | ||
<IconButton | ||
className="components-toolbar__control" | ||
label={ __( 'Edit file' ) } | ||
onClick={ open } | ||
icon="edit" | ||
/> | ||
) } | ||
/> | ||
</Toolbar> | ||
</BlockControls> | ||
<div className={ classes }> | ||
<div> | ||
<FileBlockEditableLink | ||
className={ className } | ||
placeholder={ __( 'Write file name…' ) } | ||
text={ fileName } | ||
href={ textLinkHref } | ||
updateFileName={ ( text ) => setAttributes( { fileName: text } ) } | ||
/> | ||
{ showDownloadButton && | ||
<div className={ `${ className }__button-richtext-wrapper` }> | ||
{ /* Using RichText here instead of PlainText so that it can be styled like a button */ } | ||
<RichText | ||
tagName="div" // must be block-level or else cursor disappears | ||
className={ `${ className }__button` } | ||
value={ downloadButtonText } | ||
formattingControls={ [] } // disable controls | ||
placeholder={ __( 'Add text…' ) } | ||
keepPlaceholderOnFocus | ||
multiline="false" | ||
onChange={ ( text ) => setAttributes( { downloadButtonText: text } ) } | ||
/> | ||
</div> | ||
} | ||
</div> | ||
{ isSelected && | ||
<ClipboardButton | ||
isDefault | ||
text={ href } | ||
className={ `${ className }__copy-url-button` } | ||
onCopy={ this.confirmCopyURL } | ||
onFinishCopy={ this.resetCopyConfirmation } | ||
> | ||
{ showCopyConfirmation ? __( 'Copied!' ) : __( 'Copy URL' ) } | ||
</ClipboardButton> | ||
} | ||
</div> | ||
</Fragment> | ||
); | ||
} | ||
} | ||
|
||
export default compose( [ | ||
withSelect( ( select, props ) => { | ||
const { getMedia } = select( 'core' ); | ||
const { id } = props.attributes; | ||
return { | ||
media: id === undefined ? undefined : getMedia( id ), | ||
}; | ||
} ), | ||
withNotices, | ||
] )( FileEdit ); |
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,79 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { Component, Fragment } from '@wordpress/element'; | ||
|
||
export default class FileBlockEditableLink extends Component { | ||
constructor() { | ||
super( ...arguments ); | ||
|
||
this.copyLinkToClipboard = this.copyLinkToClipboard.bind( this ); | ||
this.showPlaceholderIfEmptyString = this.showPlaceholderIfEmptyString.bind( this ); | ||
|
||
this.state = { | ||
showPlaceholder: ! this.props.text, | ||
}; | ||
} | ||
|
||
componentDidUpdate( prevProps ) { | ||
if ( prevProps.text !== this.props.text ) { | ||
this.setState( { showPlaceholder: ! this.props.text } ); | ||
} | ||
} | ||
|
||
copyLinkToClipboard( event ) { | ||
const selectedText = document.getSelection().toString(); | ||
const htmlLink = `<a href="${ this.props.href }">${ selectedText }</a>`; | ||
event.clipboardData.setData( 'text/plain', selectedText ); | ||
event.clipboardData.setData( 'text/html', htmlLink ); | ||
} | ||
|
||
forcePlainTextPaste( event ) { | ||
event.preventDefault(); | ||
|
||
const selection = document.getSelection(); | ||
const clipboard = event.clipboardData.getData( 'text/plain' ).replace( /[\n\r]/g, '' ); | ||
const textNode = document.createTextNode( clipboard ); | ||
|
||
selection.getRangeAt( 0 ).insertNode( textNode ); | ||
selection.collapseToEnd(); | ||
} | ||
|
||
showPlaceholderIfEmptyString( event ) { | ||
this.setState( { showPlaceholder: event.target.innerText === '' } ); | ||
} | ||
|
||
render() { | ||
const { className, placeholder, text, href, updateFileName } = this.props; | ||
const { showPlaceholder } = this.state; | ||
|
||
return ( | ||
<Fragment> | ||
<a | ||
aria-label={ placeholder } | ||
className={ `${ className }__textlink` } | ||
href={ href } | ||
onBlur={ ( event ) => updateFileName( event.target.innerText ) } | ||
onInput={ this.showPlaceholderIfEmptyString } | ||
onCopy={ this.copyLinkToClipboard } | ||
onCut={ this.copyLinkToClipboard } | ||
onPaste={ this.forcePlainTextPaste } | ||
contentEditable | ||
> | ||
{ text } | ||
</a> | ||
{ showPlaceholder && | ||
// Disable reason: Only useful for mouse users | ||
/* eslint-disable jsx-a11y/no-static-element-interactions, jsx-a11y/click-events-have-key-events */ | ||
<span | ||
className={ `${ className }__textlink-placeholder` } | ||
onClick={ ( event ) => event.target.previousSibling.focus() } | ||
> | ||
{ placeholder } | ||
</span> | ||
/* eslint-enable jsx-a11y/no-static-element-interactions, jsx-a11y/click-events-have-key-events */ | ||
} | ||
</Fragment> | ||
); | ||
} | ||
} |
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,35 @@ | ||
.wp-block-file { | ||
display: flex; | ||
justify-content: space-between; | ||
align-items: center; | ||
margin-bottom: 0; | ||
|
||
&.is-transient { | ||
@include loading_fade; | ||
} | ||
|
||
&__textlink { | ||
color: $blue-medium-700; | ||
min-width: 1em; | ||
text-decoration: underline; | ||
|
||
&:focus { | ||
box-shadow: none; | ||
color: $blue-medium-700; | ||
} | ||
} | ||
|
||
&__textlink-placeholder { | ||
opacity: .5; | ||
text-decoration: underline; | ||
} | ||
|
||
&__button-richtext-wrapper { | ||
display: inline-block; | ||
margin-left: 0.75em; | ||
} | ||
|
||
&__copy-url-button { | ||
margin-left: 1em; | ||
} | ||
} |
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.
Warning:
Should this be using the
RichText
component instead? There are many complexities ofcontentEditable
that are meant to be abstracted into this component for general usage like this.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.
Issue at #8023