Skip to content

Commit

Permalink
Refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
tmathern committed Aug 23, 2023
1 parent 2af6c8e commit 8382343
Showing 1 changed file with 10 additions and 15 deletions.
25 changes: 10 additions & 15 deletions blocks/aem-asset-selector/aem-asset-selector-util.js
Original file line number Diff line number Diff line change
Expand Up @@ -254,10 +254,10 @@ async function loadImageIntoHtmlElement(url) {
* @param {*} targetMimeType Target mimetype (target format)
* @returns A conversion promise resolving to a blob of the target mimetype
*/
async function convertImage(assetPublicUrl, targetMimeType, asset) {
async function convertImage(assetPublicUrl, targetMimeType='image/png', asset) {

Check failure on line 257 in blocks/aem-asset-selector/aem-asset-selector-util.js

View workflow job for this annotation

GitHub Actions / build

Default parameters should be last

Check failure on line 257 in blocks/aem-asset-selector/aem-asset-selector-util.js

View workflow job for this annotation

GitHub Actions / build

Operator '=' must be spaced
const imageElement = await loadImageIntoHtmlElement(assetPublicUrl);

return new Promise((resolve) => {
return new Promise(resolve => {

Check failure on line 260 in blocks/aem-asset-selector/aem-asset-selector-util.js

View workflow job for this annotation

GitHub Actions / build

Expected parentheses around arrow function argument
const canvas = document.createElement('canvas');
canvas.width = asset['tiff:imageWidth'];
canvas.height = asset['tiff:imageLength'];
Expand All @@ -277,16 +277,12 @@ async function convertImage(assetPublicUrl, targetMimeType, asset) {
* @returns {Promise} Resolves when the item has been written to the clipboard.
*/
async function copyToClipboardWithBinary(assetPublicUrl, mimeType, asset) {
let data;
let clipboardOptions = {};

Check failure on line 280 in blocks/aem-asset-selector/aem-asset-selector-util.js

View workflow job for this annotation

GitHub Actions / build

'clipboardOptions' is never reassigned. Use 'const' instead

if (!CLIPBOARD_SUPPORTED_BINARY_MIMETYPES.includes(mimeType)) {
const copiedBlob = await convertImage(assetPublicUrl, 'image/png', asset);

const clipboardOptions = {};
clipboardOptions['image/png'] = copiedBlob;
data = [
new ClipboardItem(clipboardOptions),
];
const clipboardTargetMimetype = 'image/png';
const copiedBlob = await convertImage(assetPublicUrl, clipboardTargetMimetype, asset);
clipboardOptions[clipboardTargetMimetype] = copiedBlob;
} else {
const binary = await fetch(assetPublicUrl);

Expand All @@ -298,14 +294,13 @@ async function copyToClipboardWithBinary(assetPublicUrl, mimeType, asset) {
if (!blob) {
throw new Error('No blob provided in asset response');
}

const clipboardOptions = {};
clipboardOptions[mimeType] = blob;
data = [
new ClipboardItem(clipboardOptions),
];
}

const data = [
new ClipboardItem(clipboardOptions),
];

return navigator.clipboard.write(data);
}

Expand Down

0 comments on commit 8382343

Please sign in to comment.