Skip to content

Commit

Permalink
Patterns: use existing download function for JSON downloads to fix no…
Browse files Browse the repository at this point in the history
…n-ASCII encoding (#55912)

* downloadjs has a bug where it cannot decode non-ASCII characters.
Gutenberg's reusable blocks code has an existing download function that we can use for JSON. This commit removes the downloadjs dependency and copies over the reusable blocks download function.

* Added comment to explain duplicate

* Remove IE11-specific code.
  • Loading branch information
ramonjd authored and mikachan committed Nov 13, 2023
1 parent 04fa57d commit 0b6e6e6
Showing 1 changed file with 21 additions and 3 deletions.
24 changes: 21 additions & 3 deletions packages/edit-site/src/components/page-patterns/grid-item.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
* External dependencies
*/
import classnames from 'classnames';
import downloadjs from 'downloadjs';
import { paramCase as kebabCase } from 'change-case';

/**
Expand Down Expand Up @@ -52,6 +51,25 @@ import { store as editSiteStore } from '../../store';
import { useLink } from '../routes/link';
import { unlock } from '../../lock-unlock';

/**
* Downloads a file.
* Also used in packages/list-reusable-blocks/src/utils/file.js.
*
* @param {string} fileName File Name.
* @param {string} content File Content.
* @param {string} contentType File mime type.
*/
function download( fileName, content, contentType ) {
const file = new window.Blob( [ content ], { type: contentType } );
const a = document.createElement( 'a' );
a.href = URL.createObjectURL( file );
a.download = fileName;
a.style.display = 'none';
document.body.appendChild( a );
a.click();
document.body.removeChild( a );
}

const { useGlobalStyle } = unlock( blockEditorPrivateApis );

const templatePartIcons = { header, footer, uncategorized };
Expand Down Expand Up @@ -118,9 +136,9 @@ function GridItem( { categoryId, item, ...props } ) {
syncStatus: item.patternBlock.wp_pattern_sync_status,
};

return downloadjs(
JSON.stringify( json, null, 2 ),
return download(
`${ kebabCase( item.title || item.name ) }.json`,
JSON.stringify( json, null, 2 ),
'application/json'
);
};
Expand Down

0 comments on commit 0b6e6e6

Please sign in to comment.