2021-10-30 17:26:41 +00:00
|
|
|
import * as electronRemote from '@electron/remote';
|
|
|
|
const electronConsole = electronRemote.getGlobal('console') as Console;
|
|
|
|
import Logger from '../../../logger/logger';
|
2021-11-02 04:29:24 +00:00
|
|
|
const LOG = Logger.create(__filename, electronConsole);
|
2021-10-30 17:26:41 +00:00
|
|
|
|
|
|
|
import * as moment from 'moment';
|
|
|
|
import * as FileType from 'file-type';
|
|
|
|
|
|
|
|
import ElementsUtil from './require/elements-util.js';
|
|
|
|
|
|
|
|
import { Message, ShouldNeverHappenError } from '../data-types';
|
2021-11-07 16:50:30 +00:00
|
|
|
import Q from '../q-module';
|
|
|
|
import createImageOverlay from './overlay-image';
|
2021-11-07 21:57:09 +00:00
|
|
|
import createImageContextMenu from './context-menu-img';
|
2021-11-22 02:47:29 +00:00
|
|
|
import CombinedGuild from '../guild-combined';
|
2021-10-30 17:26:41 +00:00
|
|
|
|
2021-11-21 18:29:42 +00:00
|
|
|
export default function createImageResourceMessageContinued(document: Document, q: Q, guild: CombinedGuild, message: Message): HTMLElement {
|
2021-11-07 16:50:30 +00:00
|
|
|
if (!message.resourceId || !message.resourcePreviewId || !message.resourceName) {
|
|
|
|
throw new ShouldNeverHappenError('Message is not a resource message');
|
|
|
|
}
|
|
|
|
|
2021-11-22 02:47:29 +00:00
|
|
|
let element = q.create({ class: 'message continued', 'meta-id': message.id, 'meta-member-id': message.member.id, 'meta-guild-id': guild.id, content: [
|
2021-11-07 16:50:30 +00:00
|
|
|
{ class: 'timestamp', content: moment(message.sent).format('HH:mm') },
|
|
|
|
{ class: 'right', content: [
|
2021-11-07 21:57:09 +00:00
|
|
|
{ class: 'content image', style: `width: ${message.previewWidth}px; height: ${message.previewHeight}px;`, content:
|
2021-11-07 16:50:30 +00:00
|
|
|
{ tag: 'img', src: './img/loading.svg', alt: message.resourceName } }, // src will be replaced later
|
|
|
|
{ class: 'content text', content: ElementsUtil.parseMessageText(message.text ?? '') }
|
|
|
|
] }
|
|
|
|
] }) as HTMLElement;
|
|
|
|
q.$$$(element, '.content.image').addEventListener('click', () => {
|
2021-11-22 02:47:29 +00:00
|
|
|
document.body.appendChild(createImageOverlay(document, q, guild, message.resourceId as string, message.resourceName as string));
|
2021-11-07 16:50:30 +00:00
|
|
|
});
|
|
|
|
(async () => {
|
|
|
|
try {
|
2021-11-22 02:47:29 +00:00
|
|
|
let resource = await guild.fetchResource(message.resourcePreviewId as string);
|
|
|
|
let src = await ElementsUtil.getImageBufferSrc(resource.data);
|
2021-11-07 16:50:30 +00:00
|
|
|
(q.$$$(element, '.content.image img') as HTMLImageElement).src = src;
|
2021-10-30 17:26:41 +00:00
|
|
|
|
2021-11-22 02:47:29 +00:00
|
|
|
let { mime, ext } = (await FileType.fromBuffer(resource.data)) ?? { mime: null, ext: null };
|
2021-10-30 17:26:41 +00:00
|
|
|
|
2021-11-07 16:50:30 +00:00
|
|
|
if (mime === null || ext === null) throw new Error('unable to get mime/ext');
|
2021-10-30 17:26:41 +00:00
|
|
|
|
2021-11-07 16:50:30 +00:00
|
|
|
q.$$$(element, '.content.image').addEventListener('contextmenu', (e) => {
|
2021-11-22 02:47:29 +00:00
|
|
|
let contextMenu = createImageContextMenu(document, q, guild, message.resourceName as string, resource.data, mime as string, ext as string, true);
|
2021-11-07 16:50:30 +00:00
|
|
|
document.body.appendChild(contextMenu);
|
|
|
|
let relativeTo = { x: e.pageX, y: e.pageY };
|
|
|
|
ElementsUtil.alignContextElement(contextMenu, relativeTo, { top: 'centerY', left: 'right' });
|
|
|
|
});
|
|
|
|
} catch (e) {
|
|
|
|
LOG.error('error loading preview image', e);
|
|
|
|
(q.$$$(element, '.content.image img') as HTMLImageElement).src = './img/error.png';
|
|
|
|
}
|
|
|
|
})();
|
|
|
|
return element;
|
2021-10-30 17:26:41 +00:00
|
|
|
}
|