allow copying messages when on desktop without mobile mode on

This commit is contained in:
Henry Hiles 2026-09-22 19:13:32 -04:00
commit 027033ad9e
Signed by: Henry-Hiles
SSH key fingerprint: SHA256:VKQUdS31Q90KvX7EkKMHMBpUspcmItAh86a+v7PGiIs

View file

@ -1,7 +1,10 @@
import "dart:io";
import "package:fast_immutable_collections/fast_immutable_collections.dart"; import "package:fast_immutable_collections/fast_immutable_collections.dart";
import "package:material_ui/material_ui.dart"; import "package:material_ui/material_ui.dart";
import "package:flutter_riverpod/flutter_riverpod.dart"; import "package:flutter_riverpod/flutter_riverpod.dart";
import "package:flutter_widget_from_html_core/flutter_widget_from_html_core.dart"; import "package:flutter_widget_from_html_core/flutter_widget_from_html_core.dart";
import "package:nexus/controllers/settings.dart";
import "package:nexus/helpers/extensions/link_to_mention.dart"; import "package:nexus/helpers/extensions/link_to_mention.dart";
import "package:nexus/helpers/launch_helper.dart"; import "package:nexus/helpers/launch_helper.dart";
import "package:nexus/helpers/mxc_image.dart"; import "package:nexus/helpers/mxc_image.dart";
@ -18,132 +21,152 @@ class const Html(
super.key, super.key,
}) extends ConsumerWidget { }) extends ConsumerWidget {
@override @override
Widget build(BuildContext context, WidgetRef ref) => Widget build(BuildContext context, WidgetRef ref) {
// needed until https://github.com/daohoangson/flutter_widget_from_html/issues/1618 is resolved // needed until https://github.com/daohoangson/flutter_widget_from_html/issues/1618 is resolved
MaterialUiCompatibilityBridge( final htmlWidget = MaterialUiCompatibilityBridge(
child: HtmlWidget( child: HtmlWidget(
html, html,
buildAsync: false, buildAsync: false,
textStyle: textStyle, textStyle: textStyle,
customWidgetBuilder: (element) { customWidgetBuilder: (element) {
if (element.attributes.keys.contains("data-mx-profile-fallback")) { if (element.attributes.keys.contains("data-mx-profile-fallback")) {
return SizedBox.shrink(); return SizedBox.shrink();
} }
if (element.attributes.keys.contains("data-mx-spoiler")) { if (element.attributes.keys.contains("data-mx-spoiler")) {
return InlineCustomWidget(child: SpoilerText(element.text)); return InlineCustomWidget(child: SpoilerText(element.text));
} }
final height = final height =
int.tryParse(element.attributes["height"] ?? "") ?? int.tryParse(element.attributes["height"] ?? "") ??
(element.attributes.keys.contains("data-mx-emoticon") (element.attributes.keys.contains("data-mx-emoticon")
? 32 ? 32
: null) ?? : null) ??
300; 300;
final width = int.tryParse(element.attributes["width"] ?? ""); final width = int.tryParse(element.attributes["width"] ?? "");
final src = Uri.tryParse(element.attributes["src"] ?? ""); final src = Uri.tryParse(element.attributes["src"] ?? "");
return switch (element.localName) { return switch (element.localName) {
"code" => "code" =>
element.parent?.localName == "pre" element.parent?.localName == "pre"
? CodeBlock( ? CodeBlock(
element.text, element.text,
lang: element.className.replaceAll("language-", ""), lang: element.className.replaceAll("language-", ""),
) )
: null, : null,
"blockquote" => Quoted( "blockquote" => Quoted(
Html(element.innerHtml, textStyle: textStyle, roomId: roomId), Html(element.innerHtml, textStyle: textStyle, roomId: roomId),
), ),
"a" => "a" =>
element.attributes["href"]?.mention == null element.attributes["href"]?.mention == null
? null ? null
: InlineCustomWidget( : InlineCustomWidget(
child: MentionChip(element.attributes["href"]!, roomId), child: MentionChip(element.attributes["href"]!, roomId),
), ),
"img" => "img" =>
src == null src == null
? SizedBox.shrink() ? SizedBox.shrink()
: InlineCustomWidget( : InlineCustomWidget(
alignment: PlaceholderAlignment.middle, alignment: PlaceholderAlignment.middle,
child: ExpandableImage( child: ExpandableImage(
.new(mxc: src), .new(mxc: src),
child: Image( child: Image(
image: MxcImage(ref, .new(mxc: src)), image: MxcImage(ref, .new(mxc: src)),
errorBuilder: (_, error, _) => Text( errorBuilder: (_, error, _) => Text(
"Image Failed to Load", "Image Failed to Load",
style: .new( style: .new(
color: Theme.of(context).colorScheme.error, color: Theme.of(context).colorScheme.error,
),
), ),
height: height.toDouble(),
width: width?.toDouble(),
loadingBuilder: (_, child, loadingProgress) =>
loadingProgress == null
? child
: CircularProgressIndicator(),
), ),
height: height.toDouble(),
width: width?.toDouble(),
loadingBuilder: (_, child, loadingProgress) =>
loadingProgress == null
? child
: CircularProgressIndicator(),
), ),
), ),
),
// Allowed elements list // Allowed elements list
("del" || ("del" ||
"h1" || "h1" ||
"h2" || "h2" ||
"h3" || "h3" ||
"h4" || "h4" ||
"h5" || "h5" ||
"h6" || "h6" ||
"p" || "p" ||
"ul" || "ul" ||
"ol" || "ol" ||
"sup" || "sup" ||
"sub" || "sub" ||
"li" || "li" ||
"b" || "b" ||
"i" || "i" ||
"u" || "u" ||
"strong" || "strong" ||
"em" || "em" ||
"s" || "s" ||
"code" || "code" ||
"hr" || "hr" ||
"br" || "br" ||
"div" || "div" ||
"table" || "table" ||
"thead" || "thead" ||
"tbody" || "tbody" ||
"tr" || "tr" ||
"th" || "th" ||
"td" || "td" ||
"caption" || "caption" ||
"pre" || "pre" ||
"span" || "span" ||
"details" || "details" ||
"summary") => "summary") =>
null, null,
_ => SizedBox.shrink(), _ => SizedBox.shrink(),
}; };
}, },
customStylesBuilder: (element) => { customStylesBuilder: (element) => {
"width": "auto", "width": "auto",
...Map.fromEntries( ...Map.fromEntries(
element.attributes element.attributes
.mapTo<MapEntry<String, String>?>( .mapTo<MapEntry<String, String>?>(
(key, value) => switch (key) { (key, value) => switch (key) {
"data-mx-color" => .new("color", value), "data-mx-color" => .new("color", value),
"data-mx-bg-color" => .new("background-color", value), "data-mx-bg-color" => .new("background-color", value),
_ => null, _ => null,
}, },
) )
.nonNulls, .nonNulls,
), ),
}, },
onTapUrl: (url) => onTapUrl: (url) =>
ref.watch(LaunchHelper.provider).launchUrl(.parse(url)), ref.watch(LaunchHelper.provider).launchUrl(.parse(url)),
), ),
); );
final shouldBeSelectable =
ref
.watch(SettingsController.provider)
.when(
data: (data) => !data.linuxMobileMode,
error: (_, _) => false,
loading: () => false,
) ||
Platform.isIOS ||
Platform.isAndroid;
return shouldBeSelectable
? SelectableRegion(
contextMenuBuilder: (context, selectableRegionState) =>
Text("data"),
selectionControls: materialTextSelectionControls,
child: htmlWidget,
)
: htmlWidget;
}
} }