Skip to content

Commit

Permalink
[Fields] Migrate store and actions from editor package to fields pack…
Browse files Browse the repository at this point in the history
…age (#65261)

Co-authored-by: gigitux <gigitux@git.wordpress.org>
Co-authored-by: youknowriad <youknowriad@git.wordpress.org>
  • Loading branch information
3 people committed Sep 12, 2024
1 parent eba5eb7 commit d329bfb
Show file tree
Hide file tree
Showing 30 changed files with 376 additions and 26 deletions.
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
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,
} 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.

### 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
23 changes: 22 additions & 1 deletion packages/fields/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,34 @@
},
"main": "build/index.js",
"module": "build-module/index.js",
"react-native": "src/index",
"types": "build-types",
"sideEffects": [
"build-style/**",
"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';
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

0 comments on commit d329bfb

Please sign in to comment.