Skip to content

Commit

Permalink
Add rendering image captions
Browse files Browse the repository at this point in the history
  • Loading branch information
nexy7574 committed Nov 17, 2024
1 parent a142630 commit dccf87d
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 13 deletions.
54 changes: 42 additions & 12 deletions src/app/components/RenderMessageContent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -157,19 +158,48 @@ export function RenderMessageContent({
}

if (msgType === MsgType.Image) {
const content: IImageContent = getContent();
const renderCaption = content.filename && content.filename !== content.body;
return (
<MImage
content={getContent()}
renderImageContent={(props) => (
<ImageContent
{...props}
autoPlay={mediaAutoLoad}
renderImage={(p) => <Image {...p} loading="lazy" />}
renderViewer={(p) => <ImageViewer {...p} />}
/>
)}
outlined={outlineAttachment}
/>
<>
<MImage
content={getContent()}
renderBody={(props) => (
<RenderBody
{...props}
highlightRegex={highlightRegex}
htmlReactParserOptions={htmlReactParserOptions}
linkifyOpts={linkifyOpts}
/>
)}
renderImageContent={(props) => (
<ImageContent
{...props}
autoPlay={mediaAutoLoad}
renderImage={(p) => <Image {...p} loading="lazy" />}
renderViewer={(p) => <ImageViewer {...p} />}
/>
)}
outlined={outlineAttachment}
/>
{
renderCaption && (
<MNotice
edited={edited}
content={getContent()}
renderBody={(props) => (
<RenderBody
{...props}
highlightRegex={highlightRegex}
htmlReactParserOptions={htmlReactParserOptions}
linkifyOpts={linkifyOpts}
/>
)}
renderUrlsPreview={urlPreview ? renderUrlsPreview : undefined}
/>
)
}
</>
);
}

Expand Down
4 changes: 3 additions & 1 deletion src/app/components/message/MsgTypeRenderers.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -172,17 +172,19 @@ export function MNotice({ edited, content, renderBody, renderUrlsPreview }: MNot

type RenderImageContentProps = {
body: string;
filename?: string;
info?: IImageInfo & IThumbnailContent;
mimeType?: string;
url: string;
encInfo?: IEncryptedFile;
};
type MImageProps = {
content: IImageContent;
renderBody: (props: RenderBodyProps) => ReactNode;
renderImageContent: (props: RenderImageContentProps) => ReactNode;
outlined?: boolean;
};
export function MImage({ content, renderImageContent, outlined }: MImageProps) {
export function MImage({ content, renderBody, renderImageContent, outlined }: MImageProps) {
const imgInfo = content?.info;
const mxcUrl = content.file?.url ?? content.url;
if (typeof mxcUrl !== 'string') {
Expand Down
1 change: 1 addition & 0 deletions src/types/matrix/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ export type IThumbnailContent = {
export type IImageContent = {
msgtype: MsgType.Image;
body?: string;
filename?: string;
url?: string;
info?: IImageInfo & IThumbnailContent;
file?: IEncryptedFile;
Expand Down

0 comments on commit dccf87d

Please sign in to comment.