366 lines
16 KiB
TypeScript
366 lines
16 KiB
TypeScript
import * as electronRemote from '@electron/remote';
|
|
const electronConsole = electronRemote.getGlobal('console') as Console;
|
|
|
|
import Logger from '../../../../logger/logger';
|
|
const LOG = Logger.create(__filename, electronConsole);
|
|
|
|
import * as FileType from 'file-type';
|
|
|
|
import Globals from '../../globals';
|
|
|
|
import ElementsUtil from './elements-util';
|
|
import { Channel } from '../../data-types';
|
|
import ClientController from '../../client-controller';
|
|
import Q from '../../q-module';
|
|
|
|
interface HTMLElementWithRemoveSelf extends HTMLElement {
|
|
removeSelf: (() => void);
|
|
}
|
|
|
|
interface CreateUploadOverlayProps {
|
|
guild: CombinedGuild;
|
|
channel: Channel;
|
|
resourceName: string;
|
|
resourceBuffFunc: (() => Promise<Buffer>);
|
|
resourceSizeFunc: (() => Promise<number> | number);
|
|
}
|
|
|
|
interface BindImageUploadEventsProps {
|
|
maxSize: number;
|
|
acceptedMimeTypes: string[];
|
|
onChangeStart: (() => Promise<void> | void);
|
|
onCleared: (() => Promise<void> | void);
|
|
onError: ((message: string) => Promise<void> | void);
|
|
onLoaded: ((buff: Buffer, src: string) => Promise<void> | void);
|
|
}
|
|
|
|
export default class BaseElements {
|
|
// Scraped directly from discord (#)
|
|
static TEXT_CHANNEL_ICON = {
|
|
ns: 'http://www.w3.org/2000/svg', tag: 'svg', width: 24, height: 24, viewBox: '0 0 24 24', content:
|
|
{ ns: 'http://www.w3.org/2000/svg', tag: 'path', fill: 'currentColor', 'fill-rule': 'evenodd', 'clip-rule': 'evenodd',
|
|
d: 'M5.88657 21C5.57547 21 5.3399 20.7189 5.39427 20.4126L6.00001 17H2.59511C2.28449 17 2.04905 16.7198 2.10259 16.4138L2.27759 15.4138C2.31946 15.1746 2.52722 15 2.77011 15H6.35001L7.41001 9H4.00511C3.69449 9 3.45905 8.71977 3.51259 8.41381L3.68759 7.41381C3.72946 7.17456 3.93722 7 4.18011 7H7.76001L8.39677 3.41262C8.43914 3.17391 8.64664 3 8.88907 3H9.87344C10.1845 3 10.4201 3.28107 10.3657 3.58738L9.76001 7H15.76L16.3968 3.41262C16.4391 3.17391 16.6466 3 16.8891 3H17.8734C18.1845 3 18.4201 3.28107 18.3657 3.58738L17.76 7H21.1649C21.4755 7 21.711 7.28023 21.6574 7.58619L21.4824 8.58619C21.4406 8.82544 21.2328 9 20.9899 9H17.41L16.35 15H19.7549C20.0655 15 20.301 15.2802 20.2474 15.5862L20.0724 16.5862C20.0306 16.8254 19.8228 17 19.5799 17H16L15.3632 20.5874C15.3209 20.8261 15.1134 21 14.8709 21H13.8866C13.5755 21 13.3399 20.7189 13.3943 20.4126L14 17H8.00001L7.36325 20.5874C7.32088 20.8261 7.11337 21 6.87094 21H5.88657ZM9.41045 9L8.35045 15H14.3504L15.4104 9H9.41045Z' }
|
|
};
|
|
// Scraped from discord (settings cog)
|
|
static COG = {
|
|
ns: 'http://www.w3.org/2000/svg', tag: 'svg', width: 12, height: 12, viewBox: '0 0 12 12', content:
|
|
{ ns: 'http://www.w3.org/2000/svg', tag: 'path', fill: 'currentColor', 'fill-rule': 'evenodd', 'clip-rule': 'evenodd',
|
|
d: `
|
|
M 12.00,5.00
|
|
C 12.00,5.00 12.00,7.00 12.00,7.00
|
|
12.00,7.00 10.59,7.00 10.57,7.01
|
|
10.42,7.59 10.17,8.13 9.84,8.61
|
|
9.84,8.61 10.74,9.51 10.74,9.51
|
|
10.74,9.51 9.49,10.75 9.49,10.75
|
|
9.49,10.75 8.59,9.85 8.59,9.85
|
|
8.11,10.19 7.57,10.44 6.99,10.59
|
|
6.99,10.59 6.99,12.00 6.99,12.00
|
|
6.99,12.00 4.99,12.00 4.99,12.00
|
|
4.99,12.00 4.99,10.58 4.99,10.58
|
|
4.41,10.43 3.87,10.18 3.39,9.85
|
|
3.39,9.85 2.49,10.75 2.49,10.75
|
|
2.49,10.75 1.25,9.50 1.25,9.50
|
|
1.25,9.50 2.15,8.60 2.15,8.60
|
|
1.81,8.13 1.56,7.58 1.41,7.00
|
|
1.41,7.00 0.00,7.00 0.00,7.00
|
|
0.00,7.00 0.00,5.00 0.00,5.00
|
|
0.00,5.00 1.41,5.00 1.41,5.00
|
|
1.56,4.42 1.81,3.88 2.15,3.40
|
|
2.15,3.40 1.25,2.50 1.25,2.50
|
|
1.25,2.50 2.50,1.25 2.50,1.25
|
|
2.50,1.25 3.40,2.15 3.40,2.15
|
|
3.87,1.81 4.42,1.56 5.00,1.41
|
|
5.00,1.41 5.00,0.00 5.00,0.00
|
|
5.00,0.00 7.00,0.00 7.00,0.00
|
|
7.00,0.00 7.00,1.41 7.00,1.41
|
|
7.58,1.57 8.12,1.81 8.60,2.15
|
|
8.60,2.15 9.51,1.25 9.51,1.25
|
|
9.51,1.25 10.75,2.50 10.75,2.50
|
|
10.75,2.50 9.85,3.40 9.85,3.40
|
|
10.19,3.87 10.44,4.42 10.59,5.00
|
|
10.59,5.00 12.00,5.00 12.00,5.00 Z
|
|
M 6.00,8.00
|
|
C 7.10,8.00 8.00,7.10 8.00,6.00
|
|
8.00,4.90 7.10,4.00 6.00,4.00
|
|
4.90,4.00 4.00,4.90 4.00,6.00
|
|
4.00,7.10 4.90,8.00 6.00,8.00 Z` }
|
|
};
|
|
|
|
static TRASHCAN = {
|
|
ns: 'http://www.w3.org/2000/svg', tag: 'svg', width: 16, height: 16, viewBox: '0 0 64 64', content: [
|
|
{ ns: 'http://www.w3.org/2000/svg', tag: 'path', fill: 'currentColor', 'fill-rule': 'evenodd', 'clip-rule': 'evenodd',
|
|
d: 'M 11 12 C 9.338 12 8 13.338 8 15 L 8 61 C 8 62.662 9.338 64 11 64 L 53 64 C 54.662 64 56 62.662 56 61 L 56 15 C 56 13.338 54.662 12 53 12 L 11 12 z M 17 18 C 18.662 18 20 19.338 20 21 L 20 55 C 20 56.662 18.662 58 17 58 C 15.338 58 14 56.662 14 55 L 14 21 C 14 19.338 15.338 18 17 18 z M 32 18 C 33.662 18 35 19.338 35 21 L 35 55 C 35 56.662 33.662 58 32 58 C 30.338 58 29 56.662 29 55 L 29 21 C 29 19.338 30.338 18 32 18 z M 47 18 C 48.662 18 50 19.338 50 21 L 50 55 C 50 56.662 48.662 58 47 58 C 45.338 58 44 56.662 44 55 L 44 21 C 44 19.338 45.338 18 47 18 z' },
|
|
{ ns: 'http://www.w3.org/2000/svg', tag: 'rect', fill: 'currentColor', 'fill-rule': 'evenodd', 'clip-rule': 'evenodd',
|
|
width: '56', height: '6', x: '4', y: '3', rx: '3', ry: '3' },
|
|
{ ns: 'http://www.w3.org/2000/svg', tag: 'rect', fill: 'currentColor', 'fill-rule': 'evenodd', 'clip-rule': 'evenodd',
|
|
width: '16', height: '6', x: '24', y: '0', rx: '3', ry: '3' },
|
|
]
|
|
}
|
|
|
|
static DOWNLOAD = {
|
|
ns: 'http://www.w3.org/2000/svg', tag: 'svg', width: 16, height: 16, viewBox: '0 0 64 64', content: [
|
|
{ ns: 'http://www.w3.org/2000/svg', tag: 'path', fill: 'currentColor', 'fill-rule': 'evenodd', 'clip-rule': 'evenodd',
|
|
d: 'M 32 0 A 32 32 0 0 0 0 32 A 32 32 0 0 0 32 64 A 32 32 0 0 0 64 32 A 32 32 0 0 0 32 0 z M 27 11.5 L 37 11.5 L 37 39.5 L 46 39.5 L 32 52.5 L 18 39.5 L 27 39.5 L 27 11.5 z' },
|
|
]
|
|
}
|
|
|
|
static TOKEN = {
|
|
ns: 'http://www.w3.org/2000/svg', tag: 'svg', width: 16, height: 16, viewBox: '0 0 16 16', content:
|
|
{ ns: 'http://www.w3.org/2000/svg', tag: 'path', fill: 'currentColor', 'fill-rule': 'evenodd', 'clip-rule': 'evenodd',
|
|
d: 'M 7.9160156 0 A 8.0000004 8.0000004 0 0 0 1.8710938 2.8574219 A 8.0000004 8.0000004 0 0 0 2.8574219 14.128906 A 8.0000004 8.0000004 0 0 0 14.128906 13.142578 A 8.0000004 8.0000004 0 0 0 13.142578 1.8710938 A 8.0000004 8.0000004 0 0 0 7.9160156 0 z M 5.2324219 3.2851562 A 2.4000002 2.4000002 0 0 1 6.7851562 3.8476562 A 2.4000002 2.4000002 0 0 1 7.6054688 6.1015625 L 13.367188 10.9375 L 12.595703 11.857422 L 10.90625 12.003906 L 10.445312 11.619141 L 10.373047 10.773438 L 9.9121094 10.388672 L 9.0664062 10.462891 L 8.6074219 10.076172 L 8.5332031 9.2304688 L 8.0742188 8.8457031 L 7.2285156 8.9199219 L 6.0625 7.9414062 A 2.4000002 2.4000002 0 0 1 3.6992188 7.5253906 A 2.4000002 2.4000002 0 0 1 3.4042969 4.1425781 A 2.4000002 2.4000002 0 0 1 5.2324219 3.2851562 z' }
|
|
}
|
|
|
|
static CREATE = {
|
|
ns: 'http://www.w3.org/2000/svg', tag: 'svg', width: 16, height: 16, viewBox: '0 0 16 16', content:
|
|
{ ns: 'http://www.w3.org/2000/svg', tag: 'path', fill: 'currentColor', 'fill-rule': 'evenodd', 'clip-rule': 'evenodd',
|
|
d: `M 8 0 A 8 8 0 0 0 0 8 A 8 8 0 0 0 8 16 A 8 8 0 0 0 16 8 A 8 8 0 0 0 8 0 z M 7 4 L 9 4 L 9 7 L 12 7 L 12 9 L 9 9 L 9 12 L 7 12 L 7 9 L 4 9 L 4 7 L 7 7 L 7 4 z` }
|
|
}
|
|
|
|
static UP_DOWN_ARROW = {
|
|
ns: 'http://www.w3.org/2000/svg', tag: 'svg', width: 16, height: 16, viewBox: '0 0 16 16', content: [
|
|
{ ns: 'http://www.w3.org/2000/svg', tag: 'path', fill: 'currentColor', 'fill-rule': 'evenodd', 'clip-rule': 'evenodd',
|
|
d: 'M 3,1 V 11 H 2 0 l 4,4 4,-4 h -2 -1 V 1 Z' },
|
|
{ ns: 'http://www.w3.org/2000/svg', tag: 'path', fill: 'currentColor', 'fill-rule': 'evenodd', 'clip-rule': 'evenodd',
|
|
d: 'M 11,15 V 5 H 10 8 l 4,-4 4,4 H 14 13 V 15 Z' }
|
|
]
|
|
}
|
|
|
|
static TAB_ABOVE = {
|
|
ns: 'http://www.w3.org/2000/svg', tag: 'svg', width: 12, height: 8, viewBox: '0 0 12 8', content:
|
|
{ ns: 'http://www.w3.org/2000/svg', tag: 'path', fill: 'currentColor', //'fill-rule': 'evenodd', 'clip-rule': 'evenodd',
|
|
d: `
|
|
M 6,0
|
|
L 12,8
|
|
L 0,8
|
|
Z` }
|
|
};
|
|
|
|
static TAB_BELOW = {
|
|
ns: 'http://www.w3.org/2000/svg', tag: 'svg', width: 12, height: 8, viewBox: '0 0 12 8', content:
|
|
{ ns: 'http://www.w3.org/2000/svg', tag: 'path', fill: 'currentColor', //'fill-rule': 'evenodd', 'clip-rule': 'evenodd',
|
|
d: `
|
|
M 6,8
|
|
L 12,0
|
|
L 0,0
|
|
Z` }
|
|
};
|
|
|
|
static TAB_LEFT = {
|
|
ns: 'http://www.w3.org/2000/svg', tag: 'svg', width: 10, height: 20, viewBox: '0 0 8 12', content:
|
|
{ ns: 'http://www.w3.org/2000/svg', tag: 'path', fill: 'currentColor', //'fill-rule': 'evenodd', 'clip-rule': 'evenodd',
|
|
d: `
|
|
M 0,6
|
|
L 8,12
|
|
L 8,0
|
|
Z` }
|
|
}
|
|
|
|
static createContextMenu(document: Document, content: any): HTMLElementWithRemoveSelf {
|
|
const q = new Q(document);
|
|
|
|
let element = q.create({ class: 'context', content: { class: 'menu', content: content }}) as any;
|
|
element.addEventListener('mousedown', (e: Event) => {
|
|
e.stopPropagation(); // stop the bubble
|
|
});
|
|
element.removeSelf = () => {
|
|
if (element.parentElement) {
|
|
element.parentElement.removeChild(element);
|
|
}
|
|
document.body.removeEventListener('mousedown', element.removeSelf);
|
|
};
|
|
document.body.addEventListener('mousedown', element.removeSelf);
|
|
return element as HTMLElementWithRemoveSelf;
|
|
}
|
|
|
|
static createOverlay(document: Document, content: any): HTMLElementWithRemoveSelf {
|
|
const q = new Q(document);
|
|
|
|
let wasDownInternal = false; // because 'click' fires on the overlay element anyway
|
|
let element = q.create({ class: 'overlay', content: content }) as any;
|
|
let onKeyEscape = (e: KeyboardEvent) => {
|
|
if (e.key == 'Escape') {
|
|
element.removeSelf();
|
|
}
|
|
};
|
|
window.addEventListener('keydown', onKeyEscape);
|
|
element.addEventListener('mouseup', () => {
|
|
if (wasDownInternal) {
|
|
wasDownInternal = false;
|
|
return;
|
|
}
|
|
element.removeSelf();
|
|
});
|
|
element.removeSelf = () => {
|
|
if (element.parentElement) {
|
|
element.parentElement.removeChild(element);
|
|
}
|
|
window.removeEventListener('keydown', onKeyEscape);
|
|
}
|
|
q.$$$(element, '.content').addEventListener('click', (e) => {
|
|
e.stopPropagation(); // prevent the element from closing if the content is clicked on
|
|
});
|
|
q.$$$(element, '.content').addEventListener('mousedown', (e) => {
|
|
wasDownInternal = true;
|
|
});
|
|
|
|
return element as HTMLElementWithRemoveSelf;
|
|
}
|
|
|
|
static createUploadOverlay(document: Document, props: CreateUploadOverlayProps): HTMLElementWithRemoveSelf {
|
|
const q = new Q(document);
|
|
|
|
const { server, channel, resourceName, resourceBuffFunc, resourceSizeFunc } = props;
|
|
|
|
let element = BaseElements.createOverlay(document, { class: 'content upload', content: [
|
|
{ class: 'title', content: [
|
|
{ tag: 'img', src: './img/loading.svg', alt: resourceName },
|
|
{ class: 'right', content: [
|
|
{ class: 'name', content: resourceName },
|
|
{ class: 'size', content: '? B' }
|
|
] }
|
|
] },
|
|
{ class: 'text-input', placeholder: 'Add a comment (optional)', contenteditable: 'plaintext-only' },
|
|
{ class: 'lower', content: [
|
|
{ class: 'error' },
|
|
{ class: 'buttons', content: [
|
|
{ class: 'button upload', content: 'Upload to #' + channel.name }
|
|
] }
|
|
] }
|
|
|
|
] });
|
|
q.$$$(element, '.text-input').addEventListener('keydown', async (e) => {
|
|
if (e.key == 'Enter' && !e.shiftKey) {
|
|
e.preventDefault();
|
|
q.$$$(element, '.button.upload').click();
|
|
}
|
|
});
|
|
q.$$$(element, '.text-input').addEventListener('keyup', (e) => {
|
|
if (e.key == 'Backspace') {
|
|
if (q.$$$(element, '.text-input').innerText == '\n') { // sometimes, a \n gets left behind
|
|
q.$$$(element, '.text-input').innerText = '';
|
|
}
|
|
}
|
|
});
|
|
|
|
let sending = false; // prevent double-clicking from sending 2 messages
|
|
q.$$$(element, '.button.upload').addEventListener('click', async () => {
|
|
if (sending) {
|
|
return;
|
|
}
|
|
sending = true;
|
|
if (!server.isVerified) {
|
|
LOG.warn('client attempted to send message with resource while not verified');
|
|
q.$$$(element, '.error').innerText = 'Not Connected to Server';
|
|
q.$$$(element, '.button.upload').innerText = 'Try Again';
|
|
await ElementsUtil.shakeElement(q.$$$(element, '.button.upload'), 400);
|
|
sending = false;
|
|
return;
|
|
}
|
|
q.$$$(element, '.button.upload').innerText = 'Uploading...';
|
|
let text: string | null = q.$$$(element, '.text-input').innerText;
|
|
text = text.trim(); // this is not done server-side, just a client-side 'feature'
|
|
if (text == '') {
|
|
text = null;
|
|
}
|
|
if (text && text.length > Globals.MAX_TEXT_MESSAGE_LENGTH) {
|
|
LOG.warn('Attempted to upload oversized resource text message: ' + text.length + ' / ' + Globals.MAX_TEXT_MESSAGE_LENGTH + ' characters');
|
|
q.$$$(element, '.error').innerText = 'Text too long: ' + text.length + ' / ' + Globals.MAX_TEXT_MESSAGE_LENGTH + ' characters';
|
|
q.$$$(element, '.button.upload').innerText = 'Try Again';
|
|
await ElementsUtil.shakeElement(q.$$$(element, '.button.upload'), 400);
|
|
sending = false;
|
|
return;
|
|
}
|
|
let resourceBuff: Buffer;
|
|
try {
|
|
resourceBuff = await resourceBuffFunc();
|
|
} catch (e) {
|
|
LOG.error('Error loading resource', e);
|
|
q.$$$(element, '.error').innerText = 'Error loading resource. Was it moved?';
|
|
q.$$$(element, '.button.upload').innerText = 'Try Again';
|
|
await ElementsUtil.shakeElement(q.$$$(element, '.button.upload'), 400);
|
|
sending = false;
|
|
return;
|
|
}
|
|
if (resourceBuff.length > Globals.MAX_RESOURCE_SIZE) {
|
|
LOG.warn('Attempted to upload oversized resource: ' + ElementsUtil.humanSize(resourceBuff.length) + ' > ' + ElementsUtil.humanSize(Globals.MAX_RESOURCE_SIZE));
|
|
q.$$$(element, '.error').innerText = 'Resource too large: ' + ElementsUtil.humanSize(resourceBuff.length) + ' > ' + ElementsUtil.humanSize(Globals.MAX_RESOURCE_SIZE);
|
|
q.$$$(element, '.button.upload').innerText = 'Try Again';
|
|
await ElementsUtil.shakeElement(q.$$$(element, '.button.upload'), 400);
|
|
sending = false;
|
|
return;
|
|
}
|
|
try {
|
|
await server.sendMessageWithResource(channel.id, text, resourceBuff, resourceName);
|
|
} catch (e) {
|
|
q.$$$(element, '.error').innerText = 'Error uploading resource.';
|
|
q.$$$(element, '.button.upload').innerText = 'Try Again';
|
|
await ElementsUtil.shakeElement(q.$$$(element, '.button.upload'), 400);
|
|
sending = false;
|
|
return;
|
|
}
|
|
element.removeSelf(); // get rid of the overlay after the message gets sent
|
|
});
|
|
(async () => {
|
|
try {
|
|
let size = await resourceSizeFunc();
|
|
q.$$$(element, '.title .size').innerText = ElementsUtil.humanSize(size);
|
|
} catch (e) {
|
|
LOG.error('Error fetching file stat', e);
|
|
q.$$$(element, '.title .size').innerText = 'Unknown Size';
|
|
}
|
|
})();
|
|
(async () => {
|
|
if (
|
|
resourceName.toLowerCase().endsWith('.png') ||
|
|
resourceName.toLowerCase().endsWith('.jpg') ||
|
|
resourceName.toLowerCase().endsWith('.jpeg') ||
|
|
resourceName.toLowerCase().endsWith('.gif')
|
|
) {
|
|
try {
|
|
let resourceBuff = await resourceBuffFunc();
|
|
let resourceSrc = await ElementsUtil.getImageBufferSrc(resourceBuff);
|
|
(q.$$$(element, '.title img') as HTMLImageElement).src = resourceSrc;
|
|
} catch (e) {
|
|
LOG.error('Error loading image resource', e);
|
|
(q.$$$(element, 'img.avatar') as HTMLImageElement).src = './img/file-icon.png'; // not the error icon here
|
|
}
|
|
} else {
|
|
(q.$$$(element, '.title img') as HTMLImageElement).src = './img/file-icon.png';
|
|
}
|
|
})();
|
|
return element;
|
|
}
|
|
|
|
static bindImageUploadEvents(element: HTMLInputElement, props: BindImageUploadEventsProps): void {
|
|
const { maxSize, acceptedMimeTypes, onChangeStart, onCleared, onError, onLoaded } = props;
|
|
|
|
element.addEventListener('change', async () => {
|
|
await onChangeStart();
|
|
let files = element.files;
|
|
if (!files || files.length == 0) {
|
|
await onCleared();
|
|
return;
|
|
}
|
|
let file = files[0]; // only one file at a time
|
|
if (file.size > maxSize) {
|
|
await onError('Image Too Large. ' + ElementsUtil.humanSize(file.size) + ' > ' + ElementsUtil.humanSize(maxSize));
|
|
return;
|
|
}
|
|
let buff = Buffer.from(await file.arrayBuffer());
|
|
let typeResult = await FileType.fromBuffer(buff);
|
|
if (!typeResult || !acceptedMimeTypes.includes(typeResult.mime)) {
|
|
await onError('Invalid Image Type. Accepted Types: ' + acceptedMimeTypes.join(', '));
|
|
return;
|
|
}
|
|
let src: string | null = null;
|
|
try {
|
|
src = await ElementsUtil.getImageBufferSrc(buff);
|
|
} catch (e) {
|
|
await onError('Unable Parse Image');
|
|
return;
|
|
}
|
|
await onLoaded(buff, src);
|
|
});
|
|
}
|
|
}
|