BetterDiscordAddons/Plugins/ImageZoom/ImageZoom.plugin.js

274 lines
11 KiB
JavaScript
Raw Normal View History

2020-02-27 08:44:03 +01:00
//META{"name":"ImageZoom","authorId":"278543574059057154","invite":"Jx3TjNS","donate":"https://www.paypal.me/MircoWittrien","patreon":"https://www.patreon.com/MircoWittrien","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
2020-02-10 22:45:39 +01:00
var ImageZoom = (_ => {
return class ImageZoom {
getName () {return "ImageZoom";}
2019-03-12 12:08:37 +01:00
2020-05-01 09:21:01 +02:00
getVersion () {return "1.1.2";}
2019-03-12 12:08:37 +01:00
2020-02-10 22:45:39 +01:00
getAuthor () {return "DevilBro";}
2019-03-12 12:08:37 +01:00
2020-02-10 22:45:39 +01:00
getDescription () {return "Allows you to zoom in opened Images by holding left clicking on them in the Image Modal.";}
2019-03-12 12:08:37 +01:00
2020-02-10 22:45:39 +01:00
constructor () {
this.changelog = {
2020-05-01 09:21:01 +02:00
"fixed":[["Styling && Functionality","Fixed the style for the new image modal layout"]]
2020-02-10 22:45:39 +01:00
};
2019-09-04 12:34:02 +02:00
2020-02-10 22:45:39 +01:00
this.patchedModules = {
after: {
ImageModal: ["render", "componentDidMount"],
LazyImage: "componentDidMount"
}
};
}
2019-03-12 12:08:37 +01:00
2020-02-10 22:45:39 +01:00
initConstructor () {
this.css = `
${BDFDB.dotCN._imagezoomlense} {
border: 2px solid rgb(114, 137, 218);
}
2020-05-01 09:21:01 +02:00
${BDFDB.dotCN._imagezoomoperations} {
position: absolute;
}
${BDFDB.dotCNS._imagezoomoperations + BDFDB.dotCN.downloadlink} {
position: relative !important;
}
${BDFDB.dotCNS._imagezoomoperations + BDFDB.dotCN.anchor + BDFDB.dotCN.downloadlink} {
margin: 0 !important;
2020-04-29 19:30:28 +02:00
}
2020-02-10 22:45:39 +01:00
`;
2019-03-12 12:08:37 +01:00
2020-02-10 22:45:39 +01:00
this.defaults = {
settings: {
zoomlevel: {value:2, digits:1, edges:[1, 10], unit:"x", label:"ACCESSIBILITY_ZOOM_LEVEL_LABEL"},
lensesize: {value:200, digits:0, edges:[50, 1000], unit:"px", label:"context_lensesize_text"}
}
};
}
2019-03-12 12:08:37 +01:00
2020-04-11 19:32:58 +02:00
// Legacy
2020-02-10 22:45:39 +01:00
load () {}
2019-03-12 12:08:37 +01:00
2020-02-10 22:45:39 +01:00
start () {
if (!window.BDFDB) window.BDFDB = {myPlugins:{}};
if (window.BDFDB && window.BDFDB.myPlugins && typeof window.BDFDB.myPlugins == "object") window.BDFDB.myPlugins[this.getName()] = this;
let libraryScript = document.querySelector("head script#BDFDBLibraryScript");
if (!libraryScript || (performance.now() - libraryScript.getAttribute("date")) > 600000) {
if (libraryScript) libraryScript.remove();
libraryScript = document.createElement("script");
libraryScript.setAttribute("id", "BDFDBLibraryScript");
libraryScript.setAttribute("type", "text/javascript");
libraryScript.setAttribute("src", "https://mwittrien.github.io/BetterDiscordAddons/Plugins/BDFDB.min.js");
libraryScript.setAttribute("date", performance.now());
libraryScript.addEventListener("load", _ => {this.initialize();});
document.head.appendChild(libraryScript);
}
else if (window.BDFDB && typeof BDFDB === "object" && BDFDB.loaded) this.initialize();
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);
2019-03-12 12:08:37 +01:00
}
2020-02-10 22:45:39 +01:00
initialize () {
if (window.BDFDB && typeof BDFDB === "object" && BDFDB.loaded) {
if (this.started) return;
BDFDB.PluginUtils.init(this);
2019-03-12 12:08:37 +01:00
2020-02-10 22:45:39 +01:00
BDFDB.ModuleUtils.forceAllUpdates(this);
}
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
}
2020-02-10 22:45:39 +01:00
stop () {
if (window.BDFDB && typeof BDFDB === "object" && BDFDB.loaded) {
this.stopping = true;
2019-10-22 11:37:23 +02:00
2020-02-10 22:45:39 +01:00
BDFDB.ModuleUtils.forceAllUpdates(this);
2019-09-04 12:34:02 +02:00
2020-02-10 22:45:39 +01:00
BDFDB.PluginUtils.clear(this);
}
2019-03-12 12:08:37 +01:00
}
2020-04-11 19:32:58 +02:00
// Begin of own functions
2019-03-12 12:08:37 +01:00
2020-02-10 22:45:39 +01:00
processImageModal (e) {
if (e.returnvalue) {
let [children, index] = BDFDB.ReactUtils.findChildren(e.returnvalue, {props: [["className", BDFDB.disCN.downloadlink]]});
if (index > -1) {
let openContext = event => {
let settings = BDFDB.DataUtils.get(this, "settings");
BDFDB.ContextMenuUtils.open(this, event, BDFDB.ReactUtils.createElement(BDFDB.LibraryComponents.ContextMenuItems.Group, {
children: Object.keys(settings).map(type => BDFDB.ReactUtils.createElement(BDFDB.LibraryComponents.ContextMenuItems.Slider, {
defaultValue: settings[type],
digits: this.defaults.settings[type].digits,
edges: this.defaults.settings[type].edges,
renderLabel: value => {
return (this.labels[this.defaults.settings[type].label] || BDFDB.LanguageUtils.LanguageStrings[this.defaults.settings[type].label]) + ": " + value + this.defaults.settings[type].unit;
},
onValueRender: value => {
return value + this.defaults.settings[type].unit;
},
onValueChange: value => {
BDFDB.DataUtils.save(value, this, "settings", type);
}
}))
}));
};
children[index] = BDFDB.ReactUtils.createElement("span", {
2020-05-01 09:21:01 +02:00
className: BDFDB.disCN._imagezoomoperations,
2020-02-10 22:45:39 +01:00
children: [
children[index],
BDFDB.ReactUtils.createElement("span", {
className: BDFDB.disCN.downloadlink,
children: "|",
style: {margin: "0 5px"}
}),
BDFDB.ReactUtils.createElement(BDFDB.LibraryComponents.Anchor, {
className: BDFDB.disCN.downloadlink,
children: `Zoom ${BDFDB.LanguageUtils.LanguageStrings.SETTINGS}`,
onClick: openContext,
onContextMenu: openContext
})
],
});
}
2019-11-14 21:10:17 +01:00
}
2020-02-10 22:45:39 +01:00
if (e.node) BDFDB.DOMUtils.addClass(BDFDB.DOMUtils.getParent(BDFDB.dotCN.modal, e.node), BDFDB.disCN._imagezoomimagemodal);
2019-03-12 12:08:37 +01:00
}
2019-11-14 21:10:17 +01:00
2020-02-10 22:45:39 +01:00
processLazyImage (e) {
2020-05-01 09:21:01 +02:00
if (!BDFDB.DOMUtils.containsClass(e.node.parentElement, BDFDB.disCN._imagegallerysibling) && BDFDB.ReactUtils.findOwner(BDFDB.DOMUtils.getParent(BDFDB.dotCNC.modal + BDFDB.dotCN.layermodal, e.node), {name: "ImageModal"})) {
2020-02-10 22:45:39 +01:00
e.node.addEventListener("mousedown", event => {
BDFDB.ListenerUtils.stopEvent(event);
2019-11-14 21:10:17 +01:00
2020-05-01 09:21:01 +02:00
let imgRects = BDFDB.DOMUtils.getRects(e.node.firstElementChild);
2020-02-10 22:45:39 +01:00
let settings = BDFDB.DataUtils.get(this, "settings");
2019-11-14 21:10:17 +01:00
2020-05-01 09:21:01 +02:00
let lense = BDFDB.DOMUtils.create(`<div class="${BDFDB.disCN._imagezoomlense}" 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 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;"><${e.node.firstElementChild.tagName} src="${e.instance.props.src}" style="width: ${imgRects.width * settings.zoomlevel}px; height: ${imgRects.height * settings.zoomlevel}px; position: fixed !important;"${e.node.firstElementChild.tagName == "VIDEO" ? " loop autoplay" : ""}></${e.node.firstElementChild.tagName}></div></div>`);
2020-02-10 22:45:39 +01:00
let pane = lense.firstElementChild.firstElementChild;
let backdrop = BDFDB.DOMUtils.create(`<div class="${BDFDB.disCN._imagezoombackdrop}" style="background: rgba(0, 0, 0, 0.3) !important; position: absolute !important; top: 0 !important; right: 0 !important; bottom: 0 !important; left: 0 !important; pointer-events: none !important; z-index: 8000 !important;"></div>`);
2020-05-01 09:21:01 +02:00
let appMount = document.querySelector(BDFDB.dotCN.appmount);
appMount.appendChild(lense);
appMount.appendChild(backdrop);
2019-11-14 21:10:17 +01:00
2020-02-10 22:45:39 +01:00
let lenserects = BDFDB.DOMUtils.getRects(lense), panerects = BDFDB.DOMUtils.getRects(pane);
let halfW = lenserects.width / 2, halfH = lenserects.height / 2;
2020-05-01 09:21:01 +02:00
let minX = imgRects.left, maxX = minX + imgRects.width;
let minY = imgRects.top, maxY = minY + imgRects.height;
2020-02-10 22:45:39 +01:00
lense.style.setProperty("left", event.clientX - halfW + "px", "important");
lense.style.setProperty("top", event.clientY - halfH + "px", "important");
2020-05-01 09:21:01 +02:00
pane.style.setProperty("left", imgRects.left + ((settings.zoomlevel - 1) * (imgRects.left - event.clientX)) + "px", "important");
pane.style.setProperty("top", imgRects.top + ((settings.zoomlevel - 1) * (imgRects.top - event.clientY)) + "px", "important");
2019-11-14 21:10:17 +01:00
2020-02-10 22:45:39 +01:00
let dragging = event2 => {
let x = event2.clientX > maxX ? maxX - halfW : event2.clientX < minX ? minX - halfW : event2.clientX - halfW;
let y = event2.clientY > maxY ? maxY - halfH : event2.clientY < minY ? minY - halfH : event2.clientY - halfH;
lense.style.setProperty("left", x + "px", "important");
lense.style.setProperty("top", y + "px", "important");
2020-05-01 09:21:01 +02:00
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");
2020-02-10 22:45:39 +01:00
};
let releasing = _ => {
document.removeEventListener("mousemove", dragging);
document.removeEventListener("mouseup", releasing);
BDFDB.DOMUtils.remove(lense, backdrop);
};
document.addEventListener("mousemove", dragging);
document.addEventListener("mouseup", releasing);
});
}
2019-11-14 21:10:17 +01:00
}
2020-02-10 22:45:39 +01:00
setLabelsByLanguage () {
switch (BDFDB.LanguageUtils.getLanguage().id) {
case "hr": //croatian
return {
context_lensesize_text: "Veličina leće"
};
case "da": //danish
return {
context_lensesize_text: "Linsestørrelse"
};
case "de": //german
return {
context_lensesize_text: "Linsengröße"
};
case "es": //spanish
return {
context_lensesize_text: "Tamaño de la lente"
};
case "fr": //french
return {
context_lensesize_text: "Taille de la lentille"
};
case "it": //italian
return {
context_lensesize_text: "Dimensione dell'obiettivo"
};
case "nl": //dutch
return {
context_lensesize_text: "Lensgrootte"
};
case "no": //norwegian
return {
context_lensesize_text: "Linsestørrelse"
};
case "pl": //polish
return {
context_lensesize_text: "Rozmiar obiektywu"
};
case "pt-BR": //portuguese (brazil)
return {
context_lensesize_text: "Tamanho da lente"
};
case "fi": //finnish
return {
context_lensesize_text: "Linssin koko"
};
case "sv": //swedish
return {
context_lensesize_text: "Linsstorlek"
};
case "tr": //turkish
return {
context_lensesize_text: "Lens boyutu"
};
case "cs": //czech
return {
context_lensesize_text: "Velikost objektivu"
};
case "bg": //bulgarian
return {
context_lensesize_text: "Размер на обектива"
};
case "ru": //russian
return {
context_lensesize_text: "Размер объектива"
};
case "uk": //ukrainian
return {
context_lensesize_text: "Розмір об'єктива"
};
case "ja": //japanese
return {
context_lensesize_text: "Розмір об'єктива"
};
case "zh-TW": //chinese (traditional)
return {
context_lensesize_text: "鏡片尺寸"
};
case "ko": //korean
return {
context_lensesize_text: "렌즈 크기"
};
default: //default: english
return {
context_lensesize_text: "Lense Size"
};
}
2019-03-12 12:08:37 +01:00
}
}
2020-02-10 22:45:39 +01:00
})();