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

[Block Library - Post blocks]: Make post blocks uneditable when inside Query block #32505

Merged
merged 5 commits into from
Jun 8, 2021
Merged
Show file tree
Hide file tree
Changes from 4 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
16 changes: 14 additions & 2 deletions packages/block-library/src/post-author/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,21 @@ import { useSelect, useDispatch } from '@wordpress/data';
import { __ } from '@wordpress/i18n';
import { store as coreStore } from '@wordpress/core-data';

function PostAuthorEdit( { isSelected, context, attributes, setAttributes } ) {
/**
* Internal dependencies
*/
import { useIsEditablePostBlock } from '../utils/hooks';

function PostAuthorEdit( {
clientId,
isSelected,
context,
attributes,
setAttributes,
} ) {
const { postType, postId } = context;

const isEditable = useIsEditablePostBlock( clientId );
const { authorId, authorDetails, authors } = useSelect(
( select ) => {
const { getEditedEntityRecord, getUser, getUsers } = select(
Expand Down Expand Up @@ -66,7 +78,7 @@ function PostAuthorEdit( { isSelected, context, attributes, setAttributes } ) {
<>
<InspectorControls>
<PanelBody title={ __( 'Author Settings' ) }>
{ !! authors?.length && (
{ isEditable && !! authors?.length && (
<SelectControl
label={ __( 'Author' ) }
value={ authorId }
Expand Down
38 changes: 35 additions & 3 deletions packages/block-library/src/post-content/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
*/
import { __ } from '@wordpress/i18n';
import { useSelect } from '@wordpress/data';
import { useMemo, RawHTML } from '@wordpress/element';
import {
useBlockProps,
__experimentalUseInnerBlocksProps as useInnerBlocksProps,
Expand All @@ -11,10 +12,29 @@ import {
store as blockEditorStore,
Warning,
} from '@wordpress/block-editor';
import { useEntityBlockEditor } from '@wordpress/core-data';
import { useMemo } from '@wordpress/element';
import { useEntityProp, useEntityBlockEditor } from '@wordpress/core-data';

function Content( { layout, postType, postId } ) {
/**
* Internal dependencies
*/
import { useIsEditablePostBlock } from '../utils/hooks';

function ReadOnlyContent( { postType, postId } ) {
const [ , , content ] = useEntityProp(
'postType',
postType,
'content',
postId
);
const blockProps = useBlockProps();
return (
<div { ...blockProps }>
<RawHTML key="html">{ content?.rendered }</RawHTML>
</div>
);
}

function EditableContent( { layout, postType, postId } ) {
const themeSupportsLayout = useSelect( ( select ) => {
const { getSettings } = select( blockEditorStore );
return getSettings()?.supportsLayout;
Expand Down Expand Up @@ -53,6 +73,16 @@ function Content( { layout, postType, postId } ) {
return <div { ...props } />;
}

function Content( props ) {
const { clientId, postType, postId } = props;
const isEditable = useIsEditablePostBlock( clientId );
return isEditable ? (
<EditableContent { ...props } />
) : (
<ReadOnlyContent postType={ postType } postId={ postId } />
);
}

function Placeholder() {
const blockProps = useBlockProps();
return (
Expand All @@ -76,6 +106,7 @@ function RecursionError() {
}

export default function PostContentEdit( {
clientId,
context: { postId: contextPostId, postType: contextPostType },
attributes,
} ) {
Expand All @@ -95,6 +126,7 @@ export default function PostContentEdit( {
postType={ contextPostType }
postId={ contextPostId }
layout={ layout }
clientId={ clientId }
/>
) : (
<Placeholder />
Expand Down
15 changes: 13 additions & 2 deletions packages/block-library/src/post-date/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,21 @@ import {
import { __, sprintf } from '@wordpress/i18n';
import { edit } from '@wordpress/icons';

export default function PostDateEdit( { attributes, context, setAttributes } ) {
/**
* Internal dependencies
*/
import { useIsEditablePostBlock } from '../utils/hooks';

export default function PostDateEdit( {
clientId,
attributes,
context,
setAttributes,
} ) {
const { textAlign, format, isLink } = attributes;
const { postId, postType } = context;

const isEditable = useIsEditablePostBlock( clientId );
const [ siteFormat ] = useEntityProp( 'root', 'site', 'date_format' );
const [ date, setDate ] = useEntityProp(
'postType',
Expand Down Expand Up @@ -98,7 +109,7 @@ export default function PostDateEdit( { attributes, context, setAttributes } ) {
} }
/>

{ date && (
{ date && isEditable && (
<ToolbarButton
icon={ edit }
title={ __( 'Change Date' ) }
Expand Down
38 changes: 25 additions & 13 deletions packages/block-library/src/post-excerpt/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ import {
import { PanelBody, RangeControl, ToggleControl } from '@wordpress/components';
import { __ } from '@wordpress/i18n';

/**
* Internal dependencies
*/
import { useIsEditablePostBlock } from '../utils/hooks';

function usePostContentExcerpt( wordCount, postId, postType ) {
// Don't destrcuture items from content here, it can be undefined.
const [ , , content ] = useEntityProp(
Expand All @@ -41,11 +46,13 @@ function usePostContentExcerpt( wordCount, postId, postType ) {
}

export default function PostExcerptEditor( {
clientId,
attributes: { textAlign, wordCount, moreText, showMoreOnNewLine },
setAttributes,
isSelected,
context: { postId, postType },
} ) {
const isEditable = useIsEditablePostBlock( clientId );
const [ excerpt, setExcerpt ] = useEntityProp(
'postType',
postType,
Expand Down Expand Up @@ -84,6 +91,23 @@ export default function PostExcerptEditor( {
}
/>
);
const excerptContent = isEditable ? (
<RichText
className={
! showMoreOnNewLine &&
'wp-block-post-excerpt__excerpt is-inline'
}
aria-label={ __( 'Post excerpt text' ) }
value={
excerpt ||
postContentExcerpt ||
( isSelected ? '' : __( 'No post excerpt found' ) )
}
onChange={ setExcerpt }
/>
) : (
excerpt || postContentExcerpt || __( 'No post excerpt found' )
youknowriad marked this conversation as resolved.
Show resolved Hide resolved
);
return (
<>
<BlockControls>
Expand Down Expand Up @@ -119,19 +143,7 @@ export default function PostExcerptEditor( {
</PanelBody>
</InspectorControls>
<div { ...blockProps }>
<RichText
className={
! showMoreOnNewLine &&
'wp-block-post-excerpt__excerpt is-inline'
}
aria-label={ __( 'Post excerpt text' ) }
value={
excerpt ||
postContentExcerpt ||
( isSelected ? '' : __( 'No post excerpt found' ) )
}
onChange={ setExcerpt }
/>
{ excerptContent }
{ ! showMoreOnNewLine && ' ' }
{ showMoreOnNewLine ? (
<p className="wp-block-post-excerpt__more-text">
Expand Down
21 changes: 16 additions & 5 deletions packages/block-library/src/post-featured-image/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ import {
import { __, sprintf } from '@wordpress/i18n';
import { postFeaturedImage } from '@wordpress/icons';

/**
* Internal dependencies
*/
import { useIsEditablePostBlock } from '../utils/hooks';

const ALLOWED_MEDIA_TYPES = [ 'image' ];
const placeholderChip = (
<div className="post-featured-image_placeholder">
Expand All @@ -29,12 +34,14 @@ const placeholderChip = (
);

function PostFeaturedImageDisplay( {
clientId,
attributes: { isLink },
setAttributes,
context: { postId, postType },
noticeUI,
noticeOperations,
} ) {
const isEditable = useIsEditablePostBlock( clientId );
const [ featuredImage, setFeaturedImage ] = useEntityProp(
'postType',
postType,
Expand All @@ -46,6 +53,7 @@ function PostFeaturedImageDisplay( {
featuredImage && select( coreStore ).getMedia( featuredImage ),
[ featuredImage ]
);
const blockProps = useBlockProps();
const onSelectImage = ( value ) => {
if ( value?.id ) {
setFeaturedImage( value.id );
Expand All @@ -56,6 +64,9 @@ function PostFeaturedImageDisplay( {
noticeOperations.createErrorNotice( message );
}
let image;
if ( ! featuredImage && ! isEditable ) {
return <div { ...blockProps }>{ placeholderChip }</div>;
}
if ( ! featuredImage ) {
image = (
<MediaPlaceholder
Expand Down Expand Up @@ -100,8 +111,8 @@ function PostFeaturedImageDisplay( {
/>
</PanelBody>
</InspectorControls>
<BlockControls group="other">
{ !! media && (
{ !! media && isEditable && (
<BlockControls group="other">
<MediaReplaceFlow
mediaId={ featuredImage }
mediaURL={ media.source_url }
Expand All @@ -110,9 +121,9 @@ function PostFeaturedImageDisplay( {
onSelect={ onSelectImage }
onError={ onUploadError }
/>
) }
</BlockControls>
<figure { ...useBlockProps() }>{ image }</figure>
</BlockControls>
) }
<figure { ...blockProps }>{ image }</figure>
</>
);
}
Expand Down
21 changes: 18 additions & 3 deletions packages/block-library/src/post-title/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,16 @@ import { store as coreStore } from '@wordpress/core-data';
* Internal dependencies
*/
import HeadingLevelDropdown from '../heading/heading-level-dropdown';
import { useIsEditablePostBlock } from '../utils/hooks';

export default function PostTitleEdit( {
clientId,
attributes: { level, textAlign, isLink, rel, linkTarget },
setAttributes,
context: { postType, postId },
} ) {
const TagName = 0 === level ? 'p' : 'h' + level;

const isEditable = useIsEditablePostBlock( clientId );
const post = useSelect(
( select ) =>
select( coreStore ).getEditedEntityRecord(
Expand Down Expand Up @@ -60,7 +62,7 @@ export default function PostTitleEdit( {
);

if ( postType && postId ) {
titleElement = (
titleElement = isEditable ? (
<PlainText
tagName={ TagName }
placeholder={ __( 'No Title' ) }
Expand All @@ -73,11 +75,13 @@ export default function PostTitleEdit( {
__experimentalVersion={ 2 }
{ ...( isLink ? {} : blockProps ) }
/>
) : (
<TagName { ...( isLink ? {} : blockProps ) }>{ title }</TagName>
);
}

if ( isLink ) {
titleElement = (
titleElement = isEditable ? (
<TagName { ...blockProps }>
<PlainText
tagName="a"
Expand All @@ -94,6 +98,17 @@ export default function PostTitleEdit( {
__experimentalVersion={ 2 }
/>
</TagName>
) : (
<TagName { ...blockProps }>
<a
href={ link }
target={ linkTarget }
rel={ rel }
onClick={ ( event ) => event.preventDefault() }
>
{ title }
</a>
</TagName>
);
}

Expand Down
30 changes: 30 additions & 0 deletions packages/block-library/src/utils/hooks.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/**
* WordPress dependencies
*/
import { useSelect } from '@wordpress/data';
import { store as blockEditorStore } from '@wordpress/block-editor';

/**
* Internal dependencies
*/
import queryMetaData from '../query/block.json';
const { name: queryBlockName } = queryMetaData;

export function useIsEditablePostBlock( clientId ) {
ntsekouras marked this conversation as resolved.
Show resolved Hide resolved
return useSelect(
( select ) => {
const { getBlockParents, getBlockName } = select(
blockEditorStore
);
const blockParents = getBlockParents( clientId );
const hasQueryParent = blockParents.some(
( parentClientId ) =>
getBlockName( parentClientId ) === queryBlockName
);
return ! hasQueryParent;
},
[ clientId ]
);
}

export default { useIsEditablePostBlock };