BetterDiscordAddons/Plugins/ImageZoom/ImageZoom.plugin.js

191 lines
9.6 KiB
JavaScript
Raw Normal View History

2019-09-20 22:32:52 +02:00
//META{"name":"ImageZoom","website":"https://github.com/mwittrien/BetterDiscordAddons/tree/master/Plugins/ImageZoom","source":"https://raw.githubusercontent.com/mwittrien/BetterDiscordAddons/master/Plugins/ImageZoom/ImageZoom.plugin.js"}*//
2019-03-12 12:08:37 +01:00
class ImageZoom {
getName () {return "ImageZoom";}
2019-09-11 13:19:17 +02:00
getVersion () {return "1.0.4";}
2019-03-12 12:08:37 +01:00
getAuthor () {return "DevilBro";}
getDescription () {return "Allows you to zoom in opened Images by holding left clicking on them in the Image Modal.";}
2019-09-04 12:34:02 +02:00
constructor () {
2019-05-12 09:58:43 +02:00
this.changelog = {
2019-09-11 13:19:17 +02:00
"fixed":[["Light Theme Update","Fixed bugs for the Light Theme Update, which broke 99% of my plugins"]]
2019-05-12 09:58:43 +02:00
};
2019-09-04 12:34:02 +02:00
2019-03-12 12:08:37 +01:00
this.patchModules = {
2019-05-12 09:58:43 +02:00
"ImageModal":["componentDidMount","componentWillUnmount"],
"Icon":["componentDidMount","componentWillUnmount"],
2019-09-04 12:34:02 +02:00
};
}
2019-03-12 12:08:37 +01:00
2019-09-04 12:34:02 +02:00
initConstructor () {
2019-03-12 12:08:37 +01:00
this.css = `
.imagezoom-lense {
border: 2px solid rgb(114, 137, 218);
}
.imagezoom-backdrop {
position: absolute !important;
top: 0 !important;
right: 0 !important;
bottom: 0 !important;
left: 0 !important;
z-index: 8000 !important;
}`;
this.defaults = {
settings: {
2019-03-12 12:31:50 +01:00
zoomlevel: {value:2, digits:1, min:1, max:10, unit:"x", name:"Zoom Level"},
lensesize: {value:200, digits:0, min:50, max:1000, unit:"px", name:"Lense Size"}
2019-03-12 12:08:37 +01:00
}
};
}
//legacy
load () {}
start () {
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) {
2019-03-12 12:08:37 +01:00
if (libraryScript) libraryScript.remove();
libraryScript = document.createElement("script");
2019-05-26 13:55:26 +02:00
libraryScript.setAttribute("id", "BDFDBLibraryScript");
2019-03-12 12:08:37 +01: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-03-12 12:08:37 +01:00
libraryScript.setAttribute("date", performance.now());
2019-05-26 13:55:26 +02:00
libraryScript.addEventListener("load", () => {this.initialize();});
2019-03-12 12:08:37 +01:00
document.head.appendChild(libraryScript);
}
else if (global.BDFDB && typeof BDFDB === "object" && BDFDB.loaded) this.initialize();
this.startTimeout = setTimeout(() => {this.initialize();}, 30000);
}
initialize () {
if (global.BDFDB && typeof BDFDB === "object" && BDFDB.loaded) {
if (this.started) return;
2019-10-22 18:55:25 +02:00
BDFDB.PluginUtils.init(this);
2019-03-12 12:08:37 +01:00
2019-10-22 18:55:25 +02:00
BDFDB.ModuleUtils.forceAllUpdates(this);
2019-03-12 12:08:37 +01:00
}
2019-10-22 18:55:25 +02:00
else console.error(`%c[${this.getName()}]%c`, 'color: #3a71c1; font-weight: 700;', '', 'Fatal Error: Could not load BD functions!');
2019-03-12 12:08:37 +01:00
}
stop () {
if (global.BDFDB && typeof BDFDB === "object" && BDFDB.loaded) {
2019-10-22 11:37:23 +02:00
this.stopping = true;
2019-03-12 12:08:37 +01:00
for (let img of document.querySelectorAll(BDFDB.dotCNS.imagewrapper + "img")) if (img.ImageZoomMouseDownListener) {
img.removeEventListener("mousedown", img.ImageZoomMouseDownListener);
delete img.ImageZoomMouseDownListener;
img.removeAttribute("draggable");
}
2019-09-04 12:34:02 +02:00
2019-03-12 12:08:37 +01:00
BDFDB.removeEles(".imagezoom-contextmenu", ".imagezoom-separator", ".imagezoom-settings", ".imagezoom-lense", ".imagezoom-backdrop");
2019-09-04 12:34:02 +02:00
2019-10-22 18:55:25 +02:00
BDFDB.PluginUtils.clear(this);
2019-03-12 12:08:37 +01:00
}
}
// begin of own functions
2019-09-11 13:19:17 +02:00
processImageModal (instance, wrapper, returnvalue, methodnames) {
2019-03-12 12:08:37 +01:00
if (methodnames.includes("componentDidMount")) {
let modal = BDFDB.getParentEle(BDFDB.dotCN.modal, wrapper);
if (!modal) return;
2019-03-12 14:28:19 +01:00
let inner = modal.querySelector(BDFDB.dotCN.modalinner);
if (!inner) return;
2019-03-12 12:08:37 +01:00
let start = performance.now();
let waitForImg = setInterval(() => {
2019-03-12 12:58:39 +01:00
let img = modal.querySelector(BDFDB.dotCNS.imagewrapper + "img," + BDFDB.dotCNS.imagewrapper + "video");
2019-05-16 10:49:51 +02:00
if (img && img.src && !BDFDB.containsClass(img, BDFDB.disCN.imageplaceholder)) {
2019-03-12 12:08:37 +01:00
clearInterval(waitForImg);
img.setAttribute("draggable", "false");
2019-03-12 14:28:19 +01:00
inner.firstElementChild.appendChild(BDFDB.htmlToElement(`<span class="${BDFDB.disCN.downloadlink} imagezoom-separator" style="margin: 0px 5px;"> | </div>`));
2019-10-19 11:41:39 +02:00
let settingslink = BDFDB.htmlToElement(`<span class="${BDFDB.disCN.downloadlink} imagezoom-settings">Zoom ${BDFDB.LanguageUtils.LanguageStrings.SETTINGS}</div>`);
2019-03-12 14:28:19 +01:00
inner.firstElementChild.appendChild(settingslink);
2019-03-12 12:08:37 +01:00
let openContext = e => {
let settings = BDFDB.getAllData(this, "settings");
2019-09-11 13:19:17 +02:00
let items = [];
2019-10-22 18:55:25 +02:00
for (let type in settings) items.push(BDFDB.ReactUtils.createElement(BDFDB.LibraryComponents.ContextMenuSliderItem, {
2019-09-11 13:19:17 +02:00
label: this.defaults.settings[type].name + ": " + settings[type] + this.defaults.settings[type].unit,
className: `BDFDB-contextMenuSliderItem ${this.name}-contextMenuSliderItem ${this.name}-${type}-contextMenuSliderItem`,
type,
defaultValue: BDFDB.mapRange([this.defaults.settings[type].min, this.defaults.settings[type].max], [0, 100], settings[type]),
onValueChange: value => {
BDFDB.saveData(type, Math.round(BDFDB.mapRange([0, 100], [this.defaults.settings[type].min, this.defaults.settings[type].max], value)), this, "settings");
},
onValueRender: value => {
2019-10-22 18:55:25 +02:00
setImmediate(() => {for (let slider of document.querySelectorAll(BDFDB.dotCN.contextmenuitemslider)) if (BDFDB.ReactUtils.getValue(slider, "return.memoizedProps.type") == type) {
2019-09-11 13:19:17 +02:00
value = Math.round(BDFDB.mapRange([0, 100], [this.defaults.settings[type].min, this.defaults.settings[type].max], value));
let label = slider.querySelector(BDFDB.dotCN.contextmenulabel);
if (label) label.innerText = this.defaults.settings[type].name + ": " + value + this.defaults.settings[type].unit;
let bubble = slider.querySelector(BDFDB.dotCN.sliderbubble);
if (bubble) bubble.innerText = value + this.defaults.settings[type].unit;
break;
}});
}
}));
2019-10-22 18:55:25 +02:00
const itemGroup = BDFDB.ReactUtils.createElement(BDFDB.LibraryComponents.ContextMenuItemGroup, {
2019-09-11 13:19:17 +02:00
className: `BDFDB-contextMenuItemGroup ${this.name}-contextMenuItemGroup`,
children: items
});
BDFDB.openContextMenu(this, e, itemGroup);
2019-03-12 12:08:37 +01:00
};
settingslink.addEventListener("click", openContext);
settingslink.addEventListener("contextmenu", openContext);
img.ImageZoomMouseDownListener = e => {
2019-10-22 18:55:25 +02:00
BDFDB.ListenerUtils.stopEvent(e);
2019-03-12 12:08:37 +01:00
BDFDB.appendLocalStyle("ImageZoomCrossHair", "* {cursor: crosshair !important;}");
2019-09-04 12:34:02 +02:00
2019-03-12 12:08:37 +01:00
let imgrects = BDFDB.getRects(img);
2019-03-12 12:58:39 +01:00
let settings = BDFDB.getAllData(this, "settings");
2019-09-04 12:34:02 +02:00
2019-05-12 09:58:43 +02:00
let lense = BDFDB.htmlToElement(`<div class="imagezoom-lense" style="clip-path: circle(${(settings.lensesize/2) + 2}px at center) !important; border-radius: 50% !important; pointer-events: none !important; z-index: 10000 !important; width: ${settings.lensesize}px !important; height: ${settings.lensesize}px !important; position: fixed !important;"><div class="imagezoom-lense-inner" style="position: absolute !important; top: 0 !important; right: 0 !important; bottom: 0 !important; left: 0 !important; clip-path: circle(${settings.lensesize/2}px at center) !important;"><${img.tagName} class="imagezoom-pane" src="${img.src}" style="width: ${imgrects.width * settings.zoomlevel}px; height: ${imgrects.height * settings.zoomlevel}px; position: fixed !important;"${img.tagName == "VIDEO" ? " loop autoplay" : ""}></${img.tagName}></div></div>`);
let pane = lense.querySelector(".imagezoom-pane");
2019-03-12 12:08:37 +01:00
let backdrop = BDFDB.htmlToElement(`<div class="imagezoom-backdrop" style="background: rgba(0,0,0,0.2) !important;"></div>`);
document.querySelector(BDFDB.dotCN.appmount).appendChild(lense);
document.querySelector(BDFDB.dotCN.appmount).appendChild(backdrop);
2019-09-04 12:34:02 +02:00
2019-03-12 12:08:37 +01:00
let lenserects = BDFDB.getRects(lense), panerects = BDFDB.getRects(pane);
let halfW = lenserects.width / 2, halfH = lenserects.height / 2;
let minX = imgrects.left, maxX = minX + imgrects.width;
let minY = imgrects.top, maxY = minY + imgrects.height;
lense.style.setProperty("left", e.clientX - halfW + "px", "important");
lense.style.setProperty("top", e.clientY - halfH + "px", "important");
pane.style.setProperty("left", imgrects.left + ((settings.zoomlevel - 1) * (imgrects.left - e.clientX)) + "px", "important");
pane.style.setProperty("top", imgrects.top + ((settings.zoomlevel - 1) * (imgrects.top - e.clientY)) + "px", "important");
2019-09-04 12:34:02 +02:00
2019-03-12 12:08:37 +01:00
let dragging = e2 => {
let x = e2.clientX > maxX ? maxX - halfW : e2.clientX < minX ? minX - halfW : e2.clientX - halfW;
let y = e2.clientY > maxY ? maxY - halfH : e2.clientY < minY ? minY - halfH : e2.clientY - halfH;
lense.style.setProperty("left", x + "px", "important");
lense.style.setProperty("top", y + "px", "important");
pane.style.setProperty("left", imgrects.left + ((settings.zoomlevel - 1) * (imgrects.left - x - halfW)) + "px", "important");
pane.style.setProperty("top", imgrects.top + ((settings.zoomlevel - 1) * (imgrects.top - y - halfH)) + "px", "important");
};
let releasing = e2 => {
BDFDB.removeLocalStyle('ImageZoomCrossHair');
document.removeEventListener("mousemove", dragging);
document.removeEventListener("mouseup", releasing);
BDFDB.removeEles(lense, backdrop);
};
document.addEventListener("mousemove", dragging);
document.addEventListener("mouseup", releasing);
};
img.addEventListener("mousedown", img.ImageZoomMouseDownListener);
}
else if (performance.now() - start > 10000) {
clearInterval(waitForImg);
}
}, 100);
}
else if (methodnames.includes("componentWillUnmount")) {
BDFDB.removeEles(".imagezoom-contextmenu", ".imagezoom-separator", ".imagezoom-settings", ".imagezoom-lense", ".imagezoom-backdrop");
}
}
}