Skip to content

Commit

Permalink
Mobile - ClipboardProvider - Use state
Browse files Browse the repository at this point in the history
  • Loading branch information
Gerardo Pacheco committed Nov 25, 2020
1 parent cc9044a commit d163761
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,7 @@ const BlockActionsMenu = ( {
} ) => {
const pickerRef = useRef();
const moversOptions = { keys: [ 'icon', 'actionTitle' ] };
const {
clipboard: { current: currentClipboard },
updateClipboard,
} = useContext( ClipboardContext );
const { clipboard, updateClipboard } = useContext( ClipboardContext );

const {
actionTitle: {
Expand Down Expand Up @@ -125,16 +122,19 @@ const BlockActionsMenu = ( {
] );

function onPasteBlock() {
if ( ! currentClipboard ) {
if ( ! clipboard ) {
return;
}

pasteBlock( rawHandler( { HTML: currentClipboard } )[ 0 ] );
pasteBlock( rawHandler( { HTML: clipboard } )[ 0 ] );
}

function isPasteEnabled() {
const clipboardBlock =
currentClipboard && rawHandler( { HTML: currentClipboard } )[ 0 ];
if ( ! clipboard ) {
return false;
}

const clipboardBlock = rawHandler( { HTML: clipboard } )[ 0 ];

return (
clipboardBlock &&
Expand Down
3 changes: 1 addition & 2 deletions packages/block-editor/src/components/inserter/menu.native.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,9 +120,8 @@ export class InserterMenu extends Component {
getBlockType,
} = this.props;

const { current: currentClipboard } = clipboard;
const clipboardBlock =
currentClipboard && rawHandler( { HTML: currentClipboard } )[ 0 ];
clipboard && rawHandler( { HTML: clipboard } )[ 0 ];
const shouldAddClipboardBlock =
clipboardBlock &&
canInsertBlockType( clipboardBlock.name, destinationRootClientId );
Expand Down
6 changes: 3 additions & 3 deletions packages/components/src/mobile/clipboard/index.native.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
/**
* WordPress dependencies
*/
import { createContext, useCallback, useRef } from '@wordpress/element';
import { createContext, useCallback, useState } from '@wordpress/element';

export const ClipboardContext = createContext( {} );
const { Provider, Consumer } = ClipboardContext;
export { Consumer as ClipboardConsumer };

export default function ClipboardProvider( { children } ) {
const clipboard = useRef();
const [ clipboard, setClipboard ] = useState();
const updateClipboard = useCallback( ( clipboardUpdate ) => {
clipboard.current = clipboardUpdate;
setClipboard( clipboardUpdate );
}, [] );

return (
Expand Down

0 comments on commit d163761

Please sign in to comment.