Chat: highlighted, clickable links in messages
Markdown links, autolinks and bare http(s)/www. URLs open externally through url_launcher (http, https and mailto only) in assistant markdown and operator messages; code is never linkified. Links use the host primary colour with an underline that thickens on hover, and offer Copy link on long press and right-click without breaking text selection.
This commit is contained in:
@@ -1,18 +1,21 @@
|
||||
import 'dart:async';
|
||||
|
||||
import 'package:flutter/cupertino.dart';
|
||||
import 'package:flutter/gestures.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter/semantics.dart';
|
||||
|
||||
import '../../../../ui/adaptive/host.dart';
|
||||
import '../../../../ui/layout/wheel_scroll_forwarding.dart';
|
||||
import '../sessions/chat_adaptive.dart';
|
||||
import 'chat_link_launcher.dart';
|
||||
import 'chat_link_spans.dart';
|
||||
import 'chat_markdown.dart';
|
||||
import 'chat_markdown_cache.dart';
|
||||
import 'chat_message_bubble.dart';
|
||||
|
||||
/// Renders one assistant text part with native text widgets and the host's own
|
||||
/// theme roles. Link activation is the caller's job: [onOpenLink] receives only
|
||||
/// an `http`/`https` target. No font family, size, colour or component
|
||||
/// theme roles. Links (`http`, `https`, `mailto` and `www.` hosts only) open
|
||||
/// through [onOpenLink] when given, else the [ChatLinkScope] launcher, which by
|
||||
/// default opens them outside the app. No font family, size, colour or component
|
||||
/// dimension is overridden; code keeps its exact whitespace and scrolls
|
||||
/// sideways instead of widening the transcript.
|
||||
class ChatMarkdownView extends StatefulWidget {
|
||||
@@ -34,14 +37,15 @@ Widget chatMarkdownText(
|
||||
}) => ChatMarkdownView(text: text, onOpenLink: onOpenLink);
|
||||
|
||||
class _ChatMarkdownViewState extends State<ChatMarkdownView> {
|
||||
final Map<String, TapGestureRecognizer> _links = {};
|
||||
late final ChatLinkSpans _links = ChatLinkSpans(
|
||||
onChanged: () => setState(() => _rendered = null),
|
||||
);
|
||||
Widget? _rendered;
|
||||
|
||||
@override
|
||||
void didUpdateWidget(ChatMarkdownView oldWidget) {
|
||||
super.didUpdateWidget(oldWidget);
|
||||
if (widget.text != oldWidget.text ||
|
||||
(widget.onOpenLink == null) != (oldWidget.onOpenLink == null)) {
|
||||
if (widget.text != oldWidget.text) {
|
||||
_rendered = null;
|
||||
}
|
||||
}
|
||||
@@ -56,9 +60,7 @@ class _ChatMarkdownViewState extends State<ChatMarkdownView> {
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
for (final recognizer in _links.values) {
|
||||
recognizer.dispose();
|
||||
}
|
||||
_links.dispose();
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
@@ -71,7 +73,7 @@ class _ChatMarkdownViewState extends State<ChatMarkdownView> {
|
||||
chatControlsStyleOf(context) == ChatControlsStyle.cupertino;
|
||||
final gap = chatParagraphGap(context);
|
||||
final blocks = chatMarkdownCache.blocksFor(widget.text);
|
||||
return _rendered = _selectable(
|
||||
return _rendered = _links.selectionArea(
|
||||
context,
|
||||
Column(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
@@ -94,23 +96,6 @@ class _ChatMarkdownViewState extends State<ChatMarkdownView> {
|
||||
double _gapAfter(MarkdownBlock previous, double paragraphGap) =>
|
||||
previous is MarkdownHeading ? 8 : paragraphGap;
|
||||
|
||||
/// Selection is provided by the native [SelectionArea]. A Cupertino-only app
|
||||
/// has no `MaterialLocalizations`, which `SelectionArea` asserts on, so one
|
||||
/// English default localization scope is added there for its context menu.
|
||||
Widget _selectable(BuildContext context, Widget child) {
|
||||
final area = SelectionArea(child: child);
|
||||
if (!usesCupertinoHost(context)) return area;
|
||||
return Localizations(
|
||||
locale: Localizations.maybeLocaleOf(context) ?? const Locale('en'),
|
||||
delegates: const [
|
||||
DefaultWidgetsLocalizations.delegate,
|
||||
DefaultMaterialLocalizations.delegate,
|
||||
DefaultCupertinoLocalizations.delegate,
|
||||
],
|
||||
child: area,
|
||||
);
|
||||
}
|
||||
|
||||
Widget _block(BuildContext context, MarkdownBlock block, bool cupertino) {
|
||||
switch (block) {
|
||||
case MarkdownHeading(level: final level, text: final text):
|
||||
@@ -303,38 +288,16 @@ class _ChatMarkdownViewState extends State<ChatMarkdownView> {
|
||||
String url,
|
||||
List<MarkdownInline> spans,
|
||||
bool cupertino,
|
||||
) {
|
||||
final children = _spans(context, spans, cupertino);
|
||||
final onOpenLink = widget.onOpenLink;
|
||||
if (!isSafeMarkdownLink(url) || onOpenLink == null) {
|
||||
return TextSpan(children: children);
|
||||
}
|
||||
final recognizer = _links.putIfAbsent(url, TapGestureRecognizer.new);
|
||||
recognizer.onTap = () => widget.onOpenLink?.call(url);
|
||||
return _withRecognizer(
|
||||
TextSpan(
|
||||
style: const TextStyle(decoration: TextDecoration.underline),
|
||||
children: children,
|
||||
),
|
||||
recognizer,
|
||||
);
|
||||
}
|
||||
|
||||
/// A `TextSpan` only dispatches a recognizer when it also owns text, not when
|
||||
/// it only owns children, so a link's recognizer is copied onto its text
|
||||
/// leaves. The underline stays on the shared parent.
|
||||
InlineSpan _withRecognizer(InlineSpan span, TapGestureRecognizer recognizer) {
|
||||
if (span is! TextSpan) return span;
|
||||
final children = span.children;
|
||||
return TextSpan(
|
||||
text: span.text,
|
||||
style: span.style,
|
||||
recognizer: span.text == null ? span.recognizer : recognizer,
|
||||
children: children == null
|
||||
? null
|
||||
: [for (final child in children) _withRecognizer(child, recognizer)],
|
||||
);
|
||||
}
|
||||
) => _links.link(
|
||||
context,
|
||||
url,
|
||||
_spans(context, spans, cupertino),
|
||||
onOpen: (uri) {
|
||||
final onOpenLink = widget.onOpenLink;
|
||||
if (onOpenLink != null) return onOpenLink(uri.toString());
|
||||
unawaited(openChatLink(context, uri));
|
||||
},
|
||||
);
|
||||
|
||||
/// Heading roles are a non-increasing sequence from the host's own text
|
||||
/// theme. H3–H6 share one title role, matching the web renderer's flat
|
||||
|
||||
Reference in New Issue
Block a user