-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
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 ] | ||
); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
/** | ||
* Internal dependencies | ||
*/ | ||
import './copy-content'; |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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'; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
|
@@ -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; |
There was a problem hiding this comment.
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
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.
There was a problem hiding this comment.
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
orconnect
.