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

Don't allow multiple CTA / attachment block to be pasted. #3601

Merged
merged 6 commits into from
Oct 23, 2019
Merged
Show file tree
Hide file tree
Changes from 3 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
Original file line number Diff line number Diff line change
Expand Up @@ -13,30 +13,27 @@ import { withDispatch, useSelect, useDispatch } from '@wordpress/data';
/**
* Internal dependencies
*/
import { copyTextToClipBoard, ensureAllowedBlocksOnPaste, isPageBlock } from '../../helpers';
import { copyTextToClipBoard, isPageBlock, useIsBlockAllowedOnPage, displayPasteError } from '../../helpers';

function CopyPasteHandler( { children, onCopy, clientId, isSelected } ) {
const {
isFirstPage,
canUserUseUnfilteredHTML,
getCopiedMarkupState,
} = useSelect(
( select ) => {
const {
getBlockOrder,
getSettings,
} = select( 'core/block-editor' );
const { getSettings } = select( 'core/block-editor' );
const { __experimentalCanUserUseUnfilteredHTML } = getSettings();
const { getCopiedMarkup } = select( 'amp/story' );
return {
isFirstPage: getBlockOrder().indexOf( clientId ) === 0,
canUserUseUnfilteredHTML: __experimentalCanUserUseUnfilteredHTML,
getCopiedMarkupState: getCopiedMarkup,
};
}, [ clientId ]
}, [ ]
);

const { insertBlocks } = useDispatch( 'core/block-editor' );
const { insertBlock } = useDispatch( 'core/block-editor' );
const { createErrorNotice } = useDispatch( 'core/notices' );
const isBlockAllowedOnPage = useIsBlockAllowedOnPage();

const onPaste = ( event ) => {
// Ignore if the Page is not the selected page.
Expand Down Expand Up @@ -75,9 +72,17 @@ function CopyPasteHandler( { children, onCopy, clientId, isSelected } ) {
canUserUseUnfilteredHTML,
} );

if ( content.length > 0 ) {
insertBlocks( ensureAllowedBlocksOnPaste( content, clientId, isFirstPage ), null, clientId );
if ( ! clientId || ! content.length ) {
return;
}

content.forEach( ( pastedBlock ) => {
if ( isBlockAllowedOnPage( pastedBlock.name, clientId ) ) {
insertBlock( pastedBlock, null, clientId );
} else {
displayPasteError( pastedBlock.name, createErrorNotice );
}
} );
};

return (
Expand Down
31 changes: 19 additions & 12 deletions assets/src/stories-editor/components/context-menu/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,7 @@ import { useDispatch, useSelect } from '@wordpress/data';
*/
import './edit.css';
import {
copyTextToClipBoard,
ensureAllowedBlocksOnPaste,
copyTextToClipBoard, displayPasteError,
isPageBlock,
useIsBlockAllowedOnPage,
useMoveBlockToPage,
Expand Down Expand Up @@ -63,10 +62,11 @@ const ContextMenu = ( props ) => {
const {
removeBlock,
insertBlock,
insertBlocks,
updateBlockAttributes,
} = useDispatch( 'core/block-editor' );

const { createErrorNotice } = useDispatch( 'core/notices' );

const { setCopiedMarkup } = useDispatch( 'amp/story' );

const { __experimentalCanUserUseUnfilteredHTML: canUserUseUnfilteredHTML, isRTL } = getSettings();
Expand Down Expand Up @@ -109,17 +109,24 @@ const ContextMenu = ( props ) => {
return;
}

const isFirstPage = getBlockOrder().indexOf( pageClientId ) === 0;
insertBlocks( ensureAllowedBlocksOnPaste( content, pageClientId, isFirstPage ), null, pageClientId ).then( ( { blocks } ) => {
for ( const block of blocks ) {
if ( ALLOWED_MOVABLE_BLOCKS.includes( block.name ) ) {
updateBlockAttributes( block.clientId, {
positionTop: insidePercentageY,
positionLeft: insidePercentageX,
content.forEach( ( pastedBlock ) => {
if ( isBlockAllowedOnPage( pastedBlock.name, pageClientId ) ) {
insertBlock( pastedBlock, null, pageClientId ).then( ( { blocks } ) => {
blocks.forEach( ( block ) => {
if ( ALLOWED_MOVABLE_BLOCKS.includes( block.name ) ) {
updateBlockAttributes( block.clientId, {
positionTop: insidePercentageY,
positionLeft: insidePercentageX,
} );
}
} );
}
} ).catch( () => {
displayPasteError( pastedBlock.name, createErrorNotice );
} );
} else {
displayPasteError( pastedBlock.name, createErrorNotice );
}
} ).catch( () => {} );
} );
};

const cutBlock = ( clientId ) => {
Expand Down
28 changes: 28 additions & 0 deletions assets/src/stories-editor/helpers/displayPasteError.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/**
* WordPress dependencies
*/
import { getBlockType } from '@wordpress/blocks';
import { __, sprintf } from '@wordpress/i18n';

/**
* Helper function to display a paste error message
*
* @param {string} name Name of block.
* @param {Function} createErrorNotice Bring in dispatchable createErrorNotice function.
*/
const displayPasteError = ( name, createErrorNotice ) => {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Instead of having to pass createErrorNotice, couldn't this just be turned into a custom React hook?

const useDisplayPasteError = ( name ) => {
	const { createErrorNotice } = useDispatch( 'core/notices' );
	// ...
}

In assets/src/stories-editor/components/context-menu/index.js you could then do:

const displayPasteError = useDisplayPasteError( pastedBlock.name );
displayPasteError();

const blockType = getBlockType( name );
const removeMessage = sprintf(
// translators: %s: Type of block (i.e. Text, Image etc)
__( 'Unable to paste %s block.', 'amp' ),
blockType.title
);
createErrorNotice(
removeMessage,
{
type: 'snackbar',
isDismissible: true,
}
);
};
export default displayPasteError;
40 changes: 0 additions & 40 deletions assets/src/stories-editor/helpers/ensureAllowedBlocksOnPaste.js

This file was deleted.

2 changes: 1 addition & 1 deletion assets/src/stories-editor/helpers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ export { default as processMedia } from './processMedia';
export { default as addVideoAriaLabel } from './addVideoAriaLabel';
export { default as getCallToActionBlock } from './getCallToActionBlock';
export { default as getPageAttachmentBlock } from './getPageAttachmentBlock';
export { default as ensureAllowedBlocksOnPaste } from './ensureAllowedBlocksOnPaste';
export { default as isPageBlock } from './isPageBlock';
export { default as copyTextToClipBoard } from './copyTextToClipBoard';
export { default as getPosterImageFromFileObj } from './getPosterImageFromFileObj';
Expand All @@ -61,3 +60,4 @@ export { default as getRelativeElementPosition } from './getRelativeElementPosit
export { default as isCTABlock } from './isCTABlock';
export { default as useMoveBlockToPage } from './useMoveBlockToPage';
export { default as useIsBlockAllowedOnPage } from './useIsBlockAllowedOnPage';
export { default as displayPasteError } from './displayPasteError';

This file was deleted.