Skip to content
This repository was archived by the owner on Oct 22, 2024. It is now read-only.

Commit c5ca667

Browse files
committed
Add support for rendering media captions
1 parent 6b384fe commit c5ca667

File tree

3 files changed

+48
-1
lines changed

3 files changed

+48
-1
lines changed

src/components/views/messages/MessageEvent.tsx

+36
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,32 @@ export default class MessageEvent extends React.Component<IProps> implements IMe
190190
}
191191
}
192192

193+
const hasCaption =
194+
[MsgType.Image, MsgType.File, MsgType.Audio, MsgType.Video].includes(msgtype as MsgType) &&
195+
content.filename &&
196+
content.filename !== content.body;
197+
if (hasCaption) {
198+
return <CaptionBody
199+
ref={this.body}
200+
mxEvent={this.props.mxEvent}
201+
highlights={this.props.highlights}
202+
highlightLink={this.props.highlightLink}
203+
showUrlPreview={this.props.showUrlPreview}
204+
forExport={this.props.forExport}
205+
maxImageHeight={this.props.maxImageHeight}
206+
replacingEventId={this.props.replacingEventId}
207+
editState={this.props.editState}
208+
onHeightChanged={this.props.onHeightChanged}
209+
onMessageAllowed={this.onTileUpdate}
210+
permalinkCreator={this.props.permalinkCreator}
211+
mediaEventHelper={this.mediaHelper}
212+
getRelationsForEvent={this.props.getRelationsForEvent}
213+
isSeeingThroughMessageHiddenForModeration={this.props.isSeeingThroughMessageHiddenForModeration}
214+
inhibitInteraction={this.props.inhibitInteraction}
215+
WrappedBodyType={BodyType}
216+
/>;
217+
}
218+
193219
return BodyType ? (
194220
<BodyType
195221
ref={this.body}
@@ -212,3 +238,13 @@ export default class MessageEvent extends React.Component<IProps> implements IMe
212238
) : null;
213239
}
214240
}
241+
242+
const CaptionBody: React.FunctionComponent<IBodyProps & { WrappedBodyType: React.ComponentType<IBodyProps> }> = ({
243+
WrappedBodyType,
244+
...props
245+
}) => (
246+
<div className="mx_EventTile_content">
247+
<WrappedBodyType {...props} />
248+
<TextualBody {...{ ...props, ref: undefined }} />
249+
</div>
250+
);

src/components/views/messages/TextualBody.tsx

+9
Original file line numberDiff line numberDiff line change
@@ -556,6 +556,7 @@ export default class TextualBody extends React.Component<IBodyProps, IState> {
556556
const content = mxEvent.getContent();
557557
const isNotice = content.msgtype === MsgType.Notice;
558558
const isEmote = content.msgtype === MsgType.Emote;
559+
const isCaption = [MsgType.Image, MsgType.File, MsgType.Audio, MsgType.Video].includes(content.msgtype as MsgType);
559560

560561
const willHaveWrapper =
561562
this.props.replacingEventId || this.props.isSeeingThroughMessageHiddenForModeration || isEmote;
@@ -635,6 +636,14 @@ export default class TextualBody extends React.Component<IBodyProps, IState> {
635636
</div>
636637
);
637638
}
639+
if (isCaption) {
640+
return (
641+
<div className="mx_MTextBody mx_EventTile_caption" onClick={this.onBodyLinkClick}>
642+
{body}
643+
{widgets}
644+
</div>
645+
);
646+
}
638647
return (
639648
<div className="mx_MTextBody mx_EventTile_content" onClick={this.onBodyLinkClick}>
640649
{body}

src/utils/FileUtils.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,9 @@ export function presentableTextForFile(
3838
shortened = false,
3939
): string {
4040
let text = fallbackText;
41-
if (content.body?.length) {
41+
if (content.filename?.length) {
42+
text = content.filename;
43+
} else if (content.body?.length) {
4244
// The content body should be the name of the file including a
4345
// file extension.
4446
text = content.body;

0 commit comments

Comments
 (0)