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

Move duplicateTemplatePart action to the @wordpress/fields package #65390

Merged
merged 53 commits into from
Dec 3, 2024
Merged
Show file tree
Hide file tree
Changes from 47 commits
Commits
Show all changes
53 commits
Select commit Hold shift + click to select a range
67ff043
Migrate store and actions from editor package to fields package
gigitux Sep 12, 2024
a71e726
fix documentation
gigitux Sep 12, 2024
0141d8c
fix import
gigitux Sep 12, 2024
f21a974
remove duplicate code
gigitux Sep 12, 2024
7063ee8
Merge branch 'trunk' of github.com:WordPress/gutenberg into add/actio…
gigitux Sep 12, 2024
644dab1
update package-lock
gigitux Sep 12, 2024
3616964
refactor PR
gigitux Sep 12, 2024
f7f1d4d
fix mobile build
gigitux Sep 12, 2024
ae0efeb
update package.json
gigitux Sep 12, 2024
4356a42
migrate actions to @wordpress/fields package
gigitux Sep 12, 2024
bf1462a
Merge branch 'trunk' of github.com:WordPress/gutenberg into add/actio…
gigitux Sep 12, 2024
63adf35
reduce change
gigitux Sep 13, 2024
c938a15
register new action
gigitux Sep 13, 2024
66479da
improve code
gigitux Sep 13, 2024
0bf760d
fix actionId
gigitux Sep 13, 2024
08bb03d
update package-lock.json
gigitux Sep 13, 2024
b599c42
remove comment
gigitux Sep 13, 2024
337b3e5
rename function
gigitux Sep 13, 2024
1016ef8
rename functions
gigitux Sep 13, 2024
39d25d1
improve code
gigitux Sep 13, 2024
dc03f57
Merge branch 'trunk' of github.com:WordPress/gutenberg into add/actio…
gigitux Sep 16, 2024
599c0a4
Migrate duplicate-template-part action to @wordpress/fields package
gigitux Sep 17, 2024
60e7a58
Merge branch 'trunk' of github.com:WordPress/gutenberg into add/actio…
gigitux Sep 17, 2024
81c0980
fix build
gigitux Sep 17, 2024
7b2bb82
restore files
gigitux Sep 17, 2024
d6fcf4a
fix CSS
gigitux Sep 17, 2024
0316536
restore delete post action
gigitux Sep 18, 2024
485237f
fix import order
gigitux Sep 18, 2024
5a4fb59
move to a flat structure
gigitux Sep 18, 2024
0deeed3
remove not necessary file
gigitux Sep 18, 2024
d42430d
Merge branch 'add/action-dataviews-2' of github.com:gigitux/gutenberg…
gigitux Sep 19, 2024
400a67d
Merge branch 'trunk' of github.com:WordPress/gutenberg into add/actio…
gigitux Sep 25, 2024
18d6029
Merge branch 'trunk' of github.com:WordPress/gutenberg into add/actio…
gigitux Oct 1, 2024
ff299b6
Merge branch 'trunk' of github.com:WordPress/gutenberg into add/actio…
gigitux Oct 24, 2024
36adb07
regenerate package-lock.json file
gigitux Oct 24, 2024
47da440
generate documentation
gigitux Oct 24, 2024
64423cf
delete constant file
gigitux Oct 24, 2024
199c313
update class
gigitux Oct 25, 2024
8571360
rename styles.scss to style.scss
gigitux Oct 25, 2024
844a98a
Merge branch 'trunk' of github.com:WordPress/gutenberg into add/actio…
gigitux Oct 28, 2024
0bd24a7
Merge branch 'trunk' of github.com:WordPress/gutenberg into add/actio…
gigitux Nov 1, 2024
00533bd
Merge branch 'trunk' of github.com:WordPress/gutenberg into add/actio…
gigitux Nov 26, 2024
13ea482
restore changes
gigitux Nov 26, 2024
6dede40
fix comment
gigitux Nov 26, 2024
63247d0
fix comment
gigitux Nov 26, 2024
3b6b664
improve documentation
gigitux Nov 26, 2024
8857196
Merge branch 'trunk' of github.com:WordPress/gutenberg into add/actio…
gigitux Nov 27, 2024
084383c
Merge branch 'trunk' of github.com:WordPress/gutenberg into add/actio…
gigitux Dec 2, 2024
4583cf8
export only CreateTemplatePartModal component
gigitux Dec 2, 2024
02da400
Merge branch 'add/action-dataviews-3' of github.com:gigitux/gutenberg…
gigitux Dec 2, 2024
e0f518e
restore not necessary changes
gigitux Dec 2, 2024
b9fb6f3
export only needed types
gigitux Dec 3, 2024
bb1f366
fix fallback value
gigitux Dec 3, 2024
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
1 change: 1 addition & 0 deletions package-lock.json

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

2 changes: 1 addition & 1 deletion packages/base-styles/_z-index.scss
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ $z-layers: (
// Should be above the popover (dropdown)
".reusable-blocks-menu-items__convert-modal": 1000001,
".patterns-menu-items__convert-modal": 1000001,
".editor-create-template-part-modal": 1000001,
".fields-create-template-part-modal": 1000001,
".block-editor-block-lock-modal": 1000001,
".block-editor-template-part__selection-modal": 1000001,
".block-editor-block-rename-modal": 1000001,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import { symbolFilled } from '@wordpress/icons';
/**
* Internal dependencies
*/
import CreateTemplatePartModal from '../create-template-part-modal';
import { CreateTemplatePartModal } from '@wordpress/fields';
Copy link
Contributor

Choose a reason for hiding this comment

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

Yes, I agree this is weird, but also not sure what's the best place for these wp-core-data aware components. That said I can live with it being in fields for now.

Copy link
Contributor

Choose a reason for hiding this comment

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

Maybe we should add a comment explaining the choice.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

With 47da440 I added some documentation.

Copy link
Member

@oandregal oandregal Dec 4, 2024

Choose a reason for hiding this comment

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

I've looked where this modal is being used. It looks like there are two additional places other than the action:

Patterns > Add new Block Toolbar
Screenshot 2024-12-04 at 11 57 19 Screenshot 2024-12-04 at 11 58 56

The second case is a bit more difficult to absorb declaratively because it's very custom. However, it seems the first one ("add new") we should be able to absorb as part of the actions registration. Note how all entities have it. It's a bit complex in patterns due to how they are setup, but I presume we want the same design ("add new" button) for any new registered CPT:

Patterns Templates Pages
Screenshot 2024-12-04 at 11 57 19 Screenshot 2024-12-04 at 12 02 12 Screenshot 2024-12-04 at 12 02 03

We could look at declare some actions as "insertion", similarly to how some of them are "destructive", perhaps.


export default function ConvertToTemplatePart( { clientIds, blocks } ) {
const [ isModalOpen, setIsModalOpen ] = useState( false );
Expand Down
64 changes: 0 additions & 64 deletions packages/editor/src/dataviews/actions/utils.ts

This file was deleted.

4 changes: 2 additions & 2 deletions packages/editor/src/dataviews/store/private-actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ import { doAction } from '@wordpress/hooks';
/**
* Internal dependencies
*/
import type { PostType } from '../types';
import { store as editorStore } from '../../store';
import { unlock } from '../../lock-unlock';
import type { PostType } from '@wordpress/fields';
import {
viewPost,
viewPostRevisions,
Expand All @@ -24,6 +24,7 @@ import {
renamePost,
resetPost,
deletePost,
duplicateTemplatePart,
featuredImageField,
dateField,
parentField,
Expand All @@ -34,7 +35,6 @@ import {
authorField,
titleField,
} from '@wordpress/fields';
import duplicateTemplatePart from '../actions/duplicate-template-part';

export function registerEntityAction< Item >(
kind: string,
Expand Down
85 changes: 0 additions & 85 deletions packages/editor/src/dataviews/types.ts

This file was deleted.

2 changes: 1 addition & 1 deletion packages/editor/src/private-apis.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import { lock } from './lock-unlock';
import { EntitiesSavedStatesExtensible } from './components/entities-saved-states';
import EditorContentSlotFill from './components/editor-interface/content-slot-fill';
import BackButton from './components/header/back-button';
import CreateTemplatePartModal from './components/create-template-part-modal';
import Editor from './components/editor';
import PluginPostExcerpt from './components/post-excerpt/plugin';
import PostCardPanel from './components/post-card-panel';
Expand All @@ -24,6 +23,7 @@ import {
mergeBaseAndUserConfigs,
GlobalStylesProvider,
} from './components/global-styles-provider';
import { CreateTemplatePartModal } from '@wordpress/fields';
import { registerCoreBlockBindingsSources } from './bindings/api';
import { getTemplateInfo } from './utils/get-template-info';

Expand Down
1 change: 0 additions & 1 deletion packages/editor/src/style.scss
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
@import "./components/autocompleters/style.scss";
@import "./components/collab-sidebar/style.scss";
@import "./components/collapsible-block-toolbar/style.scss";
@import "./components/create-template-part-modal/style.scss";
@import "./components/block-settings-menu/style.scss";
@import "./components/blog-title/style.scss";
@import "./components/document-bar/style.scss";
Expand Down
32 changes: 32 additions & 0 deletions packages/fields/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,30 @@ Author field for BasePost.

Comment status field for BasePost.

### CreateTemplatePartModal

A React component that renders a modal for creating a template part. The modal displays a title and the contents for creating the template part. This component should not live in this package, it should be moved to a dedicated package responsible for managing template.

_Parameters_

- _props_ `Object`: The component props.
- _props.modalTitle_ `{ modalTitle: string; } & CreateTemplatePartModalContentsProps[ 'modalTitle' ]`:

### CreateTemplatePartModalContents

A React component that renders the content of a model for creating a template part. This component should not live in this package; it should be moved to a dedicated package responsible for managing template.

_Parameters_

- _props_ `Object`: - The component props.
- _props.defaultArea_ `[string]`: - The default area for the template part.
- _props.blocks_ `[Array]`: - The blocks to be included in the template part.
- _props.confirmLabel_ `[string]`: - The label for the confirm button.
- _props.closeModal_ `Function`: - Function to close the modal.
- _props.onCreate_ `Function`: - Function to call when the template part is successfully created.
- _props.onError_ `[Function]`: - Function to call when there is an error creating the template part.
- _props.defaultTitle_ `[string]`: - The default title for the template part.
youknowriad marked this conversation as resolved.
Show resolved Hide resolved

### dateField

Date field for BasePost.
Expand All @@ -42,6 +66,14 @@ Undocumented declaration.

Undocumented declaration.

### duplicateTemplatePart
youknowriad marked this conversation as resolved.
Show resolved Hide resolved

This action is used to duplicate a template part.

_Type_

- `Action< TemplatePart >`

### exportPattern

Undocumented declaration.
Expand Down
1 change: 1 addition & 0 deletions packages/fields/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
"@babel/runtime": "7.25.7",
"@wordpress/api-fetch": "*",
"@wordpress/blob": "*",
"@wordpress/block-editor": "*",
youknowriad marked this conversation as resolved.
Show resolved Hide resolved
"@wordpress/blocks": "*",
"@wordpress/components": "*",
"@wordpress/compose": "*",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,17 @@ import type { Action } from '@wordpress/dataviews';
/**
* Internal dependencies
*/
import { TEMPLATE_PART_POST_TYPE } from '../../store/constants';
import { CreateTemplatePartModalContents } from '../../components/create-template-part-modal';
import { getItemTitle } from './utils';
import type { Post, TemplatePart } from '../types';
import { CreateTemplatePartModalContents } from '../components/create-template-part-modal';
import { getItemTitle } from './utils';

/**
* This action is used to duplicate a template part.
*/
const duplicateTemplatePart: Action< TemplatePart > = {
id: 'duplicate-template-part',
label: _x( 'Duplicate', 'action label' ),
isEligible: ( item ) => item.type === TEMPLATE_PART_POST_TYPE,
isEligible: ( item ) => item.type === 'wp_template_part',
modalHeader: _x( 'Duplicate template part', 'action label' ),
RenderModal: ( { items, closeModal } ) => {
const [ item ] = items;
Expand Down Expand Up @@ -61,7 +63,7 @@ const duplicateTemplatePart: Action< TemplatePart > = {
onCreate={ onTemplatePartSuccess }
onError={ closeModal }
confirmLabel={ _x( 'Duplicate', 'action label' ) }
closeModal={ closeModal }
closeModal={ closeModal ?? ( () => void 0 ) }
youknowriad marked this conversation as resolved.
Show resolved Hide resolved
/>
);
},
Expand Down
1 change: 1 addition & 0 deletions packages/fields/src/actions/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@ export { default as permanentlyDeletePost } from './permanently-delete-post';
export { default as restorePost } from './restore-post';
export { default as trashPost } from './trash-post';
export { default as deletePost } from './delete-post';
export { default as duplicateTemplatePart } from './duplicate-template-part';
9 changes: 3 additions & 6 deletions packages/fields/src/actions/rename-post.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,6 @@ import {
isTemplateRemovable,
isTemplate,
isTemplatePart,
TEMPLATE_ORIGINS,
TEMPLATE_PART_POST_TYPE,
TEMPLATE_POST_TYPE,
} from './utils';
import type { CoreDataError, PostWithPermissions } from '../types';

Expand All @@ -45,8 +42,8 @@ const renamePost: Action< PostWithPermissions > = {
// Templates, template parts and patterns have special checks for renaming.
if (
! [
TEMPLATE_POST_TYPE,
TEMPLATE_PART_POST_TYPE,
'wp_template',
'wp_template_part',
...Object.values( PATTERN_TYPES ),
].includes( post.type )
) {
Expand All @@ -64,7 +61,7 @@ const renamePost: Action< PostWithPermissions > = {

if ( isTemplatePart( post ) ) {
return (
post.source === TEMPLATE_ORIGINS.custom &&
post.source === 'custom' &&
! post?.has_theme_file &&
post.permissions?.update
);
Expand Down
13 changes: 4 additions & 9 deletions packages/fields/src/actions/reset-post.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,7 @@ import apiFetch from '@wordpress/api-fetch';
/**
* Internal dependencies
*/
import {
getItemTitle,
isTemplateOrTemplatePart,
TEMPLATE_ORIGINS,
TEMPLATE_POST_TYPE,
} from './utils';
import { getItemTitle, isTemplateOrTemplatePart } from './utils';
import type { CoreDataError, Template, TemplatePart } from '../types';

const isTemplateRevertable = (
Expand All @@ -38,7 +33,7 @@ const isTemplateRevertable = (
}

return (
templateOrTemplatePart.source === TEMPLATE_ORIGINS.custom &&
templateOrTemplatePart.source === 'custom' &&
( Boolean( templateOrTemplatePart?.plugin ) ||
templateOrTemplatePart?.has_theme_file )
);
Expand Down Expand Up @@ -186,7 +181,7 @@ const resetPostAction: Action< Template | TemplatePart > = {
isEligible: ( item ) => {
return (
isTemplateOrTemplatePart( item ) &&
item?.source === TEMPLATE_ORIGINS.custom &&
item?.source === 'custom' &&
( Boolean( item.type === 'wp_template' && item?.plugin ) ||
item?.has_theme_file )
);
Expand Down Expand Up @@ -231,7 +226,7 @@ const resetPostAction: Action< Template | TemplatePart > = {
);
} catch ( error ) {
let fallbackErrorMessage;
if ( items[ 0 ].type === TEMPLATE_POST_TYPE ) {
if ( items[ 0 ].type === 'wp_template' ) {
fallbackErrorMessage =
items.length === 1
? __(
Expand Down
Loading
Loading