BetterDiscordAddons/Plugins/ImageGallery/ImageGallery.plugin.js

182 lines
7.0 KiB
JavaScript
Raw Normal View History

2019-09-20 22:32:52 +02:00
//META{"name":"ImageGallery","website":"https://github.com/mwittrien/BetterDiscordAddons/tree/master/Plugins/ImageGallery","source":"https://raw.githubusercontent.com/mwittrien/BetterDiscordAddons/master/Plugins/ImageGallery/ImageGallery.plugin.js"}*//
2018-10-11 10:21:26 +02:00
class ImageGallery {
2019-01-11 21:57:59 +01:00
getName () {return "ImageGallery";}
2020-01-17 20:17:52 +01:00
getVersion () {return "1.6.2";}
2019-01-11 21:57:59 +01:00
getAuthor () {return "DevilBro";}
getDescription () {return "Allows the user to browse through images sent inside the same message.";}
2019-01-26 22:45:19 +01:00
2019-09-04 12:34:02 +02:00
constructor () {
2019-02-18 11:34:42 +01:00
this.changelog = {
2020-01-17 20:17:52 +01:00
"fixed":[["Spoiler Images","No longer included unrevealed as spoiler marked images in the gallery preview"]],
2019-11-14 23:15:44 +01:00
"improved":[["New Library Structure & React","Restructured my Library and switched to React rendering instead of DOM manipulation"]]
2019-02-18 11:34:42 +01:00
};
2019-09-04 12:34:02 +02:00
2019-11-14 17:56:26 +01:00
this.patchedModules = {
after: {
2019-11-14 23:15:44 +01:00
ImageModal: ["render", "componentDidMount"]
2019-11-14 17:56:26 +01:00
}
2019-09-04 12:34:02 +02:00
};
}
initConstructor () {
2018-10-11 10:21:26 +02:00
this.eventFired = false;
2019-01-26 22:45:19 +01:00
2019-11-24 09:18:13 +01:00
this.css = `
${BDFDB.dotCNS._imagegallerygallery + BDFDB.dotCN.imagewrapper + BDFDB.dotCN._imagegalleryprevious},
${BDFDB.dotCNS._imagegallerygallery + BDFDB.dotCN.imagewrapper + BDFDB.dotCN._imagegallerynext} {
position: fixed;
z-index: -1;
}
${BDFDB.dotCNS._imagegallerygallery + BDFDB.dotCN.imagewrapper + BDFDB.dotCN._imagegalleryprevious} {
2018-10-11 10:21:26 +02:00
right: 90%;
}
2019-11-24 09:18:13 +01:00
${BDFDB.dotCNS._imagegallerygallery + BDFDB.dotCN.imagewrapper + BDFDB.dotCN._imagegallerynext} {
2018-10-11 10:21:26 +02:00
left: 90%;
}`;
}
//legacy
load () {}
start () {
2020-01-17 19:50:31 +01:00
if (!window.BDFDB) window.BDFDB = {myPlugins:{}};
if (window.BDFDB && window.BDFDB.myPlugins && typeof window.BDFDB.myPlugins == "object") window.BDFDB.myPlugins[this.getName()] = this;
2020-01-21 12:56:26 +01:00
let libraryScript = document.querySelector("head script#BDFDBLibraryScript");
2019-05-26 13:55:26 +02:00
if (!libraryScript || (performance.now() - libraryScript.getAttribute("date")) > 600000) {
2018-10-11 10:21:26 +02:00
if (libraryScript) libraryScript.remove();
libraryScript = document.createElement("script");
2019-05-26 13:55:26 +02:00
libraryScript.setAttribute("id", "BDFDBLibraryScript");
2018-10-11 10:21:26 +02:00
libraryScript.setAttribute("type", "text/javascript");
2019-10-18 10:56:41 +02:00
libraryScript.setAttribute("src", "https://mwittrien.github.io/BetterDiscordAddons/Plugins/BDFDB.min.js");
2019-01-17 23:48:29 +01:00
libraryScript.setAttribute("date", performance.now());
2020-01-14 00:06:07 +01:00
libraryScript.addEventListener("load", _ => {this.initialize();});
2018-10-11 10:21:26 +02:00
document.head.appendChild(libraryScript);
}
2020-01-17 19:50:31 +01:00
else if (window.BDFDB && typeof BDFDB === "object" && BDFDB.loaded) this.initialize();
2020-01-14 00:06:07 +01:00
this.startTimeout = setTimeout(_ => {
2019-11-01 10:27:07 +01:00
try {return this.initialize();}
catch (err) {console.error(`%c[${this.getName()}]%c`, "color: #3a71c1; font-weight: 700;", "", "Fatal Error: Could not initiate plugin! " + err);}
}, 30000);
2018-10-11 10:21:26 +02:00
}
initialize () {
2020-01-17 19:50:31 +01:00
if (window.BDFDB && typeof BDFDB === "object" && BDFDB.loaded) {
2019-01-22 11:05:54 +01:00
if (this.started) return;
2019-10-22 18:55:25 +02:00
BDFDB.PluginUtils.init(this);
2019-01-26 22:45:19 +01:00
2019-10-22 18:55:25 +02:00
BDFDB.ModuleUtils.forceAllUpdates(this);
2018-10-11 10:21:26 +02:00
}
2019-11-01 10:14:50 +01:00
else console.error(`%c[${this.getName()}]%c`, "color: #3a71c1; font-weight: 700;", "", "Fatal Error: Could not load BD functions!");
2018-10-11 10:21:26 +02:00
}
stop () {
2020-01-17 19:50:31 +01:00
if (window.BDFDB && typeof BDFDB === "object" && BDFDB.loaded) {
2019-10-22 11:37:23 +02:00
this.stopping = true;
2019-11-14 23:15:44 +01:00
BDFDB.ModuleUtils.forceAllUpdates(this);
2019-01-26 22:45:19 +01:00
2019-10-22 18:55:25 +02:00
BDFDB.PluginUtils.clear(this);
2018-10-11 10:21:26 +02:00
}
}
2019-01-26 22:45:19 +01:00
2018-10-11 10:21:26 +02:00
// begin of own functions
2019-01-26 22:45:19 +01:00
2019-11-14 23:15:44 +01:00
processImageModal (e) {
let message = this.getMessageGroupOfImage(e.instance.props.src);
if (message) {
if (e.returnvalue) {
2020-01-17 20:17:52 +01:00
let images = Array.from(message.querySelectorAll(BDFDB.dotCNS.imagewrapper + "img")).filter(img => !BDFDB.DOMUtils.getParent(BDFDB.dotCN.spoilerhidden, img));
let next, previous;
2019-11-14 23:15:44 +01:00
for (let i = 0; i < images.length; i++) if (this.getSrcOfImage(e.instance.props.src) == this.getSrcOfImage(images[i])) {
next = this.getSrcOfImage(images[i+1]);
previous = this.getSrcOfImage(images[i-1]);
break;
2018-10-11 10:21:26 +02:00
}
2019-11-14 23:15:44 +01:00
if (next) {
if (e.instance.nextRef) e.returnvalue.props.children.splice(1, 0, e.instance.nextRef);
else this.loadImage(e.instance, this.getSrcOfImage(next), "next");
2019-01-11 21:57:59 +01:00
}
2019-11-14 23:15:44 +01:00
if (previous) {
if (e.instance.previousRef) e.returnvalue.props.children.push(e.instance.previousRef);
else this.loadImage(e.instance, this.getSrcOfImage(previous), "previous");
2018-10-11 10:21:26 +02:00
}
}
2019-11-14 23:15:44 +01:00
if (e.node) {
2019-11-24 09:18:13 +01:00
BDFDB.DOMUtils.addClass(BDFDB.DOMUtils.getParent(BDFDB.dotCN.modal, e.node), BDFDB.disCN._imagegallerygallery);
2019-11-14 23:15:44 +01:00
this.cleanUpListeners();
document.keydownImageGalleryListener = event => {
if (!document.contains(e.node)) this.cleanUpListeners();
else if (!this.eventFired) {
this.eventFired = true;
if (event.keyCode == 37) this.switchImages(e.instance, "previous");
else if (event.keyCode == 39) this.switchImages(e.instance, "next");
}
};
document.keyupImageGalleryListener = _ => {
this.eventFired = false;
if (!document.contains(e.node)) this.cleanUpListeners();
};
document.addEventListener("keydown", document.keydownImageGalleryListener);
document.addEventListener("keyup", document.keyupImageGalleryListener);
2018-10-11 10:21:26 +02:00
}
}
2019-01-17 23:48:29 +01:00
}
2019-01-26 22:45:19 +01:00
2019-11-14 23:15:44 +01:00
getMessageGroupOfImage (src) {
if (src) for (let group of document.querySelectorAll(BDFDB.dotCN.messagegroup)) for (let img of group.querySelectorAll(BDFDB.dotCNS.imagewrapper + "img")) if (img.src && this.getSrcOfImage(img) == this.getSrcOfImage(src)) return group;
return null;
2018-10-11 10:21:26 +02:00
}
2019-01-26 22:45:19 +01:00
2019-11-14 23:15:44 +01:00
getSrcOfImage (img) {
if (!img) return null;
return (typeof img == "string" ? img : (img.src || (img.querySelector("canvas") ? img.querySelector("canvas").src : ""))).split("?width=")[0];
}
loadImage (instance, src, type) {
let imagethrowaway = document.createElement("img");
imagethrowaway.src = src;
imagethrowaway.onload = _ => {
2020-01-17 20:17:52 +01:00
let arects = BDFDB.DOMUtils.getRects(document.querySelector(BDFDB.dotCN.appmount));
let resizeY = (arects.height/imagethrowaway.naturalHeight) * 0.65, resizeX = (arects.width/imagethrowaway.naturalWidth) * 0.8;
let resize = resizeX < resizeY ? resizeX : resizeY;
let newHeight = imagethrowaway.naturalHeight * resize;
let newWidth = imagethrowaway.naturalWidth * resize;
2019-11-14 23:15:44 +01:00
instance[type + "Ref"] = BDFDB.ReactUtils.createElement(BDFDB.LibraryComponents.LazyImage, {
2019-11-24 09:18:13 +01:00
className: BDFDB.disCN[`_imagegallery${type}`],
2019-11-14 23:15:44 +01:00
src: src,
height: imagethrowaway.naturalHeight,
width: imagethrowaway.naturalWidth,
maxHeight: newHeight,
maxWidth: newWidth,
onClick: _ => {this.switchImages(instance, type);}
});
BDFDB.ReactUtils.forceUpdate(instance);
2018-10-11 10:21:26 +02:00
};
}
2019-11-14 23:15:44 +01:00
switchImages (instance, type) {
let imageRef = instance[type + "Ref"];
if (!imageRef) return;
delete instance.previousRef;
delete instance.nextRef;
instance.props.original = imageRef.props.src;
instance.props.placeholder = imageRef.props.src;
instance.props.src = imageRef.props.src;
instance.props.height = imageRef.props.height;
instance.props.width = imageRef.props.width;
BDFDB.ReactUtils.forceUpdate(instance);
}
cleanUpListeners () {
document.removeEventListener("keydown", document.keydownImageGalleryListener);
document.removeEventListener("keyup", document.keyupImageGalleryListener);
delete document.keydownImageGalleryListener;
delete document.keyupImageGalleryListener;
2018-10-11 10:21:26 +02:00
}
}