Skip to content

Commit

Permalink
Block: add copy action (#22214)
Browse files Browse the repository at this point in the history
* Block: add copy action

* Only serialise on copy
  • Loading branch information
ellatrix authored May 8, 2020
1 parent 0984ca0 commit fca4e02
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
2 changes: 2 additions & 0 deletions packages/block-editor/src/components/block-actions/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ function BlockActions( {
onInsertBefore,
onRemove,
onUngroup,
blocks,
} ) {
return children( {
canDuplicate,
Expand All @@ -32,6 +33,7 @@ function BlockActions( {
onInsertBefore,
onRemove,
onUngroup,
blocks,
} );
}

Expand Down
23 changes: 23 additions & 0 deletions packages/block-editor/src/components/block-settings-menu/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,12 @@ import {
DropdownMenu,
MenuGroup,
MenuItem,
ClipboardButton,
} from '@wordpress/components';
import { useSelect } from '@wordpress/data';
import { moreHorizontal } from '@wordpress/icons';
import { useState } from '@wordpress/element';
import { serialize } from '@wordpress/blocks';

/**
* Internal dependencies
Expand Down Expand Up @@ -56,6 +59,8 @@ export function BlockSettingsMenu( { clientIds } ) {
};
}, [] );

const [ hasCopied, setHasCopied ] = useState();

return (
<BlockActions clientIds={ clientIds }>
{ ( {
Expand All @@ -66,6 +71,7 @@ export function BlockSettingsMenu( { clientIds } ) {
onInsertAfter,
onInsertBefore,
onRemove,
blocks,
} ) => (
<ToolbarGroup>
<ToolbarItem>
Expand Down Expand Up @@ -98,6 +104,23 @@ export function BlockSettingsMenu( { clientIds } ) {
}
/>
) }
<ClipboardButton
text={ () =>
serialize( blocks )
}
role="menuitem"
className="components-menu-item__button"
onCopy={ () => {
setHasCopied( true );
} }
onFinishCopy={ () =>
setHasCopied( false )
}
>
{ hasCopied
? __( 'Copied!' )
: __( 'Copy' ) }
</ClipboardButton>
{ canDuplicate && (
<MenuItem
onClick={ flow(
Expand Down

0 comments on commit fca4e02

Please sign in to comment.