Skip to content

Commit

Permalink
Add: Copy all content to edit site
Browse files Browse the repository at this point in the history
  • Loading branch information
jorgefilipecosta committed Jan 13, 2022
1 parent cef5825 commit eea24a5
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/**
* WordPress dependencies
*/
import { MenuItem } from '@wordpress/components';
import { useSelect, useDispatch } from '@wordpress/data';
import { __ } from '@wordpress/i18n';
import { useCopyToClipboard } from '@wordpress/compose';
import { store as noticesStore } from '@wordpress/notices';
import { store as coreStore } from '@wordpress/core-data';
import { __unstableSerializeAndClean } from '@wordpress/blocks';

/**
* Internal dependencies
*/
import { store as editSiteStore } from '../../../store';

export default function CopyContentMenuItem() {
const { createNotice } = useDispatch( noticesStore );
const getText = useSelect( ( select ) => {
return () => {
const { getEditedPostId, getEditedPostType } = select(
editSiteStore
);
const { getEditedEntityRecord } = select( coreStore );
const record = getEditedEntityRecord(
'postType',
getEditedPostType(),
getEditedPostId()
);
if ( record ) {
if ( typeof record.content === 'function' ) {
return record.content( record );
} else if ( record.blocks ) {
return __unstableSerializeAndClean( record.blocks );
} else if ( record.content ) {
return record.content;
}
}
return '';
};
}, [] );

function onSuccess() {
createNotice( 'info', __( 'All content copied.' ), {
isDismissible: true,
type: 'snackbar',
} );
}

const ref = useCopyToClipboard( getText, onSuccess );

return <MenuItem ref={ ref }>{ __( 'Copy all content' ) }</MenuItem>;
}
2 changes: 2 additions & 0 deletions packages/edit-site/src/components/header/more-menu/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import FeatureToggle from '../feature-toggle';
import ToolsMoreMenuGroup from '../tools-more-menu-group';
import SiteExport from './site-export';
import WelcomeGuideMenuItem from './welcome-guide-menu-item';
import CopyContentMenuItem from './copy-content-menu-item';

const POPOVER_PROPS = {
className: 'edit-site-more-menu__content',
Expand Down Expand Up @@ -91,6 +92,7 @@ export default function MoreMenu() {
{ __( 'Keyboard shortcuts' ) }
</MenuItem>
<WelcomeGuideMenuItem />
<CopyContentMenuItem />
<MenuItem
icon={ external }
role="menuitem"
Expand Down

0 comments on commit eea24a5

Please sign in to comment.