From 02437a6a20d5e1cc139c3953a57c37c911f70612 Mon Sep 17 00:00:00 2001 From: nexy7574 Date: Mon, 6 Jan 2025 01:44:22 +0000 Subject: [PATCH] Render captions to m.file, m.image, m.video, and m.audio (#2059) * Add rendering image captions * Handle sending captions for images * Fix caption rendering on m.video and m.audio too * Remove unused renderBody() parameter * Fix m.file rendering body instead of filename where possible * Add caption rendering for generic files + Fix video and audio not properly sending captions * Use m.text for captions & render on demand * Allow custom HTML in sending captions * Don't *send* captions * mvoe content const into renderCaption() --------- Co-authored-by: Ajay Bura <32841439+ajbura@users.noreply.github.com> --- src/app/components/RenderMessageContent.tsx | 173 +++++++++++------- .../components/message/MsgTypeRenderers.tsx | 7 +- src/app/features/room/msgContent.ts | 7 +- src/types/matrix/common.ts | 4 + 4 files changed, 118 insertions(+), 73 deletions(-) diff --git a/src/app/components/RenderMessageContent.tsx b/src/app/components/RenderMessageContent.tsx index 1ce37e5c..55b0ffc2 100644 --- a/src/app/components/RenderMessageContent.tsx +++ b/src/app/components/RenderMessageContent.tsx @@ -29,6 +29,7 @@ import { ImageViewer } from './image-viewer'; import { PdfViewer } from './Pdf-viewer'; import { TextViewer } from './text-viewer'; import { testMatrixTo } from '../plugins/matrix-to'; +import {IImageContent} from "../../types/matrix/common"; type RenderMessageContentProps = { displayName: string; @@ -67,38 +68,63 @@ export function RenderMessageContent({ ); }; + const renderCaption = () => { + const content: IImageContent = getContent(); + if(content.filename && content.filename !== content.body) { + return ( + ( + + )} + renderUrlsPreview={urlPreview ? renderUrlsPreview : undefined} + /> + ) + } + return null; + } const renderFile = () => ( - ( - ( - + ( + } - /> - )} - renderAsTextFile={() => ( - } - /> - )} - > - - - )} - outlined={outlineAttachment} - /> + renderAsPdfFile={() => ( + } + /> + )} + renderAsTextFile={() => ( + } + /> + )} + > + + + + )} + outlined={outlineAttachment} + /> + {renderCaption()} + ); if (msgType === MsgType.Text) { @@ -158,36 +184,40 @@ export function RenderMessageContent({ if (msgType === MsgType.Image) { return ( - ( - } - renderViewer={(p) => } - /> - )} - outlined={outlineAttachment} - /> + <> + ( + } + renderViewer={(p) => } + /> + )} + outlined={outlineAttachment} + /> + {renderCaption()} + ); } if (msgType === MsgType.Video) { return ( - ( - ( + <> + ( + ( ( @@ -195,26 +225,33 @@ export function RenderMessageContent({ )} /> ) - : undefined - } - renderVideo={(p) =>