add ui and stuffs
This commit is contained in:
parent
c73c3fa636
commit
e0bb9bf77d
|
@ -1,6 +1,12 @@
|
||||||
{
|
{
|
||||||
"extends": "stylelint-config-standard",
|
"extends": "stylelint-config-standard",
|
||||||
"rules": {
|
"rules": {
|
||||||
"indentation": 4
|
"indentation": 4,
|
||||||
|
"selector-class-pattern": [
|
||||||
|
"^[a-z][a-zA-Z0-9]*(-[a-z0-9][a-zA-Z0-9]*)*$",
|
||||||
|
{
|
||||||
|
"message": "Expected class selector to be kebab-case with camelCase segments"
|
||||||
|
}
|
||||||
|
]
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -18,26 +18,23 @@ export const enum OverrideFlags {
|
||||||
|
|
||||||
export interface UserOverride {
|
export interface UserOverride {
|
||||||
username: string;
|
username: string;
|
||||||
avatarUrl: string | null;
|
avatarUrl: string;
|
||||||
bannerUrl: string | null;
|
bannerUrl: string;
|
||||||
flags: OverrideFlags;
|
flags: OverrideFlags;
|
||||||
}
|
}
|
||||||
|
|
||||||
export function makeBlankUserOverride(): UserOverride {
|
export const emptyOverride: UserOverride = Object.freeze({
|
||||||
return {
|
username: "",
|
||||||
username: "",
|
avatarUrl: "",
|
||||||
avatarUrl: "",
|
bannerUrl: "",
|
||||||
bannerUrl: "",
|
flags: OverrideFlags.None,
|
||||||
flags: OverrideFlags.None,
|
});
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
const emptyConstantOverride = makeBlankUserOverride();
|
|
||||||
|
|
||||||
export const settings = definePluginSettings({})
|
export const settings = definePluginSettings({})
|
||||||
.withPrivateSettings<{
|
.withPrivateSettings<{
|
||||||
users?: Record<string, UserOverride>;
|
users?: Record<string, UserOverride>;
|
||||||
}>();
|
}>();
|
||||||
|
|
||||||
export const getUserOverride = (userId: string) => settings.store.users?.[userId] ?? emptyConstantOverride;
|
export const getUserOverride = (userId: string) => settings.store.users?.[userId] ?? emptyOverride;
|
||||||
|
|
||||||
export const hasFlag = (field: OverrideFlags, flag: OverrideFlags) => (field & flag) === flag;
|
export const hasFlag = (field: OverrideFlags, flag: OverrideFlags) => (field & flag) === flag;
|
||||||
|
|
|
@ -4,6 +4,8 @@
|
||||||
* SPDX-License-Identifier: GPL-3.0-or-later
|
* SPDX-License-Identifier: GPL-3.0-or-later
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
import "./styles.css";
|
||||||
|
|
||||||
import { Devs } from "@utils/constants";
|
import { Devs } from "@utils/constants";
|
||||||
import definePlugin from "@utils/types";
|
import definePlugin from "@utils/types";
|
||||||
import { Menu } from "@webpack/common";
|
import { Menu } from "@webpack/common";
|
||||||
|
@ -12,8 +14,6 @@ import { User } from "discord-types/general";
|
||||||
import { getUserOverride, hasFlag, OverrideFlags, settings } from "./data";
|
import { getUserOverride, hasFlag, OverrideFlags, settings } from "./data";
|
||||||
import { openUserEditModal } from "./modal";
|
import { openUserEditModal } from "./modal";
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
export default definePlugin({
|
export default definePlugin({
|
||||||
name: "EditUsers",
|
name: "EditUsers",
|
||||||
description: "Edit users",
|
description: "Edit users",
|
||||||
|
|
|
@ -4,40 +4,167 @@
|
||||||
* SPDX-License-Identifier: GPL-3.0-or-later
|
* SPDX-License-Identifier: GPL-3.0-or-later
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
import { classNameFactory } from "@api/Styles";
|
||||||
import { Flex } from "@components/Flex";
|
import { Flex } from "@components/Flex";
|
||||||
import { ModalCloseButton, ModalContent, ModalFooter, ModalHeader, ModalRoot, openModal } from "@utils/modal";
|
import { Margins } from "@utils/margins";
|
||||||
import { Button, Text } from "@webpack/common";
|
import { ModalCloseButton, ModalContent, ModalFooter, ModalHeader, ModalProps, ModalRoot, ModalSize, openModal } from "@utils/modal";
|
||||||
|
import { Button, DisplayProfileUtils, showToast, Switch, TabBar, Text, TextInput, Toasts, UsernameUtils, useState } from "@webpack/common";
|
||||||
import { User } from "discord-types/general";
|
import { User } from "discord-types/general";
|
||||||
|
import type { Dispatch, SetStateAction } from "react";
|
||||||
|
|
||||||
function EditModal({ user }: { user: User; }) {
|
import { emptyOverride, hasFlag, OverrideFlags, settings, UserOverride } from "./data";
|
||||||
// TODO trolley
|
|
||||||
return null;
|
const cl = classNameFactory("vc-editUsers-");
|
||||||
|
|
||||||
|
interface OverrideProps {
|
||||||
|
override: UserOverride;
|
||||||
|
setOverride: Dispatch<SetStateAction<UserOverride>>;
|
||||||
}
|
}
|
||||||
|
|
||||||
export function openUserEditModal(user) {
|
interface SettingsRowProps extends OverrideProps {
|
||||||
openModal(props => (
|
overrideKey: keyof Omit<UserOverride, "flags">;
|
||||||
<ModalRoot {...props}>
|
name: string;
|
||||||
|
flagDisable: OverrideFlags;
|
||||||
|
flagPrefer: OverrideFlags;
|
||||||
|
placeholder: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
function SettingsRow(props: SettingsRowProps) {
|
||||||
|
const { name, override, setOverride, overrideKey, placeholder, flagDisable, flagPrefer } = props;
|
||||||
|
const namePlural = name + "s";
|
||||||
|
const { flags } = override;
|
||||||
|
|
||||||
|
const toggleFlag = (on: boolean, flag: OverrideFlags) =>
|
||||||
|
on
|
||||||
|
? flags | flag
|
||||||
|
: flags & ~flag;
|
||||||
|
|
||||||
|
return (
|
||||||
|
<>
|
||||||
|
<TextInput
|
||||||
|
className={Margins.bottom16}
|
||||||
|
value={override[overrideKey]}
|
||||||
|
onChange={v => setOverride(o => ({ ...o, [overrideKey]: v }))}
|
||||||
|
placeholder={placeholder}
|
||||||
|
autoFocus
|
||||||
|
/>
|
||||||
|
<Switch
|
||||||
|
value={hasFlag(flags, flagDisable)}
|
||||||
|
onChange={v => setOverride(o => ({ ...o, flags: toggleFlag(v, flagDisable) }))}
|
||||||
|
note={`Will use the user's global ${name} (or your EditUser configured ${name}) over server specific ${namePlural}`}
|
||||||
|
>
|
||||||
|
Disable server specific {namePlural}
|
||||||
|
</Switch>
|
||||||
|
<Switch
|
||||||
|
value={hasFlag(flags, flagPrefer)}
|
||||||
|
onChange={v => setOverride(o => ({ ...o, flags: toggleFlag(v, flagPrefer) }))}
|
||||||
|
note={`Will use server specific ${namePlural} over the EditUser configured ${name}`}
|
||||||
|
hideBorder
|
||||||
|
>
|
||||||
|
Prefer server specific {namePlural}
|
||||||
|
</Switch>
|
||||||
|
</>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
const Tabs = {
|
||||||
|
username: {
|
||||||
|
name: "Username",
|
||||||
|
flagDisable: OverrideFlags.DisableNicks,
|
||||||
|
flagPrefer: OverrideFlags.PreferServerNicks,
|
||||||
|
placeholder: (user: User) => UsernameUtils.getName(user),
|
||||||
|
},
|
||||||
|
avatarUrl: {
|
||||||
|
name: "Avatar",
|
||||||
|
flagDisable: OverrideFlags.DisableServerAvatars,
|
||||||
|
flagPrefer: OverrideFlags.KeepServerAvatar,
|
||||||
|
placeholder: (user: User) => user.getAvatarURL(),
|
||||||
|
},
|
||||||
|
bannerUrl: {
|
||||||
|
name: "Banner",
|
||||||
|
flagDisable: OverrideFlags.DisableServerBanners,
|
||||||
|
flagPrefer: OverrideFlags.KeepServerBanner,
|
||||||
|
placeholder: (user: User) => DisplayProfileUtils.getDisplayProfile(user.id)?.getBannerURL({ canAnimate: true, size: 64 }) ?? "",
|
||||||
|
},
|
||||||
|
} as const;
|
||||||
|
const TabKeys = Object.keys(Tabs) as (keyof typeof Tabs)[];
|
||||||
|
|
||||||
|
function EditTabs({ user, override, setOverride }: { user: User; } & OverrideProps) {
|
||||||
|
const [currentTabName, setCurrentTabName] = useState(TabKeys[0]);
|
||||||
|
|
||||||
|
const currentTab = Tabs[currentTabName];
|
||||||
|
|
||||||
|
return (
|
||||||
|
<>
|
||||||
|
<TabBar
|
||||||
|
type="top"
|
||||||
|
look="brand"
|
||||||
|
className={cl("tabBar")}
|
||||||
|
selectedItem={currentTabName}
|
||||||
|
onItemSelect={setCurrentTabName}
|
||||||
|
>
|
||||||
|
{TabKeys.map(key => (
|
||||||
|
<TabBar.Item
|
||||||
|
id={key}
|
||||||
|
key={key}
|
||||||
|
className={cl("tab")}
|
||||||
|
>
|
||||||
|
{Tabs[key].name}
|
||||||
|
</TabBar.Item>
|
||||||
|
))}
|
||||||
|
</TabBar>
|
||||||
|
|
||||||
|
<SettingsRow
|
||||||
|
{...currentTab}
|
||||||
|
override={override}
|
||||||
|
overrideKey={currentTabName}
|
||||||
|
setOverride={setOverride}
|
||||||
|
placeholder={currentTab.placeholder(user)}
|
||||||
|
/>
|
||||||
|
</>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
function EditModal({ user, modalProps }: { user: User; modalProps: ModalProps; }) {
|
||||||
|
const [override, setOverride] = useState(() => ({ ...settings.store.users?.[user.id] ?? emptyOverride }));
|
||||||
|
|
||||||
|
return (
|
||||||
|
<ModalRoot {...modalProps} size={ModalSize.MEDIUM}>
|
||||||
<ModalHeader>
|
<ModalHeader>
|
||||||
<Text variant="heading-lg/semibold" style={{ flexGrow: 1 }}>Notification Log</Text>
|
<Text variant="heading-lg/semibold" style={{ flexGrow: 1 }}>Edit User {user.username}</Text>
|
||||||
<ModalCloseButton onClick={props.onClose} />
|
<ModalCloseButton onClick={modalProps.onClose} />
|
||||||
</ModalHeader>
|
</ModalHeader>
|
||||||
|
|
||||||
<ModalContent>
|
<ModalContent>
|
||||||
<EditModal user={user} />
|
<div className={cl("modal")}>
|
||||||
|
<EditTabs user={user} override={override} setOverride={setOverride} />
|
||||||
|
</div>
|
||||||
</ModalContent>
|
</ModalContent>
|
||||||
|
|
||||||
<ModalFooter>
|
<ModalFooter>
|
||||||
<Flex>
|
<Flex>
|
||||||
<Button
|
<Button
|
||||||
color={Button.Colors.RED}
|
color={Button.Colors.RED}
|
||||||
|
onClick={() => setOverride({ ...emptyOverride })}
|
||||||
>
|
>
|
||||||
Reset Settings
|
Reset All
|
||||||
</Button>
|
</Button>
|
||||||
<Button>
|
<Button
|
||||||
|
onClick={() => {
|
||||||
|
const s = settings.store.users ??= {};
|
||||||
|
s[user.id] = override;
|
||||||
|
modalProps.onClose();
|
||||||
|
showToast("Saved! Switch chats for changes to apply everywhere", Toasts.Type.SUCCESS, { position: Toasts.Position.BOTTOM });
|
||||||
|
}}
|
||||||
|
>
|
||||||
Save
|
Save
|
||||||
</Button>
|
</Button>
|
||||||
</Flex>
|
</Flex>
|
||||||
</ModalFooter>
|
</ModalFooter>
|
||||||
</ModalRoot>
|
</ModalRoot>
|
||||||
));
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
export function openUserEditModal(user: User) {
|
||||||
|
openModal(props => <EditModal user={user} modalProps={props} />);
|
||||||
}
|
}
|
||||||
|
|
14
src/plugins/editUsers/styles.css
Normal file
14
src/plugins/editUsers/styles.css
Normal file
|
@ -0,0 +1,14 @@
|
||||||
|
.vc-editUsers-modal {
|
||||||
|
padding: 1em 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
.vc-editUsers-tabBar {
|
||||||
|
gap: 1em;
|
||||||
|
margin-bottom: 16px;
|
||||||
|
border-bottom: 2px solid var(--background-modifier-accent);
|
||||||
|
}
|
||||||
|
|
||||||
|
.vc-editUsers-tab {
|
||||||
|
padding-bottom: 8px;
|
||||||
|
}
|
||||||
|
|
|
@ -24,7 +24,7 @@ import { DevsById } from "./constants";
|
||||||
* Calls .join(" ") on the arguments
|
* Calls .join(" ") on the arguments
|
||||||
* classes("one", "two") => "one two"
|
* classes("one", "two") => "one two"
|
||||||
*/
|
*/
|
||||||
export function classes(...classes: Array<string | null | undefined>) {
|
export function classes(...classes: Array<string | null | undefined | false>) {
|
||||||
return classes.filter(Boolean).join(" ");
|
return classes.filter(Boolean).join(" ");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
62
src/webpack/common/types/utils.d.ts
vendored
62
src/webpack/common/types/utils.d.ts
vendored
|
@ -16,7 +16,7 @@
|
||||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import { Guild, GuildMember } from "discord-types/general";
|
import { Guild, GuildMember, User } from "discord-types/general";
|
||||||
import type { ReactNode } from "react";
|
import type { ReactNode } from "react";
|
||||||
import { LiteralUnion } from "type-fest";
|
import { LiteralUnion } from "type-fest";
|
||||||
|
|
||||||
|
@ -256,3 +256,63 @@ export interface PopoutActions {
|
||||||
close(key: string): void;
|
close(key: string): void;
|
||||||
setAlwaysOnTop(key: string, alwaysOnTop: boolean): void;
|
setAlwaysOnTop(key: string, alwaysOnTop: boolean): void;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export type UserNameUtilsTagInclude = LiteralUnion<"auto" | "always" | "never", string>;
|
||||||
|
export interface UserNameUtilsTagOptions {
|
||||||
|
forcePomelo?: boolean;
|
||||||
|
identifiable?: UserNameUtilsTagInclude;
|
||||||
|
decoration?: UserNameUtilsTagInclude;
|
||||||
|
mode?: "full" | "username";
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface UsernameUtils {
|
||||||
|
getGlobalName(user: User): string;
|
||||||
|
getFormattedName(user: User, useTagInsteadOfUsername?: boolean): string;
|
||||||
|
getName(user: User): string;
|
||||||
|
useName(user: User): string;
|
||||||
|
getUserTag(user: User, options?: UserNameUtilsTagOptions): string;
|
||||||
|
useUserTag(user: User, options?: UserNameUtilsTagOptions): string;
|
||||||
|
|
||||||
|
|
||||||
|
useDirectMessageRecipient: any;
|
||||||
|
humanizeStatus: any;
|
||||||
|
}
|
||||||
|
|
||||||
|
export class DisplayProfile {
|
||||||
|
userId: string;
|
||||||
|
banner?: string;
|
||||||
|
bio?: string;
|
||||||
|
pronouns?: string;
|
||||||
|
accentColor?: number;
|
||||||
|
themeColors?: number[];
|
||||||
|
popoutAnimationParticleType?: any;
|
||||||
|
profileEffectId?: string;
|
||||||
|
_userProfile?: any;
|
||||||
|
_guildMemberProfile?: any;
|
||||||
|
canUsePremiumProfileCustomization: boolean;
|
||||||
|
canEditThemes: boolean;
|
||||||
|
premiumGuildSince: Date | null;
|
||||||
|
premiumSince: Date | null;
|
||||||
|
premiumType?: number;
|
||||||
|
primaryColor?: number;
|
||||||
|
|
||||||
|
getBadges(): Array<{
|
||||||
|
id: string;
|
||||||
|
description: string;
|
||||||
|
icon: string;
|
||||||
|
link?: string;
|
||||||
|
}>;
|
||||||
|
getBannerURL(options: { canAnimate: boolean; size: number; }): string;
|
||||||
|
getLegacyUsername(): string | null;
|
||||||
|
hasFullProfile(): boolean;
|
||||||
|
hasPremiumCustomization(): boolean;
|
||||||
|
hasThemeColors(): boolean;
|
||||||
|
isUsingGuildMemberBanner(): boolean;
|
||||||
|
isUsingGuildMemberBio(): boolean;
|
||||||
|
isUsingGuildMemberPronouns(): boolean;
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface DisplayProfileUtils {
|
||||||
|
getDisplayProfile(userId: string, guildId?: string, customStores?: any): DisplayProfile | null;
|
||||||
|
useDisplayProfile(userId: string, guildId?: string, customStores?: any): DisplayProfile | null;
|
||||||
|
}
|
||||||
|
|
|
@ -73,6 +73,25 @@ const ToastPosition = {
|
||||||
BOTTOM: 1
|
BOTTOM: 1
|
||||||
};
|
};
|
||||||
|
|
||||||
|
export interface ToastData {
|
||||||
|
message: string,
|
||||||
|
id: string,
|
||||||
|
/**
|
||||||
|
* Toasts.Type
|
||||||
|
*/
|
||||||
|
type: number,
|
||||||
|
options?: ToastOptions;
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface ToastOptions {
|
||||||
|
/**
|
||||||
|
* Toasts.Position
|
||||||
|
*/
|
||||||
|
position?: number;
|
||||||
|
component?: React.ReactNode,
|
||||||
|
duration?: number;
|
||||||
|
}
|
||||||
|
|
||||||
export const Toasts = {
|
export const Toasts = {
|
||||||
Type: ToastType,
|
Type: ToastType,
|
||||||
Position: ToastPosition,
|
Position: ToastPosition,
|
||||||
|
@ -81,23 +100,9 @@ export const Toasts = {
|
||||||
|
|
||||||
// hack to merge with the following interface, dunno if there's a better way
|
// hack to merge with the following interface, dunno if there's a better way
|
||||||
...{} as {
|
...{} as {
|
||||||
show(data: {
|
show(data: ToastData): void;
|
||||||
message: string,
|
|
||||||
id: string,
|
|
||||||
/**
|
|
||||||
* Toasts.Type
|
|
||||||
*/
|
|
||||||
type: number,
|
|
||||||
options?: {
|
|
||||||
/**
|
|
||||||
* Toasts.Position
|
|
||||||
*/
|
|
||||||
position?: number;
|
|
||||||
component?: React.ReactNode,
|
|
||||||
duration?: number;
|
|
||||||
};
|
|
||||||
}): void;
|
|
||||||
pop(): void;
|
pop(): void;
|
||||||
|
create(message: string, type: number, options?: ToastOptions): ToastData;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -105,18 +110,15 @@ export const Toasts = {
|
||||||
waitFor("showToast", m => {
|
waitFor("showToast", m => {
|
||||||
Toasts.show = m.showToast;
|
Toasts.show = m.showToast;
|
||||||
Toasts.pop = m.popToast;
|
Toasts.pop = m.popToast;
|
||||||
|
Toasts.create = m.createToast;
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Show a simple toast. If you need more options, use Toasts.show manually
|
* Show a simple toast. If you need more options, use Toasts.show manually
|
||||||
*/
|
*/
|
||||||
export function showToast(message: string, type = ToastType.MESSAGE) {
|
export function showToast(message: string, type = ToastType.MESSAGE, options?: ToastOptions) {
|
||||||
Toasts.show({
|
Toasts.show(Toasts.create(message, type, options));
|
||||||
id: Toasts.genId(),
|
|
||||||
message,
|
|
||||||
type
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export const UserUtils = {
|
export const UserUtils = {
|
||||||
|
@ -172,3 +174,9 @@ export const PopoutActions: t.PopoutActions = mapMangledModuleLazy('type:"POPOUT
|
||||||
close: filters.byCode('type:"POPOUT_WINDOW_CLOSE"'),
|
close: filters.byCode('type:"POPOUT_WINDOW_CLOSE"'),
|
||||||
setAlwaysOnTop: filters.byCode('type:"POPOUT_WINDOW_SET_ALWAYS_ON_TOP"'),
|
setAlwaysOnTop: filters.byCode('type:"POPOUT_WINDOW_SET_ALWAYS_ON_TOP"'),
|
||||||
});
|
});
|
||||||
|
|
||||||
|
export const UsernameUtils: t.UsernameUtils = findByPropsLazy("useName", "getGlobalName");
|
||||||
|
export const DisplayProfileUtils: t.DisplayProfileUtils = mapMangledModuleLazy(/=\i\.getUserProfile\(\i\),\i=\i\.getGuildMemberProfile\(/, {
|
||||||
|
getDisplayProfile: filters.byCode(".getGuildMemberProfile("),
|
||||||
|
useDisplayProfile: filters.byCode(/\[\i\.\i,\i\.\i],\(\)=>/)
|
||||||
|
});
|
||||||
|
|
|
@ -38,31 +38,43 @@ export let cache: WebpackInstance["c"];
|
||||||
|
|
||||||
export type FilterFn = (mod: any) => boolean;
|
export type FilterFn = (mod: any) => boolean;
|
||||||
|
|
||||||
|
type PropsFilter = Array<string>;
|
||||||
|
type CodeFilter = Array<string | RegExp>;
|
||||||
|
type StoreNameFilter = string;
|
||||||
|
|
||||||
|
const stringMatches = (s: string, filter: CodeFilter) =>
|
||||||
|
filter.every(f =>
|
||||||
|
typeof f === "string"
|
||||||
|
? s.includes(f)
|
||||||
|
: f.test(s)
|
||||||
|
);
|
||||||
|
|
||||||
export const filters = {
|
export const filters = {
|
||||||
byProps: (...props: string[]): FilterFn =>
|
byProps: (...props: PropsFilter): FilterFn =>
|
||||||
props.length === 1
|
props.length === 1
|
||||||
? m => m[props[0]] !== void 0
|
? m => m[props[0]] !== void 0
|
||||||
: m => props.every(p => m[p] !== void 0),
|
: m => props.every(p => m[p] !== void 0),
|
||||||
|
|
||||||
byCode: (...code: string[]): FilterFn => m => {
|
byCode: (...code: CodeFilter): FilterFn => {
|
||||||
if (typeof m !== "function") return false;
|
code = code.map(canonicalizeMatch);
|
||||||
const s = Function.prototype.toString.call(m);
|
return m => {
|
||||||
for (const c of code) {
|
if (typeof m !== "function") return false;
|
||||||
if (!s.includes(c)) return false;
|
return stringMatches(Function.prototype.toString.call(m), code);
|
||||||
}
|
};
|
||||||
return true;
|
|
||||||
},
|
},
|
||||||
byStoreName: (name: string): FilterFn => m =>
|
byStoreName: (name: StoreNameFilter): FilterFn => m =>
|
||||||
m.constructor?.displayName === name,
|
m.constructor?.displayName === name,
|
||||||
|
|
||||||
componentByCode: (...code: string[]): FilterFn => {
|
componentByCode: (...code: CodeFilter): FilterFn => {
|
||||||
const filter = filters.byCode(...code);
|
const filter = filters.byCode(...code);
|
||||||
return m => {
|
return m => {
|
||||||
if (filter(m)) return true;
|
if (filter(m)) return true;
|
||||||
if (!m.$$typeof) return false;
|
if (!m.$$typeof) return false;
|
||||||
if (m.type && m.type.render) return filter(m.type.render); // memo + forwardRef
|
if (m.type)
|
||||||
if (m.type) return filter(m.type); // memos
|
return m.type.render
|
||||||
if (m.render) return filter(m.render); // forwardRefs
|
? filter(m.type.render) // memo + forwardRef
|
||||||
|
: filter(m.type); // memo
|
||||||
|
if (m.render) return filter(m.render); // forwardRef
|
||||||
return false;
|
return false;
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
@ -245,15 +257,9 @@ export const findBulk = traceFunction("findBulk", function findBulk(...filterFns
|
||||||
* Find the id of the first module factory that includes all the given code
|
* Find the id of the first module factory that includes all the given code
|
||||||
* @returns string or null
|
* @returns string or null
|
||||||
*/
|
*/
|
||||||
export const findModuleId = traceFunction("findModuleId", function findModuleId(...code: string[]) {
|
export const findModuleId = traceFunction("findModuleId", function findModuleId(...code: CodeFilter) {
|
||||||
outer:
|
|
||||||
for (const id in wreq.m) {
|
for (const id in wreq.m) {
|
||||||
const str = wreq.m[id].toString();
|
if (stringMatches(wreq.m[id].toString(), code)) return id;
|
||||||
|
|
||||||
for (const c of code) {
|
|
||||||
if (!str.includes(c)) continue outer;
|
|
||||||
}
|
|
||||||
return id;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const err = new Error("Didn't find module with code(s):\n" + code.join("\n"));
|
const err = new Error("Didn't find module with code(s):\n" + code.join("\n"));
|
||||||
|
@ -272,7 +278,7 @@ export const findModuleId = traceFunction("findModuleId", function findModuleId(
|
||||||
* Find the first module factory that includes all the given code
|
* Find the first module factory that includes all the given code
|
||||||
* @returns The module factory or null
|
* @returns The module factory or null
|
||||||
*/
|
*/
|
||||||
export function findModuleFactory(...code: string[]) {
|
export function findModuleFactory(...code: CodeFilter) {
|
||||||
const id = findModuleId(...code);
|
const id = findModuleId(...code);
|
||||||
if (!id) return null;
|
if (!id) return null;
|
||||||
|
|
||||||
|
@ -325,7 +331,7 @@ export function findLazy(filter: FilterFn) {
|
||||||
/**
|
/**
|
||||||
* Find the first module that has the specified properties
|
* Find the first module that has the specified properties
|
||||||
*/
|
*/
|
||||||
export function findByProps(...props: string[]) {
|
export function findByProps(...props: PropsFilter) {
|
||||||
const res = find(filters.byProps(...props), { isIndirect: true });
|
const res = find(filters.byProps(...props), { isIndirect: true });
|
||||||
if (!res)
|
if (!res)
|
||||||
handleModuleNotFound("findByProps", ...props);
|
handleModuleNotFound("findByProps", ...props);
|
||||||
|
@ -335,7 +341,7 @@ export function findByProps(...props: string[]) {
|
||||||
/**
|
/**
|
||||||
* Find the first module that has the specified properties, lazily
|
* Find the first module that has the specified properties, lazily
|
||||||
*/
|
*/
|
||||||
export function findByPropsLazy(...props: string[]) {
|
export function findByPropsLazy(...props: PropsFilter) {
|
||||||
if (IS_REPORTER) lazyWebpackSearchHistory.push(["findByProps", props]);
|
if (IS_REPORTER) lazyWebpackSearchHistory.push(["findByProps", props]);
|
||||||
|
|
||||||
return proxyLazy(() => findByProps(...props));
|
return proxyLazy(() => findByProps(...props));
|
||||||
|
@ -344,7 +350,7 @@ export function findByPropsLazy(...props: string[]) {
|
||||||
/**
|
/**
|
||||||
* Find the first function that includes all the given code
|
* Find the first function that includes all the given code
|
||||||
*/
|
*/
|
||||||
export function findByCode(...code: string[]) {
|
export function findByCode(...code: CodeFilter) {
|
||||||
const res = find(filters.byCode(...code), { isIndirect: true });
|
const res = find(filters.byCode(...code), { isIndirect: true });
|
||||||
if (!res)
|
if (!res)
|
||||||
handleModuleNotFound("findByCode", ...code);
|
handleModuleNotFound("findByCode", ...code);
|
||||||
|
@ -354,7 +360,7 @@ export function findByCode(...code: string[]) {
|
||||||
/**
|
/**
|
||||||
* Find the first function that includes all the given code, lazily
|
* Find the first function that includes all the given code, lazily
|
||||||
*/
|
*/
|
||||||
export function findByCodeLazy(...code: string[]) {
|
export function findByCodeLazy(...code: CodeFilter) {
|
||||||
if (IS_REPORTER) lazyWebpackSearchHistory.push(["findByCode", code]);
|
if (IS_REPORTER) lazyWebpackSearchHistory.push(["findByCode", code]);
|
||||||
|
|
||||||
return proxyLazy(() => findByCode(...code));
|
return proxyLazy(() => findByCode(...code));
|
||||||
|
@ -363,7 +369,7 @@ export function findByCodeLazy(...code: string[]) {
|
||||||
/**
|
/**
|
||||||
* Find a store by its displayName
|
* Find a store by its displayName
|
||||||
*/
|
*/
|
||||||
export function findStore(name: string) {
|
export function findStore(name: StoreNameFilter) {
|
||||||
const res = find(filters.byStoreName(name), { isIndirect: true });
|
const res = find(filters.byStoreName(name), { isIndirect: true });
|
||||||
if (!res)
|
if (!res)
|
||||||
handleModuleNotFound("findStore", name);
|
handleModuleNotFound("findStore", name);
|
||||||
|
@ -373,7 +379,7 @@ export function findStore(name: string) {
|
||||||
/**
|
/**
|
||||||
* Find a store by its displayName, lazily
|
* Find a store by its displayName, lazily
|
||||||
*/
|
*/
|
||||||
export function findStoreLazy(name: string) {
|
export function findStoreLazy(name: StoreNameFilter) {
|
||||||
if (IS_REPORTER) lazyWebpackSearchHistory.push(["findStore", [name]]);
|
if (IS_REPORTER) lazyWebpackSearchHistory.push(["findStore", [name]]);
|
||||||
|
|
||||||
return proxyLazy(() => findStore(name));
|
return proxyLazy(() => findStore(name));
|
||||||
|
@ -382,7 +388,7 @@ export function findStoreLazy(name: string) {
|
||||||
/**
|
/**
|
||||||
* Finds the component which includes all the given code. Checks for plain components, memos and forwardRefs
|
* Finds the component which includes all the given code. Checks for plain components, memos and forwardRefs
|
||||||
*/
|
*/
|
||||||
export function findComponentByCode(...code: string[]) {
|
export function findComponentByCode(...code: CodeFilter) {
|
||||||
const res = find(filters.componentByCode(...code), { isIndirect: true });
|
const res = find(filters.componentByCode(...code), { isIndirect: true });
|
||||||
if (!res)
|
if (!res)
|
||||||
handleModuleNotFound("findComponentByCode", ...code);
|
handleModuleNotFound("findComponentByCode", ...code);
|
||||||
|
@ -407,7 +413,7 @@ export function findComponentLazy<T extends object = any>(filter: FilterFn) {
|
||||||
/**
|
/**
|
||||||
* Finds the first component that includes all the given code, lazily
|
* Finds the first component that includes all the given code, lazily
|
||||||
*/
|
*/
|
||||||
export function findComponentByCodeLazy<T extends object = any>(...code: string[]) {
|
export function findComponentByCodeLazy<T extends object = any>(...code: CodeFilter) {
|
||||||
if (IS_REPORTER) lazyWebpackSearchHistory.push(["findComponentByCode", code]);
|
if (IS_REPORTER) lazyWebpackSearchHistory.push(["findComponentByCode", code]);
|
||||||
|
|
||||||
return LazyComponent<T>(() => {
|
return LazyComponent<T>(() => {
|
||||||
|
@ -421,7 +427,7 @@ export function findComponentByCodeLazy<T extends object = any>(...code: string[
|
||||||
/**
|
/**
|
||||||
* Finds the first component that is exported by the first prop name, lazily
|
* Finds the first component that is exported by the first prop name, lazily
|
||||||
*/
|
*/
|
||||||
export function findExportedComponentLazy<T extends object = any>(...props: string[]) {
|
export function findExportedComponentLazy<T extends object = any>(...props: PropsFilter) {
|
||||||
if (IS_REPORTER) lazyWebpackSearchHistory.push(["findExportedComponent", props]);
|
if (IS_REPORTER) lazyWebpackSearchHistory.push(["findExportedComponent", props]);
|
||||||
|
|
||||||
return LazyComponent<T>(() => {
|
return LazyComponent<T>(() => {
|
||||||
|
@ -445,10 +451,13 @@ export function findExportedComponentLazy<T extends object = any>(...props: stri
|
||||||
* closeModal: filters.byCode("key==")
|
* closeModal: filters.byCode("key==")
|
||||||
* })
|
* })
|
||||||
*/
|
*/
|
||||||
export const mapMangledModule = traceFunction("mapMangledModule", function mapMangledModule<S extends string>(code: string, mappers: Record<S, FilterFn>): Record<S, any> {
|
export const mapMangledModule = traceFunction("mapMangledModule", function mapMangledModule<S extends string>(code: string | RegExp | CodeFilter, mappers: Record<S, FilterFn>): Record<S, any> {
|
||||||
|
if (!Array.isArray(code)) code = [code];
|
||||||
|
code = code.map(canonicalizeMatch);
|
||||||
|
|
||||||
const exports = {} as Record<S, any>;
|
const exports = {} as Record<S, any>;
|
||||||
|
|
||||||
const id = findModuleId(code);
|
const id = findModuleId(...code);
|
||||||
if (id === null)
|
if (id === null)
|
||||||
return exports;
|
return exports;
|
||||||
|
|
||||||
|
@ -482,7 +491,7 @@ export const mapMangledModule = traceFunction("mapMangledModule", function mapMa
|
||||||
* closeModal: filters.byCode("key==")
|
* closeModal: filters.byCode("key==")
|
||||||
* })
|
* })
|
||||||
*/
|
*/
|
||||||
export function mapMangledModuleLazy<S extends string>(code: string, mappers: Record<S, FilterFn>): Record<S, any> {
|
export function mapMangledModuleLazy<S extends string>(code: string | RegExp | CodeFilter, mappers: Record<S, FilterFn>): Record<S, any> {
|
||||||
if (IS_REPORTER) lazyWebpackSearchHistory.push(["mapMangledModule", [code, mappers]]);
|
if (IS_REPORTER) lazyWebpackSearchHistory.push(["mapMangledModule", [code, mappers]]);
|
||||||
|
|
||||||
return proxyLazy(() => mapMangledModule(code, mappers));
|
return proxyLazy(() => mapMangledModule(code, mappers));
|
||||||
|
@ -497,7 +506,7 @@ export const ChunkIdsRegex = /\("([^"]+?)"\)/g;
|
||||||
* @param matcher A RegExp that returns the chunk ids array as the first capture group and the entry point id as the second. Defaults to a matcher that captures the first lazy chunk loading found in the module factory
|
* @param matcher A RegExp that returns the chunk ids array as the first capture group and the entry point id as the second. Defaults to a matcher that captures the first lazy chunk loading found in the module factory
|
||||||
* @returns A promise that resolves with a boolean whether the chunks were loaded
|
* @returns A promise that resolves with a boolean whether the chunks were loaded
|
||||||
*/
|
*/
|
||||||
export async function extractAndLoadChunks(code: string[], matcher: RegExp = DefaultExtractAndLoadChunksRegex) {
|
export async function extractAndLoadChunks(code: CodeFilter, matcher: RegExp = DefaultExtractAndLoadChunksRegex) {
|
||||||
const module = findModuleFactory(...code);
|
const module = findModuleFactory(...code);
|
||||||
if (!module) {
|
if (!module) {
|
||||||
const err = new Error("extractAndLoadChunks: Couldn't find module factory");
|
const err = new Error("extractAndLoadChunks: Couldn't find module factory");
|
||||||
|
@ -562,7 +571,7 @@ export async function extractAndLoadChunks(code: string[], matcher: RegExp = Def
|
||||||
* @param matcher A RegExp that returns the chunk ids array as the first capture group and the entry point id as the second. Defaults to a matcher that captures the first lazy chunk loading found in the module factory
|
* @param matcher A RegExp that returns the chunk ids array as the first capture group and the entry point id as the second. Defaults to a matcher that captures the first lazy chunk loading found in the module factory
|
||||||
* @returns A function that returns a promise that resolves with a boolean whether the chunks were loaded, on first call
|
* @returns A function that returns a promise that resolves with a boolean whether the chunks were loaded, on first call
|
||||||
*/
|
*/
|
||||||
export function extractAndLoadChunksLazy(code: string[], matcher = DefaultExtractAndLoadChunksRegex) {
|
export function extractAndLoadChunksLazy(code: CodeFilter, matcher = DefaultExtractAndLoadChunksRegex) {
|
||||||
if (IS_REPORTER) lazyWebpackSearchHistory.push(["extractAndLoadChunks", [code, matcher]]);
|
if (IS_REPORTER) lazyWebpackSearchHistory.push(["extractAndLoadChunks", [code, matcher]]);
|
||||||
|
|
||||||
return makeLazy(() => extractAndLoadChunks(code, matcher));
|
return makeLazy(() => extractAndLoadChunks(code, matcher));
|
||||||
|
@ -572,7 +581,7 @@ export function extractAndLoadChunksLazy(code: string[], matcher = DefaultExtrac
|
||||||
* Wait for a module that matches the provided filter to be registered,
|
* Wait for a module that matches the provided filter to be registered,
|
||||||
* then call the callback with the module as the first argument
|
* then call the callback with the module as the first argument
|
||||||
*/
|
*/
|
||||||
export function waitFor(filter: string | string[] | FilterFn, callback: CallbackFn, { isIndirect = false }: { isIndirect?: boolean; } = {}) {
|
export function waitFor(filter: string | PropsFilter | FilterFn, callback: CallbackFn, { isIndirect = false }: { isIndirect?: boolean; } = {}) {
|
||||||
if (IS_REPORTER && !isIndirect) lazyWebpackSearchHistory.push(["waitFor", Array.isArray(filter) ? filter : [filter]]);
|
if (IS_REPORTER && !isIndirect) lazyWebpackSearchHistory.push(["waitFor", Array.isArray(filter) ? filter : [filter]]);
|
||||||
|
|
||||||
if (typeof filter === "string")
|
if (typeof filter === "string")
|
||||||
|
@ -593,21 +602,18 @@ export function waitFor(filter: string | string[] | FilterFn, callback: Callback
|
||||||
/**
|
/**
|
||||||
* Search modules by keyword. This searches the factory methods,
|
* Search modules by keyword. This searches the factory methods,
|
||||||
* meaning you can search all sorts of things, displayName, methodName, strings somewhere in the code, etc
|
* meaning you can search all sorts of things, displayName, methodName, strings somewhere in the code, etc
|
||||||
* @param filters One or more strings or regexes
|
* @param code One or more strings or regexes
|
||||||
* @returns Mapping of found modules
|
* @returns Mapping of found modules
|
||||||
*/
|
*/
|
||||||
export function search(...filters: Array<string | RegExp>) {
|
export function search(...code: CodeFilter) {
|
||||||
const results = {} as Record<number, Function>;
|
const results = {} as Record<number, Function>;
|
||||||
const factories = wreq.m;
|
const factories = wreq.m;
|
||||||
outer:
|
|
||||||
for (const id in factories) {
|
for (const id in factories) {
|
||||||
const factory = factories[id].original ?? factories[id];
|
const factory = factories[id].original ?? factories[id];
|
||||||
const str: string = factory.toString();
|
|
||||||
for (const filter of filters) {
|
if (stringMatches(factory.toString(), code))
|
||||||
if (typeof filter === "string" && !str.includes(filter)) continue outer;
|
results[id] = factory;
|
||||||
if (filter instanceof RegExp && !filter.test(str)) continue outer;
|
|
||||||
}
|
|
||||||
results[id] = factory;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return results;
|
return results;
|
||||||
|
|
Loading…
Reference in a new issue