-
Notifications
You must be signed in to change notification settings - Fork 4.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Avoid importing client-zip in the native build.
- Loading branch information
1 parent
80f5b07
commit afdd165
Showing
4 changed files
with
100 additions
and
91 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
69 changes: 69 additions & 0 deletions
69
packages/editor/src/components/post-actions/export-pattern-as-json-action.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import { paramCase as kebabCase } from 'change-case'; | ||
import { downloadZip } from 'client-zip'; | ||
|
||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { downloadBlob } from '@wordpress/blob'; | ||
import { __ } from '@wordpress/i18n'; | ||
import { privateApis as patternsPrivateApis } from '@wordpress/patterns'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import { unlock } from '../../lock-unlock'; | ||
|
||
// Patterns. | ||
export const { PATTERN_TYPES } = unlock( patternsPrivateApis ); | ||
|
||
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 exportPatternAsJSONAction = { | ||
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 ] - 1 ) | ||
: '' ) | ||
}.json`, | ||
lastModified: new Date(), | ||
input: getJsonFromItem( item ), | ||
}; | ||
} ); | ||
return downloadBlob( | ||
__( 'patterns-export' ) + '.zip', | ||
await downloadZip( filesToZip ).blob(), | ||
'application/zip' | ||
); | ||
}, | ||
}; |
4 changes: 4 additions & 0 deletions
4
packages/editor/src/components/post-actions/export-pattern-as-json-action.native.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
// Client-zip is meant to be used in a browser and is therefore released as an ES6 module only, | ||
// in order for the native build to succeed we are importing a null action and avoiding importing | ||
// the non working in native context client-zip module. | ||
export const exportPatternAsJSONAction = null; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters