Skip to content

Commit

Permalink
pass queryId in context to make post blocks uneditable
Browse files Browse the repository at this point in the history
  • Loading branch information
ntsekouras authored and youknowriad committed Jun 15, 2021
1 parent 0212588 commit 3099a5e
Show file tree
Hide file tree
Showing 14 changed files with 26 additions and 97 deletions.
2 changes: 1 addition & 1 deletion packages/block-library/src/post-author/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
"type": "string"
}
},
"usesContext": [ "postType", "postId" ],
"usesContext": [ "postType", "postId", "queryId" ],
"supports": {
"html": false,
"typography": {
Expand Down
12 changes: 2 additions & 10 deletions packages/block-library/src/post-author/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,21 +19,13 @@ import { useSelect, useDispatch } from '@wordpress/data';
import { __ } from '@wordpress/i18n';
import { store as coreStore } from '@wordpress/core-data';

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

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

const isDescendentOfQueryLoop = useIsDescendentOfQueryLoopBlock( clientId );
const isDescendentOfQueryLoop = !! queryId;
const { authorId, authorDetails, authors } = useSelect(
( select ) => {
const { getEditedEntityRecord, getUser, getUsers } = select(
Expand Down
2 changes: 1 addition & 1 deletion packages/block-library/src/post-content/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"category": "design",
"description": "Displays the contents of a post or page.",
"textdomain": "default",
"usesContext": [ "postId", "postType" ],
"usesContext": [ "postId", "postType", "queryId" ],
"supports": {
"align": [ "wide", "full" ],
"html": false,
Expand Down
23 changes: 6 additions & 17 deletions packages/block-library/src/post-content/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,7 @@ import { useEntityProp, useEntityBlockEditor } from '@wordpress/core-data';
/**
* Internal dependencies
*/
import {
useIsDescendentOfQueryLoopBlock,
useCanEditEntity,
} from '../utils/hooks';
import { useCanEditEntity } from '../utils/hooks';

function ReadOnlyContent( { userCanEdit, postType, postId } ) {
const [ , , content ] = useEntityProp(
Expand Down Expand Up @@ -81,8 +78,8 @@ function EditableContent( { layout, postType, postId } ) {
}

function Content( props ) {
const { clientId, postType, postId } = props;
const isDescendentOfQueryLoop = useIsDescendentOfQueryLoopBlock( clientId );
const { context: { queryId, postType, postId } = {} } = props;
const isDescendentOfQueryLoop = !! queryId;
const userCanEdit = useCanEditEntity(
'root',
'postType',
Expand Down Expand Up @@ -123,11 +120,8 @@ function RecursionError() {
);
}

export default function PostContentEdit( {
clientId,
context: { postId: contextPostId, postType: contextPostType },
attributes,
} ) {
export default function PostContentEdit( { context, attributes } ) {
const { postId: contextPostId, postType: contextPostType } = context;
const { layout = {} } = attributes;
const [ hasAlreadyRendered, RecursionProvider ] = useNoRecursiveRenders(
contextPostId
Expand All @@ -140,12 +134,7 @@ export default function PostContentEdit( {
return (
<RecursionProvider>
{ contextPostId && contextPostType ? (
<Content
postType={ contextPostType }
postId={ contextPostId }
layout={ layout }
clientId={ clientId }
/>
<Content context={ context } layout={ layout } />
) : (
<Placeholder />
) }
Expand Down
2 changes: 1 addition & 1 deletion packages/block-library/src/post-date/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
"default": false
}
},
"usesContext": [ "postId", "postType" ],
"usesContext": [ "postId", "postType", "queryId" ],
"supports": {
"html": false,
"color": {
Expand Down
15 changes: 3 additions & 12 deletions packages/block-library/src/post-date/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,21 +26,12 @@ import {
import { __, sprintf } from '@wordpress/i18n';
import { edit } from '@wordpress/icons';

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

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

const isDescendentOfQueryLoop = useIsDescendentOfQueryLoopBlock( clientId );
const isDescendentOfQueryLoop = !! queryId;
const [ siteFormat ] = useEntityProp( 'root', 'site', 'date_format' );
const [ date, setDate ] = useEntityProp(
'postType',
Expand Down
2 changes: 1 addition & 1 deletion packages/block-library/src/post-excerpt/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
"default": true
}
},
"usesContext": [ "postId", "postType" ],
"usesContext": [ "postId", "postType", "queryId" ],
"supports": {
"html": false,
"color": {
Expand Down
10 changes: 3 additions & 7 deletions packages/block-library/src/post-excerpt/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,7 @@ import { __ } from '@wordpress/i18n';
/**
* Internal dependencies
*/
import {
useIsDescendentOfQueryLoopBlock,
useCanEditEntity,
} from '../utils/hooks';
import { useCanEditEntity } from '../utils/hooks';

function usePostContentExcerpt( wordCount, postId, postType ) {
// Don't destrcuture items from content here, it can be undefined.
Expand All @@ -49,13 +46,12 @@ function usePostContentExcerpt( wordCount, postId, postType ) {
}

export default function PostExcerptEditor( {
clientId,
attributes: { textAlign, wordCount, moreText, showMoreOnNewLine },
setAttributes,
isSelected,
context: { postId, postType },
context: { postId, postType, queryId },
} ) {
const isDescendentOfQueryLoop = useIsDescendentOfQueryLoopBlock( clientId );
const isDescendentOfQueryLoop = !! queryId;
const userCanEdit = useCanEditEntity(
'root',
'postType',
Expand Down
2 changes: 1 addition & 1 deletion packages/block-library/src/post-featured-image/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"default": false
}
},
"usesContext": [ "postId", "postType" ],
"usesContext": [ "postId", "postType", "queryId" ],
"supports": {
"align": [ "left", "right", "center", "wide", "full" ],
"html": false
Expand Down
10 changes: 2 additions & 8 deletions packages/block-library/src/post-featured-image/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,6 @@ import {
import { __, sprintf } from '@wordpress/i18n';
import { postFeaturedImage } from '@wordpress/icons';

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

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

function PostFeaturedImageDisplay( {
clientId,
attributes: { isLink },
setAttributes,
context: { postId, postType },
context: { postId, postType, queryId },
noticeUI,
noticeOperations,
} ) {
const isDescendentOfQueryLoop = useIsDescendentOfQueryLoopBlock( clientId );
const isDescendentOfQueryLoop = !! queryId;
const [ featuredImage, setFeaturedImage ] = useEntityProp(
'postType',
postType,
Expand Down
2 changes: 2 additions & 0 deletions packages/block-library/src/post-template/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ export default function PostTemplateEdit( {
sticky,
inherit,
} = {},
queryId,
queryContext = [ { page: 1 } ],
templateSlug,
displayLayout: { type: layoutType = 'flex', columns = 1 } = {},
Expand Down Expand Up @@ -115,6 +116,7 @@ export default function PostTemplateEdit( {
posts?.map( ( post ) => ( {
postType: post.type,
postId: post.id,
queryId,
} ) ),
[ posts ]
);
Expand Down
2 changes: 1 addition & 1 deletion packages/block-library/src/post-title/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"category": "design",
"description": "Displays the title of a post, page, or any other content-type.",
"textdomain": "default",
"usesContext": [ "postId", "postType" ],
"usesContext": [ "postId", "postType", "queryId" ],
"attributes": {
"textAlign": {
"type": "string"
Expand Down
6 changes: 2 additions & 4 deletions packages/block-library/src/post-title/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,14 @@ import { store as coreStore } from '@wordpress/core-data';
* Internal dependencies
*/
import HeadingLevelDropdown from '../heading/heading-level-dropdown';
import { useIsDescendentOfQueryLoopBlock } from '../utils/hooks';

export default function PostTitleEdit( {
clientId,
attributes: { level, textAlign, isLink, rel, linkTarget },
setAttributes,
context: { postType, postId },
context: { postType, postId, queryId },
} ) {
const TagName = 0 === level ? 'p' : 'h' + level;
const isDescendentOfQueryLoop = useIsDescendentOfQueryLoopBlock( clientId );
const isDescendentOfQueryLoop = !! queryId;
const post = useSelect(
( select ) =>
select( coreStore ).getEditedEntityRecord(
Expand Down
33 changes: 0 additions & 33 deletions packages/block-library/src/utils/hooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,40 +2,8 @@
* WordPress dependencies
*/
import { useSelect } from '@wordpress/data';
import { store as blockEditorStore } from '@wordpress/block-editor';
import { store as coreStore } from '@wordpress/core-data';

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

/**
* Hook that determines if a Post block is descendent
* of Query Loop block. If it is, the block should
* not be rendered in `readonly` mode.
*
* @param {string} clientId The ID of the block to be checked.
* @return {boolean} Whether the block is descendent of Query Loop block.
*/
export function useIsDescendentOfQueryLoopBlock( clientId ) {
return useSelect(
( select ) => {
const { getBlockParents, getBlockName } = select(
blockEditorStore
);
const blockParents = getBlockParents( clientId );
const hasQueryParent = blockParents.some(
( parentClientId ) =>
getBlockName( parentClientId ) === queryBlockName
);
return hasQueryParent;
},
[ clientId ]
);
}

/**
* Returns whether the current user can edit the given entity.
*
Expand All @@ -58,6 +26,5 @@ export function useCanEditEntity( kind, name, key, recordId ) {
}

export default {
useIsDescendentOfQueryLoopBlock,
useCanEditEntity,
};

0 comments on commit 3099a5e

Please sign in to comment.