Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 3 additions & 16 deletions packages/block-library/src/file/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -90,27 +90,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.
Expand Down
2 changes: 2 additions & 0 deletions packages/block-library/src/file/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
Expand All @@ -34,6 +35,7 @@ export const settings = {
deprecated,
edit,
save,
variations,
};

if ( window.__experimentalContentOnlyInspectorFields ) {
Expand Down
8 changes: 8 additions & 0 deletions packages/block-library/src/file/transforms.js
Original file line number Diff line number Diff line change
Expand Up @@ -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: [
{
Expand Down Expand Up @@ -47,6 +51,7 @@ const transforms = {
createBlock( 'core/file', {
blob: blobURL,
fileName: file.name,
downloadButtonText,
} )
);
}
Expand All @@ -65,6 +70,7 @@ const transforms = {
textLinkHref: attributes.src,
id: attributes.id,
anchor: attributes.anchor,
downloadButtonText,
} );
},
},
Expand All @@ -78,6 +84,7 @@ const transforms = {
textLinkHref: attributes.src,
id: attributes.id,
anchor: attributes.anchor,
downloadButtonText,
} );
},
},
Expand All @@ -92,6 +99,7 @@ const transforms = {
textLinkHref: attributes.url,
id: attributes.id,
anchor: attributes.anchor,
downloadButtonText,
} );
},
},
Expand Down
17 changes: 17 additions & 0 deletions packages/block-library/src/file/variations.js
Original file line number Diff line number Diff line change
@@ -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;
Loading