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

[Fields] Migrate store and actions from editor package to fields package #65261

Merged
merged 9 commits into from
Sep 12, 2024
Merged
Show file tree
Hide file tree
Changes from 7 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
46 changes: 44 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions packages/editor/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
"@wordpress/deprecated": "file:../deprecated",
"@wordpress/dom": "file:../dom",
"@wordpress/element": "file:../element",
"@wordpress/fields": "file:../fields",
"@wordpress/hooks": "file:../hooks",
"@wordpress/html-entities": "file:../html-entities",
"@wordpress/i18n": "file:../i18n",
Expand Down
18 changes: 10 additions & 8 deletions packages/editor/src/dataviews/store/private-actions.ts
Copy link
Contributor Author

Choose a reason for hiding this comment

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

The deleted code has been migrated to the @wordpress/fields package

Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,24 @@ import { doAction } from '@wordpress/hooks';
/**
* Internal dependencies
*/
import deletePost from '../actions/delete-post';
import duplicatePattern from '../actions/duplicate-pattern';
import duplicateTemplatePart from '../actions/duplicate-template-part';
import exportPattern from '../actions/export-pattern';
import resetPost from '../actions/reset-post';
import trashPost from '../actions/trash-post';
import permanentlyDeletePost from '../actions/permanently-delete-post';
import renamePost from '../actions/rename-post';
import reorderPage from '../actions/reorder-page';
import restorePost from '../actions/restore-post';
import type { PostType } from '../types';
import { store as editorStore } from '../../store';
import { unlock } from '../../lock-unlock';
import duplicatePost from '../actions/duplicate-post';
import viewPostRevisions from '../actions/view-post-revisions';
import viewPost from '../actions/view-post';
import {
viewPost,
viewPostRevisions,
duplicatePost,
duplicatePattern,
reorderPage,
exportPattern,
permanentlyDeletePost,
Copy link
Contributor

Choose a reason for hiding this comment

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

If we're going to export actions and fields from the same package, maybe we should consider suffixing these with Action?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah, my idea is to have a structure like this Action.

const duplicatePost: Action< BasePost > = {
id: 'duplicate-post',

For instance, the duplicatePost action will be duplicateBasePostAction.

Copy link
Member

Choose a reason for hiding this comment

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

I'm a bit confused about why this exports actions. How you all envision the wordpress/fields package? If it's about fields, it should only export fields (see suggestion). If it's about dumping every WordPress-related thing we have in DataViews, wouldn't it be best to use a name that communicates that better?

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 the @wordpress/fields will expose all the components/actions necessary for WordPress Core - we discussed this here.

} from '@wordpress/fields';
import deletePost from '../actions/delete-post';

export function registerEntityAction< Item >(
kind: string,
Expand Down
1 change: 1 addition & 0 deletions packages/editor/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
{ "path": "../deprecated" },
{ "path": "../dom" },
{ "path": "../element" },
{ "path": "../fields" },
{ "path": "../hooks" },
{ "path": "../html-entities" },
{ "path": "../i18n" },
Expand Down
48 changes: 47 additions & 1 deletion packages/fields/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,53 @@ npm install @wordpress/fields --save

<!-- START TOKEN(Autogenerated API docs) -->

Nothing to document.
### duplicatePattern

Undocumented declaration.
Copy link
Contributor

Choose a reason for hiding this comment

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

Would be cool to add some placeholder JS Docs for these to avoid the "Undocumented declaration". We can definitely do that in a separate PR though.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

After wrapped up the migration, I will:

  • add JSDocs
  • update file name and action name (as I described here)


### duplicatePost

Undocumented declaration.

### duplicatePostNative

Undocumented declaration.

### exportPattern

Undocumented declaration.

### exportPatternNative

Undocumented declaration.

### orderField

Undocumented declaration.

### permanentlyDeletePost

Undocumented declaration.

### reorderPage

Undocumented declaration.

### reorderPageNative

Undocumented declaration.

### titleField

Undocumented declaration.

### viewPost

Undocumented declaration.

### viewPostRevisions

Undocumented declaration.

<!-- END TOKEN(Autogenerated API docs) -->

Expand Down
22 changes: 21 additions & 1 deletion packages/fields/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,27 @@
"src/**/*.scss"
],
"dependencies": {
"@babel/runtime": "^7.16.0"
"@babel/runtime": "^7.16.0",
"@wordpress/blob": "file:../blob",
"@wordpress/blocks": "file:../blocks",
"@wordpress/components": "file:../components",
"@wordpress/compose": "file:../compose",
"@wordpress/core-data": "file:../core-data",
"@wordpress/data": "file:../data",
"@wordpress/dataviews": "file:../dataviews",
"@wordpress/element": "file:../element",
"@wordpress/hooks": "file:../hooks",
"@wordpress/html-entities": "file:../html-entities",
"@wordpress/i18n": "file:../i18n",
"@wordpress/icons": "file:../icons",
"@wordpress/notices": "file:../notices",
"@wordpress/patterns": "file:../patterns",
"@wordpress/primitives": "file:../primitives",
"@wordpress/private-apis": "file:../private-apis",
"@wordpress/url": "file:../url",
"@wordpress/warning": "file:../warning",
"change-case": "4.1.2",
"client-zip": "^2.4.5"
},
"peerDependencies": {
"react": "^18.0.0"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ import type { Action } from '@wordpress/dataviews';
/**
* Internal dependencies
*/
import { getItemTitle } from '../../dataviews/actions/utils';
import type { CoreDataError, BasePost } from '../types';
import { titleField } from '../fields';
import { titleField } from '../../fields';
import type { BasePost, CoreDataError } from '../../types';
import { getItemTitle } from '../utils';

const fields = [ titleField ];
const formDuplicateAction = {
Expand Down
5 changes: 5 additions & 0 deletions packages/fields/src/actions/base-post/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export { default as viewPost } from './view-post';
Copy link
Contributor

Choose a reason for hiding this comment

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

Do we need the "base-post" folder?

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 don't have a strong opinion, but I explained why I structured it in this way in the PR description! Happy to revisit it!

Copy link
Contributor

Choose a reason for hiding this comment

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

I like to start flat personally in general but I don't mind if you prefer it this way.

export { default as reorderPage } from './reorder-page';
export { default as reorderPageNative } from './reorder-page.native';
export { default as duplicatePost } from './duplicate-post';
export { default as duplicatePostNative } from './duplicate-post.native';
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ import type { Action, RenderModalProps } from '@wordpress/dataviews';
/**
* Internal dependencies
*/
import type { CoreDataError, BasePost } from '../types';
import { orderField } from '../fields';
import type { CoreDataError, BasePost } from '../../types';
import { orderField } from '../../fields';

const fields = [ orderField ];
const formOrderAction = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import type { Action } from '@wordpress/dataviews';
/**
* Internal dependencies
*/
import type { BasePost } from '../types';
import type { BasePost } from '../../types';

const viewPost: Action< BasePost > = {
id: 'view-post',
Expand Down
2 changes: 2 additions & 0 deletions packages/fields/src/actions/common/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export { default as viewPostRevisions } from './view-post-revisions';
export { default as permanentlyDeletePost } from './permanently-delete-post';
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ import { trash } from '@wordpress/icons';
/**
* Internal dependencies
*/
import { getItemTitle, isTemplateOrTemplatePart } from './utils';
import type { CoreDataError, PostWithPermissions } from '../types';
import { getItemTitle, isTemplateOrTemplatePart } from '../utils';
import type { CoreDataError, PostWithPermissions } from '../../types';

const permanentlyDeletePost: Action< PostWithPermissions > = {
id: 'permanently-delete',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import type { Action } from '@wordpress/dataviews';
/**
* Internal dependencies
*/
import type { Post } from '../types';
import type { Post } from '../../types';

const viewPostRevisions: Action< Post > = {
id: 'view-post-revisions',
Expand Down
3 changes: 3 additions & 0 deletions packages/fields/src/actions/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export * from './base-post';
export * from './common';
export * from './pattern';
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import type { Action } from '@wordpress/dataviews';
* Internal dependencies
*/
import { unlock } from '../../lock-unlock';
import type { Pattern } from '../types';
import type { Pattern } from '../../types';

// Patterns.
const { CreatePatternModalContents, useDuplicatePatternProps } =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ import type { Action } from '@wordpress/dataviews';
/**
* Internal dependencies
*/
import type { Pattern } from '../types';
import { getItemTitle } from './utils';
import type { Pattern } from '../../types';
import { getItemTitle } from '../utils';

function getJsonFromItem( item: Pattern ) {
return JSON.stringify(
Expand Down
3 changes: 3 additions & 0 deletions packages/fields/src/actions/pattern/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export { default as duplicatePattern } from './duplicate-pattern';
export { default as exportPattern } from './export-pattern';
export { default as exportPatternNative } from './export-pattern.native';
66 changes: 66 additions & 0 deletions packages/fields/src/actions/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/**
* WordPress dependencies
*/
import { decodeEntities } from '@wordpress/html-entities';

/**
* Internal dependencies
*/
import type { Post, TemplatePart, Template } from '../types';

export const TEMPLATE_POST_TYPE = 'wp_template';
export const TEMPLATE_PART_POST_TYPE = 'wp_template_part';
export const TEMPLATE_ORIGINS = {
custom: 'custom',
theme: 'theme',
plugin: 'plugin',
};

export function isTemplate( post: Post ): post is Template {
return post.type === TEMPLATE_POST_TYPE;
}

export function isTemplatePart( post: Post ): post is TemplatePart {
return post.type === TEMPLATE_PART_POST_TYPE;
}

export function isTemplateOrTemplatePart(
p: Post
): p is Template | TemplatePart {
return p.type === TEMPLATE_POST_TYPE || p.type === TEMPLATE_PART_POST_TYPE;
}

export function getItemTitle( item: Post ) {
if ( typeof item.title === 'string' ) {
return decodeEntities( item.title );
}
if ( 'rendered' in item.title ) {
return decodeEntities( item.title.rendered );
}
if ( 'raw' in item.title ) {
return decodeEntities( item.title.raw );
}
return '';
}

/**
* Check if a template is removable.
*
* @param template The template entity to check.
* @return Whether the template is removable.
*/
export function isTemplateRemovable( template: Template | TemplatePart ) {
if ( ! template ) {
return false;
}
// In patterns list page we map the templates parts to a different object
// than the one returned from the endpoint. This is why we need to check for
// two props whether is custom or has a theme file.
return (
[ template.source, template.source ].includes(
TEMPLATE_ORIGINS.custom
) &&
! Boolean( template.type === 'wp_template' && template?.plugin ) &&
! template.has_theme_file
);
}
2 changes: 2 additions & 0 deletions packages/fields/src/fields/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export { default as titleField } from './title';
export { default as orderField } from './order';
18 changes: 18 additions & 0 deletions packages/fields/src/fields/order/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/**
* WordPress dependencies
*/
import type { Field } from '@wordpress/dataviews';
import { __ } from '@wordpress/i18n';
/**
* Internal dependencies
*/
import type { BasePost } from '../../types';

const orderField: Field< BasePost > = {
type: 'integer',
id: 'menu_order',
label: __( 'Order' ),
description: __( 'Determines the order of pages.' ),
};

export default orderField;
Loading
Loading