Discord contextmenu patching

This commit is contained in:
Jiiks 2018-08-22 10:53:20 +03:00
parent 07fc98670c
commit bb37b89d35
3 changed files with 57 additions and 4 deletions

View File

@ -8,7 +8,7 @@
* LICENSE file in the root directory of this source tree.
*/
import { DOM, BdUI, BdMenu, Modals, Reflection, Toasts, Notifications, BdContextMenu } from 'ui';
import { DOM, BdUI, BdMenu, Modals, Reflection, Toasts, Notifications, BdContextMenu, DiscordContextMenu } from 'ui';
import BdCss from './styles/index.scss';
import { Events, CssEditor, Globals, Settings, Database, Updater, ModuleManager, PluginManager, ThemeManager, ExtModuleManager, Vendor, WebpackModules, Patcher, MonkeyPatch, ReactComponents, ReactHelpers, ReactAutoPatcher, DiscordApi, BdWebApi, Connectivity, Cache } from 'modules';
import { ClientLogger as Logger, ClientIPC, Utils } from 'common';
@ -28,7 +28,7 @@ class BetterDiscord {
Logger.log('main', 'BetterDiscord starting');
this._bd = {
DOM, BdUI, BdMenu, Modals, Reflection, Toasts, Notifications, BdContextMenu,
DOM, BdUI, BdMenu, Modals, Reflection, Toasts, Notifications, BdContextMenu, DiscordContextMenu,
Events, CssEditor, Globals, Settings, Database, Updater,
ModuleManager, PluginManager, ThemeManager, ExtModuleManager,

View File

@ -1,4 +1,5 @@
.bd-cm {
.bd-cm,
.da-contextMenu {
background: #18191c;
box-shadow: 0 0 1px rgba(0, 0, 0, .82), 0 1px 4px rgba(0, 0, 0, .1);
border-radius: 5px;
@ -7,7 +8,8 @@
z-index: 1005;
user-select: none;
&.bd-cmRenderLeft {
&.bd-cmRenderLeft,
&.da-invertChildX {
.bd-cm {
margin-left: -170px;
}

View File

@ -8,6 +8,10 @@
* LICENSE file in the root directory of this source tree.
*/
import { ReactComponents, WebpackModules, MonkeyPatch } from 'modules';
import { VueInjector, Toasts } from 'ui';
import CMGroup from './components/contextmenu/Group.vue';
export class BdContextMenu {
/**
@ -26,3 +30,50 @@ export class BdContextMenu {
}
}
export class DiscordContextMenu {
static add(target, groups) {
if (!this.patched) this.patch();
this.menus.push({ target, groups });
}
static get menus() {
return this._menus || (this._menus = []);
}
static async patch() {
if (this.patched) return;
this.patched = true;
const self = this;
MonkeyPatch('BD:DiscordCMOCM', WebpackModules.getModuleByProps(['openContextMenu'])).instead('openContextMenu', (_, [e, fn], originalFn) => {
const overrideFn = function (...args) {
const res = fn(...args);
if (!res.hasOwnProperty('type')) return res;
if (!res.type.prototype || !res.type.prototype.render || res.type.prototype.render.__patched) return res;
MonkeyPatch('BD:DiscordCMRender', res.type.prototype).after('render', (c, a, r) => self.renderCm(c, a, r, res));
res.type.prototype.render.__patched = true;
return res;
}
return originalFn(e, overrideFn);
});
}
static renderCm(component, args, retVal, res) {
if (!retVal.props || !res.props) return;
const { target } = res.props;
const { top, left } = retVal.props.style;
if (!target || !top || !left) return;
if (!retVal.props.children) return;
if (!(retVal.props.children instanceof Array)) retVal.props.children = [retVal.props.children];
for (const menu of this.menus.filter(menu => menu.target(target))) {
retVal.props.children.push(VueInjector.createReactElement(CMGroup, {
top,
left,
closeMenu: () => WebpackModules.getModuleByProps(['closeContextMenu']).closeContextMenu(),
items: menu.groups
}));
}
}
}