-
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
Feature: Added InnerBlock Support for Download Button & Add Support for download attribute #68510
base: trunk
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,11 +21,11 @@ import { | |
RichText, | ||
useBlockProps, | ||
store as blockEditorStore, | ||
__experimentalGetElementClassName, | ||
InnerBlocks, | ||
} from '@wordpress/block-editor'; | ||
import { useEffect, useState } from '@wordpress/element'; | ||
import { useState } from '@wordpress/element'; | ||
import { useCopyToClipboard } from '@wordpress/compose'; | ||
import { __, _x } from '@wordpress/i18n'; | ||
import { __ } from '@wordpress/i18n'; | ||
import { file as icon } from '@wordpress/icons'; | ||
import { store as coreStore } from '@wordpress/core-data'; | ||
import { store as noticesStore } from '@wordpress/notices'; | ||
|
@@ -69,7 +69,6 @@ function FileEdit( { attributes, isSelected, setAttributes, clientId } ) { | |
textLinkHref, | ||
textLinkTarget, | ||
showDownloadButton, | ||
downloadButtonText, | ||
displayPreview, | ||
previewHeight, | ||
} = attributes; | ||
|
@@ -85,27 +84,14 @@ function FileEdit( { attributes, isSelected, setAttributes, clientId } ) { | |
); | ||
|
||
const { createErrorNotice } = useDispatch( noticesStore ); | ||
const { toggleSelection, __unstableMarkNextChangeAsNotPersistent } = | ||
useDispatch( blockEditorStore ); | ||
const { toggleSelection } = useDispatch( blockEditorStore ); | ||
|
||
useUploadMediaFromBlobURL( { | ||
url: temporaryURL, | ||
onChange: onSelectFile, | ||
onError: onUploadError, | ||
} ); | ||
|
||
// Note: Handle setting a default value for `downloadButtonText` via HTML API | ||
// when it supports replacing text content for HTML tags. | ||
useEffect( () => { | ||
if ( RichText.isEmpty( downloadButtonText ) ) { | ||
__unstableMarkNextChangeAsNotPersistent(); | ||
setAttributes( { | ||
downloadButtonText: _x( 'Download', 'button label' ), | ||
} ); | ||
} | ||
// This effect should only run on mount. | ||
}, [] ); | ||
|
||
function onSelectFile( newMedia ) { | ||
if ( ! newMedia || ! newMedia.url ) { | ||
// Reset attributes. | ||
|
@@ -302,29 +288,34 @@ function FileEdit( { attributes, isSelected, setAttributes, clientId } ) { | |
href={ textLinkHref } | ||
/> | ||
{ showDownloadButton && ( | ||
<div className="wp-block-file__button-richtext-wrapper"> | ||
{ /* Using RichText here instead of PlainText so that it can be styled like a button. */ } | ||
<RichText | ||
identifier="downloadButtonText" | ||
tagName="div" // Must be block-level or else cursor disappears. | ||
aria-label={ __( 'Download button text' ) } | ||
className={ clsx( | ||
'wp-block-file__button', | ||
__experimentalGetElementClassName( | ||
'button' | ||
) | ||
) } | ||
value={ downloadButtonText } | ||
withoutInteractiveFormatting | ||
placeholder={ __( 'Add text…' ) } | ||
onChange={ ( text ) => | ||
setAttributes( { | ||
downloadButtonText: | ||
removeAnchorTag( text ), | ||
} ) | ||
} | ||
/> | ||
</div> | ||
<InnerBlocks | ||
allowedBlocks={ [ 'core/buttons' ] } | ||
template={ [ | ||
[ | ||
'core/buttons', | ||
{}, | ||
[ | ||
[ | ||
'core/button', | ||
{ | ||
text: __( 'Download' ), | ||
lock: { | ||
remove: true, | ||
move: true, | ||
}, | ||
url: href || temporaryURL, | ||
download: true, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 commentThe reason will be displayed to describe this comment to others. Learn more. I went ahead with implementing the download attribute on button block for now, I think there is a need for discussion if alternative exists but I feel implementing download attribute is better way to handle this. |
||
ariaLabel: __( | ||
'Download button text' | ||
), | ||
}, | ||
], | ||
], | ||
], | ||
] } | ||
templateLock="all" | ||
renderAppender={ false } | ||
/> | ||
) } | ||
</div> | ||
</div> | ||
|
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.
In almost all cases, you want to use
useInnerBlocksProps
rather than<InnerBlocks />
these days because that allows you to match the markup between the editor and the frontend (you don't get additionaldiv
elements injected in the editor)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 see, thanks for pointing that out. I'll look into updating it to use useInnerBlocksProps!
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 have updated the code to reflect the changes with
useInnerBlocksProps
. Thanks