From b31e143053d0a6071597d6946e071f2109ae9dfb Mon Sep 17 00:00:00 2001 From: Jorge Costa Date: Fri, 9 Feb 2024 17:38:06 +0000 Subject: [PATCH 1/4] Add: Bulk export patterns action. --- package-lock.json | 12 ++++++ packages/edit-site/package.json | 1 + .../dataviews-pattern-actions.js | 43 ++++++++++++++----- 3 files changed, 46 insertions(+), 10 deletions(-) diff --git a/package-lock.json b/package-lock.json index 6f17ffb7edef6..69b377cac1c11 100644 --- a/package-lock.json +++ b/package-lock.json @@ -22223,6 +22223,11 @@ "integrity": "sha512-GRMWDxpOB6Dgk2E5Uo+3eEBvtOOlimMmpbFiKuLFnQzYDavtLFY3K5ona41jgN/WdRZtG7utuVSVTL4HbZHGkw==", "dev": true }, + "node_modules/client-zip": { + "version": "2.4.4", + "resolved": "https://registry.npmjs.org/client-zip/-/client-zip-2.4.4.tgz", + "integrity": "sha512-Ixk40BUI7VvNDxW7SCze20GbCuC+gjP4tGkXUpo6/W96bOf96HSed6cOQVeUOIe74SJAG/dIrBr7AtR4xBVnsA==" + }, "node_modules/cliui": { "version": "6.0.0", "resolved": "https://registry.npmjs.org/cliui/-/cliui-6.0.0.tgz", @@ -55252,6 +55257,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", @@ -70379,6 +70385,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", @@ -74231,6 +74238,11 @@ "integrity": "sha512-GRMWDxpOB6Dgk2E5Uo+3eEBvtOOlimMmpbFiKuLFnQzYDavtLFY3K5ona41jgN/WdRZtG7utuVSVTL4HbZHGkw==", "dev": true }, + "client-zip": { + "version": "2.4.4", + "resolved": "https://registry.npmjs.org/client-zip/-/client-zip-2.4.4.tgz", + "integrity": "sha512-Ixk40BUI7VvNDxW7SCze20GbCuC+gjP4tGkXUpo6/W96bOf96HSed6cOQVeUOIe74SJAG/dIrBr7AtR4xBVnsA==" + }, "cliui": { "version": "6.0.0", "resolved": "https://registry.npmjs.org/cliui/-/cliui-6.0.0.tgz", diff --git a/packages/edit-site/package.json b/packages/edit-site/package.json index c4f31d385e5d2..f62faf0b8cd8f 100644 --- a/packages/edit-site/package.json +++ b/packages/edit-site/package.json @@ -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", diff --git a/packages/edit-site/src/components/page-patterns/dataviews-pattern-actions.js b/packages/edit-site/src/components/page-patterns/dataviews-pattern-actions.js index 5a68f731c2afe..5d3948685db87 100644 --- a/packages/edit-site/src/components/page-patterns/dataviews-pattern-actions.js +++ b/packages/edit-site/src/components/page-patterns/dataviews-pattern-actions.js @@ -2,6 +2,7 @@ * External dependencies */ import { paramCase as kebabCase } from 'change-case'; +import { downloadZip } from 'client-zip'; /** * WordPress dependencies @@ -41,21 +42,43 @@ 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 filesToZip = items.map( ( item ) => { + return { + name: `${ kebabCase( item.title || item.name ) }.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' ); }, }; From fb6d19ff7128f017aedd94d6b89884a3f12c8de3 Mon Sep 17 00:00:00 2001 From: Jorge Costa Date: Thu, 15 Feb 2024 18:17:42 +0000 Subject: [PATCH 2/4] Fix patterns with the same are overwritten on the zip. --- .../components/page-patterns/dataviews-pattern-actions.js | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/packages/edit-site/src/components/page-patterns/dataviews-pattern-actions.js b/packages/edit-site/src/components/page-patterns/dataviews-pattern-actions.js index 5d3948685db87..8dfe09f26c76f 100644 --- a/packages/edit-site/src/components/page-patterns/dataviews-pattern-actions.js +++ b/packages/edit-site/src/components/page-patterns/dataviews-pattern-actions.js @@ -68,9 +68,15 @@ export const exportJSONaction = { 'application/json' ); } + const nameCount = {}; const filesToZip = items.map( ( item ) => { + const name = kebabCase( item.title || item.name ); + nameCount[ name ] = ( nameCount[ name ] || 0 ) + 1; return { - name: `${ kebabCase( item.title || item.name ) }.json`, + name: `${ + name + + ( nameCount[ name ] > 1 ? '-' + nameCount[ name ] : '' ) + }.json`, lastModified: new Date(), input: getJsonFromItem( item ), }; From cae8b68fcf84caf9065e354b49aada04b7eae6c5 Mon Sep 17 00:00:00 2001 From: Jorge Costa Date: Fri, 16 Feb 2024 11:04:44 +0000 Subject: [PATCH 3/4] Use operating system convention for repeated names. --- .../src/components/page-patterns/dataviews-pattern-actions.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/edit-site/src/components/page-patterns/dataviews-pattern-actions.js b/packages/edit-site/src/components/page-patterns/dataviews-pattern-actions.js index 8dfe09f26c76f..af608f28fa1e6 100644 --- a/packages/edit-site/src/components/page-patterns/dataviews-pattern-actions.js +++ b/packages/edit-site/src/components/page-patterns/dataviews-pattern-actions.js @@ -75,7 +75,7 @@ export const exportJSONaction = { return { name: `${ name + - ( nameCount[ name ] > 1 ? '-' + nameCount[ name ] : '' ) + ( nameCount[ name ] > 1 ? '-' + nameCount[ name ] - 1 : '' ) }.json`, lastModified: new Date(), input: getJsonFromItem( item ), From 5a9a7a1325d7cdbbe0099b1e904d93af285d52ba Mon Sep 17 00:00:00 2001 From: Jorge Costa Date: Fri, 16 Feb 2024 11:22:57 +0000 Subject: [PATCH 4/4] Fix priority issue --- .../src/components/page-patterns/dataviews-pattern-actions.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/packages/edit-site/src/components/page-patterns/dataviews-pattern-actions.js b/packages/edit-site/src/components/page-patterns/dataviews-pattern-actions.js index af608f28fa1e6..e908a49bb3608 100644 --- a/packages/edit-site/src/components/page-patterns/dataviews-pattern-actions.js +++ b/packages/edit-site/src/components/page-patterns/dataviews-pattern-actions.js @@ -75,7 +75,9 @@ export const exportJSONaction = { return { name: `${ name + - ( nameCount[ name ] > 1 ? '-' + nameCount[ name ] - 1 : '' ) + ( nameCount[ name ] > 1 + ? '-' + ( nameCount[ name ] - 1 ) + : '' ) }.json`, lastModified: new Date(), input: getJsonFromItem( item ),