2021-10-30 17:26:41 +00:00
|
|
|
import * as path from 'path';
|
|
|
|
|
|
|
|
import * as sharp from 'sharp';
|
2021-11-07 16:50:30 +00:00
|
|
|
import * as electron from 'electron';
|
2021-10-30 17:26:41 +00:00
|
|
|
|
|
|
|
import BaseElements from './require/base-elements';
|
|
|
|
import ElementsUtil from './require/elements-util';
|
|
|
|
|
|
|
|
import ClientController from '../client-controller';
|
2021-11-07 16:50:30 +00:00
|
|
|
import Q from '../q-module';
|
2021-10-30 17:26:41 +00:00
|
|
|
|
2021-11-07 16:50:30 +00:00
|
|
|
export default function createImageContextMenu(
|
|
|
|
document: Document,
|
|
|
|
q: Q,
|
|
|
|
server: ClientController,
|
|
|
|
resourceName: string,
|
|
|
|
buffer: Buffer,
|
|
|
|
mime: string,
|
|
|
|
ext: string,
|
|
|
|
isPreview: boolean
|
|
|
|
): HTMLElement {
|
|
|
|
// TODO: try/catch around sharp?
|
|
|
|
let contextMenu = BaseElements.createContextMenu(document, { class: 'image', content: [
|
|
|
|
{ class: 'item copy-image', content: 'Copy Image' + (isPreview ? ' Preview' : '') },
|
|
|
|
{ class: 'item save-image', content: 'Save Image' + (isPreview ? ' Preview' : '') },
|
|
|
|
] });
|
|
|
|
q.$$$(contextMenu, '.copy-image').addEventListener('click', async () => {
|
|
|
|
q.$$$(contextMenu, '.copy-image').innerText = 'Copying...';
|
|
|
|
let nativeImage: electron.NativeImage;
|
|
|
|
if (mime != 'image/png' && mime != 'image/jpeg' && mime != 'image/jpg') {
|
|
|
|
// use sharp to convert to png since nativeImage only supports jpeg/png
|
|
|
|
nativeImage = electron.nativeImage.createFromBuffer(await sharp(buffer).png().toBuffer());
|
|
|
|
} else {
|
|
|
|
nativeImage = electron.nativeImage.createFromBuffer(buffer);
|
|
|
|
}
|
|
|
|
electron.clipboard.writeImage(nativeImage);
|
|
|
|
q.$$$(contextMenu, '.copy-image').innerText = 'Copied to Clipboard';
|
|
|
|
});
|
|
|
|
q.$$$(contextMenu, '.save-image').addEventListener('click', ElementsUtil.createDownloadListener({
|
|
|
|
downloadBuff: buffer, server: server,
|
|
|
|
resourceName: path.basename(resourceName, '.' + ext) + (isPreview ? '-preview.' : '.') + ext,
|
|
|
|
downloadStartFunc: () => {},
|
|
|
|
writeStartFunc: () => { q.$$$(contextMenu, '.save-image').innerText = 'Writing...'; },
|
|
|
|
writeFailFunc: () => {
|
|
|
|
q.$$$(contextMenu, '.save-image').innerText = 'Write Failed. Click to Try Again';
|
|
|
|
},
|
|
|
|
successFunc: () => { q.$$$(contextMenu, '.save-image').innerText = 'Reveal' + (isPreview ? ' Preview' : '') + ' in Explorer'; }
|
|
|
|
}));
|
|
|
|
return contextMenu;
|
2021-10-30 17:26:41 +00:00
|
|
|
}
|