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.predicate;
|
||||||
delete patch.group;
|
delete patch.group;
|
||||||
|
|
||||||
|
if (!Array.isArray(patch.find))
|
||||||
|
patch.find = [patch.find];
|
||||||
|
|
||||||
if (!Array.isArray(patch.replacement))
|
if (!Array.isArray(patch.replacement))
|
||||||
patch.replacement = [patch.replacement];
|
patch.replacement = [patch.replacement];
|
||||||
|
|
||||||
|
|
|
@ -33,8 +33,8 @@ if (IS_DEV) {
|
||||||
var differ = require("diff") as typeof import("diff");
|
var differ = require("diff") as typeof import("diff");
|
||||||
}
|
}
|
||||||
|
|
||||||
const findCandidates = debounce(function ({ find, setModule, setError }) {
|
const findCandidates = debounce(function ({ finds, setModule, setError }) {
|
||||||
const candidates = search(find);
|
const candidates = search(...finds);
|
||||||
const keys = Object.keys(candidates);
|
const keys = Object.keys(candidates);
|
||||||
const len = keys.length;
|
const len = keys.length;
|
||||||
if (len === 0)
|
if (len === 0)
|
||||||
|
@ -180,7 +180,8 @@ function ReplacementInput({ replacement, setReplacement, replacementError }) {
|
||||||
|
|
||||||
return (
|
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
|
<TextInput
|
||||||
value={replacement?.toString()}
|
value={replacement?.toString()}
|
||||||
onChange={onChange}
|
onChange={onChange}
|
||||||
|
@ -188,7 +189,7 @@ function ReplacementInput({ replacement, setReplacement, replacementError }) {
|
||||||
/>
|
/>
|
||||||
{!isFunc && (
|
{!isFunc && (
|
||||||
<div className="vc-text-selectable">
|
<div className="vc-text-selectable">
|
||||||
<Forms.FormTitle>Cheat Sheet</Forms.FormTitle>
|
<Forms.FormTitle className={Margins.top8}>Cheat Sheet</Forms.FormTitle>
|
||||||
{Object.entries({
|
{Object.entries({
|
||||||
"\\i": "Special regex escape sequence that matches identifiers (varnames, classnames, etc.)",
|
"\\i": "Special regex escape sequence that matches identifiers (varnames, classnames, etc.)",
|
||||||
"$$": "Insert a $",
|
"$$": "Insert a $",
|
||||||
|
@ -220,11 +221,12 @@ function ReplacementInput({ replacement, setReplacement, replacementError }) {
|
||||||
|
|
||||||
interface FullPatchInputProps {
|
interface FullPatchInputProps {
|
||||||
setFind(v: string): void;
|
setFind(v: string): void;
|
||||||
|
setFinds(v: string[]): void;
|
||||||
setMatch(v: string): void;
|
setMatch(v: string): void;
|
||||||
setReplacement(v: string | ReplaceFn): 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 [fullPatch, setFullPatch] = React.useState<string>("");
|
||||||
const [fullPatchError, setFullPatchError] = 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.match) throw new Error("No 'replacement.match' field");
|
||||||
if (!parsed.replacement.replace) throw new Error("No 'replacement.replace' 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);
|
setMatch(parsed.replacement.match instanceof RegExp ? parsed.replacement.match.source : parsed.replacement.match);
|
||||||
setReplacement(parsed.replacement.replace);
|
setReplacement(parsed.replacement.replace);
|
||||||
setFullPatchError("");
|
setFullPatchError("");
|
||||||
|
@ -266,7 +269,7 @@ function FullPatchInput({ setFind, setMatch, setReplacement }: FullPatchInputPro
|
||||||
}
|
}
|
||||||
|
|
||||||
return <>
|
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} />
|
<TextArea value={fullPatch} onChange={setFullPatch} onBlur={update} />
|
||||||
{fullPatchError !== "" && <Forms.FormText style={{ color: "var(--text-danger)" }}>{fullPatchError}</Forms.FormText>}
|
{fullPatchError !== "" && <Forms.FormText style={{ color: "var(--text-danger)" }}>{fullPatchError}</Forms.FormText>}
|
||||||
</>;
|
</>;
|
||||||
|
@ -274,6 +277,7 @@ function FullPatchInput({ setFind, setMatch, setReplacement }: FullPatchInputPro
|
||||||
|
|
||||||
function PatchHelper() {
|
function PatchHelper() {
|
||||||
const [find, setFind] = React.useState<string>("");
|
const [find, setFind] = React.useState<string>("");
|
||||||
|
const [finds, setFinds] = React.useState<string[]>([]);
|
||||||
const [match, setMatch] = React.useState<string>("");
|
const [match, setMatch] = React.useState<string>("");
|
||||||
const [replacement, setReplacement] = React.useState<string | ReplaceFn>("");
|
const [replacement, setReplacement] = React.useState<string | ReplaceFn>("");
|
||||||
|
|
||||||
|
@ -285,20 +289,39 @@ function PatchHelper() {
|
||||||
const code = React.useMemo(() => {
|
const code = React.useMemo(() => {
|
||||||
return `
|
return `
|
||||||
{
|
{
|
||||||
find: ${JSON.stringify(find)},
|
find: ${finds.length > 1 ? `[${finds.map(f => JSON.stringify(f)).join(", ")}]` : finds.length > 0 ? JSON.stringify(finds[0]) : "[]"},
|
||||||
replacement: {
|
replacement: {
|
||||||
match: /${match.replace(/(?<!\\)\//g, "\\/")}/,
|
match: /${match.replace(/(?<!\\)\//g, "\\/")}/,
|
||||||
replace: ${typeof replacement === "function" ? replacement.toString() : JSON.stringify(replacement)}
|
replace: ${typeof replacement === "function" ? replacement.toString() : JSON.stringify(replacement)}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
`.trim();
|
`.trim();
|
||||||
}, [find, match, replacement]);
|
}, [finds, match, replacement]);
|
||||||
|
|
||||||
function onFindChange(v: string) {
|
function onFindChange(v: string) {
|
||||||
setFindError(void 0);
|
setFindError(void 0);
|
||||||
setFind(v);
|
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>
|
<Forms.FormTitle>full patch</Forms.FormTitle>
|
||||||
<FullPatchInput
|
<FullPatchInput
|
||||||
setFind={onFindChange}
|
setFind={onFindChange}
|
||||||
|
setFinds={setFinds}
|
||||||
setMatch={onMatchChange}
|
setMatch={onMatchChange}
|
||||||
setReplacement={setReplacement}
|
setReplacement={setReplacement}
|
||||||
/>
|
/>
|
||||||
|
|
||||||
<Forms.FormTitle>find</Forms.FormTitle>
|
<Forms.FormTitle className={Margins.top8}>find</Forms.FormTitle>
|
||||||
<TextInput
|
<TextInput
|
||||||
type="text"
|
type="text"
|
||||||
value={find}
|
value={find}
|
||||||
onChange={onFindChange}
|
onChange={onFindChange}
|
||||||
|
onBlur={onFindBlur}
|
||||||
error={findError}
|
error={findError}
|
||||||
/>
|
/>
|
||||||
|
|
||||||
<Forms.FormTitle>match</Forms.FormTitle>
|
<Forms.FormTitle className={Margins.top8}>match</Forms.FormTitle>
|
||||||
<CheckedTextInput
|
<CheckedTextInput
|
||||||
value={match}
|
value={match}
|
||||||
onChange={onMatchChange}
|
onChange={onMatchChange}
|
||||||
|
@ -342,6 +367,7 @@ function PatchHelper() {
|
||||||
}}
|
}}
|
||||||
/>
|
/>
|
||||||
|
|
||||||
|
<div className={Margins.top8} />
|
||||||
<ReplacementInput
|
<ReplacementInput
|
||||||
replacement={replacement}
|
replacement={replacement}
|
||||||
setReplacement={setReplacement}
|
setReplacement={setReplacement}
|
||||||
|
|
|
@ -79,6 +79,8 @@ for (const p of pluginsValues) {
|
||||||
if (p.patches && isPluginEnabled(p.name)) {
|
if (p.patches && isPluginEnabled(p.name)) {
|
||||||
for (const patch of p.patches) {
|
for (const patch of p.patches) {
|
||||||
patch.plugin = p.name;
|
patch.plugin = p.name;
|
||||||
|
if (!Array.isArray(patch.find))
|
||||||
|
patch.find = [patch.find];
|
||||||
if (!Array.isArray(patch.replacement))
|
if (!Array.isArray(patch.replacement))
|
||||||
patch.replacement = [patch.replacement];
|
patch.replacement = [patch.replacement];
|
||||||
patches.push(patch);
|
patches.push(patch);
|
||||||
|
|
|
@ -18,9 +18,68 @@
|
||||||
|
|
||||||
import { findGroupChildrenByChildId } from "@api/ContextMenu";
|
import { findGroupChildrenByChildId } from "@api/ContextMenu";
|
||||||
import { definePluginSettings } from "@api/Settings";
|
import { definePluginSettings } from "@api/Settings";
|
||||||
|
import ErrorBoundary from "@components/ErrorBoundary";
|
||||||
import { Devs } from "@utils/constants";
|
import { Devs } from "@utils/constants";
|
||||||
import definePlugin, { OptionType } from "@utils/types";
|
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({
|
const settings = definePluginSettings({
|
||||||
forceServerHome: {
|
forceServerHome: {
|
||||||
|
@ -30,12 +89,6 @@ const settings = definePluginSettings({
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
function useForceServerHome() {
|
|
||||||
const { forceServerHome } = settings.use(["forceServerHome"]);
|
|
||||||
|
|
||||||
return forceServerHome;
|
|
||||||
}
|
|
||||||
|
|
||||||
export default definePlugin({
|
export default definePlugin({
|
||||||
name: "ResurrectHome",
|
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.",
|
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)/,
|
match: /getMutableGuildChannelsForGuild\(\i\);return\(0,\i\.useStateFromStores\).+?\]\)(?=}function)/,
|
||||||
replace: m => `${m}&&!$self.useForceServerHome()`
|
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,
|
useForceServerHome,
|
||||||
|
useDisableViewServerHome,
|
||||||
|
|
||||||
contextMenus: {
|
contextMenus: {
|
||||||
"guild-context"(children, props) {
|
"guild-context"(children, props) {
|
||||||
const forceServerHome = useForceServerHome();
|
const { forceServerHome } = settings.use(["forceServerHome"]);
|
||||||
|
|
||||||
if (!props?.guild) return;
|
if (!props?.guild) return;
|
||||||
|
|
||||||
|
|
|
@ -36,7 +36,7 @@ export interface PatchReplacement {
|
||||||
|
|
||||||
export interface Patch {
|
export interface Patch {
|
||||||
plugin: string;
|
plugin: string;
|
||||||
find: string;
|
find: string | string[];
|
||||||
replacement: PatchReplacement | PatchReplacement[];
|
replacement: PatchReplacement | PatchReplacement[];
|
||||||
/** Whether this patch should apply to multiple modules */
|
/** Whether this patch should apply to multiple modules */
|
||||||
all?: boolean;
|
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));
|
const executePatch = traceFunction(`patch by ${patch.plugin}`, (match: string | RegExp, replace: string) => code.replace(match, replace));
|
||||||
if (patch.predicate && !patch.predicate()) continue;
|
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);
|
patchedBy.add(patch.plugin);
|
||||||
|
|
||||||
const previousMod = mod;
|
const previousMod = mod;
|
||||||
|
|
Loading…
Reference in a new issue