fix: Introduce a TextMessage widget that doesn't render HTML

Messages that don't have the format set to `org.matrix.custom.html` shouldn't be interpreted as HTML. This achieves that by adding a simpler TextMessage widget that renders messages as plain text.

TextMessage was created by copying relevant parts of HtmlMessage, so there's definitely potential for deduplicating.
pull/2015/head
Rafał Hirsch 3 months ago
parent 90f7cca0a5
commit 155f8f6328
No known key found for this signature in database

@ -22,6 +22,7 @@ import 'html_message.dart';
import 'image_bubble.dart';
import 'map_bubble.dart';
import 'message_download_content.dart';
import 'text_message.dart';
class MessageContent extends StatelessWidget {
final Event event;
@ -255,43 +256,56 @@ class MessageContent extends StatelessWidget {
},
);
}
var html = AppConfig.renderHtml && event.isRichMessage
var messageContent = AppConfig.renderHtml && event.isRichMessage
? event.formattedText
: event.body;
if (event.messageType == MessageTypes.Emote) {
html = '* $html';
messageContent = '* $messageContent';
}
final bigEmotes = event.onlyEmotes &&
event.numberEmotes > 0 &&
event.numberEmotes <= 3;
final textFontSize = AppConfig.fontSizeFactor *
AppConfig.messageFontSize *
(bigEmotes ? 5 : 1);
final linkStyle = TextStyle(
color: linkColor,
fontSize: AppConfig.fontSizeFactor * AppConfig.messageFontSize,
decoration: TextDecoration.underline,
decorationColor: linkColor,
);
return Padding(
padding: const EdgeInsets.symmetric(
horizontal: 16,
vertical: 8,
),
child: HtmlMessage(
html: html,
textColor: textColor,
room: event.room,
fontSize: AppConfig.fontSizeFactor *
AppConfig.messageFontSize *
(bigEmotes ? 5 : 1),
limitHeight: !selected,
linkStyle: TextStyle(
color: linkColor,
fontSize:
AppConfig.fontSizeFactor * AppConfig.messageFontSize,
decoration: TextDecoration.underline,
decorationColor: linkColor,
),
onOpen: (url) => UrlLauncher(context, url.url).launchUrl(),
eventId: event.eventId,
checkboxCheckedEvents: event.aggregatedEvents(
timeline,
EventCheckboxRoomExtension.relationshipType,
),
),
child: AppConfig.renderHtml && event.isRichMessage
? HtmlMessage(
html: messageContent,
textColor: textColor,
room: event.room,
fontSize: textFontSize,
limitHeight: !selected,
linkStyle: linkStyle,
onOpen: (url) =>
UrlLauncher(context, url.url).launchUrl(),
eventId: event.eventId,
checkboxCheckedEvents: event.aggregatedEvents(
timeline,
EventCheckboxRoomExtension.relationshipType,
),
)
: TextMessage(
text: messageContent,
fontSize: textFontSize,
textColor: textColor,
limitHeight: !selected,
linkStyle: linkStyle,
onOpen: (url) =>
UrlLauncher(context, url.url).launchUrl(),
),
);
}
case EventTypes.CallInvite:

@ -0,0 +1,40 @@
import 'package:flutter/material.dart';
import 'package:flutter_linkify/flutter_linkify.dart';
class TextMessage extends StatelessWidget {
final String text;
final Color textColor;
final double fontSize;
final TextStyle linkStyle;
final void Function(LinkableElement) onOpen;
final bool limitHeight;
const TextMessage({
super.key,
required this.text,
required this.fontSize,
required this.linkStyle,
required this.onOpen,
this.textColor = Colors.black,
this.limitHeight = true,
});
@override
Widget build(BuildContext context) {
return Text.rich(
LinkifySpan(
text: text,
options: const LinkifyOptions(humanize: false),
linkStyle: linkStyle,
onOpen: onOpen,
),
style: TextStyle(
fontSize: fontSize,
color: textColor,
),
maxLines: limitHeight ? 64 : null,
overflow: TextOverflow.fade,
);
}
}
Loading…
Cancel
Save