Skip to content

Commit

Permalink
fix: ensure compat with site editor
Browse files Browse the repository at this point in the history
  • Loading branch information
g-elwell committed Aug 30, 2024
1 parent c213872 commit 3f0cfcf
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 6 deletions.
1 change: 0 additions & 1 deletion packages/block-library/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@
"@wordpress/date": "file:../date",
"@wordpress/deprecated": "file:../deprecated",
"@wordpress/dom": "file:../dom",
"@wordpress/editor": "file:../editor",
"@wordpress/element": "file:../element",
"@wordpress/escape-html": "file:../escape-html",
"@wordpress/hooks": "file:../hooks",
Expand Down
18 changes: 13 additions & 5 deletions packages/block-library/src/post-template/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import {
} from '@wordpress/block-editor';
import { Spinner, ToolbarGroup } from '@wordpress/components';
import { store as coreStore } from '@wordpress/core-data';
import { store as editorStore } from '@wordpress/editor';
import { list, grid } from '@wordpress/icons';

const TEMPLATE = [
Expand Down Expand Up @@ -111,7 +110,16 @@ export default function PostTemplateEdit( {
( select ) => {
const { getEntityRecords, getTaxonomies } = select( coreStore );
const { getBlocks } = select( blockEditorStore );
const { getCurrentPostId } = select( editorStore );
// @wordpress/block-library should not depend on @wordpress/editor.
// Blocks can be loaded into a *non-post* block editor, so to avoid
// declaring @wordpress/editor as a dependency, we must access its
// store by string.
// getCurrentPostId also returns a string in the site editor and a
// number in the post editor, it's only useful to us if it's a number.
const currentPostId = parseInt(
// eslint-disable-next-line @wordpress/data-no-store-string-literals
select( 'core/editor' )?.getCurrentPostId()
);
const templateCategory =
inherit &&
templateSlug?.startsWith( 'category-' ) &&
Expand Down Expand Up @@ -163,11 +171,11 @@ export default function PostTemplateEdit( {
if ( exclude?.length ) {
query.exclude = exclude;
}
if ( excludeCurrent ) {
if ( excludeCurrent && currentPostId ) {
if ( query.exclude ) {
query.exclude = [ ...query.exclude, getCurrentPostId() ];
query.exclude = [ ...query.exclude, currentPostId ];
} else {
query.exclude = [ getCurrentPostId() ];
query.exclude = [ currentPostId ];
}
}
if ( parents?.length ) {
Expand Down

0 comments on commit 3f0cfcf

Please sign in to comment.