Skip to content

Commit

Permalink
Move usePostFields to wordpress/editor package (#67024)
Browse files Browse the repository at this point in the history
Co-authored-by: oandregal <[email protected]>
Co-authored-by: youknowriad <[email protected]>
Co-authored-by: ntsekouras <[email protected]>
  • Loading branch information
4 people authored Nov 15, 2024
1 parent a750125 commit d8e0b13
Show file tree
Hide file tree
Showing 9 changed files with 74 additions and 58 deletions.
4 changes: 4 additions & 0 deletions packages/dataviews/src/dataforms-layouts/panel/style.scss
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}
3 changes: 1 addition & 2 deletions packages/edit-site/src/components/post-edit/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,9 @@ import { privateApis as editorPrivateApis } from '@wordpress/editor';
* Internal dependencies
*/
import Page from '../page';
import usePostFields from '../post-fields';
import { unlock } from '../../lock-unlock';

const { PostCardPanel } = unlock( editorPrivateApis );
const { PostCardPanel, usePostFields } = unlock( editorPrivateApis );

const fieldsWithBulkEditSupport = [
'title',
Expand Down
50 changes: 0 additions & 50 deletions packages/edit-site/src/components/post-fields/index.js

This file was deleted.

3 changes: 0 additions & 3 deletions packages/edit-site/src/components/post-fields/style.scss

This file was deleted.

3 changes: 1 addition & 2 deletions packages/edit-site/src/components/post-list/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,8 @@ 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 { usePostActions, usePostFields } = unlock( editorPrivateApis );
const { useLocation, useHistory } = unlock( routerPrivateApis );
const { useEntityRecordsWithPermissions } = unlock( coreDataPrivateApis );
const EMPTY_ARRAY = [];
Expand Down
1 change: 0 additions & 1 deletion packages/edit-site/src/style.scss
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down
65 changes: 65 additions & 0 deletions packages/editor/src/components/post-fields/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/**
* WordPress dependencies
*/
import { useMemo } from '@wordpress/element';
import { useEntityRecords } from '@wordpress/core-data';
import type { Field } from '@wordpress/dataviews';
import {
featuredImageField,
slugField,
parentField,
passwordField,
statusField,
commentStatusField,
titleField,
dateField,
authorField,
} from '@wordpress/fields';
import type { BasePostWithEmbeddedAuthor } from '@wordpress/fields';

interface UsePostFieldsReturn {
isLoading: boolean;
fields: Field< BasePostWithEmbeddedAuthor >[];
}

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< BasePostWithEmbeddedAuthor >[],
[ authors ]
);

return {
isLoading: isLoadingAuthors,
fields,
};
}

/**
* Hook to get the fields for a post (BasePost or BasePostWithEmbeddedAuthor).
*/
export default usePostFields;
2 changes: 2 additions & 0 deletions packages/editor/src/private-apis.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -40,6 +41,7 @@ lock( privateApis, {
PostCardPanel,
PreferencesModal,
usePostActions,
usePostFields,
ToolsMoreMenuGroup,
ViewMoreMenuGroup,
ResizableEditor,
Expand Down
1 change: 1 addition & 0 deletions packages/fields/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export * from './fields';
export * from './actions';
export type * from './types';

0 comments on commit d8e0b13

Please sign in to comment.