Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor CopyContentButton as core extension #4549

Merged
merged 1 commit into from
Jan 19, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 );
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Trying to look at this file as a prospective extension author, considering readability, etc. I toyed with

const applyGlobalState = query( ( select ) => ( {} ) );

const applyLocalState = withState( { hasCopied: false } );

const Enhanced = compose(
  applyGlobalState,
  applyLocalState
)( CopyContentButton );

but I wasn't sure it improved things. At some point, more labels (such as function identifiers) may become a hinderance, and this felt slightly awkward.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's always a personal preference and really depends on the particular use case. It is fine as it is because those HOC applied are short. I usually extract variables when you need to scroll the screen to discover that a given code is part of the compose or connect.


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';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should find a place to document the exposed selectors for plugin consumption.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch. I somehow missed that. It would be awesome to automate it using JSDoc :D


/**
* 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;