Skip to content

Commit

Permalink
File Block: Move copy url button is to the toolbar (#26602)
Browse files Browse the repository at this point in the history
Co-authored-by: Riad Benguella <benguella@gmail.com>
  • Loading branch information
daddou-ma and youknowriad authored Nov 5, 2020
1 parent 5f6514c commit 4292a04
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 48 deletions.
4 changes: 4 additions & 0 deletions packages/block-library/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@
- Fix a regression ([#26545](https://github.com/WordPress/gutenberg/pull/26545)) where the Cover block lost its default background overlay color
([#26569](https://github.com/WordPress/gutenberg/pull/26569)).

### Enhancement

- File Block: Copy url button is moved to Block toolbar.

## 2.23.0 (2020-09-03)

### Enhancement
Expand Down
76 changes: 33 additions & 43 deletions packages/block-library/src/file/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@ import classnames from 'classnames';
import { getBlobByURL, isBlobURL, revokeBlobURL } from '@wordpress/blob';
import {
__unstableUseAnimate as useAnimate,
ClipboardButton,
withNotices,
ToolbarGroup,
ToolbarButton,
} from '@wordpress/components';
import { useSelect } from '@wordpress/data';
import {
Expand All @@ -21,7 +22,8 @@ import {
RichText,
useBlockProps,
} from '@wordpress/block-editor';
import { useEffect, useState } from '@wordpress/element';
import { useEffect, useState, useRef } from '@wordpress/element';
import { useCopyOnClick } from '@wordpress/compose';
import { __, _x } from '@wordpress/i18n';
import { file as icon } from '@wordpress/icons';

Expand All @@ -30,13 +32,22 @@ import { file as icon } from '@wordpress/icons';
*/
import FileBlockInspector from './inspector';

function FileEdit( {
isSelected,
attributes,
setAttributes,
noticeUI,
noticeOperations,
} ) {
function ClipboardToolbarButton( { text, disabled } ) {
const ref = useRef();
const hasCopied = useCopyOnClick( ref, text );

return (
<ToolbarButton
className="components-clipboard-toolbar-button"
ref={ ref }
disabled={ disabled }
>
{ hasCopied ? __( 'Copied!' ) : __( 'Copy URL' ) }
</ToolbarButton>
);
}

function FileEdit( { attributes, setAttributes, noticeUI, noticeOperations } ) {
const {
id,
fileName,
Expand All @@ -47,7 +58,6 @@ function FileEdit( {
downloadButtonText,
} = attributes;
const [ hasError, setHasError ] = useState( false );
const [ showCopyConfirmation, setShowCopyConfirmation ] = useState( false );
const { media, mediaUpload } = useSelect(
( select ) => ( {
media:
Expand Down Expand Up @@ -82,10 +92,6 @@ function FileEdit( {
}
}, [] );

useEffect( () => {
setShowCopyConfirmation( false );
}, [ isSelected ] );

function onSelectFile( newMedia ) {
if ( newMedia && newMedia.url ) {
setHasError( false );
Expand All @@ -104,14 +110,6 @@ function FileEdit( {
noticeOperations.createErrorNotice( message );
}

function confirmCopyURL() {
setShowCopyConfirmation( true );
}

function resetCopyConfirmation() {
setShowCopyConfirmation( false );
}

function changeLinkDestinationOption( newHref ) {
// Choose Media File or Attachment Page (when file is in Media Library)
setAttributes( { textLinkHref: newHref } );
Expand Down Expand Up @@ -171,13 +169,19 @@ function FileEdit( {
} }
/>
<BlockControls>
<MediaReplaceFlow
mediaId={ id }
mediaURL={ href }
accept="*"
onSelect={ onSelectFile }
onError={ onUploadError }
/>
<ToolbarGroup>
<MediaReplaceFlow
mediaId={ id }
mediaURL={ href }
accept="*"
onSelect={ onSelectFile }
onError={ onUploadError }
/>
<ClipboardToolbarButton
text={ href }
disabled={ isBlobURL( href ) }
/>
</ToolbarGroup>
</BlockControls>
<div { ...blockProps }>
<div className={ 'wp-block-file__content-wrapper' }>
Expand Down Expand Up @@ -214,20 +218,6 @@ function FileEdit( {
</div>
) }
</div>
{ isSelected && (
<ClipboardButton
isSecondary
text={ href }
className={ 'wp-block-file__copy-url-button' }
onCopy={ confirmCopyURL }
onFinishCopy={ resetCopyConfirmation }
disabled={ isBlobURL( href ) }
>
{ showCopyConfirmation
? __( 'Copied!' )
: __( 'Copy URL' ) }
</ClipboardButton>
) }
</div>
</>
);
Expand Down
4 changes: 0 additions & 4 deletions packages/block-library/src/file/editor.scss
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,4 @@
display: inline-block;
margin-left: 0.75em;
}

.wp-block-file__copy-url-button {
margin-left: 1em;
}
}
1 change: 0 additions & 1 deletion packages/components/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

- Introduce `Navigation` component as `__experimentalNavigation` for displaying a hierarchy of items.


### Breaking Change

- Introduce support for other units and advanced CSS properties on `FontSizePicker`. Provided the value passed to the `FontSizePicker` is a string or one of the size options passed is a string, onChange will start to be called with a string value instead of a number. On WordPress usage, font size options are now automatically converted to strings with the default "px" unit added.
Expand Down
2 changes: 2 additions & 0 deletions packages/e2e-tests/specs/local/demo.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ describe( 'new editor state', () => {
} );
expect( isDirty ).toBeTruthy();

await page.waitForSelector( 'button.editor-post-save-draft' );

expect( await page.$( 'button.editor-post-save-draft' ) ).toBeTruthy();
} );
} );

0 comments on commit 4292a04

Please sign in to comment.