Refactor image context menu (#63)
This commit is contained in:
parent
2b0a50282c
commit
19478f245c
|
@ -19,11 +19,11 @@ export const ImageMenu = ({ imageId }: ImageMenuProps) => {
|
||||||
<MenuBlock>
|
<MenuBlock>
|
||||||
<MenuList>
|
<MenuList>
|
||||||
<MenuItem onClick={() => copyImg(imageId)}>
|
<MenuItem onClick={() => copyImg(imageId)}>
|
||||||
<CopyIcon /> <MenuText>Copy imageId</MenuText>
|
<CopyIcon /> <MenuText>Copy image</MenuText>
|
||||||
</MenuItem>
|
</MenuItem>
|
||||||
<MenuItem onClick={() => downloadImg(imageId)}>
|
<MenuItem onClick={() => downloadImg(imageId)}>
|
||||||
<DownloadIcon />
|
<DownloadIcon />
|
||||||
<MenuText> Download imageId</MenuText>
|
<MenuText> Download image</MenuText>
|
||||||
</MenuItem>
|
</MenuItem>
|
||||||
</MenuList>
|
</MenuList>
|
||||||
</MenuBlock>
|
</MenuBlock>
|
||||||
|
|
|
@ -1,5 +1,3 @@
|
||||||
import { createImg } from "./createImg";
|
|
||||||
|
|
||||||
const copyToClipboard = async (pngBlob: any) => {
|
const copyToClipboard = async (pngBlob: any) => {
|
||||||
try {
|
try {
|
||||||
await navigator.clipboard.write([
|
await navigator.clipboard.write([
|
||||||
|
@ -12,27 +10,9 @@ const copyToClipboard = async (pngBlob: any) => {
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
const convertToPng = (imgBlob: any) => {
|
//Images are already converted to png by useMessenger when received
|
||||||
const canvas = document.createElement("canvas");
|
|
||||||
const ctx = canvas.getContext("2d");
|
|
||||||
const imageEl = createImg({ src: window.URL.createObjectURL(imgBlob) });
|
|
||||||
imageEl.onload = (e: any) => {
|
|
||||||
canvas.width = e.target.width;
|
|
||||||
canvas.height = e.target.height;
|
|
||||||
if (ctx) ctx.drawImage(e.target, 0, 0, e.target.width, e.target.height);
|
|
||||||
canvas.toBlob(copyToClipboard, "image/png", 1);
|
|
||||||
};
|
|
||||||
};
|
|
||||||
|
|
||||||
export const copyImg = async (image: string) => {
|
export const copyImg = async (image: string) => {
|
||||||
const img = await fetch(image);
|
const img = await fetch(image);
|
||||||
const imgBlob = await img.blob();
|
const imgBlob = await img.blob();
|
||||||
const extension = image.split(".").pop();
|
return copyToClipboard(imgBlob);
|
||||||
const supportedToBeConverted = ["jpeg", "jpg", "gif"];
|
|
||||||
if (extension && supportedToBeConverted.indexOf(extension.toLowerCase())) {
|
|
||||||
return convertToPng(imgBlob);
|
|
||||||
} else if (extension && extension.toLowerCase() === "png") {
|
|
||||||
return copyToClipboard(imgBlob);
|
|
||||||
}
|
|
||||||
return;
|
|
||||||
};
|
};
|
||||||
|
|
|
@ -1,8 +0,0 @@
|
||||||
export const createImg = (options: any) => {
|
|
||||||
options = options || {};
|
|
||||||
const img = document.createElement("img");
|
|
||||||
if (options.src) {
|
|
||||||
img.src = options.src;
|
|
||||||
}
|
|
||||||
return img;
|
|
||||||
};
|
|
|
@ -1,27 +1,10 @@
|
||||||
import { createImg } from "./createImg";
|
|
||||||
|
|
||||||
const createLink = (name: string, imgBlob: any) => {
|
|
||||||
const canvas = document.createElement("canvas");
|
|
||||||
const ctx = canvas.getContext("2d");
|
|
||||||
const imageEl = createImg({ src: window.URL.createObjectURL(imgBlob) });
|
|
||||||
imageEl.onload = (e: any) => {
|
|
||||||
canvas.width = e.target.width;
|
|
||||||
canvas.height = e.target.height;
|
|
||||||
if (ctx) ctx.drawImage(e.target, 0, 0, e.target.width, e.target.height);
|
|
||||||
const a = document.createElement("a");
|
|
||||||
a.download = `${name}.png`;
|
|
||||||
a.href = canvas.toDataURL("image/png");
|
|
||||||
a.click();
|
|
||||||
};
|
|
||||||
};
|
|
||||||
|
|
||||||
export const downloadImg = async (image: string) => {
|
export const downloadImg = async (image: string) => {
|
||||||
const img = await fetch(image);
|
try {
|
||||||
console.log(img);
|
const a = document.createElement("a");
|
||||||
const imgBlob = await img.blob();
|
a.download = `${image.split("/").pop()}.png`;
|
||||||
const name = image.split("/").pop();
|
a.href = image;
|
||||||
if (name) {
|
a.click();
|
||||||
return createLink(name, imgBlob);
|
} catch {
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
return;
|
|
||||||
};
|
};
|
||||||
|
|
Loading…
Reference in New Issue