BetterDiscordAddons/Plugins/ImageGallery/ImageGallery.plugin.js

182 lines
6.6 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";}
2019-11-14 23:15:44 +01:00
getVersion () {return "1.6.0";}
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 = {
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
2018-10-11 10:21:26 +02:00
this.css = `
2019-11-14 23:15:44 +01:00
.image-gallery ${BDFDB.dotCN.imagewrapper}.previous,
2018-10-11 10:21:26 +02:00
.image-gallery ${BDFDB.dotCN.imagewrapper}.next {
position: absolute;
}
2019-01-26 22:45:19 +01:00
2019-11-14 23:15:44 +01:00
.image-gallery ${BDFDB.dotCN.imagewrapper}.previous {
2018-10-11 10:21:26 +02:00
right: 90%;
}
2019-01-26 22:45:19 +01:00
2018-10-11 10:21:26 +02:00
.image-gallery ${BDFDB.dotCN.imagewrapper}.next {
left: 90%;
}`;
}
//legacy
load () {}
start () {
2019-02-04 09:13:15 +01:00
if (!global.BDFDB) global.BDFDB = {myPlugins:{}};
if (global.BDFDB && global.BDFDB.myPlugins && typeof global.BDFDB.myPlugins == "object") global.BDFDB.myPlugins[this.getName()] = this;
2019-05-26 13:55:26 +02:00
var libraryScript = document.querySelector('head script#BDFDBLibraryScript');
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());
2019-05-26 13:55:26 +02:00
libraryScript.addEventListener("load", () => {this.initialize();});
2018-10-11 10:21:26 +02:00
document.head.appendChild(libraryScript);
}
2019-01-17 23:48:29 +01:00
else if (global.BDFDB && typeof BDFDB === "object" && BDFDB.loaded) this.initialize();
2019-11-01 10:27:07 +01:00
this.startTimeout = setTimeout(() => {
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 () {
2019-01-17 23:48:29 +01:00
if (global.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 () {
2019-01-17 23:48:29 +01:00
if (global.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) {
let images = message.querySelectorAll(BDFDB.dotCNS.imagewrapper + "img");
var next, previous;
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) {
BDFDB.DOMUtils.addClass(BDFDB.DOMUtils.getParent(BDFDB.dotCN.modal, e.node), "image-gallery");
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 = _ => {
var arects = BDFDB.DOMUtils.getRects(document.querySelector(BDFDB.dotCN.appmount));
var resizeY = (arects.height/imagethrowaway.naturalHeight) * 0.65, resizeX = (arects.width/imagethrowaway.naturalWidth) * 0.8;
2018-10-11 10:21:26 +02:00
var resize = resizeX < resizeY ? resizeX : resizeY;
2019-11-14 23:15:44 +01:00
var newHeight = imagethrowaway.naturalHeight * resize;
var newWidth = imagethrowaway.naturalWidth * resize;
instance[type + "Ref"] = BDFDB.ReactUtils.createElement(BDFDB.LibraryComponents.LazyImage, {
className: type,
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
}
}