From c2f8837602d0bcb931f2cf3d743425e897ff1adf Mon Sep 17 00:00:00 2001 From: sunnie <78964224+sunnniee@users.noreply.github.com> Date: Tue, 28 May 2024 03:23:30 +0300 Subject: [PATCH] new plugin: MaskedLinkPaste (#2514) Co-authored-by: vee --- src/plugins/maskedLinkPaste/README.md | 5 ++++ src/plugins/maskedLinkPaste/index.ts | 36 +++++++++++++++++++++++++++ src/utils/constants.ts | 2 +- 3 files changed, 42 insertions(+), 1 deletion(-) create mode 100644 src/plugins/maskedLinkPaste/README.md create mode 100644 src/plugins/maskedLinkPaste/index.ts diff --git a/src/plugins/maskedLinkPaste/README.md b/src/plugins/maskedLinkPaste/README.md new file mode 100644 index 00000000..30beccb6 --- /dev/null +++ b/src/plugins/maskedLinkPaste/README.md @@ -0,0 +1,5 @@ +# MaskedLinkPaste + +Pasting a link while you have text selected will paste your link as a masked link at that location + +![](https://github.com/Vendicated/Vencord/assets/78964224/1d3be2c6-7957-44c9-92ec-551069d46c02) diff --git a/src/plugins/maskedLinkPaste/index.ts b/src/plugins/maskedLinkPaste/index.ts new file mode 100644 index 00000000..dc868992 --- /dev/null +++ b/src/plugins/maskedLinkPaste/index.ts @@ -0,0 +1,36 @@ +/* + * Vencord, a Discord client mod + * Copyright (c) 2023 Vendicated and contributors + * SPDX-License-Identifier: GPL-3.0-or-later + */ + +import { Devs } from "@utils/constants.js"; +import definePlugin from "@utils/types"; +import { findByPropsLazy } from "@webpack"; + +const linkRegex = /https?:\/\/(www\.)?[-a-zA-Z0-9@:%._+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b([-a-zA-Z0-9()@:%_+.~#?&//=]*)/; + +const { SlateTransforms } = findByPropsLazy("SlateTransforms"); + +export default definePlugin({ + name: "MaskedLinkPaste", + authors: [Devs.TheSun], + description: "Pasting a link while having text selected will paste a hyperlink", + patches: [{ + find: ".selection,preventEmojiSurrogates:", + replacement: { + match: /(?<=SlateTransforms.delete.{0,50})(\i)\.insertText\((\i)\)/, + replace: "$self.handlePaste($1, $2, () => $&)" + } + }], + + handlePaste(editor, content: string, originalBehavior: () => void) { + if (content && linkRegex.test(content) && editor.operations?.[0]?.type === "remove_text") { + SlateTransforms.insertText( + editor, + `[${editor.operations[0].text}](${content})` + ); + } + else originalBehavior(); + } +}); diff --git a/src/utils/constants.ts b/src/utils/constants.ts index 2f686d69..69953855 100644 --- a/src/utils/constants.ts +++ b/src/utils/constants.ts @@ -186,7 +186,7 @@ export const Devs = /* #__PURE__*/ Object.freeze({ id: 296776625432035328n, }, TheSun: { - name: "ActuallyTheSun", + name: "sunnie", id: 406028027768733696n }, axyie: {