/* * Vencord, a Discord client mod * Copyright (c) 2025 Vendicated and contributors * SPDX-License-Identifier: GPL-3.0-or-later */ import { Margins } from "@utils/margins"; import { Forms, Parser, Switch, TextInput, useEffect, useState } from "@webpack/common"; const RegexGuide = { "\\i": "Special regex escape sequence that matches identifiers (varnames, classnames, etc.)", "$$": "Insert a $", "$&": "Insert the entire match", "$`\u200b": "Insert the substring before the match", "$'": "Insert the substring after the match", "$n": "Insert the nth capturing group ($1, $2...)", "$self": "Insert the plugin instance", } as const; export function ReplacementInput({ replacement, setReplacement, replacementError }) { const [isFunc, setIsFunc] = useState(false); const [error, setError] = useState(); function onChange(v: string) { setError(void 0); if (isFunc) { try { const func = (0, eval)(v); if (typeof func === "function") setReplacement(() => func); else setError("Replacement must be a function"); } catch (e) { setReplacement(v); setError((e as Error).message); } } else { setReplacement(v); } } useEffect(() => { if (isFunc) onChange(replacement); else setError(void 0); }, [isFunc]); return ( <> {/* FormTitle adds a class if className is not set, so we set it to an empty string to prevent that */} Replacement {!isFunc && (
Cheat Sheet {Object.entries(RegexGuide).map(([placeholder, desc]) => ( {Parser.parse("`" + placeholder + "`")}: {desc} ))}
)} Treat as Function ); }