Skip to content

Commit

Permalink
Refactor CopyContentButton as core extension
Browse files Browse the repository at this point in the history
* Use withState & compose, drop class component

* Cache CopyContentButton element for EditorActions filter
  • Loading branch information
mcsf committed Jan 18, 2018
1 parent 0940478 commit 0e6fb4b
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 62 deletions.
51 changes: 0 additions & 51 deletions editor/edit-post/header/copy-content-button/index.js

This file was deleted.

16 changes: 7 additions & 9 deletions editor/edit-post/header/editor-actions/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,17 @@
* WordPress dependencies
*/
import { MenuItemsGroup } from '@wordpress/components';
import { applyFilters } from '@wordpress/hooks';
import { __ } from '@wordpress/i18n';

/**
* Internal dependencies
*/
import CopyContentButton from '../copy-content-button';

export default function EditorActions() {
return (
<MenuItemsGroup className="editor-actions"
const tools = applyFilters( 'editor.EditorActions.tools', [] );
return tools.length ? (
<MenuItemsGroup key="tools"
className="editor-actions"
label={ __( 'Tools' ) }
>
<CopyContentButton />
{ tools }
</MenuItemsGroup>
);
) : null;
}
38 changes: 38 additions & 0 deletions editor/hooks/copy-content/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/**
* WordPress dependencies
*/
import { ClipboardButton, withState } from '@wordpress/components';
import { compose } from '@wordpress/element';
import { query } from '@wordpress/data';
import { addFilter } from '@wordpress/hooks';
import { __ } from '@wordpress/i18n';

function CopyContentButton( { editedPostContent, hasCopied, setState } ) {
return (
<ClipboardButton
text={ editedPostContent }
className="components-menu-items__button"
onCopy={ () => setState( { hasCopied: true } ) }
onFinishCopy={ () => setState( { hasCopied: false } ) }
>
{ hasCopied ?
__( 'Copied!' ) :
__( 'Copy All Content' ) }
</ClipboardButton>
);
}

const Enhanced = compose(
query( ( select ) => ( {
editedPostContent: select( 'core/editor', 'getEditedPostContent' ),
} ) ),
withState( { hasCopied: false } )
)( CopyContentButton );

const buttonElement = <Enhanced key="copy-content-button" />;

addFilter(
'editor.EditorActions.tools',
'core/copy-content/button',
( children ) => [ ...children, buttonElement ]
);
4 changes: 4 additions & 0 deletions editor/hooks/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/**
* Internal dependencies
*/
import './copy-content';
1 change: 1 addition & 0 deletions editor/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { settings as dateSettings } from '@wordpress/date';
/**
* Internal dependencies
*/
import './hooks';
import './assets/stylesheets/main.scss';
import Layout from './edit-post/layout';
import { EditorProvider, ErrorBoundary } from './components';
Expand Down
10 changes: 8 additions & 2 deletions editor/store/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@ import { withRehydratation, loadAndPersist } from './persist';
import enhanceWithBrowserSize from './mobile';
import applyMiddlewares from './middlewares';
import { BREAK_MEDIUM } from './constants';
import { getEditedPostTitle } from './selectors';
import {
getEditedPostContent,
getEditedPostTitle,
} from './selectors';

/**
* Module Constants
Expand All @@ -26,6 +29,9 @@ const store = applyMiddlewares(
loadAndPersist( store, 'preferences', STORAGE_KEY, PREFERENCES_DEFAULTS );
enhanceWithBrowserSize( store, BREAK_MEDIUM );

registerSelectors( MODULE_KEY, { getEditedPostTitle } );
registerSelectors( MODULE_KEY, {
getEditedPostContent,
getEditedPostTitle,
} );

export default store;

0 comments on commit 0e6fb4b

Please sign in to comment.