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
  • Loading branch information
youknowriad committed Jul 4, 2024
1 parent 3a65582 commit e2abb45
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 21 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 @@ -919,7 +918,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,15 +30,15 @@ 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;
return item.type === 'wp_block';
},
callback: async ( items ) => {
if ( items.length === 1 ) {
Expand All @@ -53,7 +50,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 +72,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 @@ -20,18 +20,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 };

0 comments on commit e2abb45

Please sign in to comment.