Responding to @errorinn PR Comments
continuous-integration/drone/pr Build is pending Details

This commit is contained in:
Sarah Jamie Lewis 2022-01-18 14:32:45 -08:00
parent b3f06d6765
commit cd1bf07fba
5 changed files with 41 additions and 68 deletions

View File

@ -321,5 +321,3 @@ class Settings extends ChangeNotifier {
};
}
}

View File

@ -1,6 +1,7 @@
// Code Originally taken from https://github.com/Cretezy/flutter_linkify/
//
// Now uses local `linkify`
//
// Code Originally taken from https://github.com/Cretezy/flutter_linkify/ and
// subsequently modified...
// Original License for this code:
// MIT License
// Copyright (c) 2020 Charles-William Crete
@ -23,14 +24,13 @@
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';
import 'linkify.dart';
export 'linkify.dart' show LinkifyElement, LinkifyOptions, LinkableElement, TextElement, Linkifier;
/// Callback clicked link
typedef LinkCallback = void Function(LinkableElement link);
@ -131,9 +131,9 @@ class Linkify extends StatelessWidget {
.bodyText2
?.merge(style)
.copyWith(
color: Colors.blueAccent,
decoration: TextDecoration.underline,
)
color: Colors.blueAccent,
decoration: TextDecoration.underline,
)
.merge(linkStyle),
),
textAlign: textAlign,
@ -295,9 +295,9 @@ class SelectableLinkify extends StatelessWidget {
.bodyText2
?.merge(style)
.copyWith(
color: Colors.blueAccent,
decoration: TextDecoration.underline,
)
color: Colors.blueAccent,
decoration: TextDecoration.underline,
)
.merge(linkStyle),
),
textAlign: textAlign,
@ -331,26 +331,26 @@ class LinkableSpan extends WidgetSpan {
required MouseCursor mouseCursor,
required InlineSpan inlineSpan,
}) : super(
child: MouseRegion(
cursor: mouseCursor,
child: Text.rich(
inlineSpan,
),
),
);
child: MouseRegion(
cursor: mouseCursor,
child: Text.rich(
inlineSpan,
),
),
);
}
/// Raw TextSpan builder for more control on the RichText
TextSpan buildTextSpan(
List<LinkifyElement> elements, {
TextStyle? style,
TextStyle? linkStyle,
LinkCallback? onOpen,
bool useMouseRegion = false,
}) {
List<LinkifyElement> elements, {
TextStyle? style,
TextStyle? linkStyle,
LinkCallback? onOpen,
bool useMouseRegion = false,
}) {
return TextSpan(
children: elements.map<InlineSpan>(
(element) {
(element) {
if (element is LinkableElement) {
if (useMouseRegion) {
return LinkableSpan(

View File

@ -1,4 +1,6 @@
// Originally from linkify
// Originally from linkify https://github.com/Cretezy/linkify/blob/master/lib/linkify.dart
// Removed options `removeWWW` and `humanize`
//
// MIT License
//
// Copyright (c) 2019 Charles-William Crete
@ -43,8 +45,7 @@ class LinkableElement extends LinkifyElement {
bool operator ==(other) => equals(other);
@override
bool equals(other) =>
other is LinkableElement && super.equals(other) && other.url == url;
bool equals(other) => other is LinkableElement && super.equals(other) && other.url == url;
}
/// Represents an element containing text
@ -66,17 +67,10 @@ class TextElement extends LinkifyElement {
abstract class Linkifier {
const Linkifier();
List<LinkifyElement> parse(
List<LinkifyElement> elements, LinkifyOptions options);
List<LinkifyElement> parse(List<LinkifyElement> elements, LinkifyOptions options);
}
class LinkifyOptions {
/// Removes http/https from shown URLs.
final bool humanize;
/// Removes www. from shown URLs.
final bool removeWww;
/// Enables loose URL parsing (any string with "." is a URL).
final bool looseUrl;
@ -87,8 +81,6 @@ class LinkifyOptions {
final bool excludeLastPeriod;
const LinkifyOptions({
this.humanize = true,
this.removeWww = false,
this.looseUrl = false,
this.defaultToHttps = false,
this.excludeLastPeriod = true,
@ -106,10 +98,10 @@ const defaultLinkifiers = [_urlLinkifier];
/// Uses [linkTypes] to enable some types of links (URL, email).
/// Will default to all (if `null`).
List<LinkifyElement> linkify(
String text, {
LinkifyOptions options = const LinkifyOptions(),
List<Linkifier> linkifiers = defaultLinkifiers,
}) {
String text, {
LinkifyOptions options = const LinkifyOptions(),
List<Linkifier> linkifiers = defaultLinkifiers,
}) {
var list = <LinkifyElement>[TextElement(text)];
if (text.isEmpty) {

View File

@ -1,4 +1,8 @@
// Originally from linkify
// Originally from linkify: https://github.com/Cretezy/linkify/blob/master/lib/src/url.dart
//
// Removed handling of `removeWWW` and `humanize`.
// Removed auto-appending of `http(s)://` to the readable url
//
// MIT License
//
// Copyright (c) 2019 Charles-William Crete
@ -49,9 +53,7 @@ class UrlLinkifier extends Linkifier {
elements.forEach((element) {
if (element is TextElement) {
var match = options.looseUrl
? _looseUrlRegex.firstMatch(element.text)
: _urlRegex.firstMatch(element.text);
var match = options.looseUrl ? _looseUrlRegex.firstMatch(element.text) : _urlRegex.firstMatch(element.text);
if (match == null) {
list.add(element);
@ -66,32 +68,15 @@ class UrlLinkifier extends Linkifier {
var originalUrl = match.group(2)!;
String? end;
if ((options.excludeLastPeriod) &&
originalUrl[originalUrl.length - 1] == ".") {
if ((options.excludeLastPeriod) && originalUrl[originalUrl.length - 1] == ".") {
end = ".";
originalUrl = originalUrl.substring(0, originalUrl.length - 1);
}
var url = originalUrl;
// We do not, ever, change the original text of a message.
if (options.defaultToHttps) {
url = url.replaceFirst('http://', 'https://');
}
// These options are intended for the human-readable portion of
// the URI
if (options.humanize) {
originalUrl = originalUrl.replaceFirst(RegExp(r'https?://'), '');
}
if (options.removeWww) {
originalUrl = originalUrl.replaceFirst(RegExp(r'www\.'), '');
}
list.add(UrlElement(originalUrl, url));
if (end != null) {
list.add(TextElement(end));
}

View File

@ -2,8 +2,6 @@ import 'dart:io';
import 'package:cwtch/models/message.dart';
import 'package:cwtch/third_party/linkify/flutter_linkify.dart';
import 'package:cwtch/third_party/linkify/linkify.dart';
import 'package:cwtch/third_party/linkify/uri.dart';
import 'package:cwtch/widgets/malformedbubble.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
@ -69,7 +67,7 @@ class MessageBubbleState extends State<MessageBubble> {
wdgMessage = SelectableLinkify(
text: widget.content + '\u202F',
// TODO: onOpen breaks the "selectable" functionality. Maybe something to do with gesture handler?
options: LinkifyOptions(humanize: false, removeWww: false, looseUrl: true, defaultToHttps: true),
options: LinkifyOptions(looseUrl: true, defaultToHttps: true),
linkifiers: [UrlLinkifier()],
onOpen: (link) {
_modalOpenLink(context, link);