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 6 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
21 changes: 19 additions & 2 deletions packages/editor/src/components/post-actions/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
*/
import { useDispatch, useSelect } from '@wordpress/data';
import { useMemo, useEffect } from '@wordpress/element';
import { store as fieldsStore } from '@wordpress/fields';

/**
* Internal dependencies
Expand All @@ -14,17 +15,33 @@ export function usePostActions( { postType, onActionPerformed, context } ) {
const { defaultActions } = useSelect(
( select ) => {
const { getEntityActions } = unlock( select( editorStore ) );
const { getEntityActions: getEntityActionsFromFieldsPackage } =
select( fieldsStore );
Copy link
Contributor

Choose a reason for hiding this comment

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

One thing that I just thought about is that "stores" are singletons and using them in "bundled packages" create a lot of issues. For instance if a third-party decides to use the package as well, we'll end up with two stores that don't talk to each other.

This makes me thing that maybe a first step here is to extract the fields and the actions but to leave the store and registration in the "editor" package for now.


return {
defaultActions: getEntityActions( 'postType', postType ),
defaultActions: [
...getEntityActions( 'postType', postType ),
...getEntityActionsFromFieldsPackage(
'postType',
postType
),
],
};
},
[ postType ]
);

const { registerPostTypeActions } = unlock( useDispatch( editorStore ) );
const { registerPostTypeActions: registerPostTypeActionFromFieldsPackage } =
useDispatch( fieldsStore );
useEffect( () => {
registerPostTypeActions( postType );
}, [ registerPostTypeActions, postType ] );
registerPostTypeActionFromFieldsPackage( postType );
}, [
registerPostTypeActions,
postType,
registerPostTypeActionFromFieldsPackage,
] );

return useMemo( () => {
// Filter actions based on provided context. If not provided
Expand Down
27 changes: 0 additions & 27 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,15 @@ 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';

export function registerEntityAction< Item >(
kind: string,
Expand Down Expand Up @@ -90,34 +83,14 @@ export const registerPostTypeActions =
.getCurrentTheme();

const actions = [
postTypeConfig.viewable ? viewPost : undefined,
!! postTypeConfig?.supports?.revisions
? viewPostRevisions
: undefined,
// @ts-ignore
globalThis.IS_GUTENBERG_PLUGIN
? ! [ 'wp_template', 'wp_block', 'wp_template_part' ].includes(
postTypeConfig.slug
) &&
canCreate &&
duplicatePost
: undefined,
postTypeConfig.slug === 'wp_template_part' &&
canCreate &&
currentTheme?.is_block_theme
? duplicateTemplatePart
: undefined,
canCreate && postTypeConfig.slug === 'wp_block'
? duplicatePattern
: undefined,
postTypeConfig.supports?.title ? renamePost : undefined,
postTypeConfig?.supports?.[ 'page-attributes' ]
? reorderPage
: undefined,
postTypeConfig.slug === 'wp_block' ? exportPattern : undefined,
resetPost,
restorePost,
deletePost,
trashPost,
permanentlyDeletePost,
];
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
32 changes: 31 additions & 1 deletion packages/fields/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,37 @@ npm install @wordpress/fields --save

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

Nothing to document.
### orderField

Undocumented declaration.

### store

Store definition for the fields namespace.

_Related_

- <https://github.com/WordPress/gutenberg/blob/HEAD/packages/data/README.md#createReduxStore>

_Type_

- `Object`

### storeConfig

Fields data store configuration.

_Related_

- <https://github.com/WordPress/gutenberg/blob/HEAD/packages/data/README.md#registerStore>

_Type_

- `Object`

### titleField

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 @@ -9,7 +9,7 @@ import type { Action } from '@wordpress/dataviews';
/**
* Internal dependencies
*/
import { unlock } from '../../lock-unlock';
import { unlock } from '../lock-unlock';
import type { Pattern } from '../types';

// Patterns.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import type { Action } from '@wordpress/dataviews';
/**
* Internal dependencies
*/
import { getItemTitle } from '../../dataviews/actions/utils';
import { getItemTitle } from './utils';
import type { CoreDataError, BasePost } from '../types';
import { titleField } from '../fields';

Expand Down
21 changes: 21 additions & 0 deletions packages/fields/src/actions/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/**
* WordPress dependencies
*/
import { decodeEntities } from '@wordpress/html-entities';
/**
* Internal dependencies
*/
import type { Post } from '../types';

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 '';
}
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;
21 changes: 21 additions & 0 deletions packages/fields/src/fields/title/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/**
* WordPress dependencies
*/
import type { Field } from '@wordpress/dataviews';
import { __ } from '@wordpress/i18n';

/**
* Internal dependencies
*/
import type { BasePost } from '../../types';
import { getItemTitle } from '../../actions/utils';

const titleField: Field< BasePost > = {
type: 'text',
id: 'title',
label: __( 'Title' ),
placeholder: __( 'No title' ),
getValue: ( { item } ) => getItemTitle( item ),
};

export default titleField;
2 changes: 2 additions & 0 deletions packages/fields/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from './fields';
export { storeConfig, store } from './store';
9 changes: 9 additions & 0 deletions packages/fields/src/lock-unlock.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/**
* WordPress dependencies
*/
import { __dangerousOptInToUnstableAPIsOnlyForCoreModules } from '@wordpress/private-apis';
export const { lock, unlock } =
__dangerousOptInToUnstableAPIsOnlyForCoreModules(
'I acknowledge private features are not for use in themes or plugins and doing so will break in the next version of WordPress.',
'@wordpress/fields'
);
Loading
Loading