Skip to content

Commit

Permalink
Mobile - Move Clipboard context/provider into a singleton
Browse files Browse the repository at this point in the history
  • Loading branch information
Gerardo Pacheco committed Dec 29, 2020
1 parent 0bc39c9 commit b71e852
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 87 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,12 @@ import { partial, first, castArray, last, compact } from 'lodash';
/**
* WordPress dependencies
*/
import { ClipboardContext, ToolbarButton, Picker } from '@wordpress/components';
import {
getClipboard,
setClipboard,
ToolbarButton,
Picker,
} from '@wordpress/components';
import {
getBlockType,
getDefaultBlockName,
Expand All @@ -19,7 +24,7 @@ import { __, sprintf } from '@wordpress/i18n';
import { withDispatch, withSelect } from '@wordpress/data';
import { withInstanceId, compose } from '@wordpress/compose';
import { moreHorizontalMobile } from '@wordpress/icons';
import { useRef, useContext } from '@wordpress/element';
import { useRef } from '@wordpress/element';
/**
* Internal dependencies
*/
Expand Down Expand Up @@ -49,7 +54,7 @@ const BlockActionsMenu = ( {
} ) => {
const pickerRef = useRef();
const moversOptions = { keys: [ 'icon', 'actionTitle' ] };
const { clipboard, updateClipboard } = useContext( ClipboardContext );
const clipboard = getClipboard();

const {
actionTitle: {
Expand Down Expand Up @@ -162,15 +167,15 @@ const BlockActionsMenu = ( {
break;
case copyButtonOption.value:
const copyBlock = getBlocksByClientId( selectedBlockClientId );
updateClipboard( serialize( copyBlock ) );
setClipboard( serialize( copyBlock ) );
createSuccessNotice(
// translators: displayed right after the block is copied.
__( 'Block copied' )
);
break;
case cutButtonOption.value:
const cutBlock = getBlocksByClientId( selectedBlockClientId );
updateClipboard( serialize( cutBlock ) );
setClipboard( serialize( cutBlock ) );
removeBlocks( selectedBlockClientId );
createSuccessNotice(
// translators: displayed right after the block is cut.
Expand Down
87 changes: 40 additions & 47 deletions packages/block-editor/src/components/inserter/menu.native.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import {
BottomSheet,
BottomSheetConsumer,
InserterButton,
ClipboardConsumer,
getClipboard,
} from '@wordpress/components';

/**
Expand Down Expand Up @@ -116,14 +116,15 @@ export class InserterMenu extends Component {
this.setState( { numberOfColumns, itemWidth, maxWidth } );
}

getItems( clipboard ) {
getItems() {
const {
items,
canInsertBlockType,
destinationRootClientId,
getBlockType,
} = this.props;

const clipboard = getClipboard();
const clipboardBlock =
clipboard && rawHandler( { HTML: clipboard } )[ 0 ];
const shouldAddClipboardBlock =
Expand Down Expand Up @@ -163,52 +164,44 @@ export class InserterMenu extends Component {
const { numberOfColumns } = this.state;

return (
<ClipboardConsumer>
{ ( { clipboard } ) => (
<BottomSheet
isVisible={ true }
onClose={ this.onClose }
hideHeader
hasNavigation
>
<TouchableHighlight accessible={ false }>
<BottomSheetConsumer>
{ ( { listProps, safeAreaBottomInset } ) => (
<FlatList
onLayout={ this.onLayout }
key={ `InserterUI-${ numberOfColumns }` } //re-render when numberOfColumns changes
keyboardShouldPersistTaps="always"
numColumns={ numberOfColumns }
data={ this.getItems( clipboard ) }
ItemSeparatorComponent={ () => (
<TouchableWithoutFeedback
accessible={ false }
>
<View
style={
styles.rowSeparator
}
/>
</TouchableWithoutFeedback>
) }
keyExtractor={ ( item ) => item.name }
renderItem={ this.renderItem }
{ ...listProps }
contentContainerStyle={ [
...listProps.contentContainerStyle,
{
paddingBottom:
safeAreaBottomInset ||
styles.list.paddingBottom,
},
] }
/>
<BottomSheet
isVisible={ true }
onClose={ this.onClose }
hideHeader
hasNavigation
>
<TouchableHighlight accessible={ false }>
<BottomSheetConsumer>
{ ( { listProps, safeAreaBottomInset } ) => (
<FlatList
onLayout={ this.onLayout }
key={ `InserterUI-${ numberOfColumns }` } //re-render when numberOfColumns changes
keyboardShouldPersistTaps="always"
numColumns={ numberOfColumns }
data={ this.getItems() }
ItemSeparatorComponent={ () => (
<TouchableWithoutFeedback
accessible={ false }
>
<View style={ styles.rowSeparator } />
</TouchableWithoutFeedback>
) }
</BottomSheetConsumer>
</TouchableHighlight>
</BottomSheet>
) }
</ClipboardConsumer>
keyExtractor={ ( item ) => item.name }
renderItem={ this.renderItem }
{ ...listProps }
contentContainerStyle={ [
...listProps.contentContainerStyle,
{
paddingBottom:
safeAreaBottomInset ||
styles.list.paddingBottom,
},
] }
/>
) }
</BottomSheetConsumer>
</TouchableHighlight>
</BottomSheet>
);
}
}
Expand Down
6 changes: 1 addition & 5 deletions packages/components/src/index.native.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,11 +81,7 @@ export { default as LinkSettingsNavigation } from './mobile/link-settings/link-s
export { default as Image, IMAGE_DEFAULT_FOCAL_POINT } from './mobile/image';
export { default as ImageEditingButton } from './mobile/image/image-editing-button';
export { default as InserterButton } from './mobile/inserter-button';
export {
default as ClipboardProvider,
ClipboardContext,
ClipboardConsumer,
} from './mobile/clipboard';
export { setClipboard, getClipboard } from './mobile/clipboard';

// Utils
export { colorsUtils } from './mobile/color-settings/utils';
Expand Down
33 changes: 15 additions & 18 deletions packages/components/src/mobile/clipboard/index.native.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,18 @@
/**
* WordPress dependencies
*/
import { createContext, useCallback, useState } from '@wordpress/element';
const createClipboard = () => {
let currentClipboard;

export const ClipboardContext = createContext( {} );
const { Provider, Consumer } = ClipboardContext;
export { Consumer as ClipboardConsumer };
const setClipboard = ( clipboard ) => {
currentClipboard = clipboard;
};

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

return (
<Provider value={ { clipboard, updateClipboard } }>
{ children }
</Provider>
);
}
return {
setClipboard,
getClipboard,
};
};

const clipboard = createClipboard();

export const { setClipboard, getClipboard } = clipboard;
22 changes: 10 additions & 12 deletions packages/edit-post/src/editor.native.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import {
import { withDispatch, withSelect } from '@wordpress/data';
import { compose } from '@wordpress/compose';
import { subscribeSetFocusOnTitle } from '@wordpress/react-native-bridge';
import { SlotFillProvider, ClipboardProvider } from '@wordpress/components';
import { SlotFillProvider } from '@wordpress/components';
import { Preview } from '@wordpress/block-editor';

/**
Expand Down Expand Up @@ -151,17 +151,15 @@ class Editor extends Component {

return (
<SlotFillProvider>
<ClipboardProvider>
<EditorProvider
settings={ editorSettings }
post={ normalizedPost }
initialEdits={ initialEdits }
useSubRegistry={ false }
{ ...props }
>
{ this.editorMode( initialHtml, editorMode ) }
</EditorProvider>
</ClipboardProvider>
<EditorProvider
settings={ editorSettings }
post={ normalizedPost }
initialEdits={ initialEdits }
useSubRegistry={ false }
{ ...props }
>
{ this.editorMode( initialHtml, editorMode ) }
</EditorProvider>
</SlotFillProvider>
);
}
Expand Down

0 comments on commit b71e852

Please sign in to comment.