From 97d1cf49fb931ce8d0fd20874e1001e36c8ccbee Mon Sep 17 00:00:00 2001 From: George Mamadashvili Date: Tue, 16 Jun 2026 16:40:57 +0400 Subject: [PATCH] File Block: Replace on-mount downloadButtonText effect with a default variation --- packages/block-library/src/file/edit.js | 19 +++---------------- packages/block-library/src/file/index.js | 2 ++ packages/block-library/src/file/transforms.js | 8 ++++++++ packages/block-library/src/file/variations.js | 17 +++++++++++++++++ 4 files changed, 30 insertions(+), 16 deletions(-) create mode 100644 packages/block-library/src/file/variations.js diff --git a/packages/block-library/src/file/edit.js b/packages/block-library/src/file/edit.js index b796e7af6f610e..3cbb550d5f0d6c 100644 --- a/packages/block-library/src/file/edit.js +++ b/packages/block-library/src/file/edit.js @@ -23,9 +23,9 @@ import { store as blockEditorStore, __experimentalGetElementClassName, } 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'; @@ -90,8 +90,7 @@ function FileEdit( { attributes, isSelected, setAttributes, clientId } ) { ); const { createErrorNotice } = useDispatch( noticesStore ); - const { toggleSelection, __unstableMarkNextChangeAsNotPersistent } = - useDispatch( blockEditorStore ); + const { toggleSelection } = useDispatch( blockEditorStore ); useUploadMediaFromBlobURL( { url: temporaryURL, @@ -99,18 +98,6 @@ function FileEdit( { attributes, isSelected, setAttributes, clientId } ) { 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. diff --git a/packages/block-library/src/file/index.js b/packages/block-library/src/file/index.js index 648cf062d3fa1b..1e3303e9d5f73e 100644 --- a/packages/block-library/src/file/index.js +++ b/packages/block-library/src/file/index.js @@ -14,6 +14,7 @@ import edit from './edit'; import metadata from './block.json'; import save from './save'; import transforms from './transforms'; +import variations from './variations'; import { unlock } from '../lock-unlock'; const { fieldsKey, formKey } = unlock( blocksPrivateApis ); @@ -34,6 +35,7 @@ export const settings = { deprecated, edit, save, + variations, }; if ( window.__experimentalContentOnlyInspectorFields ) { diff --git a/packages/block-library/src/file/transforms.js b/packages/block-library/src/file/transforms.js index b6862d374a9f03..d2a640ce371bbf 100644 --- a/packages/block-library/src/file/transforms.js +++ b/packages/block-library/src/file/transforms.js @@ -5,8 +5,12 @@ import { createBlobURL } from '@wordpress/blob'; import { createBlock } from '@wordpress/blocks'; import { select } from '@wordpress/data'; import { store as coreStore } from '@wordpress/core-data'; +import { _x } from '@wordpress/i18n'; import { getFilename } from '@wordpress/url'; +// Transforms bypass the default variation, so set the localized default here. +const downloadButtonText = _x( 'Download', 'button label' ); + const transforms = { from: [ { @@ -47,6 +51,7 @@ const transforms = { createBlock( 'core/file', { blob: blobURL, fileName: file.name, + downloadButtonText, } ) ); } @@ -65,6 +70,7 @@ const transforms = { textLinkHref: attributes.src, id: attributes.id, anchor: attributes.anchor, + downloadButtonText, } ); }, }, @@ -78,6 +84,7 @@ const transforms = { textLinkHref: attributes.src, id: attributes.id, anchor: attributes.anchor, + downloadButtonText, } ); }, }, @@ -92,6 +99,7 @@ const transforms = { textLinkHref: attributes.url, id: attributes.id, anchor: attributes.anchor, + downloadButtonText, } ); }, }, diff --git a/packages/block-library/src/file/variations.js b/packages/block-library/src/file/variations.js new file mode 100644 index 00000000000000..f9138656bbec5e --- /dev/null +++ b/packages/block-library/src/file/variations.js @@ -0,0 +1,17 @@ +/** + * WordPress dependencies + */ +import { _x } from '@wordpress/i18n'; + +const variations = [ + { + name: 'default', + isDefault: true, + // Translatable defaults can't live in `block.json`, so set it here. + attributes: { + downloadButtonText: _x( 'Download', 'button label' ), + }, + }, +]; + +export default variations;