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

Add: Bulk export patterns action. #58897

Merged
merged 4 commits into from
Feb 16, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
12 changes: 12 additions & 0 deletions package-lock.json

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

1 change: 1 addition & 0 deletions packages/edit-site/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@
"@wordpress/wordcount": "file:../wordcount",
"change-case": "^4.1.2",
"classnames": "^2.3.1",
"client-zip": "^2.4.4",
"colord": "^2.9.2",
"deepmerge": "^4.3.0",
"fast-deep-equal": "^3.1.3",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
* External dependencies
*/
import { paramCase as kebabCase } from 'change-case';
import { downloadZip } from 'client-zip';

/**
* WordPress dependencies
Expand Down Expand Up @@ -41,21 +42,49 @@ const { useHistory } = unlock( routerPrivateApis );
const { CreatePatternModalContents, useDuplicatePatternProps } =
unlock( patternsPrivateApis );

export const exportJSONaction = {
id: 'export-pattern',
label: __( 'Export as JSON' ),
isEligible: ( item ) => item.type === PATTERN_TYPES.user,
callback: ( [ item ] ) => {
const json = {
function getJsonFromItem( item ) {
return JSON.stringify(
{
__file: item.type,
title: item.title || item.name,
content: item.patternPost.content.raw,
syncStatus: item.patternPost.wp_pattern_sync_status,
};
},
null,
2
);
}

export const exportJSONaction = {
id: 'export-pattern',
label: __( 'Export as JSON' ),
supportsBulk: true,
isEligible: ( item ) => item.type === PATTERN_TYPES.user,
callback: async ( items ) => {
if ( items.length === 1 ) {
return downloadBlob(
`${ kebabCase( items[ 0 ].title || items[ 0 ].name ) }.json`,
getJsonFromItem( items[ 0 ] ),
'application/json'
);
}
const nameCount = {};
const filesToZip = items.map( ( item ) => {
const name = kebabCase( item.title || item.name );
nameCount[ name ] = ( nameCount[ name ] || 0 ) + 1;
return {
name: `${
name +
( nameCount[ name ] > 1 ? '-' + nameCount[ name ] : '' )
Copy link
Contributor

Choose a reason for hiding this comment

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

Probably it's better to follow the convention operating systems have and start from 1 for the same name files.
For example:

copy.json
copy-1.json

So we should nameCount[ name ] - 1. What do you think?

Copy link
Member Author

Choose a reason for hiding this comment

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

I agree with the suggestion, it was applied 👍

}.json`,
lastModified: new Date(),
input: getJsonFromItem( item ),
};
} );
return downloadBlob(
`${ kebabCase( item.title || item.name ) }.json`,
JSON.stringify( json, null, 2 ),
'application/json'
__( 'patterns-export' ) + '.zip',
await downloadZip( filesToZip ).blob(),
'application/zip'
);
},
};
Expand Down
Loading