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

Site editor: consolidate constants #54484

Merged
merged 4 commits into from
Sep 18, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
5 changes: 3 additions & 2 deletions packages/edit-site/src/components/add-new-template/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@ import { store as coreStore } from '@wordpress/core-data';
* Internal dependencies
*/
import NewTemplate from './new-template';
import { TEMPLATE_POST_TYPE } from '../../utils/constants';

export default function AddNewTemplate( {
templateType = 'wp_template',
templateType = TEMPLATE_POST_TYPE,
...props
} ) {
const postType = useSelect(
Expand All @@ -22,7 +23,7 @@ export default function AddNewTemplate( {
return null;
}

if ( templateType === 'wp_template' ) {
if ( templateType === TEMPLATE_POST_TYPE ) {
return <NewTemplate { ...props } postType={ postType } />;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@ import { __, sprintf } from '@wordpress/i18n';
import { store as noticesStore } from '@wordpress/notices';
import { privateApis as routerPrivateApis } from '@wordpress/router';

/**
* Internal dependencies
*/
import { TEMPLATE_POST_TYPE } from '../../utils/constants';

/**
* Internal dependencies
*/
Expand Down Expand Up @@ -190,7 +195,7 @@ export default function NewTemplate( {
const { title, description, slug } = template;
const newTemplate = await saveEntityRecord(
'postType',
'wp_template',
TEMPLATE_POST_TYPE,
{
description,
// Slugs need to be strings, so this is for template `404`
Expand Down
15 changes: 12 additions & 3 deletions packages/edit-site/src/components/add-new-template/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@ import { useMemo, useCallback } from '@wordpress/element';
import { __, sprintf } from '@wordpress/i18n';
import { blockMeta, post, archive } from '@wordpress/icons';

/**
* Internal dependencies
*/
import { TEMPLATE_POST_TYPE } from '../../utils/constants';

/**
* @typedef IHasNameAndId
* @property {string|number} id The entity's id.
Expand Down Expand Up @@ -48,9 +53,13 @@ export const mapToIHasNameAndId = ( entities, path ) => {
export const useExistingTemplates = () => {
return useSelect(
( select ) =>
select( coreStore ).getEntityRecords( 'postType', 'wp_template', {
per_page: -1,
} ),
select( coreStore ).getEntityRecords(
'postType',
TEMPLATE_POST_TYPE,
{
per_page: -1,
}
),
[]
);
};
Expand Down
8 changes: 6 additions & 2 deletions packages/edit-site/src/components/block-editor/back-button.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,19 @@ import { privateApis as routerPrivateApis } from '@wordpress/router';
/**
* Internal dependencies
*/
import {
TEMPLATE_PART_POST_TYPE,
NAVIGATION_POST_TYPE,
} from '../../utils/constants';
import { unlock } from '../../lock-unlock';

const { useLocation, useHistory } = unlock( routerPrivateApis );

function BackButton() {
const location = useLocation();
const history = useHistory();
const isTemplatePart = location.params.postType === 'wp_template_part';
const isNavigationMenu = location.params.postType === 'wp_navigation';
const isTemplatePart = location.params.postType === TEMPLATE_PART_POST_TYPE;
const isNavigationMenu = location.params.postType === NAVIGATION_POST_TYPE;
const previousTemplateId = location.state?.fromTemplateId;

const isFocusMode = isTemplatePart || isNavigationMenu;
Expand Down
11 changes: 3 additions & 8 deletions packages/edit-site/src/components/editor/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ import CanvasLoader from '../canvas-loader';
import { unlock } from '../../lock-unlock';
import useEditedEntityRecord from '../use-edited-entity-record';
import { SidebarFixedBottomSlot } from '../sidebar-edit-mode/sidebar-fixed-bottom';
import { POST_TYPE_LABELS, TEMPLATE_POST_TYPE } from '../../utils/constants';

const { BlockRemovalWarningModal } = unlock( blockEditorPrivateApis );

Expand All @@ -58,13 +59,6 @@ const interfaceLabels = {
footer: __( 'Editor footer' ),
};

const typeLabels = {
wp_template: __( 'Template' ),
wp_template_part: __( 'Template Part' ),
wp_block: __( 'Pattern' ),
wp_navigation: __( 'Navigation' ),
};

// Prevent accidental removal of certain blocks, asking the user for
// confirmation.
const blockRemovalRules = {
Expand Down Expand Up @@ -171,7 +165,8 @@ export default function Editor( { isLoading } ) {
// translators: A breadcrumb trail in browser tab. %1$s: title of template being edited, %2$s: type of template (Template or Template Part).
__( '%1$s ‹ %2$s ‹ Editor' ),
getTitle(),
typeLabels[ editedPostType ] ?? typeLabels.wp_template
POST_TYPE_LABELS[ editedPostType ] ??
POST_TYPE_LABELS[ TEMPLATE_POST_TYPE ]
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,11 @@ import { privateApis as routerPrivateApis } from '@wordpress/router';
/**
* Internal dependencies
*/
import { TEMPLATE_PARTS, PATTERNS, SYNC_TYPES, USER_PATTERNS } from './utils';
import {
TEMPLATE_PART_POST_TYPE,
PATTERN_TYPES,
PATTERN_SYNC_TYPES,
} from '../../utils/constants';
import {
useExistingTemplateParts,
getUniqueTemplatePartTitle,
Expand All @@ -23,12 +27,12 @@ import usePatternCategories from '../sidebar-navigation-screen-patterns/use-patt
const { useHistory } = unlock( routerPrivateApis );

function getPatternMeta( item ) {
if ( item.type === PATTERNS ) {
return { wp_pattern_sync_status: SYNC_TYPES.unsynced };
if ( item.type === PATTERN_TYPES.theme ) {
return { wp_pattern_sync_status: PATTERN_SYNC_TYPES.unsynced };
}

const syncStatus = item.patternBlock.wp_pattern_sync_status;
const isUnsynced = syncStatus === SYNC_TYPES.unsynced;
const isUnsynced = syncStatus === PATTERN_SYNC_TYPES.unsynced;

return {
...item.patternBlock.meta,
Expand Down Expand Up @@ -84,9 +88,9 @@ export default function DuplicateMenuItem( {
);

history.push( {
postType: TEMPLATE_PARTS,
postType: TEMPLATE_PART_POST_TYPE,
postId: result?.id,
categoryType: TEMPLATE_PARTS,
categoryType: TEMPLATE_PART_POST_TYPE,
categoryId,
} );

Expand Down Expand Up @@ -148,7 +152,7 @@ export default function DuplicateMenuItem( {

async function createPattern() {
try {
const isThemePattern = item.type === PATTERNS;
const isThemePattern = item.type === PATTERN_TYPES.theme;
const title = sprintf(
/* translators: %s: Existing pattern title */
__( '%s (Copy)' ),
Expand Down Expand Up @@ -184,9 +188,9 @@ export default function DuplicateMenuItem( {
);

history.push( {
categoryType: PATTERNS,
categoryType: PATTERN_TYPES.theme,
categoryId,
postType: USER_PATTERNS,
postType: PATTERN_TYPES.user,
postId: result?.id,
} );

Expand All @@ -206,7 +210,9 @@ export default function DuplicateMenuItem( {
}

const createItem =
item.type === TEMPLATE_PARTS ? createTemplatePart : createPattern;
item.type === TEMPLATE_PART_POST_TYPE
? createTemplatePart
: createPattern;

return <MenuItem onClick={ createItem }>{ label }</MenuItem>;
}
27 changes: 18 additions & 9 deletions packages/edit-site/src/components/page-patterns/grid-item.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,11 @@ import { store as reusableBlocksStore } from '@wordpress/reusable-blocks';
*/
import RenameMenuItem from './rename-menu-item';
import DuplicateMenuItem from './duplicate-menu-item';
import { PATTERNS, TEMPLATE_PARTS, USER_PATTERNS, SYNC_TYPES } from './utils';
import {
PATTERN_TYPES,
TEMPLATE_PART_POST_TYPE,
PATTERN_SYNC_TYPES,
} from '../../utils/constants';
import { store as editSiteStore } from '../../store';
import { useLink } from '../routes/link';

Expand All @@ -54,9 +58,9 @@ function GridItem( { categoryId, item, ...props } ) {
const { createErrorNotice, createSuccessNotice } =
useDispatch( noticesStore );

const isUserPattern = item.type === USER_PATTERNS;
const isNonUserPattern = item.type === PATTERNS;
const isTemplatePart = item.type === TEMPLATE_PARTS;
const isUserPattern = item.type === PATTERN_TYPES.user;
const isNonUserPattern = item.type === PATTERN_TYPES.theme;
const isTemplatePart = item.type === TEMPLATE_PART_POST_TYPE;

const { onClick } = useLink( {
postType: item.type,
Expand Down Expand Up @@ -123,7 +127,8 @@ function GridItem( { categoryId, item, ...props } ) {
if ( ! isUserPattern && templatePartIcons[ categoryId ] ) {
itemIcon = templatePartIcons[ categoryId ];
} else {
itemIcon = item.syncStatus === SYNC_TYPES.full ? symbol : undefined;
itemIcon =
item.syncStatus === PATTERN_SYNC_TYPES.full ? symbol : undefined;
}

const confirmButtonText = hasThemeFile ? __( 'Clear' ) : __( 'Delete' );
Expand All @@ -143,8 +148,12 @@ function GridItem( { categoryId, item, ...props } ) {
// @see https://reakit.io/docs/composite/#performance.
id={ `edit-site-patterns-${ item.name }` }
{ ...props }
onClick={ item.type !== PATTERNS ? onClick : undefined }
aria-disabled={ item.type !== PATTERNS ? 'false' : 'true' }
onClick={
item.type !== PATTERN_TYPES.theme ? onClick : undefined
}
aria-disabled={
item.type !== PATTERN_TYPES.theme ? 'false' : 'true'
}
aria-label={ item.title }
aria-describedby={
ariaDescriptions.length
Expand Down Expand Up @@ -194,7 +203,7 @@ function GridItem( { categoryId, item, ...props } ) {
</Tooltip>
) }
<Flex as="span" gap={ 0 } justify="left">
{ item.type === PATTERNS ? (
{ item.type === PATTERN_TYPES.theme ? (
item.title
) : (
<Heading level={ 5 }>
Expand All @@ -209,7 +218,7 @@ function GridItem( { categoryId, item, ...props } ) {
</Button>
</Heading>
) }
{ item.type === PATTERNS && (
{ item.type === PATTERN_TYPES.theme && (
<Tooltip
position="top center"
text={ __( 'This pattern cannot be edited.' ) }
Expand Down
6 changes: 3 additions & 3 deletions packages/edit-site/src/components/page-patterns/header.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import { useSelect } from '@wordpress/data';
* Internal dependencies
*/
import usePatternCategories from '../sidebar-navigation-screen-patterns/use-pattern-categories';
import { TEMPLATE_PARTS, PATTERNS } from './utils';
import { TEMPLATE_PART_POST_TYPE, PATTERN_TYPES } from '../../utils/constants';

export default function PatternsHeader( {
categoryId,
Expand All @@ -29,13 +29,13 @@ export default function PatternsHeader( {
);

let title, description;
if ( type === TEMPLATE_PARTS ) {
if ( type === TEMPLATE_PART_POST_TYPE ) {
const templatePartArea = templatePartAreas.find(
( area ) => area.area === categoryId
);
title = templatePartArea?.label;
description = templatePartArea?.description;
} else if ( type === PATTERNS ) {
} else if ( type === PATTERN_TYPES.theme ) {
const patternCategory = patternCategories.find(
( category ) => category.name === categoryId
);
Expand Down
6 changes: 3 additions & 3 deletions packages/edit-site/src/components/page-patterns/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { getQueryArgs } from '@wordpress/url';
/**
* Internal dependencies
*/
import { DEFAULT_CATEGORY, DEFAULT_TYPE } from './utils';
import { PATTERN_DEFAULT_CATEGORY, PATTERN_TYPES } from '../../utils/constants';
import Page from '../page';
import PatternsList from './patterns-list';
import usePatternSettings from './use-pattern-settings';
Expand All @@ -18,8 +18,8 @@ const { ExperimentalBlockEditorProvider } = unlock( blockEditorPrivateApis );

export default function PagePatterns() {
const { categoryType, categoryId } = getQueryArgs( window.location.href );
const type = categoryType || DEFAULT_TYPE;
const category = categoryId || DEFAULT_CATEGORY;
const type = categoryType || PATTERN_TYPES.user;
const category = categoryId || PATTERN_DEFAULT_CATEGORY;
const settings = usePatternSettings();

// Wrap everything in a block editor provider.
Expand Down
14 changes: 7 additions & 7 deletions packages/edit-site/src/components/page-patterns/patterns-list.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,23 +27,23 @@ import usePatterns from './use-patterns';
import SidebarButton from '../sidebar-button';
import useDebouncedInput from '../../utils/use-debounced-input';
import { unlock } from '../../lock-unlock';
import { SYNC_TYPES, PATTERNS } from './utils';
import { PATTERN_SYNC_TYPES, PATTERN_TYPES } from '../../utils/constants';
import Pagination from './pagination';

const { useLocation, useHistory } = unlock( routerPrivateApis );

const SYNC_FILTERS = {
all: __( 'All' ),
[ SYNC_TYPES.full ]: __( 'Synced' ),
[ SYNC_TYPES.unsynced ]: __( 'Standard' ),
[ PATTERN_SYNC_TYPES.full ]: __( 'Synced' ),
[ PATTERN_SYNC_TYPES.unsynced ]: __( 'Standard' ),
};

const SYNC_DESCRIPTIONS = {
all: '',
[ SYNC_TYPES.full ]: __(
[ PATTERN_SYNC_TYPES.full ]: __(
'Patterns that are kept in sync across the site.'
),
[ SYNC_TYPES.unsynced ]: __(
[ PATTERN_SYNC_TYPES.unsynced ]: __(
'Patterns that can be changed freely without affecting the site.'
),
};
Expand All @@ -64,7 +64,7 @@ export default function PatternsList( { categoryId, type } ) {
const deferredSyncedFilter = useDeferredValue( syncFilter );

const isUncategorizedThemePatterns =
type === PATTERNS && categoryId === 'uncategorized';
type === PATTERN_TYPES.theme && categoryId === 'uncategorized';

const { patterns, isResolving } = usePatterns(
type,
Expand Down Expand Up @@ -155,7 +155,7 @@ export default function PatternsList( { categoryId, type } ) {
__nextHasNoMarginBottom
/>
</FlexBlock>
{ type === PATTERNS && (
{ type === PATTERN_TYPES.theme && (
<ToggleGroupControl
className="edit-site-patterns__sync-status-filter"
hideLabelFromVision
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import { store as noticesStore } from '@wordpress/notices';
/**
* Internal dependencies
*/
import { TEMPLATE_PARTS } from './utils';
import { TEMPLATE_PART_POST_TYPE } from '../../utils/constants';

export default function RenameMenuItem( { item, onClose } ) {
const [ title, setTitle ] = useState( () => item.title );
Expand All @@ -29,7 +29,7 @@ export default function RenameMenuItem( { item, onClose } ) {
const { createSuccessNotice, createErrorNotice } =
useDispatch( noticesStore );

if ( item.type === TEMPLATE_PARTS && ! item.isCustom ) {
if ( item.type === TEMPLATE_PART_POST_TYPE && ! item.isCustom ) {
return null;
}

Expand All @@ -50,7 +50,7 @@ export default function RenameMenuItem( { item, onClose } ) {
} );

createSuccessNotice(
item.type === TEMPLATE_PARTS
item.type === TEMPLATE_PART_POST_TYPE
? __( 'Template part renamed.' )
: __( 'Pattern renamed.' ),
{
Expand All @@ -59,7 +59,7 @@ export default function RenameMenuItem( { item, onClose } ) {
);
} catch ( error ) {
const fallbackErrorMessage =
item.type === TEMPLATE_PARTS
item.type === TEMPLATE_PART_POST_TYPE
? __(
'An error occurred while reverting the template part.'
)
Expand Down
Loading
Loading