Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
b795e9c
move default template types and template part areas to REST API
gigitux Oct 25, 2024
61b90b5
fix logic
gigitux Oct 25, 2024
6aaf160
fix E2E test
gigitux Nov 1, 2024
d141278
move default template types and template part areas to REST API
gigitux Nov 4, 2024
bde1d80
fix error
gigitux Nov 4, 2024
de703b5
remove not necessary file
gigitux Nov 4, 2024
f8962ce
fix naming
gigitux Nov 4, 2024
fc95c3f
remove duplicate code
gigitux Nov 4, 2024
d20b8ad
remove duplicated code
gigitux Nov 5, 2024
d361487
Merge branch 'trunk' of github.com:WordPress/gutenberg into fix/rest-…
gigitux Nov 5, 2024
afe5365
improve logic
gigitux Nov 5, 2024
ac06843
fix naming
gigitux Nov 5, 2024
69c3d6d
fix unit test
gigitux Nov 5, 2024
a5647de
update doc
gigitux Nov 5, 2024
2a04ba1
add unit test for getTemplateInfo function
gigitux Nov 5, 2024
6723634
Merge branch 'trunk' of github.com:WordPress/gutenberg into fix/rest-…
gigitux Nov 14, 2024
7241bad
restore not necessary changes
gigitux Nov 14, 2024
8f47873
fix e2e test
gigitux Nov 14, 2024
6acc708
Merge branch 'trunk' of github.com:WordPress/gutenberg into fix/rest-…
gigitux Nov 25, 2024
7aab231
remove not necessary variable
gigitux Nov 25, 2024
ffdcc1d
replace add_action with add_filter
gigitux Nov 25, 2024
e395bf6
improve readibility code
gigitux Nov 25, 2024
1f47713
make getTemplateInfo private
gigitux Nov 25, 2024
078d2f6
make templateAreas optional
gigitux Nov 25, 2024
fdc291d
add default_template_part_areas and default_template_types
gigitux Nov 25, 2024
b2c0bf5
move code to rest-api.php file
gigitux Nov 25, 2024
8f8649b
Merge branch 'trunk' of github.com:WordPress/gutenberg into fix/rest-…
gigitux Nov 26, 2024
230ba72
remove not used import
gigitux Nov 26, 2024
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
Prev Previous commit
Next Next commit
remove duplicated code
  • Loading branch information
gigitux committed Nov 5, 2024
commit d20b8adf65e4c25ad14c48ba2e733e6f0cb38d78
58 changes: 16 additions & 42 deletions packages/edit-site/src/components/use-edited-entity-record/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,53 +4,15 @@
import { useSelect } from '@wordpress/data';
import { store as coreStore } from '@wordpress/core-data';
import { decodeEntities } from '@wordpress/html-entities';
import { layout } from '@wordpress/icons';
import { getTemplatePartIcon } from '@wordpress/editor';
import { privateApis as editorPrivateApis } from '@wordpress/editor';

/**
* Internal dependencies
*/
import { store as editSiteStore } from '../../store';
import { unlock } from '../../lock-unlock';

const EMPTY_OBJECT = {};

const getInfoTemplate = ( select, template ) => {
const { description, slug, title, area } = template;

const templateTypes =
select( coreStore ).getEntityRecord( 'root', '__unstableBase' )
?.defaultTemplateTypes || EMPTY_OBJECT;

const { title: defaultTitle, description: defaultDescription } =
Object.values( templateTypes ).find( ( type ) => type.slug === slug ) ??
EMPTY_OBJECT;

const templateTitle = typeof title === 'string' ? title : title?.rendered;
const templateDescription =
typeof description === 'string' ? description : description?.raw;

const templateAreas =
select( coreStore ).getEntityRecord( 'root', '__unstableBase' )
?.defaultTemplatePartAreas || [];

const templateAreasWithIcon = templateAreas.map( ( item ) => ( {
...item,
icon: getTemplatePartIcon( item.icon ),
} ) );

const templateIcon =
templateAreasWithIcon.find( ( item ) => area === item.area )?.icon ||
layout;

return {
title:
templateTitle && templateTitle !== slug
? templateTitle
: defaultTitle || slug,
description: templateDescription || defaultDescription,
icon: templateIcon,
};
};
const { getTemplateInfo } = unlock( editorPrivateApis );

export default function useEditedEntityRecord( postType, postId ) {
const { record, title, description, isLoaded, icon } = useSelect(
Expand All @@ -68,7 +30,19 @@ export default function useEditedEntityRecord( postType, postId ) {
usedPostId
);

const templateInfo = getInfoTemplate( select, _record );
const templateAreas =
select( coreStore ).getEntityRecord( 'root', '__unstableBase' )
?.defaultTemplatePartAreas || [];

const templateTypes =
select( coreStore ).getEntityRecord( 'root', '__unstableBase' )
?.defaultTemplateTypes || [];

const templateInfo = getTemplateInfo( {
template: _record,
templateAreas,
templateTypes,
} );

const _isLoaded =
usedPostId &&
Expand Down
4 changes: 4 additions & 0 deletions packages/editor/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -440,6 +440,10 @@ getDerivedStateFromError is used to render a fallback UI after an error has been

> **Deprecated** since 5.3, use `wp.blockEditor.getFontSizeClass` instead.

### getTemplateInfo
Copy link
Contributor

Choose a reason for hiding this comment

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

If this is a private API, why the doc is being added here. It might be that it's exported publicly as well.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good catch! Fixed with 1f47713


Undocumented declaration.

### getTemplatePartIcon

Helper function to retrieve the corresponding icon by name.
Expand Down
23 changes: 16 additions & 7 deletions packages/editor/src/components/document-bar/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import { decodeEntities } from '@wordpress/html-entities';
import { TEMPLATE_POST_TYPES } from '../../store/constants';
import { store as editorStore } from '../../store';
import usePageTypeBadge from '../../utils/pageTypeBadge';
import { getTemplateInfo } from '../../utils';

/** @typedef {import("@wordpress/components").IconType} IconType */

Expand Down Expand Up @@ -60,12 +61,8 @@ export default function DocumentBar( props ) {
templateTitle,
onNavigateToPreviousEntityRecord,
} = useSelect( ( select ) => {
const {
getCurrentPostType,
getCurrentPostId,
getEditorSettings,
__experimentalGetTemplateInfo: getTemplateInfo,
} = select( editorStore );
const { getCurrentPostType, getCurrentPostId, getEditorSettings } =
select( editorStore );
const {
getEditedEntityRecord,
getPostType,
Expand All @@ -78,7 +75,19 @@ export default function DocumentBar( props ) {
_postType,
_postId
);
const _templateInfo = getTemplateInfo( _document );

const templateAreas =
select( coreStore ).getEntityRecord( 'root', '__unstableBase' )
?.defaultTemplatePartAreas || [];

const templateTypes =
select( coreStore ).getEntityRecord( 'root', '__unstableBase' )
?.defaultTemplateTypes || [];
const _templateInfo = getTemplateInfo( {
templateAreas,
templateTypes,
template: _document,
} );
const _postTypeLabel = getPostType( _postType )?.labels?.singular_name;

return {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { decodeEntities } from '@wordpress/html-entities';
*/
import { store as editorStore } from '../../store';
import { unlock } from '../../lock-unlock';
import { getTemplateInfo } from '../../utils';

export default function EntityRecordItem( { record, checked, onChange } ) {
const { name, kind, title, key } = record;
Expand All @@ -33,11 +34,21 @@ export default function EntityRecordItem( { record, checked, onChange } ) {
name,
key
);

const templateAreas =
select( coreStore ).getEntityRecord( 'root', '__unstableBase' )
?.defaultTemplatePartAreas || [];

const templateTypes =
select( coreStore ).getEntityRecord( 'root', '__unstableBase' )
?.defaultTemplateTypes || [];

return {
entityRecordTitle:
select( editorStore ).__experimentalGetTemplateInfo(
template
).title,
entityRecordTitle: getTemplateInfo( {
template,
templateAreas,
templateTypes,
} ).title,
Copy link
Member

@Mamaduka Mamaduka Nov 15, 2024

Choose a reason for hiding this comment

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

This new getTemplateInfo util requires template parts area data just to give us a template title. When working with templates, we can probably pass an empty array. What do you think?

If we decide to go in this direction, let's update all other similar occurrences.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think that you're right! I make templateAreas an optional parameter: 078d2f6

hasPostMetaChanges: unlock(
select( editorStore )
).hasPostMetaChanges( name, key ),
Expand Down
3 changes: 2 additions & 1 deletion packages/editor/src/private-apis.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import {
GlobalStylesProvider,
} from './components/global-styles-provider';
import { registerCoreBlockBindingsSources } from './bindings/api';
import { getTemplateInfo } from './utils';

const { store: interfaceStore, ...remainingInterfaceApis } = interfaceApis;

Expand All @@ -44,7 +45,7 @@ lock( privateApis, {
ViewMoreMenuGroup,
ResizableEditor,
registerCoreBlockBindingsSources,

getTemplateInfo,
// This is a temporary private API while we're updating the site editor to use EditorProvider.
interfaceStore,
...remainingInterfaceApis,
Expand Down
40 changes: 7 additions & 33 deletions packages/editor/src/store/selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import { addQueryArgs, cleanForSlug } from '@wordpress/url';
import { createSelector, createRegistrySelector } from '@wordpress/data';
import deprecated from '@wordpress/deprecated';
import { Platform } from '@wordpress/element';
import { layout } from '@wordpress/icons';
import { store as blockEditorStore } from '@wordpress/block-editor';
import { store as coreStore } from '@wordpress/core-data';
import { store as preferencesStore } from '@wordpress/preferences';
Expand All @@ -29,6 +28,7 @@ import {
import { getPostRawValue } from './reducer';
import { getTemplatePartIcon } from '../utils/get-template-part-icon';
import { unlock } from '../lock-unlock';
import { getTemplateInfo } from '../utils';

/**
* Shared reference to an empty object for cases where it is important to avoid
Expand Down Expand Up @@ -1799,45 +1799,19 @@ export const __experimentalGetTemplateInfo = createRegistrySelector(
return EMPTY_OBJECT;
}

const { description, slug, title, area } = template;

const templateTypes =
select( coreStore ).getEntityRecord( 'root', '__unstableBase' )
?.defaultTemplateTypes || EMPTY_OBJECT;

const { title: defaultTitle, description: defaultDescription } =
Object.values( templateTypes ).find(
( type ) => type.slug === slug
) ?? EMPTY_OBJECT;

const templateTitle =
typeof title === 'string' ? title : title?.rendered;
const templateDescription =
typeof description === 'string'
? description
: description?.raw;
?.defaultTemplateTypes || [];

const templateAreas =
select( coreStore ).getEntityRecord( 'root', '__unstableBase' )
?.defaultTemplatePartAreas || [];

const templateAreasWithIcon = templateAreas.map( ( item ) => ( {
...item,
icon: getTemplatePartIcon( item.icon ),
} ) );

const templateIcon =
templateAreasWithIcon.find( ( item ) => area === item.area )
?.icon || layout;

return {
title:
templateTitle && templateTitle !== slug
? templateTitle
: defaultTitle || slug,
description: templateDescription || defaultDescription,
icon: templateIcon,
};
return getTemplateInfo( {
template,
templateAreas,
templateTypes,
} );
} )
);

Expand Down
44 changes: 44 additions & 0 deletions packages/editor/src/utils/get-template-info.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/**
* WordPress dependencies
*/
import { layout } from '@wordpress/icons';
/**
* Internal dependencies
*/
import { getTemplatePartIcon } from '.';

const EMPTY_OBJECT = {};

export const getTemplateInfo = ( {
templateTypes,
templateAreas,
template,
} ) => {
const { description, slug, title, area } = template;

const { title: defaultTitle, description: defaultDescription } =
Object.values( templateTypes ).find( ( type ) => type.slug === slug ) ??
EMPTY_OBJECT;

const templateTitle = typeof title === 'string' ? title : title?.rendered;
const templateDescription =
typeof description === 'string' ? description : description?.raw;

const templateAreasWithIcon = templateAreas.map( ( item ) => ( {
...item,
icon: getTemplatePartIcon( item.icon ),
} ) );

const templateIcon =
templateAreasWithIcon.find( ( item ) => area === item.area )?.icon ||
layout;

return {
title:
templateTitle && templateTitle !== slug
? templateTitle
: defaultTitle || slug,
description: templateDescription || defaultDescription,
icon: templateIcon,
};
};
1 change: 1 addition & 0 deletions packages/editor/src/utils/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ import mediaUpload from './media-upload';
export { mediaUpload };
export { cleanForSlug } from './url.js';
export { getTemplatePartIcon } from './get-template-part-icon';
Copy link
Contributor

Choose a reason for hiding this comment

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

It's funny that we already have a getTemplatePartIcon. It must be something that is very similar no?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

They are the same 😁, but I don't think that we can import getTemplatePartIcon from editor package in block-library package.

Copy link
Contributor

Choose a reason for hiding this comment

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

yep, I think most of these belong in core-data but let's not focus on that for now.

export { getTemplateInfo } from './get-template-info';