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

DataViews: Register the export pattern action like any third-party action #63046

Merged
merged 2 commits into from
Jul 5, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
2 changes: 0 additions & 2 deletions packages/editor/src/components/post-actions/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ import {
} from '../../store/constants';
import { store as editorStore } from '../../store';
import { unlock } from '../../lock-unlock';
import { exportPatternAsJSONAction } from './export-pattern-action';
import { CreateTemplatePartModalContents } from '../create-template-part-modal';
import { getItemTitle } from '../../dataviews/actions/utils';

Expand Down Expand Up @@ -914,7 +913,6 @@ export function usePostActions( { postType, onActionPerformed, context } ) {
duplicateTemplatePartAction,
isPattern && userCanCreatePostType && duplicatePatternAction,
supportsTitle && renamePostActionForPostType,
isPattern && exportPatternAsJSONAction,
! isTemplateOrTemplatePart && restorePostActionForPostType,
! isTemplateOrTemplatePart &&
! isPattern &&
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,15 @@ import { downloadZip } from 'client-zip';
*/
import { downloadBlob } from '@wordpress/blob';
import { __ } from '@wordpress/i18n';
import { privateApis as patternsPrivateApis } from '@wordpress/patterns';
import type { Action } from '@wordpress/dataviews';

/**
* Internal dependencies
*/
import { unlock } from '../../lock-unlock';
import { getItemTitle } from '../../dataviews/actions/utils';
import type { Pattern } from '../types';
import { getItemTitle } from './utils';

// Patterns.
const { PATTERN_TYPES } = unlock( patternsPrivateApis );

function getJsonFromItem( item ) {
function getJsonFromItem( item: Pattern ) {
return JSON.stringify(
{
__file: item.type,
Expand All @@ -33,16 +30,10 @@ function getJsonFromItem( item ) {
);
}

export const exportPatternAsJSONAction = {
const exportPattern: Action< Pattern > = {
id: 'export-pattern',
label: __( 'Export as JSON' ),
supportsBulk: true,
isEligible: ( item ) => {
if ( ! item.type ) {
return false;
}
return item.type === PATTERN_TYPES.user;
},
callback: async ( items ) => {
if ( items.length === 1 ) {
return downloadBlob(
Expand All @@ -53,7 +44,7 @@ export const exportPatternAsJSONAction = {
'application/json'
);
}
const nameCount = {};
const nameCount: Record< string, number > = {};
const filesToZip = items.map( ( item ) => {
const name = kebabCase( getItemTitle( item ) || item.slug );
nameCount[ name ] = ( nameCount[ name ] || 0 ) + 1;
Expand All @@ -75,3 +66,5 @@ export const exportPatternAsJSONAction = {
);
},
};

export default exportPattern;
2 changes: 2 additions & 0 deletions packages/editor/src/dataviews/actions/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { type StoreDescriptor, dispatch } from '@wordpress/data';
* Internal dependencies
*/
import deletePost from './delete-post';
import exportPattern from './export-pattern';
import resetPost from './reset-post';

// @ts-ignore
Expand All @@ -18,6 +19,7 @@ export default function registerDefaultActions() {
dispatch( editorStore as StoreDescriptor )
);

registerEntityAction( 'postType', 'wp_block', exportPattern );
registerEntityAction( 'postType', '*', resetPost );
registerEntityAction( 'postType', '*', deletePost );
}
16 changes: 12 additions & 4 deletions packages/editor/src/dataviews/store/reducer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,26 @@ function actions( state: ActionState = {}, action: ReduxAction ) {
return {
...state,
[ action.kind ]: {
...state[ action.kind ],
[ action.name ]: [
...( state[ action.kind ]?.[ action.name ] ?? [] ),
...(
state[ action.kind ]?.[ action.name ] ?? []
).filter(
( _action ) => _action.id !== action.config.id
Copy link
Contributor

Choose a reason for hiding this comment

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

Why do we need this filtering? Is it just defensive programming for trying to register the same action twice or was it needed actually for this PR?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, It's to override the previous action with the same id.

),
action.config,
],
},
};
case 'UNREGISTER_ENTITY_ACTION': {
return {
...state,
[ action.kind ]: (
state[ action.kind ]?.[ action.name ] ?? []
).filter( ( _action ) => _action.id !== action.actionId ),
[ action.kind ]: {
...state[ action.kind ],
[ action.name ]: (
state[ action.kind ]?.[ action.name ] ?? []
).filter( ( _action ) => _action.id !== action.actionId ),
},
};
}
}
Expand Down
11 changes: 10 additions & 1 deletion packages/editor/src/dataviews/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,16 @@ export interface TemplateOrTemplatePart extends BasePost {
id: string;
}

export type Post = TemplateOrTemplatePart | BasePost;
export interface Pattern extends BasePost {
slug: string;
title: { raw: string };
content: {
raw: string;
};
wp_pattern_sync_status: string;
}

export type Post = TemplateOrTemplatePart | Pattern | BasePost;

// Will be unnecessary after typescript 5.0 upgrade.
export type CoreDataError = { message?: string; code?: string };
Loading