From 9f39648b0ffb6418baf2186906ed016bf7c12a5c Mon Sep 17 00:00:00 2001 From: thororen <78185467+thororen1234@users.noreply.github.com> Date: Sun, 25 Aug 2024 00:32:13 -0400 Subject: [PATCH] Fixes --- src/components/PluginSettings/PluginModal.tsx | 8 +- .../limitMiddleClickPaste/index.ts | 116 ++++++++++++++++++ src/utils/constants.ts | 4 + 3 files changed, 127 insertions(+), 1 deletion(-) create mode 100644 src/equicordplugins/limitMiddleClickPaste/index.ts diff --git a/src/components/PluginSettings/PluginModal.tsx b/src/components/PluginSettings/PluginModal.tsx index 515fa3ec..2bccf216 100644 --- a/src/components/PluginSettings/PluginModal.tsx +++ b/src/components/PluginSettings/PluginModal.tsx @@ -207,6 +207,12 @@ export default function PluginModal({ plugin, onRestartNeeded, onClose, transiti */ const pluginMeta = PluginMeta[plugin.name]; + let link: string; + if (pluginMeta.folderName.includes("equicordplugins")) { + link = `https://github.com/${gitRemote}/tree/main/${pluginMeta.folderName}`; + } else { + link = `https://github.com/${gitRemote}/tree/main/src/plugins/${pluginMeta.folderName}`; + } return ( @@ -228,7 +234,7 @@ export default function PluginModal({ plugin, onRestartNeeded, onClose, transiti
)} diff --git a/src/equicordplugins/limitMiddleClickPaste/index.ts b/src/equicordplugins/limitMiddleClickPaste/index.ts new file mode 100644 index 00000000..c39f4483 --- /dev/null +++ b/src/equicordplugins/limitMiddleClickPaste/index.ts @@ -0,0 +1,116 @@ +/* + * Vencord, a modification for Discord's desktop app + * Copyright (c) 2023 Vendicated and contributors + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . +*/ + +import { definePluginSettings } from "@api/Settings"; +import { makeRange } from "@components/PluginSettings/components"; +import { EquicordDevs } from "@utils/constants"; +import definePlugin, { OptionType } from "@utils/types"; + + +const settings = definePluginSettings({ + limitTo: { + type: OptionType.SELECT, + description: "Allow middle click pastes:", + options: [ + { + label: "Whenever a text box is active", + value: "active", + default: true + }, + { + label: "Only when clicking on a text box", + value: "direct" + } + ] + }, + reenableDelay: { + type: OptionType.SLIDER, + description: "Milliseconds until re-enabling global paste events after middle click.", + markers: makeRange(0, 1000, 500), + default: 500, + }, +}); + +let containerEl; + +export default definePlugin({ + name: "LimitMiddleClickPaste", + description: "For middle-click autoscroll users, prevents middle-click from making unwanted pastes.", + authors: [EquicordDevs.nobody], + + settings: settings, + + start() { + // Discord adds it's paste listeners to #app-mount. We can intercept them + // by attaching listeners a child element. + containerEl = document.querySelector("[class^=appAsidePanelWrapper]")!; + containerEl.addEventListener("paste", blockPastePropogation); + + // Also add them to body to intercept the event listeners on document + document.body.addEventListener("paste", blockPastePropogation); + + document.body.addEventListener("mousedown", disablePasteOnMousedown); + }, + + stop() { + containerEl.removeEventListener("paste", blockPastePropogation); + + document.body.removeEventListener("paste", blockPastePropogation); + + document.body.removeEventListener("mousedown", disablePasteOnMousedown); + pasteDisabled = false; + }, +}); + +let pasteDisabled: boolean = false; +let timeoutID: number | undefined; + +function blockPastePropogation(e: ClipboardEvent) { + if (pasteDisabled) { + e.stopImmediatePropagation(); + } +} + +function disablePasteOnMousedown(e: MouseEvent) { + if (e.button !== 1) return; + let testEl; + switch (settings.store.limitTo) { + case "active": + testEl = document.activeElement; + break; + case "direct": + testEl = e.target; + break; + } + if (maybeEditable(testEl as HTMLElement | null)) return; + window.clearTimeout(timeoutID); + pasteDisabled = true; + timeoutID = window.setTimeout(() => { + pasteDisabled = false; + }, settings.store.reenableDelay); +} + +function maybeEditable(el: HTMLElement | null): boolean { + if (!el) return false; + if (el.tagName === "INPUT" || el.tagName === "TEXTAREA") return true; + let parent: HTMLElement | null; + for (parent = el; parent; parent = parent.parentElement) { + if (parent.isContentEditable) return true; + } + return false; +} diff --git a/src/utils/constants.ts b/src/utils/constants.ts index b8953585..08173b1f 100644 --- a/src/utils/constants.ts +++ b/src/utils/constants.ts @@ -555,6 +555,10 @@ export const Devs = /* #__PURE__*/ Object.freeze({ } satisfies Record); export const EquicordDevs = Object.freeze({ + nobody: { + name: "nobody", + id: 0n + }, thororen: { name: "thororen", id: 848339671629299742n