fix: add context menu to individual attachments if > 1 attached (#348)

This commit is contained in:
3nt3 2021-10-31 22:25:33 +01:00 committed by GitHub
parent aa13e9d96f
commit b09ccd90ba
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 71 additions and 19 deletions

View file

@ -5,6 +5,8 @@ import { attachContextMenu } from "preact-context-menu";
import { memo } from "preact/compat"; import { memo } from "preact/compat";
import { useState } from "preact/hooks"; import { useState } from "preact/hooks";
import { internalEmit } from "../../../lib/eventEmitter";
import { QueuedMessage } from "../../../redux/reducers/queue"; import { QueuedMessage } from "../../../redux/reducers/queue";
import { useIntermediate } from "../../../context/intermediate/Intermediate"; import { useIntermediate } from "../../../context/intermediate/Intermediate";
@ -25,7 +27,6 @@ import Attachment from "./attachments/Attachment";
import { MessageReply } from "./attachments/MessageReply"; import { MessageReply } from "./attachments/MessageReply";
import Embed from "./embed/Embed"; import Embed from "./embed/Embed";
import InviteList from "./embed/EmbedInvite"; import InviteList from "./embed/EmbedInvite";
import { internalEmit } from "../../../lib/eventEmitter";
interface Props { interface Props {
attachContext?: boolean; attachContext?: boolean;
@ -62,10 +63,10 @@ const Message = observer(
// bree: Fatal please... // bree: Fatal please...
const userContext = attachContext const userContext = attachContext
? (attachContextMenu("Menu", { ? (attachContextMenu("Menu", {
user: message.author_id, user: message.author_id,
contextualChannel: message.channel_id, contextualChannel: message.channel_id,
// eslint-disable-next-line // eslint-disable-next-line
}) as any) }) as any)
: undefined; : undefined;
const openProfile = () => const openProfile = () =>
@ -80,9 +81,9 @@ const Message = observer(
"mention", "mention",
); );
} else { } else {
openProfile() openProfile();
} }
} };
// ! FIXME(?): animate on hover // ! FIXME(?): animate on hover
const [animate, setAnimate] = useState(false); const [animate, setAnimate] = useState(false);
@ -105,11 +106,11 @@ const Message = observer(
hideReply hideReply
? false ? false
: (head && : (head &&
!( !(
message.reply_ids && message.reply_ids &&
message.reply_ids.length > 0 message.reply_ids.length > 0
)) ?? )) ??
false false
} }
contrast={contrast} contrast={contrast}
sending={typeof queued !== "undefined"} sending={typeof queued !== "undefined"}
@ -118,10 +119,10 @@ const Message = observer(
onContextMenu={ onContextMenu={
attachContext attachContext
? attachContextMenu("Menu", { ? attachContextMenu("Menu", {
message, message,
contextualChannel: message.channel_id, contextualChannel: message.channel_id,
queued, queued,
}) })
: undefined : undefined
} }
onMouseEnter={() => setAnimate(true)} onMouseEnter={() => setAnimate(true)}

View file

@ -2,6 +2,7 @@ import { Attachment as AttachmentI } from "revolt-api/types/Autumn";
import styles from "./Attachment.module.scss"; import styles from "./Attachment.module.scss";
import classNames from "classnames"; import classNames from "classnames";
import { attachContextMenu } from "preact-context-menu";
import { useContext, useState } from "preact/hooks"; import { useContext, useState } from "preact/hooks";
import { useIntermediate } from "../../../../context/intermediate/Intermediate"; import { useIntermediate } from "../../../../context/intermediate/Intermediate";
@ -9,9 +10,9 @@ import { AppContext } from "../../../../context/revoltjs/RevoltClient";
import AttachmentActions from "./AttachmentActions"; import AttachmentActions from "./AttachmentActions";
import { SizedGrid } from "./Grid"; import { SizedGrid } from "./Grid";
import ImageFile from "./ImageFile";
import Spoiler from "./Spoiler"; import Spoiler from "./Spoiler";
import TextFile from "./TextFile"; import TextFile from "./TextFile";
import ImageFile from "./ImageFile";
interface Props { interface Props {
attachment: AttachmentI; attachment: AttachmentI;
@ -37,11 +38,18 @@ export default function Attachment({ attachment, hasContent }: Props) {
<SizedGrid <SizedGrid
width={metadata.width} width={metadata.width}
height={metadata.height} height={metadata.height}
onContextMenu={attachContextMenu("Menu", {
attachment: attachment,
})}
className={classNames({ className={classNames({
[styles.margin]: hasContent, [styles.margin]: hasContent,
spoiler, spoiler,
})}> })}>
<ImageFile attachment={attachment} width={metadata.width} height={metadata.height} /> <ImageFile
attachment={attachment}
width={metadata.width}
height={metadata.height}
/>
{spoiler && <Spoiler set={setSpoiler} />} {spoiler && <Spoiler set={setSpoiler} />}
</SizedGrid> </SizedGrid>
); );

View file

@ -63,6 +63,7 @@ interface ContextMenuData {
server_list?: string; server_list?: string;
channel?: string; channel?: string;
message?: Message; message?: Message;
attachment?: Attachment;
unread?: boolean; unread?: boolean;
queued?: QueuedMessage; queued?: QueuedMessage;
@ -468,6 +469,7 @@ function ContextMenus(props: Props) {
channel: cid, channel: cid,
server: sid, server: sid,
message, message,
attachment,
server_list, server_list,
queued, queued,
unread, unread,
@ -747,7 +749,10 @@ function ContextMenus(props: Props) {
}); });
} }
if (message.attachments) { if (
message.attachments &&
message.attachments.length == 1 // if there are multiple attachments, the individual ones have to be clicked
) {
pushDivider(); pushDivider();
const { metadata } = message.attachments[0]; const { metadata } = message.attachments[0];
const { type } = metadata; const { type } = metadata;
@ -796,6 +801,44 @@ function ContextMenus(props: Props) {
} }
} }
if (attachment) {
pushDivider();
const { metadata } = attachment;
const { type } = metadata;
generateAction(
{
action: "open_file",
attachment,
},
type === "Image"
? "open_image"
: type === "Video"
? "open_video"
: "open_file",
);
generateAction(
{
action: "save_file",
attachment,
},
type === "Image"
? "save_image"
: type === "Video"
? "save_video"
: "save_file",
);
generateAction(
{
action: "copy_file_link",
attachment,
},
"copy_link",
);
}
const id = sid ?? cid ?? uid ?? message?._id; const id = sid ?? cid ?? uid ?? message?._id;
if (id) { if (id) {
pushDivider(); pushDivider();