Fix ViewIcons & plugins that use image modals

Co-Authored-By: sadan <117494111+sadan4@users.noreply.github.com>
This commit is contained in:
Vendicated 2024-10-23 03:57:46 +02:00
parent 58c3032bb2
commit a11ccde40f
No known key found for this signature in database
GPG key ID: D66986BAF75ECF18
9 changed files with 114 additions and 54 deletions

View file

@ -67,7 +67,10 @@ const settings = definePluginSettings({
}
});
function openImage(url: string) {
const openAvatar = (url: string) => openImage(url, 512, 512);
const openBanner = (url: string) => openImage(url, 1024);
function openImage(url: string, width: number, height?: number) {
const format = url.startsWith("/") ? "png" : settings.store.format;
const u = new URL(url, window.location.href);
@ -76,11 +79,13 @@ function openImage(url: string) {
url = u.toString();
u.searchParams.set("size", "4096");
const originalUrl = u.toString();
const original = u.toString();
openImageModal(url, {
original: originalUrl,
height: 256
openImageModal({
url,
original,
width,
height
});
}
@ -93,14 +98,14 @@ const UserContext: NavContextMenuPatchCallback = (children, { user, guildId }: U
<Menu.MenuItem
id="view-avatar"
label="View Avatar"
action={() => openImage(IconUtils.getUserAvatarURL(user, true))}
action={() => openAvatar(IconUtils.getUserAvatarURL(user, true))}
icon={ImageIcon}
/>
{memberAvatar && (
<Menu.MenuItem
id="view-server-avatar"
label="View Server Avatar"
action={() => openImage(IconUtils.getGuildMemberAvatarURLSimple({
action={() => openAvatar(IconUtils.getGuildMemberAvatarURLSimple({
userId: user.id,
avatar: memberAvatar,
guildId: guildId!,
@ -126,7 +131,7 @@ const GuildContext: NavContextMenuPatchCallback = (children, { guild }: GuildCon
id="view-icon"
label="View Icon"
action={() =>
openImage(IconUtils.getGuildIconURL({
openAvatar(IconUtils.getGuildIconURL({
id,
icon,
canAnimate: true
@ -140,7 +145,7 @@ const GuildContext: NavContextMenuPatchCallback = (children, { guild }: GuildCon
id="view-banner"
label="View Banner"
action={() =>
openImage(IconUtils.getGuildBannerURL(guild, true)!)
openBanner(IconUtils.getGuildBannerURL(guild, true)!)
}
icon={ImageIcon}
/>
@ -158,7 +163,7 @@ const GroupDMContext: NavContextMenuPatchCallback = (children, { channel }: Grou
id="view-group-channel-icon"
label="View Icon"
action={() =>
openImage(IconUtils.getChannelIconURL(channel)!)
openAvatar(IconUtils.getChannelIconURL(channel)!)
}
icon={ImageIcon}
/>
@ -171,10 +176,12 @@ export default definePlugin({
authors: [Devs.Ven, Devs.TheKodeToad, Devs.Nuckyz, Devs.nyx],
description: "Makes avatars and banners in user profiles clickable, adds View Icon/Banner entries in the user, server and group channel context menu.",
tags: ["ImageUtilities"],
dependencies: ["DynamicImageModalAPI"],
settings,
openImage,
openAvatar,
openBanner,
contextMenus: {
"user-context": UserContext,
@ -188,7 +195,7 @@ export default definePlugin({
find: ".overlay:void 0,status:",
replacement: {
match: /avatarSrc:(\i),eventHandlers:(\i).+?"div",{...\2,/,
replace: "$&style:{cursor:\"pointer\"},onClick:()=>{$self.openImage($1)},"
replace: "$&style:{cursor:\"pointer\"},onClick:()=>{$self.openAvatar($1)},"
},
all: true
},
@ -197,7 +204,7 @@ export default definePlugin({
find: 'backgroundColor:"COMPLETE"',
replacement: {
match: /(\.banner,.+?),style:{(?=.+?backgroundImage:null!=(\i)\?"url\("\.concat\(\2,)/,
replace: (_, rest, bannerSrc) => `${rest},onClick:()=>${bannerSrc}!=null&&$self.openImage(${bannerSrc}),style:{cursor:${bannerSrc}!=null?"pointer":void 0,`
replace: (_, rest, bannerSrc) => `${rest},onClick:()=>${bannerSrc}!=null&&$self.openBanner(${bannerSrc}),style:{cursor:${bannerSrc}!=null?"pointer":void 0,`
}
},
// Group DMs top small & large icon
@ -205,7 +212,7 @@ export default definePlugin({
find: /\.recipients\.length>=2(?!<isMultiUserDM.{0,50})/,
replacement: {
match: /null==\i\.icon\?.+?src:(\(0,\i\.\i\).+?\))(?=[,}])/,
replace: (m, iconUrl) => `${m},onClick:()=>$self.openImage(${iconUrl})`
replace: (m, iconUrl) => `${m},onClick:()=>$self.openAvatar(${iconUrl})`
}
},
// User DMs top small icon
@ -213,7 +220,7 @@ export default definePlugin({
find: ".cursorPointer:null,children",
replacement: {
match: /.Avatar,.+?src:(.+?\))(?=[,}])/,
replace: (m, avatarUrl) => `${m},onClick:()=>$self.openImage(${avatarUrl})`
replace: (m, avatarUrl) => `${m},onClick:()=>$self.openAvatar(${avatarUrl})`
}
},
// User Dms top large icon
@ -221,7 +228,7 @@ export default definePlugin({
find: 'experimentLocation:"empty_messages"',
replacement: {
match: /.Avatar,.+?src:(.+?\))(?=[,}])/,
replace: (m, avatarUrl) => `${m},onClick:()=>$self.openImage(${avatarUrl})`
replace: (m, avatarUrl) => `${m},onClick:()=>$self.openAvatar(${avatarUrl})`
}
}
]