Skip to content

Commit cf710eb

Browse files
committed
make convertImage more flexible
1 parent ccb0820 commit cf710eb

File tree

2 files changed

+5
-5
lines changed

2 files changed

+5
-5
lines changed

web_src/js/features/copycontent.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import {copyToClipboard} from './clipboard.js';
22
import {showTemporaryTooltip} from '../modules/tippy.js';
3-
import {imageBlobToPng} from '../utils.js';
3+
import {convertImage} from '../utils.js';
44
const {i18n} = window.config;
55

66
async function doCopy(content, btn) {
@@ -47,7 +47,7 @@ export function initCopyContent() {
4747
} catch {
4848
if (isImage) { // convert image to png as last-resort as some browser only support png copy
4949
try {
50-
await doCopy(await imageBlobToPng(content), btn);
50+
await doCopy(await convertImage(content, 'image/png'), btn);
5151
} catch {
5252
showTemporaryTooltip(btn, i18n.copy_error);
5353
}

web_src/js/utils.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -104,8 +104,8 @@ export function blobToDataURI(blob) {
104104
});
105105
}
106106

107-
// convert any image Blob to a png Blob
108-
export function imageBlobToPng(blob) {
107+
// convert image Blob to another mime-type format.
108+
export function convertImage(blob, mime) {
109109
return new Promise(async (resolve, reject) => {
110110
try {
111111
const img = new Image();
@@ -118,7 +118,7 @@ export function imageBlobToPng(blob) {
118118
canvas.toBlob((blob) => {
119119
if (!(blob instanceof Blob)) return reject(new Error('imageBlobToPng failed'));
120120
resolve(blob);
121-
}, 'image/png');
121+
}, mime);
122122
});
123123
img.addEventListener('error', () => {
124124
reject(new Error('imageBlobToPng failed'));

0 commit comments

Comments
 (0)