Skip to content

Commit

Permalink
Flash block outline in response to copy action
Browse files Browse the repository at this point in the history
  • Loading branch information
mcsf committed May 12, 2020
1 parent 7d34d9a commit 99cf442
Showing 1 changed file with 27 additions and 1 deletion.
28 changes: 27 additions & 1 deletion packages/block-editor/src/components/copy-handler/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* WordPress dependencies
*/
import { useRef } from '@wordpress/element';
import { useCallback, useEffect, useRef } from '@wordpress/element';
import { serialize, pasteHandler } from '@wordpress/blocks';
import { documentHasSelection, documentHasTextSelection } from '@wordpress/dom';
import { useDispatch, useSelect } from '@wordpress/data';
Expand All @@ -12,6 +12,29 @@ import { __, sprintf } from '@wordpress/i18n';
*/
import { getPasteEventData } from '../../utils/get-paste-event-data';

function useFlashBlock() {
const { toggleBlockHighlight } = useDispatch( 'core/block-editor' );
const timeouts = useRef( [] );
const flashBlock = useCallback(
( clientId ) => {
toggleBlockHighlight( clientId, true );
const timeout = setTimeout( () => {
toggleBlockHighlight( clientId, false );
}, 1000 );
timeouts.current.push( timeout );
},
[ toggleBlockHighlight ]
);
useEffect( () => {
return () => {
timeouts.current.forEach( ( timeout ) => {
clearTimeout( timeout );
} );
};
}, [] );
return flashBlock;
}

function CopyHandler( { children } ) {
const containerRef = useRef();

Expand All @@ -30,6 +53,8 @@ function CopyHandler( { children } ) {
const { removeBlocks, replaceBlocks } = useDispatch( 'core/block-editor' );
const { createSuccessNotice } = useDispatch( 'core/notices' );

const flashBlock = useFlashBlock();

const {
__experimentalCanUserUseUnfilteredHTML: canUserUseUnfilteredHTML,
} = getSettings();
Expand Down Expand Up @@ -66,6 +91,7 @@ function CopyHandler( { children } ) {
if ( selectedBlockClientIds.length === 1 ) {
const clientId = selectedBlockClientIds[ 0 ];
const { title } = getBlockType( getBlockName( clientId ) );
flashBlock( clientId );
notice = sprintf(
// Translators: Name of the block being copied, e.g. "Paragraph"
__( 'Copied block "%s" to clipboard.' ),
Expand Down

0 comments on commit 99cf442

Please sign in to comment.