From e406199eceaf14d1c75bab4736ad05238affddb0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Maneiro?= <583546+oandregal@users.noreply.github.com> Date: Fri, 15 Nov 2024 09:53:40 +0100 Subject: [PATCH 1/4] Move usePostFields to wordpress/fields package --- .../src/components/post-edit/index.js | 2 +- .../src/components/post-fields/index.js | 50 -------------- .../src/components/post-fields/style.scss | 3 - .../src/components/post-list/index.js | 2 +- packages/edit-site/src/style.scss | 5 +- packages/fields/README.md | 4 ++ packages/fields/src/fields/index.ts | 1 + .../src/fields/use-post-fields/index.ts | 69 +++++++++++++++++++ 8 files changed, 80 insertions(+), 56 deletions(-) delete mode 100644 packages/edit-site/src/components/post-fields/index.js delete mode 100644 packages/edit-site/src/components/post-fields/style.scss create mode 100644 packages/fields/src/fields/use-post-fields/index.ts diff --git a/packages/edit-site/src/components/post-edit/index.js b/packages/edit-site/src/components/post-edit/index.js index fbff29ed67afa1..6a8d2322b2f87c 100644 --- a/packages/edit-site/src/components/post-edit/index.js +++ b/packages/edit-site/src/components/post-edit/index.js @@ -13,12 +13,12 @@ import { store as coreDataStore } from '@wordpress/core-data'; import { __experimentalVStack as VStack } from '@wordpress/components'; import { useState, useMemo, useEffect } from '@wordpress/element'; import { privateApis as editorPrivateApis } from '@wordpress/editor'; +import { usePostFields } from '@wordpress/fields'; /** * Internal dependencies */ import Page from '../page'; -import usePostFields from '../post-fields'; import { unlock } from '../../lock-unlock'; const { PostCardPanel } = unlock( editorPrivateApis ); diff --git a/packages/edit-site/src/components/post-fields/index.js b/packages/edit-site/src/components/post-fields/index.js deleted file mode 100644 index 6ba9453709f0d3..00000000000000 --- a/packages/edit-site/src/components/post-fields/index.js +++ /dev/null @@ -1,50 +0,0 @@ -/** - * WordPress dependencies - */ -import { - featuredImageField, - slugField, - parentField, - passwordField, - statusField, - commentStatusField, - titleField, - dateField, - authorField, -} from '@wordpress/fields'; -import { useMemo } from '@wordpress/element'; -import { useEntityRecords } from '@wordpress/core-data'; - -function usePostFields() { - const { records: authors, isResolving: isLoadingAuthors } = - useEntityRecords( 'root', 'user', { per_page: -1 } ); - - const fields = useMemo( - () => [ - featuredImageField, - titleField, - { - ...authorField, - elements: - authors?.map( ( { id, name } ) => ( { - value: id, - label: name, - } ) ) || [], - }, - statusField, - dateField, - slugField, - parentField, - commentStatusField, - passwordField, - ], - [ authors ] - ); - - return { - isLoading: isLoadingAuthors, - fields, - }; -} - -export default usePostFields; diff --git a/packages/edit-site/src/components/post-fields/style.scss b/packages/edit-site/src/components/post-fields/style.scss deleted file mode 100644 index adeaf9a2678253..00000000000000 --- a/packages/edit-site/src/components/post-fields/style.scss +++ /dev/null @@ -1,3 +0,0 @@ -.components-popover.components-dropdown__content.dataforms-layouts-panel__field-dropdown { - z-index: z-index(".components-popover.components-dropdown__content.dataforms-layouts-panel__field-dropdown"); -} diff --git a/packages/edit-site/src/components/post-list/index.js b/packages/edit-site/src/components/post-list/index.js index 4639cb3c950b76..26a3621da9ee60 100644 --- a/packages/edit-site/src/components/post-list/index.js +++ b/packages/edit-site/src/components/post-list/index.js @@ -13,6 +13,7 @@ import { DataViews, filterSortAndPaginate } from '@wordpress/dataviews'; import { privateApis as editorPrivateApis } from '@wordpress/editor'; import { __ } from '@wordpress/i18n'; import { drawerRight } from '@wordpress/icons'; +import { usePostFields } from '@wordpress/fields'; /** * Internal dependencies @@ -32,7 +33,6 @@ import AddNewPostModal from '../add-new-post'; import { unlock } from '../../lock-unlock'; import { useEditPostAction } from '../dataviews-actions'; import { usePrevious } from '@wordpress/compose'; -import usePostFields from '../post-fields'; const { usePostActions } = unlock( editorPrivateApis ); const { useLocation, useHistory } = unlock( routerPrivateApis ); diff --git a/packages/edit-site/src/style.scss b/packages/edit-site/src/style.scss index 0e5744fe362e35..cbbaa3b4fb6cd3 100644 --- a/packages/edit-site/src/style.scss +++ b/packages/edit-site/src/style.scss @@ -30,7 +30,6 @@ @import "./components/editor-canvas-container/style.scss"; @import "./components/post-edit/style.scss"; @import "./components/post-list/style.scss"; -@import "./components/post-fields/style.scss"; @import "./components/resizable-frame/style.scss"; @import "./hooks/push-changes-to-global-styles/style.scss"; @import "./components/global-styles/font-library-modal/style.scss"; @@ -94,3 +93,7 @@ body.js.site-editor-php { } @include wordpress-admin-schemes(); + +.components-popover.components-dropdown__content.dataforms-layouts-panel__field-dropdown { + z-index: z-index(".components-popover.components-dropdown__content.dataforms-layouts-panel__field-dropdown"); +} diff --git a/packages/fields/README.md b/packages/fields/README.md index e6cf6d3007ed97..8bcf4d2e91a0fc 100644 --- a/packages/fields/README.md +++ b/packages/fields/README.md @@ -106,6 +106,10 @@ Undocumented declaration. Undocumented declaration. +### usePostFields + +Hook to get the fields for a post (BasePost or BasePostWithEmbeddedAuthor). + ### viewPost Undocumented declaration. diff --git a/packages/fields/src/fields/index.ts b/packages/fields/src/fields/index.ts index 5ea4235af1d964..5333d03371e3c2 100644 --- a/packages/fields/src/fields/index.ts +++ b/packages/fields/src/fields/index.ts @@ -8,3 +8,4 @@ export { default as statusField } from './status'; export { default as commentStatusField } from './comment-status'; export { default as dateField } from './date'; export { default as authorField } from './author'; +export { default as usePostFields } from './use-post-fields'; diff --git a/packages/fields/src/fields/use-post-fields/index.ts b/packages/fields/src/fields/use-post-fields/index.ts new file mode 100644 index 00000000000000..88caa36e9b68ec --- /dev/null +++ b/packages/fields/src/fields/use-post-fields/index.ts @@ -0,0 +1,69 @@ +/** + * WordPress dependencies + */ +import { useMemo } from '@wordpress/element'; +import { useEntityRecords } from '@wordpress/core-data'; +import type { Field } from '@wordpress/dataviews'; + +/** + * Internal dependencies + */ +import featuredImageField from '../featured-image'; +import slugField from '../slug'; +import parentField from '../parent'; +import passwordField from '../password'; +import statusField from '../status'; +import commentStatusField from '../comment-status'; +import titleField from '../title'; +import dateField from '../date'; +import authorField from '../author'; +import type { BasePost, BasePostWithEmbeddedAuthor } from '../../types'; + +type Post = BasePost | BasePostWithEmbeddedAuthor; + +interface UsePostFieldsReturn { + isLoading: boolean; + fields: Field< Post >[]; +} + +interface Author { + id: number; + name: string; +} + +function usePostFields(): UsePostFieldsReturn { + const { records: authors, isResolving: isLoadingAuthors } = + useEntityRecords< Author >( 'root', 'user', { per_page: -1 } ); + + const fields = useMemo( + () => + [ + featuredImageField, + titleField, + { + ...authorField, + elements: authors?.map( ( { id, name } ) => ( { + value: id, + label: name, + } ) ), + }, + statusField, + dateField, + slugField, + parentField, + commentStatusField, + passwordField, + ] as Field< Post >[], + [ authors ] + ); + + return { + isLoading: isLoadingAuthors, + fields, + }; +} + +/** + * Hook to get the fields for a post (BasePost or BasePostWithEmbeddedAuthor). + */ +export default usePostFields; From 81726b35551fe25210b5c102d2a1ff82b7adb52f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Maneiro?= <583546+oandregal@users.noreply.github.com> Date: Fri, 15 Nov 2024 10:52:52 +0100 Subject: [PATCH 2/4] Move style to component --- packages/dataviews/src/dataforms-layouts/panel/style.scss | 4 ++++ packages/edit-site/src/style.scss | 4 ---- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/packages/dataviews/src/dataforms-layouts/panel/style.scss b/packages/dataviews/src/dataforms-layouts/panel/style.scss index ae69c4ff45243a..c7058f6366b3b6 100644 --- a/packages/dataviews/src/dataforms-layouts/panel/style.scss +++ b/packages/dataviews/src/dataforms-layouts/panel/style.scss @@ -44,3 +44,7 @@ .dataforms-layouts-panel__dropdown-header { margin-bottom: $grid-unit-20; } + +.components-popover.components-dropdown__content.dataforms-layouts-panel__field-dropdown { + z-index: z-index(".components-popover.components-dropdown__content.dataforms-layouts-panel__field-dropdown"); +} diff --git a/packages/edit-site/src/style.scss b/packages/edit-site/src/style.scss index cbbaa3b4fb6cd3..63ad8244a7c95e 100644 --- a/packages/edit-site/src/style.scss +++ b/packages/edit-site/src/style.scss @@ -93,7 +93,3 @@ body.js.site-editor-php { } @include wordpress-admin-schemes(); - -.components-popover.components-dropdown__content.dataforms-layouts-panel__field-dropdown { - z-index: z-index(".components-popover.components-dropdown__content.dataforms-layouts-panel__field-dropdown"); -} From d049eee629ffe118ed246c9c6183e83c1ae6354f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Maneiro?= <583546+oandregal@users.noreply.github.com> Date: Fri, 15 Nov 2024 11:12:48 +0100 Subject: [PATCH 3/4] Update Type --- packages/fields/src/fields/use-post-fields/index.ts | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/packages/fields/src/fields/use-post-fields/index.ts b/packages/fields/src/fields/use-post-fields/index.ts index 88caa36e9b68ec..7a812d88ba5116 100644 --- a/packages/fields/src/fields/use-post-fields/index.ts +++ b/packages/fields/src/fields/use-post-fields/index.ts @@ -17,13 +17,11 @@ import commentStatusField from '../comment-status'; import titleField from '../title'; import dateField from '../date'; import authorField from '../author'; -import type { BasePost, BasePostWithEmbeddedAuthor } from '../../types'; - -type Post = BasePost | BasePostWithEmbeddedAuthor; +import type { BasePostWithEmbeddedAuthor } from '../../types'; interface UsePostFieldsReturn { isLoading: boolean; - fields: Field< Post >[]; + fields: Field< BasePostWithEmbeddedAuthor >[]; } interface Author { @@ -53,7 +51,7 @@ function usePostFields(): UsePostFieldsReturn { parentField, commentStatusField, passwordField, - ] as Field< Post >[], + ] as Field< BasePostWithEmbeddedAuthor >[], [ authors ] ); From 1b76327d7dad2dba9b7c71ba10292e18914502d1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Maneiro?= <583546+oandregal@users.noreply.github.com> Date: Fri, 15 Nov 2024 11:33:48 +0100 Subject: [PATCH 4/4] Move usePostFields to editor package --- .../src/components/post-edit/index.js | 3 +-- .../src/components/post-list/index.js | 3 +-- .../src/components/post-fields}/index.ts | 26 +++++++++---------- packages/editor/src/private-apis.js | 2 ++ packages/fields/README.md | 4 --- packages/fields/src/fields/index.ts | 1 - packages/fields/src/index.ts | 1 + 7 files changed, 17 insertions(+), 23 deletions(-) rename packages/{fields/src/fields/use-post-fields => editor/src/components/post-fields}/index.ts (70%) diff --git a/packages/edit-site/src/components/post-edit/index.js b/packages/edit-site/src/components/post-edit/index.js index 6a8d2322b2f87c..a535eef4ce7878 100644 --- a/packages/edit-site/src/components/post-edit/index.js +++ b/packages/edit-site/src/components/post-edit/index.js @@ -13,7 +13,6 @@ import { store as coreDataStore } from '@wordpress/core-data'; import { __experimentalVStack as VStack } from '@wordpress/components'; import { useState, useMemo, useEffect } from '@wordpress/element'; import { privateApis as editorPrivateApis } from '@wordpress/editor'; -import { usePostFields } from '@wordpress/fields'; /** * Internal dependencies @@ -21,7 +20,7 @@ import { usePostFields } from '@wordpress/fields'; import Page from '../page'; import { unlock } from '../../lock-unlock'; -const { PostCardPanel } = unlock( editorPrivateApis ); +const { PostCardPanel, usePostFields } = unlock( editorPrivateApis ); const fieldsWithBulkEditSupport = [ 'title', diff --git a/packages/edit-site/src/components/post-list/index.js b/packages/edit-site/src/components/post-list/index.js index 26a3621da9ee60..975809b2ad6106 100644 --- a/packages/edit-site/src/components/post-list/index.js +++ b/packages/edit-site/src/components/post-list/index.js @@ -13,7 +13,6 @@ import { DataViews, filterSortAndPaginate } from '@wordpress/dataviews'; import { privateApis as editorPrivateApis } from '@wordpress/editor'; import { __ } from '@wordpress/i18n'; import { drawerRight } from '@wordpress/icons'; -import { usePostFields } from '@wordpress/fields'; /** * Internal dependencies @@ -34,7 +33,7 @@ import { unlock } from '../../lock-unlock'; import { useEditPostAction } from '../dataviews-actions'; import { usePrevious } from '@wordpress/compose'; -const { usePostActions } = unlock( editorPrivateApis ); +const { usePostActions, usePostFields } = unlock( editorPrivateApis ); const { useLocation, useHistory } = unlock( routerPrivateApis ); const { useEntityRecordsWithPermissions } = unlock( coreDataPrivateApis ); const EMPTY_ARRAY = []; diff --git a/packages/fields/src/fields/use-post-fields/index.ts b/packages/editor/src/components/post-fields/index.ts similarity index 70% rename from packages/fields/src/fields/use-post-fields/index.ts rename to packages/editor/src/components/post-fields/index.ts index 7a812d88ba5116..3d675ab763d64c 100644 --- a/packages/fields/src/fields/use-post-fields/index.ts +++ b/packages/editor/src/components/post-fields/index.ts @@ -4,20 +4,18 @@ import { useMemo } from '@wordpress/element'; import { useEntityRecords } from '@wordpress/core-data'; import type { Field } from '@wordpress/dataviews'; - -/** - * Internal dependencies - */ -import featuredImageField from '../featured-image'; -import slugField from '../slug'; -import parentField from '../parent'; -import passwordField from '../password'; -import statusField from '../status'; -import commentStatusField from '../comment-status'; -import titleField from '../title'; -import dateField from '../date'; -import authorField from '../author'; -import type { BasePostWithEmbeddedAuthor } from '../../types'; +import { + featuredImageField, + slugField, + parentField, + passwordField, + statusField, + commentStatusField, + titleField, + dateField, + authorField, +} from '@wordpress/fields'; +import type { BasePostWithEmbeddedAuthor } from '@wordpress/fields'; interface UsePostFieldsReturn { isLoading: boolean; diff --git a/packages/editor/src/private-apis.js b/packages/editor/src/private-apis.js index f9a6d4d17904ee..b49b2a69a3bf2b 100644 --- a/packages/editor/src/private-apis.js +++ b/packages/editor/src/private-apis.js @@ -16,6 +16,7 @@ import PluginPostExcerpt from './components/post-excerpt/plugin'; import PostCardPanel from './components/post-card-panel'; import PreferencesModal from './components/preferences-modal'; import { usePostActions } from './components/post-actions/actions'; +import usePostFields from './components/post-fields'; import ToolsMoreMenuGroup from './components/more-menu/tools-more-menu-group'; import ViewMoreMenuGroup from './components/more-menu/view-more-menu-group'; import ResizableEditor from './components/resizable-editor'; @@ -40,6 +41,7 @@ lock( privateApis, { PostCardPanel, PreferencesModal, usePostActions, + usePostFields, ToolsMoreMenuGroup, ViewMoreMenuGroup, ResizableEditor, diff --git a/packages/fields/README.md b/packages/fields/README.md index 8bcf4d2e91a0fc..e6cf6d3007ed97 100644 --- a/packages/fields/README.md +++ b/packages/fields/README.md @@ -106,10 +106,6 @@ Undocumented declaration. Undocumented declaration. -### usePostFields - -Hook to get the fields for a post (BasePost or BasePostWithEmbeddedAuthor). - ### viewPost Undocumented declaration. diff --git a/packages/fields/src/fields/index.ts b/packages/fields/src/fields/index.ts index 5333d03371e3c2..5ea4235af1d964 100644 --- a/packages/fields/src/fields/index.ts +++ b/packages/fields/src/fields/index.ts @@ -8,4 +8,3 @@ export { default as statusField } from './status'; export { default as commentStatusField } from './comment-status'; export { default as dateField } from './date'; export { default as authorField } from './author'; -export { default as usePostFields } from './use-post-fields'; diff --git a/packages/fields/src/index.ts b/packages/fields/src/index.ts index 4c721b85b61a40..41879a86e76bed 100644 --- a/packages/fields/src/index.ts +++ b/packages/fields/src/index.ts @@ -1,2 +1,3 @@ export * from './fields'; export * from './actions'; +export type * from './types';