Array support for find + ResurrectHome: View Server Home Button on Server Guide (#2283)
This commit is contained in:
parent
dc4c678aa3
commit
5636f9d979
6 changed files with 131 additions and 23 deletions
|
@ -299,6 +299,9 @@ function runTime(token: string) {
|
|||
delete patch.predicate;
|
||||
delete patch.group;
|
||||
|
||||
if (!Array.isArray(patch.find))
|
||||
patch.find = [patch.find];
|
||||
|
||||
if (!Array.isArray(patch.replacement))
|
||||
patch.replacement = [patch.replacement];
|
||||
|
||||
|
|
|
@ -33,8 +33,8 @@ if (IS_DEV) {
|
|||
var differ = require("diff") as typeof import("diff");
|
||||
}
|
||||
|
||||
const findCandidates = debounce(function ({ find, setModule, setError }) {
|
||||
const candidates = search(find);
|
||||
const findCandidates = debounce(function ({ finds, setModule, setError }) {
|
||||
const candidates = search(...finds);
|
||||
const keys = Object.keys(candidates);
|
||||
const len = keys.length;
|
||||
if (len === 0)
|
||||
|
@ -180,7 +180,8 @@ function ReplacementInput({ replacement, setReplacement, replacementError }) {
|
|||
|
||||
return (
|
||||
<>
|
||||
<Forms.FormTitle>replacement</Forms.FormTitle>
|
||||
{/* FormTitle adds a class if className is not set, so we set it to an empty string to prevent that */}
|
||||
<Forms.FormTitle className="">replacement</Forms.FormTitle>
|
||||
<TextInput
|
||||
value={replacement?.toString()}
|
||||
onChange={onChange}
|
||||
|
@ -188,7 +189,7 @@ function ReplacementInput({ replacement, setReplacement, replacementError }) {
|
|||
/>
|
||||
{!isFunc && (
|
||||
<div className="vc-text-selectable">
|
||||
<Forms.FormTitle>Cheat Sheet</Forms.FormTitle>
|
||||
<Forms.FormTitle className={Margins.top8}>Cheat Sheet</Forms.FormTitle>
|
||||
{Object.entries({
|
||||
"\\i": "Special regex escape sequence that matches identifiers (varnames, classnames, etc.)",
|
||||
"$$": "Insert a $",
|
||||
|
@ -220,11 +221,12 @@ function ReplacementInput({ replacement, setReplacement, replacementError }) {
|
|||
|
||||
interface FullPatchInputProps {
|
||||
setFind(v: string): void;
|
||||
setFinds(v: string[]): void;
|
||||
setMatch(v: string): void;
|
||||
setReplacement(v: string | ReplaceFn): void;
|
||||
}
|
||||
|
||||
function FullPatchInput({ setFind, setMatch, setReplacement }: FullPatchInputProps) {
|
||||
function FullPatchInput({ setFind, setFinds, setMatch, setReplacement }: FullPatchInputProps) {
|
||||
const [fullPatch, setFullPatch] = React.useState<string>("");
|
||||
const [fullPatchError, setFullPatchError] = React.useState<string>("");
|
||||
|
||||
|
@ -256,7 +258,8 @@ function FullPatchInput({ setFind, setMatch, setReplacement }: FullPatchInputPro
|
|||
if (!parsed.replacement.match) throw new Error("No 'replacement.match' field");
|
||||
if (!parsed.replacement.replace) throw new Error("No 'replacement.replace' field");
|
||||
|
||||
setFind(parsed.find);
|
||||
setFind(JSON.stringify(parsed.find));
|
||||
setFinds(parsed.find instanceof Array ? parsed.find : [parsed.find]);
|
||||
setMatch(parsed.replacement.match instanceof RegExp ? parsed.replacement.match.source : parsed.replacement.match);
|
||||
setReplacement(parsed.replacement.replace);
|
||||
setFullPatchError("");
|
||||
|
@ -266,7 +269,7 @@ function FullPatchInput({ setFind, setMatch, setReplacement }: FullPatchInputPro
|
|||
}
|
||||
|
||||
return <>
|
||||
<Forms.FormText>Paste your full JSON patch here to fill out the fields</Forms.FormText>
|
||||
<Forms.FormText className={Margins.bottom8}>Paste your full JSON patch here to fill out the fields</Forms.FormText>
|
||||
<TextArea value={fullPatch} onChange={setFullPatch} onBlur={update} />
|
||||
{fullPatchError !== "" && <Forms.FormText style={{ color: "var(--text-danger)" }}>{fullPatchError}</Forms.FormText>}
|
||||
</>;
|
||||
|
@ -274,6 +277,7 @@ function FullPatchInput({ setFind, setMatch, setReplacement }: FullPatchInputPro
|
|||
|
||||
function PatchHelper() {
|
||||
const [find, setFind] = React.useState<string>("");
|
||||
const [finds, setFinds] = React.useState<string[]>([]);
|
||||
const [match, setMatch] = React.useState<string>("");
|
||||
const [replacement, setReplacement] = React.useState<string | ReplaceFn>("");
|
||||
|
||||
|
@ -285,20 +289,39 @@ function PatchHelper() {
|
|||
const code = React.useMemo(() => {
|
||||
return `
|
||||
{
|
||||
find: ${JSON.stringify(find)},
|
||||
find: ${finds.length > 1 ? `[${finds.map(f => JSON.stringify(f)).join(", ")}]` : finds.length > 0 ? JSON.stringify(finds[0]) : "[]"},
|
||||
replacement: {
|
||||
match: /${match.replace(/(?<!\\)\//g, "\\/")}/,
|
||||
replace: ${typeof replacement === "function" ? replacement.toString() : JSON.stringify(replacement)}
|
||||
}
|
||||
}
|
||||
`.trim();
|
||||
}, [find, match, replacement]);
|
||||
}, [finds, match, replacement]);
|
||||
|
||||
function onFindChange(v: string) {
|
||||
setFindError(void 0);
|
||||
setFind(v);
|
||||
if (v.length) {
|
||||
findCandidates({ find: v, setModule, setError: setFindError });
|
||||
}
|
||||
|
||||
function onFindBlur() {
|
||||
let finds = [] as string[];
|
||||
const findArrayMatch = find.match(/^\[.*\]$/);
|
||||
|
||||
if (findArrayMatch) {
|
||||
try {
|
||||
const rawFinds = (0, eval)(`(${find})`);
|
||||
finds = rawFinds instanceof Array ? rawFinds : [rawFinds];
|
||||
} catch (e) {
|
||||
setFindError((e as Error).message);
|
||||
return;
|
||||
}
|
||||
} else if (find.length) {
|
||||
finds = [find];
|
||||
}
|
||||
|
||||
setFinds(finds);
|
||||
if (finds.length) {
|
||||
findCandidates({ finds, setModule, setError: setFindError });
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -317,19 +340,21 @@ function PatchHelper() {
|
|||
<Forms.FormTitle>full patch</Forms.FormTitle>
|
||||
<FullPatchInput
|
||||
setFind={onFindChange}
|
||||
setFinds={setFinds}
|
||||
setMatch={onMatchChange}
|
||||
setReplacement={setReplacement}
|
||||
/>
|
||||
|
||||
<Forms.FormTitle>find</Forms.FormTitle>
|
||||
<Forms.FormTitle className={Margins.top8}>find</Forms.FormTitle>
|
||||
<TextInput
|
||||
type="text"
|
||||
value={find}
|
||||
onChange={onFindChange}
|
||||
onBlur={onFindBlur}
|
||||
error={findError}
|
||||
/>
|
||||
|
||||
<Forms.FormTitle>match</Forms.FormTitle>
|
||||
<Forms.FormTitle className={Margins.top8}>match</Forms.FormTitle>
|
||||
<CheckedTextInput
|
||||
value={match}
|
||||
onChange={onMatchChange}
|
||||
|
@ -342,6 +367,7 @@ function PatchHelper() {
|
|||
}}
|
||||
/>
|
||||
|
||||
<div className={Margins.top8} />
|
||||
<ReplacementInput
|
||||
replacement={replacement}
|
||||
setReplacement={setReplacement}
|
||||
|
|
|
@ -79,6 +79,8 @@ for (const p of pluginsValues) {
|
|||
if (p.patches && isPluginEnabled(p.name)) {
|
||||
for (const patch of p.patches) {
|
||||
patch.plugin = p.name;
|
||||
if (!Array.isArray(patch.find))
|
||||
patch.find = [patch.find];
|
||||
if (!Array.isArray(patch.replacement))
|
||||
patch.replacement = [patch.replacement];
|
||||
patches.push(patch);
|
||||
|
|
|
@ -18,9 +18,68 @@
|
|||
|
||||
import { findGroupChildrenByChildId } from "@api/ContextMenu";
|
||||
import { definePluginSettings } from "@api/Settings";
|
||||
import ErrorBoundary from "@components/ErrorBoundary";
|
||||
import { Devs } from "@utils/constants";
|
||||
import definePlugin, { OptionType } from "@utils/types";
|
||||
import { Menu } from "@webpack/common";
|
||||
import { findByPropsLazy } from "@webpack";
|
||||
import { Button, Menu, Tooltip, useEffect, useState } from "@webpack/common";
|
||||
|
||||
const ChannelRowClasses = findByPropsLazy("modeConnected", "modeLocked", "icon");
|
||||
|
||||
let currentShouldViewServerHome = false;
|
||||
const shouldViewServerHomeStates = new Set<React.Dispatch<React.SetStateAction<boolean>>>();
|
||||
|
||||
function ViewServerHomeButton() {
|
||||
return (
|
||||
<Tooltip text="View Server Home">
|
||||
{tooltipProps => (
|
||||
<Button
|
||||
{...tooltipProps}
|
||||
look={Button.Looks.BLANK}
|
||||
size={Button.Sizes.NONE}
|
||||
innerClassName={ChannelRowClasses.icon}
|
||||
onClick={e => {
|
||||
e.preventDefault();
|
||||
|
||||
currentShouldViewServerHome = true;
|
||||
for (const setState of shouldViewServerHomeStates) {
|
||||
setState(true);
|
||||
}
|
||||
}}
|
||||
|
||||
>
|
||||
<svg width="20" height="20" viewBox="0 0 24 24">
|
||||
<path fill="currentColor" d="m2.4 8.4 8.38-6.46a2 2 0 0 1 2.44 0l8.39 6.45a2 2 0 0 1-.79 3.54l-.32.07-.82 8.2a2 2 0 0 1-1.99 1.8H16a1 1 0 0 1-1-1v-5a3 3 0 0 0-6 0v5a1 1 0 0 1-1 1H6.31a2 2 0 0 1-1.99-1.8L3.5 12l-.32-.07a2 2 0 0 1-.79-3.54Z" />
|
||||
</svg>
|
||||
</Button>
|
||||
)}
|
||||
</Tooltip>
|
||||
);
|
||||
}
|
||||
|
||||
function useForceServerHome() {
|
||||
const { forceServerHome } = settings.use(["forceServerHome"]);
|
||||
const [shouldViewServerHome, setShouldViewServerHome] = useState(currentShouldViewServerHome);
|
||||
|
||||
useEffect(() => {
|
||||
shouldViewServerHomeStates.add(setShouldViewServerHome);
|
||||
|
||||
return () => {
|
||||
shouldViewServerHomeStates.delete(setShouldViewServerHome);
|
||||
};
|
||||
}, []);
|
||||
|
||||
return shouldViewServerHome || forceServerHome;
|
||||
}
|
||||
|
||||
function useDisableViewServerHome() {
|
||||
useEffect(() => () => {
|
||||
currentShouldViewServerHome = false;
|
||||
for (const setState of shouldViewServerHomeStates) {
|
||||
setState(false);
|
||||
}
|
||||
}, []);
|
||||
}
|
||||
|
||||
const settings = definePluginSettings({
|
||||
forceServerHome: {
|
||||
|
@ -30,12 +89,6 @@ const settings = definePluginSettings({
|
|||
}
|
||||
});
|
||||
|
||||
function useForceServerHome() {
|
||||
const { forceServerHome } = settings.use(["forceServerHome"]);
|
||||
|
||||
return forceServerHome;
|
||||
}
|
||||
|
||||
export default definePlugin({
|
||||
name: "ResurrectHome",
|
||||
description: "Re-enables the Server Home tab when there isn't a Server Guide. Also has an option to force the Server Home over the Server Guide, which is accessible through right-clicking the Server Guide.",
|
||||
|
@ -92,14 +145,37 @@ export default definePlugin({
|
|||
match: /getMutableGuildChannelsForGuild\(\i\);return\(0,\i\.useStateFromStores\).+?\]\)(?=}function)/,
|
||||
replace: m => `${m}&&!$self.useForceServerHome()`
|
||||
}
|
||||
},
|
||||
// Add View Server Home Button to Server Guide
|
||||
{
|
||||
find: "487e85_1",
|
||||
replacement: {
|
||||
match: /(?<=text:(\i)\?\i\.\i\.Messages\.SERVER_GUIDE:\i\.\i\.Messages\.GUILD_HOME,)/,
|
||||
replace: "badge:$self.ViewServerHomeButton({serverGuide:$1}),"
|
||||
}
|
||||
},
|
||||
// Disable view Server Home override when the Server Home is unmouted
|
||||
{
|
||||
find: "69386d_5",
|
||||
replacement: {
|
||||
match: /location:"69386d_5".+?;/,
|
||||
replace: "$&$self.useDisableViewServerHome();"
|
||||
}
|
||||
}
|
||||
],
|
||||
|
||||
ViewServerHomeButton: ErrorBoundary.wrap(({ serverGuide }: { serverGuide?: boolean; }) => {
|
||||
if (serverGuide !== true) return null;
|
||||
|
||||
return <ViewServerHomeButton />;
|
||||
}),
|
||||
|
||||
useForceServerHome,
|
||||
useDisableViewServerHome,
|
||||
|
||||
contextMenus: {
|
||||
"guild-context"(children, props) {
|
||||
const forceServerHome = useForceServerHome();
|
||||
const { forceServerHome } = settings.use(["forceServerHome"]);
|
||||
|
||||
if (!props?.guild) return;
|
||||
|
||||
|
|
|
@ -36,7 +36,7 @@ export interface PatchReplacement {
|
|||
|
||||
export interface Patch {
|
||||
plugin: string;
|
||||
find: string;
|
||||
find: string | string[];
|
||||
replacement: PatchReplacement | PatchReplacement[];
|
||||
/** Whether this patch should apply to multiple modules */
|
||||
all?: boolean;
|
||||
|
|
|
@ -186,7 +186,8 @@ function patchFactories(factories: Record<string | number, (module: { exports: a
|
|||
const executePatch = traceFunction(`patch by ${patch.plugin}`, (match: string | RegExp, replace: string) => code.replace(match, replace));
|
||||
if (patch.predicate && !patch.predicate()) continue;
|
||||
|
||||
if (code.includes(patch.find)) {
|
||||
// we change all patch.find to array in plugins/index
|
||||
if ((patch.find as string[]).every(f => code.includes(f))) {
|
||||
patchedBy.add(patch.plugin);
|
||||
|
||||
const previousMod = mod;
|
||||
|
|
Loading…
Reference in a new issue