Skip to content

Commit

Permalink
[Block Library - Latest Posts]: Prevent opening the links in editor (#…
Browse files Browse the repository at this point in the history
…40593)

* use css to make them unclicable

* show toaster with warning

* update copy

* announce once per block
  • Loading branch information
ntsekouras committed May 6, 2022
1 parent 9e2c238 commit df8647c
Showing 1 changed file with 27 additions and 2 deletions.
29 changes: 27 additions & 2 deletions packages/block-library/src/latest-posts/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,11 @@ import {
useBlockProps,
store as blockEditorStore,
} from '@wordpress/block-editor';
import { useSelect } from '@wordpress/data';
import { useSelect, useDispatch } from '@wordpress/data';
import { pin, list, grid } from '@wordpress/icons';
import { store as coreStore } from '@wordpress/core-data';
import { store as noticeStore } from '@wordpress/notices';
import { useInstanceId } from '@wordpress/compose';

/**
* Internal dependencies
Expand Down Expand Up @@ -66,6 +68,7 @@ function getFeaturedImageDetails( post, size ) {
}

export default function LatestPostsEdit( { attributes, setAttributes } ) {
const instanceId = useInstanceId( LatestPostsEdit );
const {
postsToShow,
order,
Expand Down Expand Up @@ -148,6 +151,20 @@ export default function LatestPostsEdit( { attributes, setAttributes } ) {
]
);

// If a user clicks to a link prevent redirection and show a warning.
const { createWarningNotice, removeNotice } = useDispatch( noticeStore );
let noticeId;
const showRedirectionPreventedNotice = ( event ) => {
event.preventDefault();
// Remove previous warning if any, to show one at a time per block.
removeNotice( noticeId );
noticeId = `block-library/core/latest-posts/redirection-prevented/${ instanceId }`;
createWarningNotice( __( 'Links are disabled in the editor.' ), {
id: noticeId,
type: 'snackbar',
} );
};

const imageSizeOptions = imageSizes
.filter( ( { slug } ) => slug !== 'full' )
.map( ( { name, slug } ) => ( {
Expand Down Expand Up @@ -466,7 +483,11 @@ export default function LatestPostsEdit( { attributes, setAttributes } ) {
.join( ' ' ) }
{ /* translators: excerpt truncation character, default … */ }
{ __( ' … ' ) }
<a href={ post.link } rel="noopener noreferrer">
<a
href={ post.link }
rel="noopener noreferrer"
onClick={ showRedirectionPreventedNotice }
>
{ __( 'Read more' ) }
</a>
</>
Expand All @@ -483,6 +504,9 @@ export default function LatestPostsEdit( { attributes, setAttributes } ) {
className="wp-block-latest-posts__post-title"
href={ post.link }
rel="noreferrer noopener"
onClick={
showRedirectionPreventedNotice
}
>
{ featuredImage }
</a>
Expand All @@ -501,6 +525,7 @@ export default function LatestPostsEdit( { attributes, setAttributes } ) {
}
: undefined
}
onClick={ showRedirectionPreventedNotice }
>
{ ! titleTrimmed ? __( '(no title)' ) : null }
</a>
Expand Down

0 comments on commit df8647c

Please sign in to comment.