Update MoveablePopups.plugin.js

This commit is contained in:
Mirco Wittrien 2020-02-09 20:33:50 +01:00
parent ac54571b0e
commit b23e3cd3be
1 changed files with 126 additions and 116 deletions

View File

@ -1,14 +1,32 @@
//META{"name":"MoveablePopups","website":"https://github.com/mwittrien/BetterDiscordAddons/tree/master/Plugins/MoveablePopups","source":"https://raw.githubusercontent.com/mwittrien/BetterDiscordAddons/master/Plugins/MoveablePopups/MoveablePopups.plugin.js"}*//
class MoveablePopups {
var MoveablePopups = (_ => {
var dragging;
return class MoveablePopups {
getName () {return "MoveablePopups";}
getVersion () {return "1.1.5";}
getVersion () {return "1.1.6";}
getAuthor () {return "DevilBro";}
getDescription () {return "Adds the feature to move all popups and modals around like on a normal desktop. Ctrl + drag with your left mousebutton to drag element.";}
constructor () {
this.changelog = {
"improved":[["New Library Structure & React","Restructured my Library and switched to React rendering instead of DOM manipulation"]]
};
this.patchedModules = {
after: {
Popouts: "componentDidMount",
ReferencePositionLayer: "componentDidMount",
Modals: "componentDidMount",
ModalLayer: "componentDidMount"
}
};
}
//legacy
load () {}
@ -38,40 +56,7 @@ class MoveablePopups {
if (this.started) return;
BDFDB.PluginUtils.init(this);
var observer = null;
observer = new MutationObserver((changes, _) => {
changes.forEach(
(change, i) => {
if (change.addedNodes) {
change.addedNodes.forEach((node) => {
if (node && BDFDB.DOMUtils.containsClass(node, BDFDB.disCN.popout)) {
this.makeMoveable(node);
}
});
}
}
);
});
BDFDB.ObserverUtils.connect(this, BDFDB.dotCN.popouts, {name:"popoutObserver",instance:observer}, {childList: true});
observer = new MutationObserver((changes, _) => {
changes.forEach(
(change, i) => {
if (change.addedNodes) {
change.addedNodes.forEach((node) => {
if (node && BDFDB.DOMUtils.containsClass(node, BDFDB.disCN.modal) && !node.querySelector(BDFDB.dotCN.downloadlink)) {
this.makeMoveable(node.querySelector(BDFDB.dotCN.modalinner));
}
else if (node.tagName && node.querySelector(BDFDB.dotCN.modal) && !node.querySelector(BDFDB.dotCN.downloadlink)) {
this.makeMoveable(node.querySelector(BDFDB.dotCN.modalinner));
}
});
}
}
);
});
BDFDB.ObserverUtils.connect(this, BDFDB.ReactUtils.findDOMNode(BDFDB.ReactUtils.findOwner(document.querySelector(BDFDB.dotCN.app), {name:"Modals"})), {name:"modalObserver", instance:observer}, {childList: true});
BDFDB.ModuleUtils.forceAllUpdates(this);
}
else console.error(`%c[${this.getName()}]%c`, "color: #3a71c1; font-weight: 700;", "", "Fatal Error: Could not load BD functions!");
}
@ -81,6 +66,8 @@ class MoveablePopups {
if (window.BDFDB && typeof BDFDB === "object" && BDFDB.loaded) {
this.stopping = true;
BDFDB.ModuleUtils.forceAllUpdates(this);
BDFDB.PluginUtils.clear(this);
}
}
@ -88,14 +75,36 @@ class MoveablePopups {
// begin of own functions
processPopouts (e) {
BDFDB.ObserverUtils.connect(this, e.node, {name:"popoutObserver", instance:new MutationObserver(changes => {changes.forEach(change => {change.addedNodes.forEach(node => {
if (node && BDFDB.DOMUtils.containsClass(node, BDFDB.disCN.popout)) this.makeMoveable(node);
})})})}, {childList: true});
}
processReferencePositionLayer (e) {
this.makeMoveable(e.node);
}
processModals (e) {
BDFDB.ObserverUtils.connect(this, e.node, {name:"modalObserver", instance:new MutationObserver(changes => {changes.forEach(change => {change.addedNodes.forEach(node => {
if (node && !node.querySelector(BDFDB.dotCN.downloadlink)) this.makeMoveable(node.querySelector(BDFDB.dotCNC.modalinner + BDFDB.dotCN.layermodal));
else if (node.tagName && !node.querySelector(BDFDB.dotCN.downloadlink)) this.makeMoveable(node.querySelector(BDFDB.dotCNC.modalinner + BDFDB.dotCN.layermodal));
})})})}, {childList: true});
}
processModalLayer (e) {
if (e.node && !e.node.querySelector(BDFDB.dotCN.downloadlink)) this.makeMoveable(e.node.querySelector(BDFDB.dotCNC.modalinner + BDFDB.dotCN.layermodal));
}
makeMoveable (div) {
if (!Node.prototype.isPrototypeOf(div)) return;
div.removeEventListener("click", div.clickMovablePopups);
div.removeEventListener("mousedown", div.mousedownMovablePopups);
div.clickMovablePopups = e => {if (this.dragging) BDFDB.ListenerUtils.stopEvent(e);};
div.clickMovablePopups = e => {if (dragging) BDFDB.ListenerUtils.stopEvent(e);};
div.mousedownMovablePopups = e => {
if (!e.ctrlKey) return;
div.style.setProperty("position", "fixed", "important");
this.dragging = true;
dragging = true;
var rects = BDFDB.DOMUtils.getRects(div);
var transform = getComputedStyle(div, null).getPropertyValue("transform").replace(/[^0-9,-]/g,"").split(",");
var left = rects.left - (transform.length > 4 ? parseFloat(transform[4]) : 0);
@ -106,7 +115,7 @@ class MoveablePopups {
BDFDB.DOMUtils.removeLocalStyle("disableTextSelection");
document.removeEventListener("mouseup", mouseup);
document.removeEventListener("mousemove", mousemove);
BDFDB.TimeUtils.timeout(_ => {this.dragging = false},1);
BDFDB.TimeUtils.timeout(_ => {dragging = false});
};
var mousemove = e2 => {
left = left - (oldX - e2.pageX);
@ -124,3 +133,4 @@ class MoveablePopups {
div.addEventListener("mousedown", div.mousedownMovablePopups);
}
}
})();