Skip to content

Commit

Permalink
DataViews: Register the export pattern action like any third-party ac…
Browse files Browse the repository at this point in the history
…tion (#63046)

Co-authored-by: youknowriad <youknowriad@git.wordpress.org>
Co-authored-by: ntsekouras <ntsekouras@git.wordpress.org>
  • Loading branch information
3 people committed Jul 5, 2024
1 parent b8344b6 commit 31a3c5d
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 26 deletions.
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
),
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 };

1 comment on commit 31a3c5d

@github-actions
Copy link

Choose a reason for hiding this comment

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

Flaky tests detected in 31a3c5d.
Some tests passed with failed attempts. The failures may not be related to this commit but are still reported for visibility. See the documentation for more information.

🔍 Workflow run URL: https://github.com/WordPress/gutenberg/actions/runs/9808650028
📝 Reported issues:

Please sign in to comment.